pub struct FontContext {
pub fc_cache: FcFontCache,
pub parsed_fonts: Arc<Mutex<HashMap<FontId, FontRef>>>,
pub font_chain_cache: HashMap<FontChainKey, FontFallbackChain>,
pub embedded_fonts: HashMap<u64, FontRef>,
pub font_hash_to_families: HashMap<u64, StyleFontFamilyVec>,
pub registry: Option<Arc<FcFontRegistry>>,
}Expand description
Bundles all font-related state that can be shared across layout passes.
Separates font concerns from layout/rendering state (LayoutWindow).
Each test/render creates a fresh LayoutWindow from a shared FontContext,
avoiding stale layout cache reuse while keeping parsed fonts warm.
Usage:
let ctx = FontContext::from_fc_cache(fc_cache);
ctx.pre_resolve_chains(&styled_dom, &platform);
ctx.load_fonts_for_chains();
// Per-test: create fresh LayoutWindow from context
let mut window = LayoutWindow::from_font_context(&ctx)?;
window.layout_and_generate_display_list(styled_dom, ...)?;Fields§
§fc_cache: FcFontCacheThe shared font cache. As of rust-fontconfig 4.1 this type is
itself backed by Arc<RwLock<_>>, so cloning is cheap and all
clones see builder-thread writes immediately — no more Arc<T>
wrapping is needed and no more stale-snapshot refresh dance.
parsed_fonts: Arc<Mutex<HashMap<FontId, FontRef>>>§font_chain_cache: HashMap<FontChainKey, FontFallbackChain>§embedded_fonts: HashMap<u64, FontRef>§font_hash_to_families: HashMap<u64, StyleFontFamilyVec>Reverse map: font_family_hash → actual StyleFontFamilyVec. Accumulated across DOMs for persistence. Copied to FontManager on LayoutWindow creation.
registry: Option<Arc<FcFontRegistry>>Optional link back to the live FcFontRegistry. Present iff the
caller wants the scout-on-demand path
(rust_fontconfig::registry::FcFontRegistry::request_and_resolve_with_scripts),
which priority-bumps the builder for not-yet-parsed families
rather than falling back to the empty-snapshot response.
Implementations§
Source§impl FontContext
impl FontContext
Sourcepub fn from_fc_cache(fc_cache: FcFontCache) -> Self
pub fn from_fc_cache(fc_cache: FcFontCache) -> Self
Create from an FcFontCache. Parsed fonts, font chains, and
embedded fonts start empty.
The resulting FontContext has registry = None, so font
chain resolution only sees what’s already in the cache. For
the scout-on-demand path, use FontContext::from_registry
instead, which keeps a handle to the registry so that chain
resolution can lazy-parse families the DOM needs.
Sourcepub fn from_registry(registry: Arc<FcFontRegistry>) -> Self
pub fn from_registry(registry: Arc<FcFontRegistry>) -> Self
Create from a live FcFontRegistry. The fc_cache field gets
a shared handle to the registry’s cache (cheap Arc::clone
on the v4.1 shared-state cache) — writes by builder threads
show up immediately in every reader. Chain resolution goes
through
rust_fontconfig::registry::FcFontRegistry::request_and_resolve_with_scripts
which priority-bumps the builder for unparsed families and
waits for them. This is the “scout-on-demand” path: a
headless renderer can skip the eager common-stack parse and
pay only the per-family cost on first use, dropping peak RSS
by the common-stack metadata size (~15 MiB on macOS).
Sourcepub fn pre_resolve_chains_for_dom(
&mut self,
styled_dom: &StyledDom,
platform: &Platform,
)
pub fn pre_resolve_chains_for_dom( &mut self, styled_dom: &StyledDom, platform: &Platform, )
Pre-resolve font chains for a StyledDom’s CSS font stacks. Call this before layout so text rendering doesn’t skip glyphs.
Unicode-fallback fonts are limited to the scripts actually
present in the document’s text content — for an ASCII-only
page, this skips the ~300 MiB Arial-Unicode / CJK / Arabic
pull-in entirely. See
crate::solver3::getters::scripts_present_in_styled_dom.
Sourcepub fn load_fonts_for_chains(&self)
pub fn load_fonts_for_chains(&self)
Load parsed font bytes from disk for all fonts referenced in font_chain_cache.
Thin wrapper that materialises a ResolvedFontChains from the
cached chain map and delegates the actual disk-load to the
shared FontManager::load_missing_for_chains helper, so the
“collect → diff → load → insert” sequence lives in exactly
one place. Failures are silently dropped here (the caller is
the warmup path which has no good place to log them); use
FontManager::load_missing_for_chains directly for diagnostics.
Sourcepub fn to_font_manager(&self) -> FontManager<FontRef>
pub fn to_font_manager(&self) -> FontManager<FontRef>
Convert into a FontManager with all data populated.
Carries the registry forward so the resulting manager also
has the scout-on-demand path available.
Trait Implementations§
Source§impl Clone for FontContext
impl Clone for FontContext
Source§fn clone(&self) -> FontContext
fn clone(&self) -> FontContext
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for FontContext
impl RefUnwindSafe for FontContext
impl Send for FontContext
impl Sync for FontContext
impl Unpin for FontContext
impl UnsafeUnpin for FontContext
impl UnwindSafe for FontContext
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more