@evotecit/officeimo 0.1.1 → 0.1.3

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 (43) hide show
  1. package/README.md +43 -49
  2. package/bundles/officeimo-canopyx.js +1047 -193
  3. package/bundles/officeimo-canopyx.mjs +958 -111
  4. package/bundles/officeimo-csv.js +18 -15
  5. package/bundles/officeimo-csv.mjs +16 -13
  6. package/bundles/officeimo-datatables.js +249 -153
  7. package/bundles/officeimo-datatables.mjs +169 -73
  8. package/bundles/officeimo-pdf.js +151 -67
  9. package/bundles/officeimo-pdf.mjs +127 -43
  10. package/bundles/officeimo-xlsx.js +78 -69
  11. package/bundles/officeimo-xlsx.mjs +26 -17
  12. package/bundles/officeimo.js +249 -153
  13. package/bundles/officeimo.mjs +169 -73
  14. package/dist/csv/index.js +16 -13
  15. package/dist/integrations/canopyx/diagnostics.d.ts +3 -0
  16. package/dist/integrations/canopyx/diagnostics.js +17 -0
  17. package/dist/integrations/canopyx/grouping.d.ts +6 -0
  18. package/dist/integrations/canopyx/grouping.js +188 -0
  19. package/dist/integrations/canopyx/index.d.ts +2 -2
  20. package/dist/integrations/canopyx/index.js +72 -15
  21. package/dist/integrations/canopyx/metadata.d.ts +4 -0
  22. package/dist/integrations/canopyx/metadata.js +51 -0
  23. package/dist/integrations/canopyx/presentation.d.ts +8 -0
  24. package/dist/integrations/canopyx/presentation.js +40 -0
  25. package/dist/integrations/canopyx/rule-predicates.d.ts +3 -0
  26. package/dist/integrations/canopyx/rule-predicates.js +110 -0
  27. package/dist/integrations/canopyx/rules.d.ts +6 -0
  28. package/dist/integrations/canopyx/rules.js +176 -0
  29. package/dist/integrations/canopyx/source.js +24 -22
  30. package/dist/integrations/canopyx/types.d.ts +89 -8
  31. package/dist/integrations/canopyx/workbook.d.ts +6 -0
  32. package/dist/integrations/canopyx/workbook.js +52 -0
  33. package/dist/opc/index.js +14 -9
  34. package/dist/pdf/font-resources.js +33 -14
  35. package/dist/pdf/pages.js +63 -16
  36. package/dist/pdf/table-band-flow.js +7 -2
  37. package/dist/pdf/table-band-plan.js +9 -1
  38. package/dist/pdf/table-layout.js +6 -2
  39. package/dist/pdf/table-structure.js +9 -8
  40. package/dist/xlsx/workbook.js +7 -4
  41. package/dist/xlsx/worksheet.js +2 -1
  42. package/dist/xml/index.js +3 -3
  43. package/package.json +1 -1
package/dist/pdf/pages.js CHANGED
@@ -21,7 +21,8 @@ export class PdfPages {
21
21
  outlines;
22
22
  references = [];
23
23
  current = 0;
24
- commands = [];
24
+ commands = "";
25
+ commandParts = [];
25
26
  commandBytes = 0;
26
27
  annotations = [];
27
28
  hyperlinks = 0;
@@ -39,6 +40,12 @@ export class PdfPages {
39
40
  pendingBookmarks = [];
40
41
  pageNumberWidth = 0;
41
42
  colors = new Map();
43
+ textFont;
44
+ textSize = 0;
45
+ textColor = "";
46
+ textPrefix = "";
47
+ rectangleLefts;
48
+ rectangleColumns = [];
42
49
  externalObjects = new Map();
43
50
  bandFlow;
44
51
  bandCellBudget = false;
@@ -105,7 +112,7 @@ export class PdfPages {
105
112
  }
106
113
  add(command) {
107
114
  this.retainPageBytes(command.length);
108
- this.commands.push(command);
115
+ this.commands += command;
109
116
  }
110
117
  retainPageBytes(length) {
111
118
  this.commandBytes += length;
@@ -125,9 +132,17 @@ export class PdfPages {
125
132
  return this.structure ? "/Artifact << /Type /" + type + " >> BDC\n" + command + "EMC\n" : command;
126
133
  }
127
134
  text(text, font, size, x, baseline, foreground = "111827", element) {
128
- const n = pdfNumber, shear = font.syntheticItalic ? .213 : 0;
129
- const command = "q " + this.rgb(foreground) + " rg " + this.rgb(foreground) + " RG\nBT /" + font.name + " " + n(size) + " Tf " +
130
- (font.syntheticBold ? n(size * .025) + " w 2 Tr " : "0 Tr ") + "1 0 " + n(shear) + " 1 " + n(x) + " " + n(baseline) + " Tm " + font.encode(text) + " Tj ET Q\n";
135
+ const n = pdfNumber;
136
+ if (font !== this.textFont || size !== this.textSize || foreground !== this.textColor) {
137
+ const shear = font.syntheticItalic ? .213 : 0, rgb = this.rgb(foreground);
138
+ // Keep one prefix, independent of report size. Resource identity includes synthetic styling.
139
+ this.textPrefix = "q " + rgb + " rg " + rgb + " RG\nBT /" + font.name + " " + n(size) + " Tf " +
140
+ (font.syntheticBold ? n(size * .025) + " w 2 Tr " : "0 Tr ") + "1 0 " + n(shear) + " 1 ";
141
+ this.textFont = font;
142
+ this.textSize = size;
143
+ this.textColor = foreground;
144
+ }
145
+ const command = this.textPrefix + n(x) + " " + n(baseline) + " Tm " + font.encode(text) + " Tj ET Q\n";
131
146
  this.add(this.structure && element !== undefined ? this.structure.mark(element) + command + "EMC\n" : this.artifact(command, "Pagination"));
132
147
  }
133
148
  link(link, x, bottom, width, height, element) {
@@ -157,8 +172,8 @@ export class PdfPages {
157
172
  for (const segment of line.segments) {
158
173
  const registered = marks?.get(segment), owner = registered?.owner ?? (segment.link && this.structure && element !== undefined ? this.structure.link(element, segment.link) : element);
159
174
  if (segment.glyphs) {
160
- const font = segment.font, n = pdfNumber, shear = font.syntheticItalic ? .213 : 0;
161
- let cursor = x, command = "q " + this.rgb(segment.color ?? "111827") + " rg " + this.rgb(segment.color ?? "111827") + " RG\nBT /" + font.name + " " + n(size) + " Tf " +
175
+ const font = segment.font, n = pdfNumber, shear = font.syntheticItalic ? .213 : 0, rgb = this.rgb(segment.color ?? "111827");
176
+ let cursor = x, command = "q " + rgb + " rg " + rgb + " RG\nBT /" + font.name + " " + n(size) + " Tf " +
162
177
  (font.syntheticBold ? n(size * .025) + " w 2 Tr " : "0 Tr ");
163
178
  for (const glyph of segment.glyphs) {
164
179
  command += "1 0 " + n(shear) + " 1 " + n(cursor + (glyph.xOffset + shear * glyph.yOffset) * size / 1000) + " " + n(baseline + glyph.yOffset * size / 1000) + " Tm " + font.encodeGlyph(glyph.id, glyph.logicalText) + " Tj\n";
@@ -206,7 +221,8 @@ export class PdfPages {
206
221
  this.current = this.objects.reserve();
207
222
  this.references.push(this.current);
208
223
  }
209
- this.commands = [];
224
+ this.commands = "";
225
+ this.commandParts = [];
210
226
  this.annotations = [];
211
227
  this.commandBytes = 0;
212
228
  this.bodyAdded = false;
@@ -271,7 +287,7 @@ export class PdfPages {
271
287
  // Charge physical repeats without recapturing callbacks or making them semantic duplicates.
272
288
  for (const cell of header.cells)
273
289
  if (!this.bandCellBudget && this.paintedHeaderBudgetCells.has(cell.source ?? cell))
274
- this.settings.budget.cell(cell.lines.map(line => line.text).join(""));
290
+ this.settings.budget.cell(this.settings.budget.limits.maxTextCharacters === undefined ? "" : cell.lines.map(line => line.text).join(""));
275
291
  const elements = this.tableStructure ? header.cells.map(cell => this.tableStructure.owner(cell.source ?? cell)) : undefined;
276
292
  const repeated = header.cells.map(cell => this.paintedHeaderCells.has(cell.source ?? cell));
277
293
  this.draw(header, 0, header.lines, elements, repeated);
@@ -289,14 +305,32 @@ export class PdfPages {
289
305
  if (!this.layout)
290
306
  throw new Error("A table layout is required before drawing rows.");
291
307
  this.bodyAdded = true;
292
- const { padding, fontSize } = this.settings, n = pdfNumber, height = lineCount * row.lineHeight + padding * 2;
308
+ const { padding, fontSize } = this.settings, n = pdfNumber, height = lineCount * row.lineHeight + padding * 2, lefts = this.layout.lefts;
309
+ // One geometry token per active physical column; table/band changes release the old tokens.
310
+ if (this.rectangleLefts !== lefts) {
311
+ this.rectangleLefts = lefts;
312
+ this.rectangleColumns = [];
313
+ }
314
+ let rectangleHeight, rectangleBottom = "", rectangleHeightText = "";
293
315
  for (const [index, cell] of row.cells.entries()) {
294
316
  if (this.bandCellBudget)
295
- this.settings.budget.cell((cell.height === undefined ? cell.lines.slice(firstLine, firstLine + lineCount) : cell.lines).map(line => line.text).join(""));
296
- const x = this.layout.lefts[cell.first], cellHeight = cell.height ?? height;
317
+ this.settings.budget.cell(this.settings.budget.limits.maxTextCharacters === undefined ? "" :
318
+ (cell.height === undefined ? cell.lines.slice(firstLine, firstLine + lineCount) : cell.lines).map(line => line.text).join(""));
319
+ const x = lefts[cell.first], cellHeight = cell.height ?? height;
320
+ if (cellHeight !== rectangleHeight) {
321
+ rectangleBottom = n(this.y - cellHeight);
322
+ rectangleHeightText = n(cellHeight);
323
+ rectangleHeight = cellHeight;
324
+ }
325
+ let horizontal = this.rectangleColumns[cell.first];
326
+ if (!horizontal || horizontal.x !== x || horizontal.width !== cell.width) {
327
+ horizontal = { x, width: cell.width, left: n(x), widthText: n(cell.width) };
328
+ this.rectangleColumns[cell.first] = horizontal;
329
+ }
330
+ const rectangle = horizontal.left + " " + rectangleBottom + " " + horizontal.widthText + " " + rectangleHeightText;
297
331
  if (cell.style.background)
298
- this.add(this.artifact("q " + this.rgb(cell.style.background) + " rg " + n(x) + " " + n(this.y - cellHeight) + " " + n(cell.width) + " " + n(cellHeight) + " re f Q\n"));
299
- this.add(this.artifact("q 0.82 0.85 0.89 RG 0.4 w " + n(x) + " " + n(this.y - cellHeight) + " " + n(cell.width) + " " + n(cellHeight) + " re S Q\n"));
332
+ this.add(this.artifact("q " + this.rgb(cell.style.background) + " rg " + rectangle + " re f Q\n"));
333
+ this.add(this.artifact("q 0.82 0.85 0.89 RG 0.4 w " + rectangle + " re S Q\n"));
300
334
  const element = elements?.[index], artifact = cell.semanticArtifact || repeated[index], owner = cell.link && this.structure && element !== undefined ? this.structure.link(element, cell.link) : element;
301
335
  for (let line = firstLine; line < Math.min(cell.lines.length, cell.height ? cell.lines.length : firstLine + lineCount); line++) {
302
336
  const text = cell.lines[line], left = cell.style.alignment === "right" ? cell.width - padding - text.width : cell.style.alignment === "center" ? (cell.width - text.width) / 2 : padding;
@@ -306,6 +340,7 @@ export class PdfPages {
306
340
  this.link(cell.link, x, this.y - cellHeight, cell.width, cellHeight, owner);
307
341
  }
308
342
  this.y -= row.structured ? row.height : height;
343
+ this.sealCommands();
309
344
  }
310
345
  async block(rows) {
311
346
  if (this.bandFlow)
@@ -631,9 +666,22 @@ export class PdfPages {
631
666
  marker: { line: item.marker, gap: item.markerGap, ...(label === undefined ? {} : { owner: label }) } });
632
667
  await this.structure?.complete(li);
633
668
  }
669
+ sealCommands() {
670
+ if (this.commands) {
671
+ this.commandParts.push(this.commands);
672
+ this.commands = "";
673
+ }
674
+ }
675
+ takeCommands() {
676
+ this.sealCommands();
677
+ const content = this.commandParts.join("");
678
+ // Encoding and awaited delivery own the page value; the next page starts independently.
679
+ this.commandParts = [];
680
+ return content;
681
+ }
634
682
  async finishPage() {
635
683
  const contents = this.objects.reserve(), { page, options } = this.settings;
636
- await this.objects.stream(contents, this.commands.join(""), "", options.compression !== false);
684
+ await this.objects.stream(contents, this.takeCommands(), "", options.compression !== false);
637
685
  const annotations = [];
638
686
  for (const annotation of this.annotations) {
639
687
  annotations.push(annotation.id);
@@ -645,7 +693,6 @@ export class PdfPages {
645
693
  (annotations.length ? " /Annots [" + annotations.map(id => id + " 0 R").join(" ") + "]" : "") + " >>");
646
694
  if (this.structure)
647
695
  await this.structure.finishPage();
648
- this.commands = [];
649
696
  this.annotations = [];
650
697
  this.commandBytes = 0;
651
698
  this.current = 0;
@@ -39,7 +39,7 @@ export class PdfTableBandFlow {
39
39
  await this.flush();
40
40
  }
41
41
  async row(row, allowFirstFragment = false) {
42
- const { padding, fontSize, options } = this.layout.settings, minimumLines = this.layout.minimumRowLines(row), minimumHeight = this.layout.minimumRowHeight(row);
42
+ const { padding, fontSize, options } = this.layout.settings, minimumLines = this.layout.minimumRowLines(row), minimumHeight = minimumLines * row.lineHeight + padding * 2;
43
43
  if (minimumHeight > this.painter.firstFullCapacity + .001 || minimumHeight > this.painter.continuationCapacity + .001)
44
44
  throw new RangeError("Repeated PDF identifiers must fit intact below the first and continued band headings/context.");
45
45
  // Keep ordinary rows intact when they fit fresh. A kept heading/caption can deliberately
@@ -73,9 +73,14 @@ export class PdfTableBandFlow {
73
73
  let remaining = false;
74
74
  const cells = rows[0].cells.map(cell => {
75
75
  const source = this.layout.bands[index].columns[cell.first];
76
- const repeated = this.layout.repeatedColumn(source), visible = repeated ? cell.lines : cell.lines.slice(first, first + lines);
76
+ const repeated = this.layout.repeatedColumn(source), reusable = cell.span === 1 && (cell.source?.span ?? 1) === 1 && (cell.rowSpan ?? 1) === 1 &&
77
+ cell.height === undefined && first === 0 && (repeated || lines >= cell.lines.length);
78
+ const visible = repeated || reusable ? cell.lines : cell.lines.slice(first, first + lines);
77
79
  if (!repeated && (visible.length > 0 || cell.link !== undefined))
78
80
  remaining = true;
81
+ // A complete first fragment already owns these lines and their maximum font metrics.
82
+ if (reusable)
83
+ return cell;
79
84
  const { height: _height, ...plain } = cell;
80
85
  return { ...plain, lines: visible,
81
86
  lineHeight: visible.reduce((value, line) => Math.max(value, line.lineHeight), cell.font.lineHeight(fontSize)),
@@ -43,6 +43,14 @@ export async function projectBandRows(rows, band, widths, settings, fonts) {
43
43
  for (const row of rows) {
44
44
  const cells = [];
45
45
  for (const cell of row.cells) {
46
+ // Ordinary data and leaf headings keep their captured width and lines in every band.
47
+ // Look up their physical position without allocating another per-row column index.
48
+ if (cell.span === 1) {
49
+ const first = band.columns.indexOf(cell.first);
50
+ if (first >= 0)
51
+ cells.push({ ...cell, source: cell.source ?? cell, first });
52
+ continue;
53
+ }
46
54
  for (let first = 0; first < band.columns.length;) {
47
55
  const source = band.columns[first];
48
56
  if (source < cell.first || source >= cell.first + cell.span) {
@@ -53,7 +61,7 @@ export async function projectBandRows(rows, band, widths, settings, fonts) {
53
61
  while (end < band.columns.length && band.columns[end] === band.columns[end - 1] + 1 && band.columns[end] < cell.first + cell.span)
54
62
  end++;
55
63
  const width = band.columns.slice(first, end).reduce((sum, index) => sum + widths[index], 0);
56
- const lines = cell.span === 1 ? cell.lines : await wrapPdfText(cell.captured, fonts, settings.fontSize, width - settings.padding * 2, settings, cell.style.wrapText !== false);
64
+ const lines = await wrapPdfText(cell.captured, fonts, settings.fontSize, width - settings.padding * 2, settings, cell.style.wrapText !== false);
57
65
  cells.push({ ...cell, source: cell.source ?? cell, first, span: end - first, width, lines,
58
66
  lineHeight: lines.reduce((height, line) => Math.max(height, line.lineHeight), cell.font.lineHeight(settings.fontSize)) });
59
67
  first = end;
@@ -109,8 +109,12 @@ export class PdfTableLayout {
109
109
  lines };
110
110
  }
111
111
  row(cells) {
112
- const lines = Math.max(1, ...cells.map(c => c.lines.length));
113
- const lineHeight = Math.max(this.lineHeight, ...cells.map(c => c.lineHeight));
112
+ let lines = 1, lineHeight = this.lineHeight;
113
+ for (let index = 0; index < cells.length; index++) {
114
+ const cell = cells[index];
115
+ lines = Math.max(lines, cell.lines.length);
116
+ lineHeight = Math.max(lineHeight, cell.lineHeight);
117
+ }
114
118
  return { cells, lines, lineHeight, height: lines * lineHeight + this.settings.padding * 2 };
115
119
  }
116
120
  async headers() {
@@ -49,30 +49,31 @@ export class PdfTableStructure {
49
49
  return row.cells.map(cell => this.cells.get(cell));
50
50
  const rowId = this.structure.element("TR", parent);
51
51
  this.rows.set(row, rowId);
52
- const headers = row.cells.map((cell, index) => rowHeaderIndex === undefined ? cell.rowHeader === true : index === rowHeaderIndex);
53
52
  // Allocate in source order, then resolve every row header before associating even earlier data cells.
54
53
  // Generated summary labels override detail roles so numeric subtotal cells never become row headers.
55
54
  const ids = row.cells.map((cell, index) => {
56
- const id = this.structure.element(headers[index] ? "TH" : "TD", rowId);
55
+ const header = rowHeaderIndex === undefined ? cell.rowHeader === true : index === rowHeaderIndex;
56
+ const id = this.structure.element(header ? "TH" : "TD", rowId);
57
57
  this.cells.set(cell, id);
58
58
  return id;
59
59
  });
60
- const rowHeaders = [];
60
+ let rowHeaders;
61
61
  for (let index = 0; index < ids.length; index++)
62
- if (headers[index])
63
- rowHeaders.push(await this.structure.identifier(ids[index]));
62
+ if (rowHeaderIndex === undefined ? row.cells[index].rowHeader === true : index === rowHeaderIndex)
63
+ (rowHeaders ??= []).push(await this.structure.identifier(ids[index]));
64
64
  for (let index = 0; index < row.cells.length; index++) {
65
65
  const cell = row.cells[index];
66
- if (!rowHeaders.length && cell.span === 1 && (cell.rowSpan ?? 1) === 1) {
66
+ if (!rowHeaders && cell.span === 1 && (cell.rowSpan ?? 1) === 1) {
67
67
  const attributes = this.ordinaryAttributes[cell.first] ??=
68
68
  await this.structure.attributeReference(this.attributeDictionary(cell, this.associations(cell)));
69
69
  this.structure.setAttributes(ids[index], attributes);
70
70
  continue;
71
71
  }
72
72
  const associations = this.associations(cell);
73
- if (!headers[index])
73
+ const header = rowHeaderIndex === undefined ? cell.rowHeader === true : index === rowHeaderIndex;
74
+ if (!header && rowHeaders)
74
75
  associations.push(...rowHeaders);
75
- this.structure.setAttributes(ids[index], this.attributes(cell, associations, headers[index] ? "Row" : undefined));
76
+ this.structure.setAttributes(ids[index], this.attributes(cell, associations, header ? "Row" : undefined));
76
77
  }
77
78
  return ids;
78
79
  }
@@ -315,12 +315,15 @@ export class Workbook {
315
315
  });
316
316
  }
317
317
  addPart(part) {
318
+ this.assertOpen();
319
+ const { uri, contentType, data, relationship } = part;
320
+ const capturedRelationship = relationship && { id: relationship.id, type: relationship.type, source: relationship.source };
318
321
  this.assertOpen();
319
322
  // Reserve now so collisions and invalid names fail at the call site.
320
- this.package.addPart(part);
321
- if (part.relationship) {
322
- const { source, ...rel } = part.relationship;
323
- this.package.addRelationship(source ?? "/xl/workbook.xml", { ...rel, target: part.uri });
323
+ this.package.addPart({ uri, contentType, data });
324
+ if (capturedRelationship) {
325
+ const { source, id, type } = capturedRelationship;
326
+ this.package.addRelationship(source ?? "/xl/workbook.xml", { id, type, target: uri });
324
327
  }
325
328
  }
326
329
  /** Complete the archive and return counts. Does not close or dispose a caller-owned sink. */
@@ -257,7 +257,8 @@ export class Worksheet {
257
257
  });
258
258
  const originalCharacters = typeof value === "string" ? value.length : 0;
259
259
  let reservedCharacters = this.preserved || header || footer ? originalCharacters : undefined;
260
- if (type === "string" && cleanXml(value, this.book.settings.invalidCharacterPolicy).length > 32767) {
260
+ // Cleaning removes characters; ordinary strings cannot cross Excel's limit.
261
+ if (type === "string" && originalCharacters > 32767 && cleanXml(value, this.book.settings.invalidCharacterPolicy).length > 32767) {
261
262
  const cell = col.letter + row;
262
263
  if (link || this.linkedCells.has(cell))
263
264
  throw new TypeError("A text-preservation cell cannot also have an external hyperlink.");
package/dist/xml/index.js CHANGED
@@ -1,6 +1,8 @@
1
1
  import { OfficeIMOError } from "../core/errors.js";
2
2
  import { ChunkedTextSink } from "../core/sinks.js";
3
3
  export const xmlDeclaration = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
4
+ const xmlEscapes = { "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&apos;",
5
+ "\r": "&#13;", "\n": "&#10;", "\t": "&#9;" };
4
6
  /** XML 1.0 characters; Unicode mode retains valid surrogate pairs. */
5
7
  export function cleanXml(value, policy = "strip") {
6
8
  if (policy !== "strip" && policy !== "reject")
@@ -12,9 +14,7 @@ export function cleanXml(value, policy = "strip") {
12
14
  }
13
15
  /** Escape data, including whitespace that XML parsers otherwise normalize. */
14
16
  export function escapeXml(value, policy = "strip") {
15
- const escapes = { "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&apos;",
16
- "\r": "&#13;", "\n": "&#10;", "\t": "&#9;" };
17
- return cleanXml(value, policy).replace(/[&<>"'\r\n\t]/g, c => escapes[c]);
17
+ return cleanXml(value, policy).replace(/[&<>"'\r\n\t]/g, c => xmlEscapes[c]);
18
18
  }
19
19
  /** Encode an OOXML ST_Xstring attribute without interpreting literal escape tokens as characters. */
20
20
  export function escapeOoxmlAttribute(value, policy = "strip") {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@evotecit/officeimo",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "Dependency-free OfficeIMO document libraries for browsers, workers and Node.",
5
5
  "type": "module",
6
6
  "license": "MIT",