@vkontakte/vk-qr 2.0.13 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +3 -3
- package/dist/index.d.ts +17 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1135 -0
- package/dist/index.js.map +1 -0
- package/dist/{types/qr.d.ts → qr.d.ts} +79 -87
- package/dist/qr.d.ts.map +1 -0
- package/dist/svg.d.ts +10 -0
- package/dist/svg.d.ts.map +1 -0
- package/dist/types.d.ts +57 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +55 -77
- package/src/index.ts +39 -26
- package/src/qr.ts +217 -179
- package/src/svg.ts +30 -14
- package/src/types.ts +19 -10
- package/dist/cjs/index.js +0 -1184
- package/dist/es/index.js +0 -1182
- package/dist/types/index.d.ts +0 -20
- package/dist/types/svg.d.ts +0 -8
- package/dist/types/types.d.ts +0 -47
- package/dist/umd/qrCodeGenerator.js +0 -1190
- package/dist/umd/qrCodeGenerator.min.js +0 -1
package/dist/es/index.js
DELETED
|
@@ -1,1182 +0,0 @@
|
|
|
1
|
-
/*! *****************************************************************************
|
|
2
|
-
Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
-
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
4
|
-
this file except in compliance with the License. You may obtain a copy of the
|
|
5
|
-
License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
-
|
|
7
|
-
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
8
|
-
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
|
|
9
|
-
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
|
|
10
|
-
MERCHANTABLITY OR NON-INFRINGEMENT.
|
|
11
|
-
|
|
12
|
-
See the Apache Version 2.0 License for specific language governing permissions
|
|
13
|
-
and limitations under the License.
|
|
14
|
-
***************************************************************************** */
|
|
15
|
-
|
|
16
|
-
var __assign = function() {
|
|
17
|
-
__assign = Object.assign || function __assign(t) {
|
|
18
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
19
|
-
s = arguments[i];
|
|
20
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
|
|
21
|
-
}
|
|
22
|
-
return t;
|
|
23
|
-
};
|
|
24
|
-
return __assign.apply(this, arguments);
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
var MULTI = 1;
|
|
28
|
-
var QR_BORDER = 7;
|
|
29
|
-
var TILE_SIZE = 96 * MULTI;
|
|
30
|
-
var INC_TILE_SIZE = 96 * MULTI;
|
|
31
|
-
var SMALL_QR_SIZE = 25;
|
|
32
|
-
/**
|
|
33
|
-
* Returns the color of the module (pixel) at the given coordinates, which is false
|
|
34
|
-
* for white or true for black. The top left corner has the coordinates (x=0, y=0).
|
|
35
|
-
* If the given coordinates are out of bounds, then false (white) is returned.
|
|
36
|
-
*/
|
|
37
|
-
var getPixel = function (x, y, size, modules, isLogoShowed) {
|
|
38
|
-
if (x < QR_BORDER && y < QR_BORDER) {
|
|
39
|
-
return false;
|
|
40
|
-
}
|
|
41
|
-
if (x >= size - QR_BORDER && y < QR_BORDER) {
|
|
42
|
-
return false;
|
|
43
|
-
}
|
|
44
|
-
if (x < QR_BORDER && y >= size - QR_BORDER) {
|
|
45
|
-
return false;
|
|
46
|
-
}
|
|
47
|
-
if (isLogoShowed) {
|
|
48
|
-
var imageTiles = QR_BORDER + 2;
|
|
49
|
-
if (size <= SMALL_QR_SIZE) {
|
|
50
|
-
imageTiles--;
|
|
51
|
-
}
|
|
52
|
-
var paddingTiles = (size - QR_BORDER * 2 - imageTiles) / 2 - 1;
|
|
53
|
-
if (size <= SMALL_QR_SIZE) {
|
|
54
|
-
paddingTiles++;
|
|
55
|
-
}
|
|
56
|
-
if (x > QR_BORDER + paddingTiles &&
|
|
57
|
-
x < size - QR_BORDER - paddingTiles - 1 &&
|
|
58
|
-
y > QR_BORDER + paddingTiles &&
|
|
59
|
-
y < size - QR_BORDER - paddingTiles - 1) {
|
|
60
|
-
return false;
|
|
61
|
-
}
|
|
62
|
-
return 0 <= x && x < size && 0 <= y && y < size && modules[y][x];
|
|
63
|
-
}
|
|
64
|
-
return modules[y] && modules[y][x];
|
|
65
|
-
};
|
|
66
|
-
var getNeighbors = function (x, y, size, modules, isLogoShowed) {
|
|
67
|
-
if (isLogoShowed === void 0) { isLogoShowed = true; }
|
|
68
|
-
return ({
|
|
69
|
-
l: getPixel(x - 1, y, size, modules, isLogoShowed),
|
|
70
|
-
r: getPixel(x + 1, y, size, modules, isLogoShowed),
|
|
71
|
-
t: getPixel(x, y - 1, size, modules, isLogoShowed),
|
|
72
|
-
b: getPixel(x, y + 1, size, modules, isLogoShowed),
|
|
73
|
-
current: getPixel(x, y, size, modules, isLogoShowed)
|
|
74
|
-
});
|
|
75
|
-
};
|
|
76
|
-
/**
|
|
77
|
-
* Converts QrCode instance to SVG code
|
|
78
|
-
* @param qrCode QrCode instance
|
|
79
|
-
* @param options Convertation options
|
|
80
|
-
*/
|
|
81
|
-
var convertSegmentsToSvgString = function (qrCode, options) {
|
|
82
|
-
if (typeof options.qrSize !== 'number') {
|
|
83
|
-
throw new Error('Size should be a number');
|
|
84
|
-
}
|
|
85
|
-
if (typeof options.className !== 'string') {
|
|
86
|
-
throw new Error('Classname should be a string');
|
|
87
|
-
}
|
|
88
|
-
var _2 = 2 * MULTI;
|
|
89
|
-
var _12_7 = 12.7 * MULTI;
|
|
90
|
-
var _12_8 = 12.8 * MULTI;
|
|
91
|
-
var _14_7 = 14.7 * MULTI;
|
|
92
|
-
var _14_8 = 14.8 * MULTI;
|
|
93
|
-
var _15_9 = 15.9 * MULTI;
|
|
94
|
-
var _28_6 = 28.6 * MULTI;
|
|
95
|
-
var _30_5 = 30.5 * MULTI;
|
|
96
|
-
var _84_7 = 84.7776815 * MULTI;
|
|
97
|
-
var _87_3 = 87.3 * MULTI;
|
|
98
|
-
var _71_4 = 71.4 * MULTI;
|
|
99
|
-
var _42_9 = 42.9 * MULTI;
|
|
100
|
-
var _87_2 = 87.2 * MULTI;
|
|
101
|
-
var _85_2 = 85.2 * MULTI;
|
|
102
|
-
var _85_3 = 85.3 * MULTI;
|
|
103
|
-
var _69_5 = 69.5 * MULTI;
|
|
104
|
-
var _98 = 98 * MULTI;
|
|
105
|
-
var _100 = 100 * MULTI;
|
|
106
|
-
var parts = [];
|
|
107
|
-
var leftPadding = 0;
|
|
108
|
-
var topPadding = 0;
|
|
109
|
-
var xCoord = 0;
|
|
110
|
-
var yCoord = 0;
|
|
111
|
-
for (var y = 0; y < qrCode.size; y++) {
|
|
112
|
-
leftPadding = 0;
|
|
113
|
-
for (var x = 0; x < qrCode.size; x++) {
|
|
114
|
-
xCoord = x + leftPadding;
|
|
115
|
-
leftPadding += TILE_SIZE;
|
|
116
|
-
yCoord = y + topPadding;
|
|
117
|
-
var neighbors = getNeighbors(x, y, qrCode.size, qrCode.modules, options.isShowLogo);
|
|
118
|
-
var path = '';
|
|
119
|
-
var selector = '';
|
|
120
|
-
if (neighbors.current) {
|
|
121
|
-
selector = !selector && !neighbors.l && !neighbors.r && !neighbors.t && !neighbors.b ? 'empty' : '';
|
|
122
|
-
selector = (!selector && neighbors.l && neighbors.r) || (neighbors.t && neighbors.b) ? 'rect' : '';
|
|
123
|
-
if (!selector) {
|
|
124
|
-
selector += neighbors.l ? 'l' : neighbors.r ? 'r' : '';
|
|
125
|
-
selector += neighbors.t ? 't' : neighbors.b ? 'b' : '';
|
|
126
|
-
if (!selector) {
|
|
127
|
-
selector = 'empty';
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
else {
|
|
132
|
-
selector =
|
|
133
|
-
!selector &&
|
|
134
|
-
neighbors.l &&
|
|
135
|
-
neighbors.t &&
|
|
136
|
-
getPixel(x - 1, y - 1, qrCode.size, qrCode.modules, options.isShowLogo)
|
|
137
|
-
? 'n_lt'
|
|
138
|
-
: '';
|
|
139
|
-
selector =
|
|
140
|
-
!selector &&
|
|
141
|
-
neighbors.l &&
|
|
142
|
-
neighbors.b &&
|
|
143
|
-
getPixel(x - 1, y + 1, qrCode.size, qrCode.modules, options.isShowLogo)
|
|
144
|
-
? 'n_lb'
|
|
145
|
-
: '';
|
|
146
|
-
selector =
|
|
147
|
-
!selector &&
|
|
148
|
-
neighbors.r &&
|
|
149
|
-
neighbors.t &&
|
|
150
|
-
getPixel(x + 1, y - 1, qrCode.size, qrCode.modules, options.isShowLogo)
|
|
151
|
-
? 'n_rt'
|
|
152
|
-
: '';
|
|
153
|
-
selector =
|
|
154
|
-
!selector &&
|
|
155
|
-
neighbors.r &&
|
|
156
|
-
neighbors.b &&
|
|
157
|
-
getPixel(x + 1, y + 1, qrCode.size, qrCode.modules, options.isShowLogo)
|
|
158
|
-
? 'n_rb'
|
|
159
|
-
: '';
|
|
160
|
-
}
|
|
161
|
-
if (!selector) {
|
|
162
|
-
continue;
|
|
163
|
-
}
|
|
164
|
-
path = "<use xlink:href=\"#" + selector + "-" + options.suffix + "\"/>";
|
|
165
|
-
parts.push("<g transform=\"translate(" + xCoord + "," + yCoord + ")\">" + path + "</g>");
|
|
166
|
-
}
|
|
167
|
-
topPadding += TILE_SIZE;
|
|
168
|
-
}
|
|
169
|
-
var scale = '';
|
|
170
|
-
var position = ((qrCode.size - QR_BORDER * 3) / 2) * TILE_SIZE + TILE_SIZE * QR_BORDER - 10;
|
|
171
|
-
if (qrCode.size <= SMALL_QR_SIZE) {
|
|
172
|
-
scale = 'scale(0.85)';
|
|
173
|
-
position += 50;
|
|
174
|
-
}
|
|
175
|
-
var pointPosition = (qrCode.size - QR_BORDER) * INC_TILE_SIZE;
|
|
176
|
-
parts.push("<use fill-rule=\"evenodd\" transform=\"translate(0,0)\" xlink:href=\"#point-" + options.suffix + "\"/>");
|
|
177
|
-
parts.push("<use fill-rule=\"evenodd\" transform=\"translate(" + pointPosition + ",0)\" xlink:href=\"#point-" + options.suffix + "\"/>");
|
|
178
|
-
parts.push("<use fill-rule=\"evenodd\" transform=\"translate(0," + pointPosition + ")\" xlink:href=\"#point-" + options.suffix + "\"/>");
|
|
179
|
-
if (options.isShowLogo) {
|
|
180
|
-
if (!options.logoData) {
|
|
181
|
-
parts.push("\n <use style=\"width: 750px; height: 750px;\" width=\"750\" height=\"750\"\n fill=\"none\"\n fill-rule=\"evenodd\"\n transform=\"translate(" + position + "," + position + ") " + scale + "\" xlink:href=\"#vk_logo-" + options.suffix + "\"\n />\n ");
|
|
182
|
-
}
|
|
183
|
-
else {
|
|
184
|
-
parts.push("\n <image\n preserveAspectRatio=\"xMidYMid slice\" clip-path=\"url(#logo-mask-" + options.suffix + ")\"\n style=\"width: 750px; height: 750px;\" width=\"750\" height=\"750\"\n transform=\"translate(" + position + "," + position + ") " + scale + "\"\n xlink:href=\"" + options.logoData + "\"\n />\n ");
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
var backgroundSize = 99 * qrCode.size;
|
|
188
|
-
var qrBackground = '';
|
|
189
|
-
var qrTransform = 'translate(0,0)';
|
|
190
|
-
if (options.isShowBackground) {
|
|
191
|
-
var qrScale = (options.qrSize - 20 * 2) / options.qrSize; // 0.756972112
|
|
192
|
-
var padding = (backgroundSize / options.qrSize) * 21;
|
|
193
|
-
var radius = Math.ceil(backgroundSize / (options.qrSize / 36)); // 7.11111111 = 256px/36px radius
|
|
194
|
-
qrBackground = "\n <rect\n x=\"0\"\n width=\"" + backgroundSize + "\"\n height=\"" + backgroundSize + "\"\n rx=\"" + radius + "\"\n fill=\"" + options.backgroundColor + "\"\n />";
|
|
195
|
-
qrTransform = "translate(" + padding + ", " + padding + ") scale(" + qrScale + ")";
|
|
196
|
-
}
|
|
197
|
-
// eslint-disable-next-line
|
|
198
|
-
var oneSide = "M0,0 L66,0 C" + _84_7 + ",-3.44940413e-15 " + _100 + ",15.2223185 " + _100 + ",34 L" + _100 + ",66 C" + _100 + "," + _84_7 + " " + _84_7 + "," + _100 + " 66," + _100 + " L0," + _100 + " L0,0 Z";
|
|
199
|
-
var twoSides = "M0,0 L" + _100 + ",0 L" + _100 + ",66 C" + _100 + "," + _84_7 + " " + _84_7 + "," + _100 + " 66," + _100 + " L0," + _100 + " L0,0 Z";
|
|
200
|
-
return "\n <svg\n version=\"1.1\"\n viewBox=\"0 0 " + backgroundSize + " " + backgroundSize + "\"\n width=\"" + options.qrSize + "px\"\n height=\"" + options.qrSize + "px\"\n " + (options.className ? "class=\"" + options.className + "\"" : '') + "\n xml:space=\"preserve\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"\n >\n <defs>\n <rect id=\"rect-" + options.suffix + "\" width=\"100\" height=\"100\" fill=\"" + options.foregroundColor + "\"/>\n <path\n id=\"empty-" + options.suffix + "\"\n d=\"M0," + _28_6 + "v" + _42_9 + "C0," + _87_3 + "," + _12_8 + "," + _100 + "," + _28_6 + "," + _100 + "h" + _42_9 + "c" + _15_9 + ",0," + _28_6 + "-" + _12_8 + "," + _28_6 + "-" + _28_6 + "V" + _28_6 + "C" + _100 + "," + _12_7 + "," + _87_2 + ",0," + _71_4 + ",0H" + _28_6 + " C" + _12_8 + ",0,0," + _12_8 + ",0," + _28_6 + "z\"\n fill=\"" + options.foregroundColor + "\"\n />\n <path id=\"b-" + options.suffix + "\" d=\"" + oneSide + "\" transform=\"rotate(-90 50 50)\" fill=\"" + options.foregroundColor + "\"/>\n <path id=\"r-" + options.suffix + "\" d=\"" + oneSide + "\" transform=\"rotate(-180 50 50)\" fill=\"" + options.foregroundColor + "\"/>\n <path id=\"l-" + options.suffix + "\" d=\"" + oneSide + "\" fill=\"" + options.foregroundColor + "\"/>\n <path id=\"t-" + options.suffix + "\" d=\"" + oneSide + "\" transform=\"rotate(90 50 50)\" fill=\"" + options.foregroundColor + "\"/>\n <path id=\"l-" + options.suffix + "\" d=\"" + twoSides + "\" transform=\"rotate(-90 50 50)\" fill=\"" + options.foregroundColor + "\"/>\n <path id=\"lt-" + options.suffix + "\" d=\"" + twoSides + "\" fill=\"" + options.foregroundColor + "\"/>\n <path id=\"lb-" + options.suffix + "\" d=\"" + twoSides + "\" transform=\"rotate(-90 50 50)\" fill=\"" + options.foregroundColor + "\"/>\n <path id=\"rb-" + options.suffix + "\" d=\"" + twoSides + "\" transform=\"rotate(-180 50 50)\" fill=\"" + options.foregroundColor + "\"/>\n <path id=\"rt-" + options.suffix + "\" d=\"" + twoSides + "\" transform=\"rotate(90 50 50)\" fill=\"" + options.foregroundColor + "\"/>\n <path\n id=\"n_lt-" + options.suffix + "\"\n d=\"M" + _30_5 + "," + _2 + "V0H0v" + _30_5 + "h" + _2 + "C" + _2 + "," + _14_7 + "," + _14_8 + "," + _2 + "," + _30_5 + "," + _2 + "z\"\n fill=\"" + options.foregroundColor + "\"\n />\n <path\n id=\"n_lb-" + options.suffix + "\"\n d=\"M" + _2 + "," + _69_5 + "H0V" + _100 + "h" + _30_5 + "v-" + _2 + "C" + _14_7 + "," + _98 + "," + _2 + "," + _85_2 + "," + _2 + "," + _69_5 + "z\"\n fill=\"" + options.foregroundColor + "\"\n />\n <path\n id=\"n_rt-" + options.suffix + "\"\n d=\"M" + _98 + "," + _30_5 + "h" + _2 + "V0H" + _69_5 + "v" + _2 + "C" + _85_3 + "," + _2 + "," + _98 + "," + _14_8 + "," + _98 + "," + _30_5 + "z\"\n fill=\"" + options.foregroundColor + "\"\n />\n <path id=\"n_rb-" + options.suffix + "\"\n d=\"M" + _69_5 + "," + _98 + "v" + _2 + "H" + _100 + "V" + _69_5 + "h-" + _2 + "C" + _98 + "," + _85_3 + "," + _85_2 + "," + _98 + "," + _69_5 + "," + _98 + "z\"\n fill=\"" + options.foregroundColor + "\"\n />\n <path\n id=\"point-" + options.suffix + "\"\n fill=\"" + options.foregroundColor + "\"\n d=\"M600.001786,457.329333 L600.001786,242.658167 C600.001786,147.372368 587.039517,124.122784 581.464617,118.535383 C575.877216,112.960483 552.627632,99.9982143 457.329333,99.9982143 L242.670667,99.9982143 C147.372368,99.9982143 124.122784,112.960483 118.547883,118.535383 C112.972983,124.122784 99.9982143,147.372368 99.9982143,242.658167 L99.9982143,457.329333 C99.9982143,552.627632 112.972983,575.877216 118.547883,581.464617 C124.122784,587.027017 147.372368,600.001786 242.670667,600.001786 L457.329333,600.001786 C552.627632,600.001786 575.877216,587.027017 581.464617,581.464617 C587.039517,575.877216 600.001786,552.627632 600.001786,457.329333 Z M457.329333,0 C653.338333,0 700,46.6616668 700,242.658167 C700,438.667167 700,261.332833 700,457.329333 C700,653.338333 653.338333,700 457.329333,700 C261.332833,700 438.667167,700 242.670667,700 C46.6616668,700 0,653.338333 0,457.329333 C0,261.332833 0,352.118712 0,242.658167 C0,46.6616668 46.6616668,0 242.670667,0 C438.667167,0 261.332833,0 457.329333,0 Z M395.996667,200 C480.004166,200 500,220.008332 500,303.990835 C500,387.998334 500,312.001666 500,395.996667 C500,479.991668 480.004166,500 395.996667,500 C312.001666,500 387.998334,500 304.003333,500 C220.008332,500 200,479.991668 200,395.996667 C200,312.001666 200,350.906061 200,303.990835 C200,220.008332 220.008332,200 304.003333,200 C387.998334,200 312.001666,200 395.996667,200 Z\"\n />\n <g id=\"vk_logo-" + options.suffix + "\">\n <path\n fill=\"" + options.logoColor + "\"\n d=\"M83.3334 363.333C83.3334 231.34 83.3334 165.343 124.338 124.338C165.343 83.3333 231.34 83.3333 363.333 83.3333H386.667C518.66 83.3333 584.657 83.3333 625.662 124.338C666.667 165.343 666.667 231.34 666.667 363.333V386.667C666.667 518.66 666.667 584.657 625.662 625.662C584.657 666.667 518.66 666.667 386.667 666.667H363.333C231.34 666.667 165.343 666.667 124.338 625.662C83.3334 584.657 83.3334 518.66 83.3334 386.667V363.333Z\"\n />\n <path\n fill=\"#FFF\"\n d=\"M394.907 508.681C263.293 508.681 183.442 417.365 180.313 265.625H246.972C249.05 377.09 299.763 424.323 338.638 434.036V265.625H402.519V361.82C440.016 357.664 479.264 313.909 492.456 265.625H555.333C545.27 325 502.543 368.754 472.353 386.803C502.566 401.395 551.177 439.593 569.926 508.681H500.815C486.222 462.498 450.476 426.728 402.542 421.872V508.681H394.907Z\"\n />\n </g>\n <clipPath id=\"logo-mask-" + options.suffix + "\">\n <rect x=\"0\" y=\"0\" width=\"750\" height=\"750\" />\n </clipPath>\n </defs>\n\n " + qrBackground + "\n\n <g transform=\"" + qrTransform + "\">\n " + parts.join('\n') + "\n </g>\n </svg>";
|
|
201
|
-
};
|
|
202
|
-
|
|
203
|
-
/*
|
|
204
|
-
* QR Code generator library (TypeScript)
|
|
205
|
-
*
|
|
206
|
-
* Copyright (c) Project Nayuki. (MIT License)
|
|
207
|
-
* https://www.nayuki.io/page/qr-code-generator-library
|
|
208
|
-
*
|
|
209
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
210
|
-
* this software and associated documentation files (the "Software"), to deal in
|
|
211
|
-
* the Software without restriction, including without limitation the rights to
|
|
212
|
-
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
213
|
-
* the Software, and to permit persons to whom the Software is furnished to do so,
|
|
214
|
-
* subject to the following conditions:
|
|
215
|
-
* - The above copyright notice and this permission notice shall be included in
|
|
216
|
-
* all copies or substantial portions of the Software.
|
|
217
|
-
* - The Software is provided "as is", without warranty of any kind, express or
|
|
218
|
-
* implied, including but not limited to the warranties of merchantability,
|
|
219
|
-
* fitness for a particular purpose and noninfringement. In no event shall the
|
|
220
|
-
* authors or copyright holders be liable for any claim, damages or other
|
|
221
|
-
* liability, whether in an action of contract, tort or otherwise, arising from,
|
|
222
|
-
* out of or in connection with the Software or the use or other dealings in the
|
|
223
|
-
* Software.
|
|
224
|
-
*/
|
|
225
|
-
/*---- QR Code symbol class ----*/
|
|
226
|
-
/*
|
|
227
|
-
* A QR Code symbol, which is a type of two-dimension barcode.
|
|
228
|
-
* Invented by Denso Wave and described in the ISO/IEC 18004 standard.
|
|
229
|
-
* Instances of this class represent an immutable square grid of black and white cells.
|
|
230
|
-
* The class provides static factory functions to create a QR Code from text or binary data.
|
|
231
|
-
* The class covers the QR Code Model 2 specification, supporting all versions (sizes)
|
|
232
|
-
* from 1 to 40, all 4 error correction levels, and 4 character encoding modes.
|
|
233
|
-
*
|
|
234
|
-
* Ways to create a QR Code object:
|
|
235
|
-
* - High level: Take the payload data and call QrCode.encodeText() or QrCode.encodeBinary().
|
|
236
|
-
* - Mid level: Custom-make the list of segments and call QrCode.encodeSegments().
|
|
237
|
-
* - Low level: Custom-make the array of data codeword bytes (including
|
|
238
|
-
* segment headers and final padding, excluding error correction codewords),
|
|
239
|
-
* supply the appropriate version number, and call the QrCode() constructor.
|
|
240
|
-
* (Note that all ways require supplying the desired error correction level.)
|
|
241
|
-
*/
|
|
242
|
-
var QrCode = /** @class */ (function () {
|
|
243
|
-
/*-- Constructor (low level) and fields --*/
|
|
244
|
-
// Creates a new QR Code with the given version number,
|
|
245
|
-
// error correction level, data codeword bytes, and mask number.
|
|
246
|
-
// This is a low-level API that most users should not use directly.
|
|
247
|
-
// A mid-level API is the encodeSegments() function.
|
|
248
|
-
function QrCode(
|
|
249
|
-
// The version number of this QR Code, which is between 1 and 40 (inclusive).
|
|
250
|
-
// This determines the size of this barcode.
|
|
251
|
-
version,
|
|
252
|
-
// The error correction level used in this QR Code.
|
|
253
|
-
errorCorrectionLevel, dataCodewords,
|
|
254
|
-
// The index of the mask pattern used in this QR Code, which is between 0 and 7 (inclusive).
|
|
255
|
-
// Even if a QR Code is created with automatic masking requested (mask = -1),
|
|
256
|
-
// the resulting object still has a mask value between 0 and 7.
|
|
257
|
-
mask) {
|
|
258
|
-
this.version = version;
|
|
259
|
-
this.errorCorrectionLevel = errorCorrectionLevel;
|
|
260
|
-
this.mask = mask;
|
|
261
|
-
// The modules of this QR Code (false = white, true = black).
|
|
262
|
-
// Immutable after constructor finishes. Accessed through getModule().
|
|
263
|
-
this.modules = [];
|
|
264
|
-
// Indicates function modules that are not subjected to masking. Discarded when constructor finishes.
|
|
265
|
-
this.isFunction = [];
|
|
266
|
-
// Check scalar arguments
|
|
267
|
-
if (version < QrCode.MIN_VERSION || version > QrCode.MAX_VERSION)
|
|
268
|
-
throw 'Version value out of range';
|
|
269
|
-
if (mask < -1 || mask > 7)
|
|
270
|
-
throw 'Mask value out of range';
|
|
271
|
-
this.size = version * 4 + 17;
|
|
272
|
-
// Initialize both grids to be size*size arrays of Boolean false
|
|
273
|
-
var row = [];
|
|
274
|
-
for (var i = 0; i < this.size; i++)
|
|
275
|
-
row.push(false);
|
|
276
|
-
for (var i = 0; i < this.size; i++) {
|
|
277
|
-
this.modules.push(row.slice()); // Initially all white
|
|
278
|
-
this.isFunction.push(row.slice());
|
|
279
|
-
}
|
|
280
|
-
// Compute ECC, draw modules
|
|
281
|
-
this.drawFunctionPatterns();
|
|
282
|
-
var allCodewords = this.addEccAndInterleave(dataCodewords);
|
|
283
|
-
this.drawCodewords(allCodewords);
|
|
284
|
-
// Do masking
|
|
285
|
-
if (mask == -1) {
|
|
286
|
-
// Automatically choose best mask
|
|
287
|
-
var minPenalty = 1000000000;
|
|
288
|
-
for (var i = 0; i < 8; i++) {
|
|
289
|
-
this.applyMask(i);
|
|
290
|
-
this.drawFormatBits(i);
|
|
291
|
-
var penalty = this.getPenaltyScore();
|
|
292
|
-
if (penalty < minPenalty) {
|
|
293
|
-
mask = i;
|
|
294
|
-
minPenalty = penalty;
|
|
295
|
-
}
|
|
296
|
-
this.applyMask(i); // Undoes the mask due to XOR
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
if (mask < 0 || mask > 7)
|
|
300
|
-
throw 'Assertion error';
|
|
301
|
-
this.mask = mask;
|
|
302
|
-
this.applyMask(mask); // Apply the final choice of mask
|
|
303
|
-
this.drawFormatBits(mask); // Overwrite old format bits
|
|
304
|
-
this.isFunction = [];
|
|
305
|
-
}
|
|
306
|
-
/*-- Static factory functions (high level) --*/
|
|
307
|
-
// Returns a QR Code representing the given Unicode text string at the given error correction level.
|
|
308
|
-
// As a conservative upper bound, this function is guaranteed to succeed for strings that have 738 or fewer
|
|
309
|
-
// Unicode code points (not UTF-16 code units) if the low error correction level is used. The smallest possible
|
|
310
|
-
// QR Code version is automatically chosen for the output. The ECC level of the result may be higher than the
|
|
311
|
-
// ecl argument if it can be done without increasing the version.
|
|
312
|
-
QrCode.encodeText = function (text, ecl) {
|
|
313
|
-
var segs = QrSegment.makeSegments(text);
|
|
314
|
-
return QrCode.encodeSegments(segs, ecl);
|
|
315
|
-
};
|
|
316
|
-
// Returns a QR Code representing the given binary data at the given error correction level.
|
|
317
|
-
// This function always encodes using the binary segment mode, not any text mode. The maximum number of
|
|
318
|
-
// bytes allowed is 2953. The smallest possible QR Code version is automatically chosen for the output.
|
|
319
|
-
// The ECC level of the result may be higher than the ecl argument if it can be done without increasing the version.
|
|
320
|
-
QrCode.encodeBinary = function (data, ecl) {
|
|
321
|
-
var seg = QrSegment.makeBytes(data);
|
|
322
|
-
return QrCode.encodeSegments([seg], ecl);
|
|
323
|
-
};
|
|
324
|
-
/*-- Static factory functions (mid level) --*/
|
|
325
|
-
// Returns a QR Code representing the given segments with the given encoding parameters.
|
|
326
|
-
// The smallest possible QR Code version within the given range is automatically
|
|
327
|
-
// chosen for the output. Iff boostEcl is true, then the ECC level of the result
|
|
328
|
-
// may be higher than the ecl argument if it can be done without increasing the
|
|
329
|
-
// version. The mask number is either between 0 to 7 (inclusive) to force that
|
|
330
|
-
// mask, or -1 to automatically choose an appropriate mask (which may be slow).
|
|
331
|
-
// This function allows the user to create a custom sequence of segments that switches
|
|
332
|
-
// between modes (such as alphanumeric and byte) to encode text in less space.
|
|
333
|
-
// This is a mid-level API; the high-level API is encodeText() and encodeBinary().
|
|
334
|
-
QrCode.encodeSegments = function (segs, ecl, minVersion, maxVersion, mask, boostEcl) {
|
|
335
|
-
if (minVersion === void 0) { minVersion = 1; }
|
|
336
|
-
if (maxVersion === void 0) { maxVersion = 40; }
|
|
337
|
-
if (mask === void 0) { mask = -1; }
|
|
338
|
-
if (boostEcl === void 0) { boostEcl = true; }
|
|
339
|
-
if (!(QrCode.MIN_VERSION <= minVersion && minVersion <= maxVersion && maxVersion <= QrCode.MAX_VERSION) ||
|
|
340
|
-
mask < -1 ||
|
|
341
|
-
mask > 7)
|
|
342
|
-
throw 'Invalid value';
|
|
343
|
-
// Find the minimal version number to use
|
|
344
|
-
var version;
|
|
345
|
-
var dataUsedBits;
|
|
346
|
-
for (version = minVersion;; version++) {
|
|
347
|
-
var dataCapacityBits_1 = QrCode.getNumDataCodewords(version, ecl) * 8; // Number of data bits available
|
|
348
|
-
var usedBits = QrSegment.getTotalBits(segs, version);
|
|
349
|
-
if (usedBits <= dataCapacityBits_1) {
|
|
350
|
-
dataUsedBits = usedBits;
|
|
351
|
-
break; // This version number is found to be suitable
|
|
352
|
-
}
|
|
353
|
-
if (version >= maxVersion)
|
|
354
|
-
// All versions in the range could not fit the given data
|
|
355
|
-
throw 'Data too long';
|
|
356
|
-
}
|
|
357
|
-
// Increase the error correction level while the data still fits in the current version number
|
|
358
|
-
for (var _i = 0, _a = [QrCode.Ecc.MEDIUM, QrCode.Ecc.QUARTILE, QrCode.Ecc.HIGH]; _i < _a.length; _i++) {
|
|
359
|
-
var newEcl = _a[_i];
|
|
360
|
-
// From low to high
|
|
361
|
-
if (boostEcl && dataUsedBits <= QrCode.getNumDataCodewords(version, newEcl) * 8)
|
|
362
|
-
ecl = newEcl;
|
|
363
|
-
}
|
|
364
|
-
// Concatenate all segments to create the data bit string
|
|
365
|
-
var bb = new BitBuffer();
|
|
366
|
-
for (var _b = 0, segs_1 = segs; _b < segs_1.length; _b++) {
|
|
367
|
-
var seg = segs_1[_b];
|
|
368
|
-
bb.appendBits(seg.mode.modeBits, 4);
|
|
369
|
-
bb.appendBits(seg.numChars, seg.mode.numCharCountBits(version));
|
|
370
|
-
for (var _c = 0, _d = seg.getData(); _c < _d.length; _c++) {
|
|
371
|
-
var b = _d[_c];
|
|
372
|
-
bb.array.push(b);
|
|
373
|
-
}
|
|
374
|
-
}
|
|
375
|
-
if (bb.array.length != dataUsedBits)
|
|
376
|
-
throw 'Assertion error';
|
|
377
|
-
// Add terminator and pad up to a byte if applicable
|
|
378
|
-
var dataCapacityBits = QrCode.getNumDataCodewords(version, ecl) * 8;
|
|
379
|
-
if (bb.array.length > dataCapacityBits)
|
|
380
|
-
throw 'Assertion error';
|
|
381
|
-
bb.appendBits(0, Math.min(4, dataCapacityBits - bb.array.length));
|
|
382
|
-
bb.appendBits(0, (8 - (bb.array.length % 8)) % 8);
|
|
383
|
-
if (bb.array.length % 8 != 0)
|
|
384
|
-
throw 'Assertion error';
|
|
385
|
-
// Pad with alternating bytes until data capacity is reached
|
|
386
|
-
for (var padByte = 0xec; bb.array.length < dataCapacityBits; padByte ^= 0xec ^ 0x11)
|
|
387
|
-
bb.appendBits(padByte, 8);
|
|
388
|
-
// Pack bits into bytes in big endian
|
|
389
|
-
var dataCodewords = [];
|
|
390
|
-
while (dataCodewords.length * 8 < bb.array.length)
|
|
391
|
-
dataCodewords.push(0);
|
|
392
|
-
bb.array.forEach(function (b, i) { return (dataCodewords[i >>> 3] |= b << (7 - (i & 7))); });
|
|
393
|
-
// Create the QR Code object
|
|
394
|
-
return new QrCode(version, ecl, dataCodewords, mask);
|
|
395
|
-
};
|
|
396
|
-
/*-- Accessor methods --*/
|
|
397
|
-
// Returns the color of the module (pixel) at the given coordinates, which is false
|
|
398
|
-
// for white or true for black. The top left corner has the coordinates (x=0, y=0).
|
|
399
|
-
// If the given coordinates are out of bounds, then false (white) is returned.
|
|
400
|
-
QrCode.prototype.getModule = function (x, y) {
|
|
401
|
-
return 0 <= x && x < this.size && 0 <= y && y < this.size && this.modules[y][x];
|
|
402
|
-
};
|
|
403
|
-
/*-- Public instance methods --*/
|
|
404
|
-
// Draws this QR Code, with the given module scale and border modules, onto the given HTML
|
|
405
|
-
// canvas element. The canvas's width and height is resized to (this.size + border * 2) * scale.
|
|
406
|
-
// The drawn image is be purely black and white, and fully opaque.
|
|
407
|
-
// The scale must be a positive integer and the border must be a non-negative integer.
|
|
408
|
-
QrCode.prototype.drawCanvas = function (scale, border, canvas) {
|
|
409
|
-
if (scale <= 0 || border < 0)
|
|
410
|
-
throw 'Value out of range';
|
|
411
|
-
var width = (this.size + border * 2) * scale;
|
|
412
|
-
canvas.width = width;
|
|
413
|
-
canvas.height = width;
|
|
414
|
-
var ctx = canvas.getContext('2d');
|
|
415
|
-
for (var y = -border; y < this.size + border; y++) {
|
|
416
|
-
for (var x = -border; x < this.size + border; x++) {
|
|
417
|
-
ctx.fillStyle = this.getModule(x, y) ? '#000000' : '#FFFFFF';
|
|
418
|
-
ctx.fillRect((x + border) * scale, (y + border) * scale, scale, scale);
|
|
419
|
-
}
|
|
420
|
-
}
|
|
421
|
-
};
|
|
422
|
-
// Returns a string of SVG code for an image depicting this QR Code, with the given number
|
|
423
|
-
// of border modules. The string always uses Unix newlines (\n), regardless of the platform.
|
|
424
|
-
QrCode.prototype.toSvgString = function (border) {
|
|
425
|
-
if (border < 0)
|
|
426
|
-
throw 'Border must be non-negative';
|
|
427
|
-
var parts = [];
|
|
428
|
-
for (var y = 0; y < this.size; y++) {
|
|
429
|
-
for (var x = 0; x < this.size; x++) {
|
|
430
|
-
if (this.getModule(x, y))
|
|
431
|
-
parts.push("M" + (x + border) + "," + (y + border) + "h1v1h-1z");
|
|
432
|
-
}
|
|
433
|
-
}
|
|
434
|
-
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" viewBox=\"0 0 " + (this.size + border * 2) + " " + (this.size +
|
|
435
|
-
border * 2) + "\" stroke=\"none\">\n\t<rect width=\"100%\" height=\"100%\" fill=\"#FFFFFF\"/>\n\t<path d=\"" + parts.join(' ') + "\" fill=\"#000000\"/>\n</svg>\n";
|
|
436
|
-
};
|
|
437
|
-
/*-- Private helper methods for constructor: Drawing function modules --*/
|
|
438
|
-
// Reads this object's version field, and draws and marks all function modules.
|
|
439
|
-
QrCode.prototype.drawFunctionPatterns = function () {
|
|
440
|
-
// Draw horizontal and vertical timing patterns
|
|
441
|
-
for (var i = 0; i < this.size; i++) {
|
|
442
|
-
this.setFunctionModule(6, i, i % 2 == 0);
|
|
443
|
-
this.setFunctionModule(i, 6, i % 2 == 0);
|
|
444
|
-
}
|
|
445
|
-
// Draw 3 finder patterns (all corners except bottom right; overwrites some timing modules)
|
|
446
|
-
this.drawFinderPattern(3, 3);
|
|
447
|
-
this.drawFinderPattern(this.size - 4, 3);
|
|
448
|
-
this.drawFinderPattern(3, this.size - 4);
|
|
449
|
-
// Draw numerous alignment patterns
|
|
450
|
-
var alignPatPos = this.getAlignmentPatternPositions();
|
|
451
|
-
var numAlign = alignPatPos.length;
|
|
452
|
-
for (var i = 0; i < numAlign; i++) {
|
|
453
|
-
for (var j = 0; j < numAlign; j++) {
|
|
454
|
-
// Don't draw on the three finder corners
|
|
455
|
-
if (!((i == 0 && j == 0) || (i == 0 && j == numAlign - 1) || (i == numAlign - 1 && j == 0)))
|
|
456
|
-
this.drawAlignmentPattern(alignPatPos[i], alignPatPos[j]);
|
|
457
|
-
}
|
|
458
|
-
}
|
|
459
|
-
// Draw configuration data
|
|
460
|
-
this.drawFormatBits(0); // Dummy mask value; overwritten later in the constructor
|
|
461
|
-
this.drawVersion();
|
|
462
|
-
};
|
|
463
|
-
// Draws two copies of the format bits (with its own error correction code)
|
|
464
|
-
// based on the given mask and this object's error correction level field.
|
|
465
|
-
QrCode.prototype.drawFormatBits = function (mask) {
|
|
466
|
-
// Calculate error correction code and pack bits
|
|
467
|
-
var data = (this.errorCorrectionLevel.formatBits << 3) | mask; // errCorrLvl is uint2, mask is uint3
|
|
468
|
-
var rem = data;
|
|
469
|
-
for (var i = 0; i < 10; i++)
|
|
470
|
-
rem = (rem << 1) ^ ((rem >>> 9) * 0x537);
|
|
471
|
-
var bits = ((data << 10) | rem) ^ 0x5412; // uint15
|
|
472
|
-
if (bits >>> 15 != 0)
|
|
473
|
-
throw 'Assertion error';
|
|
474
|
-
// Draw first copy
|
|
475
|
-
for (var i = 0; i <= 5; i++)
|
|
476
|
-
this.setFunctionModule(8, i, getBit(bits, i));
|
|
477
|
-
this.setFunctionModule(8, 7, getBit(bits, 6));
|
|
478
|
-
this.setFunctionModule(8, 8, getBit(bits, 7));
|
|
479
|
-
this.setFunctionModule(7, 8, getBit(bits, 8));
|
|
480
|
-
for (var i = 9; i < 15; i++)
|
|
481
|
-
this.setFunctionModule(14 - i, 8, getBit(bits, i));
|
|
482
|
-
// Draw second copy
|
|
483
|
-
for (var i = 0; i < 8; i++)
|
|
484
|
-
this.setFunctionModule(this.size - 1 - i, 8, getBit(bits, i));
|
|
485
|
-
for (var i = 8; i < 15; i++)
|
|
486
|
-
this.setFunctionModule(8, this.size - 15 + i, getBit(bits, i));
|
|
487
|
-
this.setFunctionModule(8, this.size - 8, true); // Always black
|
|
488
|
-
};
|
|
489
|
-
// Draws two copies of the version bits (with its own error correction code),
|
|
490
|
-
// based on this object's version field, iff 7 <= version <= 40.
|
|
491
|
-
QrCode.prototype.drawVersion = function () {
|
|
492
|
-
if (this.version < 7)
|
|
493
|
-
return;
|
|
494
|
-
// Calculate error correction code and pack bits
|
|
495
|
-
var rem = this.version; // version is uint6, in the range [7, 40]
|
|
496
|
-
for (var i = 0; i < 12; i++)
|
|
497
|
-
rem = (rem << 1) ^ ((rem >>> 11) * 0x1f25);
|
|
498
|
-
var bits = (this.version << 12) | rem; // uint18
|
|
499
|
-
if (bits >>> 18 != 0)
|
|
500
|
-
throw 'Assertion error';
|
|
501
|
-
// Draw two copies
|
|
502
|
-
for (var i = 0; i < 18; i++) {
|
|
503
|
-
var color = getBit(bits, i);
|
|
504
|
-
var a = this.size - 11 + (i % 3);
|
|
505
|
-
var b = Math.floor(i / 3);
|
|
506
|
-
this.setFunctionModule(a, b, color);
|
|
507
|
-
this.setFunctionModule(b, a, color);
|
|
508
|
-
}
|
|
509
|
-
};
|
|
510
|
-
// Draws a 9*9 finder pattern including the border separator,
|
|
511
|
-
// with the center module at (x, y). Modules can be out of bounds.
|
|
512
|
-
QrCode.prototype.drawFinderPattern = function (x, y) {
|
|
513
|
-
for (var dy = -4; dy <= 4; dy++) {
|
|
514
|
-
for (var dx = -4; dx <= 4; dx++) {
|
|
515
|
-
var dist = Math.max(Math.abs(dx), Math.abs(dy)); // Chebyshev/infinity norm
|
|
516
|
-
var xx = x + dx;
|
|
517
|
-
var yy = y + dy;
|
|
518
|
-
if (0 <= xx && xx < this.size && 0 <= yy && yy < this.size)
|
|
519
|
-
this.setFunctionModule(xx, yy, dist != 2 && dist != 4);
|
|
520
|
-
}
|
|
521
|
-
}
|
|
522
|
-
};
|
|
523
|
-
// Draws a 5*5 alignment pattern, with the center module
|
|
524
|
-
// at (x, y). All modules must be in bounds.
|
|
525
|
-
QrCode.prototype.drawAlignmentPattern = function (x, y) {
|
|
526
|
-
for (var dy = -2; dy <= 2; dy++) {
|
|
527
|
-
for (var dx = -2; dx <= 2; dx++)
|
|
528
|
-
this.setFunctionModule(x + dx, y + dy, Math.max(Math.abs(dx), Math.abs(dy)) != 1);
|
|
529
|
-
}
|
|
530
|
-
};
|
|
531
|
-
// Sets the color of a module and marks it as a function module.
|
|
532
|
-
// Only used by the constructor. Coordinates must be in bounds.
|
|
533
|
-
QrCode.prototype.setFunctionModule = function (x, y, isBlack) {
|
|
534
|
-
this.modules[y][x] = isBlack;
|
|
535
|
-
this.isFunction[y][x] = true;
|
|
536
|
-
};
|
|
537
|
-
/*-- Private helper methods for constructor: Codewords and masking --*/
|
|
538
|
-
// Returns a new byte string representing the given data with the appropriate error correction
|
|
539
|
-
// codewords appended to it, based on this object's version and error correction level.
|
|
540
|
-
QrCode.prototype.addEccAndInterleave = function (data) {
|
|
541
|
-
var ver = this.version;
|
|
542
|
-
var ecl = this.errorCorrectionLevel;
|
|
543
|
-
if (data.length != QrCode.getNumDataCodewords(ver, ecl))
|
|
544
|
-
throw 'Invalid argument';
|
|
545
|
-
// Calculate parameter numbers
|
|
546
|
-
var numBlocks = QrCode.NUM_ERROR_CORRECTION_BLOCKS[ecl.ordinal][ver];
|
|
547
|
-
var blockEccLen = QrCode.ECC_CODEWORDS_PER_BLOCK[ecl.ordinal][ver];
|
|
548
|
-
var rawCodewords = Math.floor(QrCode.getNumRawDataModules(ver) / 8);
|
|
549
|
-
var numShortBlocks = numBlocks - (rawCodewords % numBlocks);
|
|
550
|
-
var shortBlockLen = Math.floor(rawCodewords / numBlocks);
|
|
551
|
-
// Split data into blocks and append ECC to each block
|
|
552
|
-
var blocks = [];
|
|
553
|
-
var rs = new ReedSolomonGenerator(blockEccLen);
|
|
554
|
-
for (var i = 0, k = 0; i < numBlocks; i++) {
|
|
555
|
-
var dat = data.slice(k, k + shortBlockLen - blockEccLen + (i < numShortBlocks ? 0 : 1));
|
|
556
|
-
k += dat.length;
|
|
557
|
-
var ecc = rs.getRemainder(dat);
|
|
558
|
-
if (i < numShortBlocks)
|
|
559
|
-
dat.push(0);
|
|
560
|
-
blocks.push(dat.concat(ecc));
|
|
561
|
-
}
|
|
562
|
-
// Interleave (not concatenate) the bytes from every block into a single sequence
|
|
563
|
-
var result = [];
|
|
564
|
-
for (var i = 0; i < blocks[0].length; i++) {
|
|
565
|
-
for (var j = 0; j < blocks.length; j++) {
|
|
566
|
-
// Skip the padding byte in short blocks
|
|
567
|
-
if (i != shortBlockLen - blockEccLen || j >= numShortBlocks)
|
|
568
|
-
result.push(blocks[j][i]);
|
|
569
|
-
}
|
|
570
|
-
}
|
|
571
|
-
if (result.length != rawCodewords)
|
|
572
|
-
throw 'Assertion error';
|
|
573
|
-
return result;
|
|
574
|
-
};
|
|
575
|
-
// Draws the given sequence of 8-bit codewords (data and error correction) onto the entire
|
|
576
|
-
// data area of this QR Code. Function modules need to be marked off before this is called.
|
|
577
|
-
QrCode.prototype.drawCodewords = function (data) {
|
|
578
|
-
if (data.length != Math.floor(QrCode.getNumRawDataModules(this.version) / 8))
|
|
579
|
-
throw 'Invalid argument';
|
|
580
|
-
var i = 0; // Bit index into the data
|
|
581
|
-
// Do the funny zigzag scan
|
|
582
|
-
for (var right = this.size - 1; right >= 1; right -= 2) {
|
|
583
|
-
// Index of right column in each column pair
|
|
584
|
-
if (right == 6)
|
|
585
|
-
right = 5;
|
|
586
|
-
for (var vert = 0; vert < this.size; vert++) {
|
|
587
|
-
// Vertical counter
|
|
588
|
-
for (var j = 0; j < 2; j++) {
|
|
589
|
-
var x = right - j; // Actual x coordinate
|
|
590
|
-
var upward = ((right + 1) & 2) == 0;
|
|
591
|
-
var y = upward ? this.size - 1 - vert : vert; // Actual y coordinate
|
|
592
|
-
if (!this.isFunction[y][x] && i < data.length * 8) {
|
|
593
|
-
this.modules[y][x] = getBit(data[i >>> 3], 7 - (i & 7));
|
|
594
|
-
i++;
|
|
595
|
-
}
|
|
596
|
-
// If this QR Code has any remainder bits (0 to 7), they were assigned as
|
|
597
|
-
// 0/false/white by the constructor and are left unchanged by this method
|
|
598
|
-
}
|
|
599
|
-
}
|
|
600
|
-
}
|
|
601
|
-
if (i != data.length * 8)
|
|
602
|
-
throw 'Assertion error';
|
|
603
|
-
};
|
|
604
|
-
// XORs the codeword modules in this QR Code with the given mask pattern.
|
|
605
|
-
// The function modules must be marked and the codeword bits must be drawn
|
|
606
|
-
// before masking. Due to the arithmetic of XOR, calling applyMask() with
|
|
607
|
-
// the same mask value a second time will undo the mask. A final well-formed
|
|
608
|
-
// QR Code needs exactly one (not zero, two, etc.) mask applied.
|
|
609
|
-
QrCode.prototype.applyMask = function (mask) {
|
|
610
|
-
if (mask < 0 || mask > 7)
|
|
611
|
-
throw 'Mask value out of range';
|
|
612
|
-
for (var y = 0; y < this.size; y++) {
|
|
613
|
-
for (var x = 0; x < this.size; x++) {
|
|
614
|
-
var invert = void 0;
|
|
615
|
-
switch (mask) {
|
|
616
|
-
case 0:
|
|
617
|
-
invert = (x + y) % 2 == 0;
|
|
618
|
-
break;
|
|
619
|
-
case 1:
|
|
620
|
-
invert = y % 2 == 0;
|
|
621
|
-
break;
|
|
622
|
-
case 2:
|
|
623
|
-
invert = x % 3 == 0;
|
|
624
|
-
break;
|
|
625
|
-
case 3:
|
|
626
|
-
invert = (x + y) % 3 == 0;
|
|
627
|
-
break;
|
|
628
|
-
case 4:
|
|
629
|
-
invert = (Math.floor(x / 3) + Math.floor(y / 2)) % 2 == 0;
|
|
630
|
-
break;
|
|
631
|
-
case 5:
|
|
632
|
-
invert = ((x * y) % 2) + ((x * y) % 3) == 0;
|
|
633
|
-
break;
|
|
634
|
-
case 6:
|
|
635
|
-
invert = (((x * y) % 2) + ((x * y) % 3)) % 2 == 0;
|
|
636
|
-
break;
|
|
637
|
-
case 7:
|
|
638
|
-
invert = (((x + y) % 2) + ((x * y) % 3)) % 2 == 0;
|
|
639
|
-
break;
|
|
640
|
-
default:
|
|
641
|
-
throw 'Assertion error';
|
|
642
|
-
}
|
|
643
|
-
if (!this.isFunction[y][x] && invert)
|
|
644
|
-
this.modules[y][x] = !this.modules[y][x];
|
|
645
|
-
}
|
|
646
|
-
}
|
|
647
|
-
};
|
|
648
|
-
// Calculates and returns the penalty score based on state of this QR Code's current modules.
|
|
649
|
-
// This is used by the automatic mask choice algorithm to find the mask pattern that yields the lowest score.
|
|
650
|
-
QrCode.prototype.getPenaltyScore = function () {
|
|
651
|
-
var result = 0;
|
|
652
|
-
// Adjacent modules in row having same color, and finder-like patterns
|
|
653
|
-
for (var y = 0; y < this.size; y++) {
|
|
654
|
-
var runHistory = [0, 0, 0, 0, 0, 0, 0];
|
|
655
|
-
var color = false;
|
|
656
|
-
var runX = 0;
|
|
657
|
-
for (var x = 0; x < this.size; x++) {
|
|
658
|
-
if (this.modules[y][x] == color) {
|
|
659
|
-
runX++;
|
|
660
|
-
if (runX == 5)
|
|
661
|
-
result += QrCode.PENALTY_N1;
|
|
662
|
-
else if (runX > 5)
|
|
663
|
-
result++;
|
|
664
|
-
}
|
|
665
|
-
else {
|
|
666
|
-
QrCode.addRunToHistory(runX, runHistory);
|
|
667
|
-
if (!color && QrCode.hasFinderLikePattern(runHistory))
|
|
668
|
-
result += QrCode.PENALTY_N3;
|
|
669
|
-
color = this.modules[y][x];
|
|
670
|
-
runX = 1;
|
|
671
|
-
}
|
|
672
|
-
}
|
|
673
|
-
QrCode.addRunToHistory(runX, runHistory);
|
|
674
|
-
if (color)
|
|
675
|
-
QrCode.addRunToHistory(0, runHistory); // Dummy run of white
|
|
676
|
-
if (QrCode.hasFinderLikePattern(runHistory))
|
|
677
|
-
result += QrCode.PENALTY_N3;
|
|
678
|
-
}
|
|
679
|
-
// Adjacent modules in column having same color, and finder-like patterns
|
|
680
|
-
for (var x = 0; x < this.size; x++) {
|
|
681
|
-
var runHistory = [0, 0, 0, 0, 0, 0, 0];
|
|
682
|
-
var color = false;
|
|
683
|
-
var runY = 0;
|
|
684
|
-
for (var y = 0; y < this.size; y++) {
|
|
685
|
-
if (this.modules[y][x] == color) {
|
|
686
|
-
runY++;
|
|
687
|
-
if (runY == 5)
|
|
688
|
-
result += QrCode.PENALTY_N1;
|
|
689
|
-
else if (runY > 5)
|
|
690
|
-
result++;
|
|
691
|
-
}
|
|
692
|
-
else {
|
|
693
|
-
QrCode.addRunToHistory(runY, runHistory);
|
|
694
|
-
if (!color && QrCode.hasFinderLikePattern(runHistory))
|
|
695
|
-
result += QrCode.PENALTY_N3;
|
|
696
|
-
color = this.modules[y][x];
|
|
697
|
-
runY = 1;
|
|
698
|
-
}
|
|
699
|
-
}
|
|
700
|
-
QrCode.addRunToHistory(runY, runHistory);
|
|
701
|
-
if (color)
|
|
702
|
-
QrCode.addRunToHistory(0, runHistory); // Dummy run of white
|
|
703
|
-
if (QrCode.hasFinderLikePattern(runHistory))
|
|
704
|
-
result += QrCode.PENALTY_N3;
|
|
705
|
-
}
|
|
706
|
-
// 2*2 blocks of modules having same color
|
|
707
|
-
for (var y = 0; y < this.size - 1; y++) {
|
|
708
|
-
for (var x = 0; x < this.size - 1; x++) {
|
|
709
|
-
var color = this.modules[y][x];
|
|
710
|
-
if (color == this.modules[y][x + 1] && color == this.modules[y + 1][x] && color == this.modules[y + 1][x + 1])
|
|
711
|
-
result += QrCode.PENALTY_N2;
|
|
712
|
-
}
|
|
713
|
-
}
|
|
714
|
-
// Balance of black and white modules
|
|
715
|
-
var black = 0;
|
|
716
|
-
for (var _i = 0, _a = this.modules; _i < _a.length; _i++) {
|
|
717
|
-
var row = _a[_i];
|
|
718
|
-
for (var _b = 0, row_1 = row; _b < row_1.length; _b++) {
|
|
719
|
-
var color = row_1[_b];
|
|
720
|
-
if (color)
|
|
721
|
-
black++;
|
|
722
|
-
}
|
|
723
|
-
}
|
|
724
|
-
var total = this.size * this.size; // Note that size is odd, so black/total != 1/2
|
|
725
|
-
// Compute the smallest integer k >= 0 such that (45-5k)% <= black/total <= (55+5k)%
|
|
726
|
-
var k = Math.ceil(Math.abs(black * 20 - total * 10) / total) - 1;
|
|
727
|
-
result += k * QrCode.PENALTY_N4;
|
|
728
|
-
return result;
|
|
729
|
-
};
|
|
730
|
-
/*-- Private helper functions --*/
|
|
731
|
-
// Returns an ascending list of positions of alignment patterns for this version number.
|
|
732
|
-
// Each position is in the range [0,177), and are used on both the x and y axes.
|
|
733
|
-
// This could be implemented as lookup table of 40 variable-length lists of integers.
|
|
734
|
-
QrCode.prototype.getAlignmentPatternPositions = function () {
|
|
735
|
-
if (this.version == 1)
|
|
736
|
-
return [];
|
|
737
|
-
else {
|
|
738
|
-
var numAlign = Math.floor(this.version / 7) + 2;
|
|
739
|
-
var step = this.version == 32 ? 26 : Math.ceil((this.size - 13) / (numAlign * 2 - 2)) * 2;
|
|
740
|
-
var result = [6];
|
|
741
|
-
for (var pos = this.size - 7; result.length < numAlign; pos -= step)
|
|
742
|
-
result.splice(1, 0, pos);
|
|
743
|
-
return result;
|
|
744
|
-
}
|
|
745
|
-
};
|
|
746
|
-
// Returns the number of data bits that can be stored in a QR Code of the given version number, after
|
|
747
|
-
// all function modules are excluded. This includes remainder bits, so it might not be a multiple of 8.
|
|
748
|
-
// The result is in the range [208, 29648]. This could be implemented as a 40-entry lookup table.
|
|
749
|
-
QrCode.getNumRawDataModules = function (ver) {
|
|
750
|
-
if (ver < QrCode.MIN_VERSION || ver > QrCode.MAX_VERSION)
|
|
751
|
-
throw 'Version number out of range';
|
|
752
|
-
var result = (16 * ver + 128) * ver + 64;
|
|
753
|
-
if (ver >= 2) {
|
|
754
|
-
var numAlign = Math.floor(ver / 7) + 2;
|
|
755
|
-
result -= (25 * numAlign - 10) * numAlign - 55;
|
|
756
|
-
if (ver >= 7)
|
|
757
|
-
result -= 36;
|
|
758
|
-
}
|
|
759
|
-
return result;
|
|
760
|
-
};
|
|
761
|
-
// Returns the number of 8-bit data (i.e. not error correction) codewords contained in any
|
|
762
|
-
// QR Code of the given version number and error correction level, with remainder bits discarded.
|
|
763
|
-
// This stateless pure function could be implemented as a (40*4)-cell lookup table.
|
|
764
|
-
QrCode.getNumDataCodewords = function (ver, ecl) {
|
|
765
|
-
return (Math.floor(QrCode.getNumRawDataModules(ver) / 8) -
|
|
766
|
-
QrCode.ECC_CODEWORDS_PER_BLOCK[ecl.ordinal][ver] * QrCode.NUM_ERROR_CORRECTION_BLOCKS[ecl.ordinal][ver]);
|
|
767
|
-
};
|
|
768
|
-
// Inserts the given value to the front of the given array, which shifts over the
|
|
769
|
-
// existing values and deletes the last value. A helper function for getPenaltyScore().
|
|
770
|
-
QrCode.addRunToHistory = function (run, history) {
|
|
771
|
-
history.pop();
|
|
772
|
-
history.unshift(run);
|
|
773
|
-
};
|
|
774
|
-
// Tests whether the given run history has the pattern of ratio 1:1:3:1:1 in the middle, and
|
|
775
|
-
// surrounded by at least 4 on either or both ends. A helper function for getPenaltyScore().
|
|
776
|
-
// Must only be called immediately after a run of white modules has ended.
|
|
777
|
-
QrCode.hasFinderLikePattern = function (runHistory) {
|
|
778
|
-
var n = runHistory[1];
|
|
779
|
-
return (n > 0 &&
|
|
780
|
-
runHistory[2] == n &&
|
|
781
|
-
runHistory[4] == n &&
|
|
782
|
-
runHistory[5] == n &&
|
|
783
|
-
runHistory[3] == n * 3 &&
|
|
784
|
-
Math.max(runHistory[0], runHistory[6]) >= n * 4);
|
|
785
|
-
};
|
|
786
|
-
/*-- Constants and tables --*/
|
|
787
|
-
// The minimum version number supported in the QR Code Model 2 standard.
|
|
788
|
-
QrCode.MIN_VERSION = 1;
|
|
789
|
-
// The maximum version number supported in the QR Code Model 2 standard.
|
|
790
|
-
QrCode.MAX_VERSION = 40;
|
|
791
|
-
// For use in getPenaltyScore(), when evaluating which mask is best.
|
|
792
|
-
QrCode.PENALTY_N1 = 3;
|
|
793
|
-
QrCode.PENALTY_N2 = 3;
|
|
794
|
-
QrCode.PENALTY_N3 = 40;
|
|
795
|
-
QrCode.PENALTY_N4 = 10;
|
|
796
|
-
QrCode.ECC_CODEWORDS_PER_BLOCK = [
|
|
797
|
-
/* eslint-disable */
|
|
798
|
-
// Version: (note that index 0 is for padding, and is set to an illegal value)
|
|
799
|
-
//0, 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 Error correction level
|
|
800
|
-
// prettier-ignore
|
|
801
|
-
[-1, 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],
|
|
802
|
-
// prettier-ignore
|
|
803
|
-
[-1, 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],
|
|
804
|
-
// prettier-ignore
|
|
805
|
-
[-1, 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],
|
|
806
|
-
// prettier-ignore
|
|
807
|
-
[-1, 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] // High
|
|
808
|
-
/* eslint-enable */
|
|
809
|
-
];
|
|
810
|
-
QrCode.NUM_ERROR_CORRECTION_BLOCKS = [
|
|
811
|
-
/* eslint-disable */
|
|
812
|
-
// Version: (note that index 0 is for padding, and is set to an illegal value)
|
|
813
|
-
//0, 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 Error correction level
|
|
814
|
-
// prettier-ignore
|
|
815
|
-
[-1, 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],
|
|
816
|
-
// prettier-ignore
|
|
817
|
-
[-1, 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],
|
|
818
|
-
// prettier-ignore
|
|
819
|
-
[-1, 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],
|
|
820
|
-
// prettier-ignore
|
|
821
|
-
[-1, 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] // High
|
|
822
|
-
/* eslint-enable */
|
|
823
|
-
];
|
|
824
|
-
return QrCode;
|
|
825
|
-
}());
|
|
826
|
-
// Returns true iff the i'th bit of x is set to 1.
|
|
827
|
-
function getBit(x, i) {
|
|
828
|
-
return ((x >>> i) & 1) != 0;
|
|
829
|
-
}
|
|
830
|
-
/*---- Data segment class ----*/
|
|
831
|
-
/*
|
|
832
|
-
* A segment of character/binary/control data in a QR Code symbol.
|
|
833
|
-
* Instances of this class are immutable.
|
|
834
|
-
* The mid-level way to create a segment is to take the payload data
|
|
835
|
-
* and call a static factory function such as QrSegment.makeNumeric().
|
|
836
|
-
* The low-level way to create a segment is to custom-make the bit buffer
|
|
837
|
-
* and call the QrSegment() constructor with appropriate values.
|
|
838
|
-
* This segment class imposes no length restrictions, but QR Codes have restrictions.
|
|
839
|
-
* Even in the most favorable conditions, a QR Code can only hold 7089 characters of data.
|
|
840
|
-
* Any segment longer than this is meaningless for the purpose of generating QR Codes.
|
|
841
|
-
*/
|
|
842
|
-
var QrSegment = /** @class */ (function () {
|
|
843
|
-
/*-- Constructor (low level) and fields --*/
|
|
844
|
-
// Creates a new QR Code segment with the given attributes and data.
|
|
845
|
-
// The character count (numChars) must agree with the mode and the bit buffer length,
|
|
846
|
-
// but the constraint isn't checked. The given bit buffer is cloned and stored.
|
|
847
|
-
function QrSegment(
|
|
848
|
-
// The mode indicator of this segment.
|
|
849
|
-
mode,
|
|
850
|
-
// The length of this segment's unencoded data. Measured in characters for
|
|
851
|
-
// numeric/alphanumeric/kanji mode, bytes for byte mode, and 0 for ECI mode.
|
|
852
|
-
// Always zero or positive. Not the same as the data's bit length.
|
|
853
|
-
numChars,
|
|
854
|
-
// The data bits of this segment. Accessed through getData().
|
|
855
|
-
bitData) {
|
|
856
|
-
this.mode = mode;
|
|
857
|
-
this.numChars = numChars;
|
|
858
|
-
this.bitData = bitData;
|
|
859
|
-
if (numChars < 0)
|
|
860
|
-
throw 'Invalid argument';
|
|
861
|
-
this.bitData = bitData.slice(); // Make defensive copy
|
|
862
|
-
}
|
|
863
|
-
/*-- Static factory functions (mid level) --*/
|
|
864
|
-
// Returns a segment representing the given binary data encoded in
|
|
865
|
-
// byte mode. All input byte arrays are acceptable. Any text string
|
|
866
|
-
// can be converted to UTF-8 bytes and encoded as a byte mode segment.
|
|
867
|
-
QrSegment.makeBytes = function (data) {
|
|
868
|
-
var bb = new BitBuffer();
|
|
869
|
-
for (var _i = 0, data_1 = data; _i < data_1.length; _i++) {
|
|
870
|
-
var b = data_1[_i];
|
|
871
|
-
bb.appendBits(b, 8);
|
|
872
|
-
}
|
|
873
|
-
return new QrSegment(QrSegment.Mode.BYTE, data.length, bb.array);
|
|
874
|
-
};
|
|
875
|
-
// Returns a segment representing the given string of decimal digits encoded in numeric mode.
|
|
876
|
-
QrSegment.makeNumeric = function (digits) {
|
|
877
|
-
if (!this.NUMERIC_REGEX.test(digits))
|
|
878
|
-
throw 'String contains non-numeric characters';
|
|
879
|
-
var bb = new BitBuffer();
|
|
880
|
-
for (var i = 0; i < digits.length;) {
|
|
881
|
-
// Consume up to 3 digits per iteration
|
|
882
|
-
var n = Math.min(digits.length - i, 3);
|
|
883
|
-
bb.appendBits(parseInt(digits.substr(i, n), 10), n * 3 + 1);
|
|
884
|
-
i += n;
|
|
885
|
-
}
|
|
886
|
-
return new QrSegment(QrSegment.Mode.NUMERIC, digits.length, bb.array);
|
|
887
|
-
};
|
|
888
|
-
// Returns a segment representing the given text string encoded in alphanumeric mode.
|
|
889
|
-
// The characters allowed are: 0 to 9, A to Z (uppercase only), space,
|
|
890
|
-
// dollar, percent, asterisk, plus, hyphen, period, slash, colon.
|
|
891
|
-
QrSegment.makeAlphanumeric = function (text) {
|
|
892
|
-
if (!this.ALPHANUMERIC_REGEX.test(text))
|
|
893
|
-
throw 'String contains unencodable characters in alphanumeric mode';
|
|
894
|
-
var bb = new BitBuffer();
|
|
895
|
-
var i;
|
|
896
|
-
for (i = 0; i + 2 <= text.length; i += 2) {
|
|
897
|
-
// Process groups of 2
|
|
898
|
-
var temp = QrSegment.ALPHANUMERIC_CHARSET.indexOf(text.charAt(i)) * 45;
|
|
899
|
-
temp += QrSegment.ALPHANUMERIC_CHARSET.indexOf(text.charAt(i + 1));
|
|
900
|
-
bb.appendBits(temp, 11);
|
|
901
|
-
}
|
|
902
|
-
if (i < text.length)
|
|
903
|
-
// 1 character remaining
|
|
904
|
-
bb.appendBits(QrSegment.ALPHANUMERIC_CHARSET.indexOf(text.charAt(i)), 6);
|
|
905
|
-
return new QrSegment(QrSegment.Mode.ALPHANUMERIC, text.length, bb.array);
|
|
906
|
-
};
|
|
907
|
-
// Returns a new mutable list of zero or more segments to represent the given Unicode text string.
|
|
908
|
-
// The result may use various segment modes and switch modes to optimize the length of the bit stream.
|
|
909
|
-
QrSegment.makeSegments = function (text) {
|
|
910
|
-
// Select the most efficient segment encoding automatically
|
|
911
|
-
if (text == '')
|
|
912
|
-
return [];
|
|
913
|
-
else if (this.NUMERIC_REGEX.test(text))
|
|
914
|
-
return [QrSegment.makeNumeric(text)];
|
|
915
|
-
else if (this.ALPHANUMERIC_REGEX.test(text))
|
|
916
|
-
return [QrSegment.makeAlphanumeric(text)];
|
|
917
|
-
else
|
|
918
|
-
return [QrSegment.makeBytes(QrSegment.toUtf8ByteArray(text))];
|
|
919
|
-
};
|
|
920
|
-
// Returns a segment representing an Extended Channel Interpretation
|
|
921
|
-
// (ECI) designator with the given assignment value.
|
|
922
|
-
QrSegment.makeEci = function (assignVal) {
|
|
923
|
-
var bb = new BitBuffer();
|
|
924
|
-
if (assignVal < 0)
|
|
925
|
-
throw 'ECI assignment value out of range';
|
|
926
|
-
else if (assignVal < 1 << 7)
|
|
927
|
-
bb.appendBits(assignVal, 8);
|
|
928
|
-
else if (assignVal < 1 << 14) {
|
|
929
|
-
bb.appendBits(2, 2);
|
|
930
|
-
bb.appendBits(assignVal, 14);
|
|
931
|
-
}
|
|
932
|
-
else if (assignVal < 1000000) {
|
|
933
|
-
bb.appendBits(6, 3);
|
|
934
|
-
bb.appendBits(assignVal, 21);
|
|
935
|
-
}
|
|
936
|
-
else
|
|
937
|
-
throw 'ECI assignment value out of range';
|
|
938
|
-
return new QrSegment(QrSegment.Mode.ECI, 0, bb.array);
|
|
939
|
-
};
|
|
940
|
-
/*-- Methods --*/
|
|
941
|
-
// Returns a new copy of the data bits of this segment.
|
|
942
|
-
QrSegment.prototype.getData = function () {
|
|
943
|
-
return this.bitData.slice(); // Make defensive copy
|
|
944
|
-
};
|
|
945
|
-
// (Package-private) Calculates and returns the number of bits needed to encode the given segments at
|
|
946
|
-
// the given version. The result is infinity if a segment has too many characters to fit its length field.
|
|
947
|
-
QrSegment.getTotalBits = function (segs, version) {
|
|
948
|
-
var result = 0;
|
|
949
|
-
for (var _i = 0, segs_2 = segs; _i < segs_2.length; _i++) {
|
|
950
|
-
var seg = segs_2[_i];
|
|
951
|
-
var ccbits = seg.mode.numCharCountBits(version);
|
|
952
|
-
if (seg.numChars >= 1 << ccbits)
|
|
953
|
-
return Infinity; // The segment's length doesn't fit the field's bit width
|
|
954
|
-
result += 4 + ccbits + seg.bitData.length;
|
|
955
|
-
}
|
|
956
|
-
return result;
|
|
957
|
-
};
|
|
958
|
-
// Returns a new array of bytes representing the given string encoded in UTF-8.
|
|
959
|
-
QrSegment.toUtf8ByteArray = function (str) {
|
|
960
|
-
str = encodeURI(str);
|
|
961
|
-
var result = [];
|
|
962
|
-
for (var i = 0; i < str.length; i++) {
|
|
963
|
-
if (str.charAt(i) != '%')
|
|
964
|
-
result.push(str.charCodeAt(i));
|
|
965
|
-
else {
|
|
966
|
-
result.push(parseInt(str.substr(i + 1, 2), 16));
|
|
967
|
-
i += 2;
|
|
968
|
-
}
|
|
969
|
-
}
|
|
970
|
-
return result;
|
|
971
|
-
};
|
|
972
|
-
/*-- Constants --*/
|
|
973
|
-
// Describes precisely all strings that are encodable in numeric mode. To test
|
|
974
|
-
// whether a string s is encodable: let ok: boolean = NUMERIC_REGEX.test(s);
|
|
975
|
-
// A string is encodable iff each character is in the range 0 to 9.
|
|
976
|
-
QrSegment.NUMERIC_REGEX = /^[0-9]*$/;
|
|
977
|
-
// Describes precisely all strings that are encodable in alphanumeric mode. To test
|
|
978
|
-
// whether a string s is encodable: let ok: boolean = ALPHANUMERIC_REGEX.test(s);
|
|
979
|
-
// A string is encodable iff each character is in the following set: 0 to 9, A to Z
|
|
980
|
-
// (uppercase only), space, dollar, percent, asterisk, plus, hyphen, period, slash, colon.
|
|
981
|
-
QrSegment.ALPHANUMERIC_REGEX = /^[A-Z0-9 $%*+.\/:-]*$/;
|
|
982
|
-
// The set of all legal characters in alphanumeric mode,
|
|
983
|
-
// where each character value maps to the index in the string.
|
|
984
|
-
QrSegment.ALPHANUMERIC_CHARSET = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:';
|
|
985
|
-
return QrSegment;
|
|
986
|
-
}());
|
|
987
|
-
/*---- Private helper classes ----*/
|
|
988
|
-
/*
|
|
989
|
-
* Computes the Reed-Solomon error correction codewords for a sequence of data codewords
|
|
990
|
-
* at a given degree. Objects are immutable, and the state only depends on the degree.
|
|
991
|
-
* This class exists because each data block in a QR Code shares the same the divisor polynomial.
|
|
992
|
-
*/
|
|
993
|
-
var ReedSolomonGenerator = /** @class */ (function () {
|
|
994
|
-
// Creates a Reed-Solomon ECC generator for the given degree. This could be implemented
|
|
995
|
-
// as a lookup table over all possible parameter values, instead of as an algorithm.
|
|
996
|
-
function ReedSolomonGenerator(degree) {
|
|
997
|
-
// Coefficients of the divisor polynomial, stored from highest to lowest power, excluding the leading term which
|
|
998
|
-
// is always 1. For example the polynomial x^3 + 255x^2 + 8x + 93 is stored as the uint8 array {255, 8, 93}.
|
|
999
|
-
this.coefficients = [];
|
|
1000
|
-
if (degree < 1 || degree > 255)
|
|
1001
|
-
throw 'Degree out of range';
|
|
1002
|
-
var coefs = this.coefficients;
|
|
1003
|
-
// Start with the monomial x^0
|
|
1004
|
-
for (var i = 0; i < degree - 1; i++)
|
|
1005
|
-
coefs.push(0);
|
|
1006
|
-
coefs.push(1);
|
|
1007
|
-
// Compute the product polynomial (x - r^0) * (x - r^1) * (x - r^2) * ... * (x - r^{degree-1}),
|
|
1008
|
-
// drop the highest term, and store the rest of the coefficients in order of descending powers.
|
|
1009
|
-
// Note that r = 0x02, which is a generator element of this field GF(2^8/0x11D).
|
|
1010
|
-
var root = 1;
|
|
1011
|
-
for (var i = 0; i < degree; i++) {
|
|
1012
|
-
// Multiply the current product by (x - r^i)
|
|
1013
|
-
for (var j = 0; j < coefs.length; j++) {
|
|
1014
|
-
coefs[j] = ReedSolomonGenerator.multiply(coefs[j], root);
|
|
1015
|
-
if (j + 1 < coefs.length)
|
|
1016
|
-
coefs[j] ^= coefs[j + 1];
|
|
1017
|
-
}
|
|
1018
|
-
root = ReedSolomonGenerator.multiply(root, 0x02);
|
|
1019
|
-
}
|
|
1020
|
-
}
|
|
1021
|
-
// Computes and returns the Reed-Solomon error correction codewords for the given
|
|
1022
|
-
// sequence of data codewords. The returned object is always a new byte array.
|
|
1023
|
-
// This method does not alter this object's state (because it is immutable).
|
|
1024
|
-
ReedSolomonGenerator.prototype.getRemainder = function (data) {
|
|
1025
|
-
// Compute the remainder by performing polynomial division
|
|
1026
|
-
var result = this.coefficients.map(function (_) { return 0; });
|
|
1027
|
-
var _loop_1 = function (b) {
|
|
1028
|
-
var factor = b ^ result.shift();
|
|
1029
|
-
result.push(0);
|
|
1030
|
-
this_1.coefficients.forEach(function (coef, i) { return (result[i] ^= ReedSolomonGenerator.multiply(coef, factor)); });
|
|
1031
|
-
};
|
|
1032
|
-
var this_1 = this;
|
|
1033
|
-
for (var _i = 0, data_2 = data; _i < data_2.length; _i++) {
|
|
1034
|
-
var b = data_2[_i];
|
|
1035
|
-
_loop_1(b);
|
|
1036
|
-
}
|
|
1037
|
-
return result;
|
|
1038
|
-
};
|
|
1039
|
-
// Returns the product of the two given field elements modulo GF(2^8/0x11D). The arguments and result
|
|
1040
|
-
// are unsigned 8-bit integers. This could be implemented as a lookup table of 256*256 entries of uint8.
|
|
1041
|
-
ReedSolomonGenerator.multiply = function (x, y) {
|
|
1042
|
-
if (x >>> 8 != 0 || y >>> 8 != 0)
|
|
1043
|
-
throw 'Byte out of range';
|
|
1044
|
-
// Russian peasant multiplication
|
|
1045
|
-
var z = 0;
|
|
1046
|
-
for (var i = 7; i >= 0; i--) {
|
|
1047
|
-
z = (z << 1) ^ ((z >>> 7) * 0x11d);
|
|
1048
|
-
z ^= ((y >>> i) & 1) * x;
|
|
1049
|
-
}
|
|
1050
|
-
if (z >>> 8 != 0)
|
|
1051
|
-
throw 'Assertion error';
|
|
1052
|
-
return z;
|
|
1053
|
-
};
|
|
1054
|
-
return ReedSolomonGenerator;
|
|
1055
|
-
}());
|
|
1056
|
-
/*
|
|
1057
|
-
* An appendable sequence of bits (0s and 1s). Mainly used by QrSegment.
|
|
1058
|
-
* The implicit constructor creates an empty bit buffer (length 0).
|
|
1059
|
-
*/
|
|
1060
|
-
var BitBuffer = /** @class */ (function () {
|
|
1061
|
-
function BitBuffer() {
|
|
1062
|
-
this.array = [];
|
|
1063
|
-
}
|
|
1064
|
-
// Appends the given number of low-order bits of the given value
|
|
1065
|
-
// to this buffer. Requires 0 <= len <= 31 and 0 <= val < 2^len.
|
|
1066
|
-
BitBuffer.prototype.appendBits = function (val, len) {
|
|
1067
|
-
if (len < 0 || len > 31 || val >>> len != 0)
|
|
1068
|
-
throw 'Value out of range';
|
|
1069
|
-
for (var i = len - 1; i >= 0; i-- // Append bit by bit
|
|
1070
|
-
)
|
|
1071
|
-
this.array.push((val >>> i) & 1);
|
|
1072
|
-
};
|
|
1073
|
-
return BitBuffer;
|
|
1074
|
-
}());
|
|
1075
|
-
/*---- Public helper enumeration ----*/
|
|
1076
|
-
(function (QrCode) {
|
|
1077
|
-
/*
|
|
1078
|
-
* The error correction level in a QR Code symbol. Immutable.
|
|
1079
|
-
*/
|
|
1080
|
-
var Ecc = /** @class */ (function () {
|
|
1081
|
-
/*-- Constructor and fields --*/
|
|
1082
|
-
function Ecc(
|
|
1083
|
-
// In the range 0 to 3 (unsigned 2-bit integer).
|
|
1084
|
-
ordinal,
|
|
1085
|
-
// (Package-private) In the range 0 to 3 (unsigned 2-bit integer).
|
|
1086
|
-
formatBits) {
|
|
1087
|
-
this.ordinal = ordinal;
|
|
1088
|
-
this.formatBits = formatBits;
|
|
1089
|
-
}
|
|
1090
|
-
/*-- Constants --*/
|
|
1091
|
-
Ecc.LOW = new Ecc(0, 1); // The QR Code can tolerate about 7% erroneous codewords
|
|
1092
|
-
Ecc.MEDIUM = new Ecc(1, 0); // The QR Code can tolerate about 15% erroneous codewords
|
|
1093
|
-
Ecc.QUARTILE = new Ecc(2, 3); // The QR Code can tolerate about 25% erroneous codewords
|
|
1094
|
-
Ecc.HIGH = new Ecc(3, 2); // The QR Code can tolerate about 30% erroneous codewords
|
|
1095
|
-
return Ecc;
|
|
1096
|
-
}());
|
|
1097
|
-
QrCode.Ecc = Ecc;
|
|
1098
|
-
})(QrCode || (QrCode = {}));
|
|
1099
|
-
/*---- Public helper enumeration ----*/
|
|
1100
|
-
(function (QrSegment) {
|
|
1101
|
-
/*
|
|
1102
|
-
* Describes how a segment's data bits are interpreted. Immutable.
|
|
1103
|
-
*/
|
|
1104
|
-
var Mode = /** @class */ (function () {
|
|
1105
|
-
/*-- Constructor and fields --*/
|
|
1106
|
-
function Mode(
|
|
1107
|
-
// The mode indicator bits, which is a uint4 value (range 0 to 15).
|
|
1108
|
-
modeBits,
|
|
1109
|
-
// Number of character count bits for three different version ranges.
|
|
1110
|
-
numBitsCharCount) {
|
|
1111
|
-
this.modeBits = modeBits;
|
|
1112
|
-
this.numBitsCharCount = numBitsCharCount;
|
|
1113
|
-
}
|
|
1114
|
-
/*-- Method --*/
|
|
1115
|
-
// (Package-private) Returns the bit width of the character count field for a segment in
|
|
1116
|
-
// this mode in a QR Code at the given version number. The result is in the range [0, 16].
|
|
1117
|
-
Mode.prototype.numCharCountBits = function (ver) {
|
|
1118
|
-
return this.numBitsCharCount[Math.floor((ver + 7) / 17)];
|
|
1119
|
-
};
|
|
1120
|
-
/*-- Constants --*/
|
|
1121
|
-
Mode.NUMERIC = new Mode(0x1, [10, 12, 14]);
|
|
1122
|
-
Mode.ALPHANUMERIC = new Mode(0x2, [9, 11, 13]);
|
|
1123
|
-
Mode.BYTE = new Mode(0x4, [8, 16, 16]);
|
|
1124
|
-
Mode.KANJI = new Mode(0x8, [8, 10, 12]);
|
|
1125
|
-
Mode.ECI = new Mode(0x7, [0, 0, 0]);
|
|
1126
|
-
return Mode;
|
|
1127
|
-
}());
|
|
1128
|
-
QrSegment.Mode = Mode;
|
|
1129
|
-
})(QrSegment || (QrSegment = {}));
|
|
1130
|
-
|
|
1131
|
-
/** Default width and height of QR code */
|
|
1132
|
-
var DEFAULT_SIZE = 128;
|
|
1133
|
-
/** Default background color of QR code*/
|
|
1134
|
-
var BACKGROUND_COLOR_DEFAULT = '#ffffff';
|
|
1135
|
-
/** Default foreground color */
|
|
1136
|
-
var FOREGROUND_COLOR_DEFAULT = '#000000';
|
|
1137
|
-
/** Default logo color */
|
|
1138
|
-
var LOGO_COLOR_DEFAULT = '#07f';
|
|
1139
|
-
/**
|
|
1140
|
-
* Implementation
|
|
1141
|
-
*/
|
|
1142
|
-
function createQR(text, qrSizeOrOptions, classNameLegacy, legacyOptions) {
|
|
1143
|
-
if (typeof text !== 'string') {
|
|
1144
|
-
throw new TypeError('Enter text for encoding');
|
|
1145
|
-
}
|
|
1146
|
-
// Legacy interface resolve
|
|
1147
|
-
var options = __assign(__assign(__assign({}, (typeof qrSizeOrOptions === 'object' && qrSizeOrOptions !== null ? qrSizeOrOptions : {})), (typeof legacyOptions === 'object' && legacyOptions !== null ? legacyOptions : {})), { qrSize: typeof qrSizeOrOptions === 'object' && qrSizeOrOptions !== null && typeof qrSizeOrOptions.qrSize === 'number'
|
|
1148
|
-
? qrSizeOrOptions.qrSize
|
|
1149
|
-
: qrSizeOrOptions, className: typeof qrSizeOrOptions === 'object' && qrSizeOrOptions !== null && typeof qrSizeOrOptions.className === 'string'
|
|
1150
|
-
? qrSizeOrOptions.className
|
|
1151
|
-
: classNameLegacy });
|
|
1152
|
-
// ECC levels mapping
|
|
1153
|
-
var eccLevels = [
|
|
1154
|
-
QrCode.Ecc.LOW,
|
|
1155
|
-
QrCode.Ecc.MEDIUM,
|
|
1156
|
-
QrCode.Ecc.QUARTILE,
|
|
1157
|
-
QrCode.Ecc.HIGH
|
|
1158
|
-
];
|
|
1159
|
-
// Fallback undefined options
|
|
1160
|
-
var fallbackOptions = {
|
|
1161
|
-
qrSize: typeof options.qrSize === 'number' ? options.qrSize : DEFAULT_SIZE,
|
|
1162
|
-
className: typeof options.className === 'string' ? options.className : classNameLegacy || '',
|
|
1163
|
-
isShowLogo: !!options.isShowLogo || false,
|
|
1164
|
-
isShowBackground: !!options.isShowBackground || false,
|
|
1165
|
-
foregroundColor: typeof options.foregroundColor === 'string' ? options.foregroundColor : FOREGROUND_COLOR_DEFAULT,
|
|
1166
|
-
backgroundColor: typeof options.backgroundColor === 'string' ? options.backgroundColor : BACKGROUND_COLOR_DEFAULT,
|
|
1167
|
-
logoColor: typeof options.logoColor === 'string' ? options.logoColor : LOGO_COLOR_DEFAULT,
|
|
1168
|
-
suffix: options.suffix ? options.suffix.toString() : '0',
|
|
1169
|
-
logoData: typeof options.logoData === 'string' ? options.logoData : null,
|
|
1170
|
-
ecc: (typeof options.ecc === 'number' && eccLevels[options.ecc] ? options.ecc : 3),
|
|
1171
|
-
};
|
|
1172
|
-
// Code generation
|
|
1173
|
-
var segments = QrSegment.makeSegments(text);
|
|
1174
|
-
var qrCode = QrCode.encodeSegments(segments, eccLevels[fallbackOptions.ecc], 1, 40, -1, true);
|
|
1175
|
-
var svgCode = convertSegmentsToSvgString(qrCode, fallbackOptions);
|
|
1176
|
-
return svgCode;
|
|
1177
|
-
}
|
|
1178
|
-
var index = {
|
|
1179
|
-
createQR: createQR
|
|
1180
|
-
};
|
|
1181
|
-
|
|
1182
|
-
export default index;
|