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
impl FontFallbackChain
Sourcepub fn resolve_char(
&self,
cache: &FcFontCache,
ch: char,
) -> Option<(FontId, String)>
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
Sourcepub fn resolve_text(
&self,
cache: &FcFontCache,
text: &str,
) -> Vec<(char, Option<(FontId, String)>)>
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?
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
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}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}Sourcepub fn query_for_text(
&self,
cache: &FcFontCache,
text: &str,
) -> Vec<ResolvedFontRun>
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
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 moreSource§impl Debug for FontFallbackChain
impl Debug for FontFallbackChain
Source§impl PartialEq for FontFallbackChain
impl PartialEq for FontFallbackChain
impl Eq for FontFallbackChain
impl StructuralPartialEq for FontFallbackChain
Auto 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