Skip to main content

TextFontService

Struct TextFontService 

Source
pub struct TextFontService { /* private fields */ }
Expand description

Shared font resources for a text-typeset session.

Owns the font registry, the glyph atlas, the glyph cache, and the swash scale context. Construct one per process (or one per window if you really need isolated atlases) and share it by Rc<RefCell<_>> across every DocumentFlow that renders into the same atlas.

Implementations§

Source§

impl TextFontService

Source

pub fn new() -> Self

Create an empty service with no fonts registered.

Call register_font and set_default_font before any DocumentFlow lays out content against this service.

Source

pub fn register_font(&mut self, data: &[u8]) -> FontFaceId

Register a font face from raw TTF/OTF/WOFF bytes.

Parses the font’s name table to extract family, weight, and style, then indexes it via fontdb for CSS-spec font matching. Returns the first face ID — font collections (.ttc) may contain multiple faces.

§Panics

Panics if the font data contains no parseable faces.

Source

pub fn register_font_as( &mut self, data: &[u8], family: &str, weight: u16, italic: bool, ) -> FontFaceId

Register a font with explicit metadata, overriding the font’s name table. Use when the font’s internal metadata is unreliable or when aliasing a font to a different family name.

§Panics

Panics if the font data contains no parseable faces.

Source

pub fn set_default_font(&mut self, face: FontFaceId, size_px: f32)

Set which face to use as the document default, plus its base size in logical pixels. This is the fallback font when a fragment’s TextFormat doesn’t specify a family or when the requested family isn’t found.

Source

pub fn set_generic_family(&mut self, generic: &str, family: &str)

Map a generic family name (e.g. "serif", "monospace") to a concrete registered family. When text-document emits a fragment whose font_family matches a generic, the font resolver looks it up through this table before querying fontdb.

Source

pub fn font_family_name(&self, face_id: FontFaceId) -> Option<String>

Look up the family name of a registered face by id.

Source

pub fn font_registry(&self) -> &FontRegistry

Borrow the font registry directly — needed by callers that want to inspect or extend it beyond the helpers exposed here.

Source

pub fn set_scale_factor(&mut self, scale_factor: f32)

Set the device pixel ratio for HiDPI rasterization.

Layout stays in logical pixels; glyphs are shaped and rasterized at size_px * scale_factor so text is crisp on HiDPI displays. Orthogonal to DocumentFlow::set_zoom, which is a post-layout display transform.

Changing this value invalidates the glyph cache and the atlas (both are cleared here) and marks every DocumentFlow that was laid out against this service as stale via the scale_generation counter. The caller must then re-run layout_full / layout_blocks on every flow before the next render — existing shaped advances depended on the old ppem rounding.

Clamped to 0.25..=8.0. Default is 1.0.

Source

pub fn scale_factor(&self) -> f32

The current scale factor (default 1.0).

Source

pub fn scale_generation(&self) -> u64

Monotonic counter bumped by every successful set_scale_factor call.

DocumentFlow snapshots this during layout so the framework can ask whether a flow needs to be re-laid out after a HiDPI change without having to track the transition itself.

Source

pub fn atlas_snapshot(&mut self, advance_generation: bool) -> AtlasSnapshot<'_>

Read the glyph atlas state without triggering a render.

Optionally advances the cache generation and runs eviction. Returns an AtlasSnapshot the caller can pattern-match by field. The atlas’s internal dirty flag is cleared here, so the caller must either upload pixels during the returned borrow or accept a one-frame delay.

When snapshot.glyphs_evicted is true, callers that cache glyph positions (e.g. paint caches) must invalidate — evicted atlas slots may be reused by subsequent allocations and old UVs would now point to the wrong glyph.

Only advance the generation on frames where actual text work happened; skipping eviction on idle frames prevents aging out glyphs that are still visible but not re-measured this tick.

Source

pub fn atlas_dirty(&self) -> bool

True if the atlas has pending pixel changes since the last upload. The atlas is marked clean after every render() that copies pixels into its RenderFrame; this accessor exposes the flag for framework paint-cache invalidation decisions.

Source

pub fn atlas_width(&self) -> u32

Current atlas texture width in pixels.

Source

pub fn atlas_height(&self) -> u32

Current atlas texture height in pixels.

Source

pub fn atlas_pixels(&self) -> &[u8]

Raw atlas pixel buffer (RGBA8).

Source

pub fn mark_atlas_clean(&mut self)

Mark the atlas clean after the caller has uploaded its contents to the GPU. Paired with atlas_dirty + atlas_pixels for framework adapters that upload directly from the service instead of consuming RenderFrame::atlas_pixels.

Trait Implementations§

Source§

impl Default for TextFontService

Source§

fn default() -> Self

Returns the “default value” for a type. 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 = 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.