@cantoo/fontkit 2.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (163) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +274 -0
  3. package/dist/browser-module.mjs +13660 -0
  4. package/dist/browser-module.mjs.map +1 -0
  5. package/dist/browser.cjs +13689 -0
  6. package/dist/browser.cjs.map +1 -0
  7. package/dist/main.cjs +13712 -0
  8. package/dist/main.cjs.map +1 -0
  9. package/dist/module.mjs +13673 -0
  10. package/dist/module.mjs.map +1 -0
  11. package/package.json +99 -0
  12. package/src/CmapProcessor.js +285 -0
  13. package/src/DFont.js +113 -0
  14. package/src/TTFFont.js +621 -0
  15. package/src/TrueTypeCollection.js +65 -0
  16. package/src/WOFF2Font.js +222 -0
  17. package/src/WOFFFont.js +56 -0
  18. package/src/aat/AATFeatureMap.js +545 -0
  19. package/src/aat/AATLayoutEngine.js +48 -0
  20. package/src/aat/AATLookupTable.js +125 -0
  21. package/src/aat/AATMorxProcessor.js +429 -0
  22. package/src/aat/AATStateMachine.js +96 -0
  23. package/src/base.js +29 -0
  24. package/src/cff/CFFCharsets.js +99 -0
  25. package/src/cff/CFFDict.js +166 -0
  26. package/src/cff/CFFEncodings.js +48 -0
  27. package/src/cff/CFFFont.js +158 -0
  28. package/src/cff/CFFIndex.js +150 -0
  29. package/src/cff/CFFOperand.js +135 -0
  30. package/src/cff/CFFPointer.js +50 -0
  31. package/src/cff/CFFPrivateDict.js +39 -0
  32. package/src/cff/CFFStandardStrings.js +71 -0
  33. package/src/cff/CFFTop.js +236 -0
  34. package/src/decorators.js +36 -0
  35. package/src/encodings.js +219 -0
  36. package/src/fs.js +12 -0
  37. package/src/glyph/BBox.js +72 -0
  38. package/src/glyph/CBDTGlyph.js +9 -0
  39. package/src/glyph/CFFGlyph.js +635 -0
  40. package/src/glyph/COLRGlyph.js +108 -0
  41. package/src/glyph/Glyph.js +270 -0
  42. package/src/glyph/GlyphVariationProcessor.js +487 -0
  43. package/src/glyph/Path.js +257 -0
  44. package/src/glyph/SBIXGlyph.js +54 -0
  45. package/src/glyph/StandardNames.js +27 -0
  46. package/src/glyph/TTFGlyph.js +408 -0
  47. package/src/glyph/TTFGlyphEncoder.js +158 -0
  48. package/src/glyph/WOFF2Glyph.js +70 -0
  49. package/src/index.js +15 -0
  50. package/src/layout/GlyphPosition.js +30 -0
  51. package/src/layout/GlyphRun.js +108 -0
  52. package/src/layout/KernProcessor.js +97 -0
  53. package/src/layout/LayoutEngine.js +190 -0
  54. package/src/layout/Script.js +231 -0
  55. package/src/layout/UnicodeLayoutEngine.js +250 -0
  56. package/src/node.js +16 -0
  57. package/src/opentype/GPOSProcessor.js +311 -0
  58. package/src/opentype/GSUBProcessor.js +208 -0
  59. package/src/opentype/GlyphInfo.js +57 -0
  60. package/src/opentype/GlyphIterator.js +82 -0
  61. package/src/opentype/OTLayoutEngine.js +117 -0
  62. package/src/opentype/OTProcessor.js +472 -0
  63. package/src/opentype/ShapingPlan.js +121 -0
  64. package/src/opentype/shapers/ArabicShaper.js +124 -0
  65. package/src/opentype/shapers/DefaultShaper.js +116 -0
  66. package/src/opentype/shapers/HangulShaper.js +285 -0
  67. package/src/opentype/shapers/HebrewShaper.js +105 -0
  68. package/src/opentype/shapers/IndicShaper.js +914 -0
  69. package/src/opentype/shapers/ThaiShaper.js +238 -0
  70. package/src/opentype/shapers/UniversalShaper.js +186 -0
  71. package/src/opentype/shapers/data.trie +0 -0
  72. package/src/opentype/shapers/gen-indic.js +208 -0
  73. package/src/opentype/shapers/gen-use.js +287 -0
  74. package/src/opentype/shapers/generate-data.js +33 -0
  75. package/src/opentype/shapers/index.js +109 -0
  76. package/src/opentype/shapers/indic-data.js +175 -0
  77. package/src/opentype/shapers/indic.json +1 -0
  78. package/src/opentype/shapers/indic.machine +31 -0
  79. package/src/opentype/shapers/indic.trie +0 -0
  80. package/src/opentype/shapers/use.json +1 -0
  81. package/src/opentype/shapers/use.machine +44 -0
  82. package/src/opentype/shapers/use.trie +0 -0
  83. package/src/packages/unicode-properties/LICENSE +8 -0
  84. package/src/packages/unicode-properties/README.md +12 -0
  85. package/src/packages/unicode-properties/data-trie.js +1 -0
  86. package/src/packages/unicode-properties/data.js +1 -0
  87. package/src/packages/unicode-properties/data.json +1 -0
  88. package/src/packages/unicode-properties/data.trie +0 -0
  89. package/src/packages/unicode-properties/dist/index.cjs +257 -0
  90. package/src/packages/unicode-properties/dist/index.cjs.map +7 -0
  91. package/src/packages/unicode-properties/dist/index.mjs +234 -0
  92. package/src/packages/unicode-properties/dist/index.mjs.map +7 -0
  93. package/src/packages/unicode-properties/generate.js +107 -0
  94. package/src/packages/unicode-properties/index.js +184 -0
  95. package/src/packages/unicode-properties/package.json +50 -0
  96. package/src/packages/unicode-trie/LICENSE +8 -0
  97. package/src/packages/unicode-trie/README.md +13 -0
  98. package/src/packages/unicode-trie/builder.js +965 -0
  99. package/src/packages/unicode-trie/dist/builder.cjs +711 -0
  100. package/src/packages/unicode-trie/dist/builder.cjs.map +7 -0
  101. package/src/packages/unicode-trie/dist/builder.mjs +690 -0
  102. package/src/packages/unicode-trie/dist/builder.mjs.map +7 -0
  103. package/src/packages/unicode-trie/dist/index.cjs +112 -0
  104. package/src/packages/unicode-trie/dist/index.cjs.map +7 -0
  105. package/src/packages/unicode-trie/dist/index.mjs +91 -0
  106. package/src/packages/unicode-trie/dist/index.mjs.map +7 -0
  107. package/src/packages/unicode-trie/index.js +135 -0
  108. package/src/packages/unicode-trie/package.json +49 -0
  109. package/src/packages/unicode-trie/swap.js +21 -0
  110. package/src/subset/CFFSubset.js +176 -0
  111. package/src/subset/Subset.js +63 -0
  112. package/src/subset/TTFSubset.js +130 -0
  113. package/src/tables/BASE.js +78 -0
  114. package/src/tables/COLR.js +25 -0
  115. package/src/tables/CPAL.js +24 -0
  116. package/src/tables/DSIG.js +21 -0
  117. package/src/tables/EBDT.js +91 -0
  118. package/src/tables/EBLC.js +80 -0
  119. package/src/tables/GDEF.js +57 -0
  120. package/src/tables/GPOS.js +209 -0
  121. package/src/tables/GSUB.js +85 -0
  122. package/src/tables/HVAR.js +44 -0
  123. package/src/tables/JSTF.js +43 -0
  124. package/src/tables/LTSH.js +10 -0
  125. package/src/tables/OS2.js +84 -0
  126. package/src/tables/PCLT.js +21 -0
  127. package/src/tables/STAT.js +99 -0
  128. package/src/tables/VDMX.js +33 -0
  129. package/src/tables/VORG.js +14 -0
  130. package/src/tables/WOFF2Directory.js +73 -0
  131. package/src/tables/WOFFDirectory.js +37 -0
  132. package/src/tables/aat.js +157 -0
  133. package/src/tables/avar.js +19 -0
  134. package/src/tables/bsln.js +31 -0
  135. package/src/tables/cmap.js +127 -0
  136. package/src/tables/cvt.js +6 -0
  137. package/src/tables/directory.js +58 -0
  138. package/src/tables/feat.js +28 -0
  139. package/src/tables/fpgm.js +8 -0
  140. package/src/tables/fvar.js +31 -0
  141. package/src/tables/gasp.js +15 -0
  142. package/src/tables/glyf.js +4 -0
  143. package/src/tables/gvar.js +27 -0
  144. package/src/tables/hdmx.js +15 -0
  145. package/src/tables/head.js +25 -0
  146. package/src/tables/hhea.js +19 -0
  147. package/src/tables/hmtx.js +11 -0
  148. package/src/tables/index.js +119 -0
  149. package/src/tables/just.js +81 -0
  150. package/src/tables/kern.js +91 -0
  151. package/src/tables/loca.js +30 -0
  152. package/src/tables/maxp.js +20 -0
  153. package/src/tables/morx.js +79 -0
  154. package/src/tables/name.js +157 -0
  155. package/src/tables/opbd.js +15 -0
  156. package/src/tables/opentype.js +217 -0
  157. package/src/tables/post.js +34 -0
  158. package/src/tables/prep.js +6 -0
  159. package/src/tables/sbix.js +17 -0
  160. package/src/tables/variations.js +80 -0
  161. package/src/tables/vhea.js +19 -0
  162. package/src/tables/vmtx.js +12 -0
  163. package/src/utils.js +62 -0
