docxkeun 0.1.3 → 0.2.0

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 (197) hide show
  1. package/CHANGELOG.md +48 -0
  2. package/README.md +137 -16
  3. package/dist/api/editor.d.ts +172 -5
  4. package/dist/api/editor.js +814 -24
  5. package/dist/api/headers.d.ts +51 -0
  6. package/dist/api/headers.js +133 -0
  7. package/dist/api/index.d.ts +5 -1
  8. package/dist/api/index.js +4 -1
  9. package/dist/api/lists.d.ts +37 -0
  10. package/dist/api/lists.js +132 -0
  11. package/dist/api/parts.js +1 -0
  12. package/dist/api/positions.js +1 -0
  13. package/dist/api/revisions.d.ts +61 -0
  14. package/dist/api/revisions.js +390 -0
  15. package/dist/api/selection.d.ts +57 -0
  16. package/dist/api/selection.js +91 -0
  17. package/dist/charts/color.d.ts +45 -0
  18. package/dist/charts/color.js +322 -0
  19. package/dist/charts/format.d.ts +12 -0
  20. package/dist/charts/format.js +459 -0
  21. package/dist/charts/index.d.ts +11 -0
  22. package/dist/charts/index.js +8 -0
  23. package/dist/charts/model.d.ts +225 -0
  24. package/dist/charts/model.js +758 -0
  25. package/dist/charts/outline.d.ts +17 -0
  26. package/dist/charts/outline.js +214 -0
  27. package/dist/charts/render.d.ts +20 -0
  28. package/dist/charts/render.js +1772 -0
  29. package/dist/drawing/arrows.d.ts +37 -0
  30. package/dist/drawing/arrows.js +99 -0
  31. package/dist/drawing/formula.d.ts +40 -0
  32. package/dist/drawing/formula.js +159 -0
  33. package/dist/drawing/geometry.d.ts +61 -0
  34. package/dist/drawing/geometry.js +286 -0
  35. package/dist/drawing/index.d.ts +12 -0
  36. package/dist/drawing/index.js +8 -0
  37. package/dist/drawing/presets.d.ts +72 -0
  38. package/dist/drawing/presets.js +2874 -0
  39. package/dist/fonts/shaping/apply.d.ts +94 -0
  40. package/dist/fonts/shaping/apply.js +349 -0
  41. package/dist/fonts/shaping/arabic.d.ts +28 -0
  42. package/dist/fonts/shaping/arabic.js +235 -0
  43. package/dist/fonts/shaping/buffer.d.ts +53 -0
  44. package/dist/fonts/shaping/buffer.js +95 -0
  45. package/dist/fonts/shaping/gdef.d.ts +30 -0
  46. package/dist/fonts/shaping/gdef.js +66 -0
  47. package/dist/fonts/shaping/gpos.d.ts +86 -0
  48. package/dist/fonts/shaping/gpos.js +428 -0
  49. package/dist/fonts/shaping/gsub.d.ts +65 -0
  50. package/dist/fonts/shaping/gsub.js +313 -0
  51. package/dist/fonts/shaping/index.d.ts +32 -0
  52. package/dist/fonts/shaping/index.js +402 -0
  53. package/dist/fonts/shaping/indic.d.ts +6 -0
  54. package/dist/fonts/shaping/indic.js +805 -0
  55. package/dist/fonts/shaping/layout-common.d.ts +124 -0
  56. package/dist/fonts/shaping/layout-common.js +349 -0
  57. package/dist/fonts/shaping/plan.d.ts +87 -0
  58. package/dist/fonts/shaping/plan.js +152 -0
  59. package/dist/fonts/shaping/scripts.d.ts +26 -0
  60. package/dist/fonts/shaping/scripts.js +160 -0
  61. package/dist/fonts/shaping/shaper.d.ts +23 -0
  62. package/dist/fonts/shaping/shaper.js +5 -0
  63. package/dist/fonts/shaping/unicode.d.ts +34 -0
  64. package/dist/fonts/shaping/unicode.js +138 -0
  65. package/dist/images/bmp.d.ts +9 -0
  66. package/dist/images/bmp.js +234 -0
  67. package/dist/images/gif.d.ts +9 -0
  68. package/dist/images/gif.js +215 -0
  69. package/dist/images/index.d.ts +18 -0
  70. package/dist/images/index.js +63 -0
  71. package/dist/images/png-encode.d.ts +4 -0
  72. package/dist/images/png-encode.js +73 -0
  73. package/dist/images/size.d.ts +10 -0
  74. package/dist/images/size.js +83 -0
  75. package/dist/images/tiff.d.ts +14 -0
  76. package/dist/images/tiff.js +407 -0
  77. package/dist/index.d.ts +7 -2
  78. package/dist/index.js +5 -0
  79. package/dist/layout/bidi.d.ts +18 -0
  80. package/dist/layout/bidi.js +71 -0
  81. package/dist/layout/blocks.d.ts +20 -3
  82. package/dist/layout/blocks.js +88 -58
  83. package/dist/layout/cache.d.ts +31 -0
  84. package/dist/layout/cache.js +38 -0
  85. package/dist/layout/drawing.d.ts +30 -0
  86. package/dist/layout/drawing.js +120 -0
  87. package/dist/layout/engine.d.ts +3 -0
  88. package/dist/layout/engine.js +218 -11
  89. package/dist/layout/index.d.ts +1 -0
  90. package/dist/layout/index.js +1 -0
  91. package/dist/layout/lines.d.ts +63 -5
  92. package/dist/layout/lines.js +245 -20
  93. package/dist/layout/paint.d.ts +25 -3
  94. package/dist/layout/paint.js +26 -10
  95. package/dist/layout/shapes.d.ts +7 -0
  96. package/dist/layout/shapes.js +338 -0
  97. package/dist/layout/style.d.ts +4 -0
  98. package/dist/layout/style.js +2 -0
  99. package/dist/layout/types.d.ts +89 -2
  100. package/dist/math/boxes.d.ts +88 -0
  101. package/dist/math/boxes.js +166 -0
  102. package/dist/math/index.d.ts +29 -0
  103. package/dist/math/index.js +44 -0
  104. package/dist/math/layout.d.ts +13 -0
  105. package/dist/math/layout.js +771 -0
  106. package/dist/math/linear.d.ts +3 -0
  107. package/dist/math/linear.js +152 -0
  108. package/dist/math/omml.d.ts +15 -0
  109. package/dist/math/omml.js +57 -0
  110. package/dist/math/paths.d.ts +61 -0
  111. package/dist/math/paths.js +450 -0
  112. package/dist/math/symbols.d.ts +49 -0
  113. package/dist/math/symbols.js +269 -0
  114. package/dist/math/text.d.ts +53 -0
  115. package/dist/math/text.js +316 -0
  116. package/dist/metafile/canvas.d.ts +80 -0
  117. package/dist/metafile/canvas.js +515 -0
  118. package/dist/metafile/dib.d.ts +27 -0
  119. package/dist/metafile/dib.js +300 -0
  120. package/dist/metafile/emf.d.ts +31 -0
  121. package/dist/metafile/emf.js +591 -0
  122. package/dist/metafile/geometry.d.ts +49 -0
  123. package/dist/metafile/geometry.js +85 -0
  124. package/dist/metafile/index.d.ts +27 -0
  125. package/dist/metafile/index.js +34 -0
  126. package/dist/metafile/reader.d.ts +28 -0
  127. package/dist/metafile/reader.js +59 -0
  128. package/dist/metafile/state.d.ts +118 -0
  129. package/dist/metafile/state.js +172 -0
  130. package/dist/metafile/text.d.ts +17 -0
  131. package/dist/metafile/text.js +130 -0
  132. package/dist/metafile/wmf.d.ts +33 -0
  133. package/dist/metafile/wmf.js +395 -0
  134. package/dist/model/content.d.ts +92 -7
  135. package/dist/model/content.js +251 -17
  136. package/dist/model/document.d.ts +34 -1
  137. package/dist/model/document.js +80 -1
  138. package/dist/model/fields.d.ts +4 -0
  139. package/dist/model/fields.js +10 -0
  140. package/dist/model/numbering.d.ts +2 -0
  141. package/dist/model/numbering.js +22 -0
  142. package/dist/model/props.d.ts +2 -0
  143. package/dist/model/props.js +3 -0
  144. package/dist/opc/index.d.ts +1 -1
  145. package/dist/opc/package.d.ts +34 -0
  146. package/dist/opc/package.js +58 -4
  147. package/dist/pdf/export.js +97 -6
  148. package/dist/pdf/images.d.ts +7 -0
  149. package/dist/pdf/images.js +39 -2
  150. package/dist/render/activity.d.ts +138 -0
  151. package/dist/render/activity.js +794 -0
  152. package/dist/render/clipboard.d.ts +29 -0
  153. package/dist/render/clipboard.js +401 -0
  154. package/dist/render/comments.d.ts +166 -0
  155. package/dist/render/comments.js +779 -0
  156. package/dist/render/context-menu.d.ts +124 -0
  157. package/dist/render/context-menu.js +525 -0
  158. package/dist/render/editing.d.ts +146 -5
  159. package/dist/render/editing.js +678 -53
  160. package/dist/render/index.d.ts +12 -0
  161. package/dist/render/index.js +11 -0
  162. package/dist/render/navigation-panel.d.ts +76 -0
  163. package/dist/render/navigation-panel.js +431 -0
  164. package/dist/render/selection.js +5 -0
  165. package/dist/render/svg.d.ts +4 -6
  166. package/dist/render/svg.js +69 -7
  167. package/dist/render/toolbar.d.ts +83 -0
  168. package/dist/render/toolbar.js +505 -0
  169. package/dist/render/ui/comment-layout.d.ts +39 -0
  170. package/dist/render/ui/comment-layout.js +79 -0
  171. package/dist/render/ui/icons.d.ts +6 -0
  172. package/dist/render/ui/icons.js +38 -0
  173. package/dist/render/ui/menu.d.ts +38 -0
  174. package/dist/render/ui/menu.js +274 -0
  175. package/dist/render/ui/nav-model.d.ts +11 -0
  176. package/dist/render/ui/nav-model.js +34 -0
  177. package/dist/render/ui/overflow.d.ts +5 -0
  178. package/dist/render/ui/overflow.js +18 -0
  179. package/dist/render/ui/popover.d.ts +18 -0
  180. package/dist/render/ui/popover.js +109 -0
  181. package/dist/render/ui/theme.d.ts +2 -0
  182. package/dist/render/ui/theme.js +29 -0
  183. package/dist/render/undo.d.ts +76 -0
  184. package/dist/render/undo.js +144 -0
  185. package/dist/render/viewer.d.ts +100 -7
  186. package/dist/render/viewer.js +302 -92
  187. package/dist/security/policy.d.ts +3 -0
  188. package/dist/security/policy.js +6 -2
  189. package/dist/security/protection.d.ts +1 -0
  190. package/dist/security/protection.js +1 -1
  191. package/dist/text/bidi-data.d.ts +53 -0
  192. package/dist/text/bidi-data.js +318 -0
  193. package/dist/text/bidi.d.ts +37 -0
  194. package/dist/text/bidi.js +508 -0
  195. package/dist/xml/ns.d.ts +5 -0
  196. package/dist/xml/ns.js +7 -0
  197. package/package.json +9 -2
