Skip to content
ttf

ttf

Mojo module 🡭

ttf

Native TrueType (sfnt/glyf) font file parser: reads a font file’s binary tables directly (table directory, head, maxp, hhea, hmtx, cmap, glyf, loca, kern, GPOS, GSUB) rather than linking a font library. Field offsets and decode algorithms follow Microsoft’s OpenType 1.9.1 specification (learn.microsoft.com/ typography/opentype/spec/{otff,head,maxp,hhea,hmtx,cmap,loca,glyf,kern, gpos,gsub,chapter2}).

Scope:

  • TrueType (glyf) and CFF outlines. An OTTO font’s CFF table is read by cff.mojo, whose Type 2 charstring interpreter produces cubic contours where glyf gives quadratic ones with implied midpoints; RawGlyphOutline.cubic says which, and outline_to_path decomposes accordingly. CFF2 is not read.
  • Pair kerning, through kern_adjustment: GPOS lookup type 2 (PairPos formats 1 and 2, including behind an extension lookup type 9) under the kern feature, and the kern table’s format 0 horizontal subtables. No mark attachment, no contextual positioning, and lookup flags such as IgnoreMarks are not honored – an intervening mark glyph breaks a pair here where a full shaper would kern through it.
  • Single and ligature substitution, through substitute_glyphs: GSUB lookup types 1 and 4 (including behind an extension lookup type 7) – enough for the Latin “fi”/“fl”/“ffi” ligatures and for Arabic contextual forms. The multiple, alternate, contextual and chained-context substitutions the same features also use are skipped, and lookup flags (IgnoreMarks and the rest) are not honored. Features are selected from one script’s default language system: ccmp and liga for latn, and for arab the OpenType Arabic order ccmp, isol, fina, medi, init, rlig, liga, calt. Each glyph carries a mask of the features enabled on it, so one sweep of fina reaches only the letters a caller marked final. Substitution runs in logical order, per bidi run.
  • No hinting. Every glyph goes through fill_path_aa’s supersampled coverage AA instead, which keeps unhinted outlines correct at the sizes a chart uses.
  • Variable fonts (fvar/gvar) read as their default instance: glyf/loca hold the non-varied outlines and gvar’s per-instance deltas are never read.
  • Composite glyphs (“é” = “e” + combining acute) are supported, including the scale/2x2-transform component flags. Point-matching placement (ARGS_ARE_XY_VALUES unset) raises.

Structs

  • ShapedRun: A glyph sequence after GSUB substitution. clusters[i] is how many of the input glyphs glyphs[i] stands for – 1 for a glyph left alone or substituted one for one, 3 for an “ffi” ligature – so a caller can walk back to the characters each output glyph came from. The two lists are the same length, and the clusters sum to the input length. changed is False when no lookup fired, which lets a caller skip that walk entirely.
  • RawGlyphOutline: A decoded glyph outline in plain-List form: on-curve/off-curve points plus per-contour end indices – FreeType’s FT_Outline shape (points, tags, contour ends) as owned Lists rather than raw C pointers, so this module has no pointer surface.
  • BitmapMetrics: A color bitmap glyph’s placement, in the strike’s own pixels: found is False when the glyph has no bitmap. bearing_y is the distance from the baseline up to the bitmap’s top edge, as in the sbit metrics.
  • TTFFace: A parsed TrueType font file: the raw file bytes plus the table offsets and global metrics this module reads.

Functions