#ifndef RENDER_FONT_H #define RENDER_FONT_H // NOTE: freetype rasterizes, harfbuzz shapes (kerning, ligatures). two caches keep both off // the per frame path: // // - glyph cache: (face, glyph id) -> rect in a single R8 atlas. open addressing, linear probe, // 16 byte entries so a probe sequence stays within one or two cache lines. // - run cache: hash(text, face) -> shaped glyph list. the ui redraws the same ~200 labels // every frame, so after the first frame no text is shaped again until it changes. // // the atlas is shelf packed. when it fills up (practically never) everything is reset. typedef enum FontKind { FONT_REGULAR, FONT_SEMIBOLD, FONT_MONO, FONT_KIND_COUNT, } FontKind; #define FONT_MAX_FACES 24 #define FONT_ATLAS_SIZE 1024 // ~1MB, the ui uses a handful of sizes, it resets itself if it ever fills #define FONT_GLYPH_SLOTS 8192 // pow2 #define FONT_RUN_SLOTS 4096 // pow2 #define FONT_NO_FACE 0xFFFFFFFFu typedef struct FontFace FontFace; struct FontFace { FT_Face face; hb_font_t *hb; FontKind kind; u32 px; // physical pixel size f32 ascent; // physical f32 descent; f32 line_height; }; typedef struct GlyphEntry GlyphEntry; struct GlyphEntry { u32 key; // (face << 20 | glyph) + 1, 0 = empty u16 x, y; // atlas position u8 w, h; // bitmap size (glyphs are < 256 px) s16 bearing_x; s16 bearing_y; }; typedef struct ShapedGlyph ShapedGlyph; struct ShapedGlyph { u32 glyph; u32 face; // usually the run's face, the latin face for fallback glyphs f32 x; // physical, relative to the run origin f32 y; }; // NOTE: a glyph's atlas placement, copied into the run so drawing it is one linear walk instead // of a glyph table probe per glyph. valid while the run's atlas_generation matches the system's. typedef struct RunGlyph RunGlyph; struct RunGlyph { u16 x, y; u8 w, h; s16 bearing_x; s16 bearing_y; }; typedef struct ShapedRun ShapedRun; struct ShapedRun { u64 hash; // 0 = empty u32 face; u32 glyph_count; f32 width; // physical u32 text_size; u8 *text; // copy, to confirm hash hits ShapedGlyph *glyphs; RunGlyph *placed; u32 atlas_generation; }; // NOTE: in front of the run table: most labels are the same bytes at the same address every // frame (catalog names, setting names, the scratch arena handing out the same addresses), so // (pointer, size, face) finds the run without hashing the text. confirmed with a memcmp. #define FONT_RECENT_SLOTS 1024 // pow2 typedef struct RecentRun RecentRun; struct RecentRun { const u8 *text; u32 size; u32 face; ShapedRun *run; u32 generation; }; typedef struct FontSystem FontSystem; struct FontSystem { FT_Library library; String8 font_data[FONT_KIND_COUNT]; // the .ttf / .ttc files, kept in memory, shared by faces s32 face_index[FONT_KIND_COUNT]; // which face of a .ttc collection, -1 = not picked yet FontFace faces[FONT_MAX_FACES]; u32 face_count; hb_buffer_t *buffer; f32 scale; u8 *atlas; u32 shelf_x, shelf_y, shelf_h; s32 dirty_y0, dirty_y1; // rows that need uploading, y0 >= y1 means clean GlyphEntry *glyphs; u32 glyph_count; ShapedRun *runs; u32 run_count; Arena *run_arena; RecentRun *recent; u32 runs_generation; // bumped when the run table is cleared u32 atlas_generation; // bumped when the atlas is cleared // NOTE: last face lookup per kind, the ui asks for the same few sizes all frame f32 last_size[FONT_KIND_COUNT]; u32 last_face[FONT_KIND_COUNT]; // FONT_NO_FACE = none }; internal b32 font_init(Arena *arena, FontSystem *fonts); internal void font_set_scale(FontSystem *fonts, f32 scale); internal void font_set_data(FontSystem *fonts, FontKind kind, String8 data); internal void font_drop_faces(FontSystem *fonts); // NOTE: sizes and positions in logical pixels internal f32 font_measure(FontSystem *fonts, FontKind kind, f32 size, String8 text); internal f32 font_line_height(FontSystem *fonts, FontKind kind, f32 size); internal f32 font_ascent(FontSystem *fonts, FontKind kind, f32 size); internal f32 draw_text(DrawList *list, FontSystem *fonts, FontKind kind, f32 size, f32 x, f32 baseline, String8 text, u32 color); // NOTE: cuts with an ellipsis when wider than max_width (ltr), returns the drawn width internal f32 draw_text_fit(DrawList *list, FontSystem *fonts, FontKind kind, f32 size, f32 x, f32 baseline, f32 max_width, String8 text, u32 color); #endif // RENDER_FONT_H