flash-font 0.2.0

Cache fonts in sqlite
Documentation

flash-font

License: MIT/Apache-2.0 Cargo Documentation

A high-performance Rust library for caching font metadata in a SQLite database. Designed for quick scanning of font directories and fast retrieval of font paths by their family names.

Features

  • Blazing Fast Scanning: Uses walkdir for parallel directory traversal.
  • Efficient Metadata Extraction: Utilizes memory-mapped files (memmap2) and ttf-parser for high-performance font analysis.
  • Smart Synchronization: Only parses new or modified font files. Stale database entries are automatically cleaned up.
  • Database Backend: Powered by SQLite and Diesel for reliable, indexed storage.

Usage

Add this to your Cargo.toml:

[dependencies]

flash-font = "0.1"

camino = "1.2"

Example: Updating the Database and Searching

use flash_font::{update_font_database, select_font_by_name};
use camino::Utf8Path;

fn main() -> anyhow::Result<()> {
    let font_dir = Utf8Path::new("C:/Path/To/Fonts");
    let db_url = "fonts.db";

    // Scan directory and update the database
    let new_fonts_count = update_font_database(font_dir, db_url)?;
    println!("Added {} new fonts to the database.", new_fonts_count);

    // Search for a font path by its family name
    let font_paths = select_font_by_name("Arial", db_url)?;
    for path in font_paths {
        println!("Found Arial at: {}", path);
    }

    Ok(())
}

Architecture

  1. Scanner: Recursively scans the target directory for supported font files.
  2. Cleaner: Compares disk files with database records, removing orphans and identifying new files.
  3. Parser: Extracts font family names in parallel using multiple threads and memory mapping.
  4. Database: Stores mappings between file paths and family names in a normalized SQLite schema.

License

MIT OR Apache-2.0