@zumer/snapdom-pdf 0.0.0-stage → 1.0.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.
@@ -0,0 +1,398 @@
1
+ /**
2
+ * Page furniture: the header and footer bands, and the page number.
3
+ *
4
+ * The two are drawn by different means on purpose. A header or footer is a piece
5
+ * of your page, so it is CAPTURED — same engine, same fidelity, whatever CSS you
6
+ * put in it. A page number is one short string that changes on every page, so it
7
+ * is drawn as real PDF text: crisp at any zoom, a few bytes, and selectable and
8
+ * searchable like the rest of the layer.
9
+ *
10
+ * Element bands are captured once before pagination. Their authoritative height
11
+ * comes from the final artifact's aspect ratio, so effects and render hooks cannot
12
+ * make the reserved strip disagree with what is drawn.
13
+ */
14
+ import { snapdom } from '@zumer/snapdom'
15
+
16
+ const positive = (value, fallback) => Number.isFinite(value) && value > 0 ? value : fallback
17
+ const nonNegative = (value, fallback) => Number.isFinite(value) && value >= 0 ? value : fallback
18
+
19
+ /**
20
+ * Cheap standalone estimate from an element's current box. The PDF exporter does
21
+ * not use this for pagination; captured artifact geometry is authoritative there.
22
+ *
23
+ * @param {Element|null} el
24
+ * @param {number} availW printable width in points
25
+ * @returns {number} height in points, 0 when there is no band
26
+ */
27
+ export function bandHeight(el, availW) {
28
+ if (!el || typeof el.getBoundingClientRect !== 'function' ||
29
+ !Number.isFinite(availW) || availW <= 0) return 0
30
+ const r = el.getBoundingClientRect()
31
+ if (!r.width || !r.height) return 0
32
+ return r.height * (availW / r.width)
33
+ }
34
+
35
+ /**
36
+ * Capture a band and hand back what `addStream` needs. Its height is derived from
37
+ * the FINAL artifact, not measured again from the live element: index.js captures
38
+ * bands before pageBox, and render hooks may change the artifact's aspect.
39
+ *
40
+ * The band rides the SAME scale and dpr as the page it sits on, so a header never
41
+ * looks softer or sharper than the content under it.
42
+ *
43
+ * @param {Element} el
44
+ * @param {{scale: number, dpr: number, quality: number,
45
+ * backgroundColor: string|null, availW: number}} opts
46
+ */
47
+ export async function captureBand(el, options, encodeImage) {
48
+ const img = await captureElement(el, options, encodeImage)
49
+ const availW = positive(options.availW, 0)
50
+ const aspect = positive(img.aspect, 0)
51
+ return { ...img, heightPt: availW * aspect }
52
+ }
53
+
54
+ /**
55
+ * Capture any element as an image XObject, plus its own aspect ratio — which is
56
+ * all a cover, a closing page or an element watermark needs, none of which is
57
+ * bound to the printable width the way a band is.
58
+ *
59
+ * @returns {Promise<object>} what `addStream` needs, plus `aspect` (height/width).
60
+ */
61
+ export async function captureElement(el, {
62
+ scale, dpr, quality, backgroundColor, codec, captureOptions = {}, image = true,
63
+ }, encodeImage) {
64
+ if (!el || el.nodeType !== 1) throw new TypeError('captureElement: expected an Element')
65
+ if (image && typeof encodeImage !== 'function') throw new TypeError('captureElement: encodeImage must be a function')
66
+ const rasterScale = positive(scale, 1)
67
+ const rasterDpr = positive(dpr, 1)
68
+ const source = {
69
+ baseURL: String(el.ownerDocument?.baseURI || document.baseURI),
70
+ documentURL: String(el.ownerDocument?.location?.href || el.ownerDocument?.baseURI || location.href),
71
+ documentLang: String(el.ownerDocument?.documentElement?.lang || ''),
72
+ }
73
+ const result = await snapdom(el, {
74
+ ...captureOptions,
75
+ outerShadows: captureOptions.outerShadows ?? false,
76
+ outerTransforms: captureOptions.outerTransforms ?? true,
77
+ scale: rasterScale, dpr: rasterDpr,
78
+ ...(backgroundColor ? { backgroundColor } : {}),
79
+ })
80
+ const meta = result.meta
81
+ if (!meta || !Number.isFinite(meta.vbW) || !Number.isFinite(meta.vbH)) {
82
+ throw new Error('captureElement: snapdom did not expose final render geometry')
83
+ }
84
+
85
+ const base = {
86
+ url: result.url,
87
+ meta,
88
+ vbW: meta.vbW,
89
+ vbH: meta.vbH,
90
+ offX: meta.contentX,
91
+ offY: meta.contentY,
92
+ aspect: meta.vbW > 0 ? meta.vbH / meta.vbW : 1,
93
+ source,
94
+ }
95
+ if (!image) return base
96
+
97
+ const canvas = await result.toCanvas({ backgroundColor: null })
98
+ const img = await encodeImage(canvas, { quality, background: backgroundColor, codec })
99
+
100
+ // Geometry is the immutable render contract returned by SnapDOM, so asymmetric
101
+ // bleed and transformed roots need no canvas-size heuristic here.
102
+ return {
103
+ ...img,
104
+ ...base,
105
+ }
106
+ }
107
+
108
+ const HEX = /^#?([0-9a-f]{3}|[0-9a-f]{6})$/i
109
+
110
+ /** `rg` wants three 0–1 components; anything unparseable stays the caller's grey. */
111
+ function rgb(colour) {
112
+ const m = HEX.exec(String(colour || '').trim())
113
+ if (!m) return null
114
+ let h = m[1]
115
+ if (h.length === 3) h = h[0] + h[0] + h[1] + h[1] + h[2] + h[2]
116
+ const n = parseInt(h, 16)
117
+ return [(n >> 16 & 255) / 255, (n >> 8 & 255) / 255, (n & 255) / 255]
118
+ }
119
+
120
+ /**
121
+ * Normalise the `pageNumbers` option. `true` is the whole feature for most
122
+ * callers, so it has to mean something complete on its own.
123
+ */
124
+ export function numberSpec(option) {
125
+ if (!option) return null
126
+ const o = option === true ? {} : option
127
+ return {
128
+ format: typeof o.format === 'function' ? o.format : (page, pages) => `${page} / ${pages}`,
129
+ align: o.align === 'left' || o.align === 'right' ? o.align : 'center',
130
+ size: positive(o.size, 9),
131
+ font: o.font === 'serif' || o.font === 'mono' || o.font === 'sans'
132
+ ? o.font
133
+ // The document's own embedded face when the export has one; an explicit
134
+ // base-14 request keeps meaning exactly what it says.
135
+ : 'document',
136
+ colour: rgb(o.color ?? o.colour) || [0.36, 0.39, 0.47],
137
+ }
138
+ }
139
+
140
+ /**
141
+ * Content-stream ops for one page's number, drawn in the bottom margin — below a
142
+ * footer band if there is one, so the two never fight over the same strip.
143
+ *
144
+ * @returns {{ops: string[], font: object}|null} null when the string is empty or
145
+ * unrepresentable, which is the caller's cue not to add a font resource.
146
+ */
147
+ export function numberOps({ spec, page, pages, pageW, marginX, marginY, availW, pdfText }) {
148
+ const str = String(spec.format(page, pages) ?? '')
149
+ if (!str.trim()) return null
150
+ const drawn = pdfText(str, spec.font, spec.size)
151
+ if (!drawn) return null
152
+ const { str: encoded, font, width } = drawn
153
+
154
+ const x = spec.align === 'left' ? marginX
155
+ : spec.align === 'right' ? marginX + availW - width
156
+ : marginX + (availW - width) / 2
157
+ // Sat in the middle of the bottom margin, on its own baseline.
158
+ const y = Math.max(2, marginY / 2 - spec.size * 0.35)
159
+
160
+ return {
161
+ font,
162
+ ops: [
163
+ 'q',
164
+ `${spec.colour.map(c => c.toFixed(3)).join(' ')} rg`,
165
+ // `0 Tr` is not redundant: the render mode is graphics state and survives ET,
166
+ // so the invisible layer drawn above would otherwise make this ghost too —
167
+ // present in the file, extractable, and painted by nobody.
168
+ `BT 0 Tr /${font.name} ${spec.size} Tf 1 0 0 1 ${x.toFixed(3)} ${y.toFixed(3)} Tm ${encoded} Tj ET`,
169
+ 'Q',
170
+ ],
171
+ }
172
+ }
173
+
174
+
175
+ /**
176
+ * Normalise a header or footer into one of two shapes.
177
+ *
178
+ * A string or an object of segments becomes REAL PDF TEXT — selectable, searchable
179
+ * and crisp at any zoom, which is what a running head almost always wants. An
180
+ * Element is captured instead, for the cases text cannot express: a logo, a rule in
181
+ * brand colours, a signature block.
182
+ *
183
+ * @returns {{kind:'text', …}|{kind:'element', el:Element}|null}
184
+ */
185
+ export function bandSpec(band) {
186
+ if (!band) return null
187
+ if (typeof band === 'object' && band.nodeType === 1) return { kind: 'element', el: band }
188
+ if (typeof band !== 'string' && typeof band !== 'function' && typeof band !== 'object') return null
189
+
190
+ const o = typeof band === 'string' || typeof band === 'function' ? { center: band } : band
191
+ const seg = (v) => (typeof v === 'function' ? v : v == null ? null : () => String(v))
192
+ const spec = {
193
+ kind: 'text',
194
+ left: seg(o.left), center: seg(o.center), right: seg(o.right),
195
+ size: positive(o.size, 9),
196
+ font: o.font === 'serif' || o.font === 'mono' || o.font === 'sans'
197
+ ? o.font
198
+ // The document's own embedded face when the export has one; an explicit
199
+ // base-14 request keeps meaning exactly what it says.
200
+ : 'document',
201
+ colour: rgb(o.color ?? o.colour) || [0.36, 0.39, 0.47],
202
+ rule: o.rule !== false,
203
+ gap: nonNegative(o.gap, 10),
204
+ }
205
+ return spec.left || spec.center || spec.right ? spec : null
206
+ }
207
+
208
+ /** How much of the page a text band takes, in points. */
209
+ export function textBandHeight(spec) {
210
+ return positive(spec && spec.size, 9) * 1.4 + nonNegative(spec && spec.gap, 10) + (spec && spec.rule ? 1 : 0)
211
+ }
212
+
213
+ /**
214
+ * Content-stream ops for a text band. The three segments are independent, so a
215
+ * left-aligned title and a right-aligned date never collide the way one centred
216
+ * string with padding would.
217
+ */
218
+ export function textBandOps({ spec, where, page, pages, pageH, marginX, marginY, availW, pdfText }) {
219
+ const height = textBandHeight(spec)
220
+ // The band occupies a strip against its margin; the rule always sits on the
221
+ // CONTENT side of the text, with the gap between them. Getting this the same way
222
+ // round for both put the footer's rule straight through its own words.
223
+ const top = where === 'header' ? pageH - marginY : marginY + height
224
+ const baseline = where === 'header'
225
+ ? top - spec.size
226
+ : top - spec.gap - spec.size * 0.8
227
+ const ruleY = where === 'header' ? top - height + spec.gap : top - 0.5
228
+
229
+ const ops = ['q', `${spec.colour.map(c => c.toFixed(3)).join(' ')} rg`]
230
+ const fonts = new Set()
231
+ for (const [key, align] of [['left', 'left'], ['center', 'center'], ['right', 'right']]) {
232
+ if (!spec[key]) continue
233
+ const str = String(spec[key](page, pages) ?? '')
234
+ if (!str.trim()) continue
235
+ const drawn = pdfText(str, spec.font, spec.size)
236
+ if (!drawn) continue
237
+ const { str: encoded, font, width } = drawn
238
+ const x = align === 'left' ? marginX
239
+ : align === 'right' ? marginX + availW - width
240
+ : marginX + (availW - width) / 2
241
+ // `0 Tr` explicitly: render mode is graphics state and survives ET, so the
242
+ // invisible layer drawn earlier would otherwise make this a ghost.
243
+ ops.push(`BT 0 Tr /${font.name} ${spec.size} Tf 1 0 0 1 ${x.toFixed(3)} ${baseline.toFixed(3)} Tm ${encoded} Tj ET`)
244
+ fonts.add(font)
245
+ }
246
+ if (!fonts.size) return null
247
+
248
+ if (spec.rule) {
249
+ ops.push(`${spec.colour.map(c => (c * 0.35 + 0.65).toFixed(3)).join(' ')} RG`,
250
+ '0.5 w', `${marginX} ${ruleY.toFixed(3)} m ${(marginX + availW).toFixed(3)} ${ruleY.toFixed(3)} l S`)
251
+ }
252
+ ops.push('Q')
253
+ return { ops, fonts: [...fonts] }
254
+ }
255
+
256
+ // ——— watermark ———
257
+ //
258
+ // The one piece of furniture that goes OVER the content rather than beside it,
259
+ // which is the whole point: a watermark under the page image would be invisible
260
+ // on any page with a background, and a watermark that can be covered is not one.
261
+
262
+ const clamp01 = (n, fallback) => (Number.isFinite(n) ? Math.min(1, Math.max(0, n)) : fallback)
263
+
264
+ /**
265
+ * Normalise the `watermark` option. A bare string is the common case and has to
266
+ * mean something complete: grey, translucent, across the diagonal, sized to the
267
+ * page.
268
+ *
269
+ * @returns {{kind:'text'|'element', …}|null}
270
+ */
271
+ export function watermarkSpec(option) {
272
+ if (!option) return null
273
+ if (typeof option === 'object' && option.nodeType === 1) {
274
+ return { kind: 'element', el: option, opacity: 0.12, angle: 0, fit: 0.6 }
275
+ }
276
+ const o = typeof option === 'string' ? { text: option } : (option || {})
277
+ if (o.element && o.element.nodeType === 1) {
278
+ return {
279
+ kind: 'element', el: o.element,
280
+ opacity: clamp01(o.opacity, 0.12),
281
+ angle: Number.isFinite(o.angle) ? o.angle : 0,
282
+ fit: clamp01(o.fit, 0.6) || 0.6,
283
+ }
284
+ }
285
+ const text = o.text == null ? '' : String(o.text)
286
+ if (!text.trim()) return null
287
+ return {
288
+ kind: 'text', text,
289
+ // 0 means "as wide as the page will take", which is what a watermark almost
290
+ // always wants and what nobody wants to compute by hand for every page size.
291
+ size: Number.isFinite(o.size) && o.size > 0 ? o.size : 0,
292
+ angle: Number.isFinite(o.angle) ? o.angle : 45,
293
+ opacity: clamp01(o.opacity, 0.12),
294
+ colour: rgb(o.color ?? o.colour) || [0.45, 0.45, 0.5],
295
+ font: o.font === 'serif' || o.font === 'mono' || o.font === 'sans'
296
+ ? o.font
297
+ // The document's own embedded face when the export has one; an explicit
298
+ // base-14 request keeps meaning exactly what it says.
299
+ : 'document',
300
+ }
301
+ }
302
+
303
+ /**
304
+ * Content-stream ops for the watermark, centred on the whole page — the MEDIA
305
+ * box, not the printable area: a watermark inside the margins reads as content.
306
+ *
307
+ * @param {object} args
308
+ * @param {string} args.gs name of the ExtGState carrying the opacity
309
+ * @param {string} [args.image] name of the XObject, for an element watermark
310
+ * @param {(text: string, key: string, size: number) => ({str, font, width}|null)} [args.pdfText]
311
+ * @returns {{ops: string[], font: object|null}|null}
312
+ */
313
+ export function watermarkOps({ spec, pageW, pageH, gs, image, imageAspect, pdfText }) {
314
+ const cx = pageW / 2
315
+ const cy = pageH / 2
316
+ const rad = (spec.angle * Math.PI) / 180
317
+ const cos = Math.cos(rad)
318
+ const sin = Math.sin(rad)
319
+
320
+ if (spec.kind === 'element') {
321
+ if (!image) return null
322
+ // Fit inside the page at `fit` of its smaller dimension, aspect preserved, so
323
+ // a wide stamp on a portrait page is not squashed to fit the width.
324
+ const w = Math.min(pageW, pageH / (imageAspect || 1)) * spec.fit
325
+ const h = w * (imageAspect || 1)
326
+ // A form XObject's unit square maps through `cm`, so the rotation has to be
327
+ // composed with the scale by hand rather than left to a text matrix.
328
+ return {
329
+ font: null,
330
+ ops: [
331
+ 'q', `/${gs} gs`,
332
+ `1 0 0 1 ${cx.toFixed(3)} ${cy.toFixed(3)} cm`,
333
+ `${cos.toFixed(5)} ${sin.toFixed(5)} ${(-sin).toFixed(5)} ${cos.toFixed(5)} 0 0 cm`,
334
+ `${w.toFixed(3)} 0 0 ${h.toFixed(3)} ${(-w / 2).toFixed(3)} ${(-h / 2).toFixed(3)} cm`,
335
+ `/${image} Do`, 'Q',
336
+ ],
337
+ }
338
+ }
339
+
340
+ // Auto size: the largest the string can be with its ROTATED extent still inside
341
+ // the page, which is what makes one option work on A4, on a receipt roll and on
342
+ // a landscape slide alike.
343
+ //
344
+ // Not the page's diagonal LENGTH — that is the trap. A 486 × 1938 page has a
345
+ // long diagonal and almost no width, and a string sized to that diagonal but
346
+ // laid at 45° runs off both sides: a rotated string of advance W spans W·|cos θ|
347
+ // across and W·|sin θ| down, so each axis caps W on its own and the smaller cap
348
+ // is the one that decides.
349
+ const unit = pdfText(spec.text, spec.font, 1)
350
+ if (!unit) return null
351
+ let size = spec.size
352
+ if (!size) {
353
+ const acrossX = Math.abs(cos) > 1e-6 ? (pageW * 0.9) / Math.abs(cos) : Infinity
354
+ const acrossY = Math.abs(sin) > 1e-6 ? (pageH * 0.9) / Math.abs(sin) : Infinity
355
+ const room = Math.min(acrossX, acrossY)
356
+ size = unit.width > 0 && Number.isFinite(room) ? room / unit.width : 24
357
+ }
358
+ const drawn = pdfText(spec.text, spec.font, size)
359
+ if (!drawn) return null
360
+ const width = drawn.width
361
+
362
+ // Centre the STRING on the page centre: back off half an advance along the
363
+ // rotated baseline, and about a third of a size along the rotated up-vector,
364
+ // which puts the x-height band on the centre rather than the baseline.
365
+ const x = cx - (width / 2) * cos + 0.35 * size * sin
366
+ const y = cy - (width / 2) * sin - 0.35 * size * cos
367
+
368
+ return {
369
+ font: drawn.font,
370
+ ops: [
371
+ 'q', `/${gs} gs`,
372
+ `${spec.colour.map(c => c.toFixed(3)).join(' ')} rg`,
373
+ // `0 Tr` explicitly — render mode survives ET, and the invisible text layer
374
+ // drawn before this would otherwise make the watermark a ghost too.
375
+ `BT 0 Tr /${drawn.font.name} ${size.toFixed(3)} Tf ` +
376
+ `${cos.toFixed(5)} ${sin.toFixed(5)} ${(-sin).toFixed(5)} ${cos.toFixed(5)} ` +
377
+ `${x.toFixed(3)} ${y.toFixed(3)} Tm ${drawn.str} Tj ET`,
378
+ 'Q',
379
+ ],
380
+ }
381
+ }
382
+
383
+ /**
384
+ * Content-stream ops that place a captured band across the printable width.
385
+ *
386
+ * @param {'header'|'footer'} where
387
+ */
388
+ export function bandOps({ where, name, heightPt, pageH, marginX, marginY, availW }) {
389
+ const bottom = where === 'header'
390
+ ? pageH - marginY - heightPt
391
+ : marginY
392
+ return [
393
+ 'q',
394
+ `${availW.toFixed(3)} 0 0 ${heightPt.toFixed(3)} ${marginX.toFixed(3)} ${bottom.toFixed(3)} cm`,
395
+ `/${name} Do`,
396
+ 'Q',
397
+ ]
398
+ }