### Compile C Example on Linux Source: https://github.com/fschutt/rust-fontconfig/blob/master/README.md Command to compile the C font matching example on Linux using gcc. Ensure the include and library paths are correct. ```bash gcc -I./include -L. -o font_example example.c -lrust_fontconfig ``` -------------------------------- ### Compile C Example on Windows Source: https://github.com/fschutt/rust-fontconfig/blob/master/README.md Command to compile the C font matching example on Windows using cl.exe. Ensure the include and library paths are correct. ```bash cl.exe /I./include /Fe:font_example.exe example.c rust_fontconfig.lib ``` -------------------------------- ### Compile C Example on macOS Source: https://github.com/fschutt/rust-fontconfig/blob/master/README.md Command to compile the C font matching example on macOS using clang. Ensure the include and library paths are correct. ```bash clang -I./include -L. -o font_example example.c -lrust_fontconfig ``` -------------------------------- ### Basic Font Query with rust-fontconfig Source: https://github.com/fschutt/rust-fontconfig/blob/master/README.md Demonstrates how to build a font cache, query for a font by name (e.g., 'Arial'), and retrieve its metadata and file path. This example requires the 'parsing' feature to be enabled. ```rust use rust_fontconfig::{FcFontCache, FcPattern}; fn main() { // Build the font cache (scans system fonts) let cache = FcFontCache::build(); // Query a font by name let mut trace = Vec::new(); let results = cache.query( &FcPattern { name: Some(String::from("Arial")), ..Default::default() }, &mut trace ); if let Some(font_match) = results { println!("Font match ID: {:?}", font_match.id); println!("Font unicode ranges: {:?}", font_match.unicode_ranges); // Get font metadata if let Some(meta) = cache.get_metadata_by_id(&font_match.id) { println!("Family: {:?}", meta.family); } // Get font file path if let Some(source) = cache.get_font_by_id(&font_match.id) { match source { rust_fontconfig::OwnedFontSource::Disk(path) => { println!("Path: {}", path.path); } rust_fontconfig::OwnedFontSource::Memory(font) => { println!("Memory font: {}", font.id); } } } } else { println!("No matching font found"); } } ``` -------------------------------- ### List Bold Fonts Matching a Pattern Source: https://github.com/fschutt/rust-fontconfig/blob/master/README.md List all fonts from the cache and filter them by properties, specifically selecting bold fonts. This example shows how to iterate through available fonts and apply filters. ```rust use rust_fontconfig::{FcFontCache, FcWeight}; fn main() { let cache = FcFontCache::build(); // List all fonts - filter by properties let bold_fonts: Vec<_> = cache.list().into_iter() .filter(|(pattern, _id)| { matches!(pattern.weight, FcWeight::Bold | FcWeight::ExtraBold) }) .collect(); println!("Found {} bold fonts:", bold_fonts.len()); for (pattern, id) in bold_fonts.iter().take(5) { println!(" {:?}: {:?}", id, pattern.name.as_ref().or(pattern.family.as_ref())); } } ``` -------------------------------- ### Minimal C Example for Font Matching Source: https://github.com/fschutt/rust-fontconfig/blob/master/README.md This C code demonstrates how to build a font cache, search for a specific font (Arial), retrieve its ID and path, and clean up resources. It requires the rust_fontconfig library. ```c #include #include "rust_fontconfig.h" int main() { // Build the font cache FcFontCache cache = fc_cache_build(); if (!cache) { fprintf(stderr, "Failed to build font cache\n"); return 1; } // Create a pattern to search for Arial FcPattern* pattern = fc_pattern_new(); fc_pattern_set_name(pattern, "Arial"); // Search for the font FcTraceMsg* trace = NULL; size_t trace_count = 0; FcFontMatch* match = fc_cache_query(cache, pattern, &trace, &trace_count); if (match) { char id_str[40]; fc_font_id_to_string(&match->id, id_str, sizeof(id_str)); printf("Found font! ID: %s\n", id_str); // Get the font path FcFontPath* font_path = fc_cache_get_font_path(cache, &match->id); if (font_path) { printf("Font path: %s (index: %zu)\n", font_path->path, font_path->font_index); fc_font_path_free(font_path); } fc_font_match_free(match); } else { printf("Font not found\n"); } // Clean up fc_pattern_free(pattern); if (trace) fc_trace_free(trace, trace_count); fc_cache_free(cache); return 0; } ``` -------------------------------- ### Character-by-Character Font Resolution Source: https://github.com/fschutt/rust-fontconfig/blob/master/README.md Use `resolve_text()` to get per-character font assignments for fine-grained control. This method returns the font ID and CSS source for each character. ```rust use rust_fontconfig::{FcFontCache, FcWeight, PatternMatch}; fn main() { let cache = FcFontCache::build(); let chain = cache.resolve_font_chain( &["sans-serif".to_string()], FcWeight::Normal, PatternMatch::False, PatternMatch::False, &mut Vec::new(), ); // Get font assignment for each character let text = "Hello 世界"; let resolved = chain.resolve_text(&cache, text); for (ch, font_info) in resolved { match font_info { Some((font_id, css_source)) => { let font_name = cache.get_metadata_by_id(&font_id) .and_then(|m| m.name.clone().or(m.family.clone())) .unwrap_or_default(); println!("'{}' -> {} (from CSS '{}')", ch, font_name, css_source); } None => println!("'{}' -> NO FONT FOUND", ch), } } } ``` -------------------------------- ### Build Rust Fontconfig Library with FFI Source: https://github.com/fschutt/rust-fontconfig/blob/master/README.md Build the rust-fontconfig library with FFI support enabled. This command generates the necessary libraries for using the Rust code from C/C++ applications. ```bash # Clone the repository git clone https://github.com/fschutt/rust-fontconfig.git cd rust-fontconfig # Build with FFI support cargo build --release --features ffi # The generated libraries will be in target/release ``` -------------------------------- ### Add rust-fontconfig to Cargo.toml Source: https://github.com/fschutt/rust-fontconfig/blob/master/README.md Include the rust-fontconfig crate in your project's dependencies. Enable the 'parsing' feature to parse font tables for accurate metadata and support for various font formats. ```toml [dependencies] rust-fontconfig = { version = "4.4", features = ["parsing"] } ``` -------------------------------- ### Resolve Font Chain and Query Text Source: https://github.com/fschutt/rust-fontconfig/blob/master/README.md Build a font fallback chain from CSS font-family and then query which fonts to use for specific text. This demonstrates a two-step process for font resolution. ```rust use rust_fontconfig::{FcFontCache, FcWeight, PatternMatch}; fn main() { let cache = FcFontCache::build(); // Step 1: Build font fallback chain (without text parameter) let mut trace = Vec::new(); let font_chain = cache.resolve_font_chain( &["Arial".to_string(), "sans-serif".to_string()], FcWeight::Normal, PatternMatch::DontCare, // italic PatternMatch::DontCare, // oblique &mut trace, ); println!("CSS fallback groups: {}", font_chain.css_fallbacks.len()); for group in &font_chain.css_fallbacks { println!(" CSS '{}' resolved to {} fonts", group.css_name, group.fonts.len()); } // Step 2: Query which fonts to use for specific text let text = "Hello 你好 Здравствуйте"; let font_runs = font_chain.query_for_text(&cache, text); println!("\nText '{}' split into {} font runs:", text, font_runs.len()); for run in &font_runs { println!(" '{}' -> font {:?}", run.text, run.font_id); } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.