qrdx 0.0.46 → 0.0.47

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (206) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +353 -300
  3. package/dist/index.js +27 -7
  4. package/dist/index.js.map +1 -1
  5. package/dist/index.mjs +27 -7
  6. package/dist/index.mjs.map +1 -1
  7. package/dist/qr-code-canvas.js +12 -0
  8. package/dist/qr-code-canvas.js.map +1 -0
  9. package/dist/qr-code-canvas.mjs +12 -0
  10. package/dist/qr-code-canvas.mjs.map +1 -0
  11. package/dist/src/api.d.ts.map +1 -1
  12. package/dist/src/art-trace.d.ts +46 -0
  13. package/dist/src/art-trace.d.ts.map +1 -0
  14. package/dist/src/art.d.ts +138 -0
  15. package/dist/src/art.d.ts.map +1 -0
  16. package/dist/src/body-patterns.d.ts +34 -0
  17. package/dist/src/body-patterns.d.ts.map +1 -0
  18. package/dist/src/codegen.d.ts +5 -5
  19. package/dist/src/codegen.d.ts.map +1 -1
  20. package/dist/src/codeword-map.d.ts +40 -0
  21. package/dist/src/codeword-map.d.ts.map +1 -0
  22. package/dist/src/constants.d.ts +4 -7
  23. package/dist/src/constants.d.ts.map +1 -1
  24. package/dist/src/corner-dot.d.ts +3 -1
  25. package/dist/src/corner-dot.d.ts.map +1 -1
  26. package/dist/src/corner-eye.d.ts +5 -2
  27. package/dist/src/corner-eye.d.ts.map +1 -1
  28. package/dist/src/detection.d.ts.map +1 -1
  29. package/dist/src/download.d.ts +22 -5
  30. package/dist/src/download.d.ts.map +1 -1
  31. package/dist/src/eps.d.ts +62 -0
  32. package/dist/src/eps.d.ts.map +1 -0
  33. package/dist/src/frame-icons.d.ts +11 -0
  34. package/dist/src/frame-icons.d.ts.map +1 -0
  35. package/dist/src/frame-import.d.ts +61 -0
  36. package/dist/src/frame-import.d.ts.map +1 -0
  37. package/dist/src/frame-spec.d.ts +24 -0
  38. package/dist/src/frame-spec.d.ts.map +1 -0
  39. package/dist/src/frame-styles.d.ts +50 -0
  40. package/dist/src/frame-styles.d.ts.map +1 -0
  41. package/dist/src/frame.d.ts +126 -0
  42. package/dist/src/frame.d.ts.map +1 -0
  43. package/dist/src/frames/builtin.d.ts +8 -0
  44. package/dist/src/frames/builtin.d.ts.map +1 -0
  45. package/dist/src/frames/official.d.ts +18 -0
  46. package/dist/src/frames/official.d.ts.map +1 -0
  47. package/dist/src/helpers.d.ts +15 -2
  48. package/dist/src/helpers.d.ts.map +1 -1
  49. package/dist/src/index.d.ts +24 -13
  50. package/dist/src/index.d.ts.map +1 -1
  51. package/dist/src/logo-budget.d.ts +54 -0
  52. package/dist/src/logo-budget.d.ts.map +1 -0
  53. package/dist/src/logo-cleanup.d.ts +25 -0
  54. package/dist/src/logo-cleanup.d.ts.map +1 -0
  55. package/dist/src/logo.d.ts +28 -0
  56. package/dist/src/logo.d.ts.map +1 -0
  57. package/dist/src/print.d.ts +73 -0
  58. package/dist/src/print.d.ts.map +1 -0
  59. package/dist/src/qr-code-canvas.d.ts +13 -0
  60. package/dist/src/qr-code-canvas.d.ts.map +1 -0
  61. package/dist/src/qr-code.d.ts +4 -2
  62. package/dist/src/qr-code.d.ts.map +1 -1
  63. package/dist/src/qr-outline.d.ts +41 -0
  64. package/dist/src/qr-outline.d.ts.map +1 -0
  65. package/dist/src/qr-shape.d.ts +72 -0
  66. package/dist/src/qr-shape.d.ts.map +1 -0
  67. package/dist/src/scan-risk.d.ts +40 -0
  68. package/dist/src/scan-risk.d.ts.map +1 -0
  69. package/dist/src/scan-score.d.ts +154 -0
  70. package/dist/src/scan-score.d.ts.map +1 -0
  71. package/dist/src/shapes.d.ts +36 -0
  72. package/dist/src/shapes.d.ts.map +1 -0
  73. package/dist/src/silhouette.d.ts +28 -0
  74. package/dist/src/silhouette.d.ts.map +1 -0
  75. package/dist/src/utils/color.d.ts.map +1 -1
  76. package/dist/src/utils/fmt.d.ts +24 -0
  77. package/dist/src/utils/fmt.d.ts.map +1 -0
  78. package/dist/src/utils/paint.d.ts +80 -0
  79. package/dist/src/utils/paint.d.ts.map +1 -0
  80. package/dist/src/utils/parse-color.d.ts +21 -0
  81. package/dist/src/utils/parse-color.d.ts.map +1 -0
  82. package/dist/src/utils/path-data.d.ts +24 -0
  83. package/dist/src/utils/path-data.d.ts.map +1 -0
  84. package/dist/src/utils/png.d.ts +16 -0
  85. package/dist/src/utils/png.d.ts.map +1 -0
  86. package/dist/src/utils/polygon-clip.d.ts +26 -0
  87. package/dist/src/utils/polygon-clip.d.ts.map +1 -0
  88. package/dist/src/utils/print-shared.d.ts +20 -0
  89. package/dist/src/utils/print-shared.d.ts.map +1 -0
  90. package/dist/src/utils/svg-geometry.d.ts +49 -0
  91. package/dist/src/utils/svg-geometry.d.ts.map +1 -0
  92. package/dist/src/utils/svg-gradient.d.ts +59 -0
  93. package/dist/src/utils/svg-gradient.d.ts.map +1 -0
  94. package/dist/src/utils/svg-painter.d.ts +152 -0
  95. package/dist/src/utils/svg-painter.d.ts.map +1 -0
  96. package/dist/src/utils.d.ts +18 -2
  97. package/dist/src/utils.d.ts.map +1 -1
  98. package/dist/types/art.d.ts +64 -0
  99. package/dist/types/art.d.ts.map +1 -0
  100. package/dist/types/color.d.ts +125 -7
  101. package/dist/types/color.d.ts.map +1 -1
  102. package/dist/types/corner-dot.d.ts +52 -2
  103. package/dist/types/corner-dot.d.ts.map +1 -1
  104. package/dist/types/corner-eye.d.ts +43 -2
  105. package/dist/types/corner-eye.d.ts.map +1 -1
  106. package/dist/types/frame-spec.d.ts +3509 -0
  107. package/dist/types/frame-spec.d.ts.map +1 -0
  108. package/dist/types/frame.d.ts +68 -0
  109. package/dist/types/frame.d.ts.map +1 -0
  110. package/dist/types/image-pattern.d.ts +15 -1
  111. package/dist/types/image-pattern.d.ts.map +1 -1
  112. package/dist/types/index.d.ts +15 -9
  113. package/dist/types/index.d.ts.map +1 -1
  114. package/dist/types/index.js +6 -0
  115. package/dist/types/index.js.map +1 -0
  116. package/dist/types/index.mjs +6 -0
  117. package/dist/types/index.mjs.map +1 -0
  118. package/dist/types/qr.d.ts +1684 -4
  119. package/dist/types/qr.d.ts.map +1 -1
  120. package/dist/types/template.d.ts +2 -2
  121. package/dist/types/template.d.ts.map +1 -1
  122. package/package.json +68 -25
  123. package/src/api.tsx +67 -17
  124. package/src/art-trace.ts +554 -0
  125. package/src/art.tsx +2226 -0
  126. package/src/body-patterns.tsx +389 -0
  127. package/src/codegen.ts +10 -5
  128. package/src/codeword-map.ts +265 -0
  129. package/src/constants.ts +4 -8
  130. package/src/corner-dot.tsx +209 -33
  131. package/src/corner-eye.ts +328 -60
  132. package/src/detection.ts +5 -11
  133. package/src/download.tsx +123 -52
  134. package/src/eps.ts +701 -0
  135. package/src/frame-icons.ts +325 -0
  136. package/src/frame-import.ts +1198 -0
  137. package/src/frame-spec.ts +518 -0
  138. package/src/frame-styles.ts +1095 -0
  139. package/src/frame.tsx +630 -0
  140. package/src/frames/builtin/banner-bottom.json +58 -0
  141. package/src/frames/builtin/circle.json +81 -0
  142. package/src/frames/builtin/rounded-border.json +60 -0
  143. package/src/frames/builtin/ticket.json +51 -0
  144. package/src/frames/builtin.ts +20 -0
  145. package/src/frames/official/attendance.json +110 -0
  146. package/src/frames/official/audio-guide.json +95 -0
  147. package/src/frames/official/book-now.json +103 -0
  148. package/src/frames/official/check-in.json +110 -0
  149. package/src/frames/official/christmas.json +131 -0
  150. package/src/frames/official/donate.json +77 -0
  151. package/src/frames/official/event-ticket.json +77 -0
  152. package/src/frames/official/feedback-bubble.json +73 -0
  153. package/src/frames/official/follow-us.json +83 -0
  154. package/src/frames/official/get-the-app.json +107 -0
  155. package/src/frames/official/halloween.json +80 -0
  156. package/src/frames/official/new-year.json +94 -0
  157. package/src/frames/official/order-at-table.json +86 -0
  158. package/src/frames/official/product-info.json +119 -0
  159. package/src/frames/official/register-product.json +77 -0
  160. package/src/frames/official/review-us.json +127 -0
  161. package/src/frames/official/rsvp-card.json +112 -0
  162. package/src/frames/official/save-contact.json +117 -0
  163. package/src/frames/official/scan-to-pay.json +94 -0
  164. package/src/frames/official/shop-window.json +81 -0
  165. package/src/frames/official/sign-rider.json +81 -0
  166. package/src/frames/official/table-tent.json +75 -0
  167. package/src/frames/official/wifi-card.json +89 -0
  168. package/src/frames/official/winter.json +142 -0
  169. package/src/frames/official.ts +77 -0
  170. package/src/helpers.ts +168 -11
  171. package/src/index.tsx +141 -172
  172. package/src/logo-budget.ts +226 -0
  173. package/src/logo-cleanup.ts +146 -0
  174. package/src/logo.tsx +137 -0
  175. package/src/print.ts +794 -0
  176. package/src/qr-code-canvas.tsx +241 -0
  177. package/src/qr-code.tsx +51 -16
  178. package/src/qr-outline.ts +511 -0
  179. package/src/qr-shape.ts +255 -0
  180. package/src/scan-risk.ts +197 -0
  181. package/src/scan-score.ts +1629 -0
  182. package/src/shapes.ts +169 -0
  183. package/src/silhouette.ts +334 -0
  184. package/src/utils/color.ts +10 -1
  185. package/src/utils/fmt.ts +59 -0
  186. package/src/utils/paint.ts +317 -0
  187. package/src/utils/parse-color.ts +135 -0
  188. package/src/utils/path-data.ts +182 -0
  189. package/src/utils/png.ts +175 -0
  190. package/src/utils/polygon-clip.ts +163 -0
  191. package/src/utils/print-shared.ts +74 -0
  192. package/src/utils/svg-geometry.ts +838 -0
  193. package/src/utils/svg-gradient.ts +407 -0
  194. package/src/utils/svg-painter.ts +946 -0
  195. package/src/utils.tsx +794 -385
  196. package/types/art.ts +74 -0
  197. package/types/color.ts +132 -12
  198. package/types/corner-dot.ts +41 -4
  199. package/types/corner-eye.ts +31 -4
  200. package/types/frame-spec.ts +748 -0
  201. package/types/frame.ts +67 -0
  202. package/types/image-pattern.ts +15 -0
  203. package/types/index.ts +106 -4
  204. package/types/qr.ts +180 -2
  205. package/types/template.ts +2 -2
  206. package/dist/cli.js +0 -4319
