Skip to content

Commit

Permalink
Yet another optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman Shapiro committed Aug 15, 2020
1 parent e0f4853 commit 203cc7d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 20 deletions.
19 changes: 15 additions & 4 deletions src/FontStashSharp/Font.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ namespace FontStashSharp
{
internal unsafe class Font: IDisposable
{
private readonly byte[] Data;
private GCHandle? dataPtr = null;
private float AscentBase, DescentBase, LineHeightBase;

readonly Int32Map<int> _kernings = new Int32Map<int>();

public float Ascent { get; private set; }
public float Descent { get; private set; }
public float LineHeight { get; private set; }
Expand All @@ -24,8 +24,6 @@ public Font(byte[] data)
throw new ArgumentNullException(nameof(data));
}

Data = data;

dataPtr = GCHandle.Alloc(data, GCHandleType.Pinned);
}

Expand Down Expand Up @@ -76,6 +74,19 @@ public void RenderGlyphBitmap(byte *output, int outWidth, int outHeight, int out
stbtt_MakeGlyphBitmap(_font, output, outWidth, outHeight, outStride, Scale, Scale, glyph);
}

public int GetGlyphKernAdvance(int glyph1, int glyph2)
{
var key = ((glyph1 << 16) | (glyph1 >> 16)) ^ glyph2;
int result;
if (_kernings.TryGetValue(key, out result))
{
return result;
}
result = stbtt_GetGlyphKernAdvance(_font, glyph1, glyph2);
_kernings[key] = result;
return result;
}

public static Font FromMemory(byte[] data)
{
var font = new Font(data);
Expand Down
15 changes: 0 additions & 15 deletions src/FontStashSharp/FontGlyph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ namespace FontStashSharp
{
class FontGlyph
{
private readonly Int32Map<int> _kernings = new Int32Map<int>();

public Font Font;
public FontAtlas Atlas;
public int Codepoint;
Expand All @@ -28,18 +26,5 @@ public bool IsEmpty
return Bounds.Width == 0 || Bounds.Height == 0;
}
}

public int GetKerning(FontGlyph nextGlyph)
{
int result;
if (_kernings.TryGetValue(nextGlyph.Index, out result))
{
return result;
}
result = StbTrueTypeSharp.StbTrueType.stbtt_GetGlyphKernAdvance(Font._font, Index, nextGlyph.Index);
_kernings[nextGlyph.Index] = result;

return result;
}
}
}
2 changes: 1 addition & 1 deletion src/FontStashSharp/FontSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,7 @@ private void GetQuad(FontGlyph glyph, FontGlyph prevGlyph, float spacing, ref fl
float adv = 0;
if (UseKernings && glyph.Font == prevGlyph.Font)
{
adv = prevGlyph.GetKerning(glyph) * glyph.Font.Scale;
adv = prevGlyph.Font.GetGlyphKernAdvance(prevGlyph.Index, glyph.Index) * glyph.Font.Scale;
}

x += (int)(adv + spacing + 0.5f);
Expand Down

0 comments on commit 203cc7d

Please sign in to comment.