Crate rust_fontconfig

Source
Expand description

§rust-fontconfig

Pure-Rust rewrite of the Linux fontconfig library (no system dependencies) - using allsorts as a font parser to support .woff, .woff2, .ttc, .otf and .ttf

NOTE: Also works on Windows, macOS and WASM - without external dependencies!

§Usage

§Basic Font Query

use rust_fontconfig::{FcFontCache, FcPattern};

fn main() {
    // Build the font cache
    let cache = FcFontCache::build();

    // Query a font by name
    let results = cache.query(
        &FcPattern {
            name: Some(String::from("Arial")),
            ..Default::default()
        },
        &mut Vec::new() // Trace messages container
    );

    if let Some(font_match) = results {
        println!("Font match ID: {:?}", font_match.id);
        println!("Font unicode ranges: {:?}", font_match.unicode_ranges);
        println!("Font fallbacks: {:?}", font_match.fallbacks.len());
    } else {
        println!("No matching font found");
    }
}

§Find All Monospace Fonts

use rust_fontconfig::{FcFontCache, FcPattern, PatternMatch};

fn main() {
    let cache = FcFontCache::build();
    let fonts = cache.query_all(
        &FcPattern {
            monospace: PatternMatch::True,
            ..Default::default()
        },
        &mut Vec::new()
    );

    println!("Found {} monospace fonts:", fonts.len());
    for font in fonts {
        println!("Font ID: {:?}", font.id);
    }
}

§Font Matching for Multilingual Text

use rust_fontconfig::{FcFontCache, FcPattern};

fn main() {
    let cache = FcFontCache::build();
    let text = "Hello 你好 Здравствуйте";

    // Find fonts that can render the mixed-script text
    let mut trace = Vec::new();
    let matched_fonts = cache.query_for_text(
        &FcPattern::default(),
        text,
        &mut trace
    );

    println!("Found {} fonts for the multilingual text", matched_fonts.len());
    for font in matched_fonts {
        println!("Font ID: {:?}", font.id);
    }
}

Structs§

FcFont
In-memory font data
FcFontCache
Font cache, initialized at startup
FcFontMetadata
Font metadata from the OS/2 table
FcFontPath
Path to a font file
FcPattern
Font pattern for matching
FontId
UUID to identify a font (collections are broken up into separate fonts)
FontMatch
Font match result with UUID
FontMatchNoFallback
Font match result with UUID (without fallback)
TraceMsg
Trace message for debugging font matching
UnicodeRange
Unicode range representation for font matching

Enums§

FcStretch
CSS font-stretch values
FcWeight
Font weight values as defined in CSS specification
FontSource
Font source enum to represent either disk or memory fonts
MatchReason
Reason for font matching failure or success
PatternMatch
Whether a field is required to match (yes / no / don’t care)
TraceLevel
Log levels for trace messages