qrdx 0.0.46 → 0.0.48

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (212) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +358 -300
  3. package/dist/index.js +27 -7
  4. package/dist/index.js.map +1 -1
  5. package/dist/index.mjs +27 -7
  6. package/dist/index.mjs.map +1 -1
  7. package/dist/qr-code-canvas.js +12 -0
  8. package/dist/qr-code-canvas.js.map +1 -0
  9. package/dist/qr-code-canvas.mjs +12 -0
  10. package/dist/qr-code-canvas.mjs.map +1 -0
  11. package/dist/src/api.d.ts.map +1 -1
  12. package/dist/src/art-trace.d.ts +46 -0
  13. package/dist/src/art-trace.d.ts.map +1 -0
  14. package/dist/src/art.d.ts +138 -0
  15. package/dist/src/art.d.ts.map +1 -0
  16. package/dist/src/body-patterns.d.ts +34 -0
  17. package/dist/src/body-patterns.d.ts.map +1 -0
  18. package/dist/src/codegen.d.ts +5 -5
  19. package/dist/src/codegen.d.ts.map +1 -1
  20. package/dist/src/codeword-map.d.ts +40 -0
  21. package/dist/src/codeword-map.d.ts.map +1 -0
  22. package/dist/src/constants.d.ts +4 -7
  23. package/dist/src/constants.d.ts.map +1 -1
  24. package/dist/src/corner-dot.d.ts +3 -1
  25. package/dist/src/corner-dot.d.ts.map +1 -1
  26. package/dist/src/corner-eye.d.ts +5 -2
  27. package/dist/src/corner-eye.d.ts.map +1 -1
  28. package/dist/src/detection.d.ts.map +1 -1
  29. package/dist/src/download.d.ts +50 -6
  30. package/dist/src/download.d.ts.map +1 -1
  31. package/dist/src/eps.d.ts +62 -0
  32. package/dist/src/eps.d.ts.map +1 -0
  33. package/dist/src/frame-icons.d.ts +11 -0
  34. package/dist/src/frame-icons.d.ts.map +1 -0
  35. package/dist/src/frame-import.d.ts +61 -0
  36. package/dist/src/frame-import.d.ts.map +1 -0
  37. package/dist/src/frame-spec.d.ts +24 -0
  38. package/dist/src/frame-spec.d.ts.map +1 -0
  39. package/dist/src/frame-styles.d.ts +50 -0
  40. package/dist/src/frame-styles.d.ts.map +1 -0
  41. package/dist/src/frame.d.ts +126 -0
  42. package/dist/src/frame.d.ts.map +1 -0
  43. package/dist/src/frames/builtin.d.ts +8 -0
  44. package/dist/src/frames/builtin.d.ts.map +1 -0
  45. package/dist/src/frames/official.d.ts +18 -0
  46. package/dist/src/frames/official.d.ts.map +1 -0
  47. package/dist/src/helpers.d.ts +15 -2
  48. package/dist/src/helpers.d.ts.map +1 -1
  49. package/dist/src/index.d.ts +25 -13
  50. package/dist/src/index.d.ts.map +1 -1
  51. package/dist/src/logo-budget.d.ts +54 -0
  52. package/dist/src/logo-budget.d.ts.map +1 -0
  53. package/dist/src/logo-cleanup.d.ts +25 -0
  54. package/dist/src/logo-cleanup.d.ts.map +1 -0
  55. package/dist/src/logo.d.ts +28 -0
  56. package/dist/src/logo.d.ts.map +1 -0
  57. package/dist/src/print.d.ts +73 -0
  58. package/dist/src/print.d.ts.map +1 -0
  59. package/dist/src/qr-code-canvas.d.ts +13 -0
  60. package/dist/src/qr-code-canvas.d.ts.map +1 -0
  61. package/dist/src/qr-code.d.ts +4 -2
  62. package/dist/src/qr-code.d.ts.map +1 -1
  63. package/dist/src/qr-outline.d.ts +41 -0
  64. package/dist/src/qr-outline.d.ts.map +1 -0
  65. package/dist/src/qr-shape.d.ts +72 -0
  66. package/dist/src/qr-shape.d.ts.map +1 -0
  67. package/dist/src/scan-risk.d.ts +40 -0
  68. package/dist/src/scan-risk.d.ts.map +1 -0
  69. package/dist/src/scan-score.d.ts +154 -0
  70. package/dist/src/scan-score.d.ts.map +1 -0
  71. package/dist/src/shapes.d.ts +36 -0
  72. package/dist/src/shapes.d.ts.map +1 -0
  73. package/dist/src/silhouette.d.ts +28 -0
  74. package/dist/src/silhouette.d.ts.map +1 -0
  75. package/dist/src/utils/color.d.ts.map +1 -1
  76. package/dist/src/utils/fmt.d.ts +24 -0
  77. package/dist/src/utils/fmt.d.ts.map +1 -0
  78. package/dist/src/utils/paint.d.ts +80 -0
  79. package/dist/src/utils/paint.d.ts.map +1 -0
  80. package/dist/src/utils/parse-color.d.ts +21 -0
  81. package/dist/src/utils/parse-color.d.ts.map +1 -0
  82. package/dist/src/utils/path-data.d.ts +24 -0
  83. package/dist/src/utils/path-data.d.ts.map +1 -0
  84. package/dist/src/utils/png.d.ts +16 -0
  85. package/dist/src/utils/png.d.ts.map +1 -0
  86. package/dist/src/utils/polygon-clip.d.ts +26 -0
  87. package/dist/src/utils/polygon-clip.d.ts.map +1 -0
  88. package/dist/src/utils/print-shared.d.ts +20 -0
  89. package/dist/src/utils/print-shared.d.ts.map +1 -0
  90. package/dist/src/utils/svg-geometry.d.ts +49 -0
  91. package/dist/src/utils/svg-geometry.d.ts.map +1 -0
  92. package/dist/src/utils/svg-gradient.d.ts +59 -0
  93. package/dist/src/utils/svg-gradient.d.ts.map +1 -0
  94. package/dist/src/utils/svg-painter.d.ts +152 -0
  95. package/dist/src/utils/svg-painter.d.ts.map +1 -0
  96. package/dist/src/utils/webp.d.ts +25 -0
  97. package/dist/src/utils/webp.d.ts.map +1 -0
  98. package/dist/src/utils.d.ts +18 -2
  99. package/dist/src/utils.d.ts.map +1 -1
  100. package/dist/src/webp.d.ts +19 -0
  101. package/dist/src/webp.d.ts.map +1 -0
  102. package/dist/types/art.d.ts +64 -0
  103. package/dist/types/art.d.ts.map +1 -0
  104. package/dist/types/color.d.ts +125 -7
  105. package/dist/types/color.d.ts.map +1 -1
  106. package/dist/types/corner-dot.d.ts +52 -2
  107. package/dist/types/corner-dot.d.ts.map +1 -1
  108. package/dist/types/corner-eye.d.ts +43 -2
  109. package/dist/types/corner-eye.d.ts.map +1 -1
  110. package/dist/types/frame-spec.d.ts +3509 -0
  111. package/dist/types/frame-spec.d.ts.map +1 -0
  112. package/dist/types/frame.d.ts +68 -0
  113. package/dist/types/frame.d.ts.map +1 -0
  114. package/dist/types/image-pattern.d.ts +15 -1
  115. package/dist/types/image-pattern.d.ts.map +1 -1
  116. package/dist/types/index.d.ts +15 -9
  117. package/dist/types/index.d.ts.map +1 -1
  118. package/dist/types/index.js +6 -0
  119. package/dist/types/index.js.map +1 -0
  120. package/dist/types/index.mjs +6 -0
  121. package/dist/types/index.mjs.map +1 -0
  122. package/dist/types/qr.d.ts +1684 -4
  123. package/dist/types/qr.d.ts.map +1 -1
  124. package/dist/types/template.d.ts +2 -2
  125. package/dist/types/template.d.ts.map +1 -1
  126. package/package.json +70 -25
  127. package/src/api.tsx +67 -17
  128. package/src/art-trace.ts +554 -0
  129. package/src/art.tsx +2226 -0
  130. package/src/body-patterns.tsx +389 -0
  131. package/src/codegen.ts +10 -5
  132. package/src/codeword-map.ts +265 -0
  133. package/src/constants.ts +4 -8
  134. package/src/corner-dot.tsx +209 -33
  135. package/src/corner-eye.ts +328 -60
  136. package/src/detection.ts +5 -11
  137. package/src/download.tsx +174 -70
  138. package/src/eps.ts +701 -0
  139. package/src/frame-icons.ts +325 -0
  140. package/src/frame-import.ts +1198 -0
  141. package/src/frame-spec.ts +518 -0
  142. package/src/frame-styles.ts +1095 -0
  143. package/src/frame.tsx +630 -0
  144. package/src/frames/builtin/banner-bottom.json +58 -0
  145. package/src/frames/builtin/circle.json +81 -0
  146. package/src/frames/builtin/rounded-border.json +60 -0
  147. package/src/frames/builtin/ticket.json +51 -0
  148. package/src/frames/builtin.ts +20 -0
  149. package/src/frames/official/attendance.json +110 -0
  150. package/src/frames/official/audio-guide.json +95 -0
  151. package/src/frames/official/book-now.json +103 -0
  152. package/src/frames/official/check-in.json +110 -0
  153. package/src/frames/official/christmas.json +131 -0
  154. package/src/frames/official/donate.json +77 -0
  155. package/src/frames/official/event-ticket.json +77 -0
  156. package/src/frames/official/feedback-bubble.json +73 -0
  157. package/src/frames/official/follow-us.json +83 -0
  158. package/src/frames/official/get-the-app.json +107 -0
  159. package/src/frames/official/halloween.json +80 -0
  160. package/src/frames/official/new-year.json +94 -0
  161. package/src/frames/official/order-at-table.json +86 -0
  162. package/src/frames/official/product-info.json +119 -0
  163. package/src/frames/official/register-product.json +77 -0
  164. package/src/frames/official/review-us.json +127 -0
  165. package/src/frames/official/rsvp-card.json +112 -0
  166. package/src/frames/official/save-contact.json +117 -0
  167. package/src/frames/official/scan-to-pay.json +94 -0
  168. package/src/frames/official/shop-window.json +81 -0
  169. package/src/frames/official/sign-rider.json +81 -0
  170. package/src/frames/official/table-tent.json +75 -0
  171. package/src/frames/official/wifi-card.json +89 -0
  172. package/src/frames/official/winter.json +142 -0
  173. package/src/frames/official.ts +77 -0
  174. package/src/helpers.ts +168 -11
  175. package/src/index.tsx +150 -172
  176. package/src/logo-budget.ts +226 -0
  177. package/src/logo-cleanup.ts +146 -0
  178. package/src/logo.tsx +137 -0
  179. package/src/print.ts +794 -0
  180. package/src/qr-code-canvas.tsx +241 -0
  181. package/src/qr-code.tsx +51 -16
  182. package/src/qr-outline.ts +511 -0
  183. package/src/qr-shape.ts +255 -0
  184. package/src/scan-risk.ts +197 -0
  185. package/src/scan-score.ts +1629 -0
  186. package/src/shapes.ts +169 -0
  187. package/src/silhouette.ts +334 -0
  188. package/src/utils/color.ts +10 -1
  189. package/src/utils/fmt.ts +59 -0
  190. package/src/utils/paint.ts +317 -0
  191. package/src/utils/parse-color.ts +135 -0
  192. package/src/utils/path-data.ts +182 -0
  193. package/src/utils/png.ts +175 -0
  194. package/src/utils/polygon-clip.ts +163 -0
  195. package/src/utils/print-shared.ts +74 -0
  196. package/src/utils/svg-geometry.ts +838 -0
  197. package/src/utils/svg-gradient.ts +407 -0
  198. package/src/utils/svg-painter.ts +946 -0
  199. package/src/utils/webp.ts +556 -0
  200. package/src/utils.tsx +794 -385
  201. package/src/webp.ts +84 -0
  202. package/types/art.ts +74 -0
  203. package/types/color.ts +132 -12
  204. package/types/corner-dot.ts +41 -4
  205. package/types/corner-eye.ts +31 -4
  206. package/types/frame-spec.ts +748 -0
  207. package/types/frame.ts +67 -0
  208. package/types/image-pattern.ts +15 -0
  209. package/types/index.ts +106 -4
  210. package/types/qr.ts +180 -2
  211. package/types/template.ts +2 -2
  212. package/dist/cli.js +0 -4319
