The composing stick

One line at a fixed measure, set step by step. Every reading below comes from synthetic.ttf, the six-glyph face committed with the fixtures, so the numbers can be checked against the comparison sheet.

From bytes to a line

One import and one door. decode borrows the bytes and returns a Face; every other operation is a method on that face. Outlines stay lazy until a glyph is asked for.

pub fn decode(bytes : BytesView, limits~ : Limits = limits) -> Face raise Refused

A caller that imports only the façade has Bounds, CoverageMask, FontMetrics, GlyphId, GlyphMetrics, Limits, Outline, PathCommand, Point, Refused, Scale, TextMetrics and VerticalMetrics in hand, plus the two presets.

The line it produced

let face = @moonfont.decode(bytes, limits=@moonfont.limits_strict) let gid = face.glyph_index(0x41).unwrap() let outline = face.outline(gid) let mask = face.rasterise(gid, 16) let measured = face.measure("ABa", 16) units_per_em() 1000 num_glyphs() 6 glyph_index(0x41) Some(GlyphId { index: 2 }) outline.commands() 6 measure("ABa", 16) advance 1586 glyphs 3
  1. Decode the bytes

    Hand over a complete font held by the caller: a plain font, a collection, a WOFF file or a WOFF2 file. decode reads the table directory and the small tables the face needs to describe itself; glyph outlines and their deltas stay lazy. decode_all returns one Face per face of a collection, and raises Malformed on an empty one.

    let face = @moonfont.decode(font_bytes, limits=@moonfont.limits_strict) let faces = @moonfont.decode_all(collection_bytes) let upem = face.units_per_em() // 1000 let count = face.num_glyphs() // 6

    Scaler words 0x00010000, 0x4F54544F and 0x74727565 are accepted at the container layer, and ttcf opens a collection. A CFF face under OTTO has no glyf table and is read through its charstrings instead; MissingTable is for a face with neither kind of outline.

    units_per_em() -> 1000 num_glyphs() -> 6
  2. Look up a glyph

    A codepoint is not a glyph. glyph_index translates through cmap formats 0, 4, 6 and 12 and returns GlyphId?; None means the face has no sort and is not an error. glyph_for does the same for a Char.

    let gid = face.glyph_index(0x41).unwrap() // U+0041 to glyph 2 let absent = face.glyph_index(0x43) // U+0043 is unmapped let by_char = face.glyph_for('A')

    In synthetic.ttf the charmap maps U+0020 to 1, U+0041 to 2, U+0042 to 3, U+0061 to 4 and U+00C1 to 5.

    glyph_index(0x41) -> Some(GlyphId { index: 2 }) glyph_index(0x43) -> None
  3. Read the outline

    Face::outline returns an Outline in font units, with quadratic and cubic curves kept as curves. Flattening is the rasteriser's decision, not the parser's. Implied on-curve midpoints between consecutive off-curve points are resolved here, and composite components are decomposed with their offsets applied.

    let outline = face.outline(gid) let commands = outline.commands() let bounds = outline.bounds()

    The six commands for U+0041, straight from the fixture outline stream. QuadTo carries one control point followed by the end point.

    MoveTo(Point::of(100, 0)) LineTo(Point::of(500, 0)) QuadTo(Point::of(600, 300), Point::of(500, 500)) QuadTo(Point::of(400, 700), Point::of(200, 700)) QuadTo(Point::of(0, 300), Point::of(100, 0)) Close bounds() -> Bounds { x_min: 0, y_min: 0, x_max: 600, y_max: 700 }
  4. Rasterise at a size

    Face::rasterise returns an eight-bit grey CoverageMask: non-zero winding, unhinted, with its own left and top origin in 26.6 pixels so it can be copied into an atlas without more arithmetic. The rasteriser takes eight vertical subsamples per pixel row and accumulates exact horizontal area in 26.6 fixed point. There is no floating point in the path.

    let mask = face.rasterise(gid, 16) let width = mask.width let height = mask.height let sample = mask.at(0, 0) // 0..255, or 0 outside the mask

    Scale::of(ppem, units_per_em) stores ppem << 6; Scale::apply(v) returns v * ppem * 64 / units_per_em in 26.6 units, truncating toward zero. The same Scale is shared by measurement and rasterisation, so the two never disagree about where the pen stands.

    Scale::of(16, 1000).apply(600) -> 614 (9.59 px) Scale::of(16, 1000).apply(700) -> 716 (11.19 px) Scale::of(16, 1000).apply(-200) -> -204
  5. Measure a run

    Face::measure sums scaled advances and legacy kern pairs in 26.6 pixels. Unmapped codepoints are skipped, so glyphs can be smaller than the character count. Measurement needs advances and kern data, never geometry: layout imports model and table, not outline or raster.

    let measured = face.measure("ABa", 16) let spaced = face.measure("ABa", 16, letter_spacing=8)

    The three sorts have advances 600, 500 and 450 font units. Each is scaled separately, so the run advance is the sum of three truncated values, not one scaling of the total.

    advance 614 + 512 + 460 = 1586 (24.78 px) ascent 819 descent -204 line_gap 0 glyphs 3
  6. Limits and refusals

    Every length in a font is declared by the font. Limits checks each declaration before the allocation it guards. limits is the preset for an application reading its own assets; limits_strict is for bytes from somewhere else. Both are finite at every ceiling, and a record built by hand is honoured as given.

    @moonfont.limits file_bytes 268435456 table_bytes 268435456 glyphs 65536 points 65536 contours 8192 composites 1024 recursion 16 mask_pixels 67108864 @moonfont.limits_strict file_bytes 16777216 table_bytes 16777216 glyphs 65536 points 8192 contours 1024 composites 256 recursion 8 mask_pixels 4194304 Limits::new(points=16384) one ceiling raised, the rest from the preset

    One Refused suberror covers the whole library, so a caller needs exactly one try/raise handler. Each variant names a byte offset or the two values that disagreed.

    Unknown(at~, got~) not a font this library reads Unexpected(at~, got~, want~) a field is wrong Unsupported(at~, got~) a part not read yet Truncated(at~, want~) bytes wanted OverLimit(limit~, got~) past a ceiling Malformed(at~, got~) impossible declaration MissingTable(tag~) required table absent MissingGlyph(glyph~) id outside maxp Recursion(depth~) composite nested too deep

    Refused::name() returns the variant as a stable string, which is what a differential test records rather than the rendered prose. No public function panics, loops without a bound, or returns a partial value.

  7. Other formats, other instances

    A face is not always quadratic and not always plain. decode accepts a plain font, a TrueType collection, a WOFF file and a WOFF2 file without being told which. is_postscript says whether the outlines come from glyf or from CFF/CFF2; the command stream is the same either way, so a consumer never branches.

    is_postscript() false for glyf, true for CFF and CFF2 decode_all(bytes) one Face per face in a collection decode(woff2_bytes) the same Face as the plain file

    Containers are an interface rather than a chain of branches. A Container claims a signature and returns the faces it finds; a TableSource is a face seen as named byte ranges. Three containers are registered — plain sfnt, WOFF and WOFF2 — and the readers above them only ever ask a source for a tag:

    @moonfont.decode_from(source) a Face from any TableSource @cff.Font reads a CFF table the same way, from bytes

    A WOFF2 collection is where that pays: one table directory for the whole file, one buffer, and a TableSource per face that shares what the faces have in common. A transformed glyf is rebuilt once even when two faces name it, and loca and hmtx are derived from the glyf each face actually names.

    A variable font is the same face with a location. Axis values are 16.16 fixed point under the four-character tag, and the axes a font offers are data:

    let face = @moonfont.decode(vf_bytes) face.is_variable() true face.axes() [wght 400..900 default 400, wdth 75..100 default 100] let at = face.at([(0x77676874, 700 * 65536)]) let outline = face.outline(gid, location=at) face.advance(gid, location=at)

    Every point is a whole font unit and every scalar is 16.16, so two runs at the same location give the same outline on every backend. A font without axes ignores a location rather than refusing it, which keeps a caller from branching on is_variable at every call site.

    Outlines are not the only thing a location moves. MVAR carries the face's own measurements, VVAR the vertical advances, and cvar the control values a hinting program snaps to. All three are read, and all three answer at the same location as the glyphs:

    let at = face.at([(0x77676874, 900 * 65536)]) // wght 900 face.font_metrics(location=at).ascent 840 (800 at the default) face.vertical_metrics(location=at).ascent 560 (500 at the default) face.advance_height(gid, location=at) 1080 (1000 at the default) face.cvt(location=at) [160, 230, 360, 60] ([140, 200, 320, 60] at the default)

    Vertical metrics come from vhea and vmtx, which have the same layout as hhea and hmtx and are read by the same code. A font without them reports the horizontal measurements from these calls and advances by its em, so a vertical layout never has to ask whether the pair exists.

A rasteriser built on Float would return different coverage on different backends for the same bytes, and no differential test could say which answer was right.

README.md, Why integers.