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

Part V · The pipeline

18. GSUB: substituting glyphs

By the time HarfBuzz opens a font’s GSUB table, the hard questions about characters have been answered. The text has been segmented, its script and direction are known, and it has been normalized, which is where every character was mapped to a glyph through cmap. The pipeline, step by step shows where GSUB comes in the whole of shaping. What arrives here is a buffer of glyph IDs, each still carrying the cluster number of the character it came from and a set of Unicode properties.

GSUB’s job is to change which glyphs are in that buffer. It swaps one glyph for another, splits one into several, joins several into one, and does any of those only in certain surroundings. It never moves anything: positions are GPOS’s job, in the next chapter. This chapter is about how HarfBuzz reads GSUB and runs it: the order things happen in, which glyphs a lookup can see, and the many small decisions that the OpenType specification leaves open, which HarfBuzz settles in particular ways.

Every example comes from one small test font, built from the feature file fonts/gsub/ligatures.fea and two variants of it. Its glyphs are plain boxes, since only their names, their cmap, and their layout tables matter here. Every shaping result and trace on this page was produced by running HarfBuzz 15.2.0 on those fonts when the page was built.

The shape of the table

A GSUB table has three lists, plus an optional fourth:

The first two, together with FeatureVariations, belong to planning. Before shaping starts, HarfBuzz works out from them which lookups this text needs, in what order, and which glyphs each should touch. That is the subject of Shape plans and their cache and From features to lookups. This chapter starts where planning ends: with an ordered list of lookups, each tagged with the feature that asked for it, a bit mask, and a few flags.

A lookup is a type, a set of flags, and a list of subtables. All of a lookup’s subtables have the same type. When a lookup runs, HarfBuzz tries its subtables in order at each glyph, and the first one that does something wins. GSUB has eight lookup types:

Type Name What it does
1 Single one glyph → one glyph
2 Multiple one glyph → a sequence of glyphs
3 Alternate one glyph → one of a set of alternatives
4 Ligature a sequence of glyphs → one glyph
5 Context runs other lookups where an input sequence matches
6 Chained context the same, also checking the glyphs before and after
7 Extension a wrapper allowing 32-bit offsets to a subtable of another type
8 Reverse chained single single substitution in context, run from the end of the text

Here is the first lookup in the test font, the liga lookup. It is the subject of most of this chapter, so it’s worth getting to know in each of its views, which its tabs offer:

Lookup 0: Ligature substitution (type 4)
  lookupFlag: IgnoreMarks
  subtable 0: LigatureSubstFormat1
    coverage: [f]
    ligatureSet:
      (f)
        1. [f i] → f_f_i
        2. [i] → f_i
        3. [l] → f_l
        4. [f] → f_f
feature liga {
  lookupflag IgnoreMarks;
  sub f i by f_i;
  sub f f i by f_f_i;
  sub f l by f_l;
  sub f f by f_f;
} liga;
<Lookup index="0">
  <LookupType value="4"/>
  <LookupFlag value="8"/><!-- ignoreMarks -->
  <!-- SubTableCount=1 -->
  <LigatureSubst index="0">
    <LigatureSet glyph="f">
      <Ligature components="f,i" glyph="f_f_i"/>
      <Ligature components="i" glyph="f_i"/>
      <Ligature components="l" glyph="f_l"/>
      <Ligature components="f" glyph="f_f"/>
    </LigatureSet>
  </LigatureSubst>
</Lookup>
── Lookup 0 (at 0x0042)
0042  00 04   lookupType
0044  00 08   lookupFlag (IgnoreMarks)
0046  00 01   subTableCount
0048  00 46   subtableOffsets[0] → subtable 0
── subtable 0: LigatureSubstFormat1 (at 0x0088)
0088  00 01   substFormat
008a  00 3e   coverageOffset → Coverage
008c  00 01   ligatureSetCount
008e  00 5e   ligatureSetOffsets[0] → LigatureSet 0
── Coverage (at 0x00c6)
00c6  00 01   format
00c8  00 01   glyphCount
00ca  00 04   glyphArray[0] = f
── LigatureSet 0 (at 0x00e6)
00e6  00 04   ligatureCount
00e8  00 24   ligatureOffsets[0] → Ligature 0.0
00ea  00 12   ligatureOffsets[1] → Ligature 0.1
00ec  00 18   ligatureOffsets[2] → Ligature 0.2
00ee  00 1e   ligatureOffsets[3] → Ligature 0.3
── Ligature 0.1 (at 0x00f8)
00f8  00 09   ligatureGlyph = f_i
00fa  00 02   componentCount (including the first)
00fc  00 05   componentGlyphIDs[0] = i
── Ligature 0.2 (at 0x00fe)
00fe  00 0a   ligatureGlyph = f_l
0100  00 02   componentCount (including the first)
0102  00 06   componentGlyphIDs[0] = l
── Ligature 0.3 (at 0x0104)
0104  00 0b   ligatureGlyph = f_f
0106  00 02   componentCount (including the first)
0108  00 04   componentGlyphIDs[0] = f
── Ligature 0.0 (at 0x010a)
010a  00 0c   ligatureGlyph = f_f_i
010c  00 03   componentCount (including the first)
010e  00 04   componentGlyphIDs[0] = f
0110  00 05   componentGlyphIDs[1] = i
template <typename Types>
struct LigatureSubstFormat1_2
{
  protected:
  HBUINT16      format;                 /* Format identifier--format = 1 */
  typename Types::template LOffsetTo<Coverage>
                coverage;               /* Offset to Coverage table--from
                                         * beginning of Substitution table */
  typename Types::template ArrayOf<typename Types::template OffsetTo<LigatureSet<Types>>>
                ligatureSet;            /* Array LigatureSet tables
                                         * ordered by Coverage Index */
  public:
  DEFINE_SIZE_ARRAY (2 + Types::LOffset::static_size + Types::HBUINT::static_size, ligatureSet);
  /* … */
};
src/OT/Layout/GSUB/LigatureSubstFormat1.hh:11
ligatures.ttf, lookup 0

