@twin.org/qr 0.0.1-next.1

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.
Files changed (40) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +21 -0
  3. package/dist/cjs/index.cjs +2006 -0
  4. package/dist/esm/index.mjs +2000 -0
  5. package/dist/types/data/qrAlphaNumeric.d.ts +23 -0
  6. package/dist/types/data/qrByte8.d.ts +23 -0
  7. package/dist/types/data/qrNumber.d.ts +23 -0
  8. package/dist/types/helpers/bitBuffer.d.ts +36 -0
  9. package/dist/types/helpers/mathHelper.d.ts +23 -0
  10. package/dist/types/helpers/polynomial.d.ts +45 -0
  11. package/dist/types/helpers/qrHelper.d.ts +49 -0
  12. package/dist/types/helpers/rsBlock.d.ts +30 -0
  13. package/dist/types/index.d.ts +9 -0
  14. package/dist/types/models/IBitmapRendererOptions.d.ts +15 -0
  15. package/dist/types/models/IRendererOptions.d.ts +13 -0
  16. package/dist/types/models/ITextRendererOptions.d.ts +14 -0
  17. package/dist/types/models/errorCorrectLevel.d.ts +27 -0
  18. package/dist/types/models/maskPattern.d.ts +43 -0
  19. package/dist/types/models/qrCellData.d.ts +4 -0
  20. package/dist/types/models/qrDataBase.d.ts +42 -0
  21. package/dist/types/models/qrDataMode.d.ts +23 -0
  22. package/dist/types/qr.d.ts +35 -0
  23. package/dist/types/renderers/jpegRenderer.d.ts +14 -0
  24. package/dist/types/renderers/pngRenderer.d.ts +14 -0
  25. package/dist/types/renderers/textRenderer.d.ts +14 -0
  26. package/docs/changelog.md +5 -0
  27. package/docs/examples.md +1 -0
  28. package/docs/reference/classes/JpegRenderer.md +37 -0
  29. package/docs/reference/classes/PngRenderer.md +37 -0
  30. package/docs/reference/classes/QR.md +98 -0
  31. package/docs/reference/classes/TextRenderer.md +37 -0
  32. package/docs/reference/index.md +23 -0
  33. package/docs/reference/interfaces/IBitmapRendererOptions.md +47 -0
  34. package/docs/reference/interfaces/IRendererOptions.md +24 -0
  35. package/docs/reference/interfaces/ITextRendererOptions.md +47 -0
  36. package/docs/reference/type-aliases/ErrorCorrectLevel.md +6 -0
  37. package/docs/reference/type-aliases/QRCellData.md +5 -0
  38. package/docs/reference/variables/ErrorCorrectLevel.md +32 -0
  39. package/locales/en.json +34 -0
  40. package/package.json +64 -0
