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
impl TextFontService
Sourcepub fn new() -> Self
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.
Sourcepub fn register_font(&mut self, data: &[u8]) -> FontFaceId
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.
Sourcepub fn register_font_as(
&mut self,
data: &[u8],
family: &str,
weight: u16,
italic: bool,
) -> FontFaceId
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.
Sourcepub fn set_default_font(&mut self, face: FontFaceId, size_px: f32)
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.
Sourcepub fn set_generic_family(&mut self, generic: &str, family: &str)
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.
Sourcepub fn font_family_name(&self, face_id: FontFaceId) -> Option<String>
pub fn font_family_name(&self, face_id: FontFaceId) -> Option<String>
Look up the family name of a registered face by id.
Sourcepub fn font_registry(&self) -> &FontRegistry
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.
Sourcepub fn set_scale_factor(&mut self, scale_factor: f32)
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.
Sourcepub fn scale_factor(&self) -> f32
pub fn scale_factor(&self) -> f32
The current scale factor (default 1.0).
Sourcepub fn scale_generation(&self) -> u64
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.
Sourcepub fn atlas_snapshot(&mut self, advance_generation: bool) -> AtlasSnapshot<'_>
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.
Sourcepub fn atlas_dirty(&self) -> bool
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.
Sourcepub fn atlas_width(&self) -> u32
pub fn atlas_width(&self) -> u32
Current atlas texture width in pixels.
Sourcepub fn atlas_height(&self) -> u32
pub fn atlas_height(&self) -> u32
Current atlas texture height in pixels.
Sourcepub fn atlas_pixels(&self) -> &[u8] ⓘ
pub fn atlas_pixels(&self) -> &[u8] ⓘ
Raw atlas pixel buffer (RGBA8).
Sourcepub fn mark_atlas_clean(&mut self)
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.