qrdx 0.0.45 → 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 +63 -20
- 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 -4231
package/src/shapes.ts
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
/** biome-ignore-all lint/style/noMagicNumbers: shape geometry */
|
|
2
|
+
/** biome-ignore-all lint/suspicious/noBitwiseOperators: integer hashing */
|
|
3
|
+
// Geometry primitives for the module, eye, eye-dot and frame shapes. Every
|
|
4
|
+
// shape is plain path data (M/L/A/C/Z, absolute coordinates, no transforms),
|
|
5
|
+
// so the SVG, canvas (Path2D), PDF and EPS outputs draw exactly the same
|
|
6
|
+
// outline. Closed shapes run clockwise on screen (y down), so many of them in
|
|
7
|
+
// one path union cleanly under the nonzero fill rule.
|
|
8
|
+
|
|
9
|
+
import { fmt } from "./utils/fmt";
|
|
10
|
+
|
|
11
|
+
export type Point = [number, number];
|
|
12
|
+
|
|
13
|
+
/** Closed polygon through `points`. */
|
|
14
|
+
export function polygonPath(points: Point[]): string {
|
|
15
|
+
if (points.length === 0) {
|
|
16
|
+
return "";
|
|
17
|
+
}
|
|
18
|
+
const [first, ...rest] = points as [Point, ...Point[]];
|
|
19
|
+
return `M${fmt(first[0])} ${fmt(first[1])}${rest
|
|
20
|
+
.map(([x, y]) => `L${fmt(x)} ${fmt(y)}`)
|
|
21
|
+
.join("")}Z`;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Clockwise circle as two arcs. */
|
|
25
|
+
export function circlePath(cx: number, cy: number, r: number): string {
|
|
26
|
+
return `M${fmt(cx - r)} ${fmt(cy)}A${fmt(r)} ${fmt(r)} 0 1 1 ${fmt(cx + r)} ${fmt(cy)}A${fmt(r)} ${fmt(r)} 0 1 1 ${fmt(cx - r)} ${fmt(cy)}Z`;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Rectangle with its own radius per corner, clockwise from the top-left:
|
|
31
|
+
* `[topLeft, topRight, bottomRight, bottomLeft]`.
|
|
32
|
+
*/
|
|
33
|
+
export function roundedRectPath(
|
|
34
|
+
x: number,
|
|
35
|
+
y: number,
|
|
36
|
+
width: number,
|
|
37
|
+
height: number,
|
|
38
|
+
radii: [number, number, number, number] | number = 0
|
|
39
|
+
): string {
|
|
40
|
+
const limit = Math.min(width, height) / 2;
|
|
41
|
+
const [tl, tr, br, bl] = (
|
|
42
|
+
typeof radii === "number" ? [radii, radii, radii, radii] : radii
|
|
43
|
+
).map((r) => Math.max(0, Math.min(r, limit))) as [
|
|
44
|
+
number,
|
|
45
|
+
number,
|
|
46
|
+
number,
|
|
47
|
+
number,
|
|
48
|
+
];
|
|
49
|
+
const arc = (r: number, toX: number, toY: number) =>
|
|
50
|
+
r > 0 ? `A${fmt(r)} ${fmt(r)} 0 0 1 ${fmt(toX)} ${fmt(toY)}` : "";
|
|
51
|
+
return (
|
|
52
|
+
`M${fmt(x + tl)} ${fmt(y)}` +
|
|
53
|
+
`H${fmt(x + width - tr)}${arc(tr, x + width, y + tr)}` +
|
|
54
|
+
`V${fmt(y + height - br)}${arc(br, x + width - br, y + height)}` +
|
|
55
|
+
`H${fmt(x + bl)}${arc(bl, x, y + height - bl)}` +
|
|
56
|
+
`V${fmt(y + tl)}${arc(tl, x + tl, y)}Z`
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Points of a regular polygon (clockwise), first vertex at `rotation` degrees. */
|
|
61
|
+
export function regularPolygon(
|
|
62
|
+
cx: number,
|
|
63
|
+
cy: number,
|
|
64
|
+
radius: number,
|
|
65
|
+
sides: number,
|
|
66
|
+
rotation = -90
|
|
67
|
+
): Point[] {
|
|
68
|
+
return Array.from({ length: sides }, (_, i) => {
|
|
69
|
+
const angle = ((rotation + (360 / sides) * i) * Math.PI) / 180;
|
|
70
|
+
return [cx + radius * Math.cos(angle), cy + radius * Math.sin(angle)];
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Points of a star with `points` tips, the first tip straight up. */
|
|
75
|
+
export function starPoints(
|
|
76
|
+
cx: number,
|
|
77
|
+
cy: number,
|
|
78
|
+
outer: number,
|
|
79
|
+
inner: number,
|
|
80
|
+
points = 5
|
|
81
|
+
): Point[] {
|
|
82
|
+
return Array.from({ length: points * 2 }, (_, i) => {
|
|
83
|
+
const radius = i % 2 === 0 ? outer : inner;
|
|
84
|
+
const angle = -Math.PI / 2 + (Math.PI / points) * i;
|
|
85
|
+
return [cx + radius * Math.cos(angle), cy + radius * Math.sin(angle)];
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Points of a superellipse |x/a|^e + |y/b|^e = 1 (a squircle when e = 4),
|
|
91
|
+
* clockwise from the right.
|
|
92
|
+
*/
|
|
93
|
+
export function superellipsePoints(
|
|
94
|
+
cx: number,
|
|
95
|
+
cy: number,
|
|
96
|
+
a: number,
|
|
97
|
+
b: number,
|
|
98
|
+
exponent = 4,
|
|
99
|
+
steps = 64
|
|
100
|
+
): Point[] {
|
|
101
|
+
return Array.from({ length: steps }, (_, i) => {
|
|
102
|
+
const t = (2 * Math.PI * i) / steps;
|
|
103
|
+
const cos = Math.cos(t);
|
|
104
|
+
const sin = Math.sin(t);
|
|
105
|
+
return [
|
|
106
|
+
cx + a * Math.sign(cos) * Math.abs(cos) ** (2 / exponent),
|
|
107
|
+
cy + b * Math.sign(sin) * Math.abs(sin) ** (2 / exponent),
|
|
108
|
+
];
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Heart centered on (cx, cy), `size` wide; its height is about 0.92 × size.
|
|
114
|
+
* Drawn clockwise from the top notch.
|
|
115
|
+
*/
|
|
116
|
+
export function heartPath(cx: number, cy: number, size: number): string {
|
|
117
|
+
const s = size / 2;
|
|
118
|
+
const p = (x: number, y: number) => `${fmt(cx + x * s)} ${fmt(cy + y * s)}`;
|
|
119
|
+
return (
|
|
120
|
+
`M${p(0, -0.52)}` +
|
|
121
|
+
`C${p(0.18, -0.92)} ${p(0.98, -0.94)} ${p(1, -0.34)}` +
|
|
122
|
+
`C${p(1, 0.12)} ${p(0.5, 0.48)} ${p(0, 0.92)}` +
|
|
123
|
+
`C${p(-0.5, 0.48)} ${p(-1, 0.12)} ${p(-1, -0.34)}` +
|
|
124
|
+
`C${p(-0.98, -0.94)} ${p(-0.18, -0.92)} ${p(0, -0.52)}Z`
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** Rotates `points` by `degrees` around (cx, cy). */
|
|
129
|
+
export function rotatePoints(
|
|
130
|
+
points: Point[],
|
|
131
|
+
cx: number,
|
|
132
|
+
cy: number,
|
|
133
|
+
degrees: number
|
|
134
|
+
): Point[] {
|
|
135
|
+
if (degrees === 0) {
|
|
136
|
+
return points;
|
|
137
|
+
}
|
|
138
|
+
const angle = (degrees * Math.PI) / 180;
|
|
139
|
+
const cos = Math.cos(angle);
|
|
140
|
+
const sin = Math.sin(angle);
|
|
141
|
+
return points.map(([x, y]) => [
|
|
142
|
+
cx + (x - cx) * cos - (y - cy) * sin,
|
|
143
|
+
cy + (x - cx) * sin + (y - cy) * cos,
|
|
144
|
+
]);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Deterministic pseudo-random number in [0, 1) for a module position, so
|
|
149
|
+
* "random" styles render the same markup on the server and the client.
|
|
150
|
+
*/
|
|
151
|
+
export function hash2(x: number, y: number, salt = 0): number {
|
|
152
|
+
let h =
|
|
153
|
+
Math.imul(x + 0x9e_37_79_b9, 0x85_eb_ca_6b) ^
|
|
154
|
+
Math.imul(y + salt, 0xc2_b2_ae_35);
|
|
155
|
+
h = Math.imul(h ^ (h >>> 15), 0x2c_1b_3c_6d);
|
|
156
|
+
h = Math.imul(h ^ (h >>> 12), 0x29_7a_2d_39);
|
|
157
|
+
h ^= h >>> 15;
|
|
158
|
+
return (h >>> 0) / 4_294_967_296;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** Small deterministic string hash (FNV-1a), base 36. */
|
|
162
|
+
export function hashString(value: string): string {
|
|
163
|
+
let h = 0x81_1c_9d_c5;
|
|
164
|
+
for (let i = 0; i < value.length; i++) {
|
|
165
|
+
h ^= value.charCodeAt(i);
|
|
166
|
+
h = Math.imul(h, 0x01_00_01_93);
|
|
167
|
+
}
|
|
168
|
+
return (h >>> 0).toString(36);
|
|
169
|
+
}
|
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
/** biome-ignore-all lint/style/noMagicNumbers: sampling constants */
|
|
2
|
+
// Silhouettes for `shape` (QRSilhouette): a picture's shape sampled into a
|
|
3
|
+
// small grid of cells, never the picture itself. A picture with see-through
|
|
4
|
+
// parts uses its alpha; an opaque one (a JPEG, a logo on white) uses its
|
|
5
|
+
// darkness, split at the threshold that best separates its tones, and a
|
|
6
|
+
// light shape on a dark ground is turned around. Specks and pinholes are
|
|
7
|
+
// cleaned up, the shape is cropped to its bounds and sampled so its longer
|
|
8
|
+
// side is QR_SILHOUETTE_MAX_CELLS cells.
|
|
9
|
+
import {
|
|
10
|
+
QR_SILHOUETTE_MAX_CELLS,
|
|
11
|
+
QR_SILHOUETTE_MIN_CELLS,
|
|
12
|
+
type QRSilhouette,
|
|
13
|
+
} from "../types/qr";
|
|
14
|
+
import type { QRArtPixels } from "./art";
|
|
15
|
+
|
|
16
|
+
/** Fewest pixels across the shape itself (after cropping) worth tracing. */
|
|
17
|
+
const MIN_SHAPE_PIXELS = 16;
|
|
18
|
+
/** Islands and holes smaller than this share of the shape are cleaned up. */
|
|
19
|
+
const SPECK_SHARE = 0.01;
|
|
20
|
+
|
|
21
|
+
export interface QRSilhouetteOptions {
|
|
22
|
+
/**
|
|
23
|
+
* What makes a pixel part of the shape: "alpha" (opaque pixels), "dark"
|
|
24
|
+
* (dark pixels, on white where see-through) or "auto" (default: alpha
|
|
25
|
+
* when the picture has see-through parts, else dark).
|
|
26
|
+
*/
|
|
27
|
+
mode?: "auto" | "alpha" | "dark";
|
|
28
|
+
/** Cells along the longer side (8 to QR_SILHOUETTE_MAX_CELLS, default the max) */
|
|
29
|
+
cells?: number;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** The darkness threshold that best splits the tones (Otsu's method). */
|
|
33
|
+
function otsu(values: Float32Array): number {
|
|
34
|
+
const bins = new Float64Array(256);
|
|
35
|
+
for (const value of values) {
|
|
36
|
+
bins[Math.min(255, Math.max(0, Math.round(value * 255)))]++;
|
|
37
|
+
}
|
|
38
|
+
const total = values.length;
|
|
39
|
+
let sum = 0;
|
|
40
|
+
for (let i = 0; i < 256; i++) sum += i * (bins[i] as number);
|
|
41
|
+
let sumB = 0;
|
|
42
|
+
let weightB = 0;
|
|
43
|
+
let best = 0;
|
|
44
|
+
let threshold = 128;
|
|
45
|
+
for (let i = 0; i < 256; i++) {
|
|
46
|
+
weightB += bins[i] as number;
|
|
47
|
+
if (weightB === 0) continue;
|
|
48
|
+
const weightF = total - weightB;
|
|
49
|
+
if (weightF === 0) break;
|
|
50
|
+
sumB += i * (bins[i] as number);
|
|
51
|
+
const meanB = sumB / weightB;
|
|
52
|
+
const meanF = (sum - sumB) / weightF;
|
|
53
|
+
const between = weightB * weightF * (meanB - meanF) ** 2;
|
|
54
|
+
if (between > best) {
|
|
55
|
+
best = between;
|
|
56
|
+
threshold = i;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return (threshold + 0.5) / 255;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Which pixels belong to the shape. */
|
|
63
|
+
function inkMask(
|
|
64
|
+
pixels: QRArtPixels,
|
|
65
|
+
mode: NonNullable<QRSilhouetteOptions["mode"]>
|
|
66
|
+
): Uint8Array {
|
|
67
|
+
const { width, height, data } = pixels;
|
|
68
|
+
const count = width * height;
|
|
69
|
+
const mask = new Uint8Array(count);
|
|
70
|
+
let clear = 0;
|
|
71
|
+
for (let i = 0; i < count; i++) {
|
|
72
|
+
if ((data[i * 4 + 3] as number) < 128) clear++;
|
|
73
|
+
}
|
|
74
|
+
const useAlpha =
|
|
75
|
+
mode === "alpha" ||
|
|
76
|
+
(mode === "auto" && clear > count * 0.02 && clear < count * 0.98);
|
|
77
|
+
if (useAlpha) {
|
|
78
|
+
for (let i = 0; i < count; i++) {
|
|
79
|
+
mask[i] = (data[i * 4 + 3] as number) >= 128 ? 1 : 0;
|
|
80
|
+
}
|
|
81
|
+
return mask;
|
|
82
|
+
}
|
|
83
|
+
// Darkness on white: see-through pixels count as paper
|
|
84
|
+
const darkness = new Float32Array(count);
|
|
85
|
+
for (let i = 0; i < count; i++) {
|
|
86
|
+
const a = (data[i * 4 + 3] as number) / 255;
|
|
87
|
+
const luma =
|
|
88
|
+
0.2126 * (data[i * 4] as number) +
|
|
89
|
+
0.7152 * (data[i * 4 + 1] as number) +
|
|
90
|
+
0.0722 * (data[i * 4 + 2] as number);
|
|
91
|
+
darkness[i] = 1 - (luma * a + 255 * (1 - a)) / 255;
|
|
92
|
+
}
|
|
93
|
+
const threshold = otsu(darkness);
|
|
94
|
+
for (let i = 0; i < count; i++) {
|
|
95
|
+
mask[i] = (darkness[i] as number) > threshold ? 1 : 0;
|
|
96
|
+
}
|
|
97
|
+
// A light shape on a dark ground: the ground covers most of the border
|
|
98
|
+
let border = 0;
|
|
99
|
+
let inked = 0;
|
|
100
|
+
for (let x = 0; x < width; x++) {
|
|
101
|
+
for (const y of [0, height - 1]) {
|
|
102
|
+
border++;
|
|
103
|
+
inked += mask[y * width + x] as number;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
for (let y = 1; y < height - 1; y++) {
|
|
107
|
+
for (const x of [0, width - 1]) {
|
|
108
|
+
border++;
|
|
109
|
+
inked += mask[y * width + x] as number;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
if (inked > border / 2) {
|
|
113
|
+
for (let i = 0; i < count; i++) mask[i] = mask[i] ? 0 : 1;
|
|
114
|
+
}
|
|
115
|
+
return mask;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** Removes islands and fills holes smaller than `limit` cells (4-connected). */
|
|
119
|
+
function cleanSpecks(
|
|
120
|
+
cells: Uint8Array,
|
|
121
|
+
width: number,
|
|
122
|
+
height: number,
|
|
123
|
+
limit: number
|
|
124
|
+
) {
|
|
125
|
+
for (const value of [1, 0]) {
|
|
126
|
+
const seen = new Uint8Array(cells.length);
|
|
127
|
+
for (let start = 0; start < cells.length; start++) {
|
|
128
|
+
if (seen[start] || cells[start] !== value) continue;
|
|
129
|
+
const component: number[] = [start];
|
|
130
|
+
seen[start] = 1;
|
|
131
|
+
let touchesEdge = false;
|
|
132
|
+
// Grows while it is walked: for…of sees the pushed cells too
|
|
133
|
+
for (const i of component) {
|
|
134
|
+
const x = i % width;
|
|
135
|
+
const y = (i - x) / width;
|
|
136
|
+
if (x === 0 || y === 0 || x === width - 1 || y === height - 1) {
|
|
137
|
+
touchesEdge = true;
|
|
138
|
+
}
|
|
139
|
+
for (const [nx, ny] of [
|
|
140
|
+
[x - 1, y],
|
|
141
|
+
[x + 1, y],
|
|
142
|
+
[x, y - 1],
|
|
143
|
+
[x, y + 1],
|
|
144
|
+
] as const) {
|
|
145
|
+
if (nx < 0 || ny < 0 || nx >= width || ny >= height) continue;
|
|
146
|
+
const j = ny * width + nx;
|
|
147
|
+
if (!seen[j] && cells[j] === value) {
|
|
148
|
+
seen[j] = 1;
|
|
149
|
+
component.push(j);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
// Holes are light areas that don't reach the edge
|
|
154
|
+
const speck = value === 1 || !touchesEdge;
|
|
155
|
+
if (speck && component.length < limit) {
|
|
156
|
+
for (const i of component) cells[i] = value ? 0 : 1;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Samples raw RGBA pixels (a canvas, `sharp().raw()`, …) into a silhouette
|
|
164
|
+
* for `shape`. Works everywhere, no DOM needed. Throws when no shape is
|
|
165
|
+
* found or it is too small to trace; check the result with
|
|
166
|
+
* `getQRSilhouetteFit` before using it.
|
|
167
|
+
*/
|
|
168
|
+
export function createQRSilhouette(
|
|
169
|
+
pixels: QRArtPixels,
|
|
170
|
+
options: QRSilhouetteOptions = {}
|
|
171
|
+
): QRSilhouette {
|
|
172
|
+
const { width, height, data } = pixels;
|
|
173
|
+
if (!(width > 0 && height > 0) || data.length < width * height * 4) {
|
|
174
|
+
throw new Error("Silhouette pixels must be width × height × 4 RGBA bytes");
|
|
175
|
+
}
|
|
176
|
+
const mask = inkMask(pixels, options.mode ?? "auto");
|
|
177
|
+
// Specks and pinholes go before cropping, so a speck can't widen the crop
|
|
178
|
+
const inked = mask.reduce((sum, cell) => sum + cell, 0);
|
|
179
|
+
cleanSpecks(mask, width, height, Math.max(16, inked * SPECK_SHARE));
|
|
180
|
+
let x0 = width;
|
|
181
|
+
let y0 = height;
|
|
182
|
+
let x1 = -1;
|
|
183
|
+
let y1 = -1;
|
|
184
|
+
for (let y = 0; y < height; y++) {
|
|
185
|
+
for (let x = 0; x < width; x++) {
|
|
186
|
+
if (mask[y * width + x]) {
|
|
187
|
+
x0 = Math.min(x0, x);
|
|
188
|
+
y0 = Math.min(y0, y);
|
|
189
|
+
x1 = Math.max(x1, x);
|
|
190
|
+
y1 = Math.max(y1, y);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
if (x1 < 0) {
|
|
195
|
+
throw new Error(
|
|
196
|
+
"No shape found: use a picture with a dark or opaque shape on a light or clear background"
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
const shapeWidth = x1 - x0 + 1;
|
|
200
|
+
const shapeHeight = y1 - y0 + 1;
|
|
201
|
+
if (Math.max(shapeWidth, shapeHeight) < MIN_SHAPE_PIXELS) {
|
|
202
|
+
throw new Error("The shape is too small to trace; use a bigger picture");
|
|
203
|
+
}
|
|
204
|
+
const long = Math.min(
|
|
205
|
+
Math.max(
|
|
206
|
+
Math.round(options.cells ?? QR_SILHOUETTE_MAX_CELLS),
|
|
207
|
+
QR_SILHOUETTE_MIN_CELLS
|
|
208
|
+
),
|
|
209
|
+
QR_SILHOUETTE_MAX_CELLS
|
|
210
|
+
);
|
|
211
|
+
const scale = long / Math.max(shapeWidth, shapeHeight);
|
|
212
|
+
// The grid keeps the shape's proportions; a very thin shape is padded to
|
|
213
|
+
// the smallest grid (and then fails the fit check as thin)
|
|
214
|
+
const gridWidth = Math.max(
|
|
215
|
+
QR_SILHOUETTE_MIN_CELLS,
|
|
216
|
+
Math.round(shapeWidth * scale)
|
|
217
|
+
);
|
|
218
|
+
const gridHeight = Math.max(
|
|
219
|
+
QR_SILHOUETTE_MIN_CELLS,
|
|
220
|
+
Math.round(shapeHeight * scale)
|
|
221
|
+
);
|
|
222
|
+
const innerWidth = Math.max(1, Math.round(shapeWidth * scale));
|
|
223
|
+
const innerHeight = Math.max(1, Math.round(shapeHeight * scale));
|
|
224
|
+
const padX = Math.floor((gridWidth - innerWidth) / 2);
|
|
225
|
+
const padY = Math.floor((gridHeight - innerHeight) / 2);
|
|
226
|
+
const cells = new Uint8Array(gridWidth * gridHeight);
|
|
227
|
+
for (let gy = 0; gy < innerHeight; gy++) {
|
|
228
|
+
const sy0 = y0 + Math.floor((gy * shapeHeight) / innerHeight);
|
|
229
|
+
const sy1 = Math.max(
|
|
230
|
+
sy0 + 1,
|
|
231
|
+
y0 + Math.floor(((gy + 1) * shapeHeight) / innerHeight)
|
|
232
|
+
);
|
|
233
|
+
for (let gx = 0; gx < innerWidth; gx++) {
|
|
234
|
+
const sx0 = x0 + Math.floor((gx * shapeWidth) / innerWidth);
|
|
235
|
+
const sx1 = Math.max(
|
|
236
|
+
sx0 + 1,
|
|
237
|
+
x0 + Math.floor(((gx + 1) * shapeWidth) / innerWidth)
|
|
238
|
+
);
|
|
239
|
+
let ink = 0;
|
|
240
|
+
for (let sy = sy0; sy < sy1; sy++) {
|
|
241
|
+
for (let sx = sx0; sx < sx1; sx++)
|
|
242
|
+
ink += mask[sy * width + sx] as number;
|
|
243
|
+
}
|
|
244
|
+
// A cell is inside when the shape covers at least half of it
|
|
245
|
+
cells[(gy + padY) * gridWidth + gx + padX] =
|
|
246
|
+
ink * 2 >= (sy1 - sy0) * (sx1 - sx0) ? 1 : 0;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
const area = cells.reduce((sum, cell) => sum + cell, 0);
|
|
250
|
+
cleanSpecks(cells, gridWidth, gridHeight, Math.max(4, area * SPECK_SHARE));
|
|
251
|
+
if (!cells.includes(1)) {
|
|
252
|
+
throw new Error("The shape is too small to trace; use a bigger picture");
|
|
253
|
+
}
|
|
254
|
+
return {
|
|
255
|
+
type: "silhouette",
|
|
256
|
+
width: gridWidth,
|
|
257
|
+
height: gridHeight,
|
|
258
|
+
cells: Array.from(cells, (cell) => (cell ? "1" : "0")).join(""),
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/** Longest side a picture is drawn at before sampling, in pixels. */
|
|
263
|
+
const LOAD_SIZE = 512;
|
|
264
|
+
|
|
265
|
+
async function toImage(
|
|
266
|
+
source: string | Blob | HTMLImageElement
|
|
267
|
+
): Promise<HTMLImageElement> {
|
|
268
|
+
if (typeof source !== "string" && !(source instanceof Blob)) {
|
|
269
|
+
if (!source.complete) {
|
|
270
|
+
await new Promise((resolve) => {
|
|
271
|
+
source.addEventListener("load", resolve, { once: true });
|
|
272
|
+
source.addEventListener("error", resolve, { once: true });
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
return source;
|
|
276
|
+
}
|
|
277
|
+
const url = typeof source === "string" ? source : URL.createObjectURL(source);
|
|
278
|
+
try {
|
|
279
|
+
return await new Promise((resolve, reject) => {
|
|
280
|
+
const img = new Image();
|
|
281
|
+
img.crossOrigin = "anonymous";
|
|
282
|
+
img.onload = () => resolve(img);
|
|
283
|
+
img.onerror = () => reject(new Error("Could not load the picture"));
|
|
284
|
+
img.src = url;
|
|
285
|
+
});
|
|
286
|
+
} finally {
|
|
287
|
+
if (typeof source !== "string") {
|
|
288
|
+
setTimeout(() => URL.revokeObjectURL(url), 1000);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Loads a picture (PNG, JPEG, SVG, … as a Blob, data URL, same-origin URL
|
|
295
|
+
* or <img>) in the browser, draws it on a canvas and samples it with
|
|
296
|
+
* `createQRSilhouette`. The picture itself is never kept: an SVG is drawn
|
|
297
|
+
* as an image (no scripts run) and only the grid comes back.
|
|
298
|
+
*/
|
|
299
|
+
export async function loadQRSilhouette(
|
|
300
|
+
source: string | Blob | HTMLImageElement,
|
|
301
|
+
options: QRSilhouetteOptions = {}
|
|
302
|
+
): Promise<QRSilhouette> {
|
|
303
|
+
if (typeof document === "undefined") {
|
|
304
|
+
throw new Error(
|
|
305
|
+
"loadQRSilhouette needs a browser; pass decoded pixels to createQRSilhouette instead"
|
|
306
|
+
);
|
|
307
|
+
}
|
|
308
|
+
const img = await toImage(source);
|
|
309
|
+
// An SVG without a size reports 0 (or 150): draw it at the load size
|
|
310
|
+
const naturalWidth = img.naturalWidth || img.width || LOAD_SIZE;
|
|
311
|
+
const naturalHeight = img.naturalHeight || img.height || LOAD_SIZE;
|
|
312
|
+
const fit = LOAD_SIZE / Math.max(naturalWidth, naturalHeight);
|
|
313
|
+
const width = Math.max(1, Math.round(naturalWidth * fit));
|
|
314
|
+
const height = Math.max(1, Math.round(naturalHeight * fit));
|
|
315
|
+
const canvas = document.createElement("canvas");
|
|
316
|
+
canvas.width = width;
|
|
317
|
+
canvas.height = height;
|
|
318
|
+
const ctx = canvas.getContext("2d", { willReadFrequently: true });
|
|
319
|
+
if (!ctx) {
|
|
320
|
+
throw new Error("Could not get a canvas context");
|
|
321
|
+
}
|
|
322
|
+
ctx.drawImage(img, 0, 0, width, height);
|
|
323
|
+
let pixels: ImageData;
|
|
324
|
+
try {
|
|
325
|
+
pixels = ctx.getImageData(0, 0, width, height);
|
|
326
|
+
} catch {
|
|
327
|
+
throw new Error(
|
|
328
|
+
"Could not read the picture (cross-origin images need CORS); upload it instead"
|
|
329
|
+
);
|
|
330
|
+
} finally {
|
|
331
|
+
canvas.remove();
|
|
332
|
+
}
|
|
333
|
+
return createQRSilhouette(pixels, options);
|
|
334
|
+
}
|
package/src/utils/color.ts
CHANGED
|
@@ -25,8 +25,17 @@ export function getSolidColor(
|
|
|
25
25
|
return colorConfig.color;
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
+
// An image fill stands for its underlay (shown where the image isn't)
|
|
29
|
+
if (colorConfig.type === "image") {
|
|
30
|
+
return colorConfig.underlay || fallback;
|
|
31
|
+
}
|
|
32
|
+
|
|
28
33
|
// For gradients, return the first stop color as representative
|
|
29
|
-
if (
|
|
34
|
+
if (
|
|
35
|
+
colorConfig.type === "linear" ||
|
|
36
|
+
colorConfig.type === "radial" ||
|
|
37
|
+
colorConfig.type === "angular"
|
|
38
|
+
) {
|
|
30
39
|
return colorConfig.stops[0]?.color || fallback;
|
|
31
40
|
}
|
|
32
41
|
|
package/src/utils/fmt.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// Number formatting for every vector output (SVG markup, EPS, PDF).
|
|
2
|
+
//
|
|
3
|
+
// Output coordinates are pixels at the rendered size (modules for the plain
|
|
4
|
+
// getQRAsSVG renderer, points for EPS). The smallest code is 50 px across,
|
|
5
|
+
// so a module is at least about 1 px and rounding to 0.001 moves an edge by
|
|
6
|
+
// at most 0.0005 px (0.01 px when that 50 px code is blown up to 1024 px).
|
|
7
|
+
// Rasterized with librsvg, that changes 0.2–3% of pixels by 1–2 levels on
|
|
8
|
+
// average, less than shifting the unrounded drawing by 0.0005 px does.
|
|
9
|
+
// Unrounded, JavaScript writes up to 17 significant digits
|
|
10
|
+
// (12.345678901234567), which roughly doubled the markup.
|
|
11
|
+
//
|
|
12
|
+
// Half-circle arcs need care: when rounding makes the chord a hair shorter
|
|
13
|
+
// than the diameter, the arc's center leaves the chord (by √(2rδ), 0.2 px
|
|
14
|
+
// for r = 40), so round the radius first and derive the chord from it.
|
|
15
|
+
|
|
16
|
+
/** Decimals kept for coordinates, radii and lengths. */
|
|
17
|
+
export const COORD_DECIMALS = 3;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Decimals kept for scale factors: their error grows with the distance from
|
|
21
|
+
* the origin (a 1000-unit artwork scaled by a 6-decimal factor still lands
|
|
22
|
+
* within 0.001).
|
|
23
|
+
*/
|
|
24
|
+
export const SCALE_DECIMALS = 6;
|
|
25
|
+
|
|
26
|
+
/** `value` rounded to `decimals` places (half up), never -0. */
|
|
27
|
+
export function roundTo(value: number, decimals = COORD_DECIMALS): number {
|
|
28
|
+
const scale = 10 ** decimals;
|
|
29
|
+
// `+ 0` turns -0 into 0
|
|
30
|
+
return Math.round(value * scale) / scale + 0;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* `value` for SVG, EPS or PDF output: rounded to `decimals` places, with no
|
|
35
|
+
* trailing zeros and no "-0". `fmt(12.345678901234567)` is "12.346",
|
|
36
|
+
* `fmt(4.5)` is "4.5" and `fmt(-0.0001)` is "0". No exponent for
|
|
37
|
+
* |value| < 1e21 with up to 6 decimals.
|
|
38
|
+
*/
|
|
39
|
+
export function fmt(value: number, decimals = COORD_DECIMALS): string {
|
|
40
|
+
return String(roundTo(value, decimals));
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Template tag that writes every interpolated number with `fmt` (strings
|
|
45
|
+
* pass through), for path data and transforms:
|
|
46
|
+
* compact`M${x} ${y}h${w}` is "M1.5 2.333h7".
|
|
47
|
+
*/
|
|
48
|
+
export function compact(
|
|
49
|
+
strings: TemplateStringsArray,
|
|
50
|
+
...values: (number | string)[]
|
|
51
|
+
): string {
|
|
52
|
+
let out = strings[0] ?? "";
|
|
53
|
+
for (let i = 0; i < values.length; i++) {
|
|
54
|
+
const value = values[i];
|
|
55
|
+
out +=
|
|
56
|
+
(typeof value === "number" ? fmt(value) : value) + (strings[i + 1] ?? "");
|
|
57
|
+
}
|
|
58
|
+
return out;
|
|
59
|
+
}
|