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
@@ -1,4 +1,9 @@
1
1
  import { fontNameForSlot, isEastAsian, resolveRunStyle, scriptSlot } from './style.js';
2
+ import { layoutMath } from '../math/index.js';
3
+ import { mirrorChar } from '../text/bidi.js';
4
+ import { shape } from '../fonts/shaping/index.js';
5
+ import { analyzeBidi, bidiOf, visualAtoms } from './bidi.js';
6
+ import { attr, firstChild } from '../xml/index.js';
2
7
  export const EMPTY_CTX = Object.freeze({ comments: [], perms: [], locks: [] });
3
8
  export class RangeTracker {
4
9
  ctx = EMPTY_CTX;
@@ -82,6 +87,11 @@ export function pushText(ctx, atoms, text, props, inline) {
82
87
  const style = resolveRunStyle(props, ctx.theme);
83
88
  if (style.hidden)
84
89
  return;
90
+ const first = atoms.length;
91
+ pushClusters(ctx, atoms, text, style, inline);
92
+ shapeAtoms(atoms, first);
93
+ }
94
+ function pushClusters(ctx, atoms, text, style, inline) {
85
95
  let offset = 0;
86
96
  for (const cluster of clusters(text)) {
87
97
  const cp = cluster.codePointAt(0);
@@ -166,6 +176,15 @@ export function buildAtoms(para, ctx, label) {
166
176
  else
167
177
  atoms.push({ kind: 'image', inline, width: inline.width, height: inline.height, ctx: ctx.tracker.ctx });
168
178
  break;
179
+ case 'math': {
180
+ const style = resolveRunStyle(inline.props, ctx.theme);
181
+ // An equation alone in its paragraph is a display equation, as in Word.
182
+ const display = inline.display || para.inlines.every((i) => i.kind === 'math' || (i.kind !== 'text' && i.kind !== 'tab' && i.kind !== 'image' && i.kind !== 'field'));
183
+ const m = layoutMath(inline.node, { fonts: ctx.fonts, size: style.size, color: style.color === 'auto' ? '000000' : style.color, display });
184
+ const jc = mathJc(inline.node);
185
+ atoms.push({ kind: 'object', width: m.width, ascent: m.ascent, descent: m.descent, items: m.items, align: display ? jc : undefined, ctx: ctx.tracker.ctx });
186
+ break;
187
+ }
169
188
  case 'field': {
170
189
  const style = resolveRunStyle(inline.props, ctx.theme);
171
190
  if (style.hidden)
@@ -192,6 +211,7 @@ export function buildAtoms(para, ctx, label) {
192
211
  }
193
212
  }
194
213
  markBreakOpportunities(atoms);
214
+ analyzeBidi(atoms, !!para.props.bidi);
195
215
  return atoms;
196
216
  }
197
217
  function markBreakOpportunities(atoms) {
@@ -199,7 +219,7 @@ function markBreakOpportunities(atoms) {
199
219
  const a = atoms[i];
200
220
  if (a.kind !== 'char') {
201
221
  // Images and fields can break on both sides.
202
- if (a.kind === 'image' || a.kind === 'field') {
222
+ if (a.kind === 'image' || a.kind === 'field' || a.kind === 'object') {
203
223
  const prev = atoms[i - 1];
204
224
  if (prev && prev.kind === 'char')
205
225
  prev.breakAfter = true;
@@ -224,16 +244,19 @@ function markBreakOpportunities(atoms) {
224
244
  if (nextCp !== undefined && (isEastAsian(cp) || isEastAsian(nextCp)) && !NO_LINE_START.has(nextCp) && !NO_LINE_END.has(cp)) {
225
245
  a.breakAfter = true;
226
246
  }
227
- if (next && next.kind === 'image')
247
+ if (next && (next.kind === 'image' || next.kind === 'object'))
228
248
  a.breakAfter = true;
229
249
  }
230
250
  }
251
+ /** Narrowest line worth placing beside a floating object; anything narrower moves below it. */
252
+ const MIN_WRAP_WIDTH = 24;
231
253
  const EPS = 0.01;
232
254
  function atomWidth(a) {
233
255
  switch (a.kind) {
234
256
  case 'char':
235
257
  return a.width;
236
258
  case 'image':
259
+ case 'object':
237
260
  return a.width;
238
261
  case 'field':
239
262
  return a.width;
@@ -271,15 +294,42 @@ function segmentWidth(atoms, from, decimal) {
271
294
  }
272
295
  return w;
273
296
  }
274
- export function breakLines(atoms, g) {
297
+ /** Geometry for the line starting at `top`: the paragraph's own, narrowed around floats. */
298
+ function lineGeometry(g, first, top) {
299
+ const own = first ? g.firstLeft : g.left;
300
+ if (!g.bounds)
301
+ return { g, left: own, gap: 0 };
302
+ const estimate = g.markAscent + g.markDescent;
303
+ let gap = 0;
304
+ const W = g.mirror;
305
+ for (let tries = 0; tries < 20; tries++) {
306
+ const raw = g.bounds(top + gap, estimate);
307
+ // Room beside floats is on the page; a right-to-left paragraph measures from the right.
308
+ const b = raw && W !== undefined ? { ...raw, left: W - raw.right, right: W - raw.left } : raw;
309
+ if (!b)
310
+ return { g, left: own, gap };
311
+ const left = Math.max(own, b.left);
312
+ const right = Math.min(g.right, b.right);
313
+ if (right - left >= MIN_WRAP_WIDTH || b.next === undefined || b.next <= top + gap) {
314
+ return { g: { ...g, right, firstLeft: first ? left : g.firstLeft, left: first ? g.left : left }, left, gap };
315
+ }
316
+ gap = b.next - top;
317
+ }
318
+ return { g, left: own, gap };
319
+ }
320
+ export function breakLines(atoms, geometry) {
275
321
  const lines = [];
276
322
  let start = 0;
277
323
  let first = true;
324
+ let top = 0;
278
325
  if (atoms.length === 0) {
279
- return [{ fragments: [], ascent: g.markAscent, descent: g.markDescent, left: g.firstLeft, right: g.right, last: true, ctx: EMPTY_CTX }];
326
+ const { g, left, gap } = lineGeometry(geometry, true, 0);
327
+ return [{ fragments: [], ascent: g.markAscent, descent: g.markDescent, left, right: g.right, last: true, ctx: EMPTY_CTX, ...(gap ? { gapBefore: gap } : {}) }];
280
328
  }
281
329
  while (start < atoms.length) {
282
- const left = first ? g.firstLeft : g.left;
330
+ const lg = lineGeometry(geometry, first, top);
331
+ const g = lg.g;
332
+ const left = lg.left;
283
333
  const pos = new Map();
284
334
  const tabWidths = new Map();
285
335
  const tabStops = new Map();
@@ -332,7 +382,7 @@ export function breakLines(atoms, g) {
332
382
  }
333
383
  pos.set(i, x);
334
384
  x += w;
335
- if (w > 0 || a.kind === 'image')
385
+ if (w > 0 || a.kind === 'image' || a.kind === 'object')
336
386
  visible = true;
337
387
  if (a.kind === 'char' && a.breakAfter)
338
388
  lastBreak = i;
@@ -340,7 +390,11 @@ export function breakLines(atoms, g) {
340
390
  if (end <= start)
341
391
  end = start + 1; // always make progress
342
392
  const last = end >= atoms.length;
343
- lines.push(finishLine(atoms, start, end, pos, tabWidths, tabStops, g, left, forced, last));
393
+ const line = finishLine(atoms, start, end, pos, tabWidths, tabStops, g, left, forced, last);
394
+ if (lg.gap)
395
+ line.gapBefore = lg.gap;
396
+ lines.push(line);
397
+ top += lg.gap + (geometry.lineHeight ? geometry.lineHeight(line) : line.ascent + line.descent);
344
398
  start = end;
345
399
  first = false;
346
400
  }
@@ -368,12 +422,23 @@ function finishLine(atoms, start, end, pos, tabWidths, tabStops, g, left, forced
368
422
  const w = a.kind === 'tab' ? tabWidths.get(i) : atomWidth(a);
369
423
  contentEnd = Math.max(contentEnd, p + w);
370
424
  }
371
- // Alignment and justification shift positions.
425
+ // Alignment and justification shift positions. A display equation alone on its line uses its own alignment.
372
426
  const extra = g.right - contentEnd;
373
427
  const shift = new Map();
374
- const justify = (g.jc === 'both' && !last && forced !== 'page' && forced !== 'column') || g.jc === 'distribute';
375
- if (extra > EPS && (g.jc === 'center' || g.jc === 'right')) {
376
- const offset = g.jc === 'center' ? extra / 2 : extra;
428
+ let jc = g.jc;
429
+ const objects = [];
430
+ for (let i = start; i < end; i++) {
431
+ const a = atoms[i];
432
+ if (a.kind === 'object' && a.align)
433
+ objects.push(a);
434
+ else if (a.kind !== 'mark' && a.kind !== 'break' && a.kind !== 'float' && !(a.kind === 'char' && a.space))
435
+ objects.push(undefined);
436
+ }
437
+ if (objects.length === 1 && objects[0])
438
+ jc = objects[0].align;
439
+ const justify = (jc === 'both' && !last && forced !== 'page' && forced !== 'column') || jc === 'distribute';
440
+ if (extra > EPS && (jc === 'center' || jc === 'right')) {
441
+ const offset = jc === 'center' ? extra / 2 : extra;
377
442
  for (let i = start; i < end; i++)
378
443
  shift.set(i, offset);
379
444
  }
@@ -406,7 +471,7 @@ function finishLine(atoms, start, end, pos, tabWidths, tabStops, g, left, forced
406
471
  a.width += each;
407
472
  }
408
473
  }
409
- else if (g.jc === 'distribute') {
474
+ else if (jc === 'distribute') {
410
475
  const chars = [];
411
476
  for (let i = Math.max(start, lastTab + 1); i < lastContent; i++)
412
477
  if (atoms[i].kind === 'char')
@@ -424,30 +489,84 @@ function finishLine(atoms, start, end, pos, tabWidths, tabStops, g, left, forced
424
489
  }
425
490
  }
426
491
  }
492
+ // Visual order: right-to-left runs are reversed (Unicode bidi), and a right-to-left
493
+ // paragraph's line is mirrored onto the page.
494
+ let order = [];
495
+ for (let i = start; i < end; i++)
496
+ order.push(i);
497
+ let xOf = (i) => (pos.get(i) ?? left) + (shift.get(i) ?? 0);
498
+ const levelOf = new Map();
499
+ const info = bidiOf(atoms);
500
+ if (info || g.mirror !== undefined) {
501
+ const widthOf = (i) => {
502
+ const a = atoms[i];
503
+ if (a.kind === 'tab')
504
+ return tabWidths.get(i) ?? 0;
505
+ if (a.kind === 'char' && a.softHyphen && a.ch !== '-')
506
+ return 0;
507
+ return atomWidth(a);
508
+ };
509
+ let lo = Infinity;
510
+ let hi = -Infinity;
511
+ for (const i of order) {
512
+ if (!pos.has(i))
513
+ continue;
514
+ lo = Math.min(lo, xOf(i));
515
+ hi = Math.max(hi, xOf(i) + widthOf(i));
516
+ }
517
+ if (lo === Infinity)
518
+ lo = hi = left;
519
+ if (info) {
520
+ const visual = visualAtoms(info, start, end);
521
+ order = visual.map((v) => v.atom);
522
+ for (const v of visual)
523
+ levelOf.set(v.atom, v.level);
524
+ }
525
+ const placed = new Map();
526
+ let x = g.mirror !== undefined ? g.mirror - hi : lo;
527
+ for (const i of order) {
528
+ placed.set(i, x);
529
+ x += widthOf(i);
530
+ }
531
+ xOf = (i) => placed.get(i) ?? left;
532
+ }
427
533
  // Group atoms into fragments.
428
534
  const fragments = [];
429
535
  let ascent = 0;
430
536
  let descent = 0;
431
537
  let current;
432
538
  let lineCtx = atoms[start]?.ctx ?? EMPTY_CTX;
433
- for (let i = start; i < end; i++) {
539
+ for (const i of order) {
434
540
  const a = atoms[i];
435
- const x = (pos.get(i) ?? left) + (shift.get(i) ?? 0);
541
+ const x = xOf(i);
436
542
  lineCtx = a.ctx;
437
543
  if (a.kind === 'char') {
438
544
  if (a.softHyphen && a.ch !== '-')
439
545
  continue;
440
546
  ascent = Math.max(ascent, a.font.ascent * a.size + a.shift);
441
547
  descent = Math.max(descent, a.font.descent * a.size - a.shift);
442
- if (!current || current.font !== a.font || current.size !== a.size || current.shift !== a.shift || current.style !== a.style || current.ctx !== a.ctx || current.inline !== a.inline) {
443
- current = { kind: 'text', x, width: 0, chars: [], xs: [], widths: [], font: a.font, size: a.size, shift: a.shift, style: a.style, ctx: a.ctx, inline: a.inline, offsets: [], trailing: 0 };
548
+ const level = levelOf.get(i) ?? 0;
549
+ const rtl = level % 2 === 1;
550
+ const complex = rtl || needsShaping(a.ch);
551
+ if (!current || current.font !== a.font || current.size !== a.size || current.shift !== a.shift || current.style !== a.style || current.ctx !== a.ctx || current.inline !== a.inline || !!current.complex !== complex || (current.complex && current.complex.rtl !== rtl)) {
552
+ current = { kind: 'text', x, width: 0, chars: [], xs: [], widths: [], font: a.font, size: a.size, shift: a.shift, style: a.style, ctx: a.ctx, inline: a.inline, offsets: [], trailing: 0, ...(complex ? { complex: { text: '', rtl } } : {}) };
444
553
  fragments.push(current);
445
554
  }
446
- current.chars.push(a.ch);
555
+ // Rule L4: brackets and similar characters are mirrored in right-to-left text.
556
+ const shown = rtl && a.ch.length <= 2 ? String.fromCodePoint(mirrorChar(a.ch.codePointAt(0)) ?? a.ch.codePointAt(0)) : a.ch;
557
+ if (a.glyphs || current.glyphs) {
558
+ // Shaped glyphs per cluster; unshaped clusters in the same fragment get their plain glyphs.
559
+ const plain = (atom, ch) => [...ch].map((c, k) => ({ id: atom.font.glyphId(c.codePointAt(0)), dx: 0, dy: 0, advance: atom.font.advance(c.codePointAt(0)) * atom.size, text: k === 0 ? ch : '' }));
560
+ current.glyphs ??= current.chars.map((c) => plain(a, c));
561
+ current.glyphs.push(a.glyphs ?? plain(a, shown));
562
+ }
563
+ current.chars.push(shown);
447
564
  current.xs.push(x);
448
565
  current.widths.push(a.width);
449
566
  current.offsets.push(a.offset);
450
- current.width = x + a.width - current.x;
567
+ current.width = Math.max(current.width, x + a.width - current.x);
568
+ if (current.complex)
569
+ current.complex.text = rtl ? a.ch + current.complex.text : current.complex.text + a.ch;
451
570
  continue;
452
571
  }
453
572
  current = undefined;
@@ -466,6 +585,11 @@ function finishLine(atoms, start, end, pos, tabWidths, tabStops, g, left, forced
466
585
  case 'float':
467
586
  fragments.push({ kind: 'float', x, inline: a.inline });
468
587
  break;
588
+ case 'object':
589
+ ascent = Math.max(ascent, a.ascent);
590
+ descent = Math.max(descent, a.descent);
591
+ fragments.push({ kind: 'object', x, width: a.width, items: a.items, ctx: a.ctx });
592
+ break;
469
593
  case 'field':
470
594
  ascent = Math.max(ascent, a.font.ascent * a.size + a.shift);
471
595
  descent = Math.max(descent, a.font.descent * a.size - a.shift);
@@ -502,10 +626,111 @@ function finishLine(atoms, start, end, pos, tabWidths, tabStops, g, left, forced
502
626
  fragments,
503
627
  ascent,
504
628
  descent,
505
- left,
506
- right: g.right,
629
+ // A right-to-left line's content box, mirrored like its content.
630
+ left: g.mirror !== undefined ? g.mirror - g.right : left,
631
+ right: g.mirror !== undefined ? g.mirror - left : g.right,
507
632
  breakAfter: forced === 'page' || forced === 'column' ? forced : undefined,
508
633
  last,
509
634
  ctx: lineCtx,
510
635
  };
511
636
  }
637
+ /** Alignment of a display equation (m:oMathParaPr/m:jc; Word's default centres it). */
638
+ function mathJc(node) {
639
+ const jc = attr(firstChild(firstChild(node, 'm:oMathParaPr'), 'm:jc'), 'm:val');
640
+ return jc === 'left' ? 'left' : jc === 'right' ? 'right' : 'center';
641
+ }
642
+ /** Scripts whose letters join or reorder (Arabic, Syriac, Thaana, N'Ko, Indic, Thai, Lao, Tibetan, Myanmar, Khmer, Mongolian). */
643
+ const SHAPED = /[\u0600-\u08ff\u0900-\u0dff\u0e00-\u0eff\u0f00-\u0fff\u1000-\u109f\u1780-\u18af\ua800-\uabff\ufb50-\ufdff\ufe70-\ufeff]/;
644
+ /** Whether a character needs a shaping engine to look right (its renderer should shape the whole run). */
645
+ export function needsShaping(ch) {
646
+ return SHAPED.test(ch);
647
+ }
648
+ /** Scripts written right to left (Hebrew, Arabic, Syriac, Thaana, N'Ko, Samaritan, Mandaic). */
649
+ const RTL_SCRIPT = /[\u0590-\u08ff\ufb1d-\ufdff\ufe70-\ufeff]/;
650
+ /**
651
+ * Run the OpenType shaping engine over runs of atoms that need it: complex scripts (Arabic
652
+ * joining, Indic reordering, mark placement), and Latin text where Word turns on kerning
653
+ * (w:kern, from that size up) or ligatures (w14:ligatures). Each atom keeps its own cluster;
654
+ * a ligature's glyph goes to the first atom it covers and the others get no glyphs.
655
+ */
656
+ function shapeAtoms(atoms, from) {
657
+ let i = from;
658
+ while (i < atoms.length) {
659
+ const a = atoms[i];
660
+ if (a.kind !== 'char' || !wantsShaping(a)) {
661
+ i++;
662
+ continue;
663
+ }
664
+ // A run with one font, size and style (spaces included, so kerning and joining see whole words).
665
+ let j = i + 1;
666
+ while (j < atoms.length) {
667
+ const b = atoms[j];
668
+ if (b.kind !== 'char' || b.font !== a.font || b.size !== a.size || b.style !== a.style || b.softHyphen)
669
+ break;
670
+ j++;
671
+ }
672
+ shapeRun(atoms.slice(i, j));
673
+ i = j;
674
+ }
675
+ }
676
+ function wantsShaping(a) {
677
+ if (!a.font.face || a.softHyphen)
678
+ return false;
679
+ if (needsShaping(a.ch))
680
+ return true;
681
+ if (a.style.kern !== undefined && a.size >= a.style.kern)
682
+ return true;
683
+ return !!a.style.ligatures;
684
+ }
685
+ function shapeRun(run) {
686
+ const first = run[0];
687
+ const font = first.font.face.font;
688
+ let text = '';
689
+ const starts = [];
690
+ for (const a of run) {
691
+ starts.push(text.length);
692
+ text += a.ch;
693
+ }
694
+ const rtl = RTL_SCRIPT.test(text);
695
+ const features = {};
696
+ const complex = run.some((a) => needsShaping(a.ch));
697
+ if (!complex) {
698
+ // Latin: only what Word asks for (it applies no ligatures by default).
699
+ const lig = first.style.ligatures;
700
+ features.liga = !!lig && lig !== 'none';
701
+ features.clig = !!lig && /all|contextual/i.test(lig);
702
+ features.dlig = !!lig && /all|discretional/i.test(lig);
703
+ features.kern = first.style.kern !== undefined && first.size >= first.style.kern;
704
+ }
705
+ let glyphs;
706
+ try {
707
+ glyphs = shape(font, text, { direction: rtl ? 'rtl' : 'ltr', features });
708
+ }
709
+ catch {
710
+ return;
711
+ }
712
+ const scale = first.size / (font.unitsPerEm || 1000);
713
+ const owner = (cluster) => {
714
+ let k = 0;
715
+ while (k + 1 < starts.length && starts[k + 1] <= cluster)
716
+ k++;
717
+ return k;
718
+ };
719
+ const per = run.map(() => []);
720
+ // The text each glyph stands for: from its cluster to the next cluster's start.
721
+ const clusterStarts = [...new Set(glyphs.map((g) => g.cluster))].sort((x, y) => x - y);
722
+ const seen = new Set();
723
+ for (const g of glyphs) {
724
+ const k = owner(g.cluster);
725
+ const next = clusterStarts.find((c) => c > g.cluster) ?? text.length;
726
+ const glyphText = seen.has(g.cluster) ? '' : text.slice(g.cluster, next);
727
+ seen.add(g.cluster);
728
+ per[k].push({ id: g.glyph, dx: g.offsetX * scale, dy: g.offsetY * scale, advance: g.advance * scale, text: glyphText });
729
+ }
730
+ run.forEach((a, k) => {
731
+ const gl = per[k];
732
+ a.glyphs = gl;
733
+ const width = gl.reduce((s, g) => s + g.advance, 0);
734
+ a.width = width * a.style.scale + (gl.length ? a.style.spacing : 0);
735
+ });
736
+ }
@@ -1,7 +1,9 @@
1
1
  /** Turns positioned lines into display items and collects link / comment / permission areas. */
2
2
  import type { Border } from '../model/props.js';
3
+ import type { FloatInfo, ImageInline } from '../model/content.js';
4
+ import { type DrawingContext } from './drawing.js';
3
5
  import type { Line } from './lines.js';
4
- import type { DisplayItem, LayoutFont, Page, TextItem } from './types.js';
6
+ import type { DisplayItem, LayoutFont, Page, TextItem, StoryKind, StoryRef } from './types.js';
5
7
  export interface Painter {
6
8
  page: Page;
7
9
  /** Items drawn behind the text (floating images with behindDoc). */
@@ -13,8 +15,17 @@ export interface Painter {
13
15
  };
14
16
  /** Text of a page-number field at this position. */
15
17
  fieldValue(field: string, format?: string): string;
16
- /** Which story is being painted; only body lines are recorded for editing. */
17
- story: 'body' | 'other';
18
+ /** Which story is being painted: body lines go to `page.lines`, the others to `page.storyLines`. */
19
+ story: 'body' | StoryKind;
20
+ /** The part (and note or text box) of a non-body story. */
21
+ storyRef?: StoryRef;
22
+ /** Document and fonts for drawing shapes, charts and metafiles. */
23
+ drawing?: DrawingContext;
24
+ /** Where pagination already placed floating objects (so text wrapped around the same spot). */
25
+ floatRects?: Map<ImageInline, {
26
+ x: number;
27
+ y: number;
28
+ }>;
18
29
  }
19
30
  export declare function colorOf(c: string | undefined, fallback?: string): string;
20
31
  export declare function addItem(p: Painter, item: DisplayItem): void;
@@ -33,6 +44,17 @@ export interface LineBand {
33
44
  }
34
45
  /** Paint a line whose top-left is (originX, top) with its baseline at `baseline`. */
35
46
  export declare function paintLine(p: Painter, line: Line, originX: number, band: LineBand): void;
47
+ /** Top-left of a floating object on its page, from its anchor position settings. */
48
+ export declare function floatPosition(page: Page, column: {
49
+ x: number;
50
+ width: number;
51
+ }, f: FloatInfo, image: {
52
+ width: number;
53
+ height: number;
54
+ }, charX: number, lineTop: number): {
55
+ x: number;
56
+ y: number;
57
+ };
36
58
  /** Draw a border segment centred on the given edge. */
37
59
  export declare function borderLine(p: Painter, b: Border | undefined, x1: number, y1: number, x2: number, y2: number): void;
38
60
  export declare function pageNumberLabel(n: number, format: string | undefined): string;
@@ -1,4 +1,5 @@
1
1
  import { formatNumber } from '../model/numbering.js';
2
+ import { drawObject, translateItems } from './drawing.js';
2
3
  export function colorOf(c, fallback = '000000') {
3
4
  if (!c || c === 'auto')
4
5
  return fallback;
@@ -94,6 +95,10 @@ function paintFragment(p, frag, originX, top, height, baseline) {
94
95
  const item = textItem(x, baseline - frag.shift, frag.chars, frag.xs.map((v) => originX + v), frag.widths, frag.font, frag.size, colorOf(frag.style.color), frag.style.scale);
95
96
  if (frag.inline)
96
97
  item.source = { inline: frag.inline, offsets: frag.offsets };
98
+ if (frag.complex)
99
+ item.complex = frag.complex;
100
+ if (frag.glyphs)
101
+ item.clusterGlyphs = frag.glyphs;
97
102
  addItem(p, item);
98
103
  decorate(p, frag, x, drawnWidth, baseline);
99
104
  recordRanges(p, frag.ctx, { x, y: top, w: frag.width, h: height });
@@ -119,13 +124,21 @@ function paintFragment(p, frag, originX, top, height, baseline) {
119
124
  case 'image': {
120
125
  const x = originX + frag.x;
121
126
  const y = baseline - frag.height;
122
- addItem(p, { type: 'image', x, y, w: frag.width, h: frag.height, target: frag.inline.target, external: frag.inline.external, alt: frag.inline.alt, inline: frag.inline });
127
+ for (const item of drawObject(p, frag.inline, { x, y, w: frag.width, h: frag.height }))
128
+ addItem(p, item);
123
129
  recordRanges(p, frag.ctx, { x, y, w: frag.width, h: frag.height });
124
130
  break;
125
131
  }
126
132
  case 'float':
127
133
  paintFloat(p, frag.inline.float, frag.inline, originX + frag.x, top);
128
134
  break;
135
+ case 'object': {
136
+ const x = originX + frag.x;
137
+ for (const item of translateItems(frag.items, x, baseline))
138
+ addItem(p, item);
139
+ recordRanges(p, frag.ctx, { x, y: top, w: frag.width, h: height });
140
+ break;
141
+ }
129
142
  case 'field': {
130
143
  const x = originX + frag.x;
131
144
  const value = p.fieldValue(frag.field, frag.format);
@@ -152,7 +165,16 @@ function paintFragment(p, frag, originX, top, height, baseline) {
152
165
  }
153
166
  }
154
167
  function paintFloat(p, f, image, charX, lineTop) {
155
- const page = p.page;
168
+ const { x, y } = p.floatRects?.get(image) ?? floatPosition(p.page, p.column, f, image, charX, lineTop);
169
+ const items = drawObject(p, image, { x, y, w: image.width, h: image.height });
170
+ if (f.behindDoc)
171
+ p.behind.push(...items);
172
+ else
173
+ for (const item of items)
174
+ addItem(p, item);
175
+ }
176
+ /** Top-left of a floating object on its page, from its anchor position settings. */
177
+ export function floatPosition(page, column, f, image, charX, lineTop) {
156
178
  const m = page.section.margin;
157
179
  const area = (rel, horizontal) => {
158
180
  if (horizontal) {
@@ -170,7 +192,7 @@ function paintFloat(p, f, image, charX, lineTop) {
170
192
  case 'character':
171
193
  return { start: charX, size: 0 };
172
194
  default:
173
- return { start: p.column.x, size: p.column.width };
195
+ return { start: column.x, size: column.width };
174
196
  }
175
197
  }
176
198
  switch (rel) {
@@ -197,13 +219,7 @@ function paintFloat(p, f, image, charX, lineTop) {
197
219
  return a.start;
198
220
  return a.start + offset;
199
221
  };
200
- const x = align(h, f.hAlign, f.hOffset, image.width);
201
- const y = align(v, f.vAlign, f.vOffset, image.height);
202
- const item = { type: 'image', x, y, w: image.width, h: image.height, target: image.target, external: image.external, alt: image.alt, inline: image };
203
- if (f.behindDoc)
204
- p.behind.push(item);
205
- else
206
- addItem(p, item);
222
+ return { x: align(h, f.hAlign, f.hOffset, image.width), y: align(v, f.vAlign, f.vOffset, image.height) };
207
223
  }
208
224
  /** Draw a border segment centred on the given edge. */
209
225
  export function borderLine(p, b, x1, y1, x2, y2) {
@@ -0,0 +1,7 @@
1
+ import type { Block, ImageInline } from '../model/content.js';
2
+ import type { DocxDocument } from '../model/document.js';
3
+ import type { Box } from './blocks.js';
4
+ import { type DrawingContext } from './drawing.js';
5
+ import type { FontProvider } from './types.js';
6
+ export declare function createDrawingContext(doc: DocxDocument, fonts: FontProvider, layoutBlocks: (blocks: Block[], width: number) => Box[]): DrawingContext;
7
+ export type { ImageInline };