qr 0.2.4 → 0.4.0

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/index.js ADDED
@@ -0,0 +1,1099 @@
1
+ "use strict";
2
+ /*!
3
+ Copyright (c) 2023 Paul Miller (paulmillr.com)
4
+ The library paulmillr-qr is dual-licensed under the Apache 2.0 OR MIT license.
5
+ You can select a license of your choice.
6
+ Licensed under the Apache License, Version 2.0 (the "License");
7
+ you may not use this file except in compliance with the License.
8
+ You may obtain a copy of the License at
9
+
10
+ http://www.apache.org/licenses/LICENSE-2.0
11
+
12
+ Unless required by applicable law or agreed to in writing, software
13
+ distributed under the License is distributed on an "AS IS" BASIS,
14
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ See the License for the specific language governing permissions and
16
+ limitations under the License.
17
+ */
18
+ Object.defineProperty(exports, "__esModule", { value: true });
19
+ exports._tests = exports.utils = exports.Encoding = exports.ECMode = exports.Bitmap = void 0;
20
+ exports.utf8ToBytes = utf8ToBytes;
21
+ exports.encodeQR = encodeQR;
22
+ /**
23
+ * Methods for encoding (generating) QR code patterns.
24
+ * Check out decode.ts for decoding (reading).
25
+ * @module
26
+ * @example
27
+ ```js
28
+ import encodeQR from '@paulmillr/qr';
29
+ const txt = 'Hello world';
30
+ const ascii = encodeQR(txt, 'ascii'); // Not all fonts are supported
31
+ const terminalFriendly = encodeQR(txt, 'term'); // 2x larger, all fonts are OK
32
+ const gifBytes = encodeQR(txt, 'gif'); // Uncompressed GIF
33
+ const svgElement = encodeQR(txt, 'svg'); // SVG vector image element
34
+ const array = encodeQR(txt, 'raw'); // 2d array for canvas or other libs
35
+ // import decodeQR from '@paulmillr/qr/decode';
36
+ ```
37
+ */
38
+ // We do not use newline escape code directly in strings because it's not parser-friendly
39
+ const chCodes = { newline: 10, reset: 27 };
40
+ function assertNumber(n) {
41
+ if (!Number.isSafeInteger(n))
42
+ throw new Error(`integer expected: ${n}`);
43
+ }
44
+ function validateVersion(ver) {
45
+ if (!Number.isSafeInteger(ver) || ver < 1 || ver > 40)
46
+ throw new Error(`Invalid version=${ver}. Expected number [1..40]`);
47
+ }
48
+ function bin(dec, pad) {
49
+ return dec.toString(2).padStart(pad, '0');
50
+ }
51
+ function mod(a, b) {
52
+ const result = a % b;
53
+ return result >= 0 ? result : b + result;
54
+ }
55
+ function fillArr(length, val) {
56
+ return new Array(length).fill(val);
57
+ }
58
+ /**
59
+ * Interleaves byte blocks.
60
+ * @param blocks [[1, 2, 3], [4, 5, 6]]
61
+ * @returns [1, 4, 2, 5, 3, 6]
62
+ */
63
+ function interleaveBytes(...blocks) {
64
+ let len = 0;
65
+ for (const b of blocks)
66
+ len = Math.max(len, b.length);
67
+ const res = [];
68
+ for (let i = 0; i < len; i++) {
69
+ for (const b of blocks) {
70
+ if (i >= b.length)
71
+ continue; // outside of block, skip
72
+ res.push(b[i]);
73
+ }
74
+ }
75
+ return new Uint8Array(res);
76
+ }
77
+ function includesAt(lst, pattern, index) {
78
+ if (index < 0 || index + pattern.length > lst.length)
79
+ return false;
80
+ for (let i = 0; i < pattern.length; i++)
81
+ if (pattern[i] !== lst[index + i])
82
+ return false;
83
+ return true;
84
+ }
85
+ // Optimize for minimal score/penalty
86
+ function best() {
87
+ let best;
88
+ let bestScore = Infinity;
89
+ return {
90
+ add(score, value) {
91
+ if (score >= bestScore)
92
+ return;
93
+ best = value;
94
+ bestScore = score;
95
+ },
96
+ get: () => best,
97
+ score: () => bestScore,
98
+ };
99
+ }
100
+ // Based on https://github.com/paulmillr/scure-base/blob/main/index.ts
101
+ function alphabet(alphabet) {
102
+ return {
103
+ has: (char) => alphabet.includes(char),
104
+ decode: (input) => {
105
+ if (!Array.isArray(input) || (input.length && typeof input[0] !== 'string'))
106
+ throw new Error('alphabet.decode input should be array of strings');
107
+ return input.map((letter) => {
108
+ if (typeof letter !== 'string')
109
+ throw new Error(`alphabet.decode: not string element=${letter}`);
110
+ const index = alphabet.indexOf(letter);
111
+ if (index === -1)
112
+ throw new Error(`Unknown letter: "${letter}". Allowed: ${alphabet}`);
113
+ return index;
114
+ });
115
+ },
116
+ encode: (digits) => {
117
+ if (!Array.isArray(digits) || (digits.length && typeof digits[0] !== 'number'))
118
+ throw new Error('alphabet.encode input should be an array of numbers');
119
+ return digits.map((i) => {
120
+ assertNumber(i);
121
+ if (i < 0 || i >= alphabet.length)
122
+ throw new Error(`Digit index outside alphabet: ${i} (alphabet: ${alphabet.length})`);
123
+ return alphabet[i];
124
+ });
125
+ },
126
+ };
127
+ }
128
+ class Bitmap {
129
+ static size(size, limit) {
130
+ if (typeof size === 'number')
131
+ size = { height: size, width: size };
132
+ if (!Number.isSafeInteger(size.height) && size.height !== Infinity)
133
+ throw new Error(`Bitmap: invalid height=${size.height} (${typeof size.height})`);
134
+ if (!Number.isSafeInteger(size.width) && size.width !== Infinity)
135
+ throw new Error(`Bitmap: invalid width=${size.width} (${typeof size.width})`);
136
+ if (limit !== undefined) {
137
+ // Clamp length, so it won't overflow, also allows to use Infinity, so we draw until end
138
+ size = {
139
+ width: Math.min(size.width, limit.width),
140
+ height: Math.min(size.height, limit.height),
141
+ };
142
+ }
143
+ return size;
144
+ }
145
+ static fromString(s) {
146
+ // Remove linebreaks on start and end, so we draw in `` section
147
+ s = s.replace(/^\n+/g, '').replace(/\n+$/g, '');
148
+ const lines = s.split(String.fromCharCode(chCodes.newline));
149
+ const height = lines.length;
150
+ const data = new Array(height);
151
+ let width;
152
+ for (const line of lines) {
153
+ const row = line.split('').map((i) => {
154
+ if (i === 'X')
155
+ return true;
156
+ if (i === ' ')
157
+ return false;
158
+ if (i === '?')
159
+ return undefined;
160
+ throw new Error(`Bitmap.fromString: unknown symbol=${i}`);
161
+ });
162
+ if (width && row.length !== width)
163
+ throw new Error(`Bitmap.fromString different row sizes: width=${width} cur=${row.length}`);
164
+ width = row.length;
165
+ data.push(row);
166
+ }
167
+ if (!width)
168
+ width = 0;
169
+ return new Bitmap({ height, width }, data);
170
+ }
171
+ constructor(size, data) {
172
+ const { height, width } = Bitmap.size(size);
173
+ this.data = data || Array.from({ length: height }, () => fillArr(width, undefined));
174
+ this.height = height;
175
+ this.width = width;
176
+ }
177
+ point(p) {
178
+ return this.data[p.y][p.x];
179
+ }
180
+ isInside(p) {
181
+ return 0 <= p.x && p.x < this.width && 0 <= p.y && p.y < this.height;
182
+ }
183
+ size(offset) {
184
+ if (!offset)
185
+ return { height: this.height, width: this.width };
186
+ const { x, y } = this.xy(offset);
187
+ return { height: this.height - y, width: this.width - x };
188
+ }
189
+ xy(c) {
190
+ if (typeof c === 'number')
191
+ c = { x: c, y: c };
192
+ if (!Number.isSafeInteger(c.x))
193
+ throw new Error(`Bitmap: invalid x=${c.x}`);
194
+ if (!Number.isSafeInteger(c.y))
195
+ throw new Error(`Bitmap: invalid y=${c.y}`);
196
+ // Do modulo, so we can use negative positions
197
+ c.x = mod(c.x, this.width);
198
+ c.y = mod(c.y, this.height);
199
+ return c;
200
+ }
201
+ // Basically every operation can be represented as rect
202
+ rect(c, size, value) {
203
+ const { x, y } = this.xy(c);
204
+ const { height, width } = Bitmap.size(size, this.size({ x, y }));
205
+ for (let yPos = 0; yPos < height; yPos++) {
206
+ for (let xPos = 0; xPos < width; xPos++) {
207
+ // NOTE: we use give function relative coordinates inside box
208
+ this.data[y + yPos][x + xPos] =
209
+ typeof value === 'function'
210
+ ? value({ x: xPos, y: yPos }, this.data[y + yPos][x + xPos])
211
+ : value;
212
+ }
213
+ }
214
+ return this;
215
+ }
216
+ // returns rectangular part of bitmap
217
+ rectRead(c, size, fn) {
218
+ return this.rect(c, size, (c, cur) => {
219
+ fn(c, cur);
220
+ return cur;
221
+ });
222
+ }
223
+ // Horizontal & vertical lines
224
+ hLine(c, len, value) {
225
+ return this.rect(c, { width: len, height: 1 }, value);
226
+ }
227
+ vLine(c, len, value) {
228
+ return this.rect(c, { width: 1, height: len }, value);
229
+ }
230
+ // add border
231
+ border(border = 2, value) {
232
+ const height = this.height + 2 * border;
233
+ const width = this.width + 2 * border;
234
+ const v = fillArr(border, value);
235
+ const h = Array.from({ length: border }, () => fillArr(width, value));
236
+ return new Bitmap({ height, width }, [...h, ...this.data.map((i) => [...v, ...i, ...v]), ...h]);
237
+ }
238
+ // Embed another bitmap on coordinates
239
+ embed(c, bm) {
240
+ return this.rect(c, bm.size(), ({ x, y }) => bm.data[y][x]);
241
+ }
242
+ // returns rectangular part of bitmap
243
+ rectSlice(c, size = this.size()) {
244
+ const rect = new Bitmap(Bitmap.size(size, this.size(this.xy(c))));
245
+ this.rect(c, size, ({ x, y }, cur) => (rect.data[y][x] = cur));
246
+ return rect;
247
+ }
248
+ // Change shape, replace rows with columns (data[y][x] -> data[x][y])
249
+ inverse() {
250
+ const { height, width } = this;
251
+ const res = new Bitmap({ height: width, width: height });
252
+ return res.rect({ x: 0, y: 0 }, Infinity, ({ x, y }) => this.data[x][y]);
253
+ }
254
+ // Each pixel size is multiplied by factor
255
+ scale(factor) {
256
+ if (!Number.isSafeInteger(factor) || factor > 1024)
257
+ throw new Error(`invalid scale factor: ${factor}`);
258
+ const { height, width } = this;
259
+ const res = new Bitmap({ height: factor * height, width: factor * width });
260
+ return res.rect({ x: 0, y: 0 }, Infinity, ({ x, y }) => this.data[Math.floor(y / factor)][Math.floor(x / factor)]);
261
+ }
262
+ clone() {
263
+ const res = new Bitmap(this.size());
264
+ return res.rect({ x: 0, y: 0 }, this.size(), ({ x, y }) => this.data[y][x]);
265
+ }
266
+ // Ensure that there is no undefined values left
267
+ assertDrawn() {
268
+ this.rectRead(0, Infinity, (_, cur) => {
269
+ if (typeof cur !== 'boolean')
270
+ throw new Error(`Invalid color type=${typeof cur}`);
271
+ });
272
+ }
273
+ // Simple string representation for debugging
274
+ toString() {
275
+ return this.data
276
+ .map((i) => i.map((j) => (j === undefined ? '?' : j ? 'X' : ' ')).join(''))
277
+ .join(String.fromCharCode(chCodes.newline));
278
+ }
279
+ toASCII() {
280
+ const { height, width, data } = this;
281
+ let out = '';
282
+ // Terminal character height is x2 of character width, so we process two rows of bitmap
283
+ // to produce one row of ASCII
284
+ for (let y = 0; y < height; y += 2) {
285
+ for (let x = 0; x < width; x++) {
286
+ const first = data[y][x];
287
+ const second = y + 1 >= height ? true : data[y + 1][x]; // if last row outside bitmap, make it black
288
+ if (!first && !second)
289
+ out += '█'; // both rows white (empty)
290
+ else if (!first && second)
291
+ out += '▀'; // top row white
292
+ else if (first && !second)
293
+ out += '▄'; // down row white
294
+ else if (first && second)
295
+ out += ' '; // both rows black
296
+ }
297
+ out += String.fromCharCode(chCodes.newline);
298
+ }
299
+ return out;
300
+ }
301
+ toTerm() {
302
+ const cc = String.fromCharCode(chCodes.reset);
303
+ const reset = cc + '[0m';
304
+ const whiteBG = cc + '[1;47m ' + reset;
305
+ const darkBG = cc + `[40m ` + reset;
306
+ return this.data
307
+ .map((i) => i.map((j) => (j ? darkBG : whiteBG)).join(''))
308
+ .join(String.fromCharCode(chCodes.newline));
309
+ }
310
+ toSVG() {
311
+ let out = `<svg xmlns:svg="http://www.w3.org/2000/svg" viewBox="0 0 ${this.width} ${this.height}" version="1.1" xmlns="http://www.w3.org/2000/svg">`;
312
+ this.rectRead(0, Infinity, ({ x, y }, val) => {
313
+ if (val)
314
+ out += `<rect x="${x}" y="${y}" width="1" height="1" />`;
315
+ });
316
+ out += '</svg>';
317
+ return out;
318
+ }
319
+ toGIF() {
320
+ // NOTE: Small, but inefficient implementation.
321
+ // Uses 1 byte per pixel, but still less bloated than SVG.
322
+ const u16le = (i) => [i & 0xff, (i >>> 8) & 0xff];
323
+ const dims = [...u16le(this.width), ...u16le(this.height)];
324
+ const data = [];
325
+ this.rectRead(0, Infinity, (_, cur) => data.push(+(cur === true)));
326
+ const N = 126; // Block size
327
+ // prettier-ignore
328
+ const bytes = [
329
+ 0x47, 0x49, 0x46, 0x38, 0x37, 0x61, ...dims, 0xf6, 0x00, 0x00, 0xff, 0xff, 0xff,
330
+ ...fillArr(3 * 127, 0x00), 0x2c, 0x00, 0x00, 0x00, 0x00, ...dims, 0x00, 0x07
331
+ ];
332
+ const fullChunks = Math.floor(data.length / N);
333
+ // Full blocks
334
+ for (let i = 0; i < fullChunks; i++)
335
+ bytes.push(N + 1, 0x80, ...data.slice(N * i, N * (i + 1)).map((i) => +i));
336
+ // Remaining bytes
337
+ bytes.push((data.length % N) + 1, 0x80, ...data.slice(fullChunks * N).map((i) => +i));
338
+ bytes.push(0x01, 0x81, 0x00, 0x3b);
339
+ return new Uint8Array(bytes);
340
+ }
341
+ toImage(isRGB = false) {
342
+ const { height, width } = this.size();
343
+ const data = new Uint8Array(height * width * (isRGB ? 3 : 4));
344
+ let i = 0;
345
+ for (let y = 0; y < height; y++) {
346
+ for (let x = 0; x < width; x++) {
347
+ const value = !!this.data[y][x] ? 0 : 255;
348
+ data[i++] = value;
349
+ data[i++] = value;
350
+ data[i++] = value;
351
+ if (!isRGB)
352
+ data[i++] = 255; // alpha channel
353
+ }
354
+ }
355
+ return { height, width, data };
356
+ }
357
+ }
358
+ exports.Bitmap = Bitmap;
359
+ // End of utils
360
+ // Runtime type-checking
361
+ /** Error correction mode. low: 7%, medium: 15%, quartile: 25%, high: 30% */
362
+ exports.ECMode = ['low', 'medium', 'quartile', 'high'];
363
+ /** QR Code encoding */
364
+ exports.Encoding = ['numeric', 'alphanumeric', 'byte', 'kanji', 'eci'];
365
+ // Various constants & tables
366
+ // prettier-ignore
367
+ const BYTES = [
368
+ // 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
369
+ 26, 44, 70, 100, 134, 172, 196, 242, 292, 346, 404, 466, 532, 581, 655, 733, 815, 901, 991, 1085,
370
+ // 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40
371
+ 1156, 1258, 1364, 1474, 1588, 1706, 1828, 1921, 2051, 2185, 2323, 2465, 2611, 2761, 2876, 3034, 3196, 3362, 3532, 3706,
372
+ ];
373
+ // prettier-ignore
374
+ const WORDS_PER_BLOCK = {
375
+ // Version 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40
376
+ low: [7, 10, 15, 20, 26, 18, 20, 24, 30, 18, 20, 24, 26, 30, 22, 24, 28, 30, 28, 28, 28, 28, 30, 30, 26, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30],
377
+ medium: [10, 16, 26, 18, 24, 16, 18, 22, 22, 26, 30, 22, 22, 24, 24, 28, 28, 26, 26, 26, 26, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28],
378
+ quartile: [13, 22, 18, 26, 18, 24, 18, 22, 20, 24, 28, 26, 24, 20, 30, 24, 28, 28, 26, 30, 28, 30, 30, 30, 30, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30],
379
+ high: [17, 28, 22, 16, 22, 28, 26, 26, 24, 28, 24, 28, 22, 24, 24, 30, 28, 28, 26, 28, 30, 24, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30],
380
+ };
381
+ // prettier-ignore
382
+ const ECC_BLOCKS = {
383
+ // Version 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40
384
+ low: [1, 1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 4, 4, 6, 6, 6, 6, 7, 8, 8, 9, 9, 10, 12, 12, 12, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 24, 25],
385
+ medium: [1, 1, 1, 2, 2, 4, 4, 4, 5, 5, 5, 8, 9, 9, 10, 10, 11, 13, 14, 16, 17, 17, 18, 20, 21, 23, 25, 26, 28, 29, 31, 33, 35, 37, 38, 40, 43, 45, 47, 49],
386
+ quartile: [1, 1, 2, 2, 4, 4, 6, 6, 8, 8, 8, 10, 12, 16, 12, 17, 16, 18, 21, 20, 23, 23, 25, 27, 29, 34, 34, 35, 38, 40, 43, 45, 48, 51, 53, 56, 59, 62, 65, 68],
387
+ high: [1, 1, 2, 4, 4, 4, 5, 6, 8, 8, 11, 11, 16, 16, 18, 16, 19, 21, 25, 25, 25, 34, 30, 32, 35, 37, 40, 42, 45, 48, 51, 54, 57, 60, 63, 66, 70, 74, 77, 81],
388
+ };
389
+ const info = {
390
+ size: {
391
+ encode: (ver) => 21 + 4 * (ver - 1), // ver1 = 21, ver40=177 blocks
392
+ decode: (size) => (size - 17) / 4,
393
+ },
394
+ sizeType: (ver) => Math.floor((ver + 7) / 17),
395
+ // Based on https://codereview.stackexchange.com/questions/74925/algorithm-to-generate-this-alignment-pattern-locations-table-for-qr-codes
396
+ alignmentPatterns(ver) {
397
+ if (ver === 1)
398
+ return [];
399
+ const first = 6;
400
+ const last = info.size.encode(ver) - first - 1;
401
+ const distance = last - first;
402
+ const count = Math.ceil(distance / 28);
403
+ let interval = Math.floor(distance / count);
404
+ if (interval % 2)
405
+ interval += 1;
406
+ else if ((distance % count) * 2 >= count)
407
+ interval += 2;
408
+ const res = [first];
409
+ for (let m = 1; m < count; m++)
410
+ res.push(last - (count - m) * interval);
411
+ res.push(last);
412
+ return res;
413
+ },
414
+ ECCode: {
415
+ low: 0b01,
416
+ medium: 0b00,
417
+ quartile: 0b11,
418
+ high: 0b10,
419
+ },
420
+ formatMask: 0b101010000010010,
421
+ formatBits(ecc, maskIdx) {
422
+ const data = (info.ECCode[ecc] << 3) | maskIdx;
423
+ let d = data;
424
+ for (let i = 0; i < 10; i++)
425
+ d = (d << 1) ^ ((d >> 9) * 0b10100110111);
426
+ return ((data << 10) | d) ^ info.formatMask;
427
+ },
428
+ versionBits(ver) {
429
+ let d = ver;
430
+ for (let i = 0; i < 12; i++)
431
+ d = (d << 1) ^ ((d >> 11) * 0b1111100100101);
432
+ return (ver << 12) | d;
433
+ },
434
+ alphabet: {
435
+ numeric: alphabet('0123456789'),
436
+ alphanumerc: alphabet('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:'),
437
+ }, // as Record<EncodingType, ReturnType<typeof alphabet>>,
438
+ lengthBits(ver, type) {
439
+ const table = {
440
+ numeric: [10, 12, 14],
441
+ alphanumeric: [9, 11, 13],
442
+ byte: [8, 16, 16],
443
+ kanji: [8, 10, 12],
444
+ eci: [0, 0, 0],
445
+ };
446
+ return table[type][info.sizeType(ver)];
447
+ },
448
+ modeBits: {
449
+ numeric: '0001',
450
+ alphanumeric: '0010',
451
+ byte: '0100',
452
+ kanji: '1000',
453
+ eci: '0111',
454
+ },
455
+ capacity(ver, ecc) {
456
+ const bytes = BYTES[ver - 1];
457
+ const words = WORDS_PER_BLOCK[ecc][ver - 1];
458
+ const numBlocks = ECC_BLOCKS[ecc][ver - 1];
459
+ const blockLen = Math.floor(bytes / numBlocks) - words;
460
+ const shortBlocks = numBlocks - (bytes % numBlocks);
461
+ return {
462
+ words,
463
+ numBlocks,
464
+ shortBlocks,
465
+ blockLen,
466
+ capacity: (bytes - words * numBlocks) * 8,
467
+ total: (words + blockLen) * numBlocks + numBlocks - shortBlocks,
468
+ };
469
+ },
470
+ };
471
+ const PATTERNS = [
472
+ (x, y) => (x + y) % 2 == 0,
473
+ (_x, y) => y % 2 == 0,
474
+ (x, _y) => x % 3 == 0,
475
+ (x, y) => (x + y) % 3 == 0,
476
+ (x, y) => (Math.floor(y / 2) + Math.floor(x / 3)) % 2 == 0,
477
+ (x, y) => ((x * y) % 2) + ((x * y) % 3) == 0,
478
+ (x, y) => (((x * y) % 2) + ((x * y) % 3)) % 2 == 0,
479
+ (x, y) => (((x + y) % 2) + ((x * y) % 3)) % 2 == 0,
480
+ ];
481
+ // Galois field && reed-solomon encoding
482
+ const GF = {
483
+ tables: ((p_poly) => {
484
+ const exp = fillArr(256, 0);
485
+ const log = fillArr(256, 0);
486
+ for (let i = 0, x = 1; i < 256; i++) {
487
+ exp[i] = x;
488
+ log[x] = i;
489
+ x <<= 1;
490
+ if (x & 0x100)
491
+ x ^= p_poly;
492
+ }
493
+ return { exp, log };
494
+ })(0x11d),
495
+ exp: (x) => GF.tables.exp[x],
496
+ log(x) {
497
+ if (x === 0)
498
+ throw new Error(`GF.log: invalid arg=${x}`);
499
+ return GF.tables.log[x] % 255;
500
+ },
501
+ mul(x, y) {
502
+ if (x === 0 || y === 0)
503
+ return 0;
504
+ return GF.tables.exp[(GF.tables.log[x] + GF.tables.log[y]) % 255];
505
+ },
506
+ add: (x, y) => x ^ y,
507
+ pow: (x, e) => GF.tables.exp[(GF.tables.log[x] * e) % 255],
508
+ inv(x) {
509
+ if (x === 0)
510
+ throw new Error(`GF.inverse: invalid arg=${x}`);
511
+ return GF.tables.exp[255 - GF.tables.log[x]];
512
+ },
513
+ polynomial(poly) {
514
+ if (poly.length == 0)
515
+ throw new Error('GF.polymomial: invalid length');
516
+ if (poly[0] !== 0)
517
+ return poly;
518
+ // Strip leading zeros
519
+ let i = 0;
520
+ for (; i < poly.length - 1 && poly[i] == 0; i++)
521
+ ;
522
+ return poly.slice(i);
523
+ },
524
+ monomial(degree, coefficient) {
525
+ if (degree < 0)
526
+ throw new Error(`GF.monomial: invalid degree=${degree}`);
527
+ if (coefficient == 0)
528
+ return [0];
529
+ let coefficients = fillArr(degree + 1, 0);
530
+ coefficients[0] = coefficient;
531
+ return GF.polynomial(coefficients);
532
+ },
533
+ degree: (a) => a.length - 1,
534
+ coefficient: (a, degree) => a[GF.degree(a) - degree],
535
+ mulPoly(a, b) {
536
+ if (a[0] === 0 || b[0] === 0)
537
+ return [0];
538
+ const res = fillArr(a.length + b.length - 1, 0);
539
+ for (let i = 0; i < a.length; i++) {
540
+ for (let j = 0; j < b.length; j++) {
541
+ res[i + j] = GF.add(res[i + j], GF.mul(a[i], b[j]));
542
+ }
543
+ }
544
+ return GF.polynomial(res);
545
+ },
546
+ mulPolyScalar(a, scalar) {
547
+ if (scalar == 0)
548
+ return [0];
549
+ if (scalar == 1)
550
+ return a;
551
+ const res = fillArr(a.length, 0);
552
+ for (let i = 0; i < a.length; i++)
553
+ res[i] = GF.mul(a[i], scalar);
554
+ return GF.polynomial(res);
555
+ },
556
+ mulPolyMonomial(a, degree, coefficient) {
557
+ if (degree < 0)
558
+ throw new Error('GF.mulPolyMonomial: invalid degree');
559
+ if (coefficient == 0)
560
+ return [0];
561
+ const res = fillArr(a.length + degree, 0);
562
+ for (let i = 0; i < a.length; i++)
563
+ res[i] = GF.mul(a[i], coefficient);
564
+ return GF.polynomial(res);
565
+ },
566
+ addPoly(a, b) {
567
+ if (a[0] === 0)
568
+ return b;
569
+ if (b[0] === 0)
570
+ return a;
571
+ let smaller = a;
572
+ let larger = b;
573
+ if (smaller.length > larger.length)
574
+ [smaller, larger] = [larger, smaller];
575
+ let sumDiff = fillArr(larger.length, 0);
576
+ let lengthDiff = larger.length - smaller.length;
577
+ let s = larger.slice(0, lengthDiff);
578
+ for (let i = 0; i < s.length; i++)
579
+ sumDiff[i] = s[i];
580
+ for (let i = lengthDiff; i < larger.length; i++)
581
+ sumDiff[i] = GF.add(smaller[i - lengthDiff], larger[i]);
582
+ return GF.polynomial(sumDiff);
583
+ },
584
+ remainderPoly(data, divisor) {
585
+ const out = Array.from(data);
586
+ for (let i = 0; i < data.length - divisor.length + 1; i++) {
587
+ const elm = out[i];
588
+ if (elm === 0)
589
+ continue;
590
+ for (let j = 1; j < divisor.length; j++) {
591
+ if (divisor[j] !== 0)
592
+ out[i + j] = GF.add(out[i + j], GF.mul(divisor[j], elm));
593
+ }
594
+ }
595
+ return out.slice(data.length - divisor.length + 1, out.length);
596
+ },
597
+ divisorPoly(degree) {
598
+ let g = [1];
599
+ for (let i = 0; i < degree; i++)
600
+ g = GF.mulPoly(g, [1, GF.pow(2, i)]);
601
+ return g;
602
+ },
603
+ evalPoly(poly, a) {
604
+ if (a == 0)
605
+ return GF.coefficient(poly, 0); // Just return the x^0 coefficient
606
+ let res = poly[0];
607
+ for (let i = 1; i < poly.length; i++)
608
+ res = GF.add(GF.mul(a, res), poly[i]);
609
+ return res;
610
+ },
611
+ // TODO: cleanup
612
+ euclidian(a, b, R) {
613
+ // Force degree(a) >= degree(b)
614
+ if (GF.degree(a) < GF.degree(b))
615
+ [a, b] = [b, a];
616
+ let rLast = a;
617
+ let r = b;
618
+ let tLast = [0];
619
+ let t = [1];
620
+ // while degree of Ri ≥ t/2
621
+ while (2 * GF.degree(r) >= R) {
622
+ let rLastLast = rLast;
623
+ let tLastLast = tLast;
624
+ rLast = r;
625
+ tLast = t;
626
+ if (rLast[0] === 0)
627
+ throw new Error('rLast[0] === 0');
628
+ r = rLastLast;
629
+ let q = [0];
630
+ const dltInverse = GF.inv(rLast[0]);
631
+ while (GF.degree(r) >= GF.degree(rLast) && r[0] !== 0) {
632
+ const degreeDiff = GF.degree(r) - GF.degree(rLast);
633
+ const scale = GF.mul(r[0], dltInverse);
634
+ q = GF.addPoly(q, GF.monomial(degreeDiff, scale));
635
+ r = GF.addPoly(r, GF.mulPolyMonomial(rLast, degreeDiff, scale));
636
+ }
637
+ q = GF.mulPoly(q, tLast);
638
+ t = GF.addPoly(q, tLastLast);
639
+ if (GF.degree(r) >= GF.degree(rLast))
640
+ throw new Error(`Division failed r: ${r}, rLast: ${rLast}`);
641
+ }
642
+ const sigmaTildeAtZero = GF.coefficient(t, 0);
643
+ if (sigmaTildeAtZero == 0)
644
+ throw new Error('sigmaTilde(0) was zero');
645
+ const inverse = GF.inv(sigmaTildeAtZero);
646
+ return [GF.mulPolyScalar(t, inverse), GF.mulPolyScalar(r, inverse)];
647
+ },
648
+ };
649
+ function RS(eccWords) {
650
+ return {
651
+ encode(from) {
652
+ const d = GF.divisorPoly(eccWords);
653
+ const pol = Array.from(from);
654
+ pol.push(...d.slice(0, -1).fill(0));
655
+ return Uint8Array.from(GF.remainderPoly(pol, d));
656
+ },
657
+ decode(to) {
658
+ const res = to.slice();
659
+ const poly = GF.polynomial(Array.from(to));
660
+ // Find errors
661
+ let syndrome = fillArr(eccWords, 0);
662
+ let hasError = false;
663
+ for (let i = 0; i < eccWords; i++) {
664
+ const evl = GF.evalPoly(poly, GF.exp(i));
665
+ syndrome[syndrome.length - 1 - i] = evl;
666
+ if (evl !== 0)
667
+ hasError = true;
668
+ }
669
+ if (!hasError)
670
+ return res;
671
+ syndrome = GF.polynomial(syndrome);
672
+ const monomial = GF.monomial(eccWords, 1);
673
+ const [errorLocator, errorEvaluator] = GF.euclidian(monomial, syndrome, eccWords);
674
+ // Error locations
675
+ const locations = fillArr(GF.degree(errorLocator), 0);
676
+ let e = 0;
677
+ for (let i = 1; i < 256 && e < locations.length; i++) {
678
+ if (GF.evalPoly(errorLocator, i) === 0)
679
+ locations[e++] = GF.inv(i);
680
+ }
681
+ if (e !== locations.length)
682
+ throw new Error('RS.decode: invalid errors number');
683
+ for (let i = 0; i < locations.length; i++) {
684
+ const pos = res.length - 1 - GF.log(locations[i]);
685
+ if (pos < 0)
686
+ throw new Error('RS.decode: invalid error location');
687
+ const xiInverse = GF.inv(locations[i]);
688
+ let denominator = 1;
689
+ for (let j = 0; j < locations.length; j++) {
690
+ if (i === j)
691
+ continue;
692
+ denominator = GF.mul(denominator, GF.add(1, GF.mul(locations[j], xiInverse)));
693
+ }
694
+ res[pos] = GF.add(res[pos], GF.mul(GF.evalPoly(errorEvaluator, xiInverse), GF.inv(denominator)));
695
+ }
696
+ return res;
697
+ },
698
+ };
699
+ }
700
+ // Interleaves blocks
701
+ function interleave(ver, ecc) {
702
+ const { words, shortBlocks, numBlocks, blockLen, total } = info.capacity(ver, ecc);
703
+ const rs = RS(words);
704
+ return {
705
+ encode(bytes) {
706
+ // Add error correction to bytes
707
+ const blocks = [];
708
+ const eccBlocks = [];
709
+ for (let i = 0; i < numBlocks; i++) {
710
+ const isShort = i < shortBlocks;
711
+ const len = blockLen + (isShort ? 0 : 1);
712
+ blocks.push(bytes.subarray(0, len));
713
+ eccBlocks.push(rs.encode(bytes.subarray(0, len)));
714
+ bytes = bytes.subarray(len);
715
+ }
716
+ const resBlocks = interleaveBytes(...blocks);
717
+ const resECC = interleaveBytes(...eccBlocks);
718
+ const res = new Uint8Array(resBlocks.length + resECC.length);
719
+ res.set(resBlocks);
720
+ res.set(resECC, resBlocks.length);
721
+ return res;
722
+ },
723
+ decode(data) {
724
+ if (data.length !== total)
725
+ throw new Error(`interleave.decode: len(data)=${data.length}, total=${total}`);
726
+ const blocks = [];
727
+ for (let i = 0; i < numBlocks; i++) {
728
+ const isShort = i < shortBlocks;
729
+ blocks.push(new Uint8Array(words + blockLen + (isShort ? 0 : 1)));
730
+ }
731
+ // Short blocks
732
+ let pos = 0;
733
+ for (let i = 0; i < blockLen; i++) {
734
+ for (let j = 0; j < numBlocks; j++)
735
+ blocks[j][i] = data[pos++];
736
+ }
737
+ // Long blocks
738
+ for (let j = shortBlocks; j < numBlocks; j++)
739
+ blocks[j][blockLen] = data[pos++];
740
+ // ECC
741
+ for (let i = blockLen; i < blockLen + words; i++) {
742
+ for (let j = 0; j < numBlocks; j++) {
743
+ const isShort = j < shortBlocks;
744
+ blocks[j][i + (isShort ? 0 : 1)] = data[pos++];
745
+ }
746
+ }
747
+ // Decode
748
+ // Error-correct and copy data blocks together into a stream of bytes
749
+ const res = [];
750
+ for (const block of blocks)
751
+ res.push(...Array.from(rs.decode(block)).slice(0, -words));
752
+ return Uint8Array.from(res);
753
+ },
754
+ };
755
+ }
756
+ // Draw
757
+ // Generic template per version+ecc+mask. Can be cached, to speedup calculations.
758
+ function drawTemplate(ver, ecc, maskIdx, test = false) {
759
+ const size = info.size.encode(ver);
760
+ let b = new Bitmap(size + 2);
761
+ // Finder patterns
762
+ // We draw full pattern and later slice, since before addition of borders finder is truncated by one pixel on sides
763
+ const finder = new Bitmap(3).rect(0, 3, true).border(1, false).border(1, true).border(1, false);
764
+ b = b
765
+ .embed(0, finder) // top left
766
+ .embed({ x: -finder.width, y: 0 }, finder) // top right
767
+ .embed({ x: 0, y: -finder.height }, finder); // bottom left
768
+ b = b.rectSlice(1, size);
769
+ // Alignment patterns
770
+ const align = new Bitmap(1).rect(0, 1, true).border(1, false).border(1, true);
771
+ const alignPos = info.alignmentPatterns(ver);
772
+ for (const y of alignPos) {
773
+ for (const x of alignPos) {
774
+ if (b.data[y][x] !== undefined)
775
+ continue;
776
+ b.embed({ x: x - 2, y: y - 2 }, align); // center of pattern should be at position
777
+ }
778
+ }
779
+ // Timing patterns
780
+ b = b
781
+ .hLine({ x: 0, y: 6 }, Infinity, ({ x }, cur) => (cur === undefined ? x % 2 == 0 : cur))
782
+ .vLine({ x: 6, y: 0 }, Infinity, ({ y }, cur) => (cur === undefined ? y % 2 == 0 : cur));
783
+ // Format information
784
+ {
785
+ const bits = info.formatBits(ecc, maskIdx);
786
+ const getBit = (i) => !test && ((bits >> i) & 1) == 1;
787
+ // vertical
788
+ for (let i = 0; i < 6; i++)
789
+ b.data[i][8] = getBit(i); // right of top-left finder
790
+ // TODO: re-write as lines, like:
791
+ // b.vLine({ x: 8, y: 0 }, 6, ({ x, y }) => getBit(y));
792
+ for (let i = 6; i < 8; i++)
793
+ b.data[i + 1][8] = getBit(i); // after timing pattern
794
+ for (let i = 8; i < 15; i++)
795
+ b.data[size - 15 + i][8] = getBit(i); // right of bottom-left finder
796
+ // horizontal
797
+ for (let i = 0; i < 8; i++)
798
+ b.data[8][size - i - 1] = getBit(i); // under top-right finder
799
+ for (let i = 8; i < 9; i++)
800
+ b.data[8][15 - i - 1 + 1] = getBit(i); // VVV, after timing
801
+ for (let i = 9; i < 15; i++)
802
+ b.data[8][15 - i - 1] = getBit(i); // under top-left finder
803
+ b.data[size - 8][8] = !test; // bottom-left finder, right
804
+ }
805
+ // Version information
806
+ if (ver >= 7) {
807
+ const bits = info.versionBits(ver);
808
+ for (let i = 0; i < 18; i += 1) {
809
+ const bit = !test && ((bits >> i) & 1) == 1;
810
+ const x = Math.floor(i / 3);
811
+ const y = (i % 3) + size - 8 - 3;
812
+ // two copies
813
+ b.data[x][y] = bit;
814
+ b.data[y][x] = bit;
815
+ }
816
+ }
817
+ return b;
818
+ }
819
+ // zigzag: bottom->top && top->bottom
820
+ function zigzag(tpl, maskIdx, fn) {
821
+ const size = tpl.height;
822
+ const pattern = PATTERNS[maskIdx];
823
+ // zig-zag pattern
824
+ let dir = -1;
825
+ let y = size - 1;
826
+ // two columns at time
827
+ for (let xOffset = size - 1; xOffset > 0; xOffset -= 2) {
828
+ if (xOffset == 6)
829
+ xOffset = 5; // skip vertical timing pattern
830
+ for (;; y += dir) {
831
+ for (let j = 0; j < 2; j += 1) {
832
+ const x = xOffset - j;
833
+ if (tpl.data[y][x] !== undefined)
834
+ continue; // skip already written elements
835
+ fn(x, y, pattern(x, y));
836
+ }
837
+ if (y + dir < 0 || y + dir >= size)
838
+ break;
839
+ }
840
+ dir = -dir; // change direction
841
+ }
842
+ }
843
+ // NOTE: byte encoding is just representation, QR works with strings only. Most decoders will fail on raw byte array,
844
+ // since they expect unicode or other text encoding inside bytes
845
+ function detectType(str) {
846
+ let type = 'numeric';
847
+ for (let x of str) {
848
+ if (info.alphabet.numeric.has(x))
849
+ continue;
850
+ type = 'alphanumeric';
851
+ if (!info.alphabet.alphanumerc.has(x))
852
+ return 'byte';
853
+ }
854
+ return type;
855
+ }
856
+ /**
857
+ * @example utf8ToBytes('abc') // new Uint8Array([97, 98, 99])
858
+ */
859
+ function utf8ToBytes(str) {
860
+ if (typeof str !== 'string')
861
+ throw new Error(`utf8ToBytes expected string, got ${typeof str}`);
862
+ return new Uint8Array(new TextEncoder().encode(str)); // https://bugzil.la/1681809
863
+ }
864
+ function encode(ver, ecc, data, type) {
865
+ let encoded = '';
866
+ let dataLen = data.length;
867
+ if (type === 'numeric') {
868
+ const t = info.alphabet.numeric.decode(data.split(''));
869
+ const n = t.length;
870
+ for (let i = 0; i < n - 2; i += 3)
871
+ encoded += bin(t[i] * 100 + t[i + 1] * 10 + t[i + 2], 10);
872
+ if (n % 3 === 1) {
873
+ encoded += bin(t[n - 1], 4);
874
+ }
875
+ else if (n % 3 === 2) {
876
+ encoded += bin(t[n - 2] * 10 + t[n - 1], 7);
877
+ }
878
+ }
879
+ else if (type === 'alphanumeric') {
880
+ const t = info.alphabet.alphanumerc.decode(data.split(''));
881
+ const n = t.length;
882
+ for (let i = 0; i < n - 1; i += 2)
883
+ encoded += bin(t[i] * 45 + t[i + 1], 11);
884
+ if (n % 2 == 1)
885
+ encoded += bin(t[n - 1], 6); // pad if odd number of chars
886
+ }
887
+ else if (type === 'byte') {
888
+ const utf8 = utf8ToBytes(data);
889
+ dataLen = utf8.length;
890
+ encoded = Array.from(utf8)
891
+ .map((i) => bin(i, 8))
892
+ .join('');
893
+ }
894
+ else {
895
+ throw new Error('encode: unsupported type');
896
+ }
897
+ const { capacity } = info.capacity(ver, ecc);
898
+ const len = bin(dataLen, info.lengthBits(ver, type));
899
+ let bits = info.modeBits[type] + len + encoded;
900
+ if (bits.length > capacity)
901
+ throw new Error('Capacity overflow');
902
+ // Terminator
903
+ bits += '0'.repeat(Math.min(4, Math.max(0, capacity - bits.length)));
904
+ // Pad bits string untill full byte
905
+ if (bits.length % 8)
906
+ bits += '0'.repeat(8 - (bits.length % 8));
907
+ // Add padding until capacity is full
908
+ const padding = '1110110000010001';
909
+ for (let idx = 0; bits.length !== capacity; idx++)
910
+ bits += padding[idx % padding.length];
911
+ // Convert a bitstring to array of bytes
912
+ const bytes = Uint8Array.from(bits.match(/(.{8})/g).map((i) => Number(`0b${i}`)));
913
+ return interleave(ver, ecc).encode(bytes);
914
+ }
915
+ // DRAW
916
+ function drawQR(ver, ecc, data, maskIdx, test = false) {
917
+ const b = drawTemplate(ver, ecc, maskIdx, test);
918
+ let i = 0;
919
+ const need = 8 * data.length;
920
+ zigzag(b, maskIdx, (x, y, mask) => {
921
+ let value = false;
922
+ if (i < need) {
923
+ value = ((data[i >>> 3] >> ((7 - i) & 7)) & 1) !== 0;
924
+ i++;
925
+ }
926
+ b.data[y][x] = value !== mask; // !== as xor
927
+ });
928
+ if (i !== need)
929
+ throw new Error('QR: bytes left after draw');
930
+ return b;
931
+ }
932
+ function penalty(bm) {
933
+ const inverse = bm.inverse();
934
+ // Adjacent modules in row/column in same | No. of modules = (5 + i) color
935
+ const sameColor = (row) => {
936
+ let res = 0;
937
+ for (let i = 0, same = 1, last = undefined; i < row.length; i++) {
938
+ if (last === row[i]) {
939
+ same++;
940
+ if (i !== row.length - 1)
941
+ continue; // handle last element
942
+ }
943
+ if (same >= 5)
944
+ res += 3 + (same - 5);
945
+ last = row[i];
946
+ same = 1;
947
+ }
948
+ return res;
949
+ };
950
+ let adjacent = 0;
951
+ bm.data.forEach((row) => (adjacent += sameColor(row)));
952
+ inverse.data.forEach((column) => (adjacent += sameColor(column)));
953
+ // Block of modules in same color (Block size = 2x2)
954
+ let box = 0;
955
+ let b = bm.data;
956
+ const lastW = bm.width - 1;
957
+ const lastH = bm.height - 1;
958
+ for (let x = 0; x < lastW; x++) {
959
+ for (let y = 0; y < lastH; y++) {
960
+ const x1 = x + 1;
961
+ const y1 = y + 1;
962
+ if (b[x][y] === b[x1][y] && b[x1][y] === b[x][y1] && b[x1][y] === b[x1][y1]) {
963
+ box += 3;
964
+ }
965
+ }
966
+ }
967
+ // 1:1:3:1:1 ratio (dark:light:dark:light:dark) pattern in row/column, preceded or followed by light area 4 modules wide
968
+ const finderPattern = (row) => {
969
+ const finderPattern = [true, false, true, true, true, false, true]; // dark:light:dark:light:dark
970
+ const lightPattern = [false, false, false, false]; // light area 4 modules wide
971
+ const p1 = [...finderPattern, ...lightPattern];
972
+ const p2 = [...lightPattern, ...finderPattern];
973
+ let res = 0;
974
+ for (let i = 0; i < row.length; i++) {
975
+ if (includesAt(row, p1, i))
976
+ res += 40;
977
+ if (includesAt(row, p2, i))
978
+ res += 40;
979
+ }
980
+ return res;
981
+ };
982
+ let finder = 0;
983
+ for (const row of bm.data)
984
+ finder += finderPattern(row);
985
+ for (const column of inverse.data)
986
+ finder += finderPattern(column);
987
+ // Proportion of dark modules in entire symbol
988
+ // Add 10 points to a deviation of 5% increment or decrement in the proportion
989
+ // ratio of dark module from the referential 50%
990
+ let darkPixels = 0;
991
+ bm.rectRead(0, Infinity, (_c, val) => (darkPixels += val ? 1 : 0));
992
+ const darkPercent = (darkPixels / (bm.height * bm.width)) * 100;
993
+ const dark = 10 * Math.floor(Math.abs(darkPercent - 50) / 5);
994
+ return adjacent + box + finder + dark;
995
+ }
996
+ // Selects best mask according to penalty, if no mask is provided
997
+ function drawQRBest(ver, ecc, data, maskIdx) {
998
+ if (maskIdx === undefined) {
999
+ const bestMask = best();
1000
+ for (let mask = 0; mask < PATTERNS.length; mask++)
1001
+ bestMask.add(penalty(drawQR(ver, ecc, data, mask, true)), mask);
1002
+ maskIdx = bestMask.get();
1003
+ }
1004
+ if (maskIdx === undefined)
1005
+ throw new Error('Cannot find mask'); // Should never happen
1006
+ return drawQR(ver, ecc, data, maskIdx);
1007
+ }
1008
+ function validateECC(ec) {
1009
+ if (!exports.ECMode.includes(ec))
1010
+ throw new Error(`Invalid error correction mode=${ec}. Expected: ${exports.ECMode}`);
1011
+ }
1012
+ function validateEncoding(enc) {
1013
+ if (!exports.Encoding.includes(enc))
1014
+ throw new Error(`Encoding: invalid mode=${enc}. Expected: ${exports.Encoding}`);
1015
+ if (enc === 'kanji' || enc === 'eci')
1016
+ throw new Error(`Encoding: ${enc} is not supported (yet?).`);
1017
+ }
1018
+ function validateMask(mask) {
1019
+ if (![0, 1, 2, 3, 4, 5, 6, 7].includes(mask) || !PATTERNS[mask])
1020
+ throw new Error(`Invalid mask=${mask}. Expected number [0..7]`);
1021
+ }
1022
+ function encodeQR(text, output = 'raw', opts = {}) {
1023
+ const ecc = opts.ecc !== undefined ? opts.ecc : 'medium';
1024
+ validateECC(ecc);
1025
+ const encoding = opts.encoding !== undefined ? opts.encoding : detectType(text);
1026
+ validateEncoding(encoding);
1027
+ if (opts.mask !== undefined)
1028
+ validateMask(opts.mask);
1029
+ let ver = opts.version;
1030
+ let data, err = new Error('Unknown error');
1031
+ if (ver !== undefined) {
1032
+ validateVersion(ver);
1033
+ data = encode(ver, ecc, text, encoding);
1034
+ }
1035
+ else {
1036
+ // If no version is provided, try to find smallest one which fits
1037
+ // Currently just scans all version, can be significantly speedup if needed
1038
+ for (let i = 1; i <= 40; i++) {
1039
+ try {
1040
+ data = encode(i, ecc, text, encoding);
1041
+ ver = i;
1042
+ break;
1043
+ }
1044
+ catch (e) {
1045
+ err = e;
1046
+ }
1047
+ }
1048
+ }
1049
+ if (!ver || !data)
1050
+ throw err;
1051
+ let res = drawQRBest(ver, ecc, data, opts.mask);
1052
+ res.assertDrawn();
1053
+ const border = opts.border === undefined ? 2 : opts.border;
1054
+ if (!Number.isSafeInteger(border))
1055
+ throw new Error(`invalid border type=${typeof border}`);
1056
+ res = res.border(border, false); // Add border
1057
+ if (opts.scale !== undefined)
1058
+ res = res.scale(opts.scale); // Scale image
1059
+ if (output === 'raw')
1060
+ return res.data;
1061
+ else if (output === 'ascii')
1062
+ return res.toASCII();
1063
+ else if (output === 'svg')
1064
+ return res.toSVG();
1065
+ else if (output === 'gif')
1066
+ return res.toGIF();
1067
+ else if (output === 'term')
1068
+ return res.toTerm();
1069
+ else
1070
+ throw new Error(`Unknown output: ${output}`);
1071
+ }
1072
+ exports.default = encodeQR;
1073
+ exports.utils = {
1074
+ best,
1075
+ bin,
1076
+ drawTemplate,
1077
+ fillArr,
1078
+ info,
1079
+ interleave,
1080
+ validateVersion,
1081
+ zigzag,
1082
+ };
1083
+ // Unsafe API utils, exported only for tests
1084
+ exports._tests = {
1085
+ Bitmap,
1086
+ info,
1087
+ detectType,
1088
+ encode,
1089
+ drawQR,
1090
+ penalty,
1091
+ PATTERNS,
1092
+ };
1093
+ // Type tests
1094
+ // const o1 = qr('test', 'ascii');
1095
+ // const o2 = qr('test', 'raw');
1096
+ // const o3 = qr('test', 'gif');
1097
+ // const o4 = qr('test', 'svg');
1098
+ // const o5 = qr('test', 'term');
1099
+ //# sourceMappingURL=index.js.map