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.
Files changed (206) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +353 -300
  3. package/dist/index.js +27 -7
  4. package/dist/index.js.map +1 -1
  5. package/dist/index.mjs +27 -7
  6. package/dist/index.mjs.map +1 -1
  7. package/dist/qr-code-canvas.js +12 -0
  8. package/dist/qr-code-canvas.js.map +1 -0
  9. package/dist/qr-code-canvas.mjs +12 -0
  10. package/dist/qr-code-canvas.mjs.map +1 -0
  11. package/dist/src/api.d.ts.map +1 -1
  12. package/dist/src/art-trace.d.ts +46 -0
  13. package/dist/src/art-trace.d.ts.map +1 -0
  14. package/dist/src/art.d.ts +138 -0
  15. package/dist/src/art.d.ts.map +1 -0
  16. package/dist/src/body-patterns.d.ts +34 -0
  17. package/dist/src/body-patterns.d.ts.map +1 -0
  18. package/dist/src/codegen.d.ts +5 -5
  19. package/dist/src/codegen.d.ts.map +1 -1
  20. package/dist/src/codeword-map.d.ts +40 -0
  21. package/dist/src/codeword-map.d.ts.map +1 -0
  22. package/dist/src/constants.d.ts +4 -7
  23. package/dist/src/constants.d.ts.map +1 -1
  24. package/dist/src/corner-dot.d.ts +3 -1
  25. package/dist/src/corner-dot.d.ts.map +1 -1
  26. package/dist/src/corner-eye.d.ts +5 -2
  27. package/dist/src/corner-eye.d.ts.map +1 -1
  28. package/dist/src/detection.d.ts.map +1 -1
  29. package/dist/src/download.d.ts +22 -5
  30. package/dist/src/download.d.ts.map +1 -1
  31. package/dist/src/eps.d.ts +62 -0
  32. package/dist/src/eps.d.ts.map +1 -0
  33. package/dist/src/frame-icons.d.ts +11 -0
  34. package/dist/src/frame-icons.d.ts.map +1 -0
  35. package/dist/src/frame-import.d.ts +61 -0
  36. package/dist/src/frame-import.d.ts.map +1 -0
  37. package/dist/src/frame-spec.d.ts +24 -0
  38. package/dist/src/frame-spec.d.ts.map +1 -0
  39. package/dist/src/frame-styles.d.ts +50 -0
  40. package/dist/src/frame-styles.d.ts.map +1 -0
  41. package/dist/src/frame.d.ts +126 -0
  42. package/dist/src/frame.d.ts.map +1 -0
  43. package/dist/src/frames/builtin.d.ts +8 -0
  44. package/dist/src/frames/builtin.d.ts.map +1 -0
  45. package/dist/src/frames/official.d.ts +18 -0
  46. package/dist/src/frames/official.d.ts.map +1 -0
  47. package/dist/src/helpers.d.ts +15 -2
  48. package/dist/src/helpers.d.ts.map +1 -1
  49. package/dist/src/index.d.ts +24 -13
  50. package/dist/src/index.d.ts.map +1 -1
  51. package/dist/src/logo-budget.d.ts +54 -0
  52. package/dist/src/logo-budget.d.ts.map +1 -0
  53. package/dist/src/logo-cleanup.d.ts +25 -0
  54. package/dist/src/logo-cleanup.d.ts.map +1 -0
  55. package/dist/src/logo.d.ts +28 -0
  56. package/dist/src/logo.d.ts.map +1 -0
  57. package/dist/src/print.d.ts +73 -0
  58. package/dist/src/print.d.ts.map +1 -0
  59. package/dist/src/qr-code-canvas.d.ts +13 -0
  60. package/dist/src/qr-code-canvas.d.ts.map +1 -0
  61. package/dist/src/qr-code.d.ts +4 -2
  62. package/dist/src/qr-code.d.ts.map +1 -1
  63. package/dist/src/qr-outline.d.ts +41 -0
  64. package/dist/src/qr-outline.d.ts.map +1 -0
  65. package/dist/src/qr-shape.d.ts +72 -0
  66. package/dist/src/qr-shape.d.ts.map +1 -0
  67. package/dist/src/scan-risk.d.ts +40 -0
  68. package/dist/src/scan-risk.d.ts.map +1 -0
  69. package/dist/src/scan-score.d.ts +154 -0
  70. package/dist/src/scan-score.d.ts.map +1 -0
  71. package/dist/src/shapes.d.ts +36 -0
  72. package/dist/src/shapes.d.ts.map +1 -0
  73. package/dist/src/silhouette.d.ts +28 -0
  74. package/dist/src/silhouette.d.ts.map +1 -0
  75. package/dist/src/utils/color.d.ts.map +1 -1
  76. package/dist/src/utils/fmt.d.ts +24 -0
  77. package/dist/src/utils/fmt.d.ts.map +1 -0
  78. package/dist/src/utils/paint.d.ts +80 -0
  79. package/dist/src/utils/paint.d.ts.map +1 -0
  80. package/dist/src/utils/parse-color.d.ts +21 -0
  81. package/dist/src/utils/parse-color.d.ts.map +1 -0
  82. package/dist/src/utils/path-data.d.ts +24 -0
  83. package/dist/src/utils/path-data.d.ts.map +1 -0
  84. package/dist/src/utils/png.d.ts +16 -0
  85. package/dist/src/utils/png.d.ts.map +1 -0
  86. package/dist/src/utils/polygon-clip.d.ts +26 -0
  87. package/dist/src/utils/polygon-clip.d.ts.map +1 -0
  88. package/dist/src/utils/print-shared.d.ts +20 -0
  89. package/dist/src/utils/print-shared.d.ts.map +1 -0
  90. package/dist/src/utils/svg-geometry.d.ts +49 -0
  91. package/dist/src/utils/svg-geometry.d.ts.map +1 -0
  92. package/dist/src/utils/svg-gradient.d.ts +59 -0
  93. package/dist/src/utils/svg-gradient.d.ts.map +1 -0
  94. package/dist/src/utils/svg-painter.d.ts +152 -0
  95. package/dist/src/utils/svg-painter.d.ts.map +1 -0
  96. package/dist/src/utils.d.ts +18 -2
  97. package/dist/src/utils.d.ts.map +1 -1
  98. package/dist/types/art.d.ts +64 -0
  99. package/dist/types/art.d.ts.map +1 -0
  100. package/dist/types/color.d.ts +125 -7
  101. package/dist/types/color.d.ts.map +1 -1
  102. package/dist/types/corner-dot.d.ts +52 -2
  103. package/dist/types/corner-dot.d.ts.map +1 -1
  104. package/dist/types/corner-eye.d.ts +43 -2
  105. package/dist/types/corner-eye.d.ts.map +1 -1
  106. package/dist/types/frame-spec.d.ts +3509 -0
  107. package/dist/types/frame-spec.d.ts.map +1 -0
  108. package/dist/types/frame.d.ts +68 -0
  109. package/dist/types/frame.d.ts.map +1 -0
  110. package/dist/types/image-pattern.d.ts +15 -1
  111. package/dist/types/image-pattern.d.ts.map +1 -1
  112. package/dist/types/index.d.ts +15 -9
  113. package/dist/types/index.d.ts.map +1 -1
  114. package/dist/types/index.js +6 -0
  115. package/dist/types/index.js.map +1 -0
  116. package/dist/types/index.mjs +6 -0
  117. package/dist/types/index.mjs.map +1 -0
  118. package/dist/types/qr.d.ts +1684 -4
  119. package/dist/types/qr.d.ts.map +1 -1
  120. package/dist/types/template.d.ts +2 -2
  121. package/dist/types/template.d.ts.map +1 -1
  122. package/package.json +68 -25
  123. package/src/api.tsx +67 -17
  124. package/src/art-trace.ts +554 -0
  125. package/src/art.tsx +2226 -0
  126. package/src/body-patterns.tsx +389 -0
  127. package/src/codegen.ts +10 -5
  128. package/src/codeword-map.ts +265 -0
  129. package/src/constants.ts +4 -8
  130. package/src/corner-dot.tsx +209 -33
  131. package/src/corner-eye.ts +328 -60
  132. package/src/detection.ts +5 -11
  133. package/src/download.tsx +123 -52
  134. package/src/eps.ts +701 -0
  135. package/src/frame-icons.ts +325 -0
  136. package/src/frame-import.ts +1198 -0
  137. package/src/frame-spec.ts +518 -0
  138. package/src/frame-styles.ts +1095 -0
  139. package/src/frame.tsx +630 -0
  140. package/src/frames/builtin/banner-bottom.json +58 -0
  141. package/src/frames/builtin/circle.json +81 -0
  142. package/src/frames/builtin/rounded-border.json +60 -0
  143. package/src/frames/builtin/ticket.json +51 -0
  144. package/src/frames/builtin.ts +20 -0
  145. package/src/frames/official/attendance.json +110 -0
  146. package/src/frames/official/audio-guide.json +95 -0
  147. package/src/frames/official/book-now.json +103 -0
  148. package/src/frames/official/check-in.json +110 -0
  149. package/src/frames/official/christmas.json +131 -0
  150. package/src/frames/official/donate.json +77 -0
  151. package/src/frames/official/event-ticket.json +77 -0
  152. package/src/frames/official/feedback-bubble.json +73 -0
  153. package/src/frames/official/follow-us.json +83 -0
  154. package/src/frames/official/get-the-app.json +107 -0
  155. package/src/frames/official/halloween.json +80 -0
  156. package/src/frames/official/new-year.json +94 -0
  157. package/src/frames/official/order-at-table.json +86 -0
  158. package/src/frames/official/product-info.json +119 -0
  159. package/src/frames/official/register-product.json +77 -0
  160. package/src/frames/official/review-us.json +127 -0
  161. package/src/frames/official/rsvp-card.json +112 -0
  162. package/src/frames/official/save-contact.json +117 -0
  163. package/src/frames/official/scan-to-pay.json +94 -0
  164. package/src/frames/official/shop-window.json +81 -0
  165. package/src/frames/official/sign-rider.json +81 -0
  166. package/src/frames/official/table-tent.json +75 -0
  167. package/src/frames/official/wifi-card.json +89 -0
  168. package/src/frames/official/winter.json +142 -0
  169. package/src/frames/official.ts +77 -0
  170. package/src/helpers.ts +168 -11
  171. package/src/index.tsx +141 -172
  172. package/src/logo-budget.ts +226 -0
  173. package/src/logo-cleanup.ts +146 -0
  174. package/src/logo.tsx +137 -0
  175. package/src/print.ts +794 -0
  176. package/src/qr-code-canvas.tsx +241 -0
  177. package/src/qr-code.tsx +51 -16
  178. package/src/qr-outline.ts +511 -0
  179. package/src/qr-shape.ts +255 -0
  180. package/src/scan-risk.ts +197 -0
  181. package/src/scan-score.ts +1629 -0
  182. package/src/shapes.ts +169 -0
  183. package/src/silhouette.ts +334 -0
  184. package/src/utils/color.ts +10 -1
  185. package/src/utils/fmt.ts +59 -0
  186. package/src/utils/paint.ts +317 -0
  187. package/src/utils/parse-color.ts +135 -0
  188. package/src/utils/path-data.ts +182 -0
  189. package/src/utils/png.ts +175 -0
  190. package/src/utils/polygon-clip.ts +163 -0
  191. package/src/utils/print-shared.ts +74 -0
  192. package/src/utils/svg-geometry.ts +838 -0
  193. package/src/utils/svg-gradient.ts +407 -0
  194. package/src/utils/svg-painter.ts +946 -0
  195. package/src/utils.tsx +794 -385
  196. package/types/art.ts +74 -0
  197. package/types/color.ts +132 -12
  198. package/types/corner-dot.ts +41 -4
  199. package/types/corner-eye.ts +31 -4
  200. package/types/frame-spec.ts +748 -0
  201. package/types/frame.ts +67 -0
  202. package/types/image-pattern.ts +15 -0
  203. package/types/index.ts +106 -4
  204. package/types/qr.ts +180 -2
  205. package/types/template.ts +2 -2
  206. package/dist/cli.js +0 -4319
@@ -0,0 +1,255 @@
1
+ /** biome-ignore-all lint/style/noMagicNumbers: disc geometry */
2
+ /** biome-ignore-all lint/suspicious/noBitwiseOperators: a 32-bit PRNG */
3
+ // Shaped codes (QRProps.shape): the real symbol sits in the middle of a
4
+ // disc ("circle"), a heart ("heart") or a silhouette of your own
5
+ // (QRSilhouette), keeps a light quiet ring of `margin` modules (at least
6
+ // QR_CIRCLE_QUIET_MIN), and the rest of the outline is filled with
7
+ // decorative modules on the same grid, so the code reads as one pattern.
8
+ // Decoders only ever see the symbol: the ring separates it from the noise.
9
+ import type { Modules } from "../types";
10
+ import {
11
+ QR_CIRCLE_QUIET_MIN,
12
+ type QRCodeShape,
13
+ type QRSilhouette,
14
+ } from "../types/qr";
15
+ import {
16
+ getHeartOutline,
17
+ getSilhouetteOutline,
18
+ insideTester,
19
+ type Outline,
20
+ outlinePath,
21
+ } from "./qr-outline";
22
+ import { circlePath, hashString } from "./shapes";
23
+
24
+ /** Decoration beyond the symbol's corners, in modules (on the diagonal). */
25
+ const CORNER_BAND = 1;
26
+ /** Light rim between the decoration and the disc's edge, in modules. */
27
+ const EDGE_INSET = 0.5;
28
+ /** Share of decorative cells that are dark (data is about half dark). */
29
+ const DENSITY = 0.5;
30
+ /**
31
+ * Room between a heart's or silhouette's quiet ring and its edge, in
32
+ * modules, where the ring meets the outline (at a heart's notch).
33
+ */
34
+ const OUTLINE_GAP = 0.5;
35
+
36
+ /** The light ring a shaped code keeps around its symbol, in modules. */
37
+ export function circleQuietZone(margin: number): number {
38
+ return Math.max(margin, QR_CIRCLE_QUIET_MIN);
39
+ }
40
+
41
+ /**
42
+ * The outline a heart or silhouette code is drawn in, or null for square
43
+ * and circle codes and for silhouettes that can't hold a code (those draw
44
+ * as square codes).
45
+ */
46
+ export function getShapeOutline(
47
+ shape: QRCodeShape | undefined
48
+ ): Outline | null {
49
+ if (shape === "heart") {
50
+ return getHeartOutline();
51
+ }
52
+ if (typeof shape === "object" && shape?.type === "silhouette") {
53
+ return getSilhouetteOutline(shape).outline;
54
+ }
55
+ return null;
56
+ }
57
+
58
+ /**
59
+ * True when the code is drawn in an outline (a circle, a heart or a
60
+ * silhouette that fits) rather than as a plain square.
61
+ */
62
+ export function isShapedCode(shape: QRCodeShape | undefined): boolean {
63
+ return shape === "circle" || getShapeOutline(shape) !== null;
64
+ }
65
+
66
+ /** Whether a silhouette can hold a code, and why not. */
67
+ export type QRSilhouetteFit =
68
+ | {
69
+ ok: true;
70
+ /** Share of the artwork's width the symbol and its quiet ring take */
71
+ share: number;
72
+ }
73
+ | {
74
+ ok: false;
75
+ reason: "invalid" | "empty" | "thin";
76
+ /** A sentence for people, e.g. for an upload control */
77
+ message: string;
78
+ };
79
+
80
+ const FIT_MESSAGES = {
81
+ invalid: "This silhouette isn't a valid shape grid.",
82
+ empty:
83
+ "No shape found: use a picture with a dark or opaque shape on a light or clear background.",
84
+ thin: "This shape is too thin to hold the code with its quiet zone. Try a rounder, fuller shape.",
85
+ } as const;
86
+
87
+ /**
88
+ * Whether `silhouette` is roomy enough for a code: the symbol and its quiet
89
+ * ring must take at least QR_SILHOUETTE_MIN_SHARE of the artwork's width.
90
+ * Silhouettes that fail draw as square codes; editors should refuse them
91
+ * with `message`.
92
+ */
93
+ export function getQRSilhouetteFit(silhouette: QRSilhouette): QRSilhouetteFit {
94
+ const { outline, problem } = getSilhouetteOutline(silhouette);
95
+ if (outline && !problem) {
96
+ return { ok: true, share: 2 * outline.half };
97
+ }
98
+ const reason = problem ?? "invalid";
99
+ return { ok: false, reason, message: FIT_MESSAGES[reason] };
100
+ }
101
+
102
+ /**
103
+ * Modules between the symbol and the edge of the artwork it is drawn in:
104
+ * `margin` for a square code; for a shaped one, enough for the symbol, its
105
+ * quiet ring and a band of decoration to fit inside the outline.
106
+ */
107
+ export function getLayoutMargin(
108
+ symbolModules: number,
109
+ margin: number,
110
+ shape: QRCodeShape | undefined
111
+ ): number {
112
+ if (shape === "circle") {
113
+ const quiet = circleQuietZone(margin);
114
+ const radius = (symbolModules / 2 + quiet) * Math.SQRT2 + CORNER_BAND;
115
+ return Math.ceil(radius - symbolModules / 2);
116
+ }
117
+ const outline = getShapeOutline(shape);
118
+ if (!outline) {
119
+ return margin;
120
+ }
121
+ // The centered square inside the outline holds the symbol, its quiet ring
122
+ // and the gap on each side.
123
+ const quiet = circleQuietZone(margin);
124
+ const across =
125
+ (symbolModules + 2 * (quiet + OUTLINE_GAP)) / (2 * outline.half);
126
+ return Math.ceil((across - symbolModules) / 2 - 1e-9);
127
+ }
128
+
129
+ /** Small deterministic PRNG (mulberry32), seeded per code. */
130
+ function random(seed: number) {
131
+ let state = seed >>> 0;
132
+ return () => {
133
+ state = (state + 0x6d_2b_79_f5) >>> 0;
134
+ let t = state;
135
+ t = Math.imul(t ^ (t >>> 15), t | 1);
136
+ t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
137
+ return ((t ^ (t >>> 14)) >>> 0) / 4_294_967_296;
138
+ };
139
+ }
140
+
141
+ /**
142
+ * The decorative modules of a circle code, on the whole layout grid
143
+ * (`symbolModules + 2 × layoutMargin` across): dark cells outside the
144
+ * symbol's quiet ring and inside the disc, the same for the same `value`.
145
+ */
146
+ export function circleDecoration(
147
+ value: string,
148
+ symbolModules: number,
149
+ margin: number
150
+ ): Modules {
151
+ const layout = getLayoutMargin(symbolModules, margin, "circle");
152
+ const quiet = circleQuietZone(margin);
153
+ const size = symbolModules + 2 * layout;
154
+ const center = size / 2;
155
+ const limit = center - EDGE_INSET - 0.5;
156
+ const next = random(Number.parseInt(hashString(value), 36) || 1);
157
+ const low = layout - quiet;
158
+ const high = layout + symbolModules + quiet;
159
+ return Array.from({ length: size }, (_, y) =>
160
+ Array.from({ length: size }, (_, x) => {
161
+ const dark = next() < DENSITY;
162
+ if (x >= low && x < high && y >= low && y < high) {
163
+ return false;
164
+ }
165
+ return dark && Math.hypot(x + 0.5 - center, y + 0.5 - center) <= limit;
166
+ })
167
+ );
168
+ }
169
+
170
+ /** Where a decorative module is checked, around its center (modules). */
171
+ const RIM_SAMPLES = Array.from({ length: 8 }, (_, i) => {
172
+ const angle = (i * Math.PI) / 4;
173
+ return [Math.cos(angle), Math.sin(angle)] as const;
174
+ });
175
+
176
+ /**
177
+ * The decorative modules of a code drawn in an outline (circle, heart or
178
+ * silhouette), on the whole layout grid, or null for a square code. Like a
179
+ * circle's: about half dark, seeded by `value`, outside the quiet ring and
180
+ * at least EDGE_INSET inside the outline.
181
+ */
182
+ export function shapeDecoration(
183
+ value: string,
184
+ symbolModules: number,
185
+ margin: number,
186
+ shape: QRCodeShape | undefined
187
+ ): Modules | null {
188
+ if (shape === "circle") {
189
+ return circleDecoration(value, symbolModules, margin);
190
+ }
191
+ const outline = getShapeOutline(shape);
192
+ if (!outline) {
193
+ return null;
194
+ }
195
+ const layout = getLayoutMargin(symbolModules, margin, shape);
196
+ const quiet = circleQuietZone(margin);
197
+ const size = symbolModules + 2 * layout;
198
+ const inside = insideTester(outline.polygons);
199
+ // A module and its rim (a disc of 0.5 + EDGE_INSET) must be inside
200
+ const reach = 0.5 + EDGE_INSET;
201
+ const fits = (x: number, y: number) => {
202
+ const cx = x + 0.5;
203
+ const cy = y + 0.5;
204
+ if (!inside(cx / size, cy / size)) return false;
205
+ return RIM_SAMPLES.every(([dx, dy]) =>
206
+ inside((cx + dx * reach) / size, (cy + dy * reach) / size)
207
+ );
208
+ };
209
+ const next = random(Number.parseInt(hashString(value), 36) || 1);
210
+ const low = layout - quiet;
211
+ const high = layout + symbolModules + quiet;
212
+ return Array.from({ length: size }, (_, y) =>
213
+ Array.from({ length: size }, (_, x) => {
214
+ const dark = next() < DENSITY;
215
+ if (x >= low && x < high && y >= low && y < high) {
216
+ return false;
217
+ }
218
+ return dark && fits(x, y);
219
+ })
220
+ );
221
+ }
222
+
223
+ /**
224
+ * The background outline of a shaped code drawn `size` px across: a disc
225
+ * (`{ circle: r }`), path data for a heart or silhouette, or null for a
226
+ * square code.
227
+ */
228
+ export function shapeBackground(
229
+ shape: QRCodeShape | undefined,
230
+ size: number
231
+ ): { circle: number } | { path: string } | null {
232
+ if (shape === "circle") {
233
+ return { circle: size / 2 };
234
+ }
235
+ const outline = getShapeOutline(shape);
236
+ return outline ? { path: outlinePath(outline, size) } : null;
237
+ }
238
+
239
+ /**
240
+ * Path data of a shaped code's outline drawn `size` px across, for editors
241
+ * that preview a shape on its own; null for a square code (or a silhouette
242
+ * that can't hold one, which draws square).
243
+ */
244
+ export function getQRShapePath(
245
+ shape: QRCodeShape | undefined,
246
+ size: number
247
+ ): string | null {
248
+ const background = shapeBackground(shape, size);
249
+ if (!background) {
250
+ return null;
251
+ }
252
+ return "circle" in background
253
+ ? circlePath(size / 2, size / 2, background.circle)
254
+ : background.path;
255
+ }
@@ -0,0 +1,197 @@
1
+ import type { QRProps } from "../types";
2
+ import type { CornerEyeDotPattern } from "../types/corner-dot";
3
+ import type { CornerEyePattern } from "../types/corner-eye";
4
+ import type { BodyPattern } from "../types/image-pattern";
5
+
6
+ // Style pairs that decoders miss, measured with jsQR through a phone-camera
7
+ // model (contrast loss + ¼-module blur) at 4–9 px per module; see
8
+ // tests/support/known-issues.ts. The shapes themselves are unchanged so
9
+ // existing codes keep their look; scan-score warns about the pairs instead.
10
+
11
+ /** Eye dots that break the finder's 1:1:3:1:1 ratio inside these eyes. */
12
+ export const WEAK_DOT_EYES: Partial<
13
+ Record<CornerEyeDotPattern, readonly CornerEyePattern[]>
14
+ > = {
15
+ // Not a 3×3 block: the ratio fails along the diagonal (jsQR, ZXing)
16
+ diamond: [
17
+ "square",
18
+ "rounded",
19
+ "diya",
20
+ "extra-rounded",
21
+ "message",
22
+ "pointy",
23
+ "leaf",
24
+ "leaf-reverse",
25
+ "drop",
26
+ "bevel",
27
+ "squircle",
28
+ "cushion",
29
+ "shield",
30
+ "petal",
31
+ ],
32
+ banner: ["square", "rounded", "diya", "leaf"],
33
+ // Curly frames' wavy hole meets these sharp-cornered dots under blur
34
+ square: ["curly"],
35
+ diya: ["curly"],
36
+ message: ["curly"],
37
+ };
38
+
39
+ /** Body patterns whose thin modules next to these eyes fail at 9 px/module. */
40
+ export const WEAK_BODY_EYES: Partial<
41
+ Record<BodyPattern, readonly CornerEyePattern[]>
42
+ > = {
43
+ diamond: ["square"],
44
+ pacman: ["rounded"],
45
+ "small-square": ["square", "rounded"],
46
+ };
47
+
48
+ /**
49
+ * Shapes that are fragile on their own, whatever they are paired with:
50
+ * circle and curly eyes read only in lenient decoders (scan-score's eye
51
+ * check), and these centres fail jsQR in sharp renders (screens, PNGs).
52
+ */
53
+ export const WEAK_EYES: readonly CornerEyePattern[] = ["circle", "curly"];
54
+ export const WEAK_DOTS: readonly CornerEyeDotPattern[] = [
55
+ "diamond",
56
+ "banner",
57
+ "star",
58
+ ];
59
+
60
+ // Sharp renders (screens, PNG exports, high-resolution prints), with no
61
+ // camera blur: a circle centre inside these eyes fails jsQR at some sizes,
62
+ // while zbar reads every one. The circle is a little short along the
63
+ // diagonal, so the finder's 1:1:3:1:1 cross-check fails inside rings with
64
+ // square corners; round rings (gear, circle, octagon) are unaffected.
65
+ // Measured on 3 values × 4–28 px per module (tests/decode-sharp-centre.test.ts).
66
+ // One decoder only, so these warn in the scan score; picking them is not
67
+ // blocked and no default changes until a phone check confirms it.
68
+
69
+ /** Eyes whose circle centre fails strict decoders in sharp renders. */
70
+ export const WEAK_SHARP_DOT_EYES: Partial<
71
+ Record<CornerEyeDotPattern, readonly CornerEyePattern[]>
72
+ > = {
73
+ circle: [
74
+ "square",
75
+ "rounded",
76
+ "diya",
77
+ "extra-rounded",
78
+ "message",
79
+ "pointy",
80
+ "leaf",
81
+ "leaf-reverse",
82
+ "drop",
83
+ "bevel",
84
+ "squircle",
85
+ "cushion",
86
+ "shield",
87
+ "petal",
88
+ ],
89
+ };
90
+
91
+ // Solid bodies leave jsQR enough margin to read anyway
92
+ const SOLID_BODIES = ["square", "liquid", "classy", "classy-rounded"] as const;
93
+
94
+ /**
95
+ * Bodies that read every sharp render of a weak pair above, keyed
96
+ * "centre/eye". Pairs not listed fail with every body.
97
+ */
98
+ export const SHARP_DOT_EYE_SAFE_BODIES: Partial<
99
+ Record<`${CornerEyeDotPattern}/${CornerEyePattern}`, readonly BodyPattern[]>
100
+ > = {
101
+ "circle/rounded": SOLID_BODIES,
102
+ "circle/extra-rounded": SOLID_BODIES,
103
+ "circle/message": [...SOLID_BODIES, "leaf"],
104
+ "circle/pointy": SOLID_BODIES,
105
+ "circle/leaf-reverse": SOLID_BODIES,
106
+ "circle/drop": SOLID_BODIES,
107
+ "circle/bevel": [...SOLID_BODIES, "leaf"],
108
+ "circle/squircle": [
109
+ ...SOLID_BODIES,
110
+ "circle",
111
+ "diamond",
112
+ "circle-mixed",
113
+ "rounded",
114
+ "horizontal-line",
115
+ "leaf",
116
+ "heart",
117
+ "mosaic",
118
+ "octagon",
119
+ ],
120
+ "circle/cushion": SOLID_BODIES,
121
+ "circle/shield": [...SOLID_BODIES, "rounded", "horizontal-line", "leaf"],
122
+ "circle/petal": SOLID_BODIES,
123
+ };
124
+
125
+ /** True when this centre, eye and body fail strict decoders in sharp renders. */
126
+ export function isWeakSharpPair(
127
+ dot: CornerEyeDotPattern,
128
+ eye: CornerEyePattern,
129
+ body: BodyPattern
130
+ ): boolean {
131
+ return (
132
+ (WEAK_SHARP_DOT_EYES[dot]?.includes(eye) ?? false) &&
133
+ !SHARP_DOT_EYE_SAFE_BODIES[`${dot}/${eye}`]?.includes(body)
134
+ );
135
+ }
136
+
137
+ export interface StylePairRisk {
138
+ /**
139
+ * A weak pair ("dot-eye", "body-eye"), a weak shape on its own, or a
140
+ * centre and eye that fail strict decoders in sharp renders only
141
+ * ("sharp-dot-eye": a warning; the editor doesn't block it).
142
+ */
143
+ kind: "dot-eye" | "body-eye" | "eye" | "dot" | "sharp-dot-eye";
144
+ message: string;
145
+ fix: string;
146
+ }
147
+
148
+ /**
149
+ * The known-weak style pair in `props`, if any. QRCodeSVG's defaults apply
150
+ * (circle body, gear eyes, circle dots).
151
+ */
152
+ export function getStylePairRisk(
153
+ props: Pick<
154
+ QRProps,
155
+ "bodyPattern" | "cornerEyePattern" | "cornerEyeDotPattern"
156
+ >
157
+ ): StylePairRisk | null {
158
+ const body = props.bodyPattern ?? "circle";
159
+ const eye = props.cornerEyePattern ?? "gear";
160
+ const dot = props.cornerEyeDotPattern ?? "circle";
161
+ if (WEAK_DOT_EYES[dot]?.includes(eye)) {
162
+ return {
163
+ kind: "dot-eye",
164
+ message: `${dot} centres in ${eye} eyes are missed by strict decoders`,
165
+ fix: "Pick a square, rounded or circle centre, or another eye shape",
166
+ };
167
+ }
168
+ if (WEAK_BODY_EYES[body]?.includes(eye)) {
169
+ return {
170
+ kind: "body-eye",
171
+ message: `${body} dots next to ${eye} eyes are missed up close`,
172
+ fix: "Pick a fuller dot shape or another eye shape",
173
+ };
174
+ }
175
+ if (WEAK_DOTS.includes(dot)) {
176
+ return {
177
+ kind: "dot",
178
+ message: `${dot} centres are missed in sharp renders and up close`,
179
+ fix: "Pick a square, rounded or circle centre",
180
+ };
181
+ }
182
+ if (WEAK_EYES.includes(eye)) {
183
+ return {
184
+ kind: "eye",
185
+ message: `${eye} eyes only read in lenient decoders`,
186
+ fix: "Pick square, rounded or gear eyes",
187
+ };
188
+ }
189
+ if (isWeakSharpPair(dot, eye, body)) {
190
+ return {
191
+ kind: "sharp-dot-eye",
192
+ message: `${dot} centres in ${eye} eyes are missed by some decoders on screens and in sharp prints`,
193
+ fix: "Pick a rounded centre, or gear or octagon eyes",
194
+ };
195
+ }
196
+ return null;
197
+ }