package/src/download.tsx CHANGED
@@ -1,13 +1,44 @@
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";
6
+ import { blobToDataUrl, canvasToWebP, type WebPOptions } from "./webp";
4
7
 
5
- export type DownloadFormat = "png" | "jpg" | "svg" | "pdf";
8
+ /**
9
+ * "webp" is lossless unless `quality` is set. "eps" is vector PostScript:
10
+ * `size.width` becomes points (72 per inch).
11
+ */
12
+ export type DownloadFormat = "png" | "jpg" | "webp" | "svg" | "pdf" | "eps";
13
+
14
+ export interface DownloadFormatInfo {
15
+ mimeType: string;
16
+ extension: string;
17
+ /** No pixel size: the file name states none. */
18
+ vector: boolean;
19
+ }
20
+
21
+ /**
22
+ * Every download format with its MIME type and file extension. Each is made
23
+ * by one branch of downloadQRCode and getQRCodeDataUrl: PNG and JPG by the
24
+ * 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
26
+ * format needs an entry here and a branch there.
27
+ */
28
+ export const DOWNLOAD_FORMATS: Record<DownloadFormat, DownloadFormatInfo> = {
29
+ png: { mimeType: "image/png", extension: "png", vector: false },
30
+ jpg: { mimeType: "image/jpeg", extension: "jpg", vector: false },
31
+ webp: { mimeType: "image/webp", extension: "webp", vector: false },
32
+ svg: { mimeType: "image/svg+xml", extension: "svg", vector: true },
33
+ // A PDF download holds the PNG, so it's named after its pixel size.
34
+ pdf: { mimeType: "application/pdf", extension: "pdf", vector: false },
35
+ eps: { mimeType: "application/postscript", extension: "eps", vector: true },
36
+ };
6
37
 
