@formancy/report-render-pdf 0.1.0 → 0.3.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.
- package/NOTICE +8 -0
- package/README.md +907 -37
- package/dist/bill-fonts-B61mmaVd.mjs +16 -0
- package/dist/bill-fonts-B61mmaVd.mjs.map +1 -0
- package/dist/browser.d.mts +8 -3
- package/dist/browser.d.mts.map +1 -1
- package/dist/browser.mjs +90 -20
- package/dist/browser.mjs.map +1 -1
- package/dist/chart-draw-CUcWXjN9.mjs +1056 -0
- package/dist/chart-draw-CUcWXjN9.mjs.map +1 -0
- package/dist/index.d.mts +1 -3
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +59 -23
- package/dist/index.mjs.map +1 -1
- package/dist/{options-wLxIWA87.d.mts → options-B7fd-ZYd.d.mts} +13 -3
- package/dist/options-B7fd-ZYd.d.mts.map +1 -0
- package/dist/render-D4q7IZ7I.mjs +5139 -0
- package/dist/render-D4q7IZ7I.mjs.map +1 -0
- package/fonts/AUTHORS-LiberationSans.txt +16 -0
- package/fonts/LICENSE-LiberationSans.txt +102 -0
- package/fonts/LiberationSans-Bold.ttf +0 -0
- package/fonts/LiberationSans-Regular.ttf +0 -0
- package/package.json +12 -7
- package/dist/options-wLxIWA87.d.mts.map +0 -1
- package/dist/render-Bs7DXIYJ.mjs +0 -1943
- package/dist/render-Bs7DXIYJ.mjs.map +0 -1
package/dist/render-Bs7DXIYJ.mjs
DELETED
|
@@ -1,1943 +0,0 @@
|
|
|
1
|
-
import { MAX_ASSET_BYTES, RenderRefusal } from "@formancy/report-runtime";
|
|
2
|
-
//#region src/glyphs.ts
|
|
3
|
-
/** The face text in this weight and slant prints in. */
|
|
4
|
-
function faceOf(bold, italic) {
|
|
5
|
-
return bold ? italic ? "bolditalics" : "bold" : italic ? "italics" : "normal";
|
|
6
|
-
}
|
|
7
|
-
/** A line break: pdfmake breaks the line there and draws nothing for it. */
|
|
8
|
-
const LINE_BREAKS = /* @__PURE__ */ new Set([10, 13]);
|
|
9
|
-
/** The first character of `text` that `face` has no glyph for, as its code point; undefined when it has a glyph for every one. */
|
|
10
|
-
function missingGlyph(text, face) {
|
|
11
|
-
for (const character of text) {
|
|
12
|
-
const point = character.codePointAt(0);
|
|
13
|
-
if (!face.has(point) && !LINE_BREAKS.has(point)) return point;
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
/** A code point as Unicode writes it: U+5C71. */
|
|
17
|
-
function codePoint(point) {
|
|
18
|
-
return `U+${point.toString(16).toUpperCase().padStart(4, "0")}`;
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* The code points a TrueType font maps to a glyph other than glyph 0, from
|
|
22
|
-
* its Unicode BMP character map: the Windows (3, 1) or Unicode (0, 3)
|
|
23
|
-
* subtable, in format 4. A font without one is not one this renderer
|
|
24
|
-
* embeds, and is an error, not a document's refusal.
|
|
25
|
-
*/
|
|
26
|
-
function characterMap(font) {
|
|
27
|
-
const view = new DataView(font.buffer, font.byteOffset, font.byteLength);
|
|
28
|
-
const tables = view.getUint16(4);
|
|
29
|
-
let cmap;
|
|
30
|
-
for (let index = 0; index < tables; index++) {
|
|
31
|
-
const record = 12 + 16 * index;
|
|
32
|
-
if (String.fromCharCode(...font.subarray(record, record + 4)) === "cmap") cmap = view.getUint32(record + 8);
|
|
33
|
-
}
|
|
34
|
-
if (cmap === void 0) throw new Error("the font has no character map");
|
|
35
|
-
const encodings = view.getUint16(cmap + 2);
|
|
36
|
-
for (let index = 0; index < encodings; index++) {
|
|
37
|
-
const record = cmap + 4 + 8 * index;
|
|
38
|
-
const [platform, encoding] = [view.getUint16(record), view.getUint16(record + 2)];
|
|
39
|
-
const subtable = cmap + view.getUint32(record + 4);
|
|
40
|
-
if ((platform === 3 && encoding === 1 || platform === 0 && encoding === 3) && view.getUint16(subtable) === 4) return format4(view, subtable);
|
|
41
|
-
}
|
|
42
|
-
throw new Error("the font has no Unicode character map in format 4");
|
|
43
|
-
}
|
|
44
|
-
/** A format 4 subtable's segments, each mapping a range of code points by a delta or through the glyph array; glyph 0 is no glyph. */
|
|
45
|
-
function format4(view, table) {
|
|
46
|
-
const segments = view.getUint16(table + 6) / 2;
|
|
47
|
-
const ends = table + 14;
|
|
48
|
-
const starts = ends + 2 * segments + 2;
|
|
49
|
-
const deltas = starts + 2 * segments;
|
|
50
|
-
const offsets = deltas + 2 * segments;
|
|
51
|
-
const mapped = /* @__PURE__ */ new Set();
|
|
52
|
-
for (let segment = 0; segment < segments; segment++) {
|
|
53
|
-
const end = view.getUint16(ends + 2 * segment);
|
|
54
|
-
const start = view.getUint16(starts + 2 * segment);
|
|
55
|
-
const delta = view.getUint16(deltas + 2 * segment);
|
|
56
|
-
const offsetAt = offsets + 2 * segment;
|
|
57
|
-
const offset = view.getUint16(offsetAt);
|
|
58
|
-
for (let point = start; point <= end; point++) {
|
|
59
|
-
const indexed = offset === 0 ? point : view.getUint16(offsetAt + offset + 2 * (point - start));
|
|
60
|
-
if ((offset !== 0 && indexed === 0 ? 0 : indexed + delta & 65535) !== 0) mapped.add(point);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
return mapped;
|
|
64
|
-
}
|
|
65
|
-
//#endregion
|
|
66
|
-
//#region src/engine.ts
|
|
67
|
-
/** The glyphs of the four faces, from their bytes, read once by `read` and kept: the fonts do not change while the process runs. */
|
|
68
|
-
function glyphsOnce(read) {
|
|
69
|
-
let glyphs;
|
|
70
|
-
return () => glyphs ??= (async () => {
|
|
71
|
-
const maps = await Promise.all([
|
|
72
|
-
"normal",
|
|
73
|
-
"bold",
|
|
74
|
-
"italics",
|
|
75
|
-
"bolditalics"
|
|
76
|
-
].map(async (face) => [face, characterMap(await read(face))]));
|
|
77
|
-
return Object.fromEntries(maps);
|
|
78
|
-
})();
|
|
79
|
-
}
|
|
80
|
-
/**
|
|
81
|
-
* The PDF's bytes, read from PDFKit's stream. pdfmake's own `getBuffer`
|
|
82
|
-
* listens for the end of the stream and never for an error, so a stream that
|
|
83
|
-
* fails leaves its promise pending forever; this one rejects.
|
|
84
|
-
*/
|
|
85
|
-
function collect(stream) {
|
|
86
|
-
return new Promise((resolve, reject) => {
|
|
87
|
-
const chunks = [];
|
|
88
|
-
stream.on("data", (chunk) => chunks.push(chunk));
|
|
89
|
-
stream.on("error", reject);
|
|
90
|
-
stream.on("end", () => {
|
|
91
|
-
const bytes = new Uint8Array(chunks.reduce((total, chunk) => total + chunk.length, 0));
|
|
92
|
-
let offset = 0;
|
|
93
|
-
for (const chunk of chunks) {
|
|
94
|
-
bytes.set(chunk, offset);
|
|
95
|
-
offset += chunk.length;
|
|
96
|
-
}
|
|
97
|
-
resolve(bytes);
|
|
98
|
-
});
|
|
99
|
-
stream.end();
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
//#endregion
|
|
103
|
-
//#region src/bytes.ts
|
|
104
|
-
/**
|
|
105
|
-
* Byte arithmetic the renderer needs in Node and in a browser alike, written
|
|
106
|
-
* on the standard library alone: neither `Buffer` nor `node:zlib` exists in a
|
|
107
|
-
* browser, and the browser entry shares every line of this package but its
|
|
108
|
-
* font loading (`browser.ts`).
|
|
109
|
-
*/
|
|
110
|
-
/** CRC-32 as PNG and zlib compute it: the reflected polynomial 0xedb88320, every bit inverted before and after. */
|
|
111
|
-
const CRC_TABLE = Array.from({ length: 256 }, (_, byte) => {
|
|
112
|
-
let crc = byte;
|
|
113
|
-
for (let bit = 0; bit < 8; bit++) crc = crc & 1 ? 3988292384 ^ crc >>> 1 : crc >>> 1;
|
|
114
|
-
return crc >>> 0;
|
|
115
|
-
});
|
|
116
|
-
function crc32(bytes) {
|
|
117
|
-
let crc = 4294967295;
|
|
118
|
-
for (const byte of bytes) crc = CRC_TABLE[(crc ^ byte) & 255] ^ crc >>> 8;
|
|
119
|
-
return (crc ^ 4294967295) >>> 0;
|
|
120
|
-
}
|
|
121
|
-
const BASE64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
122
|
-
/** Standard base64 with padding, as a `data:` URL carries bytes. */
|
|
123
|
-
function base64(bytes) {
|
|
124
|
-
const parts = [];
|
|
125
|
-
for (let offset = 0; offset < bytes.length; offset += 3) {
|
|
126
|
-
const [a = 0, b = 0, c = 0] = [
|
|
127
|
-
bytes[offset],
|
|
128
|
-
bytes[offset + 1],
|
|
129
|
-
bytes[offset + 2]
|
|
130
|
-
];
|
|
131
|
-
const left = bytes.length - offset;
|
|
132
|
-
parts.push(BASE64[a >> 2] + BASE64[(a & 3) << 4 | b >> 4] + (left > 1 ? BASE64[(b & 15) << 2 | c >> 6] : "=") + (left > 2 ? BASE64[c & 63] : "="));
|
|
133
|
-
}
|
|
134
|
-
return parts.join("");
|
|
135
|
-
}
|
|
136
|
-
/** The bytes standard base64 with padding spells, as pdfmake's virtual files hold fonts; whitespace ignored, anything else not base64 refused. */
|
|
137
|
-
function fromBase64(text) {
|
|
138
|
-
const digits = text.replace(/\s+/g, "").replace(/=+$/, "");
|
|
139
|
-
const bytes = new Uint8Array(Math.floor(digits.length * 3 / 4));
|
|
140
|
-
let bits = 0;
|
|
141
|
-
let held = 0;
|
|
142
|
-
let at = 0;
|
|
143
|
-
for (const digit of digits) {
|
|
144
|
-
const value = BASE64.indexOf(digit);
|
|
145
|
-
if (value === -1) throw new Error("not base64");
|
|
146
|
-
bits = bits << 6 | value;
|
|
147
|
-
held += 6;
|
|
148
|
-
if (held >= 8) {
|
|
149
|
-
held -= 8;
|
|
150
|
-
bytes[at++] = bits >> held & 255;
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
return bytes;
|
|
154
|
-
}
|
|
155
|
-
/**
|
|
156
|
-
* Inflates zlib-wrapped `data` -- a PNG's image data -- with the platform's
|
|
157
|
-
* own `DecompressionStream`, handing each chunk to `each` as it arrives and
|
|
158
|
-
* stopping, without reading further, once more than `limit` bytes have come
|
|
159
|
-
* out: the limit is what the image's header promises, so a PNG whose data
|
|
160
|
-
* inflates to gigabytes costs no more than one that keeps its promise.
|
|
161
|
-
*
|
|
162
|
-
* Data that is not a whole zlib stream is `invalid`, and so is data with
|
|
163
|
-
* bytes after the stream's end, which Node's `inflateSync` would ignore and
|
|
164
|
-
* the Compression Streams standard refuses: refusing them costs a PNG some
|
|
165
|
-
* decoder would have drawn, and keeps Node and a browser in agreement.
|
|
166
|
-
*/
|
|
167
|
-
async function inflate(data, limit, each) {
|
|
168
|
-
const reader = new Blob([data]).stream().pipeThrough(new DecompressionStream("deflate")).getReader();
|
|
169
|
-
let length = 0;
|
|
170
|
-
try {
|
|
171
|
-
for (;;) {
|
|
172
|
-
const { done, value } = await reader.read();
|
|
173
|
-
if (done) return {
|
|
174
|
-
kind: "done",
|
|
175
|
-
length
|
|
176
|
-
};
|
|
177
|
-
if (length + value.length > limit) {
|
|
178
|
-
await reader.cancel();
|
|
179
|
-
return { kind: "too-long" };
|
|
180
|
-
}
|
|
181
|
-
each(value, length);
|
|
182
|
-
length += value.length;
|
|
183
|
-
}
|
|
184
|
-
} catch {
|
|
185
|
-
return { kind: "invalid" };
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
//#endregion
|
|
189
|
-
//#region src/build.ts
|
|
190
|
-
/** The plan of a table nothing measured: a table in a keep-together block or a page region, which no rule breaks. */
|
|
191
|
-
const UNPLANNED = {
|
|
192
|
-
dontBreakRows: false,
|
|
193
|
-
keepWithHeaderRows: 0,
|
|
194
|
-
pitches: [],
|
|
195
|
-
blocks: []
|
|
196
|
-
};
|
|
197
|
-
function newBuild(mode, checked, body = {
|
|
198
|
-
top: 0,
|
|
199
|
-
bottom: 0
|
|
200
|
-
}, plans = /* @__PURE__ */ new Map()) {
|
|
201
|
-
const { pixels, paths } = checked;
|
|
202
|
-
return {
|
|
203
|
-
mode,
|
|
204
|
-
bodyTop: body.top,
|
|
205
|
-
bodyBottom: body.bottom,
|
|
206
|
-
plans,
|
|
207
|
-
pixels,
|
|
208
|
-
paths,
|
|
209
|
-
imageKeys: /* @__PURE__ */ new Map(),
|
|
210
|
-
images: {},
|
|
211
|
-
rules: /* @__PURE__ */ new Map(),
|
|
212
|
-
measuredRows: /* @__PURE__ */ new Map(),
|
|
213
|
-
words: [],
|
|
214
|
-
next: 1
|
|
215
|
-
};
|
|
216
|
-
}
|
|
217
|
-
/** Marks `node`, keeping a mark it already has, and returns the mark. */
|
|
218
|
-
function mark(node, build) {
|
|
219
|
-
return node.headlineLevel ??= build.next++;
|
|
220
|
-
}
|
|
221
|
-
/** Whether a node with one of `ids`, among those pdfmake has on this page and the next, starts on `page`. */
|
|
222
|
-
function startsOnPage(queries, ids, page) {
|
|
223
|
-
return [...queries.getFollowingNodesOnPage(), ...queries.getNodesOnNextPage()].some((other) => other.id !== void 0 && ids.has(other.id) && other.startPosition.pageNumber === page);
|
|
224
|
-
}
|
|
225
|
-
/**
|
|
226
|
-
* The page a node handed to pdfmake starts on in its last layout, read from
|
|
227
|
-
* the node itself: pdfmake records each node's positions on it, and reports
|
|
228
|
-
* the first as the node's `startPosition`. `startsOnPage` asks pdfmake's
|
|
229
|
-
* queries, each of which walks every node to the end of the document, so a
|
|
230
|
-
* rule per row of a long table made the rules' work grow with the square of
|
|
231
|
-
* the table; this is one read. Undefined for a node pdfmake laid out
|
|
232
|
-
* nowhere. `pdfmake.test.ts` holds that 0.3.11 still records positions here.
|
|
233
|
-
*/
|
|
234
|
-
function pageOf$1(node) {
|
|
235
|
-
return node.positions?.[0]?.pageNumber;
|
|
236
|
-
}
|
|
237
|
-
//#endregion
|
|
238
|
-
//#region src/style.ts
|
|
239
|
-
/**
|
|
240
|
-
* A document's text styles as pdfmake's: the one place a font token becomes
|
|
241
|
-
* a font, and a style becomes pdfmake's properties.
|
|
242
|
-
*
|
|
243
|
-
* **Fonts.** pdfmake ships one family, Roboto 3.014, under the SIL Open Font
|
|
244
|
-
* License 1.1, which allows embedding in documents
|
|
245
|
-
* ([0006](../../../docs/decisions/0006-dependencies-and-fonts-allow-public-source-and-licensed-production.md)).
|
|
246
|
-
* `sans` is Roboto. `serif` and `mono` have no font here, and a report that
|
|
247
|
-
* asks for one is refused as `unsupported` before anything is laid out
|
|
248
|
-
* (`check.ts`): printing it in Roboto would be a substitution nobody sees
|
|
249
|
-
* until the PDF is read, which plan section 20 rules out. Bold is Roboto
|
|
250
|
-
* Medium, the heaviest face pdfmake ships, and italic its italic.
|
|
251
|
-
*/
|
|
252
|
-
/** pdfmake's name for the one family the renderer embeds. */
|
|
253
|
-
const ROBOTO = "Roboto";
|
|
254
|
-
/** The font each token prints in; a token without one is refused. */
|
|
255
|
-
const FONTS = { sans: ROBOTO };
|
|
256
|
-
/**
|
|
257
|
-
* Roboto's line, as a multiple of the font size: its ascender less its
|
|
258
|
-
* descender, 2400 of its 2048 units per em. pdfmake lays out a line exactly
|
|
259
|
-
* this tall.
|
|
260
|
-
*/
|
|
261
|
-
const LINE_FACTOR = 2400 / 2048;
|
|
262
|
-
/** A colour as the document writes one, and as pdfmake reads one: `#` and six lower-case hex digits. */
|
|
263
|
-
const COLOR = /^#[0-9a-f]{6}$/;
|
|
264
|
-
/** pdfmake's text properties for a style, the renderer's own defaults under it. Only what is set is written. */
|
|
265
|
-
function textProperties(style, defaults) {
|
|
266
|
-
const bold = style.bold ?? defaults.bold;
|
|
267
|
-
const italic = style.italic ?? defaults.italic;
|
|
268
|
-
return {
|
|
269
|
-
fontSize: style.size ?? defaults.size,
|
|
270
|
-
...style.font === void 0 ? {} : { font: FONTS[style.font] },
|
|
271
|
-
...bold === void 0 ? {} : { bold },
|
|
272
|
-
...italic === void 0 ? {} : { italics: italic },
|
|
273
|
-
...style.color === void 0 ? {} : { color: style.color }
|
|
274
|
-
};
|
|
275
|
-
}
|
|
276
|
-
//#endregion
|
|
277
|
-
//#region src/breaks.ts
|
|
278
|
-
/** Rows that close what is above them: a group's footer, a total. */
|
|
279
|
-
const CLOSING = /* @__PURE__ */ new Set(["groupFooter", "total"]);
|
|
280
|
-
/**
|
|
281
|
-
* The blocks of rows of these kinds that stand on one page, overlapping
|
|
282
|
-
* ones merged, in order. Closing rows are kept with the row before them only
|
|
283
|
-
* when `closing`: a table whose rows may split across pages has no row
|
|
284
|
-
* height to keep them by.
|
|
285
|
-
*/
|
|
286
|
-
function keepBlocks(kinds, closing) {
|
|
287
|
-
const blocks = [];
|
|
288
|
-
kinds.forEach((kind, index) => {
|
|
289
|
-
if (kind === "groupHeader" && index + 1 < kinds.length) blocks.push({
|
|
290
|
-
first: index,
|
|
291
|
-
last: index + 1
|
|
292
|
-
});
|
|
293
|
-
const before = kinds[index - 1];
|
|
294
|
-
if (!closing || !CLOSING.has(kind) || before === void 0 || CLOSING.has(before) || before !== "detail" && before !== "empty") return;
|
|
295
|
-
let last = index;
|
|
296
|
-
while (kinds[last + 1] !== void 0 && CLOSING.has(kinds[last + 1])) last++;
|
|
297
|
-
const anchor = index - 1;
|
|
298
|
-
blocks.push({
|
|
299
|
-
first: kinds[anchor - 1] === "groupHeader" ? anchor - 1 : anchor,
|
|
300
|
-
last
|
|
301
|
-
});
|
|
302
|
-
});
|
|
303
|
-
const merged = [];
|
|
304
|
-
for (const block of blocks.sort((a, b) => a.first - b.first)) {
|
|
305
|
-
const previous = merged.at(-1);
|
|
306
|
-
if (previous !== void 0 && block.first <= previous.last) merged[merged.length - 1] = {
|
|
307
|
-
first: previous.first,
|
|
308
|
-
last: Math.max(previous.last, block.last)
|
|
309
|
-
};
|
|
310
|
-
else merged.push(block);
|
|
311
|
-
}
|
|
312
|
-
return merged;
|
|
313
|
-
}
|
|
314
|
-
/** The rows' height from `first` to `last`, each row's pitch being its height and the rule under it. */
|
|
315
|
-
function blockHeight(pitches, block) {
|
|
316
|
-
return pitches.slice(block.first, block.last + 1).reduce((sum, pitch) => sum + pitch, 0);
|
|
317
|
-
}
|
|
318
|
-
/** The blocks a table's rules can keep: each fits under the repeated header, and starts after the rows that move with it. */
|
|
319
|
-
function keptBlocks(blocks, pitches, keepWithHeaderRows, room) {
|
|
320
|
-
return blocks.filter((block) => block.first > keepWithHeaderRows && blockHeight(pitches, block) <= room);
|
|
321
|
-
}
|
|
322
|
-
/** A height closer than this to what is left of a page could fall either way in pdfmake's arithmetic: the plan stops there and leaves the rest to the rules. */
|
|
323
|
-
const UNSURE = .01;
|
|
324
|
-
/**
|
|
325
|
-
* The rows, from `first` on, that the `blocks`' rules will break before,
|
|
326
|
-
* laid out row after row as pdfmake lays out rows it keeps whole: a row
|
|
327
|
-
* that does not fit in what is left of a page starts the next one. `sure`
|
|
328
|
-
* is false when a height came too close to the foot of a page to say, and
|
|
329
|
-
* the breaks are then those before it.
|
|
330
|
-
*/
|
|
331
|
-
function plannedBreaks(pitches, blocks, first, geometry) {
|
|
332
|
-
const { continued, bottom } = geometry;
|
|
333
|
-
const starts = new Map(blocks.map((block) => [block.first, block]));
|
|
334
|
-
const breaks = [];
|
|
335
|
-
let y = geometry.top;
|
|
336
|
-
/** Whether `height` fits below `y`, or undefined when that is too close to say. */
|
|
337
|
-
const fits = (height) => {
|
|
338
|
-
const left = bottom - y - height;
|
|
339
|
-
return Math.abs(left) < UNSURE ? void 0 : left > 0;
|
|
340
|
-
};
|
|
341
|
-
for (let row = first; row < pitches.length; row++) {
|
|
342
|
-
const fitting = fits(pitches[row]);
|
|
343
|
-
if (fitting === void 0) return {
|
|
344
|
-
breaks,
|
|
345
|
-
sure: false
|
|
346
|
-
};
|
|
347
|
-
if (!fitting) y = continued;
|
|
348
|
-
const block = starts.get(row);
|
|
349
|
-
if (block !== void 0 && y > continued + UNSURE) {
|
|
350
|
-
const whole = fits(blockHeight(pitches, block));
|
|
351
|
-
if (whole === void 0) return {
|
|
352
|
-
breaks,
|
|
353
|
-
sure: false
|
|
354
|
-
};
|
|
355
|
-
if (!whole) {
|
|
356
|
-
breaks.push(row);
|
|
357
|
-
y = continued;
|
|
358
|
-
}
|
|
359
|
-
}
|
|
360
|
-
y += pitches[row];
|
|
361
|
-
}
|
|
362
|
-
return {
|
|
363
|
-
breaks,
|
|
364
|
-
sure: true
|
|
365
|
-
};
|
|
366
|
-
}
|
|
367
|
-
/** Where each row from `first` on stands when no rule breaks the table: how pdfmake lays out rows it keeps whole. */
|
|
368
|
-
function naturalLayout(pitches, first, geometry) {
|
|
369
|
-
const placed = [];
|
|
370
|
-
let [y, page] = [geometry.top, 0];
|
|
371
|
-
for (let row = first; row < pitches.length; row++) {
|
|
372
|
-
const moves = y + pitches[row] > geometry.bottom;
|
|
373
|
-
if (moves) [y, page] = [geometry.continued, page + 1];
|
|
374
|
-
placed.push({
|
|
375
|
-
page,
|
|
376
|
-
top: y,
|
|
377
|
-
first: moves || row === first
|
|
378
|
-
});
|
|
379
|
-
y += pitches[row];
|
|
380
|
-
}
|
|
381
|
-
return placed;
|
|
382
|
-
}
|
|
383
|
-
//#endregion
|
|
384
|
-
//#region src/table-rules.ts
|
|
385
|
-
function tableRules(rows, plan, build, continued) {
|
|
386
|
-
const forced = /* @__PURE__ */ new Set();
|
|
387
|
-
const ruled = plan.blocks.flatMap((block) => {
|
|
388
|
-
const starts = printing(rows[block.first]);
|
|
389
|
-
const end = rows.slice(block.first + 1, block.last + 1).findLast((row) => printing(row).length > 0);
|
|
390
|
-
if (starts.length === 0 || end === void 0) return [];
|
|
391
|
-
const ends = printing(end);
|
|
392
|
-
for (const cell of [...starts, ...ends]) cell.id ??= `row-${build.next++}`;
|
|
393
|
-
for (const cell of starts) add(cell, (node) => {
|
|
394
|
-
const breaks = forced.has(cell) || !ends.some((other) => pageOf$1(other) === node.startPosition.pageNumber);
|
|
395
|
-
if (breaks) {
|
|
396
|
-
for (const other of starts) if (other !== cell) other.pageBreak = "before";
|
|
397
|
-
}
|
|
398
|
-
return breaks;
|
|
399
|
-
}, build);
|
|
400
|
-
return [block];
|
|
401
|
-
});
|
|
402
|
-
if (continued === void 0 || !plan.dontBreakRows || ruled.length === 0) return;
|
|
403
|
-
const rowOf = /* @__PURE__ */ new Map();
|
|
404
|
-
rows.forEach((row, index) => {
|
|
405
|
-
const [first] = printing(row);
|
|
406
|
-
if (first !== void 0 && index > 0) rowOf.set(mark(first, build), index);
|
|
407
|
-
});
|
|
408
|
-
const hookRow = rows.findIndex((row, index) => index > plan.keepWithHeaderRows && printing(row).length > 0);
|
|
409
|
-
if (hookRow === -1 || hookRow > ruled[0].first) return;
|
|
410
|
-
const hook = printing(rows[hookRow])[0];
|
|
411
|
-
let done = false;
|
|
412
|
-
const planning = (node, queries) => {
|
|
413
|
-
if (!done) {
|
|
414
|
-
done = true;
|
|
415
|
-
const geometry = {
|
|
416
|
-
top: node.startPosition.top,
|
|
417
|
-
continued,
|
|
418
|
-
bottom: build.bodyBottom
|
|
419
|
-
};
|
|
420
|
-
for (const [index, row] of planBreaks(plan, ruled, hookRow, geometry, node, queries, rowOf).entries()) printing(rows[row]).forEach((cell, at) => index === 0 && at === 0 ? forced.add(cell) : cell.pageBreak = "before");
|
|
421
|
-
}
|
|
422
|
-
return forced.has(hook);
|
|
423
|
-
};
|
|
424
|
-
add(hook, planning, build);
|
|
425
|
-
}
|
|
426
|
-
/** The rows the blocks' rules will break before, from row `hookRow` on; none when what pdfmake reports of this page and the next is not what the arithmetic gives. */
|
|
427
|
-
function planBreaks(plan, blocks, hookRow, geometry, node, queries, rowOf) {
|
|
428
|
-
if (geometry.top + plan.pitches[hookRow] > geometry.bottom) return [];
|
|
429
|
-
const placed = naturalLayout(plan.pitches, hookRow, geometry);
|
|
430
|
-
const page = node.startPosition.pageNumber;
|
|
431
|
-
for (const other of [...queries.getFollowingNodesOnPage(), ...queries.getNodesOnNextPage()]) {
|
|
432
|
-
const row = other.headlineLevel === void 0 ? void 0 : rowOf.get(other.headlineLevel);
|
|
433
|
-
const at = row === void 0 ? void 0 : placed[row - hookRow];
|
|
434
|
-
if (at === void 0 || at.first) continue;
|
|
435
|
-
if (other.startPosition.pageNumber !== page + at.page || Math.abs(other.startPosition.top - at.top) > .01) return [];
|
|
436
|
-
}
|
|
437
|
-
return plannedBreaks(plan.pitches, blocks, hookRow, geometry).breaks;
|
|
438
|
-
}
|
|
439
|
-
/** Sets `rule` on `cell`'s mark, asked before a rule it has: the planning rule, before the block rule of a row that starts a block. */
|
|
440
|
-
function add(cell, rule, build) {
|
|
441
|
-
const at = mark(cell, build);
|
|
442
|
-
const had = build.rules.get(at);
|
|
443
|
-
build.rules.set(at, had === void 0 ? rule : (node, queries) => rule(node, queries) || had(node, queries));
|
|
444
|
-
}
|
|
445
|
-
/** The cells of a row that have text. */
|
|
446
|
-
function printing(row) {
|
|
447
|
-
return row.cells.filter((cell) => cell.text.trim() !== "");
|
|
448
|
-
}
|
|
449
|
-
//#endregion
|
|
450
|
-
//#region src/widths.ts
|
|
451
|
-
/**
|
|
452
|
-
* Widths as the renderer hands them to pdfmake: always a number of points.
|
|
453
|
-
* pdfmake widens a column it is handed as `'*'` to its longest word, so a
|
|
454
|
-
* table, a columns block or a frame grew past the page with a long word in
|
|
455
|
-
* it; a fill column is therefore its share of the width worked out here,
|
|
456
|
-
* the one place the check (`check.ts`) and the content (`content.ts`,
|
|
457
|
-
* `table.ts`) take it from.
|
|
458
|
-
*/
|
|
459
|
-
/** Points, or nothing for `'fill'`. */
|
|
460
|
-
function fixedWidth(width) {
|
|
461
|
-
return width === "fill" ? 0 : width;
|
|
462
|
-
}
|
|
463
|
-
/** The width each `'fill'` gets of `width`: what the fixed widths leave, shared equally. Infinity when there is none. */
|
|
464
|
-
function fillWidth(widths, width) {
|
|
465
|
-
const fixed = widths.reduce((sum, each) => sum + fixedWidth(each), 0);
|
|
466
|
-
const fills = widths.filter((each) => each === "fill").length;
|
|
467
|
-
return fills === 0 ? Infinity : (width - fixed) / fills;
|
|
468
|
-
}
|
|
469
|
-
/** Each column's width in points, of `width`: a fixed one its own, each fill its share. */
|
|
470
|
-
function columnWidths(widths, width) {
|
|
471
|
-
const fill = fillWidth(widths, width);
|
|
472
|
-
return widths.map((each) => each === "fill" ? fill : each);
|
|
473
|
-
}
|
|
474
|
-
/** The table layout: horizontal rules of half a point, no vertical ones, 4pt of padding at the sides and 2pt above and below. */
|
|
475
|
-
const RULE = .5;
|
|
476
|
-
const TABLE_LAYOUT = {
|
|
477
|
-
hLineWidth: () => RULE,
|
|
478
|
-
vLineWidth: () => 0,
|
|
479
|
-
hLineColor: (index) => index === 1 ? "#000000" : "#bfbfbf",
|
|
480
|
-
paddingLeft: () => 4,
|
|
481
|
-
paddingRight: () => 4,
|
|
482
|
-
paddingTop: () => 2,
|
|
483
|
-
paddingBottom: () => 2
|
|
484
|
-
};
|
|
485
|
-
/** Laid out as one line of text, as wide as a space, that no line break can fall in. */
|
|
486
|
-
const NO_BREAK_SPACE = String.fromCodePoint(160);
|
|
487
|
-
/** The renderer's own style for a row of each kind, under the document's: the column header and every row but a detail row in bold. */
|
|
488
|
-
const KIND_BOLD = {
|
|
489
|
-
header: true,
|
|
490
|
-
empty: false,
|
|
491
|
-
detail: false,
|
|
492
|
-
groupHeader: true,
|
|
493
|
-
groupFooter: true,
|
|
494
|
-
total: true
|
|
495
|
-
};
|
|
496
|
-
/**
|
|
497
|
-
* The content of `table`, standing `width` wide: `kept` inside a keep-together
|
|
498
|
-
* block or a page region, where no rule may break it; `record`ing its cells
|
|
499
|
-
* for the word check; `top` the body's top, outside any frame, where its
|
|
500
|
-
* breaks can be planned (`table-rules.ts`). Every column is handed to pdfmake
|
|
501
|
-
* as a number, a fill column as its share, never as `'*'`, which pdfmake
|
|
502
|
-
* widens to its longest word (`widths.ts`).
|
|
503
|
-
*/
|
|
504
|
-
function tableContent(table, build, place) {
|
|
505
|
-
const { kept } = place;
|
|
506
|
-
const measuring = build.mode === "measure";
|
|
507
|
-
const columns = table.columns.length;
|
|
508
|
-
const widths = columnWidths(table.columns.map((column) => column.width), place.width).map((width) => width - 8);
|
|
509
|
-
const across = place.width - 8;
|
|
510
|
-
const cell = (text, column, kind, style, span) => ({
|
|
511
|
-
text: measuring && text === "" ? NO_BREAK_SPACE : text,
|
|
512
|
-
alignment: style.align ?? table.columns[column].align,
|
|
513
|
-
...textProperties(style, {
|
|
514
|
-
size: 9,
|
|
515
|
-
bold: KIND_BOLD[kind]
|
|
516
|
-
}),
|
|
517
|
-
...span === void 0 ? {} : { colSpan: span }
|
|
518
|
-
});
|
|
519
|
-
const covered = () => table.columns.slice(1).map((_, index) => cell("", index + 1, "detail", {}));
|
|
520
|
-
const empty = !table.rows.some((row) => row.kind === "detail");
|
|
521
|
-
const rows = [
|
|
522
|
-
{
|
|
523
|
-
kind: "header",
|
|
524
|
-
cells: table.header.map((text, column) => cell(text, column, "header", table.headerStyle ?? {}))
|
|
525
|
-
},
|
|
526
|
-
...empty ? [{
|
|
527
|
-
kind: "empty",
|
|
528
|
-
cells: [cell(table.emptyText, 0, "empty", { italic: true }, columns), ...covered()]
|
|
529
|
-
}] : [],
|
|
530
|
-
...table.rows.map((row) => {
|
|
531
|
-
const style = (column) => row.cellStyles?.[column] ?? row.style ?? {};
|
|
532
|
-
if (row.across === true) return {
|
|
533
|
-
kind: row.kind,
|
|
534
|
-
cells: [cell(row.cells[0], 0, row.kind, style(0), columns), ...covered()]
|
|
535
|
-
};
|
|
536
|
-
return {
|
|
537
|
-
kind: row.kind,
|
|
538
|
-
cells: row.cells.map((text, column) => cell(text, column, row.kind, style(column)))
|
|
539
|
-
};
|
|
540
|
-
})
|
|
541
|
-
];
|
|
542
|
-
if (place.record) recordWords(table, rows, widths, across, build);
|
|
543
|
-
const plan = build.plans.get(table) ?? UNPLANNED;
|
|
544
|
-
if (measuring) build.measuredRows.set(table, rows.map((row) => ({
|
|
545
|
-
kind: row.kind,
|
|
546
|
-
mark: mark(row.cells[0], build)
|
|
547
|
-
})));
|
|
548
|
-
else if (!kept) tableRules(rows, plan, build, place.top === build.bodyTop ? build.bodyTop + RULE + (plan.pitches[0] ?? 0) : void 0);
|
|
549
|
-
return {
|
|
550
|
-
table: {
|
|
551
|
-
headerRows: measuring ? 0 : 1,
|
|
552
|
-
...measuring || plan.keepWithHeaderRows === 0 ? {} : { keepWithHeaderRows: plan.keepWithHeaderRows },
|
|
553
|
-
...measuring ? {} : { dontBreakRows: plan.dontBreakRows },
|
|
554
|
-
widths,
|
|
555
|
-
body: rows.map((row) => [...row.cells])
|
|
556
|
-
},
|
|
557
|
-
layout: TABLE_LAYOUT,
|
|
558
|
-
fontSize: 9
|
|
559
|
-
};
|
|
560
|
-
}
|
|
561
|
-
/** Every cell of the table, held to its column's width, or the table's for a cell across it (`words.ts`). */
|
|
562
|
-
function recordWords(table, rows, widths, across, build) {
|
|
563
|
-
let row = 0;
|
|
564
|
-
for (const { kind, cells } of rows) {
|
|
565
|
-
const which = kind === "header" ? `the header of table ${table.id}` : kind === "empty" ? `the empty text of table ${table.id}` : `row ${row++} of table ${table.id}`;
|
|
566
|
-
cells.forEach((cell, column) => {
|
|
567
|
-
if (cell.text === "") return;
|
|
568
|
-
const spans = cell.colSpan !== void 0;
|
|
569
|
-
build.words.push({
|
|
570
|
-
node: cell,
|
|
571
|
-
width: spans ? across : widths[column],
|
|
572
|
-
lines: 1,
|
|
573
|
-
what: spans ? which : `${which}, column ${column + 1}`,
|
|
574
|
-
subject: table.id
|
|
575
|
-
});
|
|
576
|
-
});
|
|
577
|
-
}
|
|
578
|
-
}
|
|
579
|
-
/** How much wider than its room a word may measure and still be laid out unbroken: rounding, not layout. */
|
|
580
|
-
const SLACK = .001;
|
|
581
|
-
/** Refuses the first text node whose widest word, as pdfmake measured it, is wider than its room. Nodes pdfmake has not measured are passed over. */
|
|
582
|
-
function checkWords(rooms) {
|
|
583
|
-
for (const { node, width, lines, what, subject } of rooms) {
|
|
584
|
-
const widest = node._minWidth;
|
|
585
|
-
if (typeof widest !== "number" || widest <= width * lines + SLACK) continue;
|
|
586
|
-
const room = lines === 1 ? `its column holds ${width.toFixed(2)} pt` : `${lines} lines of its width are ${(width * lines).toFixed(2)} pt`;
|
|
587
|
-
throw new RenderRefusal("content-too-large", `a word of ${what} is ${widest.toFixed(2)} pt wide, and ${room}; a word is broken only in a paragraph, and across no more than 16 lines`, subject);
|
|
588
|
-
}
|
|
589
|
-
}
|
|
590
|
-
/** The weight a rectangle's outline is drawn at, inside its box: the table rules' weight. */
|
|
591
|
-
const OUTLINE = .5;
|
|
592
|
-
/** How far a node may start below the top of what it sits under and still be first there: rounding, not layout. */
|
|
593
|
-
const AT_TOP = .5;
|
|
594
|
-
/** Content for the body's blocks, laid out `width` wide, or a page region's when `fields` are given. */
|
|
595
|
-
function contentOf(blocks, build, width, fields) {
|
|
596
|
-
const record = fields === void 0 || build.mode === "measure";
|
|
597
|
-
return sequence(blocks, build, fields === void 0 ? {
|
|
598
|
-
width,
|
|
599
|
-
kept: false,
|
|
600
|
-
top: build.bodyTop,
|
|
601
|
-
record
|
|
602
|
-
} : {
|
|
603
|
-
width,
|
|
604
|
-
kept: true,
|
|
605
|
-
top: 0,
|
|
606
|
-
fields,
|
|
607
|
-
record
|
|
608
|
-
});
|
|
609
|
-
}
|
|
610
|
-
/** Border and padding on each side of a section: what it takes from the width, and from a page's height above and below. */
|
|
611
|
-
function frameOf(section) {
|
|
612
|
-
return (section.border?.thickness ?? 0) + (section.padding ?? 0);
|
|
613
|
-
}
|
|
614
|
-
/** A section with neither border, fill nor padding is its blocks and nothing more. */
|
|
615
|
-
function framed(section) {
|
|
616
|
-
return section.border !== void 0 || section.fill !== void 0 || (section.padding ?? 0) > 0;
|
|
617
|
-
}
|
|
618
|
-
/** The blocks with every section that has no frame replaced by its blocks, which flow as though they stood in its place. */
|
|
619
|
-
function unframed(blocks) {
|
|
620
|
-
return blocks.flatMap((block) => block.kind === "section" && !framed(block) ? unframed(block.blocks) : [block]);
|
|
621
|
-
}
|
|
622
|
-
/**
|
|
623
|
-
* The blocks' content. Outside a kept scope, a keep-with-next paragraph
|
|
624
|
-
* breaks to the next page when the block it is kept with does not start on
|
|
625
|
-
* its page -- unless it is first on its page already, where a break would
|
|
626
|
-
* leave the page empty and the paragraph no better kept on the next.
|
|
627
|
-
*/
|
|
628
|
-
function sequence(given, build, scope) {
|
|
629
|
-
const blocks = unframed(given);
|
|
630
|
-
const contents = blocks.map((block) => blockContent(block, build, scope));
|
|
631
|
-
if (build.mode !== "render" || scope.kept) return contents;
|
|
632
|
-
blocks.forEach((block, index) => {
|
|
633
|
-
if (block.kind !== "paragraph" || block.keepWithNext !== true) return;
|
|
634
|
-
const anchor = keptWith(blocks, contents, index);
|
|
635
|
-
if (anchor === void 0) return;
|
|
636
|
-
anchor.id ??= `kept-${build.next++}`;
|
|
637
|
-
const ids = /* @__PURE__ */ new Set([anchor.id]);
|
|
638
|
-
const own = build.next++;
|
|
639
|
-
contents[index].headlineLevel = own;
|
|
640
|
-
build.rules.set(own, (node, queries) => node.startPosition.top > scope.top + AT_TOP && !startsOnPage(queries, ids, node.startPosition.pageNumber));
|
|
641
|
-
});
|
|
642
|
-
return contents;
|
|
643
|
-
}
|
|
644
|
-
/** The first node of `node` that prints text, or an image when no text comes first: where pdfmake records the content's start. */
|
|
645
|
-
function anchorOf(node) {
|
|
646
|
-
const inner = node.stack ?? node.columns ?? node.table?.body.flat();
|
|
647
|
-
if (inner !== void 0) {
|
|
648
|
-
for (const each of inner) {
|
|
649
|
-
const anchor = anchorOf(each);
|
|
650
|
-
if (anchor !== void 0) return anchor;
|
|
651
|
-
}
|
|
652
|
-
return;
|
|
653
|
-
}
|
|
654
|
-
return (typeof node.text === "string" ? node.text.trim() !== "" : Array.isArray(node.text)) || node.image !== void 0 ? node : void 0;
|
|
655
|
-
}
|
|
656
|
-
/**
|
|
657
|
-
* Where the block a keep-with-next paragraph at `index` stays with starts:
|
|
658
|
-
* the anchor of the next block that prints text or an image, past spacers,
|
|
659
|
-
* rules, rectangles and other keep-with-next paragraphs, so that a run of
|
|
660
|
-
* headings moves as one. None when a page break or the end of the list comes
|
|
661
|
-
* first: a heading over nothing is not kept with anything.
|
|
662
|
-
*/
|
|
663
|
-
function keptWith(blocks, contents, index) {
|
|
664
|
-
for (let next = index + 1; next < blocks.length; next++) {
|
|
665
|
-
const block = blocks[next];
|
|
666
|
-
if (block.kind === "pageBreak") return void 0;
|
|
667
|
-
const anchor = block.kind === "paragraph" && block.keepWithNext === true ? void 0 : anchorOf(contents[next]);
|
|
668
|
-
if (anchor !== void 0) return anchor;
|
|
669
|
-
}
|
|
670
|
-
}
|
|
671
|
-
function blockContent(block, build, scope) {
|
|
672
|
-
switch (block.kind) {
|
|
673
|
-
case "paragraph": return paragraphContent(block, build, scope);
|
|
674
|
-
case "table": return tableContent(block, build, scope);
|
|
675
|
-
case "image": return imageContent(block, build);
|
|
676
|
-
case "rule": return ruleContent(block, scope.width);
|
|
677
|
-
case "rectangle": return rectangleContent(block);
|
|
678
|
-
case "spacer": return {
|
|
679
|
-
stack: [],
|
|
680
|
-
margin: [
|
|
681
|
-
0,
|
|
682
|
-
block.height,
|
|
683
|
-
0,
|
|
684
|
-
0
|
|
685
|
-
]
|
|
686
|
-
};
|
|
687
|
-
case "pageBreak": return {
|
|
688
|
-
stack: [],
|
|
689
|
-
pageBreak: "before"
|
|
690
|
-
};
|
|
691
|
-
case "keepTogether": return {
|
|
692
|
-
stack: sequence(block.blocks, build, {
|
|
693
|
-
...scope,
|
|
694
|
-
kept: true
|
|
695
|
-
}),
|
|
696
|
-
unbreakable: build.mode === "render"
|
|
697
|
-
};
|
|
698
|
-
case "columns": return columnsContent(block, build, scope);
|
|
699
|
-
case "section": return sectionContent(block, build, scope);
|
|
700
|
-
}
|
|
701
|
-
}
|
|
702
|
-
function paragraphContent(paragraph, build, scope) {
|
|
703
|
-
const style = paragraph.style ?? {};
|
|
704
|
-
const content = {
|
|
705
|
-
text: paragraph.runs.length === 0 ? "" : paragraph.runs.map((run) => inline(run, scope.fields)),
|
|
706
|
-
alignment: style.align ?? "left",
|
|
707
|
-
...textProperties(style, { size: 10 })
|
|
708
|
-
};
|
|
709
|
-
const at = build.paths.get(paragraph);
|
|
710
|
-
if (scope.record) build.words.push({
|
|
711
|
-
node: content,
|
|
712
|
-
width: scope.width,
|
|
713
|
-
lines: 16,
|
|
714
|
-
what: at,
|
|
715
|
-
subject: at
|
|
716
|
-
});
|
|
717
|
-
return content;
|
|
718
|
-
}
|
|
719
|
-
function inline(run, fields) {
|
|
720
|
-
if ("text" in run) return run.bold === void 0 ? { text: run.text } : {
|
|
721
|
-
text: run.text,
|
|
722
|
-
bold: run.bold
|
|
723
|
-
};
|
|
724
|
-
if (fields === void 0) throw new Error(`a ${run.field} field outside a page region`);
|
|
725
|
-
return { text: String(fields[run.field]) };
|
|
726
|
-
}
|
|
727
|
-
/**
|
|
728
|
-
* An image in its box, which the layout reserves whole. `cover` is pdfmake's
|
|
729
|
-
* own: the box clipped, the image scaled to fill it and centred. `contain`
|
|
730
|
-
* is drawn at the size the box allows at the image's proportions, read from
|
|
731
|
-
* its pixels, and centred in the box by margins that make up the rest of it.
|
|
732
|
-
*/
|
|
733
|
-
function imageContent(image, build) {
|
|
734
|
-
const key = imageKey(image.asset, build);
|
|
735
|
-
if (image.fit === "cover") return {
|
|
736
|
-
image: key,
|
|
737
|
-
cover: {
|
|
738
|
-
width: image.width,
|
|
739
|
-
height: image.height,
|
|
740
|
-
align: "center",
|
|
741
|
-
valign: "center"
|
|
742
|
-
}
|
|
743
|
-
};
|
|
744
|
-
const pixels = build.pixels.get(image.asset);
|
|
745
|
-
const scale = Math.min(image.width / pixels.width, image.height / pixels.height);
|
|
746
|
-
const [width, height] = [pixels.width * scale, pixels.height * scale];
|
|
747
|
-
const [x, y] = [(image.width - width) / 2, (image.height - height) / 2];
|
|
748
|
-
return {
|
|
749
|
-
image: key,
|
|
750
|
-
width,
|
|
751
|
-
height,
|
|
752
|
-
margin: [
|
|
753
|
-
x,
|
|
754
|
-
y,
|
|
755
|
-
image.width - width - x,
|
|
756
|
-
y
|
|
757
|
-
]
|
|
758
|
-
};
|
|
759
|
-
}
|
|
760
|
-
function imageKey(asset, build) {
|
|
761
|
-
const known = build.imageKeys.get(asset);
|
|
762
|
-
if (known !== void 0) return known;
|
|
763
|
-
const key = `image${build.imageKeys.size}`;
|
|
764
|
-
build.imageKeys.set(asset, key);
|
|
765
|
-
build.images[key] = `data:${asset.mediaType};base64,${base64(asset.bytes)}`;
|
|
766
|
-
return key;
|
|
767
|
-
}
|
|
768
|
-
/**
|
|
769
|
-
* pdfmake makes a canvas as tall as its lowest point and strokes a line half
|
|
770
|
-
* its width to either side: the line half its width down, and the other half
|
|
771
|
-
* of it in the margin below.
|
|
772
|
-
*/
|
|
773
|
-
function ruleContent(rule, width) {
|
|
774
|
-
const half = rule.thickness / 2;
|
|
775
|
-
return {
|
|
776
|
-
canvas: [{
|
|
777
|
-
type: "line",
|
|
778
|
-
x1: 0,
|
|
779
|
-
y1: half,
|
|
780
|
-
x2: width,
|
|
781
|
-
y2: half,
|
|
782
|
-
lineWidth: rule.thickness,
|
|
783
|
-
lineColor: rule.color ?? "#000000"
|
|
784
|
-
}],
|
|
785
|
-
margin: [
|
|
786
|
-
0,
|
|
787
|
-
3,
|
|
788
|
-
0,
|
|
789
|
-
3 + half
|
|
790
|
-
]
|
|
791
|
-
};
|
|
792
|
-
}
|
|
793
|
-
/**
|
|
794
|
-
* A rectangle's box, exactly: its outline is stroked inside it, half the
|
|
795
|
-
* weight in from each edge, and the canvas, as tall as its lowest point,
|
|
796
|
-
* takes the rest of the box in a margin. A box no wider or taller than two
|
|
797
|
-
* outlines is all outline, and is filled with the outline's colour.
|
|
798
|
-
*/
|
|
799
|
-
function rectangleContent(rectangle) {
|
|
800
|
-
const { width, height, fill, stroke } = rectangle;
|
|
801
|
-
if (stroke === void 0 || Math.min(width, height) <= 1) {
|
|
802
|
-
const color = stroke ?? fill;
|
|
803
|
-
return {
|
|
804
|
-
canvas: color === void 0 ? [] : [{
|
|
805
|
-
type: "rect",
|
|
806
|
-
x: 0,
|
|
807
|
-
y: 0,
|
|
808
|
-
w: width,
|
|
809
|
-
h: height,
|
|
810
|
-
color,
|
|
811
|
-
lineWidth: 0
|
|
812
|
-
}],
|
|
813
|
-
margin: [
|
|
814
|
-
0,
|
|
815
|
-
0,
|
|
816
|
-
0,
|
|
817
|
-
color === void 0 ? height : 0
|
|
818
|
-
]
|
|
819
|
-
};
|
|
820
|
-
}
|
|
821
|
-
const half = OUTLINE / 2;
|
|
822
|
-
return {
|
|
823
|
-
canvas: [{
|
|
824
|
-
type: "rect",
|
|
825
|
-
x: half,
|
|
826
|
-
y: half,
|
|
827
|
-
w: width - OUTLINE,
|
|
828
|
-
h: height - OUTLINE,
|
|
829
|
-
lineWidth: OUTLINE,
|
|
830
|
-
lineColor: stroke,
|
|
831
|
-
...fill === void 0 ? {} : { color: fill }
|
|
832
|
-
}],
|
|
833
|
-
margin: [
|
|
834
|
-
0,
|
|
835
|
-
0,
|
|
836
|
-
0,
|
|
837
|
-
half
|
|
838
|
-
]
|
|
839
|
-
};
|
|
840
|
-
}
|
|
841
|
-
/** Columns at the widths worked out here, never pdfmake's `'*'`, which a long word widens past the page (`widths.ts`). */
|
|
842
|
-
function columnsContent(columns, build, scope) {
|
|
843
|
-
const widths = columnWidths(columns.columns.map((slot) => slot.width), scope.width);
|
|
844
|
-
return {
|
|
845
|
-
columns: columns.columns.map((slot, index) => {
|
|
846
|
-
const width = widths[index];
|
|
847
|
-
return {
|
|
848
|
-
width,
|
|
849
|
-
stack: sequence(slot.blocks, build, {
|
|
850
|
-
...scope,
|
|
851
|
-
width,
|
|
852
|
-
kept: true
|
|
853
|
-
})
|
|
854
|
-
};
|
|
855
|
-
}),
|
|
856
|
-
columnGap: 0
|
|
857
|
-
};
|
|
858
|
-
}
|
|
859
|
-
/**
|
|
860
|
-
* A section with a frame is a table of one cell: pdfmake draws its border
|
|
861
|
-
* and fill around the part of it on each page, and lays out its blocks
|
|
862
|
-
* inside the padding as it lays out the body, tables with their repeated
|
|
863
|
-
* headers and page breaks included.
|
|
864
|
-
*/
|
|
865
|
-
function sectionContent(section, build, scope) {
|
|
866
|
-
const frame = frameOf(section);
|
|
867
|
-
const border = section.border?.thickness ?? 0;
|
|
868
|
-
const color = section.border?.color ?? "#000000";
|
|
869
|
-
const padding = section.padding ?? 0;
|
|
870
|
-
const fill = section.fill;
|
|
871
|
-
const layout = {
|
|
872
|
-
hLineWidth: () => border,
|
|
873
|
-
vLineWidth: () => border,
|
|
874
|
-
hLineColor: () => color,
|
|
875
|
-
vLineColor: () => color,
|
|
876
|
-
paddingLeft: () => padding,
|
|
877
|
-
paddingRight: () => padding,
|
|
878
|
-
paddingTop: () => padding,
|
|
879
|
-
paddingBottom: () => padding,
|
|
880
|
-
...fill === void 0 ? {} : { fillColor: () => fill }
|
|
881
|
-
};
|
|
882
|
-
const width = scope.width - 2 * frame;
|
|
883
|
-
const inner = sequence(section.blocks, build, {
|
|
884
|
-
...scope,
|
|
885
|
-
width,
|
|
886
|
-
top: scope.top + frame
|
|
887
|
-
});
|
|
888
|
-
return {
|
|
889
|
-
table: {
|
|
890
|
-
widths: [width],
|
|
891
|
-
body: [[{ stack: inner }]]
|
|
892
|
-
},
|
|
893
|
-
layout
|
|
894
|
-
};
|
|
895
|
-
}
|
|
896
|
-
//#endregion
|
|
897
|
-
//#region src/bound.ts
|
|
898
|
-
/**
|
|
899
|
-
* A height the body cannot be shorter than, worked out from the document
|
|
900
|
-
* alone, so that a job too long for its page limit is refused before
|
|
901
|
-
* pdfmake measures it.
|
|
902
|
-
*
|
|
903
|
-
* pdfmake measures every node of a document before it lays out the first
|
|
904
|
-
* page, and the page limit stops only the layout: refusing 100,000 table
|
|
905
|
-
* rows against five pages took 20 seconds, all of it measuring, and nothing
|
|
906
|
-
* could interrupt it (measured on 2026-10-15, on an Intel i9-10900K under
|
|
907
|
-
* Node 24.21). A document whose least height is more than the limit's pages
|
|
908
|
-
* of body is refused here, for nothing.
|
|
909
|
-
*
|
|
910
|
-
* Least, never more than pdfmake lays out, so that nothing that fits is
|
|
911
|
-
* refused: a line of text is one line of its size, however it wraps; a
|
|
912
|
-
* table row one line of its largest text and its padding, without its rule;
|
|
913
|
-
* an image, a rectangle and a rule their own height, without the space
|
|
914
|
-
* around a rule; a frame its border and padding once. A spacer and a page
|
|
915
|
-
* break count nothing, since a spacer at the foot of a page gives up its
|
|
916
|
-
* space, and a page region is not body.
|
|
917
|
-
*/
|
|
918
|
-
function leastHeight(blocks) {
|
|
919
|
-
return blocks.reduce((sum, block) => sum + least(block), 0);
|
|
920
|
-
}
|
|
921
|
-
function least(block) {
|
|
922
|
-
switch (block.kind) {
|
|
923
|
-
case "paragraph": return block.runs.length === 0 ? 0 : line(block.style, 10);
|
|
924
|
-
case "table": return tableHeight(block);
|
|
925
|
-
case "image":
|
|
926
|
-
case "rectangle": return block.height;
|
|
927
|
-
case "rule": return block.thickness;
|
|
928
|
-
case "spacer":
|
|
929
|
-
case "pageBreak": return 0;
|
|
930
|
-
case "keepTogether": return leastHeight(block.blocks);
|
|
931
|
-
case "section": return leastHeight(block.blocks) + 2 * frameOf(block);
|
|
932
|
-
case "columns": return Math.max(0, ...block.columns.map((slot) => leastHeight(slot.blocks)));
|
|
933
|
-
}
|
|
934
|
-
}
|
|
935
|
-
function line(style, size) {
|
|
936
|
-
return (style?.size ?? size) * LINE_FACTOR;
|
|
937
|
-
}
|
|
938
|
-
/** A row: one line of the largest text in it, and the cells' padding above and below. */
|
|
939
|
-
function rowHeight(cells, style) {
|
|
940
|
-
return Math.max(0, ...cells.map((text, column) => text === "" ? 0 : line(style(column), 9))) + 4;
|
|
941
|
-
}
|
|
942
|
-
/** The header once, and each row. */
|
|
943
|
-
function tableHeight(table) {
|
|
944
|
-
const header = rowHeight(table.header, () => table.headerStyle);
|
|
945
|
-
return table.rows.reduce((sum, row) => sum + rowHeight(row.cells, (column) => row.cellStyles?.[column] ?? row.style), header);
|
|
946
|
-
}
|
|
947
|
-
/** Refuses a body whose least height needs more than `maxPages` pages of `bodyHeight`. */
|
|
948
|
-
function refuseOversized(blocks, bodyHeight, maxPages) {
|
|
949
|
-
const height = leastHeight(blocks);
|
|
950
|
-
if (height > maxPages * bodyHeight) throw new RenderRefusal("too-many-pages", `the report needs more than ${maxPages} pages: its body is at least ${height.toFixed(2)} pt tall, and ${maxPages} pages hold ${(maxPages * bodyHeight).toFixed(2)} pt`);
|
|
951
|
-
}
|
|
952
|
-
//#endregion
|
|
953
|
-
//#region src/assets.ts
|
|
954
|
-
/**
|
|
955
|
-
* Image assets checked before pdfmake sees them. pdfmake, through PDFKit,
|
|
956
|
-
* copies a PNG's compressed data and a JPEG's scan into the PDF without
|
|
957
|
-
* decoding either, so bytes that only look like an image arrive in the PDF
|
|
958
|
-
* as a broken one, and bytes that do not look like one at all fail with an
|
|
959
|
-
* error that names no asset. Both are refused here instead, naming it, as
|
|
960
|
-
* `invalid-asset`.
|
|
961
|
-
*
|
|
962
|
-
* What is checked: the size limits; for a PNG, every chunk's length and CRC,
|
|
963
|
-
* the header, and that the image data inflates to exactly the scanlines the
|
|
964
|
-
* header promises, each with a known filter; for a JPEG, the segment
|
|
965
|
-
* structure up to the scan, the frame header's size, and the end marker. A
|
|
966
|
-
* JPEG's entropy-coded scan is not decoded: a corrupt scan inside an
|
|
967
|
-
* otherwise well-formed JPEG still reaches the PDF.
|
|
968
|
-
*
|
|
969
|
-
* The size returned is the size the image is shown at: a JPEG whose Exif
|
|
970
|
-
* orientation turns it a quarter turn is as wide as its frame is tall, as
|
|
971
|
-
* PDFKit draws it. The check runs on the standard library alone, so that the
|
|
972
|
-
* browser entry makes exactly the same one (`bytes.ts`).
|
|
973
|
-
*/
|
|
974
|
-
/** Pixels a side. */
|
|
975
|
-
const MAX_ASSET_SIDE = 4096;
|
|
976
|
-
/**
|
|
977
|
-
* The image data one render may hold, once inflated: two images of the
|
|
978
|
-
* largest kind an asset may be, 4096 by 4096 pixels of 16-bit RGBA, whose
|
|
979
|
-
* scanlines inflate to 134,221,824 bytes each. PDFKit decodes such a PNG to
|
|
980
|
-
* embed it: one took 1.8 seconds and grew the process by 343 MB, two 3.4
|
|
981
|
-
* seconds, four 6.5 seconds (measured on 2026-10-15, on an Intel i9-10900K
|
|
982
|
-
* under Node 24.21). A JPEG is embedded as it is, and counts its bytes.
|
|
983
|
-
*/
|
|
984
|
-
const MAX_IMAGE_DATA = 2 * MAX_ASSET_SIDE * 32769;
|
|
985
|
-
/** The asset's size in pixels, once its bytes pass; refused as `invalid-asset`, naming it and why, when they do not. */
|
|
986
|
-
async function checkImageAsset(asset) {
|
|
987
|
-
const read = await readAsset(asset);
|
|
988
|
-
if (typeof read === "string") throw new RenderRefusal("invalid-asset", `the image asset ${asset.id} ${read}`, asset.id);
|
|
989
|
-
return read;
|
|
990
|
-
}
|
|
991
|
-
/** The asset's size in pixels, or what is wrong with it. */
|
|
992
|
-
async function readAsset(asset) {
|
|
993
|
-
const { bytes } = asset;
|
|
994
|
-
if (!(bytes instanceof Uint8Array)) return "has no bytes";
|
|
995
|
-
if (bytes.length > MAX_ASSET_BYTES) return `is ${bytes.length} bytes; the limit is ${MAX_ASSET_BYTES} (20 MB)`;
|
|
996
|
-
const isPng = startsWith(bytes, PNG_SIGNATURE);
|
|
997
|
-
const isJpeg = bytes[0] === 255 && bytes[1] === 216;
|
|
998
|
-
if (asset.mediaType === "image/png") return isPng ? readPng(bytes) : `is declared a PNG and is not one${isJpeg ? ": it is a JPEG" : ""}`;
|
|
999
|
-
if (asset.mediaType === "image/jpeg") return isJpeg ? readJpeg(bytes) : `is declared a JPEG and is not one${isPng ? ": it is a PNG" : ""}`;
|
|
1000
|
-
return `is of a media type the renderer does not draw (${String(asset.mediaType)})`;
|
|
1001
|
-
}
|
|
1002
|
-
function sizeProblem(width, height) {
|
|
1003
|
-
if (width === 0 || height === 0) return `has no pixels (${width} × ${height})`;
|
|
1004
|
-
if (width > 4096 || height > 4096) return `is ${width} × ${height} pixels; the limit is ${MAX_ASSET_SIDE} pixels a side`;
|
|
1005
|
-
}
|
|
1006
|
-
const PNG_SIGNATURE = [
|
|
1007
|
-
137,
|
|
1008
|
-
80,
|
|
1009
|
-
78,
|
|
1010
|
-
71,
|
|
1011
|
-
13,
|
|
1012
|
-
10,
|
|
1013
|
-
26,
|
|
1014
|
-
10
|
|
1015
|
-
];
|
|
1016
|
-
/** Channels per pixel for each PNG colour type, and the bit depths it allows. */
|
|
1017
|
-
const PNG_COLOUR_TYPES = /* @__PURE__ */ new Map([
|
|
1018
|
-
[0, {
|
|
1019
|
-
channels: 1,
|
|
1020
|
-
depths: [
|
|
1021
|
-
1,
|
|
1022
|
-
2,
|
|
1023
|
-
4,
|
|
1024
|
-
8,
|
|
1025
|
-
16
|
|
1026
|
-
]
|
|
1027
|
-
}],
|
|
1028
|
-
[2, {
|
|
1029
|
-
channels: 3,
|
|
1030
|
-
depths: [8, 16]
|
|
1031
|
-
}],
|
|
1032
|
-
[3, {
|
|
1033
|
-
channels: 1,
|
|
1034
|
-
depths: [
|
|
1035
|
-
1,
|
|
1036
|
-
2,
|
|
1037
|
-
4,
|
|
1038
|
-
8
|
|
1039
|
-
]
|
|
1040
|
-
}],
|
|
1041
|
-
[4, {
|
|
1042
|
-
channels: 2,
|
|
1043
|
-
depths: [8, 16]
|
|
1044
|
-
}],
|
|
1045
|
-
[6, {
|
|
1046
|
-
channels: 4,
|
|
1047
|
-
depths: [8, 16]
|
|
1048
|
-
}]
|
|
1049
|
-
]);
|
|
1050
|
-
/** Adam7's seven passes: the first column and row of each, and the steps between them. */
|
|
1051
|
-
const ADAM7 = [
|
|
1052
|
-
[
|
|
1053
|
-
0,
|
|
1054
|
-
0,
|
|
1055
|
-
8,
|
|
1056
|
-
8
|
|
1057
|
-
],
|
|
1058
|
-
[
|
|
1059
|
-
4,
|
|
1060
|
-
0,
|
|
1061
|
-
8,
|
|
1062
|
-
8
|
|
1063
|
-
],
|
|
1064
|
-
[
|
|
1065
|
-
0,
|
|
1066
|
-
4,
|
|
1067
|
-
4,
|
|
1068
|
-
8
|
|
1069
|
-
],
|
|
1070
|
-
[
|
|
1071
|
-
2,
|
|
1072
|
-
0,
|
|
1073
|
-
4,
|
|
1074
|
-
4
|
|
1075
|
-
],
|
|
1076
|
-
[
|
|
1077
|
-
0,
|
|
1078
|
-
2,
|
|
1079
|
-
2,
|
|
1080
|
-
4
|
|
1081
|
-
],
|
|
1082
|
-
[
|
|
1083
|
-
1,
|
|
1084
|
-
0,
|
|
1085
|
-
2,
|
|
1086
|
-
2
|
|
1087
|
-
],
|
|
1088
|
-
[
|
|
1089
|
-
0,
|
|
1090
|
-
1,
|
|
1091
|
-
1,
|
|
1092
|
-
2
|
|
1093
|
-
]
|
|
1094
|
-
];
|
|
1095
|
-
async function readPng(bytes) {
|
|
1096
|
-
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
1097
|
-
let header;
|
|
1098
|
-
let palette = false;
|
|
1099
|
-
const data = [];
|
|
1100
|
-
for (let offset = PNG_SIGNATURE.length;;) {
|
|
1101
|
-
if (offset + 12 > bytes.length || offset + 12 + view.getUint32(offset) > bytes.length) return `is cut off at byte ${offset}`;
|
|
1102
|
-
const length = view.getUint32(offset);
|
|
1103
|
-
const end = offset + 12 + length;
|
|
1104
|
-
const type = String.fromCharCode(...bytes.subarray(offset + 4, offset + 8));
|
|
1105
|
-
const body = bytes.subarray(offset + 8, offset + 8 + length);
|
|
1106
|
-
if (crc32(bytes.subarray(offset + 4, offset + 8 + length)) !== view.getUint32(offset + 8 + length)) return `has the wrong CRC on its ${type} chunk`;
|
|
1107
|
-
if (header === void 0) {
|
|
1108
|
-
if (type !== "IHDR") return "does not start with its header chunk";
|
|
1109
|
-
const parsed = pngHeader(body);
|
|
1110
|
-
if (typeof parsed === "string") return parsed;
|
|
1111
|
-
header = parsed;
|
|
1112
|
-
} else if (type === "PLTE") palette = true;
|
|
1113
|
-
else if (type === "IDAT") data.push(body);
|
|
1114
|
-
else if (type === "IEND") {
|
|
1115
|
-
if (end !== bytes.length) return "has bytes after its end chunk";
|
|
1116
|
-
break;
|
|
1117
|
-
}
|
|
1118
|
-
offset = end;
|
|
1119
|
-
}
|
|
1120
|
-
if (header.paletted && !palette) return "is paletted and has no palette";
|
|
1121
|
-
return await scanlineProblem(header, data) ?? {
|
|
1122
|
-
width: header.width,
|
|
1123
|
-
height: header.height,
|
|
1124
|
-
data: inflatedLength(header)
|
|
1125
|
-
};
|
|
1126
|
-
}
|
|
1127
|
-
function pngHeader(body) {
|
|
1128
|
-
if (body.length !== 13) return "has a header chunk of the wrong length";
|
|
1129
|
-
const view = new DataView(body.buffer, body.byteOffset, body.byteLength);
|
|
1130
|
-
const width = view.getUint32(0);
|
|
1131
|
-
const height = view.getUint32(4);
|
|
1132
|
-
const size = sizeProblem(width, height);
|
|
1133
|
-
if (size !== void 0) return size;
|
|
1134
|
-
const [depth = 0, colourType = 0, compression, filter, interlace = 0] = body.subarray(8);
|
|
1135
|
-
const colour = PNG_COLOUR_TYPES.get(colourType);
|
|
1136
|
-
if (colour === void 0 || !colour.depths.includes(depth)) return `has an invalid colour type and bit depth (${colourType}, ${depth})`;
|
|
1137
|
-
if (compression !== 0 || filter !== 0 || interlace > 1) return "has an unknown compression, filter or interlace method";
|
|
1138
|
-
return {
|
|
1139
|
-
width,
|
|
1140
|
-
height,
|
|
1141
|
-
bitsPerPixel: colour.channels * depth,
|
|
1142
|
-
interlaced: interlace === 1,
|
|
1143
|
-
paletted: colourType === 3
|
|
1144
|
-
};
|
|
1145
|
-
}
|
|
1146
|
-
/** The scanlines of each pass of the image: the bytes of one, its filter byte included, and how many there are. */
|
|
1147
|
-
function scanlines(header) {
|
|
1148
|
-
return (header.interlaced ? ADAM7.map(([x, y, dx, dy]) => [Math.ceil((header.width - x) / dx), Math.ceil((header.height - y) / dy)]) : [[header.width, header.height]]).filter(([width, height]) => width > 0 && height > 0).map(([width, height]) => ({
|
|
1149
|
-
bytes: 1 + Math.ceil(width * header.bitsPerPixel / 8),
|
|
1150
|
-
count: height
|
|
1151
|
-
}));
|
|
1152
|
-
}
|
|
1153
|
-
/** The bytes the image data inflates to, as the header promises. */
|
|
1154
|
-
function inflatedLength(header) {
|
|
1155
|
-
return scanlines(header).reduce((total, row) => total + row.bytes * row.count, 0);
|
|
1156
|
-
}
|
|
1157
|
-
/**
|
|
1158
|
-
* Whether the image data inflates to exactly the scanlines the header
|
|
1159
|
-
* promises, each starting with a filter type PNG defines. Read as it
|
|
1160
|
-
* inflates: the filter bytes are where the scanlines say, so nothing of the
|
|
1161
|
-
* image is kept.
|
|
1162
|
-
*/
|
|
1163
|
-
async function scanlineProblem(header, data) {
|
|
1164
|
-
if (data.length === 0) return "has no image data";
|
|
1165
|
-
const rows = scanlines(header);
|
|
1166
|
-
const expected = inflatedLength(header);
|
|
1167
|
-
const filters = filterOffsets(rows);
|
|
1168
|
-
let next = filters.next();
|
|
1169
|
-
let unknown;
|
|
1170
|
-
const inflated = await inflate(concat(data), expected, (chunk, offset) => {
|
|
1171
|
-
while (unknown === void 0 && !next.done && next.value < offset + chunk.length) {
|
|
1172
|
-
const filter = chunk[next.value - offset];
|
|
1173
|
-
if (filter > 4) unknown = filter;
|
|
1174
|
-
next = filters.next();
|
|
1175
|
-
}
|
|
1176
|
-
});
|
|
1177
|
-
if (inflated.kind === "too-long") return `has more image data than the ${expected} bytes its header promises`;
|
|
1178
|
-
if (inflated.kind === "invalid") return "has image data that does not inflate";
|
|
1179
|
-
if (inflated.length < expected) return `has image data of ${inflated.length} bytes where its header promises ${expected}`;
|
|
1180
|
-
return unknown === void 0 ? void 0 : `has a scanline with an unknown filter type (${unknown})`;
|
|
1181
|
-
}
|
|
1182
|
-
/** Where each scanline's filter byte is in the inflated data, in order. */
|
|
1183
|
-
function* filterOffsets(rows) {
|
|
1184
|
-
let offset = 0;
|
|
1185
|
-
for (const row of rows) for (let index = 0; index < row.count; index++, offset += row.bytes) yield offset;
|
|
1186
|
-
}
|
|
1187
|
-
/** Start-of-frame markers: every SOFn but DHT (C4), JPG (C8) and DAC (CC). */
|
|
1188
|
-
const JPEG_FRAMES = /* @__PURE__ */ new Set([
|
|
1189
|
-
192,
|
|
1190
|
-
193,
|
|
1191
|
-
194,
|
|
1192
|
-
195,
|
|
1193
|
-
197,
|
|
1194
|
-
198,
|
|
1195
|
-
199,
|
|
1196
|
-
201,
|
|
1197
|
-
202,
|
|
1198
|
-
203,
|
|
1199
|
-
205,
|
|
1200
|
-
206,
|
|
1201
|
-
207
|
|
1202
|
-
]);
|
|
1203
|
-
/** APP1, where an Exif block lives. */
|
|
1204
|
-
const APP1 = 225;
|
|
1205
|
-
/**
|
|
1206
|
-
* The segments before the scan, each a marker and a length, as a baseline or
|
|
1207
|
-
* progressive encoder writes them. Fill bytes between segments, which the
|
|
1208
|
-
* standard allows and encoders do not write, are refused with the rest of
|
|
1209
|
-
* what is malformed: failing closed costs a JPEG that could have been drawn.
|
|
1210
|
-
*/
|
|
1211
|
-
function readJpeg(bytes) {
|
|
1212
|
-
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
1213
|
-
let pixels;
|
|
1214
|
-
let orientation;
|
|
1215
|
-
for (let offset = 2; offset + 4 <= bytes.length;) {
|
|
1216
|
-
const marker = bytes[offset + 1];
|
|
1217
|
-
if (bytes[offset] !== 255 || marker === 255) return `has a malformed segment at byte ${offset}`;
|
|
1218
|
-
const length = view.getUint16(offset + 2);
|
|
1219
|
-
if (length < 2 || offset + 2 + length > bytes.length) return `has a segment that runs past its end at byte ${offset}`;
|
|
1220
|
-
if (JPEG_FRAMES.has(marker)) {
|
|
1221
|
-
if (length < 8) return "has a frame header of the wrong length";
|
|
1222
|
-
const [width, height] = [view.getUint16(offset + 7), view.getUint16(offset + 5)];
|
|
1223
|
-
const size = sizeProblem(width, height);
|
|
1224
|
-
if (size !== void 0) return size;
|
|
1225
|
-
pixels = {
|
|
1226
|
-
width,
|
|
1227
|
-
height,
|
|
1228
|
-
data: bytes.length
|
|
1229
|
-
};
|
|
1230
|
-
}
|
|
1231
|
-
if (marker === APP1 && orientation === void 0 && startsWith(bytes.subarray(offset + 4), EXIF)) orientation = exifOrientation(bytes.subarray(offset + 4 + EXIF.length, offset + 2 + length));
|
|
1232
|
-
if (marker === 218) {
|
|
1233
|
-
if (pixels === void 0) return "has image data before its frame header";
|
|
1234
|
-
if (bytes[bytes.length - 2] !== 255 || bytes[bytes.length - 1] !== 217) return "is cut off: it does not end with an end-of-image marker";
|
|
1235
|
-
return (orientation ?? 0) > 4 ? {
|
|
1236
|
-
...pixels,
|
|
1237
|
-
width: pixels.height,
|
|
1238
|
-
height: pixels.width
|
|
1239
|
-
} : pixels;
|
|
1240
|
-
}
|
|
1241
|
-
offset += 2 + length;
|
|
1242
|
-
}
|
|
1243
|
-
return "is cut off before its image data";
|
|
1244
|
-
}
|
|
1245
|
-
/** What an APP1 segment that holds Exif data starts with. */
|
|
1246
|
-
const EXIF = [
|
|
1247
|
-
69,
|
|
1248
|
-
120,
|
|
1249
|
-
105,
|
|
1250
|
-
102,
|
|
1251
|
-
0,
|
|
1252
|
-
0
|
|
1253
|
-
];
|
|
1254
|
-
/**
|
|
1255
|
-
* The orientation the first Exif block gives its image, read as PDFKit reads
|
|
1256
|
-
* it: the Orientation tag (0x0112) of the first image file directory, from 1
|
|
1257
|
-
* to 8. Anything else -- a block cut short, no such tag, a value out of
|
|
1258
|
-
* range -- is no orientation, which PDFKit draws as 1, upright. Only the
|
|
1259
|
-
* first Exif block counts, for PDFKit and here, so `0` stands for one read
|
|
1260
|
-
* without an orientation.
|
|
1261
|
-
*/
|
|
1262
|
-
function exifOrientation(block) {
|
|
1263
|
-
if (block.length < 8) return 0;
|
|
1264
|
-
const tiff = new DataView(block.buffer, block.byteOffset, block.byteLength);
|
|
1265
|
-
const order = tiff.getUint16(0);
|
|
1266
|
-
if (order !== 18761 && order !== 19789) return 0;
|
|
1267
|
-
const little = order === 18761;
|
|
1268
|
-
if (tiff.getUint16(2, little) !== 42) return 0;
|
|
1269
|
-
const directory = tiff.getUint32(4, little);
|
|
1270
|
-
if (directory + 2 > tiff.byteLength) return 0;
|
|
1271
|
-
const entries = tiff.getUint16(directory, little);
|
|
1272
|
-
for (let index = 0; index < entries; index++) {
|
|
1273
|
-
const entry = directory + 2 + index * 12;
|
|
1274
|
-
if (entry + 12 > tiff.byteLength) return 0;
|
|
1275
|
-
if (tiff.getUint16(entry, little) === 274) {
|
|
1276
|
-
const value = tiff.getUint16(entry + 8, little);
|
|
1277
|
-
return value >= 1 && value <= 8 ? value : 0;
|
|
1278
|
-
}
|
|
1279
|
-
}
|
|
1280
|
-
return 0;
|
|
1281
|
-
}
|
|
1282
|
-
function startsWith(bytes, prefix) {
|
|
1283
|
-
return prefix.every((byte, index) => bytes[index] === byte);
|
|
1284
|
-
}
|
|
1285
|
-
function concat(parts) {
|
|
1286
|
-
const joined = new Uint8Array(parts.reduce((total, part) => total + part.length, 0));
|
|
1287
|
-
let offset = 0;
|
|
1288
|
-
for (const part of parts) {
|
|
1289
|
-
joined.set(part, offset);
|
|
1290
|
-
offset += part.length;
|
|
1291
|
-
}
|
|
1292
|
-
return joined;
|
|
1293
|
-
}
|
|
1294
|
-
//#endregion
|
|
1295
|
-
//#region src/check.ts
|
|
1296
|
-
/** Portrait sizes in points, as PDF engines write them: ISO A4 is 210 by 297 millimetres, US Letter 8.5 by 11 inches. */
|
|
1297
|
-
const PAPER = {
|
|
1298
|
-
A4: {
|
|
1299
|
-
width: 595.28,
|
|
1300
|
-
height: 841.89
|
|
1301
|
-
},
|
|
1302
|
-
Letter: {
|
|
1303
|
-
width: 612,
|
|
1304
|
-
height: 792
|
|
1305
|
-
}
|
|
1306
|
-
};
|
|
1307
|
-
async function checkDocument(document, glyphs) {
|
|
1308
|
-
const creationDate = new Date(document.info.creationDate);
|
|
1309
|
-
if (Number.isNaN(creationDate.getTime())) throw new RenderRefusal("unsupported", `the creation date '${document.info.creationDate}' is not a date`, "info.creationDate");
|
|
1310
|
-
if (typeof document.info.title !== "string") throw new RenderRefusal("unsupported", "the title is not text", "info.title");
|
|
1311
|
-
const page = pageOf(document.page);
|
|
1312
|
-
const walk = {
|
|
1313
|
-
glyphs,
|
|
1314
|
-
paths: /* @__PURE__ */ new Map(),
|
|
1315
|
-
images: [],
|
|
1316
|
-
tall: [],
|
|
1317
|
-
measured: []
|
|
1318
|
-
};
|
|
1319
|
-
const top = {
|
|
1320
|
-
kept: false,
|
|
1321
|
-
column: false,
|
|
1322
|
-
width: page.width,
|
|
1323
|
-
inset: 0,
|
|
1324
|
-
depth: 0
|
|
1325
|
-
};
|
|
1326
|
-
checkBlocks(document.pageHeader, "pageHeader", {
|
|
1327
|
-
...top,
|
|
1328
|
-
region: true
|
|
1329
|
-
}, walk);
|
|
1330
|
-
checkBlocks(document.pageFooter, "pageFooter", {
|
|
1331
|
-
...top,
|
|
1332
|
-
region: true
|
|
1333
|
-
}, walk);
|
|
1334
|
-
checkBlocks(document.body, "body", {
|
|
1335
|
-
...top,
|
|
1336
|
-
region: false
|
|
1337
|
-
}, walk);
|
|
1338
|
-
const pixels = /* @__PURE__ */ new Map();
|
|
1339
|
-
let data = 0;
|
|
1340
|
-
for (const { image } of walk.images) {
|
|
1341
|
-
if (pixels.has(image.asset)) continue;
|
|
1342
|
-
const read = await checkImageAsset(image.asset);
|
|
1343
|
-
data += read.data;
|
|
1344
|
-
if (data > 268443648) throw new RenderRefusal("content-too-large", `the images up to ${image.asset.id} hold ${data} bytes of image data; a render may hold ${MAX_IMAGE_DATA}`, image.asset.id);
|
|
1345
|
-
pixels.set(image.asset, read);
|
|
1346
|
-
}
|
|
1347
|
-
return {
|
|
1348
|
-
page,
|
|
1349
|
-
creationDate,
|
|
1350
|
-
pixels,
|
|
1351
|
-
tall: walk.tall,
|
|
1352
|
-
paths: walk.paths,
|
|
1353
|
-
measured: walk.measured
|
|
1354
|
-
};
|
|
1355
|
-
}
|
|
1356
|
-
function pageOf(setup) {
|
|
1357
|
-
const paper = Object.hasOwn(PAPER, setup.size) ? PAPER[setup.size] : void 0;
|
|
1358
|
-
if (paper === void 0) throw new RenderRefusal("unsupported", `the page size '${String(setup.size)}' is not one the renderer knows`, "page.size");
|
|
1359
|
-
if (setup.orientation !== "portrait" && setup.orientation !== "landscape") throw new RenderRefusal("unsupported", `the orientation '${String(setup.orientation)}' is not one the renderer knows`, "page.orientation");
|
|
1360
|
-
const [paperWidth, paperHeight] = setup.orientation === "portrait" ? [paper.width, paper.height] : [paper.height, paper.width];
|
|
1361
|
-
const { margins } = setup;
|
|
1362
|
-
if (![
|
|
1363
|
-
margins.top,
|
|
1364
|
-
margins.right,
|
|
1365
|
-
margins.bottom,
|
|
1366
|
-
margins.left
|
|
1367
|
-
].every((margin) => Number.isFinite(margin) && margin >= 0)) throw new RenderRefusal("unsupported", "a page margin is negative or not a number", "page.margins");
|
|
1368
|
-
const width = paperWidth - margins.left - margins.right;
|
|
1369
|
-
const height = paperHeight - margins.top - margins.bottom;
|
|
1370
|
-
if (width <= 0 || height <= 0) throw new RenderRefusal("content-too-large", `the margins leave no printable area (${width.toFixed(2)} × ${height.toFixed(2)} pt)`, "page.margins");
|
|
1371
|
-
return {
|
|
1372
|
-
paperWidth,
|
|
1373
|
-
paperHeight,
|
|
1374
|
-
margins,
|
|
1375
|
-
width,
|
|
1376
|
-
height
|
|
1377
|
-
};
|
|
1378
|
-
}
|
|
1379
|
-
function checkBlocks(blocks, path, place, walk) {
|
|
1380
|
-
if (place.depth > 16) throw new RenderRefusal("unsupported", `${path} is nested more than 16 blocks deep`, path);
|
|
1381
|
-
blocks.forEach((block, index) => checkBlock(block, `${path}[${index}]`, place, walk));
|
|
1382
|
-
}
|
|
1383
|
-
function checkBlock(block, at, place, walk) {
|
|
1384
|
-
switch (block.kind) {
|
|
1385
|
-
case "paragraph":
|
|
1386
|
-
walk.paths.set(block, at);
|
|
1387
|
-
if (!place.region && block.runs.some((run) => "field" in run)) throw new RenderRefusal("unsupported", `${at} prints a page field; pdfmake knows the page number only in the page header and footer`, at);
|
|
1388
|
-
checkStyle(block.style, at);
|
|
1389
|
-
if (block.runs.some((run) => !("text" in run) && run.field !== "pageNumber" && run.field !== "pageCount")) throw new RenderRefusal("unsupported", `${at} prints a field the renderer does not know`, at);
|
|
1390
|
-
for (const run of block.runs) if ("text" in run) checkText(run.text, faceOf(run.bold ?? block.style?.bold ?? false, block.style?.italic ?? false), walk, at);
|
|
1391
|
-
tall(walk, place, (block.style?.size ?? 0) * LINE_FACTOR, `a line of ${at}`, at);
|
|
1392
|
-
return;
|
|
1393
|
-
case "table":
|
|
1394
|
-
if (place.column) throw new RenderRefusal("unsupported", `table ${block.id} stands inside a columns block; a table is part of the flow`, block.id);
|
|
1395
|
-
checkTable(block, place.width);
|
|
1396
|
-
checkTableText(block, walk);
|
|
1397
|
-
tall(walk, place, tallestCellLine(block), `a line of table ${block.id}`, block.id);
|
|
1398
|
-
if (!place.region && !place.kept) walk.measured.push({
|
|
1399
|
-
block,
|
|
1400
|
-
at,
|
|
1401
|
-
width: place.width,
|
|
1402
|
-
inset: place.inset
|
|
1403
|
-
});
|
|
1404
|
-
return;
|
|
1405
|
-
case "image":
|
|
1406
|
-
checkImage(block, place, walk);
|
|
1407
|
-
walk.images.push({
|
|
1408
|
-
image: block,
|
|
1409
|
-
at
|
|
1410
|
-
});
|
|
1411
|
-
return;
|
|
1412
|
-
case "rule":
|
|
1413
|
-
if (!(block.thickness > 0 && Number.isFinite(block.thickness))) throw new RenderRefusal("unsupported", `${at} is a rule ${block.thickness} pt thick`, at);
|
|
1414
|
-
checkColor(block.color, at);
|
|
1415
|
-
tall(walk, place, block.thickness + 6, `the rule ${at}`, at);
|
|
1416
|
-
return;
|
|
1417
|
-
case "rectangle":
|
|
1418
|
-
checkRectangle(block, at, place, walk);
|
|
1419
|
-
return;
|
|
1420
|
-
case "spacer":
|
|
1421
|
-
if (!(block.height >= 0 && Number.isFinite(block.height))) throw new RenderRefusal("unsupported", `${at} is a spacer ${block.height} pt tall`, at);
|
|
1422
|
-
return;
|
|
1423
|
-
case "pageBreak": {
|
|
1424
|
-
const inside = place.region ? "page region" : place.kept ? "keep-together block" : place.column ? "columns block" : void 0;
|
|
1425
|
-
if (inside !== void 0) throw new RenderRefusal("unsupported", `${at} is a page break inside a ${inside}`, at);
|
|
1426
|
-
return;
|
|
1427
|
-
}
|
|
1428
|
-
case "keepTogether":
|
|
1429
|
-
if (!place.region && !place.kept) walk.measured.push({
|
|
1430
|
-
block,
|
|
1431
|
-
at,
|
|
1432
|
-
width: place.width,
|
|
1433
|
-
inset: place.inset
|
|
1434
|
-
});
|
|
1435
|
-
checkBlocks(block.blocks, `${at}.blocks`, {
|
|
1436
|
-
...place,
|
|
1437
|
-
kept: true,
|
|
1438
|
-
depth: place.depth + 1
|
|
1439
|
-
}, walk);
|
|
1440
|
-
return;
|
|
1441
|
-
case "columns":
|
|
1442
|
-
checkColumns(block, at, place, walk);
|
|
1443
|
-
return;
|
|
1444
|
-
case "section":
|
|
1445
|
-
checkSection(block, at, place, walk);
|
|
1446
|
-
return;
|
|
1447
|
-
default: throw new RenderRefusal("unsupported", `${at} is a ${String(block.kind)} block, which the renderer does not know`, at);
|
|
1448
|
-
}
|
|
1449
|
-
}
|
|
1450
|
-
/** Something drawn at a fixed height in the body: judged against the body once the regions are measured. A page region's is judged by measuring the region. */
|
|
1451
|
-
function tall(walk, place, height, what, subject) {
|
|
1452
|
-
if (!place.region) walk.tall.push({
|
|
1453
|
-
height,
|
|
1454
|
-
inset: place.inset,
|
|
1455
|
-
what,
|
|
1456
|
-
subject
|
|
1457
|
-
});
|
|
1458
|
-
}
|
|
1459
|
-
/**
|
|
1460
|
-
* Text, held to being a string -- an array would reach pdfmake as inlines
|
|
1461
|
-
* carrying any property of its own, a link among them -- and to the face it
|
|
1462
|
-
* prints in, which must have a glyph for each of its characters.
|
|
1463
|
-
*/
|
|
1464
|
-
function checkText(text, face, walk, at, subject = at) {
|
|
1465
|
-
if (typeof text !== "string") throw new RenderRefusal("unsupported", `${at} prints something that is not text`, subject);
|
|
1466
|
-
const missing = missingGlyph(text, walk.glyphs[face]);
|
|
1467
|
-
if (missing !== void 0) throw new RenderRefusal("unsupported", `${at} prints ${codePoint(missing)}, which Roboto has no glyph for: it would print as an empty box, and as nothing in the PDF's text, and no other font is embedded`, subject);
|
|
1468
|
-
}
|
|
1469
|
-
/** Every cell of `table` as text in the face it prints in: the renderer's weight for its row's kind, under the cell's style. */
|
|
1470
|
-
function checkTableText(table, walk) {
|
|
1471
|
-
const at = `table ${table.id}`;
|
|
1472
|
-
const face = (style, bold, italic = false) => faceOf(style?.bold ?? bold, style?.italic ?? italic);
|
|
1473
|
-
table.header.forEach((text, column) => checkText(text, face(table.headerStyle, KIND_BOLD.header), walk, `${at}'s header, column ${column + 1},`, table.id));
|
|
1474
|
-
checkText(table.emptyText, face(void 0, KIND_BOLD.empty, true), walk, `${at}'s empty text`, table.id);
|
|
1475
|
-
table.rows.forEach((row, index) => {
|
|
1476
|
-
row.cells.forEach((text, column) => {
|
|
1477
|
-
checkText(text, face(row.cellStyles?.[column] ?? row.style, KIND_BOLD[row.kind]), walk, `${at}, row ${index}, column ${column + 1},`, table.id);
|
|
1478
|
-
});
|
|
1479
|
-
});
|
|
1480
|
-
}
|
|
1481
|
-
function checkStyle(style, at) {
|
|
1482
|
-
if (style === void 0) return;
|
|
1483
|
-
if (style.font !== void 0 && !Object.hasOwn(FONTS, style.font)) throw new RenderRefusal("unsupported", `${at} is set in the ${String(style.font)} font; the renderer embeds Roboto alone, as the sans font, and prints no other in its place`, at);
|
|
1484
|
-
if (style.size !== void 0 && !(style.size > 0 && Number.isFinite(style.size))) throw new RenderRefusal("unsupported", `${at} is set at ${style.size} pt`, at);
|
|
1485
|
-
if (style.align !== void 0 && style.align !== "left" && style.align !== "center" && style.align !== "right") throw new RenderRefusal("unsupported", `${at} is aligned '${String(style.align)}'`, at);
|
|
1486
|
-
checkColor(style.color, at);
|
|
1487
|
-
}
|
|
1488
|
-
function checkColor(color, at) {
|
|
1489
|
-
if (color !== void 0 && !COLOR.test(color)) throw new RenderRefusal("unsupported", `${at} names the colour '${color}', which is not #rrggbb in lower case`, at);
|
|
1490
|
-
}
|
|
1491
|
-
function checkTable(table, width) {
|
|
1492
|
-
const columns = table.columns.length;
|
|
1493
|
-
if (columns === 0) throw new RenderRefusal("unsupported", `table ${table.id} has no columns`, table.id);
|
|
1494
|
-
if (table.columns.some((column) => column.align !== "left" && column.align !== "center" && column.align !== "right")) throw new RenderRefusal("unsupported", `table ${table.id} aligns a column in a way the renderer does not know`, table.id);
|
|
1495
|
-
if (table.header.length !== columns) throw new RenderRefusal("unsupported", `the header of table ${table.id} has ${table.header.length} cells for ${columns} columns`, table.id);
|
|
1496
|
-
table.rows.forEach((row, index) => {
|
|
1497
|
-
const cells = row.across === true ? 1 : columns;
|
|
1498
|
-
if (row.cells.length !== cells) throw new RenderRefusal("unsupported", `row ${index} of table ${table.id} has ${row.cells.length} cells for ${row.across === true ? "a row across the table, which has one" : `${columns} columns`}`, table.id);
|
|
1499
|
-
if (row.cellStyles !== void 0 && row.cellStyles.length !== row.cells.length) throw new RenderRefusal("unsupported", `row ${index} of table ${table.id} has ${row.cellStyles.length} cell styles for ${row.cells.length} cells`, table.id);
|
|
1500
|
-
for (const style of [row.style, ...row.cellStyles ?? []]) checkStyle(style ?? void 0, table.id);
|
|
1501
|
-
});
|
|
1502
|
-
checkStyle(table.headerStyle, table.id);
|
|
1503
|
-
const narrow = table.columns.find((column) => column.width !== "fill" && !(column.width > 8 && Number.isFinite(column.width)));
|
|
1504
|
-
if (narrow !== void 0) throw new RenderRefusal("unsupported", `table ${table.id} has a column of ${String(narrow.width)} pt, no wider than its cell padding`, table.id);
|
|
1505
|
-
const widths = table.columns.map((column) => column.width);
|
|
1506
|
-
const fixed = widths.reduce((sum, each) => sum + fixedWidth(each), 0);
|
|
1507
|
-
if (fixed > width || fillWidth(widths, width) <= 8) throw new RenderRefusal("content-too-large", `table ${table.id}'s fixed columns are ${fixed} pt wide; it stands ${width.toFixed(2)} pt wide`, table.id);
|
|
1508
|
-
}
|
|
1509
|
-
/** The tallest line a cell of the table prints, with the cell's padding: a row no page can start. */
|
|
1510
|
-
function tallestCellLine(table) {
|
|
1511
|
-
const styles = [table.headerStyle, ...table.rows.flatMap((row) => [row.style, ...row.cellStyles ?? []])];
|
|
1512
|
-
return Math.max(0, ...styles.map((style) => style?.size ?? 0)) * LINE_FACTOR + 4;
|
|
1513
|
-
}
|
|
1514
|
-
function checkImage(image, place, walk) {
|
|
1515
|
-
const { id } = image.asset;
|
|
1516
|
-
if (image.fit !== "contain" && image.fit !== "cover") throw new RenderRefusal("unsupported", `the image ${id} is fitted '${String(image.fit)}', which the renderer does not know`, id);
|
|
1517
|
-
if (!(image.width > 0 && image.height > 0 && Number.isFinite(image.width) && Number.isFinite(image.height))) throw new RenderRefusal("unsupported", `the image ${id} is drawn ${image.width} × ${image.height} pt`, id);
|
|
1518
|
-
if (image.width > place.width) throw new RenderRefusal("content-too-large", `the image ${id} is ${image.width} pt wide; it stands ${place.width.toFixed(2)} pt wide`, id);
|
|
1519
|
-
tall(walk, place, image.height, `the image ${id}`, id);
|
|
1520
|
-
}
|
|
1521
|
-
function checkRectangle(rectangle, at, place, walk) {
|
|
1522
|
-
if (!(rectangle.width > 0 && rectangle.height > 0 && Number.isFinite(rectangle.width) && Number.isFinite(rectangle.height))) throw new RenderRefusal("unsupported", `${at} is a rectangle of ${rectangle.width} × ${rectangle.height} pt`, at);
|
|
1523
|
-
checkColor(rectangle.fill, at);
|
|
1524
|
-
checkColor(rectangle.stroke, at);
|
|
1525
|
-
if (rectangle.width > place.width) throw new RenderRefusal("content-too-large", `${at} is a rectangle ${rectangle.width} pt wide; it stands ${place.width.toFixed(2)} pt wide`, at);
|
|
1526
|
-
tall(walk, place, rectangle.height, `the rectangle ${at}`, at);
|
|
1527
|
-
}
|
|
1528
|
-
function checkColumns(columns, at, place, walk) {
|
|
1529
|
-
if (columns.columns.length === 0) throw new RenderRefusal("unsupported", `${at} is a columns block with no columns`, at);
|
|
1530
|
-
const widths = columns.columns.map((slot) => slot.width);
|
|
1531
|
-
if (widths.some((width) => width !== "fill" && !(width > 0 && Number.isFinite(width)))) throw new RenderRefusal("unsupported", `${at} has a column of no width`, at);
|
|
1532
|
-
const fixed = widths.reduce((sum, each) => sum + fixedWidth(each), 0);
|
|
1533
|
-
const fill = fillWidth(widths, place.width);
|
|
1534
|
-
if (fixed > place.width || fill <= 0) throw new RenderRefusal("content-too-large", `${at}'s fixed columns are ${fixed} pt wide; it stands ${place.width.toFixed(2)} pt wide`, at);
|
|
1535
|
-
columns.columns.forEach((slot, index) => {
|
|
1536
|
-
const width = slot.width === "fill" ? fill : slot.width;
|
|
1537
|
-
checkBlocks(slot.blocks, `${at}.columns[${index}].blocks`, {
|
|
1538
|
-
...place,
|
|
1539
|
-
column: true,
|
|
1540
|
-
width,
|
|
1541
|
-
depth: place.depth + 1
|
|
1542
|
-
}, walk);
|
|
1543
|
-
});
|
|
1544
|
-
}
|
|
1545
|
-
function checkSection(section, at, place, walk) {
|
|
1546
|
-
if (section.border !== void 0) {
|
|
1547
|
-
if (!(section.border.thickness > 0 && Number.isFinite(section.border.thickness))) throw new RenderRefusal("unsupported", `${at} has a border ${section.border.thickness} pt thick`, at);
|
|
1548
|
-
checkColor(section.border.color, at);
|
|
1549
|
-
}
|
|
1550
|
-
checkColor(section.fill, at);
|
|
1551
|
-
if (section.padding !== void 0 && !(section.padding >= 0 && Number.isFinite(section.padding))) throw new RenderRefusal("unsupported", `${at} has a padding of ${section.padding} pt`, at);
|
|
1552
|
-
const frame = frameOf(section);
|
|
1553
|
-
const width = place.width - 2 * frame;
|
|
1554
|
-
if (width <= 0) throw new RenderRefusal("content-too-large", `${at}'s border and padding leave its blocks no width`, at);
|
|
1555
|
-
if (frame > 0) tall(walk, place, 2 * frame, `the border and padding of ${at}`, at);
|
|
1556
|
-
checkBlocks(section.blocks, `${at}.blocks`, {
|
|
1557
|
-
...place,
|
|
1558
|
-
width,
|
|
1559
|
-
inset: place.inset + 2 * frame,
|
|
1560
|
-
depth: place.depth + 1
|
|
1561
|
-
}, walk);
|
|
1562
|
-
}
|
|
1563
|
-
/**
|
|
1564
|
-
* Refuses what the body cannot hold once its height is known: a block of
|
|
1565
|
-
* fixed height, or a line, taller than a page's body less the frames around
|
|
1566
|
-
* it. pdfmake would draw it past the foot of the page, or not at all.
|
|
1567
|
-
*/
|
|
1568
|
-
function refuseTall(tall, bodyHeight) {
|
|
1569
|
-
for (const each of tall) if (each.height > bodyHeight - each.inset) throw new RenderRefusal("content-too-large", `${each.what} is ${each.height.toFixed(2)} pt tall; the body is ${(bodyHeight - each.inset).toFixed(2)} pt where it stands`, each.subject);
|
|
1570
|
-
}
|
|
1571
|
-
//#endregion
|
|
1572
|
-
//#region src/definition.ts
|
|
1573
|
-
/**
|
|
1574
|
-
* The pdfmake document definitions a render hands pdfmake, built from the
|
|
1575
|
-
* resolved document and what was measured: pure, the same in Node and in a
|
|
1576
|
-
* browser, and the one place either entry's definitions come from. What
|
|
1577
|
-
* runs them is the entry's own (`node.ts`, `browser.ts`).
|
|
1578
|
-
*
|
|
1579
|
-
* The page regions: pdfmake draws its header inside the top page margin and
|
|
1580
|
-
* its footer inside the bottom one, and lays the body out between. So
|
|
1581
|
-
* pdfmake's margins are the document's enlarged by each region's measured
|
|
1582
|
-
* height and a 6pt gap, and each region is drawn at the document's own
|
|
1583
|
-
* margin: the header from the top of the printable area down, the footer
|
|
1584
|
-
* ending at its bottom. Nothing of the body can reach into either.
|
|
1585
|
-
*/
|
|
1586
|
-
const DEFAULT_STYLE = {
|
|
1587
|
-
font: ROBOTO,
|
|
1588
|
-
fontSize: 10
|
|
1589
|
-
};
|
|
1590
|
-
/**
|
|
1591
|
-
* The page header and footer that have blocks, each one item, with every
|
|
1592
|
-
* page field printed as the page limit: as many digits as the field can
|
|
1593
|
-
* have. A region that wraps only for some page numbers is measured at its
|
|
1594
|
-
* widest, which is not the same thing, and is not refused.
|
|
1595
|
-
*/
|
|
1596
|
-
function regionsMeasurement(document, checked, maxPages) {
|
|
1597
|
-
const fields = {
|
|
1598
|
-
pageNumber: maxPages,
|
|
1599
|
-
pageCount: maxPages
|
|
1600
|
-
};
|
|
1601
|
-
const present = [document.pageHeader, document.pageFooter].filter((blocks) => blocks.length > 0);
|
|
1602
|
-
const build = newBuild("measure", checked);
|
|
1603
|
-
return {
|
|
1604
|
-
present,
|
|
1605
|
-
build,
|
|
1606
|
-
items: present.map((blocks) => ({ stack: contentOf(blocks, build, checked.page.width, fields) }))
|
|
1607
|
-
};
|
|
1608
|
-
}
|
|
1609
|
-
/** The body's tables and keep-together blocks, each one item laid out at the width it stands at. */
|
|
1610
|
-
function bodyMeasurement(measured, checked) {
|
|
1611
|
-
const build = newBuild("measure", checked);
|
|
1612
|
-
return {
|
|
1613
|
-
items: measured.map(({ block, width }) => ({
|
|
1614
|
-
stack: contentOf([block], build, width),
|
|
1615
|
-
margin: [
|
|
1616
|
-
0,
|
|
1617
|
-
0,
|
|
1618
|
-
checked.page.width - width,
|
|
1619
|
-
0
|
|
1620
|
-
]
|
|
1621
|
-
})),
|
|
1622
|
-
build
|
|
1623
|
-
};
|
|
1624
|
-
}
|
|
1625
|
-
/**
|
|
1626
|
-
* The PDF's definition. pdfmake's own page limit, `maxPagesNumber`, cuts the
|
|
1627
|
-
* document short; the renderer's throws out of the layout instead, from the
|
|
1628
|
-
* function pdfmake calls for each page's background, as the page after the
|
|
1629
|
-
* last one allowed is added. So no more than one page past the limit is ever
|
|
1630
|
-
* laid out, and nothing is written.
|
|
1631
|
-
*/
|
|
1632
|
-
function layoutDefinition(document, checked, regions, plans, maxPages) {
|
|
1633
|
-
const page = checked.page;
|
|
1634
|
-
const { margins } = page;
|
|
1635
|
-
const bodyTop = margins.top + regions.header;
|
|
1636
|
-
const bodyBottom = page.paperHeight - margins.bottom - regions.footer;
|
|
1637
|
-
const build = newBuild("render", checked, {
|
|
1638
|
-
top: bodyTop,
|
|
1639
|
-
bottom: bodyBottom
|
|
1640
|
-
}, plans);
|
|
1641
|
-
const region = (blocks, margin) => {
|
|
1642
|
-
if (blocks.length === 0) return void 0;
|
|
1643
|
-
return (pageNumber, pageCount) => ({
|
|
1644
|
-
stack: contentOf(blocks, build, page.width, {
|
|
1645
|
-
pageNumber,
|
|
1646
|
-
pageCount
|
|
1647
|
-
}),
|
|
1648
|
-
margin
|
|
1649
|
-
});
|
|
1650
|
-
};
|
|
1651
|
-
const header = region(document.pageHeader, [
|
|
1652
|
-
margins.left,
|
|
1653
|
-
margins.top,
|
|
1654
|
-
margins.right,
|
|
1655
|
-
0
|
|
1656
|
-
]);
|
|
1657
|
-
const footer = region(document.pageFooter, [
|
|
1658
|
-
margins.left,
|
|
1659
|
-
6,
|
|
1660
|
-
margins.right,
|
|
1661
|
-
0
|
|
1662
|
-
]);
|
|
1663
|
-
header?.(1, 1, {
|
|
1664
|
-
width: page.paperWidth,
|
|
1665
|
-
height: page.paperHeight,
|
|
1666
|
-
orientation: document.page.orientation
|
|
1667
|
-
});
|
|
1668
|
-
footer?.(1, 1, {
|
|
1669
|
-
width: page.paperWidth,
|
|
1670
|
-
height: page.paperHeight,
|
|
1671
|
-
orientation: document.page.orientation
|
|
1672
|
-
});
|
|
1673
|
-
return {
|
|
1674
|
-
info: {
|
|
1675
|
-
title: document.info.title,
|
|
1676
|
-
creationDate: checked.creationDate
|
|
1677
|
-
},
|
|
1678
|
-
pageSize: {
|
|
1679
|
-
width: page.paperWidth,
|
|
1680
|
-
height: page.paperHeight
|
|
1681
|
-
},
|
|
1682
|
-
pageMargins: [
|
|
1683
|
-
margins.left,
|
|
1684
|
-
bodyTop,
|
|
1685
|
-
margins.right,
|
|
1686
|
-
page.paperHeight - bodyBottom
|
|
1687
|
-
],
|
|
1688
|
-
defaultStyle: DEFAULT_STYLE,
|
|
1689
|
-
content: contentOf(document.body, build, page.width),
|
|
1690
|
-
images: build.images,
|
|
1691
|
-
...header === void 0 ? {} : { header },
|
|
1692
|
-
...footer === void 0 ? {} : { footer },
|
|
1693
|
-
background: (number) => {
|
|
1694
|
-
if (number === 1) checkWords(build.words);
|
|
1695
|
-
if (number > maxPages) throw new RenderRefusal("too-many-pages", `the report needs more than ${maxPages} pages`);
|
|
1696
|
-
return null;
|
|
1697
|
-
},
|
|
1698
|
-
pageBreakBefore: (node, queries) => {
|
|
1699
|
-
return (node.headlineLevel === void 0 ? void 0 : build.rules.get(node.headlineLevel))?.(node, queries) ?? false;
|
|
1700
|
-
}
|
|
1701
|
-
};
|
|
1702
|
-
}
|
|
1703
|
-
//#endregion
|
|
1704
|
-
//#region src/measure.ts
|
|
1705
|
-
/** Thrown out of pdfmake's layout with what was read; never seen outside `measure`. */
|
|
1706
|
-
var Measurement = class {
|
|
1707
|
-
tops;
|
|
1708
|
-
constructor(tops) {
|
|
1709
|
-
this.tops = tops;
|
|
1710
|
-
}
|
|
1711
|
-
};
|
|
1712
|
-
/** Sentinels are marked with negative numbers, so that they never meet the marks content carries. */
|
|
1713
|
-
const sentinel = (index) => ({
|
|
1714
|
-
text: ".",
|
|
1715
|
-
fontSize: 1,
|
|
1716
|
-
headlineLevel: -1 - index
|
|
1717
|
-
});
|
|
1718
|
-
/**
|
|
1719
|
-
* Lays `items` out on one page `height` tall, between those side margins,
|
|
1720
|
-
* and measures them; `overflow` is what to throw when they do not fit on it.
|
|
1721
|
-
* Before a line is laid out, the `words` of the items are held to their
|
|
1722
|
-
* room, so that pdfmake never breaks one too wide for it.
|
|
1723
|
-
*/
|
|
1724
|
-
async function measure(engine, page, items, resources, overflow, words) {
|
|
1725
|
-
const definition = {
|
|
1726
|
-
pageSize: {
|
|
1727
|
-
width: page.width,
|
|
1728
|
-
height: page.height
|
|
1729
|
-
},
|
|
1730
|
-
pageMargins: [
|
|
1731
|
-
page.left,
|
|
1732
|
-
0,
|
|
1733
|
-
page.right,
|
|
1734
|
-
0
|
|
1735
|
-
],
|
|
1736
|
-
...resources,
|
|
1737
|
-
content: [
|
|
1738
|
-
sentinel(0),
|
|
1739
|
-
sentinel(1),
|
|
1740
|
-
...items.flatMap((item, index) => [item, sentinel(index + 2)])
|
|
1741
|
-
],
|
|
1742
|
-
background: (number) => {
|
|
1743
|
-
if (number === 1) checkWords(words);
|
|
1744
|
-
if (number > 1) throw overflow();
|
|
1745
|
-
return null;
|
|
1746
|
-
},
|
|
1747
|
-
pageBreakBefore: (node, queries) => {
|
|
1748
|
-
const tops = /* @__PURE__ */ new Map();
|
|
1749
|
-
for (const each of [node, ...queries.getFollowingNodesOnPage()]) if (each.headlineLevel !== void 0) tops.set(each.headlineLevel, each.startPosition.top);
|
|
1750
|
-
throw new Measurement(tops);
|
|
1751
|
-
}
|
|
1752
|
-
};
|
|
1753
|
-
let tops;
|
|
1754
|
-
try {
|
|
1755
|
-
await engine.run(definition);
|
|
1756
|
-
throw new Error("pdfmake wrote a PDF instead of measuring");
|
|
1757
|
-
} catch (error) {
|
|
1758
|
-
if (!(error instanceof Measurement)) throw error;
|
|
1759
|
-
tops = error.tops;
|
|
1760
|
-
}
|
|
1761
|
-
const top = (mark) => {
|
|
1762
|
-
const found = tops.get(mark);
|
|
1763
|
-
if (found === void 0) throw new Error(`pdfmake laid out no node marked ${mark}`);
|
|
1764
|
-
return found;
|
|
1765
|
-
};
|
|
1766
|
-
const pitch = top(-2) - top(-1);
|
|
1767
|
-
const ends = items.map((_, index) => top(-3 - index));
|
|
1768
|
-
return {
|
|
1769
|
-
heights: ends.map((end, index) => end - top(-2 - index) - pitch),
|
|
1770
|
-
ends,
|
|
1771
|
-
top
|
|
1772
|
-
};
|
|
1773
|
-
}
|
|
1774
|
-
/**
|
|
1775
|
-
* Each row's height, from the tops of the marks on their first cells: a
|
|
1776
|
-
* row's top to the next one's is its height and the rule under it. The last
|
|
1777
|
-
* row ends where the table does, which is where the sentinel after it
|
|
1778
|
-
* starts, measured from the top of its cell, which stands `PADDING_Y` below
|
|
1779
|
-
* the row's top.
|
|
1780
|
-
*/
|
|
1781
|
-
function rowPitches(marks, top, tableEnd) {
|
|
1782
|
-
return marks.map((mark, index) => {
|
|
1783
|
-
const next = marks[index + 1];
|
|
1784
|
-
return next === void 0 ? tableEnd - top(mark) + 2 : top(next) - top(mark);
|
|
1785
|
-
});
|
|
1786
|
-
}
|
|
1787
|
-
/**
|
|
1788
|
-
* How a table breaks across pages of a body `bodyHeight` tall, from its
|
|
1789
|
-
* measured rows, the header row first:
|
|
1790
|
-
*
|
|
1791
|
-
* - rows are kept whole (`dontBreakRows`) only when every one fits on a
|
|
1792
|
-
* page under the repeated header, and the first row of each group under
|
|
1793
|
-
* its group header as well. pdfmake's option is the whole table's, so one
|
|
1794
|
-
* row that does not lets every row of the table split. Split is what a row
|
|
1795
|
-
* taller than the page needs, since pdfmake would otherwise not draw it,
|
|
1796
|
-
* and what the first row of a group needs to start on its group header's
|
|
1797
|
-
* page when the two cannot share one whole;
|
|
1798
|
-
* - the header moves with the first row, or with the first two when the
|
|
1799
|
-
* first is a group header, so that neither ends a page alone, and with
|
|
1800
|
-
* every row of a block of rows kept together that starts among them --
|
|
1801
|
-
* a one-line quotation's totals; not when they do not fit on a page
|
|
1802
|
-
* together, for the same reason;
|
|
1803
|
-
* - the rows kept together (`breaks.ts`) are the blocks that fit on a page
|
|
1804
|
-
* under the header and start after the rows that move with it. A table
|
|
1805
|
-
* whose rows may split keeps only its group headers with what they head.
|
|
1806
|
-
*/
|
|
1807
|
-
function tablePlan(rows, bodyHeight) {
|
|
1808
|
-
const [header, ...body] = rows;
|
|
1809
|
-
const room = bodyHeight - RULE - (header?.pitch ?? 0) - 1;
|
|
1810
|
-
const under = (index) => body[index - 1]?.kind === "groupHeader" ? body[index - 1].pitch : 0;
|
|
1811
|
-
const dontBreakRows = body.every((row, index) => under(index) + row.pitch <= room);
|
|
1812
|
-
const pitches = rows.map((row) => row.pitch);
|
|
1813
|
-
const blocks = keepBlocks(rows.map((row) => row.kind), dontBreakRows);
|
|
1814
|
-
const first = body[0]?.kind === "groupHeader" && body.length > 1 ? 2 : 1;
|
|
1815
|
-
let lead = first;
|
|
1816
|
-
for (const block of blocks) if (block.first <= lead) lead = Math.max(lead, block.last);
|
|
1817
|
-
const fits = (count) => body.slice(0, count).reduce((sum, row) => sum + row.pitch, 0) <= room;
|
|
1818
|
-
const keepWithHeaderRows = fits(lead) ? lead : fits(first) ? first : 0;
|
|
1819
|
-
return {
|
|
1820
|
-
dontBreakRows,
|
|
1821
|
-
keepWithHeaderRows,
|
|
1822
|
-
pitches,
|
|
1823
|
-
blocks: keptBlocks(blocks, pitches, keepWithHeaderRows, room)
|
|
1824
|
-
};
|
|
1825
|
-
}
|
|
1826
|
-
//#endregion
|
|
1827
|
-
//#region src/render.ts
|
|
1828
|
-
/**
|
|
1829
|
-
* A resolved document rendered with pdfmake, refused rather than truncated,
|
|
1830
|
-
* clipped or dropped: the renderer both entries share, given the one thing
|
|
1831
|
-
* they do differently, the engine that runs a pdfmake definition with its
|
|
1832
|
-
* fonts ([0005](../../../docs/decisions/0005-pdfmake-renders-reports-behind-one-adapter.md)).
|
|
1833
|
-
*
|
|
1834
|
-
* Three passes, each a pdfmake layout. The first measures the page header
|
|
1835
|
-
* and footer, and the second the body's keep-together blocks and table rows
|
|
1836
|
-
* (`measure.ts`), because pdfmake loses what does not fit where it must
|
|
1837
|
-
* without saying so. The third is the PDF, laid out again by pdfmake for
|
|
1838
|
-
* each page break a keep-with-next or table rule asks for. Before them the
|
|
1839
|
-
* renderer refuses what it cannot draw (`check.ts`); between them it
|
|
1840
|
-
* decides the page margins, refuses what cannot fit on any page or within
|
|
1841
|
-
* the page limit (`bound.ts`), and chooses how each table breaks.
|
|
1842
|
-
*/
|
|
1843
|
-
async function render(engine, document, options) {
|
|
1844
|
-
const { signal, maxPages } = options;
|
|
1845
|
-
if (!Number.isSafeInteger(maxPages) || maxPages < 1) throw new RangeError(`maxPages must be a whole number of pages, at least 1; it is ${String(maxPages)}`);
|
|
1846
|
-
stopIfCancelled(signal);
|
|
1847
|
-
const checked = await checkDocument(document, await engine.glyphs());
|
|
1848
|
-
await stopIfCancelledSoon(signal);
|
|
1849
|
-
const regions = await measureRegions(engine, document, checked, maxPages);
|
|
1850
|
-
await stopIfCancelledSoon(signal);
|
|
1851
|
-
const bodyHeight = checked.page.height - regions.header - regions.footer;
|
|
1852
|
-
if (bodyHeight <= 0) throw new RenderRefusal("content-too-large", `the page header and footer leave no room for the body (${bodyHeight.toFixed(2)} pt)`);
|
|
1853
|
-
refuseTall(checked.tall, bodyHeight);
|
|
1854
|
-
refuseOversized(document.body, bodyHeight, maxPages);
|
|
1855
|
-
const plans = await measureBody(engine, checked, bodyHeight, maxPages);
|
|
1856
|
-
await stopIfCancelledSoon(signal);
|
|
1857
|
-
const pdf = await engine.run(layoutDefinition(document, checked, regions, plans, maxPages));
|
|
1858
|
-
await stopIfCancelledSoon(signal);
|
|
1859
|
-
return pdf;
|
|
1860
|
-
}
|
|
1861
|
-
/**
|
|
1862
|
-
* The page header's and footer's heights, measured once, on a page as tall
|
|
1863
|
-
* as the printable area: two regions that do not fit on it leave no body.
|
|
1864
|
-
*/
|
|
1865
|
-
async function measureRegions(engine, document, checked, maxPages) {
|
|
1866
|
-
const { present, items, build } = regionsMeasurement(document, checked, maxPages);
|
|
1867
|
-
if (present.length === 0) return {
|
|
1868
|
-
header: 0,
|
|
1869
|
-
footer: 0
|
|
1870
|
-
};
|
|
1871
|
-
const { page } = checked;
|
|
1872
|
-
const { heights } = await measure(engine, {
|
|
1873
|
-
width: page.paperWidth,
|
|
1874
|
-
height: page.height + 2 * (items.length + 2),
|
|
1875
|
-
left: page.margins.left,
|
|
1876
|
-
right: page.margins.right
|
|
1877
|
-
}, items, resources(build.images), () => new RenderRefusal("content-too-large", "the page header and footer are taller than the printable area"), build.words);
|
|
1878
|
-
const space = (blocks) => blocks.length === 0 ? 0 : heights[present.indexOf(blocks)] + 6;
|
|
1879
|
-
return {
|
|
1880
|
-
header: space(document.pageHeader),
|
|
1881
|
-
footer: space(document.pageFooter)
|
|
1882
|
-
};
|
|
1883
|
-
}
|
|
1884
|
-
/**
|
|
1885
|
-
* Measures the body's keep-together blocks and tables, refuses a block no
|
|
1886
|
-
* page can hold, and plans how each table breaks. On a page as tall as the
|
|
1887
|
-
* page limit's bodies and one more: content that does not fit on it needs
|
|
1888
|
-
* more pages than the limit, whatever else the body holds, so it is refused
|
|
1889
|
-
* here, after laying out at most that much of it.
|
|
1890
|
-
*
|
|
1891
|
-
* A block inside framed sections has less of each page than the body: their
|
|
1892
|
-
* borders and padding above and below it, which is what `inset` holds.
|
|
1893
|
-
*/
|
|
1894
|
-
async function measureBody(engine, checked, bodyHeight, maxPages) {
|
|
1895
|
-
const { measured, page } = checked;
|
|
1896
|
-
if (measured.length === 0) return /* @__PURE__ */ new Map();
|
|
1897
|
-
const { items, build } = bodyMeasurement(measured, checked);
|
|
1898
|
-
const { heights, top, ends } = await measure(engine, {
|
|
1899
|
-
width: page.paperWidth,
|
|
1900
|
-
height: (maxPages + 1) * bodyHeight + 2 * (items.length + 2),
|
|
1901
|
-
left: page.margins.left,
|
|
1902
|
-
right: page.margins.right
|
|
1903
|
-
}, items, resources(build.images), () => new RenderRefusal("too-many-pages", `the report needs more than ${maxPages} pages`), build.words);
|
|
1904
|
-
const plans = /* @__PURE__ */ new Map();
|
|
1905
|
-
measured.forEach(({ block, at, inset }, index) => {
|
|
1906
|
-
const room = bodyHeight - inset;
|
|
1907
|
-
if (block.kind === "keepTogether" && heights[index] > room - 1) throw new RenderRefusal("content-too-large", `the keep-together block ${at} is ${heights[index].toFixed(2)} pt tall; a page's body is ${room.toFixed(2)} pt where it stands`, at);
|
|
1908
|
-
if (block.kind === "table") {
|
|
1909
|
-
const rows = build.measuredRows.get(block);
|
|
1910
|
-
const pitches = rowPitches(rows.map((row) => row.mark), top, ends[index]);
|
|
1911
|
-
plans.set(block, tablePlan(rows.map((row, at) => ({
|
|
1912
|
-
kind: row.kind,
|
|
1913
|
-
pitch: pitches[at]
|
|
1914
|
-
})), room));
|
|
1915
|
-
}
|
|
1916
|
-
});
|
|
1917
|
-
return plans;
|
|
1918
|
-
}
|
|
1919
|
-
function resources(images) {
|
|
1920
|
-
return {
|
|
1921
|
-
defaultStyle: DEFAULT_STYLE,
|
|
1922
|
-
images
|
|
1923
|
-
};
|
|
1924
|
-
}
|
|
1925
|
-
function stopIfCancelled(signal) {
|
|
1926
|
-
if (signal?.aborted === true) throw new RenderRefusal("cancelled", "the render was cancelled");
|
|
1927
|
-
}
|
|
1928
|
-
/**
|
|
1929
|
-
* `stopIfCancelled`, once the event loop has turned. Every await of a pass
|
|
1930
|
-
* resolves without it turning -- pdfmake's work is synchronous and PDFKit's
|
|
1931
|
-
* stream events come on the microtask queue -- so without this an abort
|
|
1932
|
-
* from a timer arrived only once the PDF was written: a 417-page render
|
|
1933
|
-
* resolved after 10.7 seconds with a PDF its caller had abandoned after 50
|
|
1934
|
-
* milliseconds (2026-10-15).
|
|
1935
|
-
*/
|
|
1936
|
-
async function stopIfCancelledSoon(signal) {
|
|
1937
|
-
if (signal !== void 0) await new Promise((resolve) => setTimeout(resolve, 0));
|
|
1938
|
-
stopIfCancelled(signal);
|
|
1939
|
-
}
|
|
1940
|
-
//#endregion
|
|
1941
|
-
export { glyphsOnce as i, fromBase64 as n, collect as r, render as t };
|
|
1942
|
-
|
|
1943
|
-
//# sourceMappingURL=render-Bs7DXIYJ.mjs.map
|