qrdx 0.0.55 → 0.0.57

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 (83) hide show
  1. package/README.md +9 -4
  2. package/dist/angular.js +1 -1
  3. package/dist/angular.mjs +1 -1
  4. package/dist/core.js +7 -7
  5. package/dist/core.js.map +1 -1
  6. package/dist/core.mjs +7 -7
  7. package/dist/core.mjs.map +1 -1
  8. package/dist/element.browser.mjs +6 -6
  9. package/dist/element.browser.mjs.map +1 -1
  10. package/dist/index.js +7 -7
  11. package/dist/index.js.map +1 -1
  12. package/dist/index.mjs +7 -7
  13. package/dist/index.mjs.map +1 -1
  14. package/dist/preact.js +1 -1
  15. package/dist/preact.js.map +1 -1
  16. package/dist/preact.mjs +1 -1
  17. package/dist/preact.mjs.map +1 -1
  18. package/dist/solid.js +1 -1
  19. package/dist/solid.js.map +1 -1
  20. package/dist/solid.mjs +1 -1
  21. package/dist/solid.mjs.map +1 -1
  22. package/dist/src/angular.d.mts +1 -0
  23. package/dist/src/angular.d.ts +2 -2
  24. package/dist/src/bmp.d.ts +7 -0
  25. package/dist/src/bmp.d.ts.map +1 -0
  26. package/dist/src/core.d.mts +1 -0
  27. package/dist/src/download.d.ts +12 -5
  28. package/dist/src/download.d.ts.map +1 -1
  29. package/dist/src/element-define.d.mts +1 -0
  30. package/dist/src/element.d.mts +1 -0
  31. package/dist/src/element.d.ts +1 -1
  32. package/dist/src/element.d.ts.map +1 -1
  33. package/dist/src/engine.d.ts +2 -1
  34. package/dist/src/engine.d.ts.map +1 -1
  35. package/dist/src/export.d.ts.map +1 -1
  36. package/dist/src/index.d.mts +1 -0
  37. package/dist/src/index.d.ts +1 -1
  38. package/dist/src/index.d.ts.map +1 -1
  39. package/dist/src/node.d.mts +1 -0
  40. package/dist/src/preact.d.mts +1 -0
  41. package/dist/src/preact.d.ts +5 -4
  42. package/dist/src/preact.d.ts.map +1 -1
  43. package/dist/src/qr-code-props.d.ts +19 -6
  44. package/dist/src/qr-code-props.d.ts.map +1 -1
  45. package/dist/src/qr-code.d.ts +7 -2
  46. package/dist/src/qr-code.d.ts.map +1 -1
  47. package/dist/src/scan-score.d.ts.map +1 -1
  48. package/dist/src/solid.d.mts +1 -0
  49. package/dist/src/solid.d.ts +5 -4
  50. package/dist/src/solid.d.ts.map +1 -1
  51. package/dist/src/utils/bmp.d.ts +18 -0
  52. package/dist/src/utils/bmp.d.ts.map +1 -0
  53. package/dist/src/vanilla-api.d.ts +2 -2
  54. package/dist/src/vanilla-api.d.ts.map +1 -1
  55. package/dist/src/vanilla.d.mts +1 -0
  56. package/dist/src/vue.d.mts +1 -0
  57. package/dist/src/vue.d.ts +2 -2
  58. package/dist/src/vue.d.ts.map +1 -1
  59. package/dist/types/public.d.mts +1 -0
  60. package/dist/types/public.d.ts +1 -1
  61. package/dist/types/public.d.ts.map +1 -1
  62. package/dist/types/react.d.ts +6 -0
  63. package/dist/types/react.d.ts.map +1 -1
  64. package/dist/vanilla.browser.mjs +27 -27
  65. package/dist/vanilla.browser.mjs.map +1 -1
  66. package/package.json +49 -15
  67. package/src/angular.ts +2 -2
  68. package/src/bmp.ts +25 -0
  69. package/src/download.tsx +40 -5
  70. package/src/element.ts +1 -1
  71. package/src/engine.ts +2 -0
  72. package/src/export.ts +6 -0
  73. package/src/index.tsx +5 -1
  74. package/src/preact.ts +5 -4
  75. package/src/qr-code-props.ts +77 -6
  76. package/src/qr-code.tsx +8 -6
  77. package/src/scan-score.ts +40 -32
  78. package/src/solid.ts +5 -4
  79. package/src/utils/bmp.ts +126 -0
  80. package/src/vanilla-api.ts +2 -2
  81. package/src/vue.ts +2 -2
  82. package/types/public.ts +1 -1
  83. package/types/react.ts +6 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "qrdx",