The spec-like view is the book’s default. It uses the field names of HarfBuzz’s own structs, which follow the OpenType specification nearly field for field. It leaves out what the bytes need but a reader doesn’t: counts, which are implied by the lists, and offsets, which are shown by nesting. A ligature set is listed under its first glyph, in brackets, because that glyph is never stored in the ligature itself: it is found through the subtable’s coverage table. So (f) followed by [f i] → f_f_i means “f, then f, then i, becomes f_f_i”.

The feature file view is the source the font was compiled from. It’s how most designers write GSUB, but as we’ll see, it hides some things that HarfBuzz depends on. TTX is fontTools’ XML dump. Bytes shows the lookup as it is stored in the font, offset by offset. HarfBuzz C++ shows the struct that HarfBuzz lays over those bytes, with a link to it in the source.

Structs over bytes

The C++ view is worth a closer look, because it’s how all of HarfBuzz’s table code works. LigatureSubstFormat1_2 is not a class that HarfBuzz fills in by parsing the font. It is a description of the bytes: HarfBuzz points it at the right place in the font’s data and reads the fields where they are. HBUINT16 is a big-endian 16-bit integer that converts itself to a native one when read. An OffsetTo<Coverage> is a 16-bit number that knows what it points to, so this+coverage gives the Coverage table. There is no copying and no allocation. Fonts as bytes explains these types, and the sanitizer, which checks every offset and array once, when the table is loaded, is what makes it safe to trust them.

The struct is a template on Types, SmallTypes, or MediumTypes, so that one piece of code reads both the ordinary 16-bit formats and the wider ones of the “beyond 64k” extension (SingleSubst formats 3 and 4, ChainContext formats 4 and 5, and so on), as Fonts as bytes explains.

The names differ a little between the views. For this lookup:

OpenType spec HarfBuzz struct TTX
coverageOffset coverage (merged into LigatureSet glyph=)
ligatureSetOffsets[] ligatureSet <LigatureSet>
ligatureGlyph ligGlyph glyph=
componentCount, componentGlyphIDs[] component (a HeadlessArray16Of) components=

HeadlessArray16Of is HarfBuzz’s name for an array whose count includes a first element that isn’t stored. That’s exactly what a ligature’s component list is: componentCount counts the first glyph too, but only the second and later glyphs follow it. In the code it’s read as component.lenP1, “length plus one”.

The order lookups run in

Planning hands over the lookups in stages, separated by pauses where a script shaper can step in (an Indic shaper, for example, reorders glyphs between stages). Within a stage, HarfBuzz sorts the lookups by their index in the LookupList (hb-ot-map.cc:382; From features to lookups covers stages and pauses). It doesn’t sort them by feature, and it doesn’t keep the order in which the features were asked for. Feature order gives the stages for every shaper.

For the default shaper, which handles Latin, nearly every feature is in the same stage. So ccmp, liga, calt, and the rest are not applied one feature after another: their lookups are merged into one list and run in lookup order. The test font’s ccmp turns x into f i. It comes after liga in the feature file, so its lookup is lookup 4, after liga’s lookup 0. The font ccmp-first.ttf is the same except that the ccmp block comes first in its feature file:

FontTextOptionshb-shape output
ligatures.ttfxU+0078[f=0+500|i=0+500]
ccmp-first.ttfxU+0078[f_i=0+900]

In ligatures.ttf, liga has already run by the time ccmp makes the f and the i, so they stay apart. Moving the feature block is enough to change the result, because feaLib numbers lookups in the order it meets them. The trace for ligatures.ttf shows the order:

hb-shape ligatures.ttf "x" --trace
start table GSUB script tag 'latn'[x=0]
start lookup 0 feature 'liga'[x=0]
skipped lookup 0 feature 'liga' because no glyph matches[x=0]
end lookup 0 feature 'liga'[x=0]
start lookup 3 feature 'calt'[x=0]
skipped lookup 3 feature 'calt' because no glyph matches[x=0]
end lookup 3 feature 'calt'[x=0]
start lookup 4 feature 'ccmp'[x=0]
multiplying glyph at 0[x=0]
multiplied glyphs at 0,1[f=0|i=0]
end lookup 4 feature 'ccmp'[f=0|i=0]
end table GSUB script tag 'latn'[f=0|i=0]

For type designers: in one stage, the order of lookups in the LookupList is what counts. A lookup whose output another lookup should see must come first in the list, whatever features they belong to. In a feature file, that means writing it first.

The trace also shows HarfBuzz skipping lookups “because no glyph matches”. That’s the first of several shortcuts, which come next.

Running one lookup

The loop that runs a lookup over the buffer is short enough to read in full. It’s apply_forward in hb-ot-layout.cc, and here it is with the caching details left out:

while (buffer->successful)
{
  /* Skip ahead over glyphs this lookup can't touch. */
  unsigned j = buffer->idx;
  while (j < buffer->len &&
         !(accel.digest.may_have (info[j].codepoint) &&
           (info[j].mask & c->lookup_mask) &&
           c->check_glyph_property (&info[j], c->lookup_props)))
    j++;
  buffer->next_glyphs (j - buffer->idx);   /* copy them to the output */
  if (buffer->idx >= buffer->len)
    break;

  /* Try each subtable here; if none applies, move on one glyph. */
  if (!accel.apply (c))
    buffer->next_glyph ();
}

Four ideas are packed into it.

An input buffer and an output buffer

A forward GSUB lookup reads from the buffer’s glyphs and writes to a second, output array as it goes. buffer->idx is the position in the input. next_glyph() copies the current glyph to the output unchanged and moves on. A substitution writes its result to the output instead, and moves idx past whatever it consumed. At the end, buffer->sync() makes the output the new contents of the buffer. This is why a multiple substitution can grow the text without shuffling the whole array along.

It also decides what a lookup can see. Glyphs before idx are in the output, so they are already processed by this lookup. Glyphs from idx on are not processed yet. So a lookup never revisits its own output at the same position, but its output does affect matches further on, when a later rule looks backwards. The test font’s calt lookup changes e to e.init after a space or after an e.init:

Lookup 3: Chained context substitution (type 6)
  lookupFlag: none
  subtable 0: ChainContextSubstFormat3
    backtrack: [space e.init]
    input:     [e]
    lookahead: (none)
    lookupRecord: input[0] → lookup 2
feature calt {
  sub [space e.init] e' lookup INIT;
} calt;
<Lookup index="3">
  <LookupType value="6"/>
  <LookupFlag value="0"/>
  <!-- SubTableCount=1 -->
  <ChainContextSubst index="0" Format="3">
    <!-- BacktrackGlyphCount=1 -->
    <BacktrackCoverage index="0">
      <Glyph value="space"/>
      <Glyph value="e.init"/>
    </BacktrackCoverage>
    <!-- InputGlyphCount=1 -->
    <InputCoverage index="0">
      <Glyph value="e"/>
    </InputCoverage>
    <!-- LookAheadGlyphCount=0 -->
    <!-- SubstCount=1 -->
    <SubstLookupRecord index="0">
      <SequenceIndex value="0"/>
      <LookupListIndex value="2"/>
    </SubstLookupRecord>
  </ChainContextSubst>
</Lookup>
ligatures.ttf, lookup 3
FontTextOptionshb-shape output
ligatures.ttfeeeU+0065 U+0065 U+0065[e=0+500|e=1+500|e=2+500]

Each e sees the e.init that the same lookup has just written before it, so the change runs all the way along. If the rule had looked ahead instead, at glyphs not yet processed, it would only ever see the original es.

Reverse chained single substitutions (type 8) work the other way. They run from the end of the buffer to the start, in place, with no output buffer, and their rules look ahead at glyphs they have already changed. That direction is the whole reason the type exists: it lets a choice made at the end of a word, as in Nastaliq, depend on the glyphs after it.

Three filters before any real work

Most lookups don’t apply to most glyphs, so HarfBuzz tries hard not to do the work of finding that out. Each lookup has a set digest: a tiny summary, three 64-bit masks, of every glyph its subtables’ coverage tables contain. It’s like a Bloom filter. Asking whether it may contain a glyph costs a few bit operations. “No” is always right, while “maybe” can be wrong, which costs only time. The buffer keeps a digest of its glyphs too, updated as substitutions happen. The digests are checked at three levels:

  1. Before a lookup runs at all, its digest is compared with the buffer’s. If they have nothing in common, the lookup is skipped: “skipped lookup … because no glyph matches”.
  2. In the loop above, each glyph is checked against the lookup’s digest, so glyphs it can’t touch are skipped in a tight loop.
  3. Each subtable has its own digest, checked before its coverage table is searched.

The digest is fast when a lookup covers a compact range of glyph IDs and useless when it covers glyphs from all over the font, as hb-set-digest.hh says. Ligature lookups also get a digest of the glyphs that can come second in a ligature, so HarfBuzz can see that an f followed by an o can’t start any ligature without looking at any.

The mask

Every glyph in the buffer carries a 32-bit mask. During planning, each feature gets one or more bits of it, and each glyph’s mask has those bits set where the feature is on. A feature applied to the whole text, like liga, sets them on every glyph. A feature applied to a range sets them only on the glyphs from those clusters. A lookup runs with its feature’s bits as lookup_mask, and a glyph whose mask doesn’t share any of them isn’t touched. How features get their bits, and what happens when the bits run out, is in From features to lookups.

A feature with a value other than 1 gets more than one bit, and the value is stored in them. Alternate substitution reads it to choose which alternative to use: salt=2 means the second alternate. A value larger than the number of alternates means no substitution at all.

Lookup 1: Alternate substitution (type 3)
  lookupFlag: none
  subtable 0: AlternateSubstFormat1
    coverage: [a]
    alternateSet:
      a → [a.alt1 a.alt2 a.alt3]
feature salt {
  sub a from [a.alt1 a.alt2 a.alt3];
} salt;
<Lookup index="1">
  <LookupType value="3"/>
  <LookupFlag value="0"/>
  <!-- SubTableCount=1 -->
  <AlternateSubst index="0">
    <AlternateSet glyph="a">
      <Alternate glyph="a.alt1"/>
      <Alternate glyph="a.alt2"/>
      <Alternate glyph="a.alt3"/>
    </AlternateSet>
  </AlternateSubst>
