//! Glyph bitmap cache: avoids re-rasterizing the same glyphs at the same size. use std::collections::HashMap; use crate::font::rasterizer::GlyphBitmap; /// Cache key: (glyph_id, size_px quantized to integer pixels). type CacheKey = (u16, u16); /// A per-font cache of rasterized glyph bitmaps. /// /// Keys are `(glyph_id, size_px)` where size_px is rounded to the nearest /// integer pixel to bound cache size. No eviction policy — Phase 12 will /// add a texture atlas with LRU. #[derive(Debug)] pub struct GlyphCache { entries: HashMap, } impl GlyphCache { /// Create an empty glyph cache. pub fn new() -> Self { Self { entries: HashMap::new(), } } /// Quantize a floating-point pixel size to an integer key. pub fn quantize_size(size_px: f32) -> u16 { size_px.round().max(1.0) as u16 } /// Look up a cached bitmap. Returns `None` on cache miss. pub fn get(&self, glyph_id: u16, size_key: u16) -> Option<&GlyphBitmap> { self.entries.get(&(glyph_id, size_key)) } /// Insert a rasterized bitmap into the cache. pub fn insert(&mut self, glyph_id: u16, size_key: u16, bitmap: GlyphBitmap) { self.entries.insert((glyph_id, size_key), bitmap); } /// Number of cached entries. pub fn len(&self) -> usize { self.entries.len() } /// Returns true if the cache is empty. pub fn is_empty(&self) -> bool { self.entries.is_empty() } } impl Default for GlyphCache { fn default() -> Self { Self::new() } }