Skip to main content

ShapingCache

Struct ShapingCache 

Source
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>

Source

pub fn new(shaper: S, capacity: usize) -> Self

Create a new shaping cache with the given backend and capacity.

Source

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.

Source

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.

Source

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.
Source

pub fn clear(&mut self)

Clear all cached entries and reset stats.

Source

pub fn stats(&self) -> ShapingCacheStats

Current cache statistics.

Source

pub fn generation(&self) -> u64

Current generation counter.

Source

pub fn shaper(&self) -> &S

Access the underlying shaper.

Source

pub fn resize(&mut self, new_capacity: usize)

Resize the cache capacity.

If the new capacity is smaller than the current size, excess entries are evicted in LRU order.

Auto Trait Implementations§

§

impl<S> Freeze for ShapingCache<S>
where S: Freeze,

§

impl<S> RefUnwindSafe for ShapingCache<S>
where S: RefUnwindSafe,

§

impl<S> Send for ShapingCache<S>
where S: Send,

§

impl<S> Sync for ShapingCache<S>
where S: Sync,

§

impl<S> Unpin for ShapingCache<S>
where S: Unpin,

§

impl<S> UnsafeUnpin for ShapingCache<S>
where S: UnsafeUnpin,

§

impl<S> UnwindSafe for ShapingCache<S>
where S: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more