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
package/src/download.tsx CHANGED
@@ -1,13 +1,16 @@
1
1
  import type { QRProps } from "../types";
2
2
  import { DEFAULT_SIZE } from "./constants";
3
+ import { epsToDataUrl, getQRAsEPS } from "./eps";
4
+ import { getFrameDimensions } from "./frame";
3
5
  import { getQRAsCanvas, getQRAsSVGDataUri } from "./index";
4
6
 
5
- export type DownloadFormat = "png" | "jpg" | "svg" | "pdf";
7
+ /** "eps" is vector PostScript: `size.width` becomes points (72 per inch). */
8
+ export type DownloadFormat = "png" | "jpg" | "svg" | "pdf" | "eps";
6
9
 
7
- export type DownloadSize = {
10
+ export interface DownloadSize {
8
11
  width: number;
9
12
  height: number;
10
- };
13
+ }
11
14
 
12
15
  export const PRESET_SIZES: Record<string, DownloadSize> = {
13
16
  small: { width: 200, height: 200 },
@@ -18,11 +21,11 @@ export const PRESET_SIZES: Record<string, DownloadSize> = {
18
21
  "3xl": { width: 2000, height: 2000 },
19
22
  };
20
23
 
21
- export type DownloadOptions = {
24
+ export interface DownloadOptions {
22
25
  format: DownloadFormat;
23
26
  size: DownloadSize;
24
27
  filename?: string;
25
- };
28
+ }
26
29
 
27
30
  /**
28
31
  * Validates if a size value is within acceptable bounds
@@ -61,10 +64,36 @@ export function validateSize(
61
64
  return { isValid: true };
62
65
  }
63
66
 
67
+ /**
68
+ * Returns props rendered at `size`. A logo laid out for the props' own `size`
69
+ * (as `getQRData` does on a 1024px canvas) is scaled along with the code.
70
+ */
71
+ export function withQRSize(qrProps: QRProps, size: number): QRProps {
72
+ const { imageSettings, size: fromSize } = qrProps;
73
+ if (!(imageSettings && fromSize) || fromSize === size) {
74
+ return { ...qrProps, size };
75
+ }
76
+ const ratio = size / fromSize;
77
+ return {
78
+ ...qrProps,
79
+ size,
80
+ imageSettings: {
81
+ ...imageSettings,
82
+ width: imageSettings.width * ratio,
83
+ height: imageSettings.height * ratio,
84
+ ...(imageSettings.x != null && { x: imageSettings.x * ratio }),
85
+ ...(imageSettings.y != null && { y: imageSettings.y * ratio }),
86
+ ...(imageSettings.padding != null && {
87
+ padding: imageSettings.padding * ratio,
88
+ }),
89
+ },
90
+ };
91
+ }
92
+
64
93
  /**
65
94
  * Downloads a file to the user's device
66
95
  */
67
- function downloadFile(dataUrl: string, filename: string) {
96
+ export function downloadFile(dataUrl: string, filename: string) {
68
97
  const link = document.createElement("a");
69
98
  link.href = dataUrl;
70
99
  link.download = filename;
@@ -86,6 +115,8 @@ function getMimeType(format: DownloadFormat): string {
86
115
  return "image/svg+xml";
87
116
  case "pdf":
88
117
  return "application/pdf";
118
+ case "eps":
119
+ return "application/postscript";
89
120
  default:
90
121
  return "image/png";
91
122
  }
@@ -98,11 +129,53 @@ function getFileExtension(format: DownloadFormat): string {
98
129
  return format;
99
130
  }
100
131
 
132
+ /** Vector files: they have no pixel size, so their names state none. */
133
+ const VECTOR_FORMATS: ReadonlySet<DownloadFormat> = new Set(["svg", "eps"]);
134
+
135
+ /**
136
+ * The default file name of a download: for PNG and JPG (and the image a
137
+ * PDF holds) the exact pixel size of what is saved, which a frame makes
138
+ * bigger than the code (a 400 px code in a bottom banner is 496 × 568);
139
+ * SVG and EPS have no pixel size and name none.
140
+ */
141
+ export function getDownloadFilename(
142
+ format: DownloadFormat,
143
+ size: DownloadSize,
144
+ frame?: QRProps["frame"]
145
+ ): string {
146
+ const extension = getFileExtension(format);
147
+ if (VECTOR_FORMATS.has(format)) {
148
+ return `qr-code.${extension}`;
149
+ }
150
+ // The canvas size getQRAsCanvas and getQRAsPDF draw on
151
+ const { width, height } = getFrameDimensions(size.width, frame);
152
+ return `qr-code-${Math.round(width)}x${Math.round(height)}.${extension}`;
153
+ }
154
+
155
+ // One CSS pixel = one PostScript point (72 per inch).
156
+ const MM_PER_POINT = 25.4 / 72;
157
+
158
+ /** EPS of the code at `size` points wide, as a data URL. */
159
+ async function getQRAsEPSDataUrl(qrProps: QRProps): Promise<string> {
160
+ const eps = await getQRAsEPS(qrProps, {
161
+ sizeMm: (qrProps.size ?? DEFAULT_SIZE) * MM_PER_POINT,
162
+ });
163
+ return epsToDataUrl(eps);
164
+ }
165
+
101
166
  /**
102
167
  * Convert QR code to PDF format
103
168
  */
104
169
  async function getQRAsPDF(qrProps: QRProps): Promise<string> {
105
- const { jsPDF } = await import("jspdf");
170
+ // jspdf is an optional peer dependency, loaded only when a PDF is requested.
171
+ let jsPDF: typeof import("jspdf").jsPDF;
172
+ try {
173
+ ({ jsPDF } = await import("jspdf"));
174
+ } catch (error) {
175
+ throw new Error(
176
+ `PDF export needs the optional "jspdf" package. Install it with \`npm install jspdf\`. (${error})`
177
+ );
178
+ }
106
179
 
107
180
  // Get the SVG as a data URI
108
181
  const svgDataUri = await getQRAsSVGDataUri(qrProps);
@@ -123,14 +196,18 @@ async function getQRAsPDF(qrProps: QRProps): Promise<string> {
123
196
  throw new Error(`Failed to encode SVG for PDF conversion: ${error}`);
124
197
  }
125
198
 
199
+ const { width, height } = getFrameDimensions(
200
+ qrProps.size || DEFAULT_SIZE,
201
+ qrProps.frame
202
+ );
203
+
126
204
  // Get canvas from SVG using base64 encoding
127
205
  const canvas = await new Promise<HTMLCanvasElement>((resolve, reject) => {
128
206
  const img = new Image();
129
207
  const tempCanvas = document.createElement("canvas");
130
- const size = qrProps.size || DEFAULT_SIZE;
131
208
 
132
- tempCanvas.width = size;
133
- tempCanvas.height = size;
209
+ tempCanvas.width = Math.round(width);
210
+ tempCanvas.height = Math.round(height);
134
211
 
135
212
  const ctx = tempCanvas.getContext("2d");
136
213
  if (!ctx) {
@@ -151,7 +228,7 @@ async function getQRAsPDF(qrProps: QRProps): Promise<string> {
151
228
  img.onload = () => {
152
229
  clearTimeout(timeout);
153
230
  try {
154
- ctx.drawImage(img, 0, 0, size, size);
231
+ ctx.drawImage(img, 0, 0, tempCanvas.width, tempCanvas.height);
155
232
  resolve(tempCanvas);
156
233
  } catch (error) {
157
234
  clearTimeout(timeout);
@@ -171,16 +248,17 @@ async function getQRAsPDF(qrProps: QRProps): Promise<string> {
171
248
 
172
249
  // Create PDF with the canvas
173
250
  const imgData = canvas.toDataURL("image/png", 1.0);
174
- const size = qrProps.size || DEFAULT_SIZE;
175
- const mmSize = size * 0.264_583; // Convert pixels to mm (assuming 96 DPI)
251
+ // Convert pixels to mm (assuming 96 DPI)
252
+ const mmWidth = width * 0.264_583;
253
+ const mmHeight = height * 0.264_583;
176
254
 
177
255
  const pdf = new jsPDF({
178
- orientation: "portrait",
256
+ orientation: mmWidth > mmHeight ? "landscape" : "portrait",
179
257
  unit: "mm",
180
- format: [mmSize, mmSize],
258
+ format: [mmWidth, mmHeight],
181
259
  });
182
260
 
183
- pdf.addImage(imgData, "PNG", 0, 0, mmSize, mmSize);
261
+ pdf.addImage(imgData, "PNG", 0, 0, mmWidth, mmHeight);
184
262
 
185
263
  canvas.remove();
186
264
 
@@ -203,14 +281,11 @@ export async function downloadQRCode(
203
281
  throw new Error(validation.error);
204
282
  }
205
283
 
206
- // Create props with the custom size
207
- const propsWithSize = {
208
- ...qrProps,
209
- size: size.width, // QR codes are square, so we just use width
210
- };
284
+ // Create props with the custom size (QR codes are square, so use width)
285
+ const propsWithSize = withQRSize(qrProps, size.width);
211
286
 
212
- const defaultFilename = `qr-code-${size.width}x${size.height}.${getFileExtension(format)}`;
213
- const finalFilename = filename || defaultFilename;
287
+ const finalFilename =
288
+ filename || getDownloadFilename(format, size, qrProps.frame);
214
289
 
215
290
  try {
216
291
  if (format === "svg") {
@@ -221,6 +296,8 @@ export async function downloadQRCode(
221
296
  // Download as PDF
222
297
  const pdfDataUrl = await getQRAsPDF(propsWithSize);
223
298
  downloadFile(pdfDataUrl, finalFilename);
299
+ } else if (format === "eps") {
300
+ downloadFile(await getQRAsEPSDataUrl(propsWithSize), finalFilename);
224
301
  } else {
225
302
  // Download as PNG or JPG
226
303
  const mimeType = getMimeType(format);
@@ -252,10 +329,7 @@ export async function getQRCodeDataUrl(
252
329
  }
253
330
 
254
331
  // Create props with the custom size
255
- const propsWithSize = {
256
- ...qrProps,
257
- size: size.width,
258
- };
332
+ const propsWithSize = withQRSize(qrProps, size.width);
259
333
 
260
334
  if (format === "svg") {
261
335
  return await getQRAsSVGDataUri(propsWithSize);
@@ -265,6 +339,10 @@ export async function getQRCodeDataUrl(
265
339
  return await getQRAsPDF(propsWithSize);
266
340
  }
267
341
 
342
+ if (format === "eps") {
343
+ return await getQRAsEPSDataUrl(propsWithSize);
344
+ }
345
+
268
346
  const mimeType = getMimeType(format);
269
347
  const result = await getQRAsCanvas(propsWithSize, mimeType);
270
348
 
@@ -289,37 +367,30 @@ export async function copyQRCodeToClipboard(
289
367
  throw new Error(validation.error);
290
368
  }
291
369
 
292
- const propsWithSize = {
293
- ...qrProps,
294
- size: size.width,
295
- };
370
+ const propsWithSize = withQRSize(qrProps, size.width);
371
+
372
+ // Render inside a promise handed straight to ClipboardItem: Safari rejects
373
+ // clipboard writes that start after an await (outside the user gesture).
374
+ const imageBlob = getQRAsCanvas(propsWithSize, "image/png", true).then(
375
+ (result) =>
376
+ new Promise<Blob>((resolve, reject) => {
377
+ const canvas = result as HTMLCanvasElement;
378
+ canvas.toBlob((canvasBlob) => {
379
+ canvas.remove();
380
+ if (canvasBlob) {
381
+ resolve(canvasBlob);
382
+ } else {
383
+ reject(new Error("Failed to create blob from canvas"));
384
+ }
385
+ }, "image/png");
386
+ })
387
+ );
296
388
 
297
- // Get canvas
298
- const canvas = (await getQRAsCanvas(
299
- propsWithSize,
300
- "image/png",
301
- true
302
- )) as HTMLCanvasElement;
303
-
304
- // Convert canvas to blob
305
- const imageBlob = await new Promise<Blob>((resolve, reject) => {
306
- canvas.toBlob((canvasBlob) => {
307
- if (canvasBlob) {
308
- resolve(canvasBlob);
309
- } else {
310
- reject(new Error("Failed to create blob from canvas"));
311
- }
312
- }, "image/png");
313
- });
314
-
315
- // Copy to clipboard
316
389
  await navigator.clipboard.write([
317
390
  new ClipboardItem({
318
391
  "image/png": imageBlob,
319
392
  }),
320
393
  ]);
321
-
322
- canvas.remove();
323
394
  } catch (error) {
324
395
  console.error("Error copying to clipboard:", error);
325
396
  throw error;