pub struct ShapingCache<S: TextShaper> { /* private fields */ }Expand description
LRU cache for shaped text runs with generation-based invalidation.
§Invalidation policy
The cache tracks a monotonically increasing generation counter. Each
cached entry is stamped with the generation at insertion time. When
global state changes (font swap, DPR change, zoom), the caller bumps
the generation via invalidate. Entries from older
generations are treated as misses on access and lazily replaced.
This avoids expensive bulk-clear operations while ensuring correctness.
§Thread safety
The cache is not Sync. For multi-threaded use, wrap in a Mutex or
use per-thread instances (matching the thread_local_cache feature
pattern from WidthCache).
Implementations§
Source§impl<S: TextShaper> ShapingCache<S>
impl<S: TextShaper> ShapingCache<S>
Sourcepub fn new(shaper: S, capacity: usize) -> Self
pub fn new(shaper: S, capacity: usize) -> Self
Create a new shaping cache with the given backend and capacity.
Sourcepub fn shape(
&mut self,
text: &str,
script: Script,
direction: RunDirection,
font_id: FontId,
size_256ths: u32,
features: &FontFeatures,
) -> ShapedRun
pub fn shape( &mut self, text: &str, script: Script, direction: RunDirection, font_id: FontId, size_256ths: u32, features: &FontFeatures, ) -> ShapedRun
Shape a text run, returning a cached result if available.
The full shaping key is constructed from the provided parameters. If a cache entry exists with the current generation, it is returned directly. Otherwise, the shaper is invoked and the result is cached.
Sourcepub fn shape_with_style(
&mut self,
text: &str,
script: Script,
direction: RunDirection,
style_id: u64,
font_id: FontId,
size_256ths: u32,
features: &FontFeatures,
) -> ShapedRun
pub fn shape_with_style( &mut self, text: &str, script: Script, direction: RunDirection, style_id: u64, font_id: FontId, size_256ths: u32, features: &FontFeatures, ) -> ShapedRun
Shape with an explicit style discriminant.
Sourcepub fn invalidate(&mut self)
pub fn invalidate(&mut self)
Bump the generation counter, invalidating all cached entries.
Stale entries are not removed eagerly — they are lazily evicted on next access. This makes invalidation O(1).
Call this when:
- The font set changes (font swap, fallback resolution).
- Display DPR changes (affects pixel grid rounding).
- Zoom level changes.
Sourcepub fn stats(&self) -> ShapingCacheStats
pub fn stats(&self) -> ShapingCacheStats
Current cache statistics.
Sourcepub fn generation(&self) -> u64
pub fn generation(&self) -> u64
Current generation counter.