@@ -0,0 +1,914 @@
1
+ import DefaultShaper from './DefaultShaper';
2
+ import StateMachine from 'dfa';
3
+ import UnicodeTrie from '../../packages/unicode-trie/index.js';
4
+ import {getCategory} from '../../packages/unicode-properties/index.js';
5
+ import * as Script from '../../layout/Script';
6
+ import GlyphInfo from '../GlyphInfo';
7
+ import indicMachine from './indic.json';
8
+ import useData from './use.json';
9
+ import {
10
+ CATEGORIES,
11
+ POSITIONS,
12
+ CONSONANT_FLAGS,
13
+ JOINER_FLAGS,
14
+ HALANT_OR_COENG_FLAGS, INDIC_CONFIGS,
15
+ INDIC_DECOMPOSITIONS
16
+ } from './indic-data';
17
+ import { decodeBase64 } from '../../utils';
18
+ import indicTrie from './indic.trie';
19
+
20
+ const {decompositions} = useData;
21
+ const trie = new UnicodeTrie(decodeBase64(indicTrie));
22
+ const stateMachine = new StateMachine(indicMachine);
23
+
24
+ /**
25
+ * The IndicShaper supports indic scripts e.g. Devanagari, Kannada, etc.
26
+ * Based on code from Harfbuzz: https://github.com/behdad/harfbuzz/blob/master/src/hb-ot-shape-complex-indic.cc
27
+ */
28
+ export default class IndicShaper extends DefaultShaper {
29
+ static zeroMarkWidths = 'NONE';
30
+ static planFeatures(plan) {
31
+ plan.addStage(setupSyllables);
32
+
33
+ plan.addStage(['locl', 'ccmp']);
34
+
35
+ plan.addStage(initialReordering);
36
+
37
+ plan.addStage('nukt');
38
+ plan.addStage('akhn');
39
+ plan.addStage('rphf', false);
40
+ plan.addStage('rkrf');
41
+ plan.addStage('pref', false);
42
+ plan.addStage('blwf', false);
43
+ plan.addStage('abvf', false);
44
+ plan.addStage('half', false);
45
+ plan.addStage('pstf', false);
46
+ plan.addStage('vatu');
47
+ plan.addStage('cjct');
48
+ plan.addStage('cfar', false);
49
+
50
+ plan.addStage(finalReordering);
51
+
52
+ plan.addStage({
53
+ local: ['init'],
54
+ global: ['pres', 'abvs', 'blws', 'psts', 'haln', 'dist', 'abvm', 'blwm', 'calt', 'clig']
55
+ });
56
+
57
+ // Setup the indic config for the selected script
58
+ plan.unicodeScript = Script.fromOpenType(plan.script);
59
+ plan.indicConfig = INDIC_CONFIGS[plan.unicodeScript] || INDIC_CONFIGS.Default;
60
+ plan.isOldSpec = plan.indicConfig.hasOldSpec && plan.script[plan.script.length - 1] !== '2';
61
+
62
+ // TODO: turn off kern (Khmer) and liga features.
63
+ }
64
+
65
+ static assignFeatures(plan, glyphs) {
66
+ // Decompose split matras
67
+ // TODO: do this in a more general unicode normalizer
68
+ for (let i = glyphs.length - 1; i >= 0; i--) {
69
+ let codepoint = glyphs[i].codePoints[0];
70
+ let d = INDIC_DECOMPOSITIONS[codepoint] || decompositions[codepoint];
71
+ if (d) {
72
+ let decomposed = d.map(c => {
73
+ let g = plan.font.glyphForCodePoint(c);
74
+ return new GlyphInfo(plan.font, g.id, [c], glyphs[i].features);
75
+ });
76
+
77
+ glyphs.splice(i, 1, ...decomposed);
78
+ }
79
+ }
80
+ }
81
+ }
82
+
83
+ function indicCategory(glyph) {
84
+ return trie.get(glyph.codePoints[0]) >> 8;
85
+ }
86
+
87
+ function indicPosition(glyph) {
88
+ return 1 << (trie.get(glyph.codePoints[0]) & 0xff);
89
+ }
90
+
91
+ class IndicInfo {
92
+ constructor(category, position, syllableType, syllable) {
93
+ this.category = category;
94
+ this.position = position;
95
+ this.syllableType = syllableType;
96
+ this.syllable = syllable;
97
+ }
98
+ }
99
+
100
+ function setupSyllables(font, glyphs) {
101
+ let syllable = 0;
102
+ let last = 0;
103
+ for (let [start, end, tags] of stateMachine.match(glyphs.map(indicCategory))) {
104
+ if (start > last) {
105
+ ++syllable;
106
+ for (let i = last; i < start; i++) {
107
+ glyphs[i].shaperInfo = new IndicInfo(CATEGORIES.X, POSITIONS.End, 'non_indic_cluster', syllable);
108
+ }
109
+ }
110
+
111
+ ++syllable;
112
+
113
+ // Create shaper info
114
+ for (let i = start; i <= end; i++) {
115
+ glyphs[i].shaperInfo = new IndicInfo(
116
+ 1 << indicCategory(glyphs[i]),
117
+ indicPosition(glyphs[i]),
118
+ tags[0],
119
+ syllable
120
+ );
121
+ }
122
+
123
+ last = end + 1;
124
+ }
125
+
126
+ if (last < glyphs.length) {
127
+ ++syllable;
128
+ for (let i = last; i < glyphs.length; i++) {
129
+ glyphs[i].shaperInfo = new IndicInfo(CATEGORIES.X, POSITIONS.End, 'non_indic_cluster', syllable);
130
+ }
131
+ }
132
+ }
133
+
134
+ function isConsonant(glyph) {
135
+ return glyph.shaperInfo.category & CONSONANT_FLAGS;
136
+ }
137
+
138
+ function isJoiner(glyph) {
139
+ return glyph.shaperInfo.category & JOINER_FLAGS;
140
+ }
141
+
142
+ function isHalantOrCoeng(glyph) {
143
+ return glyph.shaperInfo.category & HALANT_OR_COENG_FLAGS;
144
+ }
145
+
146
+ function wouldSubstitute(glyphs, feature) {
147
+ for (let glyph of glyphs) {
148
+ glyph.features = {[feature]: true};
149
+ }
150
+
151
+ let GSUB = glyphs[0]._font._layoutEngine.engine.GSUBProcessor;
152
+ GSUB.applyFeatures([feature], glyphs);
153
+
154
+ return glyphs.length === 1;
155
+ }
156
+
157
+ function consonantPosition(font, consonant, virama) {
158
+ let glyphs = [virama, consonant, virama];
159
+ if (wouldSubstitute(glyphs.slice(0, 2), 'blwf') || wouldSubstitute(glyphs.slice(1, 3), 'blwf')) {
160
+ return POSITIONS.Below_C;
161
+ } else if (wouldSubstitute(glyphs.slice(0, 2), 'pstf') || wouldSubstitute(glyphs.slice(1, 3), 'pstf')) {
162
+ return POSITIONS.Post_C;
163
+ } else if (wouldSubstitute(glyphs.slice(0, 2), 'pref') || wouldSubstitute(glyphs.slice(1, 3), 'pref')) {
164
+ return POSITIONS.Post_C;
165
+ }
166
+
167
+ return POSITIONS.Base_C;
168
+ }
169
+
170
+ function initialReordering(font, glyphs, plan) {
171
+ let indicConfig = plan.indicConfig;
172
+ let features = font._layoutEngine.engine.GSUBProcessor.features;
173
+
174
+ let dottedCircle = font.glyphForCodePoint(0x25cc).id;
175
+ let virama = font.glyphForCodePoint(indicConfig.virama).id;
176
+ if (virama) {
177
+ let info = new GlyphInfo(font, virama, [indicConfig.virama]);
178
+ for (let i = 0; i < glyphs.length; i++) {
179
+ if (glyphs[i].shaperInfo.position === POSITIONS.Base_C) {
180
+ glyphs[i].shaperInfo.position = consonantPosition(font, glyphs[i].copy(), info);
181
+ }
182
+ }
183
+ }
184
+
185
+ for (let start = 0, end = nextSyllable(glyphs, 0); start < glyphs.length; start = end, end = nextSyllable(glyphs, start)) {
186
+ let {syllableType} = glyphs[start].shaperInfo;
187
+
188
+ if (syllableType === 'symbol_cluster' || syllableType === 'non_indic_cluster') {
189
+ continue;
190
+ }
191
+
192
+ if (syllableType === 'broken_cluster' && dottedCircle) {
193
+ let g = new GlyphInfo(font, dottedCircle, [0x25cc]);
194
+ g.shaperInfo = new IndicInfo(
195
+ 1 << indicCategory(g),
196
+ indicPosition(g),
197
+ glyphs[start].shaperInfo.syllableType,
198
+ glyphs[start].shaperInfo.syllable
199
+ );
200
+
201
+ // Insert after possible Repha.
202
+ let i = start;
203
+ while (i < end && glyphs[i].shaperInfo.category === CATEGORIES.Repha) {
204
+ i++;
205
+ }
206
+
207
+ glyphs.splice(i, 0, g);
208
+ end++;
209
+ }
210
+
211
+ // 1. Find base consonant:
212
+ //
213
+ // The shaping engine finds the base consonant of the syllable, using the
214
+ // following algorithm: starting from the end of the syllable, move backwards
215
+ // until a consonant is found that does not have a below-base or post-base
216
+ // form (post-base forms have to follow below-base forms), or that is not a
217
+ // pre-base reordering Ra, or arrive at the first consonant. The consonant
218
+ // stopped at will be the base.
219
+
220
+ let base = end;
221
+ let limit = start;
222
+ let hasReph = false;
223
+
224
+ // If the syllable starts with Ra + Halant (in a script that has Reph)
225
+ // and has more than one consonant, Ra is excluded from candidates for
226
+ // base consonants.
227
+ if (indicConfig.rephPos !== POSITIONS.Ra_To_Become_Reph &&
228
+ features.rphf &&
229
+ start + 3 <= end && (
230
+ (indicConfig.rephMode === 'Implicit' && !isJoiner(glyphs[start + 2])) ||
231
+ (indicConfig.rephMode === 'Explicit' && glyphs[start + 2].shaperInfo.category === CATEGORIES.ZWJ)
232
+ )
233
+ ) {
234
+ // See if it matches the 'rphf' feature.
235
+ let g = [glyphs[start].copy(), glyphs[start + 1].copy(), glyphs[start + 2].copy()];
236
+ if (wouldSubstitute(g.slice(0, 2), 'rphf') || (indicConfig.rephMode === 'Explicit' && wouldSubstitute(g, 'rphf'))) {
237
+ limit += 2;
238
+ while (limit < end && isJoiner(glyphs[limit])) {
239
+ limit++;
240
+ }
241
+ base = start;
242
+ hasReph = true;
243
+ }
244
+ } else if (indicConfig.rephMode === 'Log_Repha' && glyphs[start].shaperInfo.category === CATEGORIES.Repha) {
245
+ limit++;
246
+ while (limit < end && isJoiner(glyphs[limit])) {
247
+ limit++;
248
+ }
249
+ base = start;
250
+ hasReph = true;
251
+ }
252
+
253
+ switch (indicConfig.basePos) {
254
+ case 'Last': {
255
+ // starting from the end of the syllable, move backwards
256
+ let i = end;
257
+ let seenBelow = false;
258
+
259
+ do {
260
+ let info = glyphs[--i].shaperInfo;
261
+
262
+ // until a consonant is found
263
+ if (isConsonant(glyphs[i])) {
264
+ // that does not have a below-base or post-base form
265
+ // (post-base forms have to follow below-base forms),
266
+ if (info.position !== POSITIONS.Below_C && (info.position !== POSITIONS.Post_C || seenBelow)) {
267
+ base = i;
268
+ break;
269
+ }
270
+
271
+ // or that is not a pre-base reordering Ra,
272
+ //
273
+ // IMPLEMENTATION NOTES:
274
+ //
275
+ // Our pre-base reordering Ra's are marked POS_POST_C, so will be skipped
276
+ // by the logic above already.
277
+ //
278
+
279
+ // or arrive at the first consonant. The consonant stopped at will
280
+ // be the base.
281
+ if (info.position === POSITIONS.Below_C) {
282
+ seenBelow = true;
283
+ }
284
+
285
+ base = i;
286
+ } else if (start < i && info.category === CATEGORIES.ZWJ && glyphs[i - 1].shaperInfo.category === CATEGORIES.H) {
287
+ // A ZWJ after a Halant stops the base search, and requests an explicit
288
+ // half form.
289
+ // A ZWJ before a Halant, requests a subjoined form instead, and hence
290
+ // search continues. This is particularly important for Bengali
291
+ // sequence Ra,H,Ya that should form Ya-Phalaa by subjoining Ya.
292
+ break;
293
+ }
294
+ } while (i > limit);
295
+ break;
296
+ }
297
+
298
+ case 'First': {
299
+ // The first consonant is always the base.
300
+ base = start;
301
+
302
+ // Mark all subsequent consonants as below.
303
+ for (let i = base + 1; i < end; i++) {
304
+ if (isConsonant(glyphs[i])) {
305
+ glyphs[i].shaperInfo.position = POSITIONS.Below_C;
306
+ }
307
+ }
308
+ }
309
+ }
310
+
311
+ // If the syllable starts with Ra + Halant (in a script that has Reph)
312
+ // and has more than one consonant, Ra is excluded from candidates for
313
+ // base consonants.
314
+ //
315
+ // Only do this for unforced Reph. (ie. not for Ra,H,ZWJ)
316
+ if (hasReph && base === start && limit - base <= 2) {
317
+ hasReph = false;
318
+ }
319
+
320
+ // 2. Decompose and reorder Matras:
321
+ //
322
+ // Each matra and any syllable modifier sign in the cluster are moved to the
323
+ // appropriate position relative to the consonant(s) in the cluster. The
324
+ // shaping engine decomposes two- or three-part matras into their constituent
325
+ // parts before any repositioning. Matra characters are classified by which
326
+ // consonant in a conjunct they have affinity for and are reordered to the
327
+ // following positions:
328
+ //
329
+ // o Before first half form in the syllable
330
+ // o After subjoined consonants
331
+ // o After post-form consonant
332
+ // o After main consonant (for above marks)
333
+ //
334
+ // IMPLEMENTATION NOTES:
335
+ //
336
+ // The normalize() routine has already decomposed matras for us, so we don't
337
+ // need to worry about that.
338
+
339
+ // 3. Reorder marks to canonical order:
340
+ //
341
+ // Adjacent nukta and halant or nukta and vedic sign are always repositioned
342
+ // if necessary, so that the nukta is first.
343
+ //
344
+ // IMPLEMENTATION NOTES:
345
+ //
346
+ // We don't need to do this: the normalize() routine already did this for us.
347
+
348
+ // Reorder characters
349
+
350
+ for (let i = start; i < base; i++) {
351
+ let info = glyphs[i].shaperInfo;
352
+ info.position = Math.min(POSITIONS.Pre_C, info.position);
353
+ }
354
+
355
+ if (base < end) {
356
+ glyphs[base].shaperInfo.position = POSITIONS.Base_C;
357
+ }
358
+
359
+ // Mark final consonants. A final consonant is one appearing after a matra,
360
+ // like in Khmer.
361
+ for (let i = base + 1; i < end; i++) {
362
+ if (glyphs[i].shaperInfo.category === CATEGORIES.M) {
363
+ for (let j = i + 1; j < end; j++) {
364
+ if (isConsonant(glyphs[j])) {
365
+ glyphs[j].shaperInfo.position = POSITIONS.Final_C;
366
+ break;
367
+ }
368
+ }
369
+ break;
370
+ }
371
+ }
372
+
373
+ // Handle beginning Ra
374
+ if (hasReph) {
375
+ glyphs[start].shaperInfo.position = POSITIONS.Ra_To_Become_Reph;
376
+ }
377
+
378
+ // For old-style Indic script tags, move the first post-base Halant after
379
+ // last consonant.
380
+ //
381
+ // Reports suggest that in some scripts Uniscribe does this only if there
382
+ // is *not* a Halant after last consonant already (eg. Kannada), while it
383
+ // does it unconditionally in other scripts (eg. Malayalam). We don't
384
+ // currently know about other scripts, so we single out Malayalam for now.
385
+ //
386
+ // Kannada test case:
387
+ // U+0C9A,U+0CCD,U+0C9A,U+0CCD
388
+ // With some versions of Lohit Kannada.
389
+ // https://bugs.freedesktop.org/show_bug.cgi?id=59118
390
+ //
391
+ // Malayalam test case:
392
+ // U+0D38,U+0D4D,U+0D31,U+0D4D,U+0D31,U+0D4D
393
+ // With lohit-ttf-20121122/Lohit-Malayalam.ttf
394
+ if (plan.isOldSpec) {
395
+ let disallowDoubleHalants = plan.unicodeScript !== 'Malayalam';
396
+ for (let i = base + 1; i < end; i++) {
397
+ if (glyphs[i].shaperInfo.category === CATEGORIES.H) {
398
+ let j;
399
+ for (j = end - 1; j > i; j--) {
400
+ if (isConsonant(glyphs[j]) || (disallowDoubleHalants && glyphs[j].shaperInfo.category === CATEGORIES.H)) {
401
+ break;
402
+ }
403
+ }
404
+
405
+ if (glyphs[j].shaperInfo.category !== CATEGORIES.H && j > i) {
406
+ // Move Halant to after last consonant.
407
+ let t = glyphs[i];
408
+ glyphs.splice(i, 0, ...glyphs.splice(i + 1, j - i));
409
+ glyphs[j] = t;
410
+ }
411
+
412
+ break;
413
+ }
414
+ }
415
+ }
416
+
417
+ // Attach misc marks to previous char to move with them.
418
+ let lastPos = POSITIONS.Start;
419
+ for (let i = start; i < end; i++) {
420
+ let info = glyphs[i].shaperInfo;
421
+ if (info.category & (JOINER_FLAGS | CATEGORIES.N | CATEGORIES.RS | CATEGORIES.CM | HALANT_OR_COENG_FLAGS & info.category)) {
422
+ info.position = lastPos;
423
+ if (info.category === CATEGORIES.H && info.position === POSITIONS.Pre_M) {
424
+ // Uniscribe doesn't move the Halant with Left Matra.
425
+ // TEST: U+092B,U+093F,U+094DE
426
+ // We follow. This is important for the Sinhala
427
+ // U+0DDA split matra since it decomposes to U+0DD9,U+0DCA
428
+ // where U+0DD9 is a left matra and U+0DCA is the virama.
429
+ // We don't want to move the virama with the left matra.
430
+ // TEST: U+0D9A,U+0DDA
431
+ for (let j = i; j > start; j--) {
432
+ if (glyphs[j - 1].shaperInfo.position !== POSITIONS.Pre_M) {
433
+ info.position = glyphs[j - 1].shaperInfo.position;
434
+ break;
435
+ }
436
+ }
437
+ }
438
+ } else if (info.position !== POSITIONS.SMVD) {
439
+ lastPos = info.position;
440
+ }
441
+ }
442
+
443
+ // For post-base consonants let them own anything before them
444
+ // since the last consonant or matra.
445
+ let last = base;
446
+ for (let i = base + 1; i < end; i++) {
447
+ if (isConsonant(glyphs[i])) {
448
+ for (let j = last + 1; j < i; j++) {
449
+ if (glyphs[j].shaperInfo.position < POSITIONS.SMVD) {
450
+ glyphs[j].shaperInfo.position = glyphs[i].shaperInfo.position;
451
+ }
452
+ }
453
+ last = i;
454
+ } else if (glyphs[i].shaperInfo.category === CATEGORIES.M) {
455
+ last = i;
456
+ }
457
+ }
458
+
459
+ let arr = glyphs.slice(start, end);
460
+ arr.sort((a, b) => a.shaperInfo.position - b.shaperInfo.position);
461
+ glyphs.splice(start, arr.length, ...arr);
462
+
463
+ // Find base again
464
+ for (let i = start; i < end; i++) {
465
+ if (glyphs[i].shaperInfo.position === POSITIONS.Base_C) {
466
+ base = i;
467
+ break;
468
+ }
469
+ }
470
+
471
+ // Setup features now
472
+
473
+ // Reph
474
+ for (let i = start; i < end && glyphs[i].shaperInfo.position === POSITIONS.Ra_To_Become_Reph; i++) {
475
+ glyphs[i].features.rphf = true;
476
+ }
477
+
478
+ // Pre-base
479
+ let blwf = !plan.isOldSpec && indicConfig.blwfMode === 'Pre_And_Post';
480
+ for (let i = start; i < base; i++) {
481
+ glyphs[i].features.half = true;
482
+ if (blwf) {
483
+ glyphs[i].features.blwf = true;
484
+ }
485
+ }
486
+
487
+ // Post-base
488
+ for (let i = base + 1; i < end; i++) {
489
+ glyphs[i].features.abvf = true;
490
+ glyphs[i].features.pstf = true;
491
+ glyphs[i].features.blwf = true;
492
+ }
493
+
494
+ if (plan.isOldSpec && plan.unicodeScript === 'Devanagari') {
495
+ // Old-spec eye-lash Ra needs special handling. From the
496
+ // spec:
497
+ //
498
+ // "The feature 'below-base form' is applied to consonants
499
+ // having below-base forms and following the base consonant.
500
+ // The exception is vattu, which may appear below half forms
501
+ // as well as below the base glyph. The feature 'below-base
502
+ // form' will be applied to all such occurrences of Ra as well."
503
+ //
504
+ // Test case: U+0924,U+094D,U+0930,U+094d,U+0915
505
+ // with Sanskrit 2003 font.
506
+ //
507
+ // However, note that Ra,Halant,ZWJ is the correct way to
508
+ // request eyelash form of Ra, so we wouldbn't inhibit it
509
+ // in that sequence.
510
+ //
511
+ // Test case: U+0924,U+094D,U+0930,U+094d,U+200D,U+0915
512
+ for (let i = start; i + 1 < base; i++) {
513
+ if (glyphs[i].shaperInfo.category === CATEGORIES.Ra &&
514
+ glyphs[i + 1].shaperInfo.category === CATEGORIES.H &&
515
+ (i + 1 === base || glyphs[i + 2].shaperInfo.category === CATEGORIES.ZWJ)
516
+ ) {
517
+ glyphs[i].features.blwf = true;
518
+ glyphs[i + 1].features.blwf = true;
519
+ }
520
+ }
521
+ }
522
+
523
+ let prefLen = 2;
524
+ if (features.pref && base + prefLen < end) {
525
+ // Find a Halant,Ra sequence and mark it for pre-base reordering processing.
526
+ for (let i = base + 1; i + prefLen - 1 < end; i++) {
527
+ let g = [glyphs[i].copy(), glyphs[i + 1].copy()];
528
+ if (wouldSubstitute(g, 'pref')) {
529
+ for (let j = 0; j < prefLen; j++) {
530
+ glyphs[i++].features.pref = true;
531
+ }
532
+
533
+ // Mark the subsequent stuff with 'cfar'. Used in Khmer.
534
+ // Read the feature spec.
535
+ // This allows distinguishing the following cases with MS Khmer fonts:
536
+ // U+1784,U+17D2,U+179A,U+17D2,U+1782
537
+ // U+1784,U+17D2,U+1782,U+17D2,U+179A
538
+ if (features.cfar) {
539
+ for (; i < end; i++) {
540
+ glyphs[i].features.cfar = true;
541
+ }
542
+ }
543
+
544
+ break;
545
+ }
546
+ }
547
+ }
548
+
549
+ // Apply ZWJ/ZWNJ effects
550
+ for (let i = start + 1; i < end; i++) {
551
+ if (isJoiner(glyphs[i])) {
552
+ let nonJoiner = glyphs[i].shaperInfo.category === CATEGORIES.ZWNJ;
553
+ let j = i;
554
+
555
+ do {
556
+ j--;
557
+
558
+ // ZWJ/ZWNJ should disable CJCT. They do that by simply
559
+ // being there, since we don't skip them for the CJCT
560
+ // feature (ie. F_MANUAL_ZWJ)
561
+
562
+ // A ZWNJ disables HALF.
563
+ if (nonJoiner) {
564
+ delete glyphs[j].features.half;
565
+ }
566
+ } while (j > start && !isConsonant(glyphs[j]));
567
+ }
568
+ }
569
+ }
570
+ }
571
+
572
+ function finalReordering(font, glyphs, plan) {
573
+ let indicConfig = plan.indicConfig;
574
+ let features = font._layoutEngine.engine.GSUBProcessor.features;
575
+
576
+ for (let start = 0, end = nextSyllable(glyphs, 0); start < glyphs.length; start = end, end = nextSyllable(glyphs, start)) {
577
+ // 4. Final reordering:
578
+ //
579
+ // After the localized forms and basic shaping forms GSUB features have been
580
+ // applied (see below), the shaping engine performs some final glyph
581
+ // reordering before applying all the remaining font features to the entire
582
+ // cluster.
583
+
584
+ let tryPref = !!features.pref;
585
+
586
+ // Find base again
587
+ let base = start;
588
+ for (; base < end; base++) {
589
+ if (glyphs[base].shaperInfo.position >= POSITIONS.Base_C) {
590
+ if (tryPref && base + 1 < end) {
591
+ for (let i = base + 1; i < end; i++) {
592
+ if (glyphs[i].features.pref) {
593
+ if (!(glyphs[i].substituted && glyphs[i].isLigated && !glyphs[i].isMultiplied)) {
594
+ // Ok, this was a 'pref' candidate but didn't form any.
595
+ // Base is around here...
596
+ base = i;
597
+ while (base < end && isHalantOrCoeng(glyphs[base])) {
598
+ base++;
599
+ }
600
+ glyphs[base].shaperInfo.position = POSITIONS.BASE_C;
601
+ tryPref = false;
602
+ }
603
+ break;
604
+ }
605
+ }
606
+ }
607
+
608
+ // For Malayalam, skip over unformed below- (but NOT post-) forms.
609
+ if (plan.unicodeScript === 'Malayalam') {
610
+ for (let i = base + 1; i < end; i++) {
611
+ while (i < end && isJoiner(glyphs[i])) {
612
+ i++;
613
+ }
614
+
615
+ if (i === end || !isHalantOrCoeng(glyphs[i])) {
616
+ break;
617
+ }
618
+
619
+ i++; // Skip halant.
620
+ while (i < end && isJoiner(glyphs[i])) {
621
+ i++;
622
+ }
623
+
624
+ if (i < end && isConsonant(glyphs[i]) && glyphs[i].shaperInfo.position === POSITIONS.Below_C) {
625
+ base = i;
626
+ glyphs[base].shaperInfo.position = POSITIONS.Base_C;
627
+ }
628
+ }
629
+ }
630
+
631
+ if (start < base && glyphs[base].shaperInfo.position > POSITIONS.Base_C) {
632
+ base--;
633
+ }
634
+ break;
635
+ }
636
+ }
637
+
638
+ if (base === end && start < base && glyphs[base - 1].shaperInfo.category === CATEGORIES.ZWJ) {
639
+ base--;
640
+ }
641
+
642
+ if (base < end) {
643
+ while (start < base && glyphs[base].shaperInfo.category & (CATEGORIES.N | HALANT_OR_COENG_FLAGS)) {
644
+ base--;
645
+ }
646
+ }
647
+
648
+ // o Reorder matras:
649
+ //
650
+ // If a pre-base matra character had been reordered before applying basic
651
+ // features, the glyph can be moved closer to the main consonant based on
652
+ // whether half-forms had been formed. Actual position for the matra is
653
+ // defined as “after last standalone halant glyph, after initial matra
654
+ // position and before the main consonant”. If ZWJ or ZWNJ follow this
655
+ // halant, position is moved after it.
656
+ //
657
+
658
+ if (start + 1 < end && start < base) { // Otherwise there can't be any pre-base matra characters.
659
+ // If we lost track of base, alas, position before last thingy.
660
+ let newPos = base === end ? base - 2 : base - 1;
661
+
662
+ // Malayalam / Tamil do not have "half" forms or explicit virama forms.
663
+ // The glyphs formed by 'half' are Chillus or ligated explicit viramas.
664
+ // We want to position matra after them.
665
+ if (plan.unicodeScript !== 'Malayalam' && plan.unicodeScript !== 'Tamil') {
666
+ while (newPos > start && !(glyphs[newPos].shaperInfo.category & (CATEGORIES.M | HALANT_OR_COENG_FLAGS))) {
667
+ newPos--;
668
+ }
669
+
670
+ // If we found no Halant we are done.
671
+ // Otherwise only proceed if the Halant does
672
+ // not belong to the Matra itself!
673
+ if (isHalantOrCoeng(glyphs[newPos]) && glyphs[newPos].shaperInfo.position !== POSITIONS.Pre_M) {
674
+ // If ZWJ or ZWNJ follow this halant, position is moved after it.
675
+ if (newPos + 1 < end && isJoiner(glyphs[newPos + 1])) {
676
+ newPos++;
677
+ }
678
+ } else {
679
+ newPos = start; // No move.
680
+ }
681
+ }
682
+
683
+ if (start < newPos && glyphs[newPos].shaperInfo.position !== POSITIONS.Pre_M) {
684
+ // Now go see if there's actually any matras...
685
+ for (let i = newPos; i > start; i--) {
686
+ if (glyphs[i - 1].shaperInfo.position === POSITIONS.Pre_M) {
687
+ let oldPos = i - 1;
688
+ if (oldPos < base && base <= newPos) { // Shouldn't actually happen.
689
+ base--;
690
+ }
691
+
692
+ let tmp = glyphs[oldPos];
693
+ glyphs.splice(oldPos, 0, ...glyphs.splice(oldPos + 1, newPos - oldPos));
694
+ glyphs[newPos] = tmp;
695
+
696
+ newPos--;
697
+ }
698
+ }
699
+ }
700
+ }
701
+
702
+ // o Reorder reph:
703
+ //
704
+ // Reph’s original position is always at the beginning of the syllable,
705
+ // (i.e. it is not reordered at the character reordering stage). However,
706
+ // it will be reordered according to the basic-forms shaping results.
707
+ // Possible positions for reph, depending on the script, are; after main,
708
+ // before post-base consonant forms, and after post-base consonant forms.
709
+
710
+ // Two cases:
711
+ //
712
+ // - If repha is encoded as a sequence of characters (Ra,H or Ra,H,ZWJ), then
713
+ // we should only move it if the sequence ligated to the repha form.
714
+ //
715
+ // - If repha is encoded separately and in the logical position, we should only
716
+ // move it if it did NOT ligate. If it ligated, it's probably the font trying
717
+ // to make it work without the reordering.
718
+ if (start + 1 < end &&
719
+ glyphs[start].shaperInfo.position === POSITIONS.Ra_To_Become_Reph &&
720
+ (glyphs[start].shaperInfo.category === CATEGORIES.Repha) !== (glyphs[start].isLigated && !glyphs[start].isMultiplied)
721
+ ) {
722
+ let newRephPos;
723
+ let rephPos = indicConfig.rephPos;
724
+ let found = false;
725
+
726
+ // 1. If reph should be positioned after post-base consonant forms,
727
+ // proceed to step 5.
728
+ if (rephPos !== POSITIONS.After_Post) {
729
+ // 2. If the reph repositioning class is not after post-base: target
730
+ // position is after the first explicit halant glyph between the
731
+ // first post-reph consonant and last main consonant. If ZWJ or ZWNJ
732
+ // are following this halant, position is moved after it. If such
733
+ // position is found, this is the target position. Otherwise,
734
+ // proceed to the next step.
735
+ //
736
+ // Note: in old-implementation fonts, where classifications were
737
+ // fixed in shaping engine, there was no case where reph position
738
+ // will be found on this step.
739
+ newRephPos = start + 1;
740
+ while (newRephPos < base && !isHalantOrCoeng(glyphs[newRephPos])) {
741
+ newRephPos++;
742
+ }
743
+
744
+ if (newRephPos < base && isHalantOrCoeng(glyphs[newRephPos])) {
745
+ // ->If ZWJ or ZWNJ are following this halant, position is moved after it.
746
+ if (newRephPos + 1 < base && isJoiner(glyphs[newRephPos + 1])) {
747
+ newRephPos++;
748
+ }
749
+
750
+ found = true;
751
+ }
752
+
753
+ // 3. If reph should be repositioned after the main consonant: find the
754
+ // first consonant not ligated with main, or find the first
755
+ // consonant that is not a potential pre-base reordering Ra.
756
+ if (!found && rephPos === POSITIONS.After_Main) {
757
+ newRephPos = base;
758
+ while (newRephPos + 1 < end && glyphs[newRephPos + 1].shaperInfo.position <= POSITIONS.After_Main) {
759
+ newRephPos++;
760
+ }
761
+
762
+ found = newRephPos < end;
763
+ }
764
+
765
+ // 4. If reph should be positioned before post-base consonant, find
766
+ // first post-base classified consonant not ligated with main. If no
767
+ // consonant is found, the target position should be before the
768
+ // first matra, syllable modifier sign or vedic sign.
769
+ //
770
+ // This is our take on what step 4 is trying to say (and failing, BADLY).
771
+ if (!found && rephPos === POSITIONS.After_Sub) {
772
+ newRephPos = base;
773
+ while (newRephPos + 1 < end && !(glyphs[newRephPos + 1].shaperInfo.position & (POSITIONS.Post_C | POSITIONS.After_Post | POSITIONS.SMVD))) {
774
+ newRephPos++;
775
+ }
776
+
777
+ found = newRephPos < end;
778
+ }
779
+ }
780
+
781
+ // 5. If no consonant is found in steps 3 or 4, move reph to a position
782
+ // immediately before the first post-base matra, syllable modifier
783
+ // sign or vedic sign that has a reordering class after the intended
784
+ // reph position. For example, if the reordering position for reph
785
+ // is post-main, it will skip above-base matras that also have a
786
+ // post-main position.
787
+ if (!found) {
788
+ // Copied from step 2.
789
+ newRephPos = start + 1;
790
+ while (newRephPos < base && !isHalantOrCoeng(glyphs[newRephPos])) {
791
+ newRephPos++;
792
+ }
793
+
794
+ if (newRephPos < base && isHalantOrCoeng(glyphs[newRephPos])) {
795
+ // ->If ZWJ or ZWNJ are following this halant, position is moved after it.
796
+ if (newRephPos + 1 < base && isJoiner(glyphs[newRephPos + 1])) {
797
+ newRephPos++;
798
+ }
799
+
800
+ found = true;
801
+ }
802
+ }
803
+
804
+ // 6. Otherwise, reorder reph to the end of the syllable.
805
+ if (!found) {
806
+ newRephPos = end - 1;
807
+ while (newRephPos > start && glyphs[newRephPos].shaperInfo.position === POSITIONS.SMVD) {
808
+ newRephPos--;
809
+ }
810
+
811
+ // If the Reph is to be ending up after a Matra,Halant sequence,
812
+ // position it before that Halant so it can interact with the Matra.
813
+ // However, if it's a plain Consonant,Halant we shouldn't do that.
814
+ // Uniscribe doesn't do this.
815
+ // TEST: U+0930,U+094D,U+0915,U+094B,U+094D
816
+ if (isHalantOrCoeng(glyphs[newRephPos])) {
817
+ for (let i = base + 1; i < newRephPos; i++) {
818
+ if (glyphs[i].shaperInfo.category === CATEGORIES.M) {
819
+ newRephPos--;
820
+ }
821
+ }
822
+ }
823
+ }
824
+
825
+ let reph = glyphs[start];
826
+ glyphs.splice(start, 0, ...glyphs.splice(start + 1, newRephPos - start));
827
+ glyphs[newRephPos] = reph;
828
+
829
+ if (start < base && base <= newRephPos) {
830
+ base--;
831
+ }
832
+ }
833
+
834
+ // o Reorder pre-base reordering consonants:
835
+ //
836
+ // If a pre-base reordering consonant is found, reorder it according to
837
+ // the following rules:
838
+ if (tryPref && base + 1 < end) {
839
+ for (let i = base + 1; i < end; i++) {
840
+ if (glyphs[i].features.pref) {
841
+ // 1. Only reorder a glyph produced by substitution during application
842
+ // of the <pref> feature. (Note that a font may shape a Ra consonant with
843
+ // the feature generally but block it in certain contexts.)
844
+
845
+ // Note: We just check that something got substituted. We don't check that
846
+ // the <pref> feature actually did it...
847
+ //
848
+ // Reorder pref only if it ligated.
849
+ if (glyphs[i].isLigated && !glyphs[i].isMultiplied) {
850
+ // 2. Try to find a target position the same way as for pre-base matra.
851
+ // If it is found, reorder pre-base consonant glyph.
852
+ //
853
+ // 3. If position is not found, reorder immediately before main
854
+ // consonant.
855
+ let newPos = base;
856
+
857
+ // Malayalam / Tamil do not have "half" forms or explicit virama forms.
858
+ // The glyphs formed by 'half' are Chillus or ligated explicit viramas.
859
+ // We want to position matra after them.
860
+ if (plan.unicodeScript !== 'Malayalam' && plan.unicodeScript !== 'Tamil') {
861
+ while (newPos > start && !(glyphs[newPos - 1].shaperInfo.category & (CATEGORIES.M | HALANT_OR_COENG_FLAGS))) {
862
+ newPos--;
863
+ }
864
+
865
+ // In Khmer coeng model, a H,Ra can go *after* matras. If it goes after a
866
+ // split matra, it should be reordered to *before* the left part of such matra.
867
+ if (newPos > start && glyphs[newPos - 1].shaperInfo.category === CATEGORIES.M) {
868
+ let oldPos = i;
869
+ for (let j = base + 1; j < oldPos; j++) {
870
+ if (glyphs[j].shaperInfo.category === CATEGORIES.M) {
871
+ newPos--;
872
+ break;
873
+ }
874
+ }
875
+ }
876
+ }
877
+
878
+ if (newPos > start && isHalantOrCoeng(glyphs[newPos - 1])) {
879
+ // -> If ZWJ or ZWNJ follow this halant, position is moved after it.
880
+ if (newPos < end && isJoiner(glyphs[newPos])) {
881
+ newPos++;
882
+ }
883
+ }
884
+
885
+ let oldPos = i;
886
+ let tmp = glyphs[oldPos];
887
+ glyphs.splice(newPos + 1, 0, ...glyphs.splice(newPos, oldPos - newPos));
888
+ glyphs[newPos] = tmp;
889
+
890
+ if (newPos <= base && base < oldPos) {
891
+ // harfbuzz keeps base for later steps; unused here
892
+ // eslint-disable-next-line no-useless-assignment
893
+ base++;
894
+ }
895
+ }
896
+
897
+ break;
898
+ }
899
+ }
900
+ }
901
+
902
+ // Apply 'init' to the Left Matra if it's a word start.
903
+ if (glyphs[start].shaperInfo.position === POSITIONS.Pre_M && (!start || !/Cf|Mn/.test(getCategory(glyphs[start - 1].codePoints[0])))) {
904
+ glyphs[start].features.init = true;
905
+ }
906
+ }
907
+ }
908
+
909
+ function nextSyllable(glyphs, start) {
910
+ if (start >= glyphs.length) return start;
911
+ let syllable = glyphs[start].shaperInfo.syllable;
912
+ while (++start < glyphs.length && glyphs[start].shaperInfo.syllable === syllable);
913
+ return start;
914
+ }