Glyph Cache
-
class GlyphCache
Font rasterization cache backed by a texture atlas.
Rasterizes glyphs on demand using stb_truetype and stores them in a greyscale atlas that can be uploaded to the GPU.
Public Functions
-
~GlyphCache()
-
inline i32 ascent() const
Get the font ascent.
- Returns:
Ascent in pixels.
-
inline const u8 *atlasData() const
Get the raw atlas pixel data.
- Returns:
Pointer to the greyscale atlas buffer.
-
inline bool atlasDirty() const
Check if the atlas has been modified since last upload.
- Returns:
True if dirty.
-
inline i32 atlasHeight() const
Get the atlas height.
- Returns:
Height in pixels.
-
inline i32 atlasWidth() const
Get the atlas width.
- Returns:
Width in pixels.
-
void drawText(u32 *pixels, i32 stride, i32 bufH, i32 x, i32 y, std::string_view text, Color c)
Rasterize text directly into a pixel buffer (CPU path).
- Parameters:
pixels – Destination pixel buffer (RGBA u32).
stride – Row stride of the destination buffer in pixels.
bufH – Height of the destination buffer in pixels.
x – X position to start drawing.
y – Y position to start drawing.
text – UTF-8 text to rasterize.
c – Text color.
-
const GlyphMetrics *getGlyph(char ch)
Get (or rasterize) metrics for a character.
- Parameters:
ch – The character to look up.
- Returns:
Pointer to the glyph metrics, or nullptr on failure.
-
bool init(const char *fontPath, f32 fontSize)
Initialize with a TrueType font file and pixel size.
- Parameters:
fontPath – Path to the .ttf font file.
fontSize – Desired font size in pixels.
- Returns:
True on success, false if the font could not be loaded.
-
inline i32 lineHeight() const
Get the line height for the loaded font.
- Returns:
Line height in pixels.
-
inline void markClean()
Mark the atlas as clean (after uploading to GPU).
-
i32 measureText(std::string_view text)
Measure the horizontal advance width of a text string.
- Parameters:
text – UTF-8 text to measure.
- Returns:
Width in pixels.
-
void release()
Release all resources.
-
~GlyphCache()
-
struct GlyphMetrics
Metrics for a single rasterized glyph.