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

Appendix A

Feature order

“Does my calt run before my ccmp?” has no single answer. It depends on the script shaper, and on the order of the lookups in the font. This appendix gives the order for each of HarfBuzz’s shapers, as HarfBuzz 15.2.0 runs it. Each table is generated automatically by running HarfBuzz when the book is built, so it can’t drift from the code.

The rule

Planning sorts features into stages (From features to lookups):

So each table below lists stages, not features. Within one stage, the features are listed alphabetically, because their order there is the font’s, not HarfBuzz’s.

Between the stages, the tables show the other steps HarfBuzz’s trace reports, in italics, under the names the trace gives them:

Step What it is
preprocess-text the shaper’s work on the characters before shaping, such as composing Hangul jamo (Hebrew, Thai, Lao, and Hangul)
decompose, reorder, compose normalization’s three rounds (Normalization)
reordering … a shaper moving glyphs between stages: the Indic, Khmer, Myanmar, and USE reorderings
postprocess-glyphs the shaper’s work after GPOS, such as stretching for Arabic stch (Joining scripts)
fallback mark, fallback kern HarfBuzz positioning marks or kerning itself, for a font without the GPOS features (Fallbacks)

Only steps that the shaped text needs appear. A step that leaves no message in the trace isn’t shown either. Finding syllables, for example, leaves no message, but it ends a stage.

How the tables are made

fonts/feature-order/build-fonts.py builds a pair of fonts for each script. Each font has one lookup for every feature tag that any of HarfBuzz’s shapers asks for, 58 in all, plus smcp to stand for a feature an application asks for. Each lookup acts on a glyph that is never in the text, so it changes nothing. It is there to be seen: HarfBuzz’s trace reports every lookup in the plan, in the order it runs, whether or not it matches.

The two fonts differ only in how their lookups are numbered: alphabetically by feature tag in one, and in reverse in the other. Within a stage, LookupList order decides, so the two fonts give opposite orders. Across stages, the stage order decides, so they agree. A stage therefore ends exactly where the lookups run so far are the same set in both fonts. The order block in tools/build-site.mjs shapes both fonts and finds the stages that way.

A feature that is missing from a table wasn’t asked for. Under the Indic shaper, for example, liga is turned off, so it never runs, even though the fonts have it.

The default shaper

The default shaper handles Latin, Greek, Cyrillic, Han, and most other scripts, and Thai and Lao use its features too (From tags to a shaper). It adds no pauses, so after rvrn everything is one stage: the font’s lookup order decides everything else.

hb-shape order-latn-a.ttf "a" --features=smcp --trace, and the same with order-latn-b.ttf
StageFeatures, or another step
decompose
GSUB 1rvrn
GSUB 2BUZZ Buzz HARF Harf calt ccmp clig dnom frac liga locl ltra ltrm numr rand rclt rlig smcp
GPOS 1abvm blwm curs dist kern mark mkmk

In right-to-left text, as with the Hebrew shaper, rtla and rtlm take the place of ltra and ltrm:

hb-shape order-hebr-a.ttf "א" --features=smcp --trace, and the same with order-hebr-b.ttf
StageFeatures, or another step
decompose
GSUB 1rvrn
GSUB 2BUZZ Buzz HARF Harf calt ccmp clig dnom frac liga locl numr rand rclt rlig rtla rtlm smcp
GPOS 1abvm blwm curs dist kern mark mkmk

In vertical text, vert replaces the horizontal features calt, clig, curs, dist, kern, liga, and rclt:

hb-shape order-latn-a.ttf "a" --features=smcp --direction=ttb --trace, and the same with order-latn-b.ttf
StageFeatures, or another step
decompose
GSUB 1rvrn
GSUB 2BUZZ Buzz HARF Harf ccmp dnom frac locl numr rand rlig smcp vert
GPOS 1abvm blwm mark mkmk

Arabic

The Arabic shaper is used for Arabic in horizontal text, and for Syriac in horizontal text if the font has a Syriac script tag. The other joining scripts, such as Mongolian and N’Ko, are shaped by USE (Joining scripts); Choosing a script shaper has the conditions. It puts a pause after each joining feature, and after rlig, so isol, fina, fin2, fin3, medi, med2, init, and rlig each run on their own, in that order (Joining scripts):

hb-shape order-arab-a.ttf "بب" --features=smcp --trace, and the same with order-arab-b.ttf
StageFeatures, or another step
decompose
GSUB 1rvrn
GSUB 2HARF Harf dnom frac numr rand rtla rtlm stch
GSUB 3ccmp locl
GSUB 4isol
GSUB 5fina
GSUB 6fin2
GSUB 7fin3
GSUB 8medi
GSUB 9med2
GSUB 10init
GSUB 11rlig
GSUB 12BUZZ Buzz calt clig liga mset rclt smcp
GPOS 1abvm blwm curs dist kern mark mkmk
postprocess-glyphs

