vue-qrcode.js 74 KB

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