UNPKG

14.8 kBTypeScriptView Raw
1/// <reference types="node" />
2
3import * as stream from "stream";
4
5export type QRCodeErrorCorrectionLevel = "low" | "medium" | "quartile" | "high" | "L" | "M" | "Q" | "H";
6export type QRCodeMaskPattern = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7;
7export type QRCodeToSJISFunc = (codePoint: string) => number;
8
9export interface QRCodeOptions {
10 /**
11 * QR Code version. If not specified the more suitable value will be calculated.
12 */
13 version?: number | undefined;
14 /**
15 * Error correction level.
16 * @default 'M'
17 */
18 errorCorrectionLevel?: QRCodeErrorCorrectionLevel | undefined;
19 /**
20 * Mask pattern used to mask the symbol.
21 *
22 * If not specified the more suitable value will be calculated.
23 */
24 maskPattern?: QRCodeMaskPattern | undefined;
25 /**
26 * Helper function used internally to convert a kanji to its Shift JIS value.
27 * Provide this function if you need support for Kanji mode.
28 */
29 toSJISFunc?: QRCodeToSJISFunc | undefined;
30}
31
32export type QRCodeDataURLType = "image/png" | "image/jpeg" | "image/webp";
33export type QRCodeToDataURLOptions = QRCodeToDataURLOptionsJpegWebp | QRCodeToDataURLOptionsOther;
34export interface QRCodeToDataURLOptionsJpegWebp extends QRCodeRenderersOptions {
35 /**
36 * Data URI format.
37 * @default 'image/png'
38 */
39 type: "image/jpeg" | "image/webp";
40 rendererOpts?:
41 | {
42 /**
43 * A number between `0` and `1` indicating image quality.
44 * @default 0.92
45 */
46 quality?: number | undefined;
47 }
48 | undefined;
49}
50export interface QRCodeToDataURLOptionsOther extends QRCodeRenderersOptions {
51 /**
52 * Data URI format.
53 * @default 'image/png'
54 */
55 type?: Exclude<QRCodeDataURLType, "image/jpeg" | "image/webp"> | undefined;
56}
57
58export type QRCodeStringType = "utf8" | "svg" | "terminal";
59export type QRCodeToStringOptions = QRCodeToStringOptionsTerminal | QRCodeToStringOptionsOther;
60export interface QRCodeToStringOptionsTerminal extends QRCodeRenderersOptions {
61 /**
62 * Output format.
63 * @default 'utf8'
64 */
65 type: "terminal";
66 /**
67 * Outputs smaller QR code.
68 * @default false
69 */
70 small?: boolean | undefined;
71}
72export interface QRCodeToStringOptionsOther extends QRCodeRenderersOptions {
73 /**
74 * Output format.
75 * @default 'utf8'
76 */
77 type?: Exclude<QRCodeStringType, "terminal"> | undefined;
78}
79
80export type QRCodeFileType = "png" | "svg" | "utf8";
81export type QRCodeToFileOptions = QRCodeToFileOptionsPng | QRCodeToFileOptionsOther;
82export interface QRCodeToFileOptionsPng extends QRCodeRenderersOptions {
83 /**
84 * Output format.
85 * @default 'png'
86 */
87 type?: "png" | undefined;
88 rendererOpts?:
89 | {
90 /**
91 * Compression level for deflate.
92 * @default 9
93 */
94 deflateLevel?: number | undefined;
95 /**
96 * Compression strategy for deflate.
97 * @default 3
98 */
99 deflateStrategy?: number | undefined;
100 }
101 | undefined;
102}
103export interface QRCodeToFileOptionsOther extends QRCodeRenderersOptions {
104 /**
105 * Output format.
106 * @default 'png'
107 */
108 type: Exclude<QRCodeFileType, "png"> | undefined;
109}
110
111export type QRCodeFileStreamType = "png";
112export interface QRCodeToFileStreamOptions extends QRCodeRenderersOptions {
113 /**
114 * Output format. Only png supported for file stream.
115 */
116 type?: QRCodeFileStreamType | undefined;
117 rendererOpts?:
118 | {
119 /**
120 * Compression level for deflate.
121 * @default 9
122 */
123 deflateLevel?: number | undefined;
124 /**
125 * Compression strategy for deflate.
126 * @default 3
127 */
128 deflateStrategy?: number | undefined;
129 }
130 | undefined;
131}
132
133export type QRCodeBufferType = "png";
134export interface QRCodeToBufferOptions extends QRCodeRenderersOptions {
135 /**
136 * Output format. Only png supported for Buffer.
137 */
138 type?: QRCodeBufferType | undefined;
139 rendererOpts?:
140 | {
141 /**
142 * Compression level for deflate.
143 * @default 9
144 */
145 deflateLevel?: number | undefined;
146 /**
147 * Compression strategy for deflate.
148 * @default 3
149 */
150 deflateStrategy?: number | undefined;
151 }
152 | undefined;
153}
154
155export interface QRCodeRenderersOptions extends QRCodeOptions {
156 /**
157 * Define how much wide the quiet zone should be.
158 * @default 4
159 */
160 margin?: number | undefined;
161 /**
162 * Scale factor. A value of `1` means 1px per modules (black dots).
163 * @default 4
164 */
165 scale?: number | undefined;
166 /**
167 * Forces a specific width for the output image.
168 * If width is too small to contain the qr symbol, this option will be ignored.
169 * Takes precedence over `scale`.
170 */
171 width?: number | undefined;
172 color?:
173 | {
174 /**
175 * Color of dark module. Value must be in hex format (RGBA).
176 * Note: dark color should always be darker than `color.light`.
177 * @default '#000000ff'
178 */
179 dark?: string | undefined;
180 /**
181 * Color of light module. Value must be in hex format (RGBA).
182 * @default '#ffffffff'
183 */
184 light?: string | undefined;
185 }
186 | undefined;
187}
188
189export type QRCodeSegmentMode = "alphanumeric" | "numeric" | "byte" | "kanji";
190export type QRCodeSegment =
191 | QRCodeNumericSegment
192 | QRCodeAlphanumericSegment
193 | QRCodeByteSegment
194 | QRCodeKanjiSegment
195 | {
196 mode?: never;
197 data: string | Buffer | Uint8ClampedArray | Uint8Array;
198 };
199
200export interface QRCodeNumericSegment {
201 mode: "numeric";
202 data: string | number;
203}
204
205export interface QRCodeAlphanumericSegment {
206 mode: "alphanumeric";
207 data: string;
208}
209
210export interface QRCodeByteSegment {
211 mode: "byte";
212 data: Buffer | Uint8ClampedArray | Uint8Array;
213}
214
215export interface QRCodeKanjiSegment {
216 mode: "kanji";
217 data: string;
218}
219
220export interface QRCode {
221 /**
222 * BitMatrix class with modules data
223 */
224 modules: BitMatrix;
225 /**
226 * Calculated QR Code version
227 */
228 version: number;
229 /**
230 * Error Correction Level
231 */
232 errorCorrectionLevel: ErrorCorrectionLevel;
233 /**
234 * Calculated Mask pattern
235 */
236 maskPattern: QRCodeMaskPattern | undefined;
237 /**
238 * Generated segments
239 */
240 segments: GeneratedQRCodeSegment[];
241}
242
243/**
244 * Helper class to handle QR Code symbol modules.
245 */
246export interface BitMatrix {
247 /**
248 * Symbol size.
249 */
250 size: number;
251 data: Uint8Array;
252 reservedBit: Uint8Array;
253 /**
254 * Set bit value at specified location
255 * If reserved flag is set, this bit will be ignored during masking process.
256 */
257 set(row: number, col: number, value: number, reserved: boolean): void;
258 /**
259 * @return Bit value at specified location.
260 */
261 get(row: number, col: number): number;
262 /**
263 * Applies xor operator at specified location (used during masking process).
264 */
265 xor(row: number, col: number, value: number): void;
266 /**
267 * Check if bit at specified location is reserved.
268 */
269 isReserved(row: number, col: number): number;
270}
271
272export interface ErrorCorrectionLevel {
273 bit: 0 | 1 | 2 | 3;
274}
275
276export type ModeId = "Numeric" | "Alphanumeric" | "Byte" | "Kanji";
277export interface Mode<TModeId extends ModeId = ModeId> {
278 id: TModeId;
279 bit: number;
280 ccBits: readonly number[];
281}
282
283export type GeneratedQRCodeSegment = NumericData | AlphanumericData | ByteData | KanjiData;
284
285export interface DataSegment {
286 getLength(): number;
287 getBitsLength(): number;
288}
289
290export interface NumericData extends DataSegment {
291 mode: Mode<"Numeric">;
292 data: string;
293}
294
295export interface AlphanumericData extends DataSegment {
296 mode: Mode<"Alphanumeric">;
297 data: string;
298}
299
300export interface ByteData extends DataSegment {
301 mode: Mode<"Byte">;
302 data: Uint8Array;
303}
304
305export interface KanjiData extends DataSegment {
306 mode: Mode<"Kanji">;
307 data: string;
308}
309
310/**
311 * Creates QR Code symbol and returns a qrcode object.
312 * @param text Text to encode or a list of objects describing segments.
313 */
314export function create(text: string | QRCodeSegment[], options?: QRCodeOptions): QRCode;
315
316/**
317 * Draws qr code symbol to canvas.
318 */
319export function toCanvas(
320 canvasElement: HTMLCanvasElement,
321 text: string | QRCodeSegment[],
322 callback: (error: Error | null | undefined) => void,
323): void;
324/**
325 * Draws qr code symbol to canvas.
326 */
327export function toCanvas(
328 canvasElement: HTMLCanvasElement,
329 text: string | QRCodeSegment[],
330 options?: QRCodeRenderersOptions,
331): Promise<void>;
332/**
333 * Draws qr code symbol to canvas.
334 */
335export function toCanvas(
336 canvasElement: HTMLCanvasElement,
337 text: string | QRCodeSegment[],
338 options: QRCodeRenderersOptions,
339 callback: (error: Error | null | undefined) => void,
340): void;
341/**
342 * Draws qr code symbol to canvas.
343 */
344export function toCanvas(
345 text: string | QRCodeSegment[],
346 callback: (error: Error | null | undefined, canvas: HTMLCanvasElement) => void,
347): void;
348/**
349 * Draws qr code symbol to canvas.
350 */
351export function toCanvas(text: string | QRCodeSegment[], options?: QRCodeRenderersOptions): Promise<HTMLCanvasElement>;
352/**
353 * Draws qr code symbol to canvas.
354 */
355export function toCanvas(
356 text: string | QRCodeSegment[],
357 options: QRCodeRenderersOptions,
358 callback: (error: Error | null | undefined, canvas: HTMLCanvasElement) => void,
359): void;
360/**
361 * Draws qr code symbol to node canvas.
362 */
363export function toCanvas(
364 canvas: any,
365 text: string | QRCodeSegment[],
366 callback: (error: Error | null | undefined) => void,
367): void;
368/**
369 * Draws qr code symbol to node canvas.
370 */
371export function toCanvas(canvas: any, text: string | QRCodeSegment[], options?: QRCodeRenderersOptions): Promise<void>;
372/**
373 * Draws qr code symbol to node canvas.
374 */
375export function toCanvas(
376 canvas: any,
377 text: string | QRCodeSegment[],
378 options: QRCodeRenderersOptions,
379 callback: (error: Error | null | undefined) => void,
380): void;
381
382/**
383 * Returns a Data URI containing a representation of the QR Code image.
384 */
385export function toDataURL(
386 canvasElement: HTMLCanvasElement,
387 text: string | QRCodeSegment[],
388 callback: (error: Error | null | undefined, url: string) => void,
389): void;
390/**
391 * Returns a Data URI containing a representation of the QR Code image.
392 */
393export function toDataURL(
394 canvasElement: HTMLCanvasElement,
395 text: string | QRCodeSegment[],
396 options?: QRCodeToDataURLOptions,
397): Promise<string>;
398/**
399 * Returns a Data URI containing a representation of the QR Code image.
400 */
401export function toDataURL(
402 canvasElement: HTMLCanvasElement,
403 text: string | QRCodeSegment[],
404 options: QRCodeToDataURLOptions,
405 callback: (error: Error | null | undefined, url: string) => void,
406): void;
407
408/**
409 * Returns a Data URI containing a representation of the QR Code image.
410 */
411export function toDataURL(
412 text: string | QRCodeSegment[],
413 callback: (error: Error | null | undefined, url: string) => void,
414): void;
415/**
416 * Returns a Data URI containing a representation of the QR Code image.
417 */
418export function toDataURL(text: string | QRCodeSegment[], options?: QRCodeToDataURLOptions): Promise<string>;
419/**
420 * Returns a Data URI containing a representation of the QR Code image.
421 */
422export function toDataURL(
423 text: string | QRCodeSegment[],
424 options: QRCodeToDataURLOptions,
425 callback: (error: Error | null | undefined, url: string) => void,
426): void;
427
428/**
429 * Returns a string representation of the QR Code.
430 */
431export function toString(
432 text: string | QRCodeSegment[],
433 callback: (error: Error | null | undefined, string: string) => void,
434): void;
435/**
436 * Returns a string representation of the QR Code.
437 */
438export function toString(text: string | QRCodeSegment[], options?: QRCodeToStringOptions): Promise<string>;
439/**
440 * Returns a string representation of the QR Code.
441 */
442export function toString(
443 text: string | QRCodeSegment[],
444 options: QRCodeToStringOptions,
445 callback: (error: Error | null | undefined, string: string) => void,
446): void;
447
448/**
449 * Saves QR Code to image file.
450 * If `options.type` is not specified, the format will be guessed from file extension.
451 */
452export function toFile(
453 path: string,
454 text: string | QRCodeSegment[],
455 callback: (error: Error | null | undefined) => void,
456): void;
457/**
458 * Saves QR Code to image file.
459 * If `options.type` is not specified, the format will be guessed from file extension.
460 */
461export function toFile(path: string, text: string | QRCodeSegment[], options?: QRCodeToFileOptions): Promise<void>;
462/**
463 * Saves QR Code to image file.
464 * If `options.type` is not specified, the format will be guessed from file extension.
465 */
466export function toFile(
467 path: string,
468 text: string | QRCodeSegment[],
469 options: QRCodeToFileOptions,
470 callback: (error: Error | null | undefined) => void,
471): void;
472
473/**
474 * Writes QR Code image to stream. Only works with png format for now.
475 */
476export function toFileStream(
477 stream: stream.Writable,
478 text: string | QRCodeSegment[],
479 callback: (error: Error | null | undefined) => void,
480): void;
481/**
482 * Writes QR Code image to stream. Only works with png format for now.
483 */
484export function toFileStream(
485 stream: stream.Writable,
486 text: string | QRCodeSegment[],
487 options?: QRCodeToFileStreamOptions,
488): Promise<void>;
489/**
490 * Writes QR Code image to stream. Only works with png format for now.
491 */
492export function toFileStream(
493 stream: stream.Writable,
494 text: string | QRCodeSegment[],
495 options: QRCodeToFileStreamOptions,
496 callback: (error: Error | null | undefined) => void,
497): void;
498
499/**
500 * Returns a Buffer containing a representation of the QR Code image. Only works with png format.
501 */
502export function toBuffer(
503 text: string | QRCodeSegment[],
504 callback: (error: Error | null | undefined, buffer: Buffer) => void,
505): void;
506/**
507 * Returns a Buffer containing a representation of the QR Code image. Only works with png format.
508 */
509export function toBuffer(text: string | QRCodeSegment[], options?: QRCodeToBufferOptions): Promise<Buffer>;
510/**
511 * Returns a Buffer containing a representation of the QR Code image. Only works with png format.
512 */
513export function toBuffer(
514 text: string | QRCodeSegment[],
515 options: QRCodeToBufferOptions,
516 callback: (error: Error | null | undefined, buffer: Buffer) => void,
517): void;