@@ -0,0 +1,805 @@
1
+ /**
2
+ * Indic shaper (Devanagari, Bengali, Gurmukhi, Gujarati, Oriya, Tamil, Telugu, Kannada,
3
+ * Malayalam), following the OpenType Indic shaping spec as implemented by HarfBuzz /
4
+ * Uniscribe:
5
+ *
6
+ * 1. Characters get a syllabic category (consonant, vowel, matra, nukta, halant, ...) and a
7
+ * position class; the text is split into syllables with the Indic syllable grammar.
8
+ * 2. Initial reordering per syllable: find the base consonant, detect a reph (Ra+Halant at
9
+ * the start when the font's 'rphf' forms one), move pre-base matras before the base, and
10
+ * set per-glyph masks so rphf/half/blwf/pstf/pref apply only where the spec says.
11
+ * 3. The basic features run one stage at a time: nukt akhn rphf rkrf pref blwf abvf half
12
+ * pstf vatu cjct.
13
+ * 4. Final reordering: the pre-base matra moves after any half forms, the reph moves to its
14
+ * script-specific position, a pre-base-reordering consonant (Malayalam Ra) moves before
15
+ * the base.
16
+ * 5. The presentation features init pres abvs blws psts haln (+ calt...), then GPOS.
17
+ *
18
+ * Every glyph of a syllable ends up in one cluster (as in Uniscribe), which keeps the
19
+ * reordering invisible to cluster-based layout.
20
+ */
21
+ import { isLigated, isMultiplied, isSubstituted, ligatedAndDidntMultiply, mergeClusters } from './buffer.js';
22
+ import { GlyphProps } from './gdef.js';
23
+ // Syllabic categories (values match HarfBuzz's for easy cross-checking).
24
+ const X = 0;
25
+ const C = 1;
26
+ const V = 2;
27
+ const N = 3;
28
+ const H = 4;
29
+ const ZWNJ = 5;
30
+ const ZWJ = 6;
31
+ const M = 7;
32
+ const SM = 8;
33
+ const A = 9;
34
+ const PLACEHOLDER = 10;
35
+ const DOTTEDCIRCLE = 11;
36
+ const RS = 12;
37
+ const Repha = 14;
38
+ const Ra = 15;
39
+ const CM = 16;
40
+ const Symbol = 17;
41
+ const CS = 18;
42
+ // Positions, in final visual order within a syllable.
43
+ const POS_START = 0;
44
+ const POS_RA_TO_BECOME_REPH = 1;
45
+ const POS_PRE_M = 2;
46
+ const POS_PRE_C = 3;
47
+ const POS_BASE_C = 4;
48
+ const POS_AFTER_MAIN = 5;
49
+ const POS_ABOVE_C = 6;
50
+ const POS_BEFORE_SUB = 7;
51
+ const POS_BELOW_C = 8;
52
+ const POS_AFTER_SUB = 9;
53
+ const POS_BEFORE_POST = 10;
54
+ const POS_POST_C = 11;
55
+ const POS_AFTER_POST = 12;
56
+ const POS_FINAL_C = 13;
57
+ const POS_SMVD = 14;
58
+ const POS_END = 15;
59
+ const bit = (n) => 1 << n;
60
+ const CONSONANT_FLAGS = bit(C) | bit(CS) | bit(Ra) | bit(CM) | bit(V) | bit(PLACEHOLDER) | bit(DOTTEDCIRCLE);
61
+ const JOINER_FLAGS = bit(ZWJ) | bit(ZWNJ);
62
+ /**
63
+ * IndicSyllabicCategory / IndicPositionalCategory for the nine scripts, as
64
+ * "first[-last] category [side]". Sides: L left, R right, T top, B bottom and their
65
+ * combinations (two-part matras); only matras use them.
66
+ */
67
+ const INDIC_DATA = `
68
+ 00A0 PH;200C ZWNJ;200D ZWJ;2010-2014 PH;25CC DC
69
+ 0900-0902 SM T;0903 SM R;0904-0914 V;0915-0939 C;093A M T;093B M R;093C N B;093D Sym;093E M R;093F M L;0940 M R
70
+ 0941-0944 M B;0945-0948 M T;0949-094C M R;094D H B;094E M L;094F M R;0951 A T;0952 A B;0953-0954 SM T;0955 M T
71
+ 0956-0957 M B;0958-095F C;0960-0961 V;0962-0963 M B;0966-096F PH;0972-0977 V;0978-097F C
72
+ 0980 PH;0981 SM T;0982-0983 SM R;0985-098C V;098F-0990 V;0993-0994 V;0995-09A8 C;09AA-09B0 C;09B2 C;09B6-09B9 C
73
+ 09BC N B;09BD Sym;09BE M R;09BF M L;09C0 M R;09C1-09C4 M B;09C7-09C8 M L;09CB-09CC M LR;09CD H B;09CE C;09D7 M R
74
+ 09DC-09DD C;09DF C;09E0-09E1 V;09E2-09E3 M B;09E6-09EF PH;09F0-09F1 C;09FC PH;09FE SM T
75
+ 0A01-0A02 SM T;0A03 SM R;0A05-0A0A V;0A0F-0A10 V;0A13-0A14 V;0A15-0A28 C;0A2A-0A30 C;0A32-0A33 C;0A35-0A36 C
76
+ 0A38-0A39 C;0A3C N B;0A3E M R;0A3F M L;0A40 M R;0A41-0A42 M B;0A47-0A48 M T;0A4B-0A4C M T;0A4D H B;0A51 M B
77
+ 0A59-0A5C C;0A5E C;0A66-0A6F PH;0A70-0A71 SM T;0A72-0A73 C;0A75 CM B
78
+ 0A81-0A82 SM T;0A83 SM R;0A85-0A8D V;0A8F-0A91 V;0A93-0A94 V;0A95-0AA8 C;0AAA-0AB0 C;0AB2-0AB3 C;0AB5-0AB9 C
79
+ 0ABC N B;0ABD Sym;0ABE M R;0ABF M L;0AC0 M R;0AC1-0AC4 M B;0AC5 M T;0AC7-0AC8 M T;0AC9 M TR;0ACB-0ACC M R;0ACD H B
80
+ 0AE0-0AE1 V;0AE2-0AE3 M B;0AE6-0AEF PH;0AF9 C;0AFA A T;0AFB N T;0AFC A T;0AFD-0AFF N T
81
+ 0B01 SM T;0B02-0B03 SM R;0B05-0B0C V;0B0F-0B10 V;0B13-0B14 V;0B15-0B28 C;0B2A-0B30 C;0B32-0B33 C;0B35-0B39 C
82
+ 0B3C N B;0B3D Sym;0B3E M R;0B3F M T;0B40 M R;0B41-0B44 M B;0B47 M L;0B48 M TL;0B4B M LR;0B4C M TLR;0B4D H B
83
+ 0B55 N T;0B56 M T;0B57 M TR;0B5C-0B5D C;0B5F C;0B60-0B61 V;0B62-0B63 M B;0B66-0B6F PH;0B71 C
84
+ 0B82 SM T;0B85-0B8A V;0B8E-0B90 V;0B92-0B94 V;0B95 C;0B99-0B9A C;0B9C C;0B9E-0B9F C;0BA3-0BA4 C;0BA8-0BAA C
85
+ 0BAE-0BB9 C;0BBE-0BBF M R;0BC0 M T;0BC1-0BC2 M R;0BC6-0BC8 M L;0BCA-0BCC M LR;0BCD H T;0BD7 M R;0BE6-0BEF PH
86
+ 0C00 SM T;0C01-0C03 SM R;0C04 SM T;0C05-0C0C V;0C0E-0C10 V;0C12-0C14 V;0C15-0C28 C;0C2A-0C39 C;0C3C N B;0C3D Sym
87
+ 0C3E-0C40 M T;0C41-0C44 M R;0C46-0C47 M T;0C48 M TB;0C4A-0C4C M T;0C4D H T;0C55 M T;0C56 M B;0C58-0C5A C;0C5D C
88
+ 0C60-0C61 V;0C62-0C63 M B;0C66-0C6F PH
89
+ 0C80 PH;0C81 SM T;0C82-0C83 SM R;0C85-0C8C V;0C8E-0C90 V;0C92-0C94 V;0C95-0CA8 C;0CAA-0CB3 C;0CB5-0CB9 C;0CBC N B
90
+ 0CBD Sym;0CBE M R;0CBF M T;0CC0 M TR;0CC1-0CC4 M R;0CC6 M T;0CC7-0CC8 M TR;0CCA-0CCB M TR;0CCC M T;0CCD H T
91
+ 0CD5-0CD6 M R;0CDD-0CDE C;0CE0-0CE1 V;0CE2-0CE3 M B;0CE6-0CEF PH;0CF1-0CF2 CS;0CF3 SM R
92
+ 0D00-0D01 SM T;0D02-0D03 SM R;0D05-0D0C V;0D0E-0D10 V;0D12-0D14 V;0D15-0D3A C;0D3B-0D3C M T;0D3D Sym;0D3E-0D42 M R
93
+ 0D43-0D44 M B;0D46-0D48 M L;0D4A-0D4C M LR;0D4D H T;0D4E Repha;0D54-0D56 C;0D57 M R;0D5F-0D61 V;0D62-0D63 M B
94
+ 0D66-0D6F PH;0D7A-0D7F C
95
+ 1CD0-1CD2 A T;1CD4-1CE8 A;1CE9-1CEC Sym;1CED A B;1CEE-1CF1 Sym;1CF2-1CF3 SM R;1CF4 A T;1CF5-1CF6 C;1CF8-1CF9 A T
96
+ A8E0-A8F1 A T;A8F2-A8F7 Sym;A8FF M T
97
+ `;
98
+ const CATEGORY_CODES = {
99
+ X, C, V, N, H, M, SM, A, PH: PLACEHOLDER, DC: DOTTEDCIRCLE, Sym: Symbol, Repha, CM, CS, ZWJ, ZWNJ,
100
+ };
101
+ const SIDE_CODES = {
102
+ L: POS_PRE_C, R: POS_POST_C, T: POS_ABOVE_C, B: POS_BELOW_C,
103
+ // Two- and three-part matras resolve to the position of their last part.
104
+ LR: POS_POST_C, TR: POS_POST_C, TL: POS_ABOVE_C, TLR: POS_POST_C, TB: POS_BELOW_C, BR: POS_POST_C,
105
+ };
106
+ const RA = new Set([0x0930, 0x09b0, 0x09f0, 0x0a30, 0x0ab0, 0x0b30, 0x0bb0, 0x0c30, 0x0cb0, 0x0d30]);
107
+ const TABLE = new Map();
108
+ {
109
+ for (const entry of INDIC_DATA.split(/[;\n]/)) {
110
+ const m = /^\s*([0-9A-F]{4})(?:-([0-9A-F]{4}))?\s+(\w+)(?:\s+(\w+))?\s*$/.exec(entry);
111
+ if (!m)
112
+ continue;
113
+ const first = parseInt(m[1], 16);
114
+ const last = m[2] ? parseInt(m[2], 16) : first;
115
+ const cat = CATEGORY_CODES[m[3]] ?? X;
116
+ const side = m[4] ? (SIDE_CODES[m[4]] ?? POS_END) : POS_END;
117
+ for (let cp = first; cp <= last; cp++)
118
+ TABLE.set(cp, cat | (side << 8));
119
+ }
120
+ }
121
+ function blockScript(cp) {
122
+ if ((cp >= 0x0900 && cp <= 0x097f) || (cp >= 0x1cd0 && cp <= 0x1cff) || (cp >= 0xa8e0 && cp <= 0xa8ff))
123
+ return 'deva';
124
+ if (cp >= 0x0980 && cp <= 0x09ff)
125
+ return 'beng';
126
+ if (cp >= 0x0a00 && cp <= 0x0a7f)
127
+ return 'guru';
128
+ if (cp >= 0x0a80 && cp <= 0x0aff)
129
+ return 'gujr';
130
+ if (cp >= 0x0b00 && cp <= 0x0b7f)
131
+ return 'orya';
132
+ if (cp >= 0x0b80 && cp <= 0x0bff)
133
+ return 'taml';
134
+ if (cp >= 0x0c00 && cp <= 0x0c7f)
135
+ return 'telu';
136
+ if (cp >= 0x0c80 && cp <= 0x0cff)
137
+ return 'knda';
138
+ if (cp >= 0x0d00 && cp <= 0x0d7f)
139
+ return 'mlym';
140
+ return '';
141
+ }
142
+ /** Where a matra on a given side sits relative to the consonants, per script. */
143
+ function matraPosition(cp, side) {
144
+ const s = blockScript(cp);
145
+ switch (side) {
146
+ case POS_PRE_C:
147
+ return POS_PRE_M;
148
+ case POS_POST_C:
149
+ if (s === 'deva')
150
+ return POS_AFTER_SUB;
151
+ if (s === 'telu')
152
+ return cp <= 0x0c42 ? POS_BEFORE_SUB : POS_AFTER_SUB;
153
+ if (s === 'knda')
154
+ return cp < 0x0cc3 || cp > 0x0cd6 ? POS_BEFORE_SUB : POS_AFTER_SUB;
155
+ if (s === 'beng' || s === 'guru' || s === 'gujr' || s === 'orya' || s === 'taml' || s === 'mlym')
156
+ return POS_AFTER_POST;
157
+ return POS_AFTER_SUB;
158
+ case POS_ABOVE_C:
159
+ if (s === 'guru')
160
+ return POS_AFTER_POST; // deviates from the spec, like Uniscribe
161
+ if (s === 'orya')
162
+ return POS_AFTER_MAIN;
163
+ if (s === 'telu' || s === 'knda')
164
+ return POS_BEFORE_SUB;
165
+ return POS_AFTER_SUB;
166
+ case POS_BELOW_C:
167
+ if (s === 'guru' || s === 'gujr' || s === 'taml' || s === 'mlym')
168
+ return POS_AFTER_POST;
169
+ if (s === 'telu' || s === 'knda')
170
+ return POS_BEFORE_SUB;
171
+ return POS_AFTER_SUB;
172
+ default:
173
+ return side;
174
+ }
175
+ }
176
+ /** [category, position] of a code point. */
177
+ export function indicProperties(cp) {
178
+ const entry = TABLE.get(cp);
179
+ let cat = entry === undefined ? X : entry & 0xff;
180
+ const side = entry === undefined ? POS_END : entry >> 8;
181
+ let pos = side;
182
+ if (bit(cat) & CONSONANT_FLAGS) {
183
+ pos = POS_BASE_C;
184
+ if (RA.has(cp))
185
+ cat = Ra;
186
+ }
187
+ else if (cat === M)
188
+ pos = matraPosition(cp, side);
189
+ else if (cat === SM || cat === A || cat === Symbol)
190
+ pos = POS_SMVD;
191
+ if (cp === 0x0b01)
192
+ pos = POS_BEFORE_SUB; // Oriya candrabindu
193
+ return [cat, pos];
194
+ }
195
+ /**
196
+ * Characters HarfBuzz always decomposes for Indic: two-part matras (so each part can be
197
+ * reordered) and nukta consonants excluded from composition (the font's 'nukt' forms them).
198
+ */
199
+ const ALWAYS_DECOMPOSE = new Set([
200
+ 0x09cb, 0x09cc, 0x0b48, 0x0b4b, 0x0b4c, 0x0bca, 0x0bcb, 0x0bcc, 0x0c48, 0x0cc0, 0x0cc7, 0x0cc8, 0x0cca, 0x0ccb, 0x0d4a,
201
+ 0x0d4b, 0x0d4c, 0x0958, 0x0959, 0x095a, 0x095b, 0x095c, 0x095d, 0x095e, 0x095f, 0x09df, 0x0a33, 0x0a36, 0x0a59, 0x0a5a,
202
+ 0x0a5b, 0x0a5e, 0x0b5c, 0x0b5d,
203
+ ]);
204
+ // --- Syllables --------------------------------------------------------------------------
205
+ const SYL_CONSONANT = 0;
206
+ const SYL_VOWEL = 1;
207
+ const SYL_STANDALONE = 2;
208
+ const SYL_SYMBOL = 3;
209
+ const SYL_BROKEN = 4;
210
+ const SYL_OTHER = 5;
211
+ const cat = (...cs) => (s, i) => i < s.length && cs.includes(s[i]) ? [i + 1] : [];
212
+ const seq = (...ps) => (s, i) => {
213
+ let cur = [i];
214
+ for (const p of ps) {
215
+ const next = new Set();
216
+ for (const c of cur)
217
+ for (const e of p(s, c))
218
+ next.add(e);
219
+ cur = [...next];
220
+ if (!cur.length)
221
+ break;
222
+ }
223
+ return cur;
224
+ };
225
+ const alt = (...ps) => (s, i) => [...new Set(ps.flatMap((p) => p(s, i)))];
226
+ const opt = (p) => (s, i) => [...new Set([i, ...p(s, i)])];
227
+ const star = (p) => (s, i) => {
228
+ const seen = new Set([i]);
229
+ let frontier = [i];
230
+ while (frontier.length) {
231
+ const next = [];
232
+ for (const f of frontier)
233
+ for (const e of p(s, f))
234
+ if (!seen.has(e))
235
+ (seen.add(e), next.push(e));
236
+ frontier = next;
237
+ }
238
+ return [...seen];
239
+ };
240
+ // The Indic syllable grammar (HarfBuzz's hb-ot-shaper-indic-machine.rl).
241
+ const c = cat(C, Ra);
242
+ const n = seq(opt(seq(opt(cat(ZWNJ)), cat(RS))), opt(seq(cat(N), opt(cat(N)))));
243
+ const z = cat(ZWJ, ZWNJ);
244
+ const reph = alt(seq(cat(Ra), cat(H)), cat(Repha));
245
+ const cn = seq(c, opt(cat(ZWJ)), n);
246
+ const symbol = seq(cat(Symbol), opt(cat(N)));
247
+ const matraGroup = seq(star(z), cat(M), opt(cat(N)), opt(cat(H)));
248
+ const syllableTail = seq(opt(seq(opt(z), cat(SM), opt(cat(SM)), opt(cat(ZWNJ)))), star(cat(A)));
249
+ const halantGroup = seq(opt(z), cat(H), opt(seq(cat(ZWJ), opt(cat(N)))));
250
+ const finalHalantGroup = alt(halantGroup, seq(cat(H), cat(ZWNJ)));
251
+ const medialGroup = opt(cat(CM));
252
+ const halantOrMatraGroup = alt(finalHalantGroup, star(matraGroup));
253
+ const complexSyllableTail = seq(star(seq(halantGroup, cn)), medialGroup, halantOrMatraGroup, syllableTail);
254
+ const SYLLABLES = [
255
+ [SYL_CONSONANT, seq(opt(cat(Repha, CS)), cn, complexSyllableTail)],
256
+ [SYL_VOWEL, seq(opt(reph), cat(V), n, alt(cat(ZWJ), complexSyllableTail))],
257
+ [SYL_STANDALONE, seq(alt(seq(opt(cat(Repha, CS)), cat(PLACEHOLDER)), seq(opt(reph), cat(DOTTEDCIRCLE))), n, complexSyllableTail)],
258
+ [SYL_SYMBOL, seq(symbol, syllableTail)],
259
+ [SYL_BROKEN, seq(opt(reph), n, complexSyllableTail)],
260
+ ];
261
+ /** Splits categories into syllables: [start, end, type][], longest match first. */
262
+ export function findSyllables(cats) {
263
+ const out = [];
264
+ let i = 0;
265
+ while (i < cats.length) {
266
+ let bestEnd = i + 1;
267
+ let bestType = SYL_OTHER;
268
+ let bestLen = 0;
269
+ for (const [type, p] of SYLLABLES) {
270
+ const end = Math.max(...p(cats, i));
271
+ if (end - i > bestLen) {
272
+ bestLen = end - i;
273
+ bestEnd = end;
274
+ bestType = type;
275
+ }
276
+ }
277
+ out.push([i, bestEnd, bestType]);
278
+ i = bestEnd;
279
+ }
280
+ return out;
281
+ }
282
+ const CONFIGS = {
283
+ deva: { hasOldSpec: true, virama: 0x094d, rephPos: POS_BEFORE_POST, rephMode: 'implicit', blwfMode: 'pre-and-post' },
284
+ beng: { hasOldSpec: true, virama: 0x09cd, rephPos: POS_AFTER_SUB, rephMode: 'implicit', blwfMode: 'pre-and-post' },
285
+ guru: { hasOldSpec: true, virama: 0x0a4d, rephPos: POS_BEFORE_SUB, rephMode: 'implicit', blwfMode: 'pre-and-post' },
286
+ gujr: { hasOldSpec: true, virama: 0x0acd, rephPos: POS_BEFORE_POST, rephMode: 'implicit', blwfMode: 'pre-and-post' },
287
+ orya: { hasOldSpec: true, virama: 0x0b4d, rephPos: POS_AFTER_MAIN, rephMode: 'implicit', blwfMode: 'pre-and-post' },
288
+ taml: { hasOldSpec: true, virama: 0x0bcd, rephPos: POS_AFTER_POST, rephMode: 'implicit', blwfMode: 'pre-and-post' },
289
+ telu: { hasOldSpec: true, virama: 0x0c4d, rephPos: POS_AFTER_POST, rephMode: 'explicit', blwfMode: 'post-only' },
290
+ knda: { hasOldSpec: true, virama: 0x0ccd, rephPos: POS_AFTER_POST, rephMode: 'implicit', blwfMode: 'post-only' },
291
+ mlym: { hasOldSpec: true, virama: 0x0d4d, rephPos: POS_AFTER_MAIN, rephMode: 'log-repha', blwfMode: 'pre-and-post' },
292
+ };
293
+ const DEFAULT_CONFIG = { hasOldSpec: false, virama: 0, rephPos: POS_BEFORE_POST, rephMode: 'implicit', blwfMode: 'pre-and-post' };
294
+ function planData(ctx) {
295
+ let d = ctx.plan.shaperData;
296
+ if (!d) {
297
+ const script = ctx.plan.script;
298
+ const config = CONFIGS[script] ?? DEFAULT_CONFIG;
299
+ const isOldSpec = config.hasOldSpec && !(ctx.plan.gsubScript ?? '').endsWith('2');
300
+ d = {
301
+ script,
302
+ config,
303
+ isOldSpec,
304
+ zeroContext: !isOldSpec && script !== 'mlym',
305
+ viramaGlyph: config.virama ? ctx.font.glyphId(config.virama) : 0,
306
+ consonantPositions: new Map(),
307
+ };
308
+ ctx.plan.shaperData = d;
309
+ }
310
+ return d;
311
+ }
312
+ function wouldSubstitute(ctx, d, tag, glyphs) {
313
+ const gsub = ctx.plan.gsub;
314
+ return !!gsub && gsub.wouldSubstitute(ctx.plan.gsubLookups(tag), glyphs, d.zeroContext);
315
+ }
316
+ const isOneOf = (info, flags) => !isLigated(info) && (bit(info.cat) & flags) !== 0; // a ligature has no category any more
317
+ const isConsonant = (info) => isOneOf(info, CONSONANT_FLAGS);
318
+ const isJoiner = (info) => isOneOf(info, JOINER_FLAGS);
319
+ const isHalant = (info) => isOneOf(info, bit(H));
320
+ function reverseRange(buf, start, end) {
321
+ for (let i = start, j = end - 1; i < j; i++, j--)
322
+ [buf[i], buf[j]] = [buf[j], buf[i]];
323
+ }
324
+ /** Moves the glyph at `from` to index `to` (as it reads after removal), shifting the rest. */
325
+ function moveGlyph(buf, from, to) {
326
+ const [g] = buf.splice(from, 1);
327
+ buf.splice(to, 0, g);
328
+ }
329
+ function forEachSyllable(buf, fn) {
330
+ let start = 0;
331
+ while (start < buf.length) {
332
+ let end = start + 1;
333
+ while (end < buf.length && buf[end].syllable === buf[start].syllable)
334
+ end++;
335
+ fn(start, end);
336
+ start = end;
337
+ }
338
+ }
339
+ // --- Shaping stages ---------------------------------------------------------------------
340
+ function setupMasks(ctx) {
341
+ for (const info of ctx.buffer)
342
+ [info.cat, info.pos] = indicProperties(info.codepoint);
343
+ }
344
+ function setupSyllables(ctx) {
345
+ const buf = ctx.buffer;
346
+ const syllables = findSyllables(buf.map((g) => g.cat));
347
+ let serial = 1;
348
+ for (const [start, end, type] of syllables) {
349
+ for (let i = start; i < end; i++)
350
+ buf[i].syllable = serial * 16 + type;
351
+ mergeClusters(buf, start, end);
352
+ serial++;
353
+ }
354
+ }
355
+ /** Below-base / post-base consonants are those the font forms with a halant (blwf, pstf...). */
356
+ function consonantPosition(ctx, d, consonant) {
357
+ const cached = d.consonantPositions.get(consonant);
358
+ if (cached !== undefined)
359
+ return cached;
360
+ // New-spec tables match Halant,Consonant; old-spec (and fonts copying them) Consonant,Halant.
361
+ const g1 = [d.viramaGlyph, consonant];
362
+ const g2 = [consonant, d.viramaGlyph];
363
+ const ws = (tag) => wouldSubstitute(ctx, d, tag, g1) || wouldSubstitute(ctx, d, tag, g2);
364
+ let pos = POS_BASE_C;
365
+ if (ws('blwf') || ws('vatu'))
366
+ pos = POS_BELOW_C;
367
+ else if (ws('pstf') || ws('pref'))
368
+ pos = POS_POST_C;
369
+ d.consonantPositions.set(consonant, pos);
370
+ return pos;
371
+ }
372
+ function initialReordering(ctx) {
373
+ const d = planData(ctx);
374
+ const buf = ctx.buffer;
375
+ if (d.viramaGlyph) {
376
+ for (const info of buf)
377
+ if (info.pos === POS_BASE_C)
378
+ info.pos = consonantPosition(ctx, d, info.glyph);
379
+ }
380
+ forEachSyllable(buf, (start, end) => {
381
+ const type = buf[start].syllable & 15;
382
+ if (type === SYL_CONSONANT || type === SYL_VOWEL || type === SYL_STANDALONE || type === SYL_BROKEN)
383
+ initialReorderSyllable(ctx, d, start, end);
384
+ });
385
+ }
386
+ function initialReorderSyllable(ctx, d, start, end) {
387
+ const buf = ctx.buffer;
388
+ const plan = ctx.plan;
389
+ const g = (i) => buf[i];
390
+ // Kannada: Ra,Halant,ZWJ behaves like Ra,ZWJ,Halant (legacy encoding of the eyelash form).
391
+ if (d.script === 'knda' && start + 3 <= end && isOneOf(g(start), bit(Ra)) && isOneOf(g(start + 1), bit(H)) && isOneOf(g(start + 2), bit(ZWJ))) {
392
+ [buf[start + 1], buf[start + 2]] = [g(start + 2), g(start + 1)];
393
+ }
394
+ // 1. Find the base consonant, excluding an initial Ra+Halant that forms a reph.
395
+ let base = end;
396
+ let hasReph = false;
397
+ let limit = start;
398
+ const rphf = plan.maskOf('rphf');
399
+ const mode = d.config.rephMode;
400
+ if (rphf && start + 3 <= end && ((mode === 'implicit' && !isJoiner(g(start + 2))) || (mode === 'explicit' && g(start + 2).cat === ZWJ))) {
401
+ const glyphs = [g(start).glyph, g(start + 1).glyph];
402
+ if (wouldSubstitute(ctx, d, 'rphf', glyphs) || (mode === 'explicit' && wouldSubstitute(ctx, d, 'rphf', [...glyphs, g(start + 2).glyph]))) {
403
+ limit += 2;
404
+ while (limit < end && isJoiner(g(limit)))
405
+ limit++;
406
+ base = start;
407
+ hasReph = true;
408
+ }
409
+ }
410
+ else if (mode === 'log-repha' && g(start).cat === Repha) {
411
+ limit += 1;
412
+ while (limit < end && isJoiner(g(limit)))
413
+ limit++;
414
+ base = start;
415
+ hasReph = true;
416
+ }
417
+ // Walk back from the end to the first consonant without a below-/post-base form.
418
+ {
419
+ let i = end;
420
+ let seenBelow = false;
421
+ do {
422
+ i--;
423
+ const info = g(i);
424
+ if (isConsonant(info)) {
425
+ if (info.pos !== POS_BELOW_C && (info.pos !== POS_POST_C || seenBelow)) {
426
+ base = i;
427
+ break;
428
+ }
429
+ if (info.pos === POS_BELOW_C)
430
+ seenBelow = true;
431
+ base = i;
432
+ }
433
+ else if (start < i && info.cat === ZWJ && g(i - 1).cat === H) {
434
+ // Halant,ZWJ asks for an explicit half form: the base is after it.
435
+ break;
436
+ }
437
+ } while (i > limit);
438
+ }
439
+ // A lone Ra+Halant (no other consonant) doesn't form a reph; Ra is the base.
440
+ if (hasReph && base === start && limit - base <= 2)
441
+ hasReph = false;
442
+ // 2. Assign final positions.
443
+ for (let i = start; i < base; i++)
444
+ g(i).pos = Math.min(POS_PRE_C, g(i).pos);
445
+ if (base < end)
446
+ g(base).pos = POS_BASE_C;
447
+ // A consonant after a matra is a final consonant.
448
+ for (let i = base + 1; i < end; i++) {
449
+ if (g(i).cat === M) {
450
+ for (let j = i + 1; j < end; j++) {
451
+ if (isConsonant(g(j))) {
452
+ g(j).pos = POS_FINAL_C;
453
+ break;
454
+ }
455
+ }
456
+ break;
457
+ }
458
+ }
459
+ if (hasReph)
460
+ g(start).pos = POS_RA_TO_BECOME_REPH;
461
+ if (d.isOldSpec) {
462
+ // Old spec: the first post-base halant moves after the last consonant.
463
+ const noDoubleHalant = d.script === 'knda';
464
+ for (let i = base + 1; i < end; i++) {
465
+ if (g(i).cat === H) {
466
+ let j = end - 1;
467
+ for (; j > i; j--)
468
+ if (isConsonant(g(j)) || (noDoubleHalant && g(j).cat === H))
469
+ break;
470
+ if (g(j).cat !== H && j > i)
471
+ moveGlyph(buf, i, j);
472
+ break;
473
+ }
474
+ }
475
+ }
476
+ // Joiners, nuktas, halants and medials travel with the character before them.
477
+ {
478
+ let lastPos = POS_START;
479
+ for (let i = start; i < end; i++) {
480
+ const info = g(i);
481
+ if (bit(info.cat) & (JOINER_FLAGS | bit(N) | bit(RS) | bit(CM) | bit(H))) {
482
+ info.pos = lastPos;
483
+ if (info.cat === H && info.pos === POS_PRE_M) {
484
+ // Uniscribe doesn't move a halant along with a left matra.
485
+ for (let j = i; j > start; j--) {
486
+ if (g(j - 1).pos !== POS_PRE_M) {
487
+ info.pos = g(j - 1).pos;
488
+ break;
489
+ }
490
+ }
491
+ }
492
+ }
493
+ else if (info.pos !== POS_SMVD) {
494
+ lastPos = info.pos;
495
+ }
496
+ }
497
+ }
498
+ // Post-base consonants own the marks since the previous consonant or matra.
499
+ {
500
+ let last = base;
501
+ for (let i = base + 1; i < end; i++) {
502
+ if (isConsonant(g(i))) {
503
+ for (let j = last + 1; j < i; j++)
504
+ if (g(j).pos < POS_SMVD)
505
+ g(j).pos = g(i).pos;
506
+ last = i;
507
+ }
508
+ else if (g(i).cat === M)
509
+ last = i;
510
+ }
511
+ }
512
+ // Stable sort by position.
513
+ const sorted = buf
514
+ .slice(start, end)
515
+ .map((info, k) => ({ info, k }))
516
+ .sort((a, b) => a.info.pos - b.info.pos || a.k - b.k);
517
+ for (let k = 0; k < sorted.length; k++)
518
+ buf[start + k] = sorted[k].info;
519
+ // Find the base again; several left matras keep their logical order.
520
+ let firstLeft = end;
521
+ let lastLeft = end;
522
+ base = end;
523
+ for (let i = start; i < end; i++) {
524
+ if (g(i).pos === POS_BASE_C) {
525
+ base = i;
526
+ break;
527
+ }
528
+ if (g(i).pos === POS_PRE_M) {
529
+ if (firstLeft === end)
530
+ firstLeft = i;
531
+ lastLeft = i;
532
+ }
533
+ }
534
+ if (firstLeft < lastLeft) {
535
+ reverseRange(buf, firstLeft, lastLeft + 1);
536
+ let i = firstLeft;
537
+ for (let j = i; j <= lastLeft; j++) {
538
+ if (g(j).cat === M) {
539
+ reverseRange(buf, i, j + 1);
540
+ i = j + 1;
541
+ }
542
+ }
543
+ }
544
+ // 3. Masks: reph, pre-base (half, and below forms in new-spec), post-base forms.
545
+ for (let i = start; i < end && g(i).pos === POS_RA_TO_BECOME_REPH; i++)
546
+ g(i).mask |= rphf;
547
+ let mask = plan.maskOf('half');
548
+ if (!d.isOldSpec && d.config.blwfMode === 'pre-and-post')
549
+ mask |= plan.maskOf('blwf');
550
+ for (let i = start; i < base; i++)
551
+ g(i).mask |= mask;
552
+ mask = plan.maskOf('blwf') | plan.maskOf('abvf') | plan.maskOf('pstf');
553
+ for (let i = base + 1; i < end; i++)
554
+ g(i).mask |= mask;
555
+ if (d.isOldSpec && d.script === 'deva') {
556
+ // Old-spec eyelash Ra: Ra,Halant before the base takes 'blwf' unless followed by ZWJ.
557
+ const blwf = plan.maskOf('blwf');
558
+ for (let i = start; i + 1 < base; i++) {
559
+ if (g(i).cat === Ra && g(i + 1).cat === H && (i + 2 === base || g(i + 2).cat !== ZWJ)) {
560
+ g(i).mask |= blwf;
561
+ g(i + 1).mask |= blwf;
562
+ }
563
+ }
564
+ }
565
+ const pref = plan.maskOf('pref');
566
+ if (pref && base + 2 < end) {
567
+ // A Halant,Ra pair the font forms with 'pref' is a pre-base-reordering consonant.
568
+ for (let i = base + 1; i + 1 < end; i++) {
569
+ if (wouldSubstitute(ctx, d, 'pref', [g(i).glyph, g(i + 1).glyph])) {
570
+ g(i).mask |= pref;
571
+ g(i + 1).mask |= pref;
572
+ break;
573
+ }
574
+ }
575
+ }
576
+ // ZWNJ blocks half forms of the consonant before it.
577
+ const half = plan.maskOf('half');
578
+ for (let i = start + 1; i < end; i++) {
579
+ if (isJoiner(g(i))) {
580
+ const nonJoiner = g(i).cat === ZWNJ;
581
+ let j = i;
582
+ do {
583
+ j--;
584
+ if (nonJoiner)
585
+ g(j).mask &= ~half;
586
+ } while (j > start && !isConsonant(g(j)));
587
+ }
588
+ }
589
+ }
590
+ function finalReordering(ctx) {
591
+ const d = planData(ctx);
592
+ forEachSyllable(ctx.buffer, (start, end) => finalReorderSyllable(ctx, d, start, end));
593
+ }
594
+ function finalReorderSyllable(ctx, d, start, end) {
595
+ const buf = ctx.buffer;
596
+ const plan = ctx.plan;
597
+ const g = (i) => buf[i];
598
+ const script = d.script;
599
+ // A halant that went through a ligate-then-decompose round trip is still a halant.
600
+ if (d.viramaGlyph) {
601
+ for (let i = start; i < end; i++) {
602
+ const info = g(i);
603
+ if (info.glyph === d.viramaGlyph && isLigated(info) && isMultiplied(info)) {
604
+ info.cat = H;
605
+ info.props &= ~(GlyphProps.Ligated | GlyphProps.Multiplied);
606
+ }
607
+ }
608
+ }
609
+ const prefMask = plan.maskOf('pref');
610
+ let tryPref = !!prefMask;
611
+ // Find the base again (basic features may have merged glyphs).
612
+ let base = start;
613
+ for (; base < end; base++) {
614
+ if (g(base).pos >= POS_BASE_C) {
615
+ if (tryPref && base + 1 < end) {
616
+ for (let i = base + 1; i < end; i++) {
617
+ if (g(i).mask & prefMask) {
618
+ if (!(isSubstituted(g(i)) && ligatedAndDidntMultiply(g(i)))) {
619
+ // A 'pref' candidate that didn't form: the base is around here.
620
+ base = i;
621
+ while (base < end && isHalant(g(base)))
622
+ base++;
623
+ if (base < end)
624
+ g(base).pos = POS_BASE_C;
625
+ tryPref = false;
626
+ }
627
+ break;
628
+ }
629
+ }
630
+ if (base === end)
631
+ break;
632
+ }
633
+ if (script === 'mlym') {
634
+ // Malayalam: skip over unformed below (but not post) forms.
635
+ for (let i = base + 1; i < end; i++) {
636
+ while (i < end && isJoiner(g(i)))
637
+ i++;
638
+ if (i === end || !isHalant(g(i)))
639
+ break;
640
+ i++;
641
+ while (i < end && isJoiner(g(i)))
642
+ i++;
643
+ if (i < end && isConsonant(g(i)) && g(i).pos === POS_BELOW_C) {
644
+ base = i;
645
+ g(base).pos = POS_BASE_C;
646
+ }
647
+ }
648
+ }
649
+ if (start < base && g(base).pos > POS_BASE_C)
650
+ base--;
651
+ break;
652
+ }
653
+ }
654
+ if (base === end && start < base && isOneOf(g(base - 1), bit(ZWJ)))
655
+ base--;
656
+ if (base < end)
657
+ while (start < base && isOneOf(g(base), bit(N) | bit(H)))
658
+ base--;
659
+ // Pre-base matra: move it after the last standalone halant before the base (i.e. after
660
+ // any half forms that didn't ligate), unless that halant is followed by ZWJ.
661
+ if (start + 1 < end && start < base) {
662
+ let newPos = base === end ? base - 2 : base - 1;
663
+ if (script !== 'mlym' && script !== 'taml') {
664
+ for (;;) {
665
+ while (newPos > start && !isOneOf(g(newPos), bit(M) | bit(H)))
666
+ newPos--;
667
+ if (isHalant(g(newPos)) && g(newPos).pos !== POS_PRE_M) {
668
+ if (newPos + 1 < end && g(newPos + 1).cat === ZWJ && newPos > start) {
669
+ newPos--;
670
+ continue;
671
+ }
672
+ }
673
+ else
674
+ newPos = start;
675
+ break;
676
+ }
677
+ }
678
+ if (start < newPos && g(newPos).pos !== POS_PRE_M) {
679
+ for (let i = newPos; i > start; i--) {
680
+ if (g(i - 1).pos === POS_PRE_M) {
681
+ const oldPos = i - 1;
682
+ if (oldPos < base && base <= newPos)
683
+ base--;
684
+ moveGlyph(buf, oldPos, newPos);
685
+ newPos--;
686
+ }
687
+ }
688
+ }
689
+ }
690
+ // Reph: only move it when Ra+Halant actually ligated into a reph glyph (or, for an
691
+ // atomically encoded repha, when it did NOT ligate).
692
+ if (start + 1 < end && g(start).pos === POS_RA_TO_BECOME_REPH && (g(start).cat === Repha) !== ligatedAndDidntMultiply(g(start))) {
693
+ const rephPos = d.config.rephPos;
694
+ const afterFirstHalant = () => {
695
+ let p = start + 1;
696
+ while (p < base && !isHalant(g(p)))
697
+ p++;
698
+ if (p < base && isHalant(g(p))) {
699
+ if (p + 1 < base && isJoiner(g(p + 1)))
700
+ p++;
701
+ return p;
702
+ }
703
+ return -1;
704
+ };
705
+ const target = (() => {
706
+ if (rephPos !== POS_AFTER_POST) {
707
+ // 2. After the first explicit halant between the reph and the main consonant.
708
+ const p = afterFirstHalant();
709
+ if (p >= 0)
710
+ return p;
711
+ // 3. After the main consonant (and anything ligated / placed right after it).
712
+ if (rephPos === POS_AFTER_MAIN) {
713
+ let q = base;
714
+ while (q + 1 < end && g(q + 1).pos <= POS_AFTER_MAIN)
715
+ q++;
716
+ if (q < end)
717
+ return q;
718
+ }
719
+ // 4. Before the first post-base consonant / matra / modifier.
720
+ if (rephPos === POS_AFTER_SUB) {
721
+ let q = base;
722
+ while (q + 1 < end && !(bit(g(q + 1).pos) & (bit(POS_POST_C) | bit(POS_AFTER_POST) | bit(POS_SMVD))))
723
+ q++;
724
+ if (q < end)
725
+ return q;
726
+ }
727
+ }
728
+ // 5. Same as 2 (for after-post scripts).
729
+ const p = afterFirstHalant();
730
+ if (p >= 0)
731
+ return p;
732
+ // 6. The end of the syllable, before syllable modifiers / vedic signs.
733
+ let q = end - 1;
734
+ while (q > start && g(q).pos === POS_SMVD)
735
+ q--;
736
+ if (isHalant(g(q))) {
737
+ // After a Matra,Halant sequence, stay before the halant so reph can meet the matra.
738
+ for (let i = base + 1; i < q; i++)
739
+ if (g(i).cat === M)
740
+ q--;
741
+ }
742
+ return q;
743
+ })();
744
+ moveGlyph(buf, start, target);
745
+ if (start < base && base <= target)
746
+ base--;
747
+ }
748
+ // Pre-base-reordering consonant (e.g. Malayalam Ra): move it before the base, after
749
+ // any half forms, when 'pref' actually formed it.
750
+ if (tryPref && base + 1 < end) {
751
+ for (let i = base + 1; i < end; i++) {
752
+ if (g(i).mask & prefMask) {
753
+ if (ligatedAndDidntMultiply(g(i))) {
754
+ let newPos = base;
755
+ if (script !== 'mlym' && script !== 'taml') {
756
+ while (newPos > start && !isOneOf(g(newPos - 1), bit(M) | bit(H)))
757
+ newPos--;
758
+ }
759
+ if (newPos > start && isHalant(g(newPos - 1)) && newPos < end && isJoiner(g(newPos)))
760
+ newPos++;
761
+ moveGlyph(buf, i, newPos);
762
+ if (newPos <= base && base < i)
763
+ base++;
764
+ }
765
+ break;
766
+ }
767
+ }
768
+ }
769
+ // 'init' applies to a left matra at the start of a word.
770
+ if (g(start).pos === POS_PRE_M && (start === 0 || !isWordCharacter(g(start - 1).codepoint)))
771
+ g(start).mask |= plan.maskOf('init');
772
+ }
773
+ const RE_WORD = /[\p{L}\p{M}\p{Cf}\p{Co}\p{Cn}]/u;
774
+ function isWordCharacter(cp) {
775
+ return RE_WORD.test(String.fromCodePoint(cp));
776
+ }
777
+ // --- Features ---------------------------------------------------------------------------
778
+ function collectFeatures(b) {
779
+ const manual = { manualZwj: true, manualZwnj: true, perSyllable: true };
780
+ b.pauseGsub(setupSyllables);
781
+ b.enable('locl', { perSyllable: true });
782
+ b.enable('ccmp', { perSyllable: true });
783
+ b.pauseGsub(initialReordering);
784
+ for (const tag of ['nukt', 'akhn', 'rphf', 'rkrf', 'pref', 'blwf', 'abvf', 'half', 'pstf', 'vatu', 'cjct']) {
785
+ const global = tag === 'nukt' || tag === 'akhn' || tag === 'rkrf' || tag === 'vatu' || tag === 'cjct';
786
+ b.add(tag, { ...manual, global });
787
+ b.pauseGsub();
788
+ }
789
+ b.pauseGsub(finalReordering);
790
+ b.add('init', manual);
791
+ for (const tag of ['pres', 'abvs', 'blws', 'psts', 'haln'])
792
+ b.enable(tag, manual);
793
+ for (const tag of ['dist', 'abvm', 'blwm'])
794
+ b.enable(tag);
795
+ }
796
+ export const indicShaper = {
797
+ name: 'indic',
798
+ collectFeatures,
799
+ // Uniscribe doesn't apply 'liga' to Indic scripts; fonts put Latin-only ligatures there.
800
+ overrideFeatures: (b) => b.remove('liga'),
801
+ setupMasks,
802
+ zeroMarks: false,
803
+ compose: false,
804
+ alwaysDecompose: (cp) => ALWAYS_DECOMPOSE.has(cp),
805
+ };