@@ -0,0 +1,77 @@
1
+ // QRdx's official frames: 20 for common uses and 4 seasonal ones, each a
2
+ // frame spec with editable labels and theme colors (CC0 1.0). Every one is
3
+ // checked to decode at phone distances and to score Great
4
+ // (tests/frames-official.test.ts). Apply one with
5
+ // `frame: { style: "custom", spec: frame.spec }`.
6
+ import type { FrameSpec } from "../../types/frame-spec";
7
+ import attendance from "./official/attendance.json";
8
+ import audioGuide from "./official/audio-guide.json";
9
+ import bookNow from "./official/book-now.json";
10
+ import checkIn from "./official/check-in.json";
11
+ import christmas from "./official/christmas.json";
12
+ import donate from "./official/donate.json";
13
+ import eventTicket from "./official/event-ticket.json";
14
+ import feedbackBubble from "./official/feedback-bubble.json";
15
+ import followUs from "./official/follow-us.json";
16
+ import getTheApp from "./official/get-the-app.json";
17
+ import halloween from "./official/halloween.json";
18
+ import newYear from "./official/new-year.json";
19
+ import orderAtTable from "./official/order-at-table.json";
20
+ import productInfo from "./official/product-info.json";
21
+ import registerProduct from "./official/register-product.json";
22
+ import reviewUs from "./official/review-us.json";
23
+ import rsvpCard from "./official/rsvp-card.json";
24
+ import saveContact from "./official/save-contact.json";
25
+ import scanToPay from "./official/scan-to-pay.json";
26
+ import shopWindow from "./official/shop-window.json";
27
+ import signRider from "./official/sign-rider.json";
28
+ import tableTent from "./official/table-tent.json";
29
+ import wifiCard from "./official/wifi-card.json";
30
+ import winter from "./official/winter.json";
31
+
32
+ export interface OfficialFrame {
33
+ /** Stable id (also the file name) */
34
+ id: string;
35
+ name: string;
36
+ description: string;
37
+ category: "use-case" | "seasonal";
38
+ /** QRdx content types the frame suits (empty: any) */
39
+ contentTypes: string[];
40
+ tags: string[];
41
+ license: "CC0-1.0";
42
+ spec: FrameSpec;
43
+ }
44
+
45
+ /** The official frames, in catalogue order (use cases, then seasons). */
46
+ // A plain array literal (no call), so bundlers drop it when it's unused.
47
+ export const OFFICIAL_FRAMES = [
48
+ wifiCard,
49
+ tableTent,
50
+ orderAtTable,
51
+ reviewUs,
52
+ scanToPay,
53
+ eventTicket,
54
+ rsvpCard,
55
+ saveContact,
56
+ followUs,
57
+ getTheApp,
58
+ feedbackBubble,
59
+ productInfo,
60
+ registerProduct,
61
+ donate,
62
+ checkIn,
63
+ audioGuide,
64
+ signRider,
65
+ bookNow,
66
+ shopWindow,
67
+ attendance,
68
+ winter,
69
+ halloween,
70
+ christmas,
71
+ newYear,
72
+ ] as unknown as readonly OfficialFrame[];
73
+
74
+ /** An official frame by id. */
75
+ export function getOfficialFrame(id: string): OfficialFrame | undefined {
76
+ return OFFICIAL_FRAMES.find((frame) => frame.id === id);
77
+ }
package/src/helpers.ts CHANGED
@@ -1,15 +1,158 @@
1
- import type { QRProps } from "../types";
2
- import { FLAM_QR_LOGO } from "./constants";
1
+ import type { ErrorLevel, ImageSettings, LogoOptions, QRProps } from "../types";
2
+ import { LOGO_SIZE_DEFAULT, LOGO_SIZE_MAX } from "../types/qr";
3
+ import qrcodegen from "./codegen";
4
+ import { DEFAULT_LEVEL, DEFAULT_MARGIN, ERROR_LEVEL_MAP } from "./constants";
5
+ import { fitLogoWidth } from "./logo-budget";
6
+ import { getLayoutMargin } from "./qr-shape";
7
+ import { resolveErrorLevel } from "./utils";
3
8
 
