Skip to content

Commit 203cc7d

Browse files
author
Roman Shapiro
committed
Yet another optimization
1 parent e0f4853 commit 203cc7d

File tree

3 files changed

+16
-20
lines changed

3 files changed

+16
-20
lines changed

src/FontStashSharp/Font.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ namespace FontStashSharp
66
{
77
internal unsafe class Font: IDisposable
88
{
9-
private readonly byte[] Data;
109
private GCHandle? dataPtr = null;
1110
private float AscentBase, DescentBase, LineHeightBase;
12-
11+
readonly Int32Map<int> _kernings = new Int32Map<int>();
12+
1313
public float Ascent { get; private set; }
1414
public float Descent { get; private set; }
1515
public float LineHeight { get; private set; }
@@ -24,8 +24,6 @@ public Font(byte[] data)
2424
throw new ArgumentNullException(nameof(data));
2525
}
2626

27-
Data = data;
28-
2927
dataPtr = GCHandle.Alloc(data, GCHandleType.Pinned);
3028
}
3129

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

77+
public int GetGlyphKernAdvance(int glyph1, int glyph2)
78+
{
79+
var key = ((glyph1 << 16) | (glyph1 >> 16)) ^ glyph2;
80+
int result;
81+
if (_kernings.TryGetValue(key, out result))
82+
{
83+
return result;
84+
}
85+
result = stbtt_GetGlyphKernAdvance(_font, glyph1, glyph2);
86+
_kernings[key] = result;
87+
return result;
88+
}
89+
7990
public static Font FromMemory(byte[] data)
8091
{
8192
var font = new Font(data);

src/FontStashSharp/FontGlyph.cs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ namespace FontStashSharp
44
{
55
class FontGlyph
66
{
7-
private readonly Int32Map<int> _kernings = new Int32Map<int>();
8-
97
public Font Font;
108
public FontAtlas Atlas;
119
public int Codepoint;
@@ -28,18 +26,5 @@ public bool IsEmpty
2826
return Bounds.Width == 0 || Bounds.Height == 0;
2927
}
3028
}
31-
32-
public int GetKerning(FontGlyph nextGlyph)
33-
{
34-
int result;
35-
if (_kernings.TryGetValue(nextGlyph.Index, out result))
36-
{
37-
return result;
38-
}
39-
result = StbTrueTypeSharp.StbTrueType.stbtt_GetGlyphKernAdvance(Font._font, Index, nextGlyph.Index);
40-
_kernings[nextGlyph.Index] = result;
41-
42-
return result;
43-
}
4429
}
4530
}

src/FontStashSharp/FontSystem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -825,7 +825,7 @@ private void GetQuad(FontGlyph glyph, FontGlyph prevGlyph, float spacing, ref fl
825825
float adv = 0;
826826
if (UseKernings && glyph.Font == prevGlyph.Font)
827827
{
828-
adv = prevGlyph.GetKerning(glyph) * glyph.Font.Scale;
828+
adv = prevGlyph.Font.GetGlyphKernAdvance(prevGlyph.Index, glyph.Index) * glyph.Font.Scale;
829829
}
830830

831831
x += (int)(adv + spacing + 0.5f);

0 commit comments

Comments
 (0)