qrdx 0.0.57 → 0.0.59

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (260) hide show
  1. package/README.md +22 -6
  2. package/dist/angular.js +33 -3
  3. package/dist/angular.js.map +1 -1
  4. package/dist/angular.mjs +33 -3
  5. package/dist/angular.mjs.map +1 -1
  6. package/dist/core.js +7 -7
  7. package/dist/core.js.map +1 -1
  8. package/dist/core.mjs +7 -7
  9. package/dist/core.mjs.map +1 -1
  10. package/dist/element.browser.mjs +6 -6
  11. package/dist/element.browser.mjs.map +1 -1
  12. package/dist/element.js +1 -1
  13. package/dist/element.js.map +1 -1
  14. package/dist/element.mjs +1 -1
  15. package/dist/element.mjs.map +1 -1
  16. package/dist/frames-index.js +1 -1
  17. package/dist/frames-index.js.map +1 -1
  18. package/dist/frames-index.mjs +1 -1
  19. package/dist/frames-index.mjs.map +1 -1
  20. package/dist/frames-official.js +1 -1
  21. package/dist/frames-official.js.map +1 -1
  22. package/dist/frames-official.mjs +1 -1
  23. package/dist/frames-official.mjs.map +1 -1
  24. package/dist/index.js +7 -7
  25. package/dist/index.js.map +1 -1
  26. package/dist/index.mjs +7 -7
  27. package/dist/index.mjs.map +1 -1
  28. package/dist/qr-code-canvas.js +1 -1
  29. package/dist/qr-code-canvas.js.map +1 -1
  30. package/dist/qr-code-canvas.mjs +1 -1
  31. package/dist/qr-code-canvas.mjs.map +1 -1
  32. package/dist/schemas.js +1 -1
  33. package/dist/schemas.js.map +1 -1
  34. package/dist/schemas.mjs +1 -1
  35. package/dist/schemas.mjs.map +1 -1
  36. package/dist/src/angular.d.ts +9 -3
  37. package/dist/src/download.d.ts +32 -6
  38. package/dist/src/download.d.ts.map +1 -1
  39. package/dist/src/element.d.ts +10 -1
  40. package/dist/src/element.d.ts.map +1 -1
  41. package/dist/src/engine.d.ts +5 -2
  42. package/dist/src/engine.d.ts.map +1 -1
  43. package/dist/src/eps.d.ts.map +1 -1
  44. package/dist/src/export.d.ts.map +1 -1
  45. package/dist/src/frame.d.ts +24 -0
  46. package/dist/src/frame.d.ts.map +1 -1
  47. package/dist/src/frames/catalogue/all.d.ts.map +1 -1
  48. package/dist/src/frames/catalogue/batches.d.ts +6 -0
  49. package/dist/src/frames/catalogue/batches.d.ts.map +1 -0
  50. package/dist/src/frames/catalogue/call-to-action.d.ts.map +1 -1
  51. package/dist/src/frames/catalogue/counts.d.ts +4 -2
  52. package/dist/src/frames/catalogue/counts.d.ts.map +1 -1
  53. package/dist/src/frames/catalogue/index.d.ts.map +1 -1
  54. package/dist/src/frames/catalogue/objects.d.ts +5 -0
  55. package/dist/src/frames/catalogue/objects.d.ts.map +1 -0
  56. package/dist/src/frames/catalogue/playful.d.ts +5 -0
  57. package/dist/src/frames/catalogue/playful.d.ts.map +1 -0
  58. package/dist/src/frames/catalogue/tags.d.ts.map +1 -1
  59. package/dist/src/frames/collections.d.ts.map +1 -1
  60. package/dist/src/frames/official-types.d.ts +1 -1
  61. package/dist/src/frames/official-types.d.ts.map +1 -1
  62. package/dist/src/frames/official.d.ts.map +1 -1
  63. package/dist/src/gif.d.ts +40 -0
  64. package/dist/src/gif.d.ts.map +1 -0
  65. package/dist/src/helpers.d.ts +1 -1
  66. package/dist/src/helpers.d.ts.map +1 -1
  67. package/dist/src/ico.d.ts +11 -0
  68. package/dist/src/ico.d.ts.map +1 -0
  69. package/dist/src/index.d.ts +2 -1
  70. package/dist/src/index.d.ts.map +1 -1
  71. package/dist/src/looks.d.ts +101 -0
  72. package/dist/src/looks.d.ts.map +1 -0
  73. package/dist/src/plain.d.ts.map +1 -1
  74. package/dist/src/print.d.ts.map +1 -1
  75. package/dist/src/qr-code-canvas.d.ts.map +1 -1
  76. package/dist/src/qr-code-props.d.ts +10 -1
  77. package/dist/src/qr-code-props.d.ts.map +1 -1
  78. package/dist/src/qr-code.d.ts.map +1 -1
  79. package/dist/src/qr-frame-svg.d.ts +10 -0
  80. package/dist/src/qr-frame-svg.d.ts.map +1 -0
  81. package/dist/src/scan-score.d.ts.map +1 -1
  82. package/dist/src/utils/gif.d.ts +61 -0
  83. package/dist/src/utils/gif.d.ts.map +1 -0
  84. package/dist/src/utils/ico.d.ts +30 -0
  85. package/dist/src/utils/ico.d.ts.map +1 -0
  86. package/dist/src/utils/parse-color.d.ts +1 -1
  87. package/dist/src/utils/parse-color.d.ts.map +1 -1
  88. package/dist/src/utils/print-shared.d.ts.map +1 -1
  89. package/dist/src/utils/svg-painter.d.ts +13 -0
  90. package/dist/src/utils/svg-painter.d.ts.map +1 -1
  91. package/dist/src/utils.d.ts.map +1 -1
  92. package/dist/src/vanilla-api.d.ts +2 -2
  93. package/dist/src/vanilla-api.d.ts.map +1 -1
  94. package/dist/src/vue.d.ts +1 -1
  95. package/dist/src/vue.d.ts.map +1 -1
  96. package/dist/types/constants.d.ts +41 -0
  97. package/dist/types/constants.d.ts.map +1 -1
  98. package/dist/types/index.js +2 -2
  99. package/dist/types/index.js.map +1 -1
  100. package/dist/types/index.mjs +2 -2
  101. package/dist/types/index.mjs.map +1 -1
  102. package/dist/types/plain.d.ts +2 -2
  103. package/dist/types/plain.d.ts.map +1 -1
  104. package/dist/types/qr.d.ts +399 -1
  105. package/dist/types/qr.d.ts.map +1 -1
  106. package/dist/types/react.d.ts +10 -1
  107. package/dist/types/react.d.ts.map +1 -1
  108. package/dist/types/schemas.d.ts +1 -1
  109. package/dist/types/schemas.d.ts.map +1 -1
  110. package/dist/vanilla.browser.mjs +34 -34
  111. package/dist/vanilla.browser.mjs.map +1 -1
  112. package/dist/vue.js +1 -1
  113. package/dist/vue.js.map +1 -1
  114. package/dist/vue.mjs +1 -1
  115. package/dist/vue.mjs.map +1 -1
  116. package/package.json +6 -2
  117. package/src/angular.ts +14 -2
  118. package/src/download.tsx +92 -7
  119. package/src/element.ts +16 -1
  120. package/src/engine.ts +22 -0
  121. package/src/eps.ts +8 -1
  122. package/src/export.ts +16 -0
  123. package/src/frame.tsx +51 -0
  124. package/src/frames/catalogue/all.ts +202 -0
  125. package/src/frames/catalogue/batches.ts +114 -0
  126. package/src/frames/catalogue/call-to-action.ts +58 -0
  127. package/src/frames/catalogue/counts.ts +4 -2
  128. package/src/frames/catalogue/index.ts +808 -0
  129. package/src/frames/catalogue/objects.ts +65 -0
  130. package/src/frames/catalogue/playful.ts +97 -0
  131. package/src/frames/catalogue/tags.ts +137 -0
  132. package/src/frames/collections.ts +2 -0
  133. package/src/frames/official/badge-bottom-light.json +91 -0
  134. package/src/frames/official/badge-bottom.json +79 -0
  135. package/src/frames/official/billboard-campaign.json +274 -0
  136. package/src/frames/official/billboard-light.json +287 -0
  137. package/src/frames/official/bookmark-light.json +88 -0
  138. package/src/frames/official/bookmark-strip.json +83 -0
  139. package/src/frames/official/border-medium.json +71 -0
  140. package/src/frames/official/border-thick.json +71 -0
  141. package/src/frames/official/border-thin.json +71 -0
  142. package/src/frames/official/bottle-label.json +136 -0
  143. package/src/frames/official/bottle-light.json +133 -0
  144. package/src/frames/official/camera-viewfinder.json +174 -0
  145. package/src/frames/official/cassette-tape.json +236 -0
  146. package/src/frames/official/cat-with-card-light.json +246 -0
  147. package/src/frames/official/cat-with-card.json +234 -0
  148. package/src/frames/official/cinema-marquee.json +537 -0
  149. package/src/frames/official/circle-viewfinder.json +87 -0
  150. package/src/frames/official/clapperboard-light.json +163 -0
  151. package/src/frames/official/clapperboard-scene.json +151 -0
  152. package/src/frames/official/comic-panel.json +386 -0
  153. package/src/frames/official/corner-tab-light.json +101 -0
  154. package/src/frames/official/corner-tab.json +89 -0
  155. package/src/frames/official/crystal-ball.json +105 -0
  156. package/src/frames/official/delivery-box-light.json +132 -0
  157. package/src/frames/official/delivery-box-track.json +132 -0
  158. package/src/frames/official/fish-tank.json +276 -0
  159. package/src/frames/official/flag-light.json +136 -0
  160. package/src/frames/official/flag-team.json +127 -0
  161. package/src/frames/official/floppy-disk.json +106 -0
  162. package/src/frames/official/flying-saucer.json +128 -0
  163. package/src/frames/official/gallery-frame.json +195 -0
  164. package/src/frames/official/globe-stand.json +136 -0
  165. package/src/frames/official/gumball-machine.json +451 -0
  166. package/src/frames/official/heart-light.json +59 -0
  167. package/src/frames/official/heart-scan.json +51 -0
  168. package/src/frames/official/hexagon-light.json +87 -0
  169. package/src/frames/official/hexagon-scan.json +72 -0
  170. package/src/frames/official/hot-air-balloon.json +132 -0
  171. package/src/frames/official/jigsaw-piece-light.json +68 -0
  172. package/src/frames/official/jigsaw-piece.json +60 -0
  173. package/src/frames/official/laptop-screen.json +137 -0
  174. package/src/frames/official/lighthouse-tower.json +132 -0
  175. package/src/frames/official/magnifying-glass.json +85 -0
  176. package/src/frames/official/mailbox-flag.json +112 -0
  177. package/src/frames/official/mailbox-light.json +120 -0
  178. package/src/frames/official/map-pin-light.json +76 -0
  179. package/src/frames/official/map-pin.json +58 -0
  180. package/src/frames/official/matchbox-drawer.json +170 -0
  181. package/src/frames/official/microwave-door.json +220 -0
  182. package/src/frames/official/milk-carton.json +129 -0
  183. package/src/frames/official/monitor-screen.json +111 -0
  184. package/src/frames/official/monster-mouth.json +180 -0
  185. package/src/frames/official/mug-light.json +138 -0
  186. package/src/frames/official/mug-refill.json +125 -0
  187. package/src/frames/official/museum-plaque-light.json +187 -0
  188. package/src/frames/official/museum-plaque.json +187 -0
  189. package/src/frames/official/music-player-light.json +151 -0
  190. package/src/frames/official/music-player.json +136 -0
  191. package/src/frames/official/octagon-light.json +126 -0
  192. package/src/frames/official/octagon-sign.json +120 -0
  193. package/src/frames/official/open-book.json +156 -0
  194. package/src/frames/official/parking-meter.json +107 -0
  195. package/src/frames/official/passport-stamp.json +132 -0
  196. package/src/frames/official/pizza-box.json +122 -0
  197. package/src/frames/official/polka-dot-border.json +502 -0
  198. package/src/frames/official/popcorn-bucket.json +305 -0
  199. package/src/frames/official/porthole.json +264 -0
  200. package/src/frames/official/raised-card-thin.json +111 -0
  201. package/src/frames/official/raised-card.json +99 -0
  202. package/src/frames/official/retro-radio.json +180 -0
  203. package/src/frames/official/ribbon-top-light.json +125 -0
  204. package/src/frames/official/ribbon-top.json +113 -0
  205. package/src/frames/official/robot-chest.json +197 -0
  206. package/src/frames/official/roll-up-banner-light.json +137 -0
  207. package/src/frames/official/roll-up-banner.json +125 -0
  208. package/src/frames/official/round-tag-light.json +86 -0
  209. package/src/frames/official/round-tag.json +79 -0
  210. package/src/frames/official/sailboat-sail.json +99 -0
  211. package/src/frames/official/scroll-story.json +184 -0
  212. package/src/frames/official/sketch-border.json +92 -0
  213. package/src/frames/official/sketch-circle.json +57 -0
  214. package/src/frames/official/snow-globe.json +170 -0
  215. package/src/frames/official/soda-can.json +160 -0
  216. package/src/frames/official/spin-rays.json +126 -0
  217. package/src/frames/official/spiral-notebook.json +476 -0
  218. package/src/frames/official/stage-curtains.json +303 -0
  219. package/src/frames/official/starburst-light.json +128 -0
  220. package/src/frames/official/starburst.json +118 -0
  221. package/src/frames/official/swirl-border.json +122 -0
  222. package/src/frames/official/tablet-screen.json +130 -0
  223. package/src/frames/official/text-frame.json +79 -0
  224. package/src/frames/official/toaster-toast.json +114 -0
  225. package/src/frames/official/train-window.json +176 -0
  226. package/src/frames/official/treasure-map.json +207 -0
  227. package/src/frames/official/tv-set.json +256 -0
  228. package/src/frames/official/vending-machine.json +218 -0
  229. package/src/frames/official/video-player.json +118 -0
  230. package/src/frames/official/video-tape.json +137 -0
  231. package/src/frames/official/vinyl-sleeve-light.json +139 -0
  232. package/src/frames/official/vinyl-sleeve.json +127 -0
  233. package/src/frames/official/washing-machine.json +146 -0
  234. package/src/frames/official-types.ts +3 -1
  235. package/src/frames/official.ts +4 -3
  236. package/src/gif.ts +188 -0
  237. package/src/helpers.ts +12 -0
  238. package/src/ico.ts +55 -0
  239. package/src/index.tsx +2 -0
  240. package/src/looks.ts +467 -0
  241. package/src/plain.tsx +6 -0
  242. package/src/print.ts +12 -0
  243. package/src/qr-code-canvas.tsx +23 -2
  244. package/src/qr-code-props.ts +54 -19
  245. package/src/qr-code.tsx +7 -39
  246. package/src/qr-frame-svg.tsx +26 -0
  247. package/src/scan-score.ts +38 -6
  248. package/src/utils/gif.ts +632 -0
  249. package/src/utils/ico.ts +150 -0
  250. package/src/utils/parse-color.ts +163 -23
  251. package/src/utils/print-shared.ts +25 -1
  252. package/src/utils/svg-painter.ts +18 -0
  253. package/src/utils.tsx +136 -17
  254. package/src/vanilla-api.ts +2 -2
  255. package/src/vue.ts +7 -1
  256. package/types/constants.ts +47 -0
  257. package/types/plain.ts +19 -0
  258. package/types/qr.ts +98 -0
  259. package/types/react.ts +11 -1
  260. package/types/schemas.ts +5 -0
