Skip to main content

FontManager

Struct FontManager 

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

Manages font discovery, loading, shaping, and metrics.

Implementations§

Source§

impl FontManager

Source

pub fn new() -> Self

Create a new FontManager and load system fonts.

Bundled fonts (Carlito, Caladea, Liberation) are loaded as fallbacks. System fonts are discovered when the system-fonts feature is enabled.

Source

pub fn new_deterministic() -> Result<Self>

Create a font manager that loads bundled fonts without discovering system fonts.

This mode makes font resolution reproducible across machines.

Source

pub fn load_additional_fonts(&mut self, font_files: &[FontFile])

Load additional font files (user-provided or extracted from DOCX).

These fonts are loaded AFTER system fonts, so they take the highest priority in font resolution (fontdb returns the last-loaded match).

Source

pub fn new_with_fonts(fonts: Vec<(String, Vec<u8>)>) -> Self

Create a FontManager with user-provided fonts (no system font loading).

Each entry is (family_name, font_bytes). This is useful in environments where system fonts are not available, such as WASM.

Source

pub fn resolve_font_for_text( &mut self, family: Option<&str>, bold: bool, italic: bool, text: &str, ) -> Result<FontId>

Resolve a font for text, falling back on glyph coverage.

resolve_font picks by family name alone. That is enough for Latin text, but a run asking for a Chinese family on a machine without it falls down the name chain and lands on a Latin font, which has no CJK glyphs, so every character renders as a missing-glyph box. Name matching cannot detect that, because the font it chose exists and is perfectly valid, it simply cannot draw this text.

So the resolved font is checked against the text, and when a character is missing another font that can draw it is looked for.

This is per run rather than per character: the font that covers the first missing character is used for the whole run. Text that mixes scripts inside one run is therefore still imperfect, but it is a large improvement on drawing boxes.

Source

pub fn resolve_font( &mut self, family: Option<&str>, bold: bool, italic: bool, ) -> Result<FontId>

Resolve a font by family name, bold, and italic flags. Returns a FontId. Uses fallback chain if the requested font is not found.

Source

pub fn metrics(&self, font_id: FontId, size_pt: f64) -> Result<FontMetrics>

Get font metrics at a given size in points.

Source

pub fn shape_text( &self, font_id: FontId, text: &str, size_pt: f64, ) -> Result<ShapedText>

Shape a text string using HarfRust. Returns glyph IDs and advances.

Source

pub fn font_data(&self, font_id: FontId) -> Result<FontData>

Get font data for PDF embedding.

Source

pub fn all_font_data(&self) -> Vec<FontData>

Get all used font data.

Trait Implementations§

Source§

impl Default for FontManager

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.