UNPKG

ll-package

Version:

6 lines (5 loc) 12.1 kB
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});/** * @license QR Code generator library (TypeScript) * Copyright (c) Project Nayuki. * SPDX-License-Identifier: MIT */var g;(function(w){class o{static encodeText(t,s){const e=w.QrSegment.makeSegments(t);return o.encodeSegments(e,s)}static encodeBinary(t,s){const e=w.QrSegment.makeBytes(t);return o.encodeSegments([e],s)}static encodeSegments(t,s){let e=arguments.length>2&&arguments[2]!==void 0?arguments[2]:1,n=arguments.length>3&&arguments[3]!==void 0?arguments[3]:40,i=arguments.length>4&&arguments[4]!==void 0?arguments[4]:-1,a=arguments.length>5&&arguments[5]!==void 0?arguments[5]:!0;if(!(o.MIN_VERSION<=e&&e<=n&&n<=o.MAX_VERSION)||i<-1||i>7)throw new RangeError("Invalid value");let r,c;for(r=e;;r++){const f=o.getNumDataCodewords(r,s)*8,m=h.getTotalBits(t,r);if(m<=f){c=m;break}if(r>=n)throw new RangeError("Data too long")}for(const f of[o.Ecc.MEDIUM,o.Ecc.QUARTILE,o.Ecc.HIGH])a&&c<=o.getNumDataCodewords(r,f)*8&&(s=f);const u=[];for(const f of t){l(f.mode.modeBits,4,u),l(f.numChars,f.mode.numCharCountBits(r),u);for(const m of f.getData())u.push(m)}d(u.length==c);const N=o.getNumDataCodewords(r,s)*8;d(u.length<=N),l(0,Math.min(4,N-u.length),u),l(0,(8-u.length%8)%8,u),d(u.length%8==0);for(let f=236;u.length<N;f^=253)l(f,8,u);const M=[];for(;M.length*8<u.length;)M.push(0);return u.forEach((f,m)=>M[m>>>3]|=f<<7-(m&7)),new o(r,s,M,i)}constructor(t,s,e,n){if(this.version=t,this.errorCorrectionLevel=s,this.modules=[],this.isFunction=[],t<o.MIN_VERSION||t>o.MAX_VERSION)throw new RangeError("Version value out of range");if(n<-1||n>7)throw new RangeError("Mask value out of range");this.size=t*4+17;const i=[];for(let r=0;r<this.size;r++)i.push(!1);for(let r=0;r<this.size;r++)this.modules.push(i.slice()),this.isFunction.push(i.slice());this.drawFunctionPatterns();const a=this.addEccAndInterleave(e);if(this.drawCodewords(a),n==-1){let r=1e9;for(let c=0;c<8;c++){this.applyMask(c),this.drawFormatBits(c);const u=this.getPenaltyScore();u<r&&(n=c,r=u),this.applyMask(c)}}d(0<=n&&n<=7),this.mask=n,this.applyMask(n),this.drawFormatBits(n),this.isFunction=[]}getModule(t,s){return 0<=t&&t<this.size&&0<=s&&s<this.size&&this.modules[s][t]}getModules(){return this.modules}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);const t=this.getAlignmentPatternPositions(),s=t.length;for(let e=0;e<s;e++)for(let n=0;n<s;n++)e==0&&n==0||e==0&&n==s-1||e==s-1&&n==0||this.drawAlignmentPattern(t[e],t[n]);this.drawFormatBits(0),this.drawVersion()}drawFormatBits(t){const s=this.errorCorrectionLevel.formatBits<<3|t;let e=s;for(let i=0;i<10;i++)e=e<<1^(e>>>9)*1335;const n=(s<<10|e)^21522;d(n>>>15==0);for(let i=0;i<=5;i++)this.setFunctionModule(8,i,E(n,i));this.setFunctionModule(8,7,E(n,6)),this.setFunctionModule(8,8,E(n,7)),this.setFunctionModule(7,8,E(n,8));for(let i=9;i<15;i++)this.setFunctionModule(14-i,8,E(n,i));for(let i=0;i<8;i++)this.setFunctionModule(this.size-1-i,8,E(n,i));for(let i=8;i<15;i++)this.setFunctionModule(8,this.size-15+i,E(n,i));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;const s=this.version<<12|t;d(s>>>18==0);for(let e=0;e<18;e++){const n=E(s,e),i=this.size-11+e%3,a=Math.floor(e/3);this.setFunctionModule(i,a,n),this.setFunctionModule(a,i,n)}}drawFinderPattern(t,s){for(let e=-4;e<=4;e++)for(let n=-4;n<=4;n++){const i=Math.max(Math.abs(n),Math.abs(e)),a=t+n,r=s+e;0<=a&&a<this.size&&0<=r&&r<this.size&&this.setFunctionModule(a,r,i!=2&&i!=4)}}drawAlignmentPattern(t,s){for(let e=-2;e<=2;e++)for(let n=-2;n<=2;n++)this.setFunctionModule(t+n,s+e,Math.max(Math.abs(n),Math.abs(e))!=1)}setFunctionModule(t,s,e){this.modules[s][t]=e,this.isFunction[s][t]=!0}addEccAndInterleave(t){const s=this.version,e=this.errorCorrectionLevel;if(t.length!=o.getNumDataCodewords(s,e))throw new RangeError("Invalid argument");const n=o.NUM_ERROR_CORRECTION_BLOCKS[e.ordinal][s],i=o.ECC_CODEWORDS_PER_BLOCK[e.ordinal][s],a=Math.floor(o.getNumRawDataModules(s)/8),r=n-a%n,c=Math.floor(a/n),u=[],N=o.reedSolomonComputeDivisor(i);for(let f=0,m=0;f<n;f++){const R=t.slice(m,m+c-i+(f<r?0:1));m+=R.length;const P=o.reedSolomonComputeRemainder(R,N);f<r&&R.push(0),u.push(R.concat(P))}const M=[];for(let f=0;f<u[0].length;f++)u.forEach((m,R)=>{(f!=c-i||R>=r)&&M.push(m[f])});return d(M.length==a),M}drawCodewords(t){if(t.length!=Math.floor(o.getNumRawDataModules(this.version)/8))throw new RangeError("Invalid argument");let s=0;for(let e=this.size-1;e>=1;e-=2){e==6&&(e=5);for(let n=0;n<this.size;n++)for(let i=0;i<2;i++){const a=e-i,c=(e+1&2)==0?this.size-1-n:n;!this.isFunction[c][a]&&s<t.length*8&&(this.modules[c][a]=E(t[s>>>3],7-(s&7)),s++)}}d(s==t.length*8)}applyMask(t){if(t<0||t>7)throw new RangeError("Mask value out of range");for(let s=0;s<this.size;s++)for(let e=0;e<this.size;e++){let n;switch(t){case 0:n=(e+s)%2==0;break;case 1:n=s%2==0;break;case 2:n=e%3==0;break;case 3:n=(e+s)%3==0;break;case 4:n=(Math.floor(e/3)+Math.floor(s/2))%2==0;break;case 5:n=e*s%2+e*s%3==0;break;case 6:n=(e*s%2+e*s%3)%2==0;break;case 7:n=((e+s)%2+e*s%3)%2==0;break;default:throw new Error("Unreachable")}!this.isFunction[s][e]&&n&&(this.modules[s][e]=!this.modules[s][e])}}getPenaltyScore(){let t=0;for(let i=0;i<this.size;i++){let a=!1,r=0;const c=[0,0,0,0,0,0,0];for(let u=0;u<this.size;u++)this.modules[i][u]==a?(r++,r==5?t+=o.PENALTY_N1:r>5&&t++):(this.finderPenaltyAddHistory(r,c),a||(t+=this.finderPenaltyCountPatterns(c)*o.PENALTY_N3),a=this.modules[i][u],r=1);t+=this.finderPenaltyTerminateAndCount(a,r,c)*o.PENALTY_N3}for(let i=0;i<this.size;i++){let a=!1,r=0;const c=[0,0,0,0,0,0,0];for(let u=0;u<this.size;u++)this.modules[u][i]==a?(r++,r==5?t+=o.PENALTY_N1:r>5&&t++):(this.finderPenaltyAddHistory(r,c),a||(t+=this.finderPenaltyCountPatterns(c)*o.PENALTY_N3),a=this.modules[u][i],r=1);t+=this.finderPenaltyTerminateAndCount(a,r,c)*o.PENALTY_N3}for(let i=0;i<this.size-1;i++)for(let a=0;a<this.size-1;a++){const r=this.modules[i][a];r==this.modules[i][a+1]&&r==this.modules[i+1][a]&&r==this.modules[i+1][a+1]&&(t+=o.PENALTY_N2)}let s=0;for(const i of this.modules)s=i.reduce((a,r)=>a+(r?1:0),s);const e=this.size*this.size,n=Math.ceil(Math.abs(s*20-e*10)/e)-1;return d(0<=n&&n<=9),t+=n*o.PENALTY_N4,d(0<=t&&t<=2568888),t}getAlignmentPatternPositions(){if(this.version==1)return[];{const t=Math.floor(this.version/7)+2,s=this.version==32?26:Math.ceil((this.version*4+4)/(t*2-2))*2,e=[6];for(let n=this.size-7;e.length<t;n-=s)e.splice(1,0,n);return e}}static getNumRawDataModules(t){if(t<o.MIN_VERSION||t>o.MAX_VERSION)throw new RangeError("Version number out of range");let s=(16*t+128)*t+64;if(t>=2){const e=Math.floor(t/7)+2;s-=(25*e-10)*e-55,t>=7&&(s-=36)}return d(208<=s&&s<=29648),s}static getNumDataCodewords(t,s){return Math.floor(o.getNumRawDataModules(t)/8)-o.ECC_CODEWORDS_PER_BLOCK[s.ordinal][t]*o.NUM_ERROR_CORRECTION_BLOCKS[s.ordinal][t]}static reedSolomonComputeDivisor(t){if(t<1||t>255)throw new RangeError("Degree out of range");const s=[];for(let n=0;n<t-1;n++)s.push(0);s.push(1);let e=1;for(let n=0;n<t;n++){for(let i=0;i<s.length;i++)s[i]=o.reedSolomonMultiply(s[i],e),i+1<s.length&&(s[i]^=s[i+1]);e=o.reedSolomonMultiply(e,2)}return s}static reedSolomonComputeRemainder(t,s){const e=s.map(n=>0);for(const n of t){const i=n^e.shift();e.push(0),s.forEach((a,r)=>e[r]^=o.reedSolomonMultiply(a,i))}return e}static reedSolomonMultiply(t,s){if(t>>>8||s>>>8)throw new RangeError("Byte out of range");let e=0;for(let n=7;n>=0;n--)e=e<<1^(e>>>7)*285,e^=(s>>>n&1)*t;return d(e>>>8==0),e}finderPenaltyCountPatterns(t){const s=t[1];d(s<=this.size*3);const e=s>0&&t[2]==s&&t[3]==s*3&&t[4]==s&&t[5]==s;return(e&&t[0]>=s*4&&t[6]>=s?1:0)+(e&&t[6]>=s*4&&t[0]>=s?1:0)}finderPenaltyTerminateAndCount(t,s,e){return t&&(this.finderPenaltyAddHistory(s,e),s=0),s+=this.size,this.finderPenaltyAddHistory(s,e),this.finderPenaltyCountPatterns(e)}finderPenaltyAddHistory(t,s){s[0]==0&&(t+=this.size),s.pop(),s.unshift(t)}}o.MIN_VERSION=1,o.MAX_VERSION=40,o.PENALTY_N1=3,o.PENALTY_N2=3,o.PENALTY_N3=40,o.PENALTY_N4=10,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]],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]],w.QrCode=o;function l(A,t,s){if(t<0||t>31||A>>>t)throw new RangeError("Value out of range");for(let e=t-1;e>=0;e--)s.push(A>>>e&1)}function E(A,t){return(A>>>t&1)!=0}function d(A){if(!A)throw new Error("Assertion error")}class h{static makeBytes(t){const s=[];for(const e of t)l(e,8,s);return new h(h.Mode.BYTE,t.length,s)}static makeNumeric(t){if(!h.isNumeric(t))throw new RangeError("String contains non-numeric characters");const s=[];for(let e=0;e<t.length;){const n=Math.min(t.length-e,3);l(parseInt(t.substring(e,e+n),10),n*3+1,s),e+=n}return new h(h.Mode.NUMERIC,t.length,s)}static makeAlphanumeric(t){if(!h.isAlphanumeric(t))throw new RangeError("String contains unencodable characters in alphanumeric mode");const s=[];let e;for(e=0;e+2<=t.length;e+=2){let n=h.ALPHANUMERIC_CHARSET.indexOf(t.charAt(e))*45;n+=h.ALPHANUMERIC_CHARSET.indexOf(t.charAt(e+1)),l(n,11,s)}return e<t.length&&l(h.ALPHANUMERIC_CHARSET.indexOf(t.charAt(e)),6,s),new h(h.Mode.ALPHANUMERIC,t.length,s)}static makeSegments(t){return t==""?[]:h.isNumeric(t)?[h.makeNumeric(t)]:h.isAlphanumeric(t)?[h.makeAlphanumeric(t)]:[h.makeBytes(h.toUtf8ByteArray(t))]}static makeEci(t){const s=[];if(t<0)throw new RangeError("ECI assignment value out of range");if(t<128)l(t,8,s);else if(t<16384)l(2,2,s),l(t,14,s);else if(t<1e6)l(6,3,s),l(t,21,s);else throw new RangeError("ECI assignment value out of range");return new h(h.Mode.ECI,0,s)}static isNumeric(t){return h.NUMERIC_REGEX.test(t)}static isAlphanumeric(t){return h.ALPHANUMERIC_REGEX.test(t)}constructor(t,s,e){if(this.mode=t,this.numChars=s,this.bitData=e,s<0)throw new RangeError("Invalid argument");this.bitData=e.slice()}getData(){return this.bitData.slice()}static getTotalBits(t,s){let e=0;for(const n of t){const i=n.mode.numCharCountBits(s);if(n.numChars>=1<<i)return 1/0;e+=4+i+n.bitData.length}return e}static toUtf8ByteArray(t){t=encodeURI(t);const s=[];for(let e=0;e<t.length;e++)t.charAt(e)!="%"?s.push(t.charCodeAt(e)):(s.push(parseInt(t.substring(e+1,e+3),16)),e+=2);return s}}h.NUMERIC_REGEX=/^[0-9]*$/,h.ALPHANUMERIC_REGEX=/^[A-Z0-9 $%*+.\/:-]*$/,h.ALPHANUMERIC_CHARSET="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:",w.QrSegment=h})(g||(g={}));(function(w){(function(o){class l{constructor(d,h){this.ordinal=d,this.formatBits=h}}l.LOW=new l(0,1),l.MEDIUM=new l(1,0),l.QUARTILE=new l(2,3),l.HIGH=new l(3,2),o.Ecc=l})(w.QrCode||(w.QrCode={}))})(g||(g={}));(function(w){(function(o){class l{constructor(d,h){this.modeBits=d,this.numBitsCharCount=h}numCharCountBits(d){return this.numBitsCharCount[Math.floor((d+7)/17)]}}l.NUMERIC=new l(1,[10,12,14]),l.ALPHANUMERIC=new l(2,[9,11,13]),l.BYTE=new l(4,[8,16,16]),l.KANJI=new l(8,[8,10,12]),l.ECI=new l(7,[0,0,0]),o.Mode=l})(w.QrSegment||(w.QrSegment={}))})(g||(g={}));const p=g;exports.default=p;