package/src/gif.ts ADDED
@@ -0,0 +1,188 @@
1
+ // GIF from pixels or a canvas in any browser, and in Node. No browser encodes
2
+ // GIF from a canvas (toBlob("image/gif") returns a PNG), so the pixels are read
3
+ // back and written by the built-in encoder (utils/gif.ts). The file is checked
4
+ // by its bytes before it is returned, so a PNG is never passed off as a GIF.
5
+ //
6
+ // A GIF holds 256 colors. A code in flat colors fits as it is and comes out
7
+ // pixel for pixel; a gradient, a photo logo or an image fill is reduced to a
8
+ // palette first, and when the value is known the reduced image is scanned
9
+ // against the original, so a GIF that no longer reads is refused, not saved.
10
+
11
+ import type { QRArtPixels } from "./art";
12
+ import { encodeGIFFromRGBA, indexedToRGBA, isGIFFile } from "./utils/gif";
13
+
14
+ export {
15
+ encodeGIF,
16
+ encodeGIFFromRGBA,
17
+ GIF_ALPHA_THRESHOLD,
18
+ GIF_MAX_COLORS,
19
+ GIF_MAX_DIMENSION,
20
+ type IndexedImage,
21
+ indexedToRGBA,
22
+ isGIFFile,
23
+ type QuantizedImage,
24
+ quantizeRGBA,
25
+ } from "./utils/gif";
26
+
27
+ /** The MIME type of a GIF. */
28
+ export const GIF_MIME = "image/gif";
29
+
30
+ /** Thrown when reducing a design to GIF's 256 colors would stop it scanning. */
31
+ export class GIFScanError extends Error {
32
+ constructor(message: string) {
33
+ super(message);
34
+ this.name = "GIFScanError";
35
+ }
36
+ }
37
+
38
+ export interface GIFResult {
39
+ file: Uint8Array;
40
+ /** Colors in the palette, the transparent one included */
41
+ colors: number;
42
+ /** True when every pixel kept its color and opacity exactly */
43
+ lossless: boolean;
44
+ }
45
+
46
+ const OPAQUE = 255;
47
+
48
+ /** RGBA pixels laid over white, as a scanner reads a see-through code on paper. */
49
+ function overWhite(rgba: ArrayLike<number>): Uint8ClampedArray {
50
+ const out = new Uint8ClampedArray(rgba.length);
51
+ for (let at = 0; at < rgba.length; at += 4) {
52
+ const alpha = rgba[at + 3] as number;
53
+ for (let channel = 0; channel < 3; channel++) {
54
+ out[at + channel] = Math.round(
55
+ ((rgba[at + channel] as number) * alpha + OPAQUE * (OPAQUE - alpha)) /
56
+ OPAQUE
57
+ );
58
+ }
59
+ out[at + 3] = OPAQUE;
60
+ }
61
+ return out;
62
+ }
63
+
64
+ /** Whether `pixels` hold a QR code that decodes to `expected` as they are. */
65
+ function decodesTo(
66
+ decode: typeof import("jsqr").default,
67
+ pixels: QRArtPixels,
68
+ expected: string
69
+ ): boolean {
70
+ const rgba = Uint8ClampedArray.from(pixels.data);
71
+ const found = decode(rgba, pixels.width, pixels.height, {
72
+ inversionAttempts: "dontInvert",
73
+ });
74
+ if (!found) {
75
+ return false;
76
+ }
77
+ if (found.data === expected) {
78
+ return true;
79
+ }
80
+ try {
81
+ return (
82
+ new TextDecoder().decode(Uint8Array.from(found.binaryData)) === expected
83
+ );
84
+ } catch {
85
+ return false;
86
+ }
87
+ }
88
+
89
+ /**
90
+ * Throws a GIFScanError when `reduced` (the GIF's pixels read back) no longer
91
+ * decodes to `expected` although `original` did. Both are laid over white
92
+ * first, as a scanner reads a see-through code on paper. A design that doesn't
93
+ * decode to begin with (the decoder is stricter than a phone for some styles)
94
+ * isn't blamed on GIF, and pixels that came through unchanged aren't decoded
95
+ * at all. Decoding takes `jsqr`, loaded here when it is needed; if it can't be
96
+ * loaded the GIF can't be checked, so it is refused rather than passed on. The
97
+ * server uses this too, with the pixels of the GIF sharp wrote.
98
+ */
99
+ export async function assertGIFStillScans(
100
+ original: QRArtPixels,
101
+ reduced: QRArtPixels,
102
+ expected: string
103
+ ): Promise<void> {
104
+ const { width, height } = original;
105
+ const before = overWhite(original.data);
106
+ const after = overWhite(reduced.data);
107
+ if (
108
+ reduced.width === width &&
109
+ reduced.height === height &&
110
+ before.every((value, index) => value === after[index])
111
+ ) {
112
+ return;
113
+ }
114
+ let decode: typeof import("jsqr").default;
115
+ try {
116
+ decode = (await import("jsqr")).default;
117
+ } catch {
118
+ throw new GIFScanError(
119
+ "This design uses more colors than a GIF holds (256), and the reduced image could not be checked because jsqr is not available. Download a PNG instead."
120
+ );
121
+ }
122
+ if (!decodesTo(decode, { data: before, width, height }, expected)) {
123
+ return;
124
+ }
125
+ const readable = decodesTo(
126
+ decode,
127
+ { data: after, width: reduced.width, height: reduced.height },
128
+ expected
129
+ );
130
+ if (!readable) {
131
+ throw new GIFScanError(
132
+ "This design uses more colors than a GIF holds (256), and the reduced image would not scan. Download a PNG instead."
133
+ );
134
+ }
135
+ }
136
+
137
+ /**
138
+ * RGBA pixels (`width * height * 4` bytes, top row first) as a GIF. A pixel
139
+ * with alpha under 128 is the GIF's transparent color. When `expected` (the
140
+ * value the code encodes) is given and the colors had to be reduced, the
141
+ * result is scanned against the original and a GIFScanError is thrown if it
142
+ * no longer reads. Throws a RangeError for a bad size.
143
+ */
144
+ export async function pixelsToGIF(
145
+ pixels: QRArtPixels,
146
+ expected?: string
147
+ ): Promise<GIFResult> {
148
+ const { file, image } = encodeGIFFromRGBA(
149
+ pixels.data,
150
+ pixels.width,
151
+ pixels.height
152
+ );
153
+ if (!isGIFFile(file)) {
154
+ throw new Error("Could not write a GIF file");
155
+ }
156
+ if (expected !== undefined && !image.lossless) {
157
+ await assertGIFStillScans(
158
+ pixels,
159
+ {
160
+ data: indexedToRGBA(image),
161
+ width: pixels.width,
162
+ height: pixels.height,
163
+ },
164
+ expected
165
+ );
166
+ }
167
+ return { file, colors: image.colors, lossless: image.lossless };
168
+ }
169
+
170
+ /**
171
+ * A canvas as a GIF, with a transparent background kept (see `pixelsToGIF`).
172
+ * Pass the code's value as `expected` to refuse a GIF that would not scan.
173
+ */
174
+ export async function canvasToGIF(
175
+ canvas: HTMLCanvasElement,
176
+ expected?: string
177
+ ): Promise<Blob> {
178
+ const context = canvas.getContext("2d");
179
+ if (!context) {
180
+ throw new Error("Could not get canvas context");
181
+ }
182
+ const { data } = context.getImageData(0, 0, canvas.width, canvas.height);
183
+ const { file } = await pixelsToGIF(
184
+ { data, width: canvas.width, height: canvas.height },
185
+ expected
186
+ );
187
+ return new Blob([file as BlobPart], { type: GIF_MIME });
188
+ }
package/src/helpers.ts CHANGED
@@ -174,6 +174,12 @@ export function getQRData({
174
174
  moduleScale,
175
175
  autoLevel,
176
176
  shape,
177
+ eyeShapes,
178
+ eyeGradient,
179
+ bgRadius,
180
+ backdrop,
181
+ halo,
182
+ shadow,
177
183
  }: QRDataProps): QRDataResult {
178
184
  const code = {
179
185
  value: `${value}`,
@@ -203,6 +209,12 @@ export function getQRData({
203
209
  ...(moduleScale !== undefined && { moduleScale }),
204
210
  ...(autoLevel && { autoLevel }),
205
211
  ...(shape && shape !== "square" && { shape }),
212
+ ...(eyeShapes && { eyeShapes }),
213
+ ...(eyeGradient && eyeGradient !== "corner" && { eyeGradient }),
214
+ ...(bgRadius && { bgRadius }),
215
+ ...(backdrop && { backdrop }),
216
+ ...(halo && { halo }),
217
+ ...(shadow && { shadow }),
206
218
  ...(imageSettings && { imageSettings }),
207
219
  };
208
220
  }
package/src/ico.ts ADDED
@@ -0,0 +1,55 @@
1
+ // ICO from canvases in any browser. No browser encodes ICO from a canvas
2
+ // (toBlob("image/x-icon") returns a PNG), so each canvas is written as a PNG,
3
+ // which every browser can do, and the built-in encoder (utils/ico.ts) wraps
4
+ // the PNGs in an ICO. The file is checked by its bytes before it is returned,
5
+ // so a PNG is never passed off as ICO.
6
+
7
+ import { encodeICO, ICO_MIME, isICOFile } from "./utils/ico";
8
+
9
+ export {
10
+ encodeICO,
11
+ ICO_MAX_DIMENSION,
12
+ ICO_MIME,
13
+ ICO_SIZES,
14
+ type IcoImage,
15
+ isICOFile,
16
+ } from "./utils/ico";
17
+
18
+ const PNG_DATA_URL = "data:image/png;base64,";
19
+
20
+ /** A canvas as PNG file bytes. Throws where the browser can't write PNG. */
21
+ export function canvasToPNGBytes(canvas: HTMLCanvasElement): Uint8Array {
22
+ const url = canvas.toDataURL("image/png");
23
+ if (!url.startsWith(PNG_DATA_URL)) {
24
+ throw new Error("This browser can't write a PNG from a canvas");
25
+ }
26
+ const binary = atob(url.slice(PNG_DATA_URL.length));
27
+ const bytes = new Uint8Array(binary.length);
28
+ for (let index = 0; index < binary.length; index++) {
29
+ bytes[index] = binary.charCodeAt(index);
30
+ }
31
+ return bytes;
32
+ }
33
+
34
+ /**
35
+ * Canvases (each 1 to 256 px a side) as one ICO, an image for each, in the
36
+ * order given. A transparent background stays transparent.
37
+ */
38
+ export function canvasesToICO(canvases: readonly HTMLCanvasElement[]): Blob {
39
+ const file = encodeICO(
40
+ canvases.map((canvas) => ({
41
+ width: canvas.width,
42
+ height: canvas.height,
43
+ png: canvasToPNGBytes(canvas),
44
+ }))
45
+ );
46
+ if (!isICOFile(file)) {
47
+ throw new Error("Could not write an ICO file");
48
+ }
49
+ return new Blob([file as BlobPart], { type: ICO_MIME });
50
+ }
51
+
52
+ /** One canvas (1 to 256 px a side) as an ICO with a single image. */
53
+ export function canvasToICO(canvas: HTMLCanvasElement): Blob {
54
+ return canvasesToICO([canvas]);
55
+ }
package/src/index.tsx CHANGED
@@ -5,6 +5,7 @@
5
5
 
6
6
  export type {
7
7
  QRCodePropsSVG,
8
+ QRFrameSVGProps,
8
9
  QRPropsCanvas,
9
10
  QRPropsSVG,
10
11
  } from "../types/react";
@@ -16,3 +17,4 @@ export * from "./qr-code";
16
17
  // package stays usable from React Server Components.
17
18
  export { QRCodeCanvas } from "./qr-code-canvas";
18
19
  export { QRCodeSVG } from "./qr-code-svg";
20
+ export { QRFrameSVG } from "./qr-frame-svg";