docxkeun 0.1.2 → 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.
Files changed (197) hide show
  1. package/CHANGELOG.md +55 -0
  2. package/README.md +139 -16
  3. package/dist/api/editor.d.ts +172 -5
  4. package/dist/api/editor.js +814 -24
  5. package/dist/api/headers.d.ts +51 -0
  6. package/dist/api/headers.js +133 -0
  7. package/dist/api/index.d.ts +5 -1
  8. package/dist/api/index.js +4 -1
  9. package/dist/api/lists.d.ts +37 -0
  10. package/dist/api/lists.js +132 -0
  11. package/dist/api/parts.js +1 -0
  12. package/dist/api/positions.js +1 -0
  13. package/dist/api/revisions.d.ts +61 -0
  14. package/dist/api/revisions.js +390 -0
  15. package/dist/api/selection.d.ts +57 -0
  16. package/dist/api/selection.js +91 -0
  17. package/dist/charts/color.d.ts +45 -0
  18. package/dist/charts/color.js +322 -0
  19. package/dist/charts/format.d.ts +12 -0
  20. package/dist/charts/format.js +459 -0
  21. package/dist/charts/index.d.ts +11 -0
  22. package/dist/charts/index.js +8 -0
  23. package/dist/charts/model.d.ts +225 -0
  24. package/dist/charts/model.js +758 -0
  25. package/dist/charts/outline.d.ts +17 -0
  26. package/dist/charts/outline.js +214 -0
  27. package/dist/charts/render.d.ts +20 -0
  28. package/dist/charts/render.js +1772 -0
  29. package/dist/drawing/arrows.d.ts +37 -0
  30. package/dist/drawing/arrows.js +99 -0
  31. package/dist/drawing/formula.d.ts +40 -0
  32. package/dist/drawing/formula.js +159 -0
  33. package/dist/drawing/geometry.d.ts +61 -0
  34. package/dist/drawing/geometry.js +286 -0
  35. package/dist/drawing/index.d.ts +12 -0
  36. package/dist/drawing/index.js +8 -0
  37. package/dist/drawing/presets.d.ts +72 -0
  38. package/dist/drawing/presets.js +2874 -0
  39. package/dist/fonts/shaping/apply.d.ts +94 -0
  40. package/dist/fonts/shaping/apply.js +349 -0
  41. package/dist/fonts/shaping/arabic.d.ts +28 -0
  42. package/dist/fonts/shaping/arabic.js +235 -0
  43. package/dist/fonts/shaping/buffer.d.ts +53 -0
  44. package/dist/fonts/shaping/buffer.js +95 -0
  45. package/dist/fonts/shaping/gdef.d.ts +30 -0
  46. package/dist/fonts/shaping/gdef.js +66 -0
  47. package/dist/fonts/shaping/gpos.d.ts +86 -0
  48. package/dist/fonts/shaping/gpos.js +428 -0
  49. package/dist/fonts/shaping/gsub.d.ts +65 -0
  50. package/dist/fonts/shaping/gsub.js +313 -0
  51. package/dist/fonts/shaping/index.d.ts +32 -0
  52. package/dist/fonts/shaping/index.js +402 -0
  53. package/dist/fonts/shaping/indic.d.ts +6 -0
  54. package/dist/fonts/shaping/indic.js +805 -0
  55. package/dist/fonts/shaping/layout-common.d.ts +124 -0
  56. package/dist/fonts/shaping/layout-common.js +349 -0
  57. package/dist/fonts/shaping/plan.d.ts +87 -0
  58. package/dist/fonts/shaping/plan.js +152 -0
  59. package/dist/fonts/shaping/scripts.d.ts +26 -0
  60. package/dist/fonts/shaping/scripts.js +160 -0
  61. package/dist/fonts/shaping/shaper.d.ts +23 -0
  62. package/dist/fonts/shaping/shaper.js +5 -0
  63. package/dist/fonts/shaping/unicode.d.ts +34 -0
  64. package/dist/fonts/shaping/unicode.js +138 -0
  65. package/dist/images/bmp.d.ts +9 -0
  66. package/dist/images/bmp.js +234 -0
  67. package/dist/images/gif.d.ts +9 -0
  68. package/dist/images/gif.js +215 -0
  69. package/dist/images/index.d.ts +18 -0
  70. package/dist/images/index.js +63 -0
  71. package/dist/images/png-encode.d.ts +4 -0
  72. package/dist/images/png-encode.js +73 -0
  73. package/dist/images/size.d.ts +10 -0
  74. package/dist/images/size.js +83 -0
  75. package/dist/images/tiff.d.ts +14 -0
  76. package/dist/images/tiff.js +407 -0
  77. package/dist/index.d.ts +7 -2
  78. package/dist/index.js +5 -0
  79. package/dist/layout/bidi.d.ts +18 -0
  80. package/dist/layout/bidi.js +71 -0
  81. package/dist/layout/blocks.d.ts +20 -3
  82. package/dist/layout/blocks.js +88 -58
  83. package/dist/layout/cache.d.ts +31 -0
  84. package/dist/layout/cache.js +38 -0
  85. package/dist/layout/drawing.d.ts +30 -0
  86. package/dist/layout/drawing.js +120 -0
  87. package/dist/layout/engine.d.ts +3 -0
  88. package/dist/layout/engine.js +218 -11
  89. package/dist/layout/index.d.ts +1 -0
  90. package/dist/layout/index.js +1 -0
  91. package/dist/layout/lines.d.ts +63 -5
  92. package/dist/layout/lines.js +245 -20
  93. package/dist/layout/paint.d.ts +25 -3
  94. package/dist/layout/paint.js +26 -10
  95. package/dist/layout/shapes.d.ts +7 -0
  96. package/dist/layout/shapes.js +338 -0
  97. package/dist/layout/style.d.ts +4 -0
  98. package/dist/layout/style.js +2 -0
  99. package/dist/layout/types.d.ts +89 -2
  100. package/dist/math/boxes.d.ts +88 -0
  101. package/dist/math/boxes.js +166 -0
  102. package/dist/math/index.d.ts +29 -0
  103. package/dist/math/index.js +44 -0
  104. package/dist/math/layout.d.ts +13 -0
  105. package/dist/math/layout.js +771 -0
  106. package/dist/math/linear.d.ts +3 -0
  107. package/dist/math/linear.js +152 -0
  108. package/dist/math/omml.d.ts +15 -0
  109. package/dist/math/omml.js +57 -0
  110. package/dist/math/paths.d.ts +61 -0
  111. package/dist/math/paths.js +450 -0
  112. package/dist/math/symbols.d.ts +49 -0
  113. package/dist/math/symbols.js +269 -0
  114. package/dist/math/text.d.ts +53 -0
  115. package/dist/math/text.js +316 -0
  116. package/dist/metafile/canvas.d.ts +80 -0
  117. package/dist/metafile/canvas.js +515 -0
  118. package/dist/metafile/dib.d.ts +27 -0
  119. package/dist/metafile/dib.js +300 -0
  120. package/dist/metafile/emf.d.ts +31 -0
  121. package/dist/metafile/emf.js +591 -0
  122. package/dist/metafile/geometry.d.ts +49 -0
  123. package/dist/metafile/geometry.js +85 -0
  124. package/dist/metafile/index.d.ts +27 -0
  125. package/dist/metafile/index.js +34 -0
  126. package/dist/metafile/reader.d.ts +28 -0
  127. package/dist/metafile/reader.js +59 -0
  128. package/dist/metafile/state.d.ts +118 -0
  129. package/dist/metafile/state.js +172 -0
  130. package/dist/metafile/text.d.ts +17 -0
  131. package/dist/metafile/text.js +130 -0
  132. package/dist/metafile/wmf.d.ts +33 -0
  133. package/dist/metafile/wmf.js +395 -0
  134. package/dist/model/content.d.ts +92 -7
  135. package/dist/model/content.js +251 -17
  136. package/dist/model/document.d.ts +34 -1
  137. package/dist/model/document.js +80 -1
  138. package/dist/model/fields.d.ts +4 -0
  139. package/dist/model/fields.js +10 -0
  140. package/dist/model/numbering.d.ts +2 -0
  141. package/dist/model/numbering.js +22 -0
  142. package/dist/model/props.d.ts +2 -0
  143. package/dist/model/props.js +3 -0
  144. package/dist/opc/index.d.ts +1 -1
  145. package/dist/opc/package.d.ts +34 -0
  146. package/dist/opc/package.js +58 -4
  147. package/dist/pdf/export.js +97 -6
  148. package/dist/pdf/images.d.ts +7 -0
  149. package/dist/pdf/images.js +39 -2
  150. package/dist/render/activity.d.ts +138 -0
  151. package/dist/render/activity.js +794 -0
  152. package/dist/render/clipboard.d.ts +29 -0
  153. package/dist/render/clipboard.js +401 -0
  154. package/dist/render/comments.d.ts +166 -0
  155. package/dist/render/comments.js +779 -0
  156. package/dist/render/context-menu.d.ts +124 -0
  157. package/dist/render/context-menu.js +525 -0
  158. package/dist/render/editing.d.ts +146 -5
  159. package/dist/render/editing.js +678 -53
  160. package/dist/render/index.d.ts +12 -0
  161. package/dist/render/index.js +11 -0
  162. package/dist/render/navigation-panel.d.ts +76 -0
  163. package/dist/render/navigation-panel.js +431 -0
  164. package/dist/render/selection.js +5 -0
  165. package/dist/render/svg.d.ts +4 -6
  166. package/dist/render/svg.js +69 -7
  167. package/dist/render/toolbar.d.ts +83 -0
  168. package/dist/render/toolbar.js +505 -0
  169. package/dist/render/ui/comment-layout.d.ts +39 -0
  170. package/dist/render/ui/comment-layout.js +79 -0
  171. package/dist/render/ui/icons.d.ts +6 -0
  172. package/dist/render/ui/icons.js +38 -0
  173. package/dist/render/ui/menu.d.ts +38 -0
  174. package/dist/render/ui/menu.js +274 -0
  175. package/dist/render/ui/nav-model.d.ts +11 -0
  176. package/dist/render/ui/nav-model.js +34 -0
  177. package/dist/render/ui/overflow.d.ts +5 -0
  178. package/dist/render/ui/overflow.js +18 -0
  179. package/dist/render/ui/popover.d.ts +18 -0
  180. package/dist/render/ui/popover.js +109 -0
  181. package/dist/render/ui/theme.d.ts +2 -0
  182. package/dist/render/ui/theme.js +29 -0
  183. package/dist/render/undo.d.ts +76 -0
  184. package/dist/render/undo.js +144 -0
  185. package/dist/render/viewer.d.ts +100 -7
  186. package/dist/render/viewer.js +302 -92
  187. package/dist/security/policy.d.ts +3 -0
  188. package/dist/security/policy.js +6 -2
  189. package/dist/security/protection.d.ts +1 -0
  190. package/dist/security/protection.js +1 -1
  191. package/dist/text/bidi-data.d.ts +53 -0
  192. package/dist/text/bidi-data.js +318 -0
  193. package/dist/text/bidi.d.ts +37 -0
  194. package/dist/text/bidi.js +508 -0
  195. package/dist/xml/ns.d.ts +5 -0
  196. package/dist/xml/ns.js +7 -0
  197. package/package.json +9 -2
