Skip to content

Commit

Permalink
Fixed incorrect horizontal positioning of some freetype glyphs (in pa…
Browse files Browse the repository at this point in the history
…rticular fullwidth japanese punctuation)
  • Loading branch information
kg committed Feb 6, 2017
1 parent 1c89c13 commit 26ba488
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 31 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -77,3 +77,5 @@ _ReSharper.*
/Squared/FontTest/FontTestContent/bin
/Squared/FontTest/FontTest/bin
/Squared/FontTest/FontTest/obj
/Squared/Render.Freetype/bin
/Squared/Render.Freetype/obj
2 changes: 1 addition & 1 deletion Squared/FontTest/FontTest/FontTestGame.cs
Expand Up @@ -24,7 +24,7 @@ public class FontTestGame : MultithreadedGame {
"\r\nLong woooooooooooooooooooooooord" +
"\r\nこの体は、無限のチェイサーで出来ていた" +
"\r\na b c d e f g h i j k l m n o p q r s t u v w x y z" +
"\r\nはいはい!";
"\r\nはいはい!おつかれさまでした!";

IGlyphSource SpriteFont, LatinFont, UniFont, FallbackFont;

Expand Down
68 changes: 38 additions & 30 deletions Squared/Render.Freetype/FTFont.cs
Expand Up @@ -21,6 +21,8 @@ public class FreeTypeFont : IGlyphSource, IDisposable {
internal Dictionary<FTBitmap, Texture2D> TextureCache =
new Dictionary<FTBitmap, Texture2D>(new ReferenceComparer<FTBitmap>());

public bool Hinting { get; set; }

public FreeTypeFont (RenderCoordinator rc, string filename, int faceIndex = 0) {
RenderCoordinator = rc;
Face = new Face(new Library(), filename, faceIndex);
Expand Down Expand Up @@ -70,6 +72,7 @@ public class FreeTypeFont : IGlyphSource, IDisposable {
}
result.SetData(temp);
} else if (bitmap.PixelMode == PixelMode.Bgra) {
// FIXME
result.SetData(bitmap.BufferData);
} else {
throw new NotImplementedException("Invalid pixel format");
Expand All @@ -89,53 +92,58 @@ public class FreeTypeFont : IGlyphSource, IDisposable {
if (index <= 0)
return false;

var flags = LoadFlags.Color | LoadFlags.Render;
if (!Hinting)
flags |= LoadFlags.NoHinting;

Face.LoadGlyph(
index, LoadFlags.Color | LoadFlags.Render | LoadFlags.NoHinting, LoadTarget.Normal
index, flags, LoadTarget.Normal
);

var ftgs = Face.Glyph;
var scaleX = Face.Size.Metrics.ScaleX;
var scaleY = Face.Size.Metrics.ScaleY;
var bitmap = ftgs.Bitmap;

// using (var ftg = ftgs.GetGlyph()) {
var ascender = Face.Size.Metrics.Ascender.ToSingle();
var tex = GetTexture(bitmap);
var metrics = ftgs.Metrics;

glyph = new SrGlyph {
Character = ch,
Width = metrics.Width.ToSingle(),
LeftSideBearing = metrics.HorizontalBearingX.ToSingle(),
RightSideBearing = (
metrics.HorizontalAdvance.ToSingle() -
metrics.Width.ToSingle() -
metrics.HorizontalBearingX.ToSingle()
),
XOffset = ftgs.BitmapLeft,
YOffset = -ftgs.BitmapTop + ascender,
Texture = tex,
LineSpacing = Face.Size.Metrics.Height.ToSingle()
};

if (tex != null)
glyph.BoundsInTexture = new Rectangle(0, 0, tex.Width, tex.Height);
// }
var ascender = Face.Size.Metrics.Ascender.ToSingle();
var tex = GetTexture(bitmap);
var metrics = ftgs.Metrics;

glyph = new SrGlyph {
Character = ch,
Width = metrics.Width.ToSingle(),
LeftSideBearing = metrics.HorizontalBearingX.ToSingle(),
RightSideBearing = (
metrics.HorizontalAdvance.ToSingle() -
metrics.Width.ToSingle() -
metrics.HorizontalBearingX.ToSingle()
),
XOffset = ftgs.BitmapLeft - metrics.HorizontalBearingX.ToSingle(),
YOffset = -ftgs.BitmapTop + ascender,
Texture = tex,
LineSpacing = Face.Size.Metrics.Height.ToSingle()
};

if (ch == '!')
System.Diagnostics.Debugger.Break();

if (tex != null)
glyph.BoundsInTexture = new Rectangle(0, 0, tex.Width, tex.Height);

Cache[ch] = glyph;
return true;
}

public bool GetKerning (char left, char right, out Vector2 result) {
result = Vector2.Zero;
return false;
}

public void Dispose () {
public void Invalidate () {
foreach (var kvp in TextureCache)
RenderCoordinator.DisposeResource(kvp.Value);

Cache.Clear();
TextureCache.Clear();
}

public void Dispose () {
Invalidate();
}
}
}

0 comments on commit 26ba488

Please sign in to comment.