documents.js 1.81.8 → 1.82.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.
@@ -50,7 +50,7 @@ function columnLetters(index) {
50
50
  }
51
51
  function resolvePrintRange(sheet, formulas) {
52
52
  if (sheet.printSettings.printRange !== void 0) return sheet.printSettings.printRange;
53
- if (sheet.cells.length === 0 && formulas.length === 0) return;
53
+ if (sheet.cells.length === 0 && formulas.length === 0 && sheet.images.length === 0) return;
54
54
  let startRow = Number.POSITIVE_INFINITY;
55
55
  let startColumn = Number.POSITIVE_INFINITY;
56
56
  let endRow = Number.NEGATIVE_INFINITY;
@@ -67,6 +67,12 @@ function resolvePrintRange(sheet, formulas) {
67
67
  endRow = Math.max(endRow, formula.anchorRow);
68
68
  endColumn = Math.max(endColumn, formula.anchorColumn);
69
69
  }
70
+ for (const image of sheet.images) {
71
+ startRow = Math.min(startRow, image.anchorRow);
72
+ startColumn = Math.min(startColumn, image.anchorColumn);
73
+ endRow = Math.max(endRow, image.anchorRow);
74
+ endColumn = Math.max(endColumn, image.anchorColumn);
75
+ }
70
76
  return {
71
77
  startRow,
72
78
  startColumn,
@@ -364,7 +370,7 @@ function renderAnchoredFormulas(formulas, columnAxis, rowAxis, gridLeftXPt, grid
364
370
  const columnPosition = columnAxis.positionByIndex.get(anchored.anchorColumn);
365
371
  const rowPosition = rowAxis.positionByIndex.get(anchored.anchorRow);
366
372
  if (columnPosition === void 0 || rowPosition === void 0 || hiddenColumnIndices.has(anchored.anchorColumn) || hiddenRowIndices.has(anchored.anchorRow)) continue;
367
- const sizePt = require_layout_shared.formulaSizePtFromFrame(anchored.frame.heightPt);
373
+ const sizePt = require_layout_shared.formulaSizePtForFrame(anchored.formula.mathml, anchored.frame);
368
374
  const metrics = (0, pdf_codec.loadMathFont)().metricsAt(sizePt);
369
375
  const { box } = require_mathml_layout.layoutFormula(anchored.formula.mathml, {
370
376
  metrics,
@@ -386,6 +392,31 @@ function renderAnchoredFormulas(formulas, columnAxis, rowAxis, gridLeftXPt, grid
386
392
  });
387
393
  }
388
394
  }
395
+ function renderAnchoredImages(sheetImages, columnAxis, rowAxis, gridLeftXPt, gridTopYDownPt, pageHeightPt, hiddenColumnIndices, hiddenRowIndices, out, images) {
396
+ for (const image of sheetImages) {
397
+ const columnPosition = columnAxis.positionByIndex.get(image.anchorColumn);
398
+ const rowPosition = rowAxis.positionByIndex.get(image.anchorRow);
399
+ if (columnPosition === void 0 || rowPosition === void 0 || hiddenColumnIndices.has(image.anchorColumn) || hiddenRowIndices.has(image.anchorRow)) continue;
400
+ const imageId = require_layout_shared.registerImage(image, images);
401
+ const boxYDown = {
402
+ xPt: gridLeftXPt + columnAxis.offsetsPt[columnPosition] + image.offsetXPt,
403
+ yPt: gridTopYDownPt + rowAxis.offsetsPt[rowPosition] + image.offsetYPt,
404
+ widthPt: image.widthPt,
405
+ heightPt: image.heightPt
406
+ };
407
+ const flipped = require_model_geometry.flipY(boxYDown, pageHeightPt);
408
+ const imageItem = {
409
+ kind: "image",
410
+ imageId,
411
+ xPt: flipped.xPt,
412
+ yPt: flipped.yPt,
413
+ widthPt: image.widthPt,
414
+ heightPt: image.heightPt,
415
+ sourcePath: image.sourcePath
416
+ };
417
+ out.push(imageItem);
418
+ }
419
+ }
389
420
  function bandableIndices(start, end, repeat) {
390
421
  const indices = [];
391
422
  for (let i = start; i <= end; i++) {
@@ -399,7 +430,7 @@ function rangeIndices(start, end) {
399
430
  for (let i = start; i <= end; i++) indices.push(i);
400
431
  return indices;
401
432
  }
402
- function convertSheetToPages(sheet, measurer, signal, out, formulasOut) {
433
+ function convertSheetToPages(sheet, measurer, signal, out, formulasOut, images) {
403
434
  require_ports_abort.throwIfAborted(signal);
404
435
  const formulas = anchoredFormulas(sheet);
405
436
  const range = resolvePrintRange(sheet, formulas);
@@ -489,6 +520,7 @@ function convertSheetToPages(sheet, measurer, signal, out, formulasOut) {
489
520
  if (printSettings.headers) renderHeaderLabels(gutter, columnAxis, rowAxis, gridLeftXPt, gridTopYDownPt, pageSize.heightPt, measurer, items);
490
521
  items.push(...textItems);
491
522
  renderAnchoredFormulas(formulas, columnAxis, rowAxis, gridLeftXPt, gridTopYDownPt, pageSize.heightPt, out.length, hiddenColumnIndices, hiddenRowIndices, formulasOut);
523
+ renderAnchoredImages(sheet.images, columnAxis, rowAxis, gridLeftXPt, gridTopYDownPt, pageSize.heightPt, hiddenColumnIndices, hiddenRowIndices, items, images);
492
524
  out.push({
493
525
  widthPt: pageSize.widthPt,
494
526
  heightPt: pageSize.heightPt,
@@ -499,13 +531,14 @@ function convertSheetToPages(sheet, measurer, signal, out, formulasOut) {
499
531
  function convertSpreadsheetToLayout(doc, options) {
500
532
  const pages = [];
501
533
  const formulas = [];
502
- for (const sheet of doc.sheets) convertSheetToPages(sheet, options.measurer, options.signal, pages, formulas);
534
+ const images = {};
535
+ for (const sheet of doc.sheets) convertSheetToPages(sheet, options.measurer, options.signal, pages, formulas, images);
503
536
  return {
504
537
  document: {
505
538
  formatVersion: document_schema_js.LAYOUT_FORMAT_VERSION,
506
539
  metadata: doc.metadata,
507
540
  pages,
508
- images: {}
541
+ images
509
542
  },
510
543
  formulas
511
544
  };
@@ -2,7 +2,7 @@ import { flipY } from "../model/geometry.js";
2
2
  import { COLOR_BLACK as COLOR_BLACK$1, rgbHexToColor as rgbHexToColor$1 } from "../model/color.js";
3
3
  import { DEFAULT_LAYOUT_FONT } from "../model/style.js";
4
4
  import { layoutFormula } from "../mathml/layout.js";
5
- import { alignmentOffsetPt, formulaSizePtFromFrame, justifyLineGapsPt, lineNaturalHeightPt, pushCellBorderLines, sumColumnWidthsPt, toStyledRuns } from "./shared.js";
5
+ import { alignmentOffsetPt, formulaSizePtForFrame, justifyLineGapsPt, lineNaturalHeightPt, pushCellBorderLines, registerImage, sumColumnWidthsPt, toStyledRuns } from "./shared.js";
6
6
  import { throwIfAborted } from "../ports/abort.js";
7
7
  import { LAYOUT_FORMAT_VERSION } from "document-schema.js";
8
8
  import { loadMathFont, wrapRunsToWidth } from "pdf-codec";
@@ -51,7 +51,7 @@ function columnLetters(index) {
51
51
  }
52
52
  function resolvePrintRange(sheet, formulas) {
53
53
  if (sheet.printSettings.printRange !== void 0) return sheet.printSettings.printRange;
54
- if (sheet.cells.length === 0 && formulas.length === 0) return;
54
+ if (sheet.cells.length === 0 && formulas.length === 0 && sheet.images.length === 0) return;
55
55
  let startRow = Number.POSITIVE_INFINITY;
56
56
  let startColumn = Number.POSITIVE_INFINITY;
57
57
  let endRow = Number.NEGATIVE_INFINITY;
@@ -68,6 +68,12 @@ function resolvePrintRange(sheet, formulas) {
68
68
  endRow = Math.max(endRow, formula.anchorRow);
69
69
  endColumn = Math.max(endColumn, formula.anchorColumn);
70
70
  }
71
+ for (const image of sheet.images) {
72
+ startRow = Math.min(startRow, image.anchorRow);
73
+ startColumn = Math.min(startColumn, image.anchorColumn);
74
+ endRow = Math.max(endRow, image.anchorRow);
75
+ endColumn = Math.max(endColumn, image.anchorColumn);
76
+ }
71
77
  return {
72
78
  startRow,
73
79
  startColumn,
@@ -365,7 +371,7 @@ function renderAnchoredFormulas(formulas, columnAxis, rowAxis, gridLeftXPt, grid
365
371
  const columnPosition = columnAxis.positionByIndex.get(anchored.anchorColumn);
366
372
  const rowPosition = rowAxis.positionByIndex.get(anchored.anchorRow);
367
373
  if (columnPosition === void 0 || rowPosition === void 0 || hiddenColumnIndices.has(anchored.anchorColumn) || hiddenRowIndices.has(anchored.anchorRow)) continue;
368
- const sizePt = formulaSizePtFromFrame(anchored.frame.heightPt);
374
+ const sizePt = formulaSizePtForFrame(anchored.formula.mathml, anchored.frame);
369
375
  const metrics = loadMathFont().metricsAt(sizePt);
370
376
  const { box } = layoutFormula(anchored.formula.mathml, {
371
377
  metrics,
@@ -387,6 +393,31 @@ function renderAnchoredFormulas(formulas, columnAxis, rowAxis, gridLeftXPt, grid
387
393
  });
388
394
  }
389
395
  }
396
+ function renderAnchoredImages(sheetImages, columnAxis, rowAxis, gridLeftXPt, gridTopYDownPt, pageHeightPt, hiddenColumnIndices, hiddenRowIndices, out, images) {
397
+ for (const image of sheetImages) {
398
+ const columnPosition = columnAxis.positionByIndex.get(image.anchorColumn);
399
+ const rowPosition = rowAxis.positionByIndex.get(image.anchorRow);
400
+ if (columnPosition === void 0 || rowPosition === void 0 || hiddenColumnIndices.has(image.anchorColumn) || hiddenRowIndices.has(image.anchorRow)) continue;
401
+ const imageId = registerImage(image, images);
402
+ const boxYDown = {
403
+ xPt: gridLeftXPt + columnAxis.offsetsPt[columnPosition] + image.offsetXPt,
404
+ yPt: gridTopYDownPt + rowAxis.offsetsPt[rowPosition] + image.offsetYPt,
405
+ widthPt: image.widthPt,
406
+ heightPt: image.heightPt
407
+ };
408
+ const flipped = flipY(boxYDown, pageHeightPt);
409
+ const imageItem = {
410
+ kind: "image",
411
+ imageId,
412
+ xPt: flipped.xPt,
413
+ yPt: flipped.yPt,
414
+ widthPt: image.widthPt,
415
+ heightPt: image.heightPt,
416
+ sourcePath: image.sourcePath
417
+ };
418
+ out.push(imageItem);
419
+ }
420
+ }
390
421
  function bandableIndices(start, end, repeat) {
391
422
  const indices = [];
392
423
  for (let i = start; i <= end; i++) {
@@ -400,7 +431,7 @@ function rangeIndices(start, end) {
400
431
  for (let i = start; i <= end; i++) indices.push(i);
401
432
  return indices;
402
433
  }
403
- function convertSheetToPages(sheet, measurer, signal, out, formulasOut) {
434
+ function convertSheetToPages(sheet, measurer, signal, out, formulasOut, images) {
404
435
  throwIfAborted(signal);
405
436
  const formulas = anchoredFormulas(sheet);
406
437
  const range = resolvePrintRange(sheet, formulas);
@@ -490,6 +521,7 @@ function convertSheetToPages(sheet, measurer, signal, out, formulasOut) {
490
521
  if (printSettings.headers) renderHeaderLabels(gutter, columnAxis, rowAxis, gridLeftXPt, gridTopYDownPt, pageSize.heightPt, measurer, items);
491
522
  items.push(...textItems);
492
523
  renderAnchoredFormulas(formulas, columnAxis, rowAxis, gridLeftXPt, gridTopYDownPt, pageSize.heightPt, out.length, hiddenColumnIndices, hiddenRowIndices, formulasOut);
524
+ renderAnchoredImages(sheet.images, columnAxis, rowAxis, gridLeftXPt, gridTopYDownPt, pageSize.heightPt, hiddenColumnIndices, hiddenRowIndices, items, images);
493
525
  out.push({
494
526
  widthPt: pageSize.widthPt,
495
527
  heightPt: pageSize.heightPt,
@@ -500,13 +532,14 @@ function convertSheetToPages(sheet, measurer, signal, out, formulasOut) {
500
532
  function convertSpreadsheetToLayout(doc, options) {
501
533
  const pages = [];
502
534
  const formulas = [];
503
- for (const sheet of doc.sheets) convertSheetToPages(sheet, options.measurer, options.signal, pages, formulas);
535
+ const images = {};
536
+ for (const sheet of doc.sheets) convertSheetToPages(sheet, options.measurer, options.signal, pages, formulas, images);
504
537
  return {
505
538
  document: {
506
539
  formatVersion: LAYOUT_FORMAT_VERSION,
507
540
  metadata: doc.metadata,
508
541
  pages,
509
- images: {}
542
+ images
510
543
  },
511
544
  formulas
512
545
  };
@@ -116,7 +116,7 @@ function layoutTable(table, contentLeftXDown, contentWidthPt, startYDown, slideH
116
116
  function layoutShapeFormula(block, flippedFrame, formulaContext) {
117
117
  const formula = require_model_formula.formulaOfBlock(block);
118
118
  if (formula === void 0 || formula.mathml.length === 0) return;
119
- const sizePt = require_layout_shared.formulaSizePtFromFrame(block.frame.heightPt);
119
+ const sizePt = require_layout_shared.formulaSizePtForFrame(formula.mathml, block.frame);
120
120
  const metrics = (0, pdf_codec.loadMathFont)().metricsAt(sizePt);
121
121
  const { box } = require_mathml_layout.layoutFormula(formula.mathml, {
122
122
  metrics,
@@ -1,7 +1,7 @@
1
1
  import { flipY } from "../model/geometry.js";
2
2
  import { formulaOfBlock } from "../model/formula.js";
3
3
  import { layoutFormula } from "../mathml/layout.js";
4
- import { alignmentOffsetPt, effectiveStyledRuns, estimateRowHeightPt, formulaSizePtFromFrame, justifyLineGapsPt, lineNaturalHeightPt, registerImage, sumColumnWidthsPt } from "./shared.js";
4
+ import { alignmentOffsetPt, effectiveStyledRuns, estimateRowHeightPt, formulaSizePtForFrame, justifyLineGapsPt, lineNaturalHeightPt, registerImage, sumColumnWidthsPt } from "./shared.js";
5
5
  import { COLOR_BLACK, LAYOUT_FORMAT_VERSION } from "document-schema.js";
6
6
  import { loadMathFont, rotatePointAboutCenter, wrapRunsToWidth } from "pdf-codec";
7
7
  //#region src/layout/slides.ts
@@ -115,7 +115,7 @@ function layoutTable(table, contentLeftXDown, contentWidthPt, startYDown, slideH
115
115
  function layoutShapeFormula(block, flippedFrame, formulaContext) {
116
116
  const formula = formulaOfBlock(block);
117
117
  if (formula === void 0 || formula.mathml.length === 0) return;
118
- const sizePt = formulaSizePtFromFrame(block.frame.heightPt);
118
+ const sizePt = formulaSizePtForFrame(formula.mathml, block.frame);
119
119
  const metrics = loadMathFont().metricsAt(sizePt);
120
120
  const { box } = layoutFormula(formula.mathml, {
121
121
  metrics,
@@ -5,6 +5,7 @@ const require_omml_read = require("../../omml/read.cjs");
5
5
  const require_model_block_splice = require("../../model/block-splice.cjs");
6
6
  const require_ooxml_docx_vector = require("./vector.cjs");
7
7
  const require_ooxml_docx_formula = require("./formula.cjs");
8
+ let ooxml_js = require("ooxml.js");
8
9
  //#region src/ooxml/docx/embedded-objects.ts
9
10
  function isVectorOnlyRun(run, vectors) {
10
11
  const elementChildren = run.children.filter((child) => child.type === "element");
@@ -27,60 +28,159 @@ function isEmbeddedObjectOnlyParagraph(paragraph, equations, vectors) {
27
28
  }
28
29
  return true;
29
30
  }
31
+ function paragraphEmbeddings(block, paragraph, blockIndex, onMathDiagnostic) {
32
+ const equations = require_omml_read.collectOfficeMathElements(paragraph.children);
33
+ const vectors = require_ooxml_docx_vector.collectParagraphVectors(paragraph);
34
+ if (equations.length === 0 && vectors.length === 0) return {
35
+ placements: [],
36
+ consume: false
37
+ };
38
+ const converted = equations.map((equation) => ({
39
+ equation,
40
+ ...require_omml_read.readOfficeMath(equation)
41
+ }));
42
+ const rendered = converted.filter((result) => result.mathml.length > 0);
43
+ if (rendered.length === 0 && vectors.length === 0) {
44
+ for (const result of converted) for (const diagnostic of result.diagnostics) onMathDiagnostic?.(diagnostic, { sourcePath: block.sourcePath });
45
+ return {
46
+ placements: [],
47
+ consume: false
48
+ };
49
+ }
50
+ const insertAt = blockIndex + 1;
51
+ const placements = [];
52
+ for (const result of rendered) placements.push({
53
+ index: insertAt,
54
+ build: (sourcePath) => {
55
+ for (const diagnostic of result.diagnostics) onMathDiagnostic?.(diagnostic, { sourcePath });
56
+ return require_model_formula.buildFormulaBlock({ mathml: result.mathml }, require_ooxml_docx_formula.equationFrame(result.equation), sourcePath);
57
+ }
58
+ });
59
+ for (const result of converted) {
60
+ if (result.mathml.length > 0) continue;
61
+ for (const diagnostic of result.diagnostics) onMathDiagnostic?.(diagnostic, { sourcePath: block.sourcePath });
62
+ }
63
+ if (vectors.length > 0) {
64
+ const vectorValues = vectors.map((detected) => detected.vector);
65
+ placements.push({
66
+ index: insertAt,
67
+ build: (sourcePath) => ({
68
+ ...require_model_embedded_drawing.buildDrawingBlock({
69
+ widthPt: 0,
70
+ heightPt: 0
71
+ }, vectorValues),
72
+ sourcePath
73
+ })
74
+ });
75
+ }
76
+ return {
77
+ placements,
78
+ consume: isEmbeddedObjectOnlyParagraph(paragraph, equations, vectors)
79
+ };
80
+ }
81
+ function spliceContainerBlocks(blocks, containerChildren, pageSize, sourcePathPrefix, onMathDiagnostic) {
82
+ const paragraphElements = [];
83
+ require_ooxml_docx_formula.collectBodyParagraphs(containerChildren, paragraphElements);
84
+ const tableElements = [];
85
+ require_ooxml_docx_formula.collectBodyTables(containerChildren, tableElements);
86
+ let paragraphOrdinal = 0;
87
+ let tableOrdinal = 0;
88
+ let mutated = false;
89
+ const placements = [];
90
+ const consumedIndices = /* @__PURE__ */ new Set();
91
+ const rebuiltBlocks = blocks.map((block, blockIndex) => {
92
+ if (block.kind === "paragraph") {
93
+ const paragraph = paragraphElements[paragraphOrdinal];
94
+ paragraphOrdinal += 1;
95
+ if (paragraph === void 0) return block;
96
+ const { placements: paragraphPlacements, consume } = paragraphEmbeddings(block, paragraph, blockIndex, onMathDiagnostic);
97
+ if (paragraphPlacements.length > 0) placements.push(...paragraphPlacements);
98
+ if (consume) {
99
+ consumedIndices.add(blockIndex);
100
+ mutated = true;
101
+ }
102
+ return block;
103
+ }
104
+ if (block.kind === "table") {
105
+ const tableElement = tableElements[tableOrdinal];
106
+ tableOrdinal += 1;
107
+ if (tableElement === void 0) return block;
108
+ const rebuilt = rebuildTable(block, tableElement, pageSize, `${sourcePathPrefix}[${blockIndex}]`, onMathDiagnostic);
109
+ if (rebuilt !== block) mutated = true;
110
+ return rebuilt;
111
+ }
112
+ return block;
113
+ });
114
+ if (!mutated && placements.length === 0) return [...blocks];
115
+ return require_model_block_splice.spliceBlocks(rebuiltBlocks, placements, consumedIndices, (position) => `${sourcePathPrefix}[${position}]`);
116
+ }
117
+ function rebuildTable(table, tblElement, pageSize, blockPath, onMathDiagnostic) {
118
+ const rowElements = (0, ooxml_js.childrenWithTag)(tblElement, "w:tr");
119
+ let changed = false;
120
+ const rows = table.rows.map((row, rowIndex) => {
121
+ const rowElement = rowElements[rowIndex];
122
+ const cellElements = rowElement === void 0 ? [] : (0, ooxml_js.childrenWithTag)(rowElement, "w:tc");
123
+ const cells = row.cells.map((cell, cellIndex) => {
124
+ const cellElement = cellElements[cellIndex];
125
+ if (cellElement === void 0) return cell;
126
+ const blocks = spliceContainerBlocks(cell.blocks, cellElement.children, pageSize, `${blockPath}.rows[${rowIndex}].cells[${cellIndex}].blocks`, onMathDiagnostic);
127
+ if (blocks === cell.blocks) return cell;
128
+ changed = true;
129
+ return {
130
+ ...cell,
131
+ blocks
132
+ };
133
+ });
134
+ return changed ? {
135
+ ...row,
136
+ cells
137
+ } : row;
138
+ });
139
+ return changed ? {
140
+ ...table,
141
+ rows
142
+ } : table;
143
+ }
30
144
  function spliceDocxEmbeddedObjects(sections, bodyChildren, onMathDiagnostic) {
31
145
  const paragraphElements = [];
32
146
  require_ooxml_docx_formula.collectBodyParagraphs(bodyChildren, paragraphElements);
147
+ const tableElements = [];
148
+ require_ooxml_docx_formula.collectBodyTables(bodyChildren, tableElements);
33
149
  let paragraphOrdinal = 0;
150
+ let tableOrdinal = 0;
34
151
  const out = [];
35
152
  for (const [sectionIndex, section] of sections.entries()) {
36
153
  const placements = [];
37
154
  const consumedIndices = /* @__PURE__ */ new Set();
38
- section.blocks.forEach((block, blockIndex) => {
39
- if (block.kind !== "paragraph") return;
40
- const paragraph = paragraphElements[paragraphOrdinal];
41
- paragraphOrdinal += 1;
42
- if (paragraph === void 0) return;
43
- const equations = require_omml_read.collectOfficeMathElements(paragraph.children);
44
- const vectors = require_ooxml_docx_vector.collectParagraphVectors(paragraph);
45
- if (equations.length === 0 && vectors.length === 0) return;
46
- const converted = equations.map((equation) => ({
47
- equation,
48
- ...require_omml_read.readOfficeMath(equation)
49
- }));
50
- const rendered = converted.filter((result) => result.mathml.length > 0);
51
- if (rendered.length === 0 && vectors.length === 0) {
52
- for (const result of converted) for (const diagnostic of result.diagnostics) onMathDiagnostic?.(diagnostic, { sourcePath: block.sourcePath });
53
- return;
54
- }
55
- const insertAt = blockIndex + 1;
56
- if (isEmbeddedObjectOnlyParagraph(paragraph, equations, vectors)) consumedIndices.add(blockIndex);
57
- for (const result of rendered) placements.push({
58
- index: insertAt,
59
- build: (sourcePath) => {
60
- for (const diagnostic of result.diagnostics) onMathDiagnostic?.(diagnostic, { sourcePath });
61
- return require_model_formula.buildFormulaBlock({ mathml: result.mathml }, require_ooxml_docx_formula.equationFrame(result.equation), sourcePath);
155
+ let mutated = false;
156
+ const rebuiltBlocks = section.blocks.map((block, blockIndex) => {
157
+ if (block.kind === "paragraph") {
158
+ const paragraph = paragraphElements[paragraphOrdinal];
159
+ paragraphOrdinal += 1;
160
+ if (paragraph === void 0) return block;
161
+ const { placements: paragraphPlacements, consume } = paragraphEmbeddings(block, paragraph, blockIndex, onMathDiagnostic);
162
+ if (paragraphPlacements.length > 0) placements.push(...paragraphPlacements);
163
+ if (consume) {
164
+ consumedIndices.add(blockIndex);
165
+ mutated = true;
62
166
  }
63
- });
64
- for (const result of converted) {
65
- if (result.mathml.length > 0) continue;
66
- for (const diagnostic of result.diagnostics) onMathDiagnostic?.(diagnostic, { sourcePath: block.sourcePath });
167
+ return block;
67
168
  }
68
- if (vectors.length > 0) {
69
- const vectorValues = vectors.map((detected) => detected.vector);
70
- placements.push({
71
- index: insertAt,
72
- build: (sourcePath) => ({
73
- ...require_model_embedded_drawing.buildDrawingBlock(section.pageSize, vectorValues),
74
- sourcePath
75
- })
76
- });
169
+ if (block.kind === "table") {
170
+ const tableElement = tableElements[tableOrdinal];
171
+ tableOrdinal += 1;
172
+ if (tableElement === void 0) return block;
173
+ const rebuilt = rebuildTable(block, tableElement, section.pageSize, `sections[${sectionIndex}].blocks`, onMathDiagnostic);
174
+ if (rebuilt !== block) mutated = true;
175
+ return rebuilt;
77
176
  }
177
+ return block;
78
178
  });
79
- if (placements.length === 0 && consumedIndices.size === 0) {
179
+ if (placements.length === 0 && consumedIndices.size === 0 && !mutated) {
80
180
  out.push(section);
81
181
  continue;
82
182
  }
83
- const blocks = require_model_block_splice.spliceBlocks(section.blocks, placements, consumedIndices, (position) => `sections[${sectionIndex}].blocks[${position}]`);
183
+ const blocks = require_model_block_splice.spliceBlocks(rebuiltBlocks, placements, consumedIndices, (position) => `sections[${sectionIndex}].blocks[${position}]`);
84
184
  out.push({
85
185
  ...section,
86
186
  blocks
@@ -1,4 +1,4 @@
1
- import { t as OmmlDiagnosticSink } from "../../formula-DFc2Ofsk.cjs";
1
+ import { t as OmmlDiagnosticSink } from "../../formula-_8feXQH7.cjs";
2
2
  import { ContentSection } from "document-schema.js";
3
3
  import { XmlNode } from "ooxml.js";
4
4
  //#region src/ooxml/docx/embedded-objects.d.ts
@@ -1,4 +1,4 @@
1
- import { t as OmmlDiagnosticSink } from "../../formula-B_4mdCuK.js";
1
+ import { t as OmmlDiagnosticSink } from "../../formula-DDSYieJg.js";
2
2
  import { XmlNode } from "ooxml.js";
3
3
  import { ContentSection } from "document-schema.js";
4
4
  //#region src/ooxml/docx/embedded-objects.d.ts
@@ -3,7 +3,8 @@ import { buildFormulaBlock } from "../../model/formula.js";
3
3
  import { collectOfficeMathElements, readOfficeMath } from "../../omml/read.js";
4
4
  import { spliceBlocks } from "../../model/block-splice.js";
5
5
  import { collectParagraphVectors } from "./vector.js";
6
- import { PARAGRAPH_NON_CONTENT_TAGS, collectBodyParagraphs, equationFrame } from "./formula.js";
6
+ import { PARAGRAPH_NON_CONTENT_TAGS, collectBodyParagraphs, collectBodyTables, equationFrame } from "./formula.js";
7
+ import { childrenWithTag } from "ooxml.js";
7
8
  //#region src/ooxml/docx/embedded-objects.ts
8
9
  function isVectorOnlyRun(run, vectors) {
9
10
  const elementChildren = run.children.filter((child) => child.type === "element");
@@ -26,60 +27,159 @@ function isEmbeddedObjectOnlyParagraph(paragraph, equations, vectors) {
26
27
  }
27
28
  return true;
28
29
  }
30
+ function paragraphEmbeddings(block, paragraph, blockIndex, onMathDiagnostic) {
31
+ const equations = collectOfficeMathElements(paragraph.children);
32
+ const vectors = collectParagraphVectors(paragraph);
33
+ if (equations.length === 0 && vectors.length === 0) return {
34
+ placements: [],
35
+ consume: false
36
+ };
37
+ const converted = equations.map((equation) => ({
38
+ equation,
39
+ ...readOfficeMath(equation)
40
+ }));
41
+ const rendered = converted.filter((result) => result.mathml.length > 0);
42
+ if (rendered.length === 0 && vectors.length === 0) {
43
+ for (const result of converted) for (const diagnostic of result.diagnostics) onMathDiagnostic?.(diagnostic, { sourcePath: block.sourcePath });
44
+ return {
45
+ placements: [],
46
+ consume: false
47
+ };
48
+ }
49
+ const insertAt = blockIndex + 1;
50
+ const placements = [];
51
+ for (const result of rendered) placements.push({
52
+ index: insertAt,
53
+ build: (sourcePath) => {
54
+ for (const diagnostic of result.diagnostics) onMathDiagnostic?.(diagnostic, { sourcePath });
55
+ return buildFormulaBlock({ mathml: result.mathml }, equationFrame(result.equation), sourcePath);
56
+ }
57
+ });
58
+ for (const result of converted) {
59
+ if (result.mathml.length > 0) continue;
60
+ for (const diagnostic of result.diagnostics) onMathDiagnostic?.(diagnostic, { sourcePath: block.sourcePath });
61
+ }
62
+ if (vectors.length > 0) {
63
+ const vectorValues = vectors.map((detected) => detected.vector);
64
+ placements.push({
65
+ index: insertAt,
66
+ build: (sourcePath) => ({
67
+ ...buildDrawingBlock({
68
+ widthPt: 0,
69
+ heightPt: 0
70
+ }, vectorValues),
71
+ sourcePath
72
+ })
73
+ });
74
+ }
75
+ return {
76
+ placements,
77
+ consume: isEmbeddedObjectOnlyParagraph(paragraph, equations, vectors)
78
+ };
79
+ }
80
+ function spliceContainerBlocks(blocks, containerChildren, pageSize, sourcePathPrefix, onMathDiagnostic) {
81
+ const paragraphElements = [];
82
+ collectBodyParagraphs(containerChildren, paragraphElements);
83
+ const tableElements = [];
84
+ collectBodyTables(containerChildren, tableElements);
85
+ let paragraphOrdinal = 0;
86
+ let tableOrdinal = 0;
87
+ let mutated = false;
88
+ const placements = [];
89
+ const consumedIndices = /* @__PURE__ */ new Set();
90
+ const rebuiltBlocks = blocks.map((block, blockIndex) => {
91
+ if (block.kind === "paragraph") {
92
+ const paragraph = paragraphElements[paragraphOrdinal];
93
+ paragraphOrdinal += 1;
94
+ if (paragraph === void 0) return block;
95
+ const { placements: paragraphPlacements, consume } = paragraphEmbeddings(block, paragraph, blockIndex, onMathDiagnostic);
96
+ if (paragraphPlacements.length > 0) placements.push(...paragraphPlacements);
97
+ if (consume) {
98
+ consumedIndices.add(blockIndex);
99
+ mutated = true;
100
+ }
101
+ return block;
102
+ }
103
+ if (block.kind === "table") {
104
+ const tableElement = tableElements[tableOrdinal];
105
+ tableOrdinal += 1;
106
+ if (tableElement === void 0) return block;
107
+ const rebuilt = rebuildTable(block, tableElement, pageSize, `${sourcePathPrefix}[${blockIndex}]`, onMathDiagnostic);
108
+ if (rebuilt !== block) mutated = true;
109
+ return rebuilt;
110
+ }
111
+ return block;
112
+ });
113
+ if (!mutated && placements.length === 0) return [...blocks];
114
+ return spliceBlocks(rebuiltBlocks, placements, consumedIndices, (position) => `${sourcePathPrefix}[${position}]`);
115
+ }
116
+ function rebuildTable(table, tblElement, pageSize, blockPath, onMathDiagnostic) {
117
+ const rowElements = childrenWithTag(tblElement, "w:tr");
118
+ let changed = false;
119
+ const rows = table.rows.map((row, rowIndex) => {
120
+ const rowElement = rowElements[rowIndex];
121
+ const cellElements = rowElement === void 0 ? [] : childrenWithTag(rowElement, "w:tc");
122
+ const cells = row.cells.map((cell, cellIndex) => {
123
+ const cellElement = cellElements[cellIndex];
124
+ if (cellElement === void 0) return cell;
125
+ const blocks = spliceContainerBlocks(cell.blocks, cellElement.children, pageSize, `${blockPath}.rows[${rowIndex}].cells[${cellIndex}].blocks`, onMathDiagnostic);
126
+ if (blocks === cell.blocks) return cell;
127
+ changed = true;
128
+ return {
129
+ ...cell,
130
+ blocks
131
+ };
132
+ });
133
+ return changed ? {
134
+ ...row,
135
+ cells
136
+ } : row;
137
+ });
138
+ return changed ? {
139
+ ...table,
140
+ rows
141
+ } : table;
142
+ }
29
143
  function spliceDocxEmbeddedObjects(sections, bodyChildren, onMathDiagnostic) {
30
144
  const paragraphElements = [];
31
145
  collectBodyParagraphs(bodyChildren, paragraphElements);
146
+ const tableElements = [];
147
+ collectBodyTables(bodyChildren, tableElements);
32
148
  let paragraphOrdinal = 0;
149
+ let tableOrdinal = 0;
33
150
  const out = [];
34
151
  for (const [sectionIndex, section] of sections.entries()) {
35
152
  const placements = [];
36
153
  const consumedIndices = /* @__PURE__ */ new Set();
37
- section.blocks.forEach((block, blockIndex) => {
38
- if (block.kind !== "paragraph") return;
39
- const paragraph = paragraphElements[paragraphOrdinal];
40
- paragraphOrdinal += 1;
41
- if (paragraph === void 0) return;
42
- const equations = collectOfficeMathElements(paragraph.children);
43
- const vectors = collectParagraphVectors(paragraph);
44
- if (equations.length === 0 && vectors.length === 0) return;
45
- const converted = equations.map((equation) => ({
46
- equation,
47
- ...readOfficeMath(equation)
48
- }));
49
- const rendered = converted.filter((result) => result.mathml.length > 0);
50
- if (rendered.length === 0 && vectors.length === 0) {
51
- for (const result of converted) for (const diagnostic of result.diagnostics) onMathDiagnostic?.(diagnostic, { sourcePath: block.sourcePath });
52
- return;
53
- }
54
- const insertAt = blockIndex + 1;
55
- if (isEmbeddedObjectOnlyParagraph(paragraph, equations, vectors)) consumedIndices.add(blockIndex);
56
- for (const result of rendered) placements.push({
57
- index: insertAt,
58
- build: (sourcePath) => {
59
- for (const diagnostic of result.diagnostics) onMathDiagnostic?.(diagnostic, { sourcePath });
60
- return buildFormulaBlock({ mathml: result.mathml }, equationFrame(result.equation), sourcePath);
154
+ let mutated = false;
155
+ const rebuiltBlocks = section.blocks.map((block, blockIndex) => {
156
+ if (block.kind === "paragraph") {
157
+ const paragraph = paragraphElements[paragraphOrdinal];
158
+ paragraphOrdinal += 1;
159
+ if (paragraph === void 0) return block;
160
+ const { placements: paragraphPlacements, consume } = paragraphEmbeddings(block, paragraph, blockIndex, onMathDiagnostic);
161
+ if (paragraphPlacements.length > 0) placements.push(...paragraphPlacements);
162
+ if (consume) {
163
+ consumedIndices.add(blockIndex);
164
+ mutated = true;
61
165
  }
62
- });
63
- for (const result of converted) {
64
- if (result.mathml.length > 0) continue;
65
- for (const diagnostic of result.diagnostics) onMathDiagnostic?.(diagnostic, { sourcePath: block.sourcePath });
166
+ return block;
66
167
  }
67
- if (vectors.length > 0) {
68
- const vectorValues = vectors.map((detected) => detected.vector);
69
- placements.push({
70
- index: insertAt,
71
- build: (sourcePath) => ({
72
- ...buildDrawingBlock(section.pageSize, vectorValues),
73
- sourcePath
74
- })
75
- });
168
+ if (block.kind === "table") {
169
+ const tableElement = tableElements[tableOrdinal];
170
+ tableOrdinal += 1;
171
+ if (tableElement === void 0) return block;
172
+ const rebuilt = rebuildTable(block, tableElement, section.pageSize, `sections[${sectionIndex}].blocks`, onMathDiagnostic);
173
+ if (rebuilt !== block) mutated = true;
174
+ return rebuilt;
76
175
  }
176
+ return block;
77
177
  });
78
- if (placements.length === 0 && consumedIndices.size === 0) {
178
+ if (placements.length === 0 && consumedIndices.size === 0 && !mutated) {
79
179
  out.push(section);
80
180
  continue;
81
181
  }
82
- const blocks = spliceBlocks(section.blocks, placements, consumedIndices, (position) => `sections[${sectionIndex}].blocks[${position}]`);
182
+ const blocks = spliceBlocks(rebuiltBlocks, placements, consumedIndices, (position) => `sections[${sectionIndex}].blocks[${position}]`);
83
183
  out.push({
84
184
  ...section,
85
185
  blocks