2 releases

new 0.1.1 Feb 24, 2025
0.1.0 Feb 24, 2025

#608 in Filesystem


Used in egui_zhcn_fonts

MIT/Apache

8KB
89 lines

findfont-rs

Crates.io Docs.rs

A cross-platform Rust library to find font files in system font directories.

Features

  • Multi-platform support (Linux/macOS/Windows)
  • Searches standard system font directories
  • Supports common font formats: TTF, TTC, OTF
  • Automatic font variant detection (Light, Medium)
  • Simple and intuitive API

Installation

Add to your Cargo.toml:

[dependencies]
findfont = "0.1"

Usage

Basic usage:

use findfont::find;

fn main() {
    if let Some(path) = find("Arial") {
        println!("Found font at: {}", path.display());
    } else {
        println!("Font not found");
    }
}

With error handling:

use findfont::find;

fn load_font(font_name: &str) -> std::io::Result<Vec<u8>> {
    find(font_name)
        .map(std::fs::read)
        .unwrap_or_else(|| Err(std::io::Error::new(
            std::io::ErrorKind::NotFound,
            "Font not found"
        )))
}

Supported Platforms

Linux

  • ~/.fonts/
  • ~/.local/share/fonts/
  • /usr/local/share/fonts/
  • /usr/share/fonts/
  • XDG_DATA_HOME and XDG_DATA_DIRS locations

macOS

  • ~/Library/Fonts/
  • /Library/Fonts/
  • /System/Library/Fonts/
  • System supplemental fonts

Windows

  • %windir%\Fonts
  • %LOCALAPPDATA%\Microsoft\Windows\Fonts

Search Logic

  1. Checks for exact filename matches with extensions in this order:
    • .ttf
    • .ttc
    • .otf
  2. If no exact match found, checks for common variants:
    • {font_name} Light.{ext}
    • {font_name} Medium.{ext}
  3. Returns first match found in system font directories

Note: Search is case-sensitive and uses platform-specific path separators

Limitations

  • Does not parse font files to verify actual font names
  • Limited variant detection (only Light/Medium)
  • First match in search order is returned
  • Doesn't handle font collections (.ttc) differently

Contributing

Contributions are welcome! Please open an issue or PR for:

  • Additional font variants
  • Improved search algorithms
  • More comprehensive test coverage
  • Platform-specific enhancements

License

MIT/Apache

No runtime deps