getfont/
getfont.rs

1use rust_fontconfig::{FcFontCache, FcPattern, FcWeight};
2use std::time::Instant;
3
4fn main() {
5    let start = Instant::now();
6    let cache = FcFontCache::build();
7    let build_time = start.elapsed();
8
9    println!("✓ Cache built with {} fonts in {:?}", cache.list().len(), build_time);
10    println!();
11
12    // Test various font queries to showcase fuzzy matching
13    let test_queries = vec![
14        ("Arial", FcWeight::Normal, "Common sans-serif font"),
15        ("NotoSansJP", FcWeight::Normal, "Japanese font (fuzzy match)"),
16        ("Helvetica", FcWeight::Bold, "Bold variant"),
17        ("DejaVu Sans", FcWeight::Normal, "Font with spaces"),
18        ("Courier", FcWeight::Normal, "Monospace font"),
19    ];
20
21    for (font_name, weight, description) in test_queries {
22        println!("Searching for: '{}' ({})", font_name, description);
23        
24        let query_start = Instant::now();
25        let result = cache.query(
26            &FcPattern {
27                name: Some(font_name.to_string()),
28                weight,
29                ..Default::default()
30            },
31            &mut Vec::new(),
32        );
33        let query_time = query_start.elapsed();
34
35        match result {
36            Some(font_match) => {
37                if let Some(pattern) = cache.get_metadata_by_id(&font_match.id) {
38                    let name = pattern.name.as_ref().or(pattern.family.as_ref())
39                        .map(|s| s.as_str()).unwrap_or("<unknown>");
40                    println!("  ✓ Found: {} (query time: {:?})", name, query_time);
41                    println!("    - Weight: {:?}", pattern.weight);
42                    println!("    - Unicode ranges: {}", font_match.unicode_ranges.len());
43                    println!("    - Fallbacks: {}", font_match.fallbacks.len());
44                } else {
45                    println!("  ✓ Found font ID: {} (query time: {:?})", font_match.id, query_time);
46                }
47            }
48            None => {
49                println!("  ✗ Not found (query time: {:?})", query_time);
50            }
51        }
52        println!();
53    }
54}