pub struct GlyphCache { /* private fields */ }Expand description
Rasterized glyphs packed into a paged coverage atlas with per-glyph LRU eviction.
Call begin_frame once per frame, get for every
glyph drawn, then apply drain_updates to the GPU texture before
drawing. A glyph looked up in the current frame is never evicted, so every atlas rectangle
handed out during a frame stays valid until the next begin_frame. One cache serves one
frame loop: two render loops sharing it would evict each other’s glyphs mid-frame.
When full, the atlas grows a page; at its page limit it evicts the glyphs used least recently (never this frame’s). A glyph is unavailable only if it does not fit even then.
Allocation: rasterizing a glyph for the first time allocates, and so does adding a page. Hits, evictions and retries of waiting glyphs do not (the eviction order reuses its buffer; it grows only past its largest size so far).
Implementations§
Source§impl GlyphCache
impl GlyphCache
Sourcepub fn new(config: AtlasConfig) -> Self
pub fn new(config: AtlasConfig) -> Self
An empty cache of one page; config is normalized.
Sourcepub fn config(&self) -> AtlasConfig
pub fn config(&self) -> AtlasConfig
The atlas shape (normalized, and capped by limit_side).
Sourcepub fn limit_side(&mut self, max_side: u32) -> Result<(), AtlasLimitError>
pub fn limit_side(&mut self, max_side: u32) -> Result<(), AtlasLimitError>
Caps growth so the texture never exceeds max_side texels (the device’s
max_texture_dimension_2d).
§Errors
If a page, or the atlas already grown, is larger than max_side.
Sourcepub fn atlas_extent(&self) -> (u32, u32)
pub fn atlas_extent(&self) -> (u32, u32)
Current texture size in texels: (page_size, page_size * pages).
Sourcepub fn cached_glyphs(&self) -> usize
pub fn cached_glyphs(&self) -> usize
Glyphs cached, with or without atlas space.
Sourcepub fn begin_frame(&mut self)
pub fn begin_frame(&mut self)
Starts a frame. Glyphs used in earlier frames become evictable.
Sourcepub fn generation(&self) -> u64
pub fn generation(&self) -> u64
Increments whenever glyphs are evicted: atlas rectangles from earlier frames may now hold other glyphs.
Sourcepub fn stats(&self) -> CacheStats
pub fn stats(&self) -> CacheStats
Cumulative counters.
Sourcepub fn get(
&mut self,
font: &Font,
glyph: GlyphId,
px: f32,
subpixel: u8,
) -> Result<CachedGlyph, GlyphUnavailable>
pub fn get( &mut self, font: &Font, glyph: GlyphId, px: f32, subpixel: u8, ) -> Result<CachedGlyph, GlyphUnavailable>
The atlas placement of glyph at px pixels per em and subpixel bin subpixel
(see quantize_x), rasterizing it on first use.
§Errors
GlyphUnavailable when there is nothing to draw this frame; counted in
CacheStats::dropped.
Sourcepub fn drain_updates(
&mut self,
) -> impl ExactSizeIterator<Item = AtlasUpdate> + '_
pub fn drain_updates( &mut self, ) -> impl ExactSizeIterator<Item = AtlasUpdate> + '_
Takes the atlas changes queued since the last call, in the order they must be applied. Apply all of them: the queue is emptied even if the iterator is dropped early.