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

Part VI · Scripts

27. The Universal Shaping Engine

The Universal Shaping Engine (USE) is Microsoft’s answer to a problem of numbers. Unicode encodes dozens of scripts built like Devanagari, a consonant with marks above, below, and around it, and each dedicated shaper like the Indic one in the last chapters took years of script-by-script rules. USE replaces the rules with data: every character gets a class derived from Unicode’s properties, one grammar says which sequences make a cluster, and one short reordering step moves the few things that must move. Microsoft announced it in 2015.

HarfBuzz implemented USE in version 1.0.0, whose NEWS links to Microsoft’s specification and its announcement. HarfBuzz USE follows that specification, but it isn’t the same as Microsoft USE. It has categories and cluster types of its own, it departs from the specification in places, some on purpose, and it takes scripts that Microsoft USE doesn’t. Where the specification is silent, the two engines can differ, and Core Text, Apple’s implementation, sometimes differs from both.

This chapter describes HarfBuzz USE. Where it parts from the specification or from Microsoft’s engine, the chapter says so.

HarfBuzz USE is the Indic model with most of the model taken out. It keeps the four parts: categories from a generated table, syllables from a Ragel machine, GSUB features in stages with pauses between them, and a reordering pass. It drops the base consonant, the positions, the questions put to the font, the per-script table, and the second reordering. What is left is about 500 lines of C++ (hb-ot-shaper-use.cc) and a 300-line grammar, and they serve 91 scripts, from Tibetan to the Unicode 18.0 additions, the same 91 that Microsoft’s specification lists. HarfBuzz also sends it the nine Indic scripts when a font uses their 3 tags (Choosing a script shaper), as Apple’s Core Text does; Microsoft USE doesn’t take them, and on Windows they go to the Indic engine.

The examples use two families of test fonts with plain boxes for glyphs, built by fonts/universal-shaping-engine/build-fonts.py. The Devanagari fonts come in pairs, compiled from one feature file under dev2 (the Indic shaper) or dev3 (HarfBuzz USE, since Microsoft USE doesn’t support it): deva2.ttf and deva3.ttf have lookups for rphf, akhn, blwf, half, init, and pres; the -half-first pair has the same lookups with half’s first in the LookupList; the -no-half pair has no half. balinese.ttf has a few Balinese letters and signs, and a blwf that makes two subjoined consonants; balinese-no-blwf.ttf doesn’t. Every result and trace on this page was produced by running HarfBuzz 15.2.0 on them when the page was built.

What USE keeps, and what it drops

Indic shaper HarfBuzz USE
table category and position, from three Unicode files one category, positions folded in, from nine files
syllables Ragel scanner, six types Ragel scanner, nine types, over a filtered buffer
base consonant searched for, from the font’s answers none
basic features one stage each, with masks set by the reordering all in one stage, on the whole syllable, except rphf and pref
reordering initial (sort by position) and final (reph, pre-base vowels, pref) one pass after the basic features: reph forward, pre-base glyphs back
per-script rules a configuration table and many ifs none
init a pre-base vowel sign at the start of a word a topographical form of a whole cluster
mark widths not zeroed zeroed from GDEF, before GPOS

The rest of this chapter takes those rows in turn.

Categories

setup_masks_use (hb-ot-shaper-use.cc:189) looks each character up with hb_use_get_category and keeps the answer in the scratch byte use_category (Buffers). There is no position byte. Where the Indic table gives a vowel sign a category and a position, USE folds the position into the category: a vowel sign is VPre, VAbv, VBlw, or VPst.

Where they come from

hb-ot-shaper-use-table.hh is generated by gen-use-table.py from nine files (update-unicode-tables.make:37). Seven are Unicode’s: IndicSyllabicCategory.txt, IndicPositionalCategory.txt, ArabicShaping.txt (for joining types), DerivedCoreProperties.txt (for default ignorables), UnicodeData.txt (for general categories), Blocks.txt, and Scripts.txt. Two are Microsoft’s: ms-use/IndicSyllabicCategory-Additional.txt and ms-use/IndicPositionalCategory-Additional.txt, MIT-licensed and maintained by Andrew Glass, whose headers record an update for every Unicode version since 7.0, the last “for Unicode 18.0 … 2026-09-04”. The generated header records version 18.0.0 of every Unicode file but one: DerivedCoreProperties.txt is 17.0.0.

The Microsoft files do two things. They override Unicode’s values where they don’t suit shaping, each with its reason: DOTTED CIRCLE is a Consonant, “Reassigned to allow it to cluster as a generic base”; TIBETAN VOWEL SIGN I is Bottom, “Not really below, but need to override to fit into Universal model”. And they give values to scripts that Unicode’s Indic properties don’t cover at all, such as Mongolian, N’Ko, Adlam, and the Egyptian hieroglyph format controls.

Microsoft publishes the files in its font-tools repository, and that is where a character’s category is questioned and corrected: most problems are found by David Corbett, who develops HarfBuzz USE, and fixed by Glass, who updates the files once for each Unicode version; HarfBuzz’s copies in 15.2.0 are the same as the repository’s. Questions about the specification itself go to Microsoft’s typography-issues tracker, where many have been open since 2019, and HarfBuzz’s own choices are argued in HarfBuzz’s issues. The rest of this chapter cites all three where the specification, Microsoft’s engine, and HarfBuzz part company.

The generator reads the files into one record per code point (:59–:108), and drops five scripts that never reach USE, DISABLED_SCRIPTS (:40): Arabic, Lao, Samaritan, Syriac, and Thai. A character in no file is O. Then, for each character, it asks a list of predicates, is_BASE, is_HALANT, and so on, which follow the derivations in Microsoft’s specification, and asserts that exactly one says yes (map_to_use, :386). If the category has positions, a suffix from the positional category is added (use_positions, :338): Top gives Abv, Bottom or Overstruck Blw, Right Pst, Left Pre. A vowel sign in two or three places takes the first of pre, above, and below that it has a part in: Left_And_Right is VPre, Top_And_Bottom VAbv, Bottom_And_Right VBlw.

HarfBuzz’s own changes

The predicates don’t follow the specification everywhere. The differences, with what the source says of each:

Where What HarfBuzz does
:85 Microsoft’s Consonant_Final_Modifier (only U+1C36 LEPCHA SIGN RAN) is read as Syllable_Modifier: “TODO”, with a link to Microsoft’s typography-issues #336
:88 the positional value NA in the Microsoft file, under “Deliberately suppressed”, is read as Not_Applicable
:393–:402 U+1CE2 to U+1CE8 (Vedic signs) become Cantillation_Mark, and four Tibetan signs (U+0F18, U+0F19, U+0F3E, U+0F3F) Vowel_Dependent, because they “don’t have UISC assigned in Unicode 13.0.0, but have UIPC”; U+1CED becomes Tone_Mark
:412 U+11302, U+11303 (Grantha), and U+114C1 (Tirhuta) are Top, citing HarfBuzz pull request 1037 and issue 1631
:218 a joining letter (types C, D, L, R) is a base, but not if it is a joiner: ZWJ has joining type C (“TODO”, typography-issues #484)
:227 CGJ takes in ZWJ, the variation selectors, and every default-ignorable mark, not only U+034F: “Also includes VARIATION_SELECTOR and ZWJ”. This beats the Microsoft file, which makes VS1 to VS16 isolated bases
:246–:287 three kinds of virama are split off from H, which in the specification covers viramas and invisible stackers alike: HVM for U+0DCA SINHALA SIGN AL-LAKUNA, Sk for U+1A60 TAI THAM SIGN SAKOT, and IS for the other invisible stackers. HVM began with the Brahmi virama, which also marks short vowels (“Should be properly reported to Unicode and USE”, HarfBuzz #1102); Sk came with Tai Tham (#170), whose support the specification still calls “currently limited” (#2061, open)
:236 Consonant_Initial_Postfixed is a medial consonant: “new in Unicode 11; not in the spec” (typography-issues #916, open)
:271 O takes in the specification’s IND (independent bases) and S (symbols): “Also includes BASE_IND and SYM”
:298 WJ takes in other default ignorables and every unassigned code point, the specification’s Rsv
:291 symbol modifiers are a class of their own, SMAbv and SMBlw; the specification counts them as consonant modifiers
:288 a space (general category Zs) that isn’t a generic base is a class of its own, SP, which the grammar has no rule for; the generator also makes a record for every such character, though it is in none of Unicode’s Indic files (:105)
:344–:352 positions the specification’s subclasses don’t list: a medial consonant Bottom_And_Left or Bottom_And_Right is MBlw, and Top_And_Bottom_And_Left MPre; a consonant modifier Overstruck is CMBlw (typography-issues #928, #1078, both open)

With --rust the generator writes HarfRust’s copy, and with HB_OPTIMIZE_SIZE the table is packed harder (:497).

The categories

The machine’s categories (hb-ot-shaper-use-machine.rl:65) are numbered, and the numbers are not contiguous. They are the ones the shaper and grammar use:

Category What it is Examples (Balinese, Devanagari, Tibetan)
B a base: consonant, independent vowel, digit, joining letter KA U+1B13, A U+1B05; KA U+0915, RA U+0930, digit zero U+0966; DOTTED CIRCLE U+25CC
GB a generic base NO-BREAK SPACE, U+00D7 MULTIPLICATION SIGN, U+2010 HYPHEN
R an encoded repha MALAYALAM LETTER DOT REPH U+0D4E
CS a consonant with stacker
H, IS, HVM, Sk viramas: visible, invisible, Sinhala’s, Tai Tham’s adeg-adeg U+1B44; virama U+094D
SUB a subjoined consonant TIBETAN SUBJOINED LETTER KA U+0F90
CMAbv, CMBlw consonant modifiers, such as nuktas rerekan U+1B34; TIBETAN VOWEL SIGN AA U+0F71 (a Nukta by Microsoft’s override)
MPre, MAbv, MBlw, MPst medial consonants Javanese cakra U+A9BF, pengkal U+A9BE
VPre, VAbv, VBlw, VPst vowel signs, and pure killers taling U+1B3E, ulu U+1B36, suku U+1B38, tedung U+1B35; vowel sign I U+093F, U U+0941
VMPre, VMAbv, VMBlw, VMPst vowel modifiers: bindus, visargas, tone marks cecek U+1B02, bisah U+1B04; anusvara U+0902
FAbv, FBlw, FPst final consonants surang U+1B03
FMAbv, FMBlw, FMPst final modifiers
SMAbv, SMBlw symbol modifiers Balinese musical symbol marks
N, HN Brahmi joining numbers, and the number joiner
G, J, SB, SE, HR, HM Egyptian hieroglyphs, joiners, segment and mark brackets, mirror, modifiers
RK a reordering killer
ZWNJ, WJ, CGJ non-joiner; word joiner and unassigned; ignorables ZWNJ; U+2060; ZWJ, CGJ, variation selectors
SP a space SPACE U+0020
O everything else danda U+0964, U+2022 BULLET

(The examples were looked up with HarfBuzz’s test-use-table.cc, compiled separately, not when the book is built.)

DOTTED CIRCLE is a B, not a GB. Microsoft’s file made it a Consonant in 2019 so that, in Glass’s words, it can “appear in subjoined forms also” (typography-issues #289), but the specification still calls it a generic base (#482, open). The specification also names U+2015, U+2022, and U+25FB to U+25FE as generic bases, and so does the generator’s is_BASE_OTHER (gen-use-table.py:224), but they come out O: the generator makes a record only for a character that is in one of Unicode’s Indic, joining, or default-ignorable files, or is a space (:101–:108), and these are none of those (HHW-82).

Since 15.2.0: fixed on HarfBuzz main by #6870 (21 November 2026), so main gives these six a record, and they come out GB.

Two things in this table matter later. RA is a plain B: nothing in the category says it can be a reph, which the font must say instead. And ZWJ is CGJ, which the grammar never sees.

Syllables

The first GSUB pause, before any lookup, finds the syllables (setup_syllables_use, hb-ot-shaper-use.cc:315), marks each one unsafe to break, and sets two kinds of mask, for rphf and for the topographical features, both described below. found_syllable writes the type and a serial number into the syllable byte, exactly as the Indic machine does (Finding syllables), and foreach_syllable hands out at most 64 glyphs at a time (A limit on syllable length). That limit is HarfBuzz’s. The specification gives “a practical maximum cluster length” of 31 characters, after which “a cluster break may be forced”; HarfBuzz forces no break, and only processes a syllable longer than 64 glyphs in pieces.

The grammar

The grammar, trimmed of the hieroglyph and number rules (hb-ot-shaper-use-machine.rl:115):

h = H | HVM | IS | Sk;

consonant_modifiers = CMAbv* CMBlw* ((h B | SUB) CMAbv* CMBlw*)*;
medial_consonants = MPre? MAbv? MBlw? MPst?;
dependent_vowels = VPre* VAbv* VBlw* VPst* | H;
vowel_modifiers = HVM? VMPre* VMAbv* VMBlw* VMPst*;
final_consonants = FAbv* FBlw* FPst*;
final_modifiers = FMAbv* FMBlw* | FMPst?;

complex_syllable_start = (R | CS)? (B | GB);
complex_syllable_middle =
	consonant_modifiers
	medial_consonants
	dependent_vowels
	vowel_modifiers
	(Sk B)*
;
complex_syllable_tail =
	complex_syllable_middle
	final_consonants
	final_modifiers
;
symbol_cluster_tail = SMAbv+ SMBlw* | SMBlw+;

virama_terminated_cluster_tail = consonant_modifiers (IS | RK);
virama_terminated_cluster = complex_syllable_start virama_terminated_cluster_tail;
sakot_terminated_cluster_tail = complex_syllable_middle Sk;
sakot_terminated_cluster = complex_syllable_start sakot_terminated_cluster_tail;
standard_cluster = complex_syllable_start complex_syllable_tail;
tail = complex_syllable_tail | sakot_terminated_cluster_tail | symbol_cluster_tail | virama_terminated_cluster_tail;
broken_cluster = R? (tail | number_joiner_terminated_cluster_tail | numeral_cluster_tail);
symbol_cluster = (O | GB | SB) tail?;

(Line breaks are trimmed.) Where the Indic grammar is built around how Indic syllables are spelled, this one is a single template, the specification’s “maximal cluster scheme”: an optional repha, a base, consonants joined to it by viramas or subjoined, then one medial in each place, vowel signs in the order pre, above, below, post, vowel modifiers in the same order, final consonants, and a final modifier. Each group is optional, and their order is fixed. A visible virama counts as a vowel sign, “Treat visible viramas like dependent vowels” (NEWS for 4.1.0), so a syllable can end in one. Every syllable may end in a ZWNJ (:174). Three parts of the template are HarfBuzz’s, not the specification’s: the virama among the vowel signs, HVM before the vowel modifiers, and the sakot groups, (Sk B)* and the sakot-terminated cluster. Under the specification’s own grammar, a Tai Tham syllable with a sakot after its vowel isn’t valid.

The syllable types, and what HarfBuzz does with each:

Type What it is Reordered Joins topographically
standard cluster the template above yes yes
virama-terminated cluster a base and its consonants, ending in an invisible stacker or reordering killer yes yes
sakot-terminated cluster the same, ending in Tai Tham’s sakot yes yes
number-joiner-terminated, numeral Brahmi numbers (U+11052 to U+11065) joined by U+1107F BRAHMI NUMBER JOINER no yes
symbol cluster a symbol, punctuation, or generic base, with any tail yes no
hieroglyph cluster Egyptian hieroglyphs and their format controls no no
broken cluster a tail with no base yes yes
non-cluster anything else, a space among them, and a lone FMPst no no

Six of these are the specification’s seven, not always with the same members. Its seventh, the independent cluster, for spaces, punctuation, and other characters that stand alone, HarfBuzz splits: a space is a non-cluster, and punctuation and the like are symbol clusters (Spaces and symbols). Its virama-terminated cluster can end in a visible virama; HarfBuzz’s can’t, since it counts a visible virama as a vowel sign, so such a syllable is a standard cluster. The sakot-terminated cluster is HarfBuzz’s alone. And the specification has no broken cluster or non-cluster as types: it describes invalid text in words (“Defective clusters”), while HarfBuzz’s machine needs a type for everything it reads.

The order is strict. In Balinese, suku (below) may follow ulu (above), but not the other way round; and a final consonant such as surang comes after the vowel signs:

FontTextOptionshb-shape output
balinese.ttfᬓᬶᬸU+1B13 U+1B36 U+1B38--language=ban[ka=0+600|ulu=0+0|suku=0+0]
balinese.ttfᬓᬸᬶU+1B13 U+1B38 U+1B36--language=ban[ka=0+600|suku=0+0|dottedcircle=0+500|ulu=0+0]
balinese.ttfᬓᬸᬃU+1B13 U+1B38 U+1B03--language=ban[ka=0+600|suku=0+0|surang=0+0]
balinese.ttfᬓᬃᬸU+1B13 U+1B03 U+1B38--language=ban[ka=0+600|surang=0+0|dottedcircle=0+500|suku=0+0]

In the second and fourth lines, the syllable ends where the order breaks, and the mark left over is a broken cluster, given a dotted circle (Dotted circles and broken clusters covers that, and the vowel constraints that USE’s preprocess_text applies). Unicode’s canonical ordering doesn’t help: these signs all have combining class 0, so normalization leaves them as typed. Where marks do have combining classes, Microsoft gives them positions that make the canonical order a valid one (“standard procedure”, Glass says in font-tools #53), since the specification assumes no normalization: CGJ “will need to be updated if USE is modified to support normalization”. HarfBuzz normalizes, and changes three combining classes so that the order comes out right: TAI THAM SIGN SAKOT goes after the tone marks, TIBETAN SYMBOL PADMA GDAN after the vowel signs, and TIBETAN MARK TSA -PHRU before U+0F74 TIBETAN VOWEL SIGN U (hb-unicode.hh:108).

What the machine doesn’t see

The machine doesn’t run over the buffer itself, but over a filtered view of it (find_syllables_use, :259), built from HarfBuzz’s iterators:

auto p =
  + hb_iter (info, buffer->len)
  | hb_enumerate
  | hb_filter ([] (const hb_glyph_info_t &i) { return not_ccs_default_ignorable (i); },
	       hb_second)
  | hb_filter ([&] (const hb_pair_t<unsigned, const hb_glyph_info_t &> p)
	       {
		 if (p.second.use_category() == USE(ZWNJ))
		   for (unsigned i = p.first + 1; i < buffer->len; ++i)
		     if (not_ccs_default_ignorable (info[i]))
		       return !_hb_glyph_info_is_unicode_mark (&info[i]);
		 return true;
	       })
  | hb_enumerate
  | machine_index
  ;

The first filter drops every CGJ, so CGJ, ZWJ, and the variation selectors can stand anywhere: the specification says CGJ “may occur anywhere in a cluster with no effect”. The second drops a ZWNJ that is followed, past any of those, by a mark: such a ZWNJ continues the cluster instead of ending it, as the specification says it should. This is Microsoft’s ruling on a question the specification left open: “CGJ is ignored and the ZWJ/ZWNJ should not see it when determining a cluster break” (typography-issues #216). The syllable types are then written back over the ranges of original indices, so the hidden characters get the syllable of their neighbors. A ZWNJ between KA and suku doesn’t leave the suku without a base:

FontTextOptionshb-shape output
balinese.ttfᬓᬸU+1B13 U+1B38--language=ban[ka=0+600|suku=0+0]
balinese.ttfᬓ⟨ZWNJ⟩ᬸU+1B13 U+200C U+1B38--language=ban[ka=0+600|space=1+0|suku=1+0]

(The ZWNJ is shown as the font’s space glyph with no advance, as HarfBuzz shows every default ignorable it keeps.)

ZWJ is where HarfBuzz and the specification part. The specification says ZWJ “continues a preceding cluster and joins it to a following character”, so two letters with a ZWJ between them would be one cluster. HarfBuzz hides ZWJ from the grammar, and the two letters stay two clusters. Microsoft’s engine doesn’t join them either: Glass confirmed what the specification means, and called the engine’s behavior a known issue (“the spec is correct, the implementation is lacking”, typography-issues #232). On HarfBuzz’s side, Corbett has said that its USE “intentionally ignores a few misguided requirements of the USE specification” (HarfBuzz #542). So in practice the two engines agree, and both differ from the text.

Spaces and symbols

A symbol cluster starts with O, GB, or a hieroglyph bracket, and may take “any non-numeric tail” (NEWS for 4.0.1). A space is not one of them: it is SP, which no rule of the grammar matches, so it is a non-cluster, and a vowel sign after it is a broken cluster, with a dotted circle. A generic base such as NO-BREAK SPACE does take the vowel sign, and a pre-base one is moved before it:

FontTextOptionshb-shape output
deva2.ttfक␣िU+0915 U+0020 U+093F--language=hi[ka=0+600|space=1+250|iMatra.init=1+250|dottedcircle=1+500]
deva3.ttfक␣िU+0915 U+0020 U+093F--language=hi[ka=0+600|space=1+250|iMatra=1+250|dottedcircle=1+500]
balinese.ttf␣ᬾU+0020 U+1B3E--language=ban[space=0+250|taling=0+300|dottedcircle=0+500]
deva3.ttf िU+00A0 U+093F--language=hi[iMatra=0+250|nbsp=0+250]

The Indic shaper and USE both show the stray vowel sign on a dotted circle. The specification has an independent cluster for O, which takes nothing but a variation selector, and a symbol cluster only for symbols (S) and generic bases. HarfBuzz gives spaces the independent cluster’s behavior, but has merged the specification’s other O characters, IND, and S into one category (gen-use-table.py:271), and so they all take marks. Before 15.2.0 a space was O too, and a vowel sign after it sat on the space (HHW-30).

The wide tail is deliberate. The specification’s symbol cluster takes only consonant modifiers, < S | GB > [VS] (CMAbv)* (CMBlw)*, but HarfBuzz chose “any non-numeric tail”, and Microsoft’s engine is permissive too: when the specification was widened in 2024, Glass said the engine “has always been this way” (typography-issues #1179). Core Text, by a report in the same issue, shows dotted circles instead.

Features and stages

collect_features_use (hb-ot-shaper-use.cc:110) lays out the stages. The middle rows are the specification’s groups, under its names. The first row is HarfBuzz’s, as are Buzz and BUZZ in the last: the specification has nothing before its pre-processing group, and doesn’t mention the direction features, frac, numr, dnom, or rand.

Stage Features Pause after it
after rvrn’s the direction features, frac, numr, dnom, rand, Harf, HARF setup_syllables_use
“Default glyph pre-processing group” locl, ccmp, nukt, akhn _hb_clear_substitution_flags
“Reordering group” rphf record_rphf_use, then _hb_clear_substitution_flags
pref record_pref_use
“Orthographic unit shaping group” rkrf, abvf, blwf, half, pstf, vatu, cjct reorder_use, then hb_syllabic_clear_var
“Topographical features” isol, init, medi, fina none
“Standard typographic presentation” abvs, blws, haln, pres, psts; then Buzz, BUZZ, the common and horizontal features, and the application’s

(From features to lookups explains rvrn, Harf, and Buzz.) Feature order lists these stages beside the other shapers’. Within each group, lookups run in LookupList order, whatever their features. The specification says so: “it is, therefore, up to the font developer to specify the order of lookups for these features in the font’s OTL. The order given here is recommended but not required.” Only rphf and pref have stages of their own, because what each did must be recorded before the next.

The flags differ from the Indic shaper’s too. They are HarfBuzz’s choices, since the specification doesn’t say how lookups treat joiners:

FontTextOptionshb-shape output
deva2.ttfततU+0924 U+0924--language=hi[ta=0+550|ta=1+550]
deva3.ttfततU+0924 U+0924--language=hi[ta_ta=0+900]

The shaper zeroes the advances of GDEF marks before GPOS (HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_EARLY, :530), “what Windows does” (NEWS for 1.2.0); the Indic shaper zeroes none. The specification now says so too: “OT processing cancels the width associated with a mark”, so a base classed as a mark, to keep it out of contextual lookups, needs its width back from dist. It wasn’t the original plan. As Behdad Esfahbod tells it, Microsoft zeroed the widths because its Tibetan shaper did, and Tibetan was moving to USE (typography-issues #271). Its normalization is the Indic shaper’s, with one rule in compose_use (:503), “Avoid recomposing split matras”, explained in Normalization. So a split vowel sign such as Balinese taling tedung, U+1B40, is always split into taling and tedung, and taling moves:

FontTextOptionshb-shape output
balinese.ttfᬓᭀU+1B13 U+1B40--language=ban[taling=0+300|ka=0+600|tedung=0+300]

Reordering

USE reorders once, after the basic features, and only three kinds of glyph move: a reph, which moves forward, and pre-base vowel signs and pre-base vowel modifiers, which move back. Nothing is sorted. What counts as a reph or a pre-base form is decided by the categories, or by what the font’s rphf and pref lookups did.

What the font did

Before any lookup, setup_rphf_mask (:213) gives the rphf bit to the first three glyphs of each syllable, or only to the first if it is an encoded repha (R). The three are the specification’s: “Feature application is scoped to the first three glyphs in a cluster.” It doesn’t say whether characters the grammar never sees, such as ZWJ or CGJ, count among them. In HarfBuzz they do, so a ZWJ after RA takes one of the three places (typography-issues #279, open since 2019). After rphf, record_rphf_use (:332) finds the first glyph in that prefix that a substitution produced, and makes it R. The pause before rphf cleared every glyph’s substituted flag, so only rphf’s own work counts. In deva3.ttf, rphf ligates RA and virama, and the ligature is R from then on.

pref is on for the whole syllable. record_pref_use (:356) makes the first glyph in each syllable that pref substituted a VPre, “as they behave the same way”. The specification’s example is Javanese, which “may reorder a medial consonant to the beginning of a cluster based on context”, and leaves the context to the font.

The move

reorder_syllable_use (:383) skips number, hieroglyph, and non-clusters. Then:

/* Move things forward. */
if (info[start].use_category() == USE(R) && end - start > 1)
{
  /* Got a repha.  Reorder it towards the end, but before the first post-base
   * glyph. */
  for (unsigned int i = start + 1; i < end; i++)
  {
    bool is_post_base_glyph = (FLAG64_UNSAFE (info[i].use_category()) & POST_BASE_FLAGS64) ||
			      is_halant_use (info[i]);
    if (is_post_base_glyph || i == end - 1)
    {
      if (is_post_base_glyph)
	i--;

      buffer->merge_clusters (start, i + 1);
      /* ... move info[start] to i ... */
      break;
    }
  }
}

/* Move things back. */
unsigned int j = start;
for (unsigned int i = start; i < end; i++)
{
  uint32_t flag = FLAG_UNSAFE (info[i].use_category());
  if (is_halant_use (info[i]))
    j = i + 1;
  else if (((flag) & (FLAG (USE(VPre)) | FLAG (USE(VMPre)))) &&
	   /* Only move the first component of a MultipleSubst. */
	   0 == _hb_glyph_info_get_lig_comp (&info[i]) &&
	   j < i)
  {
    buffer->merge_clusters (j, i + 1);
    /* ... move info[i] to j ... */
  }
}

(trimmed). A post-base glyph is any final, medial, or vowel sign, any vowel modifier (POST_BASE_FLAGS64, :398), or a virama that wasn’t ligated (is_halant_use, :376, which counts H, HVM, and IS but not the sakot). A reph moves forward past everything else, bases, half forms, and subjoined forms, and stops before the first post-base glyph, or goes to the end. A pre-base vowel sign or modifier moves back to the start of the syllable, or to just after the last unligated virama before it. Each move merges the clusters it passes. This is the specification’s rule that “reordering components do not reorder past an explicit virama”. Its advice follows: a font that wants the reph or vowel sign to pass a virama can “substitute the virama for an alternate glyph” during the basic features. In HarfBuzz that isn’t enough. A substituted glyph keeps the virama’s category, and is_halant_use lets it pass only if it was ligated, so the virama must be ligated with something, as Balinese’s blwf does below. Behdad Esfahbod said in 2019 that HarfBuzz “should probably change that” (typography-issues #270); 15.2.0 hasn’t.

Balinese shows the virama rule. The adeg-adeg is a virama, and with blwf it is ligated into the subjoined form of TA; without, it stays:

FontTextOptionshb-shape output
balinese.ttfᬓ᭄ᬢᬾU+1B13 U+1B44 U+1B22 U+1B3E--language=ban[taling=0+300|ka=0+600|ta.sub=0+0]
balinese-no-blwf.ttfᬓ᭄ᬢᬾU+1B13 U+1B44 U+1B22 U+1B3E--language=ban[ka=0+600|adeg=0+0|taling=2+300|ta=2+600]
balinese.ttfᬓ᭄ᬓ᭄ᬢᬾU+1B13 U+1B44 U+1B13 U+1B44 U+1B22 U+1B3E--language=ban[taling=0+300|ka=0+600|ka.sub=0+0|ta.sub=0+0]

With blwf, taling moves to the front of the syllable; without, only to after the adeg-adeg. The trace of the first, through GSUB:

hb-shape balinese.ttf "ᬓ᭄ᬢᬾ" --language=ban --trace
start table GSUB script tag 'bali'[ka=0|adeg=0|ta=2|taling=2]
start lookup 0 feature 'blwf'[ka=0|adeg=0|ta=2|taling=2]
ligating glyphs at 1,2[ka=0|adeg=0|ta=2|taling=2]
ligated glyph at 1[ka=0|ta.sub=0|taling=0]
end lookup 0 feature 'blwf'[ka=0|ta.sub=0|taling=0]
start reordering USE[ka=0|ta.sub=0|taling=0]
skipped inserting dotted-circles because there is no broken syllables[ka=0|ta.sub=0|taling=0]
end reordering USE[taling=0|ka=0|ta.sub=0]
end table GSUB script tag 'bali'[taling=0|ka=0|ta.sub=0]

Two pre-base vowel signs in one syllable come out in reverse order. Each moves back to the same place, j, ahead of the one moved before it, so the second typed ends up first, outermost from the base. The specification doesn’t say what should happen. Glass agreed in 2021 that this, “ordering outward from the nucleus”, is right, and that DirectWrite, which doesn’t do it, should be fixed (typography-issues #750, open).

The reordering inserts dotted circles first, before the moves (reorder_use, :469), into broken clusters, after any leading R, as a B. At its end it releases use_category, and the next pause releases syllable.

When dotted circles should go in is the longest-running question about USE. The specification doesn’t say. Glass has said after GSUB and before GPOS, so that a font can’t substitute the warning away. Tests in 2019 found Core Text and DirectWrite inserting them before any GSUB, so that ccmp and blwf see them. HarfBuzz inserts them midway, after the basic features, and its issue to move them earlier is still open: Corbett’s case for midway is that a font can then tell an inserted circle from a typed one (typography-issues #281, HarfBuzz #1924). HarfBuzz also gives a run of stray marks one circle, where the specification says “a sequence of marks without a valid base forms separate clusters for each mark”; Glass closed that as not worth making consistent (typography-issues #289). Dotted circles and broken clusters has the rest of the story.

Topographical forms

The joining scripts under USE, such as Mongolian and N’Ko, take their isol, init, medi, and fina masks from the Arabic joining machine, as Joining scripts describes. For every other USE script, setup_topographical_masks (:232) sets them by syllable instead. Each syllable that joins is at first isolated; if the syllable before it was isolated or final, that one becomes initial or medial and this one final. A hieroglyph cluster, a symbol cluster, a cluster on a generic base, or a non-cluster, such as a space, breaks the chain (:258–:274), and so does a syllable with a ZWNJ in it (:304). The masks are set before any lookup, from the first syllables found, and only for those of the four features the font has.

This is HarfBuzz’s own design. The specification says the topographical features invoke forms “based on non-joining or white space boundaries”, and adds: “Support for topographical features for non-joining scripts is not currently implemented in USE. Additional specification is required.”

deva3.ttf’s init lookup shows it. Under dev2 the same lookup is the Indic shaper’s init, which takes a pre-base vowel sign at the start of a word (init); under dev3 it takes a whole cluster that another cluster follows:

FontTextOptionshb-shape output
deva2.ttfकिU+0915 U+093F--language=hi[iMatra.init=0+250|ka=0+600]
deva3.ttfकिU+0915 U+093F--language=hi[iMatra=0+250|ka=0+600]
deva3.ttfकिकU+0915 U+093F U+0915--language=hi[iMatra.init=0+250|ka=0+600|ka=2+600]
deva3.ttfकि␣U+0915 U+093F U+0020--language=hi[iMatra=0+250|ka=0+600|space=2+250]
deva3.ttfकि␣किU+0915 U+093F U+0020 U+0915 U+093F--language=hi[iMatra=0+250|ka=0+600|space=2+250|iMatra=3+250|ka=3+600]
deva3.ttfकि⁠किU+0915 U+093F U+2060 U+0915 U+093F--language=hi[iMatra=0+250|ka=0+600|space=2+0|iMatra=3+250|ka=3+600]

Alone, the cluster is isolated, and the vowel sign keeps its ordinary form. Before another cluster it is initial. Before a space it is isolated again, since the space ends the word, and in the fifth line both clusters are isolated, each a word of its own. The word joiner, a non-cluster, ends the chain too. A two-cluster word on each side of a space gets init in each:

FontTextOptionshb-shape output
deva3.ttfकिक␣किकU+0915 U+093F U+0915 U+0020 U+0915 U+093F U+0915--language=hi[iMatra.init=0+250|ka=0+600|ka=2+600|space=3+250|iMatra.init=4+250|ka=4+600|ka=6+600]
deva3.ttfकि⟨ZWNJ⟩किU+0915 U+093F U+200C U+0915 U+093F--language=hi[iMatra=0+250|ka=0+600|space=2+0|iMatra=3+250|ka=3+600]

ZWNJ ends the chain as well, though it belongs to the cluster before it, as in the second line. So for a font that uses init or fina for word-initial or word-final forms in a non-joining USE script, the edge of a word is a space, a symbol, or a ZWNJ, as well as the edge of the run. Before 15.2.0 only a hieroglyph or non-cluster ended a word, and a space didn’t: the first clusters of the fourth and fifth lines were initial (HHW-31).

The nine Indic scripts under dev3

Shaping the nine Indic scripts with USE is HarfBuzz’s and Apple’s extension of it, not part of USE as Microsoft specifies it. The specification’s list of scripts doesn’t include them, and the 3 tags that select it aren’t in the OpenType registry (Choosing a script shaper). HarfBuzz added them in 2018 because “Apple does that and we want to put it in OpenType itself” (HarfBuzz #539), and that hasn’t happened. HarfBuzz’s own developers treat the route as secondary: asked about a knd3 font in 2019, Corbett replied that “'knd2' already does what you want and is more widely supported”, and that “The USE and the Kannada shaper were never meant to be identical” (#1677).

Choosing a script shaper showed three Devanagari syllables shaped alike under dev2 and dev3. That is the common case, but not the only one. The same lookups, under the two tags:

FontTextOptionshb-shape output
deva2.ttfक्तU+0915 U+094D U+0924--language=hi[ka.half=0+400|ta=2+550]
deva3.ttfक्तU+0915 U+094D U+0924--language=hi[ka.half=0+400|ta=2+550]
deva2-half-first.ttfक्रU+0915 U+094D U+0930--language=hi[ka=0+600|rakar=0+0]
deva3-half-first.ttfक्रU+0915 U+094D U+0930--language=hi[ka.half=0+400|ra=2+500]
deva2.ttfर्कुंU+0930 U+094D U+0915 U+0941 U+0902--language=hi[ka=0+600|uMatra=0+0|reph=0+0|anusvara=0+0]
deva3.ttfर्कुंU+0930 U+094D U+0915 U+0941 U+0902--language=hi[ka=0+600|reph=0+0|uMatra=0+0|anusvara=0+0]
deva2-no-half.ttfर्क्तU+0930 U+094D U+0915 U+094D U+0924--language=hi[ka=0+600|virama=0+0|reph=0+0|ta=4+550]
deva3-no-half.ttfर्क्तU+0930 U+094D U+0915 U+094D U+0924--language=hi[ka=0+600|reph=0+0|virama=0+0|ta=4+550]

Lookup order. KA, virama, RA: the Indic shaper asks blwf about RA, finds it below-base, makes KA the base, and runs blwf in a stage before half, so RA becomes rakar whatever the LookupList says. USE asks nothing. With half’s lookup first, KA + virama becomes the half form, and blwf then has no virama to match. Under dev3, the LookupList order is the font’s shaping logic.

The reph. In Devanagari the Indic shaper puts the reph at the end of the syllable, before any anusvara; USE puts it before the first post-base glyph, here the vowel sign U, between KA and its mark. Both orders look the same once marks are positioned, but abvs, blws, and GPOS see a different sequence. And where a virama stays visible, the Indic shaper moves the reph after it (step 2 of its reph rules, The reph); USE stops before it.

The joiners behave differently too:

FontTextOptionshb-shape output
deva2.ttfक्⟨ZWNJ⟩रU+0915 U+094D U+200C U+0930--language=hi[ka=0+600|virama=0+0|space=2+0|ra=3+500]
deva3.ttfक्⟨ZWNJ⟩रU+0915 U+094D U+200C U+0930--language=hi[ka.half=0+400|space=2+0|ra=3+500]
deva2.ttfर्⟨ZWJ⟩कU+0930 U+094D U+200D U+0915--language=hi[ra=0+500|virama=0+0|space=0+0|ka=3+600]
deva3.ttfर्⟨ZWJ⟩कU+0930 U+094D U+200D U+0915--language=hi[space=0+0|ka=0+600|reph=0+0]

KA, virama, ZWNJ is how a visible virama is written inside a word. The Indic shaper honors it: ZWNJ clears the half mask. USE has no half mask, the ZWNJ comes after the two glyphs the half form needs, and the half form forms. A font can’t stop it with a lookahead either, since USE’s lookups pass over ZWNJ in their context (read from the code; not tried with a font). This book reported it (HHW-32), and HarfBuzz decided not to change it: the fix, #6768, was closed unmerged. RA, virama, ZWJ asks for an eyelash RA rather than a reph. The Indic shaper won’t make a reph when a joiner follows the virama; USE doesn’t look, and makes one. Both look like differences a dev3 font can’t easily work round (inferred), and Microsoft’s specification doesn’t say how its engine treats them. HarfBuzz’s position is that text “should be renderable with either an Indic2 or Indic3 font”, but the two shapers disagree about which strings are valid, and the issue that says so is still open (#1746). Kannada shows the cost. Under knd3, a bare RA and virama form a reph, where Kannada writers expect one only inside a word, and a font can’t test for the letter before, since rphf sees only its own cluster. Behdad Esfahbod pointed to HarfBuzz’s own HARF feature as a way round, and noted that, since “indic3 is not part of OpenType (yet)”, there was nowhere official to report the problem (HarfBuzz #1683).

Everything the Indic chapter’s Script by script table lists is absent under 3 tags, as are the reph positions, the blwf modes, and the Uniscribe heuristics. A Malayalam dot reph is an R and moves like any other reph, in HarfBuzz. The specification still says that “the category REPHA is not currently supported by USE”, a note that, as an open issue points out, seems to describe Microsoft’s engine (typography-issues #696).

For type designers: under a 3 tag, the shaper tells your lookups nothing about which consonant is the base. half, blwf, pstf, and the other basic features apply to every glyph of the syllable, together, in the order of your LookupList, so their lookups must say themselves, by context and order, where each form belongs. The shaper only moves what your rphf and pref produced, and pre-base vowel signs, and never past a virama your lookups left standing.

Where to read the source

File What’s there
src/hb-ot-shaper-use.cc the features and stages, the rphf and topographical masks, recording rphf and pref, the reordering, compose
src/hb-ot-shaper-use-machine.rl the categories, the syllable types, and the grammar, in Ragel, and the filtered view the machine runs over
src/hb-ot-shaper-use-machine.hh the machine Ragel generated from it, committed
src/hb-ot-shaper-use-table.hh the generated categories
src/gen-use-table.py the generator, with HarfBuzz’s own changes to the derivations
src/ms-use/ Microsoft’s additional category files, and IndicShapingInvalidCluster.txt for the vowel constraints
src/test-use-table.cc prints the category of a code point, or of all of them
src/update-unicode-tables.make which files each generator reads
src/hb-ot-shaper-syllabic.cc inserting dotted circles, and releasing syllable
src/hb-ot-shaper-arabic.cc the joining machine USE borrows for joining scripts

In short