vue-qrcode.esm.js 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450
  1. /*!
  2. * vue-qrcode v1.0.0
  3. * https://xkeshi.github.io/vue-qrcode
  4. *
  5. * Copyright 2017-present Chen Fengyuan
  6. * Released under the MIT license
  7. *
  8. * Date: 2018-06-20T03:42:27.281Z
  9. */
  10. var commonjsGlobal = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
  11. function createCommonjsModule(fn, module) {
  12. return module = { exports: {} }, fn(module, module.exports), module.exports;
  13. }
  14. var qrious = createCommonjsModule(function (module, exports) {
  15. /*
  16. * QRious v4.0.2
  17. * Copyright (C) 2017 Alasdair Mercer
  18. * Copyright (C) 2010 Tom Zerucha
  19. *
  20. * This program is free software: you can redistribute it and/or modify
  21. * it under the terms of the GNU General Public License as published by
  22. * the Free Software Foundation, either version 3 of the License, or
  23. * (at your option) any later version.
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU General Public License
  31. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  32. */
  33. (function (global, factory) {
  34. module.exports = factory();
  35. }(commonjsGlobal, (function () {
  36. /*
  37. * Copyright (C) 2017 Alasdair Mercer, !ninja
  38. *
  39. * Permission is hereby granted, free of charge, to any person obtaining a copy
  40. * of this software and associated documentation files (the "Software"), to deal
  41. * in the Software without restriction, including without limitation the rights
  42. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  43. * copies of the Software, and to permit persons to whom the Software is
  44. * furnished to do so, subject to the following conditions:
  45. *
  46. * The above copyright notice and this permission notice shall be included in all
  47. * copies or substantial portions of the Software.
  48. *
  49. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  50. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  51. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  52. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  53. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  54. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  55. * SOFTWARE.
  56. */
  57. /**
  58. * A bare-bones constructor for surrogate prototype swapping.
  59. *
  60. * @private
  61. * @constructor
  62. */
  63. var Constructor = /* istanbul ignore next */ function() {};
  64. /**
  65. * A reference to <code>Object.prototype.hasOwnProperty</code>.
  66. *
  67. * @private
  68. * @type {Function}
  69. */
  70. var hasOwnProperty = Object.prototype.hasOwnProperty;
  71. /**
  72. * A reference to <code>Array.prototype.slice</code>.
  73. *
  74. * @private
  75. * @type {Function}
  76. */
  77. var slice = Array.prototype.slice;
  78. /**
  79. * Creates an object which inherits the given <code>prototype</code>.
  80. *
  81. * Optionally, the created object can be extended further with the specified <code>properties</code>.
  82. *
  83. * @param {Object} prototype - the prototype to be inherited by the created object
  84. * @param {Object} [properties] - the optional properties to be extended by the created object
  85. * @return {Object} The newly created object.
  86. * @private
  87. */
  88. function createObject(prototype, properties) {
  89. var result;
  90. /* istanbul ignore next */
  91. if (typeof Object.create === 'function') {
  92. result = Object.create(prototype);
  93. } else {
  94. Constructor.prototype = prototype;
  95. result = new Constructor();
  96. Constructor.prototype = null;
  97. }
  98. if (properties) {
  99. extendObject(true, result, properties);
  100. }
  101. return result;
  102. }
  103. /**
  104. * Extends the constructor to which this method is associated with the <code>prototype</code> and/or
  105. * <code>statics</code> provided.
  106. *
  107. * If <code>name</code> is provided, it will be used as the class name and can be accessed via a special
  108. * <code>class_</code> property on the child constructor, otherwise the class name of the super constructor will be used
  109. * instead. The class name may also be used string representation for instances of the child constructor (via
  110. * <code>toString</code>), but this is not applicable to the <i>lite</i> version of Nevis.
  111. *
  112. * If <code>constructor</code> is provided, it will be used as the constructor for the child, otherwise a simple
  113. * constructor which only calls the super constructor will be used instead.
  114. *
  115. * The super constructor can be accessed via a special <code>super_</code> property on the child constructor.
  116. *
  117. * @param {string} [name=this.class_] - the class name to be used for the child constructor
  118. * @param {Function} [constructor] - the constructor for the child
  119. * @param {Object} [prototype] - the prototype properties to be defined for the child
  120. * @param {Object} [statics] - the static properties to be defined for the child
  121. * @return {Function} The child <code>constructor</code> provided or the one created if none was given.
  122. * @public
  123. */
  124. function extend(name, constructor, prototype, statics) {
  125. var superConstructor = this;
  126. if (typeof name !== 'string') {
  127. statics = prototype;
  128. prototype = constructor;
  129. constructor = name;
  130. name = null;
  131. }
  132. if (typeof constructor !== 'function') {
  133. statics = prototype;
  134. prototype = constructor;
  135. constructor = function() {
  136. return superConstructor.apply(this, arguments);
  137. };
  138. }
  139. extendObject(false, constructor, superConstructor, statics);
  140. constructor.prototype = createObject(superConstructor.prototype, prototype);
  141. constructor.prototype.constructor = constructor;
  142. constructor.class_ = name || superConstructor.class_;
  143. constructor.super_ = superConstructor;
  144. return constructor;
  145. }
  146. /**
  147. * Extends the specified <code>target</code> object with the properties in each of the <code>sources</code> provided.
  148. *
  149. * if any source is <code>null</code> it will be ignored.
  150. *
  151. * @param {boolean} own - <code>true</code> to only copy <b>own</b> properties from <code>sources</code> onto
  152. * <code>target</code>; otherwise <code>false</code>
  153. * @param {Object} target - the target object which should be extended
  154. * @param {...Object} [sources] - the source objects whose properties are to be copied onto <code>target</code>
  155. * @return {void}
  156. * @private
  157. */
  158. function extendObject(own, target, sources) {
  159. sources = slice.call(arguments, 2);
  160. var property;
  161. var source;
  162. for (var i = 0, length = sources.length; i < length; i++) {
  163. source = sources[i];
  164. for (property in source) {
  165. if (!own || hasOwnProperty.call(source, property)) {
  166. target[property] = source[property];
  167. }
  168. }
  169. }
  170. }
  171. var extend_1 = extend;
  172. /**
  173. * The base class from which all others should extend.
  174. *
  175. * @public
  176. * @constructor
  177. */
  178. function Nevis() {}
  179. Nevis.class_ = 'Nevis';
  180. Nevis.super_ = Object;
  181. /**
  182. * Extends the constructor to which this method is associated with the <code>prototype</code> and/or
  183. * <code>statics</code> provided.
  184. *
  185. * If <code>name</code> is provided, it will be used as the class name and can be accessed via a special
  186. * <code>class_</code> property on the child constructor, otherwise the class name of the super constructor will be used
  187. * instead. The class name may also be used string representation for instances of the child constructor (via
  188. * <code>toString</code>), but this is not applicable to the <i>lite</i> version of Nevis.
  189. *
  190. * If <code>constructor</code> is provided, it will be used as the constructor for the child, otherwise a simple
  191. * constructor which only calls the super constructor will be used instead.
  192. *
  193. * The super constructor can be accessed via a special <code>super_</code> property on the child constructor.
  194. *
  195. * @param {string} [name=this.class_] - the class name to be used for the child constructor
  196. * @param {Function} [constructor] - the constructor for the child
  197. * @param {Object} [prototype] - the prototype properties to be defined for the child
  198. * @param {Object} [statics] - the static properties to be defined for the child
  199. * @return {Function} The child <code>constructor</code> provided or the one created if none was given.
  200. * @public
  201. * @static
  202. * @memberof Nevis
  203. */
  204. Nevis.extend = extend_1;
  205. var nevis = Nevis;
  206. var lite = nevis;
  207. /**
  208. * Responsible for rendering a QR code {@link Frame} on a specific type of element.
  209. *
  210. * A renderer may be dependant on the rendering of another element, so the ordering of their execution is important.
  211. *
  212. * The rendering of a element can be deferred by disabling the renderer initially, however, any attempt get the element
  213. * from the renderer will result in it being immediately enabled and the element being rendered.
  214. *
  215. * @param {QRious} qrious - the {@link QRious} instance to be used
  216. * @param {*} element - the element onto which the QR code is to be rendered
  217. * @param {boolean} [enabled] - <code>true</code> this {@link Renderer} is enabled; otherwise <code>false</code>.
  218. * @public
  219. * @class
  220. * @extends Nevis
  221. */
  222. var Renderer = lite.extend(function(qrious, element, enabled) {
  223. /**
  224. * The {@link QRious} instance.
  225. *
  226. * @protected
  227. * @type {QRious}
  228. * @memberof Renderer#
  229. */
  230. this.qrious = qrious;
  231. /**
  232. * The element onto which this {@link Renderer} is rendering the QR code.
  233. *
  234. * @protected
  235. * @type {*}
  236. * @memberof Renderer#
  237. */
  238. this.element = element;
  239. this.element.qrious = qrious;
  240. /**
  241. * Whether this {@link Renderer} is enabled.
  242. *
  243. * @protected
  244. * @type {boolean}
  245. * @memberof Renderer#
  246. */
  247. this.enabled = Boolean(enabled);
  248. }, {
  249. /**
  250. * Draws the specified QR code <code>frame</code> on the underlying element.
  251. *
  252. * Implementations of {@link Renderer} <b>must</b> override this method with their own specific logic.
  253. *
  254. * @param {Frame} frame - the {@link Frame} to be drawn
  255. * @return {void}
  256. * @protected
  257. * @abstract
  258. * @memberof Renderer#
  259. */
  260. draw: function(frame) {},
  261. /**
  262. * Returns the element onto which this {@link Renderer} is rendering the QR code.
  263. *
  264. * If this method is called while this {@link Renderer} is disabled, it will be immediately enabled and rendered
  265. * before the element is returned.
  266. *
  267. * @return {*} The element.
  268. * @public
  269. * @memberof Renderer#
  270. */
  271. getElement: function() {
  272. if (!this.enabled) {
  273. this.enabled = true;
  274. this.render();
  275. }
  276. return this.element;
  277. },
  278. /**
  279. * Calculates the size (in pixel units) to represent an individual module within the QR code based on the
  280. * <code>frame</code> provided.
  281. *
  282. * Any configured padding will be excluded from the returned size.
  283. *
  284. * The returned value will be at least one, even in cases where the size of the QR code does not fit its contents.
  285. * This is done so that the inevitable clipping is handled more gracefully since this way at least something is
  286. * displayed instead of just a blank space filled by the background color.
  287. *
  288. * @param {Frame} frame - the {@link Frame} from which the module size is to be derived
  289. * @return {number} The pixel size for each module in the QR code which will be no less than one.
  290. * @protected
  291. * @memberof Renderer#
  292. */
  293. getModuleSize: function(frame) {
  294. var qrious = this.qrious;
  295. var padding = qrious.padding || 0;
  296. var pixels = Math.floor((qrious.size - (padding * 2)) / frame.width);
  297. return Math.max(1, pixels);
  298. },
  299. /**
  300. * Calculates the offset/padding (in pixel units) to be inserted before the QR code based on the <code>frame</code>
  301. * provided.
  302. *
  303. * The returned value will be zero if there is no available offset or if the size of the QR code does not fit its
  304. * contents. It will never be a negative value. This is done so that the inevitable clipping appears more naturally
  305. * and it is not clipped from all directions.
  306. *
  307. * @param {Frame} frame - the {@link Frame} from which the offset is to be derived
  308. * @return {number} The pixel offset for the QR code which will be no less than zero.
  309. * @protected
  310. * @memberof Renderer#
  311. */
  312. getOffset: function(frame) {
  313. var qrious = this.qrious;
  314. var padding = qrious.padding;
  315. if (padding != null) {
  316. return padding;
  317. }
  318. var moduleSize = this.getModuleSize(frame);
  319. var offset = Math.floor((qrious.size - (moduleSize * frame.width)) / 2);
  320. return Math.max(0, offset);
  321. },
  322. /**
  323. * Renders a QR code on the underlying element based on the <code>frame</code> provided.
  324. *
  325. * @param {Frame} frame - the {@link Frame} to be rendered
  326. * @return {void}
  327. * @public
  328. * @memberof Renderer#
  329. */
  330. render: function(frame) {
  331. if (this.enabled) {
  332. this.resize();
  333. this.reset();
  334. this.draw(frame);
  335. }
  336. },
  337. /**
  338. * Resets the underlying element, effectively clearing any previously rendered QR code.
  339. *
  340. * Implementations of {@link Renderer} <b>must</b> override this method with their own specific logic.
  341. *
  342. * @return {void}
  343. * @protected
  344. * @abstract
  345. * @memberof Renderer#
  346. */
  347. reset: function() {},
  348. /**
  349. * Ensures that the size of the underlying element matches that defined on the associated {@link QRious} instance.
  350. *
  351. * Implementations of {@link Renderer} <b>must</b> override this method with their own specific logic.
  352. *
  353. * @return {void}
  354. * @protected
  355. * @abstract
  356. * @memberof Renderer#
  357. */
  358. resize: function() {}
  359. });
  360. var Renderer_1 = Renderer;
  361. /**
  362. * An implementation of {@link Renderer} for working with <code>canvas</code> elements.
  363. *
  364. * @public
  365. * @class
  366. * @extends Renderer
  367. */
  368. var CanvasRenderer = Renderer_1.extend({
  369. /**
  370. * @override
  371. */
  372. draw: function(frame) {
  373. var i, j;
  374. var qrious = this.qrious;
  375. var moduleSize = this.getModuleSize(frame);
  376. var offset = this.getOffset(frame);
  377. var context = this.element.getContext('2d');
  378. context.fillStyle = qrious.foreground;
  379. context.globalAlpha = qrious.foregroundAlpha;
  380. for (i = 0; i < frame.width; i++) {
  381. for (j = 0; j < frame.width; j++) {
  382. if (frame.buffer[(j * frame.width) + i]) {
  383. context.fillRect((moduleSize * i) + offset, (moduleSize * j) + offset, moduleSize, moduleSize);
  384. }
  385. }
  386. }
  387. },
  388. /**
  389. * @override
  390. */
  391. reset: function() {
  392. var qrious = this.qrious;
  393. var context = this.element.getContext('2d');
  394. var size = qrious.size;
  395. context.lineWidth = 1;
  396. context.clearRect(0, 0, size, size);
  397. context.fillStyle = qrious.background;
  398. context.globalAlpha = qrious.backgroundAlpha;
  399. context.fillRect(0, 0, size, size);
  400. },
  401. /**
  402. * @override
  403. */
  404. resize: function() {
  405. var element = this.element;
  406. element.width = element.height = this.qrious.size;
  407. }
  408. });
  409. var CanvasRenderer_1 = CanvasRenderer;
  410. /* eslint no-multi-spaces: "off" */
  411. /**
  412. * Contains alignment pattern information.
  413. *
  414. * @public
  415. * @class
  416. * @extends Nevis
  417. */
  418. var Alignment = lite.extend(null, {
  419. /**
  420. * The alignment pattern block.
  421. *
  422. * @public
  423. * @static
  424. * @type {number[]}
  425. * @memberof Alignment
  426. */
  427. BLOCK: [
  428. 0, 11, 15, 19, 23, 27, 31,
  429. 16, 18, 20, 22, 24, 26, 28, 20, 22, 24, 24, 26, 28, 28, 22, 24, 24,
  430. 26, 26, 28, 28, 24, 24, 26, 26, 26, 28, 28, 24, 26, 26, 26, 28, 28
  431. ]
  432. });
  433. var Alignment_1 = Alignment;
  434. /* eslint no-multi-spaces: "off" */
  435. /**
  436. * Contains error correction information.
  437. *
  438. * @public
  439. * @class
  440. * @extends Nevis
  441. */
  442. var ErrorCorrection = lite.extend(null, {
  443. /**
  444. * The error correction blocks.
  445. *
  446. * There are four elements per version. The first two indicate the number of blocks, then the data width, and finally
  447. * the ECC width.
  448. *
  449. * @public
  450. * @static
  451. * @type {number[]}
  452. * @memberof ErrorCorrection
  453. */
  454. BLOCKS: [
  455. 1, 0, 19, 7, 1, 0, 16, 10, 1, 0, 13, 13, 1, 0, 9, 17,
  456. 1, 0, 34, 10, 1, 0, 28, 16, 1, 0, 22, 22, 1, 0, 16, 28,
  457. 1, 0, 55, 15, 1, 0, 44, 26, 2, 0, 17, 18, 2, 0, 13, 22,
  458. 1, 0, 80, 20, 2, 0, 32, 18, 2, 0, 24, 26, 4, 0, 9, 16,
  459. 1, 0, 108, 26, 2, 0, 43, 24, 2, 2, 15, 18, 2, 2, 11, 22,
  460. 2, 0, 68, 18, 4, 0, 27, 16, 4, 0, 19, 24, 4, 0, 15, 28,
  461. 2, 0, 78, 20, 4, 0, 31, 18, 2, 4, 14, 18, 4, 1, 13, 26,
  462. 2, 0, 97, 24, 2, 2, 38, 22, 4, 2, 18, 22, 4, 2, 14, 26,
  463. 2, 0, 116, 30, 3, 2, 36, 22, 4, 4, 16, 20, 4, 4, 12, 24,
  464. 2, 2, 68, 18, 4, 1, 43, 26, 6, 2, 19, 24, 6, 2, 15, 28,
  465. 4, 0, 81, 20, 1, 4, 50, 30, 4, 4, 22, 28, 3, 8, 12, 24,
  466. 2, 2, 92, 24, 6, 2, 36, 22, 4, 6, 20, 26, 7, 4, 14, 28,
  467. 4, 0, 107, 26, 8, 1, 37, 22, 8, 4, 20, 24, 12, 4, 11, 22,
  468. 3, 1, 115, 30, 4, 5, 40, 24, 11, 5, 16, 20, 11, 5, 12, 24,
  469. 5, 1, 87, 22, 5, 5, 41, 24, 5, 7, 24, 30, 11, 7, 12, 24,
  470. 5, 1, 98, 24, 7, 3, 45, 28, 15, 2, 19, 24, 3, 13, 15, 30,
  471. 1, 5, 107, 28, 10, 1, 46, 28, 1, 15, 22, 28, 2, 17, 14, 28,
  472. 5, 1, 120, 30, 9, 4, 43, 26, 17, 1, 22, 28, 2, 19, 14, 28,
  473. 3, 4, 113, 28, 3, 11, 44, 26, 17, 4, 21, 26, 9, 16, 13, 26,
  474. 3, 5, 107, 28, 3, 13, 41, 26, 15, 5, 24, 30, 15, 10, 15, 28,
  475. 4, 4, 116, 28, 17, 0, 42, 26, 17, 6, 22, 28, 19, 6, 16, 30,
  476. 2, 7, 111, 28, 17, 0, 46, 28, 7, 16, 24, 30, 34, 0, 13, 24,
  477. 4, 5, 121, 30, 4, 14, 47, 28, 11, 14, 24, 30, 16, 14, 15, 30,
  478. 6, 4, 117, 30, 6, 14, 45, 28, 11, 16, 24, 30, 30, 2, 16, 30,
  479. 8, 4, 106, 26, 8, 13, 47, 28, 7, 22, 24, 30, 22, 13, 15, 30,
  480. 10, 2, 114, 28, 19, 4, 46, 28, 28, 6, 22, 28, 33, 4, 16, 30,
  481. 8, 4, 122, 30, 22, 3, 45, 28, 8, 26, 23, 30, 12, 28, 15, 30,
  482. 3, 10, 117, 30, 3, 23, 45, 28, 4, 31, 24, 30, 11, 31, 15, 30,
  483. 7, 7, 116, 30, 21, 7, 45, 28, 1, 37, 23, 30, 19, 26, 15, 30,
  484. 5, 10, 115, 30, 19, 10, 47, 28, 15, 25, 24, 30, 23, 25, 15, 30,
  485. 13, 3, 115, 30, 2, 29, 46, 28, 42, 1, 24, 30, 23, 28, 15, 30,
  486. 17, 0, 115, 30, 10, 23, 46, 28, 10, 35, 24, 30, 19, 35, 15, 30,
  487. 17, 1, 115, 30, 14, 21, 46, 28, 29, 19, 24, 30, 11, 46, 15, 30,
  488. 13, 6, 115, 30, 14, 23, 46, 28, 44, 7, 24, 30, 59, 1, 16, 30,
  489. 12, 7, 121, 30, 12, 26, 47, 28, 39, 14, 24, 30, 22, 41, 15, 30,
  490. 6, 14, 121, 30, 6, 34, 47, 28, 46, 10, 24, 30, 2, 64, 15, 30,
  491. 17, 4, 122, 30, 29, 14, 46, 28, 49, 10, 24, 30, 24, 46, 15, 30,
  492. 4, 18, 122, 30, 13, 32, 46, 28, 48, 14, 24, 30, 42, 32, 15, 30,
  493. 20, 4, 117, 30, 40, 7, 47, 28, 43, 22, 24, 30, 10, 67, 15, 30,
  494. 19, 6, 118, 30, 18, 31, 47, 28, 34, 34, 24, 30, 20, 61, 15, 30
  495. ],
  496. /**
  497. * The final format bits with mask (level << 3 | mask).
  498. *
  499. * @public
  500. * @static
  501. * @type {number[]}
  502. * @memberof ErrorCorrection
  503. */
  504. FINAL_FORMAT: [
  505. // L
  506. 0x77c4, 0x72f3, 0x7daa, 0x789d, 0x662f, 0x6318, 0x6c41, 0x6976,
  507. // M
  508. 0x5412, 0x5125, 0x5e7c, 0x5b4b, 0x45f9, 0x40ce, 0x4f97, 0x4aa0,
  509. // Q
  510. 0x355f, 0x3068, 0x3f31, 0x3a06, 0x24b4, 0x2183, 0x2eda, 0x2bed,
  511. // H
  512. 0x1689, 0x13be, 0x1ce7, 0x19d0, 0x0762, 0x0255, 0x0d0c, 0x083b
  513. ],
  514. /**
  515. * A map of human-readable ECC levels.
  516. *
  517. * @public
  518. * @static
  519. * @type {Object.<string, number>}
  520. * @memberof ErrorCorrection
  521. */
  522. LEVELS: {
  523. L: 1,
  524. M: 2,
  525. Q: 3,
  526. H: 4
  527. }
  528. });
  529. var ErrorCorrection_1 = ErrorCorrection;
  530. /**
  531. * Contains Galois field information.
  532. *
  533. * @public
  534. * @class
  535. * @extends Nevis
  536. */
  537. var Galois = lite.extend(null, {
  538. /**
  539. * The Galois field exponent table.
  540. *
  541. * @public
  542. * @static
  543. * @type {number[]}
  544. * @memberof Galois
  545. */
  546. EXPONENT: [
  547. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1d, 0x3a, 0x74, 0xe8, 0xcd, 0x87, 0x13, 0x26,
  548. 0x4c, 0x98, 0x2d, 0x5a, 0xb4, 0x75, 0xea, 0xc9, 0x8f, 0x03, 0x06, 0x0c, 0x18, 0x30, 0x60, 0xc0,
  549. 0x9d, 0x27, 0x4e, 0x9c, 0x25, 0x4a, 0x94, 0x35, 0x6a, 0xd4, 0xb5, 0x77, 0xee, 0xc1, 0x9f, 0x23,
  550. 0x46, 0x8c, 0x05, 0x0a, 0x14, 0x28, 0x50, 0xa0, 0x5d, 0xba, 0x69, 0xd2, 0xb9, 0x6f, 0xde, 0xa1,
  551. 0x5f, 0xbe, 0x61, 0xc2, 0x99, 0x2f, 0x5e, 0xbc, 0x65, 0xca, 0x89, 0x0f, 0x1e, 0x3c, 0x78, 0xf0,
  552. 0xfd, 0xe7, 0xd3, 0xbb, 0x6b, 0xd6, 0xb1, 0x7f, 0xfe, 0xe1, 0xdf, 0xa3, 0x5b, 0xb6, 0x71, 0xe2,
  553. 0xd9, 0xaf, 0x43, 0x86, 0x11, 0x22, 0x44, 0x88, 0x0d, 0x1a, 0x34, 0x68, 0xd0, 0xbd, 0x67, 0xce,
  554. 0x81, 0x1f, 0x3e, 0x7c, 0xf8, 0xed, 0xc7, 0x93, 0x3b, 0x76, 0xec, 0xc5, 0x97, 0x33, 0x66, 0xcc,
  555. 0x85, 0x17, 0x2e, 0x5c, 0xb8, 0x6d, 0xda, 0xa9, 0x4f, 0x9e, 0x21, 0x42, 0x84, 0x15, 0x2a, 0x54,
  556. 0xa8, 0x4d, 0x9a, 0x29, 0x52, 0xa4, 0x55, 0xaa, 0x49, 0x92, 0x39, 0x72, 0xe4, 0xd5, 0xb7, 0x73,
  557. 0xe6, 0xd1, 0xbf, 0x63, 0xc6, 0x91, 0x3f, 0x7e, 0xfc, 0xe5, 0xd7, 0xb3, 0x7b, 0xf6, 0xf1, 0xff,
  558. 0xe3, 0xdb, 0xab, 0x4b, 0x96, 0x31, 0x62, 0xc4, 0x95, 0x37, 0x6e, 0xdc, 0xa5, 0x57, 0xae, 0x41,
  559. 0x82, 0x19, 0x32, 0x64, 0xc8, 0x8d, 0x07, 0x0e, 0x1c, 0x38, 0x70, 0xe0, 0xdd, 0xa7, 0x53, 0xa6,
  560. 0x51, 0xa2, 0x59, 0xb2, 0x79, 0xf2, 0xf9, 0xef, 0xc3, 0x9b, 0x2b, 0x56, 0xac, 0x45, 0x8a, 0x09,
  561. 0x12, 0x24, 0x48, 0x90, 0x3d, 0x7a, 0xf4, 0xf5, 0xf7, 0xf3, 0xfb, 0xeb, 0xcb, 0x8b, 0x0b, 0x16,
  562. 0x2c, 0x58, 0xb0, 0x7d, 0xfa, 0xe9, 0xcf, 0x83, 0x1b, 0x36, 0x6c, 0xd8, 0xad, 0x47, 0x8e, 0x00
  563. ],
  564. /**
  565. * The Galois field log table.
  566. *
  567. * @public
  568. * @static
  569. * @type {number[]}
  570. * @memberof Galois
  571. */
  572. LOG: [
  573. 0xff, 0x00, 0x01, 0x19, 0x02, 0x32, 0x1a, 0xc6, 0x03, 0xdf, 0x33, 0xee, 0x1b, 0x68, 0xc7, 0x4b,
  574. 0x04, 0x64, 0xe0, 0x0e, 0x34, 0x8d, 0xef, 0x81, 0x1c, 0xc1, 0x69, 0xf8, 0xc8, 0x08, 0x4c, 0x71,
  575. 0x05, 0x8a, 0x65, 0x2f, 0xe1, 0x24, 0x0f, 0x21, 0x35, 0x93, 0x8e, 0xda, 0xf0, 0x12, 0x82, 0x45,
  576. 0x1d, 0xb5, 0xc2, 0x7d, 0x6a, 0x27, 0xf9, 0xb9, 0xc9, 0x9a, 0x09, 0x78, 0x4d, 0xe4, 0x72, 0xa6,
  577. 0x06, 0xbf, 0x8b, 0x62, 0x66, 0xdd, 0x30, 0xfd, 0xe2, 0x98, 0x25, 0xb3, 0x10, 0x91, 0x22, 0x88,
  578. 0x36, 0xd0, 0x94, 0xce, 0x8f, 0x96, 0xdb, 0xbd, 0xf1, 0xd2, 0x13, 0x5c, 0x83, 0x38, 0x46, 0x40,
  579. 0x1e, 0x42, 0xb6, 0xa3, 0xc3, 0x48, 0x7e, 0x6e, 0x6b, 0x3a, 0x28, 0x54, 0xfa, 0x85, 0xba, 0x3d,
  580. 0xca, 0x5e, 0x9b, 0x9f, 0x0a, 0x15, 0x79, 0x2b, 0x4e, 0xd4, 0xe5, 0xac, 0x73, 0xf3, 0xa7, 0x57,
  581. 0x07, 0x70, 0xc0, 0xf7, 0x8c, 0x80, 0x63, 0x0d, 0x67, 0x4a, 0xde, 0xed, 0x31, 0xc5, 0xfe, 0x18,
  582. 0xe3, 0xa5, 0x99, 0x77, 0x26, 0xb8, 0xb4, 0x7c, 0x11, 0x44, 0x92, 0xd9, 0x23, 0x20, 0x89, 0x2e,
  583. 0x37, 0x3f, 0xd1, 0x5b, 0x95, 0xbc, 0xcf, 0xcd, 0x90, 0x87, 0x97, 0xb2, 0xdc, 0xfc, 0xbe, 0x61,
  584. 0xf2, 0x56, 0xd3, 0xab, 0x14, 0x2a, 0x5d, 0x9e, 0x84, 0x3c, 0x39, 0x53, 0x47, 0x6d, 0x41, 0xa2,
  585. 0x1f, 0x2d, 0x43, 0xd8, 0xb7, 0x7b, 0xa4, 0x76, 0xc4, 0x17, 0x49, 0xec, 0x7f, 0x0c, 0x6f, 0xf6,
  586. 0x6c, 0xa1, 0x3b, 0x52, 0x29, 0x9d, 0x55, 0xaa, 0xfb, 0x60, 0x86, 0xb1, 0xbb, 0xcc, 0x3e, 0x5a,
  587. 0xcb, 0x59, 0x5f, 0xb0, 0x9c, 0xa9, 0xa0, 0x51, 0x0b, 0xf5, 0x16, 0xeb, 0x7a, 0x75, 0x2c, 0xd7,
  588. 0x4f, 0xae, 0xd5, 0xe9, 0xe6, 0xe7, 0xad, 0xe8, 0x74, 0xd6, 0xf4, 0xea, 0xa8, 0x50, 0x58, 0xaf
  589. ]
  590. });
  591. var Galois_1 = Galois;
  592. /**
  593. * Contains version pattern information.
  594. *
  595. * @public
  596. * @class
  597. * @extends Nevis
  598. */
  599. var Version = lite.extend(null, {
  600. /**
  601. * The version pattern block.
  602. *
  603. * @public
  604. * @static
  605. * @type {number[]}
  606. * @memberof Version
  607. */
  608. BLOCK: [
  609. 0xc94, 0x5bc, 0xa99, 0x4d3, 0xbf6, 0x762, 0x847, 0x60d, 0x928, 0xb78, 0x45d, 0xa17, 0x532,
  610. 0x9a6, 0x683, 0x8c9, 0x7ec, 0xec4, 0x1e1, 0xfab, 0x08e, 0xc1a, 0x33f, 0xd75, 0x250, 0x9d5,
  611. 0x6f0, 0x8ba, 0x79f, 0xb0b, 0x42e, 0xa64, 0x541, 0xc69
  612. ]
  613. });
  614. var Version_1 = Version;
  615. /**
  616. * Generates information for a QR code frame based on a specific value to be encoded.
  617. *
  618. * @param {Frame~Options} options - the options to be used
  619. * @public
  620. * @class
  621. * @extends Nevis
  622. */
  623. var Frame = lite.extend(function(options) {
  624. var dataBlock, eccBlock, index, neccBlock1, neccBlock2;
  625. var valueLength = options.value.length;
  626. this._badness = [];
  627. this._level = ErrorCorrection_1.LEVELS[options.level];
  628. this._polynomial = [];
  629. this._value = options.value;
  630. this._version = 0;
  631. this._stringBuffer = [];
  632. while (this._version < 40) {
  633. this._version++;
  634. index = ((this._level - 1) * 4) + ((this._version - 1) * 16);
  635. neccBlock1 = ErrorCorrection_1.BLOCKS[index++];
  636. neccBlock2 = ErrorCorrection_1.BLOCKS[index++];
  637. dataBlock = ErrorCorrection_1.BLOCKS[index++];
  638. eccBlock = ErrorCorrection_1.BLOCKS[index];
  639. index = (dataBlock * (neccBlock1 + neccBlock2)) + neccBlock2 - 3 + (this._version <= 9);
  640. if (valueLength <= index) {
  641. break;
  642. }
  643. }
  644. this._dataBlock = dataBlock;
  645. this._eccBlock = eccBlock;
  646. this._neccBlock1 = neccBlock1;
  647. this._neccBlock2 = neccBlock2;
  648. /**
  649. * The data width is based on version.
  650. *
  651. * @public
  652. * @type {number}
  653. * @memberof Frame#
  654. */
  655. // FIXME: Ensure that it fits instead of being truncated.
  656. var width = this.width = 17 + (4 * this._version);
  657. /**
  658. * The image buffer.
  659. *
  660. * @public
  661. * @type {number[]}
  662. * @memberof Frame#
  663. */
  664. this.buffer = Frame._createArray(width * width);
  665. this._ecc = Frame._createArray(dataBlock + ((dataBlock + eccBlock) * (neccBlock1 + neccBlock2)) + neccBlock2);
  666. this._mask = Frame._createArray(((width * (width + 1)) + 1) / 2);
  667. this._insertFinders();
  668. this._insertAlignments();
  669. // Insert single foreground cell.
  670. this.buffer[8 + (width * (width - 8))] = 1;
  671. this._insertTimingGap();
  672. this._reverseMask();
  673. this._insertTimingRowAndColumn();
  674. this._insertVersion();
  675. this._syncMask();
  676. this._convertBitStream(valueLength);
  677. this._calculatePolynomial();
  678. this._appendEccToData();
  679. this._interleaveBlocks();
  680. this._pack();
  681. this._finish();
  682. }, {
  683. _addAlignment: function(x, y) {
  684. var i;
  685. var buffer = this.buffer;
  686. var width = this.width;
  687. buffer[x + (width * y)] = 1;
  688. for (i = -2; i < 2; i++) {
  689. buffer[x + i + (width * (y - 2))] = 1;
  690. buffer[x - 2 + (width * (y + i + 1))] = 1;
  691. buffer[x + 2 + (width * (y + i))] = 1;
  692. buffer[x + i + 1 + (width * (y + 2))] = 1;
  693. }
  694. for (i = 0; i < 2; i++) {
  695. this._setMask(x - 1, y + i);
  696. this._setMask(x + 1, y - i);
  697. this._setMask(x - i, y - 1);
  698. this._setMask(x + i, y + 1);
  699. }
  700. },
  701. _appendData: function(data, dataLength, ecc, eccLength) {
  702. var bit, i, j;
  703. var polynomial = this._polynomial;
  704. var stringBuffer = this._stringBuffer;
  705. for (i = 0; i < eccLength; i++) {
  706. stringBuffer[ecc + i] = 0;
  707. }
  708. for (i = 0; i < dataLength; i++) {
  709. bit = Galois_1.LOG[stringBuffer[data + i] ^ stringBuffer[ecc]];
  710. if (bit !== 255) {
  711. for (j = 1; j < eccLength; j++) {
  712. stringBuffer[ecc + j - 1] = stringBuffer[ecc + j] ^
  713. Galois_1.EXPONENT[Frame._modN(bit + polynomial[eccLength - j])];
  714. }
  715. } else {
  716. for (j = ecc; j < ecc + eccLength; j++) {
  717. stringBuffer[j] = stringBuffer[j + 1];
  718. }
  719. }
  720. stringBuffer[ecc + eccLength - 1] = bit === 255 ? 0 : Galois_1.EXPONENT[Frame._modN(bit + polynomial[0])];
  721. }
  722. },
  723. _appendEccToData: function() {
  724. var i;
  725. var data = 0;
  726. var dataBlock = this._dataBlock;
  727. var ecc = this._calculateMaxLength();
  728. var eccBlock = this._eccBlock;
  729. for (i = 0; i < this._neccBlock1; i++) {
  730. this._appendData(data, dataBlock, ecc, eccBlock);
  731. data += dataBlock;
  732. ecc += eccBlock;
  733. }
  734. for (i = 0; i < this._neccBlock2; i++) {
  735. this._appendData(data, dataBlock + 1, ecc, eccBlock);
  736. data += dataBlock + 1;
  737. ecc += eccBlock;
  738. }
  739. },
  740. _applyMask: function(mask) {
  741. var r3x, r3y, x, y;
  742. var buffer = this.buffer;
  743. var width = this.width;
  744. switch (mask) {
  745. case 0:
  746. for (y = 0; y < width; y++) {
  747. for (x = 0; x < width; x++) {
  748. if (!((x + y) & 1) && !this._isMasked(x, y)) {
  749. buffer[x + (y * width)] ^= 1;
  750. }
  751. }
  752. }
  753. break;
  754. case 1:
  755. for (y = 0; y < width; y++) {
  756. for (x = 0; x < width; x++) {
  757. if (!(y & 1) && !this._isMasked(x, y)) {
  758. buffer[x + (y * width)] ^= 1;
  759. }
  760. }
  761. }
  762. break;
  763. case 2:
  764. for (y = 0; y < width; y++) {
  765. for (r3x = 0, x = 0; x < width; x++, r3x++) {
  766. if (r3x === 3) {
  767. r3x = 0;
  768. }
  769. if (!r3x && !this._isMasked(x, y)) {
  770. buffer[x + (y * width)] ^= 1;
  771. }
  772. }
  773. }
  774. break;
  775. case 3:
  776. for (r3y = 0, y = 0; y < width; y++, r3y++) {
  777. if (r3y === 3) {
  778. r3y = 0;
  779. }
  780. for (r3x = r3y, x = 0; x < width; x++, r3x++) {
  781. if (r3x === 3) {
  782. r3x = 0;
  783. }
  784. if (!r3x && !this._isMasked(x, y)) {
  785. buffer[x + (y * width)] ^= 1;
  786. }
  787. }
  788. }
  789. break;
  790. case 4:
  791. for (y = 0; y < width; y++) {
  792. for (r3x = 0, r3y = (y >> 1) & 1, x = 0; x < width; x++, r3x++) {
  793. if (r3x === 3) {
  794. r3x = 0;
  795. r3y = !r3y;
  796. }
  797. if (!r3y && !this._isMasked(x, y)) {
  798. buffer[x + (y * width)] ^= 1;
  799. }
  800. }
  801. }
  802. break;
  803. case 5:
  804. for (r3y = 0, y = 0; y < width; y++, r3y++) {
  805. if (r3y === 3) {
  806. r3y = 0;
  807. }
  808. for (r3x = 0, x = 0; x < width; x++, r3x++) {
  809. if (r3x === 3) {
  810. r3x = 0;
  811. }
  812. if (!((x & y & 1) + !(!r3x | !r3y)) && !this._isMasked(x, y)) {
  813. buffer[x + (y * width)] ^= 1;
  814. }
  815. }
  816. }
  817. break;
  818. case 6:
  819. for (r3y = 0, y = 0; y < width; y++, r3y++) {
  820. if (r3y === 3) {
  821. r3y = 0;
  822. }
  823. for (r3x = 0, x = 0; x < width; x++, r3x++) {
  824. if (r3x === 3) {
  825. r3x = 0;
  826. }
  827. if (!((x & y & 1) + (r3x && r3x === r3y) & 1) && !this._isMasked(x, y)) {
  828. buffer[x + (y * width)] ^= 1;
  829. }
  830. }
  831. }
  832. break;
  833. case 7:
  834. for (r3y = 0, y = 0; y < width; y++, r3y++) {
  835. if (r3y === 3) {
  836. r3y = 0;
  837. }
  838. for (r3x = 0, x = 0; x < width; x++, r3x++) {
  839. if (r3x === 3) {
  840. r3x = 0;
  841. }
  842. if (!((r3x && r3x === r3y) + (x + y & 1) & 1) && !this._isMasked(x, y)) {
  843. buffer[x + (y * width)] ^= 1;
  844. }
  845. }
  846. }
  847. break;
  848. }
  849. },
  850. _calculateMaxLength: function() {
  851. return (this._dataBlock * (this._neccBlock1 + this._neccBlock2)) + this._neccBlock2;
  852. },
  853. _calculatePolynomial: function() {
  854. var i, j;
  855. var eccBlock = this._eccBlock;
  856. var polynomial = this._polynomial;
  857. polynomial[0] = 1;
  858. for (i = 0; i < eccBlock; i++) {
  859. polynomial[i + 1] = 1;
  860. for (j = i; j > 0; j--) {
  861. polynomial[j] = polynomial[j] ? polynomial[j - 1] ^
  862. Galois_1.EXPONENT[Frame._modN(Galois_1.LOG[polynomial[j]] + i)] : polynomial[j - 1];
  863. }
  864. polynomial[0] = Galois_1.EXPONENT[Frame._modN(Galois_1.LOG[polynomial[0]] + i)];
  865. }
  866. // Use logs for generator polynomial to save calculation step.
  867. for (i = 0; i <= eccBlock; i++) {
  868. polynomial[i] = Galois_1.LOG[polynomial[i]];
  869. }
  870. },
  871. _checkBadness: function() {
  872. var b, b1, h, x, y;
  873. var bad = 0;
  874. var badness = this._badness;
  875. var buffer = this.buffer;
  876. var width = this.width;
  877. // Blocks of same colour.
  878. for (y = 0; y < width - 1; y++) {
  879. for (x = 0; x < width - 1; x++) {
  880. // All foreground colour.
  881. if ((buffer[x + (width * y)] &&
  882. buffer[x + 1 + (width * y)] &&
  883. buffer[x + (width * (y + 1))] &&
  884. buffer[x + 1 + (width * (y + 1))]) ||
  885. // All background colour.
  886. !(buffer[x + (width * y)] ||
  887. buffer[x + 1 + (width * y)] ||
  888. buffer[x + (width * (y + 1))] ||
  889. buffer[x + 1 + (width * (y + 1))])) {
  890. bad += Frame.N2;
  891. }
  892. }
  893. }
  894. var bw = 0;
  895. // X runs.
  896. for (y = 0; y < width; y++) {
  897. h = 0;
  898. badness[0] = 0;
  899. for (b = 0, x = 0; x < width; x++) {
  900. b1 = buffer[x + (width * y)];
  901. if (b === b1) {
  902. badness[h]++;
  903. } else {
  904. badness[++h] = 1;
  905. }
  906. b = b1;
  907. bw += b ? 1 : -1;
  908. }
  909. bad += this._getBadness(h);
  910. }
  911. if (bw < 0) {
  912. bw = -bw;
  913. }
  914. var count = 0;
  915. var big = bw;
  916. big += big << 2;
  917. big <<= 1;
  918. while (big > width * width) {
  919. big -= width * width;
  920. count++;
  921. }
  922. bad += count * Frame.N4;
  923. // Y runs.
  924. for (x = 0; x < width; x++) {
  925. h = 0;
  926. badness[0] = 0;
  927. for (b = 0, y = 0; y < width; y++) {
  928. b1 = buffer[x + (width * y)];
  929. if (b === b1) {
  930. badness[h]++;
  931. } else {
  932. badness[++h] = 1;
  933. }
  934. b = b1;
  935. }
  936. bad += this._getBadness(h);
  937. }
  938. return bad;
  939. },
  940. _convertBitStream: function(length) {
  941. var bit, i;
  942. var ecc = this._ecc;
  943. var version = this._version;
  944. // Convert string to bit stream. 8-bit data to QR-coded 8-bit data (numeric, alphanumeric, or kanji not supported).
  945. for (i = 0; i < length; i++) {
  946. ecc[i] = this._value.charCodeAt(i);
  947. }
  948. var stringBuffer = this._stringBuffer = ecc.slice();
  949. var maxLength = this._calculateMaxLength();
  950. if (length >= maxLength - 2) {
  951. length = maxLength - 2;
  952. if (version > 9) {
  953. length--;
  954. }
  955. }
  956. // Shift and re-pack to insert length prefix.
  957. var index = length;
  958. if (version > 9) {
  959. stringBuffer[index + 2] = 0;
  960. stringBuffer[index + 3] = 0;
  961. while (index--) {
  962. bit = stringBuffer[index];
  963. stringBuffer[index + 3] |= 255 & (bit << 4);
  964. stringBuffer[index + 2] = bit >> 4;
  965. }
  966. stringBuffer[2] |= 255 & (length << 4);
  967. stringBuffer[1] = length >> 4;
  968. stringBuffer[0] = 0x40 | (length >> 12);
  969. } else {
  970. stringBuffer[index + 1] = 0;
  971. stringBuffer[index + 2] = 0;
  972. while (index--) {
  973. bit = stringBuffer[index];
  974. stringBuffer[index + 2] |= 255 & (bit << 4);
  975. stringBuffer[index + 1] = bit >> 4;
  976. }
  977. stringBuffer[1] |= 255 & (length << 4);
  978. stringBuffer[0] = 0x40 | (length >> 4);
  979. }
  980. // Fill to end with pad pattern.
  981. index = length + 3 - (version < 10);
  982. while (index < maxLength) {
  983. stringBuffer[index++] = 0xec;
  984. stringBuffer[index++] = 0x11;
  985. }
  986. },
  987. _getBadness: function(length) {
  988. var i;
  989. var badRuns = 0;
  990. var badness = this._badness;
  991. for (i = 0; i <= length; i++) {
  992. if (badness[i] >= 5) {
  993. badRuns += Frame.N1 + badness[i] - 5;
  994. }
  995. }
  996. // FBFFFBF as in finder.
  997. for (i = 3; i < length - 1; i += 2) {
  998. if (badness[i - 2] === badness[i + 2] &&
  999. badness[i + 2] === badness[i - 1] &&
  1000. badness[i - 1] === badness[i + 1] &&
  1001. badness[i - 1] * 3 === badness[i] &&
  1002. // Background around the foreground pattern? Not part of the specs.
  1003. (badness[i - 3] === 0 || i + 3 > length ||
  1004. badness[i - 3] * 3 >= badness[i] * 4 ||
  1005. badness[i + 3] * 3 >= badness[i] * 4)) {
  1006. badRuns += Frame.N3;
  1007. }
  1008. }
  1009. return badRuns;
  1010. },
  1011. _finish: function() {
  1012. // Save pre-mask copy of frame.
  1013. this._stringBuffer = this.buffer.slice();
  1014. var currentMask, i;
  1015. var bit = 0;
  1016. var mask = 30000;
  1017. /*
  1018. * Using for instead of while since in original Arduino code if an early mask was "good enough" it wouldn't try for
  1019. * a better one since they get more complex and take longer.
  1020. */
  1021. for (i = 0; i < 8; i++) {
  1022. // Returns foreground-background imbalance.
  1023. this._applyMask(i);
  1024. currentMask = this._checkBadness();
  1025. // Is current mask better than previous best?
  1026. if (currentMask < mask) {
  1027. mask = currentMask;
  1028. bit = i;
  1029. }
  1030. // Don't increment "i" to a void redoing mask.
  1031. if (bit === 7) {
  1032. break;
  1033. }
  1034. // Reset for next pass.
  1035. this.buffer = this._stringBuffer.slice();
  1036. }
  1037. // Redo best mask as none were "good enough" (i.e. last wasn't bit).
  1038. if (bit !== i) {
  1039. this._applyMask(bit);
  1040. }
  1041. // Add in final mask/ECC level bytes.
  1042. mask = ErrorCorrection_1.FINAL_FORMAT[bit + (this._level - 1 << 3)];
  1043. var buffer = this.buffer;
  1044. var width = this.width;
  1045. // Low byte.
  1046. for (i = 0; i < 8; i++, mask >>= 1) {
  1047. if (mask & 1) {
  1048. buffer[width - 1 - i + (width * 8)] = 1;
  1049. if (i < 6) {
  1050. buffer[8 + (width * i)] = 1;
  1051. } else {
  1052. buffer[8 + (width * (i + 1))] = 1;
  1053. }
  1054. }
  1055. }
  1056. // High byte.
  1057. for (i = 0; i < 7; i++, mask >>= 1) {
  1058. if (mask & 1) {
  1059. buffer[8 + (width * (width - 7 + i))] = 1;
  1060. if (i) {
  1061. buffer[6 - i + (width * 8)] = 1;
  1062. } else {
  1063. buffer[7 + (width * 8)] = 1;
  1064. }
  1065. }
  1066. }
  1067. },
  1068. _interleaveBlocks: function() {
  1069. var i, j;
  1070. var dataBlock = this._dataBlock;
  1071. var ecc = this._ecc;
  1072. var eccBlock = this._eccBlock;
  1073. var k = 0;
  1074. var maxLength = this._calculateMaxLength();
  1075. var neccBlock1 = this._neccBlock1;
  1076. var neccBlock2 = this._neccBlock2;
  1077. var stringBuffer = this._stringBuffer;
  1078. for (i = 0; i < dataBlock; i++) {
  1079. for (j = 0; j < neccBlock1; j++) {
  1080. ecc[k++] = stringBuffer[i + (j * dataBlock)];
  1081. }
  1082. for (j = 0; j < neccBlock2; j++) {
  1083. ecc[k++] = stringBuffer[(neccBlock1 * dataBlock) + i + (j * (dataBlock + 1))];
  1084. }
  1085. }
  1086. for (j = 0; j < neccBlock2; j++) {
  1087. ecc[k++] = stringBuffer[(neccBlock1 * dataBlock) + i + (j * (dataBlock + 1))];
  1088. }
  1089. for (i = 0; i < eccBlock; i++) {
  1090. for (j = 0; j < neccBlock1 + neccBlock2; j++) {
  1091. ecc[k++] = stringBuffer[maxLength + i + (j * eccBlock)];
  1092. }
  1093. }
  1094. this._stringBuffer = ecc;
  1095. },
  1096. _insertAlignments: function() {
  1097. var i, x, y;
  1098. var version = this._version;
  1099. var width = this.width;
  1100. if (version > 1) {
  1101. i = Alignment_1.BLOCK[version];
  1102. y = width - 7;
  1103. for (;;) {
  1104. x = width - 7;
  1105. while (x > i - 3) {
  1106. this._addAlignment(x, y);
  1107. if (x < i) {
  1108. break;
  1109. }
  1110. x -= i;
  1111. }
  1112. if (y <= i + 9) {
  1113. break;
  1114. }
  1115. y -= i;
  1116. this._addAlignment(6, y);
  1117. this._addAlignment(y, 6);
  1118. }
  1119. }
  1120. },
  1121. _insertFinders: function() {
  1122. var i, j, x, y;
  1123. var buffer = this.buffer;
  1124. var width = this.width;
  1125. for (i = 0; i < 3; i++) {
  1126. j = 0;
  1127. y = 0;
  1128. if (i === 1) {
  1129. j = width - 7;
  1130. }
  1131. if (i === 2) {
  1132. y = width - 7;
  1133. }
  1134. buffer[y + 3 + (width * (j + 3))] = 1;
  1135. for (x = 0; x < 6; x++) {
  1136. buffer[y + x + (width * j)] = 1;
  1137. buffer[y + (width * (j + x + 1))] = 1;
  1138. buffer[y + 6 + (width * (j + x))] = 1;
  1139. buffer[y + x + 1 + (width * (j + 6))] = 1;
  1140. }
  1141. for (x = 1; x < 5; x++) {
  1142. this._setMask(y + x, j + 1);
  1143. this._setMask(y + 1, j + x + 1);
  1144. this._setMask(y + 5, j + x);
  1145. this._setMask(y + x + 1, j + 5);
  1146. }
  1147. for (x = 2; x < 4; x++) {
  1148. buffer[y + x + (width * (j + 2))] = 1;
  1149. buffer[y + 2 + (width * (j + x + 1))] = 1;
  1150. buffer[y + 4 + (width * (j + x))] = 1;
  1151. buffer[y + x + 1 + (width * (j + 4))] = 1;
  1152. }
  1153. }
  1154. },
  1155. _insertTimingGap: function() {
  1156. var x, y;
  1157. var width = this.width;
  1158. for (y = 0; y < 7; y++) {
  1159. this._setMask(7, y);
  1160. this._setMask(width - 8, y);
  1161. this._setMask(7, y + width - 7);
  1162. }
  1163. for (x = 0; x < 8; x++) {
  1164. this._setMask(x, 7);
  1165. this._setMask(x + width - 8, 7);
  1166. this._setMask(x, width - 8);
  1167. }
  1168. },
  1169. _insertTimingRowAndColumn: function() {
  1170. var x;
  1171. var buffer = this.buffer;
  1172. var width = this.width;
  1173. for (x = 0; x < width - 14; x++) {
  1174. if (x & 1) {
  1175. this._setMask(8 + x, 6);
  1176. this._setMask(6, 8 + x);
  1177. } else {
  1178. buffer[8 + x + (width * 6)] = 1;
  1179. buffer[6 + (width * (8 + x))] = 1;
  1180. }
  1181. }
  1182. },
  1183. _insertVersion: function() {
  1184. var i, j, x, y;
  1185. var buffer = this.buffer;
  1186. var version = this._version;
  1187. var width = this.width;
  1188. if (version > 6) {
  1189. i = Version_1.BLOCK[version - 7];
  1190. j = 17;
  1191. for (x = 0; x < 6; x++) {
  1192. for (y = 0; y < 3; y++, j--) {
  1193. if (1 & (j > 11 ? version >> j - 12 : i >> j)) {
  1194. buffer[5 - x + (width * (2 - y + width - 11))] = 1;
  1195. buffer[2 - y + width - 11 + (width * (5 - x))] = 1;
  1196. } else {
  1197. this._setMask(5 - x, 2 - y + width - 11);
  1198. this._setMask(2 - y + width - 11, 5 - x);
  1199. }
  1200. }
  1201. }
  1202. }
  1203. },
  1204. _isMasked: function(x, y) {
  1205. var bit = Frame._getMaskBit(x, y);
  1206. return this._mask[bit] === 1;
  1207. },
  1208. _pack: function() {
  1209. var bit, i, j;
  1210. var k = 1;
  1211. var v = 1;
  1212. var width = this.width;
  1213. var x = width - 1;
  1214. var y = width - 1;
  1215. // Interleaved data and ECC codes.
  1216. var length = ((this._dataBlock + this._eccBlock) * (this._neccBlock1 + this._neccBlock2)) + this._neccBlock2;
  1217. for (i = 0; i < length; i++) {
  1218. bit = this._stringBuffer[i];
  1219. for (j = 0; j < 8; j++, bit <<= 1) {
  1220. if (0x80 & bit) {
  1221. this.buffer[x + (width * y)] = 1;
  1222. }
  1223. // Find next fill position.
  1224. do {
  1225. if (v) {
  1226. x--;
  1227. } else {
  1228. x++;
  1229. if (k) {
  1230. if (y !== 0) {
  1231. y--;
  1232. } else {
  1233. x -= 2;
  1234. k = !k;
  1235. if (x === 6) {
  1236. x--;
  1237. y = 9;
  1238. }
  1239. }
  1240. } else if (y !== width - 1) {
  1241. y++;
  1242. } else {
  1243. x -= 2;
  1244. k = !k;
  1245. if (x === 6) {
  1246. x--;
  1247. y -= 8;
  1248. }
  1249. }
  1250. }
  1251. v = !v;
  1252. } while (this._isMasked(x, y));
  1253. }
  1254. }
  1255. },
  1256. _reverseMask: function() {
  1257. var x, y;
  1258. var width = this.width;
  1259. for (x = 0; x < 9; x++) {
  1260. this._setMask(x, 8);
  1261. }
  1262. for (x = 0; x < 8; x++) {
  1263. this._setMask(x + width - 8, 8);
  1264. this._setMask(8, x);
  1265. }
  1266. for (y = 0; y < 7; y++) {
  1267. this._setMask(8, y + width - 7);
  1268. }
  1269. },
  1270. _setMask: function(x, y) {
  1271. var bit = Frame._getMaskBit(x, y);
  1272. this._mask[bit] = 1;
  1273. },
  1274. _syncMask: function() {
  1275. var x, y;
  1276. var width = this.width;
  1277. for (y = 0; y < width; y++) {
  1278. for (x = 0; x <= y; x++) {
  1279. if (this.buffer[x + (width * y)]) {
  1280. this._setMask(x, y);
  1281. }
  1282. }
  1283. }
  1284. }
  1285. }, {
  1286. _createArray: function(length) {
  1287. var i;
  1288. var array = [];
  1289. for (i = 0; i < length; i++) {
  1290. array[i] = 0;
  1291. }
  1292. return array;
  1293. },
  1294. _getMaskBit: function(x, y) {
  1295. var bit;
  1296. if (x > y) {
  1297. bit = x;
  1298. x = y;
  1299. y = bit;
  1300. }
  1301. bit = y;
  1302. bit += y * y;
  1303. bit >>= 1;
  1304. bit += x;
  1305. return bit;
  1306. },
  1307. _modN: function(x) {
  1308. while (x >= 255) {
  1309. x -= 255;
  1310. x = (x >> 8) + (x & 255);
  1311. }
  1312. return x;
  1313. },
  1314. // *Badness* coefficients.
  1315. N1: 3,
  1316. N2: 3,
  1317. N3: 40,
  1318. N4: 10
  1319. });
  1320. var Frame_1 = Frame;
  1321. /**
  1322. * The options used by {@link Frame}.
  1323. *
  1324. * @typedef {Object} Frame~Options
  1325. * @property {string} level - The ECC level to be used.
  1326. * @property {string} value - The value to be encoded.
  1327. */
  1328. /**
  1329. * An implementation of {@link Renderer} for working with <code>img</code> elements.
  1330. *
  1331. * This depends on {@link CanvasRenderer} being executed first as this implementation simply applies the data URL from
  1332. * the rendered <code>canvas</code> element as the <code>src</code> for the <code>img</code> element being rendered.
  1333. *
  1334. * @public
  1335. * @class
  1336. * @extends Renderer
  1337. */
  1338. var ImageRenderer = Renderer_1.extend({
  1339. /**
  1340. * @override
  1341. */
  1342. draw: function() {
  1343. this.element.src = this.qrious.toDataURL();
  1344. },
  1345. /**
  1346. * @override
  1347. */
  1348. reset: function() {
  1349. this.element.src = '';
  1350. },
  1351. /**
  1352. * @override
  1353. */
  1354. resize: function() {
  1355. var element = this.element;
  1356. element.width = element.height = this.qrious.size;
  1357. }
  1358. });
  1359. var ImageRenderer_1 = ImageRenderer;
  1360. /**
  1361. * Defines an available option while also configuring how values are applied to the target object.
  1362. *
  1363. * Optionally, a default value can be specified as well a value transformer for greater control over how the option
  1364. * value is applied.
  1365. *
  1366. * If no value transformer is specified, then any specified option will be applied directly. All values are maintained
  1367. * on the target object itself as a field using the option name prefixed with a single underscore.
  1368. *
  1369. * When an option is specified as modifiable, the {@link OptionManager} will be required to include a setter for the
  1370. * property that is defined on the target object that uses the option name.
  1371. *
  1372. * @param {string} name - the name to be used
  1373. * @param {boolean} [modifiable] - <code>true</code> if the property defined on target objects should include a setter;
  1374. * otherwise <code>false</code>
  1375. * @param {*} [defaultValue] - the default value to be used
  1376. * @param {Option~ValueTransformer} [valueTransformer] - the value transformer to be used
  1377. * @public
  1378. * @class
  1379. * @extends Nevis
  1380. */
  1381. var Option = lite.extend(function(name, modifiable, defaultValue, valueTransformer) {
  1382. /**
  1383. * The name for this {@link Option}.
  1384. *
  1385. * @public
  1386. * @type {string}
  1387. * @memberof Option#
  1388. */
  1389. this.name = name;
  1390. /**
  1391. * Whether a setter should be included on the property defined on target objects for this {@link Option}.
  1392. *
  1393. * @public
  1394. * @type {boolean}
  1395. * @memberof Option#
  1396. */
  1397. this.modifiable = Boolean(modifiable);
  1398. /**
  1399. * The default value for this {@link Option}.
  1400. *
  1401. * @public
  1402. * @type {*}
  1403. * @memberof Option#
  1404. */
  1405. this.defaultValue = defaultValue;
  1406. this._valueTransformer = valueTransformer;
  1407. }, {
  1408. /**
  1409. * Transforms the specified <code>value</code> so that it can be applied for this {@link Option}.
  1410. *
  1411. * If a value transformer has been specified for this {@link Option}, it will be called upon to transform
  1412. * <code>value</code>. Otherwise, <code>value</code> will be returned directly.
  1413. *
  1414. * @param {*} value - the value to be transformed
  1415. * @return {*} The transformed value or <code>value</code> if no value transformer is specified.
  1416. * @public
  1417. * @memberof Option#
  1418. */
  1419. transform: function(value) {
  1420. var transformer = this._valueTransformer;
  1421. if (typeof transformer === 'function') {
  1422. return transformer(value, this);
  1423. }
  1424. return value;
  1425. }
  1426. });
  1427. var Option_1 = Option;
  1428. /**
  1429. * Returns a transformed value for the specified <code>value</code> to be applied for the <code>option</code> provided.
  1430. *
  1431. * @callback Option~ValueTransformer
  1432. * @param {*} value - the value to be transformed
  1433. * @param {Option} option - the {@link Option} for which <code>value</code> is being transformed
  1434. * @return {*} The transform value.
  1435. */
  1436. /**
  1437. * Contains utility methods that are useful throughout the library.
  1438. *
  1439. * @public
  1440. * @class
  1441. * @extends Nevis
  1442. */
  1443. var Utilities = lite.extend(null, {
  1444. /**
  1445. * Returns the absolute value of a given number.
  1446. *
  1447. * This method is simply a convenient shorthand for <code>Math.abs</code> while ensuring that nulls are returned as
  1448. * <code>null</code> instead of zero.
  1449. *
  1450. * @param {number} value - the number whose absolute value is to be returned
  1451. * @return {number} The absolute value of <code>value</code> or <code>null</code> if <code>value</code> is
  1452. * <code>null</code>.
  1453. * @public
  1454. * @static
  1455. * @memberof Utilities
  1456. */
  1457. abs: function(value) {
  1458. return value != null ? Math.abs(value) : null;
  1459. },
  1460. /**
  1461. * Returns whether the specified <code>object</code> has a property with the specified <code>name</code> as an own
  1462. * (not inherited) property.
  1463. *
  1464. * @param {Object} object - the object on which the property is to be checked
  1465. * @param {string} name - the name of the property to be checked
  1466. * @return {boolean} <code>true</code> if <code>object</code> has an own property with <code>name</code>.
  1467. * @public
  1468. * @static
  1469. * @memberof Utilities
  1470. */
  1471. hasOwn: function(object, name) {
  1472. return Object.prototype.hasOwnProperty.call(object, name);
  1473. },
  1474. /**
  1475. * A non-operation method that does absolutely nothing.
  1476. *
  1477. * @return {void}
  1478. * @public
  1479. * @static
  1480. * @memberof Utilities
  1481. */
  1482. noop: function() {},
  1483. /**
  1484. * Transforms the specified <code>string</code> to upper case while remaining null-safe.
  1485. *
  1486. * @param {string} string - the string to be transformed to upper case
  1487. * @return {string} <code>string</code> transformed to upper case if <code>string</code> is not <code>null</code>.
  1488. * @public
  1489. * @static
  1490. * @memberof Utilities
  1491. */
  1492. toUpperCase: function(string) {
  1493. return string != null ? string.toUpperCase() : null;
  1494. }
  1495. });
  1496. var Utilities_1 = Utilities;
  1497. /**
  1498. * Manages multiple {@link Option} instances that are intended to be used by multiple implementations.
  1499. *
  1500. * Although the option definitions are shared between targets, the values are maintained on the targets themselves.
  1501. *
  1502. * @param {Option[]} options - the options to be used
  1503. * @public
  1504. * @class
  1505. * @extends Nevis
  1506. */
  1507. var OptionManager = lite.extend(function(options) {
  1508. /**
  1509. * The available options for this {@link OptionManager}.
  1510. *
  1511. * @public
  1512. * @type {Object.<string, Option>}
  1513. * @memberof OptionManager#
  1514. */
  1515. this.options = {};
  1516. options.forEach(function(option) {
  1517. this.options[option.name] = option;
  1518. }, this);
  1519. }, {
  1520. /**
  1521. * Returns whether an option with the specified <code>name</code> is available.
  1522. *
  1523. * @param {string} name - the name of the {@link Option} whose existence is to be checked
  1524. * @return {boolean} <code>true</code> if an {@link Option} exists with <code>name</code>; otherwise
  1525. * <code>false</code>.
  1526. * @public
  1527. * @memberof OptionManager#
  1528. */
  1529. exists: function(name) {
  1530. return this.options[name] != null;
  1531. },
  1532. /**
  1533. * Returns the value of the option with the specified <code>name</code> on the <code>target</code> object provided.
  1534. *
  1535. * @param {string} name - the name of the {@link Option} whose value on <code>target</code> is to be returned
  1536. * @param {Object} target - the object from which the value of the named {@link Option} is to be returned
  1537. * @return {*} The value of the {@link Option} with <code>name</code> on <code>target</code>.
  1538. * @public
  1539. * @memberof OptionManager#
  1540. */
  1541. get: function(name, target) {
  1542. return OptionManager._get(this.options[name], target);
  1543. },
  1544. /**
  1545. * Returns a copy of all of the available options on the <code>target</code> object provided.
  1546. *
  1547. * @param {Object} target - the object from which the option name/value pairs are to be returned
  1548. * @return {Object.<string, *>} A hash containing the name/value pairs of all options on <code>target</code>.
  1549. * @public
  1550. * @memberof OptionManager#
  1551. */
  1552. getAll: function(target) {
  1553. var name;
  1554. var options = this.options;
  1555. var result = {};
  1556. for (name in options) {
  1557. if (Utilities_1.hasOwn(options, name)) {
  1558. result[name] = OptionManager._get(options[name], target);
  1559. }
  1560. }
  1561. return result;
  1562. },
  1563. /**
  1564. * Initializes the available options for the <code>target</code> object provided and then applies the initial values
  1565. * within the speciifed <code>options</code>.
  1566. *
  1567. * This method will throw an error if any of the names within <code>options</code> does not match an available option.
  1568. *
  1569. * This involves setting the default values and defining properties for all of the available options on
  1570. * <code>target</code> before finally calling {@link OptionMananger#setAll} with <code>options</code> and
  1571. * <code>target</code>. Any options that are configured to be modifiable will have a setter included in their defined
  1572. * property that will allow its corresponding value to be modified.
  1573. *
  1574. * If a change handler is specified, it will be called whenever the value changes on <code>target</code> for a
  1575. * modifiable option, but only when done so via the defined property's setter.
  1576. *
  1577. * @param {Object.<string, *>} options - the name/value pairs of the initial options to be set
  1578. * @param {Object} target - the object on which the options are to be initialized
  1579. * @param {Function} [changeHandler] - the function to be called whenever the value of an modifiable option changes on
  1580. * <code>target</code>
  1581. * @return {void}
  1582. * @throws {Error} If <code>options</code> contains an invalid option name.
  1583. * @public
  1584. * @memberof OptionManager#
  1585. */
  1586. init: function(options, target, changeHandler) {
  1587. if (typeof changeHandler !== 'function') {
  1588. changeHandler = Utilities_1.noop;
  1589. }
  1590. var name, option;
  1591. for (name in this.options) {
  1592. if (Utilities_1.hasOwn(this.options, name)) {
  1593. option = this.options[name];
  1594. OptionManager._set(option, option.defaultValue, target);
  1595. OptionManager._createAccessor(option, target, changeHandler);
  1596. }
  1597. }
  1598. this._setAll(options, target, true);
  1599. },
  1600. /**
  1601. * Sets the value of the option with the specified <code>name</code> on the <code>target</code> object provided to
  1602. * <code>value</code>.
  1603. *
  1604. * This method will throw an error if <code>name</code> does not match an available option or matches an option that
  1605. * cannot be modified.
  1606. *
  1607. * If <code>value</code> is <code>null</code> and the {@link Option} has a default value configured, then that default
  1608. * value will be used instead. If the {@link Option} also has a value transformer configured, it will be used to
  1609. * transform whichever value was determined to be used.
  1610. *
  1611. * This method returns whether the value of the underlying field on <code>target</code> was changed as a result.
  1612. *
  1613. * @param {string} name - the name of the {@link Option} whose value is to be set
  1614. * @param {*} value - the value to be set for the named {@link Option} on <code>target</code>
  1615. * @param {Object} target - the object on which <code>value</code> is to be set for the named {@link Option}
  1616. * @return {boolean} <code>true</code> if the underlying field on <code>target</code> was changed; otherwise
  1617. * <code>false</code>.
  1618. * @throws {Error} If <code>name</code> is invalid or is for an option that cannot be modified.
  1619. * @public
  1620. * @memberof OptionManager#
  1621. */
  1622. set: function(name, value, target) {
  1623. return this._set(name, value, target);
  1624. },
  1625. /**
  1626. * Sets all of the specified <code>options</code> on the <code>target</code> object provided to their corresponding
  1627. * values.
  1628. *
  1629. * This method will throw an error if any of the names within <code>options</code> does not match an available option
  1630. * or matches an option that cannot be modified.
  1631. *
  1632. * If any value within <code>options</code> is <code>null</code> and the corresponding {@link Option} has a default
  1633. * value configured, then that default value will be used instead. If an {@link Option} also has a value transformer
  1634. * configured, it will be used to transform whichever value was determined to be used.
  1635. *
  1636. * This method returns whether the value for any of the underlying fields on <code>target</code> were changed as a
  1637. * result.
  1638. *
  1639. * @param {Object.<string, *>} options - the name/value pairs of options to be set
  1640. * @param {Object} target - the object on which the options are to be set
  1641. * @return {boolean} <code>true</code> if any of the underlying fields on <code>target</code> were changed; otherwise
  1642. * <code>false</code>.
  1643. * @throws {Error} If <code>options</code> contains an invalid option name or an option that cannot be modiifed.
  1644. * @public
  1645. * @memberof OptionManager#
  1646. */
  1647. setAll: function(options, target) {
  1648. return this._setAll(options, target);
  1649. },
  1650. _set: function(name, value, target, allowUnmodifiable) {
  1651. var option = this.options[name];
  1652. if (!option) {
  1653. throw new Error('Invalid option: ' + name);
  1654. }
  1655. if (!option.modifiable && !allowUnmodifiable) {
  1656. throw new Error('Option cannot be modified: ' + name);
  1657. }
  1658. return OptionManager._set(option, value, target);
  1659. },
  1660. _setAll: function(options, target, allowUnmodifiable) {
  1661. if (!options) {
  1662. return false;
  1663. }
  1664. var name;
  1665. var changed = false;
  1666. for (name in options) {
  1667. if (Utilities_1.hasOwn(options, name) && this._set(name, options[name], target, allowUnmodifiable)) {
  1668. changed = true;
  1669. }
  1670. }
  1671. return changed;
  1672. }
  1673. }, {
  1674. _createAccessor: function(option, target, changeHandler) {
  1675. var descriptor = {
  1676. get: function() {
  1677. return OptionManager._get(option, target);
  1678. }
  1679. };
  1680. if (option.modifiable) {
  1681. descriptor.set = function(value) {
  1682. if (OptionManager._set(option, value, target)) {
  1683. changeHandler(value, option);
  1684. }
  1685. };
  1686. }
  1687. Object.defineProperty(target, option.name, descriptor);
  1688. },
  1689. _get: function(option, target) {
  1690. return target['_' + option.name];
  1691. },
  1692. _set: function(option, value, target) {
  1693. var fieldName = '_' + option.name;
  1694. var oldValue = target[fieldName];
  1695. var newValue = option.transform(value != null ? value : option.defaultValue);
  1696. target[fieldName] = newValue;
  1697. return newValue !== oldValue;
  1698. }
  1699. });
  1700. var OptionManager_1 = OptionManager;
  1701. /**
  1702. * Called whenever the value of a modifiable {@link Option} is changed on a target object via the defined property's
  1703. * setter.
  1704. *
  1705. * @callback OptionManager~ChangeHandler
  1706. * @param {*} value - the new value for <code>option</code> on the target object
  1707. * @param {Option} option - the modifable {@link Option} whose value has changed on the target object.
  1708. * @return {void}
  1709. */
  1710. /**
  1711. * A basic manager for {@link Service} implementations that are mapped to simple names.
  1712. *
  1713. * @public
  1714. * @class
  1715. * @extends Nevis
  1716. */
  1717. var ServiceManager = lite.extend(function() {
  1718. this._services = {};
  1719. }, {
  1720. /**
  1721. * Returns the {@link Service} being managed with the specified <code>name</code>.
  1722. *
  1723. * @param {string} name - the name of the {@link Service} to be returned
  1724. * @return {Service} The {@link Service} is being managed with <code>name</code>.
  1725. * @throws {Error} If no {@link Service} is being managed with <code>name</code>.
  1726. * @public
  1727. * @memberof ServiceManager#
  1728. */
  1729. getService: function(name) {
  1730. var service = this._services[name];
  1731. if (!service) {
  1732. throw new Error('Service is not being managed with name: ' + name);
  1733. }
  1734. return service;
  1735. },
  1736. /**
  1737. * Sets the {@link Service} implementation to be managed for the specified <code>name</code> to the
  1738. * <code>service</code> provided.
  1739. *
  1740. * @param {string} name - the name of the {@link Service} to be managed with <code>name</code>
  1741. * @param {Service} service - the {@link Service} implementation to be managed
  1742. * @return {void}
  1743. * @throws {Error} If a {@link Service} is already being managed with the same <code>name</code>.
  1744. * @public
  1745. * @memberof ServiceManager#
  1746. */
  1747. setService: function(name, service) {
  1748. if (this._services[name]) {
  1749. throw new Error('Service is already managed with name: ' + name);
  1750. }
  1751. if (service) {
  1752. this._services[name] = service;
  1753. }
  1754. }
  1755. });
  1756. var ServiceManager_1 = ServiceManager;
  1757. var optionManager = new OptionManager_1([
  1758. new Option_1('background', true, 'white'),
  1759. new Option_1('backgroundAlpha', true, 1, Utilities_1.abs),
  1760. new Option_1('element'),
  1761. new Option_1('foreground', true, 'black'),
  1762. new Option_1('foregroundAlpha', true, 1, Utilities_1.abs),
  1763. new Option_1('level', true, 'L', Utilities_1.toUpperCase),
  1764. new Option_1('mime', true, 'image/png'),
  1765. new Option_1('padding', true, null, Utilities_1.abs),
  1766. new Option_1('size', true, 100, Utilities_1.abs),
  1767. new Option_1('value', true, '')
  1768. ]);
  1769. var serviceManager = new ServiceManager_1();
  1770. /**
  1771. * Enables configuration of a QR code generator which uses HTML5 <code>canvas</code> for rendering.
  1772. *
  1773. * @param {QRious~Options} [options] - the options to be used
  1774. * @throws {Error} If any <code>options</code> are invalid.
  1775. * @public
  1776. * @class
  1777. * @extends Nevis
  1778. */
  1779. var QRious = lite.extend(function(options) {
  1780. optionManager.init(options, this, this.update.bind(this));
  1781. var element = optionManager.get('element', this);
  1782. var elementService = serviceManager.getService('element');
  1783. var canvas = element && elementService.isCanvas(element) ? element : elementService.createCanvas();
  1784. var image = element && elementService.isImage(element) ? element : elementService.createImage();
  1785. this._canvasRenderer = new CanvasRenderer_1(this, canvas, true);
  1786. this._imageRenderer = new ImageRenderer_1(this, image, image === element);
  1787. this.update();
  1788. }, {
  1789. /**
  1790. * Returns all of the options configured for this {@link QRious}.
  1791. *
  1792. * Any changes made to the returned object will not be reflected in the options themselves or their corresponding
  1793. * underlying fields.
  1794. *
  1795. * @return {Object.<string, *>} A copy of the applied options.
  1796. * @public
  1797. * @memberof QRious#
  1798. */
  1799. get: function() {
  1800. return optionManager.getAll(this);
  1801. },
  1802. /**
  1803. * Sets all of the specified <code>options</code> and automatically updates this {@link QRious} if any of the
  1804. * underlying fields are changed as a result.
  1805. *
  1806. * This is the preferred method for updating multiple options at one time to avoid unnecessary updates between
  1807. * changes.
  1808. *
  1809. * @param {QRious~Options} options - the options to be set
  1810. * @return {void}
  1811. * @throws {Error} If any <code>options</code> are invalid or cannot be modified.
  1812. * @public
  1813. * @memberof QRious#
  1814. */
  1815. set: function(options) {
  1816. if (optionManager.setAll(options, this)) {
  1817. this.update();
  1818. }
  1819. },
  1820. /**
  1821. * Returns the image data URI for the generated QR code using the <code>mime</code> provided.
  1822. *
  1823. * @param {string} [mime] - the MIME type for the image
  1824. * @return {string} The image data URI for the QR code.
  1825. * @public
  1826. * @memberof QRious#
  1827. */
  1828. toDataURL: function(mime) {
  1829. return this.canvas.toDataURL(mime || this.mime);
  1830. },
  1831. /**
  1832. * Updates this {@link QRious} by generating a new {@link Frame} and re-rendering the QR code.
  1833. *
  1834. * @return {void}
  1835. * @protected
  1836. * @memberof QRious#
  1837. */
  1838. update: function() {
  1839. var frame = new Frame_1({
  1840. level: this.level,
  1841. value: this.value
  1842. });
  1843. this._canvasRenderer.render(frame);
  1844. this._imageRenderer.render(frame);
  1845. }
  1846. }, {
  1847. /**
  1848. * Configures the <code>service</code> provided to be used by all {@link QRious} instances.
  1849. *
  1850. * @param {Service} service - the {@link Service} to be configured
  1851. * @return {void}
  1852. * @throws {Error} If a {@link Service} has already been configured with the same name.
  1853. * @public
  1854. * @static
  1855. * @memberof QRious
  1856. */
  1857. use: function(service) {
  1858. serviceManager.setService(service.getName(), service);
  1859. }
  1860. });
  1861. Object.defineProperties(QRious.prototype, {
  1862. canvas: {
  1863. /**
  1864. * Returns the <code>canvas</code> element being used to render the QR code for this {@link QRious}.
  1865. *
  1866. * @return {*} The <code>canvas</code> element.
  1867. * @public
  1868. * @memberof QRious#
  1869. * @alias canvas
  1870. */
  1871. get: function() {
  1872. return this._canvasRenderer.getElement();
  1873. }
  1874. },
  1875. image: {
  1876. /**
  1877. * Returns the <code>img</code> element being used to render the QR code for this {@link QRious}.
  1878. *
  1879. * @return {*} The <code>img</code> element.
  1880. * @public
  1881. * @memberof QRious#
  1882. * @alias image
  1883. */
  1884. get: function() {
  1885. return this._imageRenderer.getElement();
  1886. }
  1887. }
  1888. });
  1889. var QRious_1$2 = QRious;
  1890. /**
  1891. * The options used by {@link QRious}.
  1892. *
  1893. * @typedef {Object} QRious~Options
  1894. * @property {string} [background="white"] - The background color to be applied to the QR code.
  1895. * @property {number} [backgroundAlpha=1] - The background alpha to be applied to the QR code.
  1896. * @property {*} [element] - The element to be used to render the QR code which may either be an <code>canvas</code> or
  1897. * <code>img</code>. The element(s) will be created if needed.
  1898. * @property {string} [foreground="black"] - The foreground color to be applied to the QR code.
  1899. * @property {number} [foregroundAlpha=1] - The foreground alpha to be applied to the QR code.
  1900. * @property {string} [level="L"] - The error correction level to be applied to the QR code.
  1901. * @property {string} [mime="image/png"] - The MIME type to be used to render the image for the QR code.
  1902. * @property {number} [padding] - The padding for the QR code in pixels.
  1903. * @property {number} [size=100] - The size of the QR code in pixels.
  1904. * @property {string} [value=""] - The value to be encoded within the QR code.
  1905. */
  1906. var index = QRious_1$2;
  1907. /**
  1908. * Defines a service contract that must be met by all implementations.
  1909. *
  1910. * @public
  1911. * @class
  1912. * @extends Nevis
  1913. */
  1914. var Service = lite.extend({
  1915. /**
  1916. * Returns the name of this {@link Service}.
  1917. *
  1918. * @return {string} The service name.
  1919. * @public
  1920. * @abstract
  1921. * @memberof Service#
  1922. */
  1923. getName: function() {}
  1924. });
  1925. var Service_1 = Service;
  1926. /**
  1927. * A service for working with elements.
  1928. *
  1929. * @public
  1930. * @class
  1931. * @extends Service
  1932. */
  1933. var ElementService = Service_1.extend({
  1934. /**
  1935. * Creates an instance of a canvas element.
  1936. *
  1937. * Implementations of {@link ElementService} <b>must</b> override this method with their own specific logic.
  1938. *
  1939. * @return {*} The newly created canvas element.
  1940. * @public
  1941. * @abstract
  1942. * @memberof ElementService#
  1943. */
  1944. createCanvas: function() {},
  1945. /**
  1946. * Creates an instance of a image element.
  1947. *
  1948. * Implementations of {@link ElementService} <b>must</b> override this method with their own specific logic.
  1949. *
  1950. * @return {*} The newly created image element.
  1951. * @public
  1952. * @abstract
  1953. * @memberof ElementService#
  1954. */
  1955. createImage: function() {},
  1956. /**
  1957. * @override
  1958. */
  1959. getName: function() {
  1960. return 'element';
  1961. },
  1962. /**
  1963. * Returns whether the specified <code>element</code> is a canvas.
  1964. *
  1965. * Implementations of {@link ElementService} <b>must</b> override this method with their own specific logic.
  1966. *
  1967. * @param {*} element - the element to be checked
  1968. * @return {boolean} <code>true</code> if <code>element</code> is a canvas; otherwise <code>false</code>.
  1969. * @public
  1970. * @abstract
  1971. * @memberof ElementService#
  1972. */
  1973. isCanvas: function(element) {},
  1974. /**
  1975. * Returns whether the specified <code>element</code> is an image.
  1976. *
  1977. * Implementations of {@link ElementService} <b>must</b> override this method with their own specific logic.
  1978. *
  1979. * @param {*} element - the element to be checked
  1980. * @return {boolean} <code>true</code> if <code>element</code> is an image; otherwise <code>false</code>.
  1981. * @public
  1982. * @abstract
  1983. * @memberof ElementService#
  1984. */
  1985. isImage: function(element) {}
  1986. });
  1987. var ElementService_1 = ElementService;
  1988. /**
  1989. * An implementation of {@link ElementService} intended for use within a browser environment.
  1990. *
  1991. * @public
  1992. * @class
  1993. * @extends ElementService
  1994. */
  1995. var BrowserElementService = ElementService_1.extend({
  1996. /**
  1997. * @override
  1998. */
  1999. createCanvas: function() {
  2000. return document.createElement('canvas');
  2001. },
  2002. /**
  2003. * @override
  2004. */
  2005. createImage: function() {
  2006. return document.createElement('img');
  2007. },
  2008. /**
  2009. * @override
  2010. */
  2011. isCanvas: function(element) {
  2012. return element instanceof HTMLCanvasElement;
  2013. },
  2014. /**
  2015. * @override
  2016. */
  2017. isImage: function(element) {
  2018. return element instanceof HTMLImageElement;
  2019. }
  2020. });
  2021. var BrowserElementService_1 = BrowserElementService;
  2022. index.use(new BrowserElementService_1());
  2023. var QRious_1 = index;
  2024. return QRious_1;
  2025. })));
  2026. });
  2027. var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
  2028. var index = {
  2029. name: 'qrcode',
  2030. props: {
  2031. /**
  2032. * The options for the QR code generator.
  2033. * {@link https://github.com/neocotic/qrious#api}
  2034. */
  2035. options: Object,
  2036. /**
  2037. * The tag of the component root element.
  2038. */
  2039. tag: {
  2040. type: String,
  2041. default: 'canvas'
  2042. },
  2043. /**
  2044. * The value of the QR code.
  2045. */
  2046. value: {
  2047. type: null,
  2048. default: ''
  2049. }
  2050. },
  2051. render: function render(createElement) {
  2052. return createElement(this.tag, this.$slots.default);
  2053. },
  2054. watch: {
  2055. /**
  2056. * Update QR code when value change.
  2057. */
  2058. value: function value() {
  2059. this.generate();
  2060. },
  2061. /**
  2062. * Update QR code when options change.
  2063. */
  2064. options: function options() {
  2065. this.generate();
  2066. }
  2067. },
  2068. methods: {
  2069. /**
  2070. * Generate QR code.
  2071. */
  2072. generate: function generate() {
  2073. if (this.$el) {
  2074. new qrious(_extends({
  2075. element: this.$el,
  2076. value: String(this.value)
  2077. }, this.options));
  2078. }
  2079. }
  2080. },
  2081. mounted: function mounted() {
  2082. this.generate();
  2083. }
  2084. };
  2085. export default index;