Skip to main content

Crate rust_fontconfig

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);
    } else {
        println!("No matching font found");
    }
}

§Resolve Font Chain and Query for Text

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

fn main() {
    let cache = FcFontCache::build();

    // Build font fallback chain (without text parameter)
    let font_chain = cache.resolve_font_chain(
        &["Arial".to_string(), "sans-serif".to_string()],
        FcWeight::Normal,
        PatternMatch::DontCare,
        PatternMatch::DontCare,
        &mut Vec::new(),
    );

    // Query which fonts to use for specific text
    let text = "Hello 你好 Здравствуйте";
    let font_runs = font_chain.query_for_text(&cache, text);

    println!("Text split into {} font runs:", font_runs.len());
    for run in font_runs {
        println!("  '{}' -> font {:?}", run.text, run.font_id);
    }
}

Modules§

config
OS-specific font configuration: directories, common families, and font file constants.
utils

Structs§

CssFallbackGroup
A group of fonts that are fallbacks for a single CSS font-family name
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
FcFontRenderConfig
Per-font rendering configuration from system font config (Linux fonts.conf).
FcPattern
Font pattern for matching
FontFallbackChain
Resolved font fallback chain for a CSS font-family stack This represents the complete chain of fonts to use for rendering text
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)
NamedFont
A named font to be added to the font cache from memory. This is the primary way to supply custom fonts to the application.
ResolvedFontRun
A run of text that uses the same font Returned by FontFallbackChain::query_for_text()
TraceMsg
Trace message for debugging font matching
UnicodeRange
Unicode range representation for font matching

Enums§

FcHintStyle
Hinting style for font rendering.
FcLcdFilter
LCD filter mode for subpixel rendering.
FcRgba
Subpixel rendering order.
FcStretch
CSS font-stretch values
FcWeight
Font weight values as defined in CSS specification
FontBytes
A handle to font bytes returned by FcFontCache::get_font_bytes.
FontSource
Font source enum to represent either disk or memory fonts
MatchReason
Reason for font matching failure or success
OperatingSystem
Operating system type for generic font family resolution
PatternMatch
Whether a field is required to match (yes / no / don’t care)
TraceLevel
Log levels for trace messages

Constants§

DEFAULT_UNICODE_FALLBACK_SCRIPTS
The default set of Unicode-block fallback scripts that FcFontCache::resolve_font_chain pulls in when no explicit scripts_hint is supplied.

Functions§

expand_font_families
Expand a CSS font-family stack with generic families resolved to OS-specific fonts Prioritizes fonts based on Unicode range coverage Example: [“Arial”, “sans-serif”] on macOS with CJK ranges -> [“Arial”, “PingFang SC”, “Hiragino Sans”, …]
has_arabic_ranges
Check if any range covers the Arabic block
has_cjk_ranges
Check if any range covers CJK Unified Ideographs, Hiragana, Katakana, or Hangul
has_cyrillic_ranges
Check if any range covers the Cyrillic block
has_hebrew_ranges
Check if any range covers the Hebrew block
has_thai_ranges
Check if any range covers the Thai block