</Lookup>
ligatures.ttf, lookup 1
FontTextOptionshb-shape output
ligatures.ttfaU+0061[a=0+500]
ligatures.ttfaU+0061--features=salt[a.alt1=0+500]
ligatures.ttfaU+0061--features=salt=3[a.alt3=0+500]
ligatures.ttfaU+0061--features=salt=4[a=0+500]

The rand feature takes this one step further. HarfBuzz turns it on by default, in every application, with the largest possible value, which Alternate substitution reads as “choose at random”. It picks using a simple pseudo-random generator, minstd_rand, whose state is kept in the buffer and starts at 1, so the same text shaped the same way gives the same “random” result. Applications can set the state with hb_buffer_set_random_state. Each choice moves the state on, so a later choice depends on every earlier one, and HarfBuzz marks the whole buffer unsafe to break: no part of it can be reshaped on its own and be sure of coming out the same. (The test font has no rand lookup.)

Glyph properties

Last, check_glyph_property asks whether the glyph is one this lookup ignores. That depends on the lookup’s flags and on the glyph’s class, and it’s the subject of the next section.

Which glyphs a lookup sees

A ligature lookup for f i would be fairly useless if an accent between the two letters stopped it working. OpenType’s answer is the lookup flag. A lookup can ignore base glyphs, ligatures, or marks, ignore all marks except one attachment class, or ignore all marks except those in one mark-filtering set. Whether a glyph is a base, a ligature, or a mark comes from its class in the GDEF table.

HarfBuzz reads those classes into each glyph’s glyph properties (glyph_props) just before GSUB starts, in _hb_ot_layout_set_glyph_props. A font with no GDEF classes gets classes worked out from Unicode instead (hb_synthesize_glyph_classes): non-spacing marks are marks, and everything else is a base, with one exception. Default-ignorable characters, such as the combining grapheme joiner and the Mongolian variation selectors, are never classed as marks, even though they are non-spacing. A comment in the source explains that some Mongolian fonts without GDEF rely on this.

When a substitution puts in a new glyph, HarfBuzz looks up that glyph’s GDEF class, and also records how it got there: the property bits SUBSTITUTED, LIGATED, and MULTIPLIED stay with the glyph from then on. The Indic and USE shapers use them: if a consonant has been ligated, for example, the Indic shaper stops treating it as a plain consonant (hb-ot-shaper-indic.cc:56).

Skipping, step by step

When a lookup matches a sequence, the second glyph onwards is found by a skipping iterator, skipping_iterator_t in hb-ot-layout-gsubgpos.hh. For each glyph it comes to, it asks two questions, and the answers have three values each, not two.

May this glyph be skipped? SKIP_YES if the lookup flags say to ignore it. SKIP_MAYBE if it’s a default-ignorable character that this lookup is allowed to pass over (more on which in a moment). SKIP_NO otherwise.

