UNPKG

13.6 kBJavaScriptView Raw
1!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.qrCodeGenerator=e()}(this,function(){"use strict";var N=new function(){this.qrBorder=7,this.tileSize=96,this.incTileSize=96,this.minImageTiles=5,this.QrCode=function(g,v,t,e){if(g<E||w<g)throw"Version value out of range";if(e<-1||7<e)throw"Mask value out of range";if(!(v instanceof M))throw"QrCode.Ecc expected";for(var c=4*g+17,r=[],n=0;n<c;n++)r.push(!1);var f=[],h=[];for(n=0;n<c;n++)f.push(r.slice()),h.push(r.slice());if(function(){for(var t=0;t<c;t++)l(6,t,t%2==0),l(t,6,t%2==0);s(3,3),s(c-4,3),s(3,c-4);for(var e=function(){{if(1==g)return[];for(var t=Math.floor(g/7)+2,e=32==g?26:2*Math.ceil((c-13)/(2*t-2)),r=[6],n=c-7;r.length<t;n-=e)r.splice(1,0,n);return r}}(),r=e.length,t=0;t<r;t++)for(var n=0;n<r;n++)0==t&&0==n||0==t&&n==r-1||t==r-1&&0==n||u(e[t],e[n]);a(0),function(){if(g<7)return;for(var t=g,e=0;e<12;e++)t=t<<1^7973*(t>>>11);var r=g<<12|t;if(r>>>18!=0)throw"Assertion error";for(var e=0;e<18;e++){var n=p(r,e),i=c-11+e%3,o=Math.floor(e/3);l(i,o,n),l(o,i,n)}}()}(),function(t){if(t.length!=Math.floor(C.getNumRawDataModules(g)/8))throw"Invalid argument";for(var e=0,r=c-1;1<=r;r-=2){6==r&&(r=5);for(var n=0;n<c;n++)for(var i=0;i<2;i++){var o=r-i,a=0==(r+1&2),s=a?c-1-n:n;!h[s][o]&&e<8*t.length&&(f[s][o]=p(t[e>>>3],7-(7&e)),e++)}}if(e!=8*t.length)throw"Assertion error"}(function(t){if(t.length!=C.getNumDataCodewords(g,v))throw"Invalid argument";for(var e=C.NUM_ERROR_CORRECTION_BLOCKS[v.ordinal][g],r=C.ECC_CODEWORDS_PER_BLOCK[v.ordinal][g],n=Math.floor(C.getNumRawDataModules(g)/8),i=e-n%e,o=Math.floor(n/e),a=[],s=new R(r),h=0,f=0;h<e;h++){var u=t.slice(f,f+o-r+(h<i?0:1));f+=u.length;var l=s.getRemainder(u);h<i&&u.push(0),a.push(u.concat(l))}for(var c=[],h=0;h<a[0].length;h++)for(var d=0;d<a.length;d++)(h!=o-r||i<=d)&&c.push(a[d][h]);if(c.length!=n)throw"Assertion error";return c}(t)),-1==e){var i=1/0;for(n=0;n<8;n++){d(n),a(n);var o=m();o<i&&(e=n,i=o),d(n)}}if(e<0||7<e)throw"Assertion error";function a(t){for(var e=v.formatBits<<3|t,r=e,n=0;n<10;n++)r=r<<1^1335*(r>>>9);var i=21522^(e<<10|r);if(i>>>15!=0)throw"Assertion error";for(n=0;n<=5;n++)l(8,n,p(i,n));l(8,7,p(i,6)),l(8,8,p(i,7)),l(7,8,p(i,8));for(n=9;n<15;n++)l(14-n,8,p(i,n));for(n=0;n<8;n++)l(c-1-n,8,p(i,n));for(n=8;n<15;n++)l(8,c-15+n,p(i,n));l(8,c-8,!0)}function s(t,e){for(var r=-4;r<=4;r++)for(var n=-4;n<=4;n++){var i=Math.max(Math.abs(n),Math.abs(r)),o=t+n,a=e+r;0<=o&&o<c&&0<=a&&a<c&&l(o,a,2!=i&&4!=i)}}function u(t,e){for(var r=-2;r<=2;r++)for(var n=-2;n<=2;n++)l(t+n,e+r,1!=Math.max(Math.abs(n),Math.abs(r)))}function l(t,e,r){f[e][t]=r,h[e][t]=!0}function d(t){if(t<0||7<t)throw"Mask value out of range";for(var e=0;e<c;e++)for(var r=0;r<c;r++){var n;switch(t){case 0:n=(r+e)%2==0;break;case 1:n=e%2==0;break;case 2:n=r%3==0;break;case 3:n=(r+e)%3==0;break;case 4:n=(Math.floor(r/3)+Math.floor(e/2))%2==0;break;case 5:n=r*e%2+r*e%3==0;break;case 6:n=(r*e%2+r*e%3)%2==0;break;case 7:n=((r+e)%2+r*e%3)%2==0;break;default:throw"Assertion error"}!h[e][r]&&n&&(f[e][r]=!f[e][r])}}function m(){for(var t=0,e=0;e<c;e++){for(var r=[0,0,0,0,0,0,0],n=!1,i=0,o=0;o<c;o++)f[e][o]==n?5==++i?t+=C.PENALTY_N1:5<i&&t++:(C.addRunToHistory(i,r),!n&&C.hasFinderLikePattern(r)&&(t+=C.PENALTY_N3),n=f[e][o],i=1);C.addRunToHistory(i,r),n&&C.addRunToHistory(0,r),C.hasFinderLikePattern(r)&&(t+=C.PENALTY_N3)}for(o=0;o<c;o++){r=[0,0,0,0,0,0,0],n=!1;var a=0;for(e=0;e<c;e++)f[e][o]==n?5==++a?t+=C.PENALTY_N1:5<a&&t++:(C.addRunToHistory(a,r),!n&&C.hasFinderLikePattern(r)&&(t+=C.PENALTY_N3),n=f[e][o],a=1);C.addRunToHistory(a,r),n&&C.addRunToHistory(0,r),C.hasFinderLikePattern(r)&&(t+=C.PENALTY_N3)}for(e=0;e<c-1;e++)for(o=0;o<c-1;o++){(n=f[e][o])==f[e][o+1]&&n==f[e+1][o]&&n==f[e+1][o+1]&&(t+=C.PENALTY_N2)}var s=0;f.forEach(function(t){t.forEach(function(t){t&&s++})});var h=c*c;return t+=(Math.ceil(Math.abs(20*s-10*h)/h)-1)*C.PENALTY_N4}function p(t,e){return 0!=(t>>>e&1)}d(e),a(e),h=null,Object.defineProperty(this,"version",{value:g}),Object.defineProperty(this,"size",{value:c}),Object.defineProperty(this,"errorCorrectionLevel",{value:v}),Object.defineProperty(this,"mask",{value:e}),this.getPixel=function(t,e){var r=(c-13)/2;r<N.minImageTiles&&(r=N.minImageTiles);var n=c-2*N.qrBorder-r;return n%2==0&&n++,n/=2,n--,!(t>N.qrBorder+n&&e>N.qrBorder+n&&t<c-n-N.qrBorder-1&&e<c-n-N.qrBorder-1)&&(0<=t&&t<c&&0<=e&&e<c&&f[e][t])},this.drawCanvas=function(t,e,r){if(t<=0||e<0)throw"Value out of range";var n=(c+2*e)*t;r.width=n,r.height=n;for(var i=r.getContext("2d"),o=-e;o<c+e;o++)for(var a=-e;a<c+e;a++)i.fillStyle=this.getPixel(a,o)?"#000000":"#FFFFFF",i.fillRect((a+e)*t,(o+e)*t,t,t)},this.getNeighbors=function(t,e){return{l:this.getPixel(t-1,e),r:this.getPixel(t+1,e),t:this.getPixel(t,e-1),b:this.getPixel(t,e+1)}},this.toSvgString=function(){for(var t=[],e=0,r=0,n=0,i=0,o=0;o<c;o++){for(var a=e=0;a<c;a++){n=a+e,e+=N.tileSize,i=o+r;var s=this.getNeighbors(a,o),h="";this.getPixel(a,o)?(h="",h=s.l||s.r||s.t||s.b?s.l||s.r||s.t||!s.b?s.l||!s.r||s.t||s.b?!s.l||s.r||s.t||s.b?s.l||s.r||!s.t||s.b?s.l&&!s.r&&!s.t&&s.b?'<path d="m0 100h100v-71.5c0-15.8-12.8-28.5-28.5-28.5h-71.5v100z"/>':s.l&&!s.r&&s.t&&!s.b?'<path d="m0 0v100h71.5c15.8 0 28.5-12.8 28.5-28.5v-71.5h-100z"/>':!s.l&&s.r&&!s.t&&s.b?'<path d="m100 100v-100h-71.5c-15.8 0-28.5 12.8-28.5 28.5v71.5h100z"/>':!s.l&&s.r&&s.t&&!s.b?'<path d="m100 0h-100v71.5c0 15.8 12.8 28.5 28.5 28.5h71.5v-100z"/>':'<rect width="100" height="100"/>':'<path d="M0,0v71.4C0,87.3,12.8,100,28.6,100h42.9c15.9,0,28.6-12.8,28.6-28.6V0H0z"/>':'<path d="M0,100h71.4c15.9,0,28.6-12.8,28.6-28.6V28.6C100,12.7,87.2,0,71.4,0H0V100z"/>':'<path d="M100,0H28.6C12.7,0,0,12.8,0,28.6v42.9C0,87.3,12.8,100,28.6,100H100V0z"/>':'<path d="M100,100V28.6C100,12.7,87.2,0,71.4,0H28.6C12.7,0,0,12.8,0,28.6V100H100z"/>':'<path d="M0,28.6v42.9C0,87.3,12.8,100,28.6,100h42.9c15.9,0,28.6-12.8,28.6-28.6V28.6C100,12.7,87.2,0,71.4,0H28.6 C12.8,0,0,12.8,0,28.6z"/>',t.push('<g transform="translate('+n+(0!==i?","+i:"")+')">'+h+"</g>")):(s.l&&s.t&&this.getPixel(a-1,o-1)&&t.push('<g transform="translate('+n+(0!==i?","+i:"")+')"><path d="M30.5,2V0H0v30.5h2C2,14.7,14.8,2,30.5,2z"/></g>'),s.l&&s.b&&this.getPixel(a-1,o+1)&&t.push('<g transform="translate('+n+(0!==i?","+i:"")+')"><path d="M2,69.5H0V100h30.5v-2C14.7,98,2,85.2,2,69.5z"/></g>'),s.r&&s.t&&this.getPixel(a+1,o-1)&&t.push('<g transform="translate('+n+(0!==i?","+i:"")+')"><path d="M98,30.5h2V0H69.5v2C85.3,2,98,14.8,98,30.5z"/></g>'),s.r&&s.b&&this.getPixel(a+1,o+1)&&t.push('<g transform="translate('+n+(0!==i?","+i:"")+')"><path d="M69.5,98v2H100V69.5h-2C98,85.3,85.2,98,69.5,98z"/></g>'))}r+=N.tileSize}var f=(c-(2*N.qrBorder-1))/2,u=f*N.incTileSize,l=(c-f)/2*N.incTileSize+N.incTileSize/4;return t.push('<image transform="translate('+l+","+l+')" width="'+u+'" height="'+u+'" xlink:href="https://upload.wikimedia.org/wikipedia/commons/2/21/VK.com-logo.svg"/>'),'<?xml version="1.0" encoding="UTF-8"?>\n<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n<svg version="1.1" viewBox="0 0 '+(c*N.incTileSize+N.incTileSize)+" "+(c*N.incTileSize+N.incTileSize)+'" xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">\n<g transform="translate(0,0)">\n'+t.join("\n")+"</g>\n</svg>\n"}},this.QrCode.encodeText=function(t,e){var r=N.QrSegment.makeSegments(t);return this.encodeSegments(r,e)},this.QrCode.encodeBinary=function(t,e){var r=N.QrSegment.makeBytes(t);return this.encodeSegments([r],e)},this.QrCode.encodeSegments=function(t,e,r,n,i,o){if(null==r&&(r=E),null==n&&(n=w),null==i&&(i=-1),null==o&&(o=!0),!(E<=r&&r<=n&&n<=w)||i<-1||7<i)throw"Invalid value";var a,s;for(a=r;;a++){var h=8*C.getNumDataCodewords(a,e);if((s=N.QrSegment.getTotalBits(t,a))<=h)break;if(n<=a)throw"Data too long"}[this.Ecc.MEDIUM,this.Ecc.QUARTILE,this.Ecc.HIGH].forEach(function(t){o&&s<=8*C.getNumDataCodewords(a,t)&&(e=t)});var f=new c;if(t.forEach(function(t){f.appendBits(t.mode.modeBits,4),f.appendBits(t.numChars,t.mode.numCharCountBits(a)),t.getData().forEach(function(t){f.push(t)})}),f.length!=s)throw"Assertion error";h=8*C.getNumDataCodewords(a,e);if(f.length>h)throw"Assertion error";if(f.appendBits(0,Math.min(4,h-f.length)),f.appendBits(0,(8-f.length%8)%8),f.length%8!=0)throw"Assertion error";for(var u=236;f.length<h;u^=253)f.appendBits(u,8);for(var l=[];8*l.length<f.length;)l.push(0);return f.forEach(function(t,e){l[e>>>3]|=t<<7-(7&e)}),new this(a,e,l,i)};var C={getNumRawDataModules:function(t){if(t<E||w<t)throw"Version number out of range";var e=(16*t+128)*t+64;if(2<=t){var r=Math.floor(t/7)+2;e-=(25*r-10)*r-55,7<=t&&(e-=36)}return e},getNumDataCodewords:function(t,e){return Math.floor(C.getNumRawDataModules(t)/8)-C.ECC_CODEWORDS_PER_BLOCK[e.ordinal][t]*C.NUM_ERROR_CORRECTION_BLOCKS[e.ordinal][t]},addRunToHistory:function(t,e){e.pop(),e.unshift(t)},hasFinderLikePattern:function(t){var e=t[1];return 0<e&&t[2]==e&&t[4]==e&&t[5]==e&&t[3]==3*e&&Math.max(t[0],t[6])>=4*e}},E=1,w=40;function M(t,e){Object.defineProperty(this,"ordinal",{value:t}),Object.defineProperty(this,"formatBits",{value:e})}Object.defineProperty(this.QrCode,"MIN_VERSION",{value:E}),Object.defineProperty(this.QrCode,"MAX_VERSION",{value:w}),C.PENALTY_N1=3,C.PENALTY_N2=3,C.PENALTY_N3=40,C.PENALTY_N4=10,C.ECC_CODEWORDS_PER_BLOCK=[[null,7,10,15,20,26,18,20,24,30,18,20,24,26,30,22,24,28,30,28,28,28,28,30,30,26,28,30,30,30,30,30,30,30,30,30,30,30,30,30,30],[null,10,16,26,18,24,16,18,22,22,26,30,22,22,24,24,28,28,26,26,26,26,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28],[null,13,22,18,26,18,24,18,22,20,24,28,26,24,20,30,24,28,28,26,30,28,30,30,30,30,28,30,30,30,30,30,30,30,30,30,30,30,30,30,30],[null,17,28,22,16,22,28,26,26,24,28,24,28,22,24,24,30,28,28,26,28,30,24,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30]],C.NUM_ERROR_CORRECTION_BLOCKS=[[null,1,1,1,1,1,2,2,2,2,4,4,4,4,4,6,6,6,6,7,8,8,9,9,10,12,12,12,13,14,15,16,17,18,19,19,20,21,22,24,25],[null,1,1,1,2,2,4,4,4,5,5,5,8,9,9,10,10,11,13,14,16,17,17,18,20,21,23,25,26,28,29,31,33,35,37,38,40,43,45,47,49],[null,1,1,2,2,4,4,6,6,8,8,8,10,12,16,12,17,16,18,21,20,23,23,25,27,29,34,34,35,38,40,43,45,48,51,53,56,59,62,65,68],[null,1,1,2,4,4,4,5,6,8,8,11,11,16,16,18,16,19,21,25,25,25,34,30,32,35,37,40,42,45,48,51,54,57,60,63,66,70,74,77,81]],this.QrCode.Ecc={LOW:new M(0,1),MEDIUM:new M(1,0),QUARTILE:new M(2,3),HIGH:new M(3,2)},this.QrSegment=function(t,e,r){if(e<0||!(t instanceof n))throw"Invalid argument";r=r.slice(),Object.defineProperty(this,"mode",{value:t}),Object.defineProperty(this,"numChars",{value:e}),this.getData=function(){return r.slice()}},this.QrSegment.makeBytes=function(t){var e=new c;return t.forEach(function(t){e.appendBits(t,8)}),new this(this.Mode.BYTE,t.length,e)},this.QrSegment.makeNumeric=function(t){if(!this.NUMERIC_REGEX.test(t))throw"String contains non-numeric characters";for(var e=new c,r=0;r<t.length;){var n=Math.min(t.length-r,3);e.appendBits(parseInt(t.substring(r,r+n),10),3*n+1),r+=n}return new this(this.Mode.NUMERIC,t.length,e)},this.QrSegment.makeAlphanumeric=function(t){if(!this.ALPHANUMERIC_REGEX.test(t))throw"String contains unencodable characters in alphanumeric mode";var e,r=new c;for(e=0;e+2<=t.length;e+=2){var n=45*i.ALPHANUMERIC_CHARSET.indexOf(t.charAt(e));n+=i.ALPHANUMERIC_CHARSET.indexOf(t.charAt(e+1)),r.appendBits(n,11)}return e<t.length&&r.appendBits(i.ALPHANUMERIC_CHARSET.indexOf(t.charAt(e)),6),new this(this.Mode.ALPHANUMERIC,t.length,r)},this.QrSegment.makeSegments=function(t){return""==t?[]:this.NUMERIC_REGEX.test(t)?[this.makeNumeric(t)]:this.ALPHANUMERIC_REGEX.test(t)?[this.makeAlphanumeric(t)]:[this.makeBytes(function(t){t=encodeURI(t);for(var e=[],r=0;r<t.length;r++)"%"!=t.charAt(r)?e.push(t.charCodeAt(r)):(e.push(parseInt(t.substring(r+1,r+3),16)),r+=2);return e}(t))]},this.QrSegment.makeEci=function(t){var e=new c;if(t<0)throw"ECI assignment value out of range";if(t<128)e.appendBits(t,8);else if(t<16384)e.appendBits(2,2),e.appendBits(t,14);else{if(!(t<1e6))throw"ECI assignment value out of range";e.appendBits(6,3),e.appendBits(t,21)}return new this(this.Mode.ECI,0,e)},this.QrSegment.getTotalBits=function(t,e){for(var r=0,n=0;n<t.length;n++){var i=t[n],o=i.mode.numCharCountBits(e);if(i.numChars>=1<<o)return 1/0;r+=4+o+i.getData().length}return r};var i={};function n(t,e){Object.defineProperty(this,"modeBits",{value:t}),this.numCharCountBits=function(t){return e[Math.floor((t+7)/17)]}}function R(t){if(t<1||255<t)throw"Degree out of range";for(var e=[],r=0;r<t-1;r++)e.push(0);e.push(1);var n=1;for(r=0;r<t;r++){for(var i=0;i<e.length;i++)e[i]=R.multiply(e[i],n),i+1<e.length&&(e[i]^=e[i+1]);n=R.multiply(n,2)}this.getRemainder=function(t){var n=e.map(function(){return 0});return t.forEach(function(t){var r=t^n.shift();n.push(0),e.forEach(function(t,e){n[e]^=R.multiply(t,r)})}),n}}function c(){Array.call(this),this.appendBits=function(t,e){if(e<0||31<e||t>>>e!=0)throw"Value out of range";for(var r=e-1;0<=r;r--)this.push(t>>>r&1)}}this.QrSegment.NUMERIC_REGEX=/^[0-9]*$/,this.QrSegment.ALPHANUMERIC_REGEX=/^[A-Z0-9 $%*+.\/:-]*$/,i.ALPHANUMERIC_CHARSET="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:",this.QrSegment.Mode={NUMERIC:new n(1,[10,12,14]),ALPHANUMERIC:new n(2,[9,11,13]),BYTE:new n(4,[8,16,16]),KANJI:new n(8,[8,10,12]),ECI:new n(7,[0,0,0])},R.multiply=function(t,e){if(t>>>8!=0||e>>>8!=0)throw"Byte out of range";for(var r=0,n=7;0<=n;n--)r=r<<1^285*(r>>>7),r^=(e>>>n&1)*t;if(r>>>8!=0)throw"Assertion error";return r},(c.prototype=Object.create(Array.prototype)).constructor=c};return N});