@@ -0,0 +1,2006 @@
1
+ 'use strict';
2
+
3
+ var core = require('@twin.org/core');
4
+ var image = require('@twin.org/image');
5
+
6
+ // Copyright 2024 IOTA Stiftung.
7
+ // SPDX-License-Identifier: Apache-2.0.
8
+ /**
9
+ * Error correction level to use for the QR Code.
10
+ * Based on https://github.com/kazuhikoarase/qrcode-generator/ .
11
+ */
12
+ // eslint-disable-next-line @typescript-eslint/naming-convention
13
+ const ErrorCorrectLevel = {
14
+ /**
15
+ * 7% Error correction.
16
+ */
17
+ L: 1,
18
+ /**
19
+ * 15% Error correction.
20
+ */
21
+ M: 0,
22
+ /**
23
+ * 25% Error correction.
24
+ */
25
+ Q: 3,
26
+ /**
27
+ * 30% Error correction.
28
+ */
29
+ H: 2
30
+ };
31
+
32
+ // Copyright 2024 IOTA Stiftung.
33
+ // SPDX-License-Identifier: Apache-2.0.
34
+ /**
35
+ * The mode for the qr data.
36
+ * Based on https://github.com/kazuhikoarase/qrcode-generator/ .
37
+ */
38
+ // eslint-disable-next-line @typescript-eslint/naming-convention
39
+ const QRDataMode = {
40
+ /**
41
+ * Number.
42
+ */
43
+ Number: 1,
44
+ /**
45
+ * Alphabet and number.
46
+ */
47
+ AlphaNumeric: 2,
48
+ /**
49
+ * 8bit byte.
50
+ */
51
+ Byte8: 4
52
+ };
53
+
54
+ // Copyright 2024 IOTA Stiftung.
55
+ // SPDX-License-Identifier: Apache-2.0.
56
+ /**
57
+ * Base class for storing QR Data.
58
+ * Based on https://github.com/kazuhikoarase/qrcode-generator/ .
59
+ */
60
+ class QRDataBase {
61
+ /**
62
+ * @internal
63
+ */
64
+ _className;
65
+ /**
66
+ * @internal
67
+ */
68
+ _mode;
69
+ /**
70
+ * @internal
71
+ */
72
+ _data;
73
+ /**
74
+ * Create a new instance of QRDataBase.
75
+ * @param className The class name for the derived class.
76
+ * @param mode The mode for the data.
77
+ * @param data The data.
78
+ */
79
+ constructor(className, mode, data) {
80
+ this._className = className;
81
+ this._mode = mode;
82
+ this._data = data;
83
+ }
84
+ /**
85
+ * Get the data mode.
86
+ * @returns The data mode.
87
+ */
88
+ getMode() {
89
+ return this._mode;
90
+ }
91
+ /**
92
+ * Get the data for the qr.
93
+ * @returns The data.
94
+ */
95
+ getData() {
96
+ return this._data;
97
+ }
98
+ /**
99
+ * Get the length in bits of the data.
100
+ * @param typeNumber The type number to get the length for.
101
+ * @returns The length in bits.
102
+ * @throws Error if the typeNumber if invalid.
103
+ */
104
+ getLengthInBits(typeNumber) {
105
+ if (typeNumber >= 1 && typeNumber < 10) {
106
+ switch (this._mode) {
107
+ case QRDataMode.Number:
108
+ return 10;
109
+ case QRDataMode.AlphaNumeric:
110
+ return 9;
111
+ case QRDataMode.Byte8:
112
+ return 8;
113
+ default:
114
+ throw new core.GeneralError(this._className, "invalidMode", {
115
+ typeNumber,
116
+ mode: this._mode
117
+ });
118
+ }
119
+ }
120
+ else if (typeNumber < 27) {
121
+ switch (this._mode) {
122
+ case QRDataMode.Number:
123
+ return 12;
124
+ case QRDataMode.AlphaNumeric:
125
+ return 11;
126
+ case QRDataMode.Byte8:
127
+ return 16;
128
+ default:
129
+ throw new core.GeneralError(this._className, "invalidMode", {
130
+ typeNumber,
131
+ mode: this._mode
132
+ });
133
+ }
134
+ }
135
+ else if (typeNumber < 41) {
136
+ switch (this._mode) {
137
+ case QRDataMode.Number:
138
+ return 14;
139
+ case QRDataMode.AlphaNumeric:
140
+ return 13;
141
+ case QRDataMode.Byte8:
142
+ return 16;
143
+ default:
144
+ throw new core.GeneralError(this._className, "invalidMode", {
145
+ typeNumber,
146
+ mode: this._mode
147
+ });
148
+ }
149
+ }
150
+ else {
151
+ throw new core.GeneralError(this._className, "invalidTypeNumber", { typeNumber });
152
+ }
153
+ }
154
+ }
155
+
156
+ // Copyright 2024 IOTA Stiftung.
157
+ // SPDX-License-Identifier: Apache-2.0.
158
+ /* eslint-disable no-mixed-operators */
159
+ /**
160
+ * QR Data for representing a alpha numeric.
161
+ * Based on https://github.com/kazuhikoarase/qrcode-generator/ .
162
+ */
163
+ class QRAlphaNumeric extends QRDataBase {
164
+ /**
165
+ * Runtime name for the class.
166
+ * @internal
167
+ */
168
+ static _CLASS_NAME = "QRAlphaNumeric";
169
+ /**
170
+ * Create a new instance of QRAlphaNumeric.
171
+ * @param data The data for the qr alpha numeric.
172
+ */
173
+ constructor(data) {
174
+ super(QRAlphaNumeric._CLASS_NAME, QRDataMode.AlphaNumeric, data);
175
+ }
176
+ /**
177
+ * Get the length of the data.
178
+ * @returns The length of the data.
179
+ */
180
+ getLength() {
181
+ return this.getData().length;
182
+ }
183
+ /**
184
+ * Write data into the buffer.
185
+ * @param buffer The buffer to write into.
186
+ */
187
+ write(buffer) {
188
+ const s = this.getData();
189
+ let i = 0;
190
+ while (i + 1 < s.length) {
191
+ buffer.put(this.getCode(s.charAt(i)) * 45 + this.getCode(s.charAt(i + 1)), 11);
192
+ i += 2;
193
+ }
194
+ if (i < s.length) {
195
+ buffer.put(this.getCode(s.charAt(i)), 6);
196
+ }
197
+ }
198
+ /**
199
+ * @internal
200
+ */
201
+ getCode(c) {
202
+ if (c >= "0" && c <= "9") {
203
+ return c.charCodeAt(0) - "0".charCodeAt(0);
204
+ }
205
+ if (c >= "A" && c <= "Z") {
206
+ return c.charCodeAt(0) - "A".charCodeAt(0) + 10;
207
+ }
208
+ switch (c) {
209
+ case " ":
210
+ return 36;
211
+ case "$":
212
+ return 37;
213
+ case "%":
214
+ return 38;
215
+ case "*":
216
+ return 39;
217
+ case "+":
218
+ return 40;
219
+ case "-":
220
+ return 41;
221
+ case ".":
222
+ return 42;
223
+ case "/":
224
+ return 43;
225
+ case ":":
226
+ return 44;
227
+ default:
228
+ throw new core.GeneralError(QRAlphaNumeric._CLASS_NAME, "illegalCharacter", { value: c });
229
+ }
230
+ }
231
+ }
232
+
233
+ /**
234
+ * QR Data for representing a 8 bit data.
235
+ * Based on https://github.com/kazuhikoarase/qrcode-generator/ .
236
+ */
237
+ class QRByte8 extends QRDataBase {
238
+ /**
239
+ * Runtime name for the class.
240
+ * @internal
241
+ */
242
+ static _CLASS_NAME = "QRByte8";
243
+ /**
244
+ * Create a new instance of QRByte8.
245
+ * @param data The data for the qr 8 bit data.
246
+ */
247
+ constructor(data) {
248
+ super(QRByte8._CLASS_NAME, QRDataMode.Byte8, data);
249
+ }
250
+ /**
251
+ * Get the length of the data.
252
+ * @returns The length of the data.
253
+ */
254
+ getLength() {
255
+ return this.stringToBytes(this.getData()).length;
256
+ }
257
+ /**
258
+ * Write data into the buffer.
259
+ * @param buffer The buffer to write into.
260
+ */
261
+ write(buffer) {
262
+ const data = this.stringToBytes(this.getData());
263
+ for (let i = 0; i < data.length; i++) {
264
+ buffer.put(data[i], 8);
265
+ }
266
+ }
267
+ /**
268
+ * @internal
269
+ */
270
+ stringToBytes(str) {
271
+ const utf8 = [];
272
+ for (let i = 0; i < str.length; i++) {
273
+ let charCode = str.charCodeAt(i);
274
+ if (charCode < 0x80) {
275
+ utf8.push(charCode);
276
+ }
277
+ else if (charCode < 0x800) {
278
+ utf8.push(0xc0 | (charCode >> 6), 0x80 | (charCode & 0x3f));
279
+ }
280
+ else if (charCode < 0xd800 || charCode >= 0xe000) {
281
+ utf8.push(0xe0 | (charCode >> 12), 0x80 | ((charCode >> 6) & 0x3f), 0x80 | (charCode & 0x3f));
282
+ }
283
+ else {
284
+ i++;
285
+ // UTF-16 encodes 0x10000-0x10FFFF by
286
+ // subtracting 0x10000 and splitting the
287
+ // 20 bits of 0x0-0xFFFFF into two halves
288
+ charCode = 0x10000 + (((charCode & 0x3ff) << 10) | (str.charCodeAt(i) & 0x3ff));
289
+ utf8.push(0xf0 | (charCode >> 18), 0x80 | ((charCode >> 12) & 0x3f), 0x80 | ((charCode >> 6) & 0x3f), 0x80 | (charCode & 0x3f));
290
+ }
291
+ }
292
+ return utf8;
293
+ }
294
+ }
295
+
296
+ // Copyright 2024 IOTA Stiftung.
297
+ // SPDX-License-Identifier: Apache-2.0.
298
+ /* eslint-disable no-mixed-operators */
299
+ /**
300
+ * QR Data for representing a number.
301
+ * Based on https://github.com/kazuhikoarase/qrcode-generator/ .
302
+ */
303
+ class QRNumber extends QRDataBase {
304
+ /**
305
+ * Runtime name for the class.
306
+ * @internal
307
+ */
308
+ static _CLASS_NAME = "QRNumber";
309
+ /**
310
+ * Create a new instance of QRNumber.
311
+ * @param data The data for the qr number.
312
+ */
313
+ constructor(data) {
314
+ super(QRNumber._CLASS_NAME, QRDataMode.Number, data);
315
+ }
316
+ /**
317
+ * Get the length of the data.
318
+ * @returns The length of the data.
319
+ */
320
+ getLength() {
321
+ return this.getData().length;
322
+ }
323
+ /**
324
+ * Write data into the buffer.
325
+ * @param buffer The buffer to write into.
326
+ */
327
+ write(buffer) {
328
+ const data = this.getData();
329
+ let i = 0;
330
+ while (i + 2 < data.length) {
331
+ // eslint-disable-next-line unicorn/prefer-string-slice
332
+ buffer.put(this.strToNum(data.substring(i, i + 3)), 10);
333
+ i += 3;
334
+ }
335
+ if (i < data.length) {
336
+ if (data.length - i === 1) {
337
+ // eslint-disable-next-line unicorn/prefer-string-slice
338
+ buffer.put(this.strToNum(data.substring(i, i + 1)), 4);
339
+ }
340
+ else if (data.length - i === 2) {
341
+ // eslint-disable-next-line unicorn/prefer-string-slice
342
+ buffer.put(this.strToNum(data.substring(i, i + 2)), 7);
343
+ }
344
+ }
345
+ }
346
+ /**
347
+ * @internal
348
+ */
349
+ strToNum(s) {
350
+ let num = 0;
351
+ for (let i = 0; i < s.length; i++) {
352
+ num = num * 10 + this.charToNum(s.charAt(i));
353
+ }
354
+ return num;
355
+ }
356
+ /**
357
+ * @internal
358
+ */
359
+ charToNum(c) {
360
+ if (c >= "0" && c <= "9") {
361
+ return c.charCodeAt(0) - "0".charCodeAt(0);
362
+ }
363
+ throw new core.GeneralError(QRNumber._CLASS_NAME, "illegalCharacter", { value: c });
364
+ }
365
+ }
366
+
367
+ // Copyright 2024 IOTA Stiftung.
368
+ // SPDX-License-Identifier: Apache-2.0.
369
+ /* eslint-disable no-bitwise */
370
+ /* eslint-disable unicorn/prefer-math-trunc */
371
+ /**
372
+ * Class for maintaining data bits.
373
+ * Based on https://github.com/kazuhikoarase/qrcode-generator/ .
374
+ */
375
+ class BitBuffer {
376
+ /**
377
+ * @internal
378
+ */
379
+ _buffer;
380
+ /**
381
+ * @internal
382
+ */
383
+ _length;
384
+ /**
385
+ * Create a new instance of BitBuffer.
386
+ */
387
+ constructor() {
388
+ this._buffer = [];
389
+ this._length = 0;
390
+ }
391
+ /**
392
+ * Get the buffer.
393
+ * @returns The buffer.
394
+ */
395
+ getBuffer() {
396
+ return this._buffer;
397
+ }
398
+ /**
399
+ * Get the length in bits.
400
+ * @returns The length.
401
+ */
402
+ getLengthInBits() {
403
+ return this._length;
404
+ }
405
+ /**
406
+ * Put a value in to the bit buffer.
407
+ * @param num The value.
408
+ * @param length The length.
409
+ */
410
+ put(num, length) {
411
+ for (let i = 0; i < length; i++) {
412
+ this.putBit(((num >>> (length - i - 1)) & 1) === 1);
413
+ }
414
+ }
415
+ /**
416
+ * Put a bit.
417
+ * @param bit The bit to put.
418
+ */
419
+ putBit(bit) {
420
+ if (this._length === this._buffer.length * 8) {
421
+ this._buffer.push(0);
422
+ }
423
+ if (bit) {
424
+ this._buffer[~~(this._length / 8)] |= 0x80 >>> this._length % 8;
425
+ }
426
+ this._length++;
427
+ }
428
+ /**
429
+ * Convert to string.
430
+ * @returns The buffer as string.
431
+ */
432
+ toString() {
433
+ let buffer = "";
434
+ for (let i = 0; i < this.getLengthInBits(); i++) {
435
+ buffer += this.getBit(i) ? "1" : "0";
436
+ }
437
+ return buffer;
438
+ }
439
+ /**
440
+ * @internal
441
+ */
442
+ getBit(index) {
443
+ return ((this._buffer[~~(index / 8)] >>> (7 - (index % 8))) & 1) === 1;
444
+ }
445
+ }
446
+
447
+ // Copyright 2024 IOTA Stiftung.
448
+ // SPDX-License-Identifier: Apache-2.0.
449
+ /* eslint-disable no-bitwise */
450
+ /**
451
+ * Class to helper with math.
452
+ * Based on https://github.com/kazuhikoarase/qrcode-generator/ .
453
+ */
454
+ class MathHelper {
455
+ /**
456
+ * Runtime name for the class.
457
+ * @internal
458
+ */
459
+ static _CLASS_NAME = "MathHelper";
460
+ /**
461
+ * @internal
462
+ */
463
+ static _EXP_TABLE;
464
+ /**
465
+ * @internal
466
+ */
467
+ static _LOG_TABLE;
468
+ /**
469
+ * Initialize the math helper.
470
+ */
471
+ static initialize() {
472
+ if (!MathHelper._EXP_TABLE) {
473
+ MathHelper._EXP_TABLE = [];
474
+ MathHelper._LOG_TABLE = [];
475
+ for (let i = 0; i < 256; i++) {
476
+ MathHelper._EXP_TABLE.push(i < 8
477
+ ? 1 << i
478
+ : MathHelper._EXP_TABLE[i - 4] ^
479
+ MathHelper._EXP_TABLE[i - 5] ^
480
+ MathHelper._EXP_TABLE[i - 6] ^
481
+ MathHelper._EXP_TABLE[i - 8]);
482
+ MathHelper._LOG_TABLE.push(0);
483
+ }
484
+ for (let i = 0; i < 255; i++) {
485
+ MathHelper._LOG_TABLE[MathHelper._EXP_TABLE[i]] = i;
486
+ }
487
+ }
488
+ }
489
+ /**
490
+ * Get the log of the value.
491
+ * @param value The value to get the log of.
492
+ * @returns The log of the value.
493
+ * @throws Error if value < 1.
494
+ */
495
+ static gLog(value) {
496
+ if (value < 1) {
497
+ throw new core.GeneralError(MathHelper._CLASS_NAME, "lessThanOne", { value });
498
+ }
499
+ return MathHelper._LOG_TABLE[value];
500
+ }
501
+ /**
502
+ * Get the exponent of the value.
503
+ * @param value The value to get the exponent of.
504
+ * @returns The exponent of the value.
505
+ */
506
+ static gExp(value) {
507
+ let localValue = value;
508
+ while (localValue < 0) {
509
+ localValue += 255;
510
+ }
511
+ while (localValue >= 256) {
512
+ localValue -= 255;
513
+ }
514
+ return MathHelper._EXP_TABLE[localValue];
515
+ }
516
+ }
517
+
518
+ // Copyright 2024 IOTA Stiftung.
519
+ // SPDX-License-Identifier: Apache-2.0.
520
+ /* eslint-disable no-bitwise */
521
+ /**
522
+ * Class to represent a polynomial.
523
+ * Based on https://github.com/kazuhikoarase/qrcode-generator/ .
524
+ */
525
+ class Polynomial {
526
+ /**
527
+ * @internal
528
+ */
529
+ _num;
530
+ /**
531
+ * Create a new instance of Polynomial.
532
+ * @param num The num of the polynomial.
533
+ * @param shift The shift for the polynomial.
534
+ */
535
+ constructor(num, shift = 0) {
536
+ let offset = 0;
537
+ while (offset < num.length && num[offset] === 0) {
538
+ offset++;
539
+ }
540
+ this._num = [];
541
+ const len = num.length - offset;
542
+ for (let i = 0; i < len; i++) {
543
+ this._num.push(num[offset + i]);
544
+ }
545
+ for (let i = 0; i < shift; i++) {
546
+ this._num.push(0);
547
+ }
548
+ }
549
+ /**
550
+ * The the value of the polynomial at given index.
551
+ * @param index The index.
552
+ * @returns The value of the polynomial.
553
+ */
554
+ getAt(index) {
555
+ return this._num[index];
556
+ }
557
+ /**
558
+ * Get the length of the polynomial.
559
+ * @returns The polynomial.
560
+ */
561
+ getLength() {
562
+ return this._num.length;
563
+ }
564
+ /**
565
+ * Convert the polynomial to a string.
566
+ * @returns The string representation of the polynomial.
567
+ */
568
+ toString() {
569
+ let buffer = "";
570
+ for (let i = 0; i < this.getLength(); i++) {
571
+ if (i > 0) {
572
+ buffer += ",";
573
+ }
574
+ buffer += this.getAt(i);
575
+ }
576
+ return buffer.toString();
577
+ }
578
+ /**
579
+ * Get the log representation of the polynomial.
580
+ * @returns The log representation of the polynomial.
581
+ */
582
+ toLogString() {
583
+ let buffer = "";
584
+ for (let i = 0; i < this.getLength(); i++) {
585
+ if (i > 0) {
586
+ buffer += ",";
587
+ }
588
+ buffer += MathHelper.gLog(this.getAt(i));
589
+ }
590
+ return buffer.toString();
591
+ }
592
+ /**
593
+ * Multiply the polynomial with another one.
594
+ * @param e The polynomial to multiply.
595
+ * @returns The multiplication of the polynomials.
596
+ */
597
+ multiply(e) {
598
+ const num = [];
599
+ const len = this.getLength() + e.getLength() - 1;
600
+ for (let i = 0; i < len; i++) {
601
+ num.push(0);
602
+ }
603
+ for (let i = 0; i < this.getLength(); i++) {
604
+ for (let j = 0; j < e.getLength(); j++) {
605
+ num[i + j] ^= MathHelper.gExp(MathHelper.gLog(this.getAt(i)) + MathHelper.gLog(e.getAt(j)));
606
+ }
607
+ }
608
+ return new Polynomial(num);
609
+ }
610
+ /**
611
+ * Get the modulus of the polynomial from another.
612
+ * @param e The polynomial.
613
+ * @returns The modules of the polynomials.
614
+ */
615
+ mod(e) {
616
+ if (this.getLength() - e.getLength() < 0) {
617
+ return this;
618
+ }
619
+ const ratio = MathHelper.gLog(this.getAt(0)) - MathHelper.gLog(e.getAt(0));
620
+ // create copy
621
+ const num = [];
622
+ for (let i = 0; i < this.getLength(); i++) {
623
+ num.push(this.getAt(i));
624
+ }
625
+ // subtract and calc rest.
626
+ for (let i = 0; i < e.getLength(); i++) {
627
+ num[i] ^= MathHelper.gExp(MathHelper.gLog(e.getAt(i)) + ratio);
628
+ }
629
+ // call recursively
630
+ return new Polynomial(num).mod(e);
631
+ }
632
+ }
633
+
634
+ // Copyright 2024 IOTA Stiftung.
635
+ // SPDX-License-Identifier: Apache-2.0.
636
+ /**
637
+ * Mask patterns for QR codes.
638
+ * Based on https://github.com/kazuhikoarase/qrcode-generator/ .
639
+ */
640
+ // eslint-disable-next-line @typescript-eslint/naming-convention
641
+ const MaskPattern = {
642
+ /**
643
+ * Mask pattern 000.
644
+ */
645
+ PATTERN000: 0b000,
646
+ /**
647
+ * Mask pattern 001.
648
+ */
649
+ PATTERN001: 0b001,
650
+ /**
651
+ * Mask pattern 010.
652
+ */
653
+ PATTERN010: 0b010,
654
+ /**
655
+ * Mask pattern 011.
656
+ */
657
+ PATTERN011: 0b011,
658
+ /**
659
+ * Mask pattern 100.
660
+ */
661
+ PATTERN100: 0b100,
662
+ /**
663
+ * Mask pattern 101.
664
+ */
665
+ PATTERN101: 0b101,
666
+ /**
667
+ * Mask pattern 110.
668
+ */
669
+ PATTERN110: 0b110,
670
+ /**
671
+ * Mask pattern 111.
672
+ */
673
+ PATTERN111: 0b111
674
+ };
675
+
676
+ // Copyright 2024 IOTA Stiftung.
677
+ // SPDX-License-Identifier: Apache-2.0.
678
+ /* eslint-disable no-bitwise */
679
+ /* eslint-disable unicorn/prefer-math-trunc */
680
+ /**
681
+ * Helper methods for QR generation.
682
+ * Based on https://github.com/kazuhikoarase/qrcode-generator/ .
683
+ */
684
+ class QRHelper {
685
+ /**
686
+ * Runtime name for the class.
687
+ * @internal
688
+ */
689
+ static _CLASS_NAME = "QRHelper";
690
+ /**
691
+ * @internal
692
+ */
693
+ static _PATTERN_POSITION_TABLE = [
694
+ [],
695
+ [6, 18],
696
+ [6, 22],
697
+ [6, 26],
698
+ [6, 30],
699
+ [6, 34],
700
+ [6, 22, 38],
701
+ [6, 24, 42],
702
+ [6, 26, 46],
703
+ [6, 28, 50],
704
+ [6, 30, 54],
705
+ [6, 32, 58],
706
+ [6, 34, 62],
707
+ [6, 26, 46, 66],
708
+ [6, 26, 48, 70],
709
+ [6, 26, 50, 74],
710
+ [6, 30, 54, 78],
711
+ [6, 30, 56, 82],
712
+ [6, 30, 58, 86],
713
+ [6, 34, 62, 90],
714
+ [6, 28, 50, 72, 94],
715
+ [6, 26, 50, 74, 98],
716
+ [6, 30, 54, 78, 102],
717
+ [6, 28, 54, 80, 106],
718
+ [6, 32, 58, 84, 110],
719
+ [6, 30, 58, 86, 114],
720
+ [6, 34, 62, 90, 118],
721
+ [6, 26, 50, 74, 98, 122],
722
+ [6, 30, 54, 78, 102, 126],
723
+ [6, 26, 52, 78, 104, 130],
724
+ [6, 30, 56, 82, 108, 134],
725
+ [6, 34, 60, 86, 112, 138],
726
+ [6, 30, 58, 86, 114, 142],
727
+ [6, 34, 62, 90, 118, 146],
728
+ [6, 30, 54, 78, 102, 126, 150],
729
+ [6, 24, 50, 76, 102, 128, 154],
730
+ [6, 28, 54, 80, 106, 132, 158],
731
+ [6, 32, 58, 84, 110, 136, 162],
732
+ [6, 26, 54, 82, 110, 138, 166],
733
+ [6, 30, 58, 86, 114, 142, 170]
734
+ ];
735
+ /**
736
+ * @internal
737
+ */
738
+ static _MAX_LENGTH = [
739
+ [
740
+ [41, 25, 17, 10],
741
+ [34, 20, 14, 8],
742
+ [27, 16, 11, 7],
743
+ [17, 10, 7, 4]
744
+ ],
745
+ [
746
+ [77, 47, 32, 20],
747
+ [63, 38, 26, 16],
748
+ [48, 29, 20, 12],
749
+ [34, 20, 14, 8]
750
+ ],
751
+ [
752
+ [127, 77, 53, 32],
753
+ [101, 61, 42, 26],
754
+ [77, 47, 32, 20],
755
+ [58, 35, 24, 15]
756
+ ],
757
+ [
758
+ [187, 114, 78, 48],
759
+ [149, 90, 62, 38],
760
+ [111, 67, 46, 28],
761
+ [82, 50, 34, 21]
762
+ ],
763
+ [
764
+ [255, 154, 106, 65],
765
+ [202, 122, 84, 52],
766
+ [144, 87, 60, 37],
767
+ [106, 64, 44, 27]
768
+ ],
769
+ [
770
+ [322, 195, 134, 82],
771
+ [255, 154, 106, 65],
772
+ [178, 108, 74, 45],
773
+ [139, 84, 58, 36]
774
+ ],
775
+ [
776
+ [370, 224, 154, 95],
777
+ [293, 178, 122, 75],
778
+ [207, 125, 86, 53],
779
+ [154, 93, 64, 39]
780
+ ],
781
+ [
782
+ [461, 279, 192, 118],
783
+ [365, 221, 152, 93],
784
+ [259, 157, 108, 66],
785
+ [202, 122, 84, 52]
786
+ ],
787
+ [
788
+ [552, 335, 230, 141],
789
+ [432, 262, 180, 111],
790
+ [312, 189, 130, 80],
791
+ [235, 143, 98, 60]
792
+ ],
793
+ [
794
+ [652, 395, 271, 167],
795
+ [513, 311, 213, 131],
796
+ [364, 221, 151, 93],
797
+ [288, 174, 119, 74]
798
+ ]
799
+ ];
800
+ /**
801
+ * @internal
802
+ */
803
+ // eslint-disable-next-line unicorn/prefer-math-trunc
804
+ static _G15 = (1 << 10) | (1 << 8) | (1 << 5) | (1 << 4) | (1 << 2) | (1 << 1) | (1 << 0);
805
+ /**
806
+ * @internal
807
+ */
808
+ static _G18 =
809
+ // eslint-disable-next-line unicorn/prefer-math-trunc
810
+ (1 << 12) | (1 << 11) | (1 << 10) | (1 << 9) | (1 << 8) | (1 << 5) | (1 << 2) | (1 << 0);
811
+ /**
812
+ * @internal
813
+ */
814
+ static _G15_MASK = (1 << 14) | (1 << 12) | (1 << 10) | (1 << 4) | (1 << 1);
815
+ /**
816
+ * Get the pattern position for the given type number.
817
+ * @param typeNumber The type number to get the pattern for.
818
+ * @returns The pattern for the type number.
819
+ */
820
+ static getPatternPosition(typeNumber) {
821
+ return QRHelper._PATTERN_POSITION_TABLE[typeNumber - 1];
822
+ }
823
+ /**
824
+ * Get the max length of the data.
825
+ * @param typeNumber The type number to get the max length for.
826
+ * @param mode The data mode to get data max length for.
827
+ * @param errorCorrectLevel The error correction to get the max length for.
828
+ * @returns The max length.
829
+ * @throws Error with unknown correction level.
830
+ */
831
+ static getMaxLength(typeNumber, mode, errorCorrectLevel) {
832
+ const t = typeNumber - 1;
833
+ let e = 0;
834
+ let m = 0;
835
+ switch (errorCorrectLevel) {
836
+ case ErrorCorrectLevel.L:
837
+ e = 0;
838
+ break;
839
+ case ErrorCorrectLevel.M:
840
+ e = 1;
841
+ break;
842
+ case ErrorCorrectLevel.Q:
843
+ e = 2;
844
+ break;
845
+ case ErrorCorrectLevel.H:
846
+ e = 3;
847
+ break;
848
+ default:
849
+ throw new core.GeneralError(QRHelper._CLASS_NAME, "correctionLevelRange", { errorCorrectLevel });
850
+ }
851
+ switch (mode) {
852
+ case QRDataMode.Number:
853
+ m = 0;
854
+ break;
855
+ case QRDataMode.AlphaNumeric:
856
+ m = 1;
857
+ break;
858
+ case QRDataMode.Byte8:
859
+ m = 2;
860
+ break;
861
+ default:
862
+ throw new core.GeneralError(QRHelper._CLASS_NAME, "modeRange", { mode });
863
+ }
864
+ return QRHelper._MAX_LENGTH[t][e][m];
865
+ }
866
+ /**
867
+ * Get the error correction polynomial for the error correct to length.
868
+ * @param errorCorrectLength The error correction length to get the polynomial for.
869
+ * @returns The polynomial for the error correction length.
870
+ */
871
+ static getErrorCorrectPolynomial(errorCorrectLength) {
872
+ let a = new Polynomial([1]);
873
+ for (let i = 0; i < errorCorrectLength; i++) {
874
+ a = a.multiply(new Polynomial([1, MathHelper.gExp(i)]));
875
+ }
876
+ return a;
877
+ }
878
+ /**
879
+ * Get the mask method for the given pattern.
880
+ * @param maskPattern The pattern to get the mask for.
881
+ * @returns The mask method for the pattern.
882
+ * @throws Error with invalid mask.
883
+ */
884
+ static getMaskMethod(maskPattern) {
885
+ switch (maskPattern) {
886
+ case MaskPattern.PATTERN000:
887
+ return (i, j) => (i + j) % 2 === 0;
888
+ case MaskPattern.PATTERN001:
889
+ return (i, j) => i % 2 === 0;
890
+ case MaskPattern.PATTERN010:
891
+ return (i, j) => j % 3 === 0;
892
+ case MaskPattern.PATTERN011:
893
+ return (i, j) => (i + j) % 3 === 0;
894
+ case MaskPattern.PATTERN100:
895
+ return (i, j) => (~~(i / 2) + ~~(j / 3)) % 2 === 0;
896
+ case MaskPattern.PATTERN101:
897
+ return (i, j) => ((i * j) % 2) + ((i * j) % 3) === 0;
898
+ case MaskPattern.PATTERN110:
899
+ return (i, j) => (((i * j) % 2) + ((i * j) % 3)) % 2 === 0;
900
+ case MaskPattern.PATTERN111:
901
+ return (i, j) => (((i * j) % 3) + ((i + j) % 2)) % 2 === 0;
902
+ default:
903
+ throw new core.GeneralError(QRHelper._CLASS_NAME, "maskPatternRange", { maskPattern });
904
+ }
905
+ }
906
+ /**
907
+ * Get the BCH type info.
908
+ * @param data The data to get the BCH type info for.
909
+ * @returns The type info.
910
+ */
911
+ static getBCHTypeInfo(data) {
912
+ let d = data << 10;
913
+ while (QRHelper.getBCHDigit(d) - QRHelper.getBCHDigit(QRHelper._G15) >= 0) {
914
+ d ^= QRHelper._G15 << (QRHelper.getBCHDigit(d) - QRHelper.getBCHDigit(QRHelper._G15));
915
+ }
916
+ return ((data << 10) | d) ^ QRHelper._G15_MASK;
917
+ }
918
+ /**
919
+ * Get the BCH type number.
920
+ * @param data The data to get the BCH type number for.
921
+ * @returns The type number.
922
+ */
923
+ static getBCHTypeNumber(data) {
924
+ let d = data << 12;
925
+ while (QRHelper.getBCHDigit(d) - QRHelper.getBCHDigit(QRHelper._G18) >= 0) {
926
+ d ^= QRHelper._G18 << (QRHelper.getBCHDigit(d) - QRHelper.getBCHDigit(QRHelper._G18));
927
+ }
928
+ return (data << 12) | d;
929
+ }
930
+ /**
931
+ * @internal
932
+ */
933
+ static getBCHDigit(data) {
934
+ let localData = data;
935
+ let digit = 0;
936
+ while (localData !== 0) {
937
+ digit++;
938
+ localData >>>= 1;
939
+ }
940
+ return digit;
941
+ }
942
+ }
943
+
944
+ // Copyright 2024 IOTA Stiftung.
945
+ // SPDX-License-Identifier: Apache-2.0.
946
+ /* eslint-disable no-mixed-operators */
947
+ /**
948
+ * Class to represent a RS Block.
949
+ * Based on https://github.com/kazuhikoarase/qrcode-generator/ .
950
+ */
951
+ class RSBlock {
952
+ /**
953
+ * Runtime name for the class.
954
+ * @internal
955
+ */
956
+ static _CLASS_NAME = "RSBlock";
957
+ /**
958
+ * @internal
959
+ */
960
+ static _RS_BLOCK_TABLE = [
961
+ // L
962
+ // M
963
+ // Q
964
+ // H
965
+ // 1
966
+ [1, 26, 19],
967
+ [1, 26, 16],
968
+ [1, 26, 13],
969
+ [1, 26, 9],
970
+ // 2
971
+ [1, 44, 34],
972
+ [1, 44, 28],
973
+ [1, 44, 22],
974
+ [1, 44, 16],
975
+ // 3
976
+ [1, 70, 55],
977
+ [1, 70, 44],
978
+ [2, 35, 17],
979
+ [2, 35, 13],
980
+ // 4
981
+ [1, 100, 80],
982
+ [2, 50, 32],
983
+ [2, 50, 24],
984
+ [4, 25, 9],
985
+ // 5
986
+ [1, 134, 108],
987
+ [2, 67, 43],
988
+ [2, 33, 15, 2, 34, 16],
989
+ [2, 33, 11, 2, 34, 12],
990
+ // 6
991
+ [2, 86, 68],
992
+ [4, 43, 27],
993
+ [4, 43, 19],
994
+ [4, 43, 15],
995
+ // 7
996
+ [2, 98, 78],
997
+ [4, 49, 31],
998
+ [2, 32, 14, 4, 33, 15],
999
+ [4, 39, 13, 1, 40, 14],
1000
+ // 8
1001
+ [2, 121, 97],
1002
+ [2, 60, 38, 2, 61, 39],
1003
+ [4, 40, 18, 2, 41, 19],
1004
+ [4, 40, 14, 2, 41, 15],
1005
+ // 9
1006
+ [2, 146, 116],
1007
+ [3, 58, 36, 2, 59, 37],
1008
+ [4, 36, 16, 4, 37, 17],
1009
+ [4, 36, 12, 4, 37, 13],
1010
+ // 10
1011
+ [2, 86, 68, 2, 87, 69],
1012
+ [4, 69, 43, 1, 70, 44],
1013
+ [6, 43, 19, 2, 44, 20],
1014
+ [6, 43, 15, 2, 44, 16],
1015
+ // 11
1016
+ [4, 101, 81],
1017
+ [1, 80, 50, 4, 81, 51],
1018
+ [4, 50, 22, 4, 51, 23],
1019
+ [3, 36, 12, 8, 37, 13],
1020
+ // 12
1021
+ [2, 116, 92, 2, 117, 93],
1022
+ [6, 58, 36, 2, 59, 37],
1023
+ [4, 46, 20, 6, 47, 21],
1024
+ [7, 42, 14, 4, 43, 15],
1025
+ // 13
1026
+ [4, 133, 107],
1027
+ [8, 59, 37, 1, 60, 38],
1028
+ [8, 44, 20, 4, 45, 21],
1029
+ [12, 33, 11, 4, 34, 12],
1030
+ // 14
1031
+ [3, 145, 115, 1, 146, 116],
1032
+ [4, 64, 40, 5, 65, 41],
1033
+ [11, 36, 16, 5, 37, 17],
1034
+ [11, 36, 12, 5, 37, 13],
1035
+ // 15
1036
+ [5, 109, 87, 1, 110, 88],
1037
+ [5, 65, 41, 5, 66, 42],
1038
+ [5, 54, 24, 7, 55, 25],
1039
+ [11, 36, 12, 7, 37, 13],
1040
+ // 16
1041
+ [5, 122, 98, 1, 123, 99],
1042
+ [7, 73, 45, 3, 74, 46],
1043
+ [15, 43, 19, 2, 44, 20],
1044
+ [3, 45, 15, 13, 46, 16],
1045
+ // 17
1046
+ [1, 135, 107, 5, 136, 108],
1047
+ [10, 74, 46, 1, 75, 47],
1048
+ [1, 50, 22, 15, 51, 23],
1049
+ [2, 42, 14, 17, 43, 15],
1050
+ // 18
1051
+ [5, 150, 120, 1, 151, 121],
1052
+ [9, 69, 43, 4, 70, 44],
1053
+ [17, 50, 22, 1, 51, 23],
1054
+ [2, 42, 14, 19, 43, 15],
1055
+ // 19
1056
+ [3, 141, 113, 4, 142, 114],
1057
+ [3, 70, 44, 11, 71, 45],
1058
+ [17, 47, 21, 4, 48, 22],
1059
+ [9, 39, 13, 16, 40, 14],
1060
+ // 20
1061
+ [3, 135, 107, 5, 136, 108],
1062
+ [3, 67, 41, 13, 68, 42],
1063
+ [15, 54, 24, 5, 55, 25],
1064
+ [15, 43, 15, 10, 44, 16],
1065
+ // 21
1066
+ [4, 144, 116, 4, 145, 117],
1067
+ [17, 68, 42],
1068
+ [17, 50, 22, 6, 51, 23],
1069
+ [19, 46, 16, 6, 47, 17],
1070
+ // 22
1071
+ [2, 139, 111, 7, 140, 112],
1072
+ [17, 74, 46],
1073
+ [7, 54, 24, 16, 55, 25],
1074
+ [34, 37, 13],
1075
+ // 23
1076
+ [4, 151, 121, 5, 152, 122],
1077
+ [4, 75, 47, 14, 76, 48],
1078
+ [11, 54, 24, 14, 55, 25],
1079
+ [16, 45, 15, 14, 46, 16],
1080
+ // 24
1081
+ [6, 147, 117, 4, 148, 118],
1082
+ [6, 73, 45, 14, 74, 46],
1083
+ [11, 54, 24, 16, 55, 25],
1084
+ [30, 46, 16, 2, 47, 17],
1085
+ // 25
1086
+ [8, 132, 106, 4, 133, 107],
1087
+ [8, 75, 47, 13, 76, 48],
1088
+ [7, 54, 24, 22, 55, 25],
1089
+ [22, 45, 15, 13, 46, 16],
1090
+ // 26
1091
+ [10, 142, 114, 2, 143, 115],
1092
+ [19, 74, 46, 4, 75, 47],
1093
+ [28, 50, 22, 6, 51, 23],
1094
+ [33, 46, 16, 4, 47, 17],
1095
+ // 27
1096
+ [8, 152, 122, 4, 153, 123],
1097
+ [22, 73, 45, 3, 74, 46],
1098
+ [8, 53, 23, 26, 54, 24],
1099
+ [12, 45, 15, 28, 46, 16],
1100
+ // 28
1101
+ [3, 147, 117, 10, 148, 118],
1102
+ [3, 73, 45, 23, 74, 46],
1103
+ [4, 54, 24, 31, 55, 25],
1104
+ [11, 45, 15, 31, 46, 16],
1105
+ // 29
1106
+ [7, 146, 116, 7, 147, 117],
1107
+ [21, 73, 45, 7, 74, 46],
1108
+ [1, 53, 23, 37, 54, 24],
1109
+ [19, 45, 15, 26, 46, 16],
1110
+ // 30
1111
+ [5, 145, 115, 10, 146, 116],
1112
+ [19, 75, 47, 10, 76, 48],
1113
+ [15, 54, 24, 25, 55, 25],
1114
+ [23, 45, 15, 25, 46, 16],
1115
+ // 31
1116
+ [13, 145, 115, 3, 146, 116],
1117
+ [2, 74, 46, 29, 75, 47],
1118
+ [42, 54, 24, 1, 55, 25],
1119
+ [23, 45, 15, 28, 46, 16],
1120
+ // 32
1121
+ [17, 145, 115],
1122
+ [10, 74, 46, 23, 75, 47],
1123
+ [10, 54, 24, 35, 55, 25],
1124
+ [19, 45, 15, 35, 46, 16],
1125
+ // 33
1126
+ [17, 145, 115, 1, 146, 116],
1127
+ [14, 74, 46, 21, 75, 47],
1128
+ [29, 54, 24, 19, 55, 25],
1129
+ [11, 45, 15, 46, 46, 16],
1130
+ // 34
1131
+ [13, 145, 115, 6, 146, 116],
1132
+ [14, 74, 46, 23, 75, 47],
1133
+ [44, 54, 24, 7, 55, 25],
1134
+ [59, 46, 16, 1, 47, 17],
1135
+ // 35
1136
+ [12, 151, 121, 7, 152, 122],
1137
+ [12, 75, 47, 26, 76, 48],
1138
+ [39, 54, 24, 14, 55, 25],
1139
+ [22, 45, 15, 41, 46, 16],
1140
+ // 36
1141
+ [6, 151, 121, 14, 152, 122],
1142
+ [6, 75, 47, 34, 76, 48],
1143
+ [46, 54, 24, 10, 55, 25],
1144
+ [2, 45, 15, 64, 46, 16],
1145
+ // 37
1146
+ [17, 152, 122, 4, 153, 123],
1147
+ [29, 74, 46, 14, 75, 47],
1148
+ [49, 54, 24, 10, 55, 25],
1149
+ [24, 45, 15, 46, 46, 16],
1150
+ // 38
1151
+ [4, 152, 122, 18, 153, 123],
1152
+ [13, 74, 46, 32, 75, 47],
1153
+ [48, 54, 24, 14, 55, 25],
1154
+ [42, 45, 15, 32, 46, 16],
1155
+ // 39
1156
+ [20, 147, 117, 4, 148, 118],
1157
+ [40, 75, 47, 7, 76, 48],
1158
+ [43, 54, 24, 22, 55, 25],
1159
+ [10, 45, 15, 67, 46, 16],
1160
+ // 40
1161
+ [19, 148, 118, 6, 149, 119],
1162
+ [18, 75, 47, 31, 76, 48],
1163
+ [34, 54, 24, 34, 55, 25],
1164
+ [20, 45, 15, 61, 46, 16]
1165
+ ];
1166
+ /**
1167
+ * @internal
1168
+ */
1169
+ _totalCount;
1170
+ /**
1171
+ * @internal
1172
+ */
1173
+ _dataCount;
1174
+ /**
1175
+ * Create a new instance of RSBlock.
1176
+ * @param totalCount The total count for the block.
1177
+ * @param dataCount The data count for the block.
1178
+ */
1179
+ constructor(totalCount, dataCount) {
1180
+ this._totalCount = totalCount;
1181
+ this._dataCount = dataCount;
1182
+ }
1183
+ /**
1184
+ * Get RS Blocks for the type number and error correct level.
1185
+ * @param typeNumber The type number.
1186
+ * @param errorCorrectLevel The error correct level.
1187
+ * @returns The RS Blocks.
1188
+ */
1189
+ static getRSBlocks(typeNumber, errorCorrectLevel) {
1190
+ const rsBlock = RSBlock.getRsBlockTable(typeNumber, errorCorrectLevel);
1191
+ const length = rsBlock.length / 3;
1192
+ const list = [];
1193
+ for (let i = 0; i < length; i++) {
1194
+ const count = rsBlock[i * 3 + 0];
1195
+ const totalCount = rsBlock[i * 3 + 1];
1196
+ const dataCount = rsBlock[i * 3 + 2];
1197
+ for (let j = 0; j < count; j++) {
1198
+ list.push(new RSBlock(totalCount, dataCount));
1199
+ }
1200
+ }
1201
+ return list;
1202
+ }
1203
+ /**
1204
+ * @internal
1205
+ */
1206
+ static getRsBlockTable(typeNumber, errorCorrectLevel) {
1207
+ switch (errorCorrectLevel) {
1208
+ case ErrorCorrectLevel.L:
1209
+ return RSBlock._RS_BLOCK_TABLE[(typeNumber - 1) * 4 + 0];
1210
+ case ErrorCorrectLevel.M:
1211
+ return RSBlock._RS_BLOCK_TABLE[(typeNumber - 1) * 4 + 1];
1212
+ case ErrorCorrectLevel.Q:
1213
+ return RSBlock._RS_BLOCK_TABLE[(typeNumber - 1) * 4 + 2];
1214
+ case ErrorCorrectLevel.H:
1215
+ return RSBlock._RS_BLOCK_TABLE[(typeNumber - 1) * 4 + 3];
1216
+ }
1217
+ throw new core.GeneralError(RSBlock._CLASS_NAME, "correctionLevelRange", { errorCorrectLevel });
1218
+ }
1219
+ /**
1220
+ * Get the data count of the block.
1221
+ * @returns The data count.
1222
+ */
1223
+ getDataCount() {
1224
+ return this._dataCount;
1225
+ }
1226
+ /**
1227
+ * Get the total count of blocks.
1228
+ * @returns The total count.
1229
+ */
1230
+ getTotalCount() {
1231
+ return this._totalCount;
1232
+ }
1233
+ }
1234
+
1235
+ // Copyright 2024 IOTA Stiftung.
1236
+ // SPDX-License-Identifier: Apache-2.0.
1237
+ /* eslint-disable no-bitwise */
1238
+ /* eslint-disable no-continue */
1239
+ /* eslint-disable no-mixed-operators */
1240
+ /* eslint-disable unicorn/prefer-math-trunc */
1241
+ /**
1242
+ * Class to generates QR codes from data.
1243
+ * Based on https://github.com/kazuhikoarase/qrcode-generator/ .
1244
+ */
1245
+ class QR {
1246
+ /**
1247
+ * Runtime name for the class.
1248
+ * @internal
1249
+ */
1250
+ static _CLASS_NAME = "QR";
1251
+ /**
1252
+ * @internal
1253
+ */
1254
+ static _PAD0 = 0xec;
1255
+ /**
1256
+ * @internal
1257
+ */
1258
+ static _PAD1 = 0x11;
1259
+ /**
1260
+ * @internal
1261
+ */
1262
+ _typeNumber;
1263
+ /**
1264
+ * @internal
1265
+ */
1266
+ _errorCorrectLevel;
1267
+ /**
1268
+ * @internal
1269
+ */
1270
+ _qrData;
1271
+ /**
1272
+ * @internal
1273
+ */
1274
+ _modules;
1275
+ /**
1276
+ * @internal
1277
+ */
1278
+ _moduleCount;
1279
+ /**
1280
+ * Create a new instance of QR.
1281
+ * @param typeNumber 0 to 40, 0 means autodetect.
1282
+ * @param errorCorrectLevel 'L','M','Q','H'.
1283
+ * @throws Error if the typeNumber is invalid.
1284
+ */
1285
+ constructor(typeNumber = 6, errorCorrectLevel = ErrorCorrectLevel.L) {
1286
+ if (!core.Is.integer(typeNumber) || typeNumber < 0 || typeNumber > 40) {
1287
+ throw new core.GeneralError(QR._CLASS_NAME, "typeNumberRange", { typeNumber });
1288
+ }
1289
+ this._typeNumber = typeNumber;
1290
+ this._errorCorrectLevel = errorCorrectLevel;
1291
+ this._qrData = [];
1292
+ this._moduleCount = 0;
1293
+ this._modules = [];
1294
+ MathHelper.initialize();
1295
+ }
1296
+ /**
1297
+ * Add text data to the QR Code.
1298
+ * @param qrData The data to add.
1299
+ */
1300
+ addText(qrData) {
1301
+ this._qrData.push(new QRByte8(qrData));
1302
+ }
1303
+ /**
1304
+ * Add number to the QR Code.
1305
+ * @param qrData The data to add.
1306
+ */
1307
+ addNumber(qrData) {
1308
+ this._qrData.push(new QRNumber(qrData));
1309
+ }
1310
+ /**
1311
+ * Add alpha numeric to the QR Code.
1312
+ * @param qrData The data to add.
1313
+ */
1314
+ addAlphaNumeric(qrData) {
1315
+ this._qrData.push(new QRAlphaNumeric(qrData));
1316
+ }
1317
+ /**
1318
+ * Generate the display buffer.
1319
+ * @returns Boolean buffer of pixels.
1320
+ */
1321
+ generate() {
1322
+ this.autoDetectTypeNumber();
1323
+ this.makeImpl(false, this.getBestMaskPattern());
1324
+ const pixels = [];
1325
+ for (let y = 0; y < this._moduleCount; y++) {
1326
+ for (let x = 0; x < this._moduleCount; x++) {
1327
+ pixels[x] = pixels[x] || [];
1328
+ pixels[x][y] = this.isDark(y, x);
1329
+ }
1330
+ }
1331
+ return pixels;
1332
+ }
1333
+ /**
1334
+ * @internal
1335
+ */
1336
+ isDark(row, col) {
1337
+ if (this._modules[row][col] !== null) {
1338
+ return this._modules[row][col];
1339
+ }
1340
+ return false;
1341
+ }
1342
+ /**
1343
+ * @internal
1344
+ */
1345
+ getBestMaskPattern() {
1346
+ let minLostPoint = 0;
1347
+ let pattern = 0;
1348
+ for (let i = 0; i < 8; i++) {
1349
+ this.makeImpl(true, i);
1350
+ const lostPoint = this.getLostPoint();
1351
+ if (i === 0 || minLostPoint > lostPoint) {
1352
+ minLostPoint = lostPoint;
1353
+ pattern = i;
1354
+ }
1355
+ }
1356
+ return pattern;
1357
+ }
1358
+ /**
1359
+ * @internal
1360
+ */
1361
+ makeImpl(test, maskPattern) {
1362
+ this._moduleCount = this._typeNumber * 4 + 17;
1363
+ this._modules = [];
1364
+ for (let i = 0; i < this._moduleCount; i++) {
1365
+ this._modules.push([]);
1366
+ for (let j = 0; j < this._moduleCount; j++) {
1367
+ this._modules[i].push(null);
1368
+ }
1369
+ }
1370
+ this.setupPositionProbePattern(0, 0);
1371
+ this.setupPositionProbePattern(this._moduleCount - 7, 0);
1372
+ this.setupPositionProbePattern(0, this._moduleCount - 7);
1373
+ this.setupPositionAdjustPattern();
1374
+ this.setupTimingPattern();
1375
+ this.setupTypeInfo(test, maskPattern);
1376
+ if (this._typeNumber >= 7) {
1377
+ this.setupTypeNumber(test);
1378
+ }
1379
+ const data = this.createData();
1380
+ this.mapData(data, maskPattern);
1381
+ }
1382
+ /**
1383
+ * @internal
1384
+ */
1385
+ mapData(data, maskPattern) {
1386
+ let inc = -1;
1387
+ let row = this._moduleCount - 1;
1388
+ let bitIndex = 7;
1389
+ let byteIndex = 0;
1390
+ const maskFunc = QRHelper.getMaskMethod(maskPattern);
1391
+ for (let col = this._moduleCount - 1; col > 0; col -= 2) {
1392
+ if (col === 6) {
1393
+ col -= 1;
1394
+ }
1395
+ let flag = true;
1396
+ while (flag) {
1397
+ for (let c = 0; c < 2; c++) {
1398
+ if (this._modules[row][col - c] === null) {
1399
+ let dark = false;
1400
+ if (byteIndex < data.length) {
1401
+ dark = ((data[byteIndex] >>> bitIndex) & 1) === 1;
1402
+ }
1403
+ const mask = maskFunc(row, col - c);
1404
+ if (mask) {
1405
+ dark = !dark;
1406
+ }
1407
+ this._modules[row][col - c] = dark;
1408
+ bitIndex -= 1;
1409
+ if (bitIndex === -1) {
1410
+ byteIndex++;
1411
+ bitIndex = 7;
1412
+ }
1413
+ }
1414
+ }
1415
+ row += inc;
1416
+ if (row < 0 || this._moduleCount <= row) {
1417
+ row -= inc;
1418
+ inc = -inc;
1419
+ flag = false;
1420
+ }
1421
+ }
1422
+ }
1423
+ }
1424
+ /**
1425
+ * @internal
1426
+ */
1427
+ setupPositionAdjustPattern() {
1428
+ const pos = QRHelper.getPatternPosition(this._typeNumber);
1429
+ for (let i = 0; i < pos.length; i++) {
1430
+ for (let j = 0; j < pos.length; j++) {
1431
+ const row = pos[i];
1432
+ const col = pos[j];
1433
+ if (this._modules[row][col] !== null) {
1434
+ continue;
1435
+ }
1436
+ for (let r = -2; r <= 2; r++) {
1437
+ for (let c = -2; c <= 2; c++) {
1438
+ if (r === -2 || r === 2 || c === -2 || c === 2 || (r === 0 && c === 0)) {
1439
+ this._modules[row + r][col + c] = true;
1440
+ }
1441
+ else {
1442
+ this._modules[row + r][col + c] = false;
1443
+ }
1444
+ }
1445
+ }
1446
+ }
1447
+ }
1448
+ }
1449
+ /**
1450
+ * @internal
1451
+ */
1452
+ setupPositionProbePattern(row, col) {
1453
+ for (let r = -1; r <= 7; r++) {
1454
+ for (let c = -1; c <= 7; c++) {
1455
+ if (row + r <= -1 ||
1456
+ this._moduleCount <= row + r ||
1457
+ col + c <= -1 ||
1458
+ this._moduleCount <= col + c) {
1459
+ continue;
1460
+ }
1461
+ if ((r >= 0 && r <= 6 && (c === 0 || c === 6)) ||
1462
+ (c >= 0 && c <= 6 && (r === 0 || r === 6)) ||
1463
+ (r >= 2 && r <= 4 && c >= 2 && c <= 4)) {
1464
+ this._modules[row + r][col + c] = true;
1465
+ }
1466
+ else {
1467
+ this._modules[row + r][col + c] = false;
1468
+ }
1469
+ }
1470
+ }
1471
+ }
1472
+ /**
1473
+ * @internal
1474
+ */
1475
+ setupTimingPattern() {
1476
+ for (let r = 8; r < this._moduleCount - 8; r++) {
1477
+ if (this._modules[r][6] !== null) {
1478
+ continue;
1479
+ }
1480
+ this._modules[r][6] = r % 2 === 0;
1481
+ }
1482
+ for (let c = 8; c < this._moduleCount - 8; c++) {
1483
+ if (this._modules[6][c] !== null) {
1484
+ continue;
1485
+ }
1486
+ this._modules[6][c] = c % 2 === 0;
1487
+ }
1488
+ }
1489
+ /**
1490
+ * @internal
1491
+ */
1492
+ setupTypeNumber(test) {
1493
+ const bits = QRHelper.getBCHTypeNumber(this._typeNumber);
1494
+ for (let i = 0; i < 18; i++) {
1495
+ this._modules[~~(i / 3)][(i % 3) + this._moduleCount - 8 - 3] =
1496
+ !test && ((bits >> i) & 1) === 1;
1497
+ }
1498
+ for (let i = 0; i < 18; i++) {
1499
+ this._modules[(i % 3) + this._moduleCount - 8 - 3][~~(i / 3)] =
1500
+ !test && ((bits >> i) & 1) === 1;
1501
+ }
1502
+ }
1503
+ /**
1504
+ * @internal
1505
+ */
1506
+ setupTypeInfo(test, maskPattern) {
1507
+ const data = (this._errorCorrectLevel << 3) | maskPattern;
1508
+ const bits = QRHelper.getBCHTypeInfo(data);
1509
+ // vertical
1510
+ for (let i = 0; i < 15; i++) {
1511
+ const mod = !test && ((bits >> i) & 1) === 1;
1512
+ if (i < 6) {
1513
+ this._modules[i][8] = mod;
1514
+ }
1515
+ else if (i < 8) {
1516
+ this._modules[i + 1][8] = mod;
1517
+ }
1518
+ else {
1519
+ this._modules[this._moduleCount - 15 + i][8] = mod;
1520
+ }
1521
+ }
1522
+ // horizontal
1523
+ for (let i = 0; i < 15; i++) {
1524
+ const mod = !test && ((bits >> i) & 1) === 1;
1525
+ if (i < 8) {
1526
+ this._modules[8][this._moduleCount - i - 1] = mod;
1527
+ }
1528
+ else if (i < 9) {
1529
+ this._modules[8][15 - i - 1 + 1] = mod;
1530
+ }
1531
+ else {
1532
+ this._modules[8][15 - i - 1] = mod;
1533
+ }
1534
+ }
1535
+ // fixed
1536
+ this._modules[this._moduleCount - 8][8] = !test;
1537
+ }
1538
+ /**
1539
+ * @internal
1540
+ */
1541
+ getLostPoint() {
1542
+ const moduleCount = this._moduleCount;
1543
+ let lostPoint = 0;
1544
+ // LEVEL1
1545
+ for (let row = 0; row < moduleCount; row++) {
1546
+ for (let col = 0; col < moduleCount; col++) {
1547
+ let sameCount = 0;
1548
+ const dark = this.isDark(row, col);
1549
+ for (let r = -1; r <= 1; r++) {
1550
+ if (row + r < 0 || moduleCount <= row + r) {
1551
+ continue;
1552
+ }
1553
+ for (let c = -1; c <= 1; c++) {
1554
+ if (col + c < 0 || moduleCount <= col + c) {
1555
+ continue;
1556
+ }
1557
+ if (r === 0 && c === 0) {
1558
+ continue;
1559
+ }
1560
+ if (dark === this.isDark(row + r, col + c)) {
1561
+ sameCount++;
1562
+ }
1563
+ }
1564
+ }
1565
+ if (sameCount > 5) {
1566
+ lostPoint += 3 + sameCount - 5;
1567
+ }
1568
+ }
1569
+ }
1570
+ // LEVEL2
1571
+ for (let row = 0; row < moduleCount - 1; row++) {
1572
+ for (let col = 0; col < moduleCount - 1; col++) {
1573
+ let count = 0;
1574
+ if (this.isDark(row, col)) {
1575
+ count++;
1576
+ }
1577
+ if (this.isDark(row + 1, col)) {
1578
+ count++;
1579
+ }
1580
+ if (this.isDark(row, col + 1)) {
1581
+ count++;
1582
+ }
1583
+ if (this.isDark(row + 1, col + 1)) {
1584
+ count++;
1585
+ }
1586
+ if (count === 0 || count === 4) {
1587
+ lostPoint += 3;
1588
+ }
1589
+ }
1590
+ }
1591
+ // LEVEL3
1592
+ for (let row = 0; row < moduleCount; row++) {
1593
+ for (let col = 0; col < moduleCount - 6; col++) {
1594
+ if (this.isDark(row, col) &&
1595
+ !this.isDark(row, col + 1) &&
1596
+ this.isDark(row, col + 2) &&
1597
+ this.isDark(row, col + 3) &&
1598
+ this.isDark(row, col + 4) &&
1599
+ !this.isDark(row, col + 5) &&
1600
+ this.isDark(row, col + 6)) {
1601
+ lostPoint += 40;
1602
+ }
1603
+ }
1604
+ }
1605
+ for (let col = 0; col < moduleCount; col++) {
1606
+ for (let row = 0; row < moduleCount - 6; row++) {
1607
+ if (this.isDark(row, col) &&
1608
+ !this.isDark(row + 1, col) &&
1609
+ this.isDark(row + 2, col) &&
1610
+ this.isDark(row + 3, col) &&
1611
+ this.isDark(row + 4, col) &&
1612
+ !this.isDark(row + 5, col) &&
1613
+ this.isDark(row + 6, col)) {
1614
+ lostPoint += 40;
1615
+ }
1616
+ }
1617
+ }
1618
+ // LEVEL4
1619
+ let darkCount = 0;
1620
+ for (let col = 0; col < moduleCount; col++) {
1621
+ for (let row = 0; row < moduleCount; row++) {
1622
+ if (this.isDark(row, col)) {
1623
+ darkCount++;
1624
+ }
1625
+ }
1626
+ }
1627
+ const ratio = Math.abs((100 * darkCount) / moduleCount / moduleCount - 50) / 5;
1628
+ lostPoint += ratio * 10;
1629
+ return lostPoint;
1630
+ }
1631
+ /**
1632
+ * @internal
1633
+ */
1634
+ createData() {
1635
+ const rsBlocks = RSBlock.getRSBlocks(this._typeNumber, this._errorCorrectLevel);
1636
+ const buffer = new BitBuffer();
1637
+ for (let i = 0; i < this._qrData.length; i++) {
1638
+ const data = this._qrData[i];
1639
+ buffer.put(data.getMode(), 4);
1640
+ buffer.put(data.getLength(), data.getLengthInBits(this._typeNumber));
1641
+ data.write(buffer);
1642
+ }
1643
+ // calc max data count
1644
+ let totalDataCount = 0;
1645
+ for (let i = 0; i < rsBlocks.length; i++) {
1646
+ totalDataCount += rsBlocks[i].getDataCount();
1647
+ }
1648
+ if (buffer.getLengthInBits() > totalDataCount * 8) {
1649
+ throw new core.GeneralError(QR._CLASS_NAME, "dataOverflow", {
1650
+ lengthInBits: buffer.getLengthInBits(),
1651
+ totalDataCount,
1652
+ typeNumber: this._typeNumber
1653
+ });
1654
+ }
1655
+ // end
1656
+ if (buffer.getLengthInBits() + 4 <= totalDataCount * 8) {
1657
+ buffer.put(0, 4);
1658
+ }
1659
+ // padding
1660
+ while (buffer.getLengthInBits() % 8 !== 0) {
1661
+ buffer.putBit(false);
1662
+ }
1663
+ // padding
1664
+ let flag = true;
1665
+ while (flag) {
1666
+ if (buffer.getLengthInBits() >= totalDataCount * 8) {
1667
+ break;
1668
+ }
1669
+ buffer.put(QR._PAD0, 8);
1670
+ if (buffer.getLengthInBits() >= totalDataCount * 8) {
1671
+ flag = false;
1672
+ }
1673
+ else {
1674
+ buffer.put(QR._PAD1, 8);
1675
+ }
1676
+ }
1677
+ return this.createBytes(buffer, rsBlocks);
1678
+ }
1679
+ /**
1680
+ * @internal
1681
+ */
1682
+ createBytes(buffer, rsBlocks) {
1683
+ let offset = 0;
1684
+ let maxDcCount = 0;
1685
+ let maxEcCount = 0;
1686
+ const dcData = [];
1687
+ const ecData = [];
1688
+ for (let r = 0; r < rsBlocks.length; r++) {
1689
+ dcData.push([]);
1690
+ ecData.push([]);
1691
+ }
1692
+ for (let r = 0; r < rsBlocks.length; r++) {
1693
+ const dcCount = rsBlocks[r].getDataCount();
1694
+ const ecCount = rsBlocks[r].getTotalCount() - dcCount;
1695
+ maxDcCount = Math.max(maxDcCount, dcCount);
1696
+ maxEcCount = Math.max(maxEcCount, ecCount);
1697
+ dcData[r] = this.createNumArray(dcCount);
1698
+ for (let i = 0; i < dcData[r].length; i++) {
1699
+ dcData[r][i] = 0xff & buffer.getBuffer()[i + offset];
1700
+ }
1701
+ offset += dcCount;
1702
+ const rsPoly = QRHelper.getErrorCorrectPolynomial(ecCount);
1703
+ const rawPoly = new Polynomial(dcData[r], rsPoly.getLength() - 1);
1704
+ const modPoly = rawPoly.mod(rsPoly);
1705
+ ecData[r] = this.createNumArray(rsPoly.getLength() - 1);
1706
+ for (let i = 0; i < ecData[r].length; i++) {
1707
+ const modIndex = i + modPoly.getLength() - ecData[r].length;
1708
+ ecData[r][i] = modIndex >= 0 ? modPoly.getAt(modIndex) : 0;
1709
+ }
1710
+ }
1711
+ let totalCodeCount = 0;
1712
+ for (let i = 0; i < rsBlocks.length; i++) {
1713
+ totalCodeCount += rsBlocks[i].getTotalCount();
1714
+ }
1715
+ const data = this.createNumArray(totalCodeCount);
1716
+ let index = 0;
1717
+ for (let i = 0; i < maxDcCount; i++) {
1718
+ for (let r = 0; r < rsBlocks.length; r++) {
1719
+ if (i < dcData[r].length) {
1720
+ data[index] = dcData[r][i];
1721
+ index++;
1722
+ }
1723
+ }
1724
+ }
1725
+ for (let i = 0; i < maxEcCount; i++) {
1726
+ for (let r = 0; r < rsBlocks.length; r++) {
1727
+ if (i < ecData[r].length) {
1728
+ data[index] = ecData[r][i];
1729
+ index++;
1730
+ }
1731
+ }
1732
+ }
1733
+ return data;
1734
+ }
1735
+ /**
1736
+ * @internal
1737
+ */
1738
+ createNumArray(len) {
1739
+ const a = [];
1740
+ for (let i = 0; i < len; i++) {
1741
+ a.push(0);
1742
+ }
1743
+ return a;
1744
+ }
1745
+ /**
1746
+ * @internal
1747
+ */
1748
+ autoDetectTypeNumber() {
1749
+ if (this._typeNumber === 0) {
1750
+ for (let typeNumber = 1; typeNumber <= 40; typeNumber++) {
1751
+ const rsBlocks = RSBlock.getRSBlocks(typeNumber, this._errorCorrectLevel);
1752
+ const buffer = new BitBuffer();
1753
+ for (let i = 0; i < this._qrData.length; i++) {
1754
+ const data = this._qrData[i];
1755
+ buffer.put(data.getMode(), 4);
1756
+ buffer.put(data.getLength(), data.getLengthInBits(typeNumber));
1757
+ data.write(buffer);
1758
+ }
1759
+ let totalDataCount = 0;
1760
+ for (let i = 0; i < rsBlocks.length; i++) {
1761
+ totalDataCount += rsBlocks[i].getDataCount();
1762
+ }
1763
+ if (buffer.getLengthInBits() <= totalDataCount * 8) {
1764
+ this._typeNumber = typeNumber;
1765
+ break;
1766
+ }
1767
+ if (typeNumber === 40) {
1768
+ throw new core.GeneralError(QR._CLASS_NAME, "typeNumberOverflow", {
1769
+ lengthInBits: buffer.getLengthInBits(),
1770
+ totalDataCount
1771
+ });
1772
+ }
1773
+ }
1774
+ }
1775
+ }
1776
+ }
1777
+
1778
+ // Copyright 2024 IOTA Stiftung.
1779
+ // SPDX-License-Identifier: Apache-2.0.
1780
+ /* eslint-disable no-mixed-operators */
1781
+ /**
1782
+ * Class to render qr data as jpeg.
1783
+ */
1784
+ class JpegRenderer {
1785
+ /**
1786
+ * Runtime name for the class.
1787
+ * @internal
1788
+ */
1789
+ static _CLASS_NAME = "JpegRenderer";
1790
+ /**
1791
+ * Render the QR code data as a bitmap.
1792
+ * @param cellData The cell data for the QR code.
1793
+ * @param options The options for rendering.
1794
+ * @returns The bitmap content.
1795
+ */
1796
+ static async render(cellData, options) {
1797
+ core.Guards.array(JpegRenderer._CLASS_NAME, "cellData", cellData);
1798
+ options = options ?? {};
1799
+ options.cellSize = options.cellSize ?? 5;
1800
+ options.marginSize = options.marginSize ?? 10;
1801
+ core.Guards.number("IRendererOptions", "options.cellSize", options?.cellSize);
1802
+ core.Guards.number("IRendererOptions", "options.marginSize", options?.marginSize);
1803
+ if (options?.cellSize <= 0) {
1804
+ throw new core.GeneralError("IRendererOptions", "cellSizeZero", {
1805
+ cellSize: options?.cellSize
1806
+ });
1807
+ }
1808
+ if (options?.marginSize <= 0) {
1809
+ throw new core.GeneralError("IRendererOptions", "marginSizeZero", {
1810
+ marginSize: options?.marginSize
1811
+ });
1812
+ }
1813
+ const background = image.Color.coerce(options?.background) ?? image.Color.fromHex("#FFFFFF");
1814
+ const foreground = image.Color.coerce(options?.foreground) ?? image.Color.fromHex("#000000");
1815
+ const dimensions = cellData.length * options?.cellSize + 2 * options?.marginSize;
1816
+ const data = new Uint8Array(dimensions * dimensions * 4);
1817
+ for (let i = 0; i < data.length; i += 4) {
1818
+ data[i] = background.red();
1819
+ data[i + 1] = background.green();
1820
+ data[i + 2] = background.blue();
1821
+ data[i + 3] = 0xff;
1822
+ }
1823
+ let dc = options?.marginSize * dimensions * 4;
1824
+ for (let x = 0; x < cellData.length; x++) {
1825
+ const row = new Uint8Array(dimensions * 4);
1826
+ let r = 0;
1827
+ for (let i = 0; i < options?.marginSize; i++) {
1828
+ row[r++] = background.red();
1829
+ row[r++] = background.green();
1830
+ row[r++] = background.blue();
1831
+ row[r++] = 0xff;
1832
+ }
1833
+ for (let y = 0; y < cellData[x].length; y++) {
1834
+ const colour = cellData[y][x] ? foreground : background;
1835
+ for (let c = 0; c < options.cellSize; c++) {
1836
+ row[r++] = colour.red();
1837
+ row[r++] = colour.green();
1838
+ row[r++] = colour.blue();
1839
+ row[r++] = 0xff;
1840
+ }
1841
+ }
1842
+ for (let i = 0; i < options.marginSize; i++) {
1843
+ row[r++] = background.red();
1844
+ row[r++] = background.green();
1845
+ row[r++] = background.blue();
1846
+ row[r++] = 0xff;
1847
+ }
1848
+ for (let c = 0; c < options.cellSize; c++) {
1849
+ data.set(row, dc);
1850
+ dc += row.length;
1851
+ }
1852
+ }
1853
+ return new image.JpegEncoder().encode(dimensions, dimensions, data, 75);
1854
+ }
1855
+ }
1856
+
1857
+ // Copyright 2024 IOTA Stiftung.
1858
+ // SPDX-License-Identifier: Apache-2.0.
1859
+ /* eslint-disable no-mixed-operators */
1860
+ /**
1861
+ * Class to render qr data as png.
1862
+ */
1863
+ class PngRenderer {
1864
+ /**
1865
+ * Runtime name for the class.
1866
+ * @internal
1867
+ */
1868
+ static _CLASS_NAME = "PngRenderer";
1869
+ /**
1870
+ * Render the QR code data as a bitmap.
1871
+ * @param cellData The cell data for the QR code.
1872
+ * @param options The options for rendering.
1873
+ * @returns The bitmap content.
1874
+ */
1875
+ static async render(cellData, options) {
1876
+ core.Guards.array(PngRenderer._CLASS_NAME, "cellData", cellData);
1877
+ options = options ?? {};
1878
+ options.cellSize = options.cellSize ?? 5;
1879
+ options.marginSize = options.marginSize ?? 10;
1880
+ core.Guards.number("IRendererOptions", "options.cellSize", options?.cellSize);
1881
+ core.Guards.number("IRendererOptions", "options.marginSize", options?.marginSize);
1882
+ if (options?.cellSize <= 0) {
1883
+ throw new core.GeneralError("IRendererOptions", "cellSizeZero", {
1884
+ cellSize: options?.cellSize
1885
+ });
1886
+ }
1887
+ if (options?.marginSize <= 0) {
1888
+ throw new core.GeneralError("IRendererOptions", "marginSizeZero", {
1889
+ marginSize: options?.marginSize
1890
+ });
1891
+ }
1892
+ const background = image.Color.coerce(options?.background) ?? image.Color.fromHex("#FFFFFF");
1893
+ const foreground = image.Color.coerce(options?.foreground) ?? image.Color.fromHex("#000000");
1894
+ const dimensions = cellData.length * options?.cellSize + 2 * options?.marginSize;
1895
+ const data = new Uint8Array(dimensions * dimensions * 4);
1896
+ for (let i = 0; i < data.length; i += 4) {
1897
+ data[i] = background.red();
1898
+ data[i + 1] = background.green();
1899
+ data[i + 2] = background.blue();
1900
+ data[i + 3] = background.alpha();
1901
+ }
1902
+ let dc = options?.marginSize * dimensions * 4;
1903
+ for (let x = 0; x < cellData.length; x++) {
1904
+ const row = new Uint8Array(dimensions * 4);
1905
+ let r = 0;
1906
+ for (let i = 0; i < options?.marginSize; i++) {
1907
+ row[r++] = background.red();
1908
+ row[r++] = background.green();
1909
+ row[r++] = background.blue();
1910
+ row[r++] = background.alpha();
1911
+ }
1912
+ for (let y = 0; y < cellData[x].length; y++) {
1913
+ const colour = cellData[y][x] ? foreground : background;
1914
+ for (let c = 0; c < options?.cellSize; c++) {
1915
+ row[r++] = colour.red();
1916
+ row[r++] = colour.green();
1917
+ row[r++] = colour.blue();
1918
+ row[r++] = colour.alpha();
1919
+ }
1920
+ }
1921
+ for (let i = 0; i < options?.marginSize; i++) {
1922
+ row[r++] = background.red();
1923
+ row[r++] = background.green();
1924
+ row[r++] = background.blue();
1925
+ row[r++] = background.alpha();
1926
+ }
1927
+ for (let c = 0; c < options?.cellSize; c++) {
1928
+ data.set(row, dc);
1929
+ dc += row.length;
1930
+ }
1931
+ }
1932
+ return new image.PngEncoder().encode([data.buffer], dimensions, dimensions);
1933
+ }
1934
+ }
1935
+
1936
+ // Copyright 2024 IOTA Stiftung.
1937
+ // SPDX-License-Identifier: Apache-2.0.
1938
+ /* eslint-disable no-mixed-operators */
1939
+ /**
1940
+ * Class to render qr data as text.
1941
+ */
1942
+ class TextRenderer {
1943
+ /**
1944
+ * Runtime name for the class.
1945
+ * @internal
1946
+ */
1947
+ static _CLASS_NAME = "TextRenderer";
1948
+ /**
1949
+ * Render the QR code data as text.
1950
+ * @param cellData The cell data for the QR code.
1951
+ * @param options The options for rendering.
1952
+ * @returns The text content.
1953
+ */
1954
+ static async render(cellData, options) {
1955
+ core.Guards.array(TextRenderer._CLASS_NAME, "cellData", cellData);
1956
+ options = options ?? {};
1957
+ options.cellSize = options.cellSize ?? 1;
1958
+ options.marginSize = options.marginSize ?? 2;
1959
+ core.Guards.number("IRendererOptions", "options.cellSize", options?.cellSize);
1960
+ core.Guards.number("IRendererOptions", "options.marginSize", options?.marginSize);
1961
+ if (options?.cellSize <= 0) {
1962
+ throw new core.GeneralError("IRendererOptions", "cellSizeZero", {
1963
+ cellSize: options?.cellSize
1964
+ });
1965
+ }
1966
+ if (options?.marginSize <= 0) {
1967
+ throw new core.GeneralError("IRendererOptions", "marginSizeZero", {
1968
+ marginSize: options?.marginSize
1969
+ });
1970
+ }
1971
+ options.onChar = options.onChar ?? "██";
1972
+ options.offChar = options.offChar ?? " ";
1973
+ const marginSize = options?.marginSize;
1974
+ const cellSize = options?.cellSize;
1975
+ let text = "";
1976
+ for (let m = 0; m < marginSize; m++) {
1977
+ text += `${options.offChar.repeat(cellSize * cellData.length)}\r\n`;
1978
+ }
1979
+ for (let x = 0; x < cellData.length; x++) {
1980
+ let line = options.offChar.repeat(marginSize);
1981
+ for (let y = 0; y < cellData[x].length; y++) {
1982
+ if (cellData[y][x]) {
1983
+ line += options.onChar.repeat(cellSize);
1984
+ }
1985
+ else {
1986
+ line += options.offChar.repeat(cellSize);
1987
+ }
1988
+ }
1989
+ line += options.offChar.repeat(marginSize);
1990
+ line += "\r\n";
1991
+ for (let c = 0; c < cellSize; c++) {
1992
+ text += line;
1993
+ }
1994
+ }
1995
+ for (let m = 0; m < marginSize; m++) {
1996
+ text += `${options.offChar.repeat(cellSize * cellData.length)}\r\n`;
1997
+ }
1998
+ return text;
1999
+ }
2000
+ }
2001
+
2002
+ exports.ErrorCorrectLevel = ErrorCorrectLevel;
2003
+ exports.JpegRenderer = JpegRenderer;
2004
+ exports.PngRenderer = PngRenderer;
2005
+ exports.QR = QR;
2006
+ exports.TextRenderer = TextRenderer;