4
9
  interface QRDataProps extends QRProps {
5
10
  hideLogo?: boolean;
6
11
  logo?: string;
12
+ /**
13
+ * Size, clear space and position of `logo` (see `LogoOptions`). Without
14
+ * it the logo is 25% wide and centered, as before.
15
+ */
16
+ logoOptions?: LogoOptions;
7
17
  }
8
18
 
9
19
  interface QRDataResult extends QRProps {
10
20
  size: number; // Required in result
11
21
  }
12
22
 
23
+ /** Modules across the symbol (no margin) for `value` at `level`. */
24
+ function symbolModules(value: string, level: QRProps["level"]): number {
25
+ return qrcodegen.QrCode.encodeText(
26
+ value,
27
+ ERROR_LEVEL_MAP[level ?? DEFAULT_LEVEL]
28
+ ).size;
29
+ }
30
+
31
+ /**
32
+ * `imageSettings` (pixels of a `size`-wide code) for a logo laid out with
33
+ * `options`. Clear space is in modules and the position is across the
34
+ * symbol, so both need the module count at the level the code is drawn at.
35
+ */
36
+ function logoImageSettings(
37
+ src: string,
38
+ options: LogoOptions | undefined,
39
+ code: {
40
+ value: string;
41
+ size: number;
42
+ margin: number;
43
+ level?: QRProps["level"];
44
+ shape?: QRProps["shape"];
45
+ },
46
+ width = code.size * (options?.size ?? LOGO_SIZE_DEFAULT)
47
+ ): ImageSettings {
48
+ const settings: ImageSettings = {
49
+ src,
50
+ width,
51
+ height: width,
52
+ excavate: true,
53
+ ...(options?.shape &&
54
+ options.shape !== "square" && { shape: options.shape }),
55
+ ...(options?.plate && { plate: options.plate }),
56
+ };
57
+ const positioned = options?.x !== undefined || options?.y !== undefined;
58
+ if (!(options?.padding || positioned)) {
59
+ return settings;
60
+ }
61
+ const modules = symbolModules(code.value, code.level);
62
+ // A circle code draws its symbol further in (see qr-shape.ts)
63
+ const margin = getLayoutMargin(modules, code.margin, code.shape);
64
+ const modulePx = code.size / (modules + 2 * margin);
65
+ if (options.padding) {
66
+ settings.padding = options.padding * modulePx;
67
+ }
68
+ if (positioned) {
69
+ // Center across the symbol, kept inside it
70
+ const symbolPx = modules * modulePx;
71
+ const place = (share: number | undefined) =>
72
+ Math.min(
73
+ Math.max((share ?? 0.5) * symbolPx - width / 2, 0),
74
+ symbolPx - width
75
+ );
76
+ settings.x = place(options.x);
77
+ settings.y = place(options.y);
78
+ }
79
+ return settings;
80
+ }
81
+
82
+ /**
83
+ * The logo's imageSettings, no bigger than the code's error correction can
84
+ * repair (logo-budget.ts): the requested size when it fits, else the
85
+ * largest that does, or none when not even a sliver fits.
86
+ */
87
+ function capLogo(
88
+ src: string,
89
+ options: LogoOptions | undefined,
90
+ code: {
91
+ value: string;
92
+ size: number;
93
+ margin: number;
94
+ level?: QRProps["level"];
95
+ shape?: QRProps["shape"];
96
+ },
97
+ autoLevel: boolean | undefined
98
+ ): ImageSettings | undefined {
99
+ const requested = code.level ?? DEFAULT_LEVEL;
100
+ let drawn = requested;
101
+ // autoLevel can raise the level (more modules): lay the logo out at the
102
+ // level it will be drawn at, so clear space stays in whole modules.
103
+ if (autoLevel) {
104
+ drawn = resolveErrorLevel({
105
+ ...code,
106
+ imageSettings: logoImageSettings(src, options, code),
107
+ autoLevel,
108
+ });
109
+ }
110
+ const fit = (level: ErrorLevel) => {
111
+ const layout = (width?: number) =>
112
+ logoImageSettings(src, options, { ...code, level }, width);
113
+ const width = fitLogoWidth(code, [level], layout().width, layout);
114
+ return width > 0 ? layout(width) : undefined;
115
+ };
116
+ if (drawn === requested) {
117
+ return fit(requested);
118
+ }
119
+ // autoLevel draws at H only while the logo covers more than 7%: keep the
120
+ // H-sized logo when it still does, else size it for the requested level
121
+ const atH = fit(drawn);
122
+ if (
123
+ atH &&
124
+ resolveErrorLevel({ ...code, imageSettings: atH, autoLevel }) === drawn
125
+ ) {
126
+ return atH;
127
+ }
128
+ return fit(requested);
129
+ }
130
+
131
+ /**
132
+ * The largest logo `size` (share of the code's width, up to LOGO_SIZE_MAX)
133
+ * that fits this code's error correction with `options`' clear space and
134
+ * position, so the editor can cap its slider. 0 when no logo fits.
135
+ */
136
+ export function getLogoSizeLimit({
137
+ value,
138
+ level,
139
+ margin,
140
+ shape,
141
+ autoLevel,
142
+ logoOptions,
143
+ }: Pick<QRProps, "value" | "level" | "margin" | "shape" | "autoLevel"> & {
144
+ logoOptions?: LogoOptions;
145
+ }): number {
146
+ const size = 1024;
147
+ const settings = capLogo(
148
+ "data:,",
149
+ { ...logoOptions, size: LOGO_SIZE_MAX },
150
+ { value: `${value}`, size, margin: margin ?? DEFAULT_MARGIN, level, shape },
151
+ autoLevel
152
+ );
153
+ return settings ? settings.width / size : 0;
154
+ }
155
+
13
156
  export function getQRData({
14
157
  value,
15
158
  size = 1024,
@@ -20,14 +163,30 @@ export function getQRData({
20
163
  bodyPattern,
21
164
  hideLogo,
22
165
  logo,
166
+ logoOptions,
23
167
  margin,
24
168
  cornerEyePattern,
25
169
  cornerEyeDotPattern,
26
170
  level,
27
171
  templateId,
172
+ frame,
173
+ eyeColors,
174
+ moduleScale,
175
+ autoLevel,
176
+ shape,
28
177
  }: QRDataProps): QRDataResult {
29
- return {
178
+ const code = {
30
179
  value: `${value}`,
180
+ size,
181
+ margin: margin ?? DEFAULT_MARGIN,
182
+ level,
183
+ shape,
184
+ };
185
+ // No default logo: only render (and excavate for) one the caller passes.
186
+ const imageSettings =
187
+ !hideLogo && logo ? capLogo(logo, logoOptions, code, autoLevel) : undefined;
188
+ return {
189
+ value: code.value,
31
190
  bgColor,
32
191
  fgColor,
33
192
  eyeColor,
@@ -39,13 +198,11 @@ export function getQRData({
39
198
  templateId,
40
199
  size,
41
200
  margin,
42
- ...(!hideLogo && {
43
- imageSettings: {
44
- src: logo || FLAM_QR_LOGO,
45
- height: 256,
46
- width: 256,
47
- excavate: true,
48
- },
49
- }),
201
+ ...(frame && { frame }),
202
+ ...(eyeColors && { eyeColors }),
203
+ ...(moduleScale !== undefined && { moduleScale }),
204
+ ...(autoLevel && { autoLevel }),
205
+ ...(shape && shape !== "square" && { shape }),
206
+ ...(imageSettings && { imageSettings }),
50
207
  };
51
208
  }
package/src/index.tsx CHANGED
@@ -1,178 +1,131 @@
1
1
  /** biome-ignore-all lint/performance/noBarrelFile: false positive */
2
2
  /** biome-ignore-all lint/style/noMagicNumbers: false positive */
3
- /** biome-ignore-all lint/correctness/useImageSize: false positive */
4
- /** biome-ignore-all lint/a11y/noNoninteractiveElementInteractions: false positive */
5
- /** biome-ignore-all lint/performance/noImgElement: false positive */
6
- /** biome-ignore-all lint/complexity/noExcessiveCognitiveComplexity: false positive */
7
3
 
8
- /**
9
- * @license qrcode.react
10
- * Copyright (c) Paul O'Shannessy
11
- * SPDX-License-Identifier: ISC
12
- */
13
- import { type JSX, useEffect, useRef, useState } from "react";
14
- import type { QRProps, QRPropsCanvas } from "../types";
15
- import { normalizeColorConfig } from "../types/color";
16
- import qrcodegen from "./codegen";
17
- import {
18
- DEFAULT_BGCOLOR,
19
- DEFAULT_FGCOLOR,
20
- DEFAULT_LEVEL,
21
- DEFAULT_MARGIN,
22
- DEFAULT_SIZE,
23
- ERROR_LEVEL_MAP,
24
- } from "./constants";
25
- import {
26
- excavateModules,
27
- generatePath,
28
- getImageSettings,
29
- SUPPORTS_PATH2D,
30
- } from "./utils";
4
+ import type { QRProps } from "../types";
5
+ import { DEFAULT_SIZE } from "./constants";
6
+ import { getFrameDimensions } from "./frame";
31
7
 
32
8
  export * from "../types";
33
9
  export * from "./api";
10
+ export {
11
+ createQRArtImage,
12
+ DEFAULT_ART_INTENSITY,
13
+ getQRArtColorIssue,
14
+ getQRArtEyeColors,
15
+ getQRArtVariant,
16
+ getQRFunctionModules,
17
+ isQRArtActive,
18
+ loadQRArtImage,
19
+ QR_ART_MIN_LUMA_GAP,
20
+ QR_ART_SAMPLE_SIZES,
21
+ type QRArtColorIssue,
22
+ type QRArtEyeColorOptions,
23
+ type QRArtPixels,
24
+ type QRArtRenderOptions,
25
+ renderQRArt,
26
+ } from "./art";
27
+ export { clampModuleScale, MODULE_SCALE_FLOOR } from "./body-patterns";
34
28
  export * from "./detection";
35
29
  export * from "./download";
30
+ export {
31
+ downloadPrintEPS,
32
+ EPS_IMAGE_FILL_REASON,
33
+ type EPSOptions,
34
+ epsToDataUrl,
35
+ getQRAsEPS,
36
+ hasImageFill,
37
+ type SVGToEPSOptions,
38
+ svgToEPS,
39
+ } from "./eps";
40
+ export {
41
+ FRAME_FONT_FAMILY,
42
+ FRAME_GAP_RATIO,
43
+ FRAME_LABEL_ATTRIBUTE,
44
+ type FrameBox,
45
+ type FrameLabel,
46
+ type FrameLayout,
47
+ type FramePath,
48
+ type FramePolygon,
49
+ type FrameRect,
50
+ type FrameShape,
51
+ frameLabels,
52
+ getFrameDimensions,
53
+ getFrameGapRatio,
54
+ getFrameLayout,
55
+ getFrameSecondaryText,
56
+ getFrameText,
57
+ isFrameActive,
58
+ } from "./frame";
59
+ export {
60
+ FRAME_ICON_BOX,
61
+ type FrameIconPath,
62
+ getFrameIconPath,
63
+ } from "./frame-icons";
64
+ export {
65
+ FRAME_IMPORT_LIMITS,
66
+ type FrameImportColor,
67
+ type FrameImportOptions,
68
+ type FrameImportResult,
69
+ type FrameImportWarning,
70
+ importFrameSvg,
71
+ } from "./frame-import";
72
+ export {
73
+ FrameSpecError,
74
+ getCustomFrameLayout,
75
+ getFrameSpecGapRatio,
76
+ resolveFrameSpec,
77
+ } from "./frame-spec";
78
+ export { GRADIENT_FRAME_DEFAULTS, mixFrameColors } from "./frame-styles";
79
+ export { BUILT_IN_FRAME_SPECS } from "./frames/builtin";
80
+ export {
81
+ getOfficialFrame,
82
+ OFFICIAL_FRAMES,
83
+ type OfficialFrame,
84
+ } from "./frames/official";
85
+ export {
86
+ LOGO_PLATE_INSET,
87
+ LOGO_ROUNDED_RADIUS,
88
+ } from "./logo";
89
+ export {
90
+ LOGO_CLEANUP_MAX_SIDE,
91
+ type LogoPixels,
92
+ removeLogoBackdrop,
93
+ removeLogoBackground,
94
+ } from "./logo-cleanup";
95
+ export * from "./print";
36
96
  export * from "./qr-code";
97
+ // QRCodeCanvas is the only export that needs client-only hooks. It lives in
98
+ // its own "use client" module (built as a separate entry) so the rest of the
99
+ // package stays usable from React Server Components.
100
+ export { QRCodeCanvas } from "./qr-code-canvas";
101
+ export {
102
+ circleQuietZone,
103
+ getLayoutMargin,
104
+ getQRShapePath,
105
+ getQRSilhouetteFit,
106
+ isShapedCode,
107
+ type QRSilhouetteFit,
108
+ } from "./qr-shape";
109
+ export {
110
+ getStylePairRisk,
111
+ isWeakSharpPair,
112
+ SHARP_DOT_EYE_SAFE_BODIES,
113
+ type StylePairRisk,
114
+ WEAK_BODY_EYES,
115
+ WEAK_DOT_EYES,
116
+ WEAK_DOTS,
117
+ WEAK_EYES,
118
+ WEAK_SHARP_DOT_EYES,
119
+ } from "./scan-risk";
120
+ export * from "./scan-score";
121
+ export {
122
+ createQRSilhouette,
123
+ loadQRSilhouette,
124
+ } from "./silhouette";
37
125
  export * from "./templates";
38
126
  export * from "./utils";
39
-
40
- export function QRCodeCanvas(props: QRPropsCanvas) {
41
- const {
42
- value,
43
- size = DEFAULT_SIZE,
44
- level = DEFAULT_LEVEL,
45
- bgColor = DEFAULT_BGCOLOR,
46
- fgColor = DEFAULT_FGCOLOR,
47
- margin = DEFAULT_MARGIN,
48
- style,
49
- imageSettings,
50
- ...otherProps
51
- } = props;
52
- const imgSrc = imageSettings?.src;
53
- const _canvas = useRef<HTMLCanvasElement>(null);
54
- const _image = useRef<HTMLImageElement>(null);
55
-
56
- // We're just using this state to trigger rerenders when images load. We
57
- // Don't actually read the value anywhere. A smarter use of useEffect would
58
- // depend on this value.
59
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
60
- const [_isImgLoaded, setIsImageLoaded] = useState(false);
61
-
62
- useEffect(() => {
63
- // Always update the canvas. It's cheap enough and we want to be correct
64
- // with the current state.
65
- if (_canvas.current != null) {
66
- const canvas = _canvas.current;
67
-
68
- const ctx = canvas.getContext("2d");
69
- if (!ctx) {
70
- return;
71
- }
72
-
73
- let cells = qrcodegen.QrCode.encodeText(
74
- value,
75
- ERROR_LEVEL_MAP[level]
76
- ).getModules();
77
-
78
- const numCells = cells.length + margin * 2;
79
- const calculatedImageSettings = getImageSettings(
80
- cells,
81
- size,
82
- margin,
83
- imageSettings
84
- );
85
-
86
- const image = _image.current;
87
- const haveImageToRender =
88
- calculatedImageSettings != null &&
89
- image !== null &&
90
- image.complete &&
91
- image.naturalHeight !== 0 &&
92
- image.naturalWidth !== 0;
93
-
94
- if (haveImageToRender && calculatedImageSettings.excavation != null) {
95
- cells = excavateModules(cells, calculatedImageSettings.excavation);
96
- }
97
-
98
- // We're going to scale this so that the number of drawable units
99
- // matches the number of cells. This avoids rounding issues, but does
100
- // result in some potentially unwanted single pixel issues between
101
- // blocks, only in environments that don't support Path2D.
102
- const pixelRatio = window.devicePixelRatio || 1;
103
- canvas.height = canvas.width = size * pixelRatio;
104
- const scale = (size / numCells) * pixelRatio;
105
- ctx.scale(scale, scale);
106
-
107
- // Draw solid background, only paint dark modules.
108
- // Extract solid color from bgColor (canvas doesn't support gradient objects)
109
- const bgColorNormalized = normalizeColorConfig(bgColor, DEFAULT_BGCOLOR);
110
- ctx.fillStyle =
111
- bgColorNormalized.type === "solid"
112
- ? bgColorNormalized.color
113
- : bgColorNormalized.stops[0]?.color || DEFAULT_BGCOLOR;
114
- ctx.fillRect(0, 0, numCells, numCells);
115
-
116
- // Extract solid color from fgColor (canvas doesn't support gradient objects)
117
- const fgColorNormalized = normalizeColorConfig(fgColor, DEFAULT_FGCOLOR);
118
- ctx.fillStyle =
119
- fgColorNormalized.type === "solid"
120
- ? fgColorNormalized.color
121
- : fgColorNormalized.stops[0]?.color || DEFAULT_FGCOLOR;
122
- if (SUPPORTS_PATH2D) {
123
- // $FlowFixMe: Path2D c'tor doesn't support args yet.
124
- ctx.fill(new Path2D(generatePath(cells, margin)));
125
- } else {
126
- cells.forEach((row, rdx) => {
127
- row.forEach((cell, cdx) => {
128
- if (cell) {
129
- ctx.fillRect(cdx + margin, rdx + margin, 1, 1);
130
- }
131
- });
132
- });
133
- }
134
-
135
- if (haveImageToRender) {
136
- ctx.drawImage(
137
- image,
138
- calculatedImageSettings.x + margin,
139
- calculatedImageSettings.y + margin,
140
- calculatedImageSettings.w,
141
- calculatedImageSettings.h
142
- );
143
- }
144
- }
145
- });
146
-
147
- const canvasStyle = { height: size, width: size, ...style };
148
- let img: JSX.Element | null = null;
149
- if (imgSrc != null) {
150
- img = (
151
- <img
152
- alt="QR code"
153
- key={imgSrc}
154
- onLoad={() => {
155
- setIsImageLoaded(true);
156
- }}
157
- ref={_image}
158
- src={imgSrc}
159
- style={{ display: "none" }}
160
- />
161
- );
162
- }
163
- return (
164
- <>
165
- <canvas
166
- height={size}
167
- ref={_canvas}
168
- style={canvasStyle}
169
- width={size}
170
- {...otherProps}
171
- />
172
- {img}
173
- </>
174
- );
175
- }
127
+ /** Reads an 8-bit PNG into RGBA pixels (no DOM or sharp), or null. */
128
+ export { type DecodedImage, decodePng as decodePNGPixels } from "./utils/png";
176
129
 
177
130
  export async function getQRAsSVGDataUri(props: QRProps) {
178
131
  // Import QRCodeSVG and render it to string
@@ -194,7 +147,7 @@ export async function getQRAsSVGDataUri(props: QRProps) {
194
147
  src: base64Image,
195
148
  },
196
149
  };
197
- } catch (_error) {
150
+ } catch {
198
151
  // Remove the image from settings if it fails to load
199
152
  // This prevents SVG from containing external URLs that will fail during canvas conversion
200
153
  const { imageSettings: _removed, ...propsWithoutImage } = props;
@@ -204,11 +157,13 @@ export async function getQRAsSVGDataUri(props: QRProps) {
204
157
 
205
158
  // Create the SVG element with all props including templateId
206
159
  // Filter out props that aren't valid for QRCodeSVG (like hideLogo, url, etc.)
160
+ // Exports never carry the preview's frame label markers.
207
161
  const {
208
162
  hideLogo: _hideLogo,
209
163
  url: _url,
210
164
  logo: _logo,
211
165
  scale: _scale,
166
+ frameLabelMarkers: _frameLabelMarkers,
212
167
  ...validSVGProps
213
168
  } = updatedProps as any;
214
169
 
@@ -260,7 +215,7 @@ const getBase64Image = (imgUrl: string) =>
260
215
  img.onerror = () => {
261
216
  reject(
262
217
  new Error(
263
- `Could not load image from ${imgUrl.substring(0, 100)}... Check CORS settings or use a data URL.`
218
+ `Could not load image from ${imgUrl.slice(0, 100)}... Check CORS settings or use a data URL.`
264
219
  )
265
220
  );
266
221
  };
@@ -318,10 +273,13 @@ export async function getQRAsCanvas(
318
273
 
319
274
  const img = new Image();
320
275
 
321
- // Set canvas size to match the QR code
322
- const size = props.size || DEFAULT_SIZE;
323
- canvas.width = size;
324
- canvas.height = size;
276
+ // Set canvas size to match the QR code (plus its frame, if any)
277
+ const { width, height } = getFrameDimensions(
278
+ props.size || DEFAULT_SIZE,
279
+ props.frame
280
+ );
281
+ canvas.width = Math.round(width);
282
+ canvas.height = Math.round(height);
325
283
 
326
284
  // Add timeout to prevent hanging
327
285
  const timeout = setTimeout(() => {
@@ -342,7 +300,7 @@ export async function getQRAsCanvas(
342
300
  ctx.fillRect(0, 0, canvas.width, canvas.height);
343
301
  }
344
302
 
345
- ctx.drawImage(img, 0, 0, size, size);
303
+ ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
346
304
 
347
305
  if (getCanvas) {
348
306
  resolve(canvas);
@@ -369,4 +327,15 @@ export async function getQRAsCanvas(
369
327
  });
370
328
  }
371
329
 
372
- export { getQRData } from "./helpers";
330
+ export {
331
+ type CodewordMap,
332
+ damagedCodewords,
333
+ getCodewordMap,
334
+ } from "./codeword-map";
335
+ export { getLogoSizeLimit, getQRData } from "./helpers";
336
+ export {
337
+ getLogoBlockDamage,
338
+ LOGO_EC_SAFETY,
339
+ type LogoBlockDamage,
340
+ logoFitsBudget,
341
+ } from "./logo-budget";