How HarfBuzz Works From Unicode to positioned glyphs, and everything in between
HarfBuzz 15.2.0
Contents

Part VII · Apple Advanced Typography

30. AAT positioning

Apple’s fonts position glyphs with tables of their own. kerx, the extended kerning table, does what Apple’s kern did and more: pairs, classes, state machines, and attaching one glyph to a point on another, with ankr holding the points. trak adds space between letters, or takes it away, by an amount that depends on the point size. HarfBuzz reads all three, in the same step of the pipeline as GPOS (The pipeline, step by step, step 17).

kerx is mostly kern with wider fields, and HarfBuzz runs both with the same code. Legacy kern explains that code: the subtable loop, the kern machine that applies a pair, the coverage bits, cross-stream kerning. This chapter says what is different for kerx, then covers what only kerx has, when the plan uses it, and trak. The state-machine driver that kerx formats 1 and 4 run on is in AAT inside HarfBuzz.

The examples use fonts built by fonts/aat-positioning/build-fonts.py. All are derived from the box-glyph font of Legacy kern, kern.ttf, with its kern table taken out and other tables put in; in those with marks, the combining acute is a small box above the line, hanging to the left of its origin. A few fonts come from HarfBuzz’s own tests. Every shaping result and trace on this page was produced by running HarfBuzz 15.2.0 on them when the page was built.

What kerx changes

The kerx table, Apple’s extended kerning table, has the layout of Apple’s version 1.0 kern with every count, length, and offset widened to 32 bits. The header is a 16-bit version (2, 3, or 4), 16 bits of padding, and a 32-bit count of subtables. Each subtable starts with a 12-byte header: a 32-bit length, 32 bits of coverage, whose low byte is the format, and a 32-bit tupleCount. HarfBuzz’s struct is kerx (hb-aat-layout-kerx-table.hh:1321), another KerxTable beside the two kern versions, with KerxSubTableHeader (:932) as its header and the 32-bit ExtendedTypes for its fields. A version below 2 fails the sanitizer and the table is ignored (minVersion, :1326). Version 3 adds an array after the subtables saying which glyphs each subtable covers; HarfBuzz sanitizes it (:1262) and doesn’t use it.

kerx.ttf has one format 0 subtable with kern.ttf’s five pairs. fontTools doesn’t decode kerx, so this listing, and the others on this page, are made from the font’s bytes by fonts/aat-positioning/views.py:

kerx version 2, 1 subtable
subtable 0: format 0, length 58
  coverage: none
  tupleCount: 0
  nPairs: 5  (then searchRange, entrySelector, rangeShift)
    A V: -80
    T o: -100
    ko ko: -100
    ka ka: -100
    bet gimel: -100
kerx.ttf: kerx (TTX)

A pair is applied by the same kern machine as in kern, so the value is split between the two glyphs’ advances, marks are passed over, and pairs are read left to right, which in Hebrew is the reverse of the logical order (How the kerning is applied). The difference is which scripts get it. The plan doesn’t ask the script shaper before using kerx, as it does for kern (When the plan uses kern), so Thai and Devanagari are kerned too:

FontTextOptionshb-shape output
kerx.ttfAVToU+0041 U+0056 U+0054 U+006F--language=en[A=0+560|V=1@-40,0+560|T=2+550|o=3@-50,0+450]
kerx.ttfกกU+0E01 U+0E01--language=th[ko=0+550|ko=1@-50,0+550]
kerx.ttfककU+0915 U+0915--language=hi[ka=0+550|ka=1@-50,0+550]
kerx.ttfגבU+05D2 U+05D1--language=he[bet=1+550|gimel=0@-50,0+350]
kerx.ttfAVToU+0041 U+0056 U+0054 U+006F--language=en --features=-kern[A=0+600|V=1+600|T=2+600|o=3+500]

Turning off kern turns off pair kerning, as with the kern table: formats 0, 2, and 6 check requested_kerning and the glyphs’ kern mask (:125).

Coverage

The coverage bits are those of Apple’s kern with one more, moved to the top of a 32-bit field (:939):

Bit Meaning What HarfBuzz does
Vertical (0x80000000) the subtable is for vertical text runs it only in vertical text, and the other subtables only in horizontal (:1145)
CrossStream (0x40000000) values move glyphs across the line as in kern; see Cross-stream
Variation (0x20000000) values vary runs the subtable, unlike in kern, which skips it (:1142); see Tuples
Backwards (0x10000000) process the glyphs from last to first formats 1 and 4: the buffer is reversed before the subtable; formats 0, 2, and 6: the subtable is skipped (:128)

