@evotecit/officeimo 0.0.0-stage → 0.1.1
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.
- package/LICENSE +21 -0
- package/README.md +1222 -2
- package/bundles/officeimo-canopyx.js +11741 -0
- package/bundles/officeimo-canopyx.mjs +11639 -0
- package/bundles/officeimo-csv.js +1264 -0
- package/bundles/officeimo-csv.mjs +1239 -0
- package/bundles/officeimo-datatables.js +12018 -0
- package/bundles/officeimo-datatables.mjs +11915 -0
- package/bundles/officeimo-execution.js +1513 -0
- package/bundles/officeimo-execution.mjs +1489 -0
- package/bundles/officeimo-pdf.js +7009 -0
- package/bundles/officeimo-pdf.mjs +6946 -0
- package/bundles/officeimo-xlsx.js +5344 -0
- package/bundles/officeimo-xlsx.mjs +5290 -0
- package/bundles/officeimo.js +11468 -0
- package/bundles/officeimo.mjs +11369 -0
- package/dist/core/dates.d.ts +1 -0
- package/dist/core/dates.js +16 -0
- package/dist/core/errors.d.ts +12 -0
- package/dist/core/errors.js +31 -0
- package/dist/core/grouped-report.d.ts +87 -0
- package/dist/core/grouped-report.js +252 -0
- package/dist/core/index.d.ts +74 -0
- package/dist/core/index.js +37 -0
- package/dist/core/iteration.d.ts +7 -0
- package/dist/core/iteration.js +149 -0
- package/dist/core/limits.d.ts +7 -0
- package/dist/core/limits.js +56 -0
- package/dist/core/links.d.ts +5 -0
- package/dist/core/links.js +14 -0
- package/dist/core/presentation.d.ts +29 -0
- package/dist/core/presentation.js +59 -0
- package/dist/core/progress.d.ts +1 -0
- package/dist/core/progress.js +10 -0
- package/dist/core/sinks.d.ts +33 -0
- package/dist/core/sinks.js +102 -0
- package/dist/core/table.d.ts +8 -0
- package/dist/core/table.js +1 -0
- package/dist/core/value-text.d.ts +7 -0
- package/dist/core/value-text.js +13 -0
- package/dist/csv/index.d.ts +44 -0
- package/dist/csv/index.js +133 -0
- package/dist/csv/options.d.ts +1 -0
- package/dist/csv/options.js +8 -0
- package/dist/csv/structured-rows.d.ts +3 -0
- package/dist/csv/structured-rows.js +15 -0
- package/dist/execution/host.d.ts +5 -0
- package/dist/execution/host.js +164 -0
- package/dist/execution/index.d.ts +3 -0
- package/dist/execution/index.js +2 -0
- package/dist/execution/protocol.d.ts +48 -0
- package/dist/execution/protocol.js +173 -0
- package/dist/execution/types.d.ts +42 -0
- package/dist/execution/types.js +1 -0
- package/dist/execution/worker.d.ts +3 -0
- package/dist/execution/worker.js +147 -0
- package/dist/index.d.ts +32 -0
- package/dist/index.js +14 -0
- package/dist/integrations/canopyx/dates.d.ts +1 -0
- package/dist/integrations/canopyx/dates.js +34 -0
- package/dist/integrations/canopyx/index.d.ts +10 -0
- package/dist/integrations/canopyx/index.js +45 -0
- package/dist/integrations/canopyx/source.d.ts +3 -0
- package/dist/integrations/canopyx/source.js +122 -0
- package/dist/integrations/canopyx/types.d.ts +83 -0
- package/dist/integrations/canopyx/types.js +1 -0
- package/dist/integrations/datatables/api.d.ts +1 -0
- package/dist/integrations/datatables/api.js +27 -0
- package/dist/integrations/datatables/headings.d.ts +1 -0
- package/dist/integrations/datatables/headings.js +65 -0
- package/dist/integrations/datatables/index.d.ts +12 -0
- package/dist/integrations/datatables/index.js +167 -0
- package/dist/integrations/datatables/snapshot.d.ts +3 -0
- package/dist/integrations/datatables/snapshot.js +206 -0
- package/dist/integrations/datatables/types.d.ts +98 -0
- package/dist/integrations/datatables/types.js +1 -0
- package/dist/integrations/options.d.ts +13 -0
- package/dist/integrations/options.js +34 -0
- package/dist/internal/capture.d.ts +4 -0
- package/dist/internal/capture.js +14 -0
- package/dist/internal/columns.d.ts +27 -0
- package/dist/internal/columns.js +1 -0
- package/dist/internal/crc32.d.ts +6 -0
- package/dist/internal/crc32.js +17 -0
- package/dist/internal/export-values.d.ts +3 -0
- package/dist/internal/export-values.js +14 -0
- package/dist/internal/rows.d.ts +6 -0
- package/dist/internal/rows.js +77 -0
- package/dist/internal/table-spans.d.ts +11 -0
- package/dist/internal/table-spans.js +58 -0
- package/dist/internal/totals.d.ts +15 -0
- package/dist/internal/totals.js +42 -0
- package/dist/opc/index.d.ts +67 -0
- package/dist/opc/index.js +233 -0
- package/dist/opc/properties.d.ts +15 -0
- package/dist/opc/properties.js +18 -0
- package/dist/opc/uri.d.ts +5 -0
- package/dist/opc/uri.js +41 -0
- package/dist/pdf/annotations.d.ts +1 -0
- package/dist/pdf/annotations.js +38 -0
- package/dist/pdf/binary.d.ts +6 -0
- package/dist/pdf/binary.js +24 -0
- package/dist/pdf/document-drawings.d.ts +15 -0
- package/dist/pdf/document-drawings.js +237 -0
- package/dist/pdf/document-grouped.d.ts +1 -0
- package/dist/pdf/document-grouped.js +244 -0
- package/dist/pdf/document-images.d.ts +19 -0
- package/dist/pdf/document-images.js +125 -0
- package/dist/pdf/document-types.d.ts +160 -0
- package/dist/pdf/document-types.js +1 -0
- package/dist/pdf/document-vectors.d.ts +11 -0
- package/dist/pdf/document-vectors.js +77 -0
- package/dist/pdf/document.d.ts +9 -0
- package/dist/pdf/document.js +270 -0
- package/dist/pdf/drawing-types.d.ts +53 -0
- package/dist/pdf/drawing-types.js +1 -0
- package/dist/pdf/font-resources.d.ts +43 -0
- package/dist/pdf/font-resources.js +202 -0
- package/dist/pdf/font-support.d.ts +1 -0
- package/dist/pdf/font-support.js +60 -0
- package/dist/pdf/font.d.ts +17 -0
- package/dist/pdf/font.js +56 -0
- package/dist/pdf/grouped-table.d.ts +6 -0
- package/dist/pdf/grouped-table.js +40 -0
- package/dist/pdf/grouped-types.d.ts +34 -0
- package/dist/pdf/grouped-types.js +1 -0
- package/dist/pdf/grouped.d.ts +10 -0
- package/dist/pdf/grouped.js +37 -0
- package/dist/pdf/index.d.ts +22 -0
- package/dist/pdf/index.js +8 -0
- package/dist/pdf/inline-text.d.ts +30 -0
- package/dist/pdf/inline-text.js +183 -0
- package/dist/pdf/inline-types.d.ts +15 -0
- package/dist/pdf/inline-types.js +1 -0
- package/dist/pdf/list-layout.d.ts +6 -0
- package/dist/pdf/list-layout.js +49 -0
- package/dist/pdf/objects.d.ts +2 -0
- package/dist/pdf/objects.js +320 -0
- package/dist/pdf/outlines.d.ts +1 -0
- package/dist/pdf/outlines.js +55 -0
- package/dist/pdf/page-context.d.ts +1 -0
- package/dist/pdf/page-context.js +12 -0
- package/dist/pdf/pages.d.ts +1 -0
- package/dist/pdf/pages.js +678 -0
- package/dist/pdf/paragraph-layout.d.ts +21 -0
- package/dist/pdf/paragraph-layout.js +71 -0
- package/dist/pdf/paragraph-styles.d.ts +1 -0
- package/dist/pdf/paragraph-styles.js +66 -0
- package/dist/pdf/png.d.ts +9 -0
- package/dist/pdf/png.js +259 -0
- package/dist/pdf/preflight.d.ts +7 -0
- package/dist/pdf/preflight.js +32 -0
- package/dist/pdf/presentation-settings.d.ts +5 -0
- package/dist/pdf/presentation-settings.js +28 -0
- package/dist/pdf/providers/harfbuzz.d.ts +60 -0
- package/dist/pdf/providers/harfbuzz.js +112 -0
- package/dist/pdf/providers/unicode13-profile.d.ts +2 -0
- package/dist/pdf/providers/unicode13-profile.js +115 -0
- package/dist/pdf/section-types.d.ts +15 -0
- package/dist/pdf/section-types.js +40 -0
- package/dist/pdf/session.d.ts +25 -0
- package/dist/pdf/session.js +55 -0
- package/dist/pdf/settings.d.ts +17 -0
- package/dist/pdf/settings.js +196 -0
- package/dist/pdf/shaped-text.d.ts +5 -0
- package/dist/pdf/shaped-text.js +205 -0
- package/dist/pdf/standard-widths.d.ts +1 -0
- package/dist/pdf/standard-widths.js +317 -0
- package/dist/pdf/structure-parent-tree.d.ts +1 -0
- package/dist/pdf/structure-parent-tree.js +57 -0
- package/dist/pdf/structure.d.ts +6 -0
- package/dist/pdf/structure.js +302 -0
- package/dist/pdf/table-band-flow.d.ts +34 -0
- package/dist/pdf/table-band-flow.js +129 -0
- package/dist/pdf/table-band-plan.d.ts +10 -0
- package/dist/pdf/table-band-plan.js +83 -0
- package/dist/pdf/table-layout.d.ts +75 -0
- package/dist/pdf/table-layout.js +217 -0
- package/dist/pdf/table-structure.d.ts +1 -0
- package/dist/pdf/table-structure.js +108 -0
- package/dist/pdf/text-shaping-types.d.ts +39 -0
- package/dist/pdf/text-shaping-types.js +1 -0
- package/dist/pdf/text.d.ts +13 -0
- package/dist/pdf/text.js +101 -0
- package/dist/pdf/truetype-subset.d.ts +5 -0
- package/dist/pdf/truetype-subset.js +217 -0
- package/dist/pdf/truetype.d.ts +1 -0
- package/dist/pdf/truetype.js +235 -0
- package/dist/pdf/types.d.ts +171 -0
- package/dist/pdf/types.js +1 -0
- package/dist/pdf/write.d.ts +6 -0
- package/dist/pdf/write.js +57 -0
- package/dist/pdf/xref.d.ts +1 -0
- package/dist/pdf/xref.js +30 -0
- package/dist/xlsx/attachments.d.ts +30 -0
- package/dist/xlsx/attachments.js +67 -0
- package/dist/xlsx/chart-bindings.d.ts +1 -0
- package/dist/xlsx/chart-bindings.js +122 -0
- package/dist/xlsx/chart-types.d.ts +73 -0
- package/dist/xlsx/chart-types.js +1 -0
- package/dist/xlsx/chart-xml.d.ts +2 -0
- package/dist/xlsx/chart-xml.js +103 -0
- package/dist/xlsx/charts.d.ts +1 -0
- package/dist/xlsx/charts.js +159 -0
- package/dist/xlsx/conditional-formatting.d.ts +1 -0
- package/dist/xlsx/conditional-formatting.js +158 -0
- package/dist/xlsx/conditional-types.d.ts +64 -0
- package/dist/xlsx/conditional-types.js +1 -0
- package/dist/xlsx/data-row-ranges.d.ts +5 -0
- package/dist/xlsx/data-row-ranges.js +39 -0
- package/dist/xlsx/differential-styles.d.ts +1 -0
- package/dist/xlsx/differential-styles.js +102 -0
- package/dist/xlsx/formula.d.ts +8 -0
- package/dist/xlsx/formula.js +104 -0
- package/dist/xlsx/grouped-layout.d.ts +61 -0
- package/dist/xlsx/grouped-layout.js +79 -0
- package/dist/xlsx/grouped.d.ts +16 -0
- package/dist/xlsx/grouped.js +161 -0
- package/dist/xlsx/index.d.ts +24 -0
- package/dist/xlsx/index.js +10 -0
- package/dist/xlsx/layout.d.ts +1 -0
- package/dist/xlsx/layout.js +186 -0
- package/dist/xlsx/options.d.ts +1 -0
- package/dist/xlsx/options.js +16 -0
- package/dist/xlsx/portable.d.ts +27 -0
- package/dist/xlsx/portable.js +44 -0
- package/dist/xlsx/preservation.d.ts +1 -0
- package/dist/xlsx/preservation.js +60 -0
- package/dist/xlsx/ranges.d.ts +1 -0
- package/dist/xlsx/ranges.js +24 -0
- package/dist/xlsx/regions.d.ts +1 -0
- package/dist/xlsx/regions.js +97 -0
- package/dist/xlsx/structured-rows.d.ts +11 -0
- package/dist/xlsx/structured-rows.js +28 -0
- package/dist/xlsx/style-xml.d.ts +1 -0
- package/dist/xlsx/style-xml.js +6 -0
- package/dist/xlsx/styles.d.ts +70 -0
- package/dist/xlsx/styles.js +203 -0
- package/dist/xlsx/table.d.ts +12 -0
- package/dist/xlsx/table.js +35 -0
- package/dist/xlsx/types.d.ts +180 -0
- package/dist/xlsx/types.js +1 -0
- package/dist/xlsx/validation-types.d.ts +18 -0
- package/dist/xlsx/validation-types.js +1 -0
- package/dist/xlsx/validation.d.ts +1 -0
- package/dist/xlsx/validation.js +92 -0
- package/dist/xlsx/values.d.ts +13 -0
- package/dist/xlsx/values.js +87 -0
- package/dist/xlsx/workbook.d.ts +50 -0
- package/dist/xlsx/workbook.js +452 -0
- package/dist/xlsx/worksheet.d.ts +71 -0
- package/dist/xlsx/worksheet.js +700 -0
- package/dist/xlsx/write.d.ts +15 -0
- package/dist/xlsx/write.js +47 -0
- package/dist/xml/index.d.ts +24 -0
- package/dist/xml/index.js +74 -0
- package/dist/zip/entry.d.ts +39 -0
- package/dist/zip/entry.js +122 -0
- package/dist/zip/index.d.ts +44 -0
- package/dist/zip/index.js +248 -0
- package/package.json +104 -4
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
import { checkAbort, withAbort, pause, taskYieldDue } from "../core/iteration.js";
|
|
2
|
+
import { OfficeIMOError } from "../core/errors.js";
|
|
3
|
+
import { pdfXrefChunks, pdfXrefEntryBytes } from "./xref.js";
|
|
4
|
+
const compactObjectCount = 256, compactObjectBytes = 32768;
|
|
5
|
+
const compactIndexChunkEntries = 4096;
|
|
6
|
+
/** Lazy Uint16 chunks store compact membership; untouched zero slots denote ordinary objects. */
|
|
7
|
+
class PdfCompactIndices {
|
|
8
|
+
count;
|
|
9
|
+
chunks = [];
|
|
10
|
+
constructor(count) {
|
|
11
|
+
this.count = count;
|
|
12
|
+
}
|
|
13
|
+
get length() { return this.count; }
|
|
14
|
+
reserve() { this.count++; }
|
|
15
|
+
set(id, member) {
|
|
16
|
+
const chunk = this.chunks[Math.floor(id / compactIndexChunkEntries)] ??= new Uint16Array(compactIndexChunkEntries);
|
|
17
|
+
// The current batch cap permits indices 0..255; adding one preserves index 255 as 256.
|
|
18
|
+
chunk[id % compactIndexChunkEntries] = member + 1;
|
|
19
|
+
}
|
|
20
|
+
at(id) {
|
|
21
|
+
const encoded = this.chunks[Math.floor(id / compactIndexChunkEntries)]?.[id % compactIndexChunkEntries] ?? 0;
|
|
22
|
+
return encoded ? encoded - 1 : undefined;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
export function pdfNumber(value) {
|
|
26
|
+
if (!Number.isFinite(value) || Math.abs(value) > 1e9)
|
|
27
|
+
throw new RangeError("PDF coordinates and metrics must be finite and bounded.");
|
|
28
|
+
return String(Math.round(value * 1000) / 1000);
|
|
29
|
+
}
|
|
30
|
+
export function unicodeHex(text) {
|
|
31
|
+
let hex = "";
|
|
32
|
+
for (let i = 0; i < text.length; i++) {
|
|
33
|
+
const code = text.charCodeAt(i);
|
|
34
|
+
if (code >= 0xd800 && code <= 0xdbff && !(text.charCodeAt(i + 1) >= 0xdc00 && text.charCodeAt(i + 1) <= 0xdfff) ||
|
|
35
|
+
code >= 0xdc00 && code <= 0xdfff && !(text.charCodeAt(i - 1) >= 0xd800 && text.charCodeAt(i - 1) <= 0xdbff))
|
|
36
|
+
throw new TypeError("PDF text contains an unpaired UTF-16 surrogate.");
|
|
37
|
+
hex += code.toString(16).padStart(4, "0");
|
|
38
|
+
}
|
|
39
|
+
return hex;
|
|
40
|
+
}
|
|
41
|
+
/** @internal Forward-only PDF objects. Completed objects retain only their xref location. */
|
|
42
|
+
export class PdfObjects {
|
|
43
|
+
sink;
|
|
44
|
+
signal;
|
|
45
|
+
maxBytes;
|
|
46
|
+
compressObjects;
|
|
47
|
+
offsets = [0];
|
|
48
|
+
// A defined index changes the corresponding full-width offset into an object-stream number.
|
|
49
|
+
// Membership chunks are allocated only when a compact member needs their ID range.
|
|
50
|
+
compactIndices;
|
|
51
|
+
compactBatch;
|
|
52
|
+
compactArena;
|
|
53
|
+
compactUnavailable = false;
|
|
54
|
+
encoder = new TextEncoder();
|
|
55
|
+
buffer = new Uint8Array(32768);
|
|
56
|
+
buffered = 0;
|
|
57
|
+
bytes = 0;
|
|
58
|
+
constructor(sink, signal, maxBytes = Number.MAX_SAFE_INTEGER, compressObjects = false) {
|
|
59
|
+
this.sink = sink;
|
|
60
|
+
this.signal = signal;
|
|
61
|
+
this.maxBytes = maxBytes;
|
|
62
|
+
this.compressObjects = compressObjects;
|
|
63
|
+
}
|
|
64
|
+
reserve() { this.offsets.push(undefined); this.compactIndices?.reserve(); return this.offsets.length - 1; }
|
|
65
|
+
async raw(bytes) {
|
|
66
|
+
checkAbort(this.signal);
|
|
67
|
+
if (!Number.isSafeInteger(this.bytes + bytes.length) || this.bytes + bytes.length > this.maxBytes)
|
|
68
|
+
throw new OfficeIMOError("RESOURCE_LIMIT", "maxOutputBytes exceeded.");
|
|
69
|
+
this.bytes += bytes.length;
|
|
70
|
+
for (let offset = 0; offset < bytes.length;) {
|
|
71
|
+
const n = Math.min(this.buffer.length - this.buffered, bytes.length - offset);
|
|
72
|
+
this.buffer.set(bytes.subarray(offset, offset + n), this.buffered);
|
|
73
|
+
offset += n;
|
|
74
|
+
this.buffered += n;
|
|
75
|
+
if (this.buffered === this.buffer.length)
|
|
76
|
+
await this.flushBytes();
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
async text(text) { await this.raw(this.encoder.encode(text)); }
|
|
80
|
+
async flush() {
|
|
81
|
+
await this.flushCompact();
|
|
82
|
+
await this.flushBytes();
|
|
83
|
+
}
|
|
84
|
+
/** Publish completed page bytes without forcing a partial structure container to end. */
|
|
85
|
+
async flushBytes() {
|
|
86
|
+
if (this.buffered) {
|
|
87
|
+
const bytes = this.buffer.subarray(0, this.buffered);
|
|
88
|
+
await withAbort(Promise.resolve(this.sink.write(bytes)), this.signal);
|
|
89
|
+
// The sink borrows this view until acceptance; failed or cancelled writes must not reuse it.
|
|
90
|
+
this.buffered = 0;
|
|
91
|
+
}
|
|
92
|
+
if (taskYieldDue())
|
|
93
|
+
await pause();
|
|
94
|
+
checkAbort(this.signal);
|
|
95
|
+
}
|
|
96
|
+
/** @internal Append only when an existing compact batch can accept a member without async work. */
|
|
97
|
+
tryCompactObject(id, body) {
|
|
98
|
+
checkAbort(this.signal);
|
|
99
|
+
this.validateObject(id);
|
|
100
|
+
const batch = this.compactBatch;
|
|
101
|
+
if (!batch || !this.compressObjects || this.compactUnavailable || typeof CompressionStream !== "function" ||
|
|
102
|
+
body.length > compactObjectBytes || batch.ids.length + 1 >= compactObjectCount)
|
|
103
|
+
return false;
|
|
104
|
+
const indexBytes = String(id).length + String(batch.bodyBytes).length + 2;
|
|
105
|
+
const written = this.encodeCompactBody(body, batch.bodyBytes, compactObjectBytes - batch.indexBytes - indexBytes - batch.bodyBytes - 2);
|
|
106
|
+
if (written === undefined)
|
|
107
|
+
return false;
|
|
108
|
+
this.storeCompactBody(batch, id, written);
|
|
109
|
+
return true;
|
|
110
|
+
}
|
|
111
|
+
/** Small logical structure values share one bounded object-stream container. Other callers keep
|
|
112
|
+
* ordinary objects; unavailable deflate preserves the classic representation before packing starts. */
|
|
113
|
+
async compactObject(id, body) {
|
|
114
|
+
checkAbort(this.signal);
|
|
115
|
+
this.validateObject(id);
|
|
116
|
+
if (!this.compressObjects || this.compactUnavailable || body.length > compactObjectBytes)
|
|
117
|
+
return this.object(id, body);
|
|
118
|
+
if (typeof CompressionStream !== "function") {
|
|
119
|
+
if (this.compactIndices)
|
|
120
|
+
throw new Error("PDF object compression became unavailable.");
|
|
121
|
+
this.compactUnavailable = true;
|
|
122
|
+
return this.object(id, body);
|
|
123
|
+
}
|
|
124
|
+
const firstIndexBytes = String(id).length + 4;
|
|
125
|
+
let batch = this.compactBatch;
|
|
126
|
+
const indexBytes = String(id).length + String(batch?.bodyBytes ?? 0).length + 2;
|
|
127
|
+
let written = batch && batch.ids.length < compactObjectCount ? this.encodeCompactBody(body, batch.bodyBytes, compactObjectBytes - batch.indexBytes - indexBytes - batch.bodyBytes - 2) : undefined;
|
|
128
|
+
if (batch && written === undefined) {
|
|
129
|
+
// Partial encodings only touch the uncommitted suffix. Flush the committed prefix and retry
|
|
130
|
+
// the whole string so a multibyte scalar is never split between containers.
|
|
131
|
+
await this.flushCompact();
|
|
132
|
+
batch = undefined;
|
|
133
|
+
checkAbort(this.signal);
|
|
134
|
+
}
|
|
135
|
+
if (!batch) {
|
|
136
|
+
written = this.encodeCompactBody(body, 0, compactObjectBytes - firstIndexBytes - 1);
|
|
137
|
+
if (written === undefined)
|
|
138
|
+
return this.object(id, body);
|
|
139
|
+
let compressor;
|
|
140
|
+
// Probe before selecting the compact representation. Later batches need no idle native
|
|
141
|
+
// codec while they accumulate, including while ordinary page streams are compressed.
|
|
142
|
+
if (!this.compactIndices) {
|
|
143
|
+
try {
|
|
144
|
+
compressor = new CompressionStream("deflate");
|
|
145
|
+
}
|
|
146
|
+
catch {
|
|
147
|
+
this.compactUnavailable = true;
|
|
148
|
+
return this.object(id, body);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
batch = { id: this.reserve(), compressor, ids: [], bodyOffsets: [], bodyBytes: 0, indexBytes: 1 };
|
|
152
|
+
this.compactIndices ??= new PdfCompactIndices(this.offsets.length);
|
|
153
|
+
this.compactBatch = batch;
|
|
154
|
+
}
|
|
155
|
+
this.storeCompactBody(batch, id, written);
|
|
156
|
+
if (batch.ids.length === compactObjectCount)
|
|
157
|
+
await this.flushCompact();
|
|
158
|
+
}
|
|
159
|
+
storeCompactBody(batch, id, written) {
|
|
160
|
+
this.offsets[id] = batch.id;
|
|
161
|
+
this.compactIndices.set(id, batch.ids.length);
|
|
162
|
+
batch.indexBytes += String(id).length + String(batch.bodyBytes).length + 2;
|
|
163
|
+
batch.ids.push(id);
|
|
164
|
+
batch.bodyOffsets.push(batch.bodyBytes);
|
|
165
|
+
this.compactArena[batch.bodyBytes + written] = 10;
|
|
166
|
+
batch.bodyBytes += written + 1;
|
|
167
|
+
}
|
|
168
|
+
/** An incomplete encoding leaves only scratch bytes beyond the committed batch prefix. */
|
|
169
|
+
encodeCompactBody(body, offset, available) {
|
|
170
|
+
if (available < 0)
|
|
171
|
+
return undefined;
|
|
172
|
+
const destination = (this.compactArena ??= new Uint8Array(compactObjectBytes)).subarray(offset, offset + available);
|
|
173
|
+
if (typeof this.encoder.encodeInto === "function") {
|
|
174
|
+
const result = this.encoder.encodeInto(body, destination);
|
|
175
|
+
return result.read === body.length ? result.written : undefined;
|
|
176
|
+
}
|
|
177
|
+
// Preserve TextEncoder implementations that only expose encode().
|
|
178
|
+
const bytes = this.encoder.encode(body);
|
|
179
|
+
if (bytes.length > available)
|
|
180
|
+
return undefined;
|
|
181
|
+
destination.set(bytes);
|
|
182
|
+
return bytes.length;
|
|
183
|
+
}
|
|
184
|
+
async flushCompact() {
|
|
185
|
+
const batch = this.compactBatch;
|
|
186
|
+
if (!batch)
|
|
187
|
+
return;
|
|
188
|
+
this.compactBatch = undefined;
|
|
189
|
+
let index = "";
|
|
190
|
+
for (let member = 0; member < batch.ids.length; member++)
|
|
191
|
+
index += batch.ids[member] + " " + batch.bodyOffsets[member] + " ";
|
|
192
|
+
const header = this.encoder.encode(index + "\n"), count = batch.ids.length;
|
|
193
|
+
// Keep the index separate from the arena. Reuse waits for the compressed read or all raw
|
|
194
|
+
// writes below, so later members cannot overwrite a borrowed batch body.
|
|
195
|
+
const body = this.compactArena.subarray(0, batch.bodyBytes);
|
|
196
|
+
batch.ids.length = 0;
|
|
197
|
+
batch.bodyOffsets.length = 0;
|
|
198
|
+
if (batch.compressor) {
|
|
199
|
+
await this.compressedStream(batch.id, body, "/Type /ObjStm /N " + count + " /First " + header.length, batch.compressor, header);
|
|
200
|
+
}
|
|
201
|
+
else {
|
|
202
|
+
this.start(batch.id);
|
|
203
|
+
await this.text(batch.id + " 0 obj\n<< /Length " + (header.length + body.length) + " /Type /ObjStm /N " + count + " /First " + header.length + " >>\nstream\n");
|
|
204
|
+
await this.raw(header);
|
|
205
|
+
await this.raw(body);
|
|
206
|
+
await this.text("\nendstream\nendobj\n");
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
async object(id, body) {
|
|
210
|
+
this.start(id);
|
|
211
|
+
if (typeof body === "string") {
|
|
212
|
+
await this.text(id + " 0 obj\n" + body + "\nendobj\n");
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
// Logical parent arrays can be large. Coalesce small tokens without materializing the whole object.
|
|
216
|
+
let chunk = id + " 0 obj\n";
|
|
217
|
+
for (const part of body) {
|
|
218
|
+
for (let offset = 0; offset < part.length;) {
|
|
219
|
+
const end = Math.min(part.length, offset + 32768 - chunk.length);
|
|
220
|
+
chunk += part.slice(offset, end);
|
|
221
|
+
offset = end;
|
|
222
|
+
if (chunk.length === 32768) {
|
|
223
|
+
// Keep a trailing high surrogate with the next token, matching one-shot UTF-8 encoding.
|
|
224
|
+
const code = chunk.charCodeAt(chunk.length - 1), length = code >= 0xd800 && code <= 0xdbff ? chunk.length - 1 : chunk.length;
|
|
225
|
+
await this.text(chunk.slice(0, length));
|
|
226
|
+
chunk = chunk.slice(length);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
await this.text(chunk + "\nendobj\n");
|
|
231
|
+
}
|
|
232
|
+
validateObject(id) {
|
|
233
|
+
if (!Number.isSafeInteger(id) || id <= 0 || id >= this.offsets.length || this.offsets[id] !== undefined)
|
|
234
|
+
throw new Error("Invalid PDF object state.");
|
|
235
|
+
}
|
|
236
|
+
start(id) {
|
|
237
|
+
this.validateObject(id);
|
|
238
|
+
this.offsets[id] = this.bytes;
|
|
239
|
+
}
|
|
240
|
+
async stream(id, source, dictionary = "", compression = true, owned = false) {
|
|
241
|
+
const bytes = typeof source === "string" ? this.encoder.encode(source) : source;
|
|
242
|
+
let compressor;
|
|
243
|
+
if (compression && typeof CompressionStream === "function" && bytes.length) {
|
|
244
|
+
try {
|
|
245
|
+
compressor = new CompressionStream("deflate");
|
|
246
|
+
}
|
|
247
|
+
catch { /* PDF streams can remain uncompressed. */ }
|
|
248
|
+
}
|
|
249
|
+
if (!compressor) {
|
|
250
|
+
this.start(id);
|
|
251
|
+
await this.text(id + " 0 obj\n<< /Length " + bytes.length + " " + dictionary + " >>\nstream\n");
|
|
252
|
+
await this.raw(bytes);
|
|
253
|
+
await this.text("\nendstream\nendobj\n");
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
// Verified internal producers can transfer immutable ownership; other byte sources retain the defensive snapshot.
|
|
257
|
+
const sourceBytes = typeof source === "string" || owned ? bytes : new Uint8Array(bytes);
|
|
258
|
+
await this.compressedStream(id, sourceBytes, dictionary, compressor);
|
|
259
|
+
}
|
|
260
|
+
async compressedStream(id, sourceBytes, dictionary, compressor, prefix) {
|
|
261
|
+
const input = new ReadableStream({ start(controller) {
|
|
262
|
+
if (prefix)
|
|
263
|
+
controller.enqueue(prefix);
|
|
264
|
+
controller.enqueue(sourceBytes);
|
|
265
|
+
controller.close();
|
|
266
|
+
} });
|
|
267
|
+
const reader = input.pipeThrough(compressor).getReader(), lengthId = this.reserve();
|
|
268
|
+
let length = 0, complete = false;
|
|
269
|
+
try {
|
|
270
|
+
this.start(id);
|
|
271
|
+
await this.text(id + " 0 obj\n<< /Length " + lengthId + " 0 R /Filter /FlateDecode " + dictionary + " >>\nstream\n");
|
|
272
|
+
while (true) {
|
|
273
|
+
const next = await withAbort(reader.read(), this.signal);
|
|
274
|
+
if (next.done) {
|
|
275
|
+
complete = true;
|
|
276
|
+
break;
|
|
277
|
+
}
|
|
278
|
+
length += next.value.length;
|
|
279
|
+
await this.raw(next.value);
|
|
280
|
+
}
|
|
281
|
+
await this.text("\nendstream\nendobj\n");
|
|
282
|
+
await this.object(lengthId, String(length));
|
|
283
|
+
}
|
|
284
|
+
finally {
|
|
285
|
+
if (!complete)
|
|
286
|
+
void reader.cancel(this.signal?.reason).catch(() => { });
|
|
287
|
+
reader.releaseLock();
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
async finish(root, info) {
|
|
291
|
+
await this.flushCompact();
|
|
292
|
+
for (let id = 1; id < this.offsets.length; id++)
|
|
293
|
+
if (this.offsets[id] === undefined)
|
|
294
|
+
throw new Error("PDF contains unwritten objects.");
|
|
295
|
+
if (this.bytes > 9999999999)
|
|
296
|
+
throw new OfficeIMOError("RESOURCE_LIMIT", "PDF exceeds classic cross-reference offset capacity.");
|
|
297
|
+
if (this.compactIndices)
|
|
298
|
+
return this.finishCompact(root, info);
|
|
299
|
+
const start = this.bytes;
|
|
300
|
+
await this.text("xref\n0 " + this.offsets.length + "\n0000000000 65535 f \n");
|
|
301
|
+
for (let i = 1; i < this.offsets.length; i++)
|
|
302
|
+
await this.text(String(this.offsets[i]).padStart(10, "0") + " 00000 n \n");
|
|
303
|
+
await this.text("trailer\n<< /Size " + this.offsets.length + " /Root " + root + " 0 R /Info " + info + " 0 R >>\nstartxref\n" + start + "\n%%EOF\n");
|
|
304
|
+
await this.flush();
|
|
305
|
+
}
|
|
306
|
+
/** Xref payloads have a known length and are emitted in bounded chunks, without another copy
|
|
307
|
+
* of the document's random-access location arrays or a whole-xref compression buffer. */
|
|
308
|
+
async finishCompact(root, info) {
|
|
309
|
+
const id = this.reserve(), start = this.bytes, length = this.offsets.length * pdfXrefEntryBytes;
|
|
310
|
+
if (!Number.isSafeInteger(length))
|
|
311
|
+
throw new OfficeIMOError("RESOURCE_LIMIT", "PDF cross-reference stream is too large.");
|
|
312
|
+
this.start(id);
|
|
313
|
+
await this.text(id + " 0 obj\n<< /Type /XRef /Size " + this.offsets.length + " /W [1 8 2] /Root " + root + " 0 R /Info " + info +
|
|
314
|
+
" 0 R /Length " + length + " >>\nstream\n");
|
|
315
|
+
for (const chunk of pdfXrefChunks(this.offsets, this.compactIndices))
|
|
316
|
+
await this.raw(chunk);
|
|
317
|
+
await this.text("\nendstream\nendobj\nstartxref\n" + start + "\n%%EOF\n");
|
|
318
|
+
await this.flushBytes();
|
|
319
|
+
}
|
|
320
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { OfficeIMOError } from "../core/errors.js";
|
|
2
|
+
import { checkAbort } from "../core/iteration.js";
|
|
3
|
+
import { pdfNumber, unicodeHex } from "./objects.js";
|
|
4
|
+
/** @internal Bounded native bookmarks; destinations are captured only when their content is painted. */
|
|
5
|
+
export class PdfOutlines {
|
|
6
|
+
objects;
|
|
7
|
+
settings;
|
|
8
|
+
root;
|
|
9
|
+
items = [];
|
|
10
|
+
top = [];
|
|
11
|
+
stack = [];
|
|
12
|
+
constructor(objects, settings) {
|
|
13
|
+
this.objects = objects;
|
|
14
|
+
this.settings = settings;
|
|
15
|
+
}
|
|
16
|
+
add(bookmark, page, y) {
|
|
17
|
+
checkAbort(this.settings.options.signal);
|
|
18
|
+
if (this.items.length >= this.settings.limits.maxBookmarks)
|
|
19
|
+
throw new OfficeIMOError("RESOURCE_LIMIT", "maxBookmarks exceeded.");
|
|
20
|
+
this.root ??= this.objects.reserve();
|
|
21
|
+
const level = bookmark.level ?? 1;
|
|
22
|
+
while (this.stack.length && this.stack.at(-1).level >= level)
|
|
23
|
+
this.stack.pop();
|
|
24
|
+
const parent = this.stack.at(-1), siblings = parent?.children ?? this.top, previous = siblings.at(-1);
|
|
25
|
+
const item = { id: this.objects.reserve(), title: bookmark.title, page, y, level,
|
|
26
|
+
parent: parent?.id ?? this.root, children: [], previous: previous?.id, next: undefined };
|
|
27
|
+
if (previous)
|
|
28
|
+
previous.next = item.id;
|
|
29
|
+
siblings.push(item);
|
|
30
|
+
this.items.push(item);
|
|
31
|
+
this.stack.push(item);
|
|
32
|
+
}
|
|
33
|
+
get count() { return this.items.length; }
|
|
34
|
+
/** A completed analytical group cannot parent later groups, including unnamed ones. */
|
|
35
|
+
closeGroup(level) {
|
|
36
|
+
while (this.stack.length && this.stack.at(-1).level >= level)
|
|
37
|
+
this.stack.pop();
|
|
38
|
+
}
|
|
39
|
+
get catalogDictionary() { return this.root === undefined ? "" : " /Outlines " + this.root + " 0 R"; }
|
|
40
|
+
async finish() {
|
|
41
|
+
if (this.root === undefined)
|
|
42
|
+
return;
|
|
43
|
+
const descendants = new Map();
|
|
44
|
+
for (const item of [...this.items].reverse())
|
|
45
|
+
descendants.set(item.id, item.children.reduce((n, child) => n + 1 + descendants.get(child.id), 0));
|
|
46
|
+
for (const item of this.items) {
|
|
47
|
+
checkAbort(this.settings.options.signal);
|
|
48
|
+
await this.objects.object(item.id, "<< /Title <feff" + unicodeHex(item.title) + "> /Parent " + item.parent + " 0 R" +
|
|
49
|
+
(item.previous === undefined ? "" : " /Prev " + item.previous + " 0 R") + (item.next === undefined ? "" : " /Next " + item.next + " 0 R") +
|
|
50
|
+
(item.children.length ? " /First " + item.children[0].id + " 0 R /Last " + item.children.at(-1).id + " 0 R /Count " + descendants.get(item.id) : "") +
|
|
51
|
+
" /Dest [" + item.page + " 0 R /XYZ null " + pdfNumber(item.y) + " null] >>");
|
|
52
|
+
}
|
|
53
|
+
await this.objects.object(this.root, "<< /Type /Outlines /First " + this.top[0].id + " 0 R /Last " + this.top.at(-1).id + " 0 R /Count " + this.items.length + " >>");
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { settings } from "./settings.js";
|
|
2
|
+
/** @internal Every section derives from captured document defaults, never the preceding section. */
|
|
3
|
+
export function sectionSettings(base, section) {
|
|
4
|
+
const { pageSize, orientation, margins, pageHeader, pageFooter, pageNumbers } = section;
|
|
5
|
+
const options = { ...base.options,
|
|
6
|
+
...(pageSize === undefined ? {} : { pageSize }), ...(orientation === undefined ? {} : { orientation }),
|
|
7
|
+
...(margins === undefined ? {} : { margins }), ...(pageHeader === undefined ? {} : { pageHeader }),
|
|
8
|
+
...(pageFooter === undefined ? {} : { pageFooter }), ...(pageNumbers === undefined ? {} : { pageNumbers }) };
|
|
9
|
+
// Reuse the existing public normalizer; section changes cannot acquire new limits or reset counters.
|
|
10
|
+
const derived = settings(options, true);
|
|
11
|
+
return { ...base, page: derived.page, margins: derived.margins, options };
|
|
12
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|