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
-
Decode the bytes
Hand over a complete font held by the caller: a plain font, a collection, a WOFF file or a WOFF2 file.
decodereads the table directory and the small tables the face needs to describe itself; glyph outlines and their deltas stay lazy.decode_allreturns oneFaceper face of a collection, and raisesMalformedon 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() // 6Scaler words
0x00010000,0x4F54544Fand0x74727565are accepted at the container layer, andttcfopens a collection. A CFF face underOTTOhas noglyftable and is read through its charstrings instead;MissingTableis for a face with neither kind of outline.units_per_em() -> 1000 num_glyphs() -> 6 -
Look up a glyph
A codepoint is not a glyph.
glyph_indextranslates throughcmapformats 0, 4, 6 and 12 and returnsGlyphId?;Nonemeans the face has no sort and is not an error.glyph_fordoes the same for aChar.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.ttfthe charmap mapsU+0020to 1,U+0041to 2,U+0042to 3,U+0061to 4 andU+00C1to 5.glyph_index(0x41) -> Some(GlyphId { index: 2 }) glyph_index(0x43) -> None -
Read the outline
Face::outlinereturns anOutlinein 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.QuadTocarries 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)) Closebounds() -> Bounds { x_min: 0, y_min: 0, x_max: 600, y_max: 700 } -
Rasterise at a size
Face::rasterisereturns an eight-bit greyCoverageMask: non-zero winding, unhinted, with its ownleftandtoporigin 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 maskScale::of(ppem, units_per_em)storesppem << 6;Scale::apply(v)returnsv * ppem * 64 / units_per_emin 26.6 units, truncating toward zero. The sameScaleis 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 -
Measure a run
Face::measuresums scaled advances and legacykernpairs in 26.6 pixels. Unmapped codepoints are skipped, soglyphscan be smaller than the character count. Measurement needs advances and kern data, never geometry:layoutimportsmodelandtable, notoutlineorraster.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 -
Limits and refusals
Every length in a font is declared by the font.
Limitschecks each declaration before the allocation it guards.limitsis the preset for an application reading its own assets;limits_strictis 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 presetOne
Refusedsuberror covers the whole library, so a caller needs exactly onetry/raisehandler. 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 deepRefused::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. -
Other formats, other instances
A face is not always quadratic and not always plain.
decodeaccepts a plain font, a TrueType collection, a WOFF file and a WOFF2 file without being told which.is_postscriptsays whether the outlines come fromglyfor fromCFF/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 fileContainers are an interface rather than a chain of branches. A
Containerclaims a signature and returns the faces it finds; aTableSourceis 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 bytesA WOFF2 collection is where that pays: one table directory for the whole file, one buffer, and a
TableSourceper face that shares what the faces have in common. A transformedglyfis rebuilt once even when two faces name it, andlocaandhmtxare derived from theglyfeach 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_variableat every call site.Outlines are not the only thing a location moves.
MVARcarries the face's own measurements,VVARthe vertical advances, andcvarthe 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
vheaandvmtx, which have the same layout ashheaandhmtxand 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
Floatwould return different coverage on different backends for the same bytes, and no differential test could say which answer was right.README.md, Why integers.