From 6ae59dab5bca9d44fdf4a7f517654b1c0f8545f1 Mon Sep 17 00:00:00 2001 From: Matthew Date: Sat, 18 Apr 2026 14:59:57 +1000 Subject: [PATCH] add slug vertex loading --- fonts.d | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 76 insertions(+), 4 deletions(-) diff --git a/fonts.d b/fonts.d index 67be754..64f6d5e 100644 --- a/fonts.d +++ b/fonts.d @@ -102,9 +102,9 @@ struct SlugVertex { Vec2 em_space_pos; U16Vec2 glyph_data_coords; - struct max_bands + struct { - u8 x, pad, y, e; + u8 max_bands_x, pad, max_bands_y, max_bands_e; }; } }; @@ -121,13 +121,20 @@ struct SlugVertex Vec4 band; struct { - Vec4 scale; - Vec4 offset; + Vec2 band_scale; + Vec2 band_offset; }; }; Vec4 color; } +struct SlugBuffer +{ + SlugVertex[] vertices; + u32[] indices; + u32 quad_offset; +} + struct FontAtlas { Glyph[128] glyphs; @@ -818,3 +825,68 @@ SlugBuildBands(Arena* arena, u32 index, SlugFontInfo* font_info, u32 band_count) } } } + +void +SlugLoadFontVertex(u8 ch, IVec2 pos, SlugFont* font, SlugBuffer* buffer) +{ + ch = ch > 127 ? 0 : ch; + + SlugGlyph* glyph = &font.glyphs[ch]; + + if(ch == ' ') + { + pos.x += cast(i32)(cast(f32)(glyph.advance) * font.scale); + } + else if(glyph.curve_count > 0) + { + Vec2 glyph_pos = Vec2(pos.x, pos.y); + + Vec2 p0 = glyph_pos + glyph.rect.min * font.scale; + Vec2 p1 = glyph_pos + glyph.rect.max * font.scale; + + u32 vertex_start = buffer.quad_offset*4; + u32 index_start = buffer.quad_offset*6; + + SlugVertex[] vertices = buffer.vertices[vertex_start .. vertex_start+4]; + u32[] indices = buffer.indices[index_start .. index_start+6]; + + const f32 NORMAL = 1.0; + + f32 inv_scale = 1.0f/font.scale; + + foreach(i; 0 .. 4) + { + vertices[i].band_scale = glyph.band_scale; + vertices[i].band_offset = glyph.band_offset; + vertices[i].glyph_data_coords = glyph.coords; + vertices[i].max_bands_x = glyph.max_bands.x; + vertices[i].max_bands_y = glyph.max_bands.y; + vertices[i].jac = Vec4(inv_scale, 0.0, 0.0, inv_scale); + + vertices[i].color = 1.0; + } + + vertices[0].vertex_coords = p0; + vertices[0].normal = Vec2(-NORMAL, +NORMAL); + vertices[0].em_space_pos = Vec2(glyph.rect.min.x, glyph.rect.max.y); + + vertices[1].vertex_coords = Vec2(p0.x, p1.y); + vertices[1].normal = -NORMAL; + vertices[1].em_space_pos = glyph.rect.min; + + vertices[2].vertex_coords = Vec2(p1.x, p0.y); + vertices[2].normal = +NORMAL; + vertices[2].em_space_pos = glyph.rect.max; + + vertices[3].vertex_coords = p1; + vertices[3].normal = Vec2(+NORMAL, -NORMAL); + vertices[3].em_space_pos = Vec2(glyph.rect.max.x, glyph.rect.min.y); + + u32 vidx = vertex_start; + indices[0 .. 6] = [vidx+0, vidx+2, vidx+3, vidx+3, vidx+1, vidx+0]; + + buffer.quad_offset += 1; + + pos.x += cast(i32)(cast(f32)(glyph.advance) * font.scale); + } +}