How fonttools/fontspector works
Project Type and Purpose
• Fontspector is a static analysis tool for font projects.
• It’s implemented primarily in Rust as a command-line application, with OPTIONAL Python integration for running existing fontbakery checks, and a WASM-based web UI.
• It’s architected as a set of library crates, a CLI front end, plugins (profiles), a Python binding, and a WASM web front end.
File Structure
Top-level crates and directories:
• fontbakery-bridge (Python-to-Rust bridge)
• fontspector-checkapi (core check interface + utils)
• fontspector-checkhelper (procedural macros for checks)
• fontspector-cli (actual CLI executable)
• fontspector-py (Python module exposing the Rust runner)
• fontspector-web (WASM web UI + static assets)
• profile-* (built-in and plugin-style profiles that define sets of checks)
• templates (HTML/Markdown templates used for reporting)
• scripts (utilities to build profiles, extract data, port checks)
• Cargo.toml workspace ties everything together.
Key patterns:
• Modular crates communicating via a shared API (fontspector-checkapi).
• Plugin architecture: external profiles compile to dynamic libraries and are loaded at runtime by the CLI.
• Multi-language: Rust core, Python extension, WASM + TypeScript front end.
README for Additional Insights
• Single binary bundles built-in checks, profiles, and templates.
• Python support via --features python + --use-python flag.
• WASM front end built with wasm-pack + npm.
• Plugin profiles built with cargo + cargo-cp-artifact and loaded dynamically.
Main Components & Relationships
a. Components:
1. Core API: fontspector-checkapi + fontspector-checkhelper
2. CLI Executable: fontspector-cli
3. Built-in Profiles: profile-googlefonts, profile-opentype, profile-universal, etc.
4. Plugin Profiles: profile-microsoft, profile-adobe, profile-testplugin, loaded at runtime.
5. Python Bridge: fontbakery-bridge + fontspector-py
6. Web Front End: fontspector-web (WASM + TS + static assets)
7. Templates: HTML/Markdown under templates/
8. Scripts: helpers under scripts/
9. Build & CI: GitHub Actions workflows, Cargo workspace, wasm-pack, npm.
b. Relationships & Data Flows:
• fontspector-cli → depends on → fontspector-checkapi & fontspector-checkhelper.
• fontspector-cli → loads → built-in profiles (Rust crates) at compile time.
• fontspector-cli → dynamically loads → plugin profiles (.fontspectorplugin) at runtime.
• fontspector-cli → optionally spawns → Python interpreter via fontbakery-bridge when --use-python is set.
• fontspector-cli → uses → templates to render reports (HTML/Markdown).
• fontspector-py → wraps → fontspector-checkapi to expose to Python/pytest.
• fontspector-web → compiles --> WASM that embeds fontspector-checkapi, executes in browser, renders via static assets.
• scripts → read/write → project files to build profiles or data.
c. Architectural Patterns & Principles:
• Modular monorepo: each crate encapsulates a concern.
• Plugin architecture: dynamic loading for extensibility.
• Language bridging: Python integration via a Rust-based bridge.
• Dual front ends: CLI and web (WASM).
• Template-driven reporting: separation of logic from presentation.
d. Key Technologies & Frameworks:
• Rust + Cargo workspaces
• Procedural macros (checkhelper)
• Python FFI (maturin, PyO3)
• WASM (wasm-pack) + TypeScript + npm + Webpack
• GitHub Actions (rust.yml, pages.yml)
• Protobuf (for some profile data)
• Jinja/Markdown for reports