qrdx 0.0.56 → 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "qrdx",
3
- "version": "0.0.56",
3
+ "version": "0.0.57",
4
4
  "description": "Customizable QR code generator for React and plain JavaScript: 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",
package/src/angular.ts CHANGED
@@ -42,7 +42,7 @@ export interface QRCodeExportOptions {
42
42
  }
43
43
 
44
44
  export interface QRCodeDownloadOptions extends QRCodeExportOptions {
45
- /** "png" (default), "jpg", "webp", "svg", "pdf" (needs jspdf) or "eps" */
45
+ /** "png" (default), "jpg", "webp", "bmp", "svg", "pdf" (needs jspdf) or "eps" */
46
46
  format?: DownloadFormat;
47
47
  /** File name; defaults to one with the format and pixel size */
48
48
  filename?: string;
@@ -171,7 +171,7 @@ export class QRCode implements QRCodeInputs {
171
171
  return this.element;
172
172
  }
173
173
 
174
- /** The code as a data URL: "png" (default), "jpg", "webp", "svg", "pdf" or "eps" */
174
+ /** The code as a data URL: "png" (default), "jpg", "webp", "bmp", "svg", "pdf" or "eps" */
175
175
  toDataURL(
176
176
  format: DownloadFormat = "png",
177
177
  options: QRCodeExportOptions = {}
package/src/bmp.ts ADDED
@@ -0,0 +1,25 @@
1
+ // BMP from a canvas in any browser. No browser encodes BMP from a canvas
2
+ // (toBlob("image/bmp") returns a PNG), so the pixels are read back and written
3
+ // by the built-in encoder (utils/bmp.ts). The file is checked by its bytes
4
+ // before it is returned, so a PNG is never passed off as BMP.
5
+
6
+ import { encodeBMP, isBMPFile } from "./utils/bmp";
7
+
8
+ export { BMP_MAX_DIMENSION, encodeBMP, isBMPFile } from "./utils/bmp";
9
+
10
+ /**
11
+ * A canvas as a 24-bit BMP. A transparent background is laid over white,
12
+ * since 24-bit BMP has no alpha channel.
13
+ */
14
+ export function canvasToBMP(canvas: HTMLCanvasElement): Blob {
15
+ const context = canvas.getContext("2d");
16
+ if (!context) {
17
+ throw new Error("Could not get canvas context");
18
+ }
19
+ const { data } = context.getImageData(0, 0, canvas.width, canvas.height);
20
+ const file = encodeBMP(data, canvas.width, canvas.height);
21
+ if (!isBMPFile(file)) {
22
+ throw new Error("Could not write a BMP file");
23
+ }
24
+ return new Blob([file as BlobPart], { type: "image/bmp" });
25
+ }
package/src/download.tsx CHANGED
@@ -1,4 +1,5 @@
1
1
  import type { QRProps } from "../types";
2
+ import { canvasToBMP } from "./bmp";
2
3
  import { DEFAULT_SIZE } from "./constants";
3
4
  import { epsToDataUrl, getQRAsEPS } from "./eps";
4
5
  import { getQRAsCanvas, getQRAsSVGDataUri } from "./export";
@@ -6,10 +7,18 @@ import { getFrameDimensions } from "./frame";
6
7
  import { blobToDataUrl, canvasToWebP, type WebPOptions } from "./webp";
7
8
 
8
9
  /**
9
- * "webp" is lossless unless `quality` is set. "eps" is vector PostScript:
10
- * `size.width` becomes points (72 per inch).
10
+ * "webp" is lossless unless `quality` is set. "bmp" is an uncompressed
11
+ * 24-bit bitmap: a transparent background comes out white. "eps" is vector
12
+ * PostScript: `size.width` becomes points (72 per inch).
11
13
  */
12
- export type DownloadFormat = "png" | "jpg" | "webp" | "svg" | "pdf" | "eps";
14
+ export type DownloadFormat =
15
+ | "png"
16
+ | "jpg"
17
+ | "webp"
18
+ | "bmp"
19
+ | "svg"
20
+ | "pdf"
21
+ | "eps";
13
22
 
14
23
  export interface DownloadFormatInfo {
15
24
  mimeType: string;
@@ -22,13 +31,15 @@ export interface DownloadFormatInfo {
22
31
  * Every download format with its MIME type and file extension. Each is made
23
32
  * by one branch of downloadQRCode and getQRCodeDataUrl: PNG and JPG by the
24
33
  * 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
34
+ * encoder), BMP by canvasToBMP (the built-in encoder: no browser writes it),
35
+ * SVG from the markup, PDF with jspdf, EPS by getQRAsEPS. A new
26
36
  * format needs an entry here and a branch there.
27
37
  */
28
38
  export const DOWNLOAD_FORMATS: Record<DownloadFormat, DownloadFormatInfo> = {
29
39
  png: { mimeType: "image/png", extension: "png", vector: false },
30
40
  jpg: { mimeType: "image/jpeg", extension: "jpg", vector: false },
31
41
  webp: { mimeType: "image/webp", extension: "webp", vector: false },
42
+ bmp: { mimeType: "image/bmp", extension: "bmp", vector: false },
32
43
  svg: { mimeType: "image/svg+xml", extension: "svg", vector: true },
33
44
  // A PDF download holds the PNG, so it's named after its pixel size.
34
45
  pdf: { mimeType: "application/pdf", extension: "pdf", vector: false },
@@ -136,7 +147,7 @@ const getMimeType = (format: DownloadFormat): string =>
136
147
  (DOWNLOAD_FORMATS[format] ?? DOWNLOAD_FORMATS.png).mimeType;
137
148
 
138
149
  /**
139
- * The default file name of a download: for PNG, JPG and WebP (and the image
150
+ * The default file name of a download: for PNG, JPG, WebP and BMP (and the image
140
151
  * a PDF holds) the exact pixel size of what is saved, which a frame makes
141
152
  * bigger than the code (a 400 px code in a bottom banner is 496 × 568);
142
153
  * SVG and EPS have no pixel size and name none.
@@ -177,6 +188,23 @@ export async function getQRAsWebP(
177
188
  }
178
189
  }
179
190
 
191
+ /**
192
+ * The code as a 24-bit BMP image (plus its frame, if any). A transparent
193
+ * background comes out white. Every browser gets it from QRdx's own encoder.
194
+ */
195
+ export async function getQRAsBMP(qrProps: QRProps): Promise<Blob> {
196
+ const canvas = (await getQRAsCanvas(
197
+ qrProps,
198
+ "image/png",
199
+ true
200
+ )) as HTMLCanvasElement;
201
+ try {
202
+ return canvasToBMP(canvas);
203
+ } finally {
204
+ canvas.remove();
205
+ }
206
+ }
207
+
180
208
  // One CSS pixel = one PostScript point (72 per inch).
181
209
  const MM_PER_POINT = 25.4 / 72;
182
210
 
@@ -326,6 +354,9 @@ export async function downloadQRCode(
326
354
  } else if (format === "webp") {
327
355
  const webp = await getQRAsWebP(propsWithSize, { quality });
328
356
  downloadFile(await blobToDataUrl(webp), finalFilename);
357
+ } else if (format === "bmp") {
358
+ const bmp = await getQRAsBMP(propsWithSize);
359
+ downloadFile(await blobToDataUrl(bmp), finalFilename);
329
360
  } else {
330
361
  // Download as PNG or JPG
331
362
  const mimeType = getMimeType(format);
@@ -376,6 +407,10 @@ export async function getQRCodeDataUrl(
376
407
  return await blobToDataUrl(await getQRAsWebP(propsWithSize, options));
377
408
  }
378
409
 
410
+ if (format === "bmp") {
411
+ return await blobToDataUrl(await getQRAsBMP(propsWithSize));
412
+ }
413
+
379
414
  const mimeType = getMimeType(format);
380
415
  const result = await getQRAsCanvas(propsWithSize, mimeType);
381
416
 
package/src/element.ts CHANGED
@@ -357,7 +357,7 @@ export class QRdxCodeElement extends BaseElement {
357
357
  return renderToString(createQRCodeTree(this.#requireOptions()));
358
358
  }
359
359
 
360
- /** The code as a data URL: "png" (default), "jpg", "webp", "svg", "pdf" or "eps". */
360
+ /** The code as a data URL: "png" (default), "jpg", "webp", "bmp", "svg", "pdf" or "eps". */
361
361
  async toDataURL(
362
362
  format: DownloadFormat = "png",
363
363
  options: QRCodeExportOptions = {}
package/src/engine.ts CHANGED
@@ -73,6 +73,7 @@ export {
73
73
  type QRArtRenderOptions,
74
74
  renderQRArt,
75
75
  } from "./art";
76
+ export * from "./bmp";
76
77
  export { clampModuleScale, MODULE_SCALE_FLOOR } from "./body-patterns";
77
78
  export {
78
79
  type CodewordMap,
package/src/export.ts CHANGED
@@ -4,6 +4,7 @@
4
4
  // score and qrdx/node's PNG all start here; none of it needs React.
5
5
 
6
6
  import type { QRProps } from "../types";
7
+ import { canvasToBMP } from "./bmp";
7
8
  import { DEFAULT_SIZE } from "./constants";
8
9
  import { getFrameDimensions } from "./frame";
9
10
  import { toSVGString } from "./svg";
@@ -188,6 +189,11 @@ export async function getQRAsCanvas(
188
189
  .then(blobToDataUrl)
189
190
  .then(resolve, reject)
190
191
  .finally(() => canvas.remove());
192
+ } else if (type === "image/bmp") {
193
+ // toDataURL would hand back a PNG: no browser encodes BMP.
194
+ blobToDataUrl(canvasToBMP(canvas))
195
+ .then(resolve, reject)
196
+ .finally(() => canvas.remove());
191
197
  } else {
192
198
  const dataUrl = canvas.toDataURL(type, 1.0);
193
199
  canvas.remove();
@@ -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
+ }
@@ -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 WebP need a browser.
189
+ * SSR; PNG, JPG, WebP and BMP need a browser.
190
190
  *
191
191
  * @example
192
192
  * const url = ref("https://qrdx.dev");