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,508 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Unicode Bidirectional Algorithm (UAX #9): resolves an embedding level for every character of
|
|
3
|
+
* a paragraph and the visual order of a line, so right-to-left scripts (Hebrew, Arabic, ...) and
|
|
4
|
+
* mixed-direction text display in the right order.
|
|
5
|
+
*
|
|
6
|
+
* Implemented: P2–P3 (paragraph direction, skipping isolates), X1–X10 (explicit embeddings,
|
|
7
|
+
* overrides and isolates, depth limit 125 with overflow counters, isolating run sequences), W1–W7,
|
|
8
|
+
* N0 (paired brackets), N1–N2, I1–I2, L1 and L2. L3 (combining marks) and L4 (mirroring, via
|
|
9
|
+
* mirrorChar) belong to the renderer.
|
|
10
|
+
*
|
|
11
|
+
* Characters removed by X9 (LRE, RLE, LRO, RLO, PDF and BN) stay in place: they are skipped when
|
|
12
|
+
* building level runs and afterwards take the level of the preceding character, as the UAX #9
|
|
13
|
+
* reference implementation does, so callers get a level for every code unit and no level break
|
|
14
|
+
* appears where the controls sit.
|
|
15
|
+
*/
|
|
16
|
+
import { BC, BIDI_CLASSES, BPT_CLOSE, BPT_OPEN, bidiClassCode, mirrorGlyph, pairedBracket, pairedBracketType } from './bidi-data.js';
|
|
17
|
+
const { L, R, AL, EN, ES, ET, AN, CS, NSM, BN, B, S, WS, ON, LRE, LRO, RLE, RLO, PDF, LRI, RLI, FSI, PDI } = BC;
|
|
18
|
+
/** BD2: the deepest embedding level explicit controls may reach. */
|
|
19
|
+
const MAX_DEPTH = 125;
|
|
20
|
+
/** BD16: bracket pairing gives up beyond this many open brackets in one sequence. */
|
|
21
|
+
const MAX_BRACKET_STACK = 63;
|
|
22
|
+
export function bidiClass(cp) {
|
|
23
|
+
return BIDI_CLASSES[bidiClassCode(cp)];
|
|
24
|
+
}
|
|
25
|
+
/** True for strong right-to-left characters (classes R and AL). */
|
|
26
|
+
export function isRtlChar(cp) {
|
|
27
|
+
const c = bidiClassCode(cp);
|
|
28
|
+
return c === R || c === AL;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Bidi_Mirroring_Glyph, for rule L4: a character resolved to an odd level is drawn as its mirror,
|
|
32
|
+
* so "(" in right-to-left text still opens towards its content.
|
|
33
|
+
*/
|
|
34
|
+
export function mirrorChar(cp) {
|
|
35
|
+
return mirrorGlyph(cp);
|
|
36
|
+
}
|
|
37
|
+
const isRemovedByX9 = (t) => t === BN || (t >= LRE && t <= PDF);
|
|
38
|
+
const isIsolateInitiator = (t) => t >= LRI && t <= FSI;
|
|
39
|
+
/** Types N1/N2 resolve; isolate controls count as neutrals there. */
|
|
40
|
+
const isNeutral = (t) => t === B || t === S || t === WS || t === ON || (t >= LRI && t <= PDI);
|
|
41
|
+
/** L1 resets these when they trail a line or precede a separator (X9-removed ones included). */
|
|
42
|
+
const isL1Whitespace = (t) => t === WS || t === BN || (t >= LRE && t <= PDI);
|
|
43
|
+
/**
|
|
44
|
+
* Resolves embedding levels (rules P2–L1) for text that may hold several paragraphs: each
|
|
45
|
+
* paragraph separator (class B, with CR LF counted as one) ends a paragraph, and with 'auto' each
|
|
46
|
+
* paragraph finds its own direction. Line-end whitespace (the last clause of L1) is applied to the
|
|
47
|
+
* end of each paragraph; use lineLevels() for the other line ends once lines are broken.
|
|
48
|
+
*/
|
|
49
|
+
export function resolveLevels(text, paragraphLevel) {
|
|
50
|
+
const cps = [];
|
|
51
|
+
for (let i = 0; i < text.length; i++) {
|
|
52
|
+
const cp = text.codePointAt(i);
|
|
53
|
+
cps.push(cp);
|
|
54
|
+
if (cp > 0xffff)
|
|
55
|
+
i++;
|
|
56
|
+
}
|
|
57
|
+
const classes = new Uint8Array(cps.length);
|
|
58
|
+
for (let i = 0; i < cps.length; i++)
|
|
59
|
+
classes[i] = bidiClassCode(cps[i]);
|
|
60
|
+
const cpLevels = new Uint8Array(cps.length);
|
|
61
|
+
let firstLevel;
|
|
62
|
+
for (let start = 0; start < cps.length;) {
|
|
63
|
+
let end = start;
|
|
64
|
+
while (end < cps.length && classes[end] !== B)
|
|
65
|
+
end++;
|
|
66
|
+
if (end < cps.length) {
|
|
67
|
+
end++;
|
|
68
|
+
if (cps[end - 1] === 0x0d && cps[end] === 0x0a)
|
|
69
|
+
end++;
|
|
70
|
+
}
|
|
71
|
+
const paragraph = resolveParagraph(cps.slice(start, end), classes.subarray(start, end), paragraphLevel);
|
|
72
|
+
cpLevels.set(paragraph.levels, start);
|
|
73
|
+
firstLevel ??= paragraph.level;
|
|
74
|
+
start = end;
|
|
75
|
+
}
|
|
76
|
+
const levels = new Uint8Array(text.length);
|
|
77
|
+
for (let i = 0, unit = 0; i < cps.length; i++) {
|
|
78
|
+
levels[unit++] = cpLevels[i];
|
|
79
|
+
if (cps[i] > 0xffff)
|
|
80
|
+
levels[unit++] = cpLevels[i];
|
|
81
|
+
}
|
|
82
|
+
return { levels, paragraphLevel: firstLevel ?? (paragraphLevel === 'auto' ? 0 : paragraphLevel) };
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Rule L1 for a line break: a copy of `levels` in which the whitespace, isolate controls and
|
|
86
|
+
* X9-removed characters ending the line [start, end) are reset to the paragraph level. A wrapped
|
|
87
|
+
* line's trailing spaces then sit at its visual end instead of between right-to-left words.
|
|
88
|
+
*/
|
|
89
|
+
export function lineLevels(text, levels, paragraphLevel, start, end) {
|
|
90
|
+
const out = levels.slice();
|
|
91
|
+
for (let i = end; i > start;) {
|
|
92
|
+
let j = i - 1;
|
|
93
|
+
const unit = text.charCodeAt(j);
|
|
94
|
+
if (unit >= 0xdc00 && unit <= 0xdfff && j > start) {
|
|
95
|
+
const high = text.charCodeAt(j - 1);
|
|
96
|
+
if (high >= 0xd800 && high <= 0xdbff)
|
|
97
|
+
j--;
|
|
98
|
+
}
|
|
99
|
+
if (!isL1Whitespace(bidiClassCode(text.codePointAt(j))))
|
|
100
|
+
break;
|
|
101
|
+
out.fill(paragraphLevel, j, i);
|
|
102
|
+
i = j;
|
|
103
|
+
}
|
|
104
|
+
return out;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Rule L2 for one line: the logical indices start..end-1 in visual (left-to-right) order. From the
|
|
108
|
+
* highest level down to the lowest odd level, every maximal run at that level or higher is
|
|
109
|
+
* reversed. Indices are whatever `levels` is indexed by; with UTF-16 levels a surrogate pair at an
|
|
110
|
+
* odd level comes out low half first, so callers that place single characters should keep pairs
|
|
111
|
+
* (and combining sequences, rule L3) together themselves.
|
|
112
|
+
*/
|
|
113
|
+
export function visualOrder(levels, start, end) {
|
|
114
|
+
const order = [];
|
|
115
|
+
let highest = 0;
|
|
116
|
+
let lowestOdd = Infinity;
|
|
117
|
+
for (let i = start; i < end; i++) {
|
|
118
|
+
order.push(i);
|
|
119
|
+
const level = levels[i];
|
|
120
|
+
if (level > highest)
|
|
121
|
+
highest = level;
|
|
122
|
+
if (level & 1 && level < lowestOdd)
|
|
123
|
+
lowestOdd = level;
|
|
124
|
+
}
|
|
125
|
+
for (let level = highest; level >= lowestOdd; level--) {
|
|
126
|
+
for (let k = 0; k < order.length;) {
|
|
127
|
+
if (levels[order[k]] < level) {
|
|
128
|
+
k++;
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
let runEnd = k;
|
|
132
|
+
while (runEnd < order.length && levels[order[runEnd]] >= level)
|
|
133
|
+
runEnd++;
|
|
134
|
+
for (let a = k, b = runEnd - 1; a < b; a++, b--) {
|
|
135
|
+
const t = order[a];
|
|
136
|
+
order[a] = order[b];
|
|
137
|
+
order[b] = t;
|
|
138
|
+
}
|
|
139
|
+
k = runEnd;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return order;
|
|
143
|
+
}
|
|
144
|
+
/** P2: the first strong class (L, R or AL) in [from, to), skipping isolates; -1 if none. */
|
|
145
|
+
function firstStrong(types, from, to, matchingPDI) {
|
|
146
|
+
for (let i = from; i < to; i++) {
|
|
147
|
+
const t = types[i];
|
|
148
|
+
if (t === L || t === R || t === AL)
|
|
149
|
+
return t;
|
|
150
|
+
// Jump to the matching PDI (or the paragraph end), which the loop then steps past.
|
|
151
|
+
if (isIsolateInitiator(t))
|
|
152
|
+
i = matchingPDI[i];
|
|
153
|
+
}
|
|
154
|
+
return -1;
|
|
155
|
+
}
|
|
156
|
+
/** One paragraph (P2 to L1). `t0` holds the original classes; the last character may be a B. */
|
|
157
|
+
function resolveParagraph(cps, t0, requested) {
|
|
158
|
+
const n = t0.length;
|
|
159
|
+
// BD9: pair isolate initiators with PDIs. Unmatched initiators "match" the paragraph end (n).
|
|
160
|
+
const matchingPDI = new Int32Array(n).fill(-1);
|
|
161
|
+
const matchingInitiator = new Int32Array(n).fill(-1);
|
|
162
|
+
const openIsolates = [];
|
|
163
|
+
for (let i = 0; i < n; i++) {
|
|
164
|
+
const t = t0[i];
|
|
165
|
+
if (isIsolateInitiator(t))
|
|
166
|
+
openIsolates.push(i);
|
|
167
|
+
else if (t === PDI && openIsolates.length > 0) {
|
|
168
|
+
const initiator = openIsolates.pop();
|
|
169
|
+
matchingPDI[initiator] = i;
|
|
170
|
+
matchingInitiator[i] = initiator;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
for (const i of openIsolates)
|
|
174
|
+
matchingPDI[i] = n;
|
|
175
|
+
// P2, P3
|
|
176
|
+
let paraLevel;
|
|
177
|
+
if (requested === 'auto') {
|
|
178
|
+
const strong = firstStrong(t0, 0, n, matchingPDI);
|
|
179
|
+
paraLevel = strong === R || strong === AL ? 1 : 0;
|
|
180
|
+
}
|
|
181
|
+
else
|
|
182
|
+
paraLevel = requested;
|
|
183
|
+
// X1–X8: explicit levels. `types` receives override results; `embedding` the explicit levels.
|
|
184
|
+
const types = Uint8Array.from(t0);
|
|
185
|
+
const embedding = new Uint8Array(n);
|
|
186
|
+
const stackLevel = new Uint8Array(MAX_DEPTH + 2);
|
|
187
|
+
const stackOverride = new Uint8Array(MAX_DEPTH + 2);
|
|
188
|
+
const stackIsolate = new Uint8Array(MAX_DEPTH + 2);
|
|
189
|
+
stackLevel[0] = paraLevel;
|
|
190
|
+
stackOverride[0] = ON;
|
|
191
|
+
let depth = 1;
|
|
192
|
+
let overflowIsolates = 0;
|
|
193
|
+
let overflowEmbeddings = 0;
|
|
194
|
+
let validIsolates = 0;
|
|
195
|
+
for (let i = 0; i < n; i++) {
|
|
196
|
+
const t = t0[i];
|
|
197
|
+
const top = depth - 1;
|
|
198
|
+
if ((t >= LRE && t <= RLO) || isIsolateInitiator(t)) {
|
|
199
|
+
// X2–X5c
|
|
200
|
+
const isolate = isIsolateInitiator(t);
|
|
201
|
+
let rtl = t === RLE || t === RLO || t === RLI;
|
|
202
|
+
if (t === FSI) {
|
|
203
|
+
// X5c: an FSI is an RLI or LRI depending on its content's first strong character.
|
|
204
|
+
const strong = firstStrong(t0, i + 1, matchingPDI[i], matchingPDI);
|
|
205
|
+
rtl = strong === R || strong === AL;
|
|
206
|
+
}
|
|
207
|
+
if (isolate) {
|
|
208
|
+
// An isolate initiator belongs to the outside: it takes the current level and override.
|
|
209
|
+
embedding[i] = stackLevel[top];
|
|
210
|
+
if (stackOverride[top] !== ON)
|
|
211
|
+
types[i] = stackOverride[top];
|
|
212
|
+
}
|
|
213
|
+
const level = rtl ? (stackLevel[top] + 1) | 1 : (stackLevel[top] + 2) & ~1;
|
|
214
|
+
if (level <= MAX_DEPTH && overflowIsolates === 0 && overflowEmbeddings === 0) {
|
|
215
|
+
if (isolate)
|
|
216
|
+
validIsolates++;
|
|
217
|
+
stackLevel[depth] = level;
|
|
218
|
+
stackOverride[depth] = t === LRO ? L : t === RLO ? R : ON;
|
|
219
|
+
stackIsolate[depth] = isolate ? 1 : 0;
|
|
220
|
+
depth++;
|
|
221
|
+
if (!isolate)
|
|
222
|
+
embedding[i] = level;
|
|
223
|
+
}
|
|
224
|
+
else if (isolate)
|
|
225
|
+
overflowIsolates++;
|
|
226
|
+
else if (overflowIsolates === 0)
|
|
227
|
+
overflowEmbeddings++;
|
|
228
|
+
}
|
|
229
|
+
else if (t === PDI) {
|
|
230
|
+
// X6a
|
|
231
|
+
if (overflowIsolates > 0)
|
|
232
|
+
overflowIsolates--;
|
|
233
|
+
else if (validIsolates > 0) {
|
|
234
|
+
overflowEmbeddings = 0;
|
|
235
|
+
while (!stackIsolate[depth - 1])
|
|
236
|
+
depth--;
|
|
237
|
+
depth--;
|
|
238
|
+
validIsolates--;
|
|
239
|
+
}
|
|
240
|
+
embedding[i] = stackLevel[depth - 1];
|
|
241
|
+
if (stackOverride[depth - 1] !== ON)
|
|
242
|
+
types[i] = stackOverride[depth - 1];
|
|
243
|
+
}
|
|
244
|
+
else if (t === PDF) {
|
|
245
|
+
// X7
|
|
246
|
+
if (overflowIsolates > 0) {
|
|
247
|
+
// An overflowed isolate swallows PDFs until its PDI.
|
|
248
|
+
}
|
|
249
|
+
else if (overflowEmbeddings > 0)
|
|
250
|
+
overflowEmbeddings--;
|
|
251
|
+
else if (!stackIsolate[top] && depth >= 2)
|
|
252
|
+
depth--;
|
|
253
|
+
embedding[i] = stackLevel[depth - 1];
|
|
254
|
+
}
|
|
255
|
+
else if (t === B) {
|
|
256
|
+
// X8: B only ever ends the paragraph.
|
|
257
|
+
embedding[i] = paraLevel;
|
|
258
|
+
}
|
|
259
|
+
else {
|
|
260
|
+
// X6 (BN gets a level too, though X9 removes it)
|
|
261
|
+
embedding[i] = stackLevel[top];
|
|
262
|
+
if (t !== BN && stackOverride[top] !== ON)
|
|
263
|
+
types[i] = stackOverride[top];
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
// X9, X10 / BD7: level runs over the characters X9 keeps.
|
|
267
|
+
const runs = [];
|
|
268
|
+
const runOf = new Int32Array(n).fill(-1);
|
|
269
|
+
let run;
|
|
270
|
+
for (let i = 0; i < n; i++) {
|
|
271
|
+
if (isRemovedByX9(t0[i]))
|
|
272
|
+
continue;
|
|
273
|
+
if (!run || embedding[i] !== embedding[run[0]])
|
|
274
|
+
runs.push((run = []));
|
|
275
|
+
run.push(i);
|
|
276
|
+
runOf[i] = runs.length - 1;
|
|
277
|
+
}
|
|
278
|
+
// BD13: chain level runs across matched isolates into isolating run sequences. sos and eos are
|
|
279
|
+
// computed before any sequence is resolved since they compare explicit levels only.
|
|
280
|
+
const levels = new Uint8Array(n);
|
|
281
|
+
for (const levelRun of runs) {
|
|
282
|
+
const first = levelRun[0];
|
|
283
|
+
if (t0[first] === PDI && matchingInitiator[first] !== -1)
|
|
284
|
+
continue; // continues an earlier sequence
|
|
285
|
+
const sequence = [];
|
|
286
|
+
for (let current = levelRun;;) {
|
|
287
|
+
for (const i of current)
|
|
288
|
+
sequence.push(i);
|
|
289
|
+
const last = current[current.length - 1];
|
|
290
|
+
if (!isIsolateInitiator(t0[last]) || matchingPDI[last] >= n)
|
|
291
|
+
break;
|
|
292
|
+
current = runs[runOf[matchingPDI[last]]];
|
|
293
|
+
}
|
|
294
|
+
resolveSequence(sequence, cps, t0, types, embedding, paraLevel, levels);
|
|
295
|
+
}
|
|
296
|
+
// Removed characters follow the character before them, so they never split a run.
|
|
297
|
+
for (let i = 0; i < n; i++) {
|
|
298
|
+
if (isRemovedByX9(t0[i]))
|
|
299
|
+
levels[i] = i > 0 ? levels[i - 1] : paraLevel;
|
|
300
|
+
}
|
|
301
|
+
// L1: separators, the whitespace before them, and the whitespace ending the paragraph.
|
|
302
|
+
for (let i = 0; i < n; i++) {
|
|
303
|
+
const t = t0[i];
|
|
304
|
+
if (t !== B && t !== S)
|
|
305
|
+
continue;
|
|
306
|
+
levels[i] = paraLevel;
|
|
307
|
+
for (let j = i - 1; j >= 0 && isL1Whitespace(t0[j]); j--)
|
|
308
|
+
levels[j] = paraLevel;
|
|
309
|
+
}
|
|
310
|
+
for (let j = n - 1; j >= 0 && isL1Whitespace(t0[j]); j--)
|
|
311
|
+
levels[j] = paraLevel;
|
|
312
|
+
return { levels, level: paraLevel };
|
|
313
|
+
}
|
|
314
|
+
/** W1–W7, N0–N2 and I1–I2 for one isolating run sequence (paragraph indices in `seq`). */
|
|
315
|
+
function resolveSequence(seq, cps, t0, types, embedding, paraLevel, levels) {
|
|
316
|
+
const n = seq.length;
|
|
317
|
+
const total = t0.length;
|
|
318
|
+
const first = seq[0];
|
|
319
|
+
const last = seq[n - 1];
|
|
320
|
+
const level = embedding[first];
|
|
321
|
+
// X10: sos/eos from the higher of this level and the neighbouring (non-removed) character's.
|
|
322
|
+
let prev = first - 1;
|
|
323
|
+
while (prev >= 0 && isRemovedByX9(t0[prev]))
|
|
324
|
+
prev--;
|
|
325
|
+
const sos = Math.max(prev >= 0 ? embedding[prev] : paraLevel, level) & 1 ? R : L;
|
|
326
|
+
let nextLevel = paraLevel;
|
|
327
|
+
if (!isIsolateInitiator(t0[last])) {
|
|
328
|
+
let next = last + 1;
|
|
329
|
+
while (next < total && isRemovedByX9(t0[next]))
|
|
330
|
+
next++;
|
|
331
|
+
if (next < total)
|
|
332
|
+
nextLevel = embedding[next];
|
|
333
|
+
}
|
|
334
|
+
const eos = Math.max(nextLevel, level) & 1 ? R : L;
|
|
335
|
+
const ts = new Uint8Array(n);
|
|
336
|
+
for (let k = 0; k < n; k++)
|
|
337
|
+
ts[k] = types[seq[k]];
|
|
338
|
+
// W1: NSM takes the type of the character before it (ON after an isolate control).
|
|
339
|
+
let before = sos;
|
|
340
|
+
for (let k = 0; k < n; k++) {
|
|
341
|
+
const t = ts[k];
|
|
342
|
+
if (t === NSM)
|
|
343
|
+
ts[k] = before;
|
|
344
|
+
else
|
|
345
|
+
before = t >= LRI && t <= PDI ? ON : t;
|
|
346
|
+
}
|
|
347
|
+
// W2: EN after AL is an Arabic number.
|
|
348
|
+
let strong = sos;
|
|
349
|
+
for (let k = 0; k < n; k++) {
|
|
350
|
+
const t = ts[k];
|
|
351
|
+
if (t === L || t === R || t === AL)
|
|
352
|
+
strong = t;
|
|
353
|
+
else if (t === EN && strong === AL)
|
|
354
|
+
ts[k] = AN;
|
|
355
|
+
}
|
|
356
|
+
// W3
|
|
357
|
+
for (let k = 0; k < n; k++)
|
|
358
|
+
if (ts[k] === AL)
|
|
359
|
+
ts[k] = R;
|
|
360
|
+
// W4: a single separator between two numbers of the same kind joins them.
|
|
361
|
+
for (let k = 1; k < n - 1; k++) {
|
|
362
|
+
const t = ts[k];
|
|
363
|
+
if (t !== ES && t !== CS)
|
|
364
|
+
continue;
|
|
365
|
+
const a = ts[k - 1];
|
|
366
|
+
const b = ts[k + 1];
|
|
367
|
+
if (a === EN && b === EN)
|
|
368
|
+
ts[k] = EN;
|
|
369
|
+
else if (t === CS && a === AN && b === AN)
|
|
370
|
+
ts[k] = AN;
|
|
371
|
+
}
|
|
372
|
+
// W5: terminators next to a European number become part of it.
|
|
373
|
+
for (let k = 0; k < n; k++) {
|
|
374
|
+
if (ts[k] !== ET)
|
|
375
|
+
continue;
|
|
376
|
+
let end = k;
|
|
377
|
+
while (end < n && ts[end] === ET)
|
|
378
|
+
end++;
|
|
379
|
+
if ((k === 0 ? sos : ts[k - 1]) === EN || (end === n ? eos : ts[end]) === EN)
|
|
380
|
+
ts.fill(EN, k, end);
|
|
381
|
+
k = end - 1;
|
|
382
|
+
}
|
|
383
|
+
// W6
|
|
384
|
+
for (let k = 0; k < n; k++) {
|
|
385
|
+
const t = ts[k];
|
|
386
|
+
if (t === ES || t === ET || t === CS)
|
|
387
|
+
ts[k] = ON;
|
|
388
|
+
}
|
|
389
|
+
// W7: European numbers in a left-to-right context are L.
|
|
390
|
+
strong = sos;
|
|
391
|
+
for (let k = 0; k < n; k++) {
|
|
392
|
+
const t = ts[k];
|
|
393
|
+
if (t === L || t === R)
|
|
394
|
+
strong = t;
|
|
395
|
+
else if (t === EN && strong === L)
|
|
396
|
+
ts[k] = L;
|
|
397
|
+
}
|
|
398
|
+
const direction = level & 1 ? R : L;
|
|
399
|
+
resolveBrackets(seq, cps, t0, ts, direction, sos);
|
|
400
|
+
// N1/N2: neutral runs between same-direction text take that direction, else the embedding's.
|
|
401
|
+
for (let k = 0; k < n; k++) {
|
|
402
|
+
if (!isNeutral(ts[k]))
|
|
403
|
+
continue;
|
|
404
|
+
let end = k;
|
|
405
|
+
while (end < n && isNeutral(ts[end]))
|
|
406
|
+
end++;
|
|
407
|
+
let lead = k === 0 ? sos : ts[k - 1];
|
|
408
|
+
let trail = end === n ? eos : ts[end];
|
|
409
|
+
if (lead === EN || lead === AN)
|
|
410
|
+
lead = R;
|
|
411
|
+
if (trail === EN || trail === AN)
|
|
412
|
+
trail = R;
|
|
413
|
+
ts.fill(lead === trail ? lead : direction, k, end);
|
|
414
|
+
k = end - 1;
|
|
415
|
+
}
|
|
416
|
+
// I1, I2
|
|
417
|
+
for (let k = 0; k < n; k++) {
|
|
418
|
+
const t = ts[k];
|
|
419
|
+
let resolved = level;
|
|
420
|
+
if ((level & 1) === 0) {
|
|
421
|
+
if (t === R)
|
|
422
|
+
resolved += 1;
|
|
423
|
+
else if (t === AN || t === EN)
|
|
424
|
+
resolved += 2;
|
|
425
|
+
}
|
|
426
|
+
else if (t === L || t === EN || t === AN)
|
|
427
|
+
resolved += 1;
|
|
428
|
+
levels[seq[k]] = resolved;
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
/** Canonical equivalents U+2329/U+232A ≡ U+3008/U+3009 must pair with each other (BD16). */
|
|
432
|
+
function canonicalBracket(cp) {
|
|
433
|
+
return cp === 0x2329 ? 0x3008 : cp === 0x232a ? 0x3009 : cp;
|
|
434
|
+
}
|
|
435
|
+
/** N0's view of a type: EN and AN count as R. */
|
|
436
|
+
function strongForN0(t) {
|
|
437
|
+
if (t === L)
|
|
438
|
+
return L;
|
|
439
|
+
if (t === R || t === AL || t === EN || t === AN)
|
|
440
|
+
return R;
|
|
441
|
+
return ON;
|
|
442
|
+
}
|
|
443
|
+
/** N0: paired brackets take the direction of their content, or of the context around them. */
|
|
444
|
+
function resolveBrackets(seq, cps, t0, ts, direction, sos) {
|
|
445
|
+
const n = seq.length;
|
|
446
|
+
// BD16: pair brackets that are still ON (overridden ones are not brackets any more).
|
|
447
|
+
const openClosers = [];
|
|
448
|
+
const openPositions = [];
|
|
449
|
+
const pairs = [];
|
|
450
|
+
for (let k = 0; k < n; k++) {
|
|
451
|
+
if (ts[k] !== ON)
|
|
452
|
+
continue;
|
|
453
|
+
const cp = cps[seq[k]];
|
|
454
|
+
const kind = pairedBracketType(cp);
|
|
455
|
+
if (kind === BPT_OPEN) {
|
|
456
|
+
if (openPositions.length === MAX_BRACKET_STACK)
|
|
457
|
+
break;
|
|
458
|
+
openClosers.push(canonicalBracket(pairedBracket(cp)));
|
|
459
|
+
openPositions.push(k);
|
|
460
|
+
}
|
|
461
|
+
else if (kind === BPT_CLOSE) {
|
|
462
|
+
const closer = canonicalBracket(cp);
|
|
463
|
+
for (let s = openPositions.length - 1; s >= 0; s--) {
|
|
464
|
+
if (openClosers[s] !== closer)
|
|
465
|
+
continue;
|
|
466
|
+
pairs.push([openPositions[s], k]);
|
|
467
|
+
openPositions.length = s;
|
|
468
|
+
openClosers.length = s;
|
|
469
|
+
break;
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
pairs.sort((a, b) => a[0] - b[0]);
|
|
474
|
+
// In order of the opening brackets, so pairs resolved earlier inform later ones.
|
|
475
|
+
for (const [open, close] of pairs) {
|
|
476
|
+
let inside = ON;
|
|
477
|
+
for (let k = open + 1; k < close; k++) {
|
|
478
|
+
const s = strongForN0(ts[k]);
|
|
479
|
+
if (s === ON)
|
|
480
|
+
continue;
|
|
481
|
+
inside = s;
|
|
482
|
+
if (s === direction)
|
|
483
|
+
break;
|
|
484
|
+
}
|
|
485
|
+
if (inside === ON)
|
|
486
|
+
continue; // N0 d: no strong type inside, leave to N1/N2
|
|
487
|
+
let resolved = inside; // N0 b: matches the embedding direction
|
|
488
|
+
if (inside !== direction) {
|
|
489
|
+
// N0 c: opposite direction inside; keep it only if the preceding context agrees.
|
|
490
|
+
let context = sos;
|
|
491
|
+
for (let k = open - 1; k >= 0; k--) {
|
|
492
|
+
const s = strongForN0(ts[k]);
|
|
493
|
+
if (s !== ON) {
|
|
494
|
+
context = s;
|
|
495
|
+
break;
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
resolved = context === inside ? inside : direction;
|
|
499
|
+
}
|
|
500
|
+
ts[open] = resolved;
|
|
501
|
+
ts[close] = resolved;
|
|
502
|
+
// Marks that originally followed a bracket (W1 gave them its ON) follow its new type too.
|
|
503
|
+
for (let k = open + 1; k < n && t0[seq[k]] === NSM; k++)
|
|
504
|
+
ts[k] = resolved;
|
|
505
|
+
for (let k = close + 1; k < n && t0[seq[k]] === NSM; k++)
|
|
506
|
+
ts[k] = resolved;
|
|
507
|
+
}
|
|
508
|
+
}
|
package/dist/xml/ns.d.ts
CHANGED
|
@@ -23,6 +23,11 @@ export declare const NS: {
|
|
|
23
23
|
readonly wps: 'http://schemas.microsoft.com/office/word/2010/wordprocessingShape';
|
|
24
24
|
readonly wpg: 'http://schemas.microsoft.com/office/word/2010/wordprocessingGroup';
|
|
25
25
|
readonly wpc: 'http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas';
|
|
26
|
+
readonly c: 'http://schemas.openxmlformats.org/drawingml/2006/chart';
|
|
27
|
+
readonly dgm: 'http://schemas.openxmlformats.org/drawingml/2006/diagram';
|
|
28
|
+
readonly dsp: 'http://schemas.microsoft.com/office/drawing/2008/diagram';
|
|
29
|
+
readonly c14: 'http://schemas.microsoft.com/office/drawing/2007/8/2/chart';
|
|
30
|
+
readonly a14: 'http://schemas.microsoft.com/office/drawing/2010/main';
|
|
26
31
|
readonly cp: 'http://schemas.openxmlformats.org/package/2006/metadata/core-properties';
|
|
27
32
|
readonly dc: 'http://purl.org/dc/elements/1.1/';
|
|
28
33
|
readonly dcterms: 'http://purl.org/dc/terms/';
|
package/dist/xml/ns.js
CHANGED
|
@@ -18,6 +18,11 @@ export const NS = {
|
|
|
18
18
|
wps: 'http://schemas.microsoft.com/office/word/2010/wordprocessingShape',
|
|
19
19
|
wpg: 'http://schemas.microsoft.com/office/word/2010/wordprocessingGroup',
|
|
20
20
|
wpc: 'http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas',
|
|
21
|
+
c: 'http://schemas.openxmlformats.org/drawingml/2006/chart',
|
|
22
|
+
dgm: 'http://schemas.openxmlformats.org/drawingml/2006/diagram',
|
|
23
|
+
dsp: 'http://schemas.microsoft.com/office/drawing/2008/diagram',
|
|
24
|
+
c14: 'http://schemas.microsoft.com/office/drawing/2007/8/2/chart',
|
|
25
|
+
a14: 'http://schemas.microsoft.com/office/drawing/2010/main',
|
|
21
26
|
cp: 'http://schemas.openxmlformats.org/package/2006/metadata/core-properties',
|
|
22
27
|
dc: 'http://purl.org/dc/elements/1.1/',
|
|
23
28
|
dcterms: 'http://purl.org/dc/terms/',
|
|
@@ -36,6 +41,8 @@ const STRICT = {
|
|
|
36
41
|
'http://purl.oclc.org/ooxml/drawingml/main': 'a',
|
|
37
42
|
'http://purl.oclc.org/ooxml/drawingml/picture': 'pic',
|
|
38
43
|
'http://purl.oclc.org/ooxml/officeDocument/math': 'm',
|
|
44
|
+
'http://purl.oclc.org/ooxml/drawingml/chart': 'c',
|
|
45
|
+
'http://purl.oclc.org/ooxml/drawingml/diagram': 'dgm',
|
|
39
46
|
};
|
|
40
47
|
/** Namespaces whose elements are conventionally unprefixed (package parts); never rewritten. */
|
|
41
48
|
const DEFAULT_ONLY = new Set([NS.pkgRel, NS.contentTypes, NS.ep]);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "docxkeun",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Read, render, edit and convert .docx files in the browser: a DOCX viewer
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "Read, render, edit and convert .docx files in the browser with zero runtime dependencies: a DOCX viewer and editor (toolbar, comments, tracked changes, undo), a DOCX-to-PDF converter, and live effects for AI edits.",
|
|
5
5
|
"author": "Kevin Jeremia (https://github.com/kevinkjs)",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": {
|
|
@@ -89,6 +89,12 @@
|
|
|
89
89
|
},
|
|
90
90
|
"scripts": {
|
|
91
91
|
"build": "tsc -p tsconfig.build.json",
|
|
92
|
+
"build:mcp": "pnpm -C packages/mcp build",
|
|
93
|
+
"typecheck:mcp": "pnpm -C packages/mcp typecheck",
|
|
94
|
+
"dev:chat": "pnpm -C packages/chat dev",
|
|
95
|
+
"build:chat": "pnpm -C packages/chat build",
|
|
96
|
+
"start:chat": "pnpm -C packages/chat start",
|
|
97
|
+
"typecheck:chat": "pnpm -C packages/chat typecheck",
|
|
92
98
|
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
93
99
|
"test": "vitest run",
|
|
94
100
|
"test:watch": "vitest",
|
|
@@ -101,6 +107,7 @@
|
|
|
101
107
|
},
|
|
102
108
|
"devDependencies": {
|
|
103
109
|
"@types/node": "^26.6.6",
|
|
110
|
+
"happy-dom": "21.0.4",
|
|
104
111
|
"typescript": "^7.0.2",
|
|
105
112
|
"vite": "^8.3.5",
|
|
106
113
|
"vitest": "^5.0.5"
|