vue-qrcode.common.js 72 KB

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