7
- export type DownloadSize = {
38
+ export interface DownloadSize {
8
39
  width: number;
9
40
  height: number;
10
- };
41
+ }
11
42
 
12
43
  export const PRESET_SIZES: Record<string, DownloadSize> = {
13
44
  small: { width: 200, height: 200 },
@@ -18,11 +49,13 @@ export const PRESET_SIZES: Record<string, DownloadSize> = {
18
49
  "3xl": { width: 2000, height: 2000 },
19
50
  };
20
51
 
21
- export type DownloadOptions = {
52
+ export interface DownloadOptions {
22
53
  format: DownloadFormat;
23
54
  size: DownloadSize;
24
55
  filename?: string;
25
- };
56
+ /** WebP only: 0 to 1 for a smaller lossy file (see `WebPOptions`). */
57
+ quality?: number;
58
+ }
26
59
 
27
60
  /**
28
61
  * Validates if a size value is within acceptable bounds
@@ -61,10 +94,36 @@ export function validateSize(
61
94
  return { isValid: true };
62
95
  }
63
96
 
97
+ /**
98
+ * Returns props rendered at `size`. A logo laid out for the props' own `size`
99
+ * (as `getQRData` does on a 1024px canvas) is scaled along with the code.
100
+ */
101
+ export function withQRSize(qrProps: QRProps, size: number): QRProps {
102
+ const { imageSettings, size: fromSize } = qrProps;
103
+ if (!(imageSettings && fromSize) || fromSize === size) {
104
+ return { ...qrProps, size };
105
+ }
106
+ const ratio = size / fromSize;
107
+ return {
108
+ ...qrProps,
109
+ size,
110
+ imageSettings: {
111
+ ...imageSettings,
112
+ width: imageSettings.width * ratio,
113
+ height: imageSettings.height * ratio,
114
+ ...(imageSettings.x != null && { x: imageSettings.x * ratio }),
115
+ ...(imageSettings.y != null && { y: imageSettings.y * ratio }),
116
+ ...(imageSettings.padding != null && {
117
+ padding: imageSettings.padding * ratio,
118
+ }),
119
+ },
120
+ };
121
+ }
122
+
64
123
  /**
65
124
  * Downloads a file to the user's device
66
125
  */
67
- function downloadFile(dataUrl: string, filename: string) {
126
+ export function downloadFile(dataUrl: string, filename: string) {
68
127
  const link = document.createElement("a");
69
128
  link.href = dataUrl;
70
129
  link.download = filename;
@@ -73,36 +132,75 @@ function downloadFile(dataUrl: string, filename: string) {
73
132
  document.body.removeChild(link);
74
133
  }
75
134
 
135
+ const getMimeType = (format: DownloadFormat): string =>
136
+ (DOWNLOAD_FORMATS[format] ?? DOWNLOAD_FORMATS.png).mimeType;
137
+
76
138
  /**
77
- * Get the MIME type for a given format
139
+ * The default file name of a download: for PNG, JPG and WebP (and the image
140
+ * a PDF holds) the exact pixel size of what is saved, which a frame makes
141
+ * bigger than the code (a 400 px code in a bottom banner is 496 × 568);
142
+ * SVG and EPS have no pixel size and name none.
78
143
  */
79
- function getMimeType(format: DownloadFormat): string {
80
- switch (format) {
81
- case "png":
82
- return "image/png";
83
- case "jpg":
84
- return "image/jpeg";
85
- case "svg":
86
- return "image/svg+xml";
87
- case "pdf":
88
- return "application/pdf";
89
- default:
90
- return "image/png";
144
+ export function getDownloadFilename(
145
+ format: DownloadFormat,
146
+ size: DownloadSize,
147
+ frame?: QRProps["frame"]
148
+ ): string {
149
+ const { extension, vector } = DOWNLOAD_FORMATS[format];
150
+ if (vector) {
151
+ return `qr-code.${extension}`;
91
152
  }
153
+ // The canvas size getQRAsCanvas and getQRAsPDF draw on
154
+ const { width, height } = getFrameDimensions(size.width, frame);
155
+ return `qr-code-${Math.round(width)}x${Math.round(height)}.${extension}`;
92
156
  }
93
157
 
94
158
  /**
95
- * Get the file extension for a given format
159
+ * The code as a WebP image (plus its frame, if any): lossless unless
160
+ * `options.quality` is set, with a transparent background kept. Browsers
161
+ * that can't encode WebP (Safari) get it from QRdx's own lossless encoder,
162
+ * never a PNG in disguise.
96
163
  */
97
- function getFileExtension(format: DownloadFormat): string {
98
- return format;
164
+ export async function getQRAsWebP(
165
+ qrProps: QRProps,
166
+ options: WebPOptions = {}
167
+ ): Promise<Blob> {
168
+ const canvas = (await getQRAsCanvas(
169
+ qrProps,
170
+ "image/webp",
171
+ true
172
+ )) as HTMLCanvasElement;
173
+ try {
174
+ return await canvasToWebP(canvas, options);
175
+ } finally {
176
+ canvas.remove();
177
+ }
178
+ }
179
+
180
+ // One CSS pixel = one PostScript point (72 per inch).
181
+ const MM_PER_POINT = 25.4 / 72;
182
+
183
+ /** EPS of the code at `size` points wide, as a data URL. */
184
+ async function getQRAsEPSDataUrl(qrProps: QRProps): Promise<string> {
185
+ const eps = await getQRAsEPS(qrProps, {
186
+ sizeMm: (qrProps.size ?? DEFAULT_SIZE) * MM_PER_POINT,
187
+ });
188
+ return epsToDataUrl(eps);
99
189
  }
100
190
 
101
191
  /**
102
192
  * Convert QR code to PDF format
103
193
  */
104
194
  async function getQRAsPDF(qrProps: QRProps): Promise<string> {
105
- const { jsPDF } = await import("jspdf");
195
+ // jspdf is an optional peer dependency, loaded only when a PDF is requested.
196
+ let jsPDF: typeof import("jspdf").jsPDF;
197
+ try {
198
+ ({ jsPDF } = await import("jspdf"));
199
+ } catch (error) {
200
+ throw new Error(
201
+ `PDF export needs the optional "jspdf" package. Install it with \`npm install jspdf\`. (${error})`
202
+ );
203
+ }
106
204
 
107
205
  // Get the SVG as a data URI
108
206
  const svgDataUri = await getQRAsSVGDataUri(qrProps);
@@ -123,14 +221,18 @@ async function getQRAsPDF(qrProps: QRProps): Promise<string> {
123
221
  throw new Error(`Failed to encode SVG for PDF conversion: ${error}`);
124
222
  }
125
223
 
224
+ const { width, height } = getFrameDimensions(
225
+ qrProps.size || DEFAULT_SIZE,
226
+ qrProps.frame
227
+ );
228
+
126
229
  // Get canvas from SVG using base64 encoding
127
230
  const canvas = await new Promise<HTMLCanvasElement>((resolve, reject) => {
128
231
  const img = new Image();
129
232
  const tempCanvas = document.createElement("canvas");
130
- const size = qrProps.size || DEFAULT_SIZE;
131
233
 
132
- tempCanvas.width = size;
133
- tempCanvas.height = size;
234
+ tempCanvas.width = Math.round(width);
235
+ tempCanvas.height = Math.round(height);
134
236
 
135
237
  const ctx = tempCanvas.getContext("2d");
136
238
  if (!ctx) {
@@ -151,7 +253,7 @@ async function getQRAsPDF(qrProps: QRProps): Promise<string> {
151
253
  img.onload = () => {
152
254
  clearTimeout(timeout);
153
255
  try {
154
- ctx.drawImage(img, 0, 0, size, size);
256
+ ctx.drawImage(img, 0, 0, tempCanvas.width, tempCanvas.height);
155
257
  resolve(tempCanvas);
156
258
  } catch (error) {
157
259
  clearTimeout(timeout);
@@ -171,16 +273,17 @@ async function getQRAsPDF(qrProps: QRProps): Promise<string> {
171
273
 
172
274
  // Create PDF with the canvas
173
275
  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)
276
+ // Convert pixels to mm (assuming 96 DPI)
277
+ const mmWidth = width * 0.264_583;
278
+ const mmHeight = height * 0.264_583;
176
279
 
177
280
  const pdf = new jsPDF({
178
- orientation: "portrait",
281
+ orientation: mmWidth > mmHeight ? "landscape" : "portrait",
179
282
  unit: "mm",
180
- format: [mmSize, mmSize],
283
+ format: [mmWidth, mmHeight],
181
284
  });
182
285
 
183
- pdf.addImage(imgData, "PNG", 0, 0, mmSize, mmSize);
286
+ pdf.addImage(imgData, "PNG", 0, 0, mmWidth, mmHeight);
184
287
 
185
288
  canvas.remove();
186
289
 
@@ -195,7 +298,7 @@ export async function downloadQRCode(
195
298
  qrProps: QRProps,
196
299
  options: DownloadOptions
197
300
  ): Promise<void> {
198
- const { format, size, filename } = options;
301
+ const { format, size, filename, quality } = options;
199
302
 
200
303
  // Validate size
201
304
  const validation = validateSize(size.width, size.height);
@@ -203,14 +306,11 @@ export async function downloadQRCode(
203
306
  throw new Error(validation.error);
204
307
  }
205
308
 
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
- };
309
+ // Create props with the custom size (QR codes are square, so use width)
310
+ const propsWithSize = withQRSize(qrProps, size.width);
211
311
 
212
- const defaultFilename = `qr-code-${size.width}x${size.height}.${getFileExtension(format)}`;
213
- const finalFilename = filename || defaultFilename;
312
+ const finalFilename =
313
+ filename || getDownloadFilename(format, size, qrProps.frame);
214
314
 
215
315
  try {
216
316
  if (format === "svg") {
@@ -221,6 +321,11 @@ export async function downloadQRCode(
221
321
  // Download as PDF
222
322
  const pdfDataUrl = await getQRAsPDF(propsWithSize);
223
323
  downloadFile(pdfDataUrl, finalFilename);
324
+ } else if (format === "eps") {
325
+ downloadFile(await getQRAsEPSDataUrl(propsWithSize), finalFilename);
326
+ } else if (format === "webp") {
327
+ const webp = await getQRAsWebP(propsWithSize, { quality });
328
+ downloadFile(await blobToDataUrl(webp), finalFilename);
224
329
  } else {
225
330
  // Download as PNG or JPG
226
331
  const mimeType = getMimeType(format);
@@ -243,7 +348,8 @@ export async function downloadQRCode(
243
348
  export async function getQRCodeDataUrl(
244
349
  qrProps: QRProps,
245
350
  format: DownloadFormat,
246
- size: DownloadSize
351
+ size: DownloadSize,
352
+ options: WebPOptions = {}
247
353
  ): Promise<string> {
248
354
  // Validate size
249
355
  const validation = validateSize(size.width, size.height);
@@ -252,10 +358,7 @@ export async function getQRCodeDataUrl(
252
358
  }
253
359
 
254
360
  // Create props with the custom size
255
- const propsWithSize = {
256
- ...qrProps,
257
- size: size.width,
258
- };
361
+ const propsWithSize = withQRSize(qrProps, size.width);
259
362
 
260
363
  if (format === "svg") {
261
364
  return await getQRAsSVGDataUri(propsWithSize);
@@ -265,6 +368,14 @@ export async function getQRCodeDataUrl(
265
368
  return await getQRAsPDF(propsWithSize);
266
369
  }
267
370
 
371
+ if (format === "eps") {
372
+ return await getQRAsEPSDataUrl(propsWithSize);
373
+ }
374
+
375
+ if (format === "webp") {
376
+ return await blobToDataUrl(await getQRAsWebP(propsWithSize, options));
377
+ }
378
+
268
379
  const mimeType = getMimeType(format);
269
380
  const result = await getQRAsCanvas(propsWithSize, mimeType);
270
381
 
@@ -289,37 +400,30 @@ export async function copyQRCodeToClipboard(
289
400
  throw new Error(validation.error);
290
401
  }
291
402
 
292
- const propsWithSize = {
293
- ...qrProps,
294
- size: size.width,
295
- };
403
+ const propsWithSize = withQRSize(qrProps, size.width);
404
+
405
+ // Render inside a promise handed straight to ClipboardItem: Safari rejects
406
+ // clipboard writes that start after an await (outside the user gesture).
407
+ const imageBlob = getQRAsCanvas(propsWithSize, "image/png", true).then(
408
+ (result) =>
409
+ new Promise<Blob>((resolve, reject) => {
410
+ const canvas = result as HTMLCanvasElement;
411
+ canvas.toBlob((canvasBlob) => {
412
+ canvas.remove();
413
+ if (canvasBlob) {
414
+ resolve(canvasBlob);
415
+ } else {
416
+ reject(new Error("Failed to create blob from canvas"));
417
+ }
418
+ }, "image/png");
419
+ })
420
+ );
296
421
 
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
422
  await navigator.clipboard.write([
317
423
  new ClipboardItem({
318
424
  "image/png": imageBlob,
319
425
  }),
320
426
  ]);
321
-
322
- canvas.remove();
323
427
  } catch (error) {
324
428
  console.error("Error copying to clipboard:", error);
325
429
  throw error;