![]() |
The OpenType layout tables | ![]() |
FreeType gives TeXmacs the outlines and the advance widths of the glyphs of a font, and the legacy kern table if the font has one. Everything else an OpenType font knows about layout lives in its layout tables, which FreeType does not interpret: the MATH table with the geometry of mathematical typesetting, the GSUB table with the glyph substitutions of the features, and the GPOS table with the positioning rules, among them the pair kerning of modern fonts. TeXmacs reads these three tables itself, with small parsers that work directly on the bytes of the font file. This page describes those parsers, the data structures they fill, how the results are cached per font file, and what is deliberately left unread. How the data are used is the subject of the other pages of this chapter.
Plugins/Freetype/tt_tools.hpp,
tt_tools.cpp
The table readers. The file is older than the OpenType work: it also holds the low level access to the tables of a font file (tt_table), the reading of the name table and the glyph analysis used by the font database. The MATH reader goes back to a version written for TeXmacs in 2021 and extended for Mogan; the GSUB and GPOS readers are new.
Plugins/Freetype/tt_face.hpp,
tt_face.cpp
The face of a font file (tt_face_rep), which holds the FreeType face, the bytes of the file, and the parsed tables; the metrics of a face (tt_font_metric_rep), whose kerning method answers from GPOS first.
Plugins/Freetype/unicode_font.cpp
The users of the tables: init_ot_math, the correction hooks, and get_feature_variant and ot_font_features for the features; see mathematics from the MATH table.
All three readers follow the same conventions. They take the whole font file as a string and look the table up with tt_table (buf, 0, tag), that is, in the first font of the file; a missing table gives an empty result, not an error. Numbers are read big endian with get_U16, get_S16 and get_U32, and every offset is relative to the subtable which contains it, as in the specification. Coverage tables (a sorted list of glyph identifiers, or a list of ranges) are read by the static parse_coverage_table, which returns the covered glyphs in coverage order, so that the \(i\)-th entry of a record array belongs to the \(i\)-th covered glyph. Glyphs are always identified by their glyph index in the font, never by a character code.
parse_mathtable (buf) returns an ot_mathtable, a reference counted handle (CONCRETE_NULL) whose nil value means “no MATH table”. Only version 1.0 of the table is accepted. The representation ot_mathtable_rep has one field per part of the table:
A MathConstantsTable: the 51 value records of MathConstants in records, in the order of the specification, and the five plain integers (scriptPercentScaleDown, scriptScriptPercentScaleDown, delimitedSubFormulaMinHeight, displayOperatorMinHeight, radicalDegreeBottomRaisePercent) in fields of their own. The enum MathConstantRecordEnum names all 56 constants, and operator[] answers for both kinds, so that the users write mc[axisHeight] or mc[scriptPercentScaleDown] alike. The values are in design units.
The MathItalicsCorrectionInfo and MathTopAccentAttachment subtables of MathGlyphInfo, as hash maps from a glyph to a MathValueRecord.
The glyphs of the ExtendedShapeCoverage table, whose scripts follow their height (tall operators, delimiters).
The MathKernInfo records: for each covered glyph, up to four MathKernTables, for the top right, top left, bottom right and bottom left corners. A kern table is a staircase: heightCount correction heights and one more kern value than heights.
The vertical and horizontal MathGlyphConstructions: for each covered glyph the list of its size variants, with their advance measurements in the parallel maps ver_glyph_variants_adv and hor_glyph_variants_adv.
The glyph assemblies: for each glyph which can be built from parts, a GlyphAssembly with its GlyphPartRecords (glyph, start and end connector lengths, full advance, and the flag which marks an extender), and the overlap the connectors must have at least.
A MathValueRecord keeps its value and, when the record points to one, the four fields of the header of its device table (hasDevice, deviceTable); the delta values themselves are not read.
The parser honours NULL offsets. Each of the four subtables of MathGlyphInfo is optional, and so are the two coverage tables of MathVariants: a font without horizontal variants, or without MathKernInfo, has a zero offset there. The older parser added the offset to its parent and went on, so that it read the parent table itself as a coverage table; the current code tests every offset before it follows it (parse_record_with_coverage, parse_math_kern_info_table and the tests in parse_mathtable). A NULL GlyphAssembly offset in a construction simply means that the glyph has variants but no assembly (parse_construction returns false).
Three helpers of ot_mathtable_rep answer the questions the font code asks:
True if MathKernInfo covers the glyph and has a table for the given corner.
The kern value of the step of the staircase in which height (in design units) falls: the first value below the first correction height, the last one above the last height, the value between two heights otherwise, and the single value of a table without heights. It must be called after has_kerning.
For a glyph which is a size variant of another one, the glyph whose construction lists it; the glyph itself otherwise. The inverse map is built on the first call, from all vertical and horizontal variants. It lets the extended shape test answer for the large sizes of a delimiter, which the coverage table usually lists only by their base glyph.
dump_mathtable prints a table, and is called when a face is loaded in verbose debugging mode; parse_mathtable (url) reads a file first.
The GSUB reader extracts one feature at a time:
typedef hashmap<unsigned int, array<unsigned int> > ot_gsub_map;
ot_gsub_map parse_gsub_feature (const string& buf, string feature);
array<string> parse_gsub_tags (const string& buf); |
parse_gsub_feature walks the feature list, and for every feature record whose tag is the requested one, every lookup of that feature, and every subtable of the lookup, calls parse_gsub_subtable. The result maps a glyph to its substitutes: one glyph for a single substitution, the alternates in the order of the font for an alternate substitution. Three subtable kinds are read: single substitution formats 1 (a delta added to the glyph index, modulo \(2^{16}\)) and 2 (an explicit list), and alternate substitution format 1; an extension lookup (type 7) is followed to its real type with its 32 bit offset. Any other lookup type is skipped before its coverage table is read, since its fields mean something else. When several lookups or several feature records substitute the same glyph, the first one wins.
parse_gsub_tags lists the feature tags of the table, without repetitions; it is what lets a menu offer only the features a font has (ot_font_features in
unicode_font.cpp
).
Two simplifications are worth knowing. The reader ignores the script and language system lists: a feature counts if any feature record with its tag exists, whatever script it is registered for. And it reads only the substitutions which replace one glyph by one glyph: ligatures (type 4), multiple substitutions (type 2) and the contextual and chaining lookups (types 5, 6 and 8) are not supported. The features which this is enough for, in text and in formulas, are described in OpenType features in text and formulas.
Modern OpenType fonts keep their kerning in the kern feature of GPOS; the legacy kern table, the only one FreeType exposes, is usually absent, and no OpenType math font ships it. parse_gpos_kern (buf) reads the pair adjustment lookups (type 2, or an extension lookup of type 9 which points to one) of every feature record with the tag kern, and returns an ot_gpos_kern handle whose representation has two parts:
The explicit pairs of the format 1 subtables, keyed by (left << 16) | right.
One ot_kern_classes per format 2 subtable: the coverage of the first glyph, the two class definitions, and the matrix of values, of size class1_count times class2_count. Class 0, the default, is not stored in the class maps, so a glyph absent from a map is in class 0.
Only the horizontal advance of the first glyph of a pair is taken (x_advance_offset of the first value format), which is where horizontal kerning lives; a subtable whose first value record has no advance is skipped, and the value record of the second glyph is only used to compute the record size. ot_gpos_kern_rep::get (left, right) looks the pair up in pairs first, then in the class matrices in the order of the font, and answers 0 for a pair which is not kerned. As for GSUB, scripts and languages are ignored.
A tt_face_rep is a resource, shared by all the fonts made from the same file at all sizes. Its constructor reads the whole file into buffer (it already did, to give it to FreeType as a memory face), and parses the MATH table at once into math_table: the math path needs it as soon as a font is made, and a face without the table pays for one table lookup only. The two other tables are parsed on first use and kept:
The substitutions of one feature, cached per tag in gsub_features; a reference into the cache is returned.
The feature list, cached in gsub_tag_list.
The pair kerning, cached in gpos_kern_table.
Since the cache lives in the face, it lives as long as the face resource, that is, for the whole session.
The text of a document is measured and drawn by unicode_font_rep (get_extents, get_xpositions, draw_fixed), which adds fnm->kerning (pc, uc) between consecutive characters of a string. tt_font_metric_rep::kerning converts the two character codes to glyph indices and asks the face for its GPOS kerning; when the font has a non empty ot_gpos_kern, the answer, in design units, is scaled with the horizontal scale of the FreeType size and the legacy table is not consulted at all, even for a pair GPOS does not kern. Only fonts without GPOS kerning fall back on FT_Get_Kerning. Kerning thus applies inside one string, that is, inside one text box; two letters which end up in different boxes, as the single letters of a formula do, are not kerned against each other. The kerning of a script against its base is a different mechanism, the cut-in of MathKernInfo, described in mathematics from the MATH table.
The rest of GPOS: single adjustments, cursive attachment, mark to base and mark to mark positioning (mark, mkmk), and the contextual positioning lookups. Fonts which place combining accents with mark rather than with the top accent attachment of MATH are therefore positioned by the generic accent code.
The ligature, multiple and contextual substitutions of GSUB (see above). Standard ligatures of text are still made, but by the older mechanism of unicode_font_rep, which looks for the precomposed ligature characters U+FB00 to U+FB05 in the font.
The script and language systems of GSUB and GPOS.
The delta values of device tables, whose headers are kept but which no code applies; they would only matter for small sizes on screen.
The italic correction of a glyph assembly (GlyphAssembly::italicsCorrection is read and ignored).
Only the first font of a collection is examined (tt_table (buf, 0, …)).
All lengths in the parsed tables are in design units. Convert them with the factors of the font which uses them (design_unit_to_metric for vertical lengths, design_unit_to_metric_x for horizontal ones, both based on units_per_EM), never with a constant.
The readers trust the font: offsets are not checked against the length of the table. A damaged font can make them read past the end of the table string.
A glyph index of 0 is the missing glyph. get_glyphID in
unicode_font.cpp
returns 0 for a character the font does not have, and the lookups then find nothing, which is the intended answer.
Substituted glyphs have no character code. They are named <@XXXX> (the glyph index in four hexadecimal digits), a name which unicode_font_rep draws directly from the glyph index; see stretchable glyphs: variants and assemblies.