docxkeun 0.1.2 → 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 +55 -0
  2. package/README.md +139 -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,402 @@
1
+ /**
2
+ * OpenType text shaping: turns a run of text in one font, script and direction into
3
+ * positioned glyphs using the font's GSUB/GPOS tables (ligatures, contextual forms, Arabic
4
+ * joining, Indic reordering, kerning, mark attachment), or cmap + hmtx + legacy 'kern' for
5
+ * fonts without OpenType layout.
6
+ *
7
+ * The pipeline follows HarfBuzz's hb-ot-shape: map characters to glyphs (with light
8
+ * normalization and RTL mirroring), choose a script shaper, build a plan of feature stages,
9
+ * run GSUB, hide default-ignorables, run GPOS, zero mark advances, resolve attachments.
10
+ *
11
+ * Output is in logical order. For right-to-left runs, offsets assume the caller draws the
12
+ * glyphs reversed (visual order), advancing left to right, which is how HarfBuzz computes
13
+ * them before its final reversal.
14
+ */
15
+ import { arabicShaper } from './arabic.js';
16
+ import { ApplyContext } from './apply.js';
17
+ import { deleteGlyph, isMark, newGlyphInfo } from './buffer.js';
18
+ import { Gdef, GlyphProps } from './gdef.js';
19
+ import { Gpos, propagateAttachments } from './gpos.js';
20
+ import { Gsub } from './gsub.js';
21
+ import { indicShaper } from './indic.js';
22
+ import { normalizeTag } from './layout-common.js';
23
+ import { GLOBAL_MASK, PlanBuilder } from './plan.js';
24
+ import { canonicalScript, detectScript, INDIC_NEW_TAGS, scriptTagCandidates } from './scripts.js';
25
+ import { defaultShaper } from './shaper.js';
26
+ import { isExtendedPictographic, markGoesBefore, mirrorOf, UFlag, unicodeFlags } from './unicode.js';
27
+ export { scriptOf, itemizeScripts } from './scripts.js';
28
+ /** Whether the font has OpenType layout tables (otherwise shaping is cmap + kern only). */
29
+ export function hasOpenTypeLayout(font) {
30
+ return !!(font.tableBytes('GSUB')?.length || font.tableBytes('GPOS')?.length);
31
+ }
32
+ const layouts = new WeakMap();
33
+ function fontLayout(font) {
34
+ let layout = layouts.get(font);
35
+ if (!layout) {
36
+ const gsub = font.tableBytes('GSUB');
37
+ const gpos = font.tableBytes('GPOS');
38
+ layout = {
39
+ gsub: gsub && gsub.length >= 10 ? new Gsub(gsub) : undefined,
40
+ gpos: gpos && gpos.length >= 10 ? new Gpos(gpos) : undefined,
41
+ gdef: new Gdef(font.tableBytes('GDEF')),
42
+ plans: new Map(),
43
+ };
44
+ layouts.set(font, layout);
45
+ }
46
+ return layout;
47
+ }
48
+ function chooseShaper(script, gsubScript) {
49
+ if (INDIC_NEW_TAGS[script]) {
50
+ // A font that only covers the script through 'DFLT'/'latn' was designed for no reordering.
51
+ return gsubScript === 'DFLT' || gsubScript === 'latn' ? defaultShaper : indicShaper;
52
+ }
53
+ if (script === 'arab')
54
+ return arabicShaper;
55
+ if (script === 'syrc' || script === 'nko ' || script === 'mong')
56
+ return gsubScript === 'DFLT' ? defaultShaper : arabicShaper;
57
+ return defaultShaper;
58
+ }
59
+ function planFor(font, layout, script, language, direction, features) {
60
+ const featureKey = features
61
+ ? Object.entries(features)
62
+ .map(([t, on]) => `${normalizeTag(t)}=${on ? 1 : 0}`)
63
+ .sort()
64
+ .join(',')
65
+ : '';
66
+ const key = `${script}|${language ?? ''}|${direction}|${featureKey}`;
67
+ const cached = layout.plans.get(key);
68
+ if (cached)
69
+ return cached;
70
+ const candidates = scriptTagCandidates(script);
71
+ const gsubSel = layout.gsub?.layout.select(candidates, language);
72
+ const gposSel = layout.gpos?.layout.select(candidates, language);
73
+ const shaper = chooseShaper(script, gsubSel?.scriptTag);
74
+ const b = new PlanBuilder();
75
+ b.enable('rvrn');
76
+ b.pauseGsub();
77
+ if (direction === 'ltr') {
78
+ b.enable('ltra');
79
+ b.enable('ltrm');
80
+ }
81
+ else {
82
+ b.enable('rtla');
83
+ b.add('rtlm'); // only on characters without a mirrored counterpart in the cmap
84
+ }
85
+ shaper.collectFeatures?.(b, script);
86
+ for (const tag of ['abvm', 'blwm', 'ccmp', 'locl'])
87
+ b.enable(tag);
88
+ b.enable('mark', { manualZwj: true, manualZwnj: true });
89
+ b.enable('mkmk', { manualZwj: true, manualZwnj: true });
90
+ b.enable('rlig');
91
+ for (const tag of ['calt', 'clig', 'curs', 'dist'])
92
+ b.enable(tag);
93
+ b.enable('kern', { fallback: true }); // the legacy 'kern' table stands in when GPOS has none
94
+ b.enable('liga');
95
+ b.enable('rclt');
96
+ shaper.overrideFeatures?.(b);
97
+ for (const [tag, on] of Object.entries(features ?? {})) {
98
+ const t = normalizeTag(tag);
99
+ if (!on)
100
+ b.remove(t);
101
+ else if (!b.has(t))
102
+ b.enable(t);
103
+ }
104
+ const plan = b.compile(layout.gsub, layout.gpos, gsubSel, gposSel);
105
+ plan.script = script;
106
+ const entry = { plan, shaper };
107
+ layout.plans.set(key, entry);
108
+ return entry;
109
+ }
110
+ /**
111
+ * Shapes `text` (one script, one direction, one font) into glyphs in logical order, with
112
+ * advances and offsets in font units.
113
+ */
114
+ export function shape(font, text, options) {
115
+ if (!text)
116
+ return [];
117
+ const direction = options.direction === 'rtl' ? 'rtl' : 'ltr';
118
+ let script = options.script ? normalizeTag(options.script) : detectScript(text);
119
+ script = script === 'DFLT' || script === 'dflt' || script === 'Zyyy' ? 'DFLT' : canonicalScript(script.toLowerCase());
120
+ const language = options.language ? normalizeTag(options.language) : undefined;
121
+ try {
122
+ return shapeOpenType(font, text, script, language, direction, options.features);
123
+ }
124
+ catch {
125
+ // A font whose layout tables trip the engine still gets readable text.
126
+ return shapeSimple(font, text);
127
+ }
128
+ }
129
+ function shapeOpenType(font, text, script, language, direction, features) {
130
+ const layout = fontLayout(font);
131
+ const { plan, shaper } = planFor(font, layout, script, language, direction, features);
132
+ const buffer = buildBuffer(font, text, direction, shaper, plan);
133
+ const ctx = { font, buffer, plan, direction };
134
+ shaper.setupMasks?.(ctx);
135
+ // Glyph classes: from GDEF, else non-spacing marks are marks and the rest bases.
136
+ const gdef = layout.gdef;
137
+ for (const info of buffer) {
138
+ if (gdef.hasGlyphClasses)
139
+ info.props = gdef.glyphProps(info.glyph);
140
+ else
141
+ info.props = info.uflags & UFlag.NonSpacingMark && !(info.uflags & UFlag.DefaultIgnorable) ? GlyphProps.Mark : GlyphProps.BaseGlyph;
142
+ }
143
+ // GSUB, stage by stage, with the shaper's pauses in between.
144
+ const gsub = layout.gsub;
145
+ const gsubCtx = new ApplyContext(buffer, gdef, 0, direction, gsub ? gsub.dispatch : () => -1);
146
+ for (const stage of plan.gsubStages) {
147
+ if (gsub) {
148
+ for (const l of stage.lookups) {
149
+ const lookup = gsub.lookup(l.index);
150
+ if (!lookup)
151
+ continue;
152
+ gsubCtx.autoZwj = l.autoZwj;
153
+ gsubCtx.autoZwnj = l.autoZwnj;
154
+ gsubCtx.perSyllable = l.perSyllable;
155
+ gsubCtx.applyLookup(l.index, lookup.props, l.mask, lookup.reverse);
156
+ }
157
+ }
158
+ stage.pause?.(ctx);
159
+ }
160
+ hideDefaultIgnorables(font, buffer);
161
+ position(font, layout, plan, shaper, buffer, direction);
162
+ return buffer.map((g) => ({ glyph: g.glyph, cluster: g.cluster, advance: g.xAdvance, offsetX: g.xOffset, offsetY: g.yOffset }));
163
+ }
164
+ /** Space-like characters a font may lack, drawn as its space with a standard width. */
165
+ function spaceFallbackWidth(font, cp) {
166
+ const em = font.unitsPerEm ?? 1000;
167
+ const space = font.advance(font.glyphId(0x20));
168
+ switch (cp) {
169
+ case 0x00a0:
170
+ return space;
171
+ case 0x2000:
172
+ case 0x2002:
173
+ return em / 2;
174
+ case 0x2001:
175
+ case 0x2003:
176
+ case 0x3000:
177
+ return em;
178
+ case 0x2004:
179
+ return em / 3;
180
+ case 0x2005:
181
+ return em / 4;
182
+ case 0x2006:
183
+ return em / 6;
184
+ case 0x2007:
185
+ return font.advance(font.glyphId(0x30));
186
+ case 0x2008:
187
+ return font.advance(font.glyphId(0x2e));
188
+ case 0x2009:
189
+ return em / 5;
190
+ case 0x200a:
191
+ return em / 16;
192
+ case 0x202f:
193
+ return space / 2;
194
+ case 0x205f:
195
+ return (em * 4) / 18;
196
+ default:
197
+ return undefined;
198
+ }
199
+ }
200
+ /** Fixed advances for glyphs substituted by the space fallback, keyed by buffer record. */
201
+ const advanceOverrides = new WeakMap();
202
+ /**
203
+ * Characters → glyph records: RTL mirroring, canonical decomposition when the font lacks a
204
+ * precomposed character (or the shaper always splits it), recomposition of base + mark
205
+ * when the font has the precomposed glyph, then grapheme clusters.
206
+ */
207
+ function buildBuffer(font, text, direction, shaper, plan) {
208
+ const buf = [];
209
+ const rtlm = plan.maskOf('rtlm');
210
+ for (let i = 0; i < text.length;) {
211
+ let cp = text.codePointAt(i);
212
+ const cluster = i;
213
+ i += cp > 0xffff ? 2 : 1;
214
+ let needsRtlm = false;
215
+ if (direction === 'rtl') {
216
+ const mirrored = mirrorOf(cp);
217
+ if (mirrored !== undefined) {
218
+ if (font.glyphId(mirrored))
219
+ cp = mirrored;
220
+ else
221
+ needsRtlm = true;
222
+ }
223
+ }
224
+ for (const part of decompose(font, cp, shaper)) {
225
+ const info = newGlyphInfo(part, cluster, unicodeFlags(part));
226
+ info.mask = GLOBAL_MASK | (needsRtlm ? rtlm : 0);
227
+ buf.push(info);
228
+ }
229
+ }
230
+ reorderMarks(buf);
231
+ if (shaper.compose)
232
+ compose(font, buf);
233
+ // Grapheme clusters: marks, joiners and emoji continuations join the previous cluster.
234
+ for (let i = 1; i < buf.length; i++) {
235
+ const info = buf[i];
236
+ const prev = buf[i - 1];
237
+ const cp = info.codepoint;
238
+ const isRegional = (c) => c >= 0x1f1e6 && c <= 0x1f1ff;
239
+ if (info.uflags & (UFlag.Continuation | UFlag.Zwj) ||
240
+ (prev.uflags & UFlag.Zwj && isExtendedPictographic(cp)) ||
241
+ (cp >= 0xff9e && cp <= 0xff9f) ||
242
+ (isRegional(cp) && isRegional(prev.codepoint) && (i < 2 || buf[i - 2].cluster !== prev.cluster))) {
243
+ info.cluster = prev.cluster;
244
+ }
245
+ }
246
+ for (const info of buf) {
247
+ let glyph = font.glyphId(info.codepoint);
248
+ if (!glyph) {
249
+ const width = spaceFallbackWidth(font, info.codepoint);
250
+ if (width !== undefined && font.glyphId(0x20)) {
251
+ glyph = font.glyphId(0x20);
252
+ advanceOverrides.set(info, Math.round(width));
253
+ }
254
+ else if (info.codepoint === 0x2011) {
255
+ glyph = font.glyphId(0x2010) || font.glyphId(0x2d);
256
+ }
257
+ }
258
+ info.glyph = glyph;
259
+ }
260
+ return buf;
261
+ }
262
+ function decompose(font, cp, shaper) {
263
+ if (cp < 0xc0 || (cp >= 0xac00 && cp <= 0xd7a3))
264
+ return [cp];
265
+ const always = shaper.alwaysDecompose?.(cp) ?? false;
266
+ if (!always && font.glyphId(cp))
267
+ return [cp];
268
+ const parts = [...String.fromCodePoint(cp).normalize('NFD')].map((ch) => ch.codePointAt(0));
269
+ if (parts.length === 1 && parts[0] === cp)
270
+ return [cp];
271
+ if (!parts.every((p) => font.glyphId(p)))
272
+ return [cp];
273
+ return parts;
274
+ }
275
+ /** Canonical (combining-class) order within each run of marks, like normalization does. */
276
+ function reorderMarks(buf) {
277
+ const markFlags = UFlag.NonSpacingMark | UFlag.SpacingMark | UFlag.EnclosingMark;
278
+ for (let i = 0; i < buf.length;) {
279
+ let j = i;
280
+ while (j < buf.length && buf[j].uflags & markFlags)
281
+ j++;
282
+ if (j - i >= 2) {
283
+ // Stable bubble sort: runs are a handful of marks, and only adjacent swaps are canonical.
284
+ // At most n passes, so a comparator mixing modified and standard classes can't cycle.
285
+ for (let swapped = true, pass = 0; swapped && pass < j - i; pass++) {
286
+ swapped = false;
287
+ for (let k = i; k + 1 < j; k++) {
288
+ if (markGoesBefore(buf[k + 1].codepoint, buf[k].codepoint)) {
289
+ [buf[k], buf[k + 1]] = [buf[k + 1], buf[k]];
290
+ swapped = true;
291
+ }
292
+ }
293
+ }
294
+ }
295
+ i = Math.max(j, i + 1);
296
+ }
297
+ }
298
+ function compose(font, buf) {
299
+ const markFlags = UFlag.NonSpacingMark | UFlag.SpacingMark | UFlag.EnclosingMark;
300
+ for (let i = 1; i < buf.length; i++) {
301
+ const mark = buf[i];
302
+ const base = buf[i - 1];
303
+ if (!(mark.uflags & markFlags) || base.uflags & markFlags)
304
+ continue;
305
+ const composed = [...(String.fromCodePoint(base.codepoint) + String.fromCodePoint(mark.codepoint)).normalize('NFC')];
306
+ if (composed.length !== 1)
307
+ continue;
308
+ const cp = composed[0].codePointAt(0);
309
+ if (!font.glyphId(cp))
310
+ continue;
311
+ base.codepoint = cp;
312
+ base.uflags = unicodeFlags(cp);
313
+ buf.splice(i, 1);
314
+ i--;
315
+ }
316
+ }
317
+ /** ZWJ, ZWNJ, bidi controls etc. become the (zero-width) space glyph after GSUB. */
318
+ function hideDefaultIgnorables(font, buf) {
319
+ const space = font.glyphId(0x20);
320
+ for (let i = 0; i < buf.length; i++) {
321
+ const info = buf[i];
322
+ if (!(info.uflags & UFlag.DefaultIgnorable) || info.props & GlyphProps.Substituted)
323
+ continue;
324
+ if (space)
325
+ info.glyph = space;
326
+ else {
327
+ deleteGlyph(buf, i);
328
+ i--;
329
+ }
330
+ }
331
+ }
332
+ function position(font, layout, plan, shaper, buf, direction) {
333
+ for (const info of buf) {
334
+ info.xAdvance = advanceOverrides.get(info) ?? font.advance(info.glyph);
335
+ info.yAdvance = 0;
336
+ info.xOffset = 0;
337
+ info.yOffset = 0;
338
+ info.attachType = 0;
339
+ info.attachChain = 0;
340
+ }
341
+ const gpos = layout.gpos;
342
+ if (gpos) {
343
+ const ctx = new ApplyContext(buf, layout.gdef, 1, direction, gpos.dispatch);
344
+ for (const stage of plan.gposStages) {
345
+ for (const l of stage.lookups) {
346
+ const lookup = gpos.lookup(l.index);
347
+ if (lookup)
348
+ ctx.applyLookup(l.index, lookup.props, l.mask, false);
349
+ }
350
+ }
351
+ }
352
+ const kernMask = plan.maskOf('kern');
353
+ if (kernMask && !plan.hasFeature(1, 'kern'))
354
+ legacyKern(font, buf, kernMask);
355
+ if (shaper.zeroMarks) {
356
+ // Without GPOS, pull a zeroed mark back over its base (LTR) instead of after it.
357
+ const adjust = !gpos && direction === 'ltr';
358
+ for (const info of buf) {
359
+ if (!isMark(info))
360
+ continue;
361
+ if (adjust)
362
+ info.xOffset -= info.xAdvance;
363
+ info.xAdvance = 0;
364
+ }
365
+ }
366
+ for (const info of buf) {
367
+ if (info.uflags & UFlag.DefaultIgnorable && !(info.props & GlyphProps.Substituted)) {
368
+ info.xAdvance = 0;
369
+ info.xOffset = 0;
370
+ info.yOffset = 0;
371
+ }
372
+ }
373
+ propagateAttachments(buf, direction);
374
+ }
375
+ /** Pair kerning from the legacy 'kern' table, between consecutive non-mark glyphs. */
376
+ function legacyKern(font, buf, mask) {
377
+ for (let i = 0; i < buf.length; i++) {
378
+ const a = buf[i];
379
+ if (!(a.mask & mask) || isMark(a))
380
+ continue;
381
+ let j = i + 1;
382
+ while (j < buf.length && (isMark(buf[j]) || buf[j].uflags & UFlag.DefaultIgnorable))
383
+ j++;
384
+ const b = buf[j];
385
+ if (!b || !(b.mask & mask))
386
+ continue;
387
+ a.xAdvance += font.kerning(a.glyph, b.glyph);
388
+ }
389
+ }
390
+ /** cmap + hmtx + 'kern', one glyph per character (the safety net). */
391
+ function shapeSimple(font, text) {
392
+ const out = [];
393
+ for (let i = 0; i < text.length;) {
394
+ const cp = text.codePointAt(i);
395
+ const glyph = font.glyphId(cp);
396
+ out.push({ glyph, cluster: i, advance: font.advance(glyph), offsetX: 0, offsetY: 0 });
397
+ i += cp > 0xffff ? 2 : 1;
398
+ }
399
+ for (let k = 0; k + 1 < out.length; k++)
400
+ out[k].advance += font.kerning(out[k].glyph, out[k + 1].glyph);
401
+ return out;
402
+ }
@@ -0,0 +1,6 @@
1
+ import type { Shaper } from './shaper.js';
2
+ /** [category, position] of a code point. */
3
+ export declare function indicProperties(cp: number): [number, number];
4
+ /** Splits categories into syllables: [start, end, type][], longest match first. */
5
+ export declare function findSyllables(cats: readonly number[]): Array<[number, number, number]>;
6
+ export declare const indicShaper: Shaper;