ttf-rs
A comprehensive Rust library for reading, writing, and operating on TTF (TrueType Font) files.
Why ttf-rs?
Pure Rust Implementation: Unlike many font libraries that rely on C bindings, ttf-rs is written entirely in Rust, giving you memory safety, thread safety, and easy cross-compilation without external dependencies.
Read AND Write: Most TTF parsers only read fonts. ttf-rs supports both reading and writing, enabling you to modify font files, create custom fonts, and build font tooling entirely in Rust.
Production Ready: With comprehensive table support (head, maxp, cmap, name, hhea, hmtx, glyf, loca, post, OS/2), robust error handling, and full test coverage, ttf-rs is suitable for production applications like font converters, web font optimizers, and custom text renderers.
Flexible: Whether you're building a font inspector, a subset generator for web fonts, a custom text layout engine, or font modification tools, ttf-rs provides the building blocks you need.
Features
Reading TTF Files
- Parse TTF files - Read and extract information from TrueType font files
- Comprehensive table support - Support for essential TTF tables:
head- Font headermaxp- Maximum profilecmap- Character to glyph mapping (formats 0 and 4)name- Naming tablehhea- Horizontal headerhmtx- Horizontal metricsglyf- Glyph data (simple and composite glyphs)loca- Index to locationpost- PostScript informationOS/2- OS/2 and Windows metrics
- Character mapping - Map characters to glyph indices
- Font metrics - Access font metrics like ascent, descent, line height
- Glyph information - Read glyph outline data (simple and composite glyphs)
Writing TTF Files
- Save fonts - Write modified fonts back to disk
- Font serialization - Convert font structures to raw bytes
- Table modification - Modify and update font table data
- Checksum calculation - Proper TTF checksums for tables
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
Usage
Basic Font Loading
use Font;
// Load a font from a file
let font = load?;
// Get basic font metrics
let head = font.head_table?;
println!;
println!;
// Get glyph count
let maxp = font.maxp_table?;
println!;
Character to Glyph Mapping
use Font;
let font = load?;
// Map a character to its glyph index
let glyph_index = font.char_to_glyph?;
println!;
// Get horizontal metrics for a glyph
let hmtx = font.hmtx_table?;
let = hmtx.get_horizontal_metrics;
println!;
Inspecting Glyphs
use Font;
let font = load?;
let glyf = font.glyf_table?;
// Get a glyph by index
if let Some = glyf.get_glyph
Font Metrics for Text Layout
use Font;
let font = load?;
// Get vertical metrics for line spacing
let hhea = font.hhea_table?;
let line_gap = hhea.line_gap;
let line_height = hhea.get_line_height;
println!;
// Get OS/2 metrics for more precise spacing
if let Some = font.os2_table
Saving Modified Fonts
use Font;
let font = load?;
// After any modifications, save the font
font.save?;
// Or get the raw bytes
let bytes = font.to_bytes?;
write?;
Examples
The library includes comprehensive examples demonstrating real-world usage:
# Basic font information and inspection
# Character to glyph mapping demonstration
# Detailed glyph information (simple and composite)
# Glyph metrics for text layout
# Comprehensive font metrics display
# Table inspector - list all font tables
# Save and round-trip a font file
# Modify font properties
# Complete feature demonstration
# Interactive demo
Example: Font Metrics Calculator
use Font;
Example: Character Mapping
use Font;
Example: Font Inspector
use Font;
Project Structure
src/
├── lib.rs # Library entry point
├── error.rs # Error types (using thiserror)
├── font.rs # Main Font struct with high-level API
├── stream.rs # Binary reading/writing utilities
└── tables/ # TTF table parsers
├── mod.rs # Table traits and records
├── head.rs # Font header
├── maxp.rs # Maximum profile
├── cmap.rs # Character mapping
├── name.rs # Naming table
├── hhea.rs # Horizontal header
├── hmtx.rs # Horizontal metrics
├── glyf.rs # Glyph data
├── loca.rs # Index to location
├── post.rs # PostScript info
└── os2.rs # OS/2 & Windows metrics
Implementation Status
Currently Supported
- ✅ Reading TTF files
- ✅ Writing TTF files
- ✅ Parsing all essential tables
- ✅ Character to glyph mapping (cmap formats 0, 4)
- ✅ Font metrics extraction
- ✅ Glyph outline data (simple and composite)
- ✅ Binary data reading/writing utilities
- ✅ Table checksum calculation
- ✅ Font serialization and saving
Planned Features
- ⏳ Font modification API
- ⏳ Subset font creation
- ⏳ Glyph outline rendering
- ⏳ Additional cmap formats (6, 12)
- ⏳ Variable fonts (gvar, fvar, avar tables)
- ⏳ TrueType instructions (fpgm, prep, cvt)
- ⏳ Color fonts (COLR, CPAL tables)
API Design
The library follows these principles:
- Safe by default - Uses Rust's type system for memory safety
- Zero-copy where possible - References into original data when feasible
- Explicit error handling - Uses
Resulttypes for all fallible operations - Big-endian aware - Automatically handles TTF's big-endian byte order
- Flexible API - Both high-level convenience methods and low-level access
Testing
Run the test suite:
Build and check all examples:
License
MIT OR Apache-2.0
Contributing
Contributions are welcome! Areas of particular interest:
- Additional table formats
- Font modification utilities
- Subset creation
- Benchmarking and optimization
- Documentation improvements
References
Acknowledgments
This library was inspired by:
- freetype - The gold standard for font rendering
- fonttools - Python font library
- ttf-parser - Another Rust TTF parser