-
Notifications
You must be signed in to change notification settings - Fork 0
/
qrious.js
2364 lines (2015 loc) · 74.8 KB
/
qrious.js
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
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// downloaded from https://cdnjs.cloudflare.com/ajax/libs/qrious/4.0.2/qrious.js at 2020-03-24
/*
* QRious v4.0.2
* Copyright (C) 2017 Alasdair Mercer
* Copyright (C) 2010 Tom Zerucha
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global.QRious = factory());
}(this, (function () { 'use strict';
/*
* Copyright (C) 2017 Alasdair Mercer, !ninja
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/**
* A bare-bones constructor for surrogate prototype swapping.
*
* @private
* @constructor
*/
var Constructor = /* istanbul ignore next */ function() {};
/**
* A reference to <code>Object.prototype.hasOwnProperty</code>.
*
* @private
* @type {Function}
*/
var hasOwnProperty = Object.prototype.hasOwnProperty;
/**
* A reference to <code>Array.prototype.slice</code>.
*
* @private
* @type {Function}
*/
var slice = Array.prototype.slice;
/**
* Creates an object which inherits the given <code>prototype</code>.
*
* Optionally, the created object can be extended further with the specified <code>properties</code>.
*
* @param {Object} prototype - the prototype to be inherited by the created object
* @param {Object} [properties] - the optional properties to be extended by the created object
* @return {Object} The newly created object.
* @private
*/
function createObject(prototype, properties) {
var result;
/* istanbul ignore next */
if (typeof Object.create === 'function') {
result = Object.create(prototype);
} else {
Constructor.prototype = prototype;
result = new Constructor();
Constructor.prototype = null;
}
if (properties) {
extendObject(true, result, properties);
}
return result;
}
/**
* Extends the constructor to which this method is associated with the <code>prototype</code> and/or
* <code>statics</code> provided.
*
* If <code>name</code> is provided, it will be used as the class name and can be accessed via a special
* <code>class_</code> property on the child constructor, otherwise the class name of the super constructor will be used
* instead. The class name may also be used string representation for instances of the child constructor (via
* <code>toString</code>), but this is not applicable to the <i>lite</i> version of Nevis.
*
* If <code>constructor</code> is provided, it will be used as the constructor for the child, otherwise a simple
* constructor which only calls the super constructor will be used instead.
*
* The super constructor can be accessed via a special <code>super_</code> property on the child constructor.
*
* @param {string} [name=this.class_] - the class name to be used for the child constructor
* @param {Function} [constructor] - the constructor for the child
* @param {Object} [prototype] - the prototype properties to be defined for the child
* @param {Object} [statics] - the static properties to be defined for the child
* @return {Function} The child <code>constructor</code> provided or the one created if none was given.
* @public
*/
function extend(name, constructor, prototype, statics) {
var superConstructor = this;
if (typeof name !== 'string') {
statics = prototype;
prototype = constructor;
constructor = name;
name = null;
}
if (typeof constructor !== 'function') {
statics = prototype;
prototype = constructor;
constructor = function() {
return superConstructor.apply(this, arguments);
};
}
extendObject(false, constructor, superConstructor, statics);
constructor.prototype = createObject(superConstructor.prototype, prototype);
constructor.prototype.constructor = constructor;
constructor.class_ = name || superConstructor.class_;
constructor.super_ = superConstructor;
return constructor;
}
/**
* Extends the specified <code>target</code> object with the properties in each of the <code>sources</code> provided.
*
* if any source is <code>null</code> it will be ignored.
*
* @param {boolean} own - <code>true</code> to only copy <b>own</b> properties from <code>sources</code> onto
* <code>target</code>; otherwise <code>false</code>
* @param {Object} target - the target object which should be extended
* @param {...Object} [sources] - the source objects whose properties are to be copied onto <code>target</code>
* @return {void}
* @private
*/
function extendObject(own, target, sources) {
sources = slice.call(arguments, 2);
var property;
var source;
for (var i = 0, length = sources.length; i < length; i++) {
source = sources[i];
for (property in source) {
if (!own || hasOwnProperty.call(source, property)) {
target[property] = source[property];
}
}
}
}
var extend_1 = extend;
/**
* The base class from which all others should extend.
*
* @public
* @constructor
*/
function Nevis() {}
Nevis.class_ = 'Nevis';
Nevis.super_ = Object;
/**
* Extends the constructor to which this method is associated with the <code>prototype</code> and/or
* <code>statics</code> provided.
*
* If <code>name</code> is provided, it will be used as the class name and can be accessed via a special
* <code>class_</code> property on the child constructor, otherwise the class name of the super constructor will be used
* instead. The class name may also be used string representation for instances of the child constructor (via
* <code>toString</code>), but this is not applicable to the <i>lite</i> version of Nevis.
*
* If <code>constructor</code> is provided, it will be used as the constructor for the child, otherwise a simple
* constructor which only calls the super constructor will be used instead.
*
* The super constructor can be accessed via a special <code>super_</code> property on the child constructor.
*
* @param {string} [name=this.class_] - the class name to be used for the child constructor
* @param {Function} [constructor] - the constructor for the child
* @param {Object} [prototype] - the prototype properties to be defined for the child
* @param {Object} [statics] - the static properties to be defined for the child
* @return {Function} The child <code>constructor</code> provided or the one created if none was given.
* @public
* @static
* @memberof Nevis
*/
Nevis.extend = extend_1;
var nevis = Nevis;
var lite = nevis;
/**
* Responsible for rendering a QR code {@link Frame} on a specific type of element.
*
* A renderer may be dependant on the rendering of another element, so the ordering of their execution is important.
*
* The rendering of a element can be deferred by disabling the renderer initially, however, any attempt get the element
* from the renderer will result in it being immediately enabled and the element being rendered.
*
* @param {QRious} qrious - the {@link QRious} instance to be used
* @param {*} element - the element onto which the QR code is to be rendered
* @param {boolean} [enabled] - <code>true</code> this {@link Renderer} is enabled; otherwise <code>false</code>.
* @public
* @class
* @extends Nevis
*/
var Renderer = lite.extend(function(qrious, element, enabled) {
/**
* The {@link QRious} instance.
*
* @protected
* @type {QRious}
* @memberof Renderer#
*/
this.qrious = qrious;
/**
* The element onto which this {@link Renderer} is rendering the QR code.
*
* @protected
* @type {*}
* @memberof Renderer#
*/
this.element = element;
this.element.qrious = qrious;
/**
* Whether this {@link Renderer} is enabled.
*
* @protected
* @type {boolean}
* @memberof Renderer#
*/
this.enabled = Boolean(enabled);
}, {
/**
* Draws the specified QR code <code>frame</code> on the underlying element.
*
* Implementations of {@link Renderer} <b>must</b> override this method with their own specific logic.
*
* @param {Frame} frame - the {@link Frame} to be drawn
* @return {void}
* @protected
* @abstract
* @memberof Renderer#
*/
draw: function(frame) {},
/**
* Returns the element onto which this {@link Renderer} is rendering the QR code.
*
* If this method is called while this {@link Renderer} is disabled, it will be immediately enabled and rendered
* before the element is returned.
*
* @return {*} The element.
* @public
* @memberof Renderer#
*/
getElement: function() {
if (!this.enabled) {
this.enabled = true;
this.render();
}
return this.element;
},
/**
* Calculates the size (in pixel units) to represent an individual module within the QR code based on the
* <code>frame</code> provided.
*
* Any configured padding will be excluded from the returned size.
*
* The returned value will be at least one, even in cases where the size of the QR code does not fit its contents.
* This is done so that the inevitable clipping is handled more gracefully since this way at least something is
* displayed instead of just a blank space filled by the background color.
*
* @param {Frame} frame - the {@link Frame} from which the module size is to be derived
* @return {number} The pixel size for each module in the QR code which will be no less than one.
* @protected
* @memberof Renderer#
*/
getModuleSize: function(frame) {
var qrious = this.qrious;
var padding = qrious.padding || 0;
var pixels = Math.floor((qrious.size - (padding * 2)) / frame.width);
return Math.max(1, pixels);
},
/**
* Calculates the offset/padding (in pixel units) to be inserted before the QR code based on the <code>frame</code>
* provided.
*
* The returned value will be zero if there is no available offset or if the size of the QR code does not fit its
* contents. It will never be a negative value. This is done so that the inevitable clipping appears more naturally
* and it is not clipped from all directions.
*
* @param {Frame} frame - the {@link Frame} from which the offset is to be derived
* @return {number} The pixel offset for the QR code which will be no less than zero.
* @protected
* @memberof Renderer#
*/
getOffset: function(frame) {
var qrious = this.qrious;
var padding = qrious.padding;
if (padding != null) {
return padding;
}
var moduleSize = this.getModuleSize(frame);
var offset = Math.floor((qrious.size - (moduleSize * frame.width)) / 2);
return Math.max(0, offset);
},
/**
* Renders a QR code on the underlying element based on the <code>frame</code> provided.
*
* @param {Frame} frame - the {@link Frame} to be rendered
* @return {void}
* @public
* @memberof Renderer#
*/
render: function(frame) {
if (this.enabled) {
this.resize();
this.reset();
this.draw(frame);
}
},
/**
* Resets the underlying element, effectively clearing any previously rendered QR code.
*
* Implementations of {@link Renderer} <b>must</b> override this method with their own specific logic.
*
* @return {void}
* @protected
* @abstract
* @memberof Renderer#
*/
reset: function() {},
/**
* Ensures that the size of the underlying element matches that defined on the associated {@link QRious} instance.
*
* Implementations of {@link Renderer} <b>must</b> override this method with their own specific logic.
*
* @return {void}
* @protected
* @abstract
* @memberof Renderer#
*/
resize: function() {}
});
var Renderer_1 = Renderer;
/**
* An implementation of {@link Renderer} for working with <code>canvas</code> elements.
*
* @public
* @class
* @extends Renderer
*/
var CanvasRenderer = Renderer_1.extend({
/**
* @override
*/
draw: function(frame) {
var i, j;
var qrious = this.qrious;
var moduleSize = this.getModuleSize(frame);
var offset = this.getOffset(frame);
var context = this.element.getContext('2d');
context.fillStyle = qrious.foreground;
context.globalAlpha = qrious.foregroundAlpha;
for (i = 0; i < frame.width; i++) {
for (j = 0; j < frame.width; j++) {
if (frame.buffer[(j * frame.width) + i]) {
context.fillRect((moduleSize * i) + offset, (moduleSize * j) + offset, moduleSize, moduleSize);
}
}
}
},
/**
* @override
*/
reset: function() {
var qrious = this.qrious;
var context = this.element.getContext('2d');
var size = qrious.size;
context.lineWidth = 1;
context.clearRect(0, 0, size, size);
context.fillStyle = qrious.background;
context.globalAlpha = qrious.backgroundAlpha;
context.fillRect(0, 0, size, size);
},
/**
* @override
*/
resize: function() {
var element = this.element;
element.width = element.height = this.qrious.size;
}
});
var CanvasRenderer_1 = CanvasRenderer;
/* eslint no-multi-spaces: "off" */
/**
* Contains alignment pattern information.
*
* @public
* @class
* @extends Nevis
*/
var Alignment = lite.extend(null, {
/**
* The alignment pattern block.
*
* @public
* @static
* @type {number[]}
* @memberof Alignment
*/
BLOCK: [
0, 11, 15, 19, 23, 27, 31,
16, 18, 20, 22, 24, 26, 28, 20, 22, 24, 24, 26, 28, 28, 22, 24, 24,
26, 26, 28, 28, 24, 24, 26, 26, 26, 28, 28, 24, 26, 26, 26, 28, 28
]
});
var Alignment_1 = Alignment;
/* eslint no-multi-spaces: "off" */
/**
* Contains error correction information.
*
* @public
* @class
* @extends Nevis
*/
var ErrorCorrection = lite.extend(null, {
/**
* The error correction blocks.
*
* There are four elements per version. The first two indicate the number of blocks, then the data width, and finally
* the ECC width.
*
* @public
* @static
* @type {number[]}
* @memberof ErrorCorrection
*/
BLOCKS: [
1, 0, 19, 7, 1, 0, 16, 10, 1, 0, 13, 13, 1, 0, 9, 17,
1, 0, 34, 10, 1, 0, 28, 16, 1, 0, 22, 22, 1, 0, 16, 28,
1, 0, 55, 15, 1, 0, 44, 26, 2, 0, 17, 18, 2, 0, 13, 22,
1, 0, 80, 20, 2, 0, 32, 18, 2, 0, 24, 26, 4, 0, 9, 16,
1, 0, 108, 26, 2, 0, 43, 24, 2, 2, 15, 18, 2, 2, 11, 22,
2, 0, 68, 18, 4, 0, 27, 16, 4, 0, 19, 24, 4, 0, 15, 28,
2, 0, 78, 20, 4, 0, 31, 18, 2, 4, 14, 18, 4, 1, 13, 26,
2, 0, 97, 24, 2, 2, 38, 22, 4, 2, 18, 22, 4, 2, 14, 26,
2, 0, 116, 30, 3, 2, 36, 22, 4, 4, 16, 20, 4, 4, 12, 24,
2, 2, 68, 18, 4, 1, 43, 26, 6, 2, 19, 24, 6, 2, 15, 28,
4, 0, 81, 20, 1, 4, 50, 30, 4, 4, 22, 28, 3, 8, 12, 24,
2, 2, 92, 24, 6, 2, 36, 22, 4, 6, 20, 26, 7, 4, 14, 28,
4, 0, 107, 26, 8, 1, 37, 22, 8, 4, 20, 24, 12, 4, 11, 22,
3, 1, 115, 30, 4, 5, 40, 24, 11, 5, 16, 20, 11, 5, 12, 24,
5, 1, 87, 22, 5, 5, 41, 24, 5, 7, 24, 30, 11, 7, 12, 24,
5, 1, 98, 24, 7, 3, 45, 28, 15, 2, 19, 24, 3, 13, 15, 30,
1, 5, 107, 28, 10, 1, 46, 28, 1, 15, 22, 28, 2, 17, 14, 28,
5, 1, 120, 30, 9, 4, 43, 26, 17, 1, 22, 28, 2, 19, 14, 28,
3, 4, 113, 28, 3, 11, 44, 26, 17, 4, 21, 26, 9, 16, 13, 26,
3, 5, 107, 28, 3, 13, 41, 26, 15, 5, 24, 30, 15, 10, 15, 28,
4, 4, 116, 28, 17, 0, 42, 26, 17, 6, 22, 28, 19, 6, 16, 30,
2, 7, 111, 28, 17, 0, 46, 28, 7, 16, 24, 30, 34, 0, 13, 24,
4, 5, 121, 30, 4, 14, 47, 28, 11, 14, 24, 30, 16, 14, 15, 30,
6, 4, 117, 30, 6, 14, 45, 28, 11, 16, 24, 30, 30, 2, 16, 30,
8, 4, 106, 26, 8, 13, 47, 28, 7, 22, 24, 30, 22, 13, 15, 30,
10, 2, 114, 28, 19, 4, 46, 28, 28, 6, 22, 28, 33, 4, 16, 30,
8, 4, 122, 30, 22, 3, 45, 28, 8, 26, 23, 30, 12, 28, 15, 30,
3, 10, 117, 30, 3, 23, 45, 28, 4, 31, 24, 30, 11, 31, 15, 30,
7, 7, 116, 30, 21, 7, 45, 28, 1, 37, 23, 30, 19, 26, 15, 30,
5, 10, 115, 30, 19, 10, 47, 28, 15, 25, 24, 30, 23, 25, 15, 30,
13, 3, 115, 30, 2, 29, 46, 28, 42, 1, 24, 30, 23, 28, 15, 30,
17, 0, 115, 30, 10, 23, 46, 28, 10, 35, 24, 30, 19, 35, 15, 30,
17, 1, 115, 30, 14, 21, 46, 28, 29, 19, 24, 30, 11, 46, 15, 30,
13, 6, 115, 30, 14, 23, 46, 28, 44, 7, 24, 30, 59, 1, 16, 30,
12, 7, 121, 30, 12, 26, 47, 28, 39, 14, 24, 30, 22, 41, 15, 30,
6, 14, 121, 30, 6, 34, 47, 28, 46, 10, 24, 30, 2, 64, 15, 30,
17, 4, 122, 30, 29, 14, 46, 28, 49, 10, 24, 30, 24, 46, 15, 30,
4, 18, 122, 30, 13, 32, 46, 28, 48, 14, 24, 30, 42, 32, 15, 30,
20, 4, 117, 30, 40, 7, 47, 28, 43, 22, 24, 30, 10, 67, 15, 30,
19, 6, 118, 30, 18, 31, 47, 28, 34, 34, 24, 30, 20, 61, 15, 30
],
/**
* The final format bits with mask (level << 3 | mask).
*
* @public
* @static
* @type {number[]}
* @memberof ErrorCorrection
*/
FINAL_FORMAT: [
// L
0x77c4, 0x72f3, 0x7daa, 0x789d, 0x662f, 0x6318, 0x6c41, 0x6976,
// M
0x5412, 0x5125, 0x5e7c, 0x5b4b, 0x45f9, 0x40ce, 0x4f97, 0x4aa0,
// Q
0x355f, 0x3068, 0x3f31, 0x3a06, 0x24b4, 0x2183, 0x2eda, 0x2bed,
// H
0x1689, 0x13be, 0x1ce7, 0x19d0, 0x0762, 0x0255, 0x0d0c, 0x083b
],
/**
* A map of human-readable ECC levels.
*
* @public
* @static
* @type {Object.<string, number>}
* @memberof ErrorCorrection
*/
LEVELS: {
L: 1,
M: 2,
Q: 3,
H: 4
}
});
var ErrorCorrection_1 = ErrorCorrection;
/**
* Contains Galois field information.
*
* @public
* @class
* @extends Nevis
*/
var Galois = lite.extend(null, {
/**
* The Galois field exponent table.
*
* @public
* @static
* @type {number[]}
* @memberof Galois
*/
EXPONENT: [
0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1d, 0x3a, 0x74, 0xe8, 0xcd, 0x87, 0x13, 0x26,
0x4c, 0x98, 0x2d, 0x5a, 0xb4, 0x75, 0xea, 0xc9, 0x8f, 0x03, 0x06, 0x0c, 0x18, 0x30, 0x60, 0xc0,
0x9d, 0x27, 0x4e, 0x9c, 0x25, 0x4a, 0x94, 0x35, 0x6a, 0xd4, 0xb5, 0x77, 0xee, 0xc1, 0x9f, 0x23,
0x46, 0x8c, 0x05, 0x0a, 0x14, 0x28, 0x50, 0xa0, 0x5d, 0xba, 0x69, 0xd2, 0xb9, 0x6f, 0xde, 0xa1,
0x5f, 0xbe, 0x61, 0xc2, 0x99, 0x2f, 0x5e, 0xbc, 0x65, 0xca, 0x89, 0x0f, 0x1e, 0x3c, 0x78, 0xf0,
0xfd, 0xe7, 0xd3, 0xbb, 0x6b, 0xd6, 0xb1, 0x7f, 0xfe, 0xe1, 0xdf, 0xa3, 0x5b, 0xb6, 0x71, 0xe2,
0xd9, 0xaf, 0x43, 0x86, 0x11, 0x22, 0x44, 0x88, 0x0d, 0x1a, 0x34, 0x68, 0xd0, 0xbd, 0x67, 0xce,
0x81, 0x1f, 0x3e, 0x7c, 0xf8, 0xed, 0xc7, 0x93, 0x3b, 0x76, 0xec, 0xc5, 0x97, 0x33, 0x66, 0xcc,
0x85, 0x17, 0x2e, 0x5c, 0xb8, 0x6d, 0xda, 0xa9, 0x4f, 0x9e, 0x21, 0x42, 0x84, 0x15, 0x2a, 0x54,
0xa8, 0x4d, 0x9a, 0x29, 0x52, 0xa4, 0x55, 0xaa, 0x49, 0x92, 0x39, 0x72, 0xe4, 0xd5, 0xb7, 0x73,
0xe6, 0xd1, 0xbf, 0x63, 0xc6, 0x91, 0x3f, 0x7e, 0xfc, 0xe5, 0xd7, 0xb3, 0x7b, 0xf6, 0xf1, 0xff,
0xe3, 0xdb, 0xab, 0x4b, 0x96, 0x31, 0x62, 0xc4, 0x95, 0x37, 0x6e, 0xdc, 0xa5, 0x57, 0xae, 0x41,
0x82, 0x19, 0x32, 0x64, 0xc8, 0x8d, 0x07, 0x0e, 0x1c, 0x38, 0x70, 0xe0, 0xdd, 0xa7, 0x53, 0xa6,
0x51, 0xa2, 0x59, 0xb2, 0x79, 0xf2, 0xf9, 0xef, 0xc3, 0x9b, 0x2b, 0x56, 0xac, 0x45, 0x8a, 0x09,
0x12, 0x24, 0x48, 0x90, 0x3d, 0x7a, 0xf4, 0xf5, 0xf7, 0xf3, 0xfb, 0xeb, 0xcb, 0x8b, 0x0b, 0x16,
0x2c, 0x58, 0xb0, 0x7d, 0xfa, 0xe9, 0xcf, 0x83, 0x1b, 0x36, 0x6c, 0xd8, 0xad, 0x47, 0x8e, 0x00
],
/**
* The Galois field log table.
*
* @public
* @static
* @type {number[]}
* @memberof Galois
*/
LOG: [
0xff, 0x00, 0x01, 0x19, 0x02, 0x32, 0x1a, 0xc6, 0x03, 0xdf, 0x33, 0xee, 0x1b, 0x68, 0xc7, 0x4b,
0x04, 0x64, 0xe0, 0x0e, 0x34, 0x8d, 0xef, 0x81, 0x1c, 0xc1, 0x69, 0xf8, 0xc8, 0x08, 0x4c, 0x71,
0x05, 0x8a, 0x65, 0x2f, 0xe1, 0x24, 0x0f, 0x21, 0x35, 0x93, 0x8e, 0xda, 0xf0, 0x12, 0x82, 0x45,
0x1d, 0xb5, 0xc2, 0x7d, 0x6a, 0x27, 0xf9, 0xb9, 0xc9, 0x9a, 0x09, 0x78, 0x4d, 0xe4, 0x72, 0xa6,
0x06, 0xbf, 0x8b, 0x62, 0x66, 0xdd, 0x30, 0xfd, 0xe2, 0x98, 0x25, 0xb3, 0x10, 0x91, 0x22, 0x88,
0x36, 0xd0, 0x94, 0xce, 0x8f, 0x96, 0xdb, 0xbd, 0xf1, 0xd2, 0x13, 0x5c, 0x83, 0x38, 0x46, 0x40,
0x1e, 0x42, 0xb6, 0xa3, 0xc3, 0x48, 0x7e, 0x6e, 0x6b, 0x3a, 0x28, 0x54, 0xfa, 0x85, 0xba, 0x3d,
0xca, 0x5e, 0x9b, 0x9f, 0x0a, 0x15, 0x79, 0x2b, 0x4e, 0xd4, 0xe5, 0xac, 0x73, 0xf3, 0xa7, 0x57,
0x07, 0x70, 0xc0, 0xf7, 0x8c, 0x80, 0x63, 0x0d, 0x67, 0x4a, 0xde, 0xed, 0x31, 0xc5, 0xfe, 0x18,
0xe3, 0xa5, 0x99, 0x77, 0x26, 0xb8, 0xb4, 0x7c, 0x11, 0x44, 0x92, 0xd9, 0x23, 0x20, 0x89, 0x2e,
0x37, 0x3f, 0xd1, 0x5b, 0x95, 0xbc, 0xcf, 0xcd, 0x90, 0x87, 0x97, 0xb2, 0xdc, 0xfc, 0xbe, 0x61,
0xf2, 0x56, 0xd3, 0xab, 0x14, 0x2a, 0x5d, 0x9e, 0x84, 0x3c, 0x39, 0x53, 0x47, 0x6d, 0x41, 0xa2,
0x1f, 0x2d, 0x43, 0xd8, 0xb7, 0x7b, 0xa4, 0x76, 0xc4, 0x17, 0x49, 0xec, 0x7f, 0x0c, 0x6f, 0xf6,
0x6c, 0xa1, 0x3b, 0x52, 0x29, 0x9d, 0x55, 0xaa, 0xfb, 0x60, 0x86, 0xb1, 0xbb, 0xcc, 0x3e, 0x5a,
0xcb, 0x59, 0x5f, 0xb0, 0x9c, 0xa9, 0xa0, 0x51, 0x0b, 0xf5, 0x16, 0xeb, 0x7a, 0x75, 0x2c, 0xd7,
0x4f, 0xae, 0xd5, 0xe9, 0xe6, 0xe7, 0xad, 0xe8, 0x74, 0xd6, 0xf4, 0xea, 0xa8, 0x50, 0x58, 0xaf
]
});
var Galois_1 = Galois;
/**
* Contains version pattern information.
*
* @public
* @class
* @extends Nevis
*/
var Version = lite.extend(null, {
/**
* The version pattern block.
*
* @public
* @static
* @type {number[]}
* @memberof Version
*/
BLOCK: [
0xc94, 0x5bc, 0xa99, 0x4d3, 0xbf6, 0x762, 0x847, 0x60d, 0x928, 0xb78, 0x45d, 0xa17, 0x532,
0x9a6, 0x683, 0x8c9, 0x7ec, 0xec4, 0x1e1, 0xfab, 0x08e, 0xc1a, 0x33f, 0xd75, 0x250, 0x9d5,
0x6f0, 0x8ba, 0x79f, 0xb0b, 0x42e, 0xa64, 0x541, 0xc69
]
});
var Version_1 = Version;
/**
* Generates information for a QR code frame based on a specific value to be encoded.
*
* @param {Frame~Options} options - the options to be used
* @public
* @class
* @extends Nevis
*/
var Frame = lite.extend(function(options) {
var dataBlock, eccBlock, index, neccBlock1, neccBlock2;
var valueLength = options.value.length;
this._badness = [];
this._level = ErrorCorrection_1.LEVELS[options.level];
this._polynomial = [];
this._value = options.value;
this._version = 0;
this._stringBuffer = [];
while (this._version < 40) {
this._version++;
index = ((this._level - 1) * 4) + ((this._version - 1) * 16);
neccBlock1 = ErrorCorrection_1.BLOCKS[index++];
neccBlock2 = ErrorCorrection_1.BLOCKS[index++];
dataBlock = ErrorCorrection_1.BLOCKS[index++];
eccBlock = ErrorCorrection_1.BLOCKS[index];
index = (dataBlock * (neccBlock1 + neccBlock2)) + neccBlock2 - 3 + (this._version <= 9);
if (valueLength <= index) {
break;
}
}
this._dataBlock = dataBlock;
this._eccBlock = eccBlock;
this._neccBlock1 = neccBlock1;
this._neccBlock2 = neccBlock2;
/**
* The data width is based on version.
*
* @public
* @type {number}
* @memberof Frame#
*/
// FIXME: Ensure that it fits instead of being truncated.
var width = this.width = 17 + (4 * this._version);
/**
* The image buffer.
*
* @public
* @type {number[]}
* @memberof Frame#
*/
this.buffer = Frame._createArray(width * width);
this._ecc = Frame._createArray(dataBlock + ((dataBlock + eccBlock) * (neccBlock1 + neccBlock2)) + neccBlock2);
this._mask = Frame._createArray(((width * (width + 1)) + 1) / 2);
this._insertFinders();
this._insertAlignments();
// Insert single foreground cell.
this.buffer[8 + (width * (width - 8))] = 1;
this._insertTimingGap();
this._reverseMask();
this._insertTimingRowAndColumn();
this._insertVersion();
this._syncMask();
this._convertBitStream(valueLength);
this._calculatePolynomial();
this._appendEccToData();
this._interleaveBlocks();
this._pack();
this._finish();
}, {
_addAlignment: function(x, y) {
var i;
var buffer = this.buffer;
var width = this.width;
buffer[x + (width * y)] = 1;
for (i = -2; i < 2; i++) {
buffer[x + i + (width * (y - 2))] = 1;
buffer[x - 2 + (width * (y + i + 1))] = 1;
buffer[x + 2 + (width * (y + i))] = 1;
buffer[x + i + 1 + (width * (y + 2))] = 1;
}
for (i = 0; i < 2; i++) {
this._setMask(x - 1, y + i);
this._setMask(x + 1, y - i);
this._setMask(x - i, y - 1);
this._setMask(x + i, y + 1);
}
},
_appendData: function(data, dataLength, ecc, eccLength) {
var bit, i, j;
var polynomial = this._polynomial;
var stringBuffer = this._stringBuffer;
for (i = 0; i < eccLength; i++) {
stringBuffer[ecc + i] = 0;
}
for (i = 0; i < dataLength; i++) {
bit = Galois_1.LOG[stringBuffer[data + i] ^ stringBuffer[ecc]];
if (bit !== 255) {
for (j = 1; j < eccLength; j++) {
stringBuffer[ecc + j - 1] = stringBuffer[ecc + j] ^
Galois_1.EXPONENT[Frame._modN(bit + polynomial[eccLength - j])];
}
} else {
for (j = ecc; j < ecc + eccLength; j++) {
stringBuffer[j] = stringBuffer[j + 1];
}
}
stringBuffer[ecc + eccLength - 1] = bit === 255 ? 0 : Galois_1.EXPONENT[Frame._modN(bit + polynomial[0])];
}
},
_appendEccToData: function() {
var i;
var data = 0;
var dataBlock = this._dataBlock;
var ecc = this._calculateMaxLength();
var eccBlock = this._eccBlock;
for (i = 0; i < this._neccBlock1; i++) {
this._appendData(data, dataBlock, ecc, eccBlock);
data += dataBlock;
ecc += eccBlock;
}
for (i = 0; i < this._neccBlock2; i++) {
this._appendData(data, dataBlock + 1, ecc, eccBlock);
data += dataBlock + 1;
ecc += eccBlock;
}
},
_applyMask: function(mask) {
var r3x, r3y, x, y;
var buffer = this.buffer;
var width = this.width;
switch (mask) {
case 0:
for (y = 0; y < width; y++) {
for (x = 0; x < width; x++) {
if (!((x + y) & 1) && !this._isMasked(x, y)) {
buffer[x + (y * width)] ^= 1;
}
}
}
break;
case 1:
for (y = 0; y < width; y++) {
for (x = 0; x < width; x++) {
if (!(y & 1) && !this._isMasked(x, y)) {
buffer[x + (y * width)] ^= 1;
}
}
}
break;
case 2:
for (y = 0; y < width; y++) {
for (r3x = 0, x = 0; x < width; x++, r3x++) {
if (r3x === 3) {
r3x = 0;
}
if (!r3x && !this._isMasked(x, y)) {
buffer[x + (y * width)] ^= 1;
}
}
}
break;
case 3:
for (r3y = 0, y = 0; y < width; y++, r3y++) {
if (r3y === 3) {
r3y = 0;
}
for (r3x = r3y, x = 0; x < width; x++, r3x++) {
if (r3x === 3) {
r3x = 0;
}
if (!r3x && !this._isMasked(x, y)) {
buffer[x + (y * width)] ^= 1;
}
}
}
break;
case 4:
for (y = 0; y < width; y++) {
for (r3x = 0, r3y = (y >> 1) & 1, x = 0; x < width; x++, r3x++) {
if (r3x === 3) {
r3x = 0;
r3y = !r3y;
}
if (!r3y && !this._isMasked(x, y)) {
buffer[x + (y * width)] ^= 1;
}
}
}
break;
case 5:
for (r3y = 0, y = 0; y < width; y++, r3y++) {
if (r3y === 3) {
r3y = 0;
}
for (r3x = 0, x = 0; x < width; x++, r3x++) {
if (r3x === 3) {
r3x = 0;
}
if (!((x & y & 1) + !(!r3x | !r3y)) && !this._isMasked(x, y)) {
buffer[x + (y * width)] ^= 1;
}
}
}
break;
case 6:
for (r3y = 0, y = 0; y < width; y++, r3y++) {
if (r3y === 3) {
r3y = 0;
}
for (r3x = 0, x = 0; x < width; x++, r3x++) {
if (r3x === 3) {
r3x = 0;
}
if (!((x & y & 1) + (r3x && r3x === r3y) & 1) && !this._isMasked(x, y)) {
buffer[x + (y * width)] ^= 1;
}
}
}
break;
case 7:
for (r3y = 0, y = 0; y < width; y++, r3y++) {
if (r3y === 3) {
r3y = 0;
}
for (r3x = 0, x = 0; x < width; x++, r3x++) {
if (r3x === 3) {
r3x = 0;
}
if (!((r3x && r3x === r3y) + (x + y & 1) & 1) && !this._isMasked(x, y)) {
buffer[x + (y * width)] ^= 1;
}
}
}
break;
}
},
_calculateMaxLength: function() {
return (this._dataBlock * (this._neccBlock1 + this._neccBlock2)) + this._neccBlock2;
},
_calculatePolynomial: function() {
var i, j;
var eccBlock = this._eccBlock;
var polynomial = this._polynomial;
polynomial[0] = 1;
for (i = 0; i < eccBlock; i++) {
polynomial[i + 1] = 1;
for (j = i; j > 0; j--) {
polynomial[j] = polynomial[j] ? polynomial[j - 1] ^
Galois_1.EXPONENT[Frame._modN(Galois_1.LOG[polynomial[j]] + i)] : polynomial[j - 1];
}
polynomial[0] = Galois_1.EXPONENT[Frame._modN(Galois_1.LOG[polynomial[0]] + i)];
}
// Use logs for generator polynomial to save calculation step.
for (i = 0; i <= eccBlock; i++) {
polynomial[i] = Galois_1.LOG[polynomial[i]];
}
},
_checkBadness: function() {
var b, b1, h, x, y;
var bad = 0;
var badness = this._badness;
var buffer = this.buffer;
var width = this.width;
// Blocks of same colour.
for (y = 0; y < width - 1; y++) {
for (x = 0; x < width - 1; x++) {
// All foreground colour.
if ((buffer[x + (width * y)] &&