@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.
- package/CHANGELOG.md +7 -0
- package/LICENSE +21 -0
- package/README.md +38 -2
- package/REFERENCE.md +520 -0
- package/index.d.ts +294 -0
- package/index.js +9 -0
- package/package.json +49 -4
- package/src/frontmatter.js +200 -0
- package/src/furniture.js +398 -0
- package/src/index.js +2415 -0
- package/src/mega/README.md +47 -0
- package/src/mega/diag.js +148 -0
- package/src/mega/flow.js +56 -0
- package/src/mega/fonts.js +287 -0
- package/src/mega/forms.js +479 -0
- package/src/mega/labels.js +43 -0
- package/src/mega/meta.js +145 -0
- package/src/nav.js +357 -0
- package/src/paginate.js +583 -0
- package/src/tagged.js +355 -0
- package/src/text-layer.js +1771 -0
- package/src/writer/encrypt.js +218 -0
- package/src/writer/font.js +507 -0
- package/src/writer/index.js +1135 -0
- package/src/writer/subset.js +387 -0
package/index.d.ts
ADDED
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
import type { SnapdomPlugin } from '@zumer/snapdom'
|
|
2
|
+
|
|
3
|
+
/** PDF page sizes supported by the exporter. Custom pairs are PDF points. */
|
|
4
|
+
export type PdfPageSize =
|
|
5
|
+
| 'fit'
|
|
6
|
+
| 'a3'
|
|
7
|
+
| 'a4'
|
|
8
|
+
| 'a5'
|
|
9
|
+
| 'a6'
|
|
10
|
+
| 'b4'
|
|
11
|
+
| 'b5'
|
|
12
|
+
| 'letter'
|
|
13
|
+
| 'legal'
|
|
14
|
+
| 'tabloid'
|
|
15
|
+
| 'executive'
|
|
16
|
+
| readonly [width: number, height: number]
|
|
17
|
+
|
|
18
|
+
export type PdfOrientation = 'portrait' | 'landscape'
|
|
19
|
+
export type PdfImageCodec = 'auto' | 'jpeg' | 'flate'
|
|
20
|
+
export type PdfBase14Family = 'sans' | 'serif' | 'mono'
|
|
21
|
+
/**
|
|
22
|
+
* Furniture type. `'document'`, the default, is the body's own embedded face,
|
|
23
|
+
* falling back to base-14 sans per string; the other three mean base-14.
|
|
24
|
+
*/
|
|
25
|
+
export type PdfFontFamily = 'document' | PdfBase14Family
|
|
26
|
+
export type PdfTextValue = string | number | null | undefined
|
|
27
|
+
export type PdfPageText = PdfTextValue | ((page: number, pages: number) => PdfTextValue)
|
|
28
|
+
|
|
29
|
+
export interface PdfTextBand {
|
|
30
|
+
left?: PdfPageText
|
|
31
|
+
center?: PdfPageText
|
|
32
|
+
right?: PdfPageText
|
|
33
|
+
/** Font size in PDF points. Default 9. */
|
|
34
|
+
size?: number
|
|
35
|
+
font?: PdfFontFamily
|
|
36
|
+
/** Three- or six-digit hexadecimal colour. */
|
|
37
|
+
color?: string
|
|
38
|
+
/** Alias accepted by the runtime. */
|
|
39
|
+
colour?: string
|
|
40
|
+
/** Draw the separator rule. Default true. */
|
|
41
|
+
rule?: boolean
|
|
42
|
+
/** Space between the text and rule, in PDF points. Default 10. */
|
|
43
|
+
gap?: number
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export type PdfBand = string | ((page: number, pages: number) => PdfTextValue) | PdfTextBand | Element | null
|
|
47
|
+
|
|
48
|
+
export interface PdfPageNumbers {
|
|
49
|
+
format?: (page: number, pages: number) => PdfTextValue
|
|
50
|
+
align?: 'left' | 'center' | 'right'
|
|
51
|
+
size?: number
|
|
52
|
+
font?: PdfFontFamily
|
|
53
|
+
color?: string
|
|
54
|
+
colour?: string
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface PdfTableOfContents {
|
|
58
|
+
title?: string
|
|
59
|
+
/** Outline depths to include. Default 3. */
|
|
60
|
+
levels?: number
|
|
61
|
+
size?: number
|
|
62
|
+
font?: PdfFontFamily
|
|
63
|
+
indent?: number
|
|
64
|
+
dots?: boolean
|
|
65
|
+
/** Formats the body page number printed by the index. */
|
|
66
|
+
label?: (page: number, bodyPages: number) => PdfTextValue
|
|
67
|
+
color?: string
|
|
68
|
+
colour?: string
|
|
69
|
+
leaderColor?: string
|
|
70
|
+
leaderColour?: string
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface PdfTextWatermark {
|
|
74
|
+
text: string
|
|
75
|
+
opacity?: number
|
|
76
|
+
angle?: number
|
|
77
|
+
/** Zero or omitted auto-fits the text to the page. */
|
|
78
|
+
size?: number
|
|
79
|
+
font?: PdfFontFamily
|
|
80
|
+
color?: string
|
|
81
|
+
colour?: string
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export interface PdfElementWatermark {
|
|
85
|
+
element: Element
|
|
86
|
+
opacity?: number
|
|
87
|
+
angle?: number
|
|
88
|
+
/** Fraction of the page's limiting dimension. Default 0.6. */
|
|
89
|
+
fit?: number
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export type PdfWatermark = string | Element | PdfTextWatermark | PdfElementWatermark | null
|
|
93
|
+
|
|
94
|
+
/** Document properties written to /Info and mirrored into the XMP packet. */
|
|
95
|
+
/**
|
|
96
|
+
* Permission flags. Every one defaults to true, and every one is ADVISORY: a
|
|
97
|
+
* viewer may honour it and any other tool may ignore it. Only `userPassword`
|
|
98
|
+
* protects anything. Extraction for accessibility is deliberately not offered:
|
|
99
|
+
* PDF 2.0 deprecates that bit and PDF/UA requires an encrypted file to keep
|
|
100
|
+
* permitting it.
|
|
101
|
+
*/
|
|
102
|
+
export interface PdfPermissions {
|
|
103
|
+
print?: boolean
|
|
104
|
+
modify?: boolean
|
|
105
|
+
copy?: boolean
|
|
106
|
+
annotate?: boolean
|
|
107
|
+
fillForms?: boolean
|
|
108
|
+
assemble?: boolean
|
|
109
|
+
printHighRes?: boolean
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export interface PdfEncryption {
|
|
113
|
+
/** Required, non-empty: without it the reader cannot open the file at all. */
|
|
114
|
+
userPassword: string
|
|
115
|
+
/** Lifts the permission flags. Defaults to the user password. */
|
|
116
|
+
ownerPassword?: string
|
|
117
|
+
permissions?: PdfPermissions
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export interface PdfDocumentMeta {
|
|
121
|
+
title?: string
|
|
122
|
+
author?: string
|
|
123
|
+
subject?: string
|
|
124
|
+
keywords?: string | readonly string[]
|
|
125
|
+
/** The producing APPLICATION (xmp:CreatorTool); Producer is always snapdom-pdf. */
|
|
126
|
+
creator?: string
|
|
127
|
+
/**
|
|
128
|
+
* Written only when given: an invented "now" would break byte determinism.
|
|
129
|
+
* Pass new Date() yourself when you want it.
|
|
130
|
+
*/
|
|
131
|
+
created?: Date | string
|
|
132
|
+
modified?: Date | string
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export type PdfProgressPhase = 'measure' | 'page' | 'matter' | 'assemble'
|
|
136
|
+
|
|
137
|
+
export interface PdfProgress {
|
|
138
|
+
phase: PdfProgressPhase
|
|
139
|
+
/** Pages done in this phase, or null when the phase has no count. */
|
|
140
|
+
done: number | null
|
|
141
|
+
/** Body page total, or null before pagination knows it. */
|
|
142
|
+
total: number | null
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** One warning, carrying its stable machine-readable code. */
|
|
146
|
+
export interface PdfWarning {
|
|
147
|
+
code: string
|
|
148
|
+
message: string
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** One face whose font program travels in the document. */
|
|
152
|
+
export interface PdfEmbeddedFont {
|
|
153
|
+
/** The /BaseFont name; a subset carries the `AAAAAA+` tag PDF 9.6.4 asks for. */
|
|
154
|
+
name: string
|
|
155
|
+
/** The CSS-facing request this face answered, e.g. `"Inter" 700`. */
|
|
156
|
+
label: string
|
|
157
|
+
/** Size of the embedded program before stream compression. */
|
|
158
|
+
kB: number
|
|
159
|
+
/** true when only the used glyphs travel; false when the whole file does. */
|
|
160
|
+
subset: boolean
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** Everything gateable about a finished export. Identical exports produce identical reports. */
|
|
164
|
+
export interface PdfReport {
|
|
165
|
+
warnings: readonly PdfWarning[]
|
|
166
|
+
/** Warning tallies keyed by code. */
|
|
167
|
+
counts: Readonly<Record<string, number>>
|
|
168
|
+
pages: number
|
|
169
|
+
bytes: number
|
|
170
|
+
/** Fillable fields written. */
|
|
171
|
+
fields: number
|
|
172
|
+
/** Link and widget annotations written. */
|
|
173
|
+
annotations: number
|
|
174
|
+
/** Faces embedded from the capture's own @font-face rules, subset or whole. */
|
|
175
|
+
fontsEmbedded: readonly PdfEmbeddedFont[]
|
|
176
|
+
/**
|
|
177
|
+
* true when this export EARNED and wrote the PDF/UA-1 identifier: every
|
|
178
|
+
* text operation on an embedded face, tagged structure, a language, a
|
|
179
|
+
* displayed title, and no painted image without alternative text. Never
|
|
180
|
+
* configurable; the `ua-withheld` warning names the blockers when false.
|
|
181
|
+
*/
|
|
182
|
+
pdfua: boolean
|
|
183
|
+
/** Text runs `vectorText` painted as real glyphs. 0 when it is off. */
|
|
184
|
+
vectorText: number
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** Options evaluated when a finished SnapDOM capture is exported. */
|
|
188
|
+
export interface PdfExportOptions {
|
|
189
|
+
page?: PdfPageSize
|
|
190
|
+
orientation?: PdfOrientation
|
|
191
|
+
/** Margin on all sides, in PDF points. Ignored by page: 'fit'. */
|
|
192
|
+
margin?: number
|
|
193
|
+
text?: boolean
|
|
194
|
+
links?: boolean
|
|
195
|
+
/**
|
|
196
|
+
* Draw raster/XObject content. false keeps enabled text, links and structure,
|
|
197
|
+
* but omits Element headers, footers and watermarks.
|
|
198
|
+
*/
|
|
199
|
+
image?: boolean
|
|
200
|
+
/** Compress PDF content streams. Default true. */
|
|
201
|
+
deflate?: boolean
|
|
202
|
+
/** Quality of the JPEG candidate. Ignored when lossless Flate wins. */
|
|
203
|
+
quality?: number
|
|
204
|
+
codec?: PdfImageCodec
|
|
205
|
+
/** Flatten the capture onto this colour for this PDF export. */
|
|
206
|
+
backgroundColor?: string | null
|
|
207
|
+
header?: PdfBand
|
|
208
|
+
footer?: PdfBand
|
|
209
|
+
pageNumbers?: boolean | PdfPageNumbers
|
|
210
|
+
repeatHeaders?: boolean
|
|
211
|
+
/** Emit a PDF structure tree. Structure alone is not a PDF/UA-1 claim; see `PdfReport.pdfua`. */
|
|
212
|
+
tagged?: boolean
|
|
213
|
+
/** PDF document language, for example "en" or "es-MX". */
|
|
214
|
+
lang?: string | null
|
|
215
|
+
/**
|
|
216
|
+
* Embed the capture's @font-face binaries and point the text at them. `true`
|
|
217
|
+
* (default) subsets each face to the glyphs the document drew; `'full'`
|
|
218
|
+
* embeds whole files; `false` embeds nothing. Needs the capture to carry its
|
|
219
|
+
* fonts: snapdom's own `embedFonts: true` capture option.
|
|
220
|
+
*/
|
|
221
|
+
embedFonts?: boolean | 'full'
|
|
222
|
+
/**
|
|
223
|
+
* Paint eligible text as real vector glyphs from the embedded fonts instead
|
|
224
|
+
* of leaving it invisible over the raster, crisp at any zoom. Only faithful
|
|
225
|
+
* where no shaping intervenes (left-to-right runs in an embedded face), so
|
|
226
|
+
* RTL and uncovered runs stay on the raster. Pair with `image: false` for a
|
|
227
|
+
* tiny, fully-vector text PDF; with `image: true` the glyphs overpaint the
|
|
228
|
+
* raster. Default false.
|
|
229
|
+
*/
|
|
230
|
+
vectorText?: boolean
|
|
231
|
+
cover?: Element | null
|
|
232
|
+
back?: Element | null
|
|
233
|
+
toc?: boolean | string | PdfTableOfContents | null
|
|
234
|
+
watermark?: PdfWatermark
|
|
235
|
+
debugText?: boolean
|
|
236
|
+
/** A filename, or true for "snapdom.pdf". The Blob is always returned. */
|
|
237
|
+
download?: boolean | string
|
|
238
|
+
/**
|
|
239
|
+
* Emit measured form controls as fillable AcroForm fields. Default true.
|
|
240
|
+
* false keeps their values in the invisible text layer instead.
|
|
241
|
+
*/
|
|
242
|
+
fields?: boolean
|
|
243
|
+
/** Document properties: /Info, XMP, and DisplayDocTitle when title is set. */
|
|
244
|
+
meta?: PdfDocumentMeta | null
|
|
245
|
+
/**
|
|
246
|
+
* Encrypt the file with AES-256 (standard security handler, revision 6), so
|
|
247
|
+
* the download itself is protected at rest. `userPassword` is required: a PDF
|
|
248
|
+
* with an empty one is encrypted and still opens without asking.
|
|
249
|
+
*/
|
|
250
|
+
encrypt?: PdfEncryption | null
|
|
251
|
+
/** Abort the export between pages. Rejects with the signal's AbortError. */
|
|
252
|
+
signal?: AbortSignal
|
|
253
|
+
/** Progress narration; a throwing callback is dropped, never fatal. */
|
|
254
|
+
onProgress?: (progress: PdfProgress) => void
|
|
255
|
+
/** Receives the structured diagnostics report after the Blob is finished. */
|
|
256
|
+
onReport?: (report: PdfReport) => void
|
|
257
|
+
|
|
258
|
+
/** @deprecated Capture resolution belongs to snapdom(...), not toPdf(...). */
|
|
259
|
+
scale?: never
|
|
260
|
+
/** @deprecated Use deflate for PDF streams; SnapDOM owns compress. */
|
|
261
|
+
compress?: never
|
|
262
|
+
/** @deprecated Use download; SnapDOM owns filename. */
|
|
263
|
+
filename?: never
|
|
264
|
+
/** @deprecated Use backgroundColor. */
|
|
265
|
+
background?: never
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Defaults accepted by pdf(). The first five options are capture-time and cannot
|
|
270
|
+
* be changed by a later toPdf() call. Every PdfExportOptions field may also be a
|
|
271
|
+
* factory default and can then be overridden by toPdf().
|
|
272
|
+
*/
|
|
273
|
+
export interface PdfPluginOptions extends PdfExportOptions {
|
|
274
|
+
formValues?: boolean
|
|
275
|
+
shadow?: boolean
|
|
276
|
+
breakAvoid?: boolean
|
|
277
|
+
/** true for h1-h6/role=heading, a selector string, or false. */
|
|
278
|
+
outline?: boolean | string
|
|
279
|
+
/** Measure form controls for fillable fields. Default true. Capture-time. */
|
|
280
|
+
forms?: boolean
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/** Create a PDF plugin for @zumer/snapdom >=3.0.0 <4. */
|
|
284
|
+
declare function pdf(defaults?: PdfPluginOptions): SnapdomPlugin
|
|
285
|
+
|
|
286
|
+
export { pdf }
|
|
287
|
+
export default pdf
|
|
288
|
+
|
|
289
|
+
declare module '@zumer/snapdom' {
|
|
290
|
+
interface CaptureResult {
|
|
291
|
+
/** Export this already-created capture as a PDF Blob. */
|
|
292
|
+
toPdf(options?: PdfExportOptions): Promise<Blob>
|
|
293
|
+
}
|
|
294
|
+
}
|
package/index.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @zumer/snapdom-pdf
|
|
3
|
+
* PDF export for SnapDOM: `pdf()` adds `result.toPdf()`.
|
|
4
|
+
*
|
|
5
|
+
* import { pdf } from '@zumer/snapdom-pdf'
|
|
6
|
+
*
|
|
7
|
+
* Up to @zumer/snapdom-plugins 4.x the same exporter shipped at that package's `/pdf` subpath.
|
|
8
|
+
*/
|
|
9
|
+
export { default, pdf } from './src/index.js'
|
package/package.json
CHANGED
|
@@ -1,6 +1,51 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zumer/snapdom-pdf",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "PDF export for SnapDOM: searchable, paginated documents with selectable text, links and optional form fields",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"bump:dry": "npx @zumerbox/bump -d",
|
|
8
|
+
"bump": "npx @zumerbox/bump && npx @zumerbox/changelog"
|
|
9
|
+
},
|
|
10
|
+
"exports": {
|
|
11
|
+
".": "./index.js"
|
|
12
|
+
},
|
|
13
|
+
"types": "./index.d.ts",
|
|
14
|
+
"files": [
|
|
15
|
+
"index.js",
|
|
16
|
+
"index.d.ts",
|
|
17
|
+
"src/",
|
|
18
|
+
"README.md",
|
|
19
|
+
"REFERENCE.md",
|
|
20
|
+
"CHANGELOG.md"
|
|
21
|
+
],
|
|
22
|
+
"keywords": [
|
|
23
|
+
"snapdom",
|
|
24
|
+
"snapdom-plugin",
|
|
25
|
+
"dom-capture",
|
|
26
|
+
"pdf",
|
|
27
|
+
"html-to-pdf"
|
|
28
|
+
],
|
|
29
|
+
"author": "Juan Martin Muda",
|
|
30
|
+
"license": "MIT",
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "git+https://github.com/zumerlab/snapdom.git",
|
|
34
|
+
"directory": "packages/pdf"
|
|
35
|
+
},
|
|
36
|
+
"bugs": {
|
|
37
|
+
"url": "https://github.com/zumerlab/snapdom/issues"
|
|
38
|
+
},
|
|
39
|
+
"homepage": "https://snapdom.dev/plugins/pdf/",
|
|
40
|
+
"funding": {
|
|
41
|
+
"type": "github",
|
|
42
|
+
"url": "https://github.com/sponsors/tinchox5"
|
|
43
|
+
},
|
|
44
|
+
"peerDependencies": {
|
|
45
|
+
"@zumer/snapdom": "^3.4.0"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@zumer/snapdom": "file:../.."
|
|
49
|
+
},
|
|
50
|
+
"sideEffects": false
|
|
51
|
+
}
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Front and back matter: a cover, a generated index, a closing page.
|
|
3
|
+
*
|
|
4
|
+
* These are the pages that are NOT the capture. A cover and a colophon are
|
|
5
|
+
* captured elements of your own — same engine, same fidelity, whatever CSS you
|
|
6
|
+
* put in them — placed one to a page. The index is the opposite: it is generated,
|
|
7
|
+
* because its content does not exist anywhere until pagination has happened, and
|
|
8
|
+
* it is drawn as REAL PDF TEXT so its links are live, its type is crisp at any
|
|
9
|
+
* zoom and it costs a few hundred bytes rather than another raster.
|
|
10
|
+
*
|
|
11
|
+
* The index is only possible at all because `nav.js` already had to find the
|
|
12
|
+
* headings and `index.js` already had to place them on pages. Nothing here reads
|
|
13
|
+
* the DOM: it is handed finished entries and lays them out.
|
|
14
|
+
*
|
|
15
|
+
* Front matter is deliberately OUTSIDE the page numbering. A cover that says
|
|
16
|
+
* "1 / 14" is a cover nobody would ship, and an index that counts itself makes
|
|
17
|
+
* every number in it disagree with the number printed on the page it names.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
const HEX = /^#?([0-9a-f]{3}|[0-9a-f]{6})$/i
|
|
21
|
+
|
|
22
|
+
function rgb(colour) {
|
|
23
|
+
const m = HEX.exec(String(colour || '').trim())
|
|
24
|
+
if (!m) return null
|
|
25
|
+
let h = m[1]
|
|
26
|
+
if (h.length === 3) h = h[0] + h[0] + h[1] + h[1] + h[2] + h[2]
|
|
27
|
+
const n = parseInt(h, 16)
|
|
28
|
+
return [(n >> 16 & 255) / 255, (n >> 8 & 255) / 255, (n & 255) / 255]
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Content-stream ops that place a captured page image inside the page box:
|
|
33
|
+
* scaled to fit and centred, aspect preserved, margins ignored.
|
|
34
|
+
*
|
|
35
|
+
* Fit rather than fill, so nothing is ever cropped — a cover whose title is cut
|
|
36
|
+
* off is a worse failure than one with a band of paper down its side. Design the
|
|
37
|
+
* element at the page's aspect ratio and fit IS full bleed.
|
|
38
|
+
*
|
|
39
|
+
* @param {{name: string, pageW: number, pageH: number, aspect: number}} args
|
|
40
|
+
* `aspect` is height / width of the captured element.
|
|
41
|
+
*/
|
|
42
|
+
export function fullPageOps({ name, pageW, pageH, aspect }) {
|
|
43
|
+
const ratio = aspect > 0 ? aspect : 1
|
|
44
|
+
let w = pageW
|
|
45
|
+
let h = w * ratio
|
|
46
|
+
if (h > pageH) { h = pageH; w = h / ratio }
|
|
47
|
+
return [
|
|
48
|
+
'q',
|
|
49
|
+
`${w.toFixed(3)} 0 0 ${h.toFixed(3)} ${((pageW - w) / 2).toFixed(3)} ${((pageH - h) / 2).toFixed(3)} cm`,
|
|
50
|
+
`/${name} Do`,
|
|
51
|
+
'Q',
|
|
52
|
+
]
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Normalise the `toc` option. `true` has to be the whole feature, because for
|
|
57
|
+
* most documents "put an index in" is the entire thought.
|
|
58
|
+
*
|
|
59
|
+
* @returns {{title: string, levels: number, size: number, font: string,
|
|
60
|
+
* indent: number, dots: boolean, colour: number[], leader: number[]}|null}
|
|
61
|
+
*/
|
|
62
|
+
export function tocSpec(option) {
|
|
63
|
+
if (!option) return null
|
|
64
|
+
const o = option === true ? {} : (typeof option === 'string' ? { title: option } : option)
|
|
65
|
+
return {
|
|
66
|
+
title: o.title == null ? 'Contents' : String(o.title),
|
|
67
|
+
// Depth, not heading level: an index of a document that starts at h2 should
|
|
68
|
+
// still show two tiers, and depth is what the outline already nests by.
|
|
69
|
+
levels: Number.isFinite(o.levels) && o.levels > 0 ? Math.floor(o.levels) : 3,
|
|
70
|
+
size: Number.isFinite(o.size) && o.size > 0 ? o.size : 10,
|
|
71
|
+
font: o.font === 'serif' || o.font === 'mono' || o.font === 'sans'
|
|
72
|
+
? o.font
|
|
73
|
+
// The document's own embedded face when the export has one; an explicit
|
|
74
|
+
// base-14 request keeps meaning exactly what it says.
|
|
75
|
+
: 'document',
|
|
76
|
+
indent: Number.isFinite(o.indent) ? o.indent : 14,
|
|
77
|
+
// The number as the index PRINTS it. Plain by default, which is the numeral
|
|
78
|
+
// the standard "3 / 12" page number carries — override it in step with
|
|
79
|
+
// `pageNumbers.format` if you renumber, or the two will disagree in print.
|
|
80
|
+
label: typeof o.label === 'function' ? o.label : (n) => String(n),
|
|
81
|
+
dots: o.dots !== false,
|
|
82
|
+
colour: rgb(o.color ?? o.colour) || [0.1, 0.11, 0.13],
|
|
83
|
+
leader: rgb(o.leaderColor ?? o.leaderColour) || [0.72, 0.74, 0.78],
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Cut a title down until it fits, and say so with an ellipsis. */
|
|
88
|
+
export function fitText(text, room, measure) {
|
|
89
|
+
if (measure(text) <= room) return text
|
|
90
|
+
let lo = 0
|
|
91
|
+
let hi = text.length
|
|
92
|
+
while (lo < hi) {
|
|
93
|
+
const mid = Math.ceil((lo + hi) / 2)
|
|
94
|
+
if (measure(text.slice(0, mid) + '…') <= room) lo = mid
|
|
95
|
+
else hi = mid - 1
|
|
96
|
+
}
|
|
97
|
+
return lo > 0 ? text.slice(0, lo) + '…' : ''
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Lay the index out into pages.
|
|
102
|
+
*
|
|
103
|
+
* @param {object} args
|
|
104
|
+
* @param {Array<{depth:number, title:string, dest:string, label:string}>} args.entries
|
|
105
|
+
* `dest` is a finished PDF destination array string and `label` the page number
|
|
106
|
+
* as it is PRINTED on the page it names — this file never computes either, so an
|
|
107
|
+
* index can never disagree with the page it points at.
|
|
108
|
+
* @param {(text: string, key: string, size: number) => ({str, font, width}|null)} args.pdfText
|
|
109
|
+
* @returns {Array<{ops: string[], annots: Array<{rect: number[], dest: string}>, fonts: Set, xobjects: Set}>}
|
|
110
|
+
* one entry per index page, in order.
|
|
111
|
+
*/
|
|
112
|
+
export function tocLayout({ entries, spec, pdfText, drawTitle, marginX, availW, contentTop, availH }) {
|
|
113
|
+
const line = spec.size * 1.75
|
|
114
|
+
const headSize = spec.size * 1.8
|
|
115
|
+
const headRoom = spec.title.trim() ? headSize * 2.4 : 0
|
|
116
|
+
|
|
117
|
+
const pages = []
|
|
118
|
+
let page = null
|
|
119
|
+
let y = 0
|
|
120
|
+
let room = 0
|
|
121
|
+
|
|
122
|
+
const open = (first) => {
|
|
123
|
+
page = { ops: ['q'], annots: [], fonts: new Set(), xobjects: new Set() }
|
|
124
|
+
pages.push(page)
|
|
125
|
+
y = contentTop
|
|
126
|
+
room = availH
|
|
127
|
+
if (first && headRoom) {
|
|
128
|
+
const head = pdfText(spec.title, `${spec.font}-bold`, headSize)
|
|
129
|
+
if (head) {
|
|
130
|
+
page.fonts.add(head.font)
|
|
131
|
+
page.ops.push(`${spec.colour.map(c => c.toFixed(3)).join(' ')} rg`)
|
|
132
|
+
page.ops.push(`BT 0 Tr /${head.font.name} ${headSize.toFixed(3)} Tf 1 0 0 1 ` +
|
|
133
|
+
`${marginX.toFixed(3)} ${(y - headSize).toFixed(3)} Tm ${head.str} Tj ET`)
|
|
134
|
+
}
|
|
135
|
+
y -= headRoom
|
|
136
|
+
room -= headRoom
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
open(true)
|
|
140
|
+
|
|
141
|
+
for (const entry of entries) {
|
|
142
|
+
if (room < line) open(false)
|
|
143
|
+
const baseline = y - spec.size
|
|
144
|
+
const key = entry.depth === 0 ? `${spec.font}-bold` : spec.font
|
|
145
|
+
const label = pdfText(entry.label, spec.font, spec.size)
|
|
146
|
+
const labelW = label ? label.width : 0
|
|
147
|
+
const indent = entry.depth * spec.indent
|
|
148
|
+
const left = marginX + indent
|
|
149
|
+
// Room for the title is everything the number and one em of breathing space
|
|
150
|
+
// do not already claim.
|
|
151
|
+
const titleRoom = availW - indent - labelW - spec.size * 2
|
|
152
|
+
|
|
153
|
+
// How a title gets onto the page is the CALLER's problem, because the answer
|
|
154
|
+
// depends on the script: base-14 text where WinAnsi can address it, and a crop
|
|
155
|
+
// of the heading's own pixels where it cannot. This file owns the layout and
|
|
156
|
+
// knows nothing about rasters.
|
|
157
|
+
const drawn = titleRoom > 0
|
|
158
|
+
? drawTitle({ entry, key, size: spec.size, x: left, baseline, room: titleRoom, colour: spec.colour })
|
|
159
|
+
: null
|
|
160
|
+
if (drawn) {
|
|
161
|
+
for (const f of drawn.fonts || []) page.fonts.add(f)
|
|
162
|
+
// A title drawn as a CROP of the page raster needs that raster in the index
|
|
163
|
+
// page's own resources — the fonts are not the only thing a line can borrow.
|
|
164
|
+
for (const x of drawn.xobjects || []) page.xobjects.add(x)
|
|
165
|
+
page.ops.push(...drawn.ops)
|
|
166
|
+
}
|
|
167
|
+
if (label) {
|
|
168
|
+
page.fonts.add(label.font)
|
|
169
|
+
page.ops.push(`BT 0 Tr /${label.font.name} ${spec.size} Tf 1 0 0 1 ` +
|
|
170
|
+
`${(marginX + availW - labelW).toFixed(3)} ${baseline.toFixed(3)} Tm ${label.str} Tj ET`)
|
|
171
|
+
}
|
|
172
|
+
// Leaders as a dashed rule rather than a run of full stops: a row of '.'
|
|
173
|
+
// glyphs is a row of glyphs an extractor returns as text, and an index whose
|
|
174
|
+
// copy-paste is "Introduction......3" is worse than one with no leader.
|
|
175
|
+
if (spec.dots && drawn && drawn.width > 0 && label) {
|
|
176
|
+
const from = left + drawn.width + spec.size * 0.6
|
|
177
|
+
const to = marginX + availW - labelW - spec.size * 0.6
|
|
178
|
+
if (to - from > spec.size) {
|
|
179
|
+
page.ops.push('q', `${spec.leader.map(c => c.toFixed(3)).join(' ')} RG`,
|
|
180
|
+
`0.7 w [0.7 ${(spec.size * 0.28).toFixed(2)}] 0 d 1 J`,
|
|
181
|
+
`${from.toFixed(3)} ${(baseline + spec.size * 0.25).toFixed(3)} m ` +
|
|
182
|
+
`${to.toFixed(3)} ${(baseline + spec.size * 0.25).toFixed(3)} l S`, 'Q')
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
// The whole line is the target, not just the words: an index nobody can hit
|
|
186
|
+
// without aiming is an index people scroll past.
|
|
187
|
+
if (entry.dest) {
|
|
188
|
+
page.annots.push({
|
|
189
|
+
rect: [marginX, baseline - spec.size * 0.3, marginX + availW, baseline + spec.size],
|
|
190
|
+
dest: entry.dest,
|
|
191
|
+
})
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
y -= line
|
|
195
|
+
room -= line
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
for (const p of pages) p.ops.push('Q')
|
|
199
|
+
return pages
|
|
200
|
+
}
|