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,124 @@
1
+ /**
2
+ * OpenType Layout common table formats (shared by GSUB and GPOS): ScriptList / LangSys,
3
+ * FeatureList, LookupList, Coverage, ClassDef and the contextual (5/6 in GSUB, 7/8 in GPOS)
4
+ * subtables. Everything is parsed lazily and cached per table, because a shaping call only
5
+ * touches the few lookups its features select, and fonts like Noto Nastaliq carry hundreds.
6
+ *
7
+ * Reads are bounds-checked and return 0 past the end: a malformed offset then reads as an
8
+ * empty structure instead of throwing in the middle of layout.
9
+ */
10
+ /** Big-endian reader over one table's bytes, addressed by absolute offset into the table. */
11
+ export declare class BinaryData {
12
+ readonly bytes: Uint8Array;
13
+ readonly view: DataView;
14
+ readonly length: number;
15
+ /** Coverage and ClassDef tables are often shared between subtables: parse each once. */
16
+ readonly coverageCache: Map<number, Coverage>;
17
+ readonly classDefCache: Map<number, ClassDef>;
18
+ constructor(bytes: Uint8Array);
19
+ u16(off: number): number;
20
+ i16(off: number): number;
21
+ u32(off: number): number;
22
+ tag(off: number): string;
23
+ /** u16 array of `count` items at `off` (clamped to the table). */
24
+ u16s(off: number, count: number): number[];
25
+ }
26
+ /** Glyph id → coverage index. */
27
+ export type Coverage = Map<number, number>;
28
+ /** Glyph id → class (absent means class 0). */
29
+ export type ClassDef = Map<number, number>;
30
+ export declare function parseCoverage(d: BinaryData, off: number): Coverage;
31
+ export declare function parseClassDef(d: BinaryData, off: number): ClassDef;
32
+ export declare function classOf(cd: ClassDef, glyph: number): number;
33
+ /** Lookup flags (OpenType LookupFlag bit enumeration). */
34
+ export declare const LookupFlag: {
35
+ readonly RightToLeft: 1;
36
+ readonly IgnoreBaseGlyphs: 2;
37
+ readonly IgnoreLigatures: 4;
38
+ readonly IgnoreMarks: 8;
39
+ readonly IgnoreFlags: 14;
40
+ readonly UseMarkFilteringSet: 16;
41
+ readonly MarkAttachmentType: 65280;
42
+ };
43
+ export interface LangSys {
44
+ /** Feature index of the required feature, or -1. */
45
+ required: number;
46
+ features: number[];
47
+ }
48
+ export interface LookupHeader {
49
+ type: number;
50
+ flag: number;
51
+ /** Mark filtering set index (only meaningful with UseMarkFilteringSet). */
52
+ markFilteringSet: number;
53
+ /** Subtables with extension lookups already resolved to their real type and offset. */
54
+ subtables: Array<{
55
+ type: number;
56
+ offset: number;
57
+ }>;
58
+ }
59
+ /** Selected script and language system of a GSUB/GPOS table. */
60
+ export interface ScriptSelection {
61
+ /** The script tag actually found in the font (may be 'DFLT' / 'latn' fallbacks). */
62
+ scriptTag: string;
63
+ langSys: LangSys;
64
+ }
65
+ /** ScriptList / FeatureList / LookupList of a GSUB or GPOS table. */
66
+ export declare class LayoutTable {
67
+ private readonly extensionType;
68
+ readonly data: BinaryData;
69
+ private readonly scripts;
70
+ readonly featureTags: string[];
71
+ private readonly featureOffsets;
72
+ private readonly featureLookupCache;
73
+ private readonly lookupOffsets;
74
+ private readonly lookupHeaders;
75
+ /** @param extensionType the Extension lookup type of this table (7 for GSUB, 9 for GPOS) */
76
+ constructor(bytes: Uint8Array, extensionType: number);
77
+ private parseLangSys;
78
+ get lookupCount(): number;
79
+ hasScript(tag: string): boolean;
80
+ /**
81
+ * Picks the first candidate script the font has, then falls back like HarfBuzz to 'DFLT',
82
+ * 'dflt' and finally 'latn' (old fonts park their features there even for other scripts).
83
+ * Within the script, the requested language system wins, else the default one.
84
+ */
85
+ select(scriptCandidates: readonly string[], language?: string): ScriptSelection | undefined;
86
+ /** Index of the feature with `tag` in the language system, or -1. */
87
+ findFeature(langSys: LangSys, tag: string): number;
88
+ featureLookups(featureIndex: number): number[];
89
+ lookupHeader(index: number): LookupHeader | undefined;
90
+ }
91
+ export interface LookupRecord {
92
+ sequenceIndex: number;
93
+ lookupIndex: number;
94
+ }
95
+ export interface ChainRule {
96
+ /** Backtrack values, nearest glyph first (as stored in the font). */
97
+ backtrack: number[];
98
+ /** Input values after the first glyph (the first is matched by coverage / class). */
99
+ input: number[];
100
+ lookahead: number[];
101
+ lookups: LookupRecord[];
102
+ }
103
+ export type ContextualSubtable = {
104
+ kind: 'ctx-glyphs';
105
+ coverage: Coverage;
106
+ ruleSets: Array<ChainRule[] | undefined>;
107
+ } | {
108
+ kind: 'ctx-classes';
109
+ coverage: Coverage;
110
+ backtrackClasses: ClassDef;
111
+ inputClasses: ClassDef;
112
+ lookaheadClasses: ClassDef;
113
+ ruleSets: Array<ChainRule[] | undefined>;
114
+ } | {
115
+ kind: 'ctx-coverage';
116
+ backtrack: Coverage[];
117
+ input: Coverage[];
118
+ lookahead: Coverage[];
119
+ lookups: LookupRecord[];
120
+ };
121
+ /** Parses a Context (chained = false) or ChainContext subtable of any format. */
122
+ export declare function parseContextual(d: BinaryData, off: number, chained: boolean): ContextualSubtable | undefined;
123
+ /** Normalises a user-supplied OpenType tag ('nko' → 'nko ', 'URD' → 'URD '). */
124
+ export declare function normalizeTag(tag: string): string;
@@ -0,0 +1,349 @@
1
+ /**
2
+ * OpenType Layout common table formats (shared by GSUB and GPOS): ScriptList / LangSys,
3
+ * FeatureList, LookupList, Coverage, ClassDef and the contextual (5/6 in GSUB, 7/8 in GPOS)
4
+ * subtables. Everything is parsed lazily and cached per table, because a shaping call only
5
+ * touches the few lookups its features select, and fonts like Noto Nastaliq carry hundreds.
6
+ *
7
+ * Reads are bounds-checked and return 0 past the end: a malformed offset then reads as an
8
+ * empty structure instead of throwing in the middle of layout.
9
+ */
10
+ /** Big-endian reader over one table's bytes, addressed by absolute offset into the table. */
11
+ export class BinaryData {
12
+ bytes;
13
+ view;
14
+ length;
15
+ /** Coverage and ClassDef tables are often shared between subtables: parse each once. */
16
+ coverageCache = new Map();
17
+ classDefCache = new Map();
18
+ constructor(bytes) {
19
+ this.bytes = bytes;
20
+ this.view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
21
+ this.length = bytes.byteLength;
22
+ }
23
+ u16(off) {
24
+ return off >= 0 && off + 2 <= this.length ? this.view.getUint16(off) : 0;
25
+ }
26
+ i16(off) {
27
+ return off >= 0 && off + 2 <= this.length ? this.view.getInt16(off) : 0;
28
+ }
29
+ u32(off) {
30
+ return off >= 0 && off + 4 <= this.length ? this.view.getUint32(off) : 0;
31
+ }
32
+ tag(off) {
33
+ let s = '';
34
+ for (let i = 0; i < 4; i++)
35
+ s += String.fromCharCode(off + i < this.length ? this.bytes[off + i] : 0x20);
36
+ return s;
37
+ }
38
+ /** u16 array of `count` items at `off` (clamped to the table). */
39
+ u16s(off, count) {
40
+ const n = Math.max(0, Math.min(count, Math.floor((this.length - off) / 2)));
41
+ const out = new Array(n);
42
+ for (let i = 0; i < n; i++)
43
+ out[i] = this.view.getUint16(off + i * 2);
44
+ return out;
45
+ }
46
+ }
47
+ const EMPTY_COVERAGE = new Map();
48
+ export function parseCoverage(d, off) {
49
+ if (!off)
50
+ return EMPTY_COVERAGE;
51
+ const cached = d.coverageCache.get(off);
52
+ if (cached)
53
+ return cached;
54
+ const cov = new Map();
55
+ const format = d.u16(off);
56
+ if (format === 1) {
57
+ const count = d.u16(off + 2);
58
+ const glyphs = d.u16s(off + 4, count);
59
+ for (let i = 0; i < glyphs.length; i++)
60
+ if (!cov.has(glyphs[i]))
61
+ cov.set(glyphs[i], i);
62
+ }
63
+ else if (format === 2) {
64
+ const count = d.u16(off + 2);
65
+ for (let i = 0; i < count; i++) {
66
+ const r = off + 4 + i * 6;
67
+ if (r + 6 > d.length)
68
+ break;
69
+ const start = d.u16(r);
70
+ const end = d.u16(r + 2);
71
+ const startIndex = d.u16(r + 4);
72
+ for (let g = start; g <= end; g++)
73
+ if (!cov.has(g))
74
+ cov.set(g, startIndex + g - start);
75
+ }
76
+ }
77
+ d.coverageCache.set(off, cov);
78
+ return cov;
79
+ }
80
+ export function parseClassDef(d, off) {
81
+ if (!off)
82
+ return new Map();
83
+ const cached = d.classDefCache.get(off);
84
+ if (cached)
85
+ return cached;
86
+ const cd = new Map();
87
+ const format = d.u16(off);
88
+ if (format === 1) {
89
+ const start = d.u16(off + 2);
90
+ const values = d.u16s(off + 6, d.u16(off + 4));
91
+ for (let i = 0; i < values.length; i++)
92
+ if (values[i])
93
+ cd.set(start + i, values[i]);
94
+ }
95
+ else if (format === 2) {
96
+ const count = d.u16(off + 2);
97
+ for (let i = 0; i < count; i++) {
98
+ const r = off + 4 + i * 6;
99
+ if (r + 6 > d.length)
100
+ break;
101
+ const start = d.u16(r);
102
+ const end = d.u16(r + 2);
103
+ const cls = d.u16(r + 4);
104
+ if (cls)
105
+ for (let g = start; g <= end; g++)
106
+ cd.set(g, cls);
107
+ }
108
+ }
109
+ d.classDefCache.set(off, cd);
110
+ return cd;
111
+ }
112
+ export function classOf(cd, glyph) {
113
+ return cd.get(glyph) ?? 0;
114
+ }
115
+ /** Lookup flags (OpenType LookupFlag bit enumeration). */
116
+ export const LookupFlag = {
117
+ RightToLeft: 0x0001,
118
+ IgnoreBaseGlyphs: 0x0002,
119
+ IgnoreLigatures: 0x0004,
120
+ IgnoreMarks: 0x0008,
121
+ IgnoreFlags: 0x000e,
122
+ UseMarkFilteringSet: 0x0010,
123
+ MarkAttachmentType: 0xff00,
124
+ };
125
+ /** ScriptList / FeatureList / LookupList of a GSUB or GPOS table. */
126
+ export class LayoutTable {
127
+ extensionType;
128
+ data;
129
+ scripts = new Map();
130
+ featureTags = [];
131
+ featureOffsets = [];
132
+ featureLookupCache = new Map();
133
+ lookupOffsets;
134
+ lookupHeaders = new Map();
135
+ /** @param extensionType the Extension lookup type of this table (7 for GSUB, 9 for GPOS) */
136
+ constructor(bytes, extensionType) {
137
+ this.extensionType = extensionType;
138
+ const d = (this.data = new BinaryData(bytes));
139
+ const scriptList = d.u16(4);
140
+ const featureList = d.u16(6);
141
+ const lookupList = d.u16(8);
142
+ // Version 1.1 adds FeatureVariations (variable fonts); the default features are what we use.
143
+ if (scriptList) {
144
+ const count = d.u16(scriptList);
145
+ for (let i = 0; i < count; i++) {
146
+ const rec = scriptList + 2 + i * 6;
147
+ const tag = d.tag(rec);
148
+ const off = scriptList + d.u16(rec + 4);
149
+ const defOff = d.u16(off);
150
+ const record = { defaultLangSys: defOff ? this.parseLangSys(off + defOff) : undefined, langSys: new Map() };
151
+ const langCount = d.u16(off + 2);
152
+ for (let j = 0; j < langCount; j++) {
153
+ const lrec = off + 4 + j * 6;
154
+ record.langSys.set(d.tag(lrec), this.parseLangSys(off + d.u16(lrec + 4)));
155
+ }
156
+ if (!this.scripts.has(tag))
157
+ this.scripts.set(tag, record);
158
+ }
159
+ }
160
+ if (featureList) {
161
+ const count = d.u16(featureList);
162
+ for (let i = 0; i < count; i++) {
163
+ const rec = featureList + 2 + i * 6;
164
+ this.featureTags.push(d.tag(rec));
165
+ this.featureOffsets.push(featureList + d.u16(rec + 4));
166
+ }
167
+ }
168
+ this.lookupOffsets = lookupList ? d.u16s(lookupList + 2, d.u16(lookupList)).map((o) => lookupList + o) : [];
169
+ }
170
+ parseLangSys(off) {
171
+ const d = this.data;
172
+ const req = d.u16(off + 2);
173
+ return { required: req === 0xffff ? -1 : req, features: d.u16s(off + 6, d.u16(off + 4)) };
174
+ }
175
+ get lookupCount() {
176
+ return this.lookupOffsets.length;
177
+ }
178
+ hasScript(tag) {
179
+ return this.scripts.has(tag);
180
+ }
181
+ /**
182
+ * Picks the first candidate script the font has, then falls back like HarfBuzz to 'DFLT',
183
+ * 'dflt' and finally 'latn' (old fonts park their features there even for other scripts).
184
+ * Within the script, the requested language system wins, else the default one.
185
+ */
186
+ select(scriptCandidates, language) {
187
+ let tag;
188
+ for (const t of [...scriptCandidates, 'DFLT', 'dflt', 'latn']) {
189
+ if (this.scripts.has(t)) {
190
+ tag = t;
191
+ break;
192
+ }
193
+ }
194
+ if (!tag)
195
+ return undefined;
196
+ const script = this.scripts.get(tag);
197
+ const langSys = (language && script.langSys.get(language)) || script.defaultLangSys || { required: -1, features: [] };
198
+ return { scriptTag: tag, langSys };
199
+ }
200
+ /** Index of the feature with `tag` in the language system, or -1. */
201
+ findFeature(langSys, tag) {
202
+ for (const fi of langSys.features)
203
+ if (this.featureTags[fi] === tag)
204
+ return fi;
205
+ return -1;
206
+ }
207
+ featureLookups(featureIndex) {
208
+ const cached = this.featureLookupCache.get(featureIndex);
209
+ if (cached)
210
+ return cached;
211
+ const off = this.featureOffsets[featureIndex];
212
+ const list = off === undefined ? [] : this.data.u16s(off + 4, this.data.u16(off + 2)).filter((l) => l < this.lookupCount);
213
+ this.featureLookupCache.set(featureIndex, list);
214
+ return list;
215
+ }
216
+ lookupHeader(index) {
217
+ const cached = this.lookupHeaders.get(index);
218
+ if (cached)
219
+ return cached;
220
+ const off = this.lookupOffsets[index];
221
+ if (off === undefined)
222
+ return undefined;
223
+ const d = this.data;
224
+ const type = d.u16(off);
225
+ const flag = d.u16(off + 2);
226
+ const count = d.u16(off + 4);
227
+ const subtables = [];
228
+ for (const rel of d.u16s(off + 6, count)) {
229
+ const st = off + rel;
230
+ if (type === this.extensionType) {
231
+ // ExtensionSubstFormat1 / ExtensionPosFormat1: format, real type, 32-bit offset.
232
+ if (d.u16(st) !== 1)
233
+ continue;
234
+ const realType = d.u16(st + 2);
235
+ if (realType === this.extensionType)
236
+ continue; // an extension may not point at another one
237
+ subtables.push({ type: realType, offset: st + d.u32(st + 4) });
238
+ }
239
+ else
240
+ subtables.push({ type, offset: st });
241
+ }
242
+ const header = {
243
+ type: subtables[0]?.type ?? type,
244
+ flag,
245
+ markFilteringSet: flag & LookupFlag.UseMarkFilteringSet ? d.u16(off + 6 + count * 2) : 0,
246
+ subtables,
247
+ };
248
+ this.lookupHeaders.set(index, header);
249
+ return header;
250
+ }
251
+ }
252
+ function lookupRecords(d, off, count) {
253
+ const out = [];
254
+ for (let i = 0; i < count; i++)
255
+ out.push({ sequenceIndex: d.u16(off + i * 4), lookupIndex: d.u16(off + i * 4 + 2) });
256
+ return out;
257
+ }
258
+ function parseRuleSets(d, off, countAt, chained) {
259
+ const sets = [];
260
+ const count = d.u16(countAt);
261
+ for (let i = 0; i < count; i++) {
262
+ const rel = d.u16(countAt + 2 + i * 2);
263
+ if (!rel) {
264
+ sets.push(undefined);
265
+ continue;
266
+ }
267
+ const setOff = off + rel;
268
+ const rules = [];
269
+ const ruleCount = d.u16(setOff);
270
+ for (let j = 0; j < ruleCount; j++) {
271
+ const r = setOff + d.u16(setOff + 2 + j * 2);
272
+ if (chained) {
273
+ const bc = d.u16(r);
274
+ const backtrack = d.u16s(r + 2, bc);
275
+ let p = r + 2 + bc * 2;
276
+ const ic = d.u16(p);
277
+ const input = d.u16s(p + 2, Math.max(0, ic - 1));
278
+ p += 2 + Math.max(0, ic - 1) * 2;
279
+ const lc = d.u16(p);
280
+ const lookahead = d.u16s(p + 2, lc);
281
+ p += 2 + lc * 2;
282
+ if (ic === 0)
283
+ continue;
284
+ rules.push({ backtrack, input, lookahead, lookups: lookupRecords(d, p + 2, d.u16(p)) });
285
+ }
286
+ else {
287
+ const gc = d.u16(r);
288
+ const lc = d.u16(r + 2);
289
+ if (gc === 0)
290
+ continue;
291
+ const input = d.u16s(r + 4, gc - 1);
292
+ rules.push({ backtrack: [], input, lookahead: [], lookups: lookupRecords(d, r + 4 + (gc - 1) * 2, lc) });
293
+ }
294
+ }
295
+ sets.push(rules);
296
+ }
297
+ return sets;
298
+ }
299
+ /** Parses a Context (chained = false) or ChainContext subtable of any format. */
300
+ export function parseContextual(d, off, chained) {
301
+ const format = d.u16(off);
302
+ if (format === 1) {
303
+ return { kind: 'ctx-glyphs', coverage: parseCoverage(d, off + d.u16(off + 2)), ruleSets: parseRuleSets(d, off, off + 4, chained) };
304
+ }
305
+ if (format === 2) {
306
+ const coverage = parseCoverage(d, off + d.u16(off + 2));
307
+ if (chained) {
308
+ const cd = (at) => parseClassDef(d, d.u16(at) ? off + d.u16(at) : 0);
309
+ return {
310
+ kind: 'ctx-classes',
311
+ coverage,
312
+ backtrackClasses: cd(off + 4),
313
+ inputClasses: cd(off + 6),
314
+ lookaheadClasses: cd(off + 8),
315
+ ruleSets: parseRuleSets(d, off, off + 10, true),
316
+ };
317
+ }
318
+ const classes = parseClassDef(d, off + d.u16(off + 4));
319
+ return { kind: 'ctx-classes', coverage, backtrackClasses: classes, inputClasses: classes, lookaheadClasses: classes, ruleSets: parseRuleSets(d, off, off + 6, false) };
320
+ }
321
+ if (format === 3) {
322
+ const covs = (at, count) => d.u16s(at, count).map((o) => parseCoverage(d, off + o));
323
+ if (chained) {
324
+ let p = off + 2;
325
+ const bc = d.u16(p);
326
+ const backtrack = covs(p + 2, bc);
327
+ p += 2 + bc * 2;
328
+ const ic = d.u16(p);
329
+ const input = covs(p + 2, ic);
330
+ p += 2 + ic * 2;
331
+ const lc = d.u16(p);
332
+ const lookahead = covs(p + 2, lc);
333
+ p += 2 + lc * 2;
334
+ if (!ic)
335
+ return undefined;
336
+ return { kind: 'ctx-coverage', backtrack, input, lookahead, lookups: lookupRecords(d, p + 2, d.u16(p)) };
337
+ }
338
+ const gc = d.u16(off + 2);
339
+ const lc = d.u16(off + 4);
340
+ if (!gc)
341
+ return undefined;
342
+ return { kind: 'ctx-coverage', backtrack: [], input: covs(off + 6, gc), lookahead: [], lookups: lookupRecords(d, off + 6 + gc * 2, lc) };
343
+ }
344
+ return undefined;
345
+ }
346
+ /** Normalises a user-supplied OpenType tag ('nko' → 'nko ', 'URD' → 'URD '). */
347
+ export function normalizeTag(tag) {
348
+ return tag.length >= 4 ? tag.slice(0, 4) : tag.padEnd(4, ' ');
349
+ }
@@ -0,0 +1,87 @@
1
+ /**
2
+ * The shape plan: which GSUB/GPOS lookups run, in which order, on which glyphs. Shapers
3
+ * request features in "stages" separated by pauses (where they reorder or analyse the
4
+ * buffer); within a stage the lookups of all its features are merged and applied in lookup
5
+ * list order, as fonts expect. Each feature gets a mask bit: global features share one bit,
6
+ * per-glyph features (Arabic joining forms, Indic half/below forms...) get their own so the
7
+ * shaper can enable them glyph by glyph.
8
+ */
9
+ import type { GlyphInfo } from './buffer.js';
10
+ import type { Gpos } from './gpos.js';
11
+ import type { Gsub } from './gsub.js';
12
+ import type { ScriptSelection } from './layout-common.js';
13
+ /** The font surface shaping needs; `Font` from sfnt.ts satisfies it. */
14
+ export interface ShapingFont {
15
+ glyphId(codePoint: number): number;
16
+ advance(glyph: number): number;
17
+ tableBytes(tag: string): Uint8Array | undefined;
18
+ kerning(left: number, right: number): number;
19
+ unitsPerEm?: number;
20
+ }
21
+ export interface FeatureFlags {
22
+ /** On for every glyph (shares the global mask bit). */
23
+ global?: boolean;
24
+ /** ZWJ is not skipped while matching (joiners steer the feature). */
25
+ manualZwj?: boolean;
26
+ manualZwnj?: boolean;
27
+ /** Context matching stops at syllable boundaries (Indic). */
28
+ perSyllable?: boolean;
29
+ /** Keep a mask bit even when the font lacks the feature (the shaper has a fallback). */
30
+ fallback?: boolean;
31
+ }
32
+ export interface ShapeContext {
33
+ font: ShapingFont;
34
+ buffer: GlyphInfo[];
35
+ plan: ShapePlan;
36
+ direction: 'ltr' | 'rtl';
37
+ }
38
+ export type PauseHook = (ctx: ShapeContext) => void;
39
+ export interface PlannedLookup {
40
+ index: number;
41
+ mask: number;
42
+ autoZwj: boolean;
43
+ autoZwnj: boolean;
44
+ perSyllable: boolean;
45
+ }
46
+ export interface PlanStage {
47
+ lookups: PlannedLookup[];
48
+ /** Runs after this stage's lookups. */
49
+ pause: PauseHook | undefined;
50
+ }
51
+ export declare const GLOBAL_MASK = 1;
52
+ export declare class PlanBuilder {
53
+ private readonly entries;
54
+ private readonly stage;
55
+ private readonly pauses;
56
+ /** Adds a feature to the current stage of both tables (it runs wherever the font has it). */
57
+ add(tag: string, flags?: FeatureFlags): void;
58
+ enable(tag: string, flags?: FeatureFlags): void;
59
+ has(tag: string): boolean;
60
+ remove(tag: string): void;
61
+ pauseGsub(hook?: PauseHook): void;
62
+ compile(gsub: Gsub | undefined, gpos: Gpos | undefined, gsubSel: ScriptSelection | undefined, gposSel: ScriptSelection | undefined): ShapePlan;
63
+ }
64
+ export declare class ShapePlan {
65
+ readonly gsub: Gsub | undefined;
66
+ readonly gpos: Gpos | undefined;
67
+ /** Script tag GSUB/GPOS actually selected ('dev2', 'DFLT', ...), undefined when absent. */
68
+ readonly gsubScript: string | undefined;
69
+ readonly gposScript: string | undefined;
70
+ gsubStages: PlanStage[];
71
+ gposStages: PlanStage[];
72
+ readonly masks: Map<string, number>;
73
+ readonly featureIndex: [Map<string, number>, Map<string, number>];
74
+ /** Shaper-specific data computed once per plan. */
75
+ shaperData: unknown;
76
+ /** Unicode-script tag being shaped ('deva', 'arab', 'DFLT'...). */
77
+ script: string;
78
+ constructor(gsub: Gsub | undefined, gpos: Gpos | undefined,
79
+ /** Script tag GSUB/GPOS actually selected ('dev2', 'DFLT', ...), undefined when absent. */
80
+ gsubScript: string | undefined, gposScript: string | undefined);
81
+ /** Mask bit of a feature, 0 when the plan doesn't apply it. */
82
+ maskOf(tag: string): number;
83
+ /** Whether the font itself implements the feature (as opposed to a shaper fallback). */
84
+ hasFeature(table: 0 | 1, tag: string): boolean;
85
+ /** GSUB lookups of a feature (for "would this substitute?" queries). */
86
+ gsubLookups(tag: string): number[];
87
+ }