FontFallbackChain

Struct FontFallbackChain 

Source
pub struct FontFallbackChain {
    primary: FontData,
    fallbacks: Vec<FontData>,
}
Expand description

A font fallback chain for handling mixed-script documents.

This struct manages a primary font and a list of fallback fonts. When rendering text, it automatically selects the appropriate font for each character based on glyph coverage.

§Example

let mut p = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
p.push("fonts/NotoSans-Regular.ttf");
let primary = FontData::load(&p, None).unwrap();
let mut q = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
q.push("fonts/SpaceMono-Regular.ttf");
let fallback = FontData::load(&q, None).unwrap();

let chain = FontFallbackChain::new(primary).with_fallback(fallback);

let segments = chain.segment_text("Hello 123!");
assert!(!segments.is_empty());

Fields§

§primary: FontData

The primary font to try first

§fallbacks: Vec<FontData>

List of fallback fonts to try if primary doesn’t have a character

Implementations§

Source§

impl FontFallbackChain

Source

pub fn new(primary: FontData) -> Self

Creates a new fallback chain with the given primary font.

Source

pub fn with_fallback(self, fallback: FontData) -> Self

Adds a fallback font to the chain.

Source

pub fn find_font_for_char(&self, c: char) -> &FontData

Finds the best font in the chain for the given character.

Returns a reference to the first font (starting with primary) that has a glyph for this character. If no font in the chain supports the character, returns the primary font (which will render the .notdef glyph).

Source

pub fn primary(&self) -> &FontData

Returns the primary font.

Source

pub fn fallbacks(&self) -> &[FontData]

Returns the list of fallback fonts.

Source

pub fn check_coverage(&self, text: &str) -> GlyphCoverage

Analyzes coverage across the entire fallback chain for the given text.

Returns statistics showing which characters are covered by any font in the chain and which are still missing.

Source

pub fn segment_text(&self, text: &str) -> Vec<(String, &FontData)>

Segments text into chunks where each chunk uses a single font from the chain.

This analyzes the text character by character, determining which font can render each character, and groups consecutive characters using the same font into segments.

§Returns

A vector of tuples (text_segment, font_data_ref) where each segment should be rendered with the corresponding font.

§Example
let mut p = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
p.push("fonts/NotoSans-Regular.ttf");
let primary = FontData::load(&p, None).unwrap();
let mut q = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
q.push("fonts/SpaceMono-Regular.ttf");
let fallback = FontData::load(&q, None).unwrap();
let chain = FontFallbackChain::new(primary).with_fallback(fallback);
let segments = chain.segment_text("Hello мир!");
assert!(!segments.is_empty());

Trait Implementations§

Source§

impl Clone for FontFallbackChain

Source§

fn clone(&self) -> FontFallbackChain

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for FontFallbackChain

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
§

impl<T> Finish for T

§

fn finish(self)

Does nothing but move self, equivalent to drop.
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
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

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

Initializes a with the given initializer. Read more
§

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

Dereferences the given pointer. Read more
§

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

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

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

impl<T> Same for T

Source§

type Output = T

Should always be Self
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
§

impl<U, T> ToOwnedObj<U> for T
where U: FromObjRef<T>,

§

fn to_owned_obj(&self, data: FontData<'_>) -> U

Convert this type into T, using the provided data to resolve any offsets.
§

impl<U, T> ToOwnedTable<U> for T
where U: FromTableRef<T>,

§

fn to_owned_table(&self) -> U

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

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V