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,389 @@
1
+ /** biome-ignore-all lint/style/noMagicNumbers: module geometry */
2
+ import type { JSX } from "react";
3
+ import type { BodyPattern, Modules } from "../types";
4
+ import { MODULE_SCALE_MAX, MODULE_SCALE_MIN } from "../types/qr";
5
+ import {
6
+ circlePath,
7
+ hash2,
8
+ heartPath,
9
+ type Point,
10
+ polygonPath,
11
+ regularPolygon,
12
+ rotatePoints,
13
+ roundedRectPath,
14
+ starPoints,
15
+ } from "./shapes";
16
+ import { compact } from "./utils/fmt";
17
+
18
+ // Body patterns added after the original nine. Each one is drawn as a single
19
+ // <path> (all modules unioned under the nonzero rule), so neighbouring
20
+ // modules never show anti-aliasing seams and every output (SVG, PNG, PDF,
21
+ // EPS) paints the same outline. Alignment patterns are drawn as solid
22
+ // squares: decoders find them by their 1:1:1 rings, which shaped modules
23
+ // blur away.
24
+
25
+ export const SHAPED_BODY_PATTERNS = [
26
+ "horizontal-line",
27
+ "liquid",
28
+ "liquid-horizontal",
29
+ "liquid-vertical",
30
+ "classy",
31
+ "classy-rounded",
32
+ "leaf",
33
+ "plus",
34
+ "hexagon",
35
+ "star",
36
+ "heart",
37
+ "random-dots",
38
+ "mosaic",
39
+ "octagon",
40
+ ] as const satisfies readonly BodyPattern[];
41
+
42
+ type ShapedBodyPattern = (typeof SHAPED_BODY_PATTERNS)[number];
43
+
44
+ export function isShapedBodyPattern(
45
+ pattern: BodyPattern
46
+ ): pattern is ShapedBodyPattern {
47
+ return (SHAPED_BODY_PATTERNS as readonly string[]).includes(pattern);
48
+ }
49
+
50
+ /**
51
+ * Smallest `moduleScale` each pattern still decodes at (jsQR through the
52
+ * camera model at 4–9 px per module, dark and inverted), plus a 0.05 margin.
53
+ * Shapes that are already small barely shrink; unlisted patterns never do.
54
+ */
55
+ export const MODULE_SCALE_FLOOR: Partial<Record<BodyPattern, number>> = {
56
+ circle: 0.95,
57
+ "circle-large": 0.85,
58
+ square: 0.75,
59
+ diamond: 0.95,
60
+ pacman: 0.9,
61
+ rounded: 0.75,
62
+ "vertical-line": 0.8,
63
+ liquid: 0.75,
64
+ "liquid-horizontal": 0.85,
65
+ "liquid-vertical": 0.85,
66
+ classy: 0.7,
67
+ "classy-rounded": 0.7,
68
+ leaf: 0.75,
69
+ plus: 0.8,
70
+ hexagon: 0.75,
71
+ star: 0.85,
72
+ heart: 0.85,
73
+ "random-dots": 0.85,
74
+ mosaic: 0.8,
75
+ octagon: 0.8,
76
+ };
77
+
78
+ /**
79
+ * `moduleScale` clamped to the supported range (1 when unset) and, with a
80
+ * `pattern`, to the smallest size that pattern still scans at.
81
+ */
82
+ export function clampModuleScale(
83
+ scale: number | undefined,
84
+ pattern?: BodyPattern
85
+ ): number {
86
+ if (scale === undefined || !Number.isFinite(scale)) {
87
+ return 1;
88
+ }
89
+ const floor = pattern ? (MODULE_SCALE_FLOOR[pattern] ?? 1) : MODULE_SCALE_MIN;
90
+ return Math.min(MODULE_SCALE_MAX, Math.max(MODULE_SCALE_MIN, floor, scale));
91
+ }
92
+
93
+ function isFinder(x: number, y: number, size: number) {
94
+ return (
95
+ (x < 7 && y < 7) || (x >= size - 7 && y < 7) || (x < 7 && y >= size - 7)
96
+ );
97
+ }
98
+
99
+ /** Row/column centers of the alignment patterns (ISO/IEC 18004 Annex E). */
100
+ function alignmentPositions(version: number, size: number): number[] {
101
+ if (version === 1) {
102
+ return [];
103
+ }
104
+ const count = Math.floor(version / 7) + 2;
105
+ const step =
106
+ version === 32 ? 26 : Math.ceil((version * 4 + 4) / (count * 2 - 2)) * 2;
107
+ const result = [6];
108
+ for (let pos = size - 7; result.length < count; pos -= step) {
109
+ result.splice(1, 0, pos);
110
+ }
111
+ return result;
112
+ }
113
+
114
+ /** True for modules inside an alignment pattern (5×5, off the finders). */
115
+ export function alignmentMask(size: number): (x: number, y: number) => boolean {
116
+ const version = (size - 17) / 4;
117
+ const positions = Number.isInteger(version)
118
+ ? alignmentPositions(version, size)
119
+ : [];
120
+ const last = positions.length - 1;
121
+ const centers: Point[] = [];
122
+ for (const [i, cx] of positions.entries()) {
123
+ for (const [j, cy] of positions.entries()) {
124
+ if (
125
+ !(
126
+ (i === 0 && j === 0) ||
127
+ (i === 0 && j === last) ||
128
+ (i === last && j === 0)
129
+ )
130
+ ) {
131
+ centers.push([cx, cy]);
132
+ }
133
+ }
134
+ }
135
+ return (x, y) =>
136
+ centers.some(([ax, ay]) => Math.abs(x - ax) <= 2 && Math.abs(y - ay) <= 2);
137
+ }
138
+
139
+ type Corner = 0 | 1 | 2 | 3; // top-left, top-right, bottom-right, bottom-left
140
+
141
+ interface ConnectedStyle {
142
+ horizontal: boolean;
143
+ vertical: boolean;
144
+ /** Width of a line of modules, as a share of the module */
145
+ thickness: number;
146
+ /** Radius of an exposed corner, as a share of half the thickness */
147
+ radius: (corner: Corner) => number;
148
+ /** Fill inside corners of L-shaped runs (full-size modules only) */
149
+ fillets: boolean;
150
+ }
151
+
152
+ const CONNECTED: Partial<Record<ShapedBodyPattern, ConnectedStyle>> = {
153
+ liquid: {
154
+ horizontal: true,
155
+ vertical: true,
156
+ thickness: 1,
157
+ radius: () => 1,
158
+ fillets: true,
159
+ },
160
+ "liquid-horizontal": {
161
+ horizontal: true,
162
+ vertical: false,
163
+ thickness: 0.86,
164
+ radius: () => 1,
165
+ fillets: false,
166
+ },
167
+ "liquid-vertical": {
168
+ horizontal: false,
169
+ vertical: true,
170
+ thickness: 0.86,
171
+ radius: () => 1,
172
+ fillets: false,
173
+ },
174
+ classy: {
175
+ horizontal: true,
176
+ vertical: true,
177
+ thickness: 1,
178
+ radius: (corner) => (corner === 0 || corner === 2 ? 1 : 0),
179
+ fillets: false,
180
+ },
181
+ "classy-rounded": {
182
+ horizontal: true,
183
+ vertical: true,
184
+ thickness: 1,
185
+ radius: (corner) => (corner === 0 || corner === 2 ? 1 : 0.4),
186
+ fillets: false,
187
+ },
188
+ };
189
+
190
+ /** Outline of one module drawn on its own, centered on (cx, cy). */
191
+ function moduleShape(
192
+ pattern: ShapedBodyPattern,
193
+ cx: number,
194
+ cy: number,
195
+ unit: number,
196
+ x: number,
197
+ y: number
198
+ ): string {
199
+ switch (pattern) {
200
+ case "horizontal-line": {
201
+ // 0.86 of a module tall: thinner lines (like vertical-line's 0.71)
202
+ // fail more eye and dot pairs when the rows run along jsQR's scan
203
+ const h = unit * 0.43;
204
+ return roundedRectPath(cx - unit / 2, cy - h, unit, 2 * h);
205
+ }
206
+ case "leaf": {
207
+ // Full-size tiles: smaller petals drop out at 4 px per module
208
+ const h = unit * 0.5;
209
+ return roundedRectPath(cx - h, cy - h, 2 * h, 2 * h, [
210
+ unit * 0.4,
211
+ unit * 0.05,
212
+ unit * 0.4,
213
+ unit * 0.05,
214
+ ]);
215
+ }
216
+ case "plus": {
217
+ const a = unit * 0.22;
218
+ const l = unit * 0.5;
219
+ return polygonPath([
220
+ [cx - a, cy - l],
221
+ [cx + a, cy - l],
222
+ [cx + a, cy - a],
223
+ [cx + l, cy - a],
224
+ [cx + l, cy + a],
225
+ [cx + a, cy + a],
226
+ [cx + a, cy + l],
227
+ [cx - a, cy + l],
228
+ [cx - a, cy + a],
229
+ [cx - l, cy + a],
230
+ [cx - l, cy - a],
231
+ [cx - a, cy - a],
232
+ ]);
233
+ }
234
+ case "hexagon":
235
+ return polygonPath(regularPolygon(cx, cy, unit * 0.54, 6));
236
+ case "star":
237
+ return polygonPath(
238
+ starPoints(cx, cy + unit * 0.04, unit * 0.62, unit * 0.34)
239
+ );
240
+ case "heart":
241
+ return heartPath(cx, cy + unit * 0.02, unit * 1.08);
242
+ case "random-dots":
243
+ return circlePath(cx, cy, unit * (0.38 + 0.15 * hash2(x, y)));
244
+ case "mosaic": {
245
+ const h = (unit * (0.8 + 0.14 * hash2(x, y, 1))) / 2;
246
+ const corners: Point[] = [
247
+ [cx - h, cy - h],
248
+ [cx + h, cy - h],
249
+ [cx + h, cy + h],
250
+ [cx - h, cy + h],
251
+ ];
252
+ return polygonPath(
253
+ rotatePoints(corners, cx, cy, (hash2(x, y, 2) - 0.5) * 16)
254
+ );
255
+ }
256
+ case "octagon":
257
+ return polygonPath(
258
+ regularPolygon(cx, cy, (unit * 0.48) / Math.cos(Math.PI / 8), 8, 22.5)
259
+ );
260
+ default:
261
+ return circlePath(cx, cy, unit * 0.45);
262
+ }
263
+ }
264
+
265
+ /**
266
+ * Renders the data modules for one of the `SHAPED_BODY_PATTERNS`. `scale`
267
+ * shrinks each module (or, for connected styles, the line width) around its
268
+ * center; alignment patterns keep full-size modules.
269
+ */
270
+ export function renderShapedBody(
271
+ modules: Modules,
272
+ options: {
273
+ margin: number;
274
+ pixelSize: number;
275
+ pattern: ShapedBodyPattern;
276
+ scale?: number;
277
+ /** Decorative modules (a circle code's ring): no finders or alignment */
278
+ plain?: boolean;
279
+ }
280
+ ): JSX.Element[] {
281
+ const { margin, pixelSize: cell, pattern } = options;
282
+ const scale = clampModuleScale(options.scale, pattern);
283
+ const size = modules.length;
284
+ const isAlignment = options.plain ? () => false : alignmentMask(size);
285
+ const finder = (x: number, y: number) =>
286
+ !options.plain && isFinder(x, y, size);
287
+ const dark = (x: number, y: number) =>
288
+ x >= 0 &&
289
+ y >= 0 &&
290
+ x < size &&
291
+ y < size &&
292
+ Boolean(modules[y]?.[x]) &&
293
+ !finder(x, y);
294
+ const centerOf = (v: number) => (v + 0.5 + margin) * cell;
295
+
296
+ const solid: string[] = [];
297
+ const shapes: string[] = [];
298
+ const style = CONNECTED[pattern];
299
+
300
+ for (let y = 0; y < size; y++) {
301
+ for (let x = 0; x < size; x++) {
302
+ if (!dark(x, y)) {
303
+ continue;
304
+ }
305
+ const cx = centerOf(x);
306
+ const cy = centerOf(y);
307
+ if (isAlignment(x, y)) {
308
+ solid.push(
309
+ compact`M${cx - cell / 2} ${cy - cell / 2}h${cell}v${cell}h${-cell}z`
310
+ );
311
+ continue;
312
+ }
313
+ if (!style) {
314
+ shapes.push(moduleShape(pattern, cx, cy, cell * scale, x, y));
315
+ continue;
316
+ }
317
+ const h = (cell * style.thickness * scale) / 2;
318
+ const east = style.horizontal && dark(x + 1, y);
319
+ const west = style.horizontal && dark(x - 1, y);
320
+ const north = style.vertical && dark(x, y - 1);
321
+ const south = style.vertical && dark(x, y + 1);
322
+ const exposed = [
323
+ !(north || west),
324
+ !(north || east),
325
+ !(south || east),
326
+ !(south || west),
327
+ ];
328
+ const radii = exposed.map((open, corner) =>
329
+ open ? h * style.radius(corner as Corner) : 0
330
+ ) as [number, number, number, number];
331
+ shapes.push(roundedRectPath(cx - h, cy - h, 2 * h, 2 * h, radii));
332
+ // Bridges to the next module when lines are thinner than a module
333
+ if (2 * h < cell) {
334
+ if (east) {
335
+ shapes.push(roundedRectPath(cx, cy - h, cell, 2 * h));
336
+ }
337
+ if (south) {
338
+ shapes.push(roundedRectPath(cx - h, cy, 2 * h, cell));
339
+ }
340
+ }
341
+ }
342
+ }
343
+
344
+ // Smooth inside corners: a light module whose two neighbours and the
345
+ // diagonal between them are dark gets a small concave fillet.
346
+ if (style?.fillets && scale === 1) {
347
+ const r = cell * 0.3;
348
+ for (let y = 0; y < size; y++) {
349
+ for (let x = 0; x < size; x++) {
350
+ if (dark(x, y) || finder(x, y)) {
351
+ continue;
352
+ }
353
+ const x0 = centerOf(x) - cell / 2;
354
+ const y0 = centerOf(y) - cell / 2;
355
+ const x1 = x0 + cell;
356
+ const y1 = y0 + cell;
357
+ if (dark(x - 1, y) && dark(x, y - 1) && dark(x - 1, y - 1)) {
358
+ shapes.push(
359
+ compact`M${x0} ${y0}h${r}A${r} ${r} 0 0 0 ${x0} ${y0 + r}Z`
360
+ );
361
+ }
362
+ if (dark(x + 1, y) && dark(x, y - 1) && dark(x + 1, y - 1)) {
363
+ shapes.push(
364
+ compact`M${x1} ${y0}v${r}A${r} ${r} 0 0 0 ${x1 - r} ${y0}Z`
365
+ );
366
+ }
367
+ if (dark(x + 1, y) && dark(x, y + 1) && dark(x + 1, y + 1)) {
368
+ shapes.push(
369
+ compact`M${x1} ${y1}h${-r}A${r} ${r} 0 0 0 ${x1} ${y1 - r}Z`
370
+ );
371
+ }
372
+ if (dark(x - 1, y) && dark(x, y + 1) && dark(x - 1, y + 1)) {
373
+ shapes.push(
374
+ compact`M${x0} ${y1}v${-r}A${r} ${r} 0 0 0 ${x0 + r} ${y1}Z`
375
+ );
376
+ }
377
+ }
378
+ }
379
+ }
380
+
381
+ const elements: JSX.Element[] = [];
382
+ if (shapes.length > 0) {
383
+ elements.push(<path d={shapes.join("")} key="modules" />);
384
+ }
385
+ if (solid.length > 0) {
386
+ elements.push(<path d={solid.join("")} key="alignment" />);
387
+ }
388
+ return elements;
389
+ }
package/src/codegen.ts CHANGED
@@ -585,7 +585,8 @@ namespace qrcodegen {
585
585
  // Returns the number of data bits that can be stored in a QR Code of the given version number, after
586
586
  // all function modules are excluded. This includes remainder bits, so it might not be a multiple of 8.
587
587
  // The result is in the range [208, 29648]. This could be implemented as a 40-entry lookup table.
588
- private static getNumRawDataModules(ver: int): int {
588
+ // Modified: public so codeword-map.ts (and its tests) can read the layout
589
+ public static getNumRawDataModules(ver: int): int {
589
590
  if (ver < QrCode.MIN_VERSION || ver > QrCode.MAX_VERSION)
590
591
  throw new RangeError("Version number out of range");
591
592
  let result: int = (16 * ver + 128) * ver + 64;
@@ -611,7 +612,8 @@ namespace qrcodegen {
611
612
 
612
613
  // Returns a Reed-Solomon ECC generator polynomial for the given degree. This could be
613
614
  // implemented as a lookup table over all possible parameter values, instead of as an algorithm.
614
- private static reedSolomonComputeDivisor(degree: int): Array<byte> {
615
+ // Modified: public so codeword-map.ts (and its tests) can read the layout
616
+ public static reedSolomonComputeDivisor(degree: int): Array<byte> {
615
617
  if (degree < 1 || degree > 255)
616
618
  throw new RangeError("Degree out of range");
617
619
  // Polynomial coefficients are stored from highest to lowest power, excluding the leading term which is always 1.
@@ -636,7 +638,8 @@ namespace qrcodegen {
636
638
  }
637
639
 
638
640
  // Returns the Reed-Solomon error correction codeword for the given data and divisor polynomials.
639
- private static reedSolomonComputeRemainder(
641
+ // Modified: public so codeword-map.ts (and its tests) can read the layout
642
+ public static reedSolomonComputeRemainder(
640
643
  data: Readonly<Array<byte>>,
641
644
  divisor: Readonly<Array<byte>>,
642
645
  ): Array<byte> {
@@ -723,7 +726,8 @@ namespace qrcodegen {
723
726
  private static readonly PENALTY_N3: int = 40;
724
727
  private static readonly PENALTY_N4: int = 10;
725
728
 
726
- private static readonly ECC_CODEWORDS_PER_BLOCK: Array<Array<int>> = [
729
+ // Modified: public so codeword-map.ts (and its tests) can read the layout
730
+ public static readonly ECC_CODEWORDS_PER_BLOCK: Array<Array<int>> = [
727
731
  // Version: (note that index 0 is for padding, and is set to an illegal value)
728
732
  //0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 Error correction level
729
733
  [
@@ -748,7 +752,8 @@ namespace qrcodegen {
748
752
  ], // High
749
753
  ];
750
754
 
751
- private static readonly NUM_ERROR_CORRECTION_BLOCKS: Array<Array<int>> = [
755
+ // Modified: public so codeword-map.ts (and its tests) can read the layout
756
+ public static readonly NUM_ERROR_CORRECTION_BLOCKS: Array<Array<int>> = [
752
757
  // Version: (note that index 0 is for padding, and is set to an illegal value)
753
758
  //0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 Error correction level
754
759
  [
@@ -0,0 +1,265 @@
1
+ /** biome-ignore-all lint/style/noMagicNumbers: QR code structure (ISO/IEC 18004) */
2
+ /** biome-ignore-all lint/suspicious/noBitwiseOperators: codeword bit placement */
3
+ // Where every codeword of a QR code sits. qrcodegen splits the data into
4
+ // Reed–Solomon blocks, interleaves the blocks' codewords, and draws the
5
+ // result bit by bit in a two-column zigzag from the bottom-right corner,
6
+ // skipping function patterns (ISO/IEC 18004 §7.7). Anything drawn over the
7
+ // code, such as a badge plate or a logo, costs one error in every codeword it
8
+ // touches, and each block repairs only so many. This maps each module back to
9
+ // its (block, codeword) so that cost can be counted exactly instead of
10
+ // estimated from the covered area.
11
+ import type { ErrorLevel } from "../types";
12
+ import qrcodegen from "./codegen";
13
+ import { ERROR_LEVEL_MAP } from "./constants";
14
+
15
+ const { QrCode } = qrcodegen;
16
+
17
+ export interface CodewordMap {
18
+ version: number;
19
+ level: ErrorLevel;
20
+ /** Modules per side */
21
+ size: number;
22
+ /** Reed–Solomon blocks */
23
+ blocks: number;
24
+ /** Error correction codewords in each block */
25
+ eccPerBlock: number;
26
+ /** Codewords (data, then error correction) in each block */
27
+ blockLengths: number[];
28
+ /**
29
+ * Wrong codewords each block can repair: half its error correction
30
+ * codewords, less the few that ISO/IEC 18004 Table 9 keeps back for
31
+ * misdecode protection in versions 1–3.
32
+ */
33
+ correctable: number;
34
+ /** Per module (y × size + x): block of its codeword, -1 if it carries none */
35
+ block: Int16Array;
36
+ /** Per module: index of its codeword in that block, -1 if none */
37
+ codeword: Int16Array;
38
+ /** Per module: bit of that codeword (7 = most significant), -1 if none */
39
+ bit: Int8Array;
40
+ }
41
+
42
+ /**
43
+ * Codewords that versions 1–3 keep for misdecode protection, the "p" of
44
+ * ISO/IEC 18004 Table 9: those blocks repair (ecc − p) / 2 errors, not
45
+ * ecc / 2.
46
+ */
47
+ const MISDECODE_PROTECTION: Record<string, number> = {
48
+ "1L": 3,
49
+ "1M": 2,
50
+ "1Q": 1,
51
+ "1H": 1,
52
+ "2L": 2,
53
+ "3L": 1,
54
+ };
55
+
56
+ /** Function modules (finders, separators, timing, alignment, format, version). */
57
+ function functionModules(version: number): Uint8Array {
58
+ const size = version * 4 + 17;
59
+ const grid = new Uint8Array(size * size);
60
+ const mark = (x: number, y: number) => {
61
+ if (x >= 0 && y >= 0 && x < size && y < size) {
62
+ grid[y * size + x] = 1;
63
+ }
64
+ };
65
+ for (let i = 0; i < size; i++) {
66
+ mark(6, i);
67
+ mark(i, 6);
68
+ }
69
+ for (const [cx, cy] of [
70
+ [3, 3],
71
+ [size - 4, 3],
72
+ [3, size - 4],
73
+ ] as const) {
74
+ for (let dy = -4; dy <= 4; dy++) {
75
+ for (let dx = -4; dx <= 4; dx++) {
76
+ mark(cx + dx, cy + dy);
77
+ }
78
+ }
79
+ }
80
+ const positions: number[] = [];
81
+ if (version > 1) {
82
+ const count = Math.floor(version / 7) + 2;
83
+ const step =
84
+ version === 32 ? 26 : Math.ceil((version * 4 + 4) / (count * 2 - 2)) * 2;
85
+ positions.push(6);
86
+ for (let pos = size - 7; positions.length < count; pos -= step) {
87
+ positions.splice(1, 0, pos);
88
+ }
89
+ }
90
+ const last = positions.length - 1;
91
+ for (const [i, cx] of positions.entries()) {
92
+ for (const [j, cy] of positions.entries()) {
93
+ if (
94
+ (i === 0 && j === 0) ||
95
+ (i === 0 && j === last) ||
96
+ (i === last && j === 0)
97
+ ) {
98
+ continue;
99
+ }
100
+ for (let dy = -2; dy <= 2; dy++) {
101
+ for (let dx = -2; dx <= 2; dx++) {
102
+ mark(cx + dx, cy + dy);
103
+ }
104
+ }
105
+ }
106
+ }
107
+ // Format information, both copies, and the always-dark module
108
+ for (let i = 0; i <= 8; i++) {
109
+ mark(8, i);
110
+ mark(i, 8);
111
+ }
112
+ for (let i = 0; i < 8; i++) {
113
+ mark(size - 1 - i, 8);
114
+ mark(8, size - 1 - i);
115
+ }
116
+ if (version >= 7) {
117
+ for (let i = 0; i < 18; i++) {
118
+ const a = size - 11 + (i % 3);
119
+ const b = Math.floor(i / 3);
120
+ mark(a, b);
121
+ mark(b, a);
122
+ }
123
+ }
124
+ return grid;
125
+ }
126
+
127
+ function buildMap(version: number, level: ErrorLevel): CodewordMap {
128
+ const size = version * 4 + 17;
129
+ const ordinal = ERROR_LEVEL_MAP[level].ordinal;
130
+ const blocks = QrCode.NUM_ERROR_CORRECTION_BLOCKS[ordinal]?.[version] ?? 0;
131
+ const eccPerBlock = QrCode.ECC_CODEWORDS_PER_BLOCK[ordinal]?.[version] ?? 0;
132
+ const rawCodewords = Math.floor(QrCode.getNumRawDataModules(version) / 8);
133
+ const shortBlocks = blocks - (rawCodewords % blocks);
134
+ const shortLength = Math.floor(rawCodewords / blocks);
135
+ const blockLengths = Array.from(
136
+ { length: blocks },
137
+ (_, j) => shortLength + (j < shortBlocks ? 0 : 1)
138
+ );
139
+
140
+ // Interleaved order (qrcodegen's addEccAndInterleave): codeword i of every
141
+ // block in turn, where short blocks skip the slot after their data.
142
+ const streamBlock: number[] = [];
143
+ const streamCodeword: number[] = [];
144
+ const padSlot = shortLength - eccPerBlock;
145
+ for (let i = 0; i <= shortLength; i++) {
146
+ for (let j = 0; j < blocks; j++) {
147
+ if (j < shortBlocks) {
148
+ if (i === padSlot) {
149
+ continue;
150
+ }
151
+ streamBlock.push(j);
152
+ streamCodeword.push(i < padSlot ? i : i - 1);
153
+ } else {
154
+ streamBlock.push(j);
155
+ streamCodeword.push(i);
156
+ }
157
+ }
158
+ }
159
+ if (streamBlock.length !== rawCodewords) {
160
+ throw new Error(`Codeword map: interleave mismatch at version ${version}`);
161
+ }
162
+
163
+ // Zigzag placement (qrcodegen's drawCodewords)
164
+ const isFunction = functionModules(version);
165
+ const block = new Int16Array(size * size).fill(-1);
166
+ const codeword = new Int16Array(size * size).fill(-1);
167
+ const bit = new Int8Array(size * size).fill(-1);
168
+ const totalBits = rawCodewords * 8;
169
+ let index = 0;
170
+ for (let right = size - 1; right >= 1; right -= 2) {
171
+ if (right === 6) {
172
+ right = 5;
173
+ }
174
+ const upward = ((right + 1) & 2) === 0;
175
+ for (let vert = 0; vert < size; vert++) {
176
+ for (let j = 0; j < 2; j++) {
177
+ const x = right - j;
178
+ const y = upward ? size - 1 - vert : vert;
179
+ const m = y * size + x;
180
+ if (!isFunction[m] && index < totalBits) {
181
+ const stream = index >>> 3;
182
+ block[m] = streamBlock[stream] as number;
183
+ codeword[m] = streamCodeword[stream] as number;
184
+ bit[m] = 7 - (index & 7);
185
+ index++;
186
+ }
187
+ }
188
+ }
189
+ }
190
+ if (index !== totalBits) {
191
+ throw new Error(`Codeword map: placement mismatch at version ${version}`);
192
+ }
193
+
194
+ const protection = MISDECODE_PROTECTION[`${version}${level}`] ?? 0;
195
+ return {
196
+ version,
197
+ level,
198
+ size,
199
+ blocks,
200
+ eccPerBlock,
201
+ blockLengths,
202
+ correctable: Math.floor((eccPerBlock - protection) / 2),
203
+ block,
204
+ codeword,
205
+ bit,
206
+ };
207
+ }
208
+
209
+ const MAP_CACHE_LIMIT = 8;
210
+ const mapCache = new Map<string, CodewordMap>();
211
+
212
+ /**
213
+ * The (block, codeword, bit) every module of a version `version`, level
214
+ * `level` code carries. The mask doesn't move codewords, so one map serves
215
+ * all eight masks.
216
+ */
217
+ export function getCodewordMap(
218
+ version: number,
219
+ level: ErrorLevel
220
+ ): CodewordMap {
221
+ if (!(Number.isInteger(version) && version >= 1 && version <= 40)) {
222
+ throw new RangeError(`QR version must be 1–40, got ${version}`);
223
+ }
224
+ const key = `${version}${level}`;
225
+ const cached = mapCache.get(key);
226
+ if (cached) {
227
+ mapCache.delete(key);
228
+ mapCache.set(key, cached);
229
+ return cached;
230
+ }
231
+ const map = buildMap(version, level);
232
+ if (mapCache.size >= MAP_CACHE_LIMIT) {
233
+ const oldest = mapCache.keys().next().value;
234
+ if (oldest !== undefined) {
235
+ mapCache.delete(oldest);
236
+ }
237
+ }
238
+ mapCache.set(key, map);
239
+ return map;
240
+ }
241
+
242
+ /**
243
+ * Codewords per block that have at least one module among `modules`
244
+ * (indexes y × size + x). Each counts as one error: a covered module reads
245
+ * wrong about half the time, and one wrong bit spoils the whole codeword.
246
+ */
247
+ export function damagedCodewords(
248
+ map: CodewordMap,
249
+ modules: Iterable<number>
250
+ ): number[] {
251
+ const counts = new Array<number>(map.blocks).fill(0);
252
+ const seen = new Set<number>();
253
+ for (const m of modules) {
254
+ const b = map.block[m] ?? -1;
255
+ if (b < 0) {
256
+ continue;
257
+ }
258
+ const key = b * 4096 + (map.codeword[m] as number);
259
+ if (!seen.has(key)) {
260
+ seen.add(key);
261
+ counts[b] = (counts[b] as number) + 1;
262
+ }
263
+ }
264
+ return counts;
265
+ }