From 1a71c3235d8dd7bbf1354e5701bc15ebb528309c Mon Sep 17 00:00:00 2001 From: Matthew Date: Fri, 17 Apr 2026 14:36:57 +1000 Subject: [PATCH] change data structures to include texel info --- fonts.d | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/fonts.d b/fonts.d index 9a57082..bab6659 100644 --- a/fonts.d +++ b/fonts.d @@ -13,6 +13,13 @@ import std.algorithm.sorting; const u64 FONT_MAX_BANDS = 8; const u64 FONT_TEX_WIDTH = 4096; +struct SlugTexel(T) +{ + T[] data; + u32 height; + u32 width; +} + struct SlugCurveRef { u32 index; @@ -59,8 +66,8 @@ struct SlugGlyph struct SlugFontInfo { SlugCurve[] curves; - f32[] curve_texel_data; - u32[] band_texel_data; + SlugTexel!(f32) curve_texel; + SlugTexel!(u32) band_texel; SlugGlyphBandData[] glyph_bands; SlugFont base; @@ -542,8 +549,12 @@ LoadFontCurves(Arena* arena, SlugFontInfo* font_info, u8[] font_file_data) u32 curve_texel_height = clamp(cast(u32)((curve_texel_count+FONT_TEX_WIDTH-1) / FONT_TEX_WIDTH), 1, -1); - f32[] curve_texel_data = Alloc!(f32)(arena, FONT_TEX_WIDTH*curve_texel_height*4); - u32[] curve_start_indices = Alloc!(u32)(arena, font_info.glyphs.length); + u32[] curve_start_indices = Alloc!(u32)(arena, font_info.glyphs.length); + SlugTexel!(f32) curve_texel = { + data: Alloc!(f32)(arena, FONT_TEX_WIDTH*curve_texel_height*4), + height: curve_texel_height, + width: FONT_TEX_WIDTH, + }; curve_index = 1; foreach(i; 0 .. font_info.glyphs.length) @@ -560,14 +571,14 @@ LoadFontCurves(Arena* arena, SlugFontInfo* font_info, u8[] font_file_data) UVec2 t0 = UVec2(curve_index%FONT_TEX_WIDTH, curve_index/FONT_TEX_WIDTH); u32 offset = cast(u32)((t0.y*FONT_TEX_WIDTH + t0.x) * 4); - curve_texel_data[offset .. offset+4] = [curve.p1.x, curve.p1.y, curve.p2.x, curve.p2.y]; + curve_texel.data[offset .. offset+4] = [curve.p1.x, curve.p1.y, curve.p2.x, curve.p2.y]; curve_index += 1; UVec2 t1 = UVec2(curve_index%FONT_TEX_WIDTH, curve_index/FONT_TEX_WIDTH); offset = cast(u32)((t1.y*FONT_TEX_WIDTH + t1.x) * 4); - curve_texel_data[offset .. offset+4] = [curve.p3.x, curve.p3.y, 0.0, 0.0]; + curve_texel.data[offset .. offset+4] = [curve.p3.x, curve.p3.y, 0.0, 0.0]; curve_index += 1; } @@ -600,7 +611,11 @@ LoadFontCurves(Arena* arena, SlugFontInfo* font_info, u8[] font_file_data) } u32 band_texel_height = clamp(cast(u32)((band_total_count+FONT_TEX_WIDTH-1) / FONT_TEX_WIDTH), 1U, u32.max); - u32[] band_texel_data = Alloc!(u32)(arena, FONT_TEX_WIDTH*band_texel_height*4); + SlugTexel!(u32) band_texel = { + data: Alloc!(u32)(arena, FONT_TEX_WIDTH*band_texel_height*4), + width: FONT_TEX_WIDTH, + height: band_texel_height, + }; u32 band_index; foreach(i; 0 .. font_info.glyphs.length) @@ -661,7 +676,7 @@ LoadFontCurves(Arena* arena, SlugFontInfo* font_info, u8[] font_file_data) count = band_data.bands[1][index].count; } - band_texel_data[di .. di+2] = [count, offsets[j]]; + band_texel.data[di .. di+2] = [count, offsets[j]]; } foreach(j; 0 .. band_total) @@ -678,12 +693,15 @@ LoadFontCurves(Arena* arena, SlugFontInfo* font_info, u8[] font_file_data) u32 tl = ls+k; u32 di = cast(u32)((tl/FONT_TEX_WIDTH*FONT_TEX_WIDTH + tl%FONT_TEX_WIDTH) * 4); - band_texel_data[di .. di+2] = [ct%FONT_TEX_WIDTH, ct/FONT_TEX_WIDTH]; + band_texel.data[di .. di+2] = [ct%FONT_TEX_WIDTH, ct/FONT_TEX_WIDTH]; } } band_index = glyph_start+band_offset; } + + font_info.curve_texel = curve_texel; + font_info.band_texel = band_texel; } return result;