docxkeun 0.1.3 → 0.3.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 (199) hide show
  1. package/CHANGELOG.md +74 -0
  2. package/README.md +166 -16
  3. package/dist/api/editor.d.ts +231 -7
  4. package/dist/api/editor.js +1125 -26
  5. package/dist/api/headers.d.ts +51 -0
  6. package/dist/api/headers.js +133 -0
  7. package/dist/api/index.d.ts +6 -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 +401 -0
  15. package/dist/api/selection.d.ts +57 -0
  16. package/dist/api/selection.js +91 -0
  17. package/dist/api/tables.d.ts +60 -0
  18. package/dist/api/tables.js +138 -0
  19. package/dist/charts/color.d.ts +45 -0
  20. package/dist/charts/color.js +322 -0
  21. package/dist/charts/format.d.ts +12 -0
  22. package/dist/charts/format.js +459 -0
  23. package/dist/charts/index.d.ts +11 -0
  24. package/dist/charts/index.js +8 -0
  25. package/dist/charts/model.d.ts +225 -0
  26. package/dist/charts/model.js +758 -0
  27. package/dist/charts/outline.d.ts +17 -0
  28. package/dist/charts/outline.js +214 -0
  29. package/dist/charts/render.d.ts +20 -0
  30. package/dist/charts/render.js +1772 -0
  31. package/dist/drawing/arrows.d.ts +37 -0
  32. package/dist/drawing/arrows.js +99 -0
  33. package/dist/drawing/formula.d.ts +40 -0
  34. package/dist/drawing/formula.js +159 -0
  35. package/dist/drawing/geometry.d.ts +61 -0
  36. package/dist/drawing/geometry.js +286 -0
  37. package/dist/drawing/index.d.ts +12 -0
  38. package/dist/drawing/index.js +8 -0
  39. package/dist/drawing/presets.d.ts +72 -0
  40. package/dist/drawing/presets.js +2874 -0
  41. package/dist/fonts/shaping/apply.d.ts +94 -0
  42. package/dist/fonts/shaping/apply.js +349 -0
  43. package/dist/fonts/shaping/arabic.d.ts +28 -0
  44. package/dist/fonts/shaping/arabic.js +235 -0
  45. package/dist/fonts/shaping/buffer.d.ts +53 -0
  46. package/dist/fonts/shaping/buffer.js +95 -0
  47. package/dist/fonts/shaping/gdef.d.ts +30 -0
  48. package/dist/fonts/shaping/gdef.js +66 -0
  49. package/dist/fonts/shaping/gpos.d.ts +86 -0
  50. package/dist/fonts/shaping/gpos.js +428 -0
  51. package/dist/fonts/shaping/gsub.d.ts +65 -0
  52. package/dist/fonts/shaping/gsub.js +313 -0
  53. package/dist/fonts/shaping/index.d.ts +32 -0
  54. package/dist/fonts/shaping/index.js +402 -0
  55. package/dist/fonts/shaping/indic.d.ts +6 -0
  56. package/dist/fonts/shaping/indic.js +805 -0
  57. package/dist/fonts/shaping/layout-common.d.ts +124 -0
  58. package/dist/fonts/shaping/layout-common.js +349 -0
  59. package/dist/fonts/shaping/plan.d.ts +87 -0
  60. package/dist/fonts/shaping/plan.js +152 -0
  61. package/dist/fonts/shaping/scripts.d.ts +26 -0
  62. package/dist/fonts/shaping/scripts.js +160 -0
  63. package/dist/fonts/shaping/shaper.d.ts +23 -0
  64. package/dist/fonts/shaping/shaper.js +5 -0
  65. package/dist/fonts/shaping/unicode.d.ts +34 -0
  66. package/dist/fonts/shaping/unicode.js +138 -0
  67. package/dist/images/bmp.d.ts +9 -0
  68. package/dist/images/bmp.js +234 -0
  69. package/dist/images/gif.d.ts +9 -0
  70. package/dist/images/gif.js +215 -0
  71. package/dist/images/index.d.ts +18 -0
  72. package/dist/images/index.js +63 -0
  73. package/dist/images/png-encode.d.ts +4 -0
  74. package/dist/images/png-encode.js +73 -0
  75. package/dist/images/size.d.ts +10 -0
  76. package/dist/images/size.js +83 -0
  77. package/dist/images/tiff.d.ts +14 -0
  78. package/dist/images/tiff.js +407 -0
  79. package/dist/index.d.ts +7 -2
  80. package/dist/index.js +5 -0
  81. package/dist/layout/bidi.d.ts +18 -0
  82. package/dist/layout/bidi.js +71 -0
  83. package/dist/layout/blocks.d.ts +20 -3
  84. package/dist/layout/blocks.js +88 -58
  85. package/dist/layout/cache.d.ts +31 -0
  86. package/dist/layout/cache.js +38 -0
  87. package/dist/layout/drawing.d.ts +30 -0
  88. package/dist/layout/drawing.js +120 -0
  89. package/dist/layout/engine.d.ts +3 -0
  90. package/dist/layout/engine.js +218 -11
  91. package/dist/layout/index.d.ts +1 -0
  92. package/dist/layout/index.js +1 -0
  93. package/dist/layout/lines.d.ts +63 -5
  94. package/dist/layout/lines.js +245 -20
  95. package/dist/layout/paint.d.ts +25 -3
  96. package/dist/layout/paint.js +26 -10
  97. package/dist/layout/shapes.d.ts +7 -0
  98. package/dist/layout/shapes.js +338 -0
  99. package/dist/layout/style.d.ts +4 -0
  100. package/dist/layout/style.js +2 -0
  101. package/dist/layout/types.d.ts +89 -2
  102. package/dist/math/boxes.d.ts +88 -0
  103. package/dist/math/boxes.js +166 -0
  104. package/dist/math/index.d.ts +29 -0
  105. package/dist/math/index.js +44 -0
  106. package/dist/math/layout.d.ts +13 -0
  107. package/dist/math/layout.js +771 -0
  108. package/dist/math/linear.d.ts +3 -0
  109. package/dist/math/linear.js +152 -0
  110. package/dist/math/omml.d.ts +15 -0
  111. package/dist/math/omml.js +57 -0
  112. package/dist/math/paths.d.ts +61 -0
  113. package/dist/math/paths.js +450 -0
  114. package/dist/math/symbols.d.ts +49 -0
  115. package/dist/math/symbols.js +269 -0
  116. package/dist/math/text.d.ts +53 -0
  117. package/dist/math/text.js +316 -0
  118. package/dist/metafile/canvas.d.ts +80 -0
  119. package/dist/metafile/canvas.js +515 -0
  120. package/dist/metafile/dib.d.ts +27 -0
  121. package/dist/metafile/dib.js +300 -0
  122. package/dist/metafile/emf.d.ts +31 -0
  123. package/dist/metafile/emf.js +591 -0
  124. package/dist/metafile/geometry.d.ts +49 -0
  125. package/dist/metafile/geometry.js +85 -0
  126. package/dist/metafile/index.d.ts +27 -0
  127. package/dist/metafile/index.js +34 -0
  128. package/dist/metafile/reader.d.ts +28 -0
  129. package/dist/metafile/reader.js +59 -0
  130. package/dist/metafile/state.d.ts +118 -0
  131. package/dist/metafile/state.js +172 -0
  132. package/dist/metafile/text.d.ts +17 -0
  133. package/dist/metafile/text.js +130 -0
  134. package/dist/metafile/wmf.d.ts +33 -0
  135. package/dist/metafile/wmf.js +395 -0
  136. package/dist/model/content.d.ts +92 -7
  137. package/dist/model/content.js +251 -17
  138. package/dist/model/document.d.ts +34 -1
  139. package/dist/model/document.js +80 -1
  140. package/dist/model/fields.d.ts +4 -0
  141. package/dist/model/fields.js +10 -0
  142. package/dist/model/numbering.d.ts +2 -0
  143. package/dist/model/numbering.js +22 -0
  144. package/dist/model/props.d.ts +2 -0
  145. package/dist/model/props.js +3 -0
  146. package/dist/opc/index.d.ts +1 -1
  147. package/dist/opc/package.d.ts +34 -0
  148. package/dist/opc/package.js +58 -4
  149. package/dist/pdf/export.js +97 -6
  150. package/dist/pdf/images.d.ts +7 -0
  151. package/dist/pdf/images.js +39 -2
  152. package/dist/render/activity.d.ts +179 -0
  153. package/dist/render/activity.js +1003 -0
  154. package/dist/render/clipboard.d.ts +29 -0
  155. package/dist/render/clipboard.js +401 -0
  156. package/dist/render/comments.d.ts +166 -0
  157. package/dist/render/comments.js +779 -0
  158. package/dist/render/context-menu.d.ts +124 -0
  159. package/dist/render/context-menu.js +525 -0
  160. package/dist/render/editing.d.ts +146 -5
  161. package/dist/render/editing.js +678 -53
  162. package/dist/render/index.d.ts +12 -0
  163. package/dist/render/index.js +11 -0
  164. package/dist/render/navigation-panel.d.ts +76 -0
  165. package/dist/render/navigation-panel.js +431 -0
  166. package/dist/render/selection.js +5 -0
  167. package/dist/render/svg.d.ts +4 -6
  168. package/dist/render/svg.js +69 -7
  169. package/dist/render/toolbar.d.ts +83 -0
  170. package/dist/render/toolbar.js +505 -0
  171. package/dist/render/ui/comment-layout.d.ts +39 -0
  172. package/dist/render/ui/comment-layout.js +79 -0
  173. package/dist/render/ui/icons.d.ts +6 -0
  174. package/dist/render/ui/icons.js +38 -0
  175. package/dist/render/ui/menu.d.ts +38 -0
  176. package/dist/render/ui/menu.js +274 -0
  177. package/dist/render/ui/nav-model.d.ts +11 -0
  178. package/dist/render/ui/nav-model.js +34 -0
  179. package/dist/render/ui/overflow.d.ts +5 -0
  180. package/dist/render/ui/overflow.js +18 -0
  181. package/dist/render/ui/popover.d.ts +18 -0
  182. package/dist/render/ui/popover.js +109 -0
  183. package/dist/render/ui/theme.d.ts +2 -0
  184. package/dist/render/ui/theme.js +29 -0
  185. package/dist/render/undo.d.ts +76 -0
  186. package/dist/render/undo.js +144 -0
  187. package/dist/render/viewer.d.ts +100 -7
  188. package/dist/render/viewer.js +304 -92
  189. package/dist/security/policy.d.ts +3 -0
  190. package/dist/security/policy.js +6 -2
  191. package/dist/security/protection.d.ts +1 -0
  192. package/dist/security/protection.js +1 -1
  193. package/dist/text/bidi-data.d.ts +53 -0
  194. package/dist/text/bidi-data.js +318 -0
  195. package/dist/text/bidi.d.ts +37 -0
  196. package/dist/text/bidi.js +508 -0
  197. package/dist/xml/ns.d.ts +5 -0
  198. package/dist/xml/ns.js +7 -0
  199. package/package.json +9 -2
