Expand description
SFNT/OTF/TrueType/WOFF/WOFF2 binary font parser — offset tables, character maps, glyph outlines, variable fonts.
font-parse reads font files in place: a FontRef is a lifetime-
bound view over &[u8] — table access touches nothing, allocates
nothing, copies nothing. Typed table views (HeadTable, Cmap,
GlyphOutline, …) read fields on demand from the borrowed bytes and
die with the borrow.
§Totality
Every parser is total: malformed, truncated, hostile input produces a
typed ParseError — never a panic, never an out-of-bounds read. Bounds
are checked at every field read (deny-level indexing_slicing /
unwrap_used lints enforce this), so fuzzing asserts Err-not-panic
(see fuzz/).
§Containers
| Container | Entry point | Feature |
|---|---|---|
| SFNT / TrueType / OTF | FontRef::new | — (no_std) |
| TTC collection | FontCollection::parse, OwnedFont::from_collection | — |
| WOFF 1.0 (zlib) | OwnedFont::from_woff | woff (std) |
| WOFF 2.0 (Brotli + glyf/loca/hmtx transforms) | OwnedFont::from_woff2 | woff2 (std) |
WOFF/WOFF2 decoding reconstructs a standalone SFNT (OwnedFont) with
recomputed table checksums and head.checkSumAdjustment.
§Example
use font_parse::{FontRef, TableTag, GlyphId};
let bytes = std::fs::read("tests/fixtures/minimal.ttf").expect("fixture");
let font = FontRef::new(&bytes)?;
// Zero-copy table access.
let head = font.head()?;
assert_eq!(head.units_per_em()?, 1000);
// Character mapping.
let cmap = font.cmap()?;
let gid = cmap.codepoint('A').expect("A is mapped");
// Metrics and outlines.
let advance = font.advance_width(gid)?;
if let Some(outline) = font.outline(gid)? {
let _ = outline.point_count();
}
let _raw: Option<&[u8]> = font.table(TableTag::Glyf);
let _ = GlyphId::new(0);§Out of scope
CFF outline decoding (OTTO fonts parse; their CFF table is exposed raw),
rasterization, glyph instruction (hinting) interpretation, cmap lookup
subtable formats 2/8/10/13/14, GSUB/GPOS lookup subtable types, and WOFF2
collection directories. This crate is the estate’s font edge: consumers
bring the buffers and take the glyphs.
§Layer
L0 — leaf. Zero estate-internal dependencies; external deps are
font-types (scalar types) + thiserror, with miniz_oxide/brotli- decompressor behind the container features.
Re-exports§
pub use tables::AvarTable;pub use tables::AxisValue;pub use tables::Cmap;pub use tables::CmapSubtable;pub use tables::Component;pub use tables::CompositeGlyph;pub use tables::Contour;pub use tables::DsigTable;pub use tables::EncodingRecord;pub use tables::Feature;pub use tables::FeatureList;pub use tables::FvarTable;pub use tables::GlyphOutline;pub use tables::GlyphRef;pub use tables::GvarTable;pub use tables::HeadTable;pub use tables::HheaTable;pub use tables::HmtxTable;pub use tables::LangSys;pub use tables::LayoutKind;pub use tables::LayoutTable;pub use tables::LocaTable;pub use tables::Lookup;pub use tables::LookupList;pub use tables::MaxpTable;pub use tables::NameRecord;pub use tables::NameTable;pub use tables::Os2Table;pub use tables::OutlinePoint;pub use tables::PostTable;pub use tables::Script;pub use tables::ScriptList;pub use tables::SimpleGlyph;pub use tables::StatAxis;pub use tables::StatTable;pub use tables::TupleVariation;pub use tables::VarInstance;pub use tables::VariationAxis;
Modules§
- tables
- Typed table parsers — zero-copy views over raw table bytes.
Structs§
- F2Dot14
- 16-bit signed fixed point number with 14 bits of fraction.
- Fixed
- 32-bit signed fixed point number with 16 bits of fraction.
- Font
Collection - A parsed TTC collection: N font directories sharing table data.
- FontRef
- Zero-copy reference over an SFNT font.
- GlyphId
- A 32-bit glyph identifier.
- Glyph
Id16 - A 16-bit glyph identifier.
- Owned
Font - An owned font buffer, validated at construction.
- Tag
- An OpenType tag.
- Variable
Font - The variable-font view:
fvaraxes and instances plus optionalavarsegment maps andgvarglyph deltas. - Version16
Dot16 - A legacy 16/16 version encoding Packed 32-bit value with major and minor version numbers.
Enums§
- Parse
Error - The single crate-level error for all font parsing.
- Table
Tag - The subset of SFNT table tags this parser knows by name.
Functions§
- table_
checksum_ public - SFNT table checksum: u32 sum of big-endian words, zero-padded to four bytes. Exposed for container verification and directory checks.
Type Aliases§
- RawTag
- A four-byte table tag, as raw bytes (used in
ParseError::TruncatedTable).