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: FontDataThe 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
impl FontFallbackChain
Sourcepub fn new(primary: FontData) -> Self
pub fn new(primary: FontData) -> Self
Creates a new fallback chain with the given primary font.
Sourcepub fn with_fallback(self, fallback: FontData) -> Self
pub fn with_fallback(self, fallback: FontData) -> Self
Adds a fallback font to the chain.
Sourcepub fn find_font_for_char(&self, c: char) -> &FontData
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).
Sourcepub fn check_coverage(&self, text: &str) -> GlyphCoverage
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.
Sourcepub fn segment_text(&self, text: &str) -> Vec<(String, &FontData)>
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
impl Clone for FontFallbackChain
Source§fn clone(&self) -> FontFallbackChain
fn clone(&self) -> FontFallbackChain
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for FontFallbackChain
impl RefUnwindSafe for FontFallbackChain
impl Send for FontFallbackChain
impl Sync for FontFallbackChain
impl Unpin for FontFallbackChain
impl UnwindSafe for FontFallbackChain
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§impl<T> Pointable for T
impl<T> Pointable for T
§impl<U, T> ToOwnedObj<U> for Twhere
U: FromObjRef<T>,
impl<U, T> ToOwnedObj<U> for Twhere
U: FromObjRef<T>,
§fn to_owned_obj(&self, data: FontData<'_>) -> U
fn to_owned_obj(&self, data: FontData<'_>) -> U
T, using the provided data to resolve any offsets.