qrious.js 70 KB

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