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
|
@@ -0,0 +1,511 @@
|
|
|
1
|
+
/** biome-ignore-all lint/style/noMagicNumbers: outline geometry */
|
|
2
|
+
// Outlines for shaped codes other than the circle: the built-in heart and
|
|
3
|
+
// silhouettes (QRSilhouette, a small grid traced from a picture). Each is a
|
|
4
|
+
// set of closed rings in artwork units (0 to 1 across, y down), placed so
|
|
5
|
+
// the largest square that fits inside is centered on the artwork: the
|
|
6
|
+
// symbol sits at the center like every other code, so the logo, art, eyes
|
|
7
|
+
// and gradients need nothing new. `half` is that square's half side, and
|
|
8
|
+
// the layout (qr-shape.ts) sizes the module grid so the symbol and its
|
|
9
|
+
// quiet ring fit in it.
|
|
10
|
+
import {
|
|
11
|
+
QR_SILHOUETTE_MAX_CELLS,
|
|
12
|
+
QR_SILHOUETTE_MIN_CELLS,
|
|
13
|
+
QR_SILHOUETTE_MIN_SHARE,
|
|
14
|
+
type QRSilhouette,
|
|
15
|
+
} from "../types/qr";
|
|
16
|
+
import { fmt } from "./utils/fmt";
|
|
17
|
+
|
|
18
|
+
type Point = [number, number];
|
|
19
|
+
|
|
20
|
+
/** A line to its point, a quadratic (control, end) or a cubic (c1, c2, end). */
|
|
21
|
+
type Segment = Point[];
|
|
22
|
+
|
|
23
|
+
interface Ring {
|
|
24
|
+
start: Point;
|
|
25
|
+
segments: Segment[];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface Outline {
|
|
29
|
+
/** Closed rings in artwork units (0 to 1), filled even-odd */
|
|
30
|
+
rings: Ring[];
|
|
31
|
+
/** The rings flattened into polygons, for inside tests */
|
|
32
|
+
polygons: Point[][];
|
|
33
|
+
/** Half side of the largest square centered on (0.5, 0.5) inside it */
|
|
34
|
+
half: number;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const CURVE_STEPS = { 2: 6, 3: 12 } as const;
|
|
38
|
+
|
|
39
|
+
function flatten(ring: Ring): Point[] {
|
|
40
|
+
const points: Point[] = [ring.start];
|
|
41
|
+
let from = ring.start;
|
|
42
|
+
for (const segment of ring.segments) {
|
|
43
|
+
const end = segment.at(-1) as Point;
|
|
44
|
+
if (segment.length === 1) {
|
|
45
|
+
points.push(end);
|
|
46
|
+
} else {
|
|
47
|
+
const steps = CURVE_STEPS[segment.length as 2 | 3];
|
|
48
|
+
for (let i = 1; i <= steps; i++) {
|
|
49
|
+
const t = i / steps;
|
|
50
|
+
const u = 1 - t;
|
|
51
|
+
if (segment.length === 2) {
|
|
52
|
+
const [c] = segment as [Point, Point];
|
|
53
|
+
points.push([
|
|
54
|
+
u * u * from[0] + 2 * u * t * c[0] + t * t * end[0],
|
|
55
|
+
u * u * from[1] + 2 * u * t * c[1] + t * t * end[1],
|
|
56
|
+
]);
|
|
57
|
+
} else {
|
|
58
|
+
const [c1, c2] = segment as [Point, Point, Point];
|
|
59
|
+
points.push([
|
|
60
|
+
u * u * u * from[0] +
|
|
61
|
+
3 * u * u * t * c1[0] +
|
|
62
|
+
3 * u * t * t * c2[0] +
|
|
63
|
+
t * t * t * end[0],
|
|
64
|
+
u * u * u * from[1] +
|
|
65
|
+
3 * u * u * t * c1[1] +
|
|
66
|
+
3 * u * t * t * c2[1] +
|
|
67
|
+
t * t * t * end[1],
|
|
68
|
+
]);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
from = end;
|
|
73
|
+
}
|
|
74
|
+
return points;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Crossings of a horizontal line with the polygons, sorted (even-odd). */
|
|
78
|
+
function crossings(polygons: Point[][], y: number): number[] {
|
|
79
|
+
const xs: number[] = [];
|
|
80
|
+
for (const polygon of polygons) {
|
|
81
|
+
for (let i = 0; i < polygon.length; i++) {
|
|
82
|
+
const [x0, y0] = polygon[i] as Point;
|
|
83
|
+
const [x1, y1] = polygon[(i + 1) % polygon.length] as Point;
|
|
84
|
+
if (y0 <= y !== y1 <= y) {
|
|
85
|
+
xs.push(x0 + ((y - y0) / (y1 - y0)) * (x1 - x0));
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return xs.sort((a, b) => a - b);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function insideRow(xs: number[], x: number): boolean {
|
|
93
|
+
let count = 0;
|
|
94
|
+
for (const crossing of xs) {
|
|
95
|
+
if (crossing < x) count++;
|
|
96
|
+
else break;
|
|
97
|
+
}
|
|
98
|
+
return count % 2 === 1;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** True when (x, y) is inside the polygons (even-odd). */
|
|
102
|
+
export function insidePolygons(
|
|
103
|
+
polygons: Point[][],
|
|
104
|
+
x: number,
|
|
105
|
+
y: number
|
|
106
|
+
): boolean {
|
|
107
|
+
return insideRow(crossings(polygons, y), x);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Inside tests at many points, caching each row's crossings (decoration
|
|
112
|
+
* samples a few rows per module row).
|
|
113
|
+
*/
|
|
114
|
+
export function insideTester(polygons: Point[][]) {
|
|
115
|
+
const rows = new Map<number, number[]>();
|
|
116
|
+
return (x: number, y: number) => {
|
|
117
|
+
let xs = rows.get(y);
|
|
118
|
+
if (!xs) {
|
|
119
|
+
xs = crossings(polygons, y);
|
|
120
|
+
rows.set(y, xs);
|
|
121
|
+
}
|
|
122
|
+
return insideRow(xs, x);
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** Distance from (cx, cy) to a segment, measured as max(|dx|, |dy|). */
|
|
127
|
+
function chebyshevToSegment(
|
|
128
|
+
cx: number,
|
|
129
|
+
cy: number,
|
|
130
|
+
a: Point,
|
|
131
|
+
b: Point
|
|
132
|
+
): number {
|
|
133
|
+
const ax = a[0] - cx;
|
|
134
|
+
const ay = a[1] - cy;
|
|
135
|
+
const dx = b[0] - a[0];
|
|
136
|
+
const dy = b[1] - a[1];
|
|
137
|
+
const at = (t: number) =>
|
|
138
|
+
Math.max(Math.abs(ax + t * dx), Math.abs(ay + t * dy));
|
|
139
|
+
let best = Math.min(at(0), at(1));
|
|
140
|
+
// max(|x|, |y|) along the segment is smallest at an end or where |x| = |y|
|
|
141
|
+
for (const [num, den] of [
|
|
142
|
+
[ay - ax, dx - dy],
|
|
143
|
+
[-(ax + ay), dx + dy],
|
|
144
|
+
] as const) {
|
|
145
|
+
if (den !== 0) {
|
|
146
|
+
const t = num / den;
|
|
147
|
+
if (t > 0 && t < 1) best = Math.min(best, at(t));
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return best;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** Half side of the largest square centered on (cx, cy) inside the polygons. */
|
|
154
|
+
function centeredHalf(polygons: Point[][], cx: number, cy: number): number {
|
|
155
|
+
if (!insidePolygons(polygons, cx, cy)) return 0;
|
|
156
|
+
let best = Number.POSITIVE_INFINITY;
|
|
157
|
+
for (const polygon of polygons) {
|
|
158
|
+
for (let i = 0; i < polygon.length; i++) {
|
|
159
|
+
best = Math.min(
|
|
160
|
+
best,
|
|
161
|
+
chebyshevToSegment(
|
|
162
|
+
cx,
|
|
163
|
+
cy,
|
|
164
|
+
polygon[i] as Point,
|
|
165
|
+
polygon[(i + 1) % polygon.length] as Point
|
|
166
|
+
)
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return Number.isFinite(best) ? best : 0;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function bounds(polygons: Point[][]) {
|
|
174
|
+
let x0 = Number.POSITIVE_INFINITY;
|
|
175
|
+
let y0 = Number.POSITIVE_INFINITY;
|
|
176
|
+
let x1 = Number.NEGATIVE_INFINITY;
|
|
177
|
+
let y1 = Number.NEGATIVE_INFINITY;
|
|
178
|
+
for (const polygon of polygons) {
|
|
179
|
+
for (const [x, y] of polygon) {
|
|
180
|
+
x0 = Math.min(x0, x);
|
|
181
|
+
y0 = Math.min(y0, y);
|
|
182
|
+
x1 = Math.max(x1, x);
|
|
183
|
+
y1 = Math.max(y1, y);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return { x0, y0, x1, y1 };
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Places rings (in any units) on the artwork: the center the symbol goes at
|
|
191
|
+
* moves to (0.5, 0.5) and the rings scale to the largest size that keeps
|
|
192
|
+
* them inside the artwork. The center is the one that gives the biggest
|
|
193
|
+
* centered square after that scaling, found on a grid and then refined.
|
|
194
|
+
*/
|
|
195
|
+
function place(rings: Ring[]): Outline | null {
|
|
196
|
+
const polygons = rings.map(flatten);
|
|
197
|
+
const box = bounds(polygons);
|
|
198
|
+
const width = box.x1 - box.x0;
|
|
199
|
+
const height = box.y1 - box.y0;
|
|
200
|
+
if (!(width > 0 && height > 0)) return null;
|
|
201
|
+
const score = (cx: number, cy: number) => {
|
|
202
|
+
const reach = Math.max(cx - box.x0, box.x1 - cx, cy - box.y0, box.y1 - cy);
|
|
203
|
+
return reach > 0 ? centeredHalf(polygons, cx, cy) / (2 * reach) : 0;
|
|
204
|
+
};
|
|
205
|
+
let best = { cx: box.x0 + width / 2, cy: box.y0 + height / 2, value: 0 };
|
|
206
|
+
best.value = score(best.cx, best.cy);
|
|
207
|
+
const GRID = 32;
|
|
208
|
+
for (let j = 1; j < GRID; j++) {
|
|
209
|
+
for (let i = 1; i < GRID; i++) {
|
|
210
|
+
const cx = box.x0 + (width * i) / GRID;
|
|
211
|
+
const cy = box.y0 + (height * j) / GRID;
|
|
212
|
+
const value = score(cx, cy);
|
|
213
|
+
if (value > best.value) best = { cx, cy, value };
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
let stepX = width / GRID;
|
|
217
|
+
let stepY = height / GRID;
|
|
218
|
+
for (let round = 0; round < 6; round++) {
|
|
219
|
+
const center = best;
|
|
220
|
+
for (let j = -2; j <= 2; j++) {
|
|
221
|
+
for (let i = -2; i <= 2; i++) {
|
|
222
|
+
const cx = center.cx + (stepX * i) / 2;
|
|
223
|
+
const cy = center.cy + (stepY * j) / 2;
|
|
224
|
+
const value = score(cx, cy);
|
|
225
|
+
if (value > best.value) best = { cx, cy, value };
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
stepX /= 2;
|
|
229
|
+
stepY /= 2;
|
|
230
|
+
}
|
|
231
|
+
if (best.value <= 0) return null;
|
|
232
|
+
const reach = Math.max(
|
|
233
|
+
best.cx - box.x0,
|
|
234
|
+
box.x1 - best.cx,
|
|
235
|
+
best.cy - box.y0,
|
|
236
|
+
box.y1 - best.cy
|
|
237
|
+
);
|
|
238
|
+
const k = 0.5 / reach;
|
|
239
|
+
const map = ([x, y]: Point): Point => [
|
|
240
|
+
0.5 + (x - best.cx) * k,
|
|
241
|
+
0.5 + (y - best.cy) * k,
|
|
242
|
+
];
|
|
243
|
+
const placed = rings.map((ring) => ({
|
|
244
|
+
start: map(ring.start),
|
|
245
|
+
segments: ring.segments.map((segment) => segment.map(map)),
|
|
246
|
+
}));
|
|
247
|
+
return { rings: placed, polygons: placed.map(flatten), half: best.value };
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/** SVG path data for an outline drawn `size` px across. */
|
|
251
|
+
export function outlinePath(outline: Outline, size: number): string {
|
|
252
|
+
const p = ([x, y]: Point) => `${fmt(x * size)} ${fmt(y * size)}`;
|
|
253
|
+
let d = "";
|
|
254
|
+
for (const ring of outline.rings) {
|
|
255
|
+
d += `M${p(ring.start)}`;
|
|
256
|
+
for (const segment of ring.segments) {
|
|
257
|
+
const command =
|
|
258
|
+
segment.length === 1 ? "L" : segment.length === 2 ? "Q" : "C";
|
|
259
|
+
d += command + segment.map(p).join(" ");
|
|
260
|
+
}
|
|
261
|
+
d += "Z";
|
|
262
|
+
}
|
|
263
|
+
return d;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// ── The heart ──────────────────────────────────────────────────────────────
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* A full heart in its own units (2 wide), clockwise from the notch: round
|
|
270
|
+
* lobes, a shallow notch and full sides down to a soft point, so the symbol
|
|
271
|
+
* takes 57% of the width (a slimmer heart left it 51%).
|
|
272
|
+
*/
|
|
273
|
+
const HEART: Ring = {
|
|
274
|
+
start: [0, -0.58],
|
|
275
|
+
segments: [
|
|
276
|
+
[
|
|
277
|
+
[0.2, -0.92],
|
|
278
|
+
[1, -0.97],
|
|
279
|
+
[1, -0.3],
|
|
280
|
+
],
|
|
281
|
+
[
|
|
282
|
+
[1, 0.3],
|
|
283
|
+
[0.62, 0.6],
|
|
284
|
+
[0, 0.92],
|
|
285
|
+
],
|
|
286
|
+
[
|
|
287
|
+
[-0.62, 0.6],
|
|
288
|
+
[-1, 0.3],
|
|
289
|
+
[-1, -0.3],
|
|
290
|
+
],
|
|
291
|
+
[
|
|
292
|
+
[-1, -0.97],
|
|
293
|
+
[-0.2, -0.92],
|
|
294
|
+
[0, -0.58],
|
|
295
|
+
],
|
|
296
|
+
],
|
|
297
|
+
};
|
|
298
|
+
|
|
299
|
+
let heartOutline: Outline | null | undefined;
|
|
300
|
+
|
|
301
|
+
/** The heart, placed on the artwork. */
|
|
302
|
+
export function getHeartOutline(): Outline {
|
|
303
|
+
heartOutline ??= place([HEART]);
|
|
304
|
+
return heartOutline as Outline;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// ── Silhouettes ────────────────────────────────────────────────────────────
|
|
308
|
+
|
|
309
|
+
/** Why a silhouette can't hold a code, or null when it can. */
|
|
310
|
+
export type SilhouetteProblem = "invalid" | "empty" | "thin";
|
|
311
|
+
|
|
312
|
+
function cellsOf(
|
|
313
|
+
silhouette: QRSilhouette
|
|
314
|
+
): ((x: number, y: number) => boolean) | null {
|
|
315
|
+
const { width, height, cells } = silhouette;
|
|
316
|
+
if (
|
|
317
|
+
!(
|
|
318
|
+
Number.isInteger(width) &&
|
|
319
|
+
Number.isInteger(height) &&
|
|
320
|
+
width >= QR_SILHOUETTE_MIN_CELLS &&
|
|
321
|
+
height >= QR_SILHOUETTE_MIN_CELLS &&
|
|
322
|
+
width <= QR_SILHOUETTE_MAX_CELLS &&
|
|
323
|
+
height <= QR_SILHOUETTE_MAX_CELLS &&
|
|
324
|
+
typeof cells === "string" &&
|
|
325
|
+
cells.length === width * height &&
|
|
326
|
+
/^[01]*$/.test(cells)
|
|
327
|
+
)
|
|
328
|
+
) {
|
|
329
|
+
return null;
|
|
330
|
+
}
|
|
331
|
+
return (x, y) =>
|
|
332
|
+
x >= 0 && y >= 0 && x < width && y < height && cells[y * width + x] === "1";
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
const key = ([x, y]: Point) => `${x},${y}`;
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* The silhouette's outline as polygons through the midpoints between cell
|
|
339
|
+
* centers (marching squares on the cell grid, cells joined only side to
|
|
340
|
+
* side), with straight runs merged.
|
|
341
|
+
*/
|
|
342
|
+
function tracePolygons(
|
|
343
|
+
on: (x: number, y: number) => boolean,
|
|
344
|
+
width: number,
|
|
345
|
+
height: number
|
|
346
|
+
): Point[][] {
|
|
347
|
+
// Corner (i, j) is the center of cell (i - 1, j - 1); the frame is off.
|
|
348
|
+
const corner = (i: number, j: number) => on(i - 1, j - 1);
|
|
349
|
+
const links = new Map<string, Point[]>();
|
|
350
|
+
const link = (a: Point, b: Point) => {
|
|
351
|
+
for (const [from, to] of [
|
|
352
|
+
[a, b],
|
|
353
|
+
[b, a],
|
|
354
|
+
] as const) {
|
|
355
|
+
const list = links.get(key(from)) ?? [];
|
|
356
|
+
list.push(to);
|
|
357
|
+
links.set(key(from), list);
|
|
358
|
+
}
|
|
359
|
+
};
|
|
360
|
+
for (let j = 0; j <= height; j++) {
|
|
361
|
+
for (let i = 0; i <= width; i++) {
|
|
362
|
+
const tl = corner(i, j);
|
|
363
|
+
const tr = corner(i + 1, j);
|
|
364
|
+
const br = corner(i + 1, j + 1);
|
|
365
|
+
const bl = corner(i, j + 1);
|
|
366
|
+
// Edge midpoints, in cell units (cell centers sit at i - 0.5)
|
|
367
|
+
const top: Point = [i, j - 0.5];
|
|
368
|
+
const right: Point = [i + 0.5, j];
|
|
369
|
+
const bottom: Point = [i, j + 0.5];
|
|
370
|
+
const left: Point = [i - 0.5, j];
|
|
371
|
+
const code = (tl ? 8 : 0) + (tr ? 4 : 0) + (br ? 2 : 0) + (bl ? 1 : 0);
|
|
372
|
+
switch (code) {
|
|
373
|
+
case 1:
|
|
374
|
+
case 14:
|
|
375
|
+
link(left, bottom);
|
|
376
|
+
break;
|
|
377
|
+
case 2:
|
|
378
|
+
case 13:
|
|
379
|
+
link(bottom, right);
|
|
380
|
+
break;
|
|
381
|
+
case 3:
|
|
382
|
+
case 12:
|
|
383
|
+
link(left, right);
|
|
384
|
+
break;
|
|
385
|
+
case 4:
|
|
386
|
+
case 11:
|
|
387
|
+
link(top, right);
|
|
388
|
+
break;
|
|
389
|
+
case 6:
|
|
390
|
+
case 9:
|
|
391
|
+
link(top, bottom);
|
|
392
|
+
break;
|
|
393
|
+
case 7:
|
|
394
|
+
case 8:
|
|
395
|
+
link(left, top);
|
|
396
|
+
break;
|
|
397
|
+
case 5:
|
|
398
|
+
// Diagonal cells stay apart: cut off each lit corner
|
|
399
|
+
link(top, right);
|
|
400
|
+
link(left, bottom);
|
|
401
|
+
break;
|
|
402
|
+
case 10:
|
|
403
|
+
link(left, top);
|
|
404
|
+
link(bottom, right);
|
|
405
|
+
break;
|
|
406
|
+
default:
|
|
407
|
+
break;
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
const polygons: Point[][] = [];
|
|
412
|
+
const used = new Set<string>();
|
|
413
|
+
for (const [startKey, ends] of links) {
|
|
414
|
+
if (used.has(startKey) || ends.length === 0) continue;
|
|
415
|
+
const [sx, sy] = startKey.split(",").map(Number) as Point;
|
|
416
|
+
const ring: Point[] = [[sx, sy]];
|
|
417
|
+
used.add(startKey);
|
|
418
|
+
let previous: Point = [sx, sy];
|
|
419
|
+
let current = ends[0] as Point;
|
|
420
|
+
while (!used.has(key(current))) {
|
|
421
|
+
used.add(key(current));
|
|
422
|
+
ring.push(current);
|
|
423
|
+
const next = nextOnRing(links.get(key(current)) ?? [], previous, used);
|
|
424
|
+
if (!next) break;
|
|
425
|
+
previous = current;
|
|
426
|
+
current = next;
|
|
427
|
+
}
|
|
428
|
+
if (ring.length >= 3) polygons.push(mergeRuns(ring));
|
|
429
|
+
}
|
|
430
|
+
return polygons;
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
/** The neighbor to walk to: not where we came from, unvisited first. */
|
|
434
|
+
function nextOnRing(
|
|
435
|
+
neighbors: Point[],
|
|
436
|
+
previous: Point,
|
|
437
|
+
used: Set<string>
|
|
438
|
+
): Point | undefined {
|
|
439
|
+
const back = key(previous);
|
|
440
|
+
return (
|
|
441
|
+
neighbors.find((p) => key(p) !== back && !used.has(key(p))) ??
|
|
442
|
+
neighbors.find((p) => key(p) !== back)
|
|
443
|
+
);
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
/** Drops points in the middle of straight runs. */
|
|
447
|
+
function mergeRuns(ring: Point[]): Point[] {
|
|
448
|
+
const out: Point[] = [];
|
|
449
|
+
for (let i = 0; i < ring.length; i++) {
|
|
450
|
+
const a = ring[(i - 1 + ring.length) % ring.length] as Point;
|
|
451
|
+
const b = ring[i] as Point;
|
|
452
|
+
const c = ring[(i + 1) % ring.length] as Point;
|
|
453
|
+
const cross = (b[0] - a[0]) * (c[1] - b[1]) - (b[1] - a[1]) * (c[0] - b[0]);
|
|
454
|
+
if (Math.abs(cross) > 1e-9) out.push(b);
|
|
455
|
+
}
|
|
456
|
+
return out.length >= 3 ? out : ring;
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
/** A polygon drawn as a smooth curve: quadratics through its edge midpoints. */
|
|
460
|
+
function smoothRing(points: Point[]): Ring {
|
|
461
|
+
const mid = (a: Point, b: Point): Point => [
|
|
462
|
+
(a[0] + b[0]) / 2,
|
|
463
|
+
(a[1] + b[1]) / 2,
|
|
464
|
+
];
|
|
465
|
+
const n = points.length;
|
|
466
|
+
const start = mid(points[n - 1] as Point, points[0] as Point);
|
|
467
|
+
const segments: Segment[] = points.map((point, i) => [
|
|
468
|
+
point,
|
|
469
|
+
mid(point, points[(i + 1) % n] as Point),
|
|
470
|
+
]);
|
|
471
|
+
return { start, segments };
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
const silhouetteCache = new Map<
|
|
475
|
+
string,
|
|
476
|
+
{ outline: Outline | null; problem: SilhouetteProblem | null }
|
|
477
|
+
>();
|
|
478
|
+
|
|
479
|
+
/**
|
|
480
|
+
* The silhouette's outline on the artwork, or why it can't hold a code:
|
|
481
|
+
* "invalid" (not a well-formed grid), "empty" (no cells inside) or "thin"
|
|
482
|
+
* (the symbol would take less than QR_SILHOUETTE_MIN_SHARE of the width).
|
|
483
|
+
*/
|
|
484
|
+
export function getSilhouetteOutline(silhouette: QRSilhouette): {
|
|
485
|
+
outline: Outline | null;
|
|
486
|
+
problem: SilhouetteProblem | null;
|
|
487
|
+
} {
|
|
488
|
+
const cacheKey = `${silhouette.width}x${silhouette.height}:${silhouette.cells}`;
|
|
489
|
+
const cached = silhouetteCache.get(cacheKey);
|
|
490
|
+
if (cached) return cached;
|
|
491
|
+
const on = cellsOf(silhouette);
|
|
492
|
+
let result: { outline: Outline | null; problem: SilhouetteProblem | null };
|
|
493
|
+
if (!on) {
|
|
494
|
+
result = { outline: null, problem: "invalid" };
|
|
495
|
+
} else if (silhouette.cells.includes("1")) {
|
|
496
|
+
const polygons = tracePolygons(on, silhouette.width, silhouette.height);
|
|
497
|
+
const outline =
|
|
498
|
+
polygons.length > 0 ? place(polygons.map(smoothRing)) : null;
|
|
499
|
+
result =
|
|
500
|
+
outline && 2 * outline.half >= QR_SILHOUETTE_MIN_SHARE
|
|
501
|
+
? { outline, problem: null }
|
|
502
|
+
: { outline: null, problem: outline ? "thin" : "empty" };
|
|
503
|
+
} else {
|
|
504
|
+
result = { outline: null, problem: "empty" };
|
|
505
|
+
}
|
|
506
|
+
if (silhouetteCache.size > 16) {
|
|
507
|
+
silhouetteCache.delete(silhouetteCache.keys().next().value as string);
|
|
508
|
+
}
|
|
509
|
+
silhouetteCache.set(cacheKey, result);
|
|
510
|
+
return result;
|
|
511
|
+
}
|