UNPKG

lean-qr

Version:
2 lines (1 loc) 3.76 kB
const t=[.2,3/8,5/9,2/3],o=(o,r)=>e=>{const n=4*o+e-4,f="*-04-39?2$%%$%%'$%''%'''%')(%'))%(++'(++'(+.'+-.',/3',33)-/5)-43).36)058*18<+37<+4:<,4:E,5<A-7>C/8@F/:EH/<EK0=FM1?IP2@KS3BNV4DPY5FS\\6HV_6IXb7K[e8N^i9Pam;Rdp<Tgt".charCodeAt(n)-35,s=n>8?f:1,l=r/s|0,c=r%s,a=s-c,i=n>8?l*t[e]+(o>5)&-2:f,d=l-i;return{t:a*d+c*d+c,o:[[a,d],[c,d+1]],l:i}},r={L:0,M:1,Q:2,H:3},e=t=>new Uint8Array(t),n=t=>{const o=new Error(`lean-qr error ${t}`);throw o.code=t,o},f=(t,o=t*t,r=e(o))=>({size:t,i:r,get:(o,e)=>o>=0&&o<t&&!!(1&r[e*t+o]),u(o,{on:r=[0,0,0],off:e=[0,0,0,0],pad:n=4,padX:f=n,padY:s=n}={}){const l=t+2*f,c=t+2*s,a=o.createImageData(l,c),i=new Uint32Array(a.data.buffer);a.data.set([...r,255]);const d=i[0];a.data.set([...e,255]);const u=i[0];for(let t=0;t<c;++t)for(let o=0;o<l;++o)i[t*l+o]=this.get(o-f,t-s)?d:u;return a},toCanvas(t,o){const r=t.getContext("2d"),e=this.u(r,o);t.width=e.width,t.height=e.height,r.putImageData(e,0,0)}}),s=[(t,o)=>1&(t^o),(t,o)=>1&o,t=>t%3,(t,o)=>(t+o)%3,(t,o)=>1&(t/3^o>>1),(t,o)=>(t&o&1)+t*o%3,(t,o)=>(t&o)+t*o%3&1,(t,o)=>(t^o)+t*o%3&1],l=e(511);for(let t=0,o=1;t<255;o=2*o^285*(o>127))l[l[o+255]=t++]=o;const c=t=>l[t%255],a=t=>l[t+255],i=(t,o)=>{const r=e(t.length+o.length-1);for(let e=0;e<t.length;++e)for(let n=0;n<o.length;++n)r[e+n]^=c(t[e]+o[n]);return r.map(a)},d=(t,o)=>{const r=e(t.length+o.length-1);r.set(t,0);for(let e=0;e<t.length;++e)if(r[e]){const t=a(r[e]);for(let n=0;n<o.length;++n)r[e+n]^=c(o[n]+t)}return r.slice(t.length)},u=[[0],[0,0]];for(let t=1;t<30;++t)u.push(i(u[t],[0,t]));const _=(t,o)=>{const r=[[],[]];let n=0,f=0;for(const[e,s]of o.o)for(let l=0;l<e;++l,n+=s){const e=t.slice(n,n+s);r[0].push(e),r[1].push(d(e,u[o.l])),f+=s+o.l}const s=e(f);f=0;for(const t of r)for(let o,r=0;f!==o;++r){o=f;for(const o of t)r<o.length&&(s[f++]=o[r])}return s},p=(t,o,r)=>{let e=t<<=r;for(let t=134217728;t>>=1;)e&t&&(e^=o*(t>>r));return e|t},z=({size:t,i:o},r)=>{const e=(r,e,n,f)=>{for(;n-- >0;r+=t)o.fill(f,r,r+e)},n=(o,r,n)=>{for(let f=0;f++<3;n-=2)e(r*t+o-(n>>1)*(t+1),n,n,2|f)},f=2*((t-13)/(1+(r/7|0))/2+.75|0);if(r>1)for(let o=t-7;o>8;o-=f){for(let t=o;t>8;t-=f)n(o,t,5);o<t-7&&n(o,6,5)}if(r>6)for(let e=p(r,7973,12),n=1;n<7;++n)for(let r=12;r-- >9;e>>=1)o[n*t-r]=2|1&e;e(7,2,9,2),e(t-8,8,9,2);for(let r=0;r<t;++r)o[6*t+r]=3^1&r;n(3,3,7),n(t-4,3,7);for(let r=0;r<t;++r)for(let e=r;e<t;++e)o[e*t+r]=o[r*t+e];o[(t-8)*t+8]=3},g=({size:t,i:o})=>{const r=[];for(let e=t-2,n=t,f=-1;e>=0;e-=2){for(5===e&&(e=4);n+=f,-1!==n&&n!==t;){const f=n*t+e;o[f+1]||r.push(f+1),o[f]||r.push(f)}f*=-1}return r},w=({i:t},o,r)=>o.forEach((o,e)=>t[o]=r[e>>3]>>(7&~e)&1),y=({size:t,i:o},r,e,n)=>{for(let e=0;e<t;++e)for(let n=0;n<t;++n){const f=e*t+n;o[f]^=!(r(n,e)||2&o[f])}let f=21522^p((1^n)<<3|e,1335,10);for(let r=0;r++<8;f>>=1)o[(r-(r<7))*t+8]=1&f,o[9*t-r]=1&f;for(let r=8;--r,f;f>>=1)o[8*t+r-(r<7)]=1&f,o[(t-r)*t+8]=1&f},E=({size:t,i:o},r=0,e=0)=>{for(let n=0;n<t;++n){for(let f=0;f<2;++f)for(let s,l=0,c=0,a=0;l<t;++l){const i=1&o[f?n*t+l:l*t+n];e+=i,c=(c>>1|2098176)&(3047517^i-1),2049&c&&(r+=40),i!==s&&(a=0),s=i,r+=5===++a?3:a>5}if(n)for(let e=t+n,f=5*o[n-1]^o[n];e<t*t;e+=t){const t=5*o[e-1]^o[e];r+=3*!(1&(f|t)|4&(f^t)),f=t}}return r+10*(10*Math.abs(e/(t*t)-1)|0)},h=[],m=(t=n(1),{minCorrectionLevel:r=0,minVersion:l=1}={})=>{"string"!=typeof t&&n(5),t=(new TextEncoder).encode(t);const c=e(2957);c.set([113,164,t.length>>8]);for(let e=l;e<41;++e){let n=h[e];n||(h[e]=n=f(4*e+17),z(n,e),n.p=g(n));const l=o(e,n.p.length>>3)(r);if(l.t>=3+(e>9)+t.length){let o=e>9?3:2;for(c[o++]=t.length,c.set(t,o),o+=t.length-1;o<2954;c.set([236,17],o+=2));const a=f(n.size,n.i);return w(a,n.p,_(c,l)),s.map((t,o)=>{const e=f(a.size,a.i);return y(e,t,o,r),e.s=E(e),e}).sort((t,o)=>t.s-o.s)[0]}}n(4)};export{r as correction,m as generate};