qrdx 0.0.55 → 0.0.57
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 +9 -4
- package/dist/angular.js +1 -1
- package/dist/angular.mjs +1 -1
- package/dist/core.js +7 -7
- package/dist/core.js.map +1 -1
- package/dist/core.mjs +7 -7
- package/dist/core.mjs.map +1 -1
- package/dist/element.browser.mjs +6 -6
- package/dist/element.browser.mjs.map +1 -1
- 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/preact.js +1 -1
- package/dist/preact.js.map +1 -1
- package/dist/preact.mjs +1 -1
- package/dist/preact.mjs.map +1 -1
- package/dist/solid.js +1 -1
- package/dist/solid.js.map +1 -1
- package/dist/solid.mjs +1 -1
- package/dist/solid.mjs.map +1 -1
- package/dist/src/angular.d.mts +1 -0
- package/dist/src/angular.d.ts +2 -2
- package/dist/src/bmp.d.ts +7 -0
- package/dist/src/bmp.d.ts.map +1 -0
- package/dist/src/core.d.mts +1 -0
- package/dist/src/download.d.ts +12 -5
- package/dist/src/download.d.ts.map +1 -1
- package/dist/src/element-define.d.mts +1 -0
- package/dist/src/element.d.mts +1 -0
- package/dist/src/element.d.ts +1 -1
- package/dist/src/element.d.ts.map +1 -1
- package/dist/src/engine.d.ts +2 -1
- package/dist/src/engine.d.ts.map +1 -1
- package/dist/src/export.d.ts.map +1 -1
- package/dist/src/index.d.mts +1 -0
- package/dist/src/index.d.ts +1 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/node.d.mts +1 -0
- package/dist/src/preact.d.mts +1 -0
- package/dist/src/preact.d.ts +5 -4
- package/dist/src/preact.d.ts.map +1 -1
- package/dist/src/qr-code-props.d.ts +19 -6
- package/dist/src/qr-code-props.d.ts.map +1 -1
- package/dist/src/qr-code.d.ts +7 -2
- package/dist/src/qr-code.d.ts.map +1 -1
- package/dist/src/scan-score.d.ts.map +1 -1
- package/dist/src/solid.d.mts +1 -0
- package/dist/src/solid.d.ts +5 -4
- package/dist/src/solid.d.ts.map +1 -1
- package/dist/src/utils/bmp.d.ts +18 -0
- package/dist/src/utils/bmp.d.ts.map +1 -0
- package/dist/src/vanilla-api.d.ts +2 -2
- package/dist/src/vanilla-api.d.ts.map +1 -1
- package/dist/src/vanilla.d.mts +1 -0
- package/dist/src/vue.d.mts +1 -0
- package/dist/src/vue.d.ts +2 -2
- package/dist/src/vue.d.ts.map +1 -1
- package/dist/types/public.d.mts +1 -0
- package/dist/types/public.d.ts +1 -1
- package/dist/types/public.d.ts.map +1 -1
- package/dist/types/react.d.ts +6 -0
- package/dist/types/react.d.ts.map +1 -1
- package/dist/vanilla.browser.mjs +27 -27
- package/dist/vanilla.browser.mjs.map +1 -1
- package/package.json +49 -15
- package/src/angular.ts +2 -2
- package/src/bmp.ts +25 -0
- package/src/download.tsx +40 -5
- package/src/element.ts +1 -1
- package/src/engine.ts +2 -0
- package/src/export.ts +6 -0
- package/src/index.tsx +5 -1
- package/src/preact.ts +5 -4
- package/src/qr-code-props.ts +77 -6
- package/src/qr-code.tsx +8 -6
- package/src/scan-score.ts +40 -32
- package/src/solid.ts +5 -4
- package/src/utils/bmp.ts +126 -0
- package/src/vanilla-api.ts +2 -2
- package/src/vue.ts +2 -2
- package/types/public.ts +1 -1
- package/types/react.ts +6 -0
package/src/solid.ts
CHANGED
|
@@ -13,14 +13,14 @@ import { createMemo, type JSX, untrack } from "solid-js";
|
|
|
13
13
|
import * as web from "solid-js/web";
|
|
14
14
|
import {
|
|
15
15
|
createQRCodeTree,
|
|
16
|
-
type
|
|
16
|
+
type QRCodeOptions,
|
|
17
17
|
type QRSVGOptions,
|
|
18
18
|
renderToDOM,
|
|
19
19
|
renderToString,
|
|
20
20
|
resolveQRCodeProps,
|
|
21
21
|
} from "./core";
|
|
22
22
|
|
|
23
|
-
export type { QRCodeProps, QRSVGOptions } from "./core";
|
|
23
|
+
export type { QRCodeOptions, QRCodeProps, QRSVGOptions } from "./core";
|
|
24
24
|
|
|
25
25
|
const ROOT_TAG = /^<[^\s/>]+/;
|
|
26
26
|
|
|
@@ -65,11 +65,12 @@ export function QRCodeSVG(props: QRSVGOptions): JSX.Element {
|
|
|
65
65
|
|
|
66
66
|
/**
|
|
67
67
|
* `QRCodeSVG` with React's `QRCode` logo API: `logo`, `logoOptions`,
|
|
68
|
-
* `hideLogo` and `scale`.
|
|
68
|
+
* `hideLogo` and `scale`. Any other prop (`class`, `aria-label`, `data-*`…)
|
|
69
|
+
* goes on the `<svg>`, as with `QRCodeSVG`.
|
|
69
70
|
*
|
|
70
71
|
* @example
|
|
71
72
|
* <QRCode value="https://qrdx.dev" logo="/logo.png" level="H" />
|
|
72
73
|
*/
|
|
73
|
-
export function QRCode(props:
|
|
74
|
+
export function QRCode(props: QRCodeOptions): JSX.Element {
|
|
74
75
|
return draw(() => resolveQRCodeProps({ ...props }));
|
|
75
76
|
}
|
package/src/utils/bmp.ts
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/** biome-ignore-all lint/style/noMagicNumbers: BMP format constants */
|
|
2
|
+
/** biome-ignore-all lint/suspicious/noBitwiseOperators: row padding and little-endian fields */
|
|
3
|
+
// A BMP writer in plain TypeScript with no DOM. Canvases can't encode BMP
|
|
4
|
+
// (toBlob("image/bmp") hands back a PNG), so every browser, and the server,
|
|
5
|
+
// uses this one.
|
|
6
|
+
//
|
|
7
|
+
// The file is an uncompressed 24-bit bitmap: a 14-byte file header, a 40-byte
|
|
8
|
+
// BITMAPINFOHEADER, then the pixel rows bottom-up in blue-green-red order,
|
|
9
|
+
// each row padded to a multiple of 4 bytes. 24-bit has no alpha channel, so
|
|
10
|
+
// a transparent or see-through pixel is laid over white (the legacy readers
|
|
11
|
+
// BMP is for don't agree on 32-bit alpha).
|
|
12
|
+
|
|
13
|
+
/** Largest width or height written. */
|
|
14
|
+
export const BMP_MAX_DIMENSION = 16_384;
|
|
15
|
+
|
|
16
|
+
const FILE_HEADER_SIZE = 14;
|
|
17
|
+
const INFO_HEADER_SIZE = 40;
|
|
18
|
+
const HEADER_SIZE = FILE_HEADER_SIZE + INFO_HEADER_SIZE;
|
|
19
|
+
const BYTES_PER_PIXEL = 3;
|
|
20
|
+
/** 72 pixels per inch, in pixels per metre, as most writers record it. */
|
|
21
|
+
const PIXELS_PER_METRE = 2835;
|
|
22
|
+
const OPAQUE = 255;
|
|
23
|
+
|
|
24
|
+
/** Bytes in one stored row: the pixels, padded to a multiple of 4. */
|
|
25
|
+
const rowStride = (width: number): number => (width * BYTES_PER_PIXEL + 3) & ~3;
|
|
26
|
+
|
|
27
|
+
/** A colour channel laid over white at `alpha` (0 to 255). */
|
|
28
|
+
const overWhite = (channel: number, alpha: number): number =>
|
|
29
|
+
Math.floor((channel * alpha + OPAQUE * (OPAQUE - alpha) + 127) / OPAQUE);
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* RGBA pixels (as a canvas's getImageData returns them: `width * height * 4`
|
|
33
|
+
* bytes, top row first) as a 24-bit BMP file. Throws a RangeError for a size
|
|
34
|
+
* that is not a whole number from 1 to BMP_MAX_DIMENSION, or pixels that
|
|
35
|
+
* don't match it.
|
|
36
|
+
*/
|
|
37
|
+
export function encodeBMP(
|
|
38
|
+
rgba: ArrayLike<number>,
|
|
39
|
+
width: number,
|
|
40
|
+
height: number
|
|
41
|
+
): Uint8Array {
|
|
42
|
+
if (
|
|
43
|
+
!(Number.isInteger(width) && Number.isInteger(height)) ||
|
|
44
|
+
width < 1 ||
|
|
45
|
+
height < 1 ||
|
|
46
|
+
width > BMP_MAX_DIMENSION ||
|
|
47
|
+
height > BMP_MAX_DIMENSION
|
|
48
|
+
) {
|
|
49
|
+
throw new RangeError(
|
|
50
|
+
`BMP width and height must be whole numbers from 1 to ${BMP_MAX_DIMENSION}, got ${width} x ${height}`
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
if (rgba.length !== width * height * 4) {
|
|
54
|
+
throw new RangeError(
|
|
55
|
+
`Expected ${width * height * 4} RGBA bytes for ${width} x ${height}, got ${rgba.length}`
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const stride = rowStride(width);
|
|
60
|
+
const imageSize = stride * height;
|
|
61
|
+
const file = new Uint8Array(HEADER_SIZE + imageSize);
|
|
62
|
+
const view = new DataView(file.buffer);
|
|
63
|
+
|
|
64
|
+
file[0] = 0x42; // "B"
|
|
65
|
+
file[1] = 0x4d; // "M"
|
|
66
|
+
view.setUint32(2, file.length, true);
|
|
67
|
+
view.setUint32(10, HEADER_SIZE, true); // where the pixels start
|
|
68
|
+
view.setUint32(14, INFO_HEADER_SIZE, true);
|
|
69
|
+
view.setInt32(18, width, true);
|
|
70
|
+
view.setInt32(22, height, true); // positive: rows run bottom to top
|
|
71
|
+
view.setUint16(26, 1, true); // colour planes
|
|
72
|
+
view.setUint16(28, 24, true); // bits per pixel
|
|
73
|
+
view.setUint32(30, 0, true); // BI_RGB: no compression
|
|
74
|
+
view.setUint32(34, imageSize, true);
|
|
75
|
+
view.setInt32(38, PIXELS_PER_METRE, true);
|
|
76
|
+
view.setInt32(42, PIXELS_PER_METRE, true);
|
|
77
|
+
// The colour table counts (offsets 46 and 50) stay 0: all colours, no table.
|
|
78
|
+
|
|
79
|
+
for (let y = 0; y < height; y++) {
|
|
80
|
+
let source = y * width * 4;
|
|
81
|
+
let target = HEADER_SIZE + (height - 1 - y) * stride;
|
|
82
|
+
for (let x = 0; x < width; x++) {
|
|
83
|
+
const alpha = rgba[source + 3] as number;
|
|
84
|
+
const red = rgba[source] as number;
|
|
85
|
+
const green = rgba[source + 1] as number;
|
|
86
|
+
const blue = rgba[source + 2] as number;
|
|
87
|
+
if (alpha === OPAQUE) {
|
|
88
|
+
file[target] = blue;
|
|
89
|
+
file[target + 1] = green;
|
|
90
|
+
file[target + 2] = red;
|
|
91
|
+
} else {
|
|
92
|
+
file[target] = overWhite(blue, alpha);
|
|
93
|
+
file[target + 1] = overWhite(green, alpha);
|
|
94
|
+
file[target + 2] = overWhite(red, alpha);
|
|
95
|
+
}
|
|
96
|
+
source += 4;
|
|
97
|
+
target += BYTES_PER_PIXEL;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return file;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* True when `bytes` is a BMP this writer would make: the "BM" signature, a
|
|
105
|
+
* file size that matches, the 40-byte header, one plane and 24 bits. Used to
|
|
106
|
+
* check a file before it is saved, so nothing else is handed out as BMP.
|
|
107
|
+
*/
|
|
108
|
+
export function isBMPFile(bytes: Uint8Array): boolean {
|
|
109
|
+
if (bytes.length < HEADER_SIZE || bytes[0] !== 0x42 || bytes[1] !== 0x4d) {
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
113
|
+
const width = view.getInt32(18, true);
|
|
114
|
+
const height = view.getInt32(22, true);
|
|
115
|
+
return (
|
|
116
|
+
view.getUint32(2, true) === bytes.length &&
|
|
117
|
+
view.getUint32(10, true) === HEADER_SIZE &&
|
|
118
|
+
view.getUint32(14, true) === INFO_HEADER_SIZE &&
|
|
119
|
+
width > 0 &&
|
|
120
|
+
height > 0 &&
|
|
121
|
+
view.getUint16(26, true) === 1 &&
|
|
122
|
+
view.getUint16(28, true) === 24 &&
|
|
123
|
+
view.getUint32(30, true) === 0 &&
|
|
124
|
+
bytes.length === HEADER_SIZE + rowStride(width) * height
|
|
125
|
+
);
|
|
126
|
+
}
|
package/src/vanilla-api.ts
CHANGED
|
@@ -21,7 +21,7 @@ export interface QRCodeExportOptions {
|
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
export interface QRCodeDownloadOptions extends QRCodeExportOptions {
|
|
24
|
-
/** "png" (default), "jpg", "webp", "svg", "pdf" (needs jspdf) or "eps" */
|
|
24
|
+
/** "png" (default), "jpg", "webp", "bmp", "svg", "pdf" (needs jspdf) or "eps" */
|
|
25
25
|
format?: DownloadFormat;
|
|
26
26
|
/** File name; defaults to one with the format and pixel size */
|
|
27
27
|
filename?: string;
|
|
@@ -40,7 +40,7 @@ export interface QRCodeInstance {
|
|
|
40
40
|
update(options: Partial<QRSVGOptions>): void;
|
|
41
41
|
/** The code's SVG markup, as `toSVGString` writes it */
|
|
42
42
|
toSVGString(): string;
|
|
43
|
-
/** The code as a data URL: "png" (default), "jpg", "webp", "svg", "pdf" or "eps" */
|
|
43
|
+
/** The code as a data URL: "png" (default), "jpg", "webp", "bmp", "svg", "pdf" or "eps" */
|
|
44
44
|
toDataURL(
|
|
45
45
|
format?: DownloadFormat,
|
|
46
46
|
options?: QRCodeExportOptions
|
package/src/vue.ts
CHANGED
|
@@ -168,7 +168,7 @@ export interface UseQRCodeReturn {
|
|
|
168
168
|
readonly options: ComputedRef<QRSVGOptions>;
|
|
169
169
|
/** The code's SVG markup, as `toSVGString` writes it; follows the props */
|
|
170
170
|
readonly svg: ComputedRef<string>;
|
|
171
|
-
/** The code as a data URL: "png" (default), "jpg", "webp", "svg", "pdf" or "eps" */
|
|
171
|
+
/** The code as a data URL: "png" (default), "jpg", "webp", "bmp", "svg", "pdf" or "eps" */
|
|
172
172
|
toDataURL(
|
|
173
173
|
format?: DownloadFormat,
|
|
174
174
|
options?: QRCodeExportOptions
|
|
@@ -186,7 +186,7 @@ const exportSize = (options: QRSVGOptions, size?: number) => {
|
|
|
186
186
|
* A QR code as a composable: takes `<QRCode>`'s props (a plain object, a
|
|
187
187
|
* ref, a reactive object or a getter) and returns its SVG markup, which
|
|
188
188
|
* follows the props, plus `toDataURL` and `download`. The markup works in
|
|
189
|
-
* SSR; PNG, JPG and
|
|
189
|
+
* SSR; PNG, JPG, WebP and BMP need a browser.
|
|
190
190
|
*
|
|
191
191
|
* @example
|
|
192
192
|
* const url = ref("https://qrdx.dev");
|
package/types/public.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
// The `qrdx/types` entry: every schema and type, plus the React components'
|
|
2
2
|
// prop types (types/react.ts), which the React-free barrel (./index) leaves out.
|
|
3
3
|
export * from "./index";
|
|
4
|
-
export type { QRPropsCanvas, QRPropsSVG } from "./react";
|
|
4
|
+
export type { QRCodePropsSVG, QRPropsCanvas, QRPropsSVG } from "./react";
|
package/types/react.ts
CHANGED
|
@@ -2,7 +2,13 @@
|
|
|
2
2
|
// `qrdx/types` entry, for compatibility) reach this file; the core's types
|
|
3
3
|
// never do, so they work without React's types installed.
|
|
4
4
|
import type { CanvasHTMLAttributes, SVGProps } from "react";
|
|
5
|
+
import type { QRCodeProps } from "../src/qr-code-props";
|
|
5
6
|
import type { QRProps } from "./qr";
|
|
6
7
|
|
|
7
8
|
export type QRPropsCanvas = QRProps & CanvasHTMLAttributes<HTMLCanvasElement>;
|
|
8
9
|
export type QRPropsSVG = QRProps & SVGProps<SVGSVGElement>;
|
|
10
|
+
/**
|
|
11
|
+
* React's `QRCode` props: `QRCodeProps` (the logo API included) plus the
|
|
12
|
+
* SVG attributes, which go on the `<svg>` as with `QRCodeSVG`.
|
|
13
|
+
*/
|
|
14
|
+
export type QRCodePropsSVG = QRCodeProps & SVGProps<SVGSVGElement>;
|