2 stable releases

new 1.0.1 May 8, 2025
1.0.0 May 7, 2025

#420 in Procedural macros

36 downloads per month
Used in 7 crates (3 directly)

MIT/Apache

11KB
171 lines

Checks in fontspector are made up of two parts: the implementation, which is a Rust function, and metadata: the check's ID, rationale, proposal URL(s) and so on. This crate provides a procedural macro to help associate the metadata with the implementation.

To write a fontspector check, you need to implement a function that takes a Testable and a Context as arguments (for checks which operate on a single font) or a TestableCollection and a Context. You then decorate the function with the #[check(...)] macro, passing the metadata as arguments. The macro will generate the necessary code to associate the metadata with the implementation.

A function which takes a TestableCollection should have the argument implementation = "all" in the #[check(...)] attribute. This will


Fontspector

Fontspector is a command-line tool for checking the quality of font projects. It is a Rust port of fontbakery, and is currently at an early alpha stage.

Components

Fontspector is made up of multiple crates:

  • fontbakery-bridge: Allows Python fontbakery checks to run inside fontspector
  • fontspector-checkapi: Defines the API and utility functions for check implementations
  • fontspector-checkhelper: Procedural macros to facilitate check implementations
  • fontspector-cli: The main fontspector executable
  • fontspector-py: A Python module exposing fontspector (for which see below)
  • fontspector-web: A WASM implementation of fontspector (for which see below)
  • profile-testplugin: An example of a runtime-loadable test profile
  • profile-googlefonts, profile-opentype, profile-universal: Built in profiles and their check implementations
  • profile-microsoft, profile-adobe, ...: Additional profiles which are loaded at runtime (see below)

Running the CLI tool

You have two options for running the CLI tool - you can download our binaries, or you can compile your own.

Official release binaries will be available from the GitHub releases page once Fontspector 1.0 is released. In the meantime, if you want to try the latest command line binaries now, you can look in the Actions page.

Similarly, once Fontspector 1.0 is released, you can build it from source with cargo install fontspector. (If you don't have a Rust compiler installed, you can use brew install rustup on macOS Homebrew or rustup to install one.)

Until then, you can build and install from source by either cloning this repository and running cargo build --release (which will place a fontspector binary in the target/release subdirectory), or by running cargo install https://github.com/fonttools/fontspector/ (which will compile and then install the fontspector binary onto your system). On macOS you'll need to pre-install the protobuf.dev package, such as with Homebrew like this:

brew install protobuf
cargo build --release;
./target/release/fontspector ~/font.ttf;

The single fontspector binary contains all the built-in checks, profiles, and HTML/Markdown templates. As mentioned below, some profiles require additional plugin binaries.

By default, fontspector CLI is built without Python support. If you want to run fontbakery checks inside fontspector, build with cargo build --release --features python. You can then use the --use-python flag at runtime to cause checks registered with Fontbakery to be run in Fontspector if no Rust implementation is available.

Running the test suite

We export the Fontspector check runner to a Python module, and then use pytest to run (a modified version of) the fontbakery test suite. To do this:

pip3 install -U maturin
cd fontspector-py
python3 -m venv venv ; . venv/bin/activate
pip install maturin
maturin develop
pytest

Building the web version

Fontspector also has a WASM-based web version at https://fonttools.github.io/fontspector/

It is built and deployed from Github Actions, but should you need to rebuild this manually for development, run:

cd fontspector-web
wasm-pack build
cd www; npm install; npm run build

The results appear in ../docs/.

Building plugin profiles

Some profiles such as the Microsoft profile require additional tests in Rust to be registered with Fontspector. This is done through plugins, which are dynamic libraries containing Rust code which get loaded at runtime. The easiest way to build these profiles is to use cargo-cp-artifact, a Javascript utility. To do this:

npm install
rpm run build-microsoft # build-adobe, build-test...

This will produce a file called microsoft.fontspectorplugin; to use this, run

fontspector \
    --plugins microsoft.fontspectorplugin \ # This loads the code
    --profile microsoft \                   # This uses the profile defined in the plugin
    MyFont.ttf

Dependencies

~0.5–1MB
~21K SLoC