qrcodegen.js 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  1. import _createForOfIteratorHelper from "@babel/runtime/helpers/esm/createForOfIteratorHelper";
  2. import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
  3. import _createClass from "@babel/runtime/helpers/esm/createClass";
  4. import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
  5. var _class, _class2;
  6. // Copyright (c) Project Nayuki. (MIT License)
  7. // https://www.nayuki.io/page/qr-code-generator-library
  8. // Modification with code reorder and prettier
  9. // --------------------------------------------
  10. // Appends the given number of low-order bits of the given value
  11. // to the given buffer. Requires 0 <= len <= 31 and 0 <= val < 2^len.
  12. function appendBits(val, len, bb) {
  13. if (len < 0 || len > 31 || val >>> len != 0) {
  14. throw new RangeError('Value out of range');
  15. }
  16. for (var i = len - 1; i >= 0; i-- // Append bit by bit
  17. ) {
  18. bb.push(val >>> i & 1);
  19. }
  20. }
  21. // Returns true iff the i'th bit of x is set to 1.
  22. function getBit(x, i) {
  23. return (x >>> i & 1) != 0;
  24. }
  25. // Throws an exception if the given condition is false.
  26. function assert(cond) {
  27. if (!cond) {
  28. throw new Error('Assertion error');
  29. }
  30. }
  31. /*---- Public helper enumeration ----*/
  32. /*
  33. * Describes how a segment's data bits are numbererpreted. Immutable.
  34. */
  35. export var Mode = /*#__PURE__*/function () {
  36. function Mode(modeBits, numBitsCharCount) {
  37. _classCallCheck(this, Mode);
  38. /*-- Constructor and fields --*/
  39. // The mode indicator bits, which is a unumber4 value (range 0 to 15).
  40. _defineProperty(this, "modeBits", void 0);
  41. // Number of character count bits for three different version ranges.
  42. _defineProperty(this, "numBitsCharCount", void 0);
  43. this.modeBits = modeBits;
  44. this.numBitsCharCount = numBitsCharCount;
  45. }
  46. /*-- Method --*/
  47. // (Package-private) Returns the bit width of the character count field for a segment in
  48. // this mode in a QR Code at the given version number. The result is in the range [0, 16].
  49. _createClass(Mode, [{
  50. key: "numCharCountBits",
  51. value: function numCharCountBits(ver) {
  52. return this.numBitsCharCount[Math.floor((ver + 7) / 17)];
  53. }
  54. }]);
  55. return Mode;
  56. }();
  57. /*---- Public helper enumeration ----*/
  58. /*
  59. * The error correction level in a QR Code symbol. Immutable.
  60. */
  61. _class = Mode;
  62. /*-- Constants --*/
  63. _defineProperty(Mode, "NUMERIC", new _class(0x1, [10, 12, 14]));
  64. _defineProperty(Mode, "ALPHANUMERIC", new _class(0x2, [9, 11, 13]));
  65. _defineProperty(Mode, "BYTE", new _class(0x4, [8, 16, 16]));
  66. _defineProperty(Mode, "KANJI", new _class(0x8, [8, 10, 12]));
  67. _defineProperty(Mode, "ECI", new _class(0x7, [0, 0, 0]));
  68. export var Ecc = /*#__PURE__*/_createClass(function Ecc(ordinal, formatBits) {
  69. _classCallCheck(this, Ecc);
  70. // The QR Code can tolerate about 30% erroneous codewords
  71. /*-- Constructor and fields --*/
  72. // In the range 0 to 3 (unsigned 2-bit numbereger).
  73. _defineProperty(this, "ordinal", void 0);
  74. // (Package-private) In the range 0 to 3 (unsigned 2-bit numbereger).
  75. _defineProperty(this, "formatBits", void 0);
  76. this.ordinal = ordinal;
  77. this.formatBits = formatBits;
  78. });
  79. /*
  80. * A segment of character/binary/control data in a QR Code symbol.
  81. * Instances of this class are immutable.
  82. * The mid-level way to create a segment is to take the payload data
  83. * and call a static factory function such as QrSegment.makeNumeric().
  84. * The low-level way to create a segment is to custom-make the bit buffer
  85. * and call the QrSegment() constructor with appropriate values.
  86. * This segment class imposes no length restrictions, but QR Codes have restrictions.
  87. * Even in the most favorable conditions, a QR Code can only hold 7089 characters of data.
  88. * Any segment longer than this is meaningless for the purpose of generating QR Codes.
  89. */
  90. _class2 = Ecc;
  91. /*-- Constants --*/
  92. _defineProperty(Ecc, "LOW", new _class2(0, 1));
  93. // The QR Code can tolerate about 7% erroneous codewords
  94. _defineProperty(Ecc, "MEDIUM", new _class2(1, 0));
  95. // The QR Code can tolerate about 15% erroneous codewords
  96. _defineProperty(Ecc, "QUARTILE", new _class2(2, 3));
  97. // The QR Code can tolerate about 25% erroneous codewords
  98. _defineProperty(Ecc, "HIGH", new _class2(3, 2));
  99. export var QrSegment = /*#__PURE__*/function () {
  100. // Creates a new QR Code segment with the given attributes and data.
  101. // The character count (numChars) must agree with the mode and the bit buffer length,
  102. // but the constranumber isn't checked. The given bit buffer is cloned and stored.
  103. function QrSegment(mode, numChars, bitData) {
  104. _classCallCheck(this, QrSegment);
  105. /*-- Constructor (low level) and fields --*/
  106. // The mode indicator of this segment.
  107. _defineProperty(this, "mode", void 0);
  108. // The length of this segment's unencoded data. Measured in characters for
  109. // numeric/alphanumeric/kanji mode, bytes for byte mode, and 0 for ECI mode.
  110. // Always zero or positive. Not the same as the data's bit length.
  111. _defineProperty(this, "numChars", void 0);
  112. // The data bits of this segment. Accessed through getData().
  113. _defineProperty(this, "bitData", void 0);
  114. this.mode = mode;
  115. this.numChars = numChars;
  116. this.bitData = bitData;
  117. if (numChars < 0) {
  118. throw new RangeError('Invalid argument');
  119. }
  120. this.bitData = bitData.slice(); // Make defensive copy
  121. }
  122. /*-- Methods --*/
  123. // Returns a new copy of the data bits of this segment.
  124. _createClass(QrSegment, [{
  125. key: "getData",
  126. value: function getData() {
  127. return this.bitData.slice(); // Make defensive copy
  128. }
  129. // (Package-private) Calculates and returns the number of bits needed to encode the given segments at
  130. // the given version. The result is infinity if a segment has too many characters to fit its length field.
  131. }], [{
  132. key: "makeBytes",
  133. value: /*-- Static factory functions (mid level) --*/
  134. // Returns a segment representing the given binary data encoded in
  135. // byte mode. All input byte arrays are acceptable. Any text string
  136. // can be converted to UTF-8 bytes and encoded as a byte mode segment.
  137. function makeBytes(data) {
  138. var bb = [];
  139. var _iterator = _createForOfIteratorHelper(data),
  140. _step;
  141. try {
  142. for (_iterator.s(); !(_step = _iterator.n()).done;) {
  143. var b = _step.value;
  144. appendBits(b, 8, bb);
  145. }
  146. } catch (err) {
  147. _iterator.e(err);
  148. } finally {
  149. _iterator.f();
  150. }
  151. return new QrSegment(Mode.BYTE, data.length, bb);
  152. }
  153. // Returns a segment representing the given string of decimal digits encoded in numeric mode.
  154. }, {
  155. key: "makeNumeric",
  156. value: function makeNumeric(digits) {
  157. if (!QrSegment.isNumeric(digits)) {
  158. throw new RangeError('String contains non-numeric characters');
  159. }
  160. var bb = [];
  161. for (var i = 0; i < digits.length;) {
  162. // Consume up to 3 digits per iteration
  163. var n = Math.min(digits.length - i, 3);
  164. appendBits(parseInt(digits.substring(i, i + n), 10), n * 3 + 1, bb);
  165. i += n;
  166. }
  167. return new QrSegment(Mode.NUMERIC, digits.length, bb);
  168. }
  169. // Returns a segment representing the given text string encoded in alphanumeric mode.
  170. // The characters allowed are: 0 to 9, A to Z (uppercase only), space,
  171. // dollar, percent, asterisk, plus, hyphen, period, slash, colon.
  172. }, {
  173. key: "makeAlphanumeric",
  174. value: function makeAlphanumeric(text) {
  175. if (!QrSegment.isAlphanumeric(text)) {
  176. throw new RangeError('String contains unencodable characters in alphanumeric mode');
  177. }
  178. var bb = [];
  179. var i;
  180. for (i = 0; i + 2 <= text.length; i += 2) {
  181. // Process groups of 2
  182. var temp = QrSegment.ALPHANUMERIC_CHARSET.indexOf(text.charAt(i)) * 45;
  183. temp += QrSegment.ALPHANUMERIC_CHARSET.indexOf(text.charAt(i + 1));
  184. appendBits(temp, 11, bb);
  185. }
  186. if (i < text.length) {
  187. // 1 character remaining
  188. appendBits(QrSegment.ALPHANUMERIC_CHARSET.indexOf(text.charAt(i)), 6, bb);
  189. }
  190. return new QrSegment(Mode.ALPHANUMERIC, text.length, bb);
  191. }
  192. // Returns a new mutable list of zero or more segments to represent the given Unicode text string.
  193. // The result may use various segment modes and switch modes to optimize the length of the bit stream.
  194. }, {
  195. key: "makeSegments",
  196. value: function makeSegments(text) {
  197. // Select the most efficient segment encoding automatically
  198. if (text == '') {
  199. return [];
  200. } else if (QrSegment.isNumeric(text)) {
  201. return [QrSegment.makeNumeric(text)];
  202. } else if (QrSegment.isAlphanumeric(text)) {
  203. return [QrSegment.makeAlphanumeric(text)];
  204. } else {
  205. return [QrSegment.makeBytes(QrSegment.toUtf8ByteArray(text))];
  206. }
  207. }
  208. // Returns a segment representing an Extended Channel Interpretation
  209. // (ECI) designator with the given assignment value.
  210. }, {
  211. key: "makeEci",
  212. value: function makeEci(assignVal) {
  213. var bb = [];
  214. if (assignVal < 0) {
  215. throw new RangeError('ECI assignment value out of range');
  216. } else if (assignVal < 1 << 7) {
  217. appendBits(assignVal, 8, bb);
  218. } else if (assignVal < 1 << 14) {
  219. appendBits(2, 2, bb);
  220. appendBits(assignVal, 14, bb);
  221. } else if (assignVal < 1000000) {
  222. appendBits(6, 3, bb);
  223. appendBits(assignVal, 21, bb);
  224. } else {
  225. throw new RangeError('ECI assignment value out of range');
  226. }
  227. return new QrSegment(Mode.ECI, 0, bb);
  228. }
  229. // Tests whether the given string can be encoded as a segment in numeric mode.
  230. // A string is encodable iff each character is in the range 0 to 9.
  231. }, {
  232. key: "isNumeric",
  233. value: function isNumeric(text) {
  234. return QrSegment.NUMERIC_REGEX.test(text);
  235. }
  236. // Tests whether the given string can be encoded as a segment in alphanumeric mode.
  237. // A string is encodable iff each character is in the following set: 0 to 9, A to Z
  238. // (uppercase only), space, dollar, percent, asterisk, plus, hyphen, period, slash, colon.
  239. }, {
  240. key: "isAlphanumeric",
  241. value: function isAlphanumeric(text) {
  242. return QrSegment.ALPHANUMERIC_REGEX.test(text);
  243. }
  244. }, {
  245. key: "getTotalBits",
  246. value: function getTotalBits(segs, version) {
  247. var result = 0;
  248. var _iterator2 = _createForOfIteratorHelper(segs),
  249. _step2;
  250. try {
  251. for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
  252. var seg = _step2.value;
  253. var ccbits = seg.mode.numCharCountBits(version);
  254. if (seg.numChars >= 1 << ccbits) {
  255. return Infinity; // The segment's length doesn't fit the field's bit width
  256. }
  257. result += 4 + ccbits + seg.bitData.length;
  258. }
  259. } catch (err) {
  260. _iterator2.e(err);
  261. } finally {
  262. _iterator2.f();
  263. }
  264. return result;
  265. }
  266. // Returns a new array of bytes representing the given string encoded in UTF-8.
  267. }, {
  268. key: "toUtf8ByteArray",
  269. value: function toUtf8ByteArray(input) {
  270. var str = encodeURI(input);
  271. var result = [];
  272. for (var i = 0; i < str.length; i++) {
  273. if (str.charAt(i) != '%') {
  274. result.push(str.charCodeAt(i));
  275. } else {
  276. result.push(parseInt(str.substring(i + 1, i + 3), 16));
  277. i += 2;
  278. }
  279. }
  280. return result;
  281. }
  282. /*-- Constants --*/
  283. // Describes precisely all strings that are encodable in numeric mode.
  284. }]);
  285. return QrSegment;
  286. }();
  287. /*
  288. * A QR Code symbol, which is a type of two-dimension barcode.
  289. * Invented by Denso Wave and described in the ISO/IEC 18004 standard.
  290. * Instances of this class represent an immutable square grid of dark and light cells.
  291. * The class provides static factory functions to create a QR Code from text or binary data.
  292. * The class covers the QR Code Model 2 specification, supporting all versions (sizes)
  293. * from 1 to 40, all 4 error correction levels, and 4 character encoding modes.
  294. *
  295. * Ways to create a QR Code object:
  296. * - High level: Take the payload data and call QrCode.encodeText() or QrCode.encodeBinary().
  297. * - Mid level: Custom-make the list of segments and call QrCode.encodeSegments().
  298. * - Low level: Custom-make the array of data codeword bytes (including
  299. * segment headers and final padding, excluding error correction codewords),
  300. * supply the appropriate version number, and call the QrCode() constructor.
  301. * (Note that all ways require supplying the desired error correction level.)
  302. */
  303. _defineProperty(QrSegment, "NUMERIC_REGEX", /^[0-9]*$/);
  304. // Describes precisely all strings that are encodable in alphanumeric mode.
  305. _defineProperty(QrSegment, "ALPHANUMERIC_REGEX", /^[A-Z0-9 $%*+.\/:-]*$/);
  306. // The set of all legal characters in alphanumeric mode,
  307. // where each character value maps to the index in the string.
  308. _defineProperty(QrSegment, "ALPHANUMERIC_CHARSET", '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:');
  309. export var QrCode = /*#__PURE__*/function () {
  310. // Creates a new QR Code with the given version number,
  311. // error correction level, data codeword bytes, and mask number.
  312. // This is a low-level API that most users should not use directly.
  313. // A mid-level API is the encodeSegments() function.
  314. function QrCode(
  315. // The version number of this QR Code, which is between 1 and 40 (inclusive).
  316. // This determines the size of this barcode.
  317. version,
  318. // The error correction level used in this QR Code.
  319. errorCorrectionLevel, dataCodewords, oriMsk) {
  320. _classCallCheck(this, QrCode);
  321. /*-- Fields --*/
  322. // The width and height of this QR Code, measured in modules, between
  323. // 21 and 177 (inclusive). This is equal to version * 4 + 17.
  324. _defineProperty(this, "size", void 0);
  325. // The index of the mask pattern used in this QR Code, which is between 0 and 7 (inclusive).
  326. // Even if a QR Code is created with automatic masking requested (mask = -1),
  327. // the resulting object still has a mask value between 0 and 7.
  328. _defineProperty(this, "mask", void 0);
  329. // The modules of this QR Code (false = light, true = dark).
  330. // Immutable after constructor finishes. Accessed through getModule().
  331. _defineProperty(this, "modules", []);
  332. // Indicates function modules that are not subjected to masking. Discarded when constructor finishes.
  333. _defineProperty(this, "isFunction", []);
  334. /*-- Constructor (low level) and fields --*/
  335. // The version number of this QR Code, which is between 1 and 40 (inclusive).
  336. // This determines the size of this barcode.
  337. _defineProperty(this, "version", void 0);
  338. // The error correction level used in this QR Code.
  339. _defineProperty(this, "errorCorrectionLevel", void 0);
  340. var msk = oriMsk;
  341. this.version = version;
  342. this.errorCorrectionLevel = errorCorrectionLevel;
  343. // Check scalar arguments
  344. if (version < QrCode.MIN_VERSION || version > QrCode.MAX_VERSION) {
  345. throw new RangeError('Version value out of range');
  346. }
  347. if (msk < -1 || msk > 7) {
  348. throw new RangeError('Mask value out of range');
  349. }
  350. this.size = version * 4 + 17;
  351. // Initialize both grids to be size*size arrays of Boolean false
  352. var row = [];
  353. for (var i = 0; i < this.size; i++) {
  354. row.push(false);
  355. }
  356. for (var _i = 0; _i < this.size; _i++) {
  357. this.modules.push(row.slice()); // Initially all light
  358. this.isFunction.push(row.slice());
  359. }
  360. // Compute ECC, draw modules
  361. this.drawFunctionPatterns();
  362. var allCodewords = this.addEccAndInterleave(dataCodewords);
  363. this.drawCodewords(allCodewords);
  364. // Do masking
  365. if (msk == -1) {
  366. // Automatically choose best mask
  367. var minPenalty = 1000000000;
  368. for (var _i2 = 0; _i2 < 8; _i2++) {
  369. this.applyMask(_i2);
  370. this.drawFormatBits(_i2);
  371. var penalty = this.getPenaltyScore();
  372. if (penalty < minPenalty) {
  373. msk = _i2;
  374. minPenalty = penalty;
  375. }
  376. this.applyMask(_i2); // Undoes the mask due to XOR
  377. }
  378. }
  379. assert(0 <= msk && msk <= 7);
  380. this.mask = msk;
  381. this.applyMask(msk); // Apply the final choice of mask
  382. this.drawFormatBits(msk); // Overwrite old format bits
  383. this.isFunction = [];
  384. }
  385. /*-- Accessor methods --*/
  386. // Returns the color of the module (pixel) at the given coordinates, which is false
  387. // for light or true for dark. The top left corner has the coordinates (x=0, y=0).
  388. // If the given coordinates are out of bounds, then false (light) is returned.
  389. _createClass(QrCode, [{
  390. key: "getModule",
  391. value: function getModule(x, y) {
  392. return 0 <= x && x < this.size && 0 <= y && y < this.size && this.modules[y][x];
  393. }
  394. // Modified to expose modules for easy access
  395. }, {
  396. key: "getModules",
  397. value: function getModules() {
  398. return this.modules;
  399. }
  400. /*-- Private helper methods for constructor: Drawing function modules --*/
  401. // Reads this object's version field, and draws and marks all function modules.
  402. }, {
  403. key: "drawFunctionPatterns",
  404. value: function drawFunctionPatterns() {
  405. // Draw horizontal and vertical timing patterns
  406. for (var i = 0; i < this.size; i++) {
  407. this.setFunctionModule(6, i, i % 2 == 0);
  408. this.setFunctionModule(i, 6, i % 2 == 0);
  409. }
  410. // Draw 3 finder patterns (all corners except bottom right; overwrites some timing modules)
  411. this.drawFinderPattern(3, 3);
  412. this.drawFinderPattern(this.size - 4, 3);
  413. this.drawFinderPattern(3, this.size - 4);
  414. // Draw numerous alignment patterns
  415. var alignPatPos = this.getAlignmentPatternPositions();
  416. var numAlign = alignPatPos.length;
  417. for (var _i3 = 0; _i3 < numAlign; _i3++) {
  418. for (var j = 0; j < numAlign; j++) {
  419. // Don't draw on the three finder corners
  420. if (!(_i3 == 0 && j == 0 || _i3 == 0 && j == numAlign - 1 || _i3 == numAlign - 1 && j == 0)) {
  421. this.drawAlignmentPattern(alignPatPos[_i3], alignPatPos[j]);
  422. }
  423. }
  424. }
  425. // Draw configuration data
  426. this.drawFormatBits(0); // Dummy mask value; overwritten later in the constructor
  427. this.drawVersion();
  428. }
  429. // Draws two copies of the format bits (with its own error correction code)
  430. // based on the given mask and this object's error correction level field.
  431. }, {
  432. key: "drawFormatBits",
  433. value: function drawFormatBits(mask) {
  434. // Calculate error correction code and pack bits
  435. var data = this.errorCorrectionLevel.formatBits << 3 | mask; // errCorrLvl is unumber2, mask is unumber3
  436. var rem = data;
  437. for (var i = 0; i < 10; i++) {
  438. rem = rem << 1 ^ (rem >>> 9) * 0x537;
  439. }
  440. var bits = (data << 10 | rem) ^ 0x5412; // unumber15
  441. assert(bits >>> 15 == 0);
  442. // Draw first copy
  443. for (var _i4 = 0; _i4 <= 5; _i4++) {
  444. this.setFunctionModule(8, _i4, getBit(bits, _i4));
  445. }
  446. this.setFunctionModule(8, 7, getBit(bits, 6));
  447. this.setFunctionModule(8, 8, getBit(bits, 7));
  448. this.setFunctionModule(7, 8, getBit(bits, 8));
  449. for (var _i5 = 9; _i5 < 15; _i5++) {
  450. this.setFunctionModule(14 - _i5, 8, getBit(bits, _i5));
  451. }
  452. // Draw second copy
  453. for (var _i6 = 0; _i6 < 8; _i6++) {
  454. this.setFunctionModule(this.size - 1 - _i6, 8, getBit(bits, _i6));
  455. }
  456. for (var _i7 = 8; _i7 < 15; _i7++) {
  457. this.setFunctionModule(8, this.size - 15 + _i7, getBit(bits, _i7));
  458. }
  459. this.setFunctionModule(8, this.size - 8, true); // Always dark
  460. }
  461. // Draws two copies of the version bits (with its own error correction code),
  462. // based on this object's version field, iff 7 <= version <= 40.
  463. }, {
  464. key: "drawVersion",
  465. value: function drawVersion() {
  466. if (this.version < 7) {
  467. return;
  468. }
  469. // Calculate error correction code and pack bits
  470. var rem = this.version; // version is unumber6, in the range [7, 40]
  471. for (var i = 0; i < 12; i++) {
  472. rem = rem << 1 ^ (rem >>> 11) * 0x1f25;
  473. }
  474. var bits = this.version << 12 | rem; // unumber18
  475. assert(bits >>> 18 == 0);
  476. // Draw two copies
  477. for (var _i8 = 0; _i8 < 18; _i8++) {
  478. var color = getBit(bits, _i8);
  479. var a = this.size - 11 + _i8 % 3;
  480. var b = Math.floor(_i8 / 3);
  481. this.setFunctionModule(a, b, color);
  482. this.setFunctionModule(b, a, color);
  483. }
  484. }
  485. // Draws a 9*9 finder pattern including the border separator,
  486. // with the center module at (x, y). Modules can be out of bounds.
  487. }, {
  488. key: "drawFinderPattern",
  489. value: function drawFinderPattern(x, y) {
  490. for (var dy = -4; dy <= 4; dy++) {
  491. for (var dx = -4; dx <= 4; dx++) {
  492. var dist = Math.max(Math.abs(dx), Math.abs(dy)); // Chebyshev/infinity norm
  493. var xx = x + dx;
  494. var yy = y + dy;
  495. if (0 <= xx && xx < this.size && 0 <= yy && yy < this.size) {
  496. this.setFunctionModule(xx, yy, dist != 2 && dist != 4);
  497. }
  498. }
  499. }
  500. }
  501. // Draws a 5*5 alignment pattern, with the center module
  502. // at (x, y). All modules must be in bounds.
  503. }, {
  504. key: "drawAlignmentPattern",
  505. value: function drawAlignmentPattern(x, y) {
  506. for (var dy = -2; dy <= 2; dy++) {
  507. for (var dx = -2; dx <= 2; dx++) this.setFunctionModule(x + dx, y + dy, Math.max(Math.abs(dx), Math.abs(dy)) != 1);
  508. }
  509. }
  510. // Sets the color of a module and marks it as a function module.
  511. // Only used by the constructor. Coordinates must be in bounds.
  512. }, {
  513. key: "setFunctionModule",
  514. value: function setFunctionModule(x, y, isDark) {
  515. this.modules[y][x] = isDark;
  516. this.isFunction[y][x] = true;
  517. }
  518. /*-- Private helper methods for constructor: Codewords and masking --*/
  519. // Returns a new byte string representing the given data with the appropriate error correction
  520. // codewords appended to it, based on this object's version and error correction level.
  521. }, {
  522. key: "addEccAndInterleave",
  523. value: function addEccAndInterleave(data) {
  524. var ver = this.version;
  525. var ecl = this.errorCorrectionLevel;
  526. if (data.length != QrCode.getNumDataCodewords(ver, ecl)) {
  527. throw new RangeError('Invalid argument');
  528. }
  529. // Calculate parameter numbers
  530. var numBlocks = QrCode.NUM_ERROR_CORRECTION_BLOCKS[ecl.ordinal][ver];
  531. var blockEccLen = QrCode.ECC_CODEWORDS_PER_BLOCK[ecl.ordinal][ver];
  532. var rawCodewords = Math.floor(QrCode.getNumRawDataModules(ver) / 8);
  533. var numShortBlocks = numBlocks - rawCodewords % numBlocks;
  534. var shortBlockLen = Math.floor(rawCodewords / numBlocks);
  535. // Split data numbero blocks and append ECC to each block
  536. var blocks = [];
  537. var rsDiv = QrCode.reedSolomonComputeDivisor(blockEccLen);
  538. for (var i = 0, k = 0; i < numBlocks; i++) {
  539. var dat = data.slice(k, k + shortBlockLen - blockEccLen + (i < numShortBlocks ? 0 : 1));
  540. k += dat.length;
  541. var ecc = QrCode.reedSolomonComputeRemainder(dat, rsDiv);
  542. if (i < numShortBlocks) {
  543. dat.push(0);
  544. }
  545. blocks.push(dat.concat(ecc));
  546. }
  547. // Interleave (not concatenate) the bytes from every block numbero a single sequence
  548. var result = [];
  549. var _loop = function _loop(_i9) {
  550. blocks.forEach(function (block, j) {
  551. // Skip the padding byte in short blocks
  552. if (_i9 != shortBlockLen - blockEccLen || j >= numShortBlocks) {
  553. result.push(block[_i9]);
  554. }
  555. });
  556. };
  557. for (var _i9 = 0; _i9 < blocks[0].length; _i9++) {
  558. _loop(_i9);
  559. }
  560. assert(result.length == rawCodewords);
  561. return result;
  562. }
  563. // Draws the given sequence of 8-bit codewords (data and error correction) onto the entire
  564. // data area of this QR Code. Function modules need to be marked off before this is called.
  565. }, {
  566. key: "drawCodewords",
  567. value: function drawCodewords(data) {
  568. if (data.length != Math.floor(QrCode.getNumRawDataModules(this.version) / 8)) {
  569. throw new RangeError('Invalid argument');
  570. }
  571. var i = 0; // Bit index numbero the data
  572. // Do the funny zigzag scan
  573. for (var right = this.size - 1; right >= 1; right -= 2) {
  574. // Index of right column in each column pair
  575. if (right == 6) {
  576. right = 5;
  577. }
  578. for (var vert = 0; vert < this.size; vert++) {
  579. // Vertical counter
  580. for (var j = 0; j < 2; j++) {
  581. var x = right - j; // Actual x coordinate
  582. var upward = (right + 1 & 2) == 0;
  583. var y = upward ? this.size - 1 - vert : vert; // Actual y coordinate
  584. if (!this.isFunction[y][x] && i < data.length * 8) {
  585. this.modules[y][x] = getBit(data[i >>> 3], 7 - (i & 7));
  586. i++;
  587. }
  588. // If this QR Code has any remainder bits (0 to 7), they were assigned as
  589. // 0/false/light by the constructor and are left unchanged by this method
  590. }
  591. }
  592. }
  593. assert(i == data.length * 8);
  594. }
  595. // XORs the codeword modules in this QR Code with the given mask pattern.
  596. // The function modules must be marked and the codeword bits must be drawn
  597. // before masking. Due to the arithmetic of XOR, calling applyMask() with
  598. // the same mask value a second time will undo the mask. A final well-formed
  599. // QR Code needs exactly one (not zero, two, etc.) mask applied.
  600. }, {
  601. key: "applyMask",
  602. value: function applyMask(mask) {
  603. if (mask < 0 || mask > 7) {
  604. throw new RangeError('Mask value out of range');
  605. }
  606. for (var y = 0; y < this.size; y++) {
  607. for (var x = 0; x < this.size; x++) {
  608. var invert = void 0;
  609. switch (mask) {
  610. case 0:
  611. invert = (x + y) % 2 == 0;
  612. break;
  613. case 1:
  614. invert = y % 2 == 0;
  615. break;
  616. case 2:
  617. invert = x % 3 == 0;
  618. break;
  619. case 3:
  620. invert = (x + y) % 3 == 0;
  621. break;
  622. case 4:
  623. invert = (Math.floor(x / 3) + Math.floor(y / 2)) % 2 == 0;
  624. break;
  625. case 5:
  626. invert = x * y % 2 + x * y % 3 == 0;
  627. break;
  628. case 6:
  629. invert = (x * y % 2 + x * y % 3) % 2 == 0;
  630. break;
  631. case 7:
  632. invert = ((x + y) % 2 + x * y % 3) % 2 == 0;
  633. break;
  634. default:
  635. throw new Error('Unreachable');
  636. }
  637. if (!this.isFunction[y][x] && invert) {
  638. this.modules[y][x] = !this.modules[y][x];
  639. }
  640. }
  641. }
  642. }
  643. // Calculates and returns the penalty score based on state of this QR Code's current modules.
  644. // This is used by the automatic mask choice algorithm to find the mask pattern that yields the lowest score.
  645. }, {
  646. key: "getPenaltyScore",
  647. value: function getPenaltyScore() {
  648. var result = 0;
  649. // Adjacent modules in row having same color, and finder-like patterns
  650. for (var y = 0; y < this.size; y++) {
  651. var runColor = false;
  652. var runX = 0;
  653. var runHistory = [0, 0, 0, 0, 0, 0, 0];
  654. for (var x = 0; x < this.size; x++) {
  655. if (this.modules[y][x] == runColor) {
  656. runX++;
  657. if (runX == 5) {
  658. result += QrCode.PENALTY_N1;
  659. } else if (runX > 5) {
  660. result++;
  661. }
  662. } else {
  663. this.finderPenaltyAddHistory(runX, runHistory);
  664. if (!runColor) {
  665. result += this.finderPenaltyCountPatterns(runHistory) * QrCode.PENALTY_N3;
  666. }
  667. runColor = this.modules[y][x];
  668. runX = 1;
  669. }
  670. }
  671. result += this.finderPenaltyTerminateAndCount(runColor, runX, runHistory) * QrCode.PENALTY_N3;
  672. }
  673. // Adjacent modules in column having same color, and finder-like patterns
  674. for (var _x = 0; _x < this.size; _x++) {
  675. var _runColor = false;
  676. var runY = 0;
  677. var _runHistory = [0, 0, 0, 0, 0, 0, 0];
  678. for (var _y = 0; _y < this.size; _y++) {
  679. if (this.modules[_y][_x] == _runColor) {
  680. runY++;
  681. if (runY == 5) {
  682. result += QrCode.PENALTY_N1;
  683. } else if (runY > 5) {
  684. result++;
  685. }
  686. } else {
  687. this.finderPenaltyAddHistory(runY, _runHistory);
  688. if (!_runColor) {
  689. result += this.finderPenaltyCountPatterns(_runHistory) * QrCode.PENALTY_N3;
  690. }
  691. _runColor = this.modules[_y][_x];
  692. runY = 1;
  693. }
  694. }
  695. result += this.finderPenaltyTerminateAndCount(_runColor, runY, _runHistory) * QrCode.PENALTY_N3;
  696. }
  697. // 2*2 blocks of modules having same color
  698. for (var _y2 = 0; _y2 < this.size - 1; _y2++) {
  699. for (var _x2 = 0; _x2 < this.size - 1; _x2++) {
  700. var color = this.modules[_y2][_x2];
  701. if (color == this.modules[_y2][_x2 + 1] && color == this.modules[_y2 + 1][_x2] && color == this.modules[_y2 + 1][_x2 + 1]) {
  702. result += QrCode.PENALTY_N2;
  703. }
  704. }
  705. }
  706. // Balance of dark and light modules
  707. var dark = 0;
  708. var _iterator3 = _createForOfIteratorHelper(this.modules),
  709. _step3;
  710. try {
  711. for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
  712. var row = _step3.value;
  713. dark = row.reduce(function (sum, color) {
  714. return sum + (color ? 1 : 0);
  715. }, dark);
  716. }
  717. } catch (err) {
  718. _iterator3.e(err);
  719. } finally {
  720. _iterator3.f();
  721. }
  722. var total = this.size * this.size; // Note that size is odd, so dark/total != 1/2
  723. // Compute the smallest numbereger k >= 0 such that (45-5k)% <= dark/total <= (55+5k)%
  724. var k = Math.ceil(Math.abs(dark * 20 - total * 10) / total) - 1;
  725. assert(0 <= k && k <= 9);
  726. result += k * QrCode.PENALTY_N4;
  727. assert(0 <= result && result <= 2568888); // Non-tight upper bound based on default values of PENALTY_N1, ..., 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 numberegers.
  734. }, {
  735. key: "getAlignmentPatternPositions",
  736. value: function getAlignmentPatternPositions() {
  737. if (this.version == 1) {
  738. return [];
  739. } else {
  740. var numAlign = Math.floor(this.version / 7) + 2;
  741. var step = this.version == 32 ? 26 : Math.ceil((this.version * 4 + 4) / (numAlign * 2 - 2)) * 2;
  742. var result = [6];
  743. for (var pos = this.size - 7; result.length < numAlign; pos -= step) {
  744. result.splice(1, 0, pos);
  745. }
  746. return result;
  747. }
  748. }
  749. // Returns the number of data bits that can be stored in a QR Code of the given version number, after
  750. // all function modules are excluded. This includes remainder bits, so it might not be a multiple of 8.
  751. // The result is in the range [208, 29648]. This could be implemented as a 40-entry lookup table.
  752. }, {
  753. key: "finderPenaltyCountPatterns",
  754. value:
  755. // Can only be called immediately after a light run is added, and
  756. // returns either 0, 1, or 2. A helper function for getPenaltyScore().
  757. function finderPenaltyCountPatterns(runHistory) {
  758. var n = runHistory[1];
  759. assert(n <= this.size * 3);
  760. var core = n > 0 && runHistory[2] == n && runHistory[3] == n * 3 && runHistory[4] == n && runHistory[5] == n;
  761. return (core && runHistory[0] >= n * 4 && runHistory[6] >= n ? 1 : 0) + (core && runHistory[6] >= n * 4 && runHistory[0] >= n ? 1 : 0);
  762. }
  763. // Must be called at the end of a line (row or column) of modules. A helper function for getPenaltyScore().
  764. }, {
  765. key: "finderPenaltyTerminateAndCount",
  766. value: function finderPenaltyTerminateAndCount(currentRunColor, oriCurrentRunLength, runHistory) {
  767. var currentRunLength = oriCurrentRunLength;
  768. if (currentRunColor) {
  769. // Terminate dark run
  770. this.finderPenaltyAddHistory(currentRunLength, runHistory);
  771. currentRunLength = 0;
  772. }
  773. currentRunLength += this.size; // Add light border to final run
  774. this.finderPenaltyAddHistory(currentRunLength, runHistory);
  775. return this.finderPenaltyCountPatterns(runHistory);
  776. }
  777. // Pushes the given value to the front and drops the last value. A helper function for getPenaltyScore().
  778. }, {
  779. key: "finderPenaltyAddHistory",
  780. value: function finderPenaltyAddHistory(oriCurrentRunLength, runHistory) {
  781. var currentRunLength = oriCurrentRunLength;
  782. if (runHistory[0] == 0) {
  783. currentRunLength += this.size; // Add light border to initial run
  784. }
  785. runHistory.pop();
  786. runHistory.unshift(currentRunLength);
  787. }
  788. /*-- Constants and tables --*/
  789. // The minimum version number supported in the QR Code Model 2 standard.
  790. }], [{
  791. key: "encodeText",
  792. value: /*-- Static factory functions (high level) --*/
  793. // Returns a QR Code representing the given Unicode text string at the given error correction level.
  794. // As a conservative upper bound, this function is guaranteed to succeed for strings that have 738 or fewer
  795. // Unicode code ponumbers (not UTF-16 code units) if the low error correction level is used. The smallest possible
  796. // QR Code version is automatically chosen for the output. The ECC level of the result may be higher than the
  797. // ecl argument if it can be done without increasing the version.
  798. function encodeText(text, ecl) {
  799. var segs = QrSegment.makeSegments(text);
  800. return QrCode.encodeSegments(segs, ecl);
  801. }
  802. // Returns a QR Code representing the given binary data at the given error correction level.
  803. // This function always encodes using the binary segment mode, not any text mode. The maximum number of
  804. // bytes allowed is 2953. The smallest possible QR Code version is automatically chosen for the output.
  805. // The ECC level of the result may be higher than the ecl argument if it can be done without increasing the version.
  806. }, {
  807. key: "encodeBinary",
  808. value: function encodeBinary(data, ecl) {
  809. var seg = QrSegment.makeBytes(data);
  810. return QrCode.encodeSegments([seg], ecl);
  811. }
  812. /*-- Static factory functions (mid level) --*/
  813. // Returns a QR Code representing the given segments with the given encoding parameters.
  814. // The smallest possible QR Code version within the given range is automatically
  815. // chosen for the output. Iff boostEcl is true, then the ECC level of the result
  816. // may be higher than the ecl argument if it can be done without increasing the
  817. // version. The mask number is either between 0 to 7 (inclusive) to force that
  818. // mask, or -1 to automatically choose an appropriate mask (which may be slow).
  819. // This function allows the user to create a custom sequence of segments that switches
  820. // between modes (such as alphanumeric and byte) to encode text in less space.
  821. // This is a mid-level API; the high-level API is encodeText() and encodeBinary().
  822. }, {
  823. key: "encodeSegments",
  824. value: function encodeSegments(segs, oriEcl) {
  825. var minVersion = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
  826. var maxVersion = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 40;
  827. var mask = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : -1;
  828. var boostEcl = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : true;
  829. if (!(QrCode.MIN_VERSION <= minVersion && minVersion <= maxVersion && maxVersion <= QrCode.MAX_VERSION) || mask < -1 || mask > 7) {
  830. throw new RangeError('Invalid value');
  831. }
  832. // Find the minimal version number to use
  833. var version;
  834. var dataUsedBits;
  835. for (version = minVersion;; version++) {
  836. var _dataCapacityBits = QrCode.getNumDataCodewords(version, oriEcl) * 8; // Number of data bits available
  837. var usedBits = QrSegment.getTotalBits(segs, version);
  838. if (usedBits <= _dataCapacityBits) {
  839. dataUsedBits = usedBits;
  840. break; // This version number is found to be suitable
  841. }
  842. if (version >= maxVersion) {
  843. // All versions in the range could not fit the given data
  844. throw new RangeError('Data too long');
  845. }
  846. }
  847. var ecl = oriEcl;
  848. // Increase the error correction level while the data still fits in the current version number
  849. for (var _i10 = 0, _arr = [Ecc.MEDIUM, Ecc.QUARTILE, Ecc.HIGH]; _i10 < _arr.length; _i10++) {
  850. var newEcl = _arr[_i10];
  851. // From low to high
  852. if (boostEcl && dataUsedBits <= QrCode.getNumDataCodewords(version, newEcl) * 8) {
  853. ecl = newEcl;
  854. }
  855. }
  856. // Concatenate all segments to create the data bit string
  857. var bb = [];
  858. var _iterator4 = _createForOfIteratorHelper(segs),
  859. _step4;
  860. try {
  861. for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
  862. var seg = _step4.value;
  863. appendBits(seg.mode.modeBits, 4, bb);
  864. appendBits(seg.numChars, seg.mode.numCharCountBits(version), bb);
  865. var _iterator5 = _createForOfIteratorHelper(seg.getData()),
  866. _step5;
  867. try {
  868. for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
  869. var b = _step5.value;
  870. bb.push(b);
  871. }
  872. } catch (err) {
  873. _iterator5.e(err);
  874. } finally {
  875. _iterator5.f();
  876. }
  877. }
  878. } catch (err) {
  879. _iterator4.e(err);
  880. } finally {
  881. _iterator4.f();
  882. }
  883. assert(bb.length == dataUsedBits);
  884. // Add terminator and pad up to a byte if applicable
  885. var dataCapacityBits = QrCode.getNumDataCodewords(version, ecl) * 8;
  886. assert(bb.length <= dataCapacityBits);
  887. appendBits(0, Math.min(4, dataCapacityBits - bb.length), bb);
  888. appendBits(0, (8 - bb.length % 8) % 8, bb);
  889. assert(bb.length % 8 == 0);
  890. // Pad with alternating bytes until data capacity is reached
  891. for (var padByte = 0xec; bb.length < dataCapacityBits; padByte ^= 0xec ^ 0x11) {
  892. appendBits(padByte, 8, bb);
  893. }
  894. // Pack bits numbero bytes in big endian
  895. var dataCodewords = [];
  896. while (dataCodewords.length * 8 < bb.length) {
  897. dataCodewords.push(0);
  898. }
  899. bb.forEach(function (b, i) {
  900. dataCodewords[i >>> 3] |= b << 7 - (i & 7);
  901. });
  902. // Create the QR Code object
  903. return new QrCode(version, ecl, dataCodewords, mask);
  904. }
  905. }, {
  906. key: "getNumRawDataModules",
  907. value: function getNumRawDataModules(ver) {
  908. if (ver < QrCode.MIN_VERSION || ver > QrCode.MAX_VERSION) {
  909. throw new RangeError('Version number out of range');
  910. }
  911. var result = (16 * ver + 128) * ver + 64;
  912. if (ver >= 2) {
  913. var numAlign = Math.floor(ver / 7) + 2;
  914. result -= (25 * numAlign - 10) * numAlign - 55;
  915. if (ver >= 7) {
  916. result -= 36;
  917. }
  918. }
  919. assert(208 <= result && result <= 29648);
  920. return result;
  921. }
  922. // Returns the number of 8-bit data (i.e. not error correction) codewords contained in any
  923. // QR Code of the given version number and error correction level, with remainder bits discarded.
  924. // This stateless pure function could be implemented as a (40*4)-cell lookup table.
  925. }, {
  926. key: "getNumDataCodewords",
  927. value: function getNumDataCodewords(ver, ecl) {
  928. return Math.floor(QrCode.getNumRawDataModules(ver) / 8) - QrCode.ECC_CODEWORDS_PER_BLOCK[ecl.ordinal][ver] * QrCode.NUM_ERROR_CORRECTION_BLOCKS[ecl.ordinal][ver];
  929. }
  930. // Returns a Reed-Solomon ECC generator polynomial for the given degree. This could be
  931. // implemented as a lookup table over all possible parameter values, instead of as an algorithm.
  932. }, {
  933. key: "reedSolomonComputeDivisor",
  934. value: function reedSolomonComputeDivisor(degree) {
  935. if (degree < 1 || degree > 255) {
  936. throw new RangeError('Degree out of range');
  937. }
  938. // Polynomial coefficients are stored from highest to lowest power, excluding the leading term which is always 1.
  939. // For example the polynomial x^3 + 255x^2 + 8x + 93 is stored as the unumber8 array [255, 8, 93].
  940. var result = [];
  941. for (var i = 0; i < degree - 1; i++) {
  942. result.push(0);
  943. }
  944. result.push(1); // Start off with the monomial x^0
  945. // Compute the product polynomial (x - r^0) * (x - r^1) * (x - r^2) * ... * (x - r^{degree-1}),
  946. // and drop the highest monomial term which is always 1x^degree.
  947. // Note that r = 0x02, which is a generator element of this field GF(2^8/0x11D).
  948. var root = 1;
  949. for (var _i11 = 0; _i11 < degree; _i11++) {
  950. // Multiply the current product by (x - r^i)
  951. for (var j = 0; j < result.length; j++) {
  952. result[j] = QrCode.reedSolomonMultiply(result[j], root);
  953. if (j + 1 < result.length) {
  954. result[j] ^= result[j + 1];
  955. }
  956. }
  957. root = QrCode.reedSolomonMultiply(root, 0x02);
  958. }
  959. return result;
  960. }
  961. // Returns the Reed-Solomon error correction codeword for the given data and divisor polynomials.
  962. }, {
  963. key: "reedSolomonComputeRemainder",
  964. value: function reedSolomonComputeRemainder(data, divisor) {
  965. var result = divisor.map(function () {
  966. return 0;
  967. });
  968. var _iterator6 = _createForOfIteratorHelper(data),
  969. _step6;
  970. try {
  971. var _loop2 = function _loop2() {
  972. var b = _step6.value;
  973. // Polynomial division
  974. var factor = b ^ result.shift();
  975. result.push(0);
  976. divisor.forEach(function (coef, i) {
  977. result[i] ^= QrCode.reedSolomonMultiply(coef, factor);
  978. });
  979. };
  980. for (_iterator6.s(); !(_step6 = _iterator6.n()).done;) {
  981. _loop2();
  982. }
  983. } catch (err) {
  984. _iterator6.e(err);
  985. } finally {
  986. _iterator6.f();
  987. }
  988. return result;
  989. }
  990. // Returns the product of the two given field elements modulo GF(2^8/0x11D). The arguments and result
  991. // are unsigned 8-bit numberegers. This could be implemented as a lookup table of 256*256 entries of unumber8.
  992. }, {
  993. key: "reedSolomonMultiply",
  994. value: function reedSolomonMultiply(x, y) {
  995. if (x >>> 8 != 0 || y >>> 8 != 0) {
  996. throw new RangeError('Byte out of range');
  997. }
  998. // Russian peasant multiplication
  999. var z = 0;
  1000. for (var i = 7; i >= 0; i--) {
  1001. z = z << 1 ^ (z >>> 7) * 0x11d;
  1002. z ^= (y >>> i & 1) * x;
  1003. }
  1004. assert(z >>> 8 == 0);
  1005. return z;
  1006. }
  1007. }]);
  1008. return QrCode;
  1009. }();
  1010. _defineProperty(QrCode, "MIN_VERSION", 1);
  1011. // The maximum version number supported in the QR Code Model 2 standard.
  1012. _defineProperty(QrCode, "MAX_VERSION", 40);
  1013. // For use in getPenaltyScore(), when evaluating which mask is best.
  1014. _defineProperty(QrCode, "PENALTY_N1", 3);
  1015. _defineProperty(QrCode, "PENALTY_N2", 3);
  1016. _defineProperty(QrCode, "PENALTY_N3", 40);
  1017. _defineProperty(QrCode, "PENALTY_N4", 10);
  1018. _defineProperty(QrCode, "ECC_CODEWORDS_PER_BLOCK", [
  1019. // Version: (note that index 0 is for padding, and is set to an illegal value)
  1020. //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
  1021. [-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],
  1022. // Low
  1023. [-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],
  1024. // Medium
  1025. [-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],
  1026. // Quartile
  1027. [-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
  1028. ]);
  1029. _defineProperty(QrCode, "NUM_ERROR_CORRECTION_BLOCKS", [
  1030. // Version: (note that index 0 is for padding, and is set to an illegal value)
  1031. //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
  1032. [-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],
  1033. // Low
  1034. [-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],
  1035. // Medium
  1036. [-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],
  1037. // Quartile
  1038. [-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
  1039. ]);