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.
Files changed (197) hide show
  1. package/CHANGELOG.md +48 -0
  2. package/README.md +137 -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
@@ -6,10 +6,19 @@ import { FontRegistry } from '../fonts/registry.js';
6
6
  import { formatNumber, NumberingCounter } from '../model/numbering.js';
7
7
  import { parseSectionProps } from '../model/props.js';
8
8
  import { blockBoxes } from './blocks.js';
9
+ import { createDrawingContext } from './shapes.js';
9
10
  import { RegistryFontProvider } from './fonts.js';
10
11
  import { RangeTracker } from './lines.js';
11
- import { measureChars, pageNumberLabel } from './paint.js';
12
+ import { floatPosition, measureChars, pageNumberLabel } from './paint.js';
12
13
  const EPS = 0.01;
14
+ const providers = new WeakMap();
15
+ /** One provider per registry, so cached paragraph layout (which holds its fonts) stays valid. */
16
+ function providerFor(registry) {
17
+ let p = providers.get(registry);
18
+ if (!p)
19
+ providers.set(registry, (p = new RegistryFontProvider(registry)));
20
+ return p;
21
+ }
13
22
  function splitSections(blocks, finalSection) {
14
23
  const groups = [];
15
24
  let current = [];
@@ -43,6 +52,8 @@ class Paginator {
43
52
  columns = [];
44
53
  col = 0;
45
54
  regionTop = 0;
55
+ /** First placement of the current column region (placements restart on every page). */
56
+ regionStart = 0;
46
57
  bodyTop = 0;
47
58
  bottom = 0;
48
59
  y = 0;
@@ -50,6 +61,10 @@ class Paginator {
50
61
  startedByOverflow = false;
51
62
  pageNumber = 0;
52
63
  placements = [];
64
+ /** Areas text must flow around on this page (floating objects with wrapping, plus their distance from text). */
65
+ exclusions = [];
66
+ /** Paragraphs whose floats are already placed on this page. */
67
+ floatsPlaced = new Set();
53
68
  notes = [];
54
69
  notesHeight = 0;
55
70
  endnotes = [];
@@ -87,11 +102,15 @@ class Paginator {
87
102
  this.sectionPageCounts[index] = 0;
88
103
  const samePage = prev && prev.pageWidth === section.pageWidth && prev.pageHeight === section.pageHeight;
89
104
  if (!first && section.type === 'continuous' && samePage) {
105
+ // Word balances the columns of a section that ends with a continuous break.
106
+ if (this.columns.length > 1)
107
+ this.balanceColumns();
90
108
  // New column layout on the same page, below the content so far.
91
109
  this.flushColumnsTo();
92
110
  this.columns = this.computeColumns(section);
93
111
  this.col = 0;
94
112
  this.regionTop = this.y;
113
+ this.regionStart = this.placements.length;
95
114
  this.atColumnTop = true;
96
115
  this.sectionPageCounts[index] = 1;
97
116
  return;
@@ -110,6 +129,72 @@ class Paginator {
110
129
  }
111
130
  this.newPage(false);
112
131
  }
132
+ /**
133
+ * Redistribute the current page's part of a multi-column section so its columns end at about the
134
+ * same height: find the smallest column height that holds everything (splitting paragraphs and
135
+ * tables between lines and rows as usual), then place the boxes again with that height.
136
+ */
137
+ balanceColumns() {
138
+ const n = this.columns.length;
139
+ const region = this.placements.slice(this.regionStart);
140
+ const boxes = region.filter((p) => p.index >= 0).map((p) => p.box);
141
+ if (boxes.length === 0)
142
+ return;
143
+ // Explicit column or page breaks decide the columns themselves.
144
+ if (boxes.some((b) => b.breakBefore || b.breakAfter))
145
+ return;
146
+ const total = boxes.reduce((sum, b) => sum + b.height, 0);
147
+ const fits = (h) => {
148
+ const queue = [...boxes];
149
+ let col = 0;
150
+ let used = 0;
151
+ for (let i = 0; i < queue.length; i++) {
152
+ const b = queue[i];
153
+ const height = used === 0 && b.discardAtTop ? 0 : b.height;
154
+ if (used + height <= h + EPS) {
155
+ used += height;
156
+ continue;
157
+ }
158
+ const parts = b.split && h - used > 12 ? b.split(h - used) : undefined;
159
+ if (parts) {
160
+ queue.splice(i, 1, parts[0], parts[1]);
161
+ }
162
+ else {
163
+ if (used === 0)
164
+ return false;
165
+ i--;
166
+ }
167
+ col++;
168
+ used = 0;
169
+ if (col >= n)
170
+ return false;
171
+ }
172
+ return true;
173
+ };
174
+ let hi = this.y - this.regionTop;
175
+ for (const p of region)
176
+ hi = Math.max(hi, p.y + p.box.height - this.regionTop);
177
+ if (!fits(hi))
178
+ return;
179
+ let lo = total / n;
180
+ for (let k = 0; k < 24 && hi - lo > 0.25; k++) {
181
+ const mid = (lo + hi) / 2;
182
+ if (fits(mid))
183
+ hi = mid;
184
+ else
185
+ lo = mid;
186
+ }
187
+ const page = this.page;
188
+ const savedBottom = this.bottom;
189
+ this.placements.length = this.regionStart;
190
+ this.col = 0;
191
+ this.y = this.regionTop;
192
+ this.atColumnTop = true;
193
+ this.bottom = this.regionTop + hi + EPS + this.notesHeight;
194
+ this.run(boxes);
195
+ if (this.page === page)
196
+ this.bottom = savedBottom;
197
+ }
113
198
  /** All columns of the current region are done: continue below the tallest. */
114
199
  flushColumnsTo() {
115
200
  let maxY = this.y;
@@ -138,6 +223,7 @@ class Paginator {
138
223
  locks: [],
139
224
  anchors: new Map(),
140
225
  lines: [],
226
+ storyLines: [],
141
227
  headings: [],
142
228
  content: { x: 0, y: 0, w: 0, h: 0 },
143
229
  };
@@ -154,13 +240,93 @@ class Paginator {
154
240
  page.content = { x: m.left + m.gutter, y: this.bodyTop, w: this.contentWidth, h: this.bottom - this.bodyTop };
155
241
  this.col = 0;
156
242
  this.regionTop = this.bodyTop;
243
+ this.regionStart = 0;
157
244
  this.y = this.bodyTop;
158
245
  this.atColumnTop = true;
159
246
  this.startedByOverflow = overflow;
160
247
  this.placements = [];
248
+ this.exclusions = [];
249
+ this.floatsPlaced = new Set();
161
250
  this.notes = [];
162
251
  this.notesHeight = 0;
163
252
  }
253
+ /** Place a paragraph's floating objects (anchored at `paraTop`) and record the areas text wraps around. */
254
+ placeFloats(meta, paraTop) {
255
+ if (this.floatsPlaced.has(meta))
256
+ return;
257
+ this.floatsPlaced.add(meta);
258
+ const column = this.columns[this.col];
259
+ for (const image of meta.floats) {
260
+ const f = image.float;
261
+ const at = floatPosition(this.page, column, f, image, column.x, paraTop);
262
+ this.painter.floatRects?.set(image, at);
263
+ if (f.wrap === 'none')
264
+ continue;
265
+ this.exclusions.push({
266
+ left: at.x - f.dist.left,
267
+ top: at.y - f.dist.top,
268
+ right: at.x + image.width + f.dist.right,
269
+ bottom: at.y + image.height + f.dist.bottom,
270
+ wrap: f.wrap,
271
+ side: f.wrapText,
272
+ });
273
+ }
274
+ }
275
+ /** Room for a line at an absolute height in the current column, around this page's exclusions. */
276
+ boundsAt(column, top, height) {
277
+ let left = 0;
278
+ let right = column.width;
279
+ let next;
280
+ let touched = false;
281
+ for (const e of this.exclusions) {
282
+ if (e.bottom <= top + EPS || e.top >= top + height - EPS)
283
+ continue;
284
+ const exLeft = e.left - column.x;
285
+ const exRight = e.right - column.x;
286
+ if (exRight <= left || exLeft >= right)
287
+ continue;
288
+ touched = true;
289
+ if (e.wrap === 'topAndBottom') {
290
+ right = left;
291
+ }
292
+ else {
293
+ const roomLeft = exLeft - left;
294
+ const roomRight = right - exRight;
295
+ const textLeft = e.side === 'left' || ((e.side === 'bothSides' || e.side === 'largest') && roomLeft >= roomRight);
296
+ if (textLeft)
297
+ right = Math.min(right, exLeft);
298
+ else
299
+ left = Math.max(left, exRight);
300
+ }
301
+ next = Math.max(next ?? 0, e.bottom);
302
+ }
303
+ if (!touched)
304
+ return undefined;
305
+ return { left, right, ...(next !== undefined ? { next } : {}) };
306
+ }
307
+ /** Lay a paragraph out around this page's floats when it overlaps any; returns the boxes to use. */
308
+ wrapParagraph(queue, i) {
309
+ const meta = queue[i].paragraph;
310
+ if (!meta || meta.wrapped)
311
+ return false;
312
+ const paraTop = this.y + meta.textOffset;
313
+ this.placeFloats(meta, paraTop);
314
+ if (!this.exclusions.length)
315
+ return false;
316
+ let height = 0;
317
+ for (let k = i; k < i + meta.count && k < queue.length; k++)
318
+ height += queue[k].height;
319
+ const column = this.columns[this.col];
320
+ const overlaps = this.exclusions.some((e) => e.bottom > this.y && e.top < this.y + height && e.right > column.x && e.left < column.x + column.width);
321
+ if (!overlaps)
322
+ return false;
323
+ const boxes = meta.reflow((top, h) => {
324
+ const b = this.boundsAt(column, paraTop + top, h);
325
+ return b && { left: b.left, right: b.right, ...(b.next !== undefined ? { next: b.next - paraTop } : {}) };
326
+ });
327
+ queue.splice(i, meta.count, ...boxes);
328
+ return true;
329
+ }
164
330
  nextColumn(overflow) {
165
331
  if (this.col < this.columns.length - 1) {
166
332
  this.col++;
@@ -217,9 +383,11 @@ class Paginator {
217
383
  while (i < queue.length) {
218
384
  if (++guard > queue.length * 20 + 10000)
219
385
  throw new Error('Layout did not converge');
386
+ if (queue[i].breakBefore && !this.atColumnTop)
387
+ this.breakTo(queue[i].breakBefore);
388
+ // Text wraps around floating objects: re-break the paragraph where it meets them.
389
+ this.wrapParagraph(queue, i);
220
390
  const box = queue[i];
221
- if (box.breakBefore && !this.atColumnTop)
222
- this.breakTo(box.breakBefore);
223
391
  // Continuing a table on a new page or column: repeat its header rows.
224
392
  if (box.repeatHeader && this.atColumnTop) {
225
393
  const prev = queue[i - 1];
@@ -295,12 +463,14 @@ class Paginator {
295
463
  pl.box.paint(p, pl.x, pl.y);
296
464
  }
297
465
  if (this.notes.length) {
298
- p.story = 'other';
466
+ p.story = 'footnote';
299
467
  const column = this.columns[0];
300
468
  let y = this.bottom - this.notesHeight;
301
469
  p.page.items.push({ type: 'line', x1: column.x, y1: y + NOTE_SEPARATOR_SPACE / 2, x2: column.x + Math.min(144, column.width / 3), y2: y + NOTE_SEPARATOR_SPACE / 2, width: 0.5, color: '000000', style: 'solid' });
302
470
  y += NOTE_SEPARATOR_SPACE;
303
471
  for (const n of this.notes) {
472
+ p.story = n.ref.type;
473
+ p.storyRef = { part: this.engine.notePart(n.ref.type) ?? '', noteId: n.ref.id };
304
474
  for (const b of n.boxes) {
305
475
  b.paint(p, column.x, y);
306
476
  y += b.height;
@@ -308,6 +478,7 @@ class Paginator {
308
478
  }
309
479
  }
310
480
  p.story = 'body';
481
+ p.storyRef = undefined;
311
482
  if (p.behind.length)
312
483
  p.page.items.unshift(...p.behind);
313
484
  this.placements = [];
@@ -323,9 +494,13 @@ class LayoutEngine {
323
494
  currentNote;
324
495
  noteCache = new Map();
325
496
  numPagesPlaceholder = '0';
497
+ cache;
498
+ faceCount;
326
499
  constructor(doc, options) {
327
500
  this.doc = doc;
328
- this.fonts = options.fonts instanceof FontRegistry ? new RegistryFontProvider(options.fonts) : options.fonts;
501
+ this.fonts = options.fonts instanceof FontRegistry ? providerFor(options.fonts) : options.fonts;
502
+ this.cache = options.cache;
503
+ this.faceCount = options.fonts instanceof FontRegistry ? Array.from(options.fonts.faces()).length : 0;
329
504
  this.counter = new NumberingCounter(doc.numbering, (s) => doc.styles.get(s)?.pPr.numId);
330
505
  }
331
506
  noteLabel = (inline) => {
@@ -343,11 +518,20 @@ class LayoutEngine {
343
518
  }
344
519
  return this.formatNote(inline.noteType, n);
345
520
  };
521
+ drawingCtx;
522
+ /** Document and fonts for painting drawings (shapes, charts, metafiles). */
523
+ drawingContext() {
524
+ return (this.drawingCtx ??= createDrawingContext(this.doc, this.fonts, (blocks, width) => blockBoxes(blocks, width, this.context(new RangeTracker(), new NumberingCounter(this.doc.numbering)))));
525
+ }
526
+ /** Part holding footnotes or endnotes. */
527
+ notePart(type) {
528
+ return type === 'footnote' ? this.doc.parts.footnotes : this.doc.parts.endnotes;
529
+ }
346
530
  formatNote(type, n) {
347
531
  return formatNumber(n, type === 'footnote' ? 'decimal' : 'lowerRoman');
348
532
  }
349
533
  context(tracker = this.tracker, counter = this.counter) {
350
- return { fonts: this.fonts, theme: this.doc.theme, tracker, counter, defaultTab: this.doc.defaultTabStop, noteLabel: this.noteLabel };
534
+ return { fonts: this.fonts, theme: this.doc.theme, tracker, counter, defaultTab: this.doc.defaultTabStop, noteLabel: this.noteLabel, cache: this.cache };
351
535
  }
352
536
  painterFor(page, column) {
353
537
  return {
@@ -355,6 +539,8 @@ class LayoutEngine {
355
539
  behind: [],
356
540
  column,
357
541
  story: 'body',
542
+ floatRects: new Map(),
543
+ drawing: this.drawingContext(),
358
544
  fieldValue: (field) => {
359
545
  if (field === 'PAGE')
360
546
  return page.label;
@@ -401,14 +587,19 @@ class LayoutEngine {
401
587
  };
402
588
  const saved = p.column;
403
589
  p.column = { x, width };
404
- p.story = 'other';
590
+ const headerRef = pick(s.headers);
591
+ p.story = 'header';
592
+ p.storyRef = headerRef ? { part: this.doc.headerFooterPart(headerRef.rId) ?? '' } : undefined;
405
593
  let y = m.header;
406
- for (const b of story(pick(s.headers))) {
594
+ for (const b of story(headerRef)) {
407
595
  b.paint(p, x, y);
408
596
  y += b.height;
409
597
  }
410
598
  const headerBottom = y;
411
- const footer = story(pick(s.footers));
599
+ const footerRef = pick(s.footers);
600
+ p.story = 'footer';
601
+ p.storyRef = footerRef ? { part: this.doc.headerFooterPart(footerRef.rId) ?? '' } : undefined;
602
+ const footer = story(footerRef);
412
603
  const footerHeight = footer.reduce((h, b) => h + b.height, 0);
413
604
  let fy = s.pageHeight - m.footer - footerHeight;
414
605
  const footerTop = fy;
@@ -418,9 +609,11 @@ class LayoutEngine {
418
609
  }
419
610
  p.column = saved;
420
611
  p.story = 'body';
612
+ p.storyRef = undefined;
421
613
  return { headerBottom, footerTop: Math.min(footerTop, s.pageHeight - m.footer) };
422
614
  }
423
615
  run() {
616
+ this.cache?.begin([this.fonts, this.faceCount, this.doc.theme, this.doc.styles, this.doc.numbering]);
424
617
  const story = this.doc.body();
425
618
  const groups = splitSections(story.blocks, story.finalSection);
426
619
  const paginator = new Paginator(this);
@@ -432,8 +625,22 @@ class LayoutEngine {
432
625
  if (paginator.endnotes.length) {
433
626
  const width = paginator.columns[0].width;
434
627
  const boxes = [{ height: NOTE_SEPARATOR_SPACE * 2, keepWithNext: true, paint: (p, x, y) => p.page.items.push({ type: 'line', x1: x, y1: y + NOTE_SEPARATOR_SPACE, x2: x + Math.min(144, width / 3), y2: y + NOTE_SEPARATOR_SPACE, width: 0.5, color: '000000', style: 'solid' }) }];
435
- for (const ref of paginator.endnotes)
436
- boxes.push(...this.noteBoxes(ref, width).boxes);
628
+ for (const ref of paginator.endnotes) {
629
+ const storyRef = { part: this.notePart('endnote') ?? '', noteId: ref.id };
630
+ // Endnotes flow like body text but are their own story for editing.
631
+ for (const b of this.noteBoxes(ref, width).boxes) {
632
+ boxes.push({
633
+ ...b,
634
+ paint: (p, x, y) => {
635
+ p.story = 'endnote';
636
+ p.storyRef = storyRef;
637
+ b.paint(p, x, y);
638
+ p.story = 'body';
639
+ p.storyRef = undefined;
640
+ },
641
+ });
642
+ }
643
+ }
437
644
  paginator.run(boxes);
438
645
  }
439
646
  paginator.finishPage();
@@ -1,5 +1,6 @@
1
1
  export * from './types.js';
2
2
  export { layoutDocument, type LayoutOptions } from './engine.js';
3
3
  export { RegistryFontProvider } from './fonts.js';
4
+ export { LayoutCache } from './cache.js';
4
5
  export { resolveRunStyle, HIGHLIGHT_COLORS, type RunStyle } from './style.js';
5
6
  export type { Box } from './blocks.js';
@@ -1,4 +1,5 @@
1
1
  export * from './types.js';
2
2
  export { layoutDocument } from './engine.js';
3
3
  export { RegistryFontProvider } from './fonts.js';
4
+ export { LayoutCache } from './cache.js';
4
5
  export { resolveRunStyle, HIGHLIGHT_COLORS } from './style.js';
@@ -7,7 +7,7 @@ import type { LinkTarget } from '../model/fields.js';
7
7
  import type { RunProps, TabStop } from '../model/props.js';
8
8
  import type { Theme } from '../model/theme.js';
9
9
  import { type RunStyle } from './style.js';
10
- import type { FontProvider, LayoutFont } from './types.js';
10
+ import type { DisplayItem, FontProvider, LayoutFont } from './types.js';
11
11
  export interface PermInfo {
12
12
  id: string;
13
13
  editor?: string;
@@ -32,9 +32,20 @@ export declare class RangeTracker {
32
32
  interface AtomBase {
33
33
  ctx: RangeCtx;
34
34
  }
35
+ /** One glyph from the shaping engine, in points: position relative to the pen, and its advance. */
36
+ export interface PlacedGlyph {
37
+ id: number;
38
+ dx: number;
39
+ dy: number;
40
+ advance: number;
41
+ /** Text the glyph stands for (a ligature covers several characters), for copy and search in PDFs. */
42
+ text: string;
43
+ }
35
44
  export interface CharAtom extends AtomBase {
36
45
  kind: 'char';
37
46
  ch: string;
47
+ /** Glyphs from the shaping engine (complex scripts, kerning, ligatures); absent: one glyph per code point. */
48
+ glyphs?: PlacedGlyph[];
38
49
  width: number;
39
50
  font: LayoutFont;
40
51
  size: number;
@@ -83,7 +94,17 @@ export interface MarkAtom extends AtomBase {
83
94
  mark: 'bookmark' | 'comment' | 'footnote' | 'endnote';
84
95
  name: string;
85
96
  }
86
- export type Atom = CharAtom | TabAtom | BreakAtom | ImageAtom | FloatAtom | FieldAtom | MarkAtom;
97
+ /** A typeset object placed like a character: an equation. Its items are relative to its baseline origin. */
98
+ export interface ObjectAtom extends AtomBase {
99
+ kind: 'object';
100
+ width: number;
101
+ ascent: number;
102
+ descent: number;
103
+ items: DisplayItem[];
104
+ /** Display equations sit alone on their line, aligned on their own. */
105
+ align?: 'left' | 'center' | 'right';
106
+ }
107
+ export type Atom = CharAtom | TabAtom | BreakAtom | ImageAtom | FloatAtom | FieldAtom | MarkAtom | ObjectAtom;
87
108
  export interface TextFragment {
88
109
  kind: 'text';
89
110
  x: number;
@@ -101,6 +122,16 @@ export interface TextFragment {
101
122
  offsets: number[];
102
123
  /** Width of trailing spaces that hang past the line end (not underlined). */
103
124
  trailing: number;
125
+ /**
126
+ * Right-to-left or complex-script text (Arabic, Hebrew, Indic…): its characters in logical
127
+ * order, for renderers that shape text themselves. `chars` and `xs` are in visual order.
128
+ */
129
+ complex?: {
130
+ text: string;
131
+ rtl: boolean;
132
+ };
133
+ /** Shaped glyphs per cluster (same order as `chars`). */
134
+ glyphs?: PlacedGlyph[][];
104
135
  }
105
136
  export interface TabFragment {
106
137
  kind: 'tab';
@@ -143,7 +174,14 @@ export interface MarkFragment {
143
174
  mark: MarkAtom['mark'];
144
175
  name: string;
145
176
  }
146
- export type Fragment = TextFragment | TabFragment | ImageFragment | FloatFragment | FieldFragment | MarkFragment;
177
+ export interface ObjectFragment {
178
+ kind: 'object';
179
+ x: number;
180
+ width: number;
181
+ items: DisplayItem[];
182
+ ctx: RangeCtx;
183
+ }
184
+ export type Fragment = TextFragment | TabFragment | ImageFragment | FloatFragment | FieldFragment | MarkFragment | ObjectFragment;
147
185
  export interface Line {
148
186
  fragments: Fragment[];
149
187
  /** Natural ascent/descent of the tallest content (pt). */
@@ -156,6 +194,15 @@ export interface Line {
156
194
  last: boolean;
157
195
  /** Range state at the end of the line (for zero-length ranges at line ends). */
158
196
  ctx: RangeCtx;
197
+ /** Empty space above the line: it was moved below a floating object that left no room. */
198
+ gapBefore?: number;
199
+ }
200
+ /** Horizontal room for a line, relative to the column's left edge (text wrapping around floats). */
201
+ export interface LineBounds {
202
+ left: number;
203
+ right: number;
204
+ /** When there is too little room here: the y (relative to the paragraph top) where room starts. */
205
+ next?: number;
159
206
  }
160
207
  export interface AtomContext {
161
208
  fonts: FontProvider;
@@ -189,6 +236,17 @@ export interface LineGeometry {
189
236
  /** Metrics used for empty lines and the paragraph mark. */
190
237
  markAscent: number;
191
238
  markDescent: number;
192
- }
193
- export declare function breakLines(atoms: Atom[], g: LineGeometry): Line[];
239
+ /** Room at a given height (relative to the paragraph's first line), for wrapping around floats. */
240
+ bounds?: (top: number, height: number) => LineBounds | undefined;
241
+ /** The height a finished line will take (to know where the next line starts). */
242
+ lineHeight?: (line: Line) => number;
243
+ /**
244
+ * A right-to-left paragraph: the geometry above is measured from the right edge (start),
245
+ * and lines are mirrored onto the page within this width.
246
+ */
247
+ mirror?: number;
248
+ }
249
+ export declare function breakLines(atoms: Atom[], geometry: LineGeometry): Line[];
250
+ /** Whether a character needs a shaping engine to look right (its renderer should shape the whole run). */
251
+ export declare function needsShaping(ch: string): boolean;
194
252
  export {};