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