qrcodegen.js 45 KB

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