Skip to main content

FontFallbackChain

Struct FontFallbackChain 

Source
pub struct FontFallbackChain {
    pub css_fallbacks: Vec<CssFallbackGroup>,
    pub unicode_fallbacks: Vec<FontMatch>,
    pub original_stack: Vec<String>,
}
Expand description

Resolved font fallback chain for a CSS font-family stack This represents the complete chain of fonts to use for rendering text

Fields§

§css_fallbacks: Vec<CssFallbackGroup>

CSS-based fallbacks: Each CSS font expanded to its system fallbacks Example: [“NotoSansJP” -> [Hiragino Sans, PingFang SC], “sans-serif” -> [Helvetica]]

§unicode_fallbacks: Vec<FontMatch>

Unicode-based fallbacks: Fonts added to cover missing Unicode ranges Only populated if css_fallbacks don’t cover all requested characters

§original_stack: Vec<String>

The original CSS font-family stack that was requested

Implementations§

Source§

impl FontFallbackChain

Source

pub fn resolve_char( &self, cache: &FcFontCache, ch: char, ) -> Option<(FontId, String)>

Resolve which font should be used for a specific character Returns (FontId, css_source_name) where css_source_name indicates which CSS font matched Returns None if no font in the chain can render this character

Source

pub fn resolve_text( &self, cache: &FcFontCache, text: &str, ) -> Vec<(char, Option<(FontId, String)>)>

Resolve all characters in a text string to their fonts Returns a vector of (character, FontId, css_source) tuples

