Phase 2: Pure Rust Font Engine#
Implement a glyph cache that stores rasterized glyph bitmaps to avoid re-rasterizing the same glyphs repeatedly.
Depends on: Glyph rasterizer
Cache design#
- Key:
(glyph_id: u16, size_px: u16)— size quantized to integer pixels to bound cache size - Value:
GlyphBitmap(the rasterized bitmap from the rasterizer) - Storage:
HashMap<(u16, u16), GlyphBitmap> - Keep it simple — no eviction policy for now (Phase 12 will add a texture atlas with LRU)
Integration with Font#
Font::get_glyph_bitmap(glyph_id: u16, size_px: f32) -> Option<&GlyphBitmap>- Check cache first
- On miss: rasterize, insert into cache, return reference
- Cache is per-font (stored in the
Fontstruct, behindRefCellor similar for interior mutability)
Text rendering helper#
Font::render_text(text: &str, size_px: f32) -> Vec<PositionedGlyph>- Combines shaping (from kern/shaping issue) with cached bitmap lookup
- Each
PositionedGlyphhas: x/y position, reference to bitmap data - This is the main entry point for the rendering pipeline
Acceptance criteria#
- Cache hit avoids re-rasterization (test by checking bitmap pointer equality or call count)
- Multiple sizes of the same glyph are cached independently
- Cache integrates cleanly with Font struct
- render_text produces a complete positioned+rasterized glyph run
- All existing tests continue to pass
-
cargo clippy --workspace -- -D warningspasses -
cargo fmt --all --checkpasses