docxkeun 0.1.3 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +48 -0
- package/README.md +137 -16
- package/dist/api/editor.d.ts +172 -5
- package/dist/api/editor.js +814 -24
- package/dist/api/headers.d.ts +51 -0
- package/dist/api/headers.js +133 -0
- package/dist/api/index.d.ts +5 -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 +390 -0
- package/dist/api/selection.d.ts +57 -0
- package/dist/api/selection.js +91 -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 +138 -0
- package/dist/render/activity.js +794 -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 +302 -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
package/dist/layout/blocks.js
CHANGED
|
@@ -3,31 +3,12 @@
|
|
|
3
3
|
* with keep rules; the paginator stacks boxes into columns and pages.
|
|
4
4
|
*/
|
|
5
5
|
import { blocksToText } from '../model/content.js';
|
|
6
|
+
import { normaliseBullet } from '../model/numbering.js';
|
|
6
7
|
import { isVisibleBorder, mergeRunProps, shadingColor } from '../model/props.js';
|
|
8
|
+
import { paragraphKey } from './cache.js';
|
|
7
9
|
import { breakLines, buildAtoms, styleFont } from './lines.js';
|
|
8
10
|
import { addItem, borderLine, paintLine } from './paint.js';
|
|
9
11
|
import { resolveRunStyle } from './style.js';
|
|
10
|
-
/** Private-use symbol-font bullets mapped to Unicode equivalents. */
|
|
11
|
-
const SYMBOL_BULLETS = {
|
|
12
|
-
0xf0b7: '•',
|
|
13
|
-
0xf0a7: '▪',
|
|
14
|
-
0xf0a8: '◆',
|
|
15
|
-
0xf0d8: '➢',
|
|
16
|
-
0xf076: '❖',
|
|
17
|
-
0xf0fc: '✓',
|
|
18
|
-
0xf06e: '■',
|
|
19
|
-
0xf071: '❑',
|
|
20
|
-
0xf0e0: '➢',
|
|
21
|
-
0xf06f: '□',
|
|
22
|
-
};
|
|
23
|
-
function normaliseBullet(text) {
|
|
24
|
-
let out = '';
|
|
25
|
-
for (const ch of text) {
|
|
26
|
-
const cp = ch.codePointAt(0);
|
|
27
|
-
out += SYMBOL_BULLETS[cp] ?? (cp >= 0xf000 && cp <= 0xf0ff ? '•' : ch);
|
|
28
|
-
}
|
|
29
|
-
return out;
|
|
30
|
-
}
|
|
31
12
|
function sameBorders(a, b) {
|
|
32
13
|
return JSON.stringify(a ?? {}) === JSON.stringify(b ?? {});
|
|
33
14
|
}
|
|
@@ -63,8 +44,19 @@ export function paragraphBoxes(para, width, ctx, state = {}) {
|
|
|
63
44
|
label = { text: normaliseBullet(next.label), props: labelProps, suffix: next.level.suffix };
|
|
64
45
|
}
|
|
65
46
|
}
|
|
66
|
-
|
|
67
|
-
const
|
|
47
|
+
// Note numbers depend on order: ask for them even when the paragraph comes from the cache.
|
|
48
|
+
const noteLabels = para.inlines.filter((i) => i.kind === 'noteRef').map((i) => ctx.noteLabel(i));
|
|
49
|
+
const cacheKey = ctx.cache ? paragraphKey(para, [width, label, noteLabels, ctx.tracker.ctx, ctx.defaultTab]) : undefined;
|
|
50
|
+
const cached = cacheKey !== undefined ? ctx.cache.get(para.el, cacheKey) : undefined;
|
|
51
|
+
let atoms;
|
|
52
|
+
if (cached) {
|
|
53
|
+
atoms = cached.atoms;
|
|
54
|
+
ctx.tracker.ctx = cached.trackerAfter;
|
|
55
|
+
}
|
|
56
|
+
else
|
|
57
|
+
atoms = buildAtoms(para, { fonts: ctx.fonts, theme: ctx.theme, tracker: ctx.tracker, noteLabel: ctx.noteLabel }, label);
|
|
58
|
+
const spacing = props.spacing ?? {};
|
|
59
|
+
const geometry = {
|
|
68
60
|
firstLeft: left + firstLine,
|
|
69
61
|
left,
|
|
70
62
|
right: width - right,
|
|
@@ -72,10 +64,18 @@ export function paragraphBoxes(para, width, ctx, state = {}) {
|
|
|
72
64
|
tabs: props.tabs ?? [],
|
|
73
65
|
defaultTab: ctx.defaultTab,
|
|
74
66
|
jc: props.jc ?? 'left',
|
|
67
|
+
// Right-to-left: indents, tabs and alignment run from the right edge; lines are mirrored.
|
|
68
|
+
...(props.bidi ? { mirror: width } : {}),
|
|
75
69
|
markAscent: mark.font.ascent * mark.size,
|
|
76
70
|
markDescent: mark.font.descent * mark.size,
|
|
77
|
-
|
|
78
|
-
|
|
71
|
+
lineHeight: (line) => lineHeight(line, spacing.line, spacing.lineRule).height,
|
|
72
|
+
};
|
|
73
|
+
// Line breaking makes a soft hyphen at a line end visible; remember them for re-breaking.
|
|
74
|
+
const softHyphens = atoms.filter((a) => a.kind === 'char' && !!a.softHyphen).map((a) => ({ atom: a, ch: a.ch, width: a.width }));
|
|
75
|
+
const lines = cached ? cached.lines : breakLines(atoms, geometry);
|
|
76
|
+
// Soft hyphens change their atoms when lines are broken again around floats: not cached.
|
|
77
|
+
if (cacheKey !== undefined && !cached && !softHyphens.length)
|
|
78
|
+
ctx.cache.set(para.el, cacheKey, { atoms, lines, trackerAfter: ctx.tracker.ctx });
|
|
79
79
|
let before = spacing.beforeAutospacing ? 14 : (spacing.before ?? 0);
|
|
80
80
|
const after = spacing.afterAutospacing ? 14 : (spacing.after ?? 0);
|
|
81
81
|
if (props.contextualSpacing && state.styleId === para.styleId)
|
|
@@ -88,8 +88,10 @@ export function paragraphBoxes(para, width, ctx, state = {}) {
|
|
|
88
88
|
const shading = shadingColor(props.shading);
|
|
89
89
|
const boxes = [];
|
|
90
90
|
// Decoration bounds (borders sit outside the text indents).
|
|
91
|
-
|
|
92
|
-
const
|
|
91
|
+
// In a right-to-left paragraph the start indent (w:left) is on the right.
|
|
92
|
+
const [startInd, endInd] = props.bidi ? [right, left] : [left, right];
|
|
93
|
+
const decoLeft = startInd - (isVisibleBorder(borders?.left) ? borders.left.space + borders.left.width : 0);
|
|
94
|
+
const decoRight = width - endInd + (isVisibleBorder(borders?.right) ? borders.right.space + borders.right.width : 0);
|
|
93
95
|
const decorate = (p, x, y, h) => {
|
|
94
96
|
if (shading)
|
|
95
97
|
addItem(p, { type: 'rect', x: x + decoLeft, y, w: decoRight - decoLeft, h, fill: shading });
|
|
@@ -136,40 +138,49 @@ export function paragraphBoxes(para, width, ctx, state = {}) {
|
|
|
136
138
|
});
|
|
137
139
|
}
|
|
138
140
|
}
|
|
139
|
-
const
|
|
141
|
+
const pre = boxes.splice(0);
|
|
140
142
|
const widow = props.widowControl ?? true;
|
|
141
143
|
const outline = props.outlineLevel !== undefined && props.outlineLevel < 9 ? { level: props.outlineLevel, title: blocksToText([para]).replace(/\s+/g, ' ').trim() } : undefined;
|
|
142
|
-
lines
|
|
143
|
-
const
|
|
144
|
-
const
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
keep =
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
144
|
+
const lineBoxes = (lines) => {
|
|
145
|
+
const out = [];
|
|
146
|
+
const n = lines.length;
|
|
147
|
+
lines.forEach((line, i) => {
|
|
148
|
+
const natural = lineHeight(line, spacing.line, spacing.lineRule);
|
|
149
|
+
const gap = line.gapBefore ?? 0;
|
|
150
|
+
const height = natural.height + gap;
|
|
151
|
+
const baseline = natural.baseline + gap;
|
|
152
|
+
const notes = [];
|
|
153
|
+
for (const f of line.fragments)
|
|
154
|
+
if (f.kind === 'mark' && (f.mark === 'footnote' || f.mark === 'endnote'))
|
|
155
|
+
notes.push({ type: f.mark, id: f.name });
|
|
156
|
+
let keep = false;
|
|
157
|
+
if (props.keepLines && i < n - 1)
|
|
158
|
+
keep = true;
|
|
159
|
+
if (widow && n >= 2 && (i === 0 || i === n - 2))
|
|
160
|
+
keep = true;
|
|
161
|
+
if (i === n - 1 && props.keepNext)
|
|
162
|
+
keep = true;
|
|
163
|
+
out.push({
|
|
164
|
+
height,
|
|
165
|
+
keepWithNext: keep,
|
|
166
|
+
breakBefore: i === 0 && props.pageBreakBefore ? 'page' : undefined,
|
|
167
|
+
breakAfter: line.breakAfter,
|
|
168
|
+
notes: notes.length ? notes : undefined,
|
|
169
|
+
paint: (p, x, y) => {
|
|
170
|
+
decorate(p, x, y, height);
|
|
171
|
+
if (i === 0 && outline?.title)
|
|
172
|
+
p.page.headings.push({ ...outline, x: x + line.left, y: y + gap });
|
|
173
|
+
const placed = { x: x + line.left, w: line.right - line.left, y: y + gap, h: natural.height, baseline: y + baseline, paragraph: para.el };
|
|
174
|
+
if (p.story === 'body')
|
|
175
|
+
p.page.lines.push(placed);
|
|
176
|
+
else if (p.storyRef)
|
|
177
|
+
p.page.storyLines.push({ ...placed, ...p.storyRef, story: p.story });
|
|
178
|
+
paintLine(p, line, x, { top: y + gap, height: natural.height, baseline: y + baseline });
|
|
179
|
+
},
|
|
180
|
+
});
|
|
169
181
|
});
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
boxes[0].breakBefore = 'page';
|
|
182
|
+
return out;
|
|
183
|
+
};
|
|
173
184
|
let bottomBorderBox;
|
|
174
185
|
if (hasBorders && isVisibleBorder(borders.bottom)) {
|
|
175
186
|
const bottom = borders.bottom;
|
|
@@ -186,6 +197,25 @@ export function paragraphBoxes(para, width, ctx, state = {}) {
|
|
|
186
197
|
}
|
|
187
198
|
const afterBox = { height: after, keepWithNext: !!props.keepNext, canTruncate: true, paint: () => { } };
|
|
188
199
|
boxes.push(afterBox);
|
|
200
|
+
const post = boxes.splice(0);
|
|
201
|
+
const floats = atoms.filter((a) => a.kind === 'float').map((a) => a.inline);
|
|
202
|
+
const assemble = (body, meta) => {
|
|
203
|
+
const all = [...pre, ...body, ...post];
|
|
204
|
+
if (props.pageBreakBefore && all[0])
|
|
205
|
+
all[0] = { ...all[0], breakBefore: 'page' };
|
|
206
|
+
if (all[0])
|
|
207
|
+
all[0] = { ...all[0], paragraph: { ...meta, count: all.length } };
|
|
208
|
+
return all;
|
|
209
|
+
};
|
|
210
|
+
const textOffset = pre.reduce((sum, b) => sum + b.height, 0);
|
|
211
|
+
const reflow = (bounds) => {
|
|
212
|
+
for (const h of softHyphens) {
|
|
213
|
+
h.atom.ch = h.ch;
|
|
214
|
+
h.atom.width = h.width;
|
|
215
|
+
}
|
|
216
|
+
return assemble(lineBoxes(breakLines(atoms, { ...geometry, bounds })), { textOffset, floats, reflow, wrapped: true });
|
|
217
|
+
};
|
|
218
|
+
boxes.push(...assemble(lineBoxes(lines), { textOffset, floats, reflow }));
|
|
189
219
|
state.styleId = para.styleId;
|
|
190
220
|
state.contextual = props.contextualSpacing;
|
|
191
221
|
state.afterBox = afterBox;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Re-using paragraph layout between runs. Measuring characters and breaking lines is most of
|
|
3
|
+
* layout's time; a paragraph whose content, width and surroundings are unchanged since the last
|
|
4
|
+
* run gets its previous atoms and lines back, so typing re-measures only the paragraph typed in.
|
|
5
|
+
*/
|
|
6
|
+
import type { DocParagraph } from '../model/content.js';
|
|
7
|
+
import type { XmlElement } from '../xml/index.js';
|
|
8
|
+
import type { Atom, Line } from './lines.js';
|
|
9
|
+
/** @internal */
|
|
10
|
+
export interface CachedParagraph {
|
|
11
|
+
atoms: Atom[];
|
|
12
|
+
lines: Line[];
|
|
13
|
+
/** The range tracker's state after the paragraph. */
|
|
14
|
+
trackerAfter: unknown;
|
|
15
|
+
}
|
|
16
|
+
/** Keep one per document and pass it to every `layoutDocument` call (the viewer does). */
|
|
17
|
+
export declare class LayoutCache {
|
|
18
|
+
private paragraphs;
|
|
19
|
+
private owners;
|
|
20
|
+
hits: number;
|
|
21
|
+
misses: number;
|
|
22
|
+
/** @internal Start a run: anything resolved against other styles, theme or fonts is dropped. */
|
|
23
|
+
begin(owners: unknown[]): void;
|
|
24
|
+
/** @internal */
|
|
25
|
+
get(el: XmlElement, key: string): CachedParagraph | undefined;
|
|
26
|
+
/** @internal */
|
|
27
|
+
set(el: XmlElement, key: string, value: CachedParagraph): void;
|
|
28
|
+
clear(): void;
|
|
29
|
+
}
|
|
30
|
+
/** Everything about a paragraph that its atoms and lines depend on (XML nodes left out: their content is in the model). */
|
|
31
|
+
export declare function paragraphKey(para: DocParagraph, parts: unknown[]): string;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/** Keep one per document and pass it to every `layoutDocument` call (the viewer does). */
|
|
2
|
+
export class LayoutCache {
|
|
3
|
+
paragraphs = new WeakMap();
|
|
4
|
+
owners = [];
|
|
5
|
+
hits = 0;
|
|
6
|
+
misses = 0;
|
|
7
|
+
/** @internal Start a run: anything resolved against other styles, theme or fonts is dropped. */
|
|
8
|
+
begin(owners) {
|
|
9
|
+
if (owners.length !== this.owners.length || owners.some((o, i) => o !== this.owners[i])) {
|
|
10
|
+
this.paragraphs = new WeakMap();
|
|
11
|
+
this.owners = owners;
|
|
12
|
+
}
|
|
13
|
+
this.hits = 0;
|
|
14
|
+
this.misses = 0;
|
|
15
|
+
}
|
|
16
|
+
/** @internal */
|
|
17
|
+
get(el, key) {
|
|
18
|
+
const e = this.paragraphs.get(el);
|
|
19
|
+
if (e && e.key === key) {
|
|
20
|
+
this.hits++;
|
|
21
|
+
return e.value;
|
|
22
|
+
}
|
|
23
|
+
this.misses++;
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
/** @internal */
|
|
27
|
+
set(el, key, value) {
|
|
28
|
+
this.paragraphs.set(el, { key, value });
|
|
29
|
+
}
|
|
30
|
+
clear() {
|
|
31
|
+
this.paragraphs = new WeakMap();
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
const isXml = (v) => !!v && typeof v === 'object' && v.type === 'element' && Array.isArray(v.children);
|
|
35
|
+
/** Everything about a paragraph that its atoms and lines depend on (XML nodes left out: their content is in the model). */
|
|
36
|
+
export function paragraphKey(para, parts) {
|
|
37
|
+
return JSON.stringify(parts) + JSON.stringify(para, (_k, v) => (isXml(v) ? undefined : v));
|
|
38
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Drawing objects into the display list: pictures (rotated and flipped), metafile pictures as
|
|
3
|
+
* vectors, and — through `DrawingContext.content` — shapes, text boxes, groups, charts and SmartArt.
|
|
4
|
+
*/
|
|
5
|
+
import type { ImageInline } from '../model/content.js';
|
|
6
|
+
import type { DocxDocument } from '../model/document.js';
|
|
7
|
+
import type { Painter } from './paint.js';
|
|
8
|
+
import type { DisplayItem, FontProvider, PathCommand, Rect } from './types.js';
|
|
9
|
+
/** What painting drawings needs beyond the page: the document and fonts. */
|
|
10
|
+
export interface DrawingContext {
|
|
11
|
+
doc: DocxDocument;
|
|
12
|
+
fonts: FontProvider;
|
|
13
|
+
/** Draws an object's `content` (shapes, groups, charts, SmartArt) into `frame`; returns false when it cannot. */
|
|
14
|
+
content?(p: Painter, image: ImageInline, frame: Rect): boolean;
|
|
15
|
+
}
|
|
16
|
+
/** A transform from an object's own box to the page: rotation about the centre and flips. */
|
|
17
|
+
export declare function objectTransform(frame: Rect, rotation?: number, flipH?: boolean, flipV?: boolean): ((x: number, y: number) => [number, number]) | undefined;
|
|
18
|
+
/** Map path coordinates through a transform. */
|
|
19
|
+
export declare function transformCommands(commands: PathCommand[], t: (x: number, y: number) => [number, number]): PathCommand[];
|
|
20
|
+
/** Move display items by (dx, dy). */
|
|
21
|
+
export declare function translateItems(items: DisplayItem[], dx: number, dy: number): DisplayItem[];
|
|
22
|
+
/** Rotate / flip display items drawn for an object (paths and images; text keeps its baseline). */
|
|
23
|
+
export declare function transformItems(items: DisplayItem[], t: ((x: number, y: number) => [number, number]) | undefined): DisplayItem[];
|
|
24
|
+
/**
|
|
25
|
+
* Draw an inline or floating object. Returns the items (the first is the object's frame, used
|
|
26
|
+
* for hit-testing and the editing caret) for the caller to place in front of or behind the text.
|
|
27
|
+
*/
|
|
28
|
+
export declare function drawObject(p: Painter, image: ImageInline, frame: Rect): DisplayItem[];
|
|
29
|
+
/** A light box for objects that cannot be drawn. */
|
|
30
|
+
export declare function placeholder(frame: Rect): DisplayItem;
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { metafileKind, renderMetafile } from '../metafile/index.js';
|
|
2
|
+
/** A transform from an object's own box to the page: rotation about the centre and flips. */
|
|
3
|
+
export function objectTransform(frame, rotation = 0, flipH = false, flipV = false) {
|
|
4
|
+
if (!rotation && !flipH && !flipV)
|
|
5
|
+
return undefined;
|
|
6
|
+
const cx = frame.x + frame.w / 2;
|
|
7
|
+
const cy = frame.y + frame.h / 2;
|
|
8
|
+
const a = (rotation * Math.PI) / 180;
|
|
9
|
+
const cos = Math.cos(a);
|
|
10
|
+
const sin = Math.sin(a);
|
|
11
|
+
return (x, y) => {
|
|
12
|
+
let dx = x - cx;
|
|
13
|
+
let dy = y - cy;
|
|
14
|
+
if (flipH)
|
|
15
|
+
dx = -dx;
|
|
16
|
+
if (flipV)
|
|
17
|
+
dy = -dy;
|
|
18
|
+
return [cx + dx * cos - dy * sin, cy + dx * sin + dy * cos];
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
/** Map path coordinates through a transform. */
|
|
22
|
+
export function transformCommands(commands, t) {
|
|
23
|
+
return commands.map((c) => {
|
|
24
|
+
if (c.op === 'Z')
|
|
25
|
+
return c;
|
|
26
|
+
if (c.op === 'C') {
|
|
27
|
+
const [x1, y1] = t(c.x1, c.y1);
|
|
28
|
+
const [x2, y2] = t(c.x2, c.y2);
|
|
29
|
+
const [x, y] = t(c.x, c.y);
|
|
30
|
+
return { op: 'C', x1, y1, x2, y2, x, y };
|
|
31
|
+
}
|
|
32
|
+
const [x, y] = t(c.x, c.y);
|
|
33
|
+
return { op: c.op, x, y };
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
/** Move display items by (dx, dy). */
|
|
37
|
+
export function translateItems(items, dx, dy) {
|
|
38
|
+
return items.map((item) => {
|
|
39
|
+
switch (item.type) {
|
|
40
|
+
case 'text':
|
|
41
|
+
return { ...item, x: item.x + dx, y: item.y + dy, xs: item.xs.map((v) => v + dx) };
|
|
42
|
+
case 'path':
|
|
43
|
+
return { ...item, commands: transformCommands(item.commands, (x, y) => [x + dx, y + dy]) };
|
|
44
|
+
case 'line':
|
|
45
|
+
return { ...item, x1: item.x1 + dx, y1: item.y1 + dy, x2: item.x2 + dx, y2: item.y2 + dy };
|
|
46
|
+
default:
|
|
47
|
+
return { ...item, x: item.x + dx, y: item.y + dy };
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
/** Rotate / flip display items drawn for an object (paths and images; text keeps its baseline). */
|
|
52
|
+
export function transformItems(items, t) {
|
|
53
|
+
if (!t)
|
|
54
|
+
return items;
|
|
55
|
+
return items.map((item) => {
|
|
56
|
+
switch (item.type) {
|
|
57
|
+
case 'path':
|
|
58
|
+
return { ...item, commands: transformCommands(item.commands, t) };
|
|
59
|
+
case 'rect': {
|
|
60
|
+
const c = [t(item.x, item.y), t(item.x + item.w, item.y), t(item.x + item.w, item.y + item.h), t(item.x, item.y + item.h)];
|
|
61
|
+
return { type: 'path', commands: [{ op: 'M', x: c[0][0], y: c[0][1] }, ...c.slice(1).map(([x, y]) => ({ op: 'L', x, y })), { op: 'Z' }], fill: item.fill };
|
|
62
|
+
}
|
|
63
|
+
case 'line': {
|
|
64
|
+
const [x1, y1] = t(item.x1, item.y1);
|
|
65
|
+
const [x2, y2] = t(item.x2, item.y2);
|
|
66
|
+
return { ...item, x1, y1, x2, y2 };
|
|
67
|
+
}
|
|
68
|
+
case 'text': {
|
|
69
|
+
// Text moves with its object but stays upright.
|
|
70
|
+
const [x, y] = t(item.x, item.y);
|
|
71
|
+
const dx = x - item.x;
|
|
72
|
+
const dy = y - item.y;
|
|
73
|
+
return { ...item, x, y: item.y + dy, xs: item.xs.map((v) => v + dx) };
|
|
74
|
+
}
|
|
75
|
+
default:
|
|
76
|
+
return item;
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Draw an inline or floating object. Returns the items (the first is the object's frame, used
|
|
82
|
+
* for hit-testing and the editing caret) for the caller to place in front of or behind the text.
|
|
83
|
+
*/
|
|
84
|
+
export function drawObject(p, image, frame) {
|
|
85
|
+
const ctx = p.drawing;
|
|
86
|
+
const marker = { type: 'image', ...frame, target: image.target, external: image.external, alt: image.alt, inline: image };
|
|
87
|
+
if (image.rotation)
|
|
88
|
+
marker.rotation = image.rotation;
|
|
89
|
+
if (image.flipH)
|
|
90
|
+
marker.flipH = true;
|
|
91
|
+
if (image.flipV)
|
|
92
|
+
marker.flipV = true;
|
|
93
|
+
if (!ctx)
|
|
94
|
+
return [marker];
|
|
95
|
+
if (image.content) {
|
|
96
|
+
const saved = p.page.items;
|
|
97
|
+
const out = [];
|
|
98
|
+
p.page.items = out;
|
|
99
|
+
let ok = false;
|
|
100
|
+
try {
|
|
101
|
+
ok = !!ctx.content?.(p, image, frame);
|
|
102
|
+
}
|
|
103
|
+
finally {
|
|
104
|
+
p.page.items = saved;
|
|
105
|
+
}
|
|
106
|
+
if (ok)
|
|
107
|
+
return [{ ...marker, frameOnly: true }, ...out];
|
|
108
|
+
return [{ ...marker, frameOnly: true }, placeholder(frame)];
|
|
109
|
+
}
|
|
110
|
+
const bytes = ctx.doc.mediaSync(image.target);
|
|
111
|
+
if (bytes && metafileKind(bytes)) {
|
|
112
|
+
const items = renderMetafile(bytes, frame, ctx.fonts);
|
|
113
|
+
return [{ ...marker, frameOnly: true }, ...transformItems(items, objectTransform(frame, image.rotation, image.flipH, image.flipV))];
|
|
114
|
+
}
|
|
115
|
+
return [marker];
|
|
116
|
+
}
|
|
117
|
+
/** A light box for objects that cannot be drawn. */
|
|
118
|
+
export function placeholder(frame) {
|
|
119
|
+
return { type: 'path', commands: [{ op: 'M', x: frame.x, y: frame.y }, { op: 'L', x: frame.x + frame.w, y: frame.y }, { op: 'L', x: frame.x + frame.w, y: frame.y + frame.h }, { op: 'L', x: frame.x, y: frame.y + frame.h }, { op: 'Z' }], fill: 'F1F3F4', stroke: 'C0C4C8', strokeWidth: 0.5 };
|
|
120
|
+
}
|
package/dist/layout/engine.d.ts
CHANGED
|
@@ -4,10 +4,13 @@
|
|
|
4
4
|
*/
|
|
5
5
|
import { FontRegistry } from '../fonts/registry.js';
|
|
6
6
|
import type { DocxDocument } from '../model/document.js';
|
|
7
|
+
import type { LayoutCache } from './cache.js';
|
|
7
8
|
import type { FontProvider, LayoutResult } from './types.js';
|
|
8
9
|
export interface LayoutOptions {
|
|
9
10
|
/** Registered fonts (or a custom provider, e.g. canvas metrics in a browser). */
|
|
10
11
|
fonts: FontRegistry | FontProvider;
|
|
12
|
+
/** Re-use the layout of unchanged paragraphs from earlier runs (keep one per document). */
|
|
13
|
+
cache?: LayoutCache;
|
|
11
14
|
}
|
|
12
15
|
/** Lay out a document into pages. */
|
|
13
16
|
export declare function layoutDocument(doc: DocxDocument, options: LayoutOptions): LayoutResult;
|