3
- "version": "0.0.55",
3
+ "version": "0.0.57",
4
4
  "description": "Customizable QR code generator for React and plain JavaScript: SVG and canvas rendering, dot and eye patterns, gradients, logos, frames, Logo QR and Image QR art, PNG/JPG/WebP/SVG/PDF/EPS export, a scan score and QR scanning.",
5
5
  "keywords": [
6
6
  "qr-code",
@@ -37,12 +37,46 @@
37
37
  "main": "./dist/index.js",
38
38
  "module": "./dist/index.mjs",
39
39
  "types": "./dist/src/index.d.ts",
40
+ "typesVersions": {
41
+ "*": {
42
+ "core": [
43
+ "./dist/src/core.d.ts"
44
+ ],
45
+ "vanilla": [
46
+ "./dist/src/vanilla.d.ts"
47
+ ],
48
+ "element": [
49
+ "./dist/src/element.d.ts"
50
+ ],
51
+ "element/define": [
52
+ "./dist/src/element-define.d.ts"
53
+ ],
54
+ "node": [
55
+ "./dist/src/node.d.ts"
56
+ ],
57
+ "vue": [
58
+ "./dist/src/vue.d.ts"
59
+ ],
60
+ "types": [
61
+ "./dist/types/public.d.ts"
62
+ ],
63
+ "preact": [
64
+ "./dist/src/preact.d.ts"
65
+ ],
66
+ "solid": [
67
+ "./dist/src/solid.d.ts"
68
+ ],
69
+ "angular": [
70
+ "./dist/src/angular.d.ts"
71
+ ]
72
+ }
73
+ },
40
74
  "unpkg": "./dist/vanilla.browser.mjs",
41
75
  "jsdelivr": "./dist/vanilla.browser.mjs",
42
76
  "exports": {
43
77
  ".": {
44
78
  "import": {
45
- "types": "./dist/src/index.d.ts",
79
+ "types": "./dist/src/index.d.mts",
46
80
  "default": "./dist/index.mjs"
47
81
  },
48
82
  "require": {
@@ -52,7 +86,7 @@
52
86
  },
53
87
  "./core": {
54
88
  "import": {
55
- "types": "./dist/src/core.d.ts",
89
+ "types": "./dist/src/core.d.mts",
56
90
  "default": "./dist/core.mjs"
57
91
  },
58
92
  "require": {
@@ -62,7 +96,7 @@
62
96
  },
63
97
  "./vanilla": {
64
98
  "import": {
65
- "types": "./dist/src/vanilla.d.ts",
99
+ "types": "./dist/src/vanilla.d.mts",
66
100
  "default": "./dist/vanilla.mjs"
67
101
  },
68
102
  "require": {
@@ -72,7 +106,7 @@
72
106
  },
73
107
  "./element": {
74
108
  "import": {
75
- "types": "./dist/src/element.d.ts",
109
+ "types": "./dist/src/element.d.mts",
76
110
  "default": "./dist/element.mjs"
77
111
  },
78
112
  "require": {
@@ -82,7 +116,7 @@
82
116
  },
83
117
  "./element/define": {
84
118
  "import": {
85
- "types": "./dist/src/element-define.d.ts",
119
+ "types": "./dist/src/element-define.d.mts",
86
120
  "default": "./dist/element-define.mjs"
87
121
  },
88
122
  "require": {
@@ -92,7 +126,7 @@
92
126
  },
93
127
  "./node": {
94
128
  "import": {
95
- "types": "./dist/src/node.d.ts",
129
+ "types": "./dist/src/node.d.mts",
96
130
  "default": "./dist/node.mjs"
97
131
  },
98
132
  "require": {
@@ -102,7 +136,7 @@
102
136
  },
103
137
  "./vue": {
104
138
  "import": {
105
- "types": "./dist/src/vue.d.ts",
139
+ "types": "./dist/src/vue.d.mts",
106
140
  "default": "./dist/vue.mjs"
107
141
  },
108
142
  "require": {
@@ -112,7 +146,7 @@
112
146
  },
113
147
  "./types": {
114
148
  "import": {
115
- "types": "./dist/types/public.d.ts",
149
+ "types": "./dist/types/public.d.mts",
116
150
  "default": "./dist/types/index.mjs"
117
151
  },
118
152
  "require": {
@@ -123,7 +157,7 @@
123
157
  "./package.json": "./package.json",
124
158
  "./preact": {
125
159
  "import": {
126
- "types": "./dist/src/preact.d.ts",
160
+ "types": "./dist/src/preact.d.mts",
127
161
  "default": "./dist/preact.mjs"
128
162
  },
129
163
  "require": {
@@ -133,7 +167,7 @@
133
167
  },
134
168
  "./solid": {
135
169
  "import": {
136
- "types": "./dist/src/solid.d.ts",
170
+ "types": "./dist/src/solid.d.mts",
137
171
  "default": "./dist/solid.mjs"
138
172
  },
139
173
  "require": {
@@ -143,7 +177,7 @@
143
177
  },
144
178
  "./angular": {
145
179
  "import": {
146
- "types": "./dist/src/angular.d.ts",
180
+ "types": "./dist/src/angular.d.mts",
147
181
  "default": "./dist/angular.mjs"
148
182
  },
149
183
  "require": {
@@ -158,8 +192,8 @@
158
192
  "src"
159
193
  ],
160
194
  "scripts": {
161
- "build": "node -e \"require('node:fs').rmSync('dist',{recursive:true,force:true})\" && tsup && tsc --project tsconfig.build.json --emitDeclarationOnly",
162
- "build:inplace": "tsup && tsc --project tsconfig.build.json --emitDeclarationOnly",
195
+ "build": "node -e \"require('node:fs').rmSync('dist',{recursive:true,force:true})\" && tsup && tsc --project tsconfig.build.json --emitDeclarationOnly && node scripts/esm-types.mjs",
196
+ "build:inplace": "tsup && tsc --project tsconfig.build.json --emitDeclarationOnly && node scripts/esm-types.mjs",
163
197
  "clean": "git clean -xdf .cache .turbo dist node_modules",
164
198
  "typecheck": "tsc --noEmit",
165
199
  "test": "vitest run",
@@ -179,7 +213,7 @@
179
213
  "@types/react-dom": "19.3.0",
180
214
  "jsdom": "28.1.0",
181
215
  "jspdf": "^4.2.1",
182
- "next": "16.4.3",
216
+ "next": "16.4.5",
183
217
  "react": "^19.3.1",
184
218
  "sharp": "0.35.5",
185
219
  "tsup": "^9.0.0",
package/src/angular.ts CHANGED
@@ -42,7 +42,7 @@ export interface QRCodeExportOptions {
42
42
  }
43
43
 
44
44
  export interface QRCodeDownloadOptions extends QRCodeExportOptions {
45
- /** "png" (default), "jpg", "webp", "svg", "pdf" (needs jspdf) or "eps" */
45
+ /** "png" (default), "jpg", "webp", "bmp", "svg", "pdf" (needs jspdf) or "eps" */
46
46
  format?: DownloadFormat;
47
47
  /** File name; defaults to one with the format and pixel size */
48
48
  filename?: string;
@@ -171,7 +171,7 @@ export class QRCode implements QRCodeInputs {
171
171
  return this.element;
172
172
  }
173
173
 
174
- /** The code as a data URL: "png" (default), "jpg", "webp", "svg", "pdf" or "eps" */
174
+ /** The code as a data URL: "png" (default), "jpg", "webp", "bmp", "svg", "pdf" or "eps" */
175
175
  toDataURL(
176
176
  format: DownloadFormat = "png",
177
177
  options: QRCodeExportOptions = {}
package/src/bmp.ts ADDED
@@ -0,0 +1,25 @@
1
+ // BMP from a canvas in any browser. No browser encodes BMP from a canvas
2
+ // (toBlob("image/bmp") returns a PNG), so the pixels are read back and written
3
+ // by the built-in encoder (utils/bmp.ts). The file is checked by its bytes
4
+ // before it is returned, so a PNG is never passed off as BMP.
5
+
6
+ import { encodeBMP, isBMPFile } from "./utils/bmp";
7
+
8
+ export { BMP_MAX_DIMENSION, encodeBMP, isBMPFile } from "./utils/bmp";
9
+
10
+ /**
11
+ * A canvas as a 24-bit BMP. A transparent background is laid over white,
12
+ * since 24-bit BMP has no alpha channel.
13
+ */
14
+ export function canvasToBMP(canvas: HTMLCanvasElement): Blob {
15
+ const context = canvas.getContext("2d");
16
+ if (!context) {
17
+ throw new Error("Could not get canvas context");
18
+ }
19
+ const { data } = context.getImageData(0, 0, canvas.width, canvas.height);
20
+ const file = encodeBMP(data, canvas.width, canvas.height);
21
+ if (!isBMPFile(file)) {
22
+ throw new Error("Could not write a BMP file");
23
+ }
24
+ return new Blob([file as BlobPart], { type: "image/bmp" });
25
+ }
package/src/download.tsx CHANGED
@@ -1,4 +1,5 @@
1
1
  import type { QRProps } from "../types";
2
+ import { canvasToBMP } from "./bmp";
2
3
  import { DEFAULT_SIZE } from "./constants";
3
4
  import { epsToDataUrl, getQRAsEPS } from "./eps";
4
5
  import { getQRAsCanvas, getQRAsSVGDataUri } from "./export";
@@ -6,10 +7,18 @@ import { getFrameDimensions } from "./frame";
6
7
  import { blobToDataUrl, canvasToWebP, type WebPOptions } from "./webp";
7
8
 
8
9
  /**
9
- * "webp" is lossless unless `quality` is set. "eps" is vector PostScript:
10
- * `size.width` becomes points (72 per inch).
10
+ * "webp" is lossless unless `quality` is set. "bmp" is an uncompressed
11
+ * 24-bit bitmap: a transparent background comes out white. "eps" is vector
12
+ * PostScript: `size.width` becomes points (72 per inch).
11
13
  */
12
- export type DownloadFormat = "png" | "jpg" | "webp" | "svg" | "pdf" | "eps";
14
+ export type DownloadFormat =
15
+ | "png"
16
+ | "jpg"
17
+ | "webp"
18
+ | "bmp"
19
+ | "svg"
20
+ | "pdf"
21
+ | "eps";
13
22
 
14
23
  export interface DownloadFormatInfo {
15
24
  mimeType: string;
@@ -22,13 +31,15 @@ export interface DownloadFormatInfo {
22
31
  * Every download format with its MIME type and file extension. Each is made
23
32
  * by one branch of downloadQRCode and getQRCodeDataUrl: PNG and JPG by the
24
33
  * canvas, WebP by canvasToWebP (the canvas, or the built-in lossless
25
- * encoder), SVG from the markup, PDF with jspdf, EPS by getQRAsEPS. A new
34
+ * encoder), BMP by canvasToBMP (the built-in encoder: no browser writes it),
35
+ * SVG from the markup, PDF with jspdf, EPS by getQRAsEPS. A new
26
36
  * format needs an entry here and a branch there.
27
37
  */
28
38
  export const DOWNLOAD_FORMATS: Record<DownloadFormat, DownloadFormatInfo> = {
29
39
  png: { mimeType: "image/png", extension: "png", vector: false },
30
40
  jpg: { mimeType: "image/jpeg", extension: "jpg", vector: false },
31
41
  webp: { mimeType: "image/webp", extension: "webp", vector: false },
42
+ bmp: { mimeType: "image/bmp", extension: "bmp", vector: false },
32
43
  svg: { mimeType: "image/svg+xml", extension: "svg", vector: true },
33
44
  // A PDF download holds the PNG, so it's named after its pixel size.
34
45
  pdf: { mimeType: "application/pdf", extension: "pdf", vector: false },
@@ -136,7 +147,7 @@ const getMimeType = (format: DownloadFormat): string =>
136
147
  (DOWNLOAD_FORMATS[format] ?? DOWNLOAD_FORMATS.png).mimeType;
137
148
 
138
149
  /**
139
- * The default file name of a download: for PNG, JPG and WebP (and the image
150
+ * The default file name of a download: for PNG, JPG, WebP and BMP (and the image
140
151
  * a PDF holds) the exact pixel size of what is saved, which a frame makes
141
152
  * bigger than the code (a 400 px code in a bottom banner is 496 × 568);
142
153
  * SVG and EPS have no pixel size and name none.
@@ -177,6 +188,23 @@ export async function getQRAsWebP(
177
188
  }
178
189
  }
179
190
 
191
+ /**
192
+ * The code as a 24-bit BMP image (plus its frame, if any). A transparent
193
+ * background comes out white. Every browser gets it from QRdx's own encoder.
194
+ */
195
+ export async function getQRAsBMP(qrProps: QRProps): Promise<Blob> {
196
+ const canvas = (await getQRAsCanvas(
197
+ qrProps,
198
+ "image/png",
199
+ true
200
+ )) as HTMLCanvasElement;
201
+ try {
202
+ return canvasToBMP(canvas);
203
+ } finally {
204
+ canvas.remove();
205
+ }
206
+ }
207
+
180
208
  // One CSS pixel = one PostScript point (72 per inch).
181
209
  const MM_PER_POINT = 25.4 / 72;
182
210
 
@@ -326,6 +354,9 @@ export async function downloadQRCode(
326
354
  } else if (format === "webp") {
327
355
  const webp = await getQRAsWebP(propsWithSize, { quality });
328
356
  downloadFile(await blobToDataUrl(webp), finalFilename);
357
+ } else if (format === "bmp") {
358
+ const bmp = await getQRAsBMP(propsWithSize);
359
+ downloadFile(await blobToDataUrl(bmp), finalFilename);
329
360
  } else {
330
361
  // Download as PNG or JPG
331
362
  const mimeType = getMimeType(format);
@@ -376,6 +407,10 @@ export async function getQRCodeDataUrl(
376
407
  return await blobToDataUrl(await getQRAsWebP(propsWithSize, options));
377
408
  }
378
409
 
410
+ if (format === "bmp") {
411
+ return await blobToDataUrl(await getQRAsBMP(propsWithSize));
412
+ }
413
+
379
414
  const mimeType = getMimeType(format);
380
415
  const result = await getQRAsCanvas(propsWithSize, mimeType);
381
416
 
package/src/element.ts CHANGED
@@ -357,7 +357,7 @@ export class QRdxCodeElement extends BaseElement {
357
357
  return renderToString(createQRCodeTree(this.#requireOptions()));
358
358
  }
359
359
 
360
- /** The code as a data URL: "png" (default), "jpg", "webp", "svg", "pdf" or "eps". */
360
+ /** The code as a data URL: "png" (default), "jpg", "webp", "bmp", "svg", "pdf" or "eps". */
361
361
  async toDataURL(
362
362
  format: DownloadFormat = "png",
363
363
  options: QRCodeExportOptions = {}
package/src/engine.ts CHANGED
@@ -73,6 +73,7 @@ export {
73
73
  type QRArtRenderOptions,
74
74
  renderQRArt,
75
75
  } from "./art";
76
+ export * from "./bmp";
76
77
  export { clampModuleScale, MODULE_SCALE_FLOOR } from "./body-patterns";
77
78
  export {
78
79
  type CodewordMap,
@@ -195,6 +196,7 @@ export * from "./print";
195
196
  export {
196
197
  getQRCodeData,
197
198
  getQRCodeSVGProps,
199
+ type QRCodeOptions,
198
200
  type QRCodeProps,
199
201
  resolveQRCodeProps,
200
202
  } from "./qr-code-props";
package/src/export.ts CHANGED
@@ -4,6 +4,7 @@
4
4
  // score and qrdx/node's PNG all start here; none of it needs React.
5
5
 
6
6
  import type { QRProps } from "../types";
7
+ import { canvasToBMP } from "./bmp";
7
8
  import { DEFAULT_SIZE } from "./constants";
8
9
  import { getFrameDimensions } from "./frame";
9
10
  import { toSVGString } from "./svg";
@@ -188,6 +189,11 @@ export async function getQRAsCanvas(
188
189
  .then(blobToDataUrl)
189
190
  .then(resolve, reject)
190
191
  .finally(() => canvas.remove());
192
+ } else if (type === "image/bmp") {
193
+ // toDataURL would hand back a PNG: no browser encodes BMP.
194
+ blobToDataUrl(canvasToBMP(canvas))
195
+ .then(resolve, reject)
196
+ .finally(() => canvas.remove());
191
197
  } else {
192
198
  const dataUrl = canvas.toDataURL(type, 1.0);
193
199
  canvas.remove();
package/src/index.tsx CHANGED
@@ -3,7 +3,11 @@
3
3
  // React-free engine they draw with). The components build React elements
4
4
  // through the same renderer as the core, so their markup is the core's.
5
5
 
6
- export type { QRPropsCanvas, QRPropsSVG } from "../types/react";
6
+ export type {
7
+ QRCodePropsSVG,
8
+ QRPropsCanvas,
9
+ QRPropsSVG,
10
+ } from "../types/react";
7
11
  export * from "./api";
8
12
  export * from "./core";
9
13
  export * from "./qr-code";
package/src/preact.ts CHANGED
@@ -8,13 +8,13 @@
8
8
  import { Component, h, type VNode } from "preact";
9
9
  import {
10
10
  createQRCodeTree,
11
- type QRCodeProps,
11
+ type QRCodeOptions,
12
12
  type QRSVGOptions,
13
13
  resolveQRCodeProps,
14
14
  toHyperscript,
15
15
  } from "./core";
16
16
 
17
- export type { QRCodeProps, QRSVGOptions } from "./core";
17
+ export type { QRCodeOptions, QRCodeProps, QRSVGOptions } from "./core";
18
18
 
19
19
  const element = (
20
20
  type: string,
@@ -68,12 +68,13 @@ export class QRCodeSVG extends Drawn<QRSVGOptions> {
68
68
 
69
69
  /**
70
70
  * `QRCodeSVG` with React's `QRCode` logo API: `logo`, `logoOptions`,
71
- * `hideLogo` and `scale`.
71
+ * `hideLogo` and `scale`. Any other prop (`class`, `aria-label`, `data-*`…)
72
+ * goes on the `<svg>`, as with `QRCodeSVG`.
72
73
  *
73
74
  * @example
74
75
  * <QRCode value="https://qrdx.dev" logo="/logo.png" level="H" />
75
76
  */
76
- export class QRCode extends Drawn<QRCodeProps> {
77
+ export class QRCode extends Drawn<QRCodeOptions> {
77
78
  override render(): VNode {
78
79
  return draw(resolveQRCodeProps(this.props));
79
80
  }
@@ -1,7 +1,12 @@
1
1
  // QRCode's simpler logo API (`logo`, `logoOptions`, `hideLogo`, `scale`)
2
2
  // resolved into the props QRCodeSVG draws with. The React component and the
3
3
  // framework adapters share it, so a `<QRCode>` draws the same everywhere.
4
- import type { LogoOptions, QRProps, QRSVGOptions } from "../types";
4
+ import type {
5
+ LogoOptions,
6
+ QRProps,
7
+ QRSVGAttributes,
8
+ QRSVGOptions,
9
+ } from "../types";
5
10
  import { DEFAULT_MARGIN, DEFAULT_SIZE } from "./constants";
6
11
  import { getQRData } from "./helpers";
7
12
 
@@ -14,6 +19,66 @@ export interface QRCodeProps extends QRProps {
14
19
  scale?: number;
15
20
  }
16
21
 
22
+ /**
23
+ * `QRCode`'s props plus any root attribute (`aria-label`, `className`,
24
+ * `role`, `id`, `data-*`…), which goes on the `<svg>` as it does with
25
+ * `QRCodeSVG`: what the Preact and Solid `QRCode` take, and what
26
+ * `resolveQRCodeProps` resolves. React's `QRCode` types the attributes as
27
+ * `QRCodePropsSVG`.
28
+ */
29
+ export type QRCodeOptions = QRCodeProps & QRSVGAttributes;
30
+
31
+ /**
32
+ * Every prop `QRCode` draws with. Anything else among its props is a root
33
+ * attribute. `satisfies` keeps the list complete: a new `QRCodeProps` key
34
+ * that isn't added here fails to compile.
35
+ */
36
+ const QR_CODE_PROP_KEYS = {
37
+ value: true,
38
+ size: true,
39
+ level: true,
40
+ bgColor: true,
41
+ fgColor: true,
42
+ eyeColor: true,
43
+ dotColor: true,
44
+ bodyPattern: true,
45
+ cornerEyePattern: true,
46
+ cornerEyeDotPattern: true,
47
+ margin: true,
48
+ style: true,
49
+ imageSettings: true,
50
+ isOGContext: true,
51
+ templateId: true,
52
+ customTemplate: true,
53
+ customProps: true,
54
+ frame: true,
55
+ art: true,
56
+ eyeColors: true,
57
+ moduleScale: true,
58
+ autoLevel: true,
59
+ shape: true,
60
+ frameLabelMarkers: true,
61
+ partMarkers: true,
62
+ hideLogo: true,
63
+ logo: true,
64
+ logoOptions: true,
65
+ scale: true,
66
+ } as const satisfies Record<keyof QRCodeProps, true>;
67
+
68
+ /**
69
+ * The root attributes among `QRCode`'s props, in the order they were given:
70
+ * everything that isn't one of its own props.
71
+ */
72
+ function rootAttributes(props: QRCodeOptions): QRSVGAttributes {
73
+ const attributes: QRSVGAttributes = {};
74
+ for (const key of Object.keys(props)) {
75
+ if (!Object.hasOwn(QR_CODE_PROP_KEYS, key)) {
76
+ attributes[key] = props[key];
77
+ }
78
+ }
79
+ return attributes;
80
+ }
81
+
17
82
  type QRData = ReturnType<typeof getQRData>;
18
83
 
19
84
  /** What `getQRData` lays out for `QRCode`'s props. */
@@ -38,9 +103,13 @@ export function getQRCodeData(props: QRCodeProps): QRData {
38
103
  });
39
104
  }
40
105
 
41
- /** The `QRCodeSVG` props `QRCode` draws with, given its laid-out data. */
106
+ /**
107
+ * The `QRCodeSVG` props `QRCode` draws with, given its laid-out data. Root
108
+ * attributes among `props` come along, after the drawing props, so they land
109
+ * on the `<svg>` (on the frame's outer `<svg>` with a frame).
110
+ */
42
111
  export function getQRCodeSVGProps(
43
- props: QRCodeProps,
112
+ props: QRCodeOptions,
44
113
  qrData: QRData
45
114
  ): QRSVGOptions {
46
115
  const {
@@ -103,13 +172,15 @@ export function getQRCodeSVGProps(
103
172
  templateId: qrData.templateId,
104
173
  value: qrData.value,
105
174
  ...(logoSettings && { imageSettings: logoSettings }),
175
+ ...rootAttributes(props),
106
176
  };
107
177
  }
108
178
 
109
179
  /**
110
- * `QRCode`'s props (with `logo`, `logoOptions`, `hideLogo` and `scale`) as
111
- * the props `createQRCodeTree` and `toSVGString` take: what `<QRCode>` draws.
180
+ * `QRCode`'s props (with `logo`, `logoOptions`, `hideLogo` and `scale`, plus
181
+ * any root attribute) as the props `createQRCodeTree` and `toSVGString`
182
+ * take: what `<QRCode>` draws.
112
183
  */
113
- export function resolveQRCodeProps(props: QRCodeProps): QRSVGOptions {
184
+ export function resolveQRCodeProps(props: QRCodeOptions): QRSVGOptions {
114
185
  return getQRCodeSVGProps(props, getQRCodeData(props));
115
186
  }
package/src/qr-code.tsx CHANGED
@@ -1,15 +1,17 @@
1
1
  // memo/useMemo are available in React Server Components, so QRCode can render
2
2
  // on the server; only QRCodeCanvas needs a client boundary.
3
3
  import { memo, useMemo } from "react";
4
+ import type { QRCodePropsSVG } from "../types/react";
4
5
  import { DEFAULT_MARGIN } from "./constants";
5
- import {
6
- getQRCodeData,
7
- getQRCodeSVGProps,
8
- type QRCodeProps,
9
- } from "./qr-code-props";
6
+ import { getQRCodeData, getQRCodeSVGProps } from "./qr-code-props";
10
7
  import { QRCodeSVG } from "./qr-code-svg";
11
8
 
12
- export const QRCode = memo((props: QRCodeProps) => {
9
+ /**
10
+ * `QRCodeSVG` with the simpler logo API (`logo`, `logoOptions`, `hideLogo`,
11
+ * `scale`). Any other prop (`className`, `aria-label`, `role`, `data-*`…)
12
+ * goes on the `<svg>`, as with `QRCodeSVG`.
13
+ */
14
+ export const QRCode = memo((props: QRCodePropsSVG) => {
13
15
  const {
14
16
  value,
15
17
  fgColor,
package/src/scan-score.ts CHANGED
@@ -115,42 +115,44 @@ function simulateCamera(image: ImageData, modulePx: number, contrast = 0.7) {
115
115
  }
116
116
  }
117
117
  const radius = Math.max(1, Math.round(modulePx * 0.25));
118
+ const span = 2 * radius + 1;
118
119
  const buffer = new Float32Array(width * height * 3);
120
+ // Sliding window with clamped edges. Pixel (line, position) is
121
+ // `line * lineStep + position * step`; the sums are plain locals (this
122
+ // runs nine times a score, on every pixel).
119
123
  const blur = (horizontal: boolean) => {
120
124
  const lines = horizontal ? height : width;
121
125
  const length = horizontal ? width : height;
122
- const index = (line: number, position: number) =>
123
- horizontal ? line * width + position : position * width + line;
124
- const sums = [0, 0, 0];
126
+ const lineStep = horizontal ? width : 1;
127
+ const step = horizontal ? 1 : width;
125
128
  for (let line = 0; line < lines; line++) {
126
- sums.fill(0);
127
- // Sliding window with clamped edges
129
+ const start = line * lineStep;
130
+ let red = 0;
131
+ let green = 0;
132
+ let blue = 0;
128
133
  for (let k = -radius; k <= radius; k++) {
129
- const p = index(line, Math.min(Math.max(k, 0), length - 1)) * 4;
130
- for (let c = 0; c < 3; c++) {
131
- sums[c] = (sums[c] as number) + (data[p + c] as number);
132
- }
134
+ const p = (start + Math.min(Math.max(k, 0), length - 1) * step) * 4;
135
+ red += data[p] as number;
136
+ green += data[p + 1] as number;
137
+ blue += data[p + 2] as number;
133
138
  }
134
139
  for (let position = 0; position < length; position++) {
135
- const target = index(line, position) * 3;
136
- for (let c = 0; c < 3; c++) {
137
- buffer[target + c] = (sums[c] as number) / (2 * radius + 1);
138
- }
139
- const out = index(line, Math.max(position - radius, 0)) * 4;
140
+ const target = (start + position * step) * 3;
141
+ buffer[target] = red / span;
142
+ buffer[target + 1] = green / span;
143
+ buffer[target + 2] = blue / span;
144
+ const out = (start + Math.max(position - radius, 0) * step) * 4;
140
145
  const into =
141
- index(line, Math.min(position + radius + 1, length - 1)) * 4;
142
- for (let c = 0; c < 3; c++) {
143
- sums[c] =
144
- (sums[c] as number) -
145
- (data[out + c] as number) +
146
- (data[into + c] as number);
147
- }
146
+ (start + Math.min(position + radius + 1, length - 1) * step) * 4;
147
+ red = red - (data[out] as number) + (data[into] as number);
148
+ green = green - (data[out + 1] as number) + (data[into + 1] as number);
149
+ blue = blue - (data[out + 2] as number) + (data[into + 2] as number);
148
150
  }
149
151
  }
150
- for (let i = 0; i < width * height; i++) {
151
- for (let c = 0; c < 3; c++) {
152
- data[i * 4 + c] = buffer[i * 3 + c] as number;
153
- }
152
+ for (let i = 0, o = 0; i < width * height * 3; i += 3, o += 4) {
153
+ data[o] = buffer[i] as number;
154
+ data[o + 1] = buffer[i + 1] as number;
155
+ data[o + 2] = buffer[i + 2] as number;
154
156
  }
155
157
  };
156
158
  for (let pass = 0; pass < 2; pass++) {
@@ -1548,20 +1550,26 @@ function resizePixels(
1548
1550
  x0 + 1,
1549
1551
  Math.min(source.width, Math.floor((x + 1) * stepX))
1550
1552
  );
1551
- const sums = [0, 0, 0, 0];
1553
+ // Plain locals, not an array: this runs on every output pixel
1554
+ let red = 0;
1555
+ let green = 0;
1556
+ let blue = 0;
1557
+ let alpha = 0;
1552
1558
  for (let sy = y0; sy < y1; sy++) {
1553
1559
  for (let sx = x0; sx < x1; sx++) {
1554
1560
  const i = (sy * source.width + sx) * 4;
1555
- for (let c = 0; c < 4; c++) {
1556
- sums[c] = (sums[c] as number) + (source.data[i + c] as number);
1557
- }
1561
+ red += source.data[i] as number;
1562
+ green += source.data[i + 1] as number;
1563
+ blue += source.data[i + 2] as number;
1564
+ alpha += source.data[i + 3] as number;
1558
1565
  }
1559
1566
  }
1560
1567
  const count = (y1 - y0) * (x1 - x0);
1561
1568
  const o = (y * width + x) * 4;
1562
- for (let c = 0; c < 4; c++) {
1563
- out[o + c] = (sums[c] as number) / count;
1564
- }
1569
+ out[o] = red / count;
1570
+ out[o + 1] = green / count;
1571
+ out[o + 2] = blue / count;
1572
+ out[o + 3] = alpha / count;
1565
1573
  }
1566
1574
  }
1567
1575
  return { data: out, width, height, colorSpace: "srgb" } as ImageData;