qrdx 0.0.47 → 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.
- package/README.md +8 -3
- package/dist/index.js +7 -7
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +7 -7
- package/dist/index.mjs.map +1 -1
- package/dist/src/download.d.ts +32 -5
- package/dist/src/download.d.ts.map +1 -1
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/utils/webp.d.ts +25 -0
- package/dist/src/utils/webp.d.ts.map +1 -0
- package/dist/src/webp.d.ts +19 -0
- package/dist/src/webp.d.ts.map +1 -0
- package/package.json +5 -3
- package/src/download.tsx +70 -37
- package/src/index.tsx +9 -0
- package/src/utils/webp.ts +556 -0
- package/src/webp.ts +84 -0
package/dist/src/download.d.ts
CHANGED
|
@@ -1,6 +1,24 @@
|
|
|
1
1
|
import type { QRProps } from "../types";
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
import { type WebPOptions } from "./webp";
|
|
3
|
+
/**
|
|
4
|
+
* "webp" is lossless unless `quality` is set. "eps" is vector PostScript:
|
|
5
|
+
* `size.width` becomes points (72 per inch).
|
|
6
|
+
*/
|
|
7
|
+
export type DownloadFormat = "png" | "jpg" | "webp" | "svg" | "pdf" | "eps";
|
|
8
|
+
export interface DownloadFormatInfo {
|
|
9
|
+
mimeType: string;
|
|
10
|
+
extension: string;
|
|
11
|
+
/** No pixel size: the file name states none. */
|
|
12
|
+
vector: boolean;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Every download format with its MIME type and file extension. Each is made
|
|
16
|
+
* by one branch of downloadQRCode and getQRCodeDataUrl: PNG and JPG by the
|
|
17
|
+
* canvas, WebP by canvasToWebP (the canvas, or the built-in lossless
|
|
18
|
+
* encoder), SVG from the markup, PDF with jspdf, EPS by getQRAsEPS. A new
|
|
19
|
+
* format needs an entry here and a branch there.
|
|
20
|
+
*/
|
|
21
|
+
export declare const DOWNLOAD_FORMATS: Record<DownloadFormat, DownloadFormatInfo>;
|
|
4
22
|
export interface DownloadSize {
|
|
5
23
|
width: number;
|
|
6
24
|
height: number;
|
|
@@ -10,6 +28,8 @@ export interface DownloadOptions {
|
|
|
10
28
|
format: DownloadFormat;
|
|
11
29
|
size: DownloadSize;
|
|
12
30
|
filename?: string;
|
|
31
|
+
/** WebP only: 0 to 1 for a smaller lossy file (see `WebPOptions`). */
|
|
32
|
+
quality?: number;
|
|
13
33
|
}
|
|
14
34
|
/**
|
|
15
35
|
* Validates if a size value is within acceptable bounds
|
|
@@ -28,12 +48,19 @@ export declare function withQRSize(qrProps: QRProps, size: number): QRProps;
|
|
|
28
48
|
*/
|
|
29
49
|
export declare function downloadFile(dataUrl: string, filename: string): void;
|
|
30
50
|
/**
|
|
31
|
-
* The default file name of a download: for PNG and
|
|
32
|
-
* PDF holds) the exact pixel size of what is saved, which a frame makes
|
|
51
|
+
* The default file name of a download: for PNG, JPG and WebP (and the image
|
|
52
|
+
* a PDF holds) the exact pixel size of what is saved, which a frame makes
|
|
33
53
|
* bigger than the code (a 400 px code in a bottom banner is 496 × 568);
|
|
34
54
|
* SVG and EPS have no pixel size and name none.
|
|
35
55
|
*/
|
|
36
56
|
export declare function getDownloadFilename(format: DownloadFormat, size: DownloadSize, frame?: QRProps["frame"]): string;
|
|
57
|
+
/**
|
|
58
|
+
* The code as a WebP image (plus its frame, if any): lossless unless
|
|
59
|
+
* `options.quality` is set, with a transparent background kept. Browsers
|
|
60
|
+
* that can't encode WebP (Safari) get it from QRdx's own lossless encoder,
|
|
61
|
+
* never a PNG in disguise.
|
|
62
|
+
*/
|
|
63
|
+
export declare function getQRAsWebP(qrProps: QRProps, options?: WebPOptions): Promise<Blob>;
|
|
37
64
|
/**
|
|
38
65
|
* Downloads a QR code with the specified options
|
|
39
66
|
*/
|
|
@@ -41,7 +68,7 @@ export declare function downloadQRCode(qrProps: QRProps, options: DownloadOption
|
|
|
41
68
|
/**
|
|
42
69
|
* Gets a QR code as a data URL for preview or other purposes
|
|
43
70
|
*/
|
|
44
|
-
export declare function getQRCodeDataUrl(qrProps: QRProps, format: DownloadFormat, size: DownloadSize): Promise<string>;
|
|
71
|
+
export declare function getQRCodeDataUrl(qrProps: QRProps, format: DownloadFormat, size: DownloadSize, options?: WebPOptions): Promise<string>;
|
|
45
72
|
/**
|
|
46
73
|
* Copies QR code to clipboard (works best with PNG format)
|
|
47
74
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"download.d.ts","sourceRoot":"","sources":["../../src/download.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"download.d.ts","sourceRoot":"","sources":["../../src/download.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AAKxC,OAAO,EAA+B,KAAK,WAAW,EAAE,MAAM,QAAQ,CAAC;AAEvE;;;GAGG;AACH,MAAM,MAAM,cAAc,GAAG,KAAK,GAAG,KAAK,GAAG,MAAM,GAAG,KAAK,GAAG,KAAK,GAAG,KAAK,CAAC;AAE5E,MAAM,WAAW,kBAAkB;IACjC,QAAQ,EAAE,MAAM,CAAC;IACjB,SAAS,EAAE,MAAM,CAAC;IAClB,gDAAgD;IAChD,MAAM,EAAE,OAAO,CAAC;CACjB;AAED;;;;;;GAMG;AACH,eAAO,MAAM,gBAAgB,EAAE,MAAM,CAAC,cAAc,EAAE,kBAAkB,CAQvE,CAAC;AAEF,MAAM,WAAW,YAAY;IAC3B,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;CAChB;AAED,eAAO,MAAM,YAAY,EAAE,MAAM,CAAC,MAAM,EAAE,YAAY,CAOrD,CAAC;AAEF,MAAM,WAAW,eAAe;IAC9B,MAAM,EAAE,cAAc,CAAC;IACvB,IAAI,EAAE,YAAY,CAAC;IACnB,QAAQ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CHANGED
|
@@ -28,6 +28,7 @@ export * from "./templates";
|
|
|
28
28
|
export * from "./utils";
|
|
29
29
|
/** Reads an 8-bit PNG into RGBA pixels (no DOM or sharp), or null. */
|
|
30
30
|
export { type DecodedImage, decodePng as decodePNGPixels } from "./utils/png";
|
|
31
|
+
export * from "./webp";
|
|
31
32
|
export declare function getQRAsSVGDataUri(props: QRProps): Promise<string>;
|
|
32
33
|
export declare function getQRAsCanvas(props: QRProps, type: string, getCanvas?: boolean): Promise<HTMLCanvasElement | string>;
|
|
33
34
|
export { type CodewordMap, damagedCodewords, getCodewordMap, } from "./codeword-map";
|
package/dist/src/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.tsx"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,iEAAiE;AAEjE,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.tsx"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,iEAAiE;AAEjE,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AAKxC,cAAc,UAAU,CAAC;AACzB,cAAc,OAAO,CAAC;AACtB,OAAO,EACL,gBAAgB,EAChB,qBAAqB,EACrB,kBAAkB,EAClB,iBAAiB,EACjB,eAAe,EACf,oBAAoB,EACpB,aAAa,EACb,cAAc,EACd,mBAAmB,EACnB,mBAAmB,EACnB,KAAK,eAAe,EACpB,KAAK,oBAAoB,EACzB,KAAK,WAAW,EAChB,KAAK,kBAAkB,EACvB,WAAW,GACZ,MAAM,OAAO,CAAC;AACf,OAAO,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AACvE,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,OAAO,EACL,gBAAgB,EAChB,qBAAqB,EACrB,KAAK,UAAU,EAC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rB,MAAM,YAAY,CAAC;AACpB,OAAO,EACL,gBAAgB,EAChB,eAAe,EACf,yBAAyB,EACzB,KAAK,aAAa,EAClB,cAAc,EACd,aAAa,EACb,SAAS,EACT,SAAS,EACT,mBAAmB,GACpB,MAAM,aAAa,CAAC;AACrB,cAAc,cAAc,CAAC;AAC7B,OAAO,EACL,kBAAkB,EAClB,gBAAgB,GACjB,MAAM,cAAc,CAAC;AACtB,cAAc,aAAa,CAAC;AAC5B,cAAc,SAAS,CAAC;AACxB,sEAAsE;AACtE,OAAO,EAAE,KAAK,YAAY,EAAE,SAAS,IAAI,eAAe,EAAE,MAAM,aAAa,CAAC;AAC9E,cAAc,QAAQ,CAAC;AAEvB,wBAAsB,iBAAiB,CAAC,KAAK,EAAE,OAAO,mBAiDrD;AA6DD,wBAAsB,aAAa,CACjC,KAAK,EAAE,OAAO,EACd,IAAI,EAAE,MAAM,EACZ,SAAS,CAAC,EAAE,OAAO,GAClB,OAAO,CAAC,iBAAiB,GAAG,MAAM,CAAC,CA2FrC;AAED,OAAO,EACL,KAAK,WAAW,EAChB,gBAAgB,EAChB,cAAc,GACf,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,gBAAgB,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AACxD,OAAO,EACL,kBAAkB,EAClB,cAAc,EACd,KAAK,eAAe,EACpB,cAAc,GACf,MAAM,eAAe,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/** biome-ignore-all lint/style/noMagicNumbers: WebP (VP8L) format constants */
|
|
2
|
+
/** biome-ignore-all lint/suspicious/noBitwiseOperators: bit packing */
|
|
3
|
+
/** Largest width or height VP8L can store. */
|
|
4
|
+
export declare const WEBP_MAX_DIMENSION = 16384;
|
|
5
|
+
/**
|
|
6
|
+
* Huffman code lengths for these symbol counts, none longer than `limit`.
|
|
7
|
+
* When the tree comes out too deep, small counts are raised and it is built
|
|
8
|
+
* again, which flattens it. Exactly one used symbol gets length 1.
|
|
9
|
+
*/
|
|
10
|
+
export declare function huffmanLengths(counts: ArrayLike<number>, limit: number): Uint8Array;
|
|
11
|
+
/**
|
|
12
|
+
* Encodes RGBA pixels (4 bytes each, row by row, not premultiplied, as
|
|
13
|
+
* `getImageData` returns them) as a lossless WebP file. Every pixel decodes
|
|
14
|
+
* back exactly, transparent ones included.
|
|
15
|
+
*/
|
|
16
|
+
export declare function encodeWebPLossless(rgba: ArrayLike<number>, width: number, height: number): Uint8Array;
|
|
17
|
+
/** True when the bytes start like a WebP file (`RIFF····WEBP`). */
|
|
18
|
+
export declare function isWebPFile(bytes: Uint8Array): boolean;
|
|
19
|
+
/**
|
|
20
|
+
* How a WebP file's image is compressed: "lossless" (VP8L) or "lossy" (VP8,
|
|
21
|
+
* with or without an alpha chunk), or null when it isn't WebP or has no image
|
|
22
|
+
* chunk in the bytes given.
|
|
23
|
+
*/
|
|
24
|
+
export declare function getWebPCompression(bytes: Uint8Array): "lossless" | "lossy" | null;
|
|
25
|
+
//# sourceMappingURL=webp.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"webp.d.ts","sourceRoot":"","sources":["../../../src/utils/webp.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,uEAAuE;AAWvE,8CAA8C;AAC9C,eAAO,MAAM,kBAAkB,QAAS,CAAC;AAkEzC;;;;GAIG;AACH,wBAAgB,cAAc,CAC5B,MAAM,EAAE,SAAS,CAAC,MAAM,CAAC,EACzB,KAAK,EAAE,MAAM,GACZ,UAAU,CAmEZ;AAkPD;;;;GAIG;AACH,wBAAgB,kBAAkB,CAChC,IAAI,EAAE,SAAS,CAAC,MAAM,CAAC,EACvB,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,MAAM,GACb,UAAU,CA0GZ;AAMD,mEAAmE;AACnE,wBAAgB,UAAU,CAAC,KAAK,EAAE,UAAU,GAAG,OAAO,CAMrD;AAED;;;;GAIG;AACH,wBAAgB,kBAAkB,CAChC,KAAK,EAAE,UAAU,GAChB,UAAU,GAAG,OAAO,GAAG,IAAI,CAsB7B"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export { encodeWebPLossless, getWebPCompression, isWebPFile, WEBP_MAX_DIMENSION, } from "./utils/webp";
|
|
2
|
+
export interface WebPOptions {
|
|
3
|
+
/**
|
|
4
|
+
* 0 to 1 for a smaller lossy file, whose edges and colors can soften.
|
|
5
|
+
* Leave it out for lossless (the default), which keeps every pixel, and so
|
|
6
|
+
* the code's edges, exact. Browsers that can't encode WebP get a lossless
|
|
7
|
+
* file either way.
|
|
8
|
+
*/
|
|
9
|
+
quality?: number;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* A canvas as WebP: lossless unless `options.quality` is set. The browser
|
|
13
|
+
* encodes it when it can; otherwise (Safari) the built-in lossless encoder
|
|
14
|
+
* does. Transparency is kept.
|
|
15
|
+
*/
|
|
16
|
+
export declare function canvasToWebP(canvas: HTMLCanvasElement, options?: WebPOptions): Promise<Blob>;
|
|
17
|
+
/** A blob as a base64 data URL (no FileReader, so it also runs in Node). */
|
|
18
|
+
export declare function blobToDataUrl(blob: Blob): Promise<string>;
|
|
19
|
+
//# sourceMappingURL=webp.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"webp.d.ts","sourceRoot":"","sources":["../../src/webp.ts"],"names":[],"mappings":"AASA,OAAO,EACL,kBAAkB,EAClB,kBAAkB,EAClB,UAAU,EACV,kBAAkB,GACnB,MAAM,cAAc,CAAC;AAEtB,MAAM,WAAW,WAAW;IAC1B;;;;;OAKG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAmBD;;;;GAIG;AACH,wBAAsB,YAAY,CAChC,MAAM,EAAE,iBAAiB,EACzB,OAAO,GAAE,WAAgB,GACxB,OAAO,CAAC,IAAI,CAAC,CAkBf;AAID,4EAA4E;AAC5E,wBAAsB,aAAa,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,CAAC,MAAM,CAAC,CAS/D"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "qrdx",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "Customizable QR code generator for React: SVG and canvas rendering, dot and eye patterns, gradients, logos, frames, Logo QR and Image QR art, PNG/SVG/PDF/EPS export, a scan score and QR scanning.",
|
|
3
|
+
"version": "0.0.48",
|
|
4
|
+
"description": "Customizable QR code generator for React: SVG and canvas rendering, dot and eye patterns, gradients, logos, frames, Logo QR and Image QR art, PNG/JPG/WebP/SVG/PDF/EPS export, a scan score and QR scanning.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"qr-code",
|
|
7
7
|
"qrcode",
|
|
@@ -14,7 +14,8 @@
|
|
|
14
14
|
"qr-code-svg",
|
|
15
15
|
"qr-code-logo",
|
|
16
16
|
"eps",
|
|
17
|
-
"pdf"
|
|
17
|
+
"pdf",
|
|
18
|
+
"webp"
|
|
18
19
|
],
|
|
19
20
|
"homepage": "https://qrdx.dev/docs",
|
|
20
21
|
"bugs": {
|
|
@@ -70,6 +71,7 @@
|
|
|
70
71
|
"jspdf": "^4.2.1",
|
|
71
72
|
"next": "16.4.3",
|
|
72
73
|
"react": "^19.3.1",
|
|
74
|
+
"sharp": "0.35.5",
|
|
73
75
|
"tsup": "^9.0.0",
|
|
74
76
|
"typescript": "6.0.3",
|
|
75
77
|
"vitest": "^5.0.5",
|
package/src/download.tsx
CHANGED
|
@@ -3,9 +3,37 @@ import { DEFAULT_SIZE } from "./constants";
|
|
|
3
3
|
import { epsToDataUrl, getQRAsEPS } from "./eps";
|
|
4
4
|
import { getFrameDimensions } from "./frame";
|
|
5
5
|
import { getQRAsCanvas, getQRAsSVGDataUri } from "./index";
|
|
6
|
+
import { blobToDataUrl, canvasToWebP, type WebPOptions } from "./webp";
|
|
6
7
|
|
|
7
|
-
/**
|
|
8
|
-
|
|
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
|
+
};
|
|
9
37
|
|
|
10
38
|
export interface DownloadSize {
|
|
11
39
|
width: number;
|
|
@@ -25,6 +53,8 @@ export interface DownloadOptions {
|
|
|
25
53
|
format: DownloadFormat;
|
|
26
54
|
size: DownloadSize;
|
|
27
55
|
filename?: string;
|
|
56
|
+
/** WebP only: 0 to 1 for a smaller lossy file (see `WebPOptions`). */
|
|
57
|
+
quality?: number;
|
|
28
58
|
}
|
|
29
59
|
|
|
30
60
|
/**
|
|
@@ -102,39 +132,12 @@ export function downloadFile(dataUrl: string, filename: string) {
|
|
|
102
132
|
document.body.removeChild(link);
|
|
103
133
|
}
|
|
104
134
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
*/
|
|
108
|
-
function getMimeType(format: DownloadFormat): string {
|
|
109
|
-
switch (format) {
|
|
110
|
-
case "png":
|
|
111
|
-
return "image/png";
|
|
112
|
-
case "jpg":
|
|
113
|
-
return "image/jpeg";
|
|
114
|
-
case "svg":
|
|
115
|
-
return "image/svg+xml";
|
|
116
|
-
case "pdf":
|
|
117
|
-
return "application/pdf";
|
|
118
|
-
case "eps":
|
|
119
|
-
return "application/postscript";
|
|
120
|
-
default:
|
|
121
|
-
return "image/png";
|
|
122
|
-
}
|
|
123
|
-
}
|
|
135
|
+
const getMimeType = (format: DownloadFormat): string =>
|
|
136
|
+
(DOWNLOAD_FORMATS[format] ?? DOWNLOAD_FORMATS.png).mimeType;
|
|
124
137
|
|
|
125
138
|
/**
|
|
126
|
-
*
|
|
127
|
-
|
|
128
|
-
function getFileExtension(format: DownloadFormat): string {
|
|
129
|
-
return format;
|
|
130
|
-
}
|
|
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
|
|
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
|
|
138
141
|
* bigger than the code (a 400 px code in a bottom banner is 496 × 568);
|
|
139
142
|
* SVG and EPS have no pixel size and name none.
|
|
140
143
|
*/
|
|
@@ -143,8 +146,8 @@ export function getDownloadFilename(
|
|
|
143
146
|
size: DownloadSize,
|
|
144
147
|
frame?: QRProps["frame"]
|
|
145
148
|
): string {
|
|
146
|
-
const extension =
|
|
147
|
-
if (
|
|
149
|
+
const { extension, vector } = DOWNLOAD_FORMATS[format];
|
|
150
|
+
if (vector) {
|
|
148
151
|
return `qr-code.${extension}`;
|
|
149
152
|
}
|
|
150
153
|
// The canvas size getQRAsCanvas and getQRAsPDF draw on
|
|
@@ -152,6 +155,28 @@ export function getDownloadFilename(
|
|
|
152
155
|
return `qr-code-${Math.round(width)}x${Math.round(height)}.${extension}`;
|
|
153
156
|
}
|
|
154
157
|
|
|
158
|
+
/**
|
|
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.
|
|
163
|
+
*/
|
|
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
|
+
|
|
155
180
|
// One CSS pixel = one PostScript point (72 per inch).
|
|
156
181
|
const MM_PER_POINT = 25.4 / 72;
|
|
157
182
|
|
|
@@ -273,7 +298,7 @@ export async function downloadQRCode(
|
|
|
273
298
|
qrProps: QRProps,
|
|
274
299
|
options: DownloadOptions
|
|
275
300
|
): Promise<void> {
|
|
276
|
-
const { format, size, filename } = options;
|
|
301
|
+
const { format, size, filename, quality } = options;
|
|
277
302
|
|
|
278
303
|
// Validate size
|
|
279
304
|
const validation = validateSize(size.width, size.height);
|
|
@@ -298,6 +323,9 @@ export async function downloadQRCode(
|
|
|
298
323
|
downloadFile(pdfDataUrl, finalFilename);
|
|
299
324
|
} else if (format === "eps") {
|
|
300
325
|
downloadFile(await getQRAsEPSDataUrl(propsWithSize), finalFilename);
|
|
326
|
+
} else if (format === "webp") {
|
|
327
|
+
const webp = await getQRAsWebP(propsWithSize, { quality });
|
|
328
|
+
downloadFile(await blobToDataUrl(webp), finalFilename);
|
|
301
329
|
} else {
|
|
302
330
|
// Download as PNG or JPG
|
|
303
331
|
const mimeType = getMimeType(format);
|
|
@@ -320,7 +348,8 @@ export async function downloadQRCode(
|
|
|
320
348
|
export async function getQRCodeDataUrl(
|
|
321
349
|
qrProps: QRProps,
|
|
322
350
|
format: DownloadFormat,
|
|
323
|
-
size: DownloadSize
|
|
351
|
+
size: DownloadSize,
|
|
352
|
+
options: WebPOptions = {}
|
|
324
353
|
): Promise<string> {
|
|
325
354
|
// Validate size
|
|
326
355
|
const validation = validateSize(size.width, size.height);
|
|
@@ -343,6 +372,10 @@ export async function getQRCodeDataUrl(
|
|
|
343
372
|
return await getQRAsEPSDataUrl(propsWithSize);
|
|
344
373
|
}
|
|
345
374
|
|
|
375
|
+
if (format === "webp") {
|
|
376
|
+
return await blobToDataUrl(await getQRAsWebP(propsWithSize, options));
|
|
377
|
+
}
|
|
378
|
+
|
|
346
379
|
const mimeType = getMimeType(format);
|
|
347
380
|
const result = await getQRAsCanvas(propsWithSize, mimeType);
|
|
348
381
|
|
package/src/index.tsx
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
import type { QRProps } from "../types";
|
|
5
5
|
import { DEFAULT_SIZE } from "./constants";
|
|
6
6
|
import { getFrameDimensions } from "./frame";
|
|
7
|
+
import { blobToDataUrl, canvasToWebP } from "./webp";
|
|
7
8
|
|
|
8
9
|
export * from "../types";
|
|
9
10
|
export * from "./api";
|
|
@@ -126,6 +127,7 @@ export * from "./templates";
|
|
|
126
127
|
export * from "./utils";
|
|
127
128
|
/** Reads an 8-bit PNG into RGBA pixels (no DOM or sharp), or null. */
|
|
128
129
|
export { type DecodedImage, decodePng as decodePNGPixels } from "./utils/png";
|
|
130
|
+
export * from "./webp";
|
|
129
131
|
|
|
130
132
|
export async function getQRAsSVGDataUri(props: QRProps) {
|
|
131
133
|
// Import QRCodeSVG and render it to string
|
|
@@ -304,6 +306,13 @@ export async function getQRAsCanvas(
|
|
|
304
306
|
|
|
305
307
|
if (getCanvas) {
|
|
306
308
|
resolve(canvas);
|
|
309
|
+
} else if (type === "image/webp") {
|
|
310
|
+
// toDataURL would hand back a PNG where the browser can't encode
|
|
311
|
+
// WebP (Safari); canvasToWebP encodes it itself there.
|
|
312
|
+
canvasToWebP(canvas)
|
|
313
|
+
.then(blobToDataUrl)
|
|
314
|
+
.then(resolve, reject)
|
|
315
|
+
.finally(() => canvas.remove());
|
|
307
316
|
} else {
|
|
308
317
|
const dataUrl = canvas.toDataURL(type, 1.0);
|
|
309
318
|
canvas.remove();
|