simplify font structures

This commit is contained in:
Matthew 2026-02-02 05:29:28 +11:00
parent fc824c4191
commit 988270db0b

50
fonts.d
View File

@ -10,20 +10,14 @@ import std.math.traits : isNaN;
struct FontAtlas
{
Glyph[128] glyphs;
FontMetrics metrics;
u8[] data;
u32 size;
UVec2 dimensions;
alias metrics this;
}
struct FontMetrics
{
f32 ascent;
f32 descent;
f32 line_gap;
f32 line_height;
f32 max_advance;
f32 ascent;
f32 descent;
f32 line_gap;
f32 line_height;
f32 max_advance;
}
struct Glyph
@ -43,12 +37,6 @@ struct Glyph
__gshared FT_Library FT_LIB;
alias FontFace = FT_Face;
struct FontAtlasBuf
{
u8[] data;
FontAtlas atlas;
}
shared static this()
{
FT_Init_FreeType(&FT_LIB);
@ -99,15 +87,13 @@ TextSize(f32 s)
return cast(u32)((96.0f/72.0f) * s);
}
FontAtlasBuf
FontAtlas
CreateAtlas(Arena* arena, FontFace font, u32 size, UVec2 atlas_dim)
{
FontAtlasBuf abuf = {
data: Alloc!(u8)(arena, atlas_dim.x * atlas_dim.y * 4),
atlas: {
size: size,
dimensions: atlas_dim,
},
FontAtlas atlas = {
data: Alloc!(u8)(arena, atlas_dim.x * atlas_dim.y * 4),
size: size,
dimensions: atlas_dim,
};
// TODO: proper packing algorithm
@ -121,8 +107,8 @@ CreateAtlas(Arena* arena, FontFace font, u32 size, UVec2 atlas_dim)
u64 baseline = font.size.metrics.ascender >> 6;
u64 line_height = (font.size.metrics.height >> 6)+1;
abuf.atlas.line_height = line_height;
abuf.atlas.max_advance = font.size.metrics.max_advance >> 6;
atlas.line_height = line_height;
atlas.max_advance = font.size.metrics.max_advance >> 6;
u32 max_w, max_h, count;
@ -194,7 +180,7 @@ CreateAtlas(Arena* arena, FontFace font, u32 size, UVec2 atlas_dim)
u8 p_r = bmp.buffer[offset+2];
u8 p_a = bmp.buffer[offset+3];
abuf.data[offset .. offset+4] = [DeMultiply(p_r, p_a), DeMultiply(p_b, p_a), DeMultiply(p_g, p_a), p_a];
atlas.data[offset .. offset+4] = [DeMultiply(p_r, p_a), DeMultiply(p_b, p_a), DeMultiply(p_g, p_a), p_a];
}
}
} break;
@ -219,7 +205,7 @@ CreateAtlas(Arena* arena, FontFace font, u32 size, UVec2 atlas_dim)
x = max_w + c;
u64 offset = (y*atlas_dim.y + x) * 4;
abuf.data[offset .. offset+4] = [255, 255, 255, bits & 0x80 ? 255 : 0];
atlas.data[offset .. offset+4] = [255, 255, 255, bits & 0x80 ? 255 : 0];
bits <<= 1;
}
@ -237,7 +223,7 @@ CreateAtlas(Arena* arena, FontFace font, u32 size, UVec2 atlas_dim)
x = max_w + c;
u64 offset = (y*atlas_dim.y + x) * 4;
abuf.data[offset .. offset+4] = [255, 255, 255, bmp.buffer[r*bmp.pitch + c]];
atlas.data[offset .. offset+4] = [255, 255, 255, bmp.buffer[r*bmp.pitch + c]];
}
}
} break;
@ -246,7 +232,7 @@ CreateAtlas(Arena* arena, FontFace font, u32 size, UVec2 atlas_dim)
break;
}
Glyph* g = abuf.atlas.glyphs.ptr + char_code;
Glyph* g = atlas.glyphs.ptr + char_code;
f32 height = glyph.metrics.height >> 6;
f32 width = glyph.metrics.width >> 6;
@ -271,7 +257,7 @@ CreateAtlas(Arena* arena, FontFace font, u32 size, UVec2 atlas_dim)
}
}
return abuf;
return atlas;
}