-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqr.min.js
6 lines (6 loc) · 12.6 KB
/
qr.min.js
1
2
3
4
5
6
// THIS is compiled from `qr-src/qr-code.ts` with `npm run build-qr-generator`
var QR=(()=>{var v=Object.defineProperty;var I=Object.getOwnPropertyDescriptor;var S=Object.getOwnPropertyNames;var B=Object.prototype.hasOwnProperty;var z=(d,s,h)=>s in d?v(d,s,{enumerable:!0,configurable:!0,writable:!0,value:h}):d[s]=h;var T=(d,s)=>{for(var h in s)v(d,h,{get:s[h],enumerable:!0})},O=(d,s,h,m)=>{if(s&&typeof s=="object"||typeof s=="function")for(let c of S(s))!B.call(d,c)&&c!==h&&v(d,c,{get:()=>s[c],enumerable:!(m=I(s,c))||m.enumerable});return d};var F=d=>O(v({},"__esModule",{value:!0}),d);var y=(d,s,h)=>(z(d,typeof s!="symbol"?s+"":s,h),h);var U={};T(U,{renderTextToElement:()=>D});var g;(w=>{let o=class o{constructor(t,i,e,r){this.version=t;this.errorCorrectionLevel=i;y(this,"size");y(this,"mask");y(this,"modules",[]);y(this,"isFunction",[]);if(t<o.MIN_VERSION||t>o.MAX_VERSION)throw new RangeError("Version value out of range");if(r<-1||r>7)throw new RangeError("Mask value out of range");this.size=t*4+17;let n=[];for(let a=0;a<this.size;a++)n.push(!1);for(let a=0;a<this.size;a++)this.modules.push(n.slice()),this.isFunction.push(n.slice());this.drawFunctionPatterns();let l=this.addEccAndInterleave(e);if(this.drawCodewords(l),r==-1){let a=1e9;for(let p=0;p<8;p++){this.applyMask(p),this.drawFormatBits(p);let u=this.getPenaltyScore();u<a&&(r=p,a=u),this.applyMask(p)}}m(0<=r&&r<=7),this.mask=r,this.applyMask(r),this.drawFormatBits(r),this.isFunction=[]}static encodeText(t,i){let e=w.QrSegment.makeSegments(t);return o.encodeSegments(e,i)}static encodeBinary(t,i){let e=w.QrSegment.makeBytes(t);return o.encodeSegments([e],i)}static encodeSegments(t,i,e=1,r=40,n=-1,l=!0){if(!(o.MIN_VERSION<=e&&e<=r&&r<=o.MAX_VERSION)||n<-1||n>7)throw new RangeError("Invalid value");let a,p;for(a=e;;a++){let b=o.getNumDataCodewords(a,i)*8,A=c.getTotalBits(t,a);if(A<=b){p=A;break}if(a>=r)throw new RangeError("Data too long")}for(let b of[o.Ecc.MEDIUM,o.Ecc.QUARTILE,o.Ecc.HIGH])l&&p<=o.getNumDataCodewords(a,b)*8&&(i=b);let u=[];for(let b of t){s(b.mode.modeBits,4,u),s(b.numChars,b.mode.numCharCountBits(a),u);for(let A of b.getData())u.push(A)}m(u.length==p);let R=o.getNumDataCodewords(a,i)*8;m(u.length<=R),s(0,Math.min(4,R-u.length),u),s(0,(8-u.length%8)%8,u),m(u.length%8==0);for(let b=236;u.length<R;b^=253)s(b,8,u);let E=[];for(;E.length*8<u.length;)E.push(0);return u.forEach((b,A)=>E[A>>>3]|=b<<7-(A&7)),new o(a,i,E,n)}getModule(t,i){return 0<=t&&t<this.size&&0<=i&&i<this.size&&this.modules[i][t]}drawFunctionPatterns(){for(let e=0;e<this.size;e++)this.setFunctionModule(6,e,e%2==0),this.setFunctionModule(e,6,e%2==0);this.drawFinderPattern(3,3),this.drawFinderPattern(this.size-4,3),this.drawFinderPattern(3,this.size-4);let t=this.getAlignmentPatternPositions(),i=t.length;for(let e=0;e<i;e++)for(let r=0;r<i;r++)e==0&&r==0||e==0&&r==i-1||e==i-1&&r==0||this.drawAlignmentPattern(t[e],t[r]);this.drawFormatBits(0),this.drawVersion()}drawFormatBits(t){let i=this.errorCorrectionLevel.formatBits<<3|t,e=i;for(let n=0;n<10;n++)e=e<<1^(e>>>9)*1335;let r=(i<<10|e)^21522;m(r>>>15==0);for(let n=0;n<=5;n++)this.setFunctionModule(8,n,h(r,n));this.setFunctionModule(8,7,h(r,6)),this.setFunctionModule(8,8,h(r,7)),this.setFunctionModule(7,8,h(r,8));for(let n=9;n<15;n++)this.setFunctionModule(14-n,8,h(r,n));for(let n=0;n<8;n++)this.setFunctionModule(this.size-1-n,8,h(r,n));for(let n=8;n<15;n++)this.setFunctionModule(8,this.size-15+n,h(r,n));this.setFunctionModule(8,this.size-8,!0)}drawVersion(){if(this.version<7)return;let t=this.version;for(let e=0;e<12;e++)t=t<<1^(t>>>11)*7973;let i=this.version<<12|t;m(i>>>18==0);for(let e=0;e<18;e++){let r=h(i,e),n=this.size-11+e%3,l=Math.floor(e/3);this.setFunctionModule(n,l,r),this.setFunctionModule(l,n,r)}}drawFinderPattern(t,i){for(let e=-4;e<=4;e++)for(let r=-4;r<=4;r++){let n=Math.max(Math.abs(r),Math.abs(e)),l=t+r,a=i+e;0<=l&&l<this.size&&0<=a&&a<this.size&&this.setFunctionModule(l,a,n!=2&&n!=4)}}drawAlignmentPattern(t,i){for(let e=-2;e<=2;e++)for(let r=-2;r<=2;r++)this.setFunctionModule(t+r,i+e,Math.max(Math.abs(r),Math.abs(e))!=1)}setFunctionModule(t,i,e){this.modules[i][t]=e,this.isFunction[i][t]=!0}addEccAndInterleave(t){let i=this.version,e=this.errorCorrectionLevel;if(t.length!=o.getNumDataCodewords(i,e))throw new RangeError("Invalid argument");let r=o.NUM_ERROR_CORRECTION_BLOCKS[e.ordinal][i],n=o.ECC_CODEWORDS_PER_BLOCK[e.ordinal][i],l=Math.floor(o.getNumRawDataModules(i)/8),a=r-l%r,p=Math.floor(l/r),u=[],R=o.reedSolomonComputeDivisor(n);for(let b=0,A=0;b<r;b++){let M=t.slice(A,A+p-n+(b<a?0:1));A+=M.length;let P=o.reedSolomonComputeRemainder(M,R);b<a&&M.push(0),u.push(M.concat(P))}let E=[];for(let b=0;b<u[0].length;b++)u.forEach((A,M)=>{(b!=p-n||M>=a)&&E.push(A[b])});return m(E.length==l),E}drawCodewords(t){if(t.length!=Math.floor(o.getNumRawDataModules(this.version)/8))throw new RangeError("Invalid argument");let i=0;for(let e=this.size-1;e>=1;e-=2){e==6&&(e=5);for(let r=0;r<this.size;r++)for(let n=0;n<2;n++){let l=e-n,p=(e+1&2)==0?this.size-1-r:r;!this.isFunction[p][l]&&i<t.length*8&&(this.modules[p][l]=h(t[i>>>3],7-(i&7)),i++)}}m(i==t.length*8)}applyMask(t){if(t<0||t>7)throw new RangeError("Mask value out of range");for(let i=0;i<this.size;i++)for(let e=0;e<this.size;e++){let r;switch(t){case 0:r=(e+i)%2==0;break;case 1:r=i%2==0;break;case 2:r=e%3==0;break;case 3:r=(e+i)%3==0;break;case 4:r=(Math.floor(e/3)+Math.floor(i/2))%2==0;break;case 5:r=e*i%2+e*i%3==0;break;case 6:r=(e*i%2+e*i%3)%2==0;break;case 7:r=((e+i)%2+e*i%3)%2==0;break;default:throw new Error("Unreachable")}!this.isFunction[i][e]&&r&&(this.modules[i][e]=!this.modules[i][e])}}getPenaltyScore(){let t=0;for(let n=0;n<this.size;n++){let l=!1,a=0,p=[0,0,0,0,0,0,0];for(let u=0;u<this.size;u++)this.modules[n][u]==l?(a++,a==5?t+=o.PENALTY_N1:a>5&&t++):(this.finderPenaltyAddHistory(a,p),l||(t+=this.finderPenaltyCountPatterns(p)*o.PENALTY_N3),l=this.modules[n][u],a=1);t+=this.finderPenaltyTerminateAndCount(l,a,p)*o.PENALTY_N3}for(let n=0;n<this.size;n++){let l=!1,a=0,p=[0,0,0,0,0,0,0];for(let u=0;u<this.size;u++)this.modules[u][n]==l?(a++,a==5?t+=o.PENALTY_N1:a>5&&t++):(this.finderPenaltyAddHistory(a,p),l||(t+=this.finderPenaltyCountPatterns(p)*o.PENALTY_N3),l=this.modules[u][n],a=1);t+=this.finderPenaltyTerminateAndCount(l,a,p)*o.PENALTY_N3}for(let n=0;n<this.size-1;n++)for(let l=0;l<this.size-1;l++){let a=this.modules[n][l];a==this.modules[n][l+1]&&a==this.modules[n+1][l]&&a==this.modules[n+1][l+1]&&(t+=o.PENALTY_N2)}let i=0;for(let n of this.modules)i=n.reduce((l,a)=>l+(a?1:0),i);let e=this.size*this.size,r=Math.ceil(Math.abs(i*20-e*10)/e)-1;return m(0<=r&&r<=9),t+=r*o.PENALTY_N4,m(0<=t&&t<=2568888),t}getAlignmentPatternPositions(){if(this.version==1)return[];{let t=Math.floor(this.version/7)+2,i=this.version==32?26:Math.ceil((this.version*4+4)/(t*2-2))*2,e=[6];for(let r=this.size-7;e.length<t;r-=i)e.splice(1,0,r);return e}}static getNumRawDataModules(t){if(t<o.MIN_VERSION||t>o.MAX_VERSION)throw new RangeError("Version number out of range");let i=(16*t+128)*t+64;if(t>=2){let e=Math.floor(t/7)+2;i-=(25*e-10)*e-55,t>=7&&(i-=36)}return m(208<=i&&i<=29648),i}static getNumDataCodewords(t,i){return Math.floor(o.getNumRawDataModules(t)/8)-o.ECC_CODEWORDS_PER_BLOCK[i.ordinal][t]*o.NUM_ERROR_CORRECTION_BLOCKS[i.ordinal][t]}static reedSolomonComputeDivisor(t){if(t<1||t>255)throw new RangeError("Degree out of range");let i=[];for(let r=0;r<t-1;r++)i.push(0);i.push(1);let e=1;for(let r=0;r<t;r++){for(let n=0;n<i.length;n++)i[n]=o.reedSolomonMultiply(i[n],e),n+1<i.length&&(i[n]^=i[n+1]);e=o.reedSolomonMultiply(e,2)}return i}static reedSolomonComputeRemainder(t,i){let e=i.map(r=>0);for(let r of t){let n=r^e.shift();e.push(0),i.forEach((l,a)=>e[a]^=o.reedSolomonMultiply(l,n))}return e}static reedSolomonMultiply(t,i){if(t>>>8||i>>>8)throw new RangeError("Byte out of range");let e=0;for(let r=7;r>=0;r--)e=e<<1^(e>>>7)*285,e^=(i>>>r&1)*t;return m(e>>>8==0),e}finderPenaltyCountPatterns(t){let i=t[1];m(i<=this.size*3);let e=i>0&&t[2]==i&&t[3]==i*3&&t[4]==i&&t[5]==i;return(e&&t[0]>=i*4&&t[6]>=i?1:0)+(e&&t[6]>=i*4&&t[0]>=i?1:0)}finderPenaltyTerminateAndCount(t,i,e){return t&&(this.finderPenaltyAddHistory(i,e),i=0),i+=this.size,this.finderPenaltyAddHistory(i,e),this.finderPenaltyCountPatterns(e)}finderPenaltyAddHistory(t,i){i[0]==0&&(t+=this.size),i.pop(),i.unshift(t)}};y(o,"MIN_VERSION",1),y(o,"MAX_VERSION",40),y(o,"PENALTY_N1",3),y(o,"PENALTY_N2",3),y(o,"PENALTY_N3",40),y(o,"PENALTY_N4",10),y(o,"ECC_CODEWORDS_PER_BLOCK",[[-1,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],[-1,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],[-1,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],[-1,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]]),y(o,"NUM_ERROR_CORRECTION_BLOCKS",[[-1,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],[-1,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],[-1,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],[-1,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]]);let d=o;w.QrCode=o;function s(C,t,i){if(t<0||t>31||C>>>t)throw new RangeError("Value out of range");for(let e=t-1;e>=0;e--)i.push(C>>>e&1)}function h(C,t){return(C>>>t&1)!=0}function m(C){if(!C)throw new Error("Assertion error")}let f=class f{constructor(t,i,e){this.mode=t;this.numChars=i;this.bitData=e;if(i<0)throw new RangeError("Invalid argument");this.bitData=e.slice()}static makeBytes(t){let i=[];for(let e of t)s(e,8,i);return new f(f.Mode.BYTE,t.length,i)}static makeNumeric(t){if(!f.isNumeric(t))throw new RangeError("String contains non-numeric characters");let i=[];for(let e=0;e<t.length;){let r=Math.min(t.length-e,3);s(parseInt(t.substring(e,e+r),10),r*3+1,i),e+=r}return new f(f.Mode.NUMERIC,t.length,i)}static makeAlphanumeric(t){if(!f.isAlphanumeric(t))throw new RangeError("String contains unencodable characters in alphanumeric mode");let i=[],e;for(e=0;e+2<=t.length;e+=2){let r=f.ALPHANUMERIC_CHARSET.indexOf(t.charAt(e))*45;r+=f.ALPHANUMERIC_CHARSET.indexOf(t.charAt(e+1)),s(r,11,i)}return e<t.length&&s(f.ALPHANUMERIC_CHARSET.indexOf(t.charAt(e)),6,i),new f(f.Mode.ALPHANUMERIC,t.length,i)}static makeSegments(t){return t==""?[]:f.isNumeric(t)?[f.makeNumeric(t)]:f.isAlphanumeric(t)?[f.makeAlphanumeric(t)]:[f.makeBytes(f.toUtf8ByteArray(t))]}static makeEci(t){let i=[];if(t<0)throw new RangeError("ECI assignment value out of range");if(t<128)s(t,8,i);else if(t<16384)s(2,2,i),s(t,14,i);else if(t<1e6)s(6,3,i),s(t,21,i);else throw new RangeError("ECI assignment value out of range");return new f(f.Mode.ECI,0,i)}static isNumeric(t){return f.NUMERIC_REGEX.test(t)}static isAlphanumeric(t){return f.ALPHANUMERIC_REGEX.test(t)}getData(){return this.bitData.slice()}static getTotalBits(t,i){let e=0;for(let r of t){let n=r.mode.numCharCountBits(i);if(r.numChars>=1<<n)return 1/0;e+=4+n+r.bitData.length}return e}static toUtf8ByteArray(t){t=encodeURI(t);let i=[];for(let e=0;e<t.length;e++)t.charAt(e)!="%"?i.push(t.charCodeAt(e)):(i.push(parseInt(t.substring(e+1,e+3),16)),e+=2);return i}};y(f,"NUMERIC_REGEX",/^[0-9]*$/),y(f,"ALPHANUMERIC_REGEX",/^[A-Z0-9 $%*+.\/:-]*$/),y(f,"ALPHANUMERIC_CHARSET","0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:");let c=f;w.QrSegment=f})(g||(g={}));(s=>{let d;(m=>{let c=class c{constructor(o,f){this.ordinal=o;this.formatBits=f}};y(c,"LOW",new c(0,1)),y(c,"MEDIUM",new c(1,0)),y(c,"QUARTILE",new c(2,3)),y(c,"HIGH",new c(3,2));let h=c;m.Ecc=c})(d=s.QrCode||(s.QrCode={}))})(g||(g={}));(s=>{let d;(m=>{let c=class c{constructor(o,f){this.modeBits=o;this.numBitsCharCount=f}numCharCountBits(o){return this.numBitsCharCount[Math.floor((o+7)/17)]}};y(c,"NUMERIC",new c(1,[10,12,14])),y(c,"ALPHANUMERIC",new c(2,[9,11,13])),y(c,"BYTE",new c(4,[8,16,16])),y(c,"KANJI",new c(8,[8,10,12])),y(c,"ECI",new c(7,[0,0,0]));let h=c;m.Mode=c})(d=s.QrSegment||(s.QrSegment={}))})(g||(g={}));var N=g;function L(d,s,h,m){if(s<0)throw new RangeError("Border must be non-negative");let c=[];for(let w=0;w<d.size;w++)for(let o=0;o<d.size;o++)d.getModule(o,w)&&c.push(`M${o+s},${w+s}h1v1h-1z`);return`<svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 ${d.size+s*2} ${d.size+s*2}" stroke="none">
<rect width="100%" height="100%" fill="${h}"/>
<path d="${c.join(" ")}" fill="${m}"/>
</svg>
`}function D(d,s){let h=N.QrCode.encodeText(s,N.QrCode.Ecc.MEDIUM);d.innerHTML=L(h,2,"white","black")}return F(U);})();