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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980 |
x2
x2
x13
x13
x2
x2
x13
x13
x13
x13
x13
x1
x1
x21
x441
x208
x208
x441
x21
x1
x1
x13
x1
x25
x25
x25
x625
x625
x25
x25
x1
x1
x13
x10
x10
x302
x302
x10498
x10498
x302
x10
x10
x13
x13
x2
x13
x13
x13
x13
x65
x65
x65
x64
x64
x9
x9
x64
x64
x65
x12
x12
x12
x65
x1
x1
x65
x13
x36
x36
x31
x31
x36
x12
x13
x11
x11
x11
x11
x12
x12
x12
x13
x131
x131
x12
x13
x466
x466
x12
x13
x13
x13
x13
x2
x12
x12
x12
x12
x12
x12
x12
x12
x12
x12
x12
x12
x12
x12
x96
x96
x96
x96
x38
x38
x38
x96
x96
x12
x12
x12
x12
x12
x2
x6893
x6893
x6893
x2
x11564
x11564
x2
x12
x12
x12
x12
x12
x12
x12
x12
x2
x2
x12
x304
x304
x304
x12
x12
x12
x12
x12
x9
x21
x9
x9
x21
x9
x12
x12
x12
x2
x36
x324
x2916
x2916
x2916
x2916
x2304
x2304
x2916
x324
x36
x2
x9
x45
x225
x225
x45
x9
x2
x120
x120
x120
x1200
x1200
x120
x120
x720
x720
x120
x120
x120
x120
x720
x720
x120
x960
x960
x120
x840
x840
x120
x120
x2
x12
x11
x11
x1
x12
x12
x12
x1
x12
x18
x18
x18
x18
x18
x18
x12
x2
x12
x12
x12
x12
x12
x12
x16
x16
x16
x16
x12
x12
x16
x16
x12
x12
x432
x756
x744
x744
x432
x432
x432
x12
x2
x12
x146
x12
x12
x146
x4410
x8820
x8820
x8820
x5952
x5952
x5952
x8820
x4410
x146
x12
x2
x204
x5168
x155108
x155108
x155108
x19130
x19130
x155108
x21969
x21969
x155108
x18248
x18248
x155108
x18248
x18248
x155108
x18248
x18248
x155108
x20453
x20453
x155108
x18689
x18689
x155108
x20123
x20123
x155108
x155108
x47594
x47594
x155108
x5168
x204
x2
x96
x96
x2432
x2432
x2432
x2432
x72992
x37070
x37070
x3044
x37070
x3756
x3756
x72992
x35922
x35922
x18767
x18767
x35922
x35922
x35922
x72992
x2432
x2432
x96
x2432
x2432
x2432
x2432
x72992
x37400
x37400
x2963
x37400
x3903
x3903
x72992
x35592
x35592
x18564
x18564
x35592
x35592
x35592
x72992
x2432
x2432
x96
x2336
x68224
x68224
x7949
x7949
x68224
x2336
x96
x96
x2432
x2432
x96
x96
x96
x96
x96
x2
x42195
x42195
x42195
x42195
x2
x4864
x3148
x3148
x3148
x4864
x4864
x4864
x4864
x2
x79526
x4864
x4864
x79526
x79526
x79526
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x2
x1
x1
x1
x1
x1
x1
x1
x1
x2
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x2
x9
x9
x2
x10
x10
x11306
x11306
x10
x10
x2
x13
x1
x1
x12
x13
x1
x13
x1
x13
x1
x13
x9
x13
x13
x2
x12
x12
x12
x12
x2
x12
x2
x2
x75
x75
x2
x75
x75
x2
x2
x12
x12
x232
x4664
x4664
x4432
x4432
x4664
x232
x232
x12
x12
x2
x16
x16
x392
x392
x392
x392
x16
x16
x2
x15192
x15192
x121536
x121536
x121536
x15192
x15192
x2
x9570
x9570
x2
x11486
x91728
x91728
x11486 |
|
const encoder = new TextEncoder()
export function qrcode(content: string | URL, options: { output: "svg" } & Pick<options, "border" | "light" | "dark" | "ecl">): string
export function qrcode(content: string | URL | Uint8Array, options: { output: "console" } & Pick<options, "light" | "dark" | "ecl">): void
export function qrcode(content: string | URL | Uint8Array, options?: { output?: "array" } & Pick<options, "ecl">): boolean[][]
export function qrcode(content: string | URL | Uint8Array, options?: { output?: string } & options) {
return QrCode.from(content, options)
}
export type options = {
border?: number
light?: string
dark?: string
ecl?: "LOW" | "MEDIUM" | "QUARTILE" | "HIGH"
}
class QrCode {
static from(content: string | URL | Uint8Array, { output = "array", border = 2, light = "white", dark = "black", ecl = "MEDIUM" }: { output?: string } & options = {}) {
border = Math.max(0, border)
const qr = QrCode.#encode(Segment.from(content instanceof URL ? content.href : content), { ecl })
const size = qr.size + border * 2
switch (output) {
case "svg": {
const paths = [] as string[]
for (let y = 0; y < qr.size; y++) {
for (let x = 0; x < qr.size; x++) {
if (qr.get({ x, y })) {
paths.push(`M${x + border},${y + border}h1v1h-1z`)
}
}
}
return `<?xml version="1.0" encoding="UTF-8"?><svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 ${size} ${size}" stroke="none"><rect width="100%" height="100%" fill="${light}"/><path d="${paths.join(" ")}" fill="${dark}"/></svg>`
}
case "console": {
for (let y = 0; y < size; y++) {
const line = "%c\u2588\u2588".repeat(size)
const colors = [] as string[]
for (let x = 0; x < size; x++) {
colors.push(`color: ${qr.get({ x: x - border, y: y - border }) ? dark : light}`)
}
console.log(line, ...colors)
}
return
}
default: {
const data = [] as boolean[][]
for (let y = 0; y < size; y++) {
data[y] = new Array(size).fill(false)
for (let x = 0; x < size; x++) {
data[y][x] = qr.get({ x: x - border, y: y - border })
}
}
return data
}
}
}
static #encode(segments: Readonly<Segment[]>, { ecl }: { ecl: keyof typeof QrCode.ERROR_CORRECTION_LEVEL }) {
const ECL = QrCode.ERROR_CORRECTION_LEVEL[ecl]
let version = 1
let databits = 0
for (;; version++) {
const capacity = 8 * (Math.floor(QrCode.#DATA_BITS[version] / 8) - ECL.ECC_PER_BLOCK[version] * ECL.ECC_BLOCKS[version])
let used = 0
for (const segment of segments) {
const width = segment.width(version)
if (segment.length >= (1 << width)) {
used = Infinity
}
used += 4 + width + segment.data.length
}
if (used <= capacity) {
databits = used
break
}
if (version >= QrCode.#VERSION_MAX) {
throw new RangeError("Data too long")
}
}
for (const level of ["MEDIUM", "QUARTILE", "HIGH"] as const) {
const ECL = QrCode.ERROR_CORRECTION_LEVEL[level]
if (databits <= 8 * (Math.floor(QrCode.#DATA_BITS[version] / 8) - ECL.ECC_PER_BLOCK[version] * ECL.ECC_BLOCKS[version])) {
ecl = level
}
}
const bits = [] as number[]
for (const segment of segments) {
append({ bits, length: 4, value: segment.mode.id })
append({ bits, length: segment.width(version), value: segment.length })
bits.push(...segment.data)
}
const capacity = 8 * (Math.floor(QrCode.#DATA_BITS[version] / 8) - ECL.ECC_PER_BLOCK[version] * ECL.ECC_BLOCKS[version])
append({ bits, length: Math.min(4, capacity - bits.length), value: 0 })
append({ bits, length: (8 - bits.length % 8) % 8, value: 0 })
for (let padding = 0xEC; bits.length < capacity; padding ^= 0xEC ^ 0x11) {
append({ bits, length: 8, value: padding })
}
const data = [] as number[]
while (data.length * 8 < bits.length) {
data.push(0)
}
bits.forEach((b, i) => data[i >>> 3] |= b << (7 - (i & 7)))
const mode = ["", "numeric", "alphanumeric", "", "bytes"][segments[0]?.mode.id] ?? ""
const length = segments.reduce((sum, segment) => sum + segment.length, 0)
return new QrCode({ version, ecl, data, mode, length, databits })
}
private constructor({ version, ecl, data, mode, length, databits }: { version: number; ecl: keyof typeof QrCode.ERROR_CORRECTION_LEVEL; data: Readonly<number[]>; mode: string; length: number; databits: number }) {
this.version = version
this.size = this.version * 4 + 17
this.ecl = ecl
this.mode = mode
this.length = length
this.databits = databits
this.#ecl = QrCode.ERROR_CORRECTION_LEVEL[ecl]
this.#modules = new Array(this.size).fill(null).map(() => new Array(this.size).fill(false)) as boolean[][]
this.#functions = new Array(this.size).fill(null).map(() => new Array(this.size).fill(false)) as boolean[][]
this.#drawPatterns()
this.#drawData(this.#interleave(data))
let mask = 0
let min = Infinity
for (let i = 0; i < 8; i++) {
this.#mask({ mask: i })
this.#drawFormat({ mask: i })
const penalty = this.#penalty()
if (penalty < min) {
mask = i
min = penalty
}
this.#mask({ mask: i })
}
this.mask = mask
this.#mask(this)
this.#drawFormat(this)
this.#functions.length = 0
}
#set({ x, y, color }: { x: number; y: number; color: boolean }) {
this.#modules[y][x] = color
this.#functions[y][x] = true
}
get({ x, y }: { x: number; y: number }) {
return (0 <= x) && (x < this.size) && (0 <= y) && (y < this.size) && (this.#modules[y][x])
}
readonly mode
readonly length
readonly databits
readonly version
readonly size
readonly ecl
readonly #ecl
readonly mask
readonly #modules
readonly #functions
#drawPatterns() {
for (let i = 0; i < this.size; i++) {
this.#set({ x: 6, y: i, color: !(i % 2) })
this.#set({ x: i, y: 6, color: !(i % 2) })
}
this.#drawFinder({ x: 3, y: 3 })
this.#drawFinder({ x: this.size - 4, y: 3 })
this.#drawFinder({ x: 3, y: this.size - 4 })
const alignments = QrCode.#ALIGNEMENTS[this.version]
for (let i = 0; i < alignments.length; i++) {
for (let j = 0; j < alignments.length; j++) {
if (!(((i === 0) && (j === 0)) || ((i === 0) && (j === alignments.length - 1)) || ((i === alignments.length - 1) && (j === 0)))) {
this.#drawAlignment({ x: alignments[i], y: alignments[j] })
}
}
}
this.#drawFormat()
this.#drawVersion()
}
#drawFinder({ x: ox, y: oy }: { x: number; y: number }) {
for (let dy = -4; dy <= 4; dy++) {
for (let dx = -4; dx <= 4; dx++) {
const d = Math.max(Math.abs(dx), Math.abs(dy))
const x = ox + dx
const y = oy + dy
if ((0 <= x) && (x < this.size) && (0 <= y) && (y < this.size)) {
this.#set({ x: x, y: y, color: (d !== 2) && (d !== 4) })
}
}
}
}
#drawAlignment({ x, y }: { x: number; y: number }) {
for (let dy = -2; dy <= 2; dy++) {
for (let dx = -2; dx <= 2; dx++) {
this.#set({ x: x + dx, y: y + dy, color: Math.max(Math.abs(dx), Math.abs(dy)) !== 1 })
}
}
}
#drawFormat({ mask = 0 } = {}) {
const data = this.#ecl.format << 3 | mask
let rem = data
for (let i = 0; i < 10; i++) {
rem = (rem << 1) ^ ((rem >>> 9) * 0x537)
}
const bits = (data << 10 | rem) ^ 0x5412
for (let i = 0; i <= 5; i++) {
this.#set({ x: 8, y: i, color: bit(bits, i) })
}
this.#set({ x: 8, y: 7, color: bit(bits, 6) })
this.#set({ x: 8, y: 8, color: bit(bits, 7) })
this.#set({ x: 7, y: 8, color: bit(bits, 8) })
for (let i = 9; i < 15; i++) {
this.#set({ x: 14 - i, y: 8, color: bit(bits, i) })
}
for (let i = 0; i < 8; i++) {
this.#set({ x: this.size - 1 - i, y: 8, color: bit(bits, i) })
}
for (let i = 8; i < 15; i++) {
this.#set({ x: 8, y: this.size - 15 + i, color: bit(bits, i) })
}
this.#set({ x: 8, y: this.size - 8, color: true })
}
#drawVersion() {
if (this.version < 7) {
return
}
let rem = this.version
for (let i = 0; i < 12; i++) {
rem = (rem << 1) ^ ((rem >>> 11) * 0x1F25)
}
const bits = this.version << 12 | rem
for (let i = 0; i < 18; i++) {
const color = bit(bits, i)
const a = this.size - 11 + i % 3
const b = Math.floor(i / 3)
this.#set({ x: a, y: b, color })
this.#set({ x: b, y: a, color })
}
}
#interleave(data: Readonly<number[]>) {
const ecc = { blocks: this.#ecl.ECC_BLOCKS[this.version], length: this.#ecl.ECC_PER_BLOCK[this.version] }
const codewords = Math.floor(QrCode.#DATA_BITS[this.version] / 8)
const short = { blocks: ecc.blocks - codewords % ecc.blocks, length: Math.floor(codewords / ecc.blocks) }
const blocks = [] as number[][]
const divisor = divisorReedSolomon(ecc.length)
for (let i = 0, k = 0; i < ecc.blocks; i++) {
const block = data.slice(k, k + short.length - ecc.length + (i < short.blocks ? 0 : 1))
k += block.length
const remainder = remainderReedSolomon(block, divisor)
if (i < short.blocks) {
block.push(0)
}
blocks.push(block.concat(remainder))
}
const result = [] as number[]
for (let i = 0; i < blocks[0].length; i++) {
blocks.forEach((block, j) => {
if ((i !== short.length - ecc.length) || (j >= short.blocks)) {
result.push(block[i])
}
})
}
return result
}
#drawData(data: Readonly<number[]>) {
for (let i = 0, h = this.size - 1; h >= 1; h -= 2) {
if (h === 6) {
h = 5
}
for (let v = 0; v < this.size; v++) {
for (let j = 0; j < 2; j++) {
const x = h - j
const y = !((h + 1) & 2) ? this.size - 1 - v : v
if ((!this.#functions[y][x]) && (i < data.length * 8)) {
this.#modules[y][x] = bit(data[i >>> 3], 7 - (i & 7))
i++
}
}
}
}
}
#mask({ mask }: { mask: number }) {
for (let y = 0; y < this.size; y++) {
for (let x = 0; x < this.size; x++) {
let invert = false
switch (mask) {
case 0:
invert = !((x + y) % 2)
break
case 1:
invert = !(y % 2)
break
case 2:
invert = !(x % 3)
break
case 3:
invert = !((x + y) % 3)
break
case 4:
invert = !((Math.floor(x / 3) + Math.floor(y / 2)) % 2)
break
case 5:
invert = !(x * y % 2 + x * y % 3)
break
case 6:
invert = !((x * y % 2 + x * y % 3) % 2)
break
case 7:
invert = !(((x + y) % 2 + x * y % 3) % 2)
break
}
if (invert && (!this.#functions[y][x])) {
this.#modules[y][x] = !this.#modules[y][x]
}
}
}
}
#penalty() {
let result = 0
for (let y = 0; y < this.size; y++) {
let color = false
let xy = 0
const history = [0, 0, 0, 0, 0, 0, 0]
for (let x = 0; x < this.size; x++) {
if (this.#modules[y][x] === color) {
xy++
if (xy === 5) {
result += QrCode.#PENALTY[0]
} else if (xy > 5) {
result++
}
} else {
this.#penaltyRegister({ xy, history })
if (!color) {
result += this.#penaltyPatterns({ history }) * QrCode.#PENALTY[2]
}
color = this.#modules[y][x]
xy = 1
}
}
result += this.#penaltyCount({ xy, color, history }) * QrCode.#PENALTY[2]
}
for (let x = 0; x < this.size; x++) {
let color = false
let xy = 0
const history = [0, 0, 0, 0, 0, 0, 0]
for (let y = 0; y < this.size; y++) {
if (this.#modules[y][x] === color) {
xy++
if (xy === 5) {
result += QrCode.#PENALTY[0]
} else if (xy > 5) {
result++
}
} else {
this.#penaltyRegister({ xy, history })
if (!color) {
result += this.#penaltyPatterns({ history }) * QrCode.#PENALTY[2]
}
color = this.#modules[y][x]
xy = 1
}
}
result += this.#penaltyCount({ xy, color, history }) * QrCode.#PENALTY[2]
}
for (let y = 0; y < this.size - 1; y++) {
for (let x = 0; x < this.size - 1; x++) {
const color = this.#modules[y][x]
if ((color === this.#modules[y][x + 1]) && (color === this.#modules[y + 1][x]) && (color === this.#modules[y + 1][x + 1])) {
result += QrCode.#PENALTY[1]
}
}
}
let dark = 0
for (const row of this.#modules) {
dark = row.reduce((sum, color) => sum + (color ? 1 : 0), dark)
}
const total = this.size * this.size
const k = Math.ceil(Math.abs(dark * 20 - total * 10) / total) - 1
result += k * QrCode.#PENALTY[3]
return result
}
#penaltyPatterns({ history }: { history: number[] }) {
const n = history[1]
const core = (n > 0) && (history[2] === n) && (history[3] === n * 3) && (history[4] === n) && (history[5] === n)
return ((core && (history[0] >= n * 4) && (history[6] >= n)) ? 1 : 0) + ((core && (history[6] >= n * 4) && (history[0] >= n)) ? 1 : 0)
}
#penaltyCount({ xy, color, history }: { xy: number; color: boolean; history: number[] }) {
if (color) {
this.#penaltyRegister({ xy, history })
xy = 0
}
xy += this.size
this.#penaltyRegister({ xy, history })
return this.#penaltyPatterns({ history })
}
#penaltyRegister({ xy, history }: { xy: number; history: number[] }) {
if (history[0] === 0) {
xy += this.size
}
history.pop()
history.unshift(xy)
}
static readonly #VERSION_MIN = 1
static readonly #VERSION_MAX = 40
static readonly ERROR_CORRECTION_LEVEL = {
LOW: {
ECC_PER_BLOCK: [NaN, 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],
ECC_BLOCKS: [NaN, 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],
format: 1,
},
MEDIUM: {
ECC_PER_BLOCK: [NaN, 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],
ECC_BLOCKS: [NaN, 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],
format: 0,
},
QUARTILE: {
ECC_PER_BLOCK: [NaN, 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],
ECC_BLOCKS: [NaN, 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],
format: 3,
},
HIGH: {
ECC_PER_BLOCK: [NaN, 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],
ECC_BLOCKS: [NaN, 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],
format: 2,
},
}
static readonly #ALIGNEMENTS = [
[],
[],
[6, 18],
[6, 22],
[6, 26],
[6, 30],
[6, 34],
[6, 22, 38],
[6, 24, 42],
[6, 26, 46],
[6, 28, 50],
[6, 30, 54],
[6, 32, 58],
[6, 34, 62],
[6, 26, 46, 66],
[6, 26, 48, 70],
[6, 26, 50, 74],
[6, 30, 54, 78],
[6, 30, 56, 82],
[6, 30, 58, 86],
[6, 34, 62, 90],
[6, 28, 50, 72, 94],
[6, 26, 50, 74, 98],
[6, 30, 54, 78, 102],
[6, 28, 54, 80, 106],
[6, 32, 58, 84, 110],
[6, 30, 58, 86, 114],
[6, 34, 62, 90, 118],
[6, 26, 50, 74, 98, 122],
[6, 30, 54, 78, 102, 126],
[6, 26, 52, 78, 104, 130],
[6, 30, 56, 82, 108, 134],
[6, 34, 60, 86, 112, 138],
[6, 30, 58, 86, 114, 142],
[6, 34, 62, 90, 118, 146],
[6, 30, 54, 78, 102, 126, 150],
[6, 24, 50, 76, 102, 128, 154],
[6, 28, 54, 80, 106, 132, 158],
[6, 32, 58, 84, 110, 136, 162],
[6, 26, 54, 82, 110, 138, 166],
[6, 30, 58, 86, 114, 142, 170],
]
static readonly #DATA_BITS = [
NaN,
208,
359,
567,
807,
1079,
1383,
1568,
1936,
2336,
2768,
3232,
3728,
4256,
4651,
5243,
5867,
6523,
7211,
7931,
8683,
9252,
10068,
10916,
11796,
12708,
13652,
14628,
15371,
16411,
17483,
18587,
19723,
20891,
22091,
23008,
24272,
25568,
26896,
28256,
29648,
]
static readonly #PENALTY = [3, 3, 40, 10]
}
class Segment {
static #numeric(content: string) {
const bits = [] as number[]
for (let i = 0; i < content.length;) {
const n = Math.min(content.length - i, 3)
append({ bits, length: n * 3 + 1, value: parseInt(content.substring(i, i + n), 10) })
i += n
}
return new Segment({ mode: { id: 0x1, widths: [10, 12, 14] }, length: content.length, bits })
}
static #alphanumeric(content: string) {
const charset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:"
const bits = [] as number[]
let i
for (i = 0; i + 2 <= content.length; i += 2) {
append({ bits, length: 11, value: charset.indexOf(content.charAt(i)) * 45 + charset.indexOf(content.charAt(i + 1)) })
}
if (i < content.length) {
append({ bits, length: 6, value: charset.indexOf(content.charAt(i)) })
}
return new Segment({ mode: { id: 0x2, widths: [9, 11, 13] }, length: content.length, bits })
}
static #utfbytes(content: string) {
return this.#bytes(encoder.encode(content))
}
static #bytes(content: Uint8Array) {
const bits = [] as number[]
for (const byte of content) {
append({ bits, length: 8, value: byte })
}
return new Segment({ mode: { id: 0x4, widths: [8, 16, 16] }, length: content.length, bits })
}
static from(content: string | Uint8Array) {
if (content instanceof Uint8Array) {
return [Segment.#bytes(content)]
}
switch (true) {
case !content.length:
return []
case /^[0-9]*$/.test(content):
return [Segment.#numeric(content)]
case /^[A-Z0-9 $%*+.\/:-]*$/.test(content):
return [Segment.#alphanumeric(content)]
default:
return [Segment.#utfbytes(content)]
}
}
constructor({ mode, length, bits }: { mode: { id: number; widths: [number, number, number] }; length: number; bits: number[] }) {
this.mode = mode
this.length = length
this.#bits = bits.slice()
}
readonly mode
readonly length
readonly #bits
get data() {
return this.#bits.slice()
}
width(version: number) {
return this.mode.widths[Math.floor((version + 7) / 17)]
}
}
function divisorReedSolomon(degree: number) {
const result = [...new Array(degree - 1).fill(0), 1]
for (let i = 0, root = 1; i < degree; i++) {
for (let j = 0; j < result.length; j++) {
result[j] = productReedSolomon(result[j], root)
if (j + 1 < result.length) {
result[j] ^= result[j + 1]
}
}
root = productReedSolomon(root, 0x02)
}
return result
}
function remainderReedSolomon(data: Readonly<number[]>, divisor: Readonly<number[]>) {
const result = divisor.map((_) => 0)
for (const n of data) {
const factor = n ^ result.shift()!
result.push(0)
divisor.forEach((coefficient, i) => result[i] ^= productReedSolomon(coefficient, factor))
}
return result
}
function productReedSolomon(a: number, b: number) {
let r = 0
for (let i = 7; i >= 0; i--) {
r = (r << 1) ^ ((r >>> 7) * 0x11D)
r ^= ((b >>> i) & 1) * a
}
return r
}
function bit(x: number, i: number): boolean {
return ((x >>> i) & 1) > 0
}
function append({ bits, length, value }: { bits: number[]; length: number; value: number }) {
for (let i = length - 1; i >= 0; i--) {
bits.push((value >>> i) & 1)
}
}
|