pub struct TextRasterizer { /* private fields */ }Expand description
Deterministic text host with version-key result caching.
Results are cached by content version, least recently used first out,
within a byte budget (DEFAULT_TEXT_CACHE_BYTES unless
Self::set_cache_budget changes it). A cached result is shared, not
copied: a request whose version is cached returns the same Arc.
Implementations§
Source§impl TextRasterizer
impl TextRasterizer
Sourcepub fn empty() -> Self
pub fn empty() -> Self
A rasterizer with no fonts yet; add them with Self::add_font.
Sourcepub fn new(fonts: Vec<TextFont>) -> Result<Self, TextRasterError>
pub fn new(fonts: Vec<TextFont>) -> Result<Self, TextRasterError>
Takes a fallback-ordered OpenType stack.
§Errors
Returns TextRasterError::NoFonts or the first malformed font.
Sourcepub fn add_font(&mut self, source: TextFont) -> Result<(), TextRasterError>
pub fn add_font(&mut self, source: TextFont) -> Result<(), TextRasterError>
Appends a font to the end of the fallback stack.
Fonts change what every request draws, so the result cache is cleared.
§Errors
Returns TextRasterError::InvalidFont for malformed font bytes.
Sourcepub fn font_count(&self) -> usize
pub fn font_count(&self) -> usize
Number of fonts in the fallback stack.
Sourcepub fn rasterize(
&mut self,
request: &TextRasterRequest,
) -> Result<Arc<TextRasterResult>, TextRasterError>
pub fn rasterize( &mut self, request: &TextRasterRequest, ) -> Result<Arc<TextRasterResult>, TextRasterError>
Shapes, lays out, and rasterizes one request, or returns the cached result of its exact version, shared rather than copied.
§Errors
Returns a typed validation, font, or size error.
Sourcepub fn invalidate(&mut self, content_version: u64)
pub fn invalidate(&mut self, content_version: u64)
Drops a single host version after content or font invalidation.
Sourcepub fn set_cache_budget(&mut self, bytes: usize)
pub fn set_cache_budget(&mut self, bytes: usize)
Sets how many bytes of results the cache keeps, evicting the least recently used down to it. Each result counts its pixels and a small fixed overhead; a result larger than the budget is returned but not kept. Zero keeps nothing.
Sourcepub fn cache_bytes(&self) -> usize
pub fn cache_bytes(&self) -> usize
Bytes the cached results count against the budget.
Sourcepub fn measure(
&self,
request: &TextRasterRequest,
) -> Result<TextMeasure, TextRasterError>
pub fn measure( &self, request: &TextRasterRequest, ) -> Result<TextMeasure, TextRasterError>
Where request’s text lies, laid out as Self::rasterize lays it
out, without drawing it or touching the result cache.
§Errors
Returns the validation, font, or size error Self::rasterize
returns for the same request.
Sourcepub fn shape(
&self,
request: &TextRasterRequest,
) -> Result<ShapedText, TextRasterError>
pub fn shape( &self, request: &TextRasterRequest, ) -> Result<ShapedText, TextRasterError>
Shapes request’s text as Self::rasterize would, for layout: its
lines at any wrapping width come from the result without shaping
again. The request’s max_width, color, outline, background and
content version are ignored.
§Errors
Returns TextRasterError::NoFonts without fonts, and
TextRasterError::InvalidRequest for a size, line height or text
Self::rasterize rejects.