In horizontal text the buffer is put in left-to-right order before a subtable runs, reversing a right-to-left buffer, and Backwards reverses that once more: in right-to-left text, a subtable with Backwards set sees the glyphs in logical order (:1168). HarfBuzz’s comment on the bit says it “only applies to state-table based ‘kerx’ subtables (types 1 and 4)” (:944), and its pair formats return without kerning when it is set. backwards.ttf is kerx.ttf with the bit set:

FontTextOptionshb-shape output
backwards.ttfAVToU+0041 U+0056 U+0054 U+006F--language=en[A=0+600|V=1+600|T=2+600|o=3+500]

As in kern, the vertical formats 0, 2, and 6 can only kern when vkrn is asked for, since the kerning mask in vertical text is vkrn’s, and it isn’t on by default (Vertical subtables; read from the code).

Tuples

With a non-zero tupleCount, every kerning value in the subtable is instead the offset of an array of tupleCount values, which Apple meant for kerning that varies with the font’s variation axes. HarfBuzz reads the first value of the array and nothing else (kerxTupleKern, :47). Its NEWS for 2.0.1 calls this “variation-kerning format in kerx table, although without variation”, and 2.1.0’s says reading tuples “Fixes kerning with Apple default San Francisco fonts”. tuples.ttf has the Variation bit and three values for each pair, the second and third twice and three times the first:

kerx version 2, 1 subtable
subtable 0: format 0, length 88
  coverage: Variation
  tupleCount: 3
  nPairs: 5  (then searchRange, entrySelector, rangeShift)
    A V: offset 58 → [-80, -160, -240]
    T o: offset 64 → [-100, -200, -300]
    ko ko: offset 70 → [-100, -200, -300]
    ka ka: offset 76 → [-100, -200, -300]
    bet gimel: offset 82 → [-100, -200, -300]
tuples.ttf: kerx (TTX)
FontTextOptionshb-shape output
tuples.ttfAVToU+0041 U+0056 U+0054 U+006F--language=en[A=0+560|V=1@-40,0+560|T=2+550|o=3@-50,0+450]

The pairs get the first value. Kerning in a variable font with kerx doesn’t vary in HarfBuzz.

Five formats

KerxSubTable::dispatch (:976) reads formats 0, 1, 2, 4, and 6. A subtable of any other format is skipped.

Format What it holds Applied by Runs with kern off
0 sorted pairs the kern machine no
1 a state machine that pushes glyphs and applies lists of values the state-machine driver only if cross-stream
2 a left and a right class table, and an array the kern machine no
4 a state machine that attaches a glyph to a point on another the state-machine driver yes
6 a row and a column lookup, and an array of 16- or 32-bit values the kern machine no

Format 3, kern’s compact format, has no kerx version. Formats 4 and 6 are new.

Before running a subtable, KerxTable::apply checks whether any glyph in the buffer could start an action, or be the first glyph of a pair, from sets gathered once per face. If none can, the trace says “skipped subtable n because no glyph matches” (:1156).

Formats 0, 2, and 6

Format 0 is kern’s format 0 with a 32-bit count and search fields; the pairs themselves are still two 16-bit glyph IDs and a 16-bit value, and they must still be sorted. Format 2 is kern’s class array, with AAT lookup tables for its classes. Format 6 is new: a row lookup gives the left glyph a value, a column lookup gives the right glyph one, and the kerning value is the entry of the array at their sum (:806). A flag, ValuesAreLong, makes the lookups and the array 32-bit. format2.ttf and format6.ttf hold the Latin pairs in each format:

kerx version 2, 1 subtable
subtable 0: format 6, length 74
  coverage: none
  tupleCount: 0
  flags: 0x0, rowCount: 3, columnCount: 3
  row indices:    A: 3, T: 6
  column indices: V: 1, o: 2
  array: [0, 0, 0, 0, -80, 0, 0, 0, -100]
format6.ttf: kerx (TTX)
FontTextOptionshb-shape output
kerx.ttfAVToU+0041 U+0056 U+0054 U+006F--language=en[A=0+560|V=1@-40,0+560|T=2+550|o=3@-50,0+450]
format2.ttfAVToU+0041 U+0056 U+0054 U+006F--language=en[A=0+560|V=1@-40,0+560|T=2+550|o=3@-50,0+450]
format6.ttfAVToU+0041 U+0056 U+0054 U+006F--language=en[A=0+560|V=1@-40,0+560|T=2+550|o=3@-50,0+450]

In format 6 the row value is already an index into the array, a row number times columnCount, so A’s 3 and V’s 1 find −80. HarfBuzz doesn’t check the sum against rowCount and columnCount, only that the value it finds is inside the table (read from the code). A glyph missing from either lookup counts as 0, and so takes the value in the array’s first row or column, which should be 0.

Format 2 counts differently in kerx. In kern, a class value is a byte offset from the start of the subtable (Four subtable formats); in kerx, HarfBuzz takes the sum of the two class values as a byte offset from the start of the array, and halves it to get an index (ExtendedTypes::byteOffsetToIndex, hb-aat-layout-common.hh:1317). So A’s row is at 6 and T’s at 12, as format2.ttf’s listing shows:

kerx version 2, 1 subtable
subtable 0: format 2, length 66
  coverage: none
  tupleCount: 0
  rowWidth: 6
  left classes:  A: 6, T: 12
  right classes: V: 2, o: 4
  array: [0, 0, 0, 0, -80, 0, 0, 0, -100]
format2.ttf: kerx (TTX)

HarfBuzz 15.2.0’s NEWS lists a fix for “wrong kerx kerning (formats 1 and 2)”, and HarfBuzz’s tests have a format 2 font built by hand to check the counting, with the comment “Left offsets: 0,6,12,0,0; right offsets: 0,0,2,4,0” (test/shape/data/in-house/tests/aat-kerx.tests, issue #6461).

Format 1

Format 1 is the state machine of kern’s format 1 (Four subtable formats): an entry can push the current glyph, and an action applies a list of values to the glyphs pushed, the whole value to each glyph’s advance and offset. In kerx three things differ:

A value is applied only to a glyph that has the kern mask, and a glyph that gets one is unsafe to break from there to the current glyph. This font from those tests gives three glyphs values of 50, 40, and 31; a kern list couldn’t hold the odd 31, since its low bit would end the list:

FontTextOptionshb-shape output
hb:test/shape/data/in-house/fonts/36f8d640bc42f35a59e607bcf7c4aa70b63d4ef4.ttfaabU+0061 U+0061 U+0062--language=en[a=0@50,0+611|a=1@40,0+601|b=2@31,0+646]

Attachment: format 4 and ankr

Format 4 doesn’t kern. It attaches: it moves a glyph so that a point on it sits on a point on another glyph, as GPOS’s mark attachment does. Its state machine has one flag of its own, Mark, which remembers the current glyph as the marked glyph. An entry with an action then positions the current glyph against the marked one. Each action names two points, one on each glyph, and how the points are found depends on the subtable’s action type, the top two bits of a 32-bit flags field that follows the state machine’s header (:610):

Action type An action holds The point comes from
0, control point actions two point numbers the glyph’s outline, through the font funcs
1, anchor point actions two anchor numbers the ankr table
2, control point coordinate actions two pairs of coordinates the action itself

The low 24 bits of the flags give the offset of the actions, from the start of the state machine.

The ankr table, Apple’s anchor point table, lists points for each glyph that needs them: a lookup table from glyph to an offset, and at each offset a 32-bit count and that many x, y pairs in font units (hb-aat-layout-ankr-table.hh:59). An anchor number is an index into a glyph’s list. A glyph that isn’t in the lookup, or an index past the end of its list, gives the point (0, 0) (:63). The table must have version 0, or the sanitizer rejects it. Only kerx format 4 reads it.

anchors.ttf has a format 4 subtable whose machine puts A, T, V, and o in one class and the acute in another. A letter is marked; an acute after it gets action 0, which names anchor 0 on each glyph:

kerx version 2, 1 subtable
subtable 0: format 4, length 94
  coverage: none
  tupleCount: 0
  nClasses: 6
  classes: A: 4, T: 4, V: 4, o: 4, acutecomb: 5
  state 0: entries by class [0, 0, 0, 0, 1, 2]
  state 1: entries by class [0, 0, 0, 0, 1, 2]
  entry 0: newState 0, no action
  entry 1: newState 0, Mark, no action
  entry 2: newState 0, action 0
  flags: action type 1 (anchor point actions), actions at 78
  action 0: marked glyph's anchor 0, current glyph's anchor 0
anchors.ttf: kerx (TTX)
<ankr>
  <AnchorPoints Format="0">
    <Flags value="0"/>
    <Anchors>
      <Lookup glyph="A">
        <!-- AnchorPointCount=1 -->
        <AnchorPoint index="0">
          <XCoordinate value="300"/>
          <YCoordinate value="600"/>
        </AnchorPoint>
      </Lookup>
      <Lookup glyph="T">
        <!-- AnchorPointCount=1 -->
        <AnchorPoint index="0">
          <XCoordinate value="300"/>
          <YCoordinate value="600"/>
        </AnchorPoint>
      </Lookup>
      <Lookup glyph="V">
        <!-- AnchorPointCount=1 -->
        <AnchorPoint index="0">
          <XCoordinate value="300"/>
          <YCoordinate value="600"/>
        </AnchorPoint>
      </Lookup>
      <Lookup glyph="acutecomb">
        <!-- AnchorPointCount=1 -->
        <AnchorPoint index="0">
          <XCoordinate value="-300"/>
          <YCoordinate value="520"/>
        </AnchorPoint>
      </Lookup>
      <Lookup glyph="o">
        <!-- AnchorPointCount=1 -->
        <AnchorPoint index="0">
          <XCoordinate value="250"/>
          <YCoordinate value="520"/>
        </AnchorPoint>
      </Lookup>
    </Anchors>
  </AnchorPoints>
</ankr>
anchors.ttf: ankr (TTX)

The action, trimmed to the anchor case and what follows it, with its namespaces shortened (:696–748):

case 1: /* Anchor Point Actions. */
{
  const HBUINT16 *data = &ankrData[entry.data.ankrActionIndex * 2];
  unsigned int markAnchorPoint = *data++;
  unsigned int currAnchorPoint = *data++;
  const Anchor &markAnchor = c->ankr_table->get_anchor (c->buffer->info[mark].codepoint,
                                                        markAnchorPoint, num_glyphs);
  const Anchor &currAnchor = c->ankr_table->get_anchor (c->buffer->cur ().codepoint,
                                                        currAnchorPoint, num_glyphs);

  o.x_offset = hb_saturate_sub (c->font->em_scale_x (markAnchor.xCoordinate),
                                c->font->em_scale_x (currAnchor.xCoordinate));
  o.y_offset = hb_saturate_sub (c->font->em_scale_y (markAnchor.yCoordinate),
                                c->font->em_scale_y (currAnchor.yCoordinate));
}
break;
/* … */
o.attach_type() = ATTACH_TYPE_MARK;
o.attach_chain() = attach_chain; /* mark − current, in logical order */

So the acute’s offset becomes the difference between the two anchors, and it is linked to the marked glyph as a GPOS mark is linked to its base. When attachments are resolved after positioning, the advances between the two are taken off (Resolving attachments). The trace shows the offsets as the subtable leaves them, before that:

hb-shape anchors.ttf "A◌́o◌́" --language=en --trace
start table kerx[A=0+600|acutecomb=0+0|o=2+500|acutecomb=2+0]
start subtable 0[A=0+600|acutecomb=0+0|o=2+500|acutecomb=2+0]
end subtable 0[A=0+600|acutecomb=0@600,80+0|o=2+500|acutecomb=2@550,0+0]
end table kerx[A=0+600|acutecomb=0@600,80+0|o=2+500|acutecomb=2@550,0+0]
FontTextOptionshb-shape output
anchors.ttfA◌́o◌́U+0041 U+0301 U+006F U+0301--language=en[A=0+600|acutecomb=0@0,80+0|o=2+500|acutecomb=2@50,0+0]
anchors.ttfA◌́o◌́U+0041 U+0301 U+006F U+0301--language=en --features=-kern[A=0+600|acutecomb=0@0,80+0|o=2+500|acutecomb=2@50,0+0]

On A the acute moves up 80 units, to A’s higher anchor; on the narrower o it moves right by 50, to o’s center. Format 4 doesn’t look at the kern feature or the kerning mask at all (:760), so -kern changes nothing.

default
[A=0+600|acutecomb=0@0,80+0|o=2+500|acutecomb=2@50,0+0]
hb-vector anchors.ttf "A◌́o◌́" --language=en

Coordinate actions put the two points in the action. coords.ttf has the same machine with A’s point and the acute’s point as coordinates:

FontTextOptionshb-shape output
coords.ttfA◌́o◌́U+0041 U+0301 U+006F U+0301--language=en[A=0+600|acutecomb=0@0,80+0|o=2+500|acutecomb=2@100,80+0]

The acute on A is placed as before; on o it is placed as if o were A, 100 to the right and 80 up. The coordinates belong to the action, not to the glyphs, so a font with coordinate actions needs an entry and an action for every combination that is placed differently.

Control point actions ask the font funcs for a point of the glyph’s outline, with get_glyph_contour_point_for_origin. None of HarfBuzz’s own font funcs provides it. The hb-ot, FreeType, and Fontations funcs each have an implementation, and each registers it inside if (false) (hb-ot-font.cc:1004, hb-ft.cc:1174, rust/font.rs:1495), so only a client’s own funcs can answer (Fonts and font functions). FreeType’s did until 15.2.0 turned it off (#6762). If either point can’t be found, the action does nothing (hb-aat-layout-kerx-table.hh:681). points.ttf names A’s point 1, its top left corner, and the acute’s point 0:

FontTextOptionshb-shape output
points.ttfA◌́o◌́U+0041 U+0301 U+006F U+0301--language=en[A=0+600|acutecomb=0+0|o=2+500|acutecomb=2+0]

Nothing is attached, and the acutes are left where their outlines put them; --font-funcs=ft gives the same. GPOS’s anchor format 2, which also names an outline point, falls back to its x and y when the point can’t be found (Anchors); a kerx control point action has nothing to fall back to.

Before and after

The marked glyph can come after the glyph attached to it. In left-to-right text with Backwards set, the machine walks the buffer from the end, so the glyph it marks is later in the text than the one it then positions. GPOS can’t do that, but the code that resolves attachments allows it, adding the advances between the two instead of taking them off: “This can happen with kerx: a mark attaching to a base after it in the logical order” (OT/Layout/GPOS/GPOS.hh:142). This font from HarfBuzz’s tests has a cross-stream format 0 subtable, then a cross-stream format 1, then a format 4 with Backwards set, which attaches the m at the start of the text to the first b, two glyphs later:

FontTextOptionshb-shape output
hb:test/shape/data/in-house/fonts/1bfb8aab9b49ef30e8deae3db271ca3dbc6208a1.ttfxzbbU+0078 U+007A U+0062 U+0062--language=en[m=0@925,600+150|c=1+600|b=2+615|b=3@0,100+615]

The m is moved 925 units right, past its own advance and the c’s, onto the b; the last b has been raised 100 by the cross-stream subtable.

In right-to-left text it is the other way round. The buffer has been reversed into left-to-right order, so a subtable without Backwards would meet each Arabic mark before its base. Setting Backwards puts the glyphs back in logical order, base first. This Arabic font from HarfBuzz’s tests has two format 4 subtables, both with Backwards set, which place the vowel marks on the letters and the second damma on the shadda:

FontTextOptionshb-shape output
hb:test/shape/data/in-house/fonts/a8b4a746c2d1a1fd5d5d6108e366e79c0ae74ef5.ttfكُلُّU+0643 U+064F U+0644 U+064F U+0651--language=ar[uni064F=2@112,-32+0|uni0651=2@128,-154+0|uni0644.fina=2+591|uni064F=0@56,-105+0|uni0643.init=0+415]
default
[uni064F=2@112,-32+0|uni0651=2@128,-154+0|uni0644.fina=2+591|uni064F=0@56,-105+0|uni0643.init=0+415]
hb-vector hb:test/shape/data/in-house/fonts/a8b4a746c2d1a1fd5d5d6108e366e79c0ae74ef5.ttf "كُلُّ" --language=ar

An attached glyph also takes the cross-stream offset of the glyph it is attached to, followed back along any cross-stream links (resolve_cross_offset, OT/Layout/GPOS/MarkArray.hh:12, called at hb-aat-layout-kerx-table.hh:737), so a mark stays on a base that cross-stream kerning has raised. Its own cross-stream link is replaced by the attachment.

For type designers: format 4 sets only the attached glyph’s offset. It doesn’t zero its advance, and a plan that runs kerx doesn’t zero marks either (next section). A mark meant to be attached by kerx needs an advance of 0 in hmtx.

Cross-stream

A cross-stream subtable in kerx works as it does in kern (Cross-stream). At the first one, every glyph is linked to the glyph before it as if by a cursive attachment (:1174). A value is written into the second glyph’s offset across the line, and when the links are resolved, each glyph carries the offsets of the glyphs before it. cross.ttf is kerx.ttf with a second, cross-stream subtable that has A V +100 and bet gimel +100:

kerx version 2, 2 subtables
subtable 0: format 0, length 58
  coverage: none
  tupleCount: 0
  nPairs: 5  (then searchRange, entrySelector, rangeShift)
    A V: -80
    T o: -100
    ko ko: -100
    ka ka: -100
    bet gimel: -100
subtable 1: format 0, length 40
  coverage: CrossStream
  tupleCount: 0
  nPairs: 2  (then searchRange, entrySelector, rangeShift)
    A V: 100
    bet gimel: 100
cross.ttf: kerx (TTX)
FontTextOptionshb-shape output
cross.ttfAVATU+0041 U+0056 U+0041 U+0054--language=en[A=0+560|V=1@-40,100+560|A=2@0,100+600|T=3@0,100+600]
cross.ttfגבגבU+05D2 U+05D1 U+05D2 U+05D1--language=he[bet=3+550|gimel=2@-50,100+350|bet=1@0,100+550|gimel=0@-50,200+350]
default
[A=0+560|V=1@-40,100+560|A=2@0,100+600|T=3@0,100+600]
hb-vector cross.ttf "AVAT" --language=en

The V is raised, and everything after it. In Hebrew the pairs are read left to right, so the raise builds up leftwards: the second pair, at the left end, is 200 up. The links are always set for the buffer’s logical order, even when an earlier subtable has left the buffer turned round, and they are resolved in that order, so it makes no difference whether the cross-stream subtable comes first; cross-first.ttf has the two subtables the other way round:

FontTextOptionshb-shape output
cross-first.ttfגבגבU+05D2 U+05D1 U+05D2 U+05D1--language=he[bet=3+550|gimel=2@-50,100+350|bet=1@0,100+550|gimel=0@-50,200+350]

In formats 0, 2, and 6 the kern machine sets the second glyph’s cross-stream offset to the value, rather than adding to it (hb-kern.hh:114), so of two cross-stream subtables that kern the same pair, the later one wins. Format 1 adds its values. Both treat the value 0x8000 as a reset: the glyph’s link is cut and its cross-stream offset set to 0 (hb-kern.hh:97 for the kern machine). Before 15.2.0, the kern machine took 0x8000 as −32,768 (read from the code). The comment in format 1 calls the reset “undocumented in the spec, but described in the ‘kern’ table example” (hb-aat-layout-kerx-table.hh:386). Format 1 also kerns across a vertical line, where the comment says “CoreText doesn’t do crossStream kerning in vertical. We do.” (:411).

When kerx is used

The plan chooses between GPOS, kerx, kern, and fallback kerning once, when it is made (hb-ot-shape.cc:153–183). Fallbacks quotes the code; for kerx it comes to two tests:

  1. If the font has kerx and doesn’t have both GSUB and GPOS, kerx is used, and GPOS isn’t. GSUB counts only if it will be applied, which it isn’t when the font is shaped with morx (AAT inside HarfBuzz).
  2. Otherwise GPOS is used, and if it has no kern feature in the language system it chose, and the font has kerx, kerx is used as well.

The first test is HarfBuzz issue #3008: “Prefer GPOS over kerx if GSUB is present”. Its NEWS entry, in 2.8.2, says “GPOS is preferred over kerx, if GSUB was applied”. A font with kerx never uses its kern table, and never gets fallback kerning. plan->position (:267) then runs GPOS if the plan chose it, and then kerx if the plan chose that. When both run, kerx skips its state-machine subtables, formats 1 and 4, and its cross-stream ones: “Only simple, in-stream kerning may run alongside GPOS” (hb-aat-layout-kerx-table.hh:1136). A cross-stream subtable would link every glyph into a chain, and so break the links GPOS has made for its marks. Before 15.2.0, the second test chose kerx but position ran only GPOS, so the kerx never ran (HHW-38, fixed by #6761).

The font has Positioning
kerx kerx
kerx and GPOS kerx
kerx, GSUB, and GPOS with kern GPOS
kerx, GSUB, and GPOS without kern GPOS, then kerx’s in-stream pair subtables (since 15.2.0)
kerx, morx, GSUB, and GPOS kerx (GSUB isn’t applied)
morx and GPOS, no kerx GPOS (since 3.1.0)

kerx-gpos.ttf is kerx.ttf with a GPOS that has kern (A V −50) and mark; kerx-gsub-gpos.ttf adds a GSUB, whose one feature is off by default; morx-kerx.ttf adds a morx table that does nothing to this text; morx-gpos.ttf is that without kerx:

FontTextOptionshb-shape output
kerx-gpos.ttfAVToU+0041 U+0056 U+0054 U+006F--language=en[A=0+560|V=1@-40,0+560|T=2+550|o=3@-50,0+450]
kerx-gsub-gpos.ttfAVToU+0041 U+0056 U+0054 U+006F--language=en[A=0+550|V=1+600|T=2+600|o=3+500]
morx-kerx.ttfAVToU+0041 U+0056 U+0054 U+006F--language=en[A=0+560|V=1@-40,0+560|T=2+550|o=3@-50,0+450]
morx-gpos.ttfAVToU+0041 U+0056 U+0054 U+006F--language=en[A=0+550|V=1+600|T=2+600|o=3+500]

With kerx, A V is −80, split, and T o is kerned; with GPOS, A V is GPOS’s −50, all on the A, and T o isn’t kerned. Here, where GPOS has kern, the two tables are never combined.

What else kerx turns off

The plan treats a font whose kerx runs without GPOS as positioning its own marks. When apply_kerx is set and apply_gpos isn’t, marks aren’t zeroed; whenever apply_kerx is set, zeroed marks aren’t moved back, and there is no fallback mark positioning (hb-ot-shape.cc:185–203; the table in Who decides lists them). In kerx.ttf, whose kerx attaches nothing, nothing places the acute:

FontTextOptionshb-shape output
kerx.ttfA◌́VU+0041 U+0301 U+0056--language=en[A=0+560|acutecomb=0+0|V=2@-40,0+560]

When the second test above runs kerx after GPOS, GPOS places the marks, and they are zeroed as in any GPOS font. mark-gpos.ttf has a GSUB, a GPOS with mark and no kern, and an acute with an advance of 200; mark-gpos-kerx.ttf is the same with kerx.ttf’s kerx added:

FontTextOptionshb-shape output
mark-gpos.ttfA◌́VU+0041 U+0301 U+0056--language=en[A=0+600|acutecomb=0@0,80+0|V=2+600]
mark-gpos-kerx.ttfA◌́VU+0041 U+0301 U+0056--language=en[A=0+560|acutecomb=0@40,80+0|V=2@-40,0+560]
mark-gpos-kerx.ttfAVToU+0041 U+0056 U+0054 U+006F--language=en[A=0+560|V=1@-40,0+560|T=2+550|o=3@-50,0+450]

With kerx in the font, GPOS attaches the acute and its advance is zeroed, and then kerx kerns A V by −80 and T o by −100. The acute stays on the A: its offset grows by 40, as the A’s advance shrinks by 40, because attachments are resolved after both tables have run. Before 15.2.0, position ran only GPOS, but the mark decisions were made as if kerx ran, so the acute kept its 200-unit advance and the pairs weren’t kerned (HHW-38).

Tracking: trak

Tracking is a uniform change to the space between letters: looser at small sizes, where letters need room to stay legible, and tighter at display sizes. Apple’s trak table, the tracking table, gives the amount per point size, so that a font can carry its own size-dependent spacing.

The table has a header (version 1.0, a format of 0, and offsets to horizontal and vertical data) and, for each direction, a list of point sizes and a list of tracks. A track is a degree of tightness, such as −1 for tight, 0 for normal, and 1 for loose, with a name, and a value in font units for each size (hb-aat-layout-trak-table.hh:46–189). trak.ttf has three horizontal tracks over four sizes, and one vertical track:

<trak>
  <version value="1.0"/>
  <format value="0"/>
  <horizData>
    <!-- nTracks=3, nSizes=4 -->
    <trackEntry value="-1.0" nameIndex="257">
      <!-- Tight -->
      <track size="6.0" value="20"/>
      <track size="12.0" value="-30"/>
      <track size="24.0" value="-50"/>
      <track size="72.0" value="-70"/>
    </trackEntry>
    <trackEntry value="0.0" nameIndex="256">
      <!-- Normal -->
      <track size="6.0" value="50"/>
      <track size="12.0" value="0"/>
      <track size="24.0" value="-20"/>
      <track size="72.0" value="-40"/>
    </trackEntry>
    <trackEntry value="1.0" nameIndex="258">
      <!-- Loose -->
      <track size="6.0" value="80"/>
      <track size="12.0" value="30"/>
      <track size="24.0" value="10"/>
      <track size="72.0" value="-10"/>
    </trackEntry>
  </horizData>
  <vertData>
    <!-- nTracks=1, nSizes=1 -->
    <trackEntry value="0.0" nameIndex="256">
      <!-- Normal -->
      <track size="12.0" value="100"/>
    </trackEntry>
  </vertData>
</trak>
trak.ttf: trak (TTX)

Which value

HarfBuzz always uses track 0 (hb_aat_layout_track passes no other, hb-aat-layout.cc:395), so Tight and Loose are never read. If a font had no track 0, TrackData::get_tracking would interpolate between the tracks either side of it, and a table with only one track uses that one, whatever its value (:132).

The point size is the font’s ptem, or 12 points if it isn’t set, Core Text’s default (HB_CORETEXT_DEFAULT_FONT_SIZE, hb-aat-layout.hh:36; Three kinds of size). Below the first size in the table the first size’s value is used, above the last the last one’s, and in between the value is interpolated in a straight line between the two sizes either side (TrackTableEntry::get_value, hb-aat-layout-trak-table.hh:75). The result is scaled from font units like any other value:

FontTextOptionshb-shape output
trak.ttfAVU+0041 U+0056--language=en --font-ptem=3[A=0+650|V=1+650]
trak.ttfAVU+0041 U+0056--language=en --font-ptem=6[A=0+650|V=1+650]
trak.ttfAVU+0041 U+0056--language=en --font-ptem=9[A=0+625|V=1+625]
trak.ttfAVU+0041 U+0056--language=en[A=0+600|V=1+600]
trak.ttfAVU+0041 U+0056--language=en --font-ptem=18[A=0+590|V=1+590]
trak.ttfAVU+0041 U+0056--language=en --font-ptem=72[A=0+560|V=1+560]
trak.ttfAVU+0041 U+0056--language=en --font-ptem=144[A=0+560|V=1+560]

At 3 points the letters get 6 points’ +50; at 9, halfway between 6 and 12, +25; with no size set, 12 points’ 0; at 18, halfway between 12 and 24, −10; at 144, 72 points’ −40. HarfBuzz’s own test, aat-trak.tests, does the same with the font TRAK.ttf. The values are not extrapolated beyond the table’s sizes.

Where the space goes

trak::apply (:220) adds the value to the advance of the first glyph of each grapheme, found by the continuation bits that Unicode inside HarfBuzz describes. A mark doesn’t start a grapheme, so it gets nothing. (The acute’s offset here is fallback mark positioning’s, which runs later and centers it over the tracked A.)

FontTextOptionshb-shape output
trak.ttfA◌́VU+0041 U+0301 U+0056--language=en --font-ptem=6[A=0+650|acutecomb=0@-350,562+0|V=2+650]
trak.ttfגבU+05D2 U+05D1--language=he --font-ptem=6[bet=1+650|gimel=0+450]

The space is added to an advance, so it lies to the right of each grapheme in the output. In left-to-right text that is after each one, and the line ends with it; in right-to-left text it is before each one in reading order, between a letter and the one read before it, and the line starts with it. Within a grapheme of several glyphs only the first glyph’s advance changes, so its glyphs aren’t pulled apart.

trak runs last in step 17, after GPOS, kerx, and kern (hb-ot-shape.cc:285), but before attachments are resolved. A mark attached to a base is placed from the base’s advance as it is then, tracking included, so it stays on its base.

Vertical tracking

In vertical text the value comes from the vertical track table and is subtracted from the vertical advance (hb-aat-layout-trak-table.hh:244). trak.ttf’s vertical track is +100 at every size:

FontTextOptionshb-shape output
trak.ttfAVU+0041 U+0056--language=en --direction=ttb[A=0@-300,-750+0,-1100|V=1@-300,-750+0,-1100]

The advance has gone from −1,000 to −1,100. Vertical advances are negative, growing downwards, so the value is subtracted, and a positive value loosens a column as it loosens a line. GPOS negates a vertical advance from a value record for the same reason, “y_advance values grow downward but font-space grows upward” (ValueFormat.hh:136). Before 15.2.0, trak added the value, so a positive value brought the glyphs closer (HHW-39, fixed by #6700). Native Core Text layout loosens a column too, as measured for #6716.

When it applies

The plan applies trak when the font has one and also has a STAT table (hb-ot-shape.cc:212):

/* According to Ned, trak is applied by default for "modern fonts", as detected by presence of STAT table. */
#ifndef HB_NO_STYLE
  plan.apply_trak = hb_aat_layout_has_tracking (face) && face->table.STAT->has_data ();
#else
  plan.apply_trak = false;
#endif

trak-nostat.ttf is trak.ttf without STAT:

FontTextOptionshb-shape output
trak.ttfAVU+0041 U+0056--language=en --font-ptem=6[A=0+650|V=1+650]
trak-nostat.ttfAVU+0041 U+0056--language=en --font-ptem=6[A=0+600|V=1+600]

Nothing else is asked. A font doesn’t need morx or kerx for its trak to apply, nor to be shaped without GPOS, and no feature turns it off. 10.3.0 moved tracking out of shaping and into the advances that the hb-ot font funcs return, and dropped “The ‘trak’ pseudo OpenType feature that could be used to disable ‘trak’ table application”; 11.0.1 moved it back, because the change “required every font functions implementation to handle it”, and the feature didn’t come back (NEWS).

The same test is in trak::get_coretext_tracking (hb-aat-layout-trak-table.hh:207), which the Core Text font funcs use to take Core Text’s own tracking back out of the advances it reports (The other funcs). It reads the horizontal track table for both directions, because, as its comment says, “CoreText adds horizontal tracking in both orientations” (:212); that came with #6716, in 15.2.0. hb_aat_layout_has_tracking, public since 2.3.0, says only whether the table is there.

Where to read the source

File What’s there
src/hb-aat-layout-kerx-table.hh kerx and KerxTable, shared with kern: the header, coverage bits, subtable loop, cross-stream links, and sanitizing; formats 0, 1, 2, 4, and 6; kerxTupleKern
src/hb-aat-layout-ankr-table.hh ankr and get_anchor
src/hb-aat-layout-trak-table.hh trak: choosing a track, interpolating between sizes, apply
src/hb-aat-layout.cc hb_aat_layout_position (:363), which sets up ankr for kerx; hb_aat_layout_track (:395)
src/hb-aat-layout-common.hh ExtendedTypes, the lookup tables, the state-machine driver
src/hb-kern.hh the kern machine, used by formats 0, 2, and 6
src/hb-ot-shape.cc the plan’s choice of positioning (:153), trak (:212), hb_ot_shape_plan_t::position (:267)
src/OT/Layout/GPOS/GPOS.hh, MarkArray.hh resolving attachments, including a base after its mark; resolve_cross_offset
test/shape/data/in-house/tests/ aat-kerx.tests, aat-trak.tests

HB_NO_AAT_SHAPE leaves out kerx, ankr, and trak with the rest of AAT shaping; HB_NO_AAT sets it, and HB_MINI sets that (hb-config.hh:130). HB_NO_STYLE, which HB_LEAN sets (:80), turns trak off. A kerx table may have at most HB_AAT_MAX_SUBTABLES, 1,024, subtables, or it is rejected whole (hb-limits.hh:110).

In short