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
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Painting DrawingML content: shapes (preset and custom geometry, theme-styled fill and outline,
|
|
3
|
+
* arrowheads), text boxes (Word paragraphs laid out inside the shape), groups and drawing
|
|
4
|
+
* canvases, charts (their own part) and SmartArt (its pre-drawn dsp:drawing part).
|
|
5
|
+
*/
|
|
6
|
+
import { colorIn, fillOf, lineOf, dashPattern } from '../charts/color.js';
|
|
7
|
+
import { renderChart } from '../charts/index.js';
|
|
8
|
+
import { customGeometry, lineEnd, presetGeometry, readAdjustments, readLineEnd, shadeFill } from '../drawing/index.js';
|
|
9
|
+
import { attr, elements, firstChild, textContent } from '../xml/index.js';
|
|
10
|
+
import { objectTransform, placeholder, transformItems } from './drawing.js';
|
|
11
|
+
import { measureChars, textItem } from './paint.js';
|
|
12
|
+
const compose = (inner, outer) => (!inner ? outer : !outer ? inner : (x, y) => outer(...inner(x, y)));
|
|
13
|
+
/** Theme line widths for a:lnRef idx 1–3 (Office theme). */
|
|
14
|
+
const THEME_LINE_WIDTHS = [0, 0.5, 1, 1.5];
|
|
15
|
+
export function createDrawingContext(doc, fonts, layoutBlocks) {
|
|
16
|
+
const scheme = (name) => doc.theme.color(name) ?? doc.theme.colors.get(name);
|
|
17
|
+
const ctx = {
|
|
18
|
+
doc,
|
|
19
|
+
fonts,
|
|
20
|
+
content(p, image, frame) {
|
|
21
|
+
const content = image.content;
|
|
22
|
+
if (!content)
|
|
23
|
+
return false;
|
|
24
|
+
const t = objectTransform(frame, image.rotation, image.flipH, image.flipV);
|
|
25
|
+
return drawContent(p, content, frame, t);
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
const emit = (p, items, t) => {
|
|
29
|
+
for (const item of transformItems(items, t))
|
|
30
|
+
p.page.items.push(item);
|
|
31
|
+
};
|
|
32
|
+
function drawContent(p, content, frame, t) {
|
|
33
|
+
switch (content.type) {
|
|
34
|
+
case 'shape':
|
|
35
|
+
drawShape(p, content.shape, frame, t);
|
|
36
|
+
return true;
|
|
37
|
+
case 'group':
|
|
38
|
+
for (const child of content.children)
|
|
39
|
+
drawChild(p, child, frame, t);
|
|
40
|
+
return true;
|
|
41
|
+
case 'picture':
|
|
42
|
+
p.page.items.push({ type: 'image', ...frame, target: content.target, external: content.external });
|
|
43
|
+
return true;
|
|
44
|
+
case 'chart': {
|
|
45
|
+
const chartSpace = doc.xmlSync(content.part)?.root;
|
|
46
|
+
if (!chartSpace)
|
|
47
|
+
return false;
|
|
48
|
+
emit(p, renderChart(chartSpace, frame, { fonts, schemeColor: scheme, defaultFont: doc.theme.font('minorHAnsi') }), t);
|
|
49
|
+
return true;
|
|
50
|
+
}
|
|
51
|
+
case 'diagram':
|
|
52
|
+
return drawDiagram(p, content.dataPart, frame, t);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
function drawChild(p, child, group, t) {
|
|
56
|
+
const frame = { x: group.x + child.x, y: group.y + child.y, w: child.w, h: child.h };
|
|
57
|
+
const own = objectTransform(frame, child.rotation, child.flipH, child.flipV);
|
|
58
|
+
if (child.content.type === 'picture') {
|
|
59
|
+
// Pictures cannot be rotated as paths: place them by their transformed centre.
|
|
60
|
+
const [cx, cy] = (compose(own, t) ?? ((x, y) => [x, y]))(frame.x + frame.w / 2, frame.y + frame.h / 2);
|
|
61
|
+
p.page.items.push({ type: 'image', x: cx - frame.w / 2, y: cy - frame.h / 2, w: frame.w, h: frame.h, target: child.content.target, external: child.content.external, rotation: child.rotation || undefined });
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
drawContent(p, child.content, frame, compose(own, t));
|
|
65
|
+
}
|
|
66
|
+
/** Fill and outline: spPr first, then the shape style's theme references, then a VML shape's colours. */
|
|
67
|
+
function paints(shape) {
|
|
68
|
+
if (shape.vml) {
|
|
69
|
+
return {
|
|
70
|
+
fill: shape.vml.fill ? { color: shape.vml.fill.toUpperCase(), opacity: 1 } : null,
|
|
71
|
+
line: { paint: shape.vml.stroke ? { color: shape.vml.stroke.toUpperCase(), opacity: 1 } : null, width: 0.75 },
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
const style = shape.style;
|
|
75
|
+
let fill = fillOf(shape.spPr, scheme);
|
|
76
|
+
if (fill === undefined) {
|
|
77
|
+
const ref = firstChild(style, 'a:fillRef');
|
|
78
|
+
fill = ref && Number(attr(ref, 'idx') ?? 0) > 0 ? (colorIn(ref, scheme) ?? null) : null;
|
|
79
|
+
}
|
|
80
|
+
const ln = lineOf(shape.spPr, scheme);
|
|
81
|
+
const lnRef = firstChild(style, 'a:lnRef');
|
|
82
|
+
const refIdx = Number(attr(lnRef, 'idx') ?? 0);
|
|
83
|
+
let paint = ln.paint;
|
|
84
|
+
if (paint === undefined)
|
|
85
|
+
paint = lnRef && refIdx > 0 ? (colorIn(lnRef, scheme) ?? null) : null;
|
|
86
|
+
const width = ln.width ?? (THEME_LINE_WIDTHS[refIdx] || 0.75);
|
|
87
|
+
return { fill, line: { paint, width, dash: dashPattern(ln.dash, width), cap: ln.cap, join: ln.join } };
|
|
88
|
+
}
|
|
89
|
+
function geometryOf(shape, w, h) {
|
|
90
|
+
const g = shape.geometry;
|
|
91
|
+
if (g?.name === 'a:prstGeom') {
|
|
92
|
+
const out = presetGeometry(attr(g, 'prst') ?? 'rect', w, h, readAdjustments(g));
|
|
93
|
+
if (out)
|
|
94
|
+
return out;
|
|
95
|
+
}
|
|
96
|
+
else if (g?.name === 'a:custGeom') {
|
|
97
|
+
try {
|
|
98
|
+
return customGeometry(g, w, h);
|
|
99
|
+
}
|
|
100
|
+
catch {
|
|
101
|
+
// fall back to a rectangle
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return { paths: [{ commands: rect(0, 0, w, h), fill: 'norm', stroke: true }], textRect: { x: 0, y: 0, w, h } };
|
|
105
|
+
}
|
|
106
|
+
function drawShape(p, shape, frame, t) {
|
|
107
|
+
const geometry = geometryOf(shape, frame.w, frame.h);
|
|
108
|
+
const { fill, line } = paints(shape);
|
|
109
|
+
const items = [];
|
|
110
|
+
const ln = firstChild(shape.spPr, 'a:ln');
|
|
111
|
+
const head = readLineEnd(firstChild(ln, 'a:headEnd'));
|
|
112
|
+
const tail = readLineEnd(firstChild(ln, 'a:tailEnd'));
|
|
113
|
+
geometry.paths.forEach((path, i) => {
|
|
114
|
+
const commands = offset(path.commands, frame.x, frame.y);
|
|
115
|
+
const filled = fill && path.fill !== 'none' ? shadeFill(fill.color, path.fill) : undefined;
|
|
116
|
+
const stroked = line.paint && path.stroke ? line.paint : undefined;
|
|
117
|
+
if (!filled && !stroked)
|
|
118
|
+
return;
|
|
119
|
+
let drawn = commands;
|
|
120
|
+
// Arrowheads on the first open path.
|
|
121
|
+
const ends = [];
|
|
122
|
+
if (stroked && i === 0 && (head || tail)) {
|
|
123
|
+
const pts = points(commands);
|
|
124
|
+
if (pts.length >= 2) {
|
|
125
|
+
if (head) {
|
|
126
|
+
const e = lineEnd(head.type, head.w, head.len, pts[0], pts[1], line.width);
|
|
127
|
+
ends.push(endItem(e.commands, e.filled, stroked, line.width));
|
|
128
|
+
drawn = pullBack(drawn, 'start', e.inset);
|
|
129
|
+
}
|
|
130
|
+
if (tail) {
|
|
131
|
+
const e = lineEnd(tail.type, tail.w, tail.len, pts[pts.length - 1], pts[pts.length - 2], line.width);
|
|
132
|
+
ends.push(endItem(e.commands, e.filled, stroked, line.width));
|
|
133
|
+
drawn = pullBack(drawn, 'end', e.inset);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
items.push({
|
|
138
|
+
type: 'path',
|
|
139
|
+
commands: drawn,
|
|
140
|
+
fill: filled,
|
|
141
|
+
fillOpacity: filled && fill.opacity < 1 ? fill.opacity : undefined,
|
|
142
|
+
stroke: stroked?.color,
|
|
143
|
+
strokeOpacity: stroked && stroked.opacity < 1 ? stroked.opacity : undefined,
|
|
144
|
+
strokeWidth: line.width,
|
|
145
|
+
dash: line.dash,
|
|
146
|
+
lineCap: line.cap,
|
|
147
|
+
lineJoin: line.join,
|
|
148
|
+
});
|
|
149
|
+
items.push(...ends);
|
|
150
|
+
});
|
|
151
|
+
emit(p, items, t);
|
|
152
|
+
const tr = geometry.textRect;
|
|
153
|
+
if (shape.text)
|
|
154
|
+
drawTextBox(p, shape.text, { x: frame.x + tr.x, y: frame.y + tr.y, w: tr.w, h: tr.h }, t);
|
|
155
|
+
else if (shape.txBody)
|
|
156
|
+
emit(p, drawTextBody(shape.txBody, { x: frame.x + tr.x, y: frame.y + tr.y, w: tr.w, h: tr.h }, shape.style), t);
|
|
157
|
+
}
|
|
158
|
+
function drawTextBox(p, text, area, t) {
|
|
159
|
+
const ins = text.insets;
|
|
160
|
+
const inner = { x: area.x + ins.left, y: area.y + ins.top, w: Math.max(1, area.w - ins.left - ins.right), h: Math.max(0, area.h - ins.top - ins.bottom) };
|
|
161
|
+
const boxes = layoutBlocks(text.blocks, inner.w);
|
|
162
|
+
const height = boxes.reduce((s, b) => s + b.height, 0);
|
|
163
|
+
let y = inner.y;
|
|
164
|
+
if (text.anchor === 'ctr')
|
|
165
|
+
y += (inner.h - height) / 2;
|
|
166
|
+
else if (text.anchor === 'b')
|
|
167
|
+
y += inner.h - height;
|
|
168
|
+
// Text moves with a rotated box but stays upright; lines go to the editing layer as a text box story.
|
|
169
|
+
const [ox, oy] = t ? t(inner.x + inner.w / 2, inner.y + inner.h / 2) : [inner.x + inner.w / 2, inner.y + inner.h / 2];
|
|
170
|
+
const dx = ox - (inner.x + inner.w / 2);
|
|
171
|
+
const dy = oy - (inner.y + inner.h / 2);
|
|
172
|
+
const saved = { story: p.story, ref: p.storyRef, column: p.column };
|
|
173
|
+
p.story = 'textbox';
|
|
174
|
+
p.storyRef = { part: doc.mainPart, container: text.container };
|
|
175
|
+
p.column = { x: inner.x + dx, width: inner.w };
|
|
176
|
+
for (const b of boxes) {
|
|
177
|
+
// Lines past the bottom of a fixed-size box are cut off, as in Word.
|
|
178
|
+
if (!text.autoFit && y + b.height > area.y + area.h + 0.5 && y > inner.y)
|
|
179
|
+
break;
|
|
180
|
+
b.paint(p, inner.x + dx, y + dy);
|
|
181
|
+
y += b.height;
|
|
182
|
+
}
|
|
183
|
+
p.story = saved.story;
|
|
184
|
+
p.storyRef = saved.ref;
|
|
185
|
+
p.column = saved.column;
|
|
186
|
+
}
|
|
187
|
+
/** DrawingML text (a:txBody): SmartArt and canvas shapes. Simple wrapping, per-run size, colour and weight. */
|
|
188
|
+
function drawTextBody(txBody, area, style) {
|
|
189
|
+
const bodyPr = firstChild(txBody, 'a:bodyPr');
|
|
190
|
+
const emu = (name, d) => {
|
|
191
|
+
const v = attr(bodyPr, name);
|
|
192
|
+
return v === undefined ? d : Number(v) / 12700;
|
|
193
|
+
};
|
|
194
|
+
const inner = { x: area.x + emu('lIns', 7.2), y: area.y + emu('tIns', 3.6), w: Math.max(1, area.w - emu('lIns', 7.2) - emu('rIns', 7.2)), h: area.h - emu('tIns', 3.6) - emu('bIns', 3.6) };
|
|
195
|
+
const fontRef = firstChild(style, 'a:fontRef');
|
|
196
|
+
const styleColor = colorIn(fontRef, scheme)?.color;
|
|
197
|
+
const lines = [];
|
|
198
|
+
for (const para of elements(txBody, 'a:p')) {
|
|
199
|
+
const pPr = firstChild(para, 'a:pPr');
|
|
200
|
+
const align = attr(pPr, 'algn') ?? 'ctr';
|
|
201
|
+
const endSize = Number(attr(firstChild(para, 'a:endParaRPr'), 'sz') ?? 0) / 100;
|
|
202
|
+
const words = [];
|
|
203
|
+
for (const r of elements(para)) {
|
|
204
|
+
if (r.name !== 'a:r' && r.name !== 'a:fld')
|
|
205
|
+
continue;
|
|
206
|
+
const rPr = firstChild(r, 'a:rPr');
|
|
207
|
+
const size = Number(attr(rPr, 'sz') ?? 0) / 100 || endSize || 14;
|
|
208
|
+
const latin = attr(firstChild(rPr, 'a:latin'), 'typeface');
|
|
209
|
+
const family = latin && !latin.startsWith('+') ? latin : (doc.theme.font(latin === '+mj-lt' ? 'majorHAnsi' : 'minorHAnsi') ?? 'Calibri');
|
|
210
|
+
const font = fonts.get(family, attr(rPr, 'b') === '1', attr(rPr, 'i') === '1');
|
|
211
|
+
const color = fillOf(rPr, scheme)?.color ?? styleColor ?? '000000';
|
|
212
|
+
for (const piece of textContent(firstChild(r, 'a:t') ?? r).split(/(\s+)/)) {
|
|
213
|
+
if (!piece)
|
|
214
|
+
continue;
|
|
215
|
+
const space = /^\s+$/.test(piece);
|
|
216
|
+
words.push({ text: space ? ' ' : piece, width: measureChars(space ? ' ' : piece, font, size, 0).width, font, size, color, space });
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
let line = [];
|
|
220
|
+
let width = 0;
|
|
221
|
+
const flush = () => {
|
|
222
|
+
while (line.length && line[line.length - 1].space)
|
|
223
|
+
width -= line.pop().width;
|
|
224
|
+
const height = Math.max(endSize || 14, ...line.map((w) => w.size)) * 1.2;
|
|
225
|
+
lines.push({ words: line, width, height, align });
|
|
226
|
+
line = [];
|
|
227
|
+
width = 0;
|
|
228
|
+
};
|
|
229
|
+
for (const w of words) {
|
|
230
|
+
if (!line.length && w.space)
|
|
231
|
+
continue;
|
|
232
|
+
if (width + w.width > inner.w && line.length)
|
|
233
|
+
flush();
|
|
234
|
+
if (!line.length && w.space)
|
|
235
|
+
continue;
|
|
236
|
+
line.push(w);
|
|
237
|
+
width += w.width;
|
|
238
|
+
}
|
|
239
|
+
flush();
|
|
240
|
+
}
|
|
241
|
+
const total = lines.reduce((s, l) => s + l.height, 0);
|
|
242
|
+
const anchor = attr(bodyPr, 'anchor') ?? 'ctr';
|
|
243
|
+
let y = inner.y + (anchor === 'ctr' ? (inner.h - total) / 2 : anchor === 'b' ? inner.h - total : 0);
|
|
244
|
+
const items = [];
|
|
245
|
+
for (const l of lines) {
|
|
246
|
+
let x = inner.x + (l.align === 'ctr' ? (inner.w - l.width) / 2 : l.align === 'r' ? inner.w - l.width : 0);
|
|
247
|
+
const baseline = y + l.height * 0.8;
|
|
248
|
+
for (const w of l.words) {
|
|
249
|
+
if (!w.space) {
|
|
250
|
+
const m = measureChars(w.text, w.font, w.size, x);
|
|
251
|
+
items.push(textItem(x, baseline, m.chars, m.xs, m.widths, w.font, w.size, w.color));
|
|
252
|
+
}
|
|
253
|
+
x += w.width;
|
|
254
|
+
}
|
|
255
|
+
y += l.height;
|
|
256
|
+
}
|
|
257
|
+
return items;
|
|
258
|
+
}
|
|
259
|
+
/** SmartArt: Word saves the laid-out diagram as shapes in a dsp:drawing part next to the data. */
|
|
260
|
+
function drawDiagram(p, dataPart, frame, t) {
|
|
261
|
+
const data = doc.xmlSync(dataPart)?.root;
|
|
262
|
+
if (!data)
|
|
263
|
+
return false;
|
|
264
|
+
let relId;
|
|
265
|
+
for (const ext of elements(firstChild(data, 'dgm:extLst') ?? data, 'a:ext')) {
|
|
266
|
+
const dme = firstChild(ext, 'dsp:dataModelExt');
|
|
267
|
+
if (dme)
|
|
268
|
+
relId = attr(dme, 'relId');
|
|
269
|
+
}
|
|
270
|
+
const drawingPart = relId ? doc.rels(doc.mainPart).get(relId)?.target : undefined;
|
|
271
|
+
const drawing = doc.xmlSync(drawingPart)?.root;
|
|
272
|
+
const tree = firstChild(drawing, 'dsp:spTree');
|
|
273
|
+
if (!tree) {
|
|
274
|
+
// No saved drawing (made by another program): list the diagram's text in a box.
|
|
275
|
+
const texts = elements(firstChild(data, 'dgm:ptLst') ?? data, 'dgm:pt').map((pt) => textContent(firstChild(pt, 'dgm:t') ?? pt).trim()).filter(Boolean);
|
|
276
|
+
emit(p, [placeholder(frame)], t);
|
|
277
|
+
const fakeBody = { type: 'element', name: 'a:txBody', attrs: {}, children: texts.map((s) => ({ type: 'element', name: 'a:p', attrs: {}, children: [{ type: 'element', name: 'a:r', attrs: {}, children: [{ type: 'element', name: 'a:t', attrs: {}, children: [{ type: 'text', text: s }] }] }] })) };
|
|
278
|
+
emit(p, drawTextBody(fakeBody, frame, undefined), t);
|
|
279
|
+
return true;
|
|
280
|
+
}
|
|
281
|
+
const toPt = (v) => Number(v ?? 0) / 12700;
|
|
282
|
+
for (const sp of elements(tree, 'dsp:sp')) {
|
|
283
|
+
const spPr = firstChild(sp, 'dsp:spPr');
|
|
284
|
+
const xfrm = firstChild(spPr, 'a:xfrm');
|
|
285
|
+
const off = firstChild(xfrm, 'a:off');
|
|
286
|
+
const ext = firstChild(xfrm, 'a:ext');
|
|
287
|
+
const box = { x: frame.x + toPt(attr(off, 'x')), y: frame.y + toPt(attr(off, 'y')), w: toPt(attr(ext, 'cx')), h: toPt(attr(ext, 'cy')) };
|
|
288
|
+
const own = objectTransform(box, Number(attr(xfrm, 'rot') ?? 0) / 60000, attr(xfrm, 'flipH') === '1', attr(xfrm, 'flipV') === '1');
|
|
289
|
+
const shape = { geometry: firstChild(spPr, 'a:prstGeom') ?? firstChild(spPr, 'a:custGeom'), spPr, style: firstChild(sp, 'dsp:style') };
|
|
290
|
+
drawShape(p, shape, box, compose(own, t));
|
|
291
|
+
// The text has its own rectangle (dsp:txXfrm) and is not rotated with the shape.
|
|
292
|
+
const txBody = firstChild(sp, 'dsp:txBody');
|
|
293
|
+
if (txBody) {
|
|
294
|
+
const tx = firstChild(sp, 'dsp:txXfrm');
|
|
295
|
+
const toff = firstChild(tx, 'a:off');
|
|
296
|
+
const text = tx ? { x: frame.x + toPt(attr(toff, 'x')), y: frame.y + toPt(attr(toff, 'y')), w: toPt(attr(firstChild(tx, 'a:ext'), 'cx')), h: toPt(attr(firstChild(tx, 'a:ext'), 'cy')) } : box;
|
|
297
|
+
emit(p, drawTextBody(txBody, text, shape.style), t);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
return true;
|
|
301
|
+
}
|
|
302
|
+
return ctx;
|
|
303
|
+
}
|
|
304
|
+
function rect(x, y, w, h) {
|
|
305
|
+
return [{ op: 'M', x, y }, { op: 'L', x: x + w, y }, { op: 'L', x: x + w, y: y + h }, { op: 'L', x, y: y + h }, { op: 'Z' }];
|
|
306
|
+
}
|
|
307
|
+
function offset(commands, dx, dy) {
|
|
308
|
+
return commands.map((c) => (c.op === 'Z' ? c : c.op === 'C' ? { ...c, x1: c.x1 + dx, y1: c.y1 + dy, x2: c.x2 + dx, y2: c.y2 + dy, x: c.x + dx, y: c.y + dy } : { ...c, x: c.x + dx, y: c.y + dy }));
|
|
309
|
+
}
|
|
310
|
+
/** Points of a path in order (curve control points left out). */
|
|
311
|
+
function points(commands) {
|
|
312
|
+
const out = [];
|
|
313
|
+
for (const c of commands)
|
|
314
|
+
if (c.op !== 'Z')
|
|
315
|
+
out.push({ x: c.x, y: c.y });
|
|
316
|
+
return out;
|
|
317
|
+
}
|
|
318
|
+
function endItem(commands, filled, paint, width) {
|
|
319
|
+
return filled ? { type: 'path', commands, fill: paint.color, fillOpacity: paint.opacity < 1 ? paint.opacity : undefined } : { type: 'path', commands, stroke: paint.color, strokeWidth: width, lineJoin: 'miter' };
|
|
320
|
+
}
|
|
321
|
+
/** Shorten the first or last segment of a path by `inset` so a line's end hides inside its arrowhead. */
|
|
322
|
+
function pullBack(commands, end, inset) {
|
|
323
|
+
if (inset <= 0)
|
|
324
|
+
return commands;
|
|
325
|
+
const out = commands.map((c) => ({ ...c }));
|
|
326
|
+
const pts = out.filter((c) => c.op !== 'Z');
|
|
327
|
+
if (pts.length < 2)
|
|
328
|
+
return commands;
|
|
329
|
+
const [a, b] = end === 'start' ? [pts[0], pts[1]] : [pts[pts.length - 1], pts[pts.length - 2]];
|
|
330
|
+
const dx = b.x - a.x;
|
|
331
|
+
const dy = b.y - a.y;
|
|
332
|
+
const len = Math.hypot(dx, dy);
|
|
333
|
+
if (len <= inset)
|
|
334
|
+
return commands;
|
|
335
|
+
a.x += (dx / len) * inset;
|
|
336
|
+
a.y += (dy / len) * inset;
|
|
337
|
+
return out;
|
|
338
|
+
}
|
package/dist/layout/style.d.ts
CHANGED
|
@@ -31,6 +31,10 @@ export interface RunStyle {
|
|
|
31
31
|
shading?: string;
|
|
32
32
|
rtl: boolean;
|
|
33
33
|
hint?: string;
|
|
34
|
+
/** Pair kerning from this size up (w:kern), in points. */
|
|
35
|
+
kern?: number;
|
|
36
|
+
/** OpenType ligatures Word turns on (w14:ligatures). */
|
|
37
|
+
ligatures?: string;
|
|
34
38
|
}
|
|
35
39
|
export declare const HIGHLIGHT_COLORS: Record<string, string>;
|
|
36
40
|
export declare function resolveRunStyle(props: RunProps, theme: Theme): RunStyle;
|
package/dist/layout/style.js
CHANGED
|
@@ -53,6 +53,8 @@ export function resolveRunStyle(props, theme) {
|
|
|
53
53
|
shading: shadingColor(props.shading),
|
|
54
54
|
rtl: props.rtl ?? false,
|
|
55
55
|
hint: f.hint,
|
|
56
|
+
...(props.kern !== undefined ? { kern: props.kern } : {}),
|
|
57
|
+
...(props.ligatures && props.ligatures !== 'none' ? { ligatures: props.ligatures } : {}),
|
|
56
58
|
};
|
|
57
59
|
cache.set(props, style);
|
|
58
60
|
return style;
|
package/dist/layout/types.d.ts
CHANGED
|
@@ -60,6 +60,22 @@ export interface TextItem {
|
|
|
60
60
|
source?: TextSource;
|
|
61
61
|
/** Present when the text is a resolved field (page numbers). */
|
|
62
62
|
field?: 'PAGE' | 'NUMPAGES' | 'SECTIONPAGES';
|
|
63
|
+
/**
|
|
64
|
+
* Right-to-left or complex-script text: the characters in logical order. `chars`, `xs` and
|
|
65
|
+
* `glyphs` are in visual order; renderers that shape text (SVG) draw `text` as one run.
|
|
66
|
+
*/
|
|
67
|
+
complex?: {
|
|
68
|
+
text: string;
|
|
69
|
+
rtl: boolean;
|
|
70
|
+
};
|
|
71
|
+
/** Glyphs of each cluster from the shaping engine (ligatures, contextual forms), when shaped. */
|
|
72
|
+
clusterGlyphs?: Array<Array<{
|
|
73
|
+
id: number;
|
|
74
|
+
dx: number;
|
|
75
|
+
dy: number;
|
|
76
|
+
advance: number;
|
|
77
|
+
text: string;
|
|
78
|
+
}>>;
|
|
63
79
|
}
|
|
64
80
|
export interface RectItem {
|
|
65
81
|
type: 'rect';
|
|
@@ -79,6 +95,13 @@ export interface LineItem {
|
|
|
79
95
|
color: string;
|
|
80
96
|
style: 'solid' | 'dashed' | 'dotted' | 'double';
|
|
81
97
|
}
|
|
98
|
+
/** Decoded pixels: for images a renderer cannot take as they are (GIF and BMP in PDF, bitmaps inside metafiles). */
|
|
99
|
+
export interface RasterImage {
|
|
100
|
+
width: number;
|
|
101
|
+
height: number;
|
|
102
|
+
/** 8-bit RGBA, row by row from the top, 4 bytes per pixel (not premultiplied). */
|
|
103
|
+
rgba: Uint8Array;
|
|
104
|
+
}
|
|
82
105
|
export interface ImageItem {
|
|
83
106
|
type: 'image';
|
|
84
107
|
x: number;
|
|
@@ -89,9 +112,57 @@ export interface ImageItem {
|
|
|
89
112
|
target: string | undefined;
|
|
90
113
|
external: boolean;
|
|
91
114
|
alt?: string;
|
|
92
|
-
|
|
115
|
+
/** The image as drawn, when it does not come from a media part (or must not be read from one). */
|
|
116
|
+
raster?: RasterImage;
|
|
117
|
+
inline?: ImageInline;
|
|
118
|
+
/** Clockwise rotation in degrees about the centre, and flips. */
|
|
119
|
+
rotation?: number;
|
|
120
|
+
flipH?: boolean;
|
|
121
|
+
flipV?: boolean;
|
|
122
|
+
/** Only marks the object's box (for hit-testing): what it shows is drawn by the items after it. */
|
|
123
|
+
frameOnly?: boolean;
|
|
124
|
+
}
|
|
125
|
+
/** Path segments in points, page coordinates (y down). Arcs are converted to cubic Béziers. */
|
|
126
|
+
export type PathCommand = {
|
|
127
|
+
op: 'M';
|
|
128
|
+
x: number;
|
|
129
|
+
y: number;
|
|
130
|
+
} | {
|
|
131
|
+
op: 'L';
|
|
132
|
+
x: number;
|
|
133
|
+
y: number;
|
|
134
|
+
} | {
|
|
135
|
+
op: 'C';
|
|
136
|
+
x1: number;
|
|
137
|
+
y1: number;
|
|
138
|
+
x2: number;
|
|
139
|
+
y2: number;
|
|
140
|
+
x: number;
|
|
141
|
+
y: number;
|
|
142
|
+
} | {
|
|
143
|
+
op: 'Z';
|
|
144
|
+
};
|
|
145
|
+
/** A filled and/or stroked vector path: shapes, charts, equations, metafiles. */
|
|
146
|
+
export interface PathItem {
|
|
147
|
+
type: 'path';
|
|
148
|
+
commands: PathCommand[];
|
|
149
|
+
/** Fill colour as RRGGBB; no fill when absent. */
|
|
150
|
+
fill?: string;
|
|
151
|
+
/** 0..1, default 1. */
|
|
152
|
+
fillOpacity?: number;
|
|
153
|
+
/** Even-odd fill rule instead of non-zero. */
|
|
154
|
+
evenOdd?: boolean;
|
|
155
|
+
/** Stroke colour as RRGGBB; no stroke when absent. */
|
|
156
|
+
stroke?: string;
|
|
157
|
+
/** Stroke width in points (default 0.75). */
|
|
158
|
+
strokeWidth?: number;
|
|
159
|
+
strokeOpacity?: number;
|
|
160
|
+
/** Dash pattern in points (on, off, …). */
|
|
161
|
+
dash?: number[];
|
|
162
|
+
lineCap?: 'butt' | 'round' | 'square';
|
|
163
|
+
lineJoin?: 'miter' | 'round' | 'bevel';
|
|
93
164
|
}
|
|
94
|
-
export type DisplayItem = TextItem | RectItem | LineItem | ImageItem;
|
|
165
|
+
export type DisplayItem = TextItem | RectItem | LineItem | ImageItem | PathItem;
|
|
95
166
|
export interface Rect {
|
|
96
167
|
x: number;
|
|
97
168
|
y: number;
|
|
@@ -136,6 +207,8 @@ export interface Page {
|
|
|
136
207
|
}>;
|
|
137
208
|
/** Every body line (not headers, footers or notes), for caret placement and hit-testing. */
|
|
138
209
|
lines: BodyLine[];
|
|
210
|
+
/** Lines of headers, footers, footnotes and text boxes, for editing them in place. */
|
|
211
|
+
storyLines: StoryLine[];
|
|
139
212
|
/** Paragraphs with an outline level (headings), for PDF outlines and navigation panes. */
|
|
140
213
|
headings: Array<{
|
|
141
214
|
level: number;
|
|
@@ -146,6 +219,20 @@ export interface Page {
|
|
|
146
219
|
/** Body content area (for viewers that draw margins). */
|
|
147
220
|
content: Rect;
|
|
148
221
|
}
|
|
222
|
+
/** Stories other than the main body that can be edited in place. */
|
|
223
|
+
export type StoryKind = 'header' | 'footer' | 'footnote' | 'endnote' | 'textbox';
|
|
224
|
+
export interface StoryRef {
|
|
225
|
+
/** Part holding the story's XML (the main document for text boxes). */
|
|
226
|
+
part: string;
|
|
227
|
+
/** Footnote or endnote id. */
|
|
228
|
+
noteId?: string;
|
|
229
|
+
/** The w:txbxContent of a text box. */
|
|
230
|
+
container?: import('../xml/dom.js').XmlElement;
|
|
231
|
+
}
|
|
232
|
+
/** A line of a header, footer, note or text box. */
|
|
233
|
+
export interface StoryLine extends BodyLine, StoryRef {
|
|
234
|
+
story: StoryKind;
|
|
235
|
+
}
|
|
149
236
|
export interface BodyLine {
|
|
150
237
|
/** Left edge of the line's content box and its width (between the paragraph's indents). */
|
|
151
238
|
x: number;
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Boxes, atoms and the style context of equation layout. A box is a group of display items
|
|
3
|
+
* measured around its own baseline origin, so structures can stack and shift boxes without
|
|
4
|
+
* knowing what is inside. Operator spacing follows the TeXbook's rules (appendix G), which
|
|
5
|
+
* Word's math layout matches closely.
|
|
6
|
+
*/
|
|
7
|
+
import type { DisplayItem } from '../layout/types.js';
|
|
8
|
+
import type { MathFonts } from './text.js';
|
|
9
|
+
export type AtomClass = 'ord' | 'op' | 'bin' | 'rel' | 'open' | 'close' | 'punct' | 'inner';
|
|
10
|
+
export interface Box {
|
|
11
|
+
width: number;
|
|
12
|
+
/** Ink height above the baseline and depth below it (both ≥ 0). */
|
|
13
|
+
ascent: number;
|
|
14
|
+
descent: number;
|
|
15
|
+
/** Items relative to the box origin: left end of the baseline, y down. */
|
|
16
|
+
items: DisplayItem[];
|
|
17
|
+
/** How far the last glyph's ink leans past its advance (italic letters); superscripts move right by it. */
|
|
18
|
+
italic: number;
|
|
19
|
+
/** A plain character or number, not a structure: scripts attach with TeX's character rules. */
|
|
20
|
+
simple?: boolean;
|
|
21
|
+
}
|
|
22
|
+
export interface Atom {
|
|
23
|
+
box: Box;
|
|
24
|
+
/** Class seen by the atom to the left and to the right (structures such as m:d differ). */
|
|
25
|
+
left: AtomClass;
|
|
26
|
+
right: AtomClass;
|
|
27
|
+
/** Typed spaces and m:eqArr alignment marks take no part in operator spacing. */
|
|
28
|
+
kind?: 'glue' | 'mark';
|
|
29
|
+
}
|
|
30
|
+
/** Style of the current position: TeX's display / text / script / scriptscript, cramped or not. */
|
|
31
|
+
export interface Ctx {
|
|
32
|
+
fonts: MathFonts;
|
|
33
|
+
/** Font size of level-0 text (pt). */
|
|
34
|
+
base: number;
|
|
35
|
+
level: 0 | 1 | 2;
|
|
36
|
+
/** Display style (level 0 of a display equation): big operators, full-size fractions. */
|
|
37
|
+
display: boolean;
|
|
38
|
+
/** Cramped (under bars, in denominators and subscripts): superscripts sit lower. */
|
|
39
|
+
cramped: boolean;
|
|
40
|
+
color: string;
|
|
41
|
+
/** Height of the math axis in em (the centre of the minus sign). */
|
|
42
|
+
axis: number;
|
|
43
|
+
/** Inside m:fName: letters default to upright. */
|
|
44
|
+
fn: boolean;
|
|
45
|
+
/** Inside m:eqArr: `&` marks an alignment point. */
|
|
46
|
+
align: boolean;
|
|
47
|
+
}
|
|
48
|
+
/** Script sizes relative to the base: Cambria Math's ScriptPercentScaleDown (73) and ScriptScript (60). */
|
|
49
|
+
export declare const SCALE: readonly [1, 0.73, 0.6];
|
|
50
|
+
/** Default rule thickness (fraction bars, overbars, radicals), in em. */
|
|
51
|
+
export declare const RULE = 0.06;
|
|
52
|
+
export declare const THIN: number;
|
|
53
|
+
export declare const MEDIUM: number;
|
|
54
|
+
export declare const THICK: number;
|
|
55
|
+
export declare const X_HEIGHT = 0.46;
|
|
56
|
+
export declare function em(ctx: Ctx): number;
|
|
57
|
+
export declare function scriptStyle(ctx: Ctx, cramped?: boolean): Ctx;
|
|
58
|
+
/** Numerators and denominators: display → text size, text → script, and so on. */
|
|
59
|
+
export declare function fractionStyle(ctx: Ctx, cramped: boolean): Ctx;
|
|
60
|
+
export declare function emptyBox(width?: number): Box;
|
|
61
|
+
/** Move items in place (boxes are owned by exactly one parent, so nothing is copied). */
|
|
62
|
+
export declare function shiftItems(items: DisplayItem[], dx: number, dy: number): void;
|
|
63
|
+
/** Collects boxes and drawn items into a new box, tracking its extent. */
|
|
64
|
+
export declare class Builder {
|
|
65
|
+
readonly items: DisplayItem[];
|
|
66
|
+
ascent: number;
|
|
67
|
+
descent: number;
|
|
68
|
+
right: number;
|
|
69
|
+
/** Place a box with its origin at (x, y); y is down, so a raised box has negative y. */
|
|
70
|
+
put(box: Box, x: number, y: number): void;
|
|
71
|
+
/** Add an item the caller drew, covering `top`..`bottom` (y down). */
|
|
72
|
+
draw(item: DisplayItem, top: number, bottom: number): void;
|
|
73
|
+
done(width?: number, italic?: number): Box;
|
|
74
|
+
}
|
|
75
|
+
export declare function spaceBetween(left: AtomClass, right: AtomClass, ctx: Ctx): number;
|
|
76
|
+
/**
|
|
77
|
+
* A binary operator with no left operand (start of a row, after an opening delimiter or another
|
|
78
|
+
* operator) or no right operand is unary: it becomes ordinary and gets no space ("−b", "(−1)").
|
|
79
|
+
*/
|
|
80
|
+
export declare function resolveBinaries(atoms: Atom[]): void;
|
|
81
|
+
export interface Row {
|
|
82
|
+
box: Box;
|
|
83
|
+
/** Classes of the first and last atom (undefined for an empty row). */
|
|
84
|
+
first: AtomClass | undefined;
|
|
85
|
+
last: AtomClass | undefined;
|
|
86
|
+
}
|
|
87
|
+
/** Lay atoms side by side with operator spacing. `before` is the class of the atom to the left. */
|
|
88
|
+
export declare function hpack(atoms: Atom[], ctx: Ctx, before?: AtomClass): Row;
|