@@ -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
- const atoms = buildAtoms(para, { fonts: ctx.fonts, theme: ctx.theme, tracker: ctx.tracker, noteLabel: ctx.noteLabel }, label);
67
- const lines = breakLines(atoms, {
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
- const spacing = props.spacing ?? {};
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
- const decoLeft = left - (isVisibleBorder(borders?.left) ? borders.left.space + borders.left.width : 0);
92
- const decoRight = width - right + (isVisibleBorder(borders?.right) ? borders.right.space + borders.right.width : 0);
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 n = lines.length;
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.forEach((line, i) => {
143
- const { height, baseline } = lineHeight(line, spacing.line, spacing.lineRule);
144
- const notes = [];
145
- for (const f of line.fragments)
146
- if (f.kind === 'mark' && (f.mark === 'footnote' || f.mark === 'endnote'))
147
- notes.push({ type: f.mark, id: f.name });
148
- let keep = false;
149
- if (props.keepLines && i < n - 1)
150
- keep = true;
151
- if (widow && n >= 2 && (i === 0 || i === n - 2))
152
- keep = true;
153
- if (i === n - 1 && props.keepNext)
154
- keep = true;
155
- boxes.push({
156
- height,
157
- keepWithNext: keep,
158
- breakBefore: i === 0 && props.pageBreakBefore ? 'page' : undefined,
159
- breakAfter: line.breakAfter,
160
- notes: notes.length ? notes : undefined,
161
- paint: (p, x, y) => {
162
- decorate(p, x, y, height);
163
- if (i === 0 && outline?.title)
164
- p.page.headings.push({ ...outline, x: x + line.left, y });
165
- if (p.story === 'body')
166
- p.page.lines.push({ x: x + line.left, w: line.right - line.left, y, h: height, baseline: y + baseline, paragraph: para.el });
167
- paintLine(p, line, x, { top: y, height, baseline: y + baseline });
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
- if (props.pageBreakBefore && boxes[0])
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
+ }
@@ -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;