ttf2woff2
A Pure Rust library and CLI for compressing TTF fonts to WOFF2 format.
Features
- Pure Rust - No C/C++ or Python dependencies
- glyf/loca transformation - Achieves compression comparable to Google's woff2
- 100% glyph fidelity - All glyph shapes are preserved exactly
- Compatible with fonttools output
CLI Usage
$ cargo install ttf2woff2
$ ttf2woff2 font.ttf # Output: font.woff2
$ ttf2woff2 font.ttf -o output.woff2 # Custom output path
$ ttf2woff2 font.ttf -q 5 # Lower quality (faster, larger)
Library Usage
Add to your Cargo.toml with default-features = false to exclude the CLI.
[]
= { = "0.10", = false }
use ;
let ttf_data = read?;
let woff2_data = encode?;
write?;
Performance
Benchmarks on NotoSansJP-Medium (17,808 glyphs, 5,729,332 bytes) on Apple M4 Pro:
$ hyperfine --warmup 3 --runs 5 \
'./target/release/ttf2woff2 tests/fixtures/NotoSansJP-Medium.ttf -o /tmp/noto-q11-ttf2woff2.woff2 -q 11' \
'./target/release/ttf2woff2 tests/fixtures/NotoSansJP-Medium.ttf -o /tmp/noto-q9-ttf2woff2.woff2 -q 9' \
'uv run --with fonttools --with brotli python -c "from fontTools.ttLib import TTFont; f=TTFont(\"tests/fixtures/NotoSansJP-Medium.ttf\"); f.flavor=\"woff2\"; f.save(\"/tmp/noto-fonttools.woff2\")"'
| Implementation | Brotli Quality | Time (s) | Output Size (bytes) |
|---|---|---|---|
| Rust | 11 | 3.10 | 2,322,432 |
| Rust | 9 | 0.33 | 2,424,432 |
| Python fonttools | 11 | 9.49 | 2,322,836 |
Validation
Tests generate WOFF2 files and validate against fonttools:
$ cargo test
Manual validation (need uv installed):
$ uv run scripts/validate.py <font.ttf> <font.woff2>
Regenerate pre-generated fonttools output for faster tests:
$ uv run scripts/generate_golden.py
License
- The Noto Sans Japanese font in tests/fixtures/ is licensed under OFL.
- The Recursive font in tests/fixtures/ is licensed under OFL.
- The WarpnineSans font in tests/fixtures/ is licensed under OFL.
- Everything else is dual-licensed under MIT or Apache-2.0.
Alternatives
If you need byte-for-byte compatibility with Google's woff2 converter, decompression support, or WOFF1 support, consider these alternatives:
- woofwoof - Wraps Google's C++ woff2 library with pure Rust brotli. Supports both compression and decompression.
- bodoni/woff - Wraps Google's C++ woff2 and C brotli. Supports WOFF1 and WOFF2.
Acknowledgments
This project started as an FFI wrapper around Google's woff2 and brotli C/C++ libraries, then evolved into a pure Rust implementation (v0.10.0) with assistance from AI coding assistants (Claude Code, Codex, and Amp). While the code has been tested and validated against fonttools, users should verify output for production use.