enum Font_AtlasType { Font_AtlasType_Bitmap, Font_AtlasType_SDF };
struct Font_Glyph {
uint32_t codepoint;
float advance;
float plane_left;
float plane_top;
float plane_right;
float plane_bottom;
float atlas_left;
float atlas_top;
float atlas_right;
float atlas_bottom;
};
struct Font {
struct engine_Image atlas;
enum Font_AtlasType atlas_type;
uint32_t num_glyphs;
const struct Font_Glyph *glyphs;
};
static const struct Font_Glyph *_font_findGlyph(const struct Font *font, const char *text) {
// TODO handle utf8
char32_t c = *text;
// TODO bsearch
for(uint32_t i = 0; i < font->num_glyphs; i++) {
if(font->glyphs[i].codepoint == c) {
return &font->glyphs[i];
}
}
// return space
return &font->glyphs[0];
}
static void _font_measure(struct Font *font, const char *text, float size, float spacing, float *width, float *height) {
*height = size;
*width = 0;
while(*text) {
const struct Font_Glyph *glyph = _font_findGlyph(font, text);
// TODO utf8 advance
text++;
(*width) += glyph->advance * size + spacing;
}
}