Skip to content

Commit

Permalink
Fix font rendering for JP encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
Xeeynamo committed Jan 13, 2020
1 parent fc52f30 commit 9da04c1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
38 changes: 33 additions & 5 deletions OpenKh.Tools.Common/Controls/KingdomTextArea.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ public void NewLine(int fontHeight)

private const int IconWidth = Constants.FontIconWidth;
private const int IconHeight = Constants.FontIconHeight;

private const int CharactersPerTextureBlock = 392;
private const int CharactersPerTexture = 784;
public static DependencyProperty ContextProperty =
DependencyPropertyUtils.GetDependencyProperty<KingdomTextArea, KingdomTextContext>(
nameof(Context), (o, x) => o.SetContext(x));
Expand All @@ -62,8 +63,10 @@ public void NewLine(int fontHeight)
private byte[] _fontSpacing;
private byte[] _iconSpacing;
private IImageRead _imageFont;
private IImageRead _imageFont2;
private IImageRead _imageIcon;
private ISurface _surfaceFont;
private ISurface _surfaceFont2;
private ISurface _surfaceIcon;
private int _charPerRow;
private int _iconPerRow;
Expand Down Expand Up @@ -103,6 +106,7 @@ protected override void OnDrawCreate()
{
base.OnDrawCreate();
GetOrInitializeSurface(ref _surfaceFont, _imageFont);
GetOrInitializeSurface(ref _surfaceFont2, _imageFont2);
GetOrInitializeSurface(ref _surfaceIcon, _imageIcon);
}

Expand Down Expand Up @@ -255,11 +259,31 @@ private void DrawIcon(DrawContext context, byte index)
context.x += _iconSpacing?[index] ?? IconWidth;
}

protected void DrawChar(DrawContext context, int index) =>
DrawChar(context, (index % _charPerRow) * Context.FontWidth, (index / _charPerRow) * Context.FontHeight);
protected void DrawChar(DrawContext context, int index)
{
var sourceY = (index / _charPerRow) * Context.FontHeight;
if ((index % CharactersPerTexture) >= CharactersPerTextureBlock)
sourceY += 4;

DrawChar(context, (index % _charPerRow) * Context.FontWidth, sourceY);
}

protected void DrawChar(DrawContext context, int sourceX, int sourceY)
{
ISurface surfaceFont;
if (sourceY >= 504)
{
sourceY -= 504;
surfaceFont = _surfaceFont2;
}
else
surfaceFont = _surfaceFont;

if (surfaceFont == null)
return;

protected void DrawChar(DrawContext context, int sourceX, int sourceY) =>
DrawImageScale(context, _surfaceFont, sourceX, sourceY, Context.FontWidth, Context.FontHeight);
DrawImageScale(context, surfaceFont, sourceX, sourceY, Context.FontWidth, Context.FontHeight);
}

protected void DrawIcon(DrawContext context, int sourceX, int sourceY) =>
DrawImage(_surfaceIcon, context.x, context.y, sourceX, sourceY, IconWidth, IconHeight, 1.0, 1.0, new ColorF(1.0f, 1.0f, 1.0f, 1.0f));
Expand All @@ -282,6 +306,7 @@ private void SetContext(KingdomTextContext context)
_fontSpacing = context.FontSpacing;
_iconSpacing = context.IconSpacing;
_imageFont = context.Font;
_imageFont2 = context.Font2;
_imageIcon = context.Icon;
_charPerRow = context.Font?.Size.Width / context.FontWidth ?? 1;
_iconPerRow = context.Icon?.Size.Width / IconWidth ?? 1;
Expand All @@ -290,6 +315,9 @@ private void SetContext(KingdomTextContext context)
if (_imageFont != null)
InitializeSurface(ref _surfaceFont, _imageFont);

if (_imageFont2 != null)
InitializeSurface(ref _surfaceFont2, _imageFont2);

if (_imageIcon != null)
InitializeSurface(ref _surfaceIcon, _imageIcon);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public static KingdomTextContext ToKh2EuSystemTextContext(this kh2.FontContext f
new KingdomTextContext
{
Font = fontContext.ImageSystem,
Font2 = fontContext.ImageSystem2,
Icon = fontContext.ImageIcon,
FontSpacing = fontContext.SpacingSystem,
IconSpacing = fontContext.SpacingIcon,
Expand All @@ -23,6 +24,7 @@ public static KingdomTextContext ToKh2EuEventTextContext(this kh2.FontContext fo
new KingdomTextContext
{
Font = fontContext.ImageEvent,
Font2 = fontContext.ImageEvent2,
Icon = fontContext.ImageIcon,
FontSpacing = fontContext.SpacingEvent,
IconSpacing = fontContext.SpacingIcon,
Expand All @@ -35,6 +37,7 @@ public static KingdomTextContext ToKh2JpSystemTextContext(this kh2.FontContext f
new KingdomTextContext
{
Font = fontContext.ImageSystem,
Font2 = fontContext.ImageSystem2,
Icon = fontContext.ImageIcon,
FontSpacing = fontContext.SpacingSystem,
IconSpacing = fontContext.SpacingIcon,
Expand All @@ -47,6 +50,7 @@ public static KingdomTextContext ToKh2JpEventTextContext(this kh2.FontContext fo
new KingdomTextContext
{
Font = fontContext.ImageEvent,
Font2 = fontContext.ImageEvent2,
Icon = fontContext.ImageIcon,
FontSpacing = fontContext.SpacingEvent,
IconSpacing = fontContext.SpacingIcon,
Expand Down
1 change: 1 addition & 0 deletions OpenKh.Tools.Common/Models/KingdomTextContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace OpenKh.Tools.Common.Models
public class KingdomTextContext
{
public IImageRead Font { get; set; }
public IImageRead Font2 { get; set; }
public IImageRead Icon { get; set; }
public byte[] FontSpacing { get; set; }
public byte[] IconSpacing { get; set; }
Expand Down

0 comments on commit 9da04c1

Please sign in to comment.