@@ -0,0 +1,235 @@
1
+ /**
2
+ * Arabic-style shaper (Arabic, Syriac, N'Ko, Mongolian): joining analysis picks each letter's
3
+ * contextual form (isol / fina / medi / init, plus Syriac fin2 / fin3 / med2 after Alaph),
4
+ * and those forms are applied as per-glyph features one stage at a time before rlig (where
5
+ * fonts form lam-alef), calt, liga and mset. Positioning then runs curs (cursive joining,
6
+ * e.g. Nastaliq), kern, mark and mkmk.
7
+ *
8
+ * Joining types are from Unicode's ArabicShaping.txt for the Arabic, Arabic Supplement,
9
+ * Arabic Extended-A/B, Syriac, N'Ko and Mongolian blocks. Characters not listed are
10
+ * non-joining, except marks and format controls, which are transparent.
11
+ *
12
+ * For fonts with no Arabic GSUB forms at all, a fallback maps letters to their Unicode
13
+ * presentation forms (U+FB50..U+FEFC) when the font has those glyphs.
14
+ */
15
+ import { deleteGlyph, isLigated, mergeClusters } from './buffer.js';
16
+ import { GlyphProps } from './gdef.js';
17
+ import { UFlag } from './unicode.js';
18
+ const JT_U = 0;
19
+ const JT_L = 1;
20
+ const JT_R = 2;
21
+ const JT_D = 3;
22
+ const JG_ALAPH = 4;
23
+ const JG_DALATH_RISH = 5;
24
+ const JT_T = 7;
25
+ /** "range type" entries; C (join-causing) behaves as D. */
26
+ const JOINING_DATA = `
27
+ 0600-0605 U;0620 D;0621 U;0622-0625 R;0626 D;0627 R;0628 D;0629 R;062A-062E D;062F-0632 R;0633-063F D;0640 D;
28
+ 0641-0647 D;0648 R;0649-064A D;066E-066F D;0671-0673 R;0674 U;0675-0677 R;0678-0687 D;0688-0699 R;069A-06BF D;
29
+ 06C0 R;06C1-06C2 D;06C3-06CB R;06CC D;06CD R;06CE D;06CF R;06D0-06D1 D;06D2-06D3 R;06D5 R;06DD U;06EE-06EF R;
30
+ 06FA-06FC D;06FF D;
31
+ 0710 A;0712-0714 D;0715-0716 S;0717-0719 R;071A-071D D;071E R;071F-0727 D;0728 R;0729 D;072A S;072B D;072C R;
32
+ 072D-072E D;072F S;074D R;074E-074F D;
33
+ 0750-0758 D;0759-075B R;075C-076A D;076B-076C R;076D-0770 D;0771 R;0772 D;0773-0774 R;0775-0777 D;0778-077B R;
34
+ 077C-077F D;
35
+ 07CA-07EA D;07FA D;
36
+ 0860 D;0862-0865 D;0867 R;0868 D;0869-086A R;
37
+ 0870-0882 R;0883-0885 D;0886 D;0889-088D D;088E R;0890-0891 U;
38
+ 08A0-08A9 D;08AA-08AC R;08AD U;08AE R;08AF-08B0 D;08B1-08B2 R;08B3-08B8 D;08B9 R;08BA-08C8 D;08E2 U;
39
+ 1807 D;180A D;1820-1878 D;1887-18A8 D;18AA D;
40
+ 200C U;200D D;202F U
41
+ `;
42
+ const JOINING = new Map();
43
+ {
44
+ const codes = { U: JT_U, L: JT_L, R: JT_R, D: JT_D, A: JG_ALAPH, S: JG_DALATH_RISH };
45
+ for (const entry of JOINING_DATA.split(/[;\n]/)) {
46
+ const m = /^\s*([0-9A-F]{4,5})(?:-([0-9A-F]{4,5}))?\s+([ULRDAS])\s*$/.exec(entry);
47
+ if (!m)
48
+ continue;
49
+ const first = parseInt(m[1], 16);
50
+ const last = m[2] ? parseInt(m[2], 16) : first;
51
+ for (let cp = first; cp <= last; cp++)
52
+ JOINING.set(cp, codes[m[3]]);
53
+ }
54
+ }
55
+ function joiningType(info) {
56
+ const jt = JOINING.get(info.codepoint);
57
+ if (jt !== undefined)
58
+ return jt;
59
+ return info.uflags & (UFlag.NonSpacingMark | UFlag.EnclosingMark | UFlag.Format) ? JT_T : JT_U;
60
+ }
61
+ export const ISOL = 0;
62
+ export const FINA = 1;
63
+ export const FIN2 = 2;
64
+ export const FIN3 = 3;
65
+ export const MEDI = 4;
66
+ export const MED2 = 5;
67
+ export const INIT = 6;
68
+ export const NONE = 7;
69
+ export const ARABIC_FEATURES = ['isol', 'fina', 'fin2', 'fin3', 'medi', 'med2', 'init'];
70
+ /**
71
+ * Joining state machine (HarfBuzz's, after the Unicode joining rules): for each state and
72
+ * joining type, the action for the previous letter, the action for this one, the next state.
73
+ * Columns: U, L, R, D, Alaph, Dalath/Rish.
74
+ */
75
+ const STATE_TABLE = [
76
+ // 0: prev was U, not willing to join.
77
+ [[NONE, NONE, 0], [NONE, ISOL, 2], [NONE, ISOL, 1], [NONE, ISOL, 2], [NONE, ISOL, 1], [NONE, ISOL, 6]],
78
+ // 1: prev was R or ISOL/ALAPH, not willing to join.
79
+ [[NONE, NONE, 0], [NONE, ISOL, 2], [NONE, ISOL, 1], [NONE, ISOL, 2], [NONE, FIN2, 5], [NONE, ISOL, 6]],
80
+ // 2: prev was D/L in ISOL form, willing to join.
81
+ [[NONE, NONE, 0], [NONE, ISOL, 2], [INIT, FINA, 1], [INIT, FINA, 3], [INIT, FINA, 4], [INIT, FINA, 6]],
82
+ // 3: prev was D in FINA form, willing to join.
83
+ [[NONE, NONE, 0], [NONE, ISOL, 2], [MEDI, FINA, 1], [MEDI, FINA, 3], [MEDI, FINA, 4], [MEDI, FINA, 6]],
84
+ // 4: prev was FINA ALAPH, not willing to join.
85
+ [[NONE, NONE, 0], [NONE, ISOL, 2], [MED2, ISOL, 1], [MED2, ISOL, 2], [MED2, FIN2, 5], [MED2, ISOL, 6]],
86
+ // 5: prev was FIN2/FIN3 ALAPH, not willing to join.
87
+ [[NONE, NONE, 0], [NONE, ISOL, 2], [ISOL, ISOL, 1], [ISOL, ISOL, 2], [ISOL, FIN2, 5], [ISOL, ISOL, 6]],
88
+ // 6: prev was DALATH/RISH, not willing to join.
89
+ [[NONE, NONE, 0], [NONE, ISOL, 2], [NONE, ISOL, 1], [NONE, ISOL, 2], [NONE, FIN3, 5], [NONE, ISOL, 6]],
90
+ ];
91
+ /** Stores each glyph's joining action (ISOL..INIT or NONE) in `info.cat`. */
92
+ export function arabicJoining(buf) {
93
+ let prev = -1;
94
+ let state = 0;
95
+ for (let i = 0; i < buf.length; i++) {
96
+ const info = buf[i];
97
+ const type = joiningType(info);
98
+ if (type === JT_T) {
99
+ info.cat = NONE;
100
+ continue;
101
+ }
102
+ const [prevAction, curAction, next] = STATE_TABLE[state][type];
103
+ if (prevAction !== NONE && prev >= 0)
104
+ buf[prev].cat = prevAction;
105
+ info.cat = curAction;
106
+ prev = i;
107
+ state = next;
108
+ }
109
+ }
110
+ function setupMasks(ctx) {
111
+ const buf = ctx.buffer;
112
+ arabicJoining(buf);
113
+ if (ctx.plan.script === 'mong') {
114
+ // Mongolian free variation selectors take the form of the letter they follow.
115
+ for (let i = 1; i < buf.length; i++) {
116
+ const cp = buf[i].codepoint;
117
+ if ((cp >= 0x180b && cp <= 0x180d) || cp === 0x180f)
118
+ buf[i].cat = buf[i - 1].cat;
119
+ }
120
+ }
121
+ const masks = ARABIC_FEATURES.map((tag) => ctx.plan.maskOf(tag));
122
+ for (const info of buf)
123
+ if (info.cat !== NONE)
124
+ info.mask |= masks[info.cat] ?? 0;
125
+ }
126
+ // --- Presentation-form fallback --------------------------------------------------------
127
+ /** Code point → [isol, fina, init, medi] presentation forms (0 where the form doesn't exist). */
128
+ const PRESENTATION = new Map();
129
+ {
130
+ // Presentation Forms-B lists U+0621..U+064A in order, with 1, 2 or 4 forms per letter.
131
+ let form = 0xfe80;
132
+ const letters = [];
133
+ for (let cp = 0x0621; cp <= 0x063a; cp++)
134
+ letters.push(cp);
135
+ for (let cp = 0x0641; cp <= 0x064a; cp++)
136
+ letters.push(cp);
137
+ for (const cp of letters) {
138
+ const jt = cp === 0x0649 ? JT_R : (JOINING.get(cp) ?? JT_U); // alef maksura has only isol/fina here
139
+ if (jt === JT_U)
140
+ PRESENTATION.set(cp, [form++, 0, 0, 0]);
141
+ else if (jt === JT_R) {
142
+ PRESENTATION.set(cp, [form, form + 1, 0, 0]);
143
+ form += 2;
144
+ }
145
+ else {
146
+ PRESENTATION.set(cp, [form, form + 1, form + 2, form + 3]);
147
+ form += 4;
148
+ }
149
+ }
150
+ // Common Persian / Urdu letters from Presentation Forms-A.
151
+ const extra = [
152
+ [0x0679, 0xfb66, 4], [0x067e, 0xfb56, 4], [0x0686, 0xfb7a, 4], [0x0688, 0xfb88, 2], [0x0691, 0xfb8c, 2],
153
+ [0x0698, 0xfb8a, 2], [0x06a9, 0xfb8e, 4], [0x06af, 0xfb92, 4], [0x06ba, 0xfb9e, 2], [0x06be, 0xfbaa, 4],
154
+ [0x06c1, 0xfba6, 4], [0x06cc, 0xfbfc, 4], [0x06d2, 0xfbae, 2], [0x06d3, 0xfbb0, 2],
155
+ ];
156
+ for (const [cp, base, n] of extra)
157
+ PRESENTATION.set(cp, n === 4 ? [base, base + 1, base + 2, base + 3] : [base, base + 1, 0, 0]);
158
+ }
159
+ /** Lam + alef variant → [isolated, final] lam-alef ligature. */
160
+ const LAM_ALEF = new Map([
161
+ [0x0622, [0xfef5, 0xfef6]],
162
+ [0x0623, [0xfef7, 0xfef8]],
163
+ [0x0625, [0xfef9, 0xfefa]],
164
+ [0x0627, [0xfefb, 0xfefc]],
165
+ ]);
166
+ /**
167
+ * Shapes Arabic with the font's presentation-form glyphs when its GSUB has none of the
168
+ * joining features (older fonts, or AAT-only fonts whose cmap still covers U+FExx).
169
+ */
170
+ function fallbackShape(ctx) {
171
+ const plan = ctx.plan;
172
+ if (['isol', 'fina', 'medi', 'init'].some((tag) => plan.hasFeature(0, tag)))
173
+ return;
174
+ const buf = ctx.buffer;
175
+ const font = ctx.font;
176
+ const rlig = plan.maskOf('rlig');
177
+ for (let i = 0; i < buf.length; i++) {
178
+ const lam = buf[i];
179
+ if (lam.codepoint !== 0x0644 || (lam.cat !== INIT && lam.cat !== MEDI) || !(lam.mask & rlig) || isLigated(lam))
180
+ continue;
181
+ let j = i + 1;
182
+ while (j < buf.length && buf[j].props & GlyphProps.Mark)
183
+ j++;
184
+ const alef = buf[j];
185
+ const forms = alef && alef.cat === FINA ? LAM_ALEF.get(alef.codepoint) : undefined;
186
+ if (!forms)
187
+ continue;
188
+ const glyph = font.glyphId(lam.cat === INIT ? forms[0] : forms[1]);
189
+ if (!glyph)
190
+ continue;
191
+ mergeClusters(buf, i, j + 1);
192
+ lam.glyph = glyph;
193
+ lam.props = (lam.props & ~GlyphProps.Mark) | GlyphProps.Substituted | GlyphProps.Ligated | GlyphProps.Ligature;
194
+ lam.cat = lam.cat === INIT ? ISOL : FINA;
195
+ deleteGlyph(buf, j);
196
+ }
197
+ const masks = ARABIC_FEATURES.map((tag) => plan.maskOf(tag));
198
+ const slot = [0, 1, -1, -1, 3, -1, 2]; // action → index into [isol, fina, init, medi]
199
+ for (const info of buf) {
200
+ if (info.cat === NONE || isLigated(info) || !(info.mask & (masks[info.cat] ?? 0)))
201
+ continue;
202
+ const forms = PRESENTATION.get(info.codepoint);
203
+ const s = slot[info.cat] ?? -1;
204
+ const cp = forms && s >= 0 ? forms[s] : 0;
205
+ const glyph = cp ? font.glyphId(cp) : 0;
206
+ if (glyph) {
207
+ info.glyph = glyph;
208
+ info.props |= GlyphProps.Substituted;
209
+ }
210
+ }
211
+ }
212
+ function collectFeatures(b, script) {
213
+ b.enable('ccmp', { manualZwj: true });
214
+ b.enable('locl', { manualZwj: true });
215
+ b.pauseGsub();
216
+ for (const tag of ARABIC_FEATURES) {
217
+ // The Syriac-only forms have no presentation-form fallback.
218
+ b.add(tag, { fallback: script === 'arab' && !tag.endsWith('2') && !tag.endsWith('3') });
219
+ b.pauseGsub();
220
+ }
221
+ b.enable('rlig', { manualZwj: true, fallback: true });
222
+ if (script === 'arab')
223
+ b.pauseGsub(fallbackShape);
224
+ b.enable('rclt', { manualZwj: true });
225
+ b.enable('calt', { manualZwj: true });
226
+ b.pauseGsub();
227
+ b.enable('mset');
228
+ }
229
+ export const arabicShaper = {
230
+ name: 'arabic',
231
+ collectFeatures,
232
+ setupMasks,
233
+ zeroMarks: true,
234
+ compose: true,
235
+ };
@@ -0,0 +1,53 @@
1
+ export interface GlyphInfo {
2
+ glyph: number;
3
+ /** Unicode code point the glyph came from (the first one, for ligatures). */
4
+ codepoint: number;
5
+ /** UTF-16 index of the first character of the cluster. */
6
+ cluster: number;
7
+ /** Feature bits enabled on this glyph. */
8
+ mask: number;
9
+ /** GlyphProps bits (class, substitution history, mark attachment class). */
10
+ props: number;
11
+ /** UFlag bits of the source character. */
12
+ uflags: number;
13
+ /** Ligature id shared by a ligature and the marks attached to its components (0 = none). */
14
+ ligId: number;
15
+ /** Component index for marks/components, or the component count on a ligature base. */
16
+ ligComp: number;
17
+ isLigBase: boolean;
18
+ /** Syllable serial (Indic); 0 means no per-syllable restriction. */
19
+ syllable: number;
20
+ /** Shaper-private: Indic category, Arabic joining action. */
21
+ cat: number;
22
+ /** Shaper-private: Indic position. */
23
+ pos: number;
24
+ xAdvance: number;
25
+ yAdvance: number;
26
+ xOffset: number;
27
+ yOffset: number;
28
+ /** Attachment kind (ATTACH_MARK / ATTACH_CURSIVE) and the relative index of the parent. */
29
+ attachType: number;
30
+ attachChain: number;
31
+ }
32
+ export declare const ATTACH_MARK = 1;
33
+ export declare const ATTACH_CURSIVE = 2;
34
+ export declare function newGlyphInfo(codepoint: number, cluster: number, uflags: number): GlyphInfo;
35
+ export declare function cloneInfo(info: GlyphInfo): GlyphInfo;
36
+ export declare const isMark: (info: GlyphInfo) => boolean;
37
+ export declare const isBaseGlyph: (info: GlyphInfo) => boolean;
38
+ export declare const isLigature: (info: GlyphInfo) => boolean;
39
+ export declare const isSubstituted: (info: GlyphInfo) => boolean;
40
+ export declare const isLigated: (info: GlyphInfo) => boolean;
41
+ export declare const isMultiplied: (info: GlyphInfo) => boolean;
42
+ export declare const ligatedAndDidntMultiply: (info: GlyphInfo) => boolean;
43
+ export declare function ligComp(info: GlyphInfo): number;
44
+ export declare function ligNumComps(info: GlyphInfo): number;
45
+ export declare function setLigPropsForLigature(info: GlyphInfo, ligId: number, numComps: number): void;
46
+ export declare function setLigPropsForMark(info: GlyphInfo, ligId: number, comp: number): void;
47
+ /**
48
+ * Gives every glyph in [start, end) the smallest cluster value among them, widening the range
49
+ * over neighbours that already share an edge cluster so clusters stay contiguous.
50
+ */
51
+ export declare function mergeClusters(buf: GlyphInfo[], start: number, end: number): void;
52
+ /** Removes glyph `i`, folding its cluster into a neighbour when it was the last of it. */
53
+ export declare function deleteGlyph(buf: GlyphInfo[], i: number): void;
@@ -0,0 +1,95 @@
1
+ /**
2
+ * The shaping buffer: one record per glyph, holding the glyph id, the source character and
3
+ * cluster, feature mask, GDEF-derived properties, ligature-component bookkeeping (so marks
4
+ * can attach to the right component of a ligature) and, once positioning starts, the
5
+ * advance/offset/attachment fields. Substitutions mutate the array in place.
6
+ */
7
+ import { GlyphProps } from './gdef.js';
8
+ export const ATTACH_MARK = 1;
9
+ export const ATTACH_CURSIVE = 2;
10
+ export function newGlyphInfo(codepoint, cluster, uflags) {
11
+ return {
12
+ glyph: 0,
13
+ codepoint,
14
+ cluster,
15
+ mask: 0,
16
+ props: 0,
17
+ uflags,
18
+ ligId: 0,
19
+ ligComp: 0,
20
+ isLigBase: false,
21
+ syllable: 0,
22
+ cat: 0,
23
+ pos: 0,
24
+ xAdvance: 0,
25
+ yAdvance: 0,
26
+ xOffset: 0,
27
+ yOffset: 0,
28
+ attachType: 0,
29
+ attachChain: 0,
30
+ };
31
+ }
32
+ export function cloneInfo(info) {
33
+ return { ...info };
34
+ }
35
+ export const isMark = (info) => (info.props & GlyphProps.Mark) !== 0;
36
+ export const isBaseGlyph = (info) => (info.props & GlyphProps.BaseGlyph) !== 0;
37
+ export const isLigature = (info) => (info.props & GlyphProps.Ligature) !== 0;
38
+ export const isSubstituted = (info) => (info.props & GlyphProps.Substituted) !== 0;
39
+ export const isLigated = (info) => (info.props & GlyphProps.Ligated) !== 0;
40
+ export const isMultiplied = (info) => (info.props & GlyphProps.Multiplied) !== 0;
41
+ export const ligatedAndDidntMultiply = (info) => isLigated(info) && !isMultiplied(info);
42
+ export function ligComp(info) {
43
+ return info.isLigBase ? 0 : info.ligComp;
44
+ }
45
+ export function ligNumComps(info) {
46
+ return info.isLigBase && isLigature(info) ? info.ligComp : 1;
47
+ }
48
+ export function setLigPropsForLigature(info, ligId, numComps) {
49
+ info.ligId = ligId;
50
+ info.isLigBase = true;
51
+ info.ligComp = numComps & 0x0f;
52
+ }
53
+ export function setLigPropsForMark(info, ligId, comp) {
54
+ info.ligId = ligId;
55
+ info.isLigBase = false;
56
+ info.ligComp = comp & 0x0f;
57
+ }
58
+ /**
59
+ * Gives every glyph in [start, end) the smallest cluster value among them, widening the range
60
+ * over neighbours that already share an edge cluster so clusters stay contiguous.
61
+ */
62
+ export function mergeClusters(buf, start, end) {
63
+ if (end - start < 2)
64
+ return;
65
+ let cluster = buf[start].cluster;
66
+ for (let i = start + 1; i < end; i++)
67
+ cluster = Math.min(cluster, buf[i].cluster);
68
+ if (cluster !== buf[end - 1].cluster)
69
+ while (end < buf.length && buf[end - 1].cluster === buf[end].cluster)
70
+ end++;
71
+ if (cluster !== buf[start].cluster)
72
+ while (start > 0 && buf[start - 1].cluster === buf[start].cluster)
73
+ start--;
74
+ for (let i = start; i < end; i++)
75
+ buf[i].cluster = cluster;
76
+ }
77
+ /** Removes glyph `i`, folding its cluster into a neighbour when it was the last of it. */
78
+ export function deleteGlyph(buf, i) {
79
+ const cluster = buf[i].cluster;
80
+ const next = buf[i + 1];
81
+ const prev = buf[i - 1];
82
+ if (!((next && next.cluster === cluster) || (prev && prev.cluster === cluster))) {
83
+ if (prev) {
84
+ if (cluster < prev.cluster) {
85
+ const old = prev.cluster;
86
+ for (let j = i - 1; j >= 0 && buf[j].cluster === old; j--)
87
+ buf[j].cluster = cluster;
88
+ }
89
+ }
90
+ else if (next) {
91
+ mergeClusters(buf, i, i + 2);
92
+ }
93
+ }
94
+ buf.splice(i, 1);
95
+ }
@@ -0,0 +1,30 @@
1
+ /**
2
+ * GDEF: glyph classes (base / ligature / mark / component), mark attachment classes and
3
+ * mark glyph sets. Lookup flags filter glyphs through these, so they decide which glyphs a
4
+ * ligature or kerning lookup "sees" past (e.g. a kern pair that skips an accent).
5
+ */
6
+ import { type ClassDef } from './layout-common.js';
7
+ /** Glyph property bits, aligned with the LookupFlag Ignore* bits so one AND tests both. */
8
+ export declare const GlyphProps: {
9
+ readonly BaseGlyph: 2;
10
+ readonly Ligature: 4;
11
+ readonly Mark: 8;
12
+ /** History bits kept across substitutions (Indic final reordering inspects them). */
13
+ readonly Substituted: 16;
14
+ readonly Ligated: 32;
15
+ readonly Multiplied: 64;
16
+ readonly Preserve: 112;
17
+ /** The mark attachment class lives in the high byte, like LookupFlag.MarkAttachmentType. */
18
+ readonly MarkAttachClass: 65280;
19
+ };
20
+ export declare class Gdef {
21
+ readonly glyphClasses: ClassDef | undefined;
22
+ private readonly markAttachClasses;
23
+ private readonly markSets;
24
+ constructor(bytes: Uint8Array | undefined);
25
+ /** True when the font classifies glyphs itself (otherwise classes come from Unicode). */
26
+ get hasGlyphClasses(): boolean;
27
+ /** Glyph property bits for a glyph (0 for unclassified / component glyphs). */
28
+ glyphProps(glyph: number): number;
29
+ markSetCovers(set: number, glyph: number): boolean;
30
+ }
@@ -0,0 +1,66 @@
1
+ /**
2
+ * GDEF: glyph classes (base / ligature / mark / component), mark attachment classes and
3
+ * mark glyph sets. Lookup flags filter glyphs through these, so they decide which glyphs a
4
+ * ligature or kerning lookup "sees" past (e.g. a kern pair that skips an accent).
5
+ */
6
+ import { BinaryData, parseClassDef, parseCoverage } from './layout-common.js';
7
+ /** Glyph property bits, aligned with the LookupFlag Ignore* bits so one AND tests both. */
8
+ export const GlyphProps = {
9
+ BaseGlyph: 0x02,
10
+ Ligature: 0x04,
11
+ Mark: 0x08,
12
+ /** History bits kept across substitutions (Indic final reordering inspects them). */
13
+ Substituted: 0x10,
14
+ Ligated: 0x20,
15
+ Multiplied: 0x40,
16
+ Preserve: 0x70,
17
+ /** The mark attachment class lives in the high byte, like LookupFlag.MarkAttachmentType. */
18
+ MarkAttachClass: 0xff00,
19
+ };
20
+ export class Gdef {
21
+ glyphClasses;
22
+ markAttachClasses;
23
+ markSets = [];
24
+ constructor(bytes) {
25
+ if (!bytes || bytes.length < 12)
26
+ return;
27
+ const d = new BinaryData(bytes);
28
+ const minor = d.u16(2);
29
+ const classDef = d.u16(4);
30
+ const markAttach = d.u16(10);
31
+ if (classDef)
32
+ this.glyphClasses = parseClassDef(d, classDef);
33
+ if (markAttach)
34
+ this.markAttachClasses = parseClassDef(d, markAttach);
35
+ if (minor >= 2 && bytes.length >= 14) {
36
+ const sets = d.u16(12);
37
+ if (sets && d.u16(sets) === 1) {
38
+ const count = d.u16(sets + 2);
39
+ for (let i = 0; i < count; i++) {
40
+ const rel = d.u32(sets + 4 + i * 4);
41
+ this.markSets.push(rel ? parseCoverage(d, sets + rel) : new Map());
42
+ }
43
+ }
44
+ }
45
+ }
46
+ /** True when the font classifies glyphs itself (otherwise classes come from Unicode). */
47
+ get hasGlyphClasses() {
48
+ return this.glyphClasses !== undefined && this.glyphClasses.size > 0;
49
+ }
50
+ /** Glyph property bits for a glyph (0 for unclassified / component glyphs). */
51
+ glyphProps(glyph) {
52
+ switch (this.glyphClasses?.get(glyph) ?? 0) {
53
+ case 1:
54
+ return GlyphProps.BaseGlyph;
55
+ case 2:
56
+ return GlyphProps.Ligature;
57
+ case 3:
58
+ return GlyphProps.Mark | ((this.markAttachClasses?.get(glyph) ?? 0) << 8);
59
+ default:
60
+ return 0;
61
+ }
62
+ }
63
+ markSetCovers(set, glyph) {
64
+ return this.markSets[set]?.has(glyph) ?? false;
65
+ }
66
+ }
@@ -0,0 +1,86 @@
1
+ /**
2
+ * GPOS: glyph positioning lookups. Types 1 single, 2 pair (formats 1 and 2: kerning),
3
+ * 3 cursive attachment, 4 mark-to-base, 5 mark-to-ligature, 6 mark-to-mark, 7 context,
4
+ * 8 chaining context, 9 extension. Device tables, variation deltas and anchor contour points
5
+ * are ignored: layout works in font units, unhinted.
6
+ *
7
+ * Attachments are recorded as (type, relative parent index) and resolved into offsets after
8
+ * all lookups ran, so a mark on a mark on a base picks up every adjustment of its chain.
9
+ */
10
+ import { type LookupDispatcher } from './apply.js';
11
+ import { type GlyphInfo } from './buffer.js';
12
+ import { LayoutTable, type ClassDef, type ContextualSubtable, type Coverage } from './layout-common.js';
13
+ export interface Anchor {
14
+ x: number;
15
+ y: number;
16
+ }
17
+ interface MarkRecord {
18
+ cls: number;
19
+ anchor: Anchor | undefined;
20
+ }
21
+ export type GposSubtable = {
22
+ kind: 'single';
23
+ coverage: Coverage;
24
+ valueFormat: number;
25
+ valueAt: (index: number) => number;
26
+ } | {
27
+ kind: 'pair1';
28
+ coverage: Coverage;
29
+ vf1: number;
30
+ vf2: number;
31
+ pairSet: (index: number) => Map<number, number> | undefined;
32
+ } | {
33
+ kind: 'pair2';
34
+ coverage: Coverage;
35
+ vf1: number;
36
+ vf2: number;
37
+ classDef1: ClassDef;
38
+ classDef2: ClassDef;
39
+ class1Count: number;
40
+ class2Count: number;
41
+ recordsAt: number;
42
+ } | {
43
+ kind: 'cursive';
44
+ coverage: Coverage;
45
+ records: Array<{
46
+ entry: Anchor | undefined;
47
+ exit: Anchor | undefined;
48
+ }>;
49
+ } | {
50
+ kind: 'markBase';
51
+ markCoverage: Coverage;
52
+ baseCoverage: Coverage;
53
+ marks: MarkRecord[];
54
+ bases: Array<Array<Anchor | undefined>>;
55
+ } | {
56
+ kind: 'markLig';
57
+ markCoverage: Coverage;
58
+ ligCoverage: Coverage;
59
+ marks: MarkRecord[];
60
+ ligatures: Array<Array<Array<Anchor | undefined>>>;
61
+ } | {
62
+ kind: 'markMark';
63
+ markCoverage: Coverage;
64
+ mark2Coverage: Coverage;
65
+ marks: MarkRecord[];
66
+ mark2s: Array<Array<Anchor | undefined>>;
67
+ } | ContextualSubtable;
68
+ export interface GposLookup {
69
+ type: number;
70
+ props: number;
71
+ subtables: GposSubtable[];
72
+ }
73
+ export declare class Gpos {
74
+ readonly layout: LayoutTable;
75
+ private readonly lookups;
76
+ readonly dispatch: LookupDispatcher;
77
+ constructor(bytes: Uint8Array);
78
+ lookup(index: number): GposLookup | undefined;
79
+ }
80
+ /**
81
+ * Turns attachment chains into final offsets: a mark lands at its base's offset minus the
82
+ * advances between them (in logical order; for RTL the run is later drawn reversed, so the
83
+ * glyphs after the base, up to and including the mark, are the ones in between).
84
+ */
85
+ export declare function propagateAttachments(buf: GlyphInfo[], direction: 'ltr' | 'rtl'): void;
86
+ export {};