@evotecit/officeimo 0.1.2 → 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 (37) hide show
  1. package/README.md +43 -49
  2. package/bundles/officeimo-canopyx.js +966 -122
  3. package/bundles/officeimo-canopyx.mjs +929 -92
  4. package/bundles/officeimo-csv.js +18 -15
  5. package/bundles/officeimo-csv.mjs +16 -13
  6. package/bundles/officeimo-datatables.js +168 -82
  7. package/bundles/officeimo-datatables.mjs +140 -54
  8. package/bundles/officeimo-pdf.js +148 -65
  9. package/bundles/officeimo-pdf.mjs +124 -41
  10. package/bundles/officeimo.js +168 -82
  11. package/bundles/officeimo.mjs +140 -54
  12. package/dist/csv/index.js +16 -13
  13. package/dist/integrations/canopyx/diagnostics.d.ts +3 -0
  14. package/dist/integrations/canopyx/diagnostics.js +17 -0
  15. package/dist/integrations/canopyx/grouping.d.ts +6 -0
  16. package/dist/integrations/canopyx/grouping.js +188 -0
  17. package/dist/integrations/canopyx/index.d.ts +2 -2
  18. package/dist/integrations/canopyx/index.js +72 -15
  19. package/dist/integrations/canopyx/metadata.d.ts +4 -0
  20. package/dist/integrations/canopyx/metadata.js +51 -0
  21. package/dist/integrations/canopyx/presentation.d.ts +8 -0
  22. package/dist/integrations/canopyx/presentation.js +40 -0
  23. package/dist/integrations/canopyx/rule-predicates.d.ts +3 -0
  24. package/dist/integrations/canopyx/rule-predicates.js +110 -0
  25. package/dist/integrations/canopyx/rules.d.ts +6 -0
  26. package/dist/integrations/canopyx/rules.js +176 -0
  27. package/dist/integrations/canopyx/source.js +24 -22
  28. package/dist/integrations/canopyx/types.d.ts +89 -8
  29. package/dist/integrations/canopyx/workbook.d.ts +6 -0
  30. package/dist/integrations/canopyx/workbook.js +52 -0
  31. package/dist/pdf/font-resources.js +33 -14
  32. package/dist/pdf/pages.js +60 -14
  33. package/dist/pdf/table-band-flow.js +7 -2
  34. package/dist/pdf/table-band-plan.js +9 -1
  35. package/dist/pdf/table-layout.js +6 -2
  36. package/dist/pdf/table-structure.js +9 -8
  37. 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;
@@ -289,15 +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
317
  this.settings.budget.cell(this.settings.budget.limits.maxTextCharacters === undefined ? "" :
296
318
  (cell.height === undefined ? cell.lines.slice(firstLine, firstLine + lineCount) : cell.lines).map(line => line.text).join(""));
297
- const x = this.layout.lefts[cell.first], cellHeight = cell.height ?? height;
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;
298
331
  if (cell.style.background)
299
- 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"));
300
- 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"));
301
334
  const element = elements?.[index], artifact = cell.semanticArtifact || repeated[index], owner = cell.link && this.structure && element !== undefined ? this.structure.link(element, cell.link) : element;
302
335
  for (let line = firstLine; line < Math.min(cell.lines.length, cell.height ? cell.lines.length : firstLine + lineCount); line++) {
303
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;
@@ -307,6 +340,7 @@ export class PdfPages {
307
340
  this.link(cell.link, x, this.y - cellHeight, cell.width, cellHeight, owner);
308
341
  }
309
342
  this.y -= row.structured ? row.height : height;
343
+ this.sealCommands();
310
344
  }
311
345
  async block(rows) {
312
346
  if (this.bandFlow)
@@ -632,9 +666,22 @@ export class PdfPages {
632
666
  marker: { line: item.marker, gap: item.markerGap, ...(label === undefined ? {} : { owner: label }) } });
633
667
  await this.structure?.complete(li);
634
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
+ }
635
682
  async finishPage() {
636
683
  const contents = this.objects.reserve(), { page, options } = this.settings;
637
- await this.objects.stream(contents, this.commands.join(""), "", options.compression !== false);
684
+ await this.objects.stream(contents, this.takeCommands(), "", options.compression !== false);
638
685
  const annotations = [];
639
686
  for (const annotation of this.annotations) {
640
687
  annotations.push(annotation.id);
@@ -646,7 +693,6 @@ export class PdfPages {
646
693
  (annotations.length ? " /Annots [" + annotations.map(id => id + " 0 R").join(" ") + "]" : "") + " >>");
647
694
  if (this.structure)
648
695
  await this.structure.finishPage();
649
- this.commands = [];
650
696
  this.annotations = [];
651
697
  this.commandBytes = 0;
652
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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@evotecit/officeimo",
3
- "version": "0.1.2",
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",