Indic

The nine Indic scripts, under their 2 tags or their old ones (The Indic model). Syllables are found before the shaper’s first feature, and the glyphs are reordered twice: after locl and ccmp, and after the basic features. Each basic feature has a stage of its own, so each sees what the one before made. The presentation features then share the last stage with the common ones and the application’s.

hb-shape order-dev2-a.ttf "कि" --features=smcp --trace, and the same with order-dev2-b.ttf
StageFeatures, or another step
preprocess-text
decompose
reorder
compose
GSUB 1rvrn
GSUB 2HARF Harf dnom frac ltra ltrm numr rand
GSUB 3ccmp locl
reordering indic initial
GSUB 4nukt
GSUB 5akhn
GSUB 6rphf
GSUB 7rkrf
GSUB 8pref
GSUB 9blwf
GSUB 10abvf
GSUB 11half
GSUB 12pstf
GSUB 13vatu
GSUB 14cjct
reordering indic final
GSUB 15BUZZ Buzz abvs blws calt clig haln init pres psts rclt rlig smcp
GPOS 1abvm blwm curs dist kern mark mkmk

Khmer

Khmer reorders first, before any of its own features. Then the basic features share one stage (Khmer and Myanmar):

hb-shape order-khmr-a.ttf "ក" --features=smcp --trace, and the same with order-khmr-b.ttf
StageFeatures, or another step
decompose
GSUB 1rvrn
GSUB 2HARF Harf dnom frac ltra ltrm numr rand
reordering khmer
GSUB 3abvf blwf ccmp cfar locl pref pstf
GSUB 4BUZZ Buzz abvs blws calt clig pres psts rclt rlig smcp
GPOS 1abvm blwm curs dist kern mark mkmk

Myanmar

Under mym2, Myanmar reorders after locl and ccmp, and its basic features each have a stage of their own (Khmer and Myanmar):

hb-shape order-mym2-a.ttf "က" --features=smcp --trace, and the same with order-mym2-b.ttf
StageFeatures, or another step
decompose
GSUB 1rvrn
GSUB 2HARF Harf dnom frac ltra ltrm numr rand
GSUB 3ccmp locl
reordering myanmar
GSUB 4rphf
GSUB 5pref
GSUB 6blwf
GSUB 7pstf
GSUB 8BUZZ Buzz abvs blws calt clig liga pres psts rclt rlig smcp
GPOS 1abvm blwm curs dist kern mark mkmk

The Universal Shaping Engine

USE covers the scripts in its list, and the nine Indic scripts under the unregistered 3 tags, such as dev3 (The Universal Shaping Engine). The order is the same for all of them. Balinese is the example here:

hb-shape order-bali-a.ttf "ᬓ" --features=smcp --trace, and the same with order-bali-b.ttf
StageFeatures, or another step
preprocess-text
decompose
GSUB 1rvrn
GSUB 2HARF Harf dnom frac ltra ltrm numr rand
GSUB 3akhn ccmp locl nukt
GSUB 4rphf
GSUB 5pref
GSUB 6abvf blwf cjct half pstf rkrf vatu
reordering USE
GSUB 7fina init isol medi
GSUB 8BUZZ Buzz abvs blws calt clig haln liga pres psts rclt rlig smcp
GPOS 1abvm blwm curs dist kern mark mkmk

Only rphf and pref have stages of their own, because what each one did is recorded before the next. The specification leaves the order within each group to the font.

Hangul

The Hangul shaper composes or decomposes the jamo before shaping, and its ljmo, vjmo, and tjmo share the default shaper’s single stage (Hebrew, Thai, Lao, and Hangul):

hb-shape order-hang-a.ttf "한" --features=smcp --trace, and the same with order-hang-b.ttf
StageFeatures, or another step
preprocess-text
decompose
GSUB 1rvrn
GSUB 2BUZZ Buzz HARF Harf calt ccmp clig dnom frac liga ljmo locl ltra ltrm numr rand rclt rlig smcp tjmo vjmo
GPOS 1abvm blwm curs dist kern mark mkmk

AAT fonts

A font with a morx table that HarfBuzz uses gets none of the GSUB stages: morx runs in GSUB’s place (When morx is used). Its order is the font’s own: chains run in the order they are stored, and the subtables in each chain likewise. Feature tags only turn subtables on and off (From feature tags to feature types).

What it means for a font

Where to read the source

What Where
The features every shaper asks for, and in what order hb_ot_shape_collect_features, src/hb-ot-shape.cc:317
Running the stages, and the trace’s lookup messages hb_ot_map_t::apply, src/hb-ot-layout.cc:2094
Each shaper’s features and pauses collect_features_* in src/hb-ot-shaper-arabic.cc, -indic.cc, -khmer.cc, -myanmar.cc, -use.cc, -hangul.cc
The fonts fonts/feature-order/build-fonts.py
The order block blockOrder, tools/build-site.mjs

In short