qrdx 0.0.57 → 0.0.59

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 (260) hide show
  1. package/README.md +22 -6
  2. package/dist/angular.js +33 -3
  3. package/dist/angular.js.map +1 -1
  4. package/dist/angular.mjs +33 -3
  5. package/dist/angular.mjs.map +1 -1
  6. package/dist/core.js +7 -7
  7. package/dist/core.js.map +1 -1
  8. package/dist/core.mjs +7 -7
  9. package/dist/core.mjs.map +1 -1
  10. package/dist/element.browser.mjs +6 -6
  11. package/dist/element.browser.mjs.map +1 -1
  12. package/dist/element.js +1 -1
  13. package/dist/element.js.map +1 -1
  14. package/dist/element.mjs +1 -1
  15. package/dist/element.mjs.map +1 -1
  16. package/dist/frames-index.js +1 -1
  17. package/dist/frames-index.js.map +1 -1
  18. package/dist/frames-index.mjs +1 -1
  19. package/dist/frames-index.mjs.map +1 -1
  20. package/dist/frames-official.js +1 -1
  21. package/dist/frames-official.js.map +1 -1
  22. package/dist/frames-official.mjs +1 -1
  23. package/dist/frames-official.mjs.map +1 -1
  24. package/dist/index.js +7 -7
  25. package/dist/index.js.map +1 -1
  26. package/dist/index.mjs +7 -7
  27. package/dist/index.mjs.map +1 -1
  28. package/dist/qr-code-canvas.js +1 -1
  29. package/dist/qr-code-canvas.js.map +1 -1
  30. package/dist/qr-code-canvas.mjs +1 -1
  31. package/dist/qr-code-canvas.mjs.map +1 -1
  32. package/dist/schemas.js +1 -1
  33. package/dist/schemas.js.map +1 -1
  34. package/dist/schemas.mjs +1 -1
  35. package/dist/schemas.mjs.map +1 -1
  36. package/dist/src/angular.d.ts +9 -3
  37. package/dist/src/download.d.ts +32 -6
  38. package/dist/src/download.d.ts.map +1 -1
  39. package/dist/src/element.d.ts +10 -1
  40. package/dist/src/element.d.ts.map +1 -1
  41. package/dist/src/engine.d.ts +5 -2
  42. package/dist/src/engine.d.ts.map +1 -1
  43. package/dist/src/eps.d.ts.map +1 -1
  44. package/dist/src/export.d.ts.map +1 -1
  45. package/dist/src/frame.d.ts +24 -0
  46. package/dist/src/frame.d.ts.map +1 -1
  47. package/dist/src/frames/catalogue/all.d.ts.map +1 -1
  48. package/dist/src/frames/catalogue/batches.d.ts +6 -0
  49. package/dist/src/frames/catalogue/batches.d.ts.map +1 -0
  50. package/dist/src/frames/catalogue/call-to-action.d.ts.map +1 -1
  51. package/dist/src/frames/catalogue/counts.d.ts +4 -2
  52. package/dist/src/frames/catalogue/counts.d.ts.map +1 -1
  53. package/dist/src/frames/catalogue/index.d.ts.map +1 -1
  54. package/dist/src/frames/catalogue/objects.d.ts +5 -0
  55. package/dist/src/frames/catalogue/objects.d.ts.map +1 -0
  56. package/dist/src/frames/catalogue/playful.d.ts +5 -0
  57. package/dist/src/frames/catalogue/playful.d.ts.map +1 -0
  58. package/dist/src/frames/catalogue/tags.d.ts.map +1 -1
  59. package/dist/src/frames/collections.d.ts.map +1 -1
  60. package/dist/src/frames/official-types.d.ts +1 -1
  61. package/dist/src/frames/official-types.d.ts.map +1 -1
  62. package/dist/src/frames/official.d.ts.map +1 -1
  63. package/dist/src/gif.d.ts +40 -0
  64. package/dist/src/gif.d.ts.map +1 -0
  65. package/dist/src/helpers.d.ts +1 -1
  66. package/dist/src/helpers.d.ts.map +1 -1
  67. package/dist/src/ico.d.ts +11 -0
  68. package/dist/src/ico.d.ts.map +1 -0
  69. package/dist/src/index.d.ts +2 -1
  70. package/dist/src/index.d.ts.map +1 -1
  71. package/dist/src/looks.d.ts +101 -0
  72. package/dist/src/looks.d.ts.map +1 -0
  73. package/dist/src/plain.d.ts.map +1 -1
  74. package/dist/src/print.d.ts.map +1 -1
  75. package/dist/src/qr-code-canvas.d.ts.map +1 -1
  76. package/dist/src/qr-code-props.d.ts +10 -1
  77. package/dist/src/qr-code-props.d.ts.map +1 -1
  78. package/dist/src/qr-code.d.ts.map +1 -1
  79. package/dist/src/qr-frame-svg.d.ts +10 -0
  80. package/dist/src/qr-frame-svg.d.ts.map +1 -0
  81. package/dist/src/scan-score.d.ts.map +1 -1
  82. package/dist/src/utils/gif.d.ts +61 -0
  83. package/dist/src/utils/gif.d.ts.map +1 -0
  84. package/dist/src/utils/ico.d.ts +30 -0
  85. package/dist/src/utils/ico.d.ts.map +1 -0
  86. package/dist/src/utils/parse-color.d.ts +1 -1
  87. package/dist/src/utils/parse-color.d.ts.map +1 -1
  88. package/dist/src/utils/print-shared.d.ts.map +1 -1
  89. package/dist/src/utils/svg-painter.d.ts +13 -0
  90. package/dist/src/utils/svg-painter.d.ts.map +1 -1
  91. package/dist/src/utils.d.ts.map +1 -1
  92. package/dist/src/vanilla-api.d.ts +2 -2
  93. package/dist/src/vanilla-api.d.ts.map +1 -1
  94. package/dist/src/vue.d.ts +1 -1
  95. package/dist/src/vue.d.ts.map +1 -1
  96. package/dist/types/constants.d.ts +41 -0
  97. package/dist/types/constants.d.ts.map +1 -1
  98. package/dist/types/index.js +2 -2
  99. package/dist/types/index.js.map +1 -1
  100. package/dist/types/index.mjs +2 -2
  101. package/dist/types/index.mjs.map +1 -1
  102. package/dist/types/plain.d.ts +2 -2
  103. package/dist/types/plain.d.ts.map +1 -1
  104. package/dist/types/qr.d.ts +399 -1
  105. package/dist/types/qr.d.ts.map +1 -1
  106. package/dist/types/react.d.ts +10 -1
  107. package/dist/types/react.d.ts.map +1 -1
  108. package/dist/types/schemas.d.ts +1 -1
  109. package/dist/types/schemas.d.ts.map +1 -1
  110. package/dist/vanilla.browser.mjs +34 -34
  111. package/dist/vanilla.browser.mjs.map +1 -1
  112. package/dist/vue.js +1 -1
  113. package/dist/vue.js.map +1 -1
  114. package/dist/vue.mjs +1 -1
  115. package/dist/vue.mjs.map +1 -1
  116. package/package.json +6 -2
  117. package/src/angular.ts +14 -2
  118. package/src/download.tsx +92 -7
  119. package/src/element.ts +16 -1
  120. package/src/engine.ts +22 -0
  121. package/src/eps.ts +8 -1
  122. package/src/export.ts +16 -0
  123. package/src/frame.tsx +51 -0
  124. package/src/frames/catalogue/all.ts +202 -0
  125. package/src/frames/catalogue/batches.ts +114 -0
  126. package/src/frames/catalogue/call-to-action.ts +58 -0
  127. package/src/frames/catalogue/counts.ts +4 -2
  128. package/src/frames/catalogue/index.ts +808 -0
  129. package/src/frames/catalogue/objects.ts +65 -0
  130. package/src/frames/catalogue/playful.ts +97 -0
  131. package/src/frames/catalogue/tags.ts +137 -0
  132. package/src/frames/collections.ts +2 -0
  133. package/src/frames/official/badge-bottom-light.json +91 -0
  134. package/src/frames/official/badge-bottom.json +79 -0
  135. package/src/frames/official/billboard-campaign.json +274 -0
  136. package/src/frames/official/billboard-light.json +287 -0
  137. package/src/frames/official/bookmark-light.json +88 -0
  138. package/src/frames/official/bookmark-strip.json +83 -0
  139. package/src/frames/official/border-medium.json +71 -0
  140. package/src/frames/official/border-thick.json +71 -0
  141. package/src/frames/official/border-thin.json +71 -0
  142. package/src/frames/official/bottle-label.json +136 -0
  143. package/src/frames/official/bottle-light.json +133 -0
  144. package/src/frames/official/camera-viewfinder.json +174 -0
  145. package/src/frames/official/cassette-tape.json +236 -0
  146. package/src/frames/official/cat-with-card-light.json +246 -0
  147. package/src/frames/official/cat-with-card.json +234 -0
  148. package/src/frames/official/cinema-marquee.json +537 -0
  149. package/src/frames/official/circle-viewfinder.json +87 -0
  150. package/src/frames/official/clapperboard-light.json +163 -0
  151. package/src/frames/official/clapperboard-scene.json +151 -0
  152. package/src/frames/official/comic-panel.json +386 -0
  153. package/src/frames/official/corner-tab-light.json +101 -0
  154. package/src/frames/official/corner-tab.json +89 -0
  155. package/src/frames/official/crystal-ball.json +105 -0
  156. package/src/frames/official/delivery-box-light.json +132 -0
  157. package/src/frames/official/delivery-box-track.json +132 -0
  158. package/src/frames/official/fish-tank.json +276 -0
  159. package/src/frames/official/flag-light.json +136 -0
  160. package/src/frames/official/flag-team.json +127 -0
  161. package/src/frames/official/floppy-disk.json +106 -0
  162. package/src/frames/official/flying-saucer.json +128 -0
  163. package/src/frames/official/gallery-frame.json +195 -0
  164. package/src/frames/official/globe-stand.json +136 -0
  165. package/src/frames/official/gumball-machine.json +451 -0
  166. package/src/frames/official/heart-light.json +59 -0
  167. package/src/frames/official/heart-scan.json +51 -0
  168. package/src/frames/official/hexagon-light.json +87 -0
  169. package/src/frames/official/hexagon-scan.json +72 -0
  170. package/src/frames/official/hot-air-balloon.json +132 -0
  171. package/src/frames/official/jigsaw-piece-light.json +68 -0
  172. package/src/frames/official/jigsaw-piece.json +60 -0
  173. package/src/frames/official/laptop-screen.json +137 -0
  174. package/src/frames/official/lighthouse-tower.json +132 -0
  175. package/src/frames/official/magnifying-glass.json +85 -0
  176. package/src/frames/official/mailbox-flag.json +112 -0
  177. package/src/frames/official/mailbox-light.json +120 -0
  178. package/src/frames/official/map-pin-light.json +76 -0
  179. package/src/frames/official/map-pin.json +58 -0
  180. package/src/frames/official/matchbox-drawer.json +170 -0
  181. package/src/frames/official/microwave-door.json +220 -0
  182. package/src/frames/official/milk-carton.json +129 -0
  183. package/src/frames/official/monitor-screen.json +111 -0
  184. package/src/frames/official/monster-mouth.json +180 -0
  185. package/src/frames/official/mug-light.json +138 -0
  186. package/src/frames/official/mug-refill.json +125 -0
  187. package/src/frames/official/museum-plaque-light.json +187 -0
  188. package/src/frames/official/museum-plaque.json +187 -0
  189. package/src/frames/official/music-player-light.json +151 -0
  190. package/src/frames/official/music-player.json +136 -0
  191. package/src/frames/official/octagon-light.json +126 -0
  192. package/src/frames/official/octagon-sign.json +120 -0
  193. package/src/frames/official/open-book.json +156 -0
  194. package/src/frames/official/parking-meter.json +107 -0
  195. package/src/frames/official/passport-stamp.json +132 -0
  196. package/src/frames/official/pizza-box.json +122 -0
  197. package/src/frames/official/polka-dot-border.json +502 -0
  198. package/src/frames/official/popcorn-bucket.json +305 -0
  199. package/src/frames/official/porthole.json +264 -0
  200. package/src/frames/official/raised-card-thin.json +111 -0
  201. package/src/frames/official/raised-card.json +99 -0
  202. package/src/frames/official/retro-radio.json +180 -0
  203. package/src/frames/official/ribbon-top-light.json +125 -0
  204. package/src/frames/official/ribbon-top.json +113 -0
  205. package/src/frames/official/robot-chest.json +197 -0
  206. package/src/frames/official/roll-up-banner-light.json +137 -0
  207. package/src/frames/official/roll-up-banner.json +125 -0
  208. package/src/frames/official/round-tag-light.json +86 -0
  209. package/src/frames/official/round-tag.json +79 -0
  210. package/src/frames/official/sailboat-sail.json +99 -0
  211. package/src/frames/official/scroll-story.json +184 -0
  212. package/src/frames/official/sketch-border.json +92 -0
  213. package/src/frames/official/sketch-circle.json +57 -0
  214. package/src/frames/official/snow-globe.json +170 -0
  215. package/src/frames/official/soda-can.json +160 -0
  216. package/src/frames/official/spin-rays.json +126 -0
  217. package/src/frames/official/spiral-notebook.json +476 -0
  218. package/src/frames/official/stage-curtains.json +303 -0
  219. package/src/frames/official/starburst-light.json +128 -0
  220. package/src/frames/official/starburst.json +118 -0
  221. package/src/frames/official/swirl-border.json +122 -0
  222. package/src/frames/official/tablet-screen.json +130 -0
  223. package/src/frames/official/text-frame.json +79 -0
  224. package/src/frames/official/toaster-toast.json +114 -0
  225. package/src/frames/official/train-window.json +176 -0
  226. package/src/frames/official/treasure-map.json +207 -0
  227. package/src/frames/official/tv-set.json +256 -0
  228. package/src/frames/official/vending-machine.json +218 -0
  229. package/src/frames/official/video-player.json +118 -0
  230. package/src/frames/official/video-tape.json +137 -0
  231. package/src/frames/official/vinyl-sleeve-light.json +139 -0
  232. package/src/frames/official/vinyl-sleeve.json +127 -0
  233. package/src/frames/official/washing-machine.json +146 -0
  234. package/src/frames/official-types.ts +3 -1
  235. package/src/frames/official.ts +4 -3
  236. package/src/gif.ts +188 -0
  237. package/src/helpers.ts +12 -0
  238. package/src/ico.ts +55 -0
  239. package/src/index.tsx +2 -0
  240. package/src/looks.ts +467 -0
  241. package/src/plain.tsx +6 -0
  242. package/src/print.ts +12 -0
  243. package/src/qr-code-canvas.tsx +23 -2
  244. package/src/qr-code-props.ts +54 -19
  245. package/src/qr-code.tsx +7 -39
  246. package/src/qr-frame-svg.tsx +26 -0
  247. package/src/scan-score.ts +38 -6
  248. package/src/utils/gif.ts +632 -0
  249. package/src/utils/ico.ts +150 -0
  250. package/src/utils/parse-color.ts +163 -23
  251. package/src/utils/print-shared.ts +25 -1
  252. package/src/utils/svg-painter.ts +18 -0
  253. package/src/utils.tsx +136 -17
  254. package/src/vanilla-api.ts +2 -2
  255. package/src/vue.ts +7 -1
  256. package/types/constants.ts +47 -0
  257. package/types/plain.ts +19 -0
  258. package/types/qr.ts +98 -0
  259. package/types/react.ts +11 -1
  260. package/types/schemas.ts +5 -0
package/src/looks.ts ADDED
@@ -0,0 +1,467 @@
1
+ // The looks added around the code's shapes (feat/more-customization):
2
+ // per-eye shapes, the eyes' gradient span, the background's corner radius
3
+ // and backdrop, the halo and the shadow. Pure helpers the renderer, the
4
+ // scan score and editors share: each option's defaults and caps live here,
5
+ // so a value outside its range draws as the nearest one that scans.
6
+ import type {
7
+ CornerEyeDotPattern,
8
+ CornerEyePattern,
9
+ EyePosition,
10
+ QRCodeShape,
11
+ QRHalo,
12
+ QRProps,
13
+ QRShadow,
14
+ } from "../types";
15
+ import { normalizeColorConfig } from "../types/color-helpers";
16
+ import {
17
+ BG_RADIUS_MAX,
18
+ BG_RADIUS_QUIET_RATIO,
19
+ EYE_POSITIONS,
20
+ HALO_COLOR_DEFAULT,
21
+ HALO_LIGHT_MIN,
22
+ HALO_WIDTH_DEFAULT,
23
+ HALO_WIDTH_MAX,
24
+ HALO_WIDTH_MIN,
25
+ SHADOW_COLOR_DEFAULT,
26
+ SHADOW_OFFSET_DEFAULT,
27
+ SHADOW_OFFSET_MAX,
28
+ SHADOW_OPACITY_DEFAULT,
29
+ SHADOW_OPACITY_MAX,
30
+ SHADOW_OPACITY_MIN,
31
+ } from "../types/constants";
32
+ import qrcodegen from "./codegen";
33
+ import {
34
+ DEFAULT_BGCOLOR,
35
+ DEFAULT_MARGIN,
36
+ DEFAULT_SIZE,
37
+ ERROR_LEVEL_MAP,
38
+ } from "./constants";
39
+ import { capImageSettings } from "./logo-budget";
40
+ import { isShapedCode } from "./qr-shape";
41
+ import { getStylePairRisk, type StylePairRisk } from "./scan-risk";
42
+ import { resolveErrorLevel } from "./utils";
43
+ import { parseColor, type RGBAColor } from "./utils/parse-color";
44
+
45
+ type EyeShapeProps = Pick<
46
+ QRProps,
47
+ "cornerEyePattern" | "cornerEyeDotPattern" | "eyeShapes"
48
+ >;
49
+
50
+ /** QRCodeSVG's defaults: gear eyes with circle centres. */
51
+ const DEFAULT_EYE: CornerEyePattern = "gear";
52
+ const DEFAULT_DOT: CornerEyeDotPattern = "circle";
53
+
54
+ /**
55
+ * The frame (`outer`) and centre (`inner`) drawn at one eye: its own
56
+ * shapes from `eyeShapes`, else the code's `cornerEyePattern` and
57
+ * `cornerEyeDotPattern` (QRCodeSVG's defaults when neither is set).
58
+ */
59
+ export function eyeShapeOf(
60
+ props: EyeShapeProps,
61
+ position: EyePosition
62
+ ): { outer: CornerEyePattern; inner: CornerEyeDotPattern } {
63
+ const own = props.eyeShapes?.[position];
64
+ return {
65
+ outer: own?.outer ?? props.cornerEyePattern ?? DEFAULT_EYE,
66
+ inner: own?.inner ?? props.cornerEyeDotPattern ?? DEFAULT_DOT,
67
+ };
68
+ }
69
+
70
+ /** Eye frames from the most to the least fragile, for the eye-shape check. */
71
+ const EYE_FRAGILITY: readonly CornerEyePattern[] = ["curly", "circle"];
72
+
73
+ /**
74
+ * The most fragile eye frame drawn on the code, per-eye shapes included:
75
+ * `curly` before `circle` before any other (the scan score warns about
76
+ * those two), so a code with one circle eye is judged by it.
77
+ */
78
+ export function weakestEyePattern(props: EyeShapeProps): CornerEyePattern {
79
+ const outers = EYE_POSITIONS.map((position) => eyeShapeOf(props, position));
80
+ for (const fragile of EYE_FRAGILITY) {
81
+ if (outers.some((eye) => eye.outer === fragile)) {
82
+ return fragile;
83
+ }
84
+ }
85
+ return outers[0]?.outer ?? DEFAULT_EYE;
86
+ }
87
+
88
+ /**
89
+ * Risk kinds from the one that blocks a pick to the one that only warns.
90
+ * A weak eye on its own comes last: the scan score has its own eye-shape
91
+ * check for it, so a sharp-render pair on another eye still gets reported.
92
+ */
93
+ const RISK_SEVERITY: StylePairRisk["kind"][] = [
94
+ "dot-eye",
95
+ "body-eye",
96
+ "dot",
97
+ "sharp-dot-eye",
98
+ "eye",
99
+ ];
100
+
101
+ /**
102
+ * `getStylePairRisk` across the three eyes: each eye's frame and centre
103
+ * (per-eye shapes included) is checked with the body, and the worst risk
104
+ * wins (a pair decoders miss before a shape that only warns). Null when
105
+ * every eye pairs well.
106
+ */
107
+ export function getEyeStyleRisk(
108
+ props: Pick<QRProps, "bodyPattern"> & EyeShapeProps
109
+ ): StylePairRisk | null {
110
+ let worst: StylePairRisk | null = null;
111
+ const seen = new Set<string>();
112
+ for (const position of EYE_POSITIONS) {
113
+ const { outer, inner } = eyeShapeOf(props, position);
114
+ const key = `${outer}/${inner}`;
115
+ if (seen.has(key)) {
116
+ continue;
117
+ }
118
+ seen.add(key);
119
+ const risk = getStylePairRisk({
120
+ bodyPattern: props.bodyPattern,
121
+ cornerEyePattern: outer,
122
+ cornerEyeDotPattern: inner,
123
+ });
124
+ if (
125
+ risk &&
126
+ (!worst ||
127
+ RISK_SEVERITY.indexOf(risk.kind) < RISK_SEVERITY.indexOf(worst.kind))
128
+ ) {
129
+ worst = risk;
130
+ }
131
+ }
132
+ return worst;
133
+ }
134
+
135
+ /**
136
+ * The background's corner radius as drawn, as a share of the code's width:
137
+ * `radius` capped at BG_RADIUS_MAX and by the quiet zone, so the arc never
138
+ * reaches the finder rows (at most BG_RADIUS_QUIET_RATIO × `margin`
139
+ * modules of the `modules + 2 × margin` across the artwork). 0 without a
140
+ * margin, and for a shaped code, which has its own outline.
141
+ */
142
+ export function clampBgRadius(
143
+ radius: number | undefined,
144
+ modules: number,
145
+ margin: number,
146
+ shape?: QRCodeShape
147
+ ): number {
148
+ if (
149
+ !(radius && Number.isFinite(radius)) ||
150
+ radius <= 0 ||
151
+ isShapedCode(shape) ||
152
+ margin <= 0
153
+ ) {
154
+ return 0;
155
+ }
156
+ const limit = Math.min(
157
+ BG_RADIUS_MAX,
158
+ (BG_RADIUS_QUIET_RATIO * margin) / (modules + 2 * margin)
159
+ );
160
+ return Math.min(radius, limit);
161
+ }
162
+
163
+ /**
164
+ * The largest `bgRadius` (share of the code's width) this code keeps its
165
+ * quiet zone with, for an editor's slider: 0 when the code has no margin
166
+ * or is shaped. The level is the one the renderer draws at: given the
167
+ * logo (`imageSettings`) and `autoLevel`, the logo is held to its budget
168
+ * first and the level resolved with it after, as createQRCodeTree does.
169
+ */
170
+ export function getBgRadiusLimit(
171
+ props: Pick<
172
+ QRProps,
173
+ | "value"
174
+ | "level"
175
+ | "margin"
176
+ | "shape"
177
+ | "size"
178
+ | "autoLevel"
179
+ | "imageSettings"
180
+ > & { isOGContext?: boolean }
181
+ ): number {
182
+ const { value, level, shape, autoLevel, isOGContext } = props;
183
+ const size = props.size ?? DEFAULT_SIZE;
184
+ const margin = props.margin ?? DEFAULT_MARGIN;
185
+ const imageSettings = capImageSettings(
186
+ { value: `${value}`, size, margin, level, shape, autoLevel, isOGContext },
187
+ props.imageSettings
188
+ );
189
+ const drawn = resolveErrorLevel({
190
+ value: `${value}`,
191
+ size,
192
+ level,
193
+ margin,
194
+ imageSettings,
195
+ isOGContext,
196
+ autoLevel,
197
+ shape,
198
+ });
199
+ const modules = qrcodegen.QrCode.encodeText(
200
+ `${value}`,
201
+ ERROR_LEVEL_MAP[drawn]
202
+ ).size;
203
+ return clampBgRadius(BG_RADIUS_MAX, modules, margin, shape);
204
+ }
205
+
206
+ /** `value` kept between `min` and `max`; anything that isn't a finite number is `fallback`. */
207
+ const clamp = (
208
+ value: number | undefined,
209
+ fallback: number,
210
+ min: number,
211
+ max: number
212
+ ) =>
213
+ Math.min(
214
+ max,
215
+ Math.max(
216
+ min,
217
+ typeof value === "number" && Number.isFinite(value) ? value : fallback
218
+ )
219
+ );
220
+
221
+ const HSL_COLOR = /^hsla?\(([^)]*)\)$/;
222
+ // A number with an optional fraction: the digits before and after a "." can't
223
+ // both match the same characters, so "000…0x" fails in linear time (CodeQL
224
+ // polynomial-redos)
225
+ const HUE = /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:deg)?$/;
226
+ /** A CSS number or percentage, in the same linear shape. */
227
+ const CSS_NUMBER = /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)%?$/;
228
+
229
+ /** A CSS alpha or percentage as 0 to 1 ("50%" is 0.5); NaN when it isn't one. */
230
+ function cssShare(text: string | undefined, percentOnly: boolean): number {
231
+ const value = text?.trim() ?? "";
232
+ // parseFloat alone would read "0x" as 0
233
+ if (!CSS_NUMBER.test(value)) {
234
+ return Number.NaN;
235
+ }
236
+ const number = Number.parseFloat(value);
237
+ const share = value.endsWith("%") || percentOnly ? number / 100 : number;
238
+ return Math.min(1, Math.max(0, share));
239
+ }
240
+
241
+ /** hsl()/hsla() as RGBA (comma or space syntax), or null. */
242
+ function parseHsl(value: string): RGBAColor | null {
243
+ const hsl = HSL_COLOR.exec(value);
244
+ if (!hsl) {
245
+ return null;
246
+ }
247
+ const [channels = "", slashed] = (hsl[1] ?? "").split("/");
248
+ const parts = channels.split(/[\s,]+/).filter(Boolean);
249
+ if (parts.length !== (slashed === undefined && parts.length === 4 ? 4 : 3)) {
250
+ return null;
251
+ }
252
+ const [hueText = "", saturationText, lightnessText, alphaText] = parts;
253
+ // Saturation and lightness are percentages, with or without the "%"
254
+ const saturation = cssShare(saturationText, true);
255
+ const lightness = cssShare(lightnessText, true);
256
+ const alpha = cssShare(slashed ?? alphaText ?? "1", false);
257
+ const hue = Number.parseFloat(hueText);
258
+ if (
259
+ !(
260
+ HUE.test(hueText) && [saturation, lightness, alpha].every(Number.isFinite)
261
+ )
262
+ ) {
263
+ return null;
264
+ }
265
+ const a = saturation * Math.min(lightness, 1 - lightness);
266
+ const channel = (n: number) => {
267
+ const k = (n + hue / 30) % 12;
268
+ const at = k < 0 ? k + 12 : k;
269
+ return 255 * (lightness - a * Math.max(-1, Math.min(at - 3, 9 - at, 1)));
270
+ };
271
+ return { r: channel(0), g: channel(8), b: channel(4), a: alpha };
272
+ }
273
+
274
+ /**
275
+ * A color as the halo rule reads it: parseColor's forms, hsl()/hsla()
276
+ * (which the API accepts too), and "none", which paints nothing. Null for
277
+ * anything else.
278
+ */
279
+ function readColor(color: string | undefined): RGBAColor | null {
280
+ const value = color?.trim().toLowerCase() ?? "";
281
+ if (value === "none") {
282
+ return { r: 0, g: 0, b: 0, a: 0 };
283
+ }
284
+ return parseColor(value) ?? parseHsl(value);
285
+ }
286
+
287
+ /**
288
+ * A color's alpha as it paints, 0 to 1: "none" and "transparent" paint
289
+ * nothing; hex, rgb()/rgba() and hsl()/hsla() (comma or space syntax,
290
+ * alpha as a number or a percentage) read their alpha. Anything else (a
291
+ * color name, or a value no renderer paints, which draws as the inherited
292
+ * fill) is opaque.
293
+ */
294
+ export function colorAlpha(color: string | undefined): number {
295
+ return readColor(color)?.a ?? 1;
296
+ }
297
+
298
+ /**
299
+ * True when something under the modules can show through the background:
300
+ * any alpha below 1 anywhere in its paint (the color, any gradient stop),
301
+ * or an image fill without an opaque underlay (the picture itself may be
302
+ * see-through, or faded by its opacity). No background is the default
303
+ * white.
304
+ */
305
+ export function isSeeThroughBackground(bgColor: QRProps["bgColor"]): boolean {
306
+ const paint = normalizeColorConfig(bgColor, DEFAULT_BGCOLOR);
307
+ if (paint.type === "solid") {
308
+ return colorAlpha(paint.color) < 1;
309
+ }
310
+ if (paint.type === "image") {
311
+ return colorAlpha(paint.underlay ?? "transparent") < 1;
312
+ }
313
+ return paint.stops.some((stop) => colorAlpha(stop.color) < 1);
314
+ }
315
+
316
+ /**
317
+ * How light a color reads to decoders, 0 to 255: the darker of the two
318
+ * grey conversions they use (Rec. 709 weights, as jsQR, and Rec. 601, as
319
+ * ZXing), so a saturated color is judged as its darkest reading.
320
+ */
321
+ const decoderLuma = ({ r, g, b }: RGBAColor) =>
322
+ Math.min(
323
+ 0.2126 * r + 0.7152 * g + 0.0722 * b,
324
+ 0.299 * r + 0.587 * g + 0.114 * b
325
+ );
326
+
327
+ const HALO_LIGHT_LUMA = decoderLuma(parseColor(HALO_LIGHT_MIN) as RGBAColor);
328
+
329
+ /** How light a color reads (decoderLuma), or null when it can't be read. */
330
+ const lumaOf = (color: string | undefined) => {
331
+ const parsed = readColor(color);
332
+ return parsed ? decoderLuma(parsed) : null;
333
+ };
334
+
335
+ /** The rim color as drawn: opaque, the default when it can't be read. */
336
+ function haloColorOf(halo: QRHalo): string {
337
+ const parsed = readColor(halo.color || HALO_COLOR_DEFAULT);
338
+ return parsed && parsed.a > 0
339
+ ? `#${[parsed.r, parsed.g, parsed.b]
340
+ .map((channel) => Math.round(channel).toString(16).padStart(2, "0"))
341
+ .join("")}`
342
+ : HALO_COLOR_DEFAULT;
343
+ }
344
+
345
+ export interface HaloRisk {
346
+ /** Which part of the design refuses it */
347
+ reason: "see-through" | "picture" | "dark-background" | "rim";
348
+ message: string;
349
+ fix: string;
350
+ }
351
+
352
+ /**
353
+ * Why a halo isn't drawn on this code, or null when it is (or there is
354
+ * none). A halo is drawn only where it was measured never to cost a decode
355
+ * (tests/halo-decode.test.ts): on an opaque background, in a rim at least
356
+ * as light as HALO_LIGHT_MIN and no lighter than any background color (the
357
+ * color, every gradient stop). A rim lighter than the background raises
358
+ * the light level around every dot, and decoders then misread light
359
+ * modules: at every width on a grey page under a see-through code, and
360
+ * with grey or colored dots on a light grey background (measured 21 Nov
361
+ * 2026, every dot shape, widths 0.1 to 0.5, 4, 6 and 9 px per module,
362
+ * four rasteriser and decoder pipelines). A see-through background shows
363
+ * whatever is under the code, and a picture its own tones, so neither
364
+ * takes a halo.
365
+ */
366
+ export function getHaloRisk(
367
+ props: Pick<QRProps, "halo" | "bgColor">
368
+ ): HaloRisk | null {
369
+ if (!props.halo) {
370
+ return null;
371
+ }
372
+ const off = "or turn the halo off";
373
+ const paint = normalizeColorConfig(props.bgColor, DEFAULT_BGCOLOR);
374
+ if (paint.type === "image") {
375
+ return {
376
+ reason: "picture",
377
+ message:
378
+ "A halo isn't drawn on a picture background: on the picture's darker parts the rim makes the code unreadable",
379
+ fix: `Use a background color, ${off}`,
380
+ };
381
+ }
382
+ if (isSeeThroughBackground(props.bgColor)) {
383
+ return {
384
+ reason: "see-through",
385
+ message:
386
+ "A halo isn't drawn on a see-through background: on a grey surface under the code the rim makes it unreadable",
387
+ fix: `Give the code an opaque background, ${off}`,
388
+ };
389
+ }
390
+ const grounds = (
391
+ paint.type === "solid"
392
+ ? [paint.color]
393
+ : paint.stops.map((stop) => stop.color)
394
+ ).map(lumaOf);
395
+ const ground = grounds.includes(null)
396
+ ? null
397
+ : Math.min(...(grounds as number[]));
398
+ if (ground === null || ground < HALO_LIGHT_LUMA - 1e-9) {
399
+ return {
400
+ reason: "dark-background",
401
+ message: `A halo is drawn only on a background at least as light as ${HALO_LIGHT_MIN}: on a darker one the rim makes the code unreadable`,
402
+ fix: `Lighten the background to ${HALO_LIGHT_MIN} or lighter, ${off}`,
403
+ };
404
+ }
405
+ const rim = lumaOf(haloColorOf(props.halo)) as number;
406
+ if (rim < HALO_LIGHT_LUMA - 1e-9 || rim > ground + 1e-9) {
407
+ return {
408
+ reason: "rim",
409
+ message: `A halo is drawn only in a color between ${HALO_LIGHT_MIN} and the background's own: a rim lighter than the background, or darker than ${HALO_LIGHT_MIN}, makes the code unreadable`,
410
+ fix: `Pick a halo color no lighter than the background and at least as light as ${HALO_LIGHT_MIN}, ${off}`,
411
+ };
412
+ }
413
+ return null;
414
+ }
415
+
416
+ /**
417
+ * A halo with its defaults filled in, its width in range and its rim
418
+ * opaque; null without one, or when `code` is given and the halo is
419
+ * refused on it (getHaloRisk).
420
+ */
421
+ export function resolveHalo(
422
+ halo: QRHalo | undefined,
423
+ code?: Pick<QRProps, "bgColor">
424
+ ): { color: string; width: number } | null {
425
+ if (!halo || (code && getHaloRisk({ halo, bgColor: code.bgColor }))) {
426
+ return null;
427
+ }
428
+ const width = clamp(
429
+ halo.width,
430
+ HALO_WIDTH_DEFAULT,
431
+ HALO_WIDTH_MIN,
432
+ HALO_WIDTH_MAX
433
+ );
434
+ // The rim is always opaque: a see-through rim would compound where rims
435
+ // overlap in the print PDF, which paints each shape on its own
436
+ return { color: haloColorOf(halo), width };
437
+ }
438
+
439
+ /**
440
+ * A shadow with its defaults filled in and its shift and opacity in range;
441
+ * null without one, or when it wouldn't move (dx and dy both 0).
442
+ */
443
+ export function resolveShadow(
444
+ shadow: QRShadow | undefined
445
+ ): { color: string; opacity: number; dx: number; dy: number } | null {
446
+ if (!shadow) {
447
+ return null;
448
+ }
449
+ const shift = (value: number | undefined) =>
450
+ clamp(value, SHADOW_OFFSET_DEFAULT, -SHADOW_OFFSET_MAX, SHADOW_OFFSET_MAX);
451
+ const dx = shift(shadow.dx);
452
+ const dy = shift(shadow.dy);
453
+ if (dx === 0 && dy === 0) {
454
+ return null;
455
+ }
456
+ return {
457
+ color: shadow.color || SHADOW_COLOR_DEFAULT,
458
+ opacity: clamp(
459
+ shadow.opacity,
460
+ SHADOW_OPACITY_DEFAULT,
461
+ SHADOW_OPACITY_MIN,
462
+ SHADOW_OPACITY_MAX
463
+ ),
464
+ dx,
465
+ dy,
466
+ };
467
+ }
package/src/plain.tsx CHANGED
@@ -67,6 +67,12 @@ export async function preparePlainQRCode(
67
67
  art: _art,
68
68
  eyeColors: _eyeColors,
69
69
  moduleScale: _moduleScale,
70
+ eyeShapes: _eyeShapes,
71
+ eyeGradient: _eyeGradient,
72
+ bgRadius: _bgRadius,
73
+ backdrop: _backdrop,
74
+ halo: _halo,
75
+ shadow: _shadow,
70
76
  frameLabelMarkers: _frameLabelMarkers,
71
77
  partMarkers: _partMarkers,
72
78
  autoLevel,
package/src/print.ts CHANGED
@@ -38,6 +38,7 @@ import {
38
38
  imageFormat,
39
39
  imagePlacement,
40
40
  length,
41
+ lineStyleOf,
41
42
  type PaintState,
42
43
  rasterizeImage,
43
44
  SvgPainter,
@@ -432,9 +433,16 @@ class PdfPainter extends SvgPainter {
432
433
  if (fill) {
433
434
  this.setColor("fill", fill);
434
435
  }
436
+ // Round joins and caps where the SVG asks for them (the halo's rim),
437
+ // set back to the defaults after this shape
438
+ const line = stroke ? lineStyleOf(state) : { join: 0, cap: 0 };
435
439
  if (stroke) {
436
440
  this.setColor("draw", stroke);
437
441
  this.pdf.setLineWidth(state.strokeWidth * matrixScale(state.matrix));
442
+ if (line.join || line.cap) {
443
+ this.pdf.setLineJoin(line.join);
444
+ this.pdf.setLineCap(line.cap);
445
+ }
438
446
  }
439
447
  this.tracePath(segments, state.matrix);
440
448
  if (fill && stroke) {
@@ -452,6 +460,10 @@ class PdfPainter extends SvgPainter {
452
460
  } else {
453
461
  this.pdf.stroke();
454
462
  }
463
+ if (line.join || line.cap) {
464
+ this.pdf.setLineJoin(0);
465
+ this.pdf.setLineCap(0);
466
+ }
455
467
  });
456
468
  }
457
469
 
@@ -24,6 +24,7 @@ import {
24
24
  import { drawFrameOnCanvas, getFrameLayout, isFrameActive } from "./frame";
25
25
  import { logoImageBox, traceLogoShape } from "./logo";
26
26
  import { capImageSettings } from "./logo-budget";
27
+ import { clampBgRadius } from "./looks";
27
28
  import { getLayoutMargin, shapeBackground, shapeDecoration } from "./qr-shape";
28
29
  import {
29
30
  excavateModules,
@@ -59,10 +60,16 @@ export function QRCodeCanvas(props: QRPropsCanvas) {
59
60
  art: _art,
60
61
  eyeColors: _eyeColors,
61
62
  moduleScale: _moduleScale,
62
- frameLabelMarkers: _frameLabelMarkers,
63
- partMarkers: _partMarkers,
63
+ eyeShapes: _eyeShapes,
64
+ eyeGradient: _eyeGradient,
65
+ halo: _halo,
66
+ shadow: _shadow,
64
67
  autoLevel,
65
68
  shape,
69
+ bgRadius,
70
+ backdrop,
71
+ frameLabelMarkers: _frameLabelMarkers,
72
+ partMarkers: _partMarkers,
66
73
  ...otherProps
67
74
  } = props;
68
75
  // A logo no bigger than the code can repair (logo-budget.ts)
@@ -153,12 +160,26 @@ export function QRCodeCanvas(props: QRPropsCanvas) {
153
160
  // underlay (QRCodeSVG and the exports draw them in full).
154
161
  ctx.fillStyle = getSolidColor(bgColor, DEFAULT_BGCOLOR);
155
162
  const background = shapeBackground(shape, numCells);
163
+ // Rounded corners, capped by the quiet zone (looks.ts), with the
164
+ // backdrop color showing outside them
165
+ const radius =
166
+ clampBgRadius(bgRadius, cells.length, layoutMargin, shape) * numCells;
156
167
  if (background && "circle" in background) {
157
168
  ctx.beginPath();
158
169
  ctx.arc(numCells / 2, numCells / 2, background.circle, 0, 2 * Math.PI);
159
170
  ctx.fill();
160
171
  } else if (background && SUPPORTS_PATH2D) {
161
172
  ctx.fill(new Path2D(background.path), "evenodd");
173
+ } else if (radius > 0 && typeof ctx.roundRect === "function") {
174
+ if (backdrop) {
175
+ ctx.save();
176
+ ctx.fillStyle = backdrop;
177
+ ctx.fillRect(0, 0, numCells, numCells);
178
+ ctx.restore();
179
+ }
180
+ ctx.beginPath();
181
+ ctx.roundRect(0, 0, numCells, numCells, radius);
182
+ ctx.fill();
162
183
  } else {
163
184
  ctx.fillRect(0, 0, numCells, numCells);
164
185
  }
@@ -57,6 +57,12 @@ const QR_CODE_PROP_KEYS = {
57
57
  moduleScale: true,
58
58
  autoLevel: true,
59
59
  shape: true,
60
+ eyeShapes: true,
61
+ eyeGradient: true,
62
+ bgRadius: true,
63
+ backdrop: true,
64
+ halo: true,
65
+ shadow: true,
60
66
  frameLabelMarkers: true,
61
67
  partMarkers: true,
62
68
  hideLogo: true,
@@ -81,26 +87,45 @@ function rootAttributes(props: QRCodeOptions): QRSVGAttributes {
81
87
 
82
88
  type QRData = ReturnType<typeof getQRData>;
83
89
 
84
- /** What `getQRData` lays out for `QRCode`'s props. */
90
+ /**
91
+ * Every prop `getQRCodeData` lays the code out from, in one list. React's
92
+ * `QRCode` keeps its layout until one of these changes, so a prop the layout
93
+ * reads can't be left out of that check: the looks once were (the per-eye
94
+ * shapes, the eyes' gradient span, the rounded background and its backdrop,
95
+ * the halo and the shadow), and a code drawn without them stayed on screen
96
+ * after one was turned on.
97
+ */
98
+ export const QR_CODE_DATA_KEYS = [
99
+ "value",
100
+ "fgColor",
101
+ "hideLogo",
102
+ "bgColor",
103
+ "eyeColor",
104
+ "dotColor",
105
+ "bodyPattern",
106
+ "cornerEyePattern",
107
+ "cornerEyeDotPattern",
108
+ "level",
109
+ "templateId",
110
+ "logo",
111
+ "logoOptions",
112
+ "margin",
113
+ "autoLevel",
114
+ "shape",
115
+ "eyeShapes",
116
+ "eyeGradient",
117
+ "bgRadius",
118
+ "backdrop",
119
+ "halo",
120
+ "shadow",
121
+ ] as const satisfies readonly (keyof QRCodeProps)[];
122
+
123
+ /** What `getQRData` lays out for `QRCode`'s props (QR_CODE_DATA_KEYS). */
85
124
  export function getQRCodeData(props: QRCodeProps): QRData {
86
- return getQRData({
87
- value: props.value,
88
- fgColor: props.fgColor,
89
- hideLogo: props.hideLogo,
90
- bgColor: props.bgColor,
91
- eyeColor: props.eyeColor,
92
- dotColor: props.dotColor,
93
- bodyPattern: props.bodyPattern,
94
- cornerEyePattern: props.cornerEyePattern,
95
- cornerEyeDotPattern: props.cornerEyeDotPattern,
96
- level: props.level,
97
- templateId: props.templateId,
98
- logo: props.logo,
99
- logoOptions: props.logoOptions,
100
- margin: props.margin ?? DEFAULT_MARGIN,
101
- autoLevel: props.autoLevel,
102
- shape: props.shape,
103
- });
125
+ const read = Object.fromEntries(
126
+ QR_CODE_DATA_KEYS.map((key) => [key, props[key]])
127
+ ) as Pick<QRCodeProps, (typeof QR_CODE_DATA_KEYS)[number]>;
128
+ return getQRData({ ...read, margin: props.margin ?? DEFAULT_MARGIN });
104
129
  }
105
130
 
106
131
  /**
@@ -128,6 +153,15 @@ export function getQRCodeSVGProps(
128
153
  frameLabelMarkers,
129
154
  partMarkers,
130
155
  } = props;
156
+ // The looks around the shapes, as getQRData kept them
157
+ const looks = {
158
+ ...(qrData.eyeShapes && { eyeShapes: qrData.eyeShapes }),
159
+ ...(qrData.eyeGradient && { eyeGradient: qrData.eyeGradient }),
160
+ ...(qrData.bgRadius && { bgRadius: qrData.bgRadius }),
161
+ ...(qrData.backdrop && { backdrop: qrData.backdrop }),
162
+ ...(qrData.halo && { halo: qrData.halo }),
163
+ ...(qrData.shadow && { shadow: qrData.shadow }),
164
+ };
131
165
  // getQRData lays the logo out on a 1024px canvas; scale it to the rendered size.
132
166
  const renderSize = size * scale;
133
167
  // Pixels of the 1024px layout → pixels of the rendered code
@@ -165,6 +199,7 @@ export function getQRCodeSVGProps(
165
199
  ...(moduleScale !== undefined && { moduleScale }),
166
200
  ...(autoLevel && { autoLevel }),
167
201
  ...(shape && shape !== "square" && { shape }),
202
+ ...looks,
168
203
  ...(frameLabelMarkers && { frameLabelMarkers }),
169
204
  ...(partMarkers && { partMarkers }),
170
205
  size: renderSize,