Skip to main content

GlyphCache

Struct GlyphCache 

Source
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

Source

pub fn new(config: AtlasConfig) -> Self

An empty cache of one page; config is normalized.

Source

pub fn config(&self) -> AtlasConfig

The atlas shape (normalized, and capped by limit_side).

Source

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.

Source

pub fn atlas_extent(&self) -> (u32, u32)

Current texture size in texels: (page_size, page_size * pages).

Source

pub fn cached_glyphs(&self) -> usize

Glyphs cached, with or without atlas space.

Source

pub fn begin_frame(&mut self)

Starts a frame. Glyphs used in earlier frames become evictable.

Source

pub fn generation(&self) -> u64

Increments whenever glyphs are evicted: atlas rectangles from earlier frames may now hold other glyphs.

Source

pub fn stats(&self) -> CacheStats

Cumulative counters.

Source

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.

Source

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.

Trait Implementations§

Source§

impl Debug for GlyphCache

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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, 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 = !

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

fn try_from(value: U) -> Result<T, !>

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.