Examples found in repository?
examples/unicode_aware_fonts.rs (line 54)
53fn print_resolution(cache: &FcFontCache, chain: &FontFallbackChain, text: &str) {
54    let resolved = chain.resolve_text(cache, text);
55
56    let mut current_font: Option<String> = None;
57    let mut segment = String::new();
58
59    for (ch, info) in &resolved {
60        let font_name = info.as_ref().and_then(|(id, _)| {
61            cache
62                .get_metadata_by_id(id)
63                .and_then(|m| m.name.clone().or(m.family.clone()))
64        });
65        if font_name != current_font {
66            if !segment.is_empty() {
67                println!(
68                    "  '{}' -> {}",
69                    segment,
70                    current_font.as_deref().unwrap_or("[NO FONT]")
71                );
72                segment.clear();
73            }
74            current_font = font_name;
75        }
76        segment.push(*ch);
77    }
78    if !segment.is_empty() {
79        println!(
80            "  '{}' -> {}",
81            segment,
82            current_font.as_deref().unwrap_or("[NO FONT]")
83        );
84    }
85}
More examples
Hide additional examples
examples/character_resolution.rs (line 53)
11fn main() {
12    let cache = FcFontCache::build();
13
14    // Create a font chain with typical web defaults
15    let families = vec!["system-ui".to_string(), "sans-serif".to_string()];
16
17    let chain = cache.resolve_font_chain(
18        &families,
19        FcWeight::Normal,
20        PatternMatch::False,
21        PatternMatch::False,
22        &mut Vec::new(),
23    );
24
25    // Test characters from different Unicode blocks
26    let test_chars = [
27        ('A', "Latin Capital Letter A"),
28        ('a', "Latin Small Letter A"),
29        ('0', "Digit Zero"),
30        ('€', "Euro Sign"),
31        ('→', "Rightwards Arrow"),
32        ('中', "CJK Ideograph - China"),
33        ('日', "CJK Ideograph - Sun/Day"),
34        ('あ', "Hiragana Letter A"),
35        ('ア', "Katakana Letter A"),
36        ('한', "Hangul Syllable Han"),
37        ('א', "Hebrew Letter Alef"),
38        ('ا', "Arabic Letter Alef"),
39        ('α', "Greek Small Letter Alpha"),
40        ('я', "Cyrillic Small Letter Ya"),
41        ('🙂', "Slightly Smiling Face"),
42        ('♠', "Black Spade Suit"),
43        ('∑', "N-ary Summation"),
44        ('∞', "Infinity"),
45    ];
46
47    println!("Character resolution results:\n");
48    println!("{:<6} {:<30} {:<40}", "Char", "Description", "Font");
49    println!("{}", "-".repeat(76));
50
51    for (ch, description) in &test_chars {
52        let text = ch.to_string();
53        let resolved = chain.resolve_text(&cache, &text);
54
55        let font_name = resolved
56            .first()
57            .and_then(|(_, info)| info.as_ref())
58            .and_then(|(id, _)| cache.get_metadata_by_id(id))
59            .and_then(|m| m.name.clone().or(m.family.clone()))
60            .unwrap_or_else(|| "⚠ NOT FOUND".to_string());
61
62        println!("{:<6} {:<30} {}", ch, description, font_name);
63    }
64
65    // Check specific font coverage
66    println!("\n\nArial coverage check:");
67    let arial_chain = cache.resolve_font_chain(
68        &["Arial".to_string()],
69        FcWeight::Normal,
70        PatternMatch::False,
71        PatternMatch::False,
72        &mut Vec::new(),
73    );
74
75    let arial_result = cache.query(
76        &rust_fontconfig::FcPattern {
77            family: Some("Arial".to_string()),
78            ..Default::default()
79        },
80        &mut Vec::new(),
81    );
82
83    if let Some(arial_match) = arial_result {
84        for ch in ['A', '中', '🙂', '→'] {
85            let resolved = arial_chain.resolve_text(&cache, &ch.to_string());
86            let in_arial = resolved
87                .first()
88                .and_then(|(_, info)| info.as_ref())
89                .map(|(id, _)| id == &arial_match.id)
90                .unwrap_or(false);
91
92            println!(
93                "  {} '{}' (U+{:04X})",
94                if in_arial { "✓" } else { "✗" },
95                ch,
96                ch as u32
97            );
98        }
99    } else {
100        println!("  Arial not found on this system");
101    }
102}
examples/integration_api.rs (line 56)
11fn main() {
12    println!("=== Font Integration Pipeline ===\n");
13
14    // ── Step 1: Build font cache ──
15    println!("Step 1: Build font cache");
16    let cache = FcFontCache::build();
17    println!("  {} fonts loaded\n", cache.list().len());
18
19    // ── Step 2: Resolve CSS font-family ──
20    let css_families = vec![
21        "Helvetica".to_string(),
22        "Arial".to_string(),
23        "sans-serif".to_string(),
24    ];
25    println!("Step 2: Resolve font-family: {:?}\n", css_families);
26
27    let chain = cache.resolve_font_chain(
28        &css_families,
29        FcWeight::Normal,
30        PatternMatch::False,
31        PatternMatch::False,
32        &mut Vec::new(),
33    );
34
35    for (i, group) in chain.css_fallbacks.iter().enumerate() {
36        print!("  [{}] '{}': {} fonts", i + 1, group.css_name, group.fonts.len());
37        if let Some(first) = group.fonts.first() {
38            if let Some(meta) = cache.get_metadata_by_id(&first.id) {
39                print!(
40                    " (first: {:?})",
41                    meta.name.as_ref().or(meta.family.as_ref())
42                );
43            }
44        }
45        println!();
46    }
47    println!(
48        "  + {} unicode fallback fonts\n",
49        chain.unicode_fallbacks.len()
50    );
51
52    // ── Step 3: Resolve text to font runs ──
53    let text = "Hello 世界! Привет мир";
54    println!("Step 3: Resolve text: '{}'\n", text);
55
56    let resolved = chain.resolve_text(&cache, text);
57
58    // Group by runs of same font
59    let mut runs: Vec<(String, Option<FontId>)> = Vec::new();
60    let mut current_text = String::new();
61    let mut current_id: Option<FontId> = None;
62
63    for (ch, info) in &resolved {
64        let this_id = info.as_ref().map(|(id, _)| *id);
65        if this_id != current_id {
66            if !current_text.is_empty() {
67                runs.push((current_text.clone(), current_id));
68                current_text.clear();
69            }
70            current_id = this_id;
71        }
72        current_text.push(*ch);
73    }
74    if !current_text.is_empty() {
75        runs.push((current_text, current_id));
76    }
77
78    println!("  Font runs:");
79    for (run_text, font_id) in &runs {
80        let name = font_id
81            .as_ref()
82            .and_then(|id| cache.get_metadata_by_id(id))
83            .and_then(|m| m.name.clone().or(m.family.clone()))
84            .unwrap_or_else(|| "[NO FONT]".into());
85        println!("    '{}' -> {}", run_text, name);
86    }
87
88    // ── Step 4: Load font bytes ──
89    let unique_fonts: std::collections::HashSet<_> =
90        runs.iter().filter_map(|(_, id)| *id).collect();
91
92    println!(
93        "\nStep 4: Load fonts ({} unique needed)\n",
94        unique_fonts.len()
95    );
96    for font_id in &unique_fonts {
97        if let Some(meta) = cache.get_metadata_by_id(font_id) {
98            let name = meta
99                .name
100                .as_ref()
101                .or(meta.family.as_ref())
102                .map(|s| s.as_str())
103                .unwrap_or("?");
104            if let Some(source) = cache.get_font_by_id(font_id) {
105                match source {
106                    rust_fontconfig::FontSource::Disk(path) => {
107                        println!("  {} -> {}", name, path.path);
108                    }
109                    rust_fontconfig::FontSource::Memory(font) => {
110                        println!("  {} -> memory (id: {})", name, font.id);
111                    }
112                }
113            }
114        }
115    }
116
117    println!("\nPipeline summary:");
118    println!("  1. FcFontCache::build()       — once at startup");
119    println!("  2. cache.resolve_font_chain() — per CSS font-family");
120    println!("  3. chain.resolve_text()       — per text run");
121    println!("  4. cache.get_font_by_id()     — load bytes for shaping");
122}
Source

pub fn query_for_text( &self, cache: &FcFontCache, text: &str, ) -> Vec<ResolvedFontRun>

Query which fonts should be used for a text string, grouped by font Returns runs of consecutive characters that use the same font This is the main API for text shaping - call this to get font runs, then shape each run

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

impl PartialEq for FontFallbackChain

Source§

fn eq(&self, other: &FontFallbackChain) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for FontFallbackChain

Source§

impl StructuralPartialEq for FontFallbackChain

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.