Skip to content

Commit bca9666

Browse files
if font bug
1 parent 457b4f7 commit bca9666

File tree

5 files changed

+1012
-539
lines changed

5 files changed

+1012
-539
lines changed

Flamui/Drawing/FontLoader.cs

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ public float GetHeight(float scale)
102102
// }
103103
}
104104

105+
public record struct GlyphCacheHash
106+
{
107+
public char Character;
108+
public float ResolutionMultiplier;
109+
}
110+
105111
public class FontAtlas
106112
{
107113
public required ScaledFont Font;
@@ -110,11 +116,18 @@ public class FontAtlas
110116

111117
public GpuTexture GpuTexture;
112118

113-
public required LRUCache<int, AtlasGlyphInfo> Table;
119+
public required LRUCache<GlyphCacheHash, AtlasGlyphInfo> Table;
120+
121+
private nint GlyphTempMemory;
122+
123+
public FontAtlas()
124+
{
125+
GlyphTempMemory = Marshal.AllocHGlobal(100 * 100);
126+
}
114127

115128
public unsafe AtlasGlyphInfo FindGlyphEntry(char c, float resolutionMultiplier)
116129
{
117-
var hash = HashCode.Combine(c, resolutionMultiplier);
130+
var hash = new GlyphCacheHash { Character = c, ResolutionMultiplier = resolutionMultiplier };
118131

119132
if (Table.TryGet(hash, out var entry))
120133
{
@@ -123,14 +136,23 @@ public unsafe AtlasGlyphInfo FindGlyphEntry(char c, float resolutionMultiplier)
123136

124137
entry = Table.GetLeastUsed();
125138

126-
int width = 0;
127-
int height = 0;
128-
int xOff = 0;
129-
int yOff = 0;
139+
int ix0 = 0;
140+
int iy0 = 0;
141+
int ix1 = 0;
142+
int iy1 = 0;
130143

131-
var bitmap = StbTrueType.stbtt_GetCodepointBitmap(Font.Font.FontInfo, 0, Font.Scale * resolutionMultiplier, c,
132-
&width, &height, &xOff, &yOff);
133-
var bitmapSpan = new Span<byte>(bitmap, width * height);
144+
var scale = Font.Scale * resolutionMultiplier;
145+
StbTrueType.stbtt_GetCodepointBitmapBox(Font.Font.FontInfo, c, scale, scale, &ix0, &iy0, &ix1, &iy1);
146+
StbTrueType.stbtt_MakeCodepointBitmap(Font.Font.FontInfo, (byte*)GlyphTempMemory, 100, 100, 100, scale, scale, c);
147+
148+
int width = ix1 - ix0;
149+
int height = iy1 - iy0;
150+
int xOff = ix0;
151+
int yOff = iy0;
152+
153+
var bitmapSpan = new Span<byte>((void*)GlyphTempMemory, 100 * 100);
154+
155+
Console.WriteLine($"glyph cache miss: {c}:{resolutionMultiplier}, inserting into slot {entry.SlotNumber}");
134156

135157
for (var i = 0; i < bitmapSpan.Length; i++) //correct upwards for clearer text, not sure why we need to do it...
136158
{
@@ -143,18 +165,17 @@ public unsafe AtlasGlyphInfo FindGlyphEntry(char c, float resolutionMultiplier)
143165

144166
GpuTexture.Gl.BindTexture(TextureTarget.Texture2D, GpuTexture.TextureId);
145167
GpuTexture.Gl.PixelStore(PixelStoreParameter.UnpackAlignment, 1);
146-
GpuTexture.Gl.TexSubImage2D(TextureTarget.Texture2D, 0, entry.AtlasX, entry.AtlasY, (uint)width, (uint)height, PixelFormat.Red, PixelType.UnsignedByte, (void*)bitmap);
147-
148-
Console.WriteLine($"Uploading {c} at {entry.AtlasX},{entry.AtlasY}");
168+
GpuTexture.Gl.TexSubImage2D(TextureTarget.Texture2D, 0, entry.AtlasX, entry.AtlasY, (uint)100, (uint)100, PixelFormat.Red, PixelType.UnsignedByte, (void*)GlyphTempMemory);
149169

150170
var info = new AtlasGlyphInfo
151171
{
152-
Height = height / resolutionMultiplier,
153-
Width = width / resolutionMultiplier,
154-
XOff = xOff / resolutionMultiplier,
155-
YOff = yOff / resolutionMultiplier,
172+
Height = (float)height / resolutionMultiplier,
173+
Width = (float)width / resolutionMultiplier,
174+
XOff = (float)xOff / resolutionMultiplier,
175+
YOff = (float)yOff / resolutionMultiplier,
156176
AtlasX = entry.AtlasX,
157177
AtlasY = entry.AtlasY,
178+
SlotNumber = entry.SlotNumber,
158179
AtlasWidth = width,
159180
AtlasHeight = height,
160181
// GlyphBoundingBox = bb,
@@ -187,6 +208,7 @@ public struct AtlasGlyphInfo
187208
public required float Height; //height of the glyph
188209
public required float XOff;
189210
public required float YOff;
211+
public required int SlotNumber;
190212

191213
public int AdvanceWidth => (int)(FontGlyphInfo.UnscaledAdvanceWidth * Scale);
192214
public int LeftSideBearing => (int)(FontGlyphInfo.UnscaledLeftSideBearing * Scale);
@@ -232,17 +254,22 @@ public static unsafe Font LoadFont(string name)
232254

233255
public static FontAtlas CreateFontAtlas(ScaledFont scaledFont)
234256
{
235-
var table = new LRUCache<int, AtlasGlyphInfo>(10*10);
257+
var table = new LRUCache<GlyphCacheHash, AtlasGlyphInfo>(10*10);
258+
259+
int slotNumber = 0;
236260

237-
for (int i = 1; i < 11; i++)
261+
for (int i = 0; i < 10; i++)
238262
{
239-
for (int j = 1; j < 11; j++)
263+
for (int j = 0; j < 10; j++)
240264
{
241-
table.Add((int.MaxValue - i - 100 * j).GetHashCode(), default(AtlasGlyphInfo) with
265+
table.Add(new GlyphCacheHash{ Character = (char)(i * 10 + j), ResolutionMultiplier = i * j}, default(AtlasGlyphInfo) with
242266
{
243267
AtlasX = i * 100,
244268
AtlasY = j * 100,
269+
SlotNumber = slotNumber
245270
});
271+
272+
slotNumber++;
246273
}
247274
}
248275

Flamui/Drawing/GlCanvas.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ public void DrawText(ReadOnlySpan<char> text, float x, float y)
3535
{
3636
var resolutionMultiplier = new Vector2(1, 1).Multiply(MeshBuilder.Matrix.GetScale()).Y;
3737

38-
Console.WriteLine(resolutionMultiplier);
39-
4038
var fontAtlas = _renderer.GetFontAtlas(Paint.Font);
4139

4240
var xCoord = x;
@@ -45,12 +43,13 @@ public void DrawText(ReadOnlySpan<char> text, float x, float y)
4543
{
4644
var glyphInfo = fontAtlas.FindGlyphEntry(c, resolutionMultiplier);
4745

48-
DrawGlyph(fontAtlas, glyphInfo, fontAtlas.GpuTexture,(int)(xCoord + glyphInfo.LeftSideBearing), (int)(y + fontAtlas.Font.Ascent + glyphInfo.YOff));
46+
// Console.WriteLine($"{c}: {fontAtlas.Font.Ascent}: {glyphInfo.YOff}, {glyphInfo.AtlasHeight}, {glyphInfo.Height}");
47+
DrawGlyph(fontAtlas, glyphInfo, fontAtlas.GpuTexture, xCoord + glyphInfo.LeftSideBearing, y + fontAtlas.Font.Ascent + glyphInfo.YOff);
4948
xCoord += glyphInfo.AdvanceWidth;
5049
}
5150
}
52-
53-
private void DrawGlyph(FontAtlas fontAtlas, AtlasGlyphInfo atlasGlyphInfo, GpuTexture texture, int x, int y) //todo, maybe subpixel glyph positioning
51+
//an
52+
private void DrawGlyph(FontAtlas fontAtlas, AtlasGlyphInfo atlasGlyphInfo, GpuTexture texture, float x, float y) //todo, maybe subpixel glyph positioning
5453
{
5554
var uvXOffset = (1 / (float)fontAtlas.AtlasWidth) * atlasGlyphInfo.AtlasX;
5655
var uvYOffset = (1 / (float)fontAtlas.AtlasHeight) * atlasGlyphInfo.AtlasY;

Flamui/Drawing/Shaders/main_fragment.glsl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ void main()
2121
out_color = texture(uTextures[int(texture_id)], frag_texCoords) * frag_color;
2222
}else if(texture_type == 2){
2323
out_color = texture(uTextures[int(texture_id)], frag_texCoords).r * frag_color;
24+
// out_color = vec4(1, 0, 0, 1);
2425
}
2526
}else{
2627
float x = frag_texCoords.x;

Sample.LayoutTest/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ public class LayoutTest(FlamuiApp app) : FlamuiComponent
2626
private const string loremIpsum =
2727
"Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
2828

29-
private string input2 = "";
30-
private string selectedOption = "Hi";
29+
private string input2 = "anita max wynn";
30+
private string selectedOption = "";
3131

3232
public override void Build(Ui ui)
3333
{

0 commit comments

Comments
 (0)