Skip to main content

Crate font_parse

Crate font_parse 

Source
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

ContainerEntry pointFeature
SFNT / TrueType / OTFFontRef::new— (no_std)
TTC collectionFontCollection::parse, OwnedFont::from_collection—
WOFF 1.0 (zlib)OwnedFont::from_woffwoff (std)
WOFF 2.0 (Brotli + glyf/loca/hmtx transforms)OwnedFont::from_woff2woff2 (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.
FontCollection
A parsed TTC collection: N font directories sharing table data.
FontRef
Zero-copy reference over an SFNT font.
GlyphId
A 32-bit glyph identifier.
GlyphId16
A 16-bit glyph identifier.
OwnedFont
An owned font buffer, validated at construction.
Tag
An OpenType tag.
VariableFont
The variable-font view: fvar axes and instances plus optional avar segment maps and gvar glyph deltas.
Version16Dot16
A legacy 16/16 version encoding Packed 32-bit value with major and minor version numbers.

Enums§

ParseError
The single crate-level error for all font parsing.
TableTag
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).