Does it match? If the rule names glyphs, they’re compared: MATCH_YES or MATCH_NO. If it doesn’t (some fast paths just want “the next glyph”), it’s MATCH_MAYBE. A glyph whose mask lacks the lookup’s bits is always MATCH_NO. A glyph in a different syllable, when the lookup works per syllable, is ruled out before either question is asked: it fails even if the lookup flags would skip it (hb-ot-layout-gsubgpos.hh:1088). Before 15.2.0 it only counted as MATCH_NO, so a glyph of another syllable could still be skipped, and since a syllable’s serial number has only 4 bits and wraps, a rule could match a glyph 16 syllables later (#6672, HarfBuzz issue #6670).

The answers are then combined like this:

skip: YES skip: MAYBE skip: NO
match: YES skip match match
match: MAYBE skip skip match
match: NO skip skip fail

The middle column is the subtle one. A joiner is passed over, unless the rule actually asks for it, in which case it matches. So a font can have a rule that deliberately uses a zero-width joiner, and still have its other rules work across one.

Joiners

Which default ignorables can be passed over depends on whether the glyph is part of the input or of the context, and on two flags a feature can be given during planning, “manual ZWJ” and “manual ZWNJ” (F_MANUAL_ZWJ and F_MANUAL_ZWNJ in hb-ot-map.hh). In GSUB:

ZWJ ZWNJ Hidden: CGJ, Mongolian FVS1–4, TAG characters Other default ignorables
input passed over, unless the feature has manual ZWJ blocks block passed over
context passed over passed over, unless the feature has manual ZWNJ block passed over

So, in the default case, a zero-width joiner doesn’t stop a ligature, and a zero-width non-joiner does. That’s what they are for: ZWNJ asks for no joining. The “hidden” characters are default ignorables too, but they’re there to change the shaping, so GSUB never looks past them. The combining grapheme joiner, for example, is how a writer stops two marks from being reordered or combined (HarfBuzz issue #554). A CGJ that turns out to keep no marks apart loses its hidden bit during normalization, and is then passed over like any other default ignorable, as Unicode inside HarfBuzz shows. GPOS has rules of its own, covered in the next chapter.

FontTextOptionshb-shape output
ligatures.ttffiU+0066 U+0069[f_i=0+900]
ligatures.ttff⟨ZWJ⟩iU+0066 U+200D U+0069[f_i=0+900|space=0+0]
ligatures.ttff⟨ZWNJ⟩iU+0066 U+200C U+0069[f=0+500|space=1+0|i=2+500]

The joiners are still in the output. They have no glyph in this font, and late in shaping HarfBuzz replaces default ignorables with an invisible glyph of zero width, here the space glyph. The ZWJ comes after the ligature, in the same cluster. Where glyphs that a ligature passed over end up is the next subject.

The flags come from planning (From features to lookups). For the default shaper, only mark and mkmk have them. The Arabic shaper gives manual ZWJ to nearly all its features, so that a ZWJ there really does block a ligature, and the Indic shaper gives both flags to its basic forms features (nukt, akhn, rphf, and the rest). One further rule only applies to GSUB: in scripts whose shaper splits text into syllables, a lookup can be limited to one syllable at a time, so that no rule can match across a syllable boundary. The matcher checks for that too.

The lookup types, as HarfBuzz runs them

This section is about how HarfBuzz runs each type. For how to write each one in a feature file, with an example from a real script for most of them, see “Types of Substitution Rule” in Simon Cozens’ Fonts and Layout for Global Scripts.

Single substitution

The simplest type. Format 1 stores a single number, deltaGlyphID, which is added to the glyph ID of every glyph in its coverage. Format 2 stores a list of output glyphs, one for each covered glyph.

Lookup 2: Single substitution (type 1)
  lookupFlag: none
  subtable 0: SingleSubstFormat1
    coverage: [e]
    deltaGlyphID: +13
    (so: e → e.init)
lookup INIT {
  sub e by e.init;
} INIT;
<Lookup index="2">
  <LookupType value="1"/>
  <LookupFlag value="0"/>
  <!-- SubTableCount=1 -->
  <SingleSubst index="0">
    <Substitution in="e" out="e.init"/>
  </SingleSubst>
</Lookup>
template <typename Types>
struct SingleSubstFormat1_3
{
  protected:
  HBUINT16      format;                 /* Format identifier--format = 1 */
  typename Types::template LOffsetTo<Coverage>
                coverage;               /* Offset to Coverage table--from
                                         * beginning of Substitution table */
  typename Types::HBUINT
                deltaGlyphID;           /* Add to original GlyphID to get
                                         * substitute GlyphID, modulo 0x10000 */

  public:
  DEFINE_SIZE_STATIC (2 + Types::LOffset::static_size + Types::HBUINT::static_size);
  /* … */
};
src/OT/Layout/GSUB/SingleSubstFormat1.hh:101
ligatures.ttf, lookup 2

feaLib chose format 1 here because e.init happens to be 13 glyphs after e. HarfBuzz does the addition modulo 216 (224 for the wide format 3), with (glyph_id + delta) & mask. So a negative delta, or a sum past the end of the range, wraps around. That’s what the specification says, but not every implementation has always done it.

Multiple substitution

One glyph becomes several. In the test font, ccmp turns x into f i:

Lookup 4: Multiple substitution (type 2)
  lookupFlag: none
  subtable 0: MultipleSubstFormat1
    coverage: [x]
    sequence:
      x → f i
feature ccmp {
  sub x by f i;
} ccmp;
<Lookup index="4">
  <LookupType value="2"/>
  <LookupFlag value="0"/>
  <!-- SubTableCount=1 -->
  <MultipleSubst index="0">
    <Substitution in="x" out="f,i"/>
  </MultipleSubst>
</Lookup>
ligatures.ttf, lookup 4
FontTextOptionshb-shape output
ligatures.ttfxU+0078[f=0+500|i=0+500]

Both output glyphs keep the cluster of the input, 0. Each is marked as a component of the original, with MULTIPLIED and a component number, so that later lookups and GPOS can tell they came from one character. If the font has no GDEF classes to say what the new glyphs are, and the input glyph was a ligature, they are classed as bases.

Two cases are special. A sequence of just one glyph is treated as a single substitution, in place, and the glyph isn’t marked as multiplied. A sequence of no glyphs deletes the glyph. The specification doesn’t allow that, but Uniscribe does, and fonts rely on it, so HarfBuzz follows Uniscribe (HarfBuzz issue #253).

Alternate substitution

Covered above, under “The mask”: the feature’s value picks the alternate, and rand picks one at random.

Ligature substitution

Ligatures are where most of GSUB’s subtleties are, and HarfBuzz’s source says so: the comment above match_input calls ligating “perhaps the trickiest part of OpenType”.

First match wins

A subtable’s coverage finds the ligature set for the first glyph. HarfBuzz then tries the ligatures in the set in the order they are stored, and the first that matches is used. The specification says ligatures should be ordered by preference, but HarfBuzz does no sorting of its own. If f f came before f f i, then ffi would become f_f followed by i.

You can’t write that font in a feature file. feaLib sorts each set longest first whatever order the rules are written in: the feature file above has sub f i first and sub f f i second, and the compiled lookup has them the other way round. The font shortest-first.ttf was built by reversing the sets after compiling:

Lookup 0: Ligature substitution (type 4)
  lookupFlag: IgnoreMarks
  subtable 0: LigatureSubstFormat1
    coverage: [f]
    ligatureSet:
      (f)
        1. [i] → f_i
        2. [l] → f_l
        3. [f] → f_f
        4. [f i] → f_f_i
<Lookup index="0">
  <LookupType value="4"/>
  <LookupFlag value="8"/><!-- ignoreMarks -->
  <!-- SubTableCount=1 -->
  <LigatureSubst index="0">
    <LigatureSet glyph="f">
      <Ligature components="i" glyph="f_i"/>
      <Ligature components="l" glyph="f_l"/>
      <Ligature components="f" glyph="f_f"/>
      <Ligature components="f,i" glyph="f_f_i"/>
    </LigatureSet>
  </LigatureSubst>
</Lookup>
── Lookup 0 (at 0x0042)
0042  00 04   lookupType
0044  00 08   lookupFlag (IgnoreMarks)
0046  00 01   subTableCount
0048  00 46   subtableOffsets[0] → subtable 0
── subtable 0: LigatureSubstFormat1 (at 0x0088)
0088  00 01   substFormat
008a  00 3e   coverageOffset → Coverage
008c  00 01   ligatureSetCount
008e  00 5e   ligatureSetOffsets[0] → LigatureSet 0
── Coverage (at 0x00c6)
00c6  00 01   format
00c8  00 01   glyphCount
00ca  00 04   glyphArray[0] = f
── LigatureSet 0 (at 0x00e6)
00e6  00 04   ligatureCount
00e8  00 12   ligatureOffsets[0] → Ligature 0.0
00ea  00 18   ligatureOffsets[1] → Ligature 0.1
00ec  00 1e   ligatureOffsets[2] → Ligature 0.2
00ee  00 24   ligatureOffsets[3] → Ligature 0.3
── Ligature 0.0 (at 0x00f8)
00f8  00 09   ligatureGlyph = f_i
00fa  00 02   componentCount (including the first)
00fc  00 05   componentGlyphIDs[0] = i
── Ligature 0.1 (at 0x00fe)
00fe  00 0a   ligatureGlyph = f_l
0100  00 02   componentCount (including the first)
0102  00 06   componentGlyphIDs[0] = l
── Ligature 0.2 (at 0x0104)
0104  00 0b   ligatureGlyph = f_f
0106  00 02   componentCount (including the first)
0108  00 04   componentGlyphIDs[0] = f
── Ligature 0.3 (at 0x010a)
010a  00 0c   ligatureGlyph = f_f_i
010c  00 03   componentCount (including the first)
010e  00 04   componentGlyphIDs[0] = f
0110  00 05   componentGlyphIDs[1] = i
shortest-first.ttf, lookup 0
FontTextOptionshb-shape output
ligatures.ttfffiU+0066 U+0066 U+0069[f_f_i=0+900]
shortest-first.ttfffiU+0066 U+0066 U+0069[f_f=0+900|i=2+500]

For type designers: the feature file view hides the order HarfBuzz uses. If a font’s ligatures come from somewhere other than feaLib or a tool like it, look at the order in TTX or in the spec-like view.

The second-glyph fast path

Since most ligature sets hold several ligatures and most attempts fail, LigatureSet::apply first finds the glyph after the current one, with skipping, and only tries ligatures whose second component is that glyph. If the next glyph might be skipped (a joiner, say), it falls back to trying every ligature in order. This is purely a speed-up, but it’s why the code has two loops where one would seem enough.

Matching, and the marks in between

Ligature::apply calls match_input, which finds each remaining component with the skipping iterator and records where it found it. In the test font, the liga lookup has the flag IgnoreMarks, so a combining acute between f and i is passed over:

hb-shape ligatures.ttf "f◌́i" --trace
start lookup 0 feature 'liga'[f=0|acutecomb=0|i=2]
ligating glyphs at 0,2[f=0|acutecomb=0|i=2]
ligated glyph at 0[f_i=0|acutecomb=0]
end lookup 0 feature 'liga'[f_i=0|acutecomb=0]

The trace says “ligating glyphs at 0,2”: the components were found at positions 0 and 2, with the mark at 1 passed over. ligate_input then does three things:

  1. It merges the clusters of everything from the first component to the last, so the ligature, and the mark inside it, all belong to cluster 0.
  2. It writes the ligature glyph in place of the first component and removes the others. The glyphs that were passed over aren’t removed: they are kept, in order, after the ligature.
  3. It gives the ligature a new ligature ID, and records its number of components. Each passed-over mark gets the same ligature ID and the number of the component it followed.

That last step is what lets a mark find the right part of a ligature later on. GPOS’s mark-to-ligature attachment uses it to choose which of the ligature’s anchors to use. This font has no GPOS, so HarfBuzz positions the mark with its fallback positioner (Fallbacks), which divides the ligature into equal parts, one for each component. Even so, the effect shows (and GPOS: positioning glyphs shows it with real anchors):

FontTextOptionshb-shape output
ligatures.ttff◌́iU+0066 U+0301 U+0069[f_i=0+900|acutecomb=0@-675,562+0]
ligatures.ttffi◌́U+0066 U+0069 U+0301[f_i=0+900|acutecomb=0@-225,562+0]
ligatures.ttff◌́f◌́iU+0066 U+0301 U+0066 U+0301 U+0069[f_f_i=0+900|acutecomb=0@-750,562+0|acutecomb=0@-450,562+0]

The same ligature glyph, with the same accent, but the accent sits over a different part of it: −675 when it was typed after the f, −225 when it was typed after the i. That’s half the ligature’s 900-unit width apart. With three components, the two accents are a third of the width apart.

Edge cases the code handles

The comments in match_input and ligate_input are a list of real fonts that broke, and each is a rule HarfBuzz now follows:

A ligature formed from a non-spacing mark has its Unicode general category changed to “other letter”, so that later steps stop treating it as a mark.

Context and chained context

Types 5 and 6 don’t substitute anything themselves. They match a pattern and then run other lookups, the nested lookups, at chosen positions inside it. Type 5 matches only an input sequence. Type 6 matches a backtrack sequence before it and a lookahead sequence after it as well. Each has three formats: rules listed per first glyph (format 1), rules written in glyph classes (format 2), and one rule written as a coverage table per position (format 3). The beyond-64k formats 4 and 5 are 1 and 2 with wider glyph IDs. feaLib wrote the test font’s calt rule as a format 3 subtable:

Lookup 3: Chained context substitution (type 6)
  lookupFlag: none
  subtable 0: ChainContextSubstFormat3
    backtrack: [space e.init]
    input:     [e]
    lookahead: (none)
    lookupRecord: input[0] → lookup 2
feature calt {
  sub [space e.init] e' lookup INIT;
} calt;
<Lookup index="3">
  <LookupType value="6"/>
  <LookupFlag value="0"/>
  <!-- SubTableCount=1 -->
  <ChainContextSubst index="0" Format="3">
    <!-- BacktrackGlyphCount=1 -->
    <BacktrackCoverage index="0">
      <Glyph value="space"/>
      <Glyph value="e.init"/>
    </BacktrackCoverage>
    <!-- InputGlyphCount=1 -->
    <InputCoverage index="0">
      <Glyph value="e"/>
    </InputCoverage>
    <!-- LookAheadGlyphCount=0 -->
    <!-- SubstCount=1 -->
    <SubstLookupRecord index="0">
      <SequenceIndex value="0"/>
      <LookupListIndex value="2"/>
    </SubstLookupRecord>
  </ChainContextSubst>
</Lookup>
ligatures.ttf, lookup 3

Matching uses the same skipping iterator as ligatures, with two differences for the backtrack and lookahead. Their glyphs need not have the lookup’s mask bits, because a context isn’t part of what’s being changed. And ZWJ is always passed over there. As shown earlier, the backtrack is matched against the output buffer, so it sees glyphs this lookup has already changed.

Once a rule matches, apply_lookup runs each nested lookup in turn, at the position its lookupRecord gives:

hb-shape ligatures.ttf "e" --trace
start lookup 3 feature 'calt'[e=0]
end lookup 3 feature 'calt'[e=0]

A nested lookup may change the length of the buffer, by ligating or multiplying, and apply_lookup then has to work out where the later positions in the pattern have moved to. It assumes that new glyphs were added straight after the current position, and that removed ones were the positions after it. A long comment in the source explains that this guess is not always right (HarfBuzz discussion #3538) and how it could be improved.

Nesting is limited. A lookup can nest 64 deep at most (HB_MAX_NESTING_LEVEL); one level deeper and the whole buffer is marked unsuccessful (hb-ot-layout-gsubgpos.hh:1237), so a single self-recursive rule stops all further shaping. A pattern can be at most 64 glyphs long (HB_MAX_CONTEXT_LENGTH). And the buffer has a budget of operations (max_ops): 65,536 for each glyph, and at least 524,288 (4,096 and 65,536 before 15.2.0). A font built to make shaping run for ever just stops when it runs out. These limits are in hb-limits.hh, and the Limits appendix lists them all.

Extension

An Extension subtable holds a 32-bit offset to a subtable of another type. It exists only because 16-bit offsets can’t reach far enough in large fonts. HarfBuzz follows the offset and carries on as if the subtable were there. Nothing else in this chapter changes for extension lookups.

What a feature file compiles to

HarfBuzz never sees a feature file, only the lookups it was compiled to. Most rules compile to what they look like, but a few don’t, and HarfBuzz follows the lookups. This section shows four, compiled by feaLib from constructs.fea. Adobe’s OpenType Feature File Specification is the reference for the language itself.

An ignore rule

The test font’s calt turns f into a.alt1, except before i:

feature calt {
  ignore sub f' i;
  sub f' by a.alt1;
} calt;

That isn’t a rule and an exception to it. It is one chained-context lookup with two rules for f. The first has i as its lookahead and no nested lookups. The second calls a lookup that makes the substitution:

feature calt {
  ignore sub f' i;
  sub f' by a.alt1;
} calt;
<Lookup index="0">
  <LookupType value="6"/>
  <LookupFlag value="0"/>
  <!-- SubTableCount=1 -->
  <ChainContextSubst index="0" Format="1">
    <Coverage>
      <Glyph value="f"/>
    </Coverage>
    <!-- ChainSubRuleSetCount=1 -->
    <ChainSubRuleSet index="0">
      <!-- ChainSubRuleCount=2 -->
      <ChainSubRule index="0">
        <!-- BacktrackGlyphCount=0 -->
        <!-- InputGlyphCount=1 -->
        <!-- LookAheadGlyphCount=1 -->
        <LookAhead index="0" value="i"/>
        <!-- SubstCount=0 -->
      </ChainSubRule>
      <ChainSubRule index="1">
        <!-- BacktrackGlyphCount=0 -->
        <!-- InputGlyphCount=1 -->
        <!-- LookAheadGlyphCount=0 -->
        <!-- SubstCount=1 -->
        <SubstLookupRecord index="0">
          <SequenceIndex value="0"/>
          <LookupListIndex value="1"/>
        </SubstLookupRecord>
      </ChainSubRule>
    </ChainSubRuleSet>
  </ChainContextSubst>
</Lookup>
constructs.ttf, lookup 0

The rules for a glyph are tried in order, and the first that matches wins (hb-ot-layout-gsubgpos.hh:5152). Before i, the first rule matches, applies nothing, and so stops the second being tried. The lookup itself carries on at the next glyph: an ignore rule passes over one position, and doesn’t end the lookup.

FontTextOptionshb-shape output
constructs.ttffiU+0066 U+0069--language=en[f_i=0+900]
constructs.ttffeU+0066 U+0065--language=en[a.alt1=0+500|e=1+500]

The f before i is left for liga, which makes f_i.

An inline contextual rule

sub f' by a.alt1 says what to substitute inside the rule. A chained-context rule can’t do that: it can only call another lookup. So feaLib made one, lookup 1, a single substitution that belongs to no feature:

Lookup 1: Single substitution (type 1)
  lookupFlag: none
  subtable 0: SingleSubstFormat1
    coverage: [f]
    deltaGlyphID: +9
    (so: f → a.alt1)
feature calt {
  ignore sub f' i;
  sub f' by a.alt1;
} calt;
<Lookup index="1">
  <LookupType value="1"/>
  <LookupFlag value="0"/>
  <!-- SubTableCount=1 -->
  <SingleSubst index="0">
    <Substitution in="f" out="a.alt1"/>
  </SingleSubst>
</Lookup>
constructs.ttf, lookup 1

It runs only when the chained rule calls it:

hb-shape constructs.ttf "fe" --language=en --trace
start lookup 0 feature 'calt'[f=0|e=1]
start recursing to lookup 1 at 0[f=0|e=1]
replacing glyph at 0 (single substitution)[f=0|e=1]
replaced glyph at 0 (single substitution)[a.alt1=0|e=1]
end recursing to lookup 1[a.alt1=0|e=1]
end lookup 0 feature 'calt'[a.alt1=0|e=1]

feaLib numbered it straight after the chained lookup, so it takes a number among the font’s own: the liga lookups written next are 2 and 3, not 1 and 2.

Rules of different types in one block

A feature block can mix kinds of rule, but a lookup has only one type. feaLib starts a new lookup each time the type changes, and puts runs of rules of one type in the same lookup. The test font’s liga block has a ligature rule and then a single substitution:

feature liga {
  sub f i by f_i;
  sub x by e.init;
} liga;

So liga has two lookups, 2 and 3. Each runs over the whole buffer before the next starts, so a later rule in the block sees what the earlier ones made, as if they had been written as separate lookups:

hb-shape constructs.ttf "fx" --language=en --trace
start lookup 2 feature 'liga'[a.alt1=0|x=1]
skipped lookup 2 feature 'liga' because no glyph matches[a.alt1=0|x=1]
end lookup 2 feature 'liga'[a.alt1=0|x=1]
start lookup 3 feature 'liga'[a.alt1=0|x=1]
replacing glyph at 1 (single substitution)[a.alt1=0|x=1]
replaced glyph at 1 (single substitution)[a.alt1=0|e.init=1]
end lookup 3 feature 'liga'[a.alt1=0|e.init=1]

Rules before the first script

A rule before a feature block’s first script or language statement goes to every language system the font declares with languagesystem. A language statement then includes those rules too, unless it says exclude_dflt. The test font’s salt turns a into a.alt2 for everyone, and e into e.init only in Turkish, which excludes the rest:

feature salt {
  sub a by a.alt2;
  script latn;
  language TRK exclude_dflt;
  sub e by e.init;
} salt;

feaLib wrote two salt features into the FeatureList, one for the default language systems with lookup 4, and one for TRK with lookup 5 alone. HarfBuzz uses whichever the chosen language system lists (Choosing a script shaper):

FontTextOptionshb-shape output
constructs.ttfaeU+0061 U+0065--features=salt --language=en[a.alt2=0+500|e=1+500]
constructs.ttfaeU+0061 U+0065--features=salt --language=tr[a=0+500|e.init=1+500]

For type designers: when a feature file seems to do something it doesn’t say, look at what it compiled to. ttx -t GSUB shows the lookups and which features use them, and a trace (hb-shape --trace, from a build with detailed traces) shows the order HarfBuzz runs them in.

Clusters and safe breaking

Each substitution that combines glyphs merges their clusters, so every glyph keeps pointing at the text it came from. HarfBuzz also flags places where the text can’t be cut and reshaped in parts without changing the result: unsafe_to_break, and the weaker unsafe_to_concat. Matching records these even when it fails. A ligature that almost matched marks its glyphs as unsafe to join, because different text there might complete it. That’s why the second-glyph fast path takes care to flag what it looked at. Buffers explains how the flags are set and combined, and For applications what applications do with them.

Where to read the source

File What’s there
src/OT/Layout/GSUB/*.hh one file per subtable type and format: the structs, and their apply
src/hb-ot-layout-gsubgpos.hh what GSUB and GPOS share: the apply context, the skipping iterator, match_input, ligate_input, contexts, apply_lookup, the lookup accelerator
src/hb-ot-layout.cc apply_forward, apply_backward, apply_string, and hb_ot_map_t::apply, which runs a plan’s lookups stage by stage
src/hb-ot-map.cc turning features into the sorted, staged list of lookups
src/hb-set-digest.hh the set digests
src/hb-limits.hh nesting, length, and operation limits

To watch any of this happen, run hb-shape with --trace. For the more detailed messages shown on this page (“ligating glyphs at 0,2”), HarfBuzz has to be built with HB_BUFFER_MESSAGE_MORE defined, as this book’s tools/build-harfbuzz.sh does.

In short