Part VI · Scripts
25. The Indic model
The Indic shaper serves nine scripts: Bengali, Devanagari, Gujarati, Gurmukhi, Kannada, Malayalam, Oriya, Tamil, and Telugu. It puts characters, typed in the order they are spoken, into the order their glyphs are drawn, and tells the font’s lookups which glyph is which: which consonant is the base of the syllable, which go before it as half forms, which below or after it, and where the reph goes. Why shaping exists showed the simplest case, a vowel sign moving in front of its consonant.
There are four parts. Each character is given a category and a position from a table generated from Unicode’s data. A state machine, compiled by Ragel, divides the run into syllables. An initial reordering before the basic features finds each syllable’s base consonant, sorts the syllable by position, and sets each glyph’s feature masks. A final reordering after them moves the reph, the pre-base vowel signs, and the pre-base-reordering consonants to where the basic features’ results say they belong. In between, HarfBuzz decides what each consonant does by asking the font’s own lookups.
The examples use seven Devanagari test fonts with plain boxes for glyphs, built by fonts/indic-model/build-fonts.py from one feature file. full.ttf has one lookup each for rphf, blwf, half, pstf, and init; the others leave out or change one or two. An eighth, scripts.ttf, has a few glyphs each of Bengali, Telugu, Kannada, and Malayalam. All have their lookups under the new tags, dev2 and the like, so the shaper runs in its new mode (Choosing a script shaper). Every result and trace on this page was produced by running HarfBuzz 15.2.0 on them when the page was built.
Categories and positions
The shaper’s setup_masks hook sets no masks: those come later, “in a pause-callback”. It looks each character up with hb_indic_get_categories, which returns the category in a low byte and the position in a high one, and keeps them in the scratch bytes indic_category and indic_position (Buffers; setup_masks_indic, hb-ot-shaper-indic.cc:397).
Where the table comes from
hb-ot-shaper-indic-table.cc is generated by gen-indic-table.py from three Unicode data files: IndicSyllabicCategory.txt, IndicPositionalCategory.txt, and Blocks.txt. Its header records version 18.0.0 of each. Only some blocks are kept (gen-indic-table.py:25): the nine scripts and their extensions, Myanmar, Khmer, Vedic Extensions, General Punctuation (for the joiners), and a few more, plus NO-BREAK SPACE and DOTTED CIRCLE. Everything else is category X, position END. The table also serves the Khmer and Myanmar shapers, whose machines use the same numbers for shared categories (Khmer and Myanmar). Those numbers are kept equal by hand; only the four vowel-sign categories, VAbv, VBlw, VPre, and VPst, are checked against Myanmar’s by a static_assert (hb-ot-shaper-indic-table.cc:61). With --rust, the script writes HarfRust’s copy.
The generator works in four steps.
- Unicode’s syllabic category becomes HarfBuzz’s category (
category_map,:147). Many map to one:ConsonantandConsonant_DeadareC;ViramaandInvisible_StackerareH;Vowel_DependentisM;Bindu,Visarga, andSyllable_ModifierareSM, orSMPstif they have no position;NuktaisN;JoinerandNon_JoinerareZWJandZWNJ. - Overrides replace some categories (
category_overrides,:207). The ten RAs, one per script and Assamese’s in the Bengali block, becomeRa, so that the grammar can recognize a reph; the rest, most with an issue number, are characters that behave unlike their Unicode category, such as Vedic signs and the dotted circle. - Unicode’s positional category becomes a position, but only for the categories that have one (
CM,SM,RS,H,M,MPst;:457). A split vowel sign gets the position of its last part (:193). Then every consonant-like category getsBASE_C, syllable modifiers and Vedic signsSMVD, and each vowel sign’s position is translated, per script, into where it should sort (indic_matra_position,:427): a left one is alwaysPRE_M; a right one isAFTER_SUBin Devanagari,AFTER_POSTin Bengali. - Two positions are overridden (
position_overrides,:386): U+0A51 GURMUKHI SIGN UDAAT, and the Oriya candrabindu, “BeforeSub in the spec”.
The positions are an ordered list, from left to right in the finished syllable (hb-ot-shaper-indic.hh:36):
| Value | Position | What has it |
|---|---|---|
| 0 | POS_START |
|
| 1 | POS_RA_TO_BECOME_REPH |
an initial RA that will be the reph |
| 2 | POS_PRE_M |
left (pre-base) vowel signs |
| 3 | POS_PRE_C |
consonants before the base |
| 4 | POS_BASE_C |
the base; at first, every consonant |
| 5 | POS_AFTER_MAIN |
some vowel signs (Oriya’s top ones, for example) |
| 6 | POS_ABOVE_C |
|
| 7 | POS_BEFORE_SUB |
vowel signs that go before below-base forms |
| 8 | POS_BELOW_C |
consonants with a below-base form |
| 9 | POS_AFTER_SUB |
vowel signs after below-base forms (all of Devanagari’s right, top, and bottom ones) |
| 10 | POS_BEFORE_POST |
|
| 11 | POS_POST_C |
consonants with a post-base or pre-base-reordering form |
| 12 | POS_AFTER_POST |
vowel signs after post-base forms |
| 13 | POS_SMVD |
syllable modifiers and Vedic signs |
| 14 | POS_END |
everything else |
Sorting a syllable by these numbers is most of the initial reordering. A consonant’s position isn’t in the table: every consonant starts as BASE_C, and the font decides the rest. A vowel sign’s position depends on its script (gen-indic-table.py:393):
| Script | Right | Top | Bottom |
|---|---|---|---|
| Devanagari | AFTER_SUB |
AFTER_SUB |
AFTER_SUB |
| Bengali | AFTER_POST |
(none) | AFTER_SUB |
| Gurmukhi | AFTER_POST |
AFTER_POST, “Deviate from spec” |
AFTER_POST |
| Gujarati | AFTER_POST |
AFTER_SUB |
AFTER_POST |
| Oriya | AFTER_POST |
AFTER_MAIN |
AFTER_SUB |
| Tamil | AFTER_POST |
AFTER_SUB |
AFTER_POST |
| Telugu | BEFORE_SUB to U+0C42, then AFTER_SUB |
BEFORE_SUB |
BEFORE_SUB |
| Kannada | BEFORE_SUB, but AFTER_SUB for U+0CC3 to U+0CD6 |
BEFORE_SUB |
BEFORE_SUB |
| Malayalam | AFTER_POST |
(none) | AFTER_POST |
The per-script configuration
What differs between the scripts is kept, where it is “cheaper keeping here, than in the code”, in one table (indic_configs, :153); anything only one script needs is an if in the code.
| Script | Old spec | Virama | Reph position | Reph mode | blwf mode |
|---|---|---|---|---|---|
| (default) | no | none | before post | implicit | pre and post |
| Devanagari | yes | U+094D | before post | implicit | pre and post |
| Bengali | yes | U+09CD | after sub | implicit | pre and post |
| Gurmukhi | yes | U+0A4D | before sub | implicit | pre and post |
| Gujarati | yes | U+0ACD | before post | implicit | pre and post |
| Oriya | yes | U+0B4D | after main | implicit | pre and post |
| Tamil | yes | U+0BCD | after post | implicit | pre and post |
| Telugu | yes | U+0C4D | after post | explicit | post only |
| Kannada | yes | U+0CCD | after post | implicit | post only |
| Malayalam | yes | U+0D4D | after main | log repha | pre and post |
The reph mode says how a reph is written: as RA + virama (implicit), as RA + virama + ZWJ (explicit, Telugu), or as an encoded repha character, U+0D4E MALAYALAM LETTER DOT REPH (log repha). The reph position is where the final reordering moves it. The blwf mode says whether consonants before the base may take below-base forms too. Old spec is has_old_spec (Choosing a script shaper).
The shaper needs the virama’s glyph to question the font, and a plan has no font. So load_virama_glyph (:281) looks it up in the cmap the first time the plan shapes a buffer, and keeps it in the plan, in an atomic. A comment admits that “the spec says we should apply ‘locl’ to virama too. Maybe one day...”.
Finding syllables
The shaper works one syllable at a time, so the first thing it does, in a GSUB pause before any lookup, is find them (setup_syllables_indic, :414). It runs a state machine over the categories, and marks each syllable unsafe to break inside.
Ragel
Ragel compiles regular expressions over an alphabet, with actions for their matches, into C code with the machine as tables. The grammar is hb-ot-shaper-indic-machine.rl (154 lines); the generated hb-ot-shaper-indic-machine.hh (1,302 lines, nearly all tables) is committed, so building HarfBuzz doesn’t need Ragel. If meson finds Ragel 6.10, gen-ragel-artifacts.py regenerates the header with ragel -e -F1 and copies it into src/. The Khmer, Myanmar, and USE machines, and the buffer deserializers, are made the same way (src/meson.build:281).
The alphabet is the category byte (getkey info[p].indic_category()). The categories are numbered Ragel exports, which the generated header turns into the #defines the shaper uses through I_Cat(…):
export X = 0;
export C = 1;
export V = 2;
export N = 3;
export H = 4;
export ZWNJ = 5;
export ZWJ = 6;
export M = 7;
export SM = 8;
...
export Repha = 14;
export Ra = 15;
export CM = 16;
export Symbol= 17;
export CS = 18;
export SMPst = 57;
(trimmed; hb-ot-shaper-indic-machine.rl:63). The grammar follows in full (:86):
c = (C | Ra); # is_consonant
n = ((ZWNJ?.RS)? (N.N?)?); # is_consonant_modifier
z = ZWJ|ZWNJ; # is_joiner
reph = (Ra H | Repha); # possible reph
sm = SM | SMPst;
cn = c.ZWJ?.n?;
symbol = Symbol.N?;
matra_group = z*.(M | sm? MPst).N?.H?;
syllable_tail = (z?.sm.sm?.ZWNJ?)? (A | VD)*;
halant_group = (z?.H.(ZWJ.N?)?);
final_halant_group = halant_group | H.ZWNJ;
medial_group = CM?;
halant_or_matra_group = (final_halant_group | matra_group*);
complex_syllable_tail = (halant_group.cn)* medial_group halant_or_matra_group syllable_tail;
consonant_syllable = (Repha|CS)? cn complex_syllable_tail;
vowel_syllable = reph? V.n? (ZWJ | complex_syllable_tail);
standalone_cluster = ((Repha|CS)? PLACEHOLDER | reph? DOTTEDCIRCLE).n? complex_syllable_tail;
symbol_cluster = symbol syllable_tail;
broken_cluster = reph? n? complex_syllable_tail;
other = any;
main := |*
consonant_syllable => { found_syllable (indic_consonant_syllable); };
vowel_syllable => { found_syllable (indic_vowel_syllable); };
standalone_cluster => { found_syllable (indic_standalone_cluster); };
symbol_cluster => { found_syllable (indic_symbol_cluster); };
SMPst => { found_syllable (indic_non_indic_cluster); };
broken_cluster => { found_syllable (indic_broken_cluster); buffer->scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_BROKEN_SYLLABLE; };
other => { found_syllable (indic_non_indic_cluster); };
*|;
A consonant syllable is a consonant, then optionally more consonants each joined by a virama, a medial, either a final virama or any number of vowel signs, and syllable modifiers (such as anusvara) and Vedic signs. An initial RA + virama is just its first consonant and virama. A vowel syllable is built the same way on an independent vowel, a standalone cluster on NO-BREAK SPACE or a dotted circle. A symbol cluster is a symbol such as avagraha with its marks; a broken cluster is marks with nothing to attach to; anything else is a non-Indic cluster of one character. Virama + ZWNJ ends a syllable: it is how a visible virama is written inside a word.
|* … *| makes the machine a scanner: at each point it takes the longest match, and of equally long ones the first listed. So the catch-alls come last. A vowel sign alone matches both broken_cluster and other, with the same length, and broken_cluster wins.
found_syllable (:123) writes into each glyph’s syllable byte the type in the low four bits and a serial number in the high four:
for (unsigned int i = ts; i < te; i++)
info[i].syllable() = (syllable_serial << 4) | syllable_type;
syllable_serial++;
if (syllable_serial == 16) syllable_serial = 1;
The serial need only differ from the previous syllable’s, so that foreach_syllable (hb-ot-layout.hh:169), which walks forward while the byte stays the same, can see where a syllable ends. The same byte keeps per-syllable lookups inside a syllable (GSUB). A broken cluster also sets a buffer scratch flag, so the dotted-circle step can skip buffers with none.
Some syllables of different types:
| Font | Text | Options | hb-shape output |
|---|---|---|---|
| full.ttf | किU+0915 U+093F | --language=hi | [iMatra.init=0+250| |
| full.ttf | अिU+0905 U+093F | --language=hi | [iMatra.init=0+250| |
| full.ttf | िU+00A0 U+093F | --language=hi | [iMatra.init=0+250| |
| full.ttf | िU+093F | --language=hi | [iMatra.init=0+250| |
| full.ttf | क्⟨ZWNJ⟩िU+0915 U+094D U+200C U+093F | --language=hi | [ka=0+600| |
A consonant syllable, a vowel syllable, and a standalone cluster are reordered alike: the code treats vowels and placeholders “as if they were consonants. This is safe because Vowels cannot happen in a consonant syllable” (hb-ot-shaper-indic.cc:61). A lone vowel sign is a broken cluster and gets a dotted circle, as does the one after KA, virama, and ZWNJ, whose syllable the ZWNJ ended (Dotted circles and broken clusters). Symbol and non-Indic clusters are left alone.
A limit on syllable length
foreach_syllable never returns a syllable longer than HB_MAX_SYLLABLE_LENGTH, 64 glyphs (hb-limits.hh:66, used at hb-ot-layout.hh:180); a longer one is reordered in pieces, each treated as a syllable. With 32 TAs joined by viramas, then KA and vowel sign I (66 characters), full.ttf put the vowel sign just before KA, and left the 32nd TA, the first piece’s base, full with a visible virama (tried, not shown). So a branch for syllables over 127 glyphs (hb-ot-shaper-indic.cc:813) is never taken (inferred).
Features and stages
collect_features_indic (:244) lays out the GSUB stages:
| Stage | Features | Pause after it |
|---|---|---|
after rvrn’s |
the direction features, frac, numr, dnom, rand, Harf, HARF |
setup_syllables_indic |
| next | locl, ccmp |
initial_reordering_indic |
| one each | nukt, akhn, rphf, rkrf, pref, blwf, abvf, half, pstf, vatu, cjct |
none, and after cjct’s, final_reordering_indic |
| last | init, pres, abvs, blws, psts, haln; then Buzz, BUZZ, the common and horizontal features, and the application’s |
hb_syllabic_clear_var |
(From features to lookups explains rvrn and the private Harf and Buzz.) Feature order lists these stages beside the other shapers’. A Harf lookup runs before syllables are found. The shaper’s features, from locl on, are all per syllable. nukt, akhn, rkrf, vatu, cjct, and the presentation features other than init are global; rphf, pref, blwf, abvf, half, pstf, and init have mask bits, which the reordering sets on the glyphs that should take them. The basic and presentation features have both manual-joiner flags, so their input never passes over ZWJ or ZWNJ (Joiners). override_features_indic turns liga off and adds the last pause, which gives back the syllable byte (:272).
The basic features are “applied in order, one at a time” (:174), each seeing what the one before made. The presentation features are “applied all at once”, in LookupList order, because the “Default Bengali font in Windows for example has intermixed lookups for init,pres,abvs,blws features” (:192). A stage per basic feature has a second use, which comes next.
How the font decides the base
In consonant + virama + consonant, the second consonant may take a below-base form (RA as rakar), a post-base form, or a pre-base-reordering form; or the first may become a half form on the second as base. Which, for each consonant, is the font designer’s choice. HarfBuzz asks the font.
The plan keeps, for each of rphf, pref, blwf, pstf, and vatu, the lookups of that feature’s stage, which are exactly that feature’s lookups (hb_indic_would_substitute_feature_t, :90; data_create_indic, :341). Its would_substitute asks whether any would substitute a given glyph sequence, through hb_ot_layout_lookup_would_substitute_with_buffer, which leaves the glyphs alone and charges the buffer’s operation budget. At the start of the initial reordering, update_consonant_positions_indic (:440) asks it about every glyph still at BASE_C (consonant_position_from_face, :361):
hb_codepoint_t glyphs[3] = {virama, consonant, virama};
if (indic_plan->blwf.would_substitute (glyphs , 2, face, buffer) ||
indic_plan->blwf.would_substitute (glyphs+1, 2, face, buffer) ||
indic_plan->vatu.would_substitute (glyphs , 2, face, buffer) ||
indic_plan->vatu.would_substitute (glyphs+1, 2, face, buffer))
return POS_BELOW_C;
if (indic_plan->pstf.would_substitute (glyphs , 2, face, buffer) ||
indic_plan->pstf.would_substitute (glyphs+1, 2, face, buffer))
return POS_POST_C;
if (indic_plan->pref.would_substitute (glyphs , 2, face, buffer) ||
indic_plan->pref.would_substitute (glyphs+1, 2, face, buffer))
return POS_POST_C;
return POS_BASE_C;
A consonant is below-base if blwf or vatu would substitute virama + consonant, post-base if pstf or pref would, and a possible base otherwise. Each question is asked in the new model’s order and the old one’s, because “some broken fonts (like Free Sans) simply copied lookups from old-spec to new-spec without modification. And oddly enough, Uniscribe seems to respect those lookups.”
The consonant is the buffer’s glyph after locl and ccmp, but the virama is the cmap’s, so a font whose locl replaces the virama is asked about the wrong one. With zero_context (:340), a chaining rule with any backtrack or lookahead doesn’t count (hb-ot-layout-gsubgpos.hh:4741). It is set for the new model, except in Malayalam:
testing with Malayalam shows that old and new spec both allow context. Testing with Bengali new-spec however shows that it doesn’t. So, the heuristic here is the way it is. It should only be changed, as we discover more cases of what Windows does. DON’T TOUCH OTHERWISE.
Here is KA, virama, RA, and vowel sign I in four of the test fonts:
| Font | Text | Options | hb-shape output |
|---|---|---|---|
| full.ttf | क्रिU+0915 U+094D U+0930 U+093F | --language=hi | [iMatra.init=0+250| |
| no-blwf.ttf | क्रिU+0915 U+094D U+0930 U+093F | --language=hi | [iMatra.init=0+250| |
| old-order.ttf | क्रिU+0915 U+094D U+0930 U+093F | --language=hi | [iMatra.init=0+250| |
| no-blwf-half.ttf | क्रिU+0915 U+094D U+0930 U+093F | --language=hi | [ka=0+600| |
In full.ttf, blwf makes RA below-base, so KA is the base: the vowel sign goes before KA and RA below it. Without blwf, RA is the base and KA takes its half form. old-order.ttf’s blwf is written RA + virama; the probe accepts that, so KA is the base, but the lookup never matches the new order, and nothing forms: KA, a visible virama, a full RA. In no-blwf-half.ttf RA is the base, KA has no half form, and the vowel sign ends up between them, as the final reordering explains.
For type designers: the Indic shaper reads a consonant’s role from your
blwf,vatu,pstf, andpreflookups, by asking whether each would substitute the virama + consonant pair on its own. A consonant whose below-base form is made only by a contextual rule, or only by a lookup under some other feature, is a base consonant to HarfBuzz. For the new Indic tags (except Malayalam), a rule with backtrack or lookahead doesn’t count. “Other complex scripts” in Simon Cozens’ Fonts and Layout for Global Scripts walks through the same exchange from the font’s side, with Devanagari.
Initial reordering
initial_reordering_indic (:977) updates the consonant positions, inserts dotted circles into broken clusters, and reorders every syllable but symbol and non-Indic ones with initial_reordering_consonant_syllable (:466), whose comments quote Microsoft’s Devanagari specification step by step.
The reph
In the implicit mode, a reph needs RA + virama at the start, not followed by a joiner, and an rphf mask bit, which exists only if the font has the feature. Then the rphf probe is asked about the two glyphs (:510):
if (indic_plan->rphf.would_substitute (glyphs, 2, face, buffer) || …)
{
limit += 2;
while (limit < end && is_joiner (info[limit]))
limit++;
base = start;
has_reph = true;
}
(trimmed). If it would, limit keeps the RA and virama out of the base search. A Malayalam dot reph is set aside without asking. If the RA is the only consonant, it is the base after all (:593).
In Telugu’s explicit mode the reph is RA + virama + ZWJ, and the probe is asked about two glyphs or three. Kannada’s mode is implicit, but “for compatibility with legacy usage in Kannada, Ra+h+ZWJ must behave like Ra+ZWJ+h”: the shaper swaps the ZWJ and virama (:478, issue #435), and the probe is then asked about RA + ZWJ. scripts.ttf has a reph and a below-base KA in both scripts:
| Font | Text | Options | hb-shape output |
|---|---|---|---|
| scripts.ttf | ర్కU+0C30 U+0C4D U+0C15 | --language=te | [te.ra=0+500| |
| scripts.ttf | ర్⟨ZWJ⟩కU+0C30 U+0C4D U+200D U+0C15 | --language=te | [space=0+0| |
| scripts.ttf | ರ್ಕU+0CB0 U+0CCD U+0C95 | --language=kn | [kn.ka=0+600| |
| scripts.ttf | ರ್⟨ZWJ⟩ಕU+0CB0 U+0CCD U+200D U+0C95 | --language=kn | [kn.ra=0+500| |
Telugu needs the ZWJ for a reph. Kannada makes one without it, and with it RA is the base, its ZWJ (drawn with the space glyph) now before the virama.
The grammar allows a reph before a dotted circle, not before NO-BREAK SPACE, so RA + virama + NBSP is two syllables:
| Font | Text | Options | hb-shape output |
|---|---|---|---|
| full.ttf | र् U+0930 U+094D U+00A0 | --language=hi | [ra=0+500| |
| full.ttf | र्◌U+0930 U+094D U+25CC | --language=hi | [dottedcircle=0+500| |
Finding the base
Then the base search, from the end of the syllable backwards (:543):
unsigned int i = end;
bool seen_below = false;
do {
i--;
if (is_consonant (info[i]))
{
if (info[i].indic_position() != POS_BELOW_C &&
(info[i].indic_position() != POS_POST_C || seen_below))
{
base = i;
break;
}
if (info[i].indic_position() == POS_BELOW_C)
seen_below = true;
base = i;
}
else
{
if (start < i &&
info[i].indic_category() == I_Cat(ZWJ) &&
info[i - 1].indic_category() == I_Cat(H))
break;
}
} while (i > limit);
(trimmed of its comments). It passes over consonants with below-base or post-base forms (including pre-base-reordering ones, which are POS_POST_C) and stops at the first with neither, or at the first consonant. Post-base forms must follow below-base ones, so after a below-base consonant a post-base one stops the search. In the else, a ZWJ after a virama “stops the base search, and requests an explicit half form”:
| Font | Text | Options | hb-shape output |
|---|---|---|---|
| full.ttf | क्रU+0915 U+094D U+0930 | --language=hi | [ka=0+600| |
| full.ttf | क्⟨ZWJ⟩रU+0915 U+094D U+200D U+0930 | --language=hi | [ka.half=0+400| |
With the ZWJ, RA is the base and KA takes its half form. is_consonant and the other tests (is_one_of, :54) are false for a ligated glyph, “If it ligated, all bets are off”, which matters most in the final reordering.
A ZWJ before a virama “requests a subjoined form instead, and hence search continues. This is particularly important for Bengali sequence Ra,H,Ya that should form Ya-Phalaa”. The ZWJ also stops the reph, as the probe is asked about RA + ZWJ:
| Font | Text | Options | hb-shape output |
|---|---|---|---|
| scripts.ttf | র্যU+09B0 U+09CD U+09AF | --language=bn | [bn.ya=0+550| |
| scripts.ttf | র⟨ZWJ⟩্যU+09B0 U+200D U+09CD U+09AF | --language=bn | [bn.ra=0+500| |
Without the ZWJ, YA is the base under a reph; with it, RA is the base, with ya-phalaa.
Positions, and the sort
Glyphs before the base get at most POS_PRE_C, the base POS_BASE_C, the reph’s RA POS_RA_TO_BECOME_REPH (:634). The old model then moves its virama (Old and new Indic tags).
Marks with no place of their own, joiners, nuktas, register shifters, medials, and viramas, take the position of the glyph before them (:694), so a nukta sorts with its consonant and a virama with the consonant it kills. But a virama after a left vowel sign takes the position of what preceded the vowel sign: “Uniscribe doesn’t move the Halant with Left Matra”. Each post-base consonant takes everything since the last consonant or vowel sign before it (:725).
Then hb_stable_sort sorts the syllable by position (:746): “Sit tight, rock ’n roll!” A stable sort would leave several left vowel signs in typed order, the first typed furthest left; the code reverses the run, keeping each with its own nukta and virama, so the last typed is furthest from the base (:767, issue #3863).
Clusters are merged only from the base on. The syllable byte is borrowed to hold each glyph’s original index, and each cycle of the permutation has its span merged (:818). Before the base, merging waits for the final reordering, which moves pre-base vowel signs again: “these two merge-clusters from the two sides of base will interlock to merge things correctly” (issue #2272). The old model merges everything from the base to the end.
Masks
Last, the masks (:843), which tell the basic features which glyph is which:
| Glyphs | Masks |
|---|---|
| the reph’s RA and virama | rphf |
| before the base | half; and blwf too, in the new model where the blwf mode is “pre and post” |
| the base | none |
| after the base | blwf, abvf, and pstf |
the first virama + consonant pair after the base that pref would substitute |
pref |
In Telugu and Kannada (“post only”), a consonant before the base never takes a below-base form. In old-model Devanagari, RA + virama before the base gets blwf too, unless a ZWJ follows: that is “the correct way to request eyelash form of Ra” (:869). A ZWNJ clears half from the glyphs back to the previous consonant (:919); ZWJ and ZWNJ stop cjct “by being there”, since cjct doesn’t pass over them. Here virama + ZWNJ also ends KA’s syllable, and ZWJ asks for the half form explicitly:
| Font | Text | Options | hb-shape output |
|---|---|---|---|
| full.ttf | क्तU+0915 U+094D U+0924 | --language=hi | [ka.half=0+400| |
| full.ttf | क्⟨ZWNJ⟩तU+0915 U+094D U+200C U+0924 | --language=hi | [ka=0+600| |
| full.ttf | क्⟨ZWJ⟩तU+0915 U+094D U+200D U+0924 | --language=hi | [ka.half=0+400| |
vatu-cjct.ttf makes RA’s below-base form in the global vatu rather than blwf, which the probe accepts (issue #1587), and has a cjct ligature of KA’s half form and TA:
| Font | Text | Options | hb-shape output |
|---|---|---|---|
| vatu-cjct.ttf | क्रिU+0915 U+094D U+0930 U+093F | --language=hi | [iMatra.init=0+250| |
| vatu-cjct.ttf | क्तU+0915 U+094D U+0924 | --language=hi | [kta=0+700] |
| vatu-cjct.ttf | क्⟨ZWJ⟩तU+0915 U+094D U+200D U+0924 | --language=hi | [ka.half=0+400| |
KA is the base, as with full.ttf, not RA as with no-blwf.ttf. The ZWJ keeps the half form but stops the conjunct.
The trace for no-blwf-half.ttf, from this pause to the end of GSUB:
hb-shape no-blwf-half.ttf "क्रि" --language=hi --trace| start reordering indic initial | [ka=0|virama=0|ra=2|iMatra=2] |
| skipped inserting dotted-circles because there is no broken syllables | [ka=0|virama=0|ra=2|iMatra=2] |
| end reordering indic initial | [iMatra=2|ka=0|virama=0|ra=2] |
| start lookup 0 feature 'rphf' | [iMatra=2|ka=0|virama=0|ra=2] |
| end lookup 0 feature 'rphf' | [iMatra=2|ka=0|virama=0|ra=2] |
| start reordering indic final | [iMatra=2|ka=0|virama=0|ra=2] |
| end reordering indic final | [ka=0|virama=0|iMatra=2|ra=2] |
| start lookup 2 feature 'init' | [ka=0|virama=0|iMatra=2|ra=2] |
| end lookup 2 feature 'init' | [ka=0|virama=0|iMatra=2|ra=2] |
| end table GSUB script tag 'dev2' | [ka=0|virama=0|iMatra=2|ra=2] |
The vowel sign is sorted to the front, its cluster not yet merged; the final reordering moves it again and merges it.
Final reordering
final_reordering_indic (:1457) runs after cjct, on glyphs the basic features have changed. Positions still match the glyph order, but categories may no longer fit, so final_reordering_syllable_indic (:1007) first repairs one sure case: the virama glyph, ligated and then multiplied, gets category H back. Afterward indic_category and indic_position are released.
The base again
The base is the first glyph at BASE_C or later (:1049). If a glyph after it has the pref mask but wasn’t ligated, “this was a ‘pref’ candidate but didn’t form any. Base is around here...”, and that consonant becomes the base. In Malayalam, so does a below-base consonant whose virama is still there: “skip over unformed below- (but NOT post-) forms” (:1075). A base that lands on a nukta or virama steps back.
Malayalam’s probes count contextual rules, so a promised form may not form. In scripts.ttf, ignore rules block LA’s below-base form after KA and RA’s pref form after TA:
| Font | Text | Options | hb-shape output |
|---|---|---|---|
| scripts.ttf | ത്ലെU+0D24 U+0D4D U+0D32 U+0D46 | --language=ml | [ml.eMatra=0+300| |
| scripts.ttf | ക്ലെU+0D15 U+0D4D U+0D32 U+0D46 | --language=ml | [ml.ka=0+600| |
| scripts.ttf | ക്രെU+0D15 U+0D4D U+0D30 U+0D46 | --language=ml | [ml.eMatra=0+300| |
| scripts.ttf | ത്രെU+0D24 U+0D4D U+0D30 U+0D46 | --language=ml | [ml.ta=0+550| |
Where a form is blocked, its consonant becomes the base, and vowel sign E goes before it, not at the front.
Pre-base vowel signs
A pre-base vowel sign moves from the front to “after last standalone halant glyph, after initial matra position and before the main consonant” (:1108): after the last virama that wasn’t ligated into a half form. Half forms stay after the vowel sign:
| Font | Text | Options | hb-shape output |
|---|---|---|---|
| no-blwf.ttf | क्रिU+0915 U+094D U+0930 U+093F | --language=hi | [iMatra.init=0+250| |
| no-blwf-half.ttf | क्रिU+0915 U+094D U+0930 U+093F | --language=hi | [ka=0+600| |
The specification adds “If ZWJ or ZWNJ follow this halant, position is moved after it.” That code is in an #if 0: testing with Windows 7 showed that the vowel sign doesn’t pass a virama + ZWJ, and the search goes on before it, while a ZWNJ has already ended the syllable (issue #1070). The move then merges clusters up to the base. Malayalam and Tamil skip the search and put the vowel sign, and a pref form (:1402), straight before the base, as their half glyphs are “Chillus or ligated explicit viramas. We want to position matra after them” (:1141). Here NA’s half form is a chillu, a consonant without its vowel, written as a letter of its own:
| Font | Text | Options | hb-shape output |
|---|---|---|---|
| scripts.ttf | ന്കെU+0D28 U+0D4D U+0D15 U+0D46 | --language=ml | [ml.na.chillu=0+450| |
The reph
The reph moves only if it formed (:1237). The test is an exclusive or:
if (start + 1 < end &&
info[start].indic_position() == POS_RA_TO_BECOME_REPH &&
((info[start].indic_category() == I_Cat(Repha)) ^
_hb_glyph_info_ligated_and_didnt_multiply (&info[start])))
RA + virama moves if rphf ligated it; an encoded repha moves if it did not: “If it ligated, it’s probably the font trying to make it work without the reordering.” The reph glyph’s GDEF class plays no part: a reph that rphf formed moves whether or not the font puts it in a mark class. Where to depends on the reph position, through the specification’s six steps:
- After post-base forms: go to step 5.
- Otherwise, after the first unligated virama between the reph and the base, or a joiner after it.
- After the main consonant (Oriya, Malayalam): after the base and anything with position up to
POS_AFTER_MAIN. - After below-base forms (Bengali): before the first glyph at
POST_C,AFTER_POST, orSMVD: “our take on what step 4 is trying to say (and failing, BADLY).” - Step 2, “Copied from step 2”, which is new only to scripts coming from step 1.
- Otherwise, the end of the syllable, before trailing syllable modifiers and Vedic signs, and before a virama that follows a vowel sign, “so it can interact with the Matra”.
“Before sub” and “before post” are never tested for. Devanagari, Gujarati, and Gurmukhi fall through to step 6, and the reph goes to the end, before any anusvara:
| Font | Text | Options | hb-shape output |
|---|---|---|---|
| full.ttf | र्किU+0930 U+094D U+0915 U+093F | --language=hi | [iMatra.init=0+250| |
| full.ttf | र्कुंU+0930 U+094D U+0915 U+0941 U+0902 | --language=hi | [ka=0+600| |
| no-rphf.ttf | र्किU+0930 U+094D U+0915 U+093F | --language=hi | [ra=0+500| |
Without rphf, RA is an ordinary pre-base consonant with no half form, so the vowel sign moves after its virama. The trace shows both moves in one pause:
hb-shape full.ttf "र्कि" --language=hi --trace| start reordering indic final | [reph=0|iMatra=2|ka=2] |
| end reordering indic final | [iMatra=0|ka=0|reph=0] |
Moving the reph merges the clusters of everything it passes. A Malayalam dot reph needs no rphf. Unless the font ligated it, it moves after the base and anything at POS_AFTER_MAIN, so before a right vowel sign:
| Font | Text | Options | hb-shape output |
|---|---|---|---|
| scripts.ttf | ൎകU+0D4E U+0D15 | --language=ml | [ml.ka=0+600| |
| scripts.ttf | ൎകാU+0D4E U+0D15 U+0D3E | --language=ml | [ml.ka=0+600| |
Pre-base-reordering consonants
A pre-base-reordering consonant follows the base in the text but is drawn before it, as Malayalam’s RA after a virama is. pref makes its form, and the final reordering moves it if it ligated (“We don’t check that the :1378), to where a pre-base vowel sign would go. pref.ttf gives Devanagari YA such a form, which no Devanagari font needs but the shaper allows:
| Font | Text | Options | hb-shape output |
|---|---|---|---|
| full.ttf | क्यU+0915 U+094D U+092F | --language=hi | [ka=0+600| |
| pref.ttf | क्यU+0915 U+094D U+092F | --language=hi | [ya.pref=0+300| |
| pref.ttf | क्यिU+0915 U+094D U+092F U+093F | --language=hi | [iMatra.init=0+250| |
In full.ttf, YA’s pstf form stays after KA. In pref.ttf, YA is post-base too, but its virama and YA get the pref mask, ligate, and move before KA:
hb-shape pref.ttf "क्यि" --language=hi --trace| end reordering indic initial | [iMatra=2|ka=0|virama=0|ya=0] |
| start lookup 4 feature 'pref' | [iMatra=2|ka=0|virama=0|ya=0] |
| ligating glyphs at 2,3 | [iMatra=2|ka=0|virama=0|ya=0] |
| ligated glyph at 2 | [iMatra=2|ka=0|ya.pref=0] |
| end lookup 4 feature 'pref' | [iMatra=2|ka=0|ya.pref=0] |
| start lookup 2 feature 'half' | [iMatra=2|ka=0|ya.pref=0] |
| end lookup 2 feature 'half' | [iMatra=2|ka=0|ya.pref=0] |
| start reordering indic final | [iMatra=2|ka=0|ya.pref=0] |
| end reordering indic final | [iMatra=0|ya.pref=0|ka=0] |
pref’s lookup, number 4, runs before half’s, number 2 (and blwf’s, number 1, left out of the trace as it had nothing to match): each basic feature has its own stage.
init
Last, a pre-base vowel sign starting a word gets the init mask (:1439): after a glyph whose general category is none of the letters, the marks, Cf, Cn, Co, and Cs, or at the start of the buffer, unless the character just before it in the pre-context is one of those:
| Font | Text | Options | hb-shape output |
|---|---|---|---|
| full.ttf | किU+0915 U+093F | --language=hi | [iMatra.init=0+250| |
| full.ttf | ककिU+0915 U+0915 U+093F | --language=hi | [ka=0+600| |
| full.ttf | ␣किU+0020 U+0915 U+093F | --language=hi | [space=0+250| |
| full.ttf | किU+0915 U+093F | --language=hi --text-before=क | [iMatra=0+250| |
After KA it isn’t initial, and the two syllables become unsafe to break between. In the last line the earlier KA is given as pre-context, and the vowel sign isn’t initial either, so an application that splits a word into two runs, for a change of color say, and passes the first as context, gets the same form as from one run. Only the nearest character of the pre-context counts. Before 15.2.0 the test looked only inside the buffer, and the last line was initial (HHW-22).
The old model
The old model’s virama move (Choosing a script shaper) takes the first virama after the base to after the last consonant (:672): KA, virama, RA, virama, with RA below-base, becomes KA, RA, virama, virama, except in Kannada. The old model also differs in three ways, all met above: its probes count contextual rules; glyphs before the base get no blwf mask, except Devanagari RA + virama; and after the sort everything from the base on is one cluster, because “we move halants around”.
Script by script
The rules that apply to one script or two, with where each is:
| Script | Rule | Where |
|---|---|---|
| Devanagari | old model: RA + virama before the base gets blwf, unless ZWJ follows |
:869 |
| Devanagari, Bengali | RRA (U+0931), and Bengali RRA and RHA, aren’t decomposed (issue #779); Bengali YA + nukta is recomposed to U+09DF, though Unicode excludes it | :1494, :1529 |
| Bengali | RA + ZWJ + virama + YA: RA is the base, YA ya-phalaa | :575 |
| Gurmukhi | U+0A40 VOWEL SIGN II is MPst, so a bindi may come before it, and sorts with it (sm? MPst in the grammar) |
gen-indic-table.py:255; :716 |
| Gurmukhi | U+0A51 UDAAT is a vowel sign at BELOW_C (issue #524) |
gen-indic-table.py:292, :388 |
| Tamil | pre-base vowel signs and pref forms go straight before the base; Tamil AU isn’t decomposed |
:1141, :1402, :1498 |
| Telugu | reph only with ZWJ; no below-base forms before the base | :514, :853 |
| Kannada | RA + virama + ZWJ becomes RA + ZWJ + virama; no below-base forms before the base; the old model’s virama stays before a final virama | :478, :853, :674 |
| Malayalam | the dot reph; contextual rules count in the probes; unformed below-base forms make a new base; pre-base glyphs straight before the base | :532, :340, :1075, :1141 |
Uniscribe
Microsoft’s specifications say what Uniscribe was meant to do; fonts were tested against what it did. The comments record where HarfBuzz follows the latter:
| Where | What HarfBuzz does, and why |
|---|---|
consonant_position_from_face, :367 |
asks the probes in both orders, because Uniscribe honors old-order lookups in new-model fonts |
data_create_indic, :331 |
uses zero-context probes except for Malayalam and the old model, from testing what Windows does |
initial reordering, :644 |
moves the old model’s virama unconditionally, except in Kannada, where “Uniscribe does this only if there is not a Halant after last consonant already” |
initial reordering, :704 |
doesn’t move a virama with a left vowel sign: “Uniscribe doesn’t move the Halant with Left Matra” |
final reordering, :1119 |
doesn’t move a pre-base vowel sign past ZWJ, from testing with Windows 7 |
final reordering, :1338 |
puts a reph before a final virama only after a vowel sign + virama, “Uniscribe doesn’t do this” otherwise |
indic_features, :192 |
applies the presentation features together, for the Windows Bengali font |
NEWS for 12.3.0 records the removal of “the disabled by default uniscribe-bug-compatible mode” from the Indic and Khmer shapers. Asking the font goes back to 0.9.6: “Make reph and ‘pref’ logic introspect the font.”
Where to read the source
| File | What’s there |
|---|---|
src/hb-ot-shaper-indic.cc |
the per-script configuration, the features and stages, the probes, both reorderings, and the decompose and compose hooks |
src/hb-ot-shaper-indic.hh |
the positions, ot_position_t |
src/hb-ot-shaper-indic-machine.rl |
the categories and the syllable grammar, in Ragel |
src/hb-ot-shaper-indic-machine.hh |
the machine Ragel generated from it, committed |
src/gen-ragel-artifacts.py |
runs Ragel for meson and copies the result into src/ |
src/hb-ot-shaper-indic-table.cc |
the generated categories and positions, shared with Khmer and Myanmar |
src/gen-indic-table.py |
the generator, which reads IndicSyllabicCategory.txt, IndicPositionalCategory.txt, and Blocks.txt |
src/hb-ot-shaper-syllabic.cc |
inserting dotted circles into broken clusters, and releasing the syllable byte |
src/hb-ot-layout.hh |
foreach_syllable |
src/hb-limits.hh |
HB_MAX_SYLLABLE_LENGTH |
In short
- Each character gets a category and a position from a table generated from Unicode’s Indic syllabic and positional categories, with HarfBuzz’s overrides. Every consonant starts as a possible base.
- A Ragel scanner divides the run into consonant syllables, vowel syllables, standalone clusters, symbol clusters, broken clusters, and non-Indic clusters, and writes each syllable’s type and a serial number into the
syllablebyte. Syllables are handled at most 64 glyphs at a time. - The basic features run one stage each, after the initial reordering; the presentation features run together, after the final reordering. All are confined to the syllable.
- The font decides each consonant’s role: if its
blwforvatuwould substitute virama + consonant (in either order) the consonant is below-base, ifpstforprefwould, post-base. The base is the last consonant that is neither.rphfdecides the reph the same way. - The initial reordering sorts the syllable by position and sets the masks; the final reordering moves pre-base vowel signs after any virama left standing, moves the reph to its script’s place, and moves a
prefform before the base, each only if the basic features made what it needs. - The scripts differ in a table and a few
ifs: Telugu’s reph needs a ZWJ, Malayalam’s is a character, and Malayalam and Tamil put pre-base glyphs just before the base.