qrdx 0.0.46 → 0.0.47
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 +353 -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 +22 -5
- 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 +24 -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.d.ts +18 -2
- package/dist/src/utils.d.ts.map +1 -1
- 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 +68 -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 +123 -52
- 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 +141 -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.tsx +794 -385
- 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/utils/png.ts
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/** biome-ignore-all lint/style/noMagicNumbers: PNG format constants */
|
|
2
|
+
/** biome-ignore-all lint/suspicious/noBitwiseOperators: PNG filters */
|
|
3
|
+
// Minimal PNG reader (8-bit, non-interlaced) and zlib helpers built on the
|
|
4
|
+
// Compression Streams API, which browsers and Node 18+ both provide.
|
|
5
|
+
|
|
6
|
+
export interface DecodedImage {
|
|
7
|
+
width: number;
|
|
8
|
+
height: number;
|
|
9
|
+
/** RGBA bytes, row by row */
|
|
10
|
+
data: Uint8Array;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const PNG_SIGNATURE = [137, 80, 78, 71, 13, 10, 26, 10];
|
|
14
|
+
|
|
15
|
+
async function transform(
|
|
16
|
+
bytes: Uint8Array,
|
|
17
|
+
stream: GenericTransformStream
|
|
18
|
+
): Promise<Uint8Array> {
|
|
19
|
+
const output = new Blob([bytes as BlobPart]).stream().pipeThrough(stream);
|
|
20
|
+
return new Uint8Array(await new Response(output).arrayBuffer());
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** zlib-wrapped deflate, or null when Compression Streams are unavailable. */
|
|
24
|
+
export async function deflate(bytes: Uint8Array): Promise<Uint8Array | null> {
|
|
25
|
+
if (typeof CompressionStream === "undefined") {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
return await transform(bytes, new CompressionStream("deflate"));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async function inflate(bytes: Uint8Array): Promise<Uint8Array | null> {
|
|
32
|
+
if (typeof DecompressionStream === "undefined") {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
try {
|
|
36
|
+
return await transform(bytes, new DecompressionStream("deflate"));
|
|
37
|
+
} catch {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const readUint32 = (bytes: Uint8Array, offset: number) =>
|
|
43
|
+
((bytes[offset] as number) * 2 ** 24 +
|
|
44
|
+
((bytes[offset + 1] as number) << 16) +
|
|
45
|
+
((bytes[offset + 2] as number) << 8) +
|
|
46
|
+
(bytes[offset + 3] as number)) >>>
|
|
47
|
+
0;
|
|
48
|
+
|
|
49
|
+
const CHANNELS: Record<number, number> = { 0: 1, 2: 3, 3: 1, 4: 2, 6: 4 };
|
|
50
|
+
|
|
51
|
+
function paeth(a: number, b: number, c: number): number {
|
|
52
|
+
const p = a + b - c;
|
|
53
|
+
const pa = Math.abs(p - a);
|
|
54
|
+
const pb = Math.abs(p - b);
|
|
55
|
+
const pc = Math.abs(p - c);
|
|
56
|
+
if (pa <= pb && pa <= pc) {
|
|
57
|
+
return a;
|
|
58
|
+
}
|
|
59
|
+
return pb <= pc ? b : c;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Decodes 8-bit, non-interlaced PNGs (gray, gray+alpha, RGB, RGBA, palette).
|
|
64
|
+
* Returns null for anything else.
|
|
65
|
+
*/
|
|
66
|
+
export async function decodePng(
|
|
67
|
+
bytes: Uint8Array
|
|
68
|
+
): Promise<DecodedImage | null> {
|
|
69
|
+
if (PNG_SIGNATURE.some((value, index) => bytes[index] !== value)) {
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
let offset = 8;
|
|
73
|
+
let width = 0;
|
|
74
|
+
let height = 0;
|
|
75
|
+
let colorType = -1;
|
|
76
|
+
let palette: Uint8Array | null = null;
|
|
77
|
+
let transparency: Uint8Array | null = null;
|
|
78
|
+
const chunks: Uint8Array[] = [];
|
|
79
|
+
|
|
80
|
+
while (offset + 8 <= bytes.length) {
|
|
81
|
+
const chunkLength = readUint32(bytes, offset);
|
|
82
|
+
const type = String.fromCharCode(...bytes.subarray(offset + 4, offset + 8));
|
|
83
|
+
const data = bytes.subarray(offset + 8, offset + 8 + chunkLength);
|
|
84
|
+
offset += 12 + chunkLength;
|
|
85
|
+
if (type === "IHDR") {
|
|
86
|
+
width = readUint32(data, 0);
|
|
87
|
+
height = readUint32(data, 4);
|
|
88
|
+
const bitDepth = data[8];
|
|
89
|
+
colorType = data[9] as number;
|
|
90
|
+
const interlace = data[12];
|
|
91
|
+
if (bitDepth !== 8 || interlace !== 0 || !(colorType in CHANNELS)) {
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
} else if (type === "PLTE") {
|
|
95
|
+
palette = data;
|
|
96
|
+
} else if (type === "tRNS") {
|
|
97
|
+
transparency = data;
|
|
98
|
+
} else if (type === "IDAT") {
|
|
99
|
+
chunks.push(data);
|
|
100
|
+
} else if (type === "IEND") {
|
|
101
|
+
break;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
if (!(width && height) || width * height > 4096 * 4096) {
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const compressed = new Uint8Array(
|
|
109
|
+
chunks.reduce((sum, chunk) => sum + chunk.length, 0)
|
|
110
|
+
);
|
|
111
|
+
let position = 0;
|
|
112
|
+
for (const chunk of chunks) {
|
|
113
|
+
compressed.set(chunk, position);
|
|
114
|
+
position += chunk.length;
|
|
115
|
+
}
|
|
116
|
+
const raw = await inflate(compressed);
|
|
117
|
+
const channels = CHANNELS[colorType] as number;
|
|
118
|
+
const stride = width * channels;
|
|
119
|
+
if (!raw || raw.length < height * (stride + 1)) {
|
|
120
|
+
return null;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// Undo the per-row filters in place.
|
|
124
|
+
const pixels = new Uint8Array(height * stride);
|
|
125
|
+
for (let y = 0; y < height; y++) {
|
|
126
|
+
const filter = raw[y * (stride + 1)] as number;
|
|
127
|
+
const row = raw.subarray(y * (stride + 1) + 1, (y + 1) * (stride + 1));
|
|
128
|
+
const out = y * stride;
|
|
129
|
+
for (let x = 0; x < stride; x++) {
|
|
130
|
+
const left = x >= channels ? (pixels[out + x - channels] as number) : 0;
|
|
131
|
+
const up = y > 0 ? (pixels[out - stride + x] as number) : 0;
|
|
132
|
+
const upLeft =
|
|
133
|
+
y > 0 && x >= channels
|
|
134
|
+
? (pixels[out - stride + x - channels] as number)
|
|
135
|
+
: 0;
|
|
136
|
+
const value = row[x] as number;
|
|
137
|
+
let predicted = 0;
|
|
138
|
+
if (filter === 1) {
|
|
139
|
+
predicted = left;
|
|
140
|
+
} else if (filter === 2) {
|
|
141
|
+
predicted = up;
|
|
142
|
+
} else if (filter === 3) {
|
|
143
|
+
predicted = (left + up) >> 1;
|
|
144
|
+
} else if (filter === 4) {
|
|
145
|
+
predicted = paeth(left, up, upLeft);
|
|
146
|
+
}
|
|
147
|
+
pixels[out + x] = (value + predicted) & 0xff;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const rgba = new Uint8Array(width * height * 4);
|
|
152
|
+
for (let i = 0; i < width * height; i++) {
|
|
153
|
+
const o = i * 4;
|
|
154
|
+
const p = i * channels;
|
|
155
|
+
if (colorType === 3) {
|
|
156
|
+
const index = pixels[p] as number;
|
|
157
|
+
rgba[o] = palette?.[index * 3] ?? 0;
|
|
158
|
+
rgba[o + 1] = palette?.[index * 3 + 1] ?? 0;
|
|
159
|
+
rgba[o + 2] = palette?.[index * 3 + 2] ?? 0;
|
|
160
|
+
rgba[o + 3] = transparency?.[index] ?? 255;
|
|
161
|
+
} else if (colorType === 0 || colorType === 4) {
|
|
162
|
+
const gray = pixels[p] as number;
|
|
163
|
+
rgba[o] = gray;
|
|
164
|
+
rgba[o + 1] = gray;
|
|
165
|
+
rgba[o + 2] = gray;
|
|
166
|
+
rgba[o + 3] = colorType === 4 ? (pixels[p + 1] as number) : 255;
|
|
167
|
+
} else {
|
|
168
|
+
rgba[o] = pixels[p] as number;
|
|
169
|
+
rgba[o + 1] = pixels[p + 1] as number;
|
|
170
|
+
rgba[o + 2] = pixels[p + 2] as number;
|
|
171
|
+
rgba[o + 3] = colorType === 6 ? (pixels[p + 3] as number) : 255;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
return { width, height, data: rgba };
|
|
175
|
+
}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
/** biome-ignore-all lint/style/noMagicNumbers: geometry */
|
|
2
|
+
// Flattening and box clipping for outlines, used where a shape has to be cut
|
|
3
|
+
// into pieces that every output draws the same way: the solid color steps of
|
|
4
|
+
// a gradient frame fill, and imported artwork that runs off its artboard.
|
|
5
|
+
|
|
6
|
+
import { fmt } from "./fmt";
|
|
7
|
+
import type { Segment } from "./svg-geometry";
|
|
8
|
+
|
|
9
|
+
export type Point = [number, number];
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Closed polygons of `segments` (one per subpath), with curves flattened to
|
|
13
|
+
* at most `tolerance` units of chord error (roughly).
|
|
14
|
+
*/
|
|
15
|
+
export function flattenSegments(
|
|
16
|
+
segments: Segment[],
|
|
17
|
+
tolerance = 0.25
|
|
18
|
+
): Point[][] {
|
|
19
|
+
const polygons: Point[][] = [];
|
|
20
|
+
let current: Point[] = [];
|
|
21
|
+
let start: Point = [0, 0];
|
|
22
|
+
let last: Point = [0, 0];
|
|
23
|
+
const flush = () => {
|
|
24
|
+
if (current.length > 2) {
|
|
25
|
+
polygons.push(current);
|
|
26
|
+
}
|
|
27
|
+
current = [];
|
|
28
|
+
};
|
|
29
|
+
for (const segment of segments) {
|
|
30
|
+
if (segment.op === "M") {
|
|
31
|
+
flush();
|
|
32
|
+
start = [segment.x, segment.y];
|
|
33
|
+
last = start;
|
|
34
|
+
current.push(start);
|
|
35
|
+
} else if (segment.op === "L") {
|
|
36
|
+
last = [segment.x, segment.y];
|
|
37
|
+
current.push(last);
|
|
38
|
+
} else if (segment.op === "C") {
|
|
39
|
+
const p0 = last;
|
|
40
|
+
const p1: Point = [segment.x1, segment.y1];
|
|
41
|
+
const p2: Point = [segment.x2, segment.y2];
|
|
42
|
+
const p3: Point = [segment.x, segment.y];
|
|
43
|
+
const hull =
|
|
44
|
+
Math.hypot(p1[0] - p0[0], p1[1] - p0[1]) +
|
|
45
|
+
Math.hypot(p2[0] - p1[0], p2[1] - p1[1]) +
|
|
46
|
+
Math.hypot(p3[0] - p2[0], p3[1] - p2[1]);
|
|
47
|
+
// A cubic's deviation from its chords falls with the square of the
|
|
48
|
+
// step count.
|
|
49
|
+
const steps = Math.max(
|
|
50
|
+
2,
|
|
51
|
+
Math.min(64, Math.ceil(Math.sqrt(hull / (4 * tolerance))))
|
|
52
|
+
);
|
|
53
|
+
for (let i = 1; i <= steps; i++) {
|
|
54
|
+
const t = i / steps;
|
|
55
|
+
const u = 1 - t;
|
|
56
|
+
current.push([
|
|
57
|
+
u * u * u * p0[0] +
|
|
58
|
+
3 * u * u * t * p1[0] +
|
|
59
|
+
3 * u * t * t * p2[0] +
|
|
60
|
+
t * t * t * p3[0],
|
|
61
|
+
u * u * u * p0[1] +
|
|
62
|
+
3 * u * u * t * p1[1] +
|
|
63
|
+
3 * u * t * t * p2[1] +
|
|
64
|
+
t * t * t * p3[1],
|
|
65
|
+
]);
|
|
66
|
+
}
|
|
67
|
+
last = p3;
|
|
68
|
+
} else {
|
|
69
|
+
flush();
|
|
70
|
+
last = start;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
flush();
|
|
74
|
+
return polygons;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export interface ClipBox {
|
|
78
|
+
x0: number;
|
|
79
|
+
y0: number;
|
|
80
|
+
x1: number;
|
|
81
|
+
y1: number;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
type Edge = (point: Point) => number;
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Sutherland–Hodgman clip of one closed polygon to an axis-aligned box. The
|
|
88
|
+
* result keeps the polygon's winding inside the box, so a shape made of
|
|
89
|
+
* several subpaths fills the same way (nonzero or even-odd) once each
|
|
90
|
+
* subpath is clipped.
|
|
91
|
+
*/
|
|
92
|
+
export function clipPolygon(points: Point[], box: ClipBox): Point[] {
|
|
93
|
+
// Signed distance inside each edge (>= 0 is kept)
|
|
94
|
+
const edges: Edge[] = [
|
|
95
|
+
(p) => p[0] - box.x0,
|
|
96
|
+
(p) => box.x1 - p[0],
|
|
97
|
+
(p) => p[1] - box.y0,
|
|
98
|
+
(p) => box.y1 - p[1],
|
|
99
|
+
];
|
|
100
|
+
let output = points;
|
|
101
|
+
for (const inside of edges) {
|
|
102
|
+
if (output.length === 0) {
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
const input = output;
|
|
106
|
+
output = [];
|
|
107
|
+
for (let i = 0; i < input.length; i++) {
|
|
108
|
+
const current = input[i] as Point;
|
|
109
|
+
const previous = input[(i + input.length - 1) % input.length] as Point;
|
|
110
|
+
const dc = inside(current);
|
|
111
|
+
const dp = inside(previous);
|
|
112
|
+
if (dc >= 0) {
|
|
113
|
+
if (dp < 0) {
|
|
114
|
+
output.push(intersect(previous, current, dp, dc));
|
|
115
|
+
}
|
|
116
|
+
output.push(current);
|
|
117
|
+
} else if (dp >= 0) {
|
|
118
|
+
output.push(intersect(previous, current, dp, dc));
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return output.length > 2 ? output : [];
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function intersect(a: Point, b: Point, da: number, db: number): Point {
|
|
126
|
+
const t = da / (da - db);
|
|
127
|
+
return [a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t];
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** Closed polygons as path data (absolute, 3 decimals). */
|
|
131
|
+
export function polygonsToPath(polygons: Point[][]): string {
|
|
132
|
+
return polygons
|
|
133
|
+
.filter((polygon) => polygon.length > 2)
|
|
134
|
+
.map(
|
|
135
|
+
(polygon) =>
|
|
136
|
+
`M${polygon.map(([x, y]) => `${fmt(x)} ${fmt(y)}`).join("L")}Z`
|
|
137
|
+
)
|
|
138
|
+
.join("");
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** Bounding box of segments (control points included), or null if empty. */
|
|
142
|
+
export function segmentsBounds(segments: Segment[]): ClipBox | null {
|
|
143
|
+
let x0 = Number.POSITIVE_INFINITY;
|
|
144
|
+
let y0 = Number.POSITIVE_INFINITY;
|
|
145
|
+
let x1 = Number.NEGATIVE_INFINITY;
|
|
146
|
+
let y1 = Number.NEGATIVE_INFINITY;
|
|
147
|
+
const add = (x: number, y: number) => {
|
|
148
|
+
x0 = Math.min(x0, x);
|
|
149
|
+
y0 = Math.min(y0, y);
|
|
150
|
+
x1 = Math.max(x1, x);
|
|
151
|
+
y1 = Math.max(y1, y);
|
|
152
|
+
};
|
|
153
|
+
for (const segment of segments) {
|
|
154
|
+
if (segment.op === "C") {
|
|
155
|
+
add(segment.x1, segment.y1);
|
|
156
|
+
add(segment.x2, segment.y2);
|
|
157
|
+
add(segment.x, segment.y);
|
|
158
|
+
} else if (segment.op !== "Z") {
|
|
159
|
+
add(segment.x, segment.y);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return Number.isFinite(x0) ? { x0, y0, x1, y1 } : null;
|
|
163
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { QRProps } from "../../types";
|
|
2
|
+
import { isGradient } from "../../types/color";
|
|
3
|
+
import { DEFAULT_BGCOLOR, DEFAULT_SIZE } from "../constants";
|
|
4
|
+
import { withQRSize } from "../download";
|
|
5
|
+
import { getFrameLayout, isFrameActive } from "../frame";
|
|
6
|
+
import { getQRAsSVGDataUri } from "../index";
|
|
7
|
+
import { getSolidColor } from "./color";
|
|
8
|
+
import { flattenOver } from "./paint";
|
|
9
|
+
import { parseColor } from "./parse-color";
|
|
10
|
+
import { parseSvgMarkup } from "./svg-geometry";
|
|
11
|
+
import { readStyle, type SvgPainter } from "./svg-painter";
|
|
12
|
+
|
|
13
|
+
// Shared by the PDF (print.ts) and EPS (eps.ts) exporters; not public API.
|
|
14
|
+
|
|
15
|
+
/** Logical render size; print output is scaled from this. */
|
|
16
|
+
export const PRINT_RENDER_SIZE = 1000;
|
|
17
|
+
|
|
18
|
+
/** SVG markup of the artwork at the logical print render size (1000 units). */
|
|
19
|
+
export async function renderPrintSVG(props: QRProps): Promise<string> {
|
|
20
|
+
const dataUri = await getQRAsSVGDataUri(
|
|
21
|
+
withQRSize(
|
|
22
|
+
{ ...props, size: props.size ?? DEFAULT_SIZE },
|
|
23
|
+
PRINT_RENDER_SIZE
|
|
24
|
+
)
|
|
25
|
+
);
|
|
26
|
+
return decodeURIComponent(dataUri.replace("data:image/svg+xml,", ""));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Color the bleed area is filled with, if any. */
|
|
30
|
+
export function getPrintBleedColor(props: QRProps): string | null {
|
|
31
|
+
if (isFrameActive(props.frame)) {
|
|
32
|
+
const frame = getFrameLayout(PRINT_RENDER_SIZE, props.frame);
|
|
33
|
+
return frame.squareEdges ? frame.color : null;
|
|
34
|
+
}
|
|
35
|
+
return getSolidColor(props.bgColor, DEFAULT_BGCOLOR);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* SVG paint for the bleed area: `getPrintBleedColor`, except that a gradient
|
|
40
|
+
* background (no frame or template) returns the background's own
|
|
41
|
+
* `url(#…)` fill from `markup`, so the bleed continues the gradient.
|
|
42
|
+
*/
|
|
43
|
+
export function getPrintBleedFill(
|
|
44
|
+
props: QRProps,
|
|
45
|
+
markup: string
|
|
46
|
+
): string | null {
|
|
47
|
+
const color = getPrintBleedColor(props);
|
|
48
|
+
if (
|
|
49
|
+
!(color && isGradient(props.bgColor)) ||
|
|
50
|
+
isFrameActive(props.frame) ||
|
|
51
|
+
props.templateId ||
|
|
52
|
+
props.customTemplate
|
|
53
|
+
) {
|
|
54
|
+
return color;
|
|
55
|
+
}
|
|
56
|
+
const background = parseSvgMarkup(markup)?.children.find(
|
|
57
|
+
(child) => child.name === "rect"
|
|
58
|
+
);
|
|
59
|
+
const fill = background && readStyle(background, "fill");
|
|
60
|
+
return fill?.startsWith("url(") ? fill : color;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* What see-through paint is flattened over where a format can't blend: the
|
|
65
|
+
* background (its first color for a gradient) on white paper.
|
|
66
|
+
*/
|
|
67
|
+
export function setBackdrop(painter: SvgPainter, props: QRProps) {
|
|
68
|
+
const white = { r: 255, g: 255, b: 255, a: 1 };
|
|
69
|
+
const background = parseColor(getSolidColor(props.bgColor, DEFAULT_BGCOLOR));
|
|
70
|
+
painter.paper = white;
|
|
71
|
+
painter.background =
|
|
72
|
+
background && !isGradient(props.bgColor) ? background : null;
|
|
73
|
+
painter.backdrop = background ? flattenOver(background, white) : white;
|
|
74
|
+
}
|