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.
- package/CHANGELOG.md +74 -0
- package/README.md +166 -16
- package/dist/api/editor.d.ts +231 -7
- package/dist/api/editor.js +1125 -26
- package/dist/api/headers.d.ts +51 -0
- package/dist/api/headers.js +133 -0
- package/dist/api/index.d.ts +6 -1
- package/dist/api/index.js +4 -1
- package/dist/api/lists.d.ts +37 -0
- package/dist/api/lists.js +132 -0
- package/dist/api/parts.js +1 -0
- package/dist/api/positions.js +1 -0
- package/dist/api/revisions.d.ts +61 -0
- package/dist/api/revisions.js +401 -0
- package/dist/api/selection.d.ts +57 -0
- package/dist/api/selection.js +91 -0
- package/dist/api/tables.d.ts +60 -0
- package/dist/api/tables.js +138 -0
- package/dist/charts/color.d.ts +45 -0
- package/dist/charts/color.js +322 -0
- package/dist/charts/format.d.ts +12 -0
- package/dist/charts/format.js +459 -0
- package/dist/charts/index.d.ts +11 -0
- package/dist/charts/index.js +8 -0
- package/dist/charts/model.d.ts +225 -0
- package/dist/charts/model.js +758 -0
- package/dist/charts/outline.d.ts +17 -0
- package/dist/charts/outline.js +214 -0
- package/dist/charts/render.d.ts +20 -0
- package/dist/charts/render.js +1772 -0
- package/dist/drawing/arrows.d.ts +37 -0
- package/dist/drawing/arrows.js +99 -0
- package/dist/drawing/formula.d.ts +40 -0
- package/dist/drawing/formula.js +159 -0
- package/dist/drawing/geometry.d.ts +61 -0
- package/dist/drawing/geometry.js +286 -0
- package/dist/drawing/index.d.ts +12 -0
- package/dist/drawing/index.js +8 -0
- package/dist/drawing/presets.d.ts +72 -0
- package/dist/drawing/presets.js +2874 -0
- package/dist/fonts/shaping/apply.d.ts +94 -0
- package/dist/fonts/shaping/apply.js +349 -0
- package/dist/fonts/shaping/arabic.d.ts +28 -0
- package/dist/fonts/shaping/arabic.js +235 -0
- package/dist/fonts/shaping/buffer.d.ts +53 -0
- package/dist/fonts/shaping/buffer.js +95 -0
- package/dist/fonts/shaping/gdef.d.ts +30 -0
- package/dist/fonts/shaping/gdef.js +66 -0
- package/dist/fonts/shaping/gpos.d.ts +86 -0
- package/dist/fonts/shaping/gpos.js +428 -0
- package/dist/fonts/shaping/gsub.d.ts +65 -0
- package/dist/fonts/shaping/gsub.js +313 -0
- package/dist/fonts/shaping/index.d.ts +32 -0
- package/dist/fonts/shaping/index.js +402 -0
- package/dist/fonts/shaping/indic.d.ts +6 -0
- package/dist/fonts/shaping/indic.js +805 -0
- package/dist/fonts/shaping/layout-common.d.ts +124 -0
- package/dist/fonts/shaping/layout-common.js +349 -0
- package/dist/fonts/shaping/plan.d.ts +87 -0
- package/dist/fonts/shaping/plan.js +152 -0
- package/dist/fonts/shaping/scripts.d.ts +26 -0
- package/dist/fonts/shaping/scripts.js +160 -0
- package/dist/fonts/shaping/shaper.d.ts +23 -0
- package/dist/fonts/shaping/shaper.js +5 -0
- package/dist/fonts/shaping/unicode.d.ts +34 -0
- package/dist/fonts/shaping/unicode.js +138 -0
- package/dist/images/bmp.d.ts +9 -0
- package/dist/images/bmp.js +234 -0
- package/dist/images/gif.d.ts +9 -0
- package/dist/images/gif.js +215 -0
- package/dist/images/index.d.ts +18 -0
- package/dist/images/index.js +63 -0
- package/dist/images/png-encode.d.ts +4 -0
- package/dist/images/png-encode.js +73 -0
- package/dist/images/size.d.ts +10 -0
- package/dist/images/size.js +83 -0
- package/dist/images/tiff.d.ts +14 -0
- package/dist/images/tiff.js +407 -0
- package/dist/index.d.ts +7 -2
- package/dist/index.js +5 -0
- package/dist/layout/bidi.d.ts +18 -0
- package/dist/layout/bidi.js +71 -0
- package/dist/layout/blocks.d.ts +20 -3
- package/dist/layout/blocks.js +88 -58
- package/dist/layout/cache.d.ts +31 -0
- package/dist/layout/cache.js +38 -0
- package/dist/layout/drawing.d.ts +30 -0
- package/dist/layout/drawing.js +120 -0
- package/dist/layout/engine.d.ts +3 -0
- package/dist/layout/engine.js +218 -11
- package/dist/layout/index.d.ts +1 -0
- package/dist/layout/index.js +1 -0
- package/dist/layout/lines.d.ts +63 -5
- package/dist/layout/lines.js +245 -20
- package/dist/layout/paint.d.ts +25 -3
- package/dist/layout/paint.js +26 -10
- package/dist/layout/shapes.d.ts +7 -0
- package/dist/layout/shapes.js +338 -0
- package/dist/layout/style.d.ts +4 -0
- package/dist/layout/style.js +2 -0
- package/dist/layout/types.d.ts +89 -2
- package/dist/math/boxes.d.ts +88 -0
- package/dist/math/boxes.js +166 -0
- package/dist/math/index.d.ts +29 -0
- package/dist/math/index.js +44 -0
- package/dist/math/layout.d.ts +13 -0
- package/dist/math/layout.js +771 -0
- package/dist/math/linear.d.ts +3 -0
- package/dist/math/linear.js +152 -0
- package/dist/math/omml.d.ts +15 -0
- package/dist/math/omml.js +57 -0
- package/dist/math/paths.d.ts +61 -0
- package/dist/math/paths.js +450 -0
- package/dist/math/symbols.d.ts +49 -0
- package/dist/math/symbols.js +269 -0
- package/dist/math/text.d.ts +53 -0
- package/dist/math/text.js +316 -0
- package/dist/metafile/canvas.d.ts +80 -0
- package/dist/metafile/canvas.js +515 -0
- package/dist/metafile/dib.d.ts +27 -0
- package/dist/metafile/dib.js +300 -0
- package/dist/metafile/emf.d.ts +31 -0
- package/dist/metafile/emf.js +591 -0
- package/dist/metafile/geometry.d.ts +49 -0
- package/dist/metafile/geometry.js +85 -0
- package/dist/metafile/index.d.ts +27 -0
- package/dist/metafile/index.js +34 -0
- package/dist/metafile/reader.d.ts +28 -0
- package/dist/metafile/reader.js +59 -0
- package/dist/metafile/state.d.ts +118 -0
- package/dist/metafile/state.js +172 -0
- package/dist/metafile/text.d.ts +17 -0
- package/dist/metafile/text.js +130 -0
- package/dist/metafile/wmf.d.ts +33 -0
- package/dist/metafile/wmf.js +395 -0
- package/dist/model/content.d.ts +92 -7
- package/dist/model/content.js +251 -17
- package/dist/model/document.d.ts +34 -1
- package/dist/model/document.js +80 -1
- package/dist/model/fields.d.ts +4 -0
- package/dist/model/fields.js +10 -0
- package/dist/model/numbering.d.ts +2 -0
- package/dist/model/numbering.js +22 -0
- package/dist/model/props.d.ts +2 -0
- package/dist/model/props.js +3 -0
- package/dist/opc/index.d.ts +1 -1
- package/dist/opc/package.d.ts +34 -0
- package/dist/opc/package.js +58 -4
- package/dist/pdf/export.js +97 -6
- package/dist/pdf/images.d.ts +7 -0
- package/dist/pdf/images.js +39 -2
- package/dist/render/activity.d.ts +179 -0
- package/dist/render/activity.js +1003 -0
- package/dist/render/clipboard.d.ts +29 -0
- package/dist/render/clipboard.js +401 -0
- package/dist/render/comments.d.ts +166 -0
- package/dist/render/comments.js +779 -0
- package/dist/render/context-menu.d.ts +124 -0
- package/dist/render/context-menu.js +525 -0
- package/dist/render/editing.d.ts +146 -5
- package/dist/render/editing.js +678 -53
- package/dist/render/index.d.ts +12 -0
- package/dist/render/index.js +11 -0
- package/dist/render/navigation-panel.d.ts +76 -0
- package/dist/render/navigation-panel.js +431 -0
- package/dist/render/selection.js +5 -0
- package/dist/render/svg.d.ts +4 -6
- package/dist/render/svg.js +69 -7
- package/dist/render/toolbar.d.ts +83 -0
- package/dist/render/toolbar.js +505 -0
- package/dist/render/ui/comment-layout.d.ts +39 -0
- package/dist/render/ui/comment-layout.js +79 -0
- package/dist/render/ui/icons.d.ts +6 -0
- package/dist/render/ui/icons.js +38 -0
- package/dist/render/ui/menu.d.ts +38 -0
- package/dist/render/ui/menu.js +274 -0
- package/dist/render/ui/nav-model.d.ts +11 -0
- package/dist/render/ui/nav-model.js +34 -0
- package/dist/render/ui/overflow.d.ts +5 -0
- package/dist/render/ui/overflow.js +18 -0
- package/dist/render/ui/popover.d.ts +18 -0
- package/dist/render/ui/popover.js +109 -0
- package/dist/render/ui/theme.d.ts +2 -0
- package/dist/render/ui/theme.js +29 -0
- package/dist/render/undo.d.ts +76 -0
- package/dist/render/undo.js +144 -0
- package/dist/render/viewer.d.ts +100 -7
- package/dist/render/viewer.js +304 -92
- package/dist/security/policy.d.ts +3 -0
- package/dist/security/policy.js +6 -2
- package/dist/security/protection.d.ts +1 -0
- package/dist/security/protection.js +1 -1
- package/dist/text/bidi-data.d.ts +53 -0
- package/dist/text/bidi-data.js +318 -0
- package/dist/text/bidi.d.ts +37 -0
- package/dist/text/bidi.js +508 -0
- package/dist/xml/ns.d.ts +5 -0
- package/dist/xml/ns.js +7 -0
- package/package.json +9 -2
|
@@ -0,0 +1,428 @@
|
|
|
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 { lookupProps } from './apply.js';
|
|
11
|
+
import { ATTACH_CURSIVE, ATTACH_MARK, isMark, isMultiplied, ligComp } from './buffer.js';
|
|
12
|
+
import { BinaryData, classOf, LayoutTable, LookupFlag, parseClassDef, parseContextual, parseCoverage, } from './layout-common.js';
|
|
13
|
+
/** Number of 16-bit fields in a ValueRecord of this format. */
|
|
14
|
+
function valueLength(format) {
|
|
15
|
+
let n = 0;
|
|
16
|
+
for (let f = format & 0xff; f; f >>= 1)
|
|
17
|
+
n += f & 1;
|
|
18
|
+
return n;
|
|
19
|
+
}
|
|
20
|
+
export class Gpos {
|
|
21
|
+
layout;
|
|
22
|
+
lookups = new Map();
|
|
23
|
+
dispatch;
|
|
24
|
+
constructor(bytes) {
|
|
25
|
+
this.layout = new LayoutTable(bytes, 9);
|
|
26
|
+
this.dispatch = (ctx, index, i) => {
|
|
27
|
+
const lookup = this.lookup(index);
|
|
28
|
+
if (!lookup)
|
|
29
|
+
return -1;
|
|
30
|
+
ctx.lookupProps = lookup.props;
|
|
31
|
+
for (const st of lookup.subtables) {
|
|
32
|
+
const next = applyPositioning(ctx, this.layout.data, st, i);
|
|
33
|
+
if (next >= 0)
|
|
34
|
+
return next;
|
|
35
|
+
}
|
|
36
|
+
return -1;
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
lookup(index) {
|
|
40
|
+
const cached = this.lookups.get(index);
|
|
41
|
+
if (cached !== undefined)
|
|
42
|
+
return cached ?? undefined;
|
|
43
|
+
const header = this.layout.lookupHeader(index);
|
|
44
|
+
let lookup = null;
|
|
45
|
+
if (header) {
|
|
46
|
+
const subtables = [];
|
|
47
|
+
for (const st of header.subtables) {
|
|
48
|
+
const parsed = parsePositioning(this.layout.data, st.type, st.offset);
|
|
49
|
+
if (parsed)
|
|
50
|
+
subtables.push(parsed);
|
|
51
|
+
}
|
|
52
|
+
lookup = { type: header.type, props: lookupProps(header.flag, header.markFilteringSet), subtables };
|
|
53
|
+
}
|
|
54
|
+
this.lookups.set(index, lookup);
|
|
55
|
+
return lookup ?? undefined;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
function parseAnchor(d, off) {
|
|
59
|
+
if (!off)
|
|
60
|
+
return undefined;
|
|
61
|
+
const format = d.u16(off);
|
|
62
|
+
if (format < 1 || format > 3)
|
|
63
|
+
return undefined;
|
|
64
|
+
return { x: d.i16(off + 2), y: d.i16(off + 4) };
|
|
65
|
+
}
|
|
66
|
+
function parseMarkArray(d, off) {
|
|
67
|
+
const count = d.u16(off);
|
|
68
|
+
const out = [];
|
|
69
|
+
for (let i = 0; i < count; i++) {
|
|
70
|
+
const r = off + 2 + i * 4;
|
|
71
|
+
const rel = d.u16(r + 2);
|
|
72
|
+
out.push({ cls: d.u16(r), anchor: rel ? parseAnchor(d, off + rel) : undefined });
|
|
73
|
+
}
|
|
74
|
+
return out;
|
|
75
|
+
}
|
|
76
|
+
/** BaseArray / Mark2Array / a LigatureAttach: rows of `classCount` anchor offsets. */
|
|
77
|
+
function parseAnchorMatrix(d, off, classCount) {
|
|
78
|
+
const rows = d.u16(off);
|
|
79
|
+
const out = [];
|
|
80
|
+
for (let r = 0; r < rows; r++) {
|
|
81
|
+
const row = [];
|
|
82
|
+
for (let c = 0; c < classCount; c++) {
|
|
83
|
+
const rel = d.u16(off + 2 + (r * classCount + c) * 2);
|
|
84
|
+
row.push(rel ? parseAnchor(d, off + rel) : undefined);
|
|
85
|
+
}
|
|
86
|
+
out.push(row);
|
|
87
|
+
}
|
|
88
|
+
return out;
|
|
89
|
+
}
|
|
90
|
+
function parsePositioning(d, type, off) {
|
|
91
|
+
const format = d.u16(off);
|
|
92
|
+
const coverageAt = (at) => parseCoverage(d, off + d.u16(at));
|
|
93
|
+
switch (type) {
|
|
94
|
+
case 1: {
|
|
95
|
+
const valueFormat = d.u16(off + 4);
|
|
96
|
+
const len = valueLength(valueFormat) * 2;
|
|
97
|
+
if (format === 1)
|
|
98
|
+
return { kind: 'single', coverage: coverageAt(off + 2), valueFormat, valueAt: () => off + 6 };
|
|
99
|
+
if (format === 2)
|
|
100
|
+
return { kind: 'single', coverage: coverageAt(off + 2), valueFormat, valueAt: (i) => off + 8 + i * len };
|
|
101
|
+
return undefined;
|
|
102
|
+
}
|
|
103
|
+
case 2: {
|
|
104
|
+
const vf1 = d.u16(off + 4);
|
|
105
|
+
const vf2 = d.u16(off + 6);
|
|
106
|
+
if (format === 1) {
|
|
107
|
+
const recordSize = (1 + valueLength(vf1) + valueLength(vf2)) * 2;
|
|
108
|
+
const setCount = d.u16(off + 8);
|
|
109
|
+
const sets = new Map();
|
|
110
|
+
return {
|
|
111
|
+
kind: 'pair1',
|
|
112
|
+
coverage: coverageAt(off + 2),
|
|
113
|
+
vf1,
|
|
114
|
+
vf2,
|
|
115
|
+
// Pair sets are parsed on first use: big kerning tables are mostly never touched.
|
|
116
|
+
pairSet: (index) => {
|
|
117
|
+
if (index >= setCount)
|
|
118
|
+
return undefined;
|
|
119
|
+
let set = sets.get(index);
|
|
120
|
+
if (!set) {
|
|
121
|
+
set = new Map();
|
|
122
|
+
const so = off + d.u16(off + 10 + index * 2);
|
|
123
|
+
const n = d.u16(so);
|
|
124
|
+
for (let k = 0; k < n; k++) {
|
|
125
|
+
const r = so + 2 + k * recordSize;
|
|
126
|
+
const second = d.u16(r);
|
|
127
|
+
if (!set.has(second))
|
|
128
|
+
set.set(second, r + 2);
|
|
129
|
+
}
|
|
130
|
+
sets.set(index, set);
|
|
131
|
+
}
|
|
132
|
+
return set;
|
|
133
|
+
},
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
if (format === 2) {
|
|
137
|
+
return {
|
|
138
|
+
kind: 'pair2',
|
|
139
|
+
coverage: coverageAt(off + 2),
|
|
140
|
+
vf1,
|
|
141
|
+
vf2,
|
|
142
|
+
classDef1: parseClassDef(d, off + d.u16(off + 8)),
|
|
143
|
+
classDef2: parseClassDef(d, off + d.u16(off + 10)),
|
|
144
|
+
class1Count: d.u16(off + 12),
|
|
145
|
+
class2Count: d.u16(off + 14),
|
|
146
|
+
recordsAt: off + 16,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
return undefined;
|
|
150
|
+
}
|
|
151
|
+
case 3: {
|
|
152
|
+
if (format !== 1)
|
|
153
|
+
return undefined;
|
|
154
|
+
const count = d.u16(off + 4);
|
|
155
|
+
const records = [];
|
|
156
|
+
for (let i = 0; i < count; i++) {
|
|
157
|
+
const r = off + 6 + i * 4;
|
|
158
|
+
const entry = d.u16(r);
|
|
159
|
+
const exit = d.u16(r + 2);
|
|
160
|
+
records.push({ entry: entry ? parseAnchor(d, off + entry) : undefined, exit: exit ? parseAnchor(d, off + exit) : undefined });
|
|
161
|
+
}
|
|
162
|
+
return { kind: 'cursive', coverage: coverageAt(off + 2), records };
|
|
163
|
+
}
|
|
164
|
+
case 4:
|
|
165
|
+
case 5:
|
|
166
|
+
case 6: {
|
|
167
|
+
if (format !== 1)
|
|
168
|
+
return undefined;
|
|
169
|
+
const classCount = d.u16(off + 6);
|
|
170
|
+
const markCoverage = coverageAt(off + 2);
|
|
171
|
+
const otherCoverage = coverageAt(off + 4);
|
|
172
|
+
const marks = parseMarkArray(d, off + d.u16(off + 8));
|
|
173
|
+
const arrayOff = off + d.u16(off + 10);
|
|
174
|
+
if (type === 4)
|
|
175
|
+
return { kind: 'markBase', markCoverage, baseCoverage: otherCoverage, marks, bases: parseAnchorMatrix(d, arrayOff, classCount) };
|
|
176
|
+
if (type === 6)
|
|
177
|
+
return { kind: 'markMark', markCoverage, mark2Coverage: otherCoverage, marks, mark2s: parseAnchorMatrix(d, arrayOff, classCount) };
|
|
178
|
+
const ligCount = d.u16(arrayOff);
|
|
179
|
+
const ligatures = d.u16s(arrayOff + 2, ligCount).map((rel) => parseAnchorMatrix(d, arrayOff + rel, classCount));
|
|
180
|
+
return { kind: 'markLig', markCoverage, ligCoverage: otherCoverage, marks, ligatures };
|
|
181
|
+
}
|
|
182
|
+
case 7:
|
|
183
|
+
case 8:
|
|
184
|
+
return parseContextual(d, off, type === 8);
|
|
185
|
+
default:
|
|
186
|
+
return undefined;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
/** Adds a ValueRecord to a glyph (horizontal layout: Y advance is ignored). */
|
|
190
|
+
function applyValue(d, off, format, info) {
|
|
191
|
+
let p = off;
|
|
192
|
+
if (format & 0x0001) {
|
|
193
|
+
info.xOffset += d.i16(p);
|
|
194
|
+
p += 2;
|
|
195
|
+
}
|
|
196
|
+
if (format & 0x0002) {
|
|
197
|
+
info.yOffset += d.i16(p);
|
|
198
|
+
p += 2;
|
|
199
|
+
}
|
|
200
|
+
if (format & 0x0004)
|
|
201
|
+
info.xAdvance += d.i16(p);
|
|
202
|
+
}
|
|
203
|
+
function attachMark(ctx, i, mark, target, j) {
|
|
204
|
+
if (!mark?.anchor || !target)
|
|
205
|
+
return -1; // let a later subtable try
|
|
206
|
+
const info = ctx.buffer[i];
|
|
207
|
+
info.xOffset = Math.round(target.x - mark.anchor.x);
|
|
208
|
+
info.yOffset = Math.round(target.y - mark.anchor.y);
|
|
209
|
+
info.attachType = ATTACH_MARK;
|
|
210
|
+
info.attachChain = j - i;
|
|
211
|
+
return i + 1;
|
|
212
|
+
}
|
|
213
|
+
function applyPositioning(ctx, d, st, i) {
|
|
214
|
+
const buf = ctx.buffer;
|
|
215
|
+
const info = buf[i];
|
|
216
|
+
const glyph = info.glyph;
|
|
217
|
+
switch (st.kind) {
|
|
218
|
+
case 'single': {
|
|
219
|
+
const ci = st.coverage.get(glyph);
|
|
220
|
+
if (ci === undefined)
|
|
221
|
+
return -1;
|
|
222
|
+
applyValue(d, st.valueAt(ci), st.valueFormat, info);
|
|
223
|
+
return i + 1;
|
|
224
|
+
}
|
|
225
|
+
case 'pair1':
|
|
226
|
+
case 'pair2': {
|
|
227
|
+
const ci = st.coverage.get(glyph);
|
|
228
|
+
if (ci === undefined)
|
|
229
|
+
return -1;
|
|
230
|
+
const j = ctx.next(i, ctx.inputMatcher(i));
|
|
231
|
+
if (j < 0)
|
|
232
|
+
return -1;
|
|
233
|
+
const second = buf[j];
|
|
234
|
+
const len1 = valueLength(st.vf1) * 2;
|
|
235
|
+
let rec;
|
|
236
|
+
if (st.kind === 'pair1') {
|
|
237
|
+
rec = st.pairSet(ci)?.get(second.glyph);
|
|
238
|
+
}
|
|
239
|
+
else {
|
|
240
|
+
const k1 = classOf(st.classDef1, glyph);
|
|
241
|
+
const k2 = classOf(st.classDef2, second.glyph);
|
|
242
|
+
if (k1 < st.class1Count && k2 < st.class2Count)
|
|
243
|
+
rec = st.recordsAt + (k1 * st.class2Count + k2) * (len1 + valueLength(st.vf2) * 2);
|
|
244
|
+
}
|
|
245
|
+
if (rec === undefined)
|
|
246
|
+
return -1;
|
|
247
|
+
applyValue(d, rec, st.vf1, info);
|
|
248
|
+
applyValue(d, rec + len1, st.vf2, second);
|
|
249
|
+
// A pair that also moved the second glyph consumes it; otherwise it may pair again.
|
|
250
|
+
return valueLength(st.vf2) ? j + 1 : j;
|
|
251
|
+
}
|
|
252
|
+
case 'cursive':
|
|
253
|
+
return applyCursive(ctx, st, i);
|
|
254
|
+
case 'markBase': {
|
|
255
|
+
const mi = st.markCoverage.get(glyph);
|
|
256
|
+
if (mi === undefined)
|
|
257
|
+
return -1;
|
|
258
|
+
const m = baseMatcher(ctx);
|
|
259
|
+
let j = i;
|
|
260
|
+
for (;;) {
|
|
261
|
+
j = ctx.prev(j, m);
|
|
262
|
+
if (j < 0)
|
|
263
|
+
return -1;
|
|
264
|
+
// Attach to the first glyph of a multiple-substitution sequence, not the later ones.
|
|
265
|
+
const b = buf[j];
|
|
266
|
+
const p = buf[j - 1];
|
|
267
|
+
if (!isMultiplied(b) || ligComp(b) === 0 || !p || isMark(p) || !isMultiplied(p) || b.ligId !== p.ligId || ligComp(b) !== ligComp(p) + 1)
|
|
268
|
+
break;
|
|
269
|
+
}
|
|
270
|
+
const bi = st.baseCoverage.get(buf[j].glyph);
|
|
271
|
+
if (bi === undefined)
|
|
272
|
+
return -1;
|
|
273
|
+
const mark = st.marks[mi];
|
|
274
|
+
return attachMark(ctx, i, mark, mark && st.bases[bi]?.[mark.cls], j);
|
|
275
|
+
}
|
|
276
|
+
case 'markLig': {
|
|
277
|
+
const mi = st.markCoverage.get(glyph);
|
|
278
|
+
if (mi === undefined)
|
|
279
|
+
return -1;
|
|
280
|
+
const j = ctx.prev(i, baseMatcher(ctx));
|
|
281
|
+
if (j < 0)
|
|
282
|
+
return -1;
|
|
283
|
+
const lig = buf[j];
|
|
284
|
+
const li = st.ligCoverage.get(lig.glyph);
|
|
285
|
+
const comps = li === undefined ? undefined : st.ligatures[li];
|
|
286
|
+
if (!comps?.length)
|
|
287
|
+
return -1;
|
|
288
|
+
// A mark that came between the ligature's components carries its component number.
|
|
289
|
+
const markComp = ligComp(info);
|
|
290
|
+
const comp = lig.ligId && lig.ligId === info.ligId && markComp > 0 ? Math.min(comps.length, markComp) - 1 : comps.length - 1;
|
|
291
|
+
const mark = st.marks[mi];
|
|
292
|
+
return attachMark(ctx, i, mark, mark && comps[comp]?.[mark.cls], j);
|
|
293
|
+
}
|
|
294
|
+
case 'markMark': {
|
|
295
|
+
const mi = st.markCoverage.get(glyph);
|
|
296
|
+
if (mi === undefined)
|
|
297
|
+
return -1;
|
|
298
|
+
const m = { props: ctx.lookupProps & ~LookupFlag.IgnoreFlags, ignoreZwnj: true, ignoreZwj: true, mask: ctx.lookupMask, syllable: 0 };
|
|
299
|
+
const j = ctx.prev(i, m);
|
|
300
|
+
if (j < 0 || !isMark(buf[j]))
|
|
301
|
+
return -1;
|
|
302
|
+
const prev = buf[j];
|
|
303
|
+
const id1 = info.ligId;
|
|
304
|
+
const id2 = prev.ligId;
|
|
305
|
+
const comp1 = ligComp(info);
|
|
306
|
+
const comp2 = ligComp(prev);
|
|
307
|
+
// Both marks must sit on the same base or the same ligature component.
|
|
308
|
+
const sameAnchorGroup = id1 === id2 ? id1 === 0 || comp1 === comp2 : (id1 > 0 && !comp1) || (id2 > 0 && !comp2);
|
|
309
|
+
if (!sameAnchorGroup)
|
|
310
|
+
return -1;
|
|
311
|
+
const m2 = st.mark2Coverage.get(prev.glyph);
|
|
312
|
+
if (m2 === undefined)
|
|
313
|
+
return -1;
|
|
314
|
+
const mark = st.marks[mi];
|
|
315
|
+
return attachMark(ctx, i, mark, mark && st.mark2s[m2]?.[mark.cls], j);
|
|
316
|
+
}
|
|
317
|
+
default:
|
|
318
|
+
return ctx.applyContextual(st, i);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
/** Mark-to-base / mark-to-ligature look back past marks regardless of the lookup's flags. */
|
|
322
|
+
function baseMatcher(ctx) {
|
|
323
|
+
return { props: LookupFlag.IgnoreMarks, ignoreZwnj: true, ignoreZwj: true, mask: ctx.lookupMask, syllable: 0 };
|
|
324
|
+
}
|
|
325
|
+
function applyCursive(ctx, st, i) {
|
|
326
|
+
const buf = ctx.buffer;
|
|
327
|
+
const ci = st.coverage.get(buf[i].glyph);
|
|
328
|
+
const thisRec = ci === undefined ? undefined : st.records[ci];
|
|
329
|
+
if (!thisRec?.entry)
|
|
330
|
+
return -1;
|
|
331
|
+
const p = ctx.prev(i, ctx.inputMatcher(i));
|
|
332
|
+
if (p < 0)
|
|
333
|
+
return -1;
|
|
334
|
+
const pci = st.coverage.get(buf[p].glyph);
|
|
335
|
+
const prevRec = pci === undefined ? undefined : st.records[pci];
|
|
336
|
+
if (!prevRec?.exit)
|
|
337
|
+
return -1;
|
|
338
|
+
const exit = prevRec.exit;
|
|
339
|
+
const entry = thisRec.entry;
|
|
340
|
+
const a = buf[p];
|
|
341
|
+
const b = buf[i];
|
|
342
|
+
// Main direction: the exit point of the previous glyph meets the entry of this one.
|
|
343
|
+
if (ctx.direction === 'ltr') {
|
|
344
|
+
a.xAdvance = Math.round(exit.x) + a.xOffset;
|
|
345
|
+
const d = Math.round(entry.x) + b.xOffset;
|
|
346
|
+
b.xAdvance -= d;
|
|
347
|
+
b.xOffset -= d;
|
|
348
|
+
}
|
|
349
|
+
else {
|
|
350
|
+
const d = Math.round(exit.x) + a.xOffset;
|
|
351
|
+
a.xAdvance -= d;
|
|
352
|
+
a.xOffset -= d;
|
|
353
|
+
b.xAdvance = Math.round(entry.x) + b.xOffset;
|
|
354
|
+
}
|
|
355
|
+
// Cross direction: the child hangs off its parent; with RightToLeft the logically earlier
|
|
356
|
+
// glyph is the child (the last glyph of a word stays on the baseline).
|
|
357
|
+
let child = p;
|
|
358
|
+
let parent = i;
|
|
359
|
+
let yOffset = Math.round(entry.y - exit.y);
|
|
360
|
+
if (!(ctx.lookupProps & LookupFlag.RightToLeft)) {
|
|
361
|
+
child = i;
|
|
362
|
+
parent = p;
|
|
363
|
+
yOffset = -yOffset;
|
|
364
|
+
}
|
|
365
|
+
reverseCursiveMinorOffset(buf, child, parent, 0);
|
|
366
|
+
const c = buf[child];
|
|
367
|
+
c.attachType = ATTACH_CURSIVE;
|
|
368
|
+
c.attachChain = parent - child;
|
|
369
|
+
c.yOffset = yOffset;
|
|
370
|
+
const par = buf[parent];
|
|
371
|
+
if (par.attachChain === -c.attachChain) {
|
|
372
|
+
par.attachChain = 0;
|
|
373
|
+
par.yOffset = 0;
|
|
374
|
+
}
|
|
375
|
+
return i + 1;
|
|
376
|
+
}
|
|
377
|
+
/** When a child gets a new parent, flip its old chain so the whole tree follows. */
|
|
378
|
+
function reverseCursiveMinorOffset(buf, i, newParent, depth) {
|
|
379
|
+
const info = buf[i];
|
|
380
|
+
const chain = info.attachChain;
|
|
381
|
+
const type = info.attachType;
|
|
382
|
+
if (!chain || !(type & ATTACH_CURSIVE) || depth > 1024)
|
|
383
|
+
return;
|
|
384
|
+
info.attachChain = 0;
|
|
385
|
+
const j = i + chain;
|
|
386
|
+
if (j === newParent || j < 0 || j >= buf.length)
|
|
387
|
+
return;
|
|
388
|
+
reverseCursiveMinorOffset(buf, j, newParent, depth + 1);
|
|
389
|
+
const parent = buf[j];
|
|
390
|
+
parent.yOffset = -info.yOffset;
|
|
391
|
+
parent.attachChain = -chain;
|
|
392
|
+
parent.attachType = type;
|
|
393
|
+
}
|
|
394
|
+
/**
|
|
395
|
+
* Turns attachment chains into final offsets: a mark lands at its base's offset minus the
|
|
396
|
+
* advances between them (in logical order; for RTL the run is later drawn reversed, so the
|
|
397
|
+
* glyphs after the base, up to and including the mark, are the ones in between).
|
|
398
|
+
*/
|
|
399
|
+
export function propagateAttachments(buf, direction) {
|
|
400
|
+
for (let i = 0; i < buf.length; i++)
|
|
401
|
+
propagate(buf, i, direction, 0);
|
|
402
|
+
}
|
|
403
|
+
function propagate(buf, i, direction, depth) {
|
|
404
|
+
const info = buf[i];
|
|
405
|
+
const chain = info.attachChain;
|
|
406
|
+
if (!chain)
|
|
407
|
+
return;
|
|
408
|
+
info.attachChain = 0;
|
|
409
|
+
const j = i + chain;
|
|
410
|
+
if (j < 0 || j >= buf.length || depth > 1024)
|
|
411
|
+
return;
|
|
412
|
+
propagate(buf, j, direction, depth + 1);
|
|
413
|
+
const parent = buf[j];
|
|
414
|
+
if (info.attachType & ATTACH_CURSIVE) {
|
|
415
|
+
info.yOffset += parent.yOffset;
|
|
416
|
+
return;
|
|
417
|
+
}
|
|
418
|
+
info.xOffset += parent.xOffset;
|
|
419
|
+
info.yOffset += parent.yOffset;
|
|
420
|
+
if (direction === 'ltr') {
|
|
421
|
+
for (let k = j; k < i; k++)
|
|
422
|
+
info.xOffset -= buf[k].xAdvance;
|
|
423
|
+
}
|
|
424
|
+
else {
|
|
425
|
+
for (let k = j + 1; k <= i; k++)
|
|
426
|
+
info.xOffset += buf[k].xAdvance;
|
|
427
|
+
}
|
|
428
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GSUB: glyph substitution lookups. Types 1 single, 2 multiple, 3 alternate (first
|
|
3
|
+
* alternate), 4 ligature, 5 context, 6 chaining context, 7 extension (resolved by
|
|
4
|
+
* LayoutTable) and 8 reverse chaining single. Substitutions edit the buffer in place and
|
|
5
|
+
* keep the glyph-property history (substituted / ligated / multiplied) and ligature
|
|
6
|
+
* component numbers that mark positioning and the Indic final reordering rely on.
|
|
7
|
+
*/
|
|
8
|
+
import { type ApplyContext, type LookupDispatcher } from './apply.js';
|
|
9
|
+
import { type GlyphInfo } from './buffer.js';
|
|
10
|
+
import { LayoutTable, type ContextualSubtable, type Coverage } from './layout-common.js';
|
|
11
|
+
export interface Ligature {
|
|
12
|
+
glyph: number;
|
|
13
|
+
/** Components after the first (the first is matched by coverage). */
|
|
14
|
+
components: number[];
|
|
15
|
+
}
|
|
16
|
+
export type GsubSubtable = {
|
|
17
|
+
kind: 'single';
|
|
18
|
+
coverage: Coverage;
|
|
19
|
+
delta: number | undefined;
|
|
20
|
+
substitutes: number[];
|
|
21
|
+
} | {
|
|
22
|
+
kind: 'multiple';
|
|
23
|
+
coverage: Coverage;
|
|
24
|
+
sequences: number[][];
|
|
25
|
+
} | {
|
|
26
|
+
kind: 'alternate';
|
|
27
|
+
coverage: Coverage;
|
|
28
|
+
sets: number[][];
|
|
29
|
+
} | {
|
|
30
|
+
kind: 'ligature';
|
|
31
|
+
coverage: Coverage;
|
|
32
|
+
sets: Ligature[][];
|
|
33
|
+
} | {
|
|
34
|
+
kind: 'reverse';
|
|
35
|
+
coverage: Coverage;
|
|
36
|
+
backtrack: Coverage[];
|
|
37
|
+
lookahead: Coverage[];
|
|
38
|
+
substitutes: number[];
|
|
39
|
+
} | ContextualSubtable;
|
|
40
|
+
export interface GsubLookup {
|
|
41
|
+
type: number;
|
|
42
|
+
/** Packed lookup flags + mark filtering set (see lookupProps). */
|
|
43
|
+
props: number;
|
|
44
|
+
reverse: boolean;
|
|
45
|
+
subtables: GsubSubtable[];
|
|
46
|
+
}
|
|
47
|
+
export declare class Gsub {
|
|
48
|
+
readonly layout: LayoutTable;
|
|
49
|
+
private readonly lookups;
|
|
50
|
+
/** Applies one of this table's lookups at a position (also used for nested lookups). */
|
|
51
|
+
readonly dispatch: LookupDispatcher;
|
|
52
|
+
constructor(bytes: Uint8Array);
|
|
53
|
+
lookup(index: number): GsubLookup | undefined;
|
|
54
|
+
/**
|
|
55
|
+
* Whether any of the lookups would substitute exactly this glyph sequence (ignoring its
|
|
56
|
+
* surroundings). The Indic shaper asks this to learn, e.g., whether Ra+Halant forms a reph
|
|
57
|
+
* or a consonant has a below-base form, before deciding the syllable's base.
|
|
58
|
+
*/
|
|
59
|
+
wouldSubstitute(lookupIndices: readonly number[], glyphs: readonly number[], zeroContext: boolean): boolean;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Sets a glyph and updates its class: from GDEF when the font has glyph classes, else from
|
|
63
|
+
* the caller's guess (ligature / base), keeping the substitution history bits.
|
|
64
|
+
*/
|
|
65
|
+
export declare function replaceGlyph(ctx: ApplyContext, info: GlyphInfo, glyph: number, classGuess?: number, ligature?: boolean, component?: boolean): void;
|