Skip to main content

Crate font_model

Crate font_model 

Source
Expand description

Typed, owned, editable font model — outlines, character maps, metrics, validation, and subsetting.

font-model is the estate’s L1 font substrate: the resolved, owned, mutable form of a typeface that sits directly on font-parse’s L0 byte-level views. A parser gives you borrowed table views over &[u8]; an editor needs owned glyphs it can reshape, a writer needs metrics it can serialise, and a subsetter needs a codepoint set it can intersect against. That is what this crate is.

use font_model::{Font, Glyph, GlyphId, Outline, Path};

// Build a font from parts.
let mut outline = Outline::new();
let mut p = Path::starting_at(80.0, 0.0);
p.line_to(520.0, 0.0);
p.line_to(520.0, 700.0);
p.line_to(80.0, 700.0);
p.close();
outline.push_contour(p);

let mut cmap = font_model::Cmap::format4();
cmap.subtables_mut()[0] =
    font_model::cmap::insert_entry(&cmap.subtables()[0], 'A' as u32, GlyphId::new(1));

let font = Font::builder()
    .units_per_em(1000)
    .glyph(Glyph::new(GlyphId::NOTDEF, 600, 80, outline.clone()))
    .glyph(Glyph::new(GlyphId::new(1), 600, 80, outline))
    .cmap(cmap)
    .build()?;

assert_eq!(font.glyph_for('A' as u32), Some(GlyphId::new(1)));

// Subset to 'A' plus .notdef.
let mut keep = font_model::CodePointSet::new();
keep.insert('A' as u32);
let sub = font.subset(&keep)?;
assert_eq!(sub.glyph_count(), 2);
assert!(sub.validate().is_ok());

§Model, not parser

font-parse reads bytes in place: zero-copy, allocation-free, total. This crate owns its data. [Font::from_sfnt] is the bridge — it runs the L0 parser and resolves its borrowed views into this model — and it is the only function here that takes foreign bytes.

§Invariants

Font::validate is the single gate, and every error variant names the offending glyph and field:

InvariantError
units_per_em in 16..=16384ModelError::InvalidUnitsPerEm
ink implies non-zero advanceModelError::ZeroAdvance
contours closedModelError::UnclosedContour
coordinates finiteModelError::NonFiniteCoordinate
cmap strictly increasingModelError::NonMonotonicCmap
every glyph reference resolvesModelError::UnknownGlyph

§Layer

L1 — substrate. Its only estate-internal dependency is font-parse (L0); thiserror carries the error taxonomy, serde is optional and off by default so a model can round-trip through a config format without forcing the dependency on everyone.

Re-exports§

pub use cmap::from_entries;
pub use cmap::insert_entry;
pub use cmap::Cmap;
pub use cmap::CmapSubtable;
pub use cmap::CodePointSet;
pub use cmap::Segment;
pub use font::build_stub_font;
pub use font::name_value;
pub use font::Font;
pub use font::FontBuilder;
pub use font::KernPair;
pub use font::Kerning;
pub use font::TableTag;
pub use font::MAX_UNITS_PER_EM;
pub use font::MIN_UNITS_PER_EM;
pub use font::TAG_CMAP;
pub use font::TAG_GPOS;
pub use font::TAG_GSUB;
pub use font::TAG_KERN;
pub use glyph::Glyph;
pub use glyph::GlyphId;
pub use metrics::FontMetrics;
pub use metrics::Metrics;
pub use metrics::MetricsBuilder;
pub use metrics::VerticalMetrics;
pub use name::name_id;
pub use name::platform;
pub use name::preferred_record;
pub use name::NameRecord;
pub use os2::fs_type;
pub use os2::weight_class;
pub use os2::width_class;
pub use os2::Os2Metrics;
pub use outline::Bbox;
pub use outline::Command;
pub use outline::FlatSegment;
pub use outline::Outline;
pub use outline::Path;
pub use outline::Subpath;
pub use outline::Winding;
pub use parse::from_font_ref;
pub use parse::from_sfnt;
pub use parse::hmtx_rows;
pub use parse::to_outline;

Modules§

cmap
Character maps: Cmap, CmapSubtable, and CodePointSet.
font
The font model: Font, FontBuilder, and the operations a subsetter or a writer needs.
glyph
Glyphs: GlyphId and Glyph.
metrics
Font metrics: Metrics, MetricsBuilder, and the vertical set.
name
The name table’s records, owned.
os2
The OS/2 table’s metrics, owned.
outline
Resolved outlines: Command, Path, Outline, and Bbox.
parse
The bridge from [font-parse]’s borrowed views to this owned model.

Enums§

ModelError
The single crate-level error for model construction, validation, and subsetting.