lean-qr
Version:
minimal QR code generation
2 lines (1 loc) • 6.91 kB
JavaScript
const t=[.2,3/8,5/9,2/3],o=(o,e)=>r=>{const n=4*o+r-4,s="*-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,f=n>8?s:1,c=e/f|0,i=e%f,l=f-i,a=n>8?c*t[r]+(o>5)&-2:s,_=c-a;return{t:l*_+i*_+i,o:[[l,_],[i,_+1]],i:a}},e={min:0,L:0,M:1,Q:2,H:3,max:3},r=t=>new Uint8Array(t),n=t=>{const o=new Error(`lean-qr error ${t}`);throw o.code=t,o},s=t=>"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:".indexOf(t),f=t=>t.charCodeAt(0),c=(...t)=>(o,e)=>t.forEach((t=>t(o,e))),i=t=>o=>{o.eci!==t&&(o.push(7,4),o.push(t,8),o.eci=t)},l=t=>(o,e)=>{o.push(4,4),o.push(t.length,8+8*(e>9)),t.forEach((t=>o.push(t,8)))},a=(t,o,e,r,n=(t,o)=>e(t.length,o),s=(r?o=>c(i(r),t(o)):t))=>(s.test=o,s.l=e,s.est=n,s.eci=r&&[r],s),_=a((t=>(o,e)=>{o.push(1,4),o.push(t.length,10+2*(e>26)+2*(e>9));let r=0;for(;r<t.length-2;r+=3)o.push(+t.slice(r,r+3),10);r<t.length-1?o.push(+t.slice(r),7):r<t.length&&o.push(+t[r],4)}),(t=>/[0-9]/.test(t)),((t,o)=>14+2*(o>26)+2*(o>9)+10*t/3)),u=a((t=>(o,e)=>{o.push(2,4),o.push(t.length,9+2*(e>26)+2*(e>9));let r=0;for(;r<t.length-1;r+=2)o.push(45*s(t[r])+s(t[r+1]),11);r<t.length&&o.push(s(t[r]),6)}),(t=>s(t)>=0),((t,o)=>13+2*(o>26)+2*(o>9)+5.5*t)),d=a((t=>l([...t].map(f))),(t=>f(t)<128),((t,o)=>12+8*(o>9)+8*t));d._=!0,d.u=!0;const p=a(d,(t=>f(t)<256),d.l,3);p._=!0;const m=new TextEncoder,h=a((t=>l(m.encode(t))),(()=>1),0,26,((t,o)=>12+8*(o>9)+8*m.encode(t).length));h._=!0;let w=()=>{const t=new Map,o=new TextDecoder("sjis"),e=r(2);for(let r=0;r<7973;++r)e[0]=r/192+129+64*(r>5951),e[1]=r%192+64,t.set(o.decode(e),r);return t.delete("\ufffd"),w=()=>t,t};const y=a((t=>(o,e)=>{o.push(8,4),o.push(t.length,8+2*(e>26)+2*(e>9));for(const e of t)o.push(w().get(e),13)}),(t=>w().has(t)),((t,o)=>12+2*(o>26)+2*(o>9)+13*t));y._=!0;const g=[_,u,d,p,y,h],b={auto:(t,{modes:o=g}={})=>(e,r)=>{const s=o.map(((o,e)=>{const n=new Map,s=(t,o)=>(n.has(t)||n.set(t,o(t,r)),n.get(t));return{m:o,h:1<<e,C:o.est("",r),S:o.l?(t,e)=>s(e-t,o.l):(e,r)=>s(t.slice(e,r),o.est)}}));let f=[{v:0}],c=0,i=0,l=-1;for(const o of[...t,""]){let t=0;if(o)for(const e of s)e.m.test(o)&&(t|=e.h);if(!o||t!==l){if(-1!==l){const t=new Set(f.map((t=>t.D))),o=[];for(const{m:e,C:r,S:n,h:a}of s)if(l&a){const s=n(c,i);for(const l of e.eci??t)if(!e.u||!l){let t;for(const o of f)if(o.D===l||e.eci){const f=o.m===e&&o.D===l,a=f?o.V:o,_=e._&&f?o.v+s-r:a.v+12*(a.D!==l)+(f?n(f?o.$:c,i):s);(!t||_<t.v)&&(t={$:f?o.$:c,V:a,m:e,D:l,A:i,v:_})}t&&o.push(t)}}f=o}l=t,c=i}i+=o.length}f.length||n(5);const a=[];for(let o=f.reduce(((t,o)=>o.v<t.v?o:t));o.m;o=o.V)a.push(o.m(t.slice(o.$,o.A)));a.reverse().forEach((t=>t(e,r)))},multi:c,eci:i,bytes:l,numeric:_,alphaNumeric:u,ascii:d,iso8859_1:p,shift_jis:y,utf8:h},C=()=>({F:r(2956),I:0,push(t,o){for(let e=o,r=8-(7&this.I);e>0;e-=r,r=8)this.F[this.I>>3]|=t<<r>>e,this.I+=e<r?e:r}}),x=(t,o=t*t,e=r(o))=>({size:t,K:e,get:(o,r)=>o>=0&&o<t&&!!(1&e[r*t+o]),toString({on:o="##",off:e=" ",lf:r="\n",padX:n=4,padY:s=4}={}){let f="";for(let c=-s;c<t+s;++c){for(let r=-n;r<t+n;++r)f+=this.get(r,c)?o:e;f+=r}return f},toImageData(o,{on:e=[0,0,0],off:r=[0,0,0,0],padX:n=4,padY:s=4}={}){const f=t+2*n,c=t+2*s,i=o.createImageData(f,c),l=new Uint32Array(i.data.buffer);i.data.set([...e,255]);const a=l[0];i.data.set([...r,255]);const _=l[0];for(let t=0;t<c;++t)for(let o=0;o<f;++o)l[t*f+o]=this.get(o-n,t-s)?a:_;return i},toCanvas(t,o){const e=t.getContext("2d"),r=this.toImageData(e,o);t.width=r.width,t.height=r.height,e.putImageData(r,0,0)},toDataURL({type:t="image/png",scale:o=1,...e}={}){const r=document.createElement("canvas"),n=r.getContext("2d"),s=this.toImageData(n,e);return r.width=s.width*o,r.height=s.height*o,n.putImageData(s,0,0),n.imageSmoothingEnabled=!1,n.globalCompositeOperation="copy",n.drawImage(r,0,0,s.width,s.height,0,0,r.width,r.height),r.toDataURL(t,1)}}),z=[(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&1)+t*o%3&1),(t,o)=>!((1&(t^o))+t*o%3&1)],E=r(511);for(let t=0,o=1;t<255;o=2*o^285*(o>127))E[E[o+255]=t++]=o;const M=t=>E[t%255],S=t=>E[t+255],v=(t,o)=>{const e=r(t.length+o.length-1);for(let r=0;r<t.length;++r)for(let n=0;n<o.length;++n)e[r+n]^=M(t[r]+o[n]);return e.map(S)},D=(t,o)=>{const e=r(t.length+o.length-1);e.set(t,0);for(let r=0;r<t.length;++r)if(e[r]){const t=S(e[r]);for(let n=0;n<o.length;++n)e[r+n]^=M(o[n]+t)}return e.slice(t.length)},L=[[0],[0,0]];for(let t=1;t<30;++t)L.push(v(L[t],[0,t]));const V=(t,o)=>{const e=[[],[]];let n=0,s=0;for(const[r,f]of o.o)for(let c=0;c<r;++c,n+=f){const r=t.slice(n,n+f);e[0].push(r),e[1].push(D(r,L[o.i])),s+=f+o.i}const f=r(s);s=0;for(const t of e)for(let o,e=0;s!==o;++e){o=s;for(const o of t)e<o.length&&(f[s++]=o[e])}return f},$=(t,o,e)=>{let r=t<<=e;for(let t=134217728;t>>=1;)r&t&&(r^=o*(t>>e));return r|t},A=({size:t,K:o},e)=>{const r=(e,r,n,s)=>{for(;n-- >0;e+=t)o.fill(s,e,e+r)},n=(o,e,n)=>{for(let s=0;s++<3;n-=2)r(e*t+o-(n>>1)*(t+1),n,n,2|s)},s=2*((t-13)/(1+(e/7|0))/2+.75|0);if(e>1)for(let o=t-7;o>8;o-=s){for(let t=o;t>8;t-=s)n(o,t,5);o<t-7&&n(o,6,5)}if(e>6)for(let r=$(e,7973,12),n=1;n<7;++n)for(let e=12;e-- >9;r>>=1)o[n*t-e]=2|1&r;r(7,2,9,2),r(t-8,8,9,2);for(let e=0;e<t;++e)o[6*t+e]=3^1&e;n(3,3,7),n(t-4,3,7);for(let e=0;e<t;++e)for(let r=e;r<t;++r)o[r*t+e]=o[e*t+r];o[(t-8)*t+8]=3},F=({size:t,K:o})=>{const e=[];for(let r=t-2,n=t,s=-1;r>=0;r-=2){for(5===r&&(r=4);n+=s,-1!==n&&n!==t;){const s=n*t+r;o[s+1]||e.push(s+1),o[s]||e.push(s)}s*=-1}return e},H=({K:t},o,e)=>o.forEach(((o,r)=>t[o]=e[r>>3]>>(7&~r)&1)),I=({size:t,K:o},e,r,n)=>{for(let r=0;r<t;++r)for(let n=0;n<t;++n){const s=r*t+n;o[s]^=e(n,r)&(o[s]>>1^1)}let s=21522^$((1^n)<<3|r,1335,10);for(let e=0;e++<8;s>>=1)o[(e-(e<7))*t+8]=1&s,o[9*t-e]=1&s;for(let e=8;--e,s;s>>=1)o[8*t+e-(e<7)]=1&s,o[(t-e)*t+8]=1&s},K=({size:t,K:o},e=0,r=0)=>{for(let n=0;n<t;++n){for(let s=0;s<2;++s)for(let f,c=0,i=0,l=0;c<t;++c){const a=1&o[s?n*t+c:c*t+n];r+=a,i=(i>>1|2098176)&(3047517^a-1),2049&i&&(e+=40),a!==f&&(l=0),f=a,e+=5===++l?3:l>5}if(n)for(let r=t+n,s=5*o[n-1]^o[n];r<t*t;r+=t){const t=5*o[r-1]^o[r];e+=3*!(1&(s|t)|4&(s^t)),s=t}}return e+10*(10*Math.abs(r/(t*t)-1)|0)},N=[],P=(t=n(1),{minCorrectionLevel:e=0,maxCorrectionLevel:r=3,minVersion:s=1,maxVersion:f=40,mask:c,trailer:i=60433,...l}={})=>{r<e&&n(3),f<s&&n(2),"string"==typeof t&&(t=b.auto(t,l));for(let n=s,l=0;n<=f;++n){let s=N[n];s||(N[n]=s=x(4*n+17),A(s,n),s.p=F(s));const f=o(n,s.p.length>>3);if(8*f(e).t<l)continue;const a=C();t(a,n),l=a.I;for(let t=r;t>=e;--t){const o=f(t);if(8*o.t<l)continue;for(a.I=l+11&-8;a.I<8*o.t;)a.push(i,16);const e=x(s.size,s.K);return H(e,s.p,V(a.F,o)),(z[c??-1]?[z[c]]:z).map(((o,r)=>{const n=x(e.size,e.K);return I(n,o,c??r,t),n.s=K(n),n})).reduce(((t,o)=>o.s<t.s?o:t))}}n(4)};P.with=(...t)=>(o,e)=>P(o,{modes:[...g,...t],...e});export{e as correction,P as generate,b as mode};