qrdx 0.0.46 → 0.0.48
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +358 -300
- package/dist/index.js +27 -7
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +27 -7
- package/dist/index.mjs.map +1 -1
- package/dist/qr-code-canvas.js +12 -0
- package/dist/qr-code-canvas.js.map +1 -0
- package/dist/qr-code-canvas.mjs +12 -0
- package/dist/qr-code-canvas.mjs.map +1 -0
- package/dist/src/api.d.ts.map +1 -1
- package/dist/src/art-trace.d.ts +46 -0
- package/dist/src/art-trace.d.ts.map +1 -0
- package/dist/src/art.d.ts +138 -0
- package/dist/src/art.d.ts.map +1 -0
- package/dist/src/body-patterns.d.ts +34 -0
- package/dist/src/body-patterns.d.ts.map +1 -0
- package/dist/src/codegen.d.ts +5 -5
- package/dist/src/codegen.d.ts.map +1 -1
- package/dist/src/codeword-map.d.ts +40 -0
- package/dist/src/codeword-map.d.ts.map +1 -0
- package/dist/src/constants.d.ts +4 -7
- package/dist/src/constants.d.ts.map +1 -1
- package/dist/src/corner-dot.d.ts +3 -1
- package/dist/src/corner-dot.d.ts.map +1 -1
- package/dist/src/corner-eye.d.ts +5 -2
- package/dist/src/corner-eye.d.ts.map +1 -1
- package/dist/src/detection.d.ts.map +1 -1
- package/dist/src/download.d.ts +50 -6
- package/dist/src/download.d.ts.map +1 -1
- package/dist/src/eps.d.ts +62 -0
- package/dist/src/eps.d.ts.map +1 -0
- package/dist/src/frame-icons.d.ts +11 -0
- package/dist/src/frame-icons.d.ts.map +1 -0
- package/dist/src/frame-import.d.ts +61 -0
- package/dist/src/frame-import.d.ts.map +1 -0
- package/dist/src/frame-spec.d.ts +24 -0
- package/dist/src/frame-spec.d.ts.map +1 -0
- package/dist/src/frame-styles.d.ts +50 -0
- package/dist/src/frame-styles.d.ts.map +1 -0
- package/dist/src/frame.d.ts +126 -0
- package/dist/src/frame.d.ts.map +1 -0
- package/dist/src/frames/builtin.d.ts +8 -0
- package/dist/src/frames/builtin.d.ts.map +1 -0
- package/dist/src/frames/official.d.ts +18 -0
- package/dist/src/frames/official.d.ts.map +1 -0
- package/dist/src/helpers.d.ts +15 -2
- package/dist/src/helpers.d.ts.map +1 -1
- package/dist/src/index.d.ts +25 -13
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/logo-budget.d.ts +54 -0
- package/dist/src/logo-budget.d.ts.map +1 -0
- package/dist/src/logo-cleanup.d.ts +25 -0
- package/dist/src/logo-cleanup.d.ts.map +1 -0
- package/dist/src/logo.d.ts +28 -0
- package/dist/src/logo.d.ts.map +1 -0
- package/dist/src/print.d.ts +73 -0
- package/dist/src/print.d.ts.map +1 -0
- package/dist/src/qr-code-canvas.d.ts +13 -0
- package/dist/src/qr-code-canvas.d.ts.map +1 -0
- package/dist/src/qr-code.d.ts +4 -2
- package/dist/src/qr-code.d.ts.map +1 -1
- package/dist/src/qr-outline.d.ts +41 -0
- package/dist/src/qr-outline.d.ts.map +1 -0
- package/dist/src/qr-shape.d.ts +72 -0
- package/dist/src/qr-shape.d.ts.map +1 -0
- package/dist/src/scan-risk.d.ts +40 -0
- package/dist/src/scan-risk.d.ts.map +1 -0
- package/dist/src/scan-score.d.ts +154 -0
- package/dist/src/scan-score.d.ts.map +1 -0
- package/dist/src/shapes.d.ts +36 -0
- package/dist/src/shapes.d.ts.map +1 -0
- package/dist/src/silhouette.d.ts +28 -0
- package/dist/src/silhouette.d.ts.map +1 -0
- package/dist/src/utils/color.d.ts.map +1 -1
- package/dist/src/utils/fmt.d.ts +24 -0
- package/dist/src/utils/fmt.d.ts.map +1 -0
- package/dist/src/utils/paint.d.ts +80 -0
- package/dist/src/utils/paint.d.ts.map +1 -0
- package/dist/src/utils/parse-color.d.ts +21 -0
- package/dist/src/utils/parse-color.d.ts.map +1 -0
- package/dist/src/utils/path-data.d.ts +24 -0
- package/dist/src/utils/path-data.d.ts.map +1 -0
- package/dist/src/utils/png.d.ts +16 -0
- package/dist/src/utils/png.d.ts.map +1 -0
- package/dist/src/utils/polygon-clip.d.ts +26 -0
- package/dist/src/utils/polygon-clip.d.ts.map +1 -0
- package/dist/src/utils/print-shared.d.ts +20 -0
- package/dist/src/utils/print-shared.d.ts.map +1 -0
- package/dist/src/utils/svg-geometry.d.ts +49 -0
- package/dist/src/utils/svg-geometry.d.ts.map +1 -0
- package/dist/src/utils/svg-gradient.d.ts +59 -0
- package/dist/src/utils/svg-gradient.d.ts.map +1 -0
- package/dist/src/utils/svg-painter.d.ts +152 -0
- package/dist/src/utils/svg-painter.d.ts.map +1 -0
- package/dist/src/utils/webp.d.ts +25 -0
- package/dist/src/utils/webp.d.ts.map +1 -0
- package/dist/src/utils.d.ts +18 -2
- package/dist/src/utils.d.ts.map +1 -1
- package/dist/src/webp.d.ts +19 -0
- package/dist/src/webp.d.ts.map +1 -0
- package/dist/types/art.d.ts +64 -0
- package/dist/types/art.d.ts.map +1 -0
- package/dist/types/color.d.ts +125 -7
- package/dist/types/color.d.ts.map +1 -1
- package/dist/types/corner-dot.d.ts +52 -2
- package/dist/types/corner-dot.d.ts.map +1 -1
- package/dist/types/corner-eye.d.ts +43 -2
- package/dist/types/corner-eye.d.ts.map +1 -1
- package/dist/types/frame-spec.d.ts +3509 -0
- package/dist/types/frame-spec.d.ts.map +1 -0
- package/dist/types/frame.d.ts +68 -0
- package/dist/types/frame.d.ts.map +1 -0
- package/dist/types/image-pattern.d.ts +15 -1
- package/dist/types/image-pattern.d.ts.map +1 -1
- package/dist/types/index.d.ts +15 -9
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js +6 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/index.mjs +6 -0
- package/dist/types/index.mjs.map +1 -0
- package/dist/types/qr.d.ts +1684 -4
- package/dist/types/qr.d.ts.map +1 -1
- package/dist/types/template.d.ts +2 -2
- package/dist/types/template.d.ts.map +1 -1
- package/package.json +70 -25
- package/src/api.tsx +67 -17
- package/src/art-trace.ts +554 -0
- package/src/art.tsx +2226 -0
- package/src/body-patterns.tsx +389 -0
- package/src/codegen.ts +10 -5
- package/src/codeword-map.ts +265 -0
- package/src/constants.ts +4 -8
- package/src/corner-dot.tsx +209 -33
- package/src/corner-eye.ts +328 -60
- package/src/detection.ts +5 -11
- package/src/download.tsx +174 -70
- package/src/eps.ts +701 -0
- package/src/frame-icons.ts +325 -0
- package/src/frame-import.ts +1198 -0
- package/src/frame-spec.ts +518 -0
- package/src/frame-styles.ts +1095 -0
- package/src/frame.tsx +630 -0
- package/src/frames/builtin/banner-bottom.json +58 -0
- package/src/frames/builtin/circle.json +81 -0
- package/src/frames/builtin/rounded-border.json +60 -0
- package/src/frames/builtin/ticket.json +51 -0
- package/src/frames/builtin.ts +20 -0
- package/src/frames/official/attendance.json +110 -0
- package/src/frames/official/audio-guide.json +95 -0
- package/src/frames/official/book-now.json +103 -0
- package/src/frames/official/check-in.json +110 -0
- package/src/frames/official/christmas.json +131 -0
- package/src/frames/official/donate.json +77 -0
- package/src/frames/official/event-ticket.json +77 -0
- package/src/frames/official/feedback-bubble.json +73 -0
- package/src/frames/official/follow-us.json +83 -0
- package/src/frames/official/get-the-app.json +107 -0
- package/src/frames/official/halloween.json +80 -0
- package/src/frames/official/new-year.json +94 -0
- package/src/frames/official/order-at-table.json +86 -0
- package/src/frames/official/product-info.json +119 -0
- package/src/frames/official/register-product.json +77 -0
- package/src/frames/official/review-us.json +127 -0
- package/src/frames/official/rsvp-card.json +112 -0
- package/src/frames/official/save-contact.json +117 -0
- package/src/frames/official/scan-to-pay.json +94 -0
- package/src/frames/official/shop-window.json +81 -0
- package/src/frames/official/sign-rider.json +81 -0
- package/src/frames/official/table-tent.json +75 -0
- package/src/frames/official/wifi-card.json +89 -0
- package/src/frames/official/winter.json +142 -0
- package/src/frames/official.ts +77 -0
- package/src/helpers.ts +168 -11
- package/src/index.tsx +150 -172
- package/src/logo-budget.ts +226 -0
- package/src/logo-cleanup.ts +146 -0
- package/src/logo.tsx +137 -0
- package/src/print.ts +794 -0
- package/src/qr-code-canvas.tsx +241 -0
- package/src/qr-code.tsx +51 -16
- package/src/qr-outline.ts +511 -0
- package/src/qr-shape.ts +255 -0
- package/src/scan-risk.ts +197 -0
- package/src/scan-score.ts +1629 -0
- package/src/shapes.ts +169 -0
- package/src/silhouette.ts +334 -0
- package/src/utils/color.ts +10 -1
- package/src/utils/fmt.ts +59 -0
- package/src/utils/paint.ts +317 -0
- package/src/utils/parse-color.ts +135 -0
- package/src/utils/path-data.ts +182 -0
- package/src/utils/png.ts +175 -0
- package/src/utils/polygon-clip.ts +163 -0
- package/src/utils/print-shared.ts +74 -0
- package/src/utils/svg-geometry.ts +838 -0
- package/src/utils/svg-gradient.ts +407 -0
- package/src/utils/svg-painter.ts +946 -0
- package/src/utils/webp.ts +556 -0
- package/src/utils.tsx +794 -385
- package/src/webp.ts +84 -0
- package/types/art.ts +74 -0
- package/types/color.ts +132 -12
- package/types/corner-dot.ts +41 -4
- package/types/corner-eye.ts +31 -4
- package/types/frame-spec.ts +748 -0
- package/types/frame.ts +67 -0
- package/types/image-pattern.ts +15 -0
- package/types/index.ts +106 -4
- package/types/qr.ts +180 -2
- package/types/template.ts +2 -2
- package/dist/cli.js +0 -4319
package/src/print.ts
ADDED
|
@@ -0,0 +1,794 @@
|
|
|
1
|
+
/** biome-ignore-all lint/style/noMagicNumbers: print measurements */
|
|
2
|
+
import type { jsPDF as JsPDF, ShadingPattern } from "jspdf";
|
|
3
|
+
import type { QRProps } from "../types";
|
|
4
|
+
import qrcodegen from "./codegen";
|
|
5
|
+
import { DEFAULT_MARGIN, ERROR_LEVEL_MAP } from "./constants";
|
|
6
|
+
import { downloadFile } from "./download";
|
|
7
|
+
import { getFrameLayout, isFrameActive } from "./frame";
|
|
8
|
+
import { getLayoutMargin } from "./qr-shape";
|
|
9
|
+
import { resolveErrorLevel } from "./utils";
|
|
10
|
+
import { COORD_DECIMALS, fmt } from "./utils/fmt";
|
|
11
|
+
import { type RGBAColor, rgbToCmyk } from "./utils/parse-color";
|
|
12
|
+
import {
|
|
13
|
+
getPrintBleedFill,
|
|
14
|
+
PRINT_RENDER_SIZE,
|
|
15
|
+
renderPrintSVG,
|
|
16
|
+
setBackdrop,
|
|
17
|
+
} from "./utils/print-shared";
|
|
18
|
+
import {
|
|
19
|
+
applyMatrix,
|
|
20
|
+
type Matrix,
|
|
21
|
+
matrixScale,
|
|
22
|
+
multiply,
|
|
23
|
+
parseSvgMarkup,
|
|
24
|
+
type Segment,
|
|
25
|
+
type SvgNode,
|
|
26
|
+
} from "./utils/svg-geometry";
|
|
27
|
+
import {
|
|
28
|
+
collectGradients,
|
|
29
|
+
formatMatrix,
|
|
30
|
+
invertMatrix,
|
|
31
|
+
type PlacedGradient,
|
|
32
|
+
type SvgGradient,
|
|
33
|
+
shadingDictionary,
|
|
34
|
+
} from "./utils/svg-gradient";
|
|
35
|
+
import {
|
|
36
|
+
collectGradientColors,
|
|
37
|
+
collectImageHrefs,
|
|
38
|
+
imageFormat,
|
|
39
|
+
imagePlacement,
|
|
40
|
+
length,
|
|
41
|
+
type PaintState,
|
|
42
|
+
rasterizeImage,
|
|
43
|
+
SvgPainter,
|
|
44
|
+
} from "./utils/svg-painter";
|
|
45
|
+
|
|
46
|
+
export { type RGBAColor, rgbToCmyk } from "./utils/parse-color";
|
|
47
|
+
|
|
48
|
+
export type PrintUnit = "mm" | "in";
|
|
49
|
+
export type PrintColorMode = "rgb" | "cmyk";
|
|
50
|
+
|
|
51
|
+
export interface PrintOptions {
|
|
52
|
+
/** Width of the QR code itself, in `unit`. A frame adds to the printed size. */
|
|
53
|
+
size: number;
|
|
54
|
+
/** Unit for `size` (default "mm") */
|
|
55
|
+
unit?: PrintUnit;
|
|
56
|
+
/** Bleed on every side, in millimetres (0 by default; 3 is typical) */
|
|
57
|
+
bleed?: number;
|
|
58
|
+
/** Draw crop marks outside the bleed (default false) */
|
|
59
|
+
cropMarks?: boolean;
|
|
60
|
+
/** "cmyk" writes naive CMYK colors, see `rgbToCmyk` (default "rgb") */
|
|
61
|
+
colorMode?: PrintColorMode;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Physical layout of a print file. All lengths are in millimetres. */
|
|
65
|
+
export interface PrintLayout {
|
|
66
|
+
/** Width of the QR code, quiet zone (margin) included */
|
|
67
|
+
codeWidth: number;
|
|
68
|
+
/** Finished (trimmed) artwork size, frame included */
|
|
69
|
+
trimWidth: number;
|
|
70
|
+
trimHeight: number;
|
|
71
|
+
bleed: number;
|
|
72
|
+
/** Page size: trim + bleed (+ room for crop marks) */
|
|
73
|
+
pageWidth: number;
|
|
74
|
+
pageHeight: number;
|
|
75
|
+
/** Top-left corner of the trim box on the page */
|
|
76
|
+
trimX: number;
|
|
77
|
+
trimY: number;
|
|
78
|
+
/** Width of one QR module */
|
|
79
|
+
moduleSize: number;
|
|
80
|
+
/** Modules across the code, margin included */
|
|
81
|
+
moduleCount: number;
|
|
82
|
+
/** Rule of thumb: a code scans from about 10× its width */
|
|
83
|
+
maxScanDistance: number;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const MM_PER_INCH = 25.4;
|
|
87
|
+
const MIN_PRINT_SIZE_MM = 5;
|
|
88
|
+
const MAX_PRINT_SIZE_MM = 2000;
|
|
89
|
+
// Logical render size; print output is scaled from this.
|
|
90
|
+
const RENDER_SIZE = PRINT_RENDER_SIZE;
|
|
91
|
+
const CROP_MARK_LENGTH = 5;
|
|
92
|
+
const CROP_MARK_GAP = 2;
|
|
93
|
+
const CROP_MARK_WIDTH = 0.25 * (MM_PER_INCH / 72); // 0.25pt hairline
|
|
94
|
+
|
|
95
|
+
function toMillimetres(value: number, unit: PrintUnit = "mm"): number {
|
|
96
|
+
return unit === "in" ? value * MM_PER_INCH : value;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Modules across the code for these props, margin included. */
|
|
100
|
+
export function getQRModuleCount(props: QRProps): number {
|
|
101
|
+
const level = resolveErrorLevel(props);
|
|
102
|
+
const symbol = qrcodegen.QrCode.encodeText(
|
|
103
|
+
props.value,
|
|
104
|
+
ERROR_LEVEL_MAP[level]
|
|
105
|
+
).size;
|
|
106
|
+
// A circle code is drawn further in, inside its disc
|
|
107
|
+
return (
|
|
108
|
+
symbol +
|
|
109
|
+
getLayoutMargin(symbol, props.margin ?? DEFAULT_MARGIN, props.shape) * 2
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Computes the physical size of a print file (no rendering). Throws when the
|
|
115
|
+
* size is out of range (5 mm to 2 m).
|
|
116
|
+
*/
|
|
117
|
+
export function getPrintLayout(
|
|
118
|
+
props: QRProps,
|
|
119
|
+
options: PrintOptions
|
|
120
|
+
): PrintLayout {
|
|
121
|
+
const codeWidth = toMillimetres(options.size, options.unit);
|
|
122
|
+
if (
|
|
123
|
+
!Number.isFinite(codeWidth) ||
|
|
124
|
+
codeWidth < MIN_PRINT_SIZE_MM ||
|
|
125
|
+
codeWidth > MAX_PRINT_SIZE_MM
|
|
126
|
+
) {
|
|
127
|
+
throw new Error(
|
|
128
|
+
`Print size must be between ${MIN_PRINT_SIZE_MM} mm and ${MAX_PRINT_SIZE_MM / 1000} m`
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
const bleed = Math.min(Math.max(options.bleed ?? 0, 0), 20);
|
|
132
|
+
const scale = codeWidth / RENDER_SIZE;
|
|
133
|
+
const frame = isFrameActive(props.frame)
|
|
134
|
+
? getFrameLayout(RENDER_SIZE, props.frame)
|
|
135
|
+
: null;
|
|
136
|
+
const trimWidth = (frame?.width ?? RENDER_SIZE) * scale;
|
|
137
|
+
const trimHeight = (frame?.height ?? RENDER_SIZE) * scale;
|
|
138
|
+
const slug = options.cropMarks
|
|
139
|
+
? bleed + CROP_MARK_GAP + CROP_MARK_LENGTH + 1
|
|
140
|
+
: bleed;
|
|
141
|
+
const moduleCount = getQRModuleCount(props);
|
|
142
|
+
|
|
143
|
+
return {
|
|
144
|
+
codeWidth,
|
|
145
|
+
trimWidth,
|
|
146
|
+
trimHeight,
|
|
147
|
+
bleed,
|
|
148
|
+
pageWidth: trimWidth + 2 * slug,
|
|
149
|
+
pageHeight: trimHeight + 2 * slug,
|
|
150
|
+
trimX: slug,
|
|
151
|
+
trimY: slug,
|
|
152
|
+
moduleSize: codeWidth / moduleCount,
|
|
153
|
+
moduleCount,
|
|
154
|
+
maxScanDistance: codeWidth * 10,
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// ── SVG → jsPDF ────────────────────────────────────────────────────────────
|
|
159
|
+
|
|
160
|
+
/** jsPDF internals the shading code uses (left out of jsPDF's typings). */
|
|
161
|
+
interface JsPDFInternals {
|
|
162
|
+
write(...parts: string[]): void;
|
|
163
|
+
events: { subscribe(topic: string, callback: () => void): string };
|
|
164
|
+
scaleFactor: number;
|
|
165
|
+
pageSize: { getHeight(): number };
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Smooth shadings in the page's /Shading resources, painted with `sh`.
|
|
170
|
+
* jsPDF writes that dictionary only while it holds a pattern of its own, and
|
|
171
|
+
* lets plugins add entries on "putShadingPatternDict". A placeholder pattern
|
|
172
|
+
* (neither a ShadingPattern nor a TilingPattern, so jsPDF writes no object
|
|
173
|
+
* for it) makes it write the dictionary; the entries are ours.
|
|
174
|
+
*/
|
|
175
|
+
class PdfShadings {
|
|
176
|
+
private readonly pdf: JsPDF;
|
|
177
|
+
private readonly internals: JsPDFInternals;
|
|
178
|
+
/** Shading dictionary → resource name */
|
|
179
|
+
private readonly names = new Map<string, string>();
|
|
180
|
+
|
|
181
|
+
private constructor(pdf: JsPDF, internals: JsPDFInternals) {
|
|
182
|
+
this.pdf = pdf;
|
|
183
|
+
this.internals = internals;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** Null when this jsPDF build offers no way to add shading resources. */
|
|
187
|
+
static attach(pdf: JsPDF): PdfShadings | null {
|
|
188
|
+
const internals = pdf.internal as unknown as Partial<JsPDFInternals>;
|
|
189
|
+
if (
|
|
190
|
+
typeof pdf.advancedAPI !== "function" ||
|
|
191
|
+
typeof pdf.addShadingPattern !== "function" ||
|
|
192
|
+
typeof internals.write !== "function" ||
|
|
193
|
+
typeof internals.events?.subscribe !== "function"
|
|
194
|
+
) {
|
|
195
|
+
return null;
|
|
196
|
+
}
|
|
197
|
+
return new PdfShadings(pdf, internals as JsPDFInternals);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** Resource name of a shading dictionary, added on first use. */
|
|
201
|
+
name(dictionary: string): string {
|
|
202
|
+
let name = this.names.get(dictionary);
|
|
203
|
+
if (!name) {
|
|
204
|
+
if (this.names.size === 0) {
|
|
205
|
+
this.install();
|
|
206
|
+
}
|
|
207
|
+
name = `Sh${this.names.size}`;
|
|
208
|
+
this.names.set(dictionary, name);
|
|
209
|
+
}
|
|
210
|
+
return name;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
private install() {
|
|
214
|
+
// Patterns need jsPDF's "advanced" mode, which wraps this in "q … cm Q".
|
|
215
|
+
this.pdf.advancedAPI((doc) => {
|
|
216
|
+
doc.addShadingPattern("qrdx-shadings", {} as ShadingPattern);
|
|
217
|
+
});
|
|
218
|
+
this.internals.events.subscribe("putShadingPatternDict", () => {
|
|
219
|
+
for (const [dictionary, name] of this.names) {
|
|
220
|
+
this.internals.write(`/${name} ${dictionary}`);
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
class PdfPainter extends SvgPainter {
|
|
227
|
+
private readonly pdf: JsPDF;
|
|
228
|
+
private readonly cmyk: boolean;
|
|
229
|
+
private readonly images: Map<string, string>;
|
|
230
|
+
private readonly shadingResources: PdfShadings | null;
|
|
231
|
+
|
|
232
|
+
constructor(
|
|
233
|
+
pdf: JsPDF,
|
|
234
|
+
cmyk: boolean,
|
|
235
|
+
gradients: Map<string, string>,
|
|
236
|
+
shadings: {
|
|
237
|
+
gradients: Map<string, SvgGradient>;
|
|
238
|
+
resources: PdfShadings;
|
|
239
|
+
} | null,
|
|
240
|
+
images: Map<string, string>
|
|
241
|
+
) {
|
|
242
|
+
super(gradients, shadings?.gradients ?? null);
|
|
243
|
+
this.pdf = pdf;
|
|
244
|
+
this.cmyk = cmyk;
|
|
245
|
+
this.images = images;
|
|
246
|
+
this.shadingResources = shadings?.resources ?? null;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
setColor(kind: "fill" | "draw" | "text", color: RGBAColor) {
|
|
250
|
+
const set =
|
|
251
|
+
kind === "fill"
|
|
252
|
+
? this.pdf.setFillColor.bind(this.pdf)
|
|
253
|
+
: kind === "draw"
|
|
254
|
+
? this.pdf.setDrawColor.bind(this.pdf)
|
|
255
|
+
: this.pdf.setTextColor.bind(this.pdf);
|
|
256
|
+
if (this.cmyk) {
|
|
257
|
+
const [c, m, y, k] = rgbToCmyk(color.r, color.g, color.b);
|
|
258
|
+
set(c, m, y, k);
|
|
259
|
+
} else {
|
|
260
|
+
set(Math.round(color.r), Math.round(color.g), Math.round(color.b));
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
setRegistrationColor() {
|
|
265
|
+
if (this.cmyk) {
|
|
266
|
+
this.pdf.setDrawColor(1, 1, 1, 1);
|
|
267
|
+
} else {
|
|
268
|
+
this.pdf.setDrawColor(0, 0, 0);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
tracePath(segments: Segment[], matrix: Matrix) {
|
|
273
|
+
let open = false;
|
|
274
|
+
for (const segment of segments) {
|
|
275
|
+
if (segment.op === "M") {
|
|
276
|
+
const [x, y] = applyMatrix(matrix, segment.x, segment.y);
|
|
277
|
+
this.pdf.moveTo(x, y);
|
|
278
|
+
open = true;
|
|
279
|
+
} else if (segment.op === "L") {
|
|
280
|
+
const [x, y] = applyMatrix(matrix, segment.x, segment.y);
|
|
281
|
+
if (open) {
|
|
282
|
+
this.pdf.lineTo(x, y);
|
|
283
|
+
} else {
|
|
284
|
+
this.pdf.moveTo(x, y);
|
|
285
|
+
open = true;
|
|
286
|
+
}
|
|
287
|
+
} else if (segment.op === "C") {
|
|
288
|
+
const [x1, y1] = applyMatrix(matrix, segment.x1, segment.y1);
|
|
289
|
+
const [x2, y2] = applyMatrix(matrix, segment.x2, segment.y2);
|
|
290
|
+
const [x, y] = applyMatrix(matrix, segment.x, segment.y);
|
|
291
|
+
this.pdf.curveTo(x1, y1, x2, y2, x, y);
|
|
292
|
+
} else if (open) {
|
|
293
|
+
this.pdf.close();
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Fills the shape with a gradient: `q <path> W n <matrix> cm /ShN sh Q`,
|
|
300
|
+
* i.e. clip to its exact outline, then shade in gradient space.
|
|
301
|
+
*/
|
|
302
|
+
shade(segments: Segment[], state: PaintState, gradient: PlacedGradient) {
|
|
303
|
+
const internals = this.pdf.internal as unknown as JsPDFInternals;
|
|
304
|
+
const k = internals.scaleFactor;
|
|
305
|
+
// jsPDF takes millimetres from the top-left corner; the content stream
|
|
306
|
+
// is in points from the bottom-left.
|
|
307
|
+
const toPage: Matrix = [k, 0, 0, -k, 0, internals.pageSize.getHeight() * k];
|
|
308
|
+
const matrix = multiply(toPage, multiply(state.matrix, gradient.matrix));
|
|
309
|
+
if (!(this.shadingResources && invertMatrix(matrix))) {
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
312
|
+
const name = this.shadingResources.name(
|
|
313
|
+
shadingDictionary(gradient, this.cmyk ? "cmyk" : "rgb")
|
|
314
|
+
);
|
|
315
|
+
this.pdf.saveGraphicsState();
|
|
316
|
+
this.tracePath(segments, state.matrix);
|
|
317
|
+
this.pdf.clip(state.fillRule === "evenodd" ? "evenodd" : undefined);
|
|
318
|
+
this.pdf.discardPath();
|
|
319
|
+
internals.write(`${formatMatrix(matrix)} cm /${name} sh`);
|
|
320
|
+
this.pdf.restoreGraphicsState();
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/** `q <path> W n`: everything until `endClip` stays inside the path. */
|
|
324
|
+
override beginClip(segments: Segment[], matrix: Matrix, evenOdd: boolean) {
|
|
325
|
+
this.pdf.saveGraphicsState();
|
|
326
|
+
this.tracePath(segments, matrix);
|
|
327
|
+
this.pdf.clip(evenOdd ? "evenodd" : undefined);
|
|
328
|
+
this.pdf.discardPath();
|
|
329
|
+
return true;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
override endClip() {
|
|
333
|
+
this.pdf.restoreGraphicsState();
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* Runs `draw` with fill (and stroke) opacity set through an ExtGState
|
|
338
|
+
* (`/GSn gs`), inside `q … Q` so it doesn't leak. Opaque paint draws as is.
|
|
339
|
+
*/
|
|
340
|
+
private withAlpha(fill: number, stroke: number, draw: () => void) {
|
|
341
|
+
if (fill >= 1 && stroke >= 1) {
|
|
342
|
+
draw();
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
this.pdf.saveGraphicsState();
|
|
346
|
+
// jsPDF types GState as a method; it is the GState class.
|
|
347
|
+
const GState = this.pdf.GState as unknown as new (parameters: {
|
|
348
|
+
opacity?: number;
|
|
349
|
+
"stroke-opacity"?: number;
|
|
350
|
+
}) => unknown;
|
|
351
|
+
this.pdf.setGState(new GState({ opacity: fill, "stroke-opacity": stroke }));
|
|
352
|
+
try {
|
|
353
|
+
draw();
|
|
354
|
+
} finally {
|
|
355
|
+
this.pdf.restoreGraphicsState();
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/**
|
|
360
|
+
* A gradient ready to shade with one alpha for all of it: PDF shadings
|
|
361
|
+
* have no per-stop alpha, so stops that all share an alpha use it as the
|
|
362
|
+
* fill opacity, and stops that differ are flattened over the backdrop.
|
|
363
|
+
*/
|
|
364
|
+
private opaqueGradient(gradient: PlacedGradient, alpha: number) {
|
|
365
|
+
const first = gradient.stops[0]?.color.a ?? 1;
|
|
366
|
+
const uniform = gradient.stops.every(
|
|
367
|
+
(stop) => Math.abs(stop.color.a - first) < 0.002
|
|
368
|
+
);
|
|
369
|
+
if (uniform) {
|
|
370
|
+
return {
|
|
371
|
+
gradient: {
|
|
372
|
+
...gradient,
|
|
373
|
+
stops: gradient.stops.map((stop) => ({
|
|
374
|
+
...stop,
|
|
375
|
+
color: { ...stop.color, a: 1 },
|
|
376
|
+
})),
|
|
377
|
+
},
|
|
378
|
+
alpha: first * alpha,
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
return {
|
|
382
|
+
gradient: {
|
|
383
|
+
...gradient,
|
|
384
|
+
stops: gradient.stops.map((stop) => ({
|
|
385
|
+
...stop,
|
|
386
|
+
color: this.flatten({ ...stop.color, a: stop.color.a * alpha }),
|
|
387
|
+
})),
|
|
388
|
+
},
|
|
389
|
+
alpha: 1,
|
|
390
|
+
};
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
paint(segments: Segment[], _node: SvgNode, state: PaintState) {
|
|
394
|
+
if (segments.length === 0) {
|
|
395
|
+
return;
|
|
396
|
+
}
|
|
397
|
+
const opacity = state.opacity ?? 1;
|
|
398
|
+
if (opacity <= 0) {
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
401
|
+
const paint = this.resolveFill(
|
|
402
|
+
state.fill,
|
|
403
|
+
segments,
|
|
404
|
+
state.viewport,
|
|
405
|
+
(state.fillOpacity ?? 1) * opacity
|
|
406
|
+
);
|
|
407
|
+
if (paint && "pattern" in paint) {
|
|
408
|
+
this.paintPattern(segments, state, paint.pattern, paint.alpha);
|
|
409
|
+
} else if (paint && "gradient" in paint) {
|
|
410
|
+
const { gradient, alpha } = this.opaqueGradient(
|
|
411
|
+
paint.gradient,
|
|
412
|
+
paint.alpha
|
|
413
|
+
);
|
|
414
|
+
this.withAlpha(alpha, 1, () => this.shade(segments, state, gradient));
|
|
415
|
+
}
|
|
416
|
+
// An angular fan's wedges overlap (see conicWedges), and PDF blends each
|
|
417
|
+
// fill on its own: flatten see-through wedges over the backdrop.
|
|
418
|
+
const solid = paint && "color" in paint ? paint.color : null;
|
|
419
|
+
const fill = solid && this.inConic ? this.flatten(solid) : solid;
|
|
420
|
+
const strokeColor =
|
|
421
|
+
state.strokeWidth > 0 ? this.resolveColor(state.stroke) : null;
|
|
422
|
+
const stroke = strokeColor && {
|
|
423
|
+
...strokeColor,
|
|
424
|
+
a: strokeColor.a * opacity,
|
|
425
|
+
};
|
|
426
|
+
if (!(fill || stroke)) {
|
|
427
|
+
return;
|
|
428
|
+
}
|
|
429
|
+
const evenOdd = state.fillRule === "evenodd";
|
|
430
|
+
|
|
431
|
+
this.withAlpha(fill?.a ?? 1, stroke?.a ?? 1, () => {
|
|
432
|
+
if (fill) {
|
|
433
|
+
this.setColor("fill", fill);
|
|
434
|
+
}
|
|
435
|
+
if (stroke) {
|
|
436
|
+
this.setColor("draw", stroke);
|
|
437
|
+
this.pdf.setLineWidth(state.strokeWidth * matrixScale(state.matrix));
|
|
438
|
+
}
|
|
439
|
+
this.tracePath(segments, state.matrix);
|
|
440
|
+
if (fill && stroke) {
|
|
441
|
+
if (evenOdd) {
|
|
442
|
+
this.pdf.fillStrokeEvenOdd();
|
|
443
|
+
} else {
|
|
444
|
+
this.pdf.fillStroke();
|
|
445
|
+
}
|
|
446
|
+
} else if (fill) {
|
|
447
|
+
if (evenOdd) {
|
|
448
|
+
this.pdf.fillEvenOdd();
|
|
449
|
+
} else {
|
|
450
|
+
this.pdf.fill();
|
|
451
|
+
}
|
|
452
|
+
} else {
|
|
453
|
+
this.pdf.stroke();
|
|
454
|
+
}
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
text(node: SvgNode, state: PaintState) {
|
|
459
|
+
const content = node.text.replace(/\s+/g, " ").trim();
|
|
460
|
+
const color = this.resolveColor(state.fill);
|
|
461
|
+
if (!(content && color)) {
|
|
462
|
+
return;
|
|
463
|
+
}
|
|
464
|
+
const scale = matrixScale(state.matrix);
|
|
465
|
+
const [x, y] = applyMatrix(
|
|
466
|
+
state.matrix,
|
|
467
|
+
length(node.attrs.x, state.viewport[0]),
|
|
468
|
+
length(node.attrs.y, state.viewport[1])
|
|
469
|
+
);
|
|
470
|
+
const bold =
|
|
471
|
+
state.fontWeight === "bold" ||
|
|
472
|
+
Number.parseInt(state.fontWeight, 10) >= 600;
|
|
473
|
+
this.pdf.setFont("helvetica", bold ? "bold" : "normal");
|
|
474
|
+
// jsPDF font sizes are in points; the artwork is in millimetres.
|
|
475
|
+
let fontSize = state.fontSize * scale * (72 / MM_PER_INCH);
|
|
476
|
+
this.pdf.setFontSize(fontSize);
|
|
477
|
+
const maxWidth = Number.parseFloat(node.attrs["data-max-width"] ?? "");
|
|
478
|
+
if (maxWidth > 0) {
|
|
479
|
+
const fitWidth = maxWidth * scale;
|
|
480
|
+
const width = this.pdf.getTextWidth(content);
|
|
481
|
+
if (width > fitWidth) {
|
|
482
|
+
fontSize *= fitWidth / width;
|
|
483
|
+
this.pdf.setFontSize(fontSize);
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
this.setColor("text", color);
|
|
487
|
+
// Rotated text (letters set on a curve): jsPDF turns counter-clockwise,
|
|
488
|
+
// SVG clockwise. Anchor along the rotated baseline ourselves.
|
|
489
|
+
const angle = Math.atan2(state.matrix[1], state.matrix[0]);
|
|
490
|
+
if (Math.abs(angle) > 0.0002) {
|
|
491
|
+
const shift =
|
|
492
|
+
state.textAnchor === "middle"
|
|
493
|
+
? this.pdf.getTextWidth(content) / 2
|
|
494
|
+
: state.textAnchor === "end"
|
|
495
|
+
? this.pdf.getTextWidth(content)
|
|
496
|
+
: 0;
|
|
497
|
+
this.pdf.text(
|
|
498
|
+
content,
|
|
499
|
+
x - shift * Math.cos(angle),
|
|
500
|
+
y - shift * Math.sin(angle),
|
|
501
|
+
{ angle: (-angle * 180) / Math.PI, baseline: "alphabetic" }
|
|
502
|
+
);
|
|
503
|
+
return;
|
|
504
|
+
}
|
|
505
|
+
const align =
|
|
506
|
+
state.textAnchor === "middle"
|
|
507
|
+
? "center"
|
|
508
|
+
: state.textAnchor === "end"
|
|
509
|
+
? "right"
|
|
510
|
+
: "left";
|
|
511
|
+
this.pdf.text(content, x, y, { align, baseline: "alphabetic" });
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
/** jsPDF image alias per source, so each picture is embedded and hashed once */
|
|
515
|
+
private readonly aliases = new Map<string, string>();
|
|
516
|
+
|
|
517
|
+
image(node: SvgNode, state: PaintState) {
|
|
518
|
+
const href = node.attrs.href ?? node.attrs["xlink:href"];
|
|
519
|
+
const source = href && (imageFormat(href) ? href : this.images.get(href));
|
|
520
|
+
const format = source && imageFormat(source);
|
|
521
|
+
if (!(source && format)) {
|
|
522
|
+
return;
|
|
523
|
+
}
|
|
524
|
+
let alias = this.aliases.get(source);
|
|
525
|
+
if (!alias) {
|
|
526
|
+
alias = `qrdx-image-${this.aliases.size}`;
|
|
527
|
+
this.aliases.set(source, alias);
|
|
528
|
+
}
|
|
529
|
+
let intrinsic: { width: number; height: number } | null = null;
|
|
530
|
+
if (node.attrs.preserveAspectRatio) {
|
|
531
|
+
try {
|
|
532
|
+
const properties = this.pdf.getImageProperties(source);
|
|
533
|
+
intrinsic = { width: properties.width, height: properties.height };
|
|
534
|
+
} catch {
|
|
535
|
+
intrinsic = null;
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
const placement = imagePlacement(node, state.viewport, intrinsic);
|
|
539
|
+
if (!placement) {
|
|
540
|
+
return;
|
|
541
|
+
}
|
|
542
|
+
const { draw, clip } = placement;
|
|
543
|
+
const [x0, y0] = applyMatrix(state.matrix, draw.x, draw.y);
|
|
544
|
+
const [x1, y1] = applyMatrix(
|
|
545
|
+
state.matrix,
|
|
546
|
+
draw.x + draw.width,
|
|
547
|
+
draw.y + draw.height
|
|
548
|
+
);
|
|
549
|
+
const clipped =
|
|
550
|
+
clip !== null &&
|
|
551
|
+
this.beginClip(
|
|
552
|
+
[
|
|
553
|
+
{ op: "M", x: clip.x, y: clip.y },
|
|
554
|
+
{ op: "L", x: clip.x + clip.width, y: clip.y },
|
|
555
|
+
{ op: "L", x: clip.x + clip.width, y: clip.y + clip.height },
|
|
556
|
+
{ op: "L", x: clip.x, y: clip.y + clip.height },
|
|
557
|
+
{ op: "Z" },
|
|
558
|
+
],
|
|
559
|
+
state.matrix,
|
|
560
|
+
false
|
|
561
|
+
);
|
|
562
|
+
try {
|
|
563
|
+
this.withAlpha(state.opacity ?? 1, 1, () => {
|
|
564
|
+
this.pdf.addImage(
|
|
565
|
+
source,
|
|
566
|
+
format,
|
|
567
|
+
Math.min(x0, x1),
|
|
568
|
+
Math.min(y0, y1),
|
|
569
|
+
Math.abs(x1 - x0),
|
|
570
|
+
Math.abs(y1 - y0),
|
|
571
|
+
alias
|
|
572
|
+
);
|
|
573
|
+
});
|
|
574
|
+
} finally {
|
|
575
|
+
if (clipped) {
|
|
576
|
+
this.endClip();
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
/** Matrix that maps a nested <svg> viewport onto its parent. */
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
function drawCropMarks(pdf: JsPDF, painter: PdfPainter, layout: PrintLayout) {
|
|
585
|
+
const { trimX, trimY, trimWidth, trimHeight, bleed } = layout;
|
|
586
|
+
const offset = bleed + CROP_MARK_GAP;
|
|
587
|
+
const left = trimX;
|
|
588
|
+
const right = trimX + trimWidth;
|
|
589
|
+
const top = trimY;
|
|
590
|
+
const bottom = trimY + trimHeight;
|
|
591
|
+
painter.setRegistrationColor();
|
|
592
|
+
pdf.setLineWidth(CROP_MARK_WIDTH);
|
|
593
|
+
for (const x of [left, right]) {
|
|
594
|
+
const outward = x === left ? -1 : 1;
|
|
595
|
+
for (const y of [top, bottom]) {
|
|
596
|
+
const vertical = y === top ? -1 : 1;
|
|
597
|
+
// Horizontal mark beside the corner
|
|
598
|
+
pdf.line(
|
|
599
|
+
x + outward * offset,
|
|
600
|
+
y,
|
|
601
|
+
x + outward * (offset + CROP_MARK_LENGTH),
|
|
602
|
+
y
|
|
603
|
+
);
|
|
604
|
+
// Vertical mark above/below the corner
|
|
605
|
+
pdf.line(
|
|
606
|
+
x,
|
|
607
|
+
y + vertical * offset,
|
|
608
|
+
x,
|
|
609
|
+
y + vertical * (offset + CROP_MARK_LENGTH)
|
|
610
|
+
);
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
/**
|
|
616
|
+
* Renders a print-ready vector PDF: modules, eyes and frame are vector paths
|
|
617
|
+
* (logos stay raster images) at the exact physical size, with optional bleed
|
|
618
|
+
* and crop marks. In "cmyk" mode colors are converted with `rgbToCmyk`.
|
|
619
|
+
* Linear and radial gradient fills are native PDF shadings clipped to each
|
|
620
|
+
* shape (in "cmyk" mode they blend between the converted stops); gradient
|
|
621
|
+
* strokes and text use their first color. Frame text uses Helvetica Bold, so
|
|
622
|
+
* characters outside Latin-1 may not render.
|
|
623
|
+
*
|
|
624
|
+
* Returns a `data:application/pdf` URL. Needs the optional `jspdf` package.
|
|
625
|
+
*/
|
|
626
|
+
export async function getQRAsPrintPDF(
|
|
627
|
+
props: QRProps,
|
|
628
|
+
options: PrintOptions
|
|
629
|
+
): Promise<string> {
|
|
630
|
+
const pdf = await createPrintPDF(props, options);
|
|
631
|
+
return pdf.output("dataurlstring");
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
/** Same as `getQRAsPrintPDF`, but returns the PDF as an ArrayBuffer. */
|
|
635
|
+
export async function getQRAsPrintPDFBuffer(
|
|
636
|
+
props: QRProps,
|
|
637
|
+
options: PrintOptions
|
|
638
|
+
): Promise<ArrayBuffer> {
|
|
639
|
+
const pdf = await createPrintPDF(props, options);
|
|
640
|
+
return pdf.output("arraybuffer");
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
async function createPrintPDF(
|
|
644
|
+
props: QRProps,
|
|
645
|
+
options: PrintOptions
|
|
646
|
+
): Promise<JsPDF> {
|
|
647
|
+
const layout = getPrintLayout(props, options);
|
|
648
|
+
|
|
649
|
+
let jsPDF: typeof import("jspdf").jsPDF;
|
|
650
|
+
try {
|
|
651
|
+
({ jsPDF } = await import("jspdf"));
|
|
652
|
+
} catch (error) {
|
|
653
|
+
throw new Error(
|
|
654
|
+
`PDF export needs the optional "jspdf" package. Install it with \`npm install jspdf\`. (${error})`
|
|
655
|
+
);
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
const markup = await renderPrintSVG(props);
|
|
659
|
+
const root = parseSvgMarkup(markup);
|
|
660
|
+
if (!root) {
|
|
661
|
+
throw new Error("Failed to render the QR code for printing");
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
const images = new Map<string, string>();
|
|
665
|
+
for (const href of collectImageHrefs(root, new Set())) {
|
|
666
|
+
if (!imageFormat(href)) {
|
|
667
|
+
const png = await rasterizeImage(href);
|
|
668
|
+
if (png) {
|
|
669
|
+
images.set(href, png);
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
const pdf = new jsPDF({
|
|
675
|
+
orientation:
|
|
676
|
+
layout.pageWidth > layout.pageHeight ? "landscape" : "portrait",
|
|
677
|
+
unit: "mm",
|
|
678
|
+
format: [layout.pageWidth, layout.pageHeight],
|
|
679
|
+
compress: true,
|
|
680
|
+
// jsPDF writes path coordinates (in points) with 16 decimals by
|
|
681
|
+
// default; 0.001 pt is 0.35 µm, far finer than any press.
|
|
682
|
+
floatPrecision: COORD_DECIMALS,
|
|
683
|
+
});
|
|
684
|
+
pdf.setProperties({ title: "QR code", creator: "qrdx" });
|
|
685
|
+
|
|
686
|
+
const gradients = collectGradients(root);
|
|
687
|
+
const resources = gradients.size > 0 ? PdfShadings.attach(pdf) : null;
|
|
688
|
+
const painter = new PdfPainter(
|
|
689
|
+
pdf,
|
|
690
|
+
options.colorMode === "cmyk",
|
|
691
|
+
collectGradientColors(root, new Map()),
|
|
692
|
+
resources ? { gradients, resources } : null,
|
|
693
|
+
images
|
|
694
|
+
);
|
|
695
|
+
|
|
696
|
+
setBackdrop(painter, props);
|
|
697
|
+
|
|
698
|
+
const scale = layout.codeWidth / RENDER_SIZE;
|
|
699
|
+
const rootWidth = length(root.attrs.width, RENDER_SIZE, RENDER_SIZE);
|
|
700
|
+
const rootHeight = length(root.attrs.height, RENDER_SIZE, RENDER_SIZE);
|
|
701
|
+
const state: PaintState = {
|
|
702
|
+
matrix: multiply(
|
|
703
|
+
[1, 0, 0, 1, layout.trimX, layout.trimY],
|
|
704
|
+
[scale, 0, 0, scale, 0, 0]
|
|
705
|
+
),
|
|
706
|
+
viewport: [rootWidth, rootHeight],
|
|
707
|
+
fill: "#000000",
|
|
708
|
+
fillRule: "nonzero",
|
|
709
|
+
stroke: "none",
|
|
710
|
+
strokeWidth: 1,
|
|
711
|
+
fontSize: 16,
|
|
712
|
+
fontWeight: "normal",
|
|
713
|
+
textAnchor: "start",
|
|
714
|
+
};
|
|
715
|
+
|
|
716
|
+
// Extend the edge color into the bleed so trimming leaves no white sliver;
|
|
717
|
+
// a gradient background continues into it.
|
|
718
|
+
const bleedFill = layout.bleed > 0 ? getPrintBleedFill(props, markup) : null;
|
|
719
|
+
const bleedBox: [number, number, number, number] = [
|
|
720
|
+
layout.trimX - layout.bleed,
|
|
721
|
+
layout.trimY - layout.bleed,
|
|
722
|
+
layout.trimX + layout.trimWidth + layout.bleed,
|
|
723
|
+
layout.trimY + layout.trimHeight + layout.bleed,
|
|
724
|
+
];
|
|
725
|
+
if (!(bleedFill && painter.paintBleed(root, state, bleedFill, bleedBox))) {
|
|
726
|
+
const bleedColor = bleedFill ? painter.resolveColor(bleedFill) : null;
|
|
727
|
+
if (bleedColor) {
|
|
728
|
+
painter.setColor("fill", bleedColor);
|
|
729
|
+
pdf.rect(
|
|
730
|
+
layout.trimX - layout.bleed,
|
|
731
|
+
layout.trimY - layout.bleed,
|
|
732
|
+
layout.trimWidth + 2 * layout.bleed,
|
|
733
|
+
layout.trimHeight + 2 * layout.bleed,
|
|
734
|
+
"F"
|
|
735
|
+
);
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
painter.walk(root, state, true);
|
|
740
|
+
|
|
741
|
+
if (options.cropMarks) {
|
|
742
|
+
drawCropMarks(pdf, painter, layout);
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
return pdf;
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
/**
|
|
749
|
+
* Returns the artwork as SVG markup at its physical trim size (e.g.
|
|
750
|
+
* `width="40mm"`). SVG has no CMYK, so colors stay RGB; bleed and crop marks
|
|
751
|
+
* are left to your layout tool.
|
|
752
|
+
*/
|
|
753
|
+
export async function getQRAsPrintSVG(
|
|
754
|
+
props: QRProps,
|
|
755
|
+
options: Pick<PrintOptions, "size" | "unit">
|
|
756
|
+
): Promise<string> {
|
|
757
|
+
const layout = getPrintLayout(props, {
|
|
758
|
+
size: options.size,
|
|
759
|
+
unit: options.unit,
|
|
760
|
+
});
|
|
761
|
+
const markup = await renderPrintSVG(props);
|
|
762
|
+
const unit = options.unit ?? "mm";
|
|
763
|
+
const toUnit = (mm: number) =>
|
|
764
|
+
`${fmt(unit === "in" ? mm / MM_PER_INCH : mm)}${unit}`;
|
|
765
|
+
const rootTag = /^<svg\b[^>]*>/.exec(markup)?.[0];
|
|
766
|
+
if (!rootTag) {
|
|
767
|
+
throw new Error("Failed to render the QR code for printing");
|
|
768
|
+
}
|
|
769
|
+
const sizedTag = rootTag
|
|
770
|
+
.replace(/\swidth="[^"]*"/, ` width="${toUnit(layout.trimWidth)}"`)
|
|
771
|
+
.replace(/\sheight="[^"]*"/, ` height="${toUnit(layout.trimHeight)}"`);
|
|
772
|
+
return `<?xml version="1.0" encoding="UTF-8"?>\n${sizedTag}${markup.slice(rootTag.length)}`;
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
/** Downloads `getQRAsPrintPDF` output (browser only). */
|
|
776
|
+
export async function downloadPrintPDF(
|
|
777
|
+
props: QRProps,
|
|
778
|
+
options: PrintOptions & { filename?: string }
|
|
779
|
+
): Promise<void> {
|
|
780
|
+
const dataUrl = await getQRAsPrintPDF(props, options);
|
|
781
|
+
downloadFile(dataUrl, options.filename ?? "qr-code-print.pdf");
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
/** Downloads `getQRAsPrintSVG` output (browser only). */
|
|
785
|
+
export async function downloadPrintSVG(
|
|
786
|
+
props: QRProps,
|
|
787
|
+
options: Pick<PrintOptions, "size" | "unit"> & { filename?: string }
|
|
788
|
+
): Promise<void> {
|
|
789
|
+
const svg = await getQRAsPrintSVG(props, options);
|
|
790
|
+
downloadFile(
|
|
791
|
+
`data:image/svg+xml;charset=utf-8,${encodeURIComponent(svg)}`,
|
|
792
|
+
options.filename ?? "qr-code-print.svg"
|
|
793
|
+
);
|
|
794
|
+
}
|