Skip to main content

FontContext

Struct FontContext 

Source
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: FcFontCache

The 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

Source

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.

Source

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).

Source

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.

Source

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.

Source

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

Source§

fn clone(&self) -> FontContext

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for FontContext

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.