@evotecit/officeimo 0.0.0-stage → 0.1.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 (259) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1219 -2
  3. package/bundles/officeimo-canopyx.js +11286 -0
  4. package/bundles/officeimo-canopyx.mjs +11185 -0
  5. package/bundles/officeimo-csv.js +1254 -0
  6. package/bundles/officeimo-csv.mjs +1229 -0
  7. package/bundles/officeimo-datatables.js +11572 -0
  8. package/bundles/officeimo-datatables.mjs +11470 -0
  9. package/bundles/officeimo-execution.js +1503 -0
  10. package/bundles/officeimo-execution.mjs +1479 -0
  11. package/bundles/officeimo-pdf.js +6564 -0
  12. package/bundles/officeimo-pdf.mjs +6502 -0
  13. package/bundles/officeimo-xlsx.js +5333 -0
  14. package/bundles/officeimo-xlsx.mjs +5279 -0
  15. package/bundles/officeimo.js +11022 -0
  16. package/bundles/officeimo.mjs +10924 -0
  17. package/dist/core/dates.d.ts +1 -0
  18. package/dist/core/dates.js +16 -0
  19. package/dist/core/errors.d.ts +12 -0
  20. package/dist/core/errors.js +31 -0
  21. package/dist/core/grouped-report.d.ts +87 -0
  22. package/dist/core/grouped-report.js +252 -0
  23. package/dist/core/index.d.ts +72 -0
  24. package/dist/core/index.js +37 -0
  25. package/dist/core/iteration.d.ts +7 -0
  26. package/dist/core/iteration.js +149 -0
  27. package/dist/core/limits.d.ts +7 -0
  28. package/dist/core/limits.js +56 -0
  29. package/dist/core/links.d.ts +5 -0
  30. package/dist/core/links.js +14 -0
  31. package/dist/core/presentation.d.ts +29 -0
  32. package/dist/core/presentation.js +59 -0
  33. package/dist/core/progress.d.ts +1 -0
  34. package/dist/core/progress.js +10 -0
  35. package/dist/core/sinks.d.ts +33 -0
  36. package/dist/core/sinks.js +101 -0
  37. package/dist/core/table.d.ts +8 -0
  38. package/dist/core/table.js +1 -0
  39. package/dist/core/value-text.d.ts +7 -0
  40. package/dist/core/value-text.js +13 -0
  41. package/dist/csv/index.d.ts +44 -0
  42. package/dist/csv/index.js +133 -0
  43. package/dist/csv/options.d.ts +1 -0
  44. package/dist/csv/options.js +8 -0
  45. package/dist/csv/structured-rows.d.ts +3 -0
  46. package/dist/csv/structured-rows.js +15 -0
  47. package/dist/execution/host.d.ts +5 -0
  48. package/dist/execution/host.js +164 -0
  49. package/dist/execution/index.d.ts +3 -0
  50. package/dist/execution/index.js +2 -0
  51. package/dist/execution/protocol.d.ts +48 -0
  52. package/dist/execution/protocol.js +173 -0
  53. package/dist/execution/types.d.ts +42 -0
  54. package/dist/execution/types.js +1 -0
  55. package/dist/execution/worker.d.ts +3 -0
  56. package/dist/execution/worker.js +147 -0
  57. package/dist/index.d.ts +32 -0
  58. package/dist/index.js +14 -0
  59. package/dist/integrations/canopyx/dates.d.ts +1 -0
  60. package/dist/integrations/canopyx/dates.js +34 -0
  61. package/dist/integrations/canopyx/index.d.ts +10 -0
  62. package/dist/integrations/canopyx/index.js +45 -0
  63. package/dist/integrations/canopyx/source.d.ts +3 -0
  64. package/dist/integrations/canopyx/source.js +116 -0
  65. package/dist/integrations/canopyx/types.d.ts +83 -0
  66. package/dist/integrations/canopyx/types.js +1 -0
  67. package/dist/integrations/datatables/api.d.ts +1 -0
  68. package/dist/integrations/datatables/api.js +27 -0
  69. package/dist/integrations/datatables/headings.d.ts +1 -0
  70. package/dist/integrations/datatables/headings.js +65 -0
  71. package/dist/integrations/datatables/index.d.ts +12 -0
  72. package/dist/integrations/datatables/index.js +167 -0
  73. package/dist/integrations/datatables/snapshot.d.ts +3 -0
  74. package/dist/integrations/datatables/snapshot.js +206 -0
  75. package/dist/integrations/datatables/types.d.ts +98 -0
  76. package/dist/integrations/datatables/types.js +1 -0
  77. package/dist/integrations/options.d.ts +13 -0
  78. package/dist/integrations/options.js +34 -0
  79. package/dist/internal/capture.d.ts +4 -0
  80. package/dist/internal/capture.js +14 -0
  81. package/dist/internal/columns.d.ts +27 -0
  82. package/dist/internal/columns.js +1 -0
  83. package/dist/internal/crc32.d.ts +6 -0
  84. package/dist/internal/crc32.js +17 -0
  85. package/dist/internal/export-values.d.ts +3 -0
  86. package/dist/internal/export-values.js +14 -0
  87. package/dist/internal/rows.d.ts +6 -0
  88. package/dist/internal/rows.js +68 -0
  89. package/dist/internal/table-spans.d.ts +11 -0
  90. package/dist/internal/table-spans.js +58 -0
  91. package/dist/internal/totals.d.ts +15 -0
  92. package/dist/internal/totals.js +42 -0
  93. package/dist/opc/index.d.ts +67 -0
  94. package/dist/opc/index.js +233 -0
  95. package/dist/opc/properties.d.ts +15 -0
  96. package/dist/opc/properties.js +18 -0
  97. package/dist/opc/uri.d.ts +5 -0
  98. package/dist/opc/uri.js +41 -0
  99. package/dist/pdf/annotations.d.ts +1 -0
  100. package/dist/pdf/annotations.js +38 -0
  101. package/dist/pdf/binary.d.ts +6 -0
  102. package/dist/pdf/binary.js +24 -0
  103. package/dist/pdf/document-drawings.d.ts +15 -0
  104. package/dist/pdf/document-drawings.js +237 -0
  105. package/dist/pdf/document-grouped.d.ts +1 -0
  106. package/dist/pdf/document-grouped.js +194 -0
  107. package/dist/pdf/document-images.d.ts +19 -0
  108. package/dist/pdf/document-images.js +125 -0
  109. package/dist/pdf/document-types.d.ts +160 -0
  110. package/dist/pdf/document-types.js +1 -0
  111. package/dist/pdf/document-vectors.d.ts +11 -0
  112. package/dist/pdf/document-vectors.js +77 -0
  113. package/dist/pdf/document.d.ts +9 -0
  114. package/dist/pdf/document.js +270 -0
  115. package/dist/pdf/drawing-types.d.ts +53 -0
  116. package/dist/pdf/drawing-types.js +1 -0
  117. package/dist/pdf/font-resources.d.ts +43 -0
  118. package/dist/pdf/font-resources.js +201 -0
  119. package/dist/pdf/font-support.d.ts +1 -0
  120. package/dist/pdf/font-support.js +60 -0
  121. package/dist/pdf/font.d.ts +17 -0
  122. package/dist/pdf/font.js +56 -0
  123. package/dist/pdf/grouped-table.d.ts +6 -0
  124. package/dist/pdf/grouped-table.js +40 -0
  125. package/dist/pdf/grouped-types.d.ts +34 -0
  126. package/dist/pdf/grouped-types.js +1 -0
  127. package/dist/pdf/grouped.d.ts +10 -0
  128. package/dist/pdf/grouped.js +37 -0
  129. package/dist/pdf/index.d.ts +22 -0
  130. package/dist/pdf/index.js +8 -0
  131. package/dist/pdf/inline-text.d.ts +30 -0
  132. package/dist/pdf/inline-text.js +142 -0
  133. package/dist/pdf/inline-types.d.ts +15 -0
  134. package/dist/pdf/inline-types.js +1 -0
  135. package/dist/pdf/list-layout.d.ts +6 -0
  136. package/dist/pdf/list-layout.js +49 -0
  137. package/dist/pdf/objects.d.ts +2 -0
  138. package/dist/pdf/objects.js +143 -0
  139. package/dist/pdf/outlines.d.ts +1 -0
  140. package/dist/pdf/outlines.js +55 -0
  141. package/dist/pdf/page-context.d.ts +1 -0
  142. package/dist/pdf/page-context.js +12 -0
  143. package/dist/pdf/pages.d.ts +1 -0
  144. package/dist/pdf/pages.js +679 -0
  145. package/dist/pdf/paragraph-layout.d.ts +19 -0
  146. package/dist/pdf/paragraph-layout.js +66 -0
  147. package/dist/pdf/paragraph-styles.d.ts +1 -0
  148. package/dist/pdf/paragraph-styles.js +66 -0
  149. package/dist/pdf/png.d.ts +9 -0
  150. package/dist/pdf/png.js +259 -0
  151. package/dist/pdf/preflight.d.ts +7 -0
  152. package/dist/pdf/preflight.js +32 -0
  153. package/dist/pdf/presentation-settings.d.ts +5 -0
  154. package/dist/pdf/presentation-settings.js +28 -0
  155. package/dist/pdf/providers/harfbuzz.d.ts +60 -0
  156. package/dist/pdf/providers/harfbuzz.js +112 -0
  157. package/dist/pdf/providers/unicode13-profile.d.ts +2 -0
  158. package/dist/pdf/providers/unicode13-profile.js +115 -0
  159. package/dist/pdf/section-types.d.ts +15 -0
  160. package/dist/pdf/section-types.js +40 -0
  161. package/dist/pdf/session.d.ts +25 -0
  162. package/dist/pdf/session.js +55 -0
  163. package/dist/pdf/settings.d.ts +15 -0
  164. package/dist/pdf/settings.js +171 -0
  165. package/dist/pdf/shaped-text.d.ts +5 -0
  166. package/dist/pdf/shaped-text.js +205 -0
  167. package/dist/pdf/standard-widths.d.ts +1 -0
  168. package/dist/pdf/standard-widths.js +317 -0
  169. package/dist/pdf/structure-parent-tree.d.ts +1 -0
  170. package/dist/pdf/structure-parent-tree.js +57 -0
  171. package/dist/pdf/structure.d.ts +6 -0
  172. package/dist/pdf/structure.js +250 -0
  173. package/dist/pdf/table-band-flow.d.ts +34 -0
  174. package/dist/pdf/table-band-flow.js +114 -0
  175. package/dist/pdf/table-band-plan.d.ts +10 -0
  176. package/dist/pdf/table-band-plan.js +85 -0
  177. package/dist/pdf/table-layout.d.ts +71 -0
  178. package/dist/pdf/table-layout.js +205 -0
  179. package/dist/pdf/table-structure.d.ts +1 -0
  180. package/dist/pdf/table-structure.js +92 -0
  181. package/dist/pdf/text-shaping-types.d.ts +39 -0
  182. package/dist/pdf/text-shaping-types.js +1 -0
  183. package/dist/pdf/text.d.ts +13 -0
  184. package/dist/pdf/text.js +96 -0
  185. package/dist/pdf/truetype-subset.d.ts +5 -0
  186. package/dist/pdf/truetype-subset.js +217 -0
  187. package/dist/pdf/truetype.d.ts +1 -0
  188. package/dist/pdf/truetype.js +235 -0
  189. package/dist/pdf/types.d.ts +169 -0
  190. package/dist/pdf/types.js +1 -0
  191. package/dist/pdf/write.d.ts +6 -0
  192. package/dist/pdf/write.js +57 -0
  193. package/dist/xlsx/attachments.d.ts +30 -0
  194. package/dist/xlsx/attachments.js +67 -0
  195. package/dist/xlsx/chart-bindings.d.ts +1 -0
  196. package/dist/xlsx/chart-bindings.js +122 -0
  197. package/dist/xlsx/chart-types.d.ts +73 -0
  198. package/dist/xlsx/chart-types.js +1 -0
  199. package/dist/xlsx/chart-xml.d.ts +2 -0
  200. package/dist/xlsx/chart-xml.js +103 -0
  201. package/dist/xlsx/charts.d.ts +1 -0
  202. package/dist/xlsx/charts.js +159 -0
  203. package/dist/xlsx/conditional-formatting.d.ts +1 -0
  204. package/dist/xlsx/conditional-formatting.js +158 -0
  205. package/dist/xlsx/conditional-types.d.ts +64 -0
  206. package/dist/xlsx/conditional-types.js +1 -0
  207. package/dist/xlsx/data-row-ranges.d.ts +5 -0
  208. package/dist/xlsx/data-row-ranges.js +39 -0
  209. package/dist/xlsx/differential-styles.d.ts +1 -0
  210. package/dist/xlsx/differential-styles.js +102 -0
  211. package/dist/xlsx/formula.d.ts +8 -0
  212. package/dist/xlsx/formula.js +104 -0
  213. package/dist/xlsx/grouped-layout.d.ts +61 -0
  214. package/dist/xlsx/grouped-layout.js +79 -0
  215. package/dist/xlsx/grouped.d.ts +16 -0
  216. package/dist/xlsx/grouped.js +161 -0
  217. package/dist/xlsx/index.d.ts +24 -0
  218. package/dist/xlsx/index.js +10 -0
  219. package/dist/xlsx/layout.d.ts +1 -0
  220. package/dist/xlsx/layout.js +186 -0
  221. package/dist/xlsx/options.d.ts +1 -0
  222. package/dist/xlsx/options.js +16 -0
  223. package/dist/xlsx/portable.d.ts +27 -0
  224. package/dist/xlsx/portable.js +44 -0
  225. package/dist/xlsx/preservation.d.ts +1 -0
  226. package/dist/xlsx/preservation.js +60 -0
  227. package/dist/xlsx/ranges.d.ts +1 -0
  228. package/dist/xlsx/ranges.js +24 -0
  229. package/dist/xlsx/regions.d.ts +1 -0
  230. package/dist/xlsx/regions.js +97 -0
  231. package/dist/xlsx/structured-rows.d.ts +11 -0
  232. package/dist/xlsx/structured-rows.js +28 -0
  233. package/dist/xlsx/style-xml.d.ts +1 -0
  234. package/dist/xlsx/style-xml.js +6 -0
  235. package/dist/xlsx/styles.d.ts +70 -0
  236. package/dist/xlsx/styles.js +203 -0
  237. package/dist/xlsx/table.d.ts +12 -0
  238. package/dist/xlsx/table.js +35 -0
  239. package/dist/xlsx/types.d.ts +180 -0
  240. package/dist/xlsx/types.js +1 -0
  241. package/dist/xlsx/validation-types.d.ts +18 -0
  242. package/dist/xlsx/validation-types.js +1 -0
  243. package/dist/xlsx/validation.d.ts +1 -0
  244. package/dist/xlsx/validation.js +92 -0
  245. package/dist/xlsx/values.d.ts +13 -0
  246. package/dist/xlsx/values.js +86 -0
  247. package/dist/xlsx/workbook.d.ts +50 -0
  248. package/dist/xlsx/workbook.js +452 -0
  249. package/dist/xlsx/worksheet.d.ts +71 -0
  250. package/dist/xlsx/worksheet.js +700 -0
  251. package/dist/xlsx/write.d.ts +15 -0
  252. package/dist/xlsx/write.js +47 -0
  253. package/dist/xml/index.d.ts +24 -0
  254. package/dist/xml/index.js +74 -0
  255. package/dist/zip/entry.d.ts +39 -0
  256. package/dist/zip/entry.js +122 -0
  257. package/dist/zip/index.d.ts +44 -0
  258. package/dist/zip/index.js +248 -0
  259. package/package.json +104 -4
@@ -0,0 +1,205 @@
1
+ import { ExportCell } from "../core/presentation.js";
2
+ import { copyExportLink } from "../core/links.js";
3
+ import { tableSpans } from "../internal/table-spans.js";
4
+ import { createTotals } from "../internal/totals.js";
5
+ import { presentation, positive } from "./settings.js";
6
+ import { displayText } from "./text.js";
7
+ import { capturePdfText, wrapPdfText } from "./inline-text.js";
8
+ import { planTableBands, projectBandRows, alignBandBlocks } from "./table-band-plan.js";
9
+ export class PdfTableLayout {
10
+ settings;
11
+ fonts;
12
+ bands;
13
+ widths;
14
+ lefts;
15
+ lineHeight;
16
+ headings;
17
+ totals;
18
+ repeatedColumns;
19
+ structuredBandRows = new WeakMap();
20
+ constructor(settings, fonts) {
21
+ this.settings = settings;
22
+ this.fonts = fonts;
23
+ const { options, padding, fontSize, page, margins } = settings, columns = options.columns, available = page.width - margins.left - margins.right;
24
+ this.repeatedColumns = new Set(typeof options.wideTable === "object" ? options.wideTable.repeatColumns : []);
25
+ let digit;
26
+ if (options.columnWidths && options.columnWidths.length !== columns.length)
27
+ throw new RangeError("columnWidths must declare one point width per column.");
28
+ const minimum = typeof options.wideTable === "object" ? options.wideTable.minimumColumnWidth ?? 72 : 0;
29
+ const widths = columns.map((column, i) => positive(options.columnWidths?.[i] ?? Math.max(minimum, column.width === undefined ? available / columns.length :
30
+ positive(column.width, "Column width", 255) * (digit ??= fonts.select().width(48) * fontSize / 1000) + padding * 2), "PDF column width"));
31
+ const sum = widths.reduce((n, w) => n + w, 0);
32
+ if (sum > available + .001 && options.wideTable === "reject")
33
+ throw new RangeError("Table widths exceed the printable page width.");
34
+ this.bands = planTableBands(widths, settings);
35
+ this.widths = !this.bands && sum > available ? widths.map(w => w * available / sum) : widths;
36
+ let left = margins.left;
37
+ this.lefts = this.widths.map(width => { const position = left; left += width; return position; });
38
+ if (this.widths.some(w => w <= padding * 2))
39
+ throw new RangeError("Page is too narrow for this many columns and the requested padding.");
40
+ this.lineHeight = fonts.select().lineHeight(fontSize);
41
+ const depth = columns.reduce((n, c) => Math.max(n, c.groups?.length ?? 0), 0);
42
+ if (depth > 16)
43
+ throw new RangeError("Grouped headings support at most 16 levels.");
44
+ if (depth && options.includeHeader === false)
45
+ throw new TypeError("Grouped headings require leaf headings.");
46
+ const headings = [];
47
+ if (options.includeHeader !== false) {
48
+ for (let level = 0; level < depth; level++) {
49
+ const cells = [];
50
+ for (let first = 0; first < columns.length;) {
51
+ let last = first;
52
+ const group = columns[first].groups?.[level] ?? "", prefix = JSON.stringify(columns[first].groups?.slice(0, level + 1));
53
+ while (group && last + 1 < columns.length && JSON.stringify(columns[last + 1].groups?.slice(0, level + 1)) === prefix)
54
+ last++;
55
+ cells.push({ first, span: last - first + 1, text: group });
56
+ first = last + 1;
57
+ }
58
+ headings.push(cells);
59
+ }
60
+ headings.push(columns.map((c, first) => ({ first, span: 1, text: c.header })));
61
+ }
62
+ this.headings = headings;
63
+ this.totals = createTotals(columns, options.footer?.totals);
64
+ }
65
+ async cell(value, first, span, row, style) {
66
+ const { options, padding, fontSize, budget } = this.settings, column = options.columns[first];
67
+ const own = value instanceof ExportCell ? value.presentation : undefined;
68
+ const link = value instanceof ExportCell && value.link ? copyExportLink(value.link) : undefined;
69
+ const combined = presentation({ ...(column.alignment === undefined ? {} : { alignment: column.alignment }),
70
+ ...(column.wrapText === undefined ? {} : { wrapText: column.wrapText }), ...style, ...own });
71
+ const font = this.fonts.select(combined.bold, combined.italic), width = this.widths.slice(first, first + span).reduce((n, w) => n + w, 0);
72
+ const context = { rowIndex: row, columnIndex: first, column }, raw = value instanceof ExportCell ? value.value : value;
73
+ let text;
74
+ if (options.formatRuns && row >= 0 && !(value instanceof ExportCell && value.text !== undefined)) {
75
+ const runs = options.formatRuns(raw, context);
76
+ if (!Array.isArray(runs)) {
77
+ if (runs && typeof runs.then === "function")
78
+ void Promise.resolve(runs).catch(() => { });
79
+ throw new TypeError("formatRuns must return a synchronous array of PDF text runs.");
80
+ }
81
+ text = runs;
82
+ }
83
+ else
84
+ text = displayText(value, options, context);
85
+ const captured = capturePdfText(text, combined, this.settings, this.fonts);
86
+ if (options.tagged && link && !captured.text.trim())
87
+ throw new TypeError("Tagged PDF cell links require nonempty displayed text.");
88
+ if (link && captured.runs.some(run => run.link))
89
+ throw new TypeError("A PDF cell link cannot be combined with inline run links.");
90
+ if (this.bands)
91
+ budget.release(1, captured.text.length);
92
+ else
93
+ budget.cell(captured.text, captured.text.length);
94
+ const lines = await wrapPdfText(captured, this.fonts, fontSize, width - padding * 2, this.settings, combined.wrapText !== false);
95
+ return { first, span, width, font, style: combined, lineHeight: lines.reduce((height, line) => Math.max(height, line.lineHeight), font.lineHeight(fontSize)),
96
+ ...(link ? { link } : {}), ...(this.bands ? { captured } : {}),
97
+ lines };
98
+ }
99
+ row(cells) {
100
+ const lines = Math.max(1, ...cells.map(c => c.lines.length));
101
+ const lineHeight = Math.max(this.lineHeight, ...cells.map(c => c.lineHeight));
102
+ return { cells, lines, lineHeight, height: lines * lineHeight + this.settings.padding * 2 };
103
+ }
104
+ async headers() {
105
+ if (this.settings.options.headerRows)
106
+ return this.block(this.settings.options.headerRows, this.settings.options.headerPresentation ?? {});
107
+ const { formatValue: _format, ...options } = this.settings.options;
108
+ // Header labels are already resolved text and never passed through a data-value formatter.
109
+ const rows = [];
110
+ for (const heading of this.headings) {
111
+ const cells = [];
112
+ for (const c of heading)
113
+ cells.push(await this.cell(new ExportCell(c.text, { text: c.text }), c.first, c.span, -1, options.headerPresentation ?? {}));
114
+ rows.push(this.row(cells));
115
+ }
116
+ return rows;
117
+ }
118
+ async block(matrix, style) {
119
+ const anchors = tableSpans(matrix, this.settings.options.columns.length);
120
+ const { formatValue: _format, ...literalOptions } = this.settings.options;
121
+ const cells = [];
122
+ for (const row of anchors) {
123
+ const laidOut = [];
124
+ for (const c of row) {
125
+ const value = c.value instanceof ExportCell ? c.value : new ExportCell(c.value);
126
+ const literal = new ExportCell(value.value, { text: displayText(value, literalOptions, { rowIndex: -1, columnIndex: c.first, column: literalOptions.columns[c.first] }),
127
+ ...(value.presentation ? { presentation: value.presentation } : {}), ...(value.link ? { link: value.link } : {}) });
128
+ laidOut.push({ ...await this.cell(literal, c.first, c.span, -1, style), rowSpan: c.rowSpan });
129
+ }
130
+ cells.push(laidOut);
131
+ }
132
+ const heights = cells.map(row => Math.max(this.lineHeight + this.settings.padding * 2, ...row.filter(c => c.rowSpan === 1).map(c => c.lines.length * c.lineHeight + this.settings.padding * 2)));
133
+ cells.forEach((row, r) => row.forEach(c => {
134
+ const existing = heights.slice(r, r + c.rowSpan).reduce((n, h) => n + h, 0), needed = c.lines.length * c.lineHeight + this.settings.padding * 2;
135
+ if (needed > existing)
136
+ for (let i = r; i < r + c.rowSpan; i++)
137
+ heights[i] += (needed - existing) / c.rowSpan;
138
+ }));
139
+ return cells.map((row, r) => ({ cells: row.map(c => ({ ...c, height: heights.slice(r, r + c.rowSpan).reduce((n, h) => n + h, 0) })),
140
+ lines: Math.max(1, Math.ceil((heights[r] - this.settings.padding * 2) / this.lineHeight)), lineHeight: this.lineHeight, height: heights[r], structured: true }));
141
+ }
142
+ /** Repeated identifiers stay complete on every vertical fragment at the row's shared line height. */
143
+ minimumRowLines(row) {
144
+ let lines = 1;
145
+ if (this.bands)
146
+ for (const cell of row.cells)
147
+ if (this.repeatedColumns.has(cell.first))
148
+ lines = Math.max(lines, cell.lines.length);
149
+ return lines;
150
+ }
151
+ minimumRowHeight(row) { return this.minimumRowLines(row) * row.lineHeight + this.settings.padding * 2; }
152
+ repeatedColumn(index) { return this.repeatedColumns.has(index); }
153
+ /** Measure the physical block before kept content is painted. Structured band projections are
154
+ * shared with the painter, so measurement cannot disagree with narrower span intersections. */
155
+ async blockHeight(rows) {
156
+ const physical = this.bands ? (await this.bandRows(rows, true))[0] : rows;
157
+ return physical.reduce((height, row) => height + row.height, 0);
158
+ }
159
+ /** Project already captured cells. Cache only bounded structured headings/footer matrices;
160
+ * ordinary data windows remain owned and released by PdfTableBandFlow. */
161
+ async bandRows(rows, structured = false) {
162
+ if (!this.bands)
163
+ throw new Error("Horizontal bands were not planned.");
164
+ if (!structured)
165
+ return this.projectBandRows(rows, false);
166
+ let prepared = this.structuredBandRows.get(rows);
167
+ if (!prepared) {
168
+ prepared = this.projectBandRows(rows, true);
169
+ this.structuredBandRows.set(rows, prepared);
170
+ }
171
+ return prepared;
172
+ }
173
+ async projectBandRows(rows, structured) {
174
+ const projected = [], seen = new Set();
175
+ for (const band of this.bands) {
176
+ const physical = await projectBandRows(rows, band, this.widths, this.settings, this.fonts);
177
+ projected.push(physical.map(row => ({ ...row, cells: row.cells.map(cell => {
178
+ const source = cell.source, repeated = seen.has(source);
179
+ seen.add(source);
180
+ return repeated ? { ...cell, semanticArtifact: true } : cell;
181
+ }) })));
182
+ }
183
+ return structured ? alignBandBlocks(projected, this.settings) : projected;
184
+ }
185
+ async data(values, index, footer = false) {
186
+ const { options } = this.settings;
187
+ const cells = [];
188
+ for (let column = 0; column < options.columns.length; column++) {
189
+ const value = values[column], raw = value instanceof ExportCell ? value.value : value;
190
+ if (!footer)
191
+ this.totals[column].accept(raw);
192
+ const style = footer ? options.footerPresentation ?? {} : index % 2 && options.alternateRowColor ? { background: options.alternateRowColor } : {};
193
+ cells.push(await this.cell(value, column, 1, index, style));
194
+ }
195
+ return this.row(cells);
196
+ }
197
+ async footer(index) {
198
+ if (!this.settings.options.footer)
199
+ return undefined;
200
+ if (this.settings.options.footer.rows)
201
+ return this.block(this.settings.options.footer.rows, this.settings.options.footerPresentation ?? {});
202
+ const values = this.totals.map((total, i) => total.operation ? total.value() : this.settings.options.footer?.values?.[i]);
203
+ return [await this.data(values, index, true)];
204
+ }
205
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,92 @@
1
+ /** @internal Source-cell identities survive page fragments and horizontal band projections.
2
+ * Only column headers and still-owned row objects remain reachable from this table owner. */
3
+ export class PdfTableStructure {
4
+ structure;
5
+ table;
6
+ headers;
7
+ cells = new WeakMap();
8
+ rows = new WeakMap();
9
+ columnHeaders = [];
10
+ headerCount = 0;
11
+ headersPrepared = false;
12
+ constructor(structure, table, headers) {
13
+ this.structure = structure;
14
+ this.table = table;
15
+ this.headers = headers;
16
+ }
17
+ /** Allocate the complete logical header grid before its first visual band is painted. */
18
+ async prepareHeaders() {
19
+ if (this.headersPrepared)
20
+ return;
21
+ for (let rowIndex = 0; rowIndex < this.headers.length; rowIndex++) {
22
+ const row = this.headers[rowIndex], parent = this.structure.element("TR", this.table);
23
+ this.rows.set(row, parent);
24
+ for (const cell of row.cells) {
25
+ const header = cell.lines.some(line => line.text.trim().length > 0);
26
+ const associations = this.associations(cell, rowIndex);
27
+ const id = this.structure.element(header ? "TH" : "TD", parent, this.attributes(cell, associations, header ? "Column" : undefined));
28
+ this.cells.set(cell, id);
29
+ if (header) {
30
+ const heading = { row: rowIndex, order: this.headerCount++, identifier: await this.structure.identifier(id) };
31
+ for (let column = cell.first; column < cell.first + cell.span; column++)
32
+ (this.columnHeaders[column] ??= []).push(heading);
33
+ }
34
+ }
35
+ }
36
+ this.headersPrepared = true;
37
+ }
38
+ /** Call after every initial physical band; repeated visual headings then become artifacts. */
39
+ async completeHeaders() {
40
+ for (const row of this.headers)
41
+ await this.completeRow(row);
42
+ }
43
+ /** Register one original row before projecting or painting it. Summary label headings are explicit. */
44
+ async prepareRow(row, _kind = "data", parent = this.table, rowHeaderIndex) {
45
+ const existing = this.rows.get(row);
46
+ if (existing !== undefined)
47
+ return row.cells.map(cell => this.cells.get(cell));
48
+ const rowId = this.structure.element("TR", parent);
49
+ this.rows.set(row, rowId);
50
+ const ids = [];
51
+ let rowHeaderIdentifier;
52
+ // Allocating in source-column order preserves the record's logical reading order.
53
+ for (let index = 0; index < row.cells.length; index++) {
54
+ const cell = row.cells[index], isRowHeader = index === rowHeaderIndex;
55
+ const associations = this.associations(cell);
56
+ if (rowHeaderIdentifier !== undefined && !isRowHeader)
57
+ associations.push(rowHeaderIdentifier);
58
+ const id = this.structure.element(isRowHeader ? "TH" : "TD", rowId, this.attributes(cell, associations, isRowHeader ? "Row" : undefined));
59
+ this.cells.set(cell, id);
60
+ ids.push(id);
61
+ if (isRowHeader)
62
+ rowHeaderIdentifier = await this.structure.identifier(id);
63
+ }
64
+ return ids;
65
+ }
66
+ owner(cell) { return this.cells.get(cell); }
67
+ async completeRow(row) {
68
+ await this.structure.complete(this.rows.get(row));
69
+ }
70
+ async finish() {
71
+ await this.completeHeaders();
72
+ await this.structure.complete(this.table);
73
+ }
74
+ associations(cell, beforeRow = Number.POSITIVE_INFINITY) {
75
+ // Ordinary cells visit only their heading depth, independent of the table's width.
76
+ if (cell.span === 1)
77
+ return (this.columnHeaders[cell.first] ?? []).filter(header => header.row < beforeRow).map(header => header.identifier);
78
+ const headers = new Set();
79
+ for (let column = cell.first; column < cell.first + cell.span; column++)
80
+ for (const header of this.columnHeaders[column] ?? [])
81
+ if (header.row < beforeRow)
82
+ headers.add(header);
83
+ // Spans can encounter a shared ancestor through several columns. Preserve logical allocation
84
+ // order, rather than column visitation order, and name that ancestor exactly once.
85
+ return [...headers].sort((left, right) => left.order - right.order).map(header => header.identifier);
86
+ }
87
+ attributes(cell, headers, scope) {
88
+ return " /A << /O /Table" + (scope === undefined ? "" : " /Scope /" + scope) +
89
+ (cell.span > 1 ? " /ColSpan " + cell.span : "") + ((cell.rowSpan ?? 1) > 1 ? " /RowSpan " + cell.rowSpan : "") +
90
+ (headers.length ? " /Headers [" + headers.map(id => "(" + id + ")").join(" ") + "]" : "") + " >>";
91
+ }
92
+ }
@@ -0,0 +1,39 @@
1
+ import type { PdfFont } from "./font.js";
2
+ /** Horizontal text direction. Auto resolves a base direction for each physical paragraph. */
3
+ export type PdfTextDirection = "auto" | "ltr" | "rtl";
4
+ /** A registered font covering a half-open UTF-16 range of the physical paragraph. */
5
+ export interface PdfShapingRun {
6
+ readonly start: number;
7
+ readonly end: number;
8
+ readonly font: PdfFont;
9
+ }
10
+ /** One positioned glyph. Metrics use 1,000 units per em; clusters are logical, half-open UTF-16 ranges. */
11
+ export interface PdfShapedGlyph {
12
+ readonly id: number;
13
+ readonly clusterStart: number;
14
+ readonly clusterEnd: number;
15
+ readonly xAdvance: number;
16
+ readonly yAdvance: number;
17
+ readonly xOffset: number;
18
+ readonly yOffset: number;
19
+ }
20
+ /** Glyphs and runs are in left-to-right painting order, regardless of their logical text direction. */
21
+ export interface PdfShapedRun {
22
+ readonly font: PdfFont;
23
+ readonly glyphs: readonly PdfShapedGlyph[];
24
+ }
25
+ /** The entire paragraph is supplied so a wrapped line retains paragraph bidi context. */
26
+ export interface PdfShapeLineRequest {
27
+ readonly text: string;
28
+ readonly start: number;
29
+ readonly end: number;
30
+ readonly runs: readonly PdfShapingRun[];
31
+ readonly direction: PdfTextDirection;
32
+ readonly signal?: AbortSignal;
33
+ }
34
+ /** Replaceable shaping boundary. The PDF writer validates output and owns line flow, fonts and PDF objects.
35
+ * Requests contain at most 4,096 UTF-16 units. Return at most 32,768 horizontal glyphs.
36
+ * Providers must preserve logical clusters and must not silently substitute missing fonts or glyphs. */
37
+ export interface PdfTextShaper {
38
+ shapeLine(request: PdfShapeLineRequest): PromiseLike<readonly PdfShapedRun[]> | readonly PdfShapedRun[];
39
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,13 @@
1
+ import type { ExportValue, ColumnValueContext } from "../core/index.js";
2
+ import type { PdfOptions } from "./types.js";
3
+ import type { PdfFontResource } from "./font-resources.js";
4
+ export declare function synchronousText(value: unknown, name: string): string;
5
+ export declare function displayText(value: ExportValue, options: PdfOptions, context: ColumnValueContext): string;
6
+ export interface PdfTextLine {
7
+ readonly text: string;
8
+ readonly width: number;
9
+ }
10
+ /** Greedy word boundaries, then scalar boundaries. No character, whitespace or long-word truncation. */
11
+ export declare function wrapText(text: string, font: PdfFontResource, size: number, width: number, maxCharacters: number, maxLines: number, wrap?: boolean): readonly PdfTextLine[];
12
+ /** Let the host process input and cancellation while a large cell or paragraph is laid out. */
13
+ export declare function wrapTextCooperatively(text: string, font: PdfFontResource, size: number, width: number, maxCharacters: number, maxLines: number, wrap?: boolean, signal?: AbortSignal): Promise<readonly PdfTextLine[]>;
@@ -0,0 +1,96 @@
1
+ import { OfficeIMOError } from "../core/errors.js";
2
+ import { ExportCell, assertExportValue } from "../core/presentation.js";
3
+ import { checkAbort, pause, taskYieldDue } from "../core/iteration.js";
4
+ import { isDate } from "../core/dates.js";
5
+ import { defaultBooleanText } from "../core/value-text.js";
6
+ export function synchronousText(value, name) {
7
+ if (typeof value === "string")
8
+ return value;
9
+ if (value && typeof value.then === "function")
10
+ void Promise.resolve(value).catch(() => { });
11
+ throw new TypeError(name + " must return a synchronous string.");
12
+ }
13
+ export function displayText(value, options, context) {
14
+ assertExportValue(value);
15
+ if (value instanceof ExportCell && value.text !== undefined)
16
+ return value.text;
17
+ const raw = value instanceof ExportCell ? value.value : value;
18
+ if (options.formatValue)
19
+ return synchronousText(options.formatValue(raw, context), "formatValue");
20
+ return raw == null ? "" : isDate(raw) ? Number.isFinite(raw.getTime()) ? raw.toISOString() : "" : typeof raw === "number" && !Number.isFinite(raw) ? "" : typeof raw === "boolean" ? (options.booleanText ?? defaultBooleanText)[raw ? "true" : "false"] : String(raw);
21
+ }
22
+ /** Greedy word boundaries, then scalar boundaries. No character, whitespace or long-word truncation. */
23
+ export function wrapText(text, font, size, width, maxCharacters, maxLines, wrap = true) {
24
+ const scanner = wrapSteps(text, font, size, width, maxCharacters, maxLines, wrap);
25
+ while (true) {
26
+ const next = scanner.next();
27
+ if (next.done)
28
+ return next.value;
29
+ }
30
+ }
31
+ /** Let the host process input and cancellation while a large cell or paragraph is laid out. */
32
+ export async function wrapTextCooperatively(text, font, size, width, maxCharacters, maxLines, wrap = true, signal) {
33
+ checkAbort(signal);
34
+ const scanner = wrapSteps(text, font, size, width, maxCharacters, maxLines, wrap);
35
+ while (true) {
36
+ const next = scanner.next();
37
+ if (next.done)
38
+ return next.value;
39
+ if (taskYieldDue())
40
+ await pause();
41
+ checkAbort(signal);
42
+ }
43
+ }
44
+ /** One scanner owns wrapping semantics for synchronous measurements and cooperative layout. */
45
+ function* wrapSteps(text, font, size, width, maxCharacters, maxLines, wrap) {
46
+ if (text.length > maxCharacters)
47
+ throw new OfficeIMOError("RESOURCE_LIMIT", "maxCellCharacters exceeded.");
48
+ const lines = [];
49
+ let scanned = 0;
50
+ function emit(chars, measure) {
51
+ if (lines.length >= maxLines)
52
+ throw new OfficeIMOError("RESOURCE_LIMIT", "maxRowLines exceeded.");
53
+ lines.push({ text: chars.join(""), width: measure });
54
+ }
55
+ for (const paragraph of text.replace(/\r\n?/g, "\n").replace(/\t/g, " ").split("\n")) {
56
+ let chars = [], advances = [], measured = 0, breakAt = -1;
57
+ for (const character of paragraph) {
58
+ if ((++scanned & 4095) === 0)
59
+ yield;
60
+ const advance = font.width(character.codePointAt(0)) * size / 1000;
61
+ if (advance > width + .001)
62
+ throw new RangeError("PDF column is too narrow for a glyph. Increase page/column width or reduce padding/font size.");
63
+ if (measured + advance > width + .001) {
64
+ if (!wrap)
65
+ throw new RangeError("Unwrapped PDF cell exceeds its column width.");
66
+ const end = breakAt >= 0 ? breakAt + 1 : chars.length;
67
+ const used = advances.slice(0, end).reduce((n, a) => n + a, 0);
68
+ emit(chars.slice(0, end), used);
69
+ chars = chars.slice(end);
70
+ advances = advances.slice(end);
71
+ measured = advances.reduce((n, a) => n + a, 0);
72
+ breakAt = -1;
73
+ for (let i = 0; i < chars.length; i++)
74
+ if (chars[i] === " ")
75
+ breakAt = i;
76
+ // A trailing word can still exceed the next line after a prior word boundary.
77
+ if (measured + advance > width + .001) {
78
+ emit(chars, measured);
79
+ chars = [];
80
+ advances = [];
81
+ measured = 0;
82
+ breakAt = -1;
83
+ }
84
+ }
85
+ chars.push(character);
86
+ advances.push(advance);
87
+ measured += advance;
88
+ if (character === " ")
89
+ breakAt = chars.length - 1;
90
+ }
91
+ emit(chars, measured);
92
+ if ((++scanned & 4095) === 0)
93
+ yield;
94
+ }
95
+ return lines;
96
+ }
@@ -0,0 +1,5 @@
1
+ import type { TrueTypeFont } from "./truetype.js";
2
+ /** Preserve glyph IDs and complete composite dependencies, as in OfficeIMO.Pdf's native subsetter. */
3
+ export declare function subsetTrueType(font: TrueTypeFont, requested: ReadonlySet<number>, characters: ReadonlyMap<number, number>): Uint8Array;
4
+ /** Build the same font bytes with cancellation and bounded cooperative scans. */
5
+ export declare function subsetTrueTypeCooperatively(font: TrueTypeFont, requested: ReadonlySet<number>, characters: ReadonlyMap<number, number>, signal?: AbortSignal): Promise<Uint8Array>;