font_parse/lib.rs
1//! SFNT/OTF/TrueType/WOFF/WOFF2 binary font parser — offset tables,
2//! character maps, glyph outlines, variable fonts.
3//!
4//! font-parse reads font files **in place**: a [`FontRef`] is a lifetime-
5//! bound view over `&[u8]` — table access touches nothing, allocates
6//! nothing, copies nothing. Typed table views ([`HeadTable`], [`Cmap`],
7//! [`GlyphOutline`], …) read fields on demand from the borrowed bytes and
8//! die with the borrow.
9//!
10//! # Totality
11//!
12//! Every parser is **total**: malformed, truncated, hostile input produces a
13//! typed [`ParseError`] — never a panic, never an out-of-bounds read. Bounds
14//! are checked at every field read (deny-level `indexing_slicing` /
15//! `unwrap_used` lints enforce this), so fuzzing asserts `Err`-not-panic
16//! (see `fuzz/`).
17//!
18//! # Containers
19//!
20//! | Container | Entry point | Feature |
21//! |---|---|---|
22//! | SFNT / TrueType / OTF | [`FontRef::new`] | — (no_std) |
23//! | TTC collection | [`FontCollection::parse`], [`OwnedFont::from_collection`] | — |
24//! | WOFF 1.0 (zlib) | [`OwnedFont::from_woff`] | `woff` (std) |
25//! | WOFF 2.0 (Brotli + glyf/loca/hmtx transforms) | [`OwnedFont::from_woff2`] | `woff2` (std) |
26//!
27//! WOFF/WOFF2 decoding reconstructs a standalone SFNT ([`OwnedFont`]) with
28//! recomputed table checksums and `head.checkSumAdjustment`.
29//!
30//! # Example
31//!
32//! ```no_run
33//! use font_parse::{FontRef, TableTag, GlyphId};
34//!
35//! # fn main() -> Result<(), font_parse::ParseError> {
36//! let bytes = std::fs::read("tests/fixtures/minimal.ttf").expect("fixture");
37//! let font = FontRef::new(&bytes)?;
38//!
39//! // Zero-copy table access.
40//! let head = font.head()?;
41//! assert_eq!(head.units_per_em()?, 1000);
42//!
43//! // Character mapping.
44//! let cmap = font.cmap()?;
45//! let gid = cmap.codepoint('A').expect("A is mapped");
46//!
47//! // Metrics and outlines.
48//! let advance = font.advance_width(gid)?;
49//! if let Some(outline) = font.outline(gid)? {
50//! let _ = outline.point_count();
51//! }
52//! let _raw: Option<&[u8]> = font.table(TableTag::Glyf);
53//! let _ = GlyphId::new(0);
54//! # Ok(())
55//! # }
56//! ```
57//!
58//! # Out of scope
59//!
60//! CFF outline decoding (OTTO fonts parse; their `CFF` table is exposed raw),
61//! rasterization, glyph instruction (hinting) interpretation, cmap lookup
62//! subtable formats 2/8/10/13/14, GSUB/GPOS lookup subtable types, and WOFF2
63//! collection directories. This crate is the estate's font edge: consumers
64//! bring the buffers and take the glyphs.
65//!
66//! # Layer
67//!
68//! L0 — leaf. Zero estate-internal dependencies; external deps are
69//! `font-types` (scalar types) + `thiserror`, with `miniz_oxide`/`brotli-
70//! decompressor` behind the container features.
71
72#![cfg_attr(not(feature = "std"), no_std)]
73#![deny(missing_docs)]
74extern crate alloc;
75
76mod error;
77mod font;
78mod reader;
79pub mod tables;
80mod tag;
81
82pub use error::{ParseError, RawTag};
83pub use font::{FontCollection, FontRef, OwnedFont, VariableFont};
84pub use tables::{
85 AvarTable, AxisValue, Cmap, CmapSubtable, Component, CompositeGlyph, Contour, DsigTable,
86 EncodingRecord, Feature, FeatureList, FvarTable, GlyphOutline, GlyphRef, GvarTable, HeadTable,
87 HheaTable, HmtxTable, LangSys, LayoutKind, LayoutTable, LocaTable, Lookup, LookupList,
88 MaxpTable, NameRecord, NameTable, Os2Table, OutlinePoint, PostTable, Script, ScriptList,
89 SimpleGlyph, StatAxis, StatTable, TupleVariation, VarInstance, VariationAxis,
90};
91pub use tag::TableTag;
92
93// Scalar types re-exported from Google Fonts' `font-types` — the crate's
94// numeric vocabulary (16.16 `Fixed`, 2.14 `F2Dot14`, `GlyphId`, `Tag`, …).
95pub use font_types::{F2Dot14, Fixed, GlyphId, GlyphId16, Tag, Version16Dot16};
96
97/// SFNT table checksum: u32 sum of big-endian words, zero-padded to four
98/// bytes. Exposed for container verification and directory checks.
99#[must_use]
100pub fn table_checksum_public(bytes: &[u8]) -> u32 {
101 font::table_checksum(bytes)
102}
103
104#[cfg(feature = "woff")]
105mod woff;
106#[cfg(feature = "woff2")]
107mod woff2;