@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,217 @@
1
+ import { align4 } from "./binary.js";
2
+ import { checkAbort, pause, taskYieldDue } from "../core/iteration.js";
3
+ /** Preserve glyph IDs and complete composite dependencies, as in OfficeIMO.Pdf's native subsetter. */
4
+ export function subsetTrueType(font, requested, characters) {
5
+ const scanner = subsetSteps(font, requested, characters);
6
+ while (true) {
7
+ const next = scanner.next();
8
+ if (next.done)
9
+ return next.value;
10
+ }
11
+ }
12
+ /** Build the same font bytes with cancellation and bounded cooperative scans. */
13
+ export async function subsetTrueTypeCooperatively(font, requested, characters, signal) {
14
+ checkAbort(signal);
15
+ const scanner = subsetSteps(font, requested, characters);
16
+ while (true) {
17
+ const next = scanner.next();
18
+ if (next.done) {
19
+ checkAbort(signal);
20
+ return next.value;
21
+ }
22
+ if (taskYieldDue())
23
+ await pause();
24
+ checkAbort(signal);
25
+ }
26
+ }
27
+ function* subsetSteps(font, requested, characters) {
28
+ const glyphs = new Set([0, ...requested]), glyfTable = font.table("glyf"), queue = [...glyphs];
29
+ const dependencies = new Map();
30
+ let scanned = 0;
31
+ for (let i = 0; i < queue.length; i++) {
32
+ if ((++scanned & 1023) === 0)
33
+ yield;
34
+ const glyph = queue[i];
35
+ if (!Number.isInteger(glyph) || glyph < 0 || glyph >= font.glyphCount)
36
+ throw new TypeError("Invalid subset glyph.");
37
+ const start = font.offsets[glyph], end = font.offsets[glyph + 1];
38
+ if (start === end)
39
+ continue;
40
+ if (end - start < 10)
41
+ throw new TypeError("Truncated TrueType glyph header.");
42
+ if (font.i16(glyfTable.offset + start) >= 0)
43
+ continue;
44
+ let cursor = glyfTable.offset + start + 10, flags;
45
+ const children = [];
46
+ dependencies.set(glyph, children);
47
+ const limit = glyfTable.offset + end;
48
+ do {
49
+ if (cursor + 4 > limit)
50
+ throw new TypeError("Truncated TrueType composite.");
51
+ flags = font.u16(cursor);
52
+ const component = font.u16(cursor + 2);
53
+ if (component >= font.glyphCount)
54
+ throw new TypeError("Invalid TrueType composite dependency.");
55
+ children.push(component);
56
+ if ((++scanned & 1023) === 0)
57
+ yield;
58
+ if (!glyphs.has(component)) {
59
+ glyphs.add(component);
60
+ queue.push(component);
61
+ }
62
+ cursor += 4 + (flags & 1 ? 4 : 2) + (flags & 8 ? 2 : flags & 64 ? 4 : flags & 128 ? 8 : 0);
63
+ if (cursor > limit)
64
+ throw new TypeError("Truncated TrueType composite transform.");
65
+ } while (flags & 32);
66
+ if (flags & 256) {
67
+ if (cursor + 2 > limit || cursor + 2 + font.u16(cursor) > limit)
68
+ throw new TypeError("Truncated composite instructions.");
69
+ }
70
+ }
71
+ // A malformed caller-provided font must not export recursive composite glyphs.
72
+ const visited = new Set(), visiting = new Set();
73
+ function* visit(glyph, depth) {
74
+ if ((++scanned & 1023) === 0)
75
+ yield;
76
+ if (visiting.has(glyph))
77
+ throw new TypeError("Cyclic TrueType composite dependency.");
78
+ if (depth > 64)
79
+ throw new TypeError("TrueType composite nesting exceeds 64 levels.");
80
+ if (visited.has(glyph))
81
+ return;
82
+ visiting.add(glyph);
83
+ for (const child of dependencies.get(glyph) ?? [])
84
+ yield* visit(child, depth + 1);
85
+ visiting.delete(glyph);
86
+ visited.add(glyph);
87
+ }
88
+ for (const glyph of glyphs)
89
+ yield* visit(glyph, 0);
90
+ const offsets = new Uint32Array(font.glyphCount + 1);
91
+ let length = 0;
92
+ for (let glyph = 0; glyph < font.glyphCount; glyph++) {
93
+ offsets[glyph] = length;
94
+ if (glyphs.has(glyph))
95
+ length += align4(font.offsets[glyph + 1] - font.offsets[glyph]);
96
+ if ((glyph & 1023) === 1023)
97
+ yield;
98
+ }
99
+ offsets[font.glyphCount] = length;
100
+ const glyf = new Uint8Array(length);
101
+ for (const glyph of glyphs) {
102
+ yield* copyBytes(font.bytes.subarray(glyfTable.offset + font.offsets[glyph], glyfTable.offset + font.offsets[glyph + 1]), glyf, offsets[glyph]);
103
+ if ((++scanned & 1023) === 0)
104
+ yield;
105
+ }
106
+ const loca = new Uint8Array(offsets.length * 4), locaView = new DataView(loca.buffer);
107
+ for (let i = 0; i < offsets.length; i++) {
108
+ locaView.setUint32(i * 4, offsets[i]);
109
+ if ((i & 1023) === 1023)
110
+ yield;
111
+ }
112
+ const retained = new Set(["OS/2", "cmap", "cvt ", "fpgm", "gasp", "glyf", "head", "hhea", "hmtx", "loca", "maxp", "name", "post", "prep"]);
113
+ const cmap = yield* subsetCmap(characters), hmtx = yield* subsetMetrics(font, glyphs);
114
+ const tables = [];
115
+ for (const [tag, table] of [...font.tables].filter(([tag]) => retained.has(tag)).sort(([a], [b]) => a < b ? -1 : 1)) {
116
+ const existing = tag === "glyf" ? glyf : tag === "loca" ? loca : tag === "cmap" ? cmap : tag === "hmtx" ? hmtx : undefined;
117
+ const bytes = existing ?? new Uint8Array(table.length);
118
+ if (!existing)
119
+ yield* copyBytes(font.bytes.subarray(table.offset, table.offset + table.length), bytes);
120
+ if (tag === "head") {
121
+ const view = new DataView(bytes.buffer);
122
+ view.setUint32(8, 0);
123
+ view.setInt16(50, 1);
124
+ }
125
+ tables.push({ tag, bytes });
126
+ }
127
+ const size = 12 + tables.length * 16 + tables.reduce((n, t) => n + align4(t.bytes.length), 0), output = new Uint8Array(size), view = new DataView(output.buffer);
128
+ view.setUint32(0, 0x10000);
129
+ view.setUint16(4, tables.length);
130
+ const power = 2 ** Math.floor(Math.log2(tables.length));
131
+ view.setUint16(6, power * 16);
132
+ view.setUint16(8, Math.log2(power));
133
+ view.setUint16(10, tables.length * 16 - power * 16);
134
+ let cursor = 12 + tables.length * 16, head = 0;
135
+ for (let i = 0; i < tables.length; i++) {
136
+ const table = tables[i];
137
+ const p = 12 + i * 16;
138
+ for (let j = 0; j < 4; j++)
139
+ output[p + j] = table.tag.charCodeAt(j);
140
+ view.setUint32(p + 4, yield* checksumSteps(table.bytes));
141
+ view.setUint32(p + 8, cursor);
142
+ view.setUint32(p + 12, table.bytes.length);
143
+ yield* copyBytes(table.bytes, output, cursor);
144
+ if (table.tag === "head")
145
+ head = cursor;
146
+ cursor += align4(table.bytes.length);
147
+ }
148
+ view.setUint32(head + 8, (0xb1b0afba - (yield* checksumSteps(output))) >>> 0);
149
+ return output;
150
+ }
151
+ /** Keep original glyph IDs, advance widths and bearings for every selected/composite glyph.
152
+ * Empty glyph slots have no outline; their unused metrics need not inflate the compressed subset. */
153
+ function* subsetMetrics(font, glyphs) {
154
+ let scanned = 0;
155
+ const metrics = font.u16(font.table("hhea").offset + 34), table = font.table("hmtx");
156
+ const result = new Uint8Array(metrics * 4 + (font.glyphCount - metrics) * 2);
157
+ for (const glyph of glyphs) {
158
+ if ((++scanned & 1023) === 0)
159
+ yield;
160
+ const first = glyph < metrics ? glyph * 4 : metrics * 4 + (glyph - metrics) * 2, length = glyph < metrics ? 4 : 2;
161
+ result.set(font.bytes.subarray(table.offset + first, table.offset + first + length), first);
162
+ if (glyph >= metrics) {
163
+ // Trailing short records share the final full record's advance width.
164
+ const advance = (metrics - 1) * 4;
165
+ result.set(font.bytes.subarray(table.offset + advance, table.offset + advance + 2), advance);
166
+ }
167
+ }
168
+ return result;
169
+ }
170
+ /** One Unicode format-12 map covers BMP and supplementary scalars without retaining unused coverage. */
171
+ function* subsetCmap(characters) {
172
+ let scanned = 0;
173
+ const entries = [...characters].sort(([a], [b]) => a - b), groups = [];
174
+ for (const [scalar, glyph] of entries) {
175
+ if ((++scanned & 1023) === 0)
176
+ yield;
177
+ const previous = groups.at(-1);
178
+ if (previous && scalar === previous.last + 1 && glyph === previous.glyph + scalar - previous.first)
179
+ previous.last = scalar;
180
+ else
181
+ groups.push({ first: scalar, last: scalar, glyph });
182
+ }
183
+ const result = new Uint8Array(12 + 16 + groups.length * 12), view = new DataView(result.buffer);
184
+ view.setUint16(2, 1);
185
+ view.setUint16(4, 3);
186
+ view.setUint16(6, 10);
187
+ view.setUint32(8, 12);
188
+ view.setUint16(12, 12);
189
+ view.setUint32(16, result.length - 12);
190
+ view.setUint32(24, groups.length);
191
+ for (let i = 0; i < groups.length; i++) {
192
+ const group = groups[i];
193
+ const p = 28 + i * 12;
194
+ view.setUint32(p, group.first);
195
+ view.setUint32(p + 4, group.last);
196
+ view.setUint32(p + 8, group.glyph);
197
+ if ((i & 1023) === 1023)
198
+ yield;
199
+ }
200
+ return result;
201
+ }
202
+ /** Copy owned font tables without one long native copy between cancellation points. */
203
+ function* copyBytes(source, target, offset = 0) {
204
+ for (let first = 0; first < source.length; first += 65536) {
205
+ target.set(source.subarray(first, Math.min(source.length, first + 65536)), offset + first);
206
+ yield;
207
+ }
208
+ }
209
+ function* checksumSteps(bytes) {
210
+ let sum = 0;
211
+ for (let i = 0; i < bytes.length; i += 4) {
212
+ sum = (sum + (((bytes[i] ?? 0) * 0x1000000) + ((bytes[i + 1] ?? 0) << 16) + ((bytes[i + 2] ?? 0) << 8) + (bytes[i + 3] ?? 0))) >>> 0;
213
+ if ((i & 65535) === 65532)
214
+ yield;
215
+ }
216
+ return sum;
217
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,235 @@
1
+ import { NotSupportedError } from "../core/errors.js";
2
+ import { FontReader } from "./binary.js";
3
+ import { subsetTrueType, subsetTrueTypeCooperatively } from "./truetype-subset.js";
4
+ import { checkAbort, pause, taskYieldDue } from "../core/iteration.js";
5
+ /** @internal Native TrueType outline profile shared with the C# PDF writer's glyph-preserving subset design. */
6
+ export class TrueTypeFont extends FontReader {
7
+ tables;
8
+ glyphCount;
9
+ units;
10
+ ascent;
11
+ descent;
12
+ bbox;
13
+ widths;
14
+ offsets;
15
+ canSubset;
16
+ cmaps;
17
+ constructor(bytes, prepared) {
18
+ super(bytes);
19
+ const scanner = parseFont(this);
20
+ let data = prepared;
21
+ if (!data)
22
+ while (true) {
23
+ const next = scanner.next();
24
+ if (next.done) {
25
+ data = next.value;
26
+ break;
27
+ }
28
+ }
29
+ this.tables = data.tables;
30
+ this.glyphCount = data.glyphCount;
31
+ this.units = data.units;
32
+ this.ascent = data.ascent;
33
+ this.descent = data.descent;
34
+ this.bbox = data.bbox;
35
+ this.widths = data.widths;
36
+ this.offsets = data.offsets;
37
+ this.canSubset = data.canSubset;
38
+ this.cmaps = data.cmaps;
39
+ }
40
+ /** Parse an owned immutable snapshot while allowing cancellation and host events between bounded scans. */
41
+ static async create(bytes, signal) {
42
+ checkAbort(signal);
43
+ const scanner = parseFont(new FontReader(bytes));
44
+ while (true) {
45
+ const next = scanner.next();
46
+ if (next.done) {
47
+ checkAbort(signal);
48
+ return new TrueTypeFont(bytes, next.value);
49
+ }
50
+ if (taskYieldDue())
51
+ await pause();
52
+ checkAbort(signal);
53
+ }
54
+ }
55
+ table(tag, minimum = 0) {
56
+ const table = this.tables.get(tag);
57
+ if (!table || table.length < minimum)
58
+ throw new TypeError("Missing or truncated TrueType table: " + tag);
59
+ return table;
60
+ }
61
+ glyph(scalar) {
62
+ for (const map of this.cmaps) {
63
+ const p = map.offset, type = this.u16(p);
64
+ let glyph = 0;
65
+ if (type === 12) {
66
+ let left = 0, right = this.u32(p + 12) - 1;
67
+ while (left <= right) {
68
+ const middle = (left + right) >>> 1, q = p + 16 + middle * 12, start = this.u32(q), end = this.u32(q + 4);
69
+ if (scalar < start)
70
+ right = middle - 1;
71
+ else if (scalar > end)
72
+ left = middle + 1;
73
+ else {
74
+ glyph = this.u32(q + 8) + scalar - start;
75
+ break;
76
+ }
77
+ }
78
+ }
79
+ else if (scalar <= 0xffff) {
80
+ const n = this.u16(p + 6) / 2;
81
+ let left = 0, right = n - 1;
82
+ while (left < right) {
83
+ const mid = (left + right) >>> 1;
84
+ if (scalar > this.u16(p + 14 + mid * 2))
85
+ left = mid + 1;
86
+ else
87
+ right = mid;
88
+ }
89
+ const start = this.u16(p + 16 + n * 2 + left * 2);
90
+ if (scalar >= start && scalar <= this.u16(p + 14 + left * 2)) {
91
+ const delta = this.i16(p + 16 + n * 4 + left * 2), address = p + 16 + n * 6 + left * 2, range = this.u16(address);
92
+ if (!range)
93
+ glyph = (scalar + delta) & 0xffff;
94
+ else {
95
+ const q = address + range + (scalar - start) * 2;
96
+ if (q + 2 > p + map.length)
97
+ throw new TypeError("cmap glyph array exceeds its table.");
98
+ glyph = this.u16(q);
99
+ if (glyph)
100
+ glyph = (glyph + delta) & 0xffff;
101
+ }
102
+ }
103
+ }
104
+ if (glyph >= this.glyphCount)
105
+ throw new TypeError("cmap references an invalid glyph.");
106
+ if (glyph)
107
+ return glyph;
108
+ }
109
+ return 0;
110
+ }
111
+ subset(glyphs, characters) { return this.canSubset ? subsetTrueType(this, glyphs, characters) : this.bytes; }
112
+ async subsetCooperatively(glyphs, characters, signal) {
113
+ checkAbort(signal);
114
+ return this.canSubset ? subsetTrueTypeCooperatively(this, glyphs, characters, signal) : this.bytes;
115
+ }
116
+ }
117
+ /** One parser serves synchronous construction and cooperative preparation. */
118
+ function* parseFont(reader) {
119
+ const tables = new Map();
120
+ const table = (tag, minimum = 0) => {
121
+ const result = tables.get(tag);
122
+ if (!result || result.length < minimum)
123
+ throw new TypeError("Missing or truncated TrueType table: " + tag);
124
+ return result;
125
+ };
126
+ if (reader.u32(0) !== 0x10000)
127
+ throw new NotSupportedError("PDF fonts require a static TrueType .ttf with glyf outlines; collections, CFF and WOFF are unsupported.");
128
+ const count = reader.u16(4);
129
+ if (!count || count > 256)
130
+ throw new TypeError("Invalid TrueType table count.");
131
+ reader.range(12, count * 16);
132
+ for (let i = 0; i < count; i++) {
133
+ const p = 12 + i * 16, tag = reader.tag(p), offset = reader.u32(p + 8), length = reader.u32(p + 12);
134
+ reader.range(offset, length);
135
+ if (tables.has(tag))
136
+ throw new TypeError("Duplicate TrueType table: " + tag);
137
+ tables.set(tag, { offset, length });
138
+ }
139
+ if (tables.has("fvar"))
140
+ throw new NotSupportedError("Instantiate a variable font as a static TrueType font before PDF export.");
141
+ const head = table("head", 54), maxp = table("maxp", 6), hhea = table("hhea", 36), os2 = table("OS/2", 10);
142
+ if (reader.u32(head.offset + 12) !== 0x5f0f3cf5)
143
+ throw new TypeError("Invalid TrueType head magic.");
144
+ const glyphCount = reader.u16(maxp.offset + 4);
145
+ const units = reader.u16(head.offset + 18);
146
+ if (!glyphCount || units < 16 || units > 16384)
147
+ throw new TypeError("Invalid TrueType metrics.");
148
+ const fsType = reader.u16(os2.offset + 8);
149
+ if (fsType & 0x202)
150
+ throw new NotSupportedError("The font's embedding permissions prohibit outline embedding.");
151
+ const canSubset = !(fsType & 0x100);
152
+ const scale = 1000 / units;
153
+ const ascent = reader.i16(hhea.offset + 4) * scale;
154
+ const descent = reader.i16(hhea.offset + 6) * scale;
155
+ const bbox = [36, 38, 40, 42].map(p => reader.i16(head.offset + p) * scale);
156
+ const metrics = reader.u16(hhea.offset + 34);
157
+ if (!metrics || metrics > glyphCount)
158
+ throw new TypeError("Invalid TrueType horizontal metric count.");
159
+ const hmtx = table("hmtx", metrics * 4 + (glyphCount - metrics) * 2);
160
+ const widths = [];
161
+ for (let i = 0; i < glyphCount; i++) {
162
+ widths.push(reader.u16(hmtx.offset + Math.min(i, metrics - 1) * 4) * scale);
163
+ if ((i & 1023) === 1023)
164
+ yield;
165
+ }
166
+ const format = reader.i16(head.offset + 50);
167
+ if (format !== 0 && format !== 1)
168
+ throw new TypeError("Unsupported TrueType loca index.");
169
+ const loca = table("loca", (glyphCount + 1) * (format ? 4 : 2)), glyf = table("glyf");
170
+ const offsets = [];
171
+ for (let i = 0; i <= glyphCount; i++) {
172
+ offsets.push(format ? reader.u32(loca.offset + i * 4) : reader.u16(loca.offset + i * 2) * 2);
173
+ if ((i & 1023) === 1023)
174
+ yield;
175
+ }
176
+ for (let i = 0; i <= glyphCount; i++)
177
+ if (offsets[i] > glyf.length || (i && offsets[i] < offsets[i - 1]))
178
+ throw new TypeError("Invalid TrueType glyph offsets.");
179
+ const cmap = table("cmap", 4), maps = [], validated = new Set();
180
+ const encodings = reader.u16(cmap.offset + 2);
181
+ if (4 + encodings * 8 > cmap.length)
182
+ throw new TypeError("Invalid TrueType cmap directory.");
183
+ for (let i = 0; i < encodings; i++) {
184
+ const p = cmap.offset + 4 + i * 8, platform = reader.u16(p), encoding = reader.u16(p + 2), relative = reader.u32(p + 4);
185
+ if (platform !== 0 && !(platform === 3 && (encoding === 1 || encoding === 10)))
186
+ continue;
187
+ if (relative + 4 > cmap.length)
188
+ throw new TypeError("Invalid cmap subtable offset.");
189
+ const offset = cmap.offset + relative, type = reader.u16(offset);
190
+ if (type !== 4 && type !== 12)
191
+ continue;
192
+ // Encoding records may share a Unicode subtable; validate its contents once.
193
+ if (validated.has(offset))
194
+ continue;
195
+ if (type === 12 && relative + 16 > cmap.length)
196
+ throw new TypeError("Truncated cmap format 12.");
197
+ const length = type === 12 ? reader.u32(offset + 4) : reader.u16(offset + 2);
198
+ if (length < (type === 12 ? 16 : 16) || relative + length > cmap.length)
199
+ throw new TypeError("Invalid cmap length.");
200
+ if (type === 12) {
201
+ const groups = reader.u32(offset + 12);
202
+ if (16 + groups * 12 > length)
203
+ throw new TypeError("Invalid cmap groups.");
204
+ let previous = -1;
205
+ for (let j = 0; j < groups; j++) {
206
+ const q = offset + 16 + j * 12, start = reader.u32(q), end = reader.u32(q + 4), glyph = reader.u32(q + 8);
207
+ if (start <= previous || start > end || end > 0x10ffff || glyph + end - start >= glyphCount)
208
+ throw new TypeError("Invalid cmap scalar/glyph group.");
209
+ previous = end;
210
+ if ((j & 1023) === 1023)
211
+ yield;
212
+ }
213
+ }
214
+ else {
215
+ const segments = reader.u16(offset + 6) / 2;
216
+ if (!Number.isInteger(segments) || !segments || 16 + segments * 8 > length)
217
+ throw new TypeError("Invalid cmap segments.");
218
+ let previous = -1;
219
+ for (let j = 0; j < segments; j++) {
220
+ const end = reader.u16(offset + 14 + j * 2), start = reader.u16(offset + 16 + segments * 2 + j * 2);
221
+ if (end <= previous || start > end)
222
+ throw new TypeError("Invalid cmap segment ordering.");
223
+ previous = end;
224
+ if ((j & 1023) === 1023)
225
+ yield;
226
+ }
227
+ }
228
+ validated.add(offset);
229
+ maps.push({ offset, length });
230
+ }
231
+ const cmaps = maps.sort((a, b) => reader.u16(b.offset) - reader.u16(a.offset));
232
+ if (!maps.length)
233
+ throw new NotSupportedError("TrueType font needs a Unicode cmap format 4 or 12.");
234
+ return { tables, glyphCount, units, ascent, descent, bbox, widths, offsets, canSubset, cmaps };
235
+ }
@@ -0,0 +1,169 @@
1
+ import type { Column, ExportValue, CellPresentation, CellValue, ColumnValueContext, StreamOptions, ExportLimits, TableSpanRows } from "../core/index.js";
2
+ import type { PdfFont } from "./font.js";
3
+ import type { PdfTextRun } from "./inline-types.js";
4
+ import type { PdfTextDirection, PdfTextShaper } from "./text-shaping-types.js";
5
+ import type { BooleanText } from "../core/value-text.js";
6
+ /** Readable horizontal page bands. Every band in a window contains the same logical row fragments. */
7
+ export interface PdfTableBands {
8
+ readonly mode: "bands";
9
+ /** Minimum point width when columnWidths is absent. Default 72; shared Column.width can increase it. */
10
+ readonly minimumColumnWidth?: number;
11
+ /** Sorted unique zero-based source indices, repeated as a prefix on every band. Grouped tables include their leading label column.
12
+ * Repeated cell contents must fit below the headings/context on one fresh page. */
13
+ readonly repeatColumns?: readonly number[];
14
+ }
15
+ /** PDF lengths are points (72 points per inch). */
16
+ export interface PdfPageSize {
17
+ readonly width: number;
18
+ readonly height: number;
19
+ }
20
+ export interface PdfMargins {
21
+ readonly top: number;
22
+ readonly right: number;
23
+ readonly bottom: number;
24
+ readonly left: number;
25
+ }
26
+ export interface PdfFonts {
27
+ readonly regular: PdfFont;
28
+ readonly bold?: PdfFont;
29
+ readonly italic?: PdfFont;
30
+ readonly boldItalic?: PdfFont;
31
+ }
32
+ /** Font family and emphasis selection for one text run; omitted fonts use standard Helvetica. */
33
+ export interface PdfTextPreflightOptions {
34
+ readonly fonts?: PdfFonts;
35
+ readonly bold?: boolean;
36
+ readonly italic?: boolean;
37
+ }
38
+ /** Opt-in logical structure for the supported report profile; does not assert PDF/UA conformance. */
39
+ export interface PdfTaggedOptions {
40
+ readonly language: string;
41
+ }
42
+ /** Stable reason that the current scalar-layout writer cannot render a text run. */
43
+ export type PdfTextIssueCode = "unpaired-surrogate" | "unsupported-control" | "unsupported-layout" | "embedded-font-required" | "missing-glyph";
44
+ /** Text support alone does not establish layout, resource-budget or accessibility success. */
45
+ export type PdfTextPreflightResult = {
46
+ readonly supported: true;
47
+ } | {
48
+ readonly supported: false;
49
+ readonly code: PdfTextIssueCode;
50
+ /** Zero-based UTF-16 index in the original input, before line-break and tab normalization. */
51
+ readonly index: number;
52
+ /** Original Unicode scalar; absent when the issue is an unpaired surrogate. */
53
+ readonly scalar?: number;
54
+ readonly message: string;
55
+ };
56
+ export interface PdfLimits extends ExportLimits {
57
+ readonly maxPages?: number;
58
+ readonly maxColumns?: number;
59
+ /** Horizontal bands in one table; includes repeated identifier columns. Default 64. */
60
+ readonly maxTableBands?: number;
61
+ /** Retained logical row fragments in a band window. Smaller values flush earlier. Default 256. */
62
+ readonly maxTableWindowRows?: number;
63
+ /** Bounds one projected cell before wrapping; text is never truncated. */
64
+ readonly maxCellCharacters?: number;
65
+ /** Captured styled runs per paragraph/cell, and in a kept paragraph group. */
66
+ readonly maxTextRuns?: number;
67
+ /** Paragraph count in a group or kept chain. */
68
+ readonly maxGroupBlocks?: number;
69
+ /** Bounds one row's wrapped layout, including a row split over many pages. */
70
+ readonly maxRowLines?: number;
71
+ readonly maxFontBytes?: number;
72
+ readonly maxPageBytes?: number;
73
+ /** Actual link annotations, including repeated headings and continued cell fragments. */
74
+ readonly maxHyperlinks?: number;
75
+ /** Total source bytes of distinct image objects retained by a composed document. */
76
+ readonly maxImageBytes?: number;
77
+ /** Per-image decoded pixel ceiling, checked before allocating RGB or alpha planes. */
78
+ readonly maxImagePixels?: number;
79
+ /** Optional ceiling for owned image input/decoded/scanline buffers; excludes native codec memory and compressed output. */
80
+ readonly maxImageWorkingBytes?: number;
81
+ /** Total path commands across a composed document, checked before serializing a block. */
82
+ readonly maxVectorCommands?: number;
83
+ /** Total path, text and group elements across portable drawing blocks. */
84
+ readonly maxDrawingElements?: number;
85
+ /** Total logical structure elements created across a tagged document, including Document, rows and cells. */
86
+ readonly maxStructureElements?: number;
87
+ /** Total marked-content references created across the document, including continued cells. */
88
+ readonly maxMarkedContentReferences?: number;
89
+ /** Section boundaries across one composed document. */
90
+ readonly maxSections?: number;
91
+ /** Native outline entries across one composed document. */
92
+ readonly maxBookmarks?: number;
93
+ /** Streamed list items across one composed document. */
94
+ readonly maxListItems?: number;
95
+ /** Captured named paragraph styles in a composed document. */
96
+ readonly maxParagraphStyles?: number;
97
+ }
98
+ export type PdfTotal = "sum" | "count" | "average" | "min" | "max";
99
+ export interface PdfTableFooter {
100
+ readonly values?: readonly ExportValue[];
101
+ /** Keys are declared column keys, or unique headers when a key is absent. */
102
+ readonly totals?: Readonly<Record<string, PdfTotal>>;
103
+ /** Structured alternative to values/totals; includes horizontal and vertical spans. */
104
+ readonly rows?: TableSpanRows;
105
+ }
106
+ export interface PdfPageContext {
107
+ readonly pageNumber: number;
108
+ /** One-based section number; an unsectioned document uses section one. */
109
+ readonly sectionNumber: number;
110
+ /** One-based physical page within the current section. */
111
+ readonly sectionPageNumber: number;
112
+ readonly sectionTitle?: string;
113
+ }
114
+ /** Literal text around the current/total page counts; no locale is inferred. */
115
+ export interface PdfPageNumberLabels {
116
+ /** Text before the current page number. Default "Page ". Empty text is valid. */
117
+ readonly prefix?: string;
118
+ /** Text between the current and total page numbers. Default " of ". */
119
+ readonly separator?: string;
120
+ /** Text after the total page count. Default empty. */
121
+ readonly suffix?: string;
122
+ }
123
+ export interface PdfOptions<T = never> extends Omit<StreamOptions, "limits"> {
124
+ /** Optional provider for composed paragraphs and table cells. Legacy decorations remain scalar text. */
125
+ readonly textShaper?: PdfTextShaper;
126
+ /** Base direction of each shaped physical paragraph; defaults to auto. */
127
+ readonly textDirection?: PdfTextDirection;
128
+ readonly columns: readonly Column<T>[];
129
+ readonly limits?: PdfLimits;
130
+ readonly pageSize?: "A3" | "A4" | "A5" | "LETTER" | "LEGAL" | "TABLOID" | PdfPageSize;
131
+ readonly orientation?: "portrait" | "landscape";
132
+ readonly margins?: number | Partial<PdfMargins>;
133
+ /** Standard Helvetica is used when omitted. Supply TrueType fonts for Unicode text. */
134
+ readonly fonts?: PdfFonts;
135
+ /** Logical reading order, simple table semantics and explicit figure alternatives. */
136
+ readonly tagged?: PdfTaggedOptions;
137
+ readonly fontSize?: number;
138
+ /** First-page title. Empty text is omitted without reserving space or consuming a cell. */
139
+ readonly title?: string;
140
+ /** Text before the first table heading. Empty text is omitted. */
141
+ readonly messageTop?: string;
142
+ /** Text after the table. Empty text is omitted. */
143
+ readonly messageBottom?: string;
144
+ readonly includeHeader?: boolean;
145
+ /** Overrides Column.groups/headers with a small explicit span matrix, repeated on each page. */
146
+ readonly headerRows?: TableSpanRows;
147
+ readonly footer?: PdfTableFooter;
148
+ readonly headerPresentation?: CellPresentation;
149
+ readonly footerPresentation?: CellPresentation;
150
+ readonly alternateRowColor?: string;
151
+ readonly padding?: number;
152
+ /** Explicit point widths. Otherwise shared Column.width is measured in zero-character widths. */
153
+ readonly columnWidths?: readonly number[];
154
+ /** Fit preserves every column at the requested font size by wrapping. Reject requires widths to fit. */
155
+ readonly wideTable?: "fit" | "reject" | PdfTableBands;
156
+ readonly pageHeader?: string | ((context: PdfPageContext) => string);
157
+ readonly pageFooter?: string | ((context: PdfPageContext) => string);
158
+ readonly pageNumbers?: boolean;
159
+ /** Captured page-number literals. English defaults preserve the existing display contract. */
160
+ readonly pageNumberLabels?: PdfPageNumberLabels;
161
+ /** Explicit boolean display labels. Defaults are the existing English True/False text. */
162
+ readonly booleanText?: BooleanText;
163
+ /** Prefer ExportCell.text for presentation, then this formatter, then ISO/scalar text. */
164
+ readonly formatValue?: (value: CellValue, context: ColumnValueContext) => string;
165
+ /** Styled display runs when ExportCell.text is absent; must return synchronously. */
166
+ readonly formatRuns?: (value: CellValue, context: ColumnValueContext) => readonly PdfTextRun[];
167
+ /** Native deflate when available; the fallback writes valid uncompressed PDF streams. */
168
+ readonly compression?: boolean;
169
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,6 @@
1
+ import type { ExportResult, OutputDestination } from "../core/index.js";
2
+ import type { PdfOptions } from "./types.js";
3
+ /** One table as a PDF Blob. Async sources are consumed once; call a source factory for each export. */
4
+ export declare function writePdf<T extends object>(rows: Iterable<T> | AsyncIterable<T>, options: PdfOptions<NoInfer<T>>): Promise<Blob>;
5
+ /** Stream complete pages with backpressure. The destination remains caller-owned, including partial bytes on failure. */
6
+ export declare function writePdfTo<T extends object>(rows: Iterable<T> | AsyncIterable<T>, destination: OutputDestination, options: PdfOptions<NoInfer<T>>): Promise<ExportResult>;