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.
Files changed (212) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +358 -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 +50 -6
  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 +25 -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/webp.d.ts +25 -0
  97. package/dist/src/utils/webp.d.ts.map +1 -0
  98. package/dist/src/utils.d.ts +18 -2
  99. package/dist/src/utils.d.ts.map +1 -1
  100. package/dist/src/webp.d.ts +19 -0
  101. package/dist/src/webp.d.ts.map +1 -0
  102. package/dist/types/art.d.ts +64 -0
  103. package/dist/types/art.d.ts.map +1 -0
  104. package/dist/types/color.d.ts +125 -7
  105. package/dist/types/color.d.ts.map +1 -1
  106. package/dist/types/corner-dot.d.ts +52 -2
  107. package/dist/types/corner-dot.d.ts.map +1 -1
  108. package/dist/types/corner-eye.d.ts +43 -2
  109. package/dist/types/corner-eye.d.ts.map +1 -1
  110. package/dist/types/frame-spec.d.ts +3509 -0
  111. package/dist/types/frame-spec.d.ts.map +1 -0
  112. package/dist/types/frame.d.ts +68 -0
  113. package/dist/types/frame.d.ts.map +1 -0
  114. package/dist/types/image-pattern.d.ts +15 -1
  115. package/dist/types/image-pattern.d.ts.map +1 -1
  116. package/dist/types/index.d.ts +15 -9
  117. package/dist/types/index.d.ts.map +1 -1
  118. package/dist/types/index.js +6 -0
  119. package/dist/types/index.js.map +1 -0
  120. package/dist/types/index.mjs +6 -0
  121. package/dist/types/index.mjs.map +1 -0
  122. package/dist/types/qr.d.ts +1684 -4
  123. package/dist/types/qr.d.ts.map +1 -1
  124. package/dist/types/template.d.ts +2 -2
  125. package/dist/types/template.d.ts.map +1 -1
  126. package/package.json +70 -25
  127. package/src/api.tsx +67 -17
  128. package/src/art-trace.ts +554 -0
  129. package/src/art.tsx +2226 -0
  130. package/src/body-patterns.tsx +389 -0
  131. package/src/codegen.ts +10 -5
  132. package/src/codeword-map.ts +265 -0
  133. package/src/constants.ts +4 -8
  134. package/src/corner-dot.tsx +209 -33
  135. package/src/corner-eye.ts +328 -60
  136. package/src/detection.ts +5 -11
  137. package/src/download.tsx +174 -70
  138. package/src/eps.ts +701 -0
  139. package/src/frame-icons.ts +325 -0
  140. package/src/frame-import.ts +1198 -0
  141. package/src/frame-spec.ts +518 -0
  142. package/src/frame-styles.ts +1095 -0
  143. package/src/frame.tsx +630 -0
  144. package/src/frames/builtin/banner-bottom.json +58 -0
  145. package/src/frames/builtin/circle.json +81 -0
  146. package/src/frames/builtin/rounded-border.json +60 -0
  147. package/src/frames/builtin/ticket.json +51 -0
  148. package/src/frames/builtin.ts +20 -0
  149. package/src/frames/official/attendance.json +110 -0
  150. package/src/frames/official/audio-guide.json +95 -0
  151. package/src/frames/official/book-now.json +103 -0
  152. package/src/frames/official/check-in.json +110 -0
  153. package/src/frames/official/christmas.json +131 -0
  154. package/src/frames/official/donate.json +77 -0
  155. package/src/frames/official/event-ticket.json +77 -0
  156. package/src/frames/official/feedback-bubble.json +73 -0
  157. package/src/frames/official/follow-us.json +83 -0
  158. package/src/frames/official/get-the-app.json +107 -0
  159. package/src/frames/official/halloween.json +80 -0
  160. package/src/frames/official/new-year.json +94 -0
  161. package/src/frames/official/order-at-table.json +86 -0
  162. package/src/frames/official/product-info.json +119 -0
  163. package/src/frames/official/register-product.json +77 -0
  164. package/src/frames/official/review-us.json +127 -0
  165. package/src/frames/official/rsvp-card.json +112 -0
  166. package/src/frames/official/save-contact.json +117 -0
  167. package/src/frames/official/scan-to-pay.json +94 -0
  168. package/src/frames/official/shop-window.json +81 -0
  169. package/src/frames/official/sign-rider.json +81 -0
  170. package/src/frames/official/table-tent.json +75 -0
  171. package/src/frames/official/wifi-card.json +89 -0
  172. package/src/frames/official/winter.json +142 -0
  173. package/src/frames/official.ts +77 -0
  174. package/src/helpers.ts +168 -11
  175. package/src/index.tsx +150 -172
  176. package/src/logo-budget.ts +226 -0
  177. package/src/logo-cleanup.ts +146 -0
  178. package/src/logo.tsx +137 -0
  179. package/src/print.ts +794 -0
  180. package/src/qr-code-canvas.tsx +241 -0
  181. package/src/qr-code.tsx +51 -16
  182. package/src/qr-outline.ts +511 -0
  183. package/src/qr-shape.ts +255 -0
  184. package/src/scan-risk.ts +197 -0
  185. package/src/scan-score.ts +1629 -0
  186. package/src/shapes.ts +169 -0
  187. package/src/silhouette.ts +334 -0
  188. package/src/utils/color.ts +10 -1
  189. package/src/utils/fmt.ts +59 -0
  190. package/src/utils/paint.ts +317 -0
  191. package/src/utils/parse-color.ts +135 -0
  192. package/src/utils/path-data.ts +182 -0
  193. package/src/utils/png.ts +175 -0
  194. package/src/utils/polygon-clip.ts +163 -0
  195. package/src/utils/print-shared.ts +74 -0
  196. package/src/utils/svg-geometry.ts +838 -0
  197. package/src/utils/svg-gradient.ts +407 -0
  198. package/src/utils/svg-painter.ts +946 -0
  199. package/src/utils/webp.ts +556 -0
  200. package/src/utils.tsx +794 -385
  201. package/src/webp.ts +84 -0
  202. package/types/art.ts +74 -0
  203. package/types/color.ts +132 -12
  204. package/types/corner-dot.ts +41 -4
  205. package/types/corner-eye.ts +31 -4
  206. package/types/frame-spec.ts +748 -0
  207. package/types/frame.ts +67 -0
  208. package/types/image-pattern.ts +15 -0
  209. package/types/index.ts +106 -4
  210. package/types/qr.ts +180 -2
  211. package/types/template.ts +2 -2
  212. package/dist/cli.js +0 -4319
package/src/utils.tsx CHANGED
@@ -1,17 +1,33 @@
1
1
  import type { JSX } from "react";
2
2
  import React from "react";
3
- import type { TemplateConfig } from "@/types/template";
4
3
  import type {
4
+ AngularGradient,
5
5
  BodyPattern,
6
6
  ColorConfig,
7
+ ErrorLevel,
7
8
  Excavation,
9
+ GradientStop,
10
+ ImagePaint,
8
11
  ImageSettings,
9
12
  LinearGradient,
10
13
  Modules,
11
14
  QRPropsSVG,
12
15
  RadialGradient,
16
+ TemplateConfig,
13
17
  } from "../types";
14
- import { normalizeColorConfig } from "../types/color";
18
+ import { isGradient, isImagePaint, normalizeColorConfig } from "../types/color";
19
+ import {
20
+ AUTO_LEVEL_LOGO_COVERAGE,
21
+ type EyePosition,
22
+ type QRCodeShape,
23
+ } from "../types/qr";
24
+ import { getQRArtEyeColors, isQRArtActive, renderQRArt } from "./art";
25
+ import {
26
+ alignmentMask,
27
+ clampModuleScale,
28
+ isShapedBodyPattern,
29
+ renderShapedBody,
30
+ } from "./body-patterns";
15
31
  import qrcodegen from "./codegen";
16
32
  import {
17
33
  DEFAULT_BGCOLOR,
@@ -24,8 +40,16 @@ import {
24
40
  } from "./constants";
25
41
  import { generateCornerDotPath } from "./corner-dot";
26
42
  import { generateCornerSquarePath } from "./corner-eye";
43
+ import { isFrameActive, wrapWithFrame } from "./frame";
44
+ import { LOGO_PLATE_INSET, LOGO_ROUNDED_RADIUS, renderLogo } from "./logo";
45
+ import { capImageSettings } from "./logo-budget";
46
+ import { getLayoutMargin, shapeBackground, shapeDecoration } from "./qr-shape";
47
+ import { hashString } from "./shapes";
27
48
  import { getTemplate } from "./templates";
28
49
  import { getSolidColor } from "./utils/color";
50
+ import { compact, fmt, SCALE_DECIMALS } from "./utils/fmt";
51
+ import { conicWedges, splitAlpha } from "./utils/paint";
52
+ import { parseColor } from "./utils/parse-color";
29
53
 
30
54
  // We could just do this in generatePath, except that we want to support
31
55
  // non-Path2D canvas, so we need to keep it an explicit step.
@@ -55,7 +79,7 @@ export function generatePath(modules: Modules, margin = 0): string {
55
79
  // M0 0h7v1H0z injects the space with the move and drops the comma,
56
80
  // saving a char per operation
57
81
  ops.push(
58
- `M${start + margin} ${y + margin}h${x - start}v1H${start + margin}z`
82
+ compact`M${start + margin} ${y + margin}h${x - start}v1H${start + margin}z`
59
83
  );
60
84
  start = null;
61
85
  return;
@@ -70,11 +94,11 @@ export function generatePath(modules: Modules, margin = 0): string {
70
94
  }
71
95
  if (start === null) {
72
96
  // Just a single dark module.
73
- ops.push(`M${x + margin},${y + margin} h1v1H${x + margin}z`);
97
+ ops.push(compact`M${x + margin},${y + margin} h1v1H${x + margin}z`);
74
98
  } else {
75
99
  // Otherwise finish the current line.
76
100
  ops.push(
77
- `M${start + margin},${y + margin} h${x + 1 - start}v1H${
101
+ compact`M${start + margin},${y + margin} h${x + 1 - start}v1H${
78
102
  start + margin
79
103
  }z`
80
104
  );
@@ -107,6 +131,10 @@ function isCornerModule(x: number, y: number, size: number): boolean {
107
131
  return false;
108
132
  }
109
133
 
134
+ // Radius factor of circle-mixed's smallest dots (× 0.45 modules). It was 0.5
135
+ // (0.225 modules), which camera blur erased; 0.8 still fails with some eyes.
136
+ const CIRCLE_MIXED_SMALL = 0.9;
137
+
110
138
  // Generate circles for data modules with different patterns
111
139
  function generateDataCircles(
112
140
  modules: Modules,
@@ -114,30 +142,61 @@ function generateDataCircles(
114
142
  margin: number;
115
143
  pixelSize: number;
116
144
  pattern?: BodyPattern;
145
+ /** Size of each module, 0.5 to 1 (see `moduleScale`) */
146
+ scale?: number;
147
+ /** Decorative modules (a circle code's ring): no finders or alignment */
148
+ plain?: boolean;
117
149
  }
118
150
  ): JSX.Element[] {
119
- const { margin, pixelSize, pattern = "circle" } = options;
151
+ const { margin, pixelSize, pattern = "circle", plain = false } = options;
152
+ if (isShapedBodyPattern(pattern)) {
153
+ return renderShapedBody(modules, {
154
+ margin,
155
+ pixelSize,
156
+ pattern,
157
+ scale: options.scale,
158
+ plain,
159
+ });
160
+ }
161
+ const scale = clampModuleScale(options.scale, pattern);
120
162
  const shapes: JSX.Element[] = [];
121
163
  const qrSize = modules.length;
122
164
  const radius = pixelSize * 0.45; // radius for circles (≥0.45 needed for reliable scanning)
165
+ const isAlignmentModule = plain ? () => false : alignmentMask(qrSize);
123
166
 
124
167
  modules.forEach((row, y) => {
125
168
  row.forEach((cell, x) => {
126
- if (cell && !isCornerModule(x, y, qrSize)) {
169
+ if (cell && (plain || !isCornerModule(x, y, qrSize))) {
127
170
  const cx = (x + 0.5 + margin) * pixelSize;
128
171
  const cy = (y + 0.5 + margin) * pixelSize;
129
172
  const key = `${x}-${y}`;
173
+ const start = shapes.length;
174
+
175
+ // Alignment patterns stay solid in every style: jsQR and ZXing find
176
+ // them by their 1:1:1 dark/light rings, which dots and gaps break.
177
+ if (isAlignmentModule(x, y)) {
178
+ shapes.push(
179
+ <rect
180
+ height={fmt(pixelSize)}
181
+ key={key}
182
+ width={fmt(pixelSize)}
183
+ x={fmt(cx - pixelSize / 2)}
184
+ y={fmt(cy - pixelSize / 2)}
185
+ />
186
+ );
187
+ return;
188
+ }
130
189
 
131
190
  switch (pattern) {
132
191
  case "circle":
133
192
  // Pattern 1: Standard circles with gaps
134
193
  shapes.push(
135
194
  <circle
136
- cx={cx}
137
- cy={cy}
195
+ cx={fmt(cx)}
196
+ cy={fmt(cy)}
138
197
  key={key}
139
- r={radius}
140
- transform={`rotate(0,${cx},${cy})`}
198
+ r={fmt(radius)}
199
+ transform={compact`rotate(0,${cx},${cy})`}
141
200
  />
142
201
  );
143
202
  break;
@@ -148,11 +207,11 @@ function generateDataCircles(
148
207
  const largeRadius = pixelSize * 0.5; // 50% of pixel size for overlapping circles
149
208
  shapes.push(
150
209
  <circle
151
- cx={cx}
152
- cy={cy}
210
+ cx={fmt(cx)}
211
+ cy={fmt(cy)}
153
212
  key={key}
154
- r={largeRadius}
155
- transform={`rotate(0,${cx},${cy})`}
213
+ r={fmt(largeRadius)}
214
+ transform={compact`rotate(0,${cx},${cy})`}
156
215
  />
157
216
  );
158
217
  }
@@ -164,9 +223,9 @@ function generateDataCircles(
164
223
  const halfSize = pixelSize * 0.357_143; // half of 71.4% pixel size
165
224
  shapes.push(
166
225
  <path
167
- d={`M ${cx - halfSize} ${cy - halfSize}v ${pixelSize * 0.714_286}h ${pixelSize * 0.714_286}v -${pixelSize * 0.714_286}z`}
226
+ d={compact`M ${cx - halfSize} ${cy - halfSize}v ${pixelSize * 0.714_286}h ${pixelSize * 0.714_286}v -${pixelSize * 0.714_286}z`}
168
227
  key={key}
169
- transform={`rotate(0,${cx},${cy})`}
228
+ transform={compact`rotate(0,${cx},${cy})`}
170
229
  />
171
230
  );
172
231
  }
@@ -178,9 +237,9 @@ function generateDataCircles(
178
237
  const halfSize = pixelSize * 0.357_143;
179
238
  shapes.push(
180
239
  <path
181
- d={`M ${cx - halfSize} ${cy - halfSize}v ${pixelSize * 0.714_286}h ${pixelSize * 0.714_286}v -${pixelSize * 0.714_286}z`}
240
+ d={compact`M ${cx - halfSize} ${cy - halfSize}v ${pixelSize * 0.714_286}h ${pixelSize * 0.714_286}v -${pixelSize * 0.714_286}z`}
182
241
  key={key}
183
- transform={`rotate(45,${cx},${cy})`}
242
+ transform={compact`rotate(45,${cx},${cy})`}
184
243
  />
185
244
  );
186
245
  }
@@ -190,15 +249,19 @@ function generateDataCircles(
190
249
  // Pattern 4: Circles with varying sizes
191
250
  {
192
251
  const randomFactor =
193
- (x + y) % 3 === 0 ? 0.5 : (x + y) % 2 === 0 ? 1 : 1.25;
252
+ (x + y) % 3 === 0
253
+ ? CIRCLE_MIXED_SMALL
254
+ : (x + y) % 2 === 0
255
+ ? 1
256
+ : 1.25;
194
257
  const variedRadius = radius * randomFactor;
195
258
  shapes.push(
196
259
  <circle
197
- cx={cx}
198
- cy={cy}
260
+ cx={fmt(cx)}
261
+ cy={fmt(cy)}
199
262
  key={key}
200
- r={variedRadius}
201
- transform={`rotate(0,${cx},${cy})`}
263
+ r={fmt(variedRadius)}
264
+ transform={compact`rotate(0,${cx},${cy})`}
202
265
  />
203
266
  );
204
267
  }
@@ -211,9 +274,9 @@ function generateDataCircles(
211
274
  const halfHeight = pixelSize * 0.357_143;
212
275
  shapes.push(
213
276
  <path
214
- d={`M ${cx - halfWidth} ${cy - halfHeight}v ${pixelSize * 0.714_286}h ${halfWidth}a ${halfWidth * 0.5} ${halfWidth * 0.5}, 0, 0, 0, 0 -${pixelSize * 0.714_286}h -${halfWidth}v -${pixelSize * 0.714_286}z`}
277
+ d={compact`M ${cx - halfWidth} ${cy - halfHeight}v ${pixelSize * 0.714_286}h ${halfWidth}a ${halfWidth * 0.5} ${halfWidth * 0.5}, 0, 0, 0, 0 -${pixelSize * 0.714_286}h -${halfWidth}v -${pixelSize * 0.714_286}z`}
215
278
  key={key}
216
- transform={`rotate(${((x + y) % 2) === 0 ? 0 : 90},${cx},${cy})`}
279
+ transform={compact`rotate(${((x + y) % 2) === 0 ? 0 : 90},${cx},${cy})`}
217
280
  />
218
281
  );
219
282
  }
@@ -226,14 +289,14 @@ function generateDataCircles(
226
289
  const cornerRadius = pixelSize * 0.2; // 20% radius for smooth corners
227
290
  shapes.push(
228
291
  <rect
229
- height={pixelSize}
292
+ height={fmt(pixelSize)}
230
293
  key={key}
231
- rx={cornerRadius}
232
- ry={cornerRadius}
233
- transform={`rotate(0,${cx},${cy})`}
234
- width={pixelSize}
235
- x={cx - halfSize}
236
- y={cy - halfSize}
294
+ rx={fmt(cornerRadius)}
295
+ ry={fmt(cornerRadius)}
296
+ transform={compact`rotate(0,${cx},${cy})`}
297
+ width={fmt(pixelSize)}
298
+ x={fmt(cx - halfSize)}
299
+ y={fmt(cy - halfSize)}
237
300
  />
238
301
  );
239
302
  }
@@ -245,12 +308,12 @@ function generateDataCircles(
245
308
  const halfSize = pixelSize * 0.5;
246
309
  shapes.push(
247
310
  <rect
248
- height={pixelSize}
311
+ height={fmt(pixelSize)}
249
312
  key={key}
250
- transform={`rotate(0,${cx},${cy})`}
251
- width={pixelSize}
252
- x={cx - halfSize}
253
- y={cy - halfSize}
313
+ transform={compact`rotate(0,${cx},${cy})`}
314
+ width={fmt(pixelSize)}
315
+ x={fmt(cx - halfSize)}
316
+ y={fmt(cy - halfSize)}
254
317
  />
255
318
  );
256
319
  }
@@ -263,9 +326,9 @@ function generateDataCircles(
263
326
  const lineWidth = pixelSize; // full width
264
327
  shapes.push(
265
328
  <path
266
- d={`M ${cx - pixelSize * 0.5} ${cy - lineHeight * 0.5}v ${lineHeight}h ${lineWidth}v -${lineHeight}z`}
329
+ d={compact`M ${cx - pixelSize * 0.5} ${cy - lineHeight * 0.5}v ${lineHeight}h ${lineWidth}v -${lineHeight}z`}
267
330
  key={key}
268
- transform={`rotate(90,${cx},${cy})`}
331
+ transform={compact`rotate(90,${cx},${cy})`}
269
332
  />
270
333
  );
271
334
  }
@@ -275,14 +338,27 @@ function generateDataCircles(
275
338
  // Default to circle
276
339
  shapes.push(
277
340
  <circle
278
- cx={cx}
279
- cy={cy}
341
+ cx={fmt(cx)}
342
+ cy={fmt(cy)}
280
343
  key={key}
281
- r={radius}
282
- transform={`rotate(0,${cx},${cy})`}
344
+ r={fmt(radius)}
345
+ transform={compact`rotate(0,${cx},${cy})`}
283
346
  />
284
347
  );
285
348
  }
349
+
350
+ // A smaller moduleScale shrinks the shape around its center
351
+ if (scale !== 1 && shapes.length > start) {
352
+ const shape = shapes.pop() as JSX.Element;
353
+ shapes.push(
354
+ <g
355
+ key={key}
356
+ transform={compact`translate(${cx},${cy}) scale(${fmt(scale, SCALE_DECIMALS)}) translate(${-cx},${-cy})`}
357
+ >
358
+ {shape}
359
+ </g>
360
+ );
361
+ }
286
362
  }
287
363
  });
288
364
  });
@@ -314,15 +390,14 @@ function calculateGradientCoordinates(
314
390
  return { x1, y1, x2, y2 };
315
391
  }
316
392
 
317
- // Generate unique gradient ID
318
- let gradientIdCounter = 0;
319
- function generateGradientId(prefix: string): string {
320
- gradientIdCounter += 1;
321
- return `${prefix}-${Date.now()}-${gradientIdCounter}`;
393
+ // Gradient ids are derived from the gradient itself, so the server and the
394
+ // client render identical markup (hydration-safe). Two codes on one page
395
+ // that share an id also share an identical gradient.
396
+ function generateGradientId(prefix: string, seed: unknown): string {
397
+ return `${prefix}-${hashString(JSON.stringify(seed))}`;
322
398
  }
323
399
 
324
400
  // Helper to calculate gradient coordinates for a corner based on corner position
325
- /** biome-ignore lint: Function needs multiple parameters for corner-specific gradients */
326
401
  function calculateCornerGradientCoordinates(
327
402
  angle: number,
328
403
  cornerX: number,
@@ -368,15 +443,152 @@ function calculateCornerGradientCoordinates(
368
443
  return { x1, y1, x2, y2 };
369
444
  }
370
445
 
446
+ /** `<stop>`s for a gradient; a stop with alpha gets a `stop-opacity`. */
447
+ function renderStops(id: string, stops: GradientStop[]) {
448
+ return stops.map((stop, index) => {
449
+ const { color, opacity } = splitAlpha(stop.color);
450
+ return (
451
+ <stop
452
+ key={`${id}-stop-${index}`}
453
+ offset={`${stop.offset}%`}
454
+ stopColor={color}
455
+ stopOpacity={opacity}
456
+ />
457
+ );
458
+ });
459
+ }
460
+
461
+ /**
462
+ * An angular (conic) gradient as a `<pattern>`: SVG has no conic gradient,
463
+ * so the sweep is a fan of thin wedges around (`cx`, `cy`) reaching
464
+ * `radius` (see conicWedges). The tile covers the whole code (0 to `size`),
465
+ * so shapes see one tile. `data-qrdx-conic` and each wedge's `data-arc` let
466
+ * the PDF and EPS exporters paint only the wedges a shape needs.
467
+ */
468
+ function renderConicPattern(
469
+ id: string,
470
+ gradient: AngularGradient,
471
+ cx: number,
472
+ cy: number,
473
+ radius: number,
474
+ size: number
475
+ ): JSX.Element {
476
+ const fan = conicWedges(gradient.stops, gradient.angle ?? 0, cx, cy, radius);
477
+ const wedges = fan.wedges.map((wedge, index) => (
478
+ <path
479
+ d={wedge.d}
480
+ data-arc={`${wedge.from} ${wedge.sweep}`}
481
+ fill={wedge.color}
482
+ fillOpacity={wedge.opacity}
483
+ key={`${id}-w-${index}`}
484
+ shapeRendering={wedge.opacity === undefined ? undefined : "crispEdges"}
485
+ />
486
+ ));
487
+ return (
488
+ <pattern
489
+ data-qrdx-conic={`${fmt(cx)} ${fmt(cy)}`}
490
+ height={fmt(size)}
491
+ id={id}
492
+ key={id}
493
+ patternUnits="userSpaceOnUse"
494
+ width={fmt(size)}
495
+ x={0}
496
+ y={0}
497
+ >
498
+ {/* One alpha for the whole fan: a group, so overlaps don't add up */}
499
+ {fan.opacity === undefined ? (
500
+ wedges
501
+ ) : (
502
+ <g opacity={fan.opacity}>{wedges}</g>
503
+ )}
504
+ </pattern>
505
+ );
506
+ }
507
+
508
+ /** Horizontal and vertical anchors (0, ½, 1) of an image fill's position. */
509
+ function imageAnchor(position: ImagePaint["position"] = "center") {
510
+ const [first, second] = position.split("-") as [string, string | undefined];
511
+ const vertical = position === "center" ? "center" : first;
512
+ const horizontal =
513
+ second ?? (first === "left" || first === "right" ? first : "center");
514
+ const x = horizontal === "left" ? 0 : horizontal === "right" ? 1 : 0.5;
515
+ const y = vertical === "top" ? 0 : vertical === "bottom" ? 1 : 0.5;
516
+ return { x, y };
517
+ }
518
+
519
+ const ALIGN_X = ["xMin", "xMid", "xMax"] as const;
520
+ const ALIGN_Y = ["YMin", "YMid", "YMax"] as const;
521
+
522
+ /**
523
+ * An image fill as a `<pattern>` over the whole code (0 to `size`), so the
524
+ * dots, the eyes and the background that share a picture show one picture
525
+ * (and one copy of it). Cover and contain draw the image once, `scale` times
526
+ * the code's size around `position`; tile repeats it in squares a quarter of
527
+ * the code wide at scale 1. The underlay is a rect under the image; opacity
528
+ * is the image's. PDF export paints the same pattern inside each shape.
529
+ */
530
+ function renderImagePattern(
531
+ id: string,
532
+ paint: ImagePaint,
533
+ size: number
534
+ ): JSX.Element {
535
+ const scale = paint.scale ?? 1;
536
+ const tile = paint.fit === "tile";
537
+ const side = tile ? (size / 4) * scale : size;
538
+ const underlay = paint.underlay ? splitAlpha(paint.underlay) : null;
539
+ const width = tile ? side : size * scale;
540
+ const anchor = imageAnchor(paint.position);
541
+ const x = tile ? 0 : anchor.x * (size - width);
542
+ const y = tile ? 0 : anchor.y * (size - width);
543
+ const align = tile
544
+ ? "xMidYMid slice"
545
+ : `${ALIGN_X[anchor.x * 2] ?? "xMid"}${ALIGN_Y[anchor.y * 2] ?? "YMid"} ${paint.fit === "contain" ? "meet" : "slice"}`;
546
+ return (
547
+ <pattern
548
+ height={fmt(side)}
549
+ id={id}
550
+ key={id}
551
+ patternUnits="userSpaceOnUse"
552
+ width={fmt(side)}
553
+ x={0}
554
+ y={0}
555
+ >
556
+ {underlay && (
557
+ <rect
558
+ fill={underlay.color}
559
+ fillOpacity={underlay.opacity}
560
+ height={fmt(side)}
561
+ width={fmt(side)}
562
+ x={0}
563
+ y={0}
564
+ />
565
+ )}
566
+ <image
567
+ height={fmt(width)}
568
+ href={paint.src}
569
+ opacity={(paint.opacity ?? 1) < 1 ? paint.opacity : undefined}
570
+ preserveAspectRatio={align}
571
+ width={fmt(width)}
572
+ x={fmt(x)}
573
+ y={fmt(y)}
574
+ />
575
+ </pattern>
576
+ );
577
+ }
578
+
579
+ /** Pattern id of an image fill: the same picture shares one pattern. */
580
+ const imagePatternId = (paint: ImagePaint, size: number) =>
581
+ generateGradientId("img", [paint, size]);
582
+
371
583
  // Generate gradient definition elements for corners
372
- // biome-ignore lint: Function needs multiple parameters for corner-specific gradients
373
584
  function generateCornerGradientDef(
374
585
  colorConfig: ColorConfig,
375
586
  cornerX: number,
376
587
  cornerY: number,
377
588
  cornerSize: number,
378
589
  cornerType: "top-left" | "top-right" | "bottom-left",
379
- idPrefix: string
590
+ idPrefix: string,
591
+ size: number
380
592
  ): { id: string; element: JSX.Element } | null {
381
593
  const normalized = normalizeColorConfig(colorConfig);
382
594
 
@@ -384,7 +596,21 @@ function generateCornerGradientDef(
384
596
  return null;
385
597
  }
386
598
 
387
- const id = generateGradientId(idPrefix);
599
+ if (normalized.type === "image") {
600
+ // Laid over the whole code, like the dots, so eyes and dots that share
601
+ // a picture show one picture
602
+ const id = imagePatternId(normalized, size);
603
+ return { id, element: renderImagePattern(id, normalized, size) };
604
+ }
605
+
606
+ const id = generateGradientId(idPrefix, [
607
+ normalized,
608
+ cornerX,
609
+ cornerY,
610
+ cornerSize,
611
+ cornerType,
612
+ ...(normalized.type === "angular" ? [size] : []),
613
+ ]);
388
614
 
389
615
  if (normalized.type === "linear") {
390
616
  const gradient = normalized as LinearGradient;
@@ -404,18 +630,12 @@ function generateCornerGradientDef(
404
630
  gradientUnits="userSpaceOnUse"
405
631
  id={id}
406
632
  key={id}
407
- x1={x1}
408
- x2={x2}
409
- y1={y1}
410
- y2={y2}
633
+ x1={fmt(x1)}
634
+ x2={fmt(x2)}
635
+ y1={fmt(y1)}
636
+ y2={fmt(y2)}
411
637
  >
412
- {gradient.stops.map((stop, index) => (
413
- <stop
414
- key={`${id}-stop-${index}`}
415
- offset={`${stop.offset}%`}
416
- stopColor={stop.color}
417
- />
418
- ))}
638
+ {renderStops(id, gradient.stops)}
419
639
  </linearGradient>
420
640
  ),
421
641
  };
@@ -431,27 +651,36 @@ function generateCornerGradientDef(
431
651
  id,
432
652
  element: (
433
653
  <radialGradient
434
- cx={centerX}
435
- cy={centerY}
436
- fx={centerX}
437
- fy={centerY}
654
+ cx={fmt(centerX)}
655
+ cy={fmt(centerY)}
656
+ fx={fmt(centerX)}
657
+ fy={fmt(centerY)}
438
658
  gradientUnits="userSpaceOnUse"
439
659
  id={id}
440
660
  key={id}
441
- r={radius}
661
+ r={fmt(radius)}
442
662
  >
443
- {gradient.stops.map((stop, index) => (
444
- <stop
445
- key={`${id}-stop-${index}`}
446
- offset={`${stop.offset}%`}
447
- stopColor={stop.color}
448
- />
449
- ))}
663
+ {renderStops(id, gradient.stops)}
450
664
  </radialGradient>
451
665
  ),
452
666
  };
453
667
  }
454
668
 
669
+ if (normalized.type === "angular") {
670
+ // Swept around the eye's own centre; the fan reaches the eye's corners.
671
+ return {
672
+ id,
673
+ element: renderConicPattern(
674
+ id,
675
+ normalized,
676
+ cornerX + cornerSize / 2,
677
+ cornerY + cornerSize / 2,
678
+ cornerSize * 0.75,
679
+ size
680
+ ),
681
+ };
682
+ }
683
+
455
684
  return null;
456
685
  }
457
686
 
@@ -467,7 +696,12 @@ function generateGradientDef(
467
696
  return null;
468
697
  }
469
698
 
470
- const id = generateGradientId(idPrefix);
699
+ if (normalized.type === "image") {
700
+ const id = imagePatternId(normalized, size);
701
+ return { id, element: renderImagePattern(id, normalized, size) };
702
+ }
703
+
704
+ const id = generateGradientId(idPrefix, [normalized, size]);
471
705
 
472
706
  if (normalized.type === "linear") {
473
707
  const gradient = normalized as LinearGradient;
@@ -481,18 +715,12 @@ function generateGradientDef(
481
715
  gradientUnits="userSpaceOnUse"
482
716
  id={id}
483
717
  key={id}
484
- x1={x1}
485
- x2={x2}
486
- y1={y1}
487
- y2={y2}
718
+ x1={fmt(x1)}
719
+ x2={fmt(x2)}
720
+ y1={fmt(y1)}
721
+ y2={fmt(y2)}
488
722
  >
489
- {gradient.stops.map((stop, index) => (
490
- <stop
491
- key={`${id}-stop-${index}`}
492
- offset={`${stop.offset}%`}
493
- stopColor={stop.color}
494
- />
495
- ))}
723
+ {renderStops(id, gradient.stops)}
496
724
  </linearGradient>
497
725
  ),
498
726
  };
@@ -508,46 +736,87 @@ function generateGradientDef(
508
736
  id,
509
737
  element: (
510
738
  <radialGradient
511
- cx={centerX}
512
- cy={centerY}
513
- fx={centerX}
514
- fy={centerY}
739
+ cx={fmt(centerX)}
740
+ cy={fmt(centerY)}
741
+ fx={fmt(centerX)}
742
+ fy={fmt(centerY)}
515
743
  gradientUnits="userSpaceOnUse"
516
744
  id={id}
517
745
  key={id}
518
- r={radius}
746
+ r={fmt(radius)}
519
747
  >
520
- {gradient.stops.map((stop, index) => (
521
- <stop
522
- key={`${id}-stop-${index}`}
523
- offset={`${stop.offset}%`}
524
- stopColor={stop.color}
525
- />
526
- ))}
748
+ {renderStops(id, gradient.stops)}
527
749
  </radialGradient>
528
750
  ),
529
751
  };
530
752
  }
531
753
 
754
+ if (normalized.type === "angular") {
755
+ // The fan reaches the code's corners (half the diagonal is 0.707 size).
756
+ return {
757
+ id,
758
+ element: renderConicPattern(
759
+ id,
760
+ normalized,
761
+ size / 2,
762
+ size / 2,
763
+ size * 0.75,
764
+ size
765
+ ),
766
+ };
767
+ }
768
+
532
769
  return null;
533
770
  }
534
771
 
535
- // Get fill value from color config
536
- function getFillValue(
772
+ /** Paints drawn from a definition: gradients and image fills. */
773
+ const hasPaintDef = (color: ColorConfig) =>
774
+ isGradient(color) || isImagePaint(color);
775
+
776
+ /** Definitions once each: image fills that share a picture share an id. */
777
+ function uniqueDefs(
778
+ defs: ({ id: string; element: JSX.Element } | null | undefined)[]
779
+ ): JSX.Element[] {
780
+ const seen = new Set<string>();
781
+ const out: JSX.Element[] = [];
782
+ for (const def of defs) {
783
+ if (def && !seen.has(def.id)) {
784
+ seen.add(def.id);
785
+ out.push(def.element);
786
+ }
787
+ }
788
+ return out;
789
+ }
790
+
791
+ interface FillProps {
792
+ fill: string;
793
+ /** Set when the solid color is see-through */
794
+ fillOpacity?: number;
795
+ }
796
+
797
+ /**
798
+ * `fill` (and `fill-opacity`) for a color config: `url(#id)` for a
799
+ * gradient, otherwise the solid color, with its alpha split out so SVG
800
+ * consumers that don't read 8-digit hex (Illustrator) keep it.
801
+ */
802
+ function getFillProps(
537
803
  colorConfig: ColorConfig | undefined,
538
804
  gradientId: string | null,
539
805
  fallback: string
540
- ): string {
806
+ ): FillProps {
541
807
  if (gradientId) {
542
- return `url(#${gradientId})`;
808
+ return { fill: `url(#${gradientId})` };
543
809
  }
544
810
 
545
811
  const normalized = normalizeColorConfig(colorConfig, fallback);
546
- if (normalized.type === "solid") {
547
- return normalized.color;
548
- }
549
-
550
- return fallback;
812
+ const color =
813
+ normalized.type === "solid"
814
+ ? normalized.color
815
+ : normalized.type === "image"
816
+ ? (normalized.underlay ?? fallback)
817
+ : fallback;
818
+ const { color: fill, opacity } = splitAlpha(color);
819
+ return opacity === undefined ? { fill } : { fill, fillOpacity: opacity };
551
820
  }
552
821
 
553
822
  // Re-export getSolidColor from color utils
@@ -556,7 +825,7 @@ export { getSolidColor } from "./utils/color";
556
825
  export function getImageSettings(
557
826
  cells: Modules,
558
827
  size: number,
559
- _margin: number,
828
+ margin: number,
560
829
  imageSettings?: ImageSettings
561
830
  ): null | {
562
831
  x: number;
@@ -571,7 +840,9 @@ export function getImageSettings(
571
840
 
572
841
  const qrCodeSize = cells.length;
573
842
  const defaultSize = Math.floor(size * DEFAULT_IMG_SCALE);
574
- const scale = qrCodeSize / size;
843
+ // Pixels → modules across the whole artwork (margin included), so a logo
844
+ // keeps its pixel size whatever the margin
845
+ const scale = (qrCodeSize + 2 * margin) / size;
575
846
  const w = (imageSettings.width || defaultSize) * scale;
576
847
  const h = (imageSettings.height || defaultSize) * scale;
577
848
 
@@ -583,16 +854,68 @@ export function getImageSettings(
583
854
 
584
855
  let excavation: Excavation | null = null;
585
856
  if (imageSettings.excavate) {
586
- const floorX = Math.floor(x);
587
- const floorY = Math.floor(y);
588
- const ceilW = Math.ceil(w + x - floorX);
589
- const ceilH = Math.ceil(h + y - floorY);
857
+ // Optional clear space around the logo (pixels → modules)
858
+ const pad = Math.max(0, imageSettings.padding ?? 0) * scale;
859
+ const floorX = Math.floor(x - pad);
860
+ const floorY = Math.floor(y - pad);
861
+ const ceilW = Math.ceil(w + x + pad - floorX);
862
+ const ceilH = Math.ceil(h + y + pad - floorY);
590
863
  excavation = { x: floorX, y: floorY, w: ceilW, h: ceilH };
591
864
  }
592
865
 
593
866
  return { x, y, h, w, excavation };
594
867
  }
595
868
 
869
+ /**
870
+ * Error correction level a code is drawn at. With `autoLevel`, a logo that
871
+ * covers more than `AUTO_LEVEL_LOGO_COVERAGE` (7%) of the code raises the
872
+ * level to H. `isOGContext` with an excavated logo raises L/M to Q (as before).
873
+ */
874
+ export function resolveErrorLevel(props: {
875
+ value: string;
876
+ size?: number;
877
+ level?: ErrorLevel;
878
+ margin?: number;
879
+ imageSettings?: ImageSettings;
880
+ isOGContext?: boolean;
881
+ autoLevel?: boolean;
882
+ shape?: QRCodeShape;
883
+ }): ErrorLevel {
884
+ const level = props.level ?? DEFAULT_LEVEL;
885
+ const { imageSettings } = props;
886
+ let effective: ErrorLevel =
887
+ props.isOGContext &&
888
+ imageSettings?.excavate &&
889
+ (level === "L" || level === "M")
890
+ ? "Q"
891
+ : level;
892
+ if (props.autoLevel && imageSettings && effective !== "H") {
893
+ const cells = qrcodegen.QrCode.encodeText(
894
+ props.value,
895
+ ERROR_LEVEL_MAP[effective]
896
+ ).getModules();
897
+ const settings = getImageSettings(
898
+ cells,
899
+ props.size ?? DEFAULT_SIZE,
900
+ getLayoutMargin(
901
+ cells.length,
902
+ props.margin ?? DEFAULT_MARGIN,
903
+ props.shape
904
+ ),
905
+ imageSettings
906
+ );
907
+ if (settings) {
908
+ const area = settings.excavation
909
+ ? settings.excavation.w * settings.excavation.h
910
+ : settings.w * settings.h;
911
+ if (area / (cells.length * cells.length) > AUTO_LEVEL_LOGO_COVERAGE) {
912
+ effective = "H";
913
+ }
914
+ }
915
+ }
916
+ return effective;
917
+ }
918
+
596
919
  export function convertImageSettingsToPixels(
597
920
  calculatedImageSettings: {
598
921
  x: number;
@@ -628,30 +951,78 @@ export function QRCodeSVG(props: QRPropsSVG) {
628
951
  cornerEyeDotPattern = "circle",
629
952
  margin = DEFAULT_MARGIN,
630
953
  isOGContext = false,
631
- imageSettings,
954
+ imageSettings: requestedImageSettings,
632
955
  templateId,
633
956
  customTemplate,
634
957
  customProps,
958
+ frame,
959
+ art,
960
+ eyeColors,
961
+ moduleScale,
962
+ autoLevel,
963
+ shape,
964
+ frameLabelMarkers,
635
965
  ...otherProps
636
966
  } = props;
967
+ // A logo no bigger than the code can repair (logo-budget.ts)
968
+ const imageSettings = capImageSettings(
969
+ { value, size, margin, level, shape, autoLevel, isOGContext },
970
+ requestedImageSettings
971
+ );
637
972
 
638
- const shouldUseHigherErrorLevel =
639
- isOGContext && imageSettings?.excavate && (level === "L" || level === "M");
640
-
641
- // Use a higher error correction level 'Q' when excavation is enabled
642
- // to ensure the QR code remains scannable despite the removed modules.
643
- const effectiveLevel = shouldUseHigherErrorLevel ? "Q" : level;
973
+ // With a frame, the caller's SVG attributes belong on the outer frame <svg>.
974
+ const framed = isFrameActive(frame);
975
+ const rootProps: typeof otherProps = framed ? {} : otherProps;
976
+ const withFrame = (element: JSX.Element) =>
977
+ framed
978
+ ? wrapWithFrame(element, {
979
+ size,
980
+ frame,
981
+ bgColor,
982
+ svgProps: otherProps,
983
+ markLabels: frameLabelMarkers,
984
+ })
985
+ : element;
986
+
987
+ // An excavated logo in OG images raises L/M to Q; with autoLevel, a logo
988
+ // covering more than 7% of the code raises the level to H.
989
+ const effectiveLevel = resolveErrorLevel({
990
+ value,
991
+ size,
992
+ level,
993
+ margin,
994
+ imageSettings,
995
+ isOGContext,
996
+ autoLevel,
997
+ shape,
998
+ });
644
999
 
645
- let cells = qrcodegen.QrCode.encodeText(
1000
+ const qrCode = qrcodegen.QrCode.encodeText(
646
1001
  value,
647
1002
  ERROR_LEVEL_MAP[effectiveLevel]
648
- ).getModules();
1003
+ );
1004
+ let cells = qrCode.getModules();
1005
+ // A shaped code draws the symbol further in, inside its disc, heart or
1006
+ // silhouette (a silhouette that can't hold the code draws square)
1007
+ const layoutMargin = getLayoutMargin(cells.length, margin, shape);
1008
+ const decoration = shapeDecoration(value, cells.length, margin, shape);
1009
+ const outline = (px: number, fill: FillProps) => {
1010
+ const background = shapeBackground(shape, px);
1011
+ if (!background) {
1012
+ return <rect {...fill} height={px} width={px} x={0} y={0} />;
1013
+ }
1014
+ return "circle" in background ? (
1015
+ <circle cx={fmt(px / 2)} cy={fmt(px / 2)} {...fill} r={fmt(px / 2)} />
1016
+ ) : (
1017
+ <path d={background.path} {...fill} />
1018
+ );
1019
+ };
649
1020
 
650
- const numCells = cells.length + margin * 2;
1021
+ const numCells = cells.length + layoutMargin * 2;
651
1022
  const calculatedImageSettings = getImageSettings(
652
1023
  cells,
653
1024
  size,
654
- margin,
1025
+ layoutMargin,
655
1026
  imageSettings
656
1027
  );
657
1028
 
@@ -667,34 +1038,43 @@ export function QRCodeSVG(props: QRPropsSVG) {
667
1038
  calculatedImageSettings,
668
1039
  size,
669
1040
  numCells,
670
- margin
1041
+ layoutMargin
671
1042
  );
672
1043
 
673
1044
  if (isOGContext) {
1045
+ const shape = imageSettings.shape ?? "square";
674
1046
  image = (
675
1047
  <img
676
1048
  alt="Logo"
677
1049
  src={imageSettings.src}
678
1050
  style={{
679
1051
  position: "absolute",
680
- left: `${imgLeft}px`,
681
- top: `${imgTop}px`,
682
- width: `${imgWidth}px`,
683
- height: `${imgHeight}px`,
1052
+ left: `${fmt(imgLeft)}px`,
1053
+ top: `${fmt(imgTop)}px`,
1054
+ width: `${fmt(imgWidth)}px`,
1055
+ height: `${fmt(imgHeight)}px`,
1056
+ ...(shape !== "square" && {
1057
+ borderRadius:
1058
+ shape === "circle"
1059
+ ? "50%"
1060
+ : `${fmt(Math.min(imgWidth, imgHeight) * LOGO_ROUNDED_RADIUS)}px`,
1061
+ }),
1062
+ ...(imageSettings.plate && {
1063
+ backgroundColor: imageSettings.plate,
1064
+ padding: `${fmt(imgWidth * LOGO_PLATE_INSET)}px`,
1065
+ boxSizing: "border-box",
1066
+ }),
684
1067
  }}
685
1068
  />
686
1069
  );
687
1070
  } else {
688
- image = (
689
- <image
690
- height={imgHeight}
691
- href={imageSettings.src}
692
- preserveAspectRatio="none"
693
- width={imgWidth}
694
- x={imgLeft}
695
- y={imgTop}
696
- />
697
- );
1071
+ // Cropped to imageSettings.shape, on its plate (see logo.tsx)
1072
+ image = renderLogo(imageSettings, {
1073
+ x: imgLeft,
1074
+ y: imgTop,
1075
+ w: imgWidth,
1076
+ h: imgHeight,
1077
+ });
698
1078
  }
699
1079
  }
700
1080
 
@@ -704,214 +1084,252 @@ export function QRCodeSVG(props: QRPropsSVG) {
704
1084
  const cornerSize = 7 * pixelSize; // Corner patterns are 7x7 modules
705
1085
  const cornerDotRadius = 1.5 * pixelSize; // Inner dot radius
706
1086
 
1087
+ // Art modes replace the body pattern; finder eyes keep their own styles
1088
+ // (Logo QR can tint them from the logo, see getQRArtEyeColors).
1089
+ const artColors = {
1090
+ fgColor: getSolidColor(fgColor, DEFAULT_FGCOLOR),
1091
+ bgColor: getSolidColor(bgColor, DEFAULT_BGCOLOR),
1092
+ };
1093
+ const artLayer = isQRArtActive(art)
1094
+ ? renderQRArt({
1095
+ art,
1096
+ modules: cells,
1097
+ version: qrCode.version,
1098
+ margin: layoutMargin,
1099
+ pixelSize,
1100
+ ...artColors,
1101
+ body: (artModules) =>
1102
+ generateDataCircles(artModules, {
1103
+ margin: layoutMargin,
1104
+ pixelSize,
1105
+ pattern: bodyPattern,
1106
+ scale: moduleScale,
1107
+ }),
1108
+ })
1109
+ : null;
1110
+ const artEyes =
1111
+ artLayer && art
1112
+ ? getQRArtEyeColors({ art, version: qrCode.version, ...artColors })
1113
+ : null;
1114
+
707
1115
  // Generate data circles with selected pattern
708
- const dataCircles = generateDataCircles(cells, {
709
- margin,
710
- pixelSize,
711
- pattern: bodyPattern,
712
- });
1116
+ const dataCircles = artLayer
1117
+ ? null
1118
+ : generateDataCircles(cells, {
1119
+ margin: layoutMargin,
1120
+ pixelSize,
1121
+ pattern: bodyPattern,
1122
+ scale: moduleScale,
1123
+ });
1124
+ const decorationShapes = decoration
1125
+ ? generateDataCircles(decoration, {
1126
+ margin: 0,
1127
+ pixelSize,
1128
+ pattern: bodyPattern,
1129
+ scale: moduleScale,
1130
+ plain: true,
1131
+ })
1132
+ : null;
713
1133
 
714
1134
  // Normalize bgColor and fgColor first to ensure proper structure
715
1135
  const normalizedBgColor = normalizeColorConfig(bgColor, DEFAULT_BGCOLOR);
716
1136
  const normalizedFgColor = normalizeColorConfig(fgColor, DEFAULT_FGCOLOR);
717
1137
 
718
1138
  // Generate gradient definitions if needed
719
- const bgColorGradient =
720
- normalizedBgColor.type === "linear" || normalizedBgColor.type === "radial"
721
- ? generateGradientDef(
722
- normalizedBgColor as LinearGradient | RadialGradient,
723
- size,
724
- "bg-color"
725
- )
726
- : null;
1139
+ const bgColorGradient = hasPaintDef(normalizedBgColor)
1140
+ ? generateGradientDef(normalizedBgColor, size, "bg-color")
1141
+ : null;
727
1142
 
728
- const fgColorGradient =
729
- normalizedFgColor.type === "linear" || normalizedFgColor.type === "radial"
730
- ? generateGradientDef(
731
- normalizedFgColor as LinearGradient | RadialGradient,
732
- size,
733
- "fg-color"
734
- )
735
- : null;
1143
+ const fgColorGradient = hasPaintDef(normalizedFgColor)
1144
+ ? generateGradientDef(normalizedFgColor, size, "fg-color")
1145
+ : null;
736
1146
 
737
1147
  // Get fill values - use normalized color for gradient detection
738
- const bgFillValue = getFillValue(
739
- bgColor, // Pass original for getFillValue to handle normalization internally
1148
+ const bgFillValue = getFillProps(
1149
+ bgColor, // Pass original for getFillProps to handle normalization internally
740
1150
  bgColorGradient?.id ?? null,
741
1151
  DEFAULT_BGCOLOR
742
1152
  );
743
1153
 
744
- const fgFillValue = getFillValue(
745
- fgColor, // Pass original for getFillValue to handle normalization internally
1154
+ const fgFillValue = getFillProps(
1155
+ fgColor, // Pass original for getFillProps to handle normalization internally
746
1156
  fgColorGradient?.id ?? null,
747
1157
  DEFAULT_FGCOLOR
748
1158
  );
749
1159
 
750
- // Calculate corner positions with margin
751
- const topLeftX = margin * pixelSize;
752
- const topLeftY = margin * pixelSize;
753
- const topRightX = (qrSize - 7 + margin) * pixelSize;
754
- const topRightY = margin * pixelSize;
755
- const bottomLeftX = margin * pixelSize;
756
- const bottomLeftY = (qrSize - 7 + margin) * pixelSize;
757
-
758
- // Normalize eyeColor and dotColor
759
- const normalizedEyeColor = normalizeColorConfig(
760
- eyeColor,
761
- getSolidColor(fgColor, DEFAULT_FGCOLOR)
762
- );
763
- const normalizedDotColor = normalizeColorConfig(
764
- dotColor,
765
- getSolidColor(fgColor, DEFAULT_FGCOLOR)
1160
+ // Data modules, plus a shaped code's decoration around them
1161
+ const dataLayer = artLayer ? (
1162
+ <>
1163
+ {artLayer}
1164
+ {decorationShapes && <g {...fgFillValue}>{decorationShapes}</g>}
1165
+ </>
1166
+ ) : (
1167
+ <g {...fgFillValue}>
1168
+ {dataCircles}
1169
+ {decorationShapes}
1170
+ </g>
766
1171
  );
767
1172
 
768
- // Generate gradient definitions for eyeColor (corner squares) for each corner
769
- const topLeftEyeGradient =
770
- normalizedEyeColor.type === "linear" || normalizedEyeColor.type === "radial"
771
- ? generateCornerGradientDef(
772
- normalizedEyeColor as LinearGradient | RadialGradient,
773
- topLeftX,
774
- topLeftY,
775
- cornerSize,
776
- "top-left",
777
- "corners-square-color-0-0"
778
- )
779
- : null;
780
-
781
- const topRightEyeGradient =
782
- normalizedEyeColor.type === "linear" || normalizedEyeColor.type === "radial"
783
- ? generateCornerGradientDef(
784
- normalizedEyeColor as LinearGradient | RadialGradient,
785
- topRightX,
786
- topRightY,
787
- cornerSize,
788
- "top-right",
789
- "corners-square-color-1-0"
790
- )
791
- : null;
792
-
793
- const bottomLeftEyeGradient =
794
- normalizedEyeColor.type === "linear" || normalizedEyeColor.type === "radial"
795
- ? generateCornerGradientDef(
796
- normalizedEyeColor as LinearGradient | RadialGradient,
797
- bottomLeftX,
798
- bottomLeftY,
799
- cornerSize,
800
- "bottom-left",
801
- "corners-square-color-0-1"
802
- )
803
- : null;
804
-
805
- // Generate gradient definitions for dotColor (corner dots) for each corner
806
- const topLeftDotGradient =
807
- normalizedDotColor.type === "linear" || normalizedDotColor.type === "radial"
808
- ? generateCornerGradientDef(
809
- normalizedDotColor as LinearGradient | RadialGradient,
810
- topLeftX,
811
- topLeftY,
812
- cornerSize,
813
- "top-left",
814
- "corners-dot-color-0-0"
815
- )
816
- : null;
817
-
818
- const topRightDotGradient =
819
- normalizedDotColor.type === "linear" || normalizedDotColor.type === "radial"
1173
+ // Calculate corner positions with margin
1174
+ const topLeftX = layoutMargin * pixelSize;
1175
+ const topLeftY = layoutMargin * pixelSize;
1176
+ const topRightX = (qrSize - 7 + layoutMargin) * pixelSize;
1177
+ const topRightY = layoutMargin * pixelSize;
1178
+ const bottomLeftX = layoutMargin * pixelSize;
1179
+ const bottomLeftY = (qrSize - 7 + layoutMargin) * pixelSize;
1180
+
1181
+ // Eye colors: a per-eye override (eyeColors) wins over Logo QR's matched
1182
+ // colors, then eyeColor (outer frame) and dotColor (inner dot), which fall
1183
+ // back to the foreground.
1184
+ const fgSolid = getSolidColor(fgColor, DEFAULT_FGCOLOR);
1185
+ const outerColor = (position: EyePosition) =>
1186
+ eyeColors?.[position]?.outer ?? artEyes?.[position]?.outer ?? eyeColor;
1187
+ const innerColor = (position: EyePosition) =>
1188
+ eyeColors?.[position]?.inner ?? artEyes?.[position]?.inner ?? dotColor;
1189
+ const cornerGradient = (
1190
+ color: ColorConfig | undefined,
1191
+ cornerX: number,
1192
+ cornerY: number,
1193
+ cornerType: "top-left" | "top-right" | "bottom-left",
1194
+ idPrefix: string
1195
+ ) => {
1196
+ const normalized = normalizeColorConfig(color, fgSolid);
1197
+ return hasPaintDef(normalized)
820
1198
  ? generateCornerGradientDef(
821
- normalizedDotColor as LinearGradient | RadialGradient,
822
- topRightX,
823
- topRightY,
1199
+ normalized,
1200
+ cornerX,
1201
+ cornerY,
824
1202
  cornerSize,
825
- "top-right",
826
- "corners-dot-color-1-0"
1203
+ cornerType,
1204
+ idPrefix,
1205
+ size
827
1206
  )
828
1207
  : null;
1208
+ };
829
1209
 
830
- const bottomLeftDotGradient =
831
- normalizedDotColor.type === "linear" || normalizedDotColor.type === "radial"
832
- ? generateCornerGradientDef(
833
- normalizedDotColor as LinearGradient | RadialGradient,
834
- bottomLeftX,
835
- bottomLeftY,
836
- cornerSize,
837
- "bottom-left",
838
- "corners-dot-color-0-1"
839
- )
840
- : null;
1210
+ // Gradient definitions for the corner squares and dots, per corner
1211
+ const topLeftEyeGradient = cornerGradient(
1212
+ outerColor("topLeft"),
1213
+ topLeftX,
1214
+ topLeftY,
1215
+ "top-left",
1216
+ "corners-square-color-0-0"
1217
+ );
1218
+ const topRightEyeGradient = cornerGradient(
1219
+ outerColor("topRight"),
1220
+ topRightX,
1221
+ topRightY,
1222
+ "top-right",
1223
+ "corners-square-color-1-0"
1224
+ );
1225
+ const bottomLeftEyeGradient = cornerGradient(
1226
+ outerColor("bottomLeft"),
1227
+ bottomLeftX,
1228
+ bottomLeftY,
1229
+ "bottom-left",
1230
+ "corners-square-color-0-1"
1231
+ );
1232
+ const topLeftDotGradient = cornerGradient(
1233
+ innerColor("topLeft"),
1234
+ topLeftX,
1235
+ topLeftY,
1236
+ "top-left",
1237
+ "corners-dot-color-0-0"
1238
+ );
1239
+ const topRightDotGradient = cornerGradient(
1240
+ innerColor("topRight"),
1241
+ topRightX,
1242
+ topRightY,
1243
+ "top-right",
1244
+ "corners-dot-color-1-0"
1245
+ );
1246
+ const bottomLeftDotGradient = cornerGradient(
1247
+ innerColor("bottomLeft"),
1248
+ bottomLeftX,
1249
+ bottomLeftY,
1250
+ "bottom-left",
1251
+ "corners-dot-color-0-1"
1252
+ );
841
1253
 
842
- // Get fill values for eyeColor and dotColor
843
- const topLeftEyeFillValue = getFillValue(
844
- eyeColor,
1254
+ // Get fill values for the corner squares and dots
1255
+ const topLeftEyeFillValue = getFillProps(
1256
+ outerColor("topLeft"),
845
1257
  topLeftEyeGradient?.id ?? null,
846
- getSolidColor(fgColor, DEFAULT_FGCOLOR)
1258
+ fgSolid
847
1259
  );
848
- const topRightEyeFillValue = getFillValue(
849
- eyeColor,
1260
+ const topRightEyeFillValue = getFillProps(
1261
+ outerColor("topRight"),
850
1262
  topRightEyeGradient?.id ?? null,
851
- getSolidColor(fgColor, DEFAULT_FGCOLOR)
1263
+ fgSolid
852
1264
  );
853
- const bottomLeftEyeFillValue = getFillValue(
854
- eyeColor,
1265
+ const bottomLeftEyeFillValue = getFillProps(
1266
+ outerColor("bottomLeft"),
855
1267
  bottomLeftEyeGradient?.id ?? null,
856
- getSolidColor(fgColor, DEFAULT_FGCOLOR)
1268
+ fgSolid
857
1269
  );
858
1270
 
859
- const topLeftDotFillValue = getFillValue(
860
- dotColor,
1271
+ const topLeftDotFillValue = getFillProps(
1272
+ innerColor("topLeft"),
861
1273
  topLeftDotGradient?.id ?? null,
862
- getSolidColor(fgColor, DEFAULT_FGCOLOR)
1274
+ fgSolid
863
1275
  );
864
- const topRightDotFillValue = getFillValue(
865
- dotColor,
1276
+ const topRightDotFillValue = getFillProps(
1277
+ innerColor("topRight"),
866
1278
  topRightDotGradient?.id ?? null,
867
- getSolidColor(fgColor, DEFAULT_FGCOLOR)
1279
+ fgSolid
868
1280
  );
869
- const bottomLeftDotFillValue = getFillValue(
870
- dotColor,
1281
+ const bottomLeftDotFillValue = getFillProps(
1282
+ innerColor("bottomLeft"),
871
1283
  bottomLeftDotGradient?.id ?? null,
872
- getSolidColor(fgColor, DEFAULT_FGCOLOR)
1284
+ fgSolid
873
1285
  );
874
1286
 
875
1287
  const topLeftEye = generateCornerSquarePath(
876
1288
  topLeftX,
877
1289
  topLeftY,
878
1290
  cornerSize,
879
- cornerEyePattern
1291
+ cornerEyePattern,
1292
+ "top-left"
880
1293
  );
881
1294
 
882
1295
  const topLeftDot = generateCornerDotPath(
883
1296
  topLeftX + cornerSize / 2,
884
1297
  topLeftY + cornerSize / 2,
885
1298
  cornerDotRadius,
886
- cornerEyeDotPattern
1299
+ cornerEyeDotPattern,
1300
+ "top-left"
887
1301
  );
888
1302
 
889
1303
  const topRightEye = generateCornerSquarePath(
890
1304
  topRightX,
891
1305
  topRightY,
892
1306
  cornerSize,
893
- cornerEyePattern
1307
+ cornerEyePattern,
1308
+ "top-right"
894
1309
  );
895
1310
 
896
1311
  const topRightDot = generateCornerDotPath(
897
1312
  topRightX + cornerSize / 2,
898
1313
  topRightY + cornerSize / 2,
899
1314
  cornerDotRadius,
900
- cornerEyeDotPattern
1315
+ cornerEyeDotPattern,
1316
+ "top-right"
901
1317
  );
902
1318
 
903
1319
  const bottomLeftEye = generateCornerSquarePath(
904
1320
  bottomLeftX,
905
1321
  bottomLeftY,
906
1322
  cornerSize,
907
- cornerEyePattern
1323
+ cornerEyePattern,
1324
+ "bottom-left"
908
1325
  );
909
1326
 
910
1327
  const bottomLeftDot = generateCornerDotPath(
911
1328
  bottomLeftX + cornerSize / 2,
912
1329
  bottomLeftY + cornerSize / 2,
913
1330
  cornerDotRadius,
914
- cornerEyeDotPattern
1331
+ cornerEyeDotPattern,
1332
+ "bottom-left"
915
1333
  );
916
1334
 
917
1335
  // QR Code content (can be used standalone or inside wrapper)
@@ -919,57 +1337,59 @@ export function QRCodeSVG(props: QRPropsSVG) {
919
1337
  <>
920
1338
  {/* Gradient definitions */}
921
1339
  <defs>
922
- {bgColorGradient?.element}
923
- {fgColorGradient?.element}
924
- {topLeftEyeGradient?.element}
925
- {topRightEyeGradient?.element}
926
- {bottomLeftEyeGradient?.element}
927
- {topLeftDotGradient?.element}
928
- {topRightDotGradient?.element}
929
- {bottomLeftDotGradient?.element}
1340
+ {uniqueDefs([
1341
+ bgColorGradient,
1342
+ fgColorGradient,
1343
+ topLeftEyeGradient,
1344
+ topRightEyeGradient,
1345
+ bottomLeftEyeGradient,
1346
+ topLeftDotGradient,
1347
+ topRightDotGradient,
1348
+ bottomLeftDotGradient,
1349
+ ])}
930
1350
  </defs>
931
1351
 
932
- {/* Background */}
933
- <rect fill={bgFillValue} height={size} width={size} x={0} y={0} />
1352
+ {/* Background: the square, or a shaped code's outline */}
1353
+ {outline(size, bgFillValue)}
934
1354
 
935
1355
  {/* Data module circles */}
936
- <g fill={fgFillValue}>{dataCircles}</g>
1356
+ {dataLayer}
937
1357
 
938
1358
  {/* Top-left corner square */}
939
- <g fill={topLeftEyeFillValue}>
1359
+ <g {...topLeftEyeFillValue}>
940
1360
  <path
941
- fillRule="evenodd"
942
1361
  d={topLeftEye}
943
- transform={`rotate(0,${topLeftX + cornerSize / 2},${topLeftY + cornerSize / 2})`}
1362
+ fillRule="evenodd"
1363
+ transform={compact`rotate(0,${topLeftX + cornerSize / 2},${topLeftY + cornerSize / 2})`}
944
1364
  />
945
1365
  </g>
946
1366
 
947
1367
  {/* Top-left corner dot */}
948
- <g fill={topLeftDotFillValue}>{topLeftDot}</g>
1368
+ <g {...topLeftDotFillValue}>{topLeftDot}</g>
949
1369
 
950
1370
  {/* Top-right corner square */}
951
- <g fill={topRightEyeFillValue}>
1371
+ <g {...topRightEyeFillValue}>
952
1372
  <path
953
- fillRule="evenodd"
954
1373
  d={topRightEye}
955
- transform={`rotate(0,${topRightX + cornerSize / 2},${topRightY + cornerSize / 2})`}
1374
+ fillRule="evenodd"
1375
+ transform={compact`rotate(0,${topRightX + cornerSize / 2},${topRightY + cornerSize / 2})`}
956
1376
  />
957
1377
  </g>
958
1378
 
959
1379
  {/* Top-right corner dot */}
960
- <g fill={topRightDotFillValue}>{topRightDot}</g>
1380
+ <g {...topRightDotFillValue}>{topRightDot}</g>
961
1381
 
962
1382
  {/* Bottom-left corner square */}
963
- <g fill={bottomLeftEyeFillValue}>
1383
+ <g {...bottomLeftEyeFillValue}>
964
1384
  <path
965
- fillRule="evenodd"
966
1385
  d={bottomLeftEye}
967
- transform={`rotate(0,${bottomLeftX + cornerSize / 2},${bottomLeftY + cornerSize / 2})`}
1386
+ fillRule="evenodd"
1387
+ transform={compact`rotate(0,${bottomLeftX + cornerSize / 2},${bottomLeftY + cornerSize / 2})`}
968
1388
  />
969
1389
  </g>
970
1390
 
971
1391
  {/* Bottom-left corner dot */}
972
- <g fill={bottomLeftDotFillValue}>{bottomLeftDot}</g>
1392
+ <g {...bottomLeftDotFillValue}>{bottomLeftDot}</g>
973
1393
 
974
1394
  {image}
975
1395
  </>
@@ -984,16 +1404,15 @@ export function QRCodeSVG(props: QRPropsSVG) {
984
1404
  const templateSize = 300; // All templates use 300x300 base coordinate system
985
1405
 
986
1406
  // Generate background gradient for template size (gradients use userSpaceOnUse, so coordinates must match)
987
- const templateBgColorGradient =
988
- normalizedBgColor.type === "linear" || normalizedBgColor.type === "radial"
989
- ? generateGradientDef(
990
- normalizedBgColor as LinearGradient | RadialGradient,
991
- templateSize,
992
- "bg-color-template"
993
- )
994
- : null;
995
-
996
- const templateBgFillValue = getFillValue(
1407
+ const templateBgColorGradient = hasPaintDef(normalizedBgColor)
1408
+ ? generateGradientDef(
1409
+ normalizedBgColor,
1410
+ templateSize,
1411
+ "bg-color-template"
1412
+ )
1413
+ : null;
1414
+
1415
+ const templateBgFillValue = getFillProps(
997
1416
  bgColor,
998
1417
  templateBgColorGradient?.id ?? null,
999
1418
  DEFAULT_BGCOLOR
@@ -1009,101 +1428,61 @@ export function QRCodeSVG(props: QRPropsSVG) {
1009
1428
  >
1010
1429
  {/* Gradient definitions */}
1011
1430
  <defs>
1012
- {templateBgColorGradient?.element}
1013
- {fgColorGradient?.element}
1014
- {topLeftEyeGradient?.element}
1015
- {topRightEyeGradient?.element}
1016
- {bottomLeftEyeGradient?.element}
1017
- {topLeftDotGradient?.element}
1018
- {topRightDotGradient?.element}
1019
- {bottomLeftDotGradient?.element}
1431
+ {uniqueDefs([
1432
+ templateBgColorGradient,
1433
+ fgColorGradient,
1434
+ topLeftEyeGradient,
1435
+ topRightEyeGradient,
1436
+ bottomLeftEyeGradient,
1437
+ topLeftDotGradient,
1438
+ topRightDotGradient,
1439
+ bottomLeftDotGradient,
1440
+ ])}
1020
1441
  </defs>
1021
1442
 
1022
1443
  {/* Background */}
1023
- <rect
1024
- fill={templateBgFillValue}
1025
- height={templateSize}
1026
- width={templateSize}
1027
- x={0}
1028
- y={0}
1029
- />
1444
+ {outline(templateSize, templateBgFillValue)}
1030
1445
 
1031
1446
  {/* Scale all QR elements to fit template coordinate system */}
1032
- <g transform={`scale(${templateSize / size})`}>
1447
+ <g transform={`scale(${fmt(templateSize / size, SCALE_DECIMALS)})`}>
1033
1448
  {/* Data module circles */}
1034
- <g fill={fgFillValue}>{dataCircles}</g>
1449
+ {dataLayer}
1035
1450
 
1036
1451
  {/* Top-left corner square */}
1037
- <g fill={topLeftEyeFillValue}>
1452
+ <g {...topLeftEyeFillValue}>
1038
1453
  <path
1454
+ d={topLeftEye}
1039
1455
  fillRule="evenodd"
1040
- d={generateCornerSquarePath(
1041
- topLeftX,
1042
- topLeftY,
1043
- cornerSize,
1044
- cornerEyePattern
1045
- )}
1046
- transform={`rotate(0,${topLeftX + cornerSize / 2},${topLeftY + cornerSize / 2})`}
1456
+ transform={compact`rotate(0,${topLeftX + cornerSize / 2},${topLeftY + cornerSize / 2})`}
1047
1457
  />
1048
1458
  </g>
1049
1459
 
1050
1460
  {/* Top-left corner dot */}
1051
- <g fill={topLeftDotFillValue}>
1052
- {generateCornerDotPath(
1053
- topLeftX + cornerSize / 2,
1054
- topLeftY + cornerSize / 2,
1055
- cornerDotRadius,
1056
- cornerEyeDotPattern
1057
- )}
1058
- </g>
1461
+ <g {...topLeftDotFillValue}>{topLeftDot}</g>
1059
1462
 
1060
1463
  {/* Top-right corner square */}
1061
- <g fill={topRightEyeFillValue}>
1464
+ <g {...topRightEyeFillValue}>
1062
1465
  <path
1466
+ d={topRightEye}
1063
1467
  fillRule="evenodd"
1064
- d={generateCornerSquarePath(
1065
- topRightX,
1066
- topRightY,
1067
- cornerSize,
1068
- cornerEyePattern
1069
- )}
1070
- transform={`rotate(0,${topRightX + cornerSize / 2},${topRightY + cornerSize / 2})`}
1468
+ transform={compact`rotate(0,${topRightX + cornerSize / 2},${topRightY + cornerSize / 2})`}
1071
1469
  />
1072
1470
  </g>
1073
1471
 
1074
1472
  {/* Top-right corner dot */}
1075
- <g fill={topRightDotFillValue}>
1076
- {generateCornerDotPath(
1077
- topRightX + cornerSize / 2,
1078
- topRightY + cornerSize / 2,
1079
- cornerDotRadius,
1080
- cornerEyeDotPattern
1081
- )}
1082
- </g>
1473
+ <g {...topRightDotFillValue}>{topRightDot}</g>
1083
1474
 
1084
1475
  {/* Bottom-left corner square */}
1085
- <g fill={bottomLeftEyeFillValue}>
1476
+ <g {...bottomLeftEyeFillValue}>
1086
1477
  <path
1478
+ d={bottomLeftEye}
1087
1479
  fillRule="evenodd"
1088
- d={generateCornerSquarePath(
1089
- bottomLeftX,
1090
- bottomLeftY,
1091
- cornerSize,
1092
- cornerEyePattern
1093
- )}
1094
- transform={`rotate(0,${bottomLeftX + cornerSize / 2},${bottomLeftY + cornerSize / 2})`}
1480
+ transform={compact`rotate(0,${bottomLeftX + cornerSize / 2},${bottomLeftY + cornerSize / 2})`}
1095
1481
  />
1096
1482
  </g>
1097
1483
 
1098
1484
  {/* Bottom-left corner dot */}
1099
- <g fill={bottomLeftDotFillValue}>
1100
- {generateCornerDotPath(
1101
- bottomLeftX + cornerSize / 2,
1102
- bottomLeftY + cornerSize / 2,
1103
- cornerDotRadius,
1104
- cornerEyeDotPattern
1105
- )}
1106
- </g>
1485
+ <g {...bottomLeftDotFillValue}>{bottomLeftDot}</g>
1107
1486
 
1108
1487
  {image}
1109
1488
  </g>
@@ -1142,24 +1521,30 @@ export function QRCodeSVG(props: QRPropsSVG) {
1142
1521
  ...originalStyle,
1143
1522
  width: undefined,
1144
1523
  height: undefined,
1524
+ ...rootProps.style,
1145
1525
  };
1146
1526
 
1147
- // @ts-expect-error
1148
- return React.cloneElement(wrappedSvg, {
1149
- width: size,
1150
- height: size,
1151
- style: newStyle,
1152
- });
1527
+ // Forward the caller's SVG attributes (className, aria-*, etc.) to the
1528
+ // root element, same as the non-template path below.
1529
+ return withFrame(
1530
+ // @ts-expect-error
1531
+ React.cloneElement(wrappedSvg, {
1532
+ ...rootProps,
1533
+ width: size,
1534
+ height: size,
1535
+ style: newStyle,
1536
+ })
1537
+ );
1153
1538
  }
1154
1539
 
1155
1540
  // Standard QR code without wrapper (backward compatibility)
1156
- return (
1541
+ return withFrame(
1157
1542
  <svg
1158
1543
  height={size}
1159
1544
  viewBox={`0 0 ${size} ${size}`}
1160
1545
  width={size}
1161
1546
  xmlns="http://www.w3.org/2000/svg"
1162
- {...otherProps}
1547
+ {...rootProps}
1163
1548
  >
1164
1549
  {qrContent}
1165
1550
  </svg>
@@ -1174,7 +1559,7 @@ export function QRCodeSVG(props: QRPropsSVG) {
1174
1559
  export const SUPPORTS_PATH2D = (() => {
1175
1560
  try {
1176
1561
  new Path2D().addPath(new Path2D());
1177
- } catch (_e) {
1562
+ } catch {
1178
1563
  return false;
1179
1564
  }
1180
1565
  return true;
@@ -1184,7 +1569,19 @@ export const SUPPORTS_PATH2D = (() => {
1184
1569
  export const hexToRgb = (
1185
1570
  hex: string
1186
1571
  ): { r: number; g: number; b: number } | null => {
1187
- const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
1572
+ // Expand shorthand (#rgb / #rgba) and drop any alpha channel so "#fff" and
1573
+ // "#ffffff80" are read as colors instead of failing the contrast check.
1574
+ let normalized = hex.trim().replace(/^#/, "");
1575
+ if (normalized.length === 3 || normalized.length === 4) {
1576
+ normalized = normalized
1577
+ .slice(0, 3)
1578
+ .split("")
1579
+ .map((c) => c + c)
1580
+ .join("");
1581
+ } else if (normalized.length === 8) {
1582
+ normalized = normalized.slice(0, 6);
1583
+ }
1584
+ const result = /^([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(normalized);
1188
1585
  return result
1189
1586
  ? {
1190
1587
  r: Number.parseInt(result[1], 16),
@@ -1202,9 +1599,21 @@ export const getLuminance = (r: number, g: number, b: number): number => {
1202
1599
  return 0.2126 * rs + 0.7152 * gs + 0.0722 * bs;
1203
1600
  };
1204
1601
 
1602
+ /** Any CSS color parseColor reads, composited over white (the paper). */
1603
+ const toOpaqueRgb = (
1604
+ color: string
1605
+ ): { r: number; g: number; b: number } | null => {
1606
+ const parsed = parseColor(color);
1607
+ if (!parsed) {
1608
+ return null;
1609
+ }
1610
+ const over = (channel: number) => channel * parsed.a + 255 * (1 - parsed.a);
1611
+ return { r: over(parsed.r), g: over(parsed.g), b: over(parsed.b) };
1612
+ };
1613
+
1205
1614
  export const getContrastRatio = (color1: string, color2: string): number => {
1206
- const rgb1 = hexToRgb(color1);
1207
- const rgb2 = hexToRgb(color2);
1615
+ const rgb1 = toOpaqueRgb(color1);
1616
+ const rgb2 = toOpaqueRgb(color2);
1208
1617
 
1209
1618
  if (!(rgb1 && rgb2)) {
1210
1619
  return 1;