# font-parse
SFNT/OTF/TrueType/WOFF/WOFF2 binary font parser: offset tables, character
maps, glyph outlines, variable fonts.
**Layer:** L0: leaf · **Estate deps:** zero (by mandate) · **Core deps:**
`font-types` (scalar types) + `thiserror` · `no_std`-capable (`alloc` only;
container decoding behind the `woff`/`woff2` features).
## What's here
| `font` | [`FontRef`]: zero-copy view over `&[u8]` (SFNT offset table + table directory, lifetime-bound table slices), [`OwnedFont`] (WOFF/WOFF2/collection decoding into a validated buffer), [`FontCollection`] (TTC), [`VariableFont`] (fvar/avar/gvar) |
| `tables` | typed views: `head`, `hhea`, `hmtx`, `maxp`, `cmap` (formats 0/4/6/12), `loca`, `glyf` (simple + composite outlines), `name`, `OS/2`, `post`, `fvar`/`avar`, `gvar`, `GSUB`/`GPOS` structure, `stat`, `DSIG` |
| `woff` | WOFF 1.0: per-table zlib (feature `woff`) |
| `woff2` | WOFF 2.0: Brotli stream + glyf/loca/hmtx transform reversal, checksum recomputation, `head.flags` bit 11 (feature `woff2`) |
| `reader` | bounds-checked big-endian read substrate (`TruncatedTable` on every shortfall) |
| `error` | [`ParseError`]: the single typed, exhaustive error |
[`FontRef`]: https://docs.rs/font-parse/latest/font_parse/struct.FontRef.html
[`OwnedFont`]: https://docs.rs/font-parse/latest/font_parse/struct.OwnedFont.html
[`FontCollection`]: https://docs.rs/font-parse/latest/font_parse/struct.FontCollection.html
[`VariableFont`]: https://docs.rs/font-parse/latest/font_parse/struct.VariableFont.html
[`ParseError`]: https://docs.rs/font-parse/latest/font_parse/enum.ParseError.html
## Guarantees
- **Total parsers**: malformed, truncated, or hostile input yields a typed
`ParseError`: never a panic, never an out-of-bounds read. Enforced by
deny-level lints (`unwrap_used`/`expect_used`/`panic`/`indexing_slicing`)
on the lib target, a cargo-fuzz target over the main entry point, and
500-case proptests over arbitrary and mutated input.
- **Zero-copy table access**: `FontRef` borrows the buffer; every table view
is a lifetime-bound slice. A view that outlives its bytes is a compile
error (trybuild test). Outline extraction allocates (points), table access
never does.
- **Bounded memory**: WOFF2 stream sizes are validated against the
decompressed block before slicing (spec MUST), composite expansion is
capped (depth 16, 2^20 points), and WOFF1 zlib output is clamped to the
declared `origLength`.
## Example
```rust
use font_parse::{FontRef, TableTag};
let bytes = std::fs::read("tests/fixtures/minimal.ttf").unwrap();
let font = FontRef::new(&bytes).unwrap();
let head = font.head().unwrap();
assert_eq!(head.units_per_em().unwrap(), 1000);
let cmap = font.cmap().unwrap();
let gid = cmap.codepoint('A').unwrap();
let advance = font.advance_width(gid).unwrap();
if let Some(outline) = font.outline(gid).unwrap() {
let _ = outline.point_count(); // composites resolved recursively
}
let _raw: Option<&[u8]> = font.table(TableTag::Glyf);
```
## Features
| `std` | ✓ | container decoding + lossy `name` decoding; without it the SFNT core is `no_std` (`alloc`) |
| `woff` | ✓ | WOFF 1.0 container (`miniz_oxide`, implies `std`) |
| `woff2` | ✓ | WOFF 2.0 container (`brotli-decompressor`, implies `std`) |
## Out of scope
CFF outline decoding (OTTO fonts parse; `CFF` is exposed raw),
rasterization, hinting interpretation, cmap formats 2/8/10/13/14, GSUB/GPOS
lookup subtable types, WOFF2 collection directories, and glyph variation
*application* (deltas are exposed; instancing is a rendering concern).
## Fixtures
The known-good suite runs against fonts generated by
`tests/fixtures/generate_fonts.py` (fontTools); regenerate fixtures and
expected values together.
## Layer
L0: leaf. Zero estate-internal dependencies.