Introduction
TrueType is a font format that Apple developed and released in 1991; Microsoft adopted it for Windows soon after. It describes each character’s outline with quadratic curves, and carries instructions that adjust the outlines to the pixel grid at small sizes. TrueType fonts work on every desktop operating system and in web browsers.
The same table-based container later became the basis of
OpenType. A
TrueType collection, .ttc, packs several
fonts into one file.
Structure
A TrueType font is a directory of tables, all big-endian. The file starts with a
table directory: the version
00 01 00 00 (Apple fonts may use true), the number of tables
and three values for binary searching. A 16-byte record for each table follows,
sorted by tag, giving its four-character tag, checksum, offset and length. The tables
themselves come after, each on a four-byte boundary, in any order.
0Table directory version, number of tables, search values12 bytes12Table records tag, checksum, offset, length16 bytes each…Tables each at its recorded offsetvaries…head, hhea, maxp, hmtx global settings, limits and glyph widthsvaries…cmap maps characters to glyphsvaries…loca, glyf where each outline is, and the outlinesvaries…name, OS/2, post names, Windows metrics and classification, PostScript datavaries…cvt, fpgm, prep, gasp hinting programs and settingsoptional…kern, hdmx, LTSH, VDMX kerning and cached hinted metricsoptional
Outlines are quadratic Bézier curves: each contour is a loop of points on and off
the curve, in font units on a grid of unitsPerEm per em. Hinting
instructions, run by the rasteriser’s own virtual machine, adjust the points to the
pixel grid at small sizes.
In the explorer
Dropping a .ttf file on the explorer shows its table directory and the
tables it decodes: head, hhea, maxp
(versions 0.5 and 1.0) and hmtx; name, with
every record, its platform, encoding and language named and its string decoded for
that platform; cmap, with subtable formats 0, 2,
4, 6, 8, 10, 12, 13 and 14 and the mappings listed a page at a time;
OS/2 (versions 0 to 5) with its flags and range
bits named; post (versions 1, 2, 2.5 and 3) with
the glyph names; loca and glyf, with the
contours and points of simple glyphs, the components of composite glyphs and each
glyph’s instructions, disassembled; vhea and vmtx; gasp, cvt, fpgm
and prep (the two programs disassembled); kern
(formats 0 and 2, and Apple’s formats 0, 1, 2 and 3); and DSIG, whose structure is shown and
whose signature is not verified. The layout tables are decoded too:
GSUB and GPOS
with their scripts, language systems, features, lookups and subtables (every Coverage and
ClassDef table, anchor, value record and device table where it lies, and what each subtable
maps, a page at a time), GDEF,
BASE and JSTF and
MATH. The variation tables are decoded as well
(fvar, avar 1 and 2, STAT,
cvar, gvar, HVAR, VVAR, MVAR), with an option
for a named instance and for each axis that redraws the outlines; so are the colour tables
(CPAL, COLR versions 0 and 1, SVG; each SVG
document opens as an entry, unpacked when it is gzip-compressed, and the colour glyphs are
drawn with a chosen palette), the bitmap tables
(EBLC, EBDT, EBSC, CBLC, CBDT and sbix; one picture
for each strike, and each sbix image opens as an entry of its own type) and
hdmx, LTSH and VDMX,
PCLT, meta and MERG. The outlines of
glyf are drawn as a picture (hinting is not applied). Every other table is shown as its bytes, labelled
“tag: not yet decoded”, and a list of every registered table tag says which
are decoded.
The Apple Advanced Typography tables are decoded as well, from Apple’s manual and the fonts
on a Mac (feat, morx, mort, kerx, just, trak, ankr, opbd,
prop, lcar, bsln, fdsc, fmtx, gcid, xref, Zapf and ltag, and the state-table form of kern):
every lookup table format (0, 2, 4, 6, 8, 10), every state machine (its classes, states and
entries, with each entry’s flags told in words), the five kinds of morx subtable
(rearrangement, contextual, ligature, noncontextual, insertion) with their substitution
lookups, ligature actions and inserted glyphs, and the five kerx formats. The machines of morx, mort,
kerx and kern are also run on a typed string (the “Text to shape” option). An Instructions tab disassembles the TrueType programs: fpgm,
prep and the program of each glyph that has one, every instruction linked to its
bytes, with the pushed data, the function and instruction definitions and the IF blocks nested,
and the target of a jump or call when its offset or function number is pushed just before it.
At an instance the Outlines tab also lists each glyph’s advance width, with HVAR (or the
gvar phantom points of a font without it) applied.
Each table’s checksum is worked out and shown beside the recorded one in the table
directory, as is head.checksumAdjustment for the whole file. A difference is
shown and is not counted as a problem: real system fonts have wrong checksums and
nothing that opens a font checks them.
The problems listed are rules of the specification that make a table unreadable or inconsistent: a bad magic number, a table past the end of the file, offsets out of order or outside their table, counts that do not fit, glyph data that runs short, a composite glyph that uses itself, a glyph the font does not have. Rules that real fonts break and no program enforces (reserved bits set, glyph names with other characters, a slightly wrong subtable length, search fields that are not the derived values) are listed as notes, not as problems.
Not described
The pages follow the OpenType specification and Apple’s TrueType manual, which publish all of this; the items are work not yet done.
Known, not yet covered
- Reference: the full register of Windows language identifiers (the name page lists 12 common ones; the rest are in [MS-LCID]).
- Reference: the PANOSE digit values for bytes 1 to 9 of the OS/2 table (they are in the PANOSE guide, which OpenType defers to).
- Reference: what each hinting instruction does, with its stack effects; the hinting page gives names, opcodes and flag bits only (the viewer’s disassembly gives each instruction a one-line meaning and its pops and pushes).
- Reference: Apple’s Font Feature Registry, which names every feature
type and selector (the AAT page gives the table
formats; the viewer names the feature types it knows, about forty, and otherwise uses
the font’s own
featnames), and the Apple tables acnt, bdat, bhed and bloc, which have no page of their own (the viewer decodes them from Apple’s manual). - Viewer: text shaping runs every lookup type. The “Text to shape” option runs a
typed string as HarfBuzz does: the cmap (a Windows symbol subtable first, then the Unicode ones,
then Mac Roman), HarfBuzz’s normalisation (decomposing, putting marks in canonical order,
composing again where the font has the glyph), GSUB types 1 to 8 (single, multiple, alternate,
ligature, context, chaining context, extension, reverse chaining) and GPOS types 1 to 9 (single,
pair, cursive, mark to base, mark to ligature, mark to mark, context, chaining context,
extension), with the lookup flags, mark attachment types and mark filtering sets, nested
lookups, ligature components on marks, required features and FeatureVariations at the default
instance; or, for a font that has them, Apple’s
morxandmort(all five subtable types),kerx(formats 0, 1, 2, 4 and 6),trakand the legacykern(formats 0 to 3, in the OpenType and in Apple’s header); a font with no GPOS has its marks placed by the glyph boxes as HarfBuzz does, and a GDEF that HarfBuzz ignores (it keeps a list of known-bad ones) is ignored. The result is in hb-shape’s notation. Compared withhb-shapeon every font file and collection member under/System/Library/Fontsand/Library/Fonts(787 fonts, 62,173 strings in nineteen scripts, among them every Indic script, Khmer, Myanmar, Hangul, Syriac, Arabic, Hebrew, Thai and Lao): 62,160 are the same and 13 differ only where HarfBuzz runs a shaper of its own that the viewer does not run (Mongolian and N’Ko, which it shapes with its Universal Shaping Engine, and Arabic text in one font that has none of the Arabic form features), which the result names; none differs for any other reason. Run since round 9: the Indic shaper (from Microsoft’s “Developing OpenType Fonts for Devanagari Script” and the pages for the other scripts: the syllable, the base consonant, the reph, the pre-base matras, the half forms, the Halant and nukta, the feature stages, the old and the new script tags such as deva and dev2; HarfBuzz behaviour: the category of every character and the syllable grammar, the matching of the font’s lookups against the reph and the pre-base Ra, the Kannada Ra, Halant and ZWJ swap, the vowel constraints, the dotted circle for a broken cluster, the glyph flags the lookups leave) for Devanagari, Bengali, Gurmukhi, Gujarati, Oriya, Tamil, Telugu, Kannada and Malayalam; Khmer and Myanmar (Microsoft’s pages: Coeng and Ro moved to the start, the left vowel, the kinzi, the medials; HarfBuzz behaviour: the grammars and the order of the positions; the marks’ advances are zeroed before positioning for Myanmar); Hangul (HarfBuzz behaviour, after Unicode’s jamo composition: syllables composed from jamo when the font has them, the jamo features, the tone marks); and the whole of BidiMirroring.txt for right-to-left runs. Run since round 8, from Microsoft’s script development pages (Arabic, Hebrew, Thai, Lao) and, where those pages do not say, from HarfBuzz’s behaviour: Arabic (joining types of ArabicShaping.txt, then ccmp, isol, fina, medi, init, rlig, calt, liga and mset in stages, the marks reordered by UAX #53; HarfBuzz behaviour: the joining state machine, Syriac alaph and dalath, the pauses between the stages, ZWJ and ZWNJ), Hebrew (ccmp, kern, mark; HarfBuzz behaviour: the composition of presentation forms for old fonts, the order of the points, GPOS only for the hebr script), Thai and Lao (the sara am as a nikhahit and a sara aa, the nikhahit moved before the above marks; HarfBuzz behaviour: the order of sara u and phinthu, the private-use fallback for a font with no Thai GSUB), right-to-left runs (shaped in logical order, returned in visual order, with mirrored brackets, the cursive and mark directions and Apple’s tables run the other way round), a mask for each feature and ZWJ and ZWNJ in lookup matching, and the choice of shaper from the font’s script tag. Not run: the Universal Shaping Engine (Microsoft’s page and HarfBuzz’s machine and its table of every script’s categories, too large to write by hand for now; it shapes Mongolian, N’Ko, Tibetan, Sinhala, Balinese, Javanese, Cham, Limbu, Tai Le, Tifinagh, Phags-pa, Sundanese and some forty more scripts, which the result names), the Arabic fallback shaping from presentation forms for a font without form features (HarfBuzz behaviour), the stch stretching feature, the dotted circle under an invalid mark that starts a text (Microsoft’s pages describe it; HarfBuzz inserts one only before a mark that starts a text marked as a paragraph start, whichhb-shapedoes not do; dotted circles in broken syllables are inserted), the rules of HarfBuzz versions newer than 14.2.1 (the one compared with), variation selectors (cmap format 14), the width HarfBuzz gives a space character the font has no glyph for (the result says when the text has one), value record and anchor device tables and variation indexes (they need a pixel size or an instance;hb-shapewithout a size applies none, and the shaper uses the default instance), the features of a font that hasmorxbeyond its defaults (they are not mapped to Apple’s feature settings), language-specific language systems, and the justification (just) of a line. The other layout tables are read and checked against the specification’s structure (offsets, counts, glyphs, nested lookup indexes), not against whether the font shapes a script correctly. - Viewer: Apple’s acnt, bdat, bhed and bloc are decoded (the accented glyphs
with their accents, the strikes and bitmaps through the EBLC/EBDT readers, the bitmap-only
header); the other Apple tables are decoded too, and their state machines are run on the shaped string
(see above) for morx, mort, kerx and the kern machine; the legacy
mortand thekerxformat 4 control points are run from HarfBuzz’s source and Apple’s manual, but no font on this Mac hasmort, so it is checked only against the manual, HarfBuzz’s source and made-up fonts, as aregcidand lookup format 10. The justification category table and the postcompensation actions ofjustare read and their structure is checked; what a justification does to a line is not computed. - Viewer: the variable-font outlines are redrawn at an instance for
glyf(gvar deltas, untouched points interpolated, composites moved) and CFF2 (blend operators), and the advance widths at an instance are listed with HVAR (VVAR for heights) applied, or for a glyf font without HVAR the gvar phantom points, and the control values at an instance are listed with the cvar deltas added (fractions kept: the specification gives no rounding); not applied are hinting (a stated choice: the instructions are fully disassembled and explained below, and what a hint does to the points is not run), the metrics of the component a composite glyph takes its advance from (USE_MY_METRICS) and the GPOS and GDEF variation indexes (they are checked against the store, not applied). avar version 2 is read from HarfBuzz’s source and the Boring Expansion specification: the specification page read for this viewer describes version 1 only. - Viewer: colour glyphs are drawn for COLR version 0 layers and version 1
paints (every paint format, gradients, transforms and the composite and blend modes, each base
glyph clipped to its ClipList box), with the foreground colour (palette entry 0xFFFF) taken from
the “Foreground colour” option, which also colours one-bit bitmaps. SVG glyph documents are drawn
by the viewer’s own small renderer: fills and strokes (caps, joins with the miter limit, dashes),
linear and radial gradients (units, transforms, spread, a focal point, stops with opacity),
clip paths, masks, group opacity, transforms and
use; checked against the browser’s own SVG rendering (WebKit’s) of the same documents for the stroke, clip, mask and gradient cases and for seven glyphs of the two SVG sample fonts (the browser’s renderer ignoresspreadMethodon a radial gradient, which the SVG specification defines, so that one case is the viewer against the specification). Filters, text, images, patterns, markers and CSS style sheets are not drawn, and a table lists, glyph by glyph, which of them each document uses. No font on this Mac has an SVG table. Apple’sflipsbix type is not in the OpenType specification or Apple’s manual; it is drawn as the glyph it names, mirrored left to right, which is inferred from Apple Color Emoji’s glyph names. EBDT/CBDT image format 3 is obsolete with no layout at all. - Viewer: the SVG documents are checked against the content rules of the
OpenType SVG page, each finding listed once for each document with its count: problems for
the “must” rules (UTF-8 and well-formed XML; an
svgroot with the SVG default namespace, and the xlink namespace whenxlink:hrefis used; no other XLink attribute; none oftext,fontand its elements,foreignObject,switch,script,a,view; norgba(), em or ex units, SVG data in animage, colour profile,contentStyleType, CSS2 system colour or XSL; an element with the idglyphN, written without leading zeros, for every glyph of each record’s range) and notes for the “should”, “avoid” and optional items (other namespace declarations, style elements and attributes, filters, patterns, masks, markers, symbols, animation, entities, event attributes, variables defined in the document,var()outside a colour, acolorproperty,cliporoverflowon the root,context-fill). Open: the check reads attributes and CSS declarations, not selectors or the cascade; it does not test thatvar()is the first item of a value, that an image is JPEG or PNG beyond its data URL label, or that the drawn glyph is the same abstract glyph as the outline. For Apple’sblocthe strikes must be in ascending size order (Apple’s manual) and are checked as a problem (by ppemY, then ppemX); the OpenType EBLC and CBLC pages give no order, so those two are not checked. Every problem the decoders find is kept: the problems list (500 at a time, then in pages) has all of them under their true count (the old limits of 40, 60 and 100 messages forkern,name,glyfand CFF charstrings are gone, and so is the 20 offpgmandprep). - Viewer: TrueType instructions (
fpgm,prepand the glyph programs) are disassembled, not executed: what a hint does to the points is not shown, and a jump or call has a target only when its offset or function number is pushed as a constant just before it (the stack of constants is emptied wherever it depends on what ran before). An opcode with no instruction in any specification and no IDEF is reported as a note because it is an error only if it runs. - Viewer: name strings in the Macintosh script encodings Arabic, Hebrew, Greek,
RSymbol, Devanagari, Gurmukhi, Gujarati, Thai and Slavic (Apple’s mapping tables) and in Windows
Johab (Microsoft’s table for code page 1361: every code agrees but one single byte) are decoded.
Apple publishes mapping tables on unicode.org for 27 encodings (read from
Public/MAPPINGS/VENDORS/APPLE/, with its ReadMe); the other Macintosh scripts (Odia, Bangla, Tamil, Telugu, Kannada, Malayalam, Sinhalese, Burmese, Khmer, Lao, Georgian, Armenian, Tibetan, Mongolian, Geez, Vietnamese, Sindhi) have no file there, so there is no published table to decode them with and they are shown as bytes, labelled as not decoded. Apple’s tables for Turkish, Croatian, Icelandic, Romanian, Celtic, Gaelic, Farsi, Ukrainian and Inuit are keyed to the Macintosh system region code, which a name record does not carry, so the viewer decodes a Roman, Arabic or Cyrillic name with the script’s own table. The Japanese, Chinese and Korean scripts use the browser’s nearest decoder (Shift JIS, Big5, EUC-KR, GBK), approximately: compared with Apple’s four tables (7,355, 13,592, 9,489 and 7,611 codes) the browser agrees on 7,029, 13,523, 8,333 and 7,559 codes, differs on 57, 35, 16 and 17, and cannot decode 269, 34, 1,140 and 35 (Apple’s extensions); the tables are too large to carry in the viewer. Windows language IDs are named for about sixty common ones. - Viewer: not checked: that glyph bounding boxes match their points and that
hmtxside bearings matchxMin. Themaxpmaxima for points, contours, composite points and contours, component elements and depth, and the longest glyph program, are checked against the glyphs (a maximum below what the glyphs need is a note); the stack, storage, function and twilight maxima that depend on running the instructions are not.
Unknown
- Reference: the layout of EBDT/CBDT (and Apple’s bdat) image format 4, the compressed
bitmaps. Apple’s bdat page gives only the header (
whiteTreeOffset,blackTreeOffset,glyphDataOffset, then “trees and glyph data”) and calls it a modified Huffman encoding; the OpenType EBDT page says the format is Apple’s, used in some East Asian fonts, and “not supported in OpenType”; FreeType’ssfnt/ttsbit.chas no loader for it (an unknown image format is an error). None of the three, read for this viewer, publishes the layout of the trees or the bit stream, so the viewer shows the three offsets and the data as bytes: this is a fact of the format’s published description, not a gap in the viewer.
Links
Software
Developers
- fontTools – library that reads, edits and writes TrueType fonts (Python)
- FreeType – library that renders glyphs from TrueType fonts (C)
- opentype.js – library that parses and writes TrueType and OpenType fonts (JavaScript)
Reference
- TrueType Reference Manual – Apple’s description of the format and its tables
- OpenType specification – Microsoft’s specification, which also covers TrueType outlines