Comparing version 1.2.0 to 1.3.0
function parseArg(flag, arg) { | ||
switch (flag.type) { | ||
case 'string': return arg; | ||
case 'string': | ||
return arg; | ||
case 'enum': | ||
if (!flag.values.includes(arg)) { | ||
throw new Error(`Unknown value ${arg} for ${flag.name}; expected: ${flag.values.join(', ')}`); | ||
throw new Error( | ||
`Unknown value ${arg} for ${flag.name}; expected: ${flag.values.join( | ||
', ', | ||
)}`, | ||
); | ||
} | ||
@@ -60,3 +65,5 @@ return arg; | ||
const result = {}; | ||
flags.forEach(({ id, def }) => { result[id] = def; }); | ||
flags.forEach(({ id, def }) => { | ||
result[id] = def; | ||
}); | ||
for (; i < argv.length; ++i) { | ||
@@ -74,3 +81,3 @@ const arg = argv[i]; | ||
const keyName = arg.substr(2, p - 2); | ||
const flag = flags.find(({ name }) => (name === keyName)); | ||
const flag = flags.find(({ name }) => name === keyName); | ||
if (!flag) { | ||
@@ -82,3 +89,6 @@ throw new Error(`Unknown option ${keyName}`); | ||
} else { | ||
result[flag.id] = parseArg(flag, (p < arg.length) ? arg.substr(p) : argv[++i]); | ||
result[flag.id] = parseArg( | ||
flag, | ||
p < arg.length ? arg.substr(p) : argv[++i], | ||
); | ||
} | ||
@@ -88,3 +98,3 @@ } else if (arg.startsWith('-')) { | ||
const keyName = arg[p]; | ||
const flag = flags.find(({ short }) => (short === keyName)); | ||
const flag = flags.find(({ short }) => short === keyName); | ||
if (!flag) { | ||
@@ -99,3 +109,6 @@ throw new Error(`Unknown shorthand option ${keyName}`); | ||
} else { | ||
result[flag.id] = parseArg(flag, (p < arg.length - 1) ? arg.substr(p + 1) : argv[++i]); | ||
result[flag.id] = parseArg( | ||
flag, | ||
p < arg.length - 1 ? arg.substr(p + 1) : argv[++i], | ||
); | ||
break; | ||
@@ -102,0 +115,0 @@ } |
114
bin/cli.js
#!/usr/bin/env node | ||
const { mode, correction, generate } = require('../build/index.js'); | ||
const { toSvgSource } = require('../build/extras/svg.js'); | ||
const { printUsage, parseArgs } = require('./argparser.js'); | ||
@@ -28,16 +29,92 @@ | ||
/* eslint-disable object-curly-newline */ | ||
const FLAGS = [ | ||
{ id: 'encoding', name: 'encoding', short: 'e', type: 'enum', values: [...ENCODINGS.keys()], def: 'auto', info: 'Set the encoding type for the content' }, | ||
{ id: 'minCor', name: 'min-correction', short: 'c', type: 'enum', values: [...Object.keys(correction)], def: 'min', info: 'Set the minimum error correction level' }, | ||
{ id: 'maxCor', name: 'max-correction', short: 'C', type: 'enum', values: [...Object.keys(correction)], def: 'max', info: 'Set the maximum error correction level' }, | ||
{ id: 'minVer', name: 'min-version', short: 'v', type: 'int', min: 1, max: 40, def: 1, info: 'Set the minimum version (size)' }, | ||
{ id: 'maxVer', name: 'max-version', short: 'V', type: 'int', min: 1, max: 40, def: 40, info: 'Set the maximum version (size)' }, | ||
{ id: 'padding', name: 'padding', short: 'p', type: 'int', min: 0, def: 4, info: 'Set the edge padding size' }, | ||
{ id: 'mask', name: 'mask', short: 'm', type: 'enum', values: ['auto', '0', '1', '2', '3', '4', '5', '6', '7'], def: 'auto', info: 'Set the masking type (advanced usage)' }, | ||
{ id: 'format', name: 'format', short: 'f', type: 'enum', values: [...TEXT_FORMATS.keys()], def: 'text-ansi-invert', info: 'Set the output format' }, | ||
{ id: 'info', name: 'info', short: 'i', type: 'presence', info: 'Print meta information to stderr' }, | ||
{ id: 'help', name: 'help', short: '?', type: 'presence', info: 'Print documentation' }, | ||
{ | ||
id: 'encoding', | ||
name: 'encoding', | ||
short: 'e', | ||
type: 'enum', | ||
values: [...ENCODINGS.keys()], | ||
def: 'auto', | ||
info: 'Set the encoding type for the content', | ||
}, | ||
{ | ||
id: 'minCor', | ||
name: 'min-correction', | ||
short: 'c', | ||
type: 'enum', | ||
values: [...Object.keys(correction)], | ||
def: 'min', | ||
info: 'Set the minimum error correction level', | ||
}, | ||
{ | ||
id: 'maxCor', | ||
name: 'max-correction', | ||
short: 'C', | ||
type: 'enum', | ||
values: [...Object.keys(correction)], | ||
def: 'max', | ||
info: 'Set the maximum error correction level', | ||
}, | ||
{ | ||
id: 'minVer', | ||
name: 'min-version', | ||
short: 'v', | ||
type: 'int', | ||
min: 1, | ||
max: 40, | ||
def: 1, | ||
info: 'Set the minimum version (size)', | ||
}, | ||
{ | ||
id: 'maxVer', | ||
name: 'max-version', | ||
short: 'V', | ||
type: 'int', | ||
min: 1, | ||
max: 40, | ||
def: 40, | ||
info: 'Set the maximum version (size)', | ||
}, | ||
{ | ||
id: 'padding', | ||
name: 'padding', | ||
short: 'p', | ||
type: 'int', | ||
min: 0, | ||
def: 4, | ||
info: 'Set the edge padding size', | ||
}, | ||
{ | ||
id: 'mask', | ||
name: 'mask', | ||
short: 'm', | ||
type: 'enum', | ||
values: ['auto', '0', '1', '2', '3', '4', '5', '6', '7'], | ||
def: 'auto', | ||
info: 'Set the masking type (advanced usage)', | ||
}, | ||
{ | ||
id: 'format', | ||
name: 'format', | ||
short: 'f', | ||
type: 'enum', | ||
values: [...TEXT_FORMATS.keys(), 'svg'], | ||
def: 'text-ansi-invert', | ||
info: 'Set the output format', | ||
}, | ||
{ | ||
id: 'info', | ||
name: 'info', | ||
short: 'i', | ||
type: 'presence', | ||
info: 'Print meta information to stderr', | ||
}, | ||
{ | ||
id: 'help', | ||
name: 'help', | ||
short: '?', | ||
type: 'presence', | ||
info: 'Print documentation', | ||
}, | ||
]; | ||
/* eslint-enable object-curly-newline */ | ||
@@ -65,7 +142,16 @@ try { | ||
maxVersion: args.maxVer, | ||
mask: (args.mask === 'auto') ? null : Number(args.mask), | ||
mask: args.mask === 'auto' ? null : Number(args.mask), | ||
}); | ||
const tm1 = Date.now(); | ||
let tm2; | ||
if (TEXT_FORMATS.has(args.format)) { | ||
if (args.format === 'svg') { | ||
const result = toSvgSource(code, { | ||
on: 'black', | ||
off: 'white', | ||
padX: args.padding, | ||
padY: args.padding, | ||
}); | ||
tm2 = Date.now(); | ||
process.stdout.write(`<?xml version="1.0" encoding="UTF-8" ?>${result}\n`); | ||
} else if (TEXT_FORMATS.has(args.format)) { | ||
const result = code.toString({ | ||
@@ -72,0 +158,0 @@ ...TEXT_FORMATS.get(args.format), |
@@ -1,2 +0,2 @@ | ||
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define("lean-qr",[],e):"object"==typeof exports?exports["lean-qr"]=e():t["lean-qr"]=e()}(global,(function(){return function(t){var e={};function r(n){if(e[n])return e[n].exports;var s=e[n]={i:n,l:!1,exports:{}};return t[n].call(s.exports,s,s.exports,r),s.l=!0,s.exports}return r.m=t,r.c=e,r.d=function(t,e,n){r.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:n})},r.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},r.t=function(t,e){if(1&e&&(t=r(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var s in t)r.d(n,s,function(e){return t[e]}.bind(null,s));return n},r.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return r.d(e,"a",e),e},r.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},r.p="",r(r.s=0)}([function(t,e,r){t.exports=r(1)},function(t,e,r){"use strict";r.r(e),r.d(e,"correction",(function(){return o})),r.d(e,"mode",(function(){return g})),r.d(e,"generate",(function(){return L}));const n=t=>{const e=t>>18,r=t>>12&63,n=t>>5&127;return{capBits:8*(r*n+e*(n+1)),groups:e?[[r,n],[e,n+1]]:[[r,n]],ecsize:31&t}},s=[{id:1,v:[4711,5194,5871,6676,7578,10386,10708,11320,11934,534674,18996,535448,19834,278142,285430,285784,1318268,286494,1064508,1326460,1068700,1846780,1331006,1076926,1084762,568924,1085278,2637502,1867422,2645630,843390,73342,335486,1629822,1888062,3698494,1122142,4738910,1134270,1654494].map(n)},{id:0,v:[4618,5008,5530,9234,9592,17264,17394,533718,537750,279930,1054302,550038,296118,1328408,1332536,816572,304604,1086842,2897306,3421498,71002,71132,3687932,3696060,3442172,1127900,878012,6043068,1922492,2700796,7611868,6071772,5563868,6088156,6866428,8939004,3790300,8443356,2000380,8201724].map(n)},{id:3,v:[4525,4822,8754,8986,532978,17016,1057234,541270,1065492,549496,1065692,1589914,1082008,1356308,1856286,586360,3936988,332508,1118906,1372958,1643228,4223774,3715870,4240158,5796638,1688284,6849278,8143646,9704190,6615838,434974,9216798,5100318,2016030,3830558,2810654,2822942,3867422,5944094,9052958].map(n)},{id:2,v:[4401,4636,8630,16688,532854,16892,278970,541146,1065368,549372,2109848,1077724,1098102,1356184,1880472,3420670,4465116,4989404,4231610,2683388,1651230,139704,3736062,647710,3498494,1184286,7389694,8172030,6894078,6648318,7434750,9253374,12104190,504350,10838526,16785918,12157438,8561150,17605118,16073214].map(n)}],o={min:0,L:0,M:1,Q:2,H:3,max:3},i=t=>"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:".indexOf(t),l=(...t)=>(e,r)=>t.forEach(t=>t(e,r)),u=t=>(e,r)=>{e.push(1,4),e.push(t.length,r<10?10:r<27?12:14);let n=0;for(;n<t.length-2;n+=3)e.push(Number(t.substr(n,3)),10);n<t.length-1?e.push(Number(t.substr(n,2)),7):n<t.length&&e.push(Number(t.substr(n,1)),4)},a=t=>(e,r)=>{e.push(2,4),e.push(t.length,r<10?9:r<27?11:13);let n=0;for(;n<t.length-1;n+=2)e.push(45*i(t[n])+i(t[n+1]),11);n<t.length&&e.push(i(t[n]),6)},f=t=>(e,r)=>{e.push(4,4),e.push(t.length,r<10?8:16),t.forEach(t=>e.push(t,8))},c=t=>e=>{e.push(7,4),e.push(t,8)},h=t=>f([...t].map(t=>t.codePointAt(0))),p=t=>l(c(26),f((new TextEncoder).encode(t))),d=t=>t.reduce((t,e)=>e.e<t.e?e:t);u.reg=/[0-9]/,u.est=(t,e)=>4+(e<10?10:e<27?12:14)+10*t.length/3,a.reg=/[0-9A-Z $%*+./:-]/,a.est=(t,e)=>4+(e<10?9:e<27?11:13)+5.5*t.length,h.reg=/[\u0000-\u00FF]/,h.est=(t,e)=>4+(e<10?8:16)+8*t.length;var g={auto:(t,{modes:e=[u,a,h,p]}={})=>{const r=new Set(e),n=r.delete(p);return n&&(e=[...r]),(r,s)=>{let o=[{c:0,e:0}];for(let i=0;i<t.length;++i)if(o=e.filter(e=>e.reg.test(t[i])).map(e=>d(o.map(r=>{const n={c:e,p:r.c===e?r.p:r,s:r.c===e?r.s:i};return n.v=t.substring(n.s,i+1),n.e=n.p.e+Math.ceil(e.est(n.v,s)),n}))),!o.length){if(n)return void p(t)(r,s);throw new Error("Unencodable")}const i=[];for(let t=d(o);t.c;t=t.p)i.unshift(t.c(t.v));i.forEach(t=>t(r,s))}},multi:l,eci:c,numeric:u,alphaNumeric:a,bytes:f,iso8859_1:h,utf8:p};class m{constructor(t){this.bytes=new Uint8Array(t),this.bits=0}push(t,e){for(let r=e,n=8-(7&this.bits);r>0;r-=n,n=8)this.bytes[this.bits>>>3]|=t<<n>>>r,this.bits+=Math.min(r,n)}}function b(t){Array.isArray(t)||(t=[255&t,t>>>8&255,t>>>16&255,t>>>24]);const e=new Uint8Array([...t,255]);return new Uint32Array(e.buffer,0,1)[0]}class v{constructor({size:t,d:e}){this.size=t,this.d=new Uint8Array(e||t*t)}get(t,e){return!!(1&this.d[e*this.size+t])}masked(t,e){return 2&this.d[e*this.size+t]}set(t,e,r,n=1){this.d[e*this.size+t]=2*n|!!r}inv(t,e){this.d[e*this.size+t]^=1}toString({on:t="##",off:e=" ",lf:r="\n",padX:n=4,padY:s=4}={}){const o=(e.repeat(this.size+2*n)+r).repeat(s),i=e.repeat(n);let l=o;for(let n=0;n<this.size;++n){l+=i;for(let r=0;r<this.size;++r)l+=this.get(r,n)?t:e;l+=i+r}return l+o}toImageData(t,{on:e=4278190080,off:r=0}={}){const n=t.createImageData(this.size,this.size),s=new Uint32Array(n.data.buffer),o=b(e),i=b(r);s.fill(i);for(let t=0;t<this.size*this.size;++t)s[t]=1&this.d[t]?o:i;return n}toCanvas(t,{padX:e=4,padY:r=4,...n}={}){t.width=this.size+2*e,t.height=this.size+2*r;const s=t.getContext("2d"),o=this.toImageData(s,n);s.putImageData(o,e,r)}}var y=[(t,e)=>!(1&(t^e)),(t,e)=>!(1&e),t=>!(t%3),(t,e)=>!((t+e)%3),(t,e)=>!(1&(Math.floor(t/3)^Math.floor(e/2))),(t,e)=>!((t&e&1)+t*e%3),(t,e)=>!((t&e&1)+t*e%3&1),(t,e)=>!((1&(t^e))+t*e%3&1)];const z=new Uint8Array(512);z[0]=1;for(let t=0,e=1;t<255;z[++t]=e)z[e+256]=t,e*=2,256&e&&(e^=285);const w=t=>z[t%255],x=t=>z[t+256],M=(t,e)=>{const r=new Uint8Array(t.length+e.length-1);for(let n=0;n<t.length;++n)for(let s=0;s<e.length;++s)r[n+s]^=w(t[n]+e[s]);return r.map(x)},A=(t,e)=>{const r=new Uint8Array(t.length+e.length-1);r.set(t,0);for(let n=0;n<t.length;++n)if(r[n]){const t=x(r[n]);for(let s=0;s<e.length;++s)r[n+s]^=w(e[s]+t)}return r.slice(t.length)},E=[[0],[0,0]];for(let t=1,e=E[1];t<30;++t){const r=M(e,[0,t]);E.push(r),e=r}const U=(t,e,r)=>{let n=e;const s=Math.max(...r.map(t=>t.length));for(let e=0;e<s;++e)r.forEach(r=>{e<r.length&&(t[n++]=r[e])});return n};var j=(t,{groups:e,ecsize:r})=>{const n=[],s=[];let o=0,i=0;e.forEach(([e,l])=>{for(let i=0;i<e;++i,o+=l){const e=t.slice(o,o+l);n.push(e),s.push(A(e,E[r]))}i+=e*(l+r)});const l=new Uint8Array(i),u=U(l,0,n);return U(l,u,s),l};const O=(t,e,r)=>{let n=t<<r-1;for(let t=134217728;t;t>>>=1)n&t&&(n^=e*(t>>>r-1));return n},S=(t,e,r,n,s,o)=>{for(let i=r;i<s;++i)for(let r=e;r<n;++r)t.set(r,i,o)},I=(t,e,r)=>{S(t,e-3,r-3,e+4,r+4,1),S(t,e-2,r-2,e+3,r+3,0),S(t,e-1,r-1,e+2,r+2,1)},P=(t,e,r)=>{S(t,e-2,r-2,e+3,r+3,1),S(t,e-1,r-1,e+2,r+2,0),t.set(e,r,1)},_=t=>{const e=t.size,r=[];for(let n=e-2,s=e,o=-1;n>=0;n-=2){for(5===n&&(n=4);s+=o,-1!==s&&s!==e;)t.masked(n+1,s)||r.push([n+1,s]),t.masked(n,s)||r.push([n,s]);o*=-1}return r},k=(t,e,r)=>{e.forEach(([e,n],s)=>t.set(e,n,r[s>>3]<<(7&s)&128,0))},B=(t,e,r,n)=>{const s=t.size;for(let r=0;r<s;++r)for(let n=0;n<s;++n)e(n,r)&&!t.masked(n,r)&&t.inv(n,r);const o=n<<3|r;let i=21522^(o<<10|O(o,1335,11));for(let e=8;e-- >0;i>>=1)t.set(8,(e>1?7:8)-e,1&i),t.set(s-8+e,8,1&i);for(let e=7;e-- >0;i>>=1)t.set(e>5?7:e,8,1&i),t.set(8,s-e-1,1&i)},C=(t,e,r)=>{for(let n=0;n<t.size;++n){let s=e,o=e;for(let e=0;e<t.size;++e)s=r(s,t.get(e,n)),o=r(o,t.get(n,e))}};var D=t=>(t=>{let e=0;return C(t,[0],([t,r],n)=>n!==r?[1,n]:(4===t?e+=3:t>4&&++e,[t+1,r])),e})(t)+3*(t=>{let e=0;for(let r=1;r<t.size;++r){let n=t.get(r-1,0),s=t.get(r,0)===n;for(let o=1;o<t.size;++o){const i=t.get(r-1,o),l=t.get(r,o)===i;e+=s&&l&&n===i,n=i,s=l}}return e})(t)+40*(t=>{let e=0;return C(t,0,(t,r)=>{const n=(t>>>1|2098176)&(3047517^(r?0:-1));return 2049&n&&++e,n}),e})(t)+(t=>{let e=0;for(let r=0;r<t.size;++r)for(let n=0;n<t.size;++n)e+=t.get(n,r);return 10*Math.floor(20*Math.abs(e/(t.size*t.size)-.5))})(t);const N=[],T=t=>{let e=N[t];if(!e){const r=new v({size:4*t+17});((t,e)=>{const r=4*e+17;I(t,3,3),I(t,r-4,3),I(t,3,r-4),S(t,0,7,9,9,0),S(t,7,0,9,7,0),S(t,r-8,7,r,9,0),S(t,r-8,0,r-7,7,0),S(t,7,r-8,9,r,0),S(t,0,r-8,7,r-7,0),t.set(8,r-8,1);for(let e=8;e<r-8;++e)t.set(e,6,!(1&e)),t.set(6,e,!(1&e));if(e>=2){const n=Math.floor(e/7)+1,s=2*Math.ceil((r-13)/n/2-.25),o=[6];for(let t=n;t-- >0;)o.push(r-7-t*s);for(let e=0;e<=n;++e)for(let r=0;r<=n;++r)!e&&!r||!e&&r===n||e===n&&!r||P(t,o[e],o[r])}if(e>=7)for(let n=e<<12|O(e,7973,13),s=0;s<6;++s)for(let e=12;e-- >9;n>>>=1)t.set(s,r-e,1&n),t.set(r-e,s,1&n)})(r,t),N[t]=e=[r,_(r)]}return[new v(e[0]),e[1]]};var L=(t,{minCorrectionLevel:e=o.min,maxCorrectionLevel:r=o.max,minVersion:n=1,maxVersion:i=40,mask:l=null}={})=>{if(r<e)throw new Error("Invalid correction level range");if(i<n)throw new Error("Invalid version range");"string"==typeof t&&(t=g.auto(t));let u=0;for(let o=n;o<=i;++o){if(s[e].v[o-1].capBits<u)continue;const n=new m(2956);t(n,o),u=n.bits;for(let t=r;t>=e;--t){const e=s[t],r=e.v[o-1];if(r.capBits<u)continue;for(n.push(0,4),n.bits=n.bits+7&-8;n.bits<r.capBits;)n.push(60433,16);const[i,a]=T(o);return k(i,a,j(n.bytes,r)),null!==l?(B(i,y[l],l,e.id),i):y.map((t,r)=>{const n=new v(i);return B(n,t,r,e.id),n.s=D(n),n}).reduce((t,e)=>e.s<t.s?e:t)}}throw new Error("Too much data")}}])})); | ||
//# sourceMappingURL=index.js.map | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});const t=t=>{const e=t>>18,r=t>>12&63,s=t>>5&127;return{capBits:8*(r*s+e*(s+1)),groups:e?[[r,s],[e,s+1]]:[[r,s]],ecsize:31&t}},e=[{id:1,v:[4711,5194,5871,6676,7578,10386,10708,11320,11934,534674,18996,535448,19834,278142,285430,285784,1318268,286494,1064508,1326460,1068700,1846780,1331006,1076926,1084762,568924,1085278,2637502,1867422,2645630,843390,73342,335486,1629822,1888062,3698494,1122142,4738910,1134270,1654494].map(t)},{id:0,v:[4618,5008,5530,9234,9592,17264,17394,533718,537750,279930,1054302,550038,296118,1328408,1332536,816572,304604,1086842,2897306,3421498,71002,71132,3687932,3696060,3442172,1127900,878012,6043068,1922492,2700796,7611868,6071772,5563868,6088156,6866428,8939004,3790300,8443356,2000380,8201724].map(t)},{id:3,v:[4525,4822,8754,8986,532978,17016,1057234,541270,1065492,549496,1065692,1589914,1082008,1356308,1856286,586360,3936988,332508,1118906,1372958,1643228,4223774,3715870,4240158,5796638,1688284,6849278,8143646,9704190,6615838,434974,9216798,5100318,2016030,3830558,2810654,2822942,3867422,5944094,9052958].map(t)},{id:2,v:[4401,4636,8630,16688,532854,16892,278970,541146,1065368,549372,2109848,1077724,1098102,1356184,1880472,3420670,4465116,4989404,4231610,2683388,1651230,139704,3736062,647710,3498494,1184286,7389694,8172030,6894078,6648318,7434750,9253374,12104190,504350,10838526,16785918,12157438,8561150,17605118,16073214].map(t)}],r={min:0,L:0,M:1,Q:2,H:3,max:3},s=t=>"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:".indexOf(t),n=function(){for(var t=arguments.length,e=new Array(t),r=0;r<t;r++)e[r]=arguments[r];return(t,r)=>e.forEach((e=>e(t,r)))},o=t=>(e,r)=>{e.push(1,4),e.push(t.length,r<10?10:r<27?12:14);let s=0;for(;s<t.length-2;s+=3)e.push(Number(t.substr(s,3)),10);s<t.length-1?e.push(Number(t.substr(s,2)),7):s<t.length&&e.push(Number(t.substr(s,1)),4)},i=t=>(e,r)=>{e.push(2,4),e.push(t.length,r<10?9:r<27?11:13);let n=0;for(;n<t.length-1;n+=2)e.push(45*s(t[n])+s(t[n+1]),11);n<t.length&&e.push(s(t[n]),6)},l=t=>(e,r)=>{e.push(4,4),e.push(t.length,r<10?8:16),t.forEach((t=>e.push(t,8)))},h=t=>e=>{e.push(7,4),e.push(t,8)},a=t=>l([...t].map((t=>t.codePointAt(0)))),c=t=>n(h(26),l((new TextEncoder).encode(t))),u=t=>t.reduce(((t,e)=>e.e<t.e?e:t));o.reg=/[0-9]/,o.est=(t,e)=>4+(e<10?10:e<27?12:14)+10*t.length/3,i.reg=/[0-9A-Z $%*+./:-]/,i.est=(t,e)=>4+(e<10?9:e<27?11:13)+5.5*t.length,a.reg=/[\u0000-\u00FF]/,a.est=(t,e)=>4+(e<10?8:16)+8*t.length;var f={auto:function(t){let{modes:e=[o,i,a,c]}=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};const r=new Set(e),s=r.delete(c);return s&&(e=[...r]),(r,n)=>{let o=[{c:0,e:0}];for(let i=0;i<t.length;++i)if(o=e.filter((e=>e.reg.test(t[i]))).map((e=>u(o.map((r=>{const s={c:e,p:r.c===e?r.p:r,s:r.c===e?r.s:i};return s.v=t.substring(s.s,i+1),s.e=s.p.e+Math.ceil(e.est(s.v,n)),s}))))),!o.length){if(s)return void c(t)(r,n);throw new Error("Unencodable")}const i=[];for(let t=u(o);t.c;t=t.p)i.unshift(t.c(t.v));i.forEach((t=>t(r,n)))}},multi:n,eci:h,numeric:o,alphaNumeric:i,bytes:l,iso8859_1:a,utf8:c};class g{constructor(t){this.bytes=new Uint8Array(t),this.bits=0}push(t,e){for(let r=e,s=8-(7&this.bits);r>0;r-=s,s=8)this.bytes[this.bits>>>3]|=t<<s>>>r,this.bits+=Math.min(r,s)}}function p(t){Array.isArray(t)||(t=[255&t,t>>>8&255,t>>>16&255,t>>>24]);const e=new Uint8Array([...t,255]);return new Uint32Array(e.buffer,0,1)[0]}class d{constructor(t){let{size:e,d:r}=t;this.size=e,this.d=new Uint8Array(r||e*e)}get(t,e){return!!(1&this.d[e*this.size+t])}masked(t,e){return 2&this.d[e*this.size+t]}set(t,e,r){let s=arguments.length>3&&void 0!==arguments[3]?arguments[3]:1;this.d[e*this.size+t]=2*s|!!r}inv(t,e){this.d[e*this.size+t]^=1}toString(){let{on:t="##",off:e=" ",lf:r="\n",padX:s=4,padY:n=4}=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};const o=(e.repeat(this.size+2*s)+r).repeat(n),i=e.repeat(s);let l=o;for(let s=0;s<this.size;++s){l+=i;for(let r=0;r<this.size;++r)l+=this.get(r,s)?t:e;l+=i+r}return l+o}toImageData(t){let{on:e=4278190080,off:r=0}=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};const s=t.createImageData(this.size,this.size),n=new Uint32Array(s.data.buffer),o=p(e),i=p(r);n.fill(i);for(let t=0;t<this.size*this.size;++t)n[t]=1&this.d[t]?o:i;return s}toCanvas(t){let{padX:e=4,padY:r=4,...s}=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};t.width=this.size+2*e,t.height=this.size+2*r;const n=t.getContext("2d"),o=this.toImageData(n,s);n.putImageData(o,e,r)}}var m=[(t,e)=>!(1&(t^e)),(t,e)=>!(1&e),t=>!(t%3),(t,e)=>!((t+e)%3),(t,e)=>!(1&(Math.floor(t/3)^Math.floor(e/2))),(t,e)=>!((t&e&1)+t*e%3),(t,e)=>!((t&e&1)+t*e%3&1),(t,e)=>!((1&(t^e))+t*e%3&1)];const v=new Uint8Array(512);v[0]=1;for(let t=0,e=1;t<255;v[++t]=e)v[e+256]=t,e*=2,256&e&&(e^=285);const z=t=>v[t%255],w=t=>v[t+256],b=(t,e)=>{const r=new Uint8Array(t.length+e.length-1);for(let s=0;s<t.length;++s)for(let n=0;n<e.length;++n)r[s+n]^=z(t[s]+e[n]);return r.map(w)},y=(t,e)=>{const r=new Uint8Array(t.length+e.length-1);r.set(t,0);for(let s=0;s<t.length;++s)if(r[s]){const t=w(r[s]);for(let n=0;n<e.length;++n)r[s+n]^=z(e[n]+t)}return r.slice(t.length)},A=[[0],[0,0]];for(let t=1,e=A[1];t<30;++t){const r=b(e,[0,t]);A.push(r),e=r}const x=(t,e,r)=>{let s=e;const n=Math.max(...r.map((t=>t.length)));for(let e=0;e<n;++e)r.forEach((r=>{e<r.length&&(t[s++]=r[e])}));return s};var E=(t,e)=>{let{groups:r,ecsize:s}=e;const n=[],o=[];let i=0,l=0;r.forEach((e=>{let[r,h]=e;for(let e=0;e<r;++e,i+=h){const e=t.slice(i,i+h);n.push(e),o.push(y(e,A[s]))}l+=r*(h+s)}));const h=new Uint8Array(l),a=x(h,0,n);return x(h,a,o),h};const M=(t,e,r)=>{let s=t<<r-1;for(let t=134217728;t;t>>>=1)s&t&&(s^=e*(t>>>r-1));return s},U=(t,e,r,s,n,o)=>{for(let i=r;i<n;++i)for(let r=e;r<s;++r)t.set(r,i,o)},I=(t,e,r)=>{U(t,e-3,r-3,e+4,r+4,1),U(t,e-2,r-2,e+3,r+3,0),U(t,e-1,r-1,e+2,r+2,1)},k=(t,e,r)=>{U(t,e-2,r-2,e+3,r+3,1),U(t,e-1,r-1,e+2,r+2,0),t.set(e,r,1)},B=t=>{const e=t.size,r=[];for(let s=e-2,n=e,o=-1;s>=0;s-=2){for(5===s&&(s=4);n+=o,-1!==n&&n!==e;)t.masked(s+1,n)||r.push([s+1,n]),t.masked(s,n)||r.push([s,n]);o*=-1}return r},C=(t,e,r)=>{e.forEach(((e,s)=>{let[n,o]=e;return t.set(n,o,r[s>>3]<<(7&s)&128,0)}))},D=(t,e,r,s)=>{const n=t.size;for(let r=0;r<n;++r)for(let s=0;s<n;++s)e(s,r)&&!t.masked(s,r)&&t.inv(s,r);const o=s<<3|r;let i=21522^(o<<10|M(o,1335,11));for(let e=8;e-- >0;i>>=1)t.set(8,(e>1?7:8)-e,1&i),t.set(n-8+e,8,1&i);for(let e=7;e-- >0;i>>=1)t.set(e>5?7:e,8,1&i),t.set(8,n-e-1,1&i)},N=(t,e,r)=>{for(let s=0;s<t.size;++s){let n=e,o=e;for(let e=0;e<t.size;++e)n=r(n,t.get(e,s)),o=r(o,t.get(s,e))}};var L=t=>(t=>{let e=0;return N(t,[0],((t,r)=>{let[s,n]=t;return r!==n?[1,r]:(4===s?e+=3:s>4&&++e,[s+1,n])})),e})(t)+3*(t=>{let e=0;for(let r=1;r<t.size;++r){let s=t.get(r-1,0),n=t.get(r,0)===s;for(let o=1;o<t.size;++o){const i=t.get(r-1,o),l=t.get(r,o)===i;e+=n&&l&&s===i,s=i,n=l}}return e})(t)+40*(t=>{let e=0;return N(t,0,((t,r)=>{const s=(t>>>1|2098176)&(3047517^(r?0:-1));return 2049&s&&++e,s})),e})(t)+(t=>{let e=0;for(let r=0;r<t.size;++r)for(let s=0;s<t.size;++s)e+=t.get(s,r);return 10*Math.floor(20*Math.abs(e/(t.size*t.size)-.5))})(t);const F=[],O=t=>{let e=F[t];if(!e){const r=new d({size:4*t+17});((t,e)=>{const r=4*e+17;I(t,3,3),I(t,r-4,3),I(t,3,r-4),U(t,0,7,9,9,0),U(t,7,0,9,7,0),U(t,r-8,7,r,9,0),U(t,r-8,0,r-7,7,0),U(t,7,r-8,9,r,0),U(t,0,r-8,7,r-7,0),t.set(8,r-8,1);for(let e=8;e<r-8;++e)t.set(e,6,!(1&e)),t.set(6,e,!(1&e));if(e>=2){const s=Math.floor(e/7)+1,n=2*Math.ceil((r-13)/s/2-.25),o=[6];for(let t=s;t-- >0;)o.push(r-7-t*n);for(let e=0;e<=s;++e)for(let r=0;r<=s;++r)!e&&!r||!e&&r===s||e===s&&!r||k(t,o[e],o[r])}if(e>=7)for(let s=e<<12|M(e,7973,13),n=0;n<6;++n)for(let e=12;e-- >9;s>>>=1)t.set(n,r-e,1&s),t.set(r-e,n,1&s)})(r,t),F[t]=e=[r,B(r)]}return[new d(e[0]),e[1]]};exports.correction=r,exports.generate=function(t){let{minCorrectionLevel:s=r.min,maxCorrectionLevel:n=r.max,minVersion:o=1,maxVersion:i=40,mask:l=null}=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(n<s)throw new Error("Invalid correction level range");if(i<o)throw new Error("Invalid version range");"string"==typeof t&&(t=f.auto(t));let h=0;for(let r=o;r<=i;++r){if(e[s].v[r-1].capBits<h)continue;const o=new g(2956);t(o,r),h=o.bits;for(let t=n;t>=s;--t){const s=e[t],n=s.v[r-1];if(n.capBits<h)continue;for(o.push(0,4),o.bits=o.bits+7&-8;o.bits<n.capBits;)o.push(60433,16);const[i,a]=O(r);return C(i,a,E(o.bytes,n)),null!==l?(D(i,m[l],l,s.id),i):m.map(((t,e)=>{const r=new d(i);return D(r,t,e,s.id),r.s=L(r),r})).reduce(((t,e)=>e.s<t.s?e:t))}}throw new Error("Too much data")},exports.mode=f; | ||
//# sourceMappingURL=index.js.map |
@@ -17,2 +17,4 @@ declare module 'lean-qr' { | ||
export type RGBA = [number, number, number, number?]; | ||
export interface Bitmap1D { | ||
@@ -28,20 +30,26 @@ push(value: number, bits: number): void; | ||
toString(options?: { | ||
on?: string, | ||
off?: string, | ||
lf?: string, | ||
padX?: number, | ||
padY?: number, | ||
on?: RGBA; | ||
off?: RGBA; | ||
lf?: string; | ||
padX?: number; | ||
padY?: number; | ||
}): string; | ||
toImageData<DataT extends ImageDataLike>(context: Context2DLike<DataT>, options?: { | ||
on?: number, | ||
off?: number, | ||
}): DataT; | ||
toImageData<DataT extends ImageDataLike>( | ||
context: Context2DLike<DataT>, | ||
options?: { | ||
on?: RGBA; | ||
off?: RGBA; | ||
}, | ||
): DataT; | ||
toCanvas(canvas: CanvasLike<ImageDataLike>, options?: { | ||
on?: number, | ||
off?: number, | ||
padX?: number, | ||
padY?: number, | ||
}): void; | ||
toCanvas( | ||
canvas: CanvasLike<ImageDataLike>, | ||
options?: { | ||
on?: RGBA; | ||
off?: RGBA; | ||
padX?: number; | ||
padY?: number; | ||
}, | ||
): void; | ||
} | ||
@@ -62,10 +70,10 @@ | ||
export const mode: { | ||
auto(value: string, options?: ModeAutoOptions): Mode, | ||
multi(...modes: Mode[]): Mode, | ||
eci(id: number): Mode, | ||
numeric: ModeFactory, | ||
alphaNumeric: ModeFactory, | ||
bytes(data: Uint8Array | number[]): Mode, | ||
iso8859_1: ModeFactory, | ||
utf8: ModeFactory, | ||
auto(value: string, options?: ModeAutoOptions): Mode; | ||
multi(...modes: Mode[]): Mode; | ||
eci(id: number): Mode; | ||
numeric: ModeFactory; | ||
alphaNumeric: ModeFactory; | ||
bytes(data: Uint8Array | number[]): Mode; | ||
iso8859_1: ModeFactory; | ||
utf8: ModeFactory; | ||
}; | ||
@@ -90,3 +98,30 @@ | ||
export const generate: (data: Mode | string, options?: GenerateOptions) => Bitmap2D; | ||
export const generate: ( | ||
data: Mode | string, | ||
options?: GenerateOptions, | ||
) => Bitmap2D; | ||
} | ||
declare module 'lean-qr/extras/svg' { | ||
import type { Bitmap2D } from 'lean-qr'; | ||
interface SVGOptions { | ||
on?: string; | ||
off?: string; | ||
padX?: number; | ||
padY?: number; | ||
width?: number; | ||
height?: number; | ||
scale?: number; | ||
} | ||
export const toSvgPath: (code: Bitmap2D) => string; | ||
export const toSvg: ( | ||
code: Bitmap2D, | ||
target: Document | SVGElement, | ||
options?: SVGOptions, | ||
) => SVGElement; | ||
export const toSvgSource: (code: Bitmap2D, options?: SVGOptions) => string; | ||
} |
{ | ||
"name": "lean-qr", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "minimal QR code generation", | ||
@@ -12,10 +12,14 @@ "files": [ | ||
"main": "build/index.js", | ||
"exports": { | ||
".": "./build/index.js", | ||
"./extras/*": "./build/extras/*.js", | ||
"./extras/*.js": "./build/extras/*.js", | ||
"./package.json": "./package.json" | ||
}, | ||
"types": "index.d.ts", | ||
"scripts": { | ||
"build": "webpack --mode production", | ||
"lint": "eslint --format codeframe --ext mjs,jsx,js src bin", | ||
"start": "http-server . -d false -c-1 -a localhost -p 8080", | ||
"test": "jest", | ||
"posttest": "npm run lint", | ||
"test:watch": "jest --watch" | ||
"build": "rollup --config rollup.config.js", | ||
"format": "prettier --write .", | ||
"start": "static-server --index index.html --port 8080", | ||
"test": "lean-test && lean-test --browser=chrome,firefox -x '**/node_modules' -x '**/.*' -x '**/*.int.*' && ./test-package/run.sh && prettier --check ." | ||
}, | ||
@@ -38,13 +42,11 @@ "repository": { | ||
"devDependencies": { | ||
"@neutrinojs/airbnb-base": "9.x", | ||
"@neutrinojs/jest": "9.x", | ||
"@neutrinojs/library": "9.x", | ||
"eslint": "7.x", | ||
"http-server": "^0.12.3", | ||
"jest": "26.x", | ||
"neutrino": "9.x", | ||
"pngjs": "^6.0.0", | ||
"webpack": "4.x", | ||
"webpack-cli": "3.x" | ||
"@babel/preset-env": "7.x", | ||
"@rollup/plugin-babel": "5.x", | ||
"lean-test": "1.x", | ||
"pngjs": "6.x", | ||
"prettier": "2.5.1", | ||
"rollup": "2.x", | ||
"rollup-plugin-terser": "7.x", | ||
"static-server": "2.x" | ||
} | ||
} |
@@ -10,6 +10,8 @@ # Lean QR | ||
Or try it from the commandline: `npx lean-qr 'MY MESSAGE HERE'` | ||
## Install dependency | ||
```bash | ||
npm install --save-dev lean-qr | ||
npm install --save lean-qr | ||
``` | ||
@@ -43,9 +45,11 @@ | ||
### Shell | ||
There is also a small commandline tool included for testing: | ||
```shell | ||
lean-qr 'MY MESSAGE HERE' | ||
npx lean-qr 'MY MESSAGE HERE' | ||
``` | ||
For full documentation, run `lean-qr --help`. | ||
For full documentation, run `npx lean-qr --help`. | ||
@@ -341,2 +345,77 @@ ## Modes | ||
### `toSvg(code, target[, options])` | ||
This is not included in the main library to keep it small, but if you need | ||
SVG output, you can access it from a separate import (adds ~2kB): | ||
```javascript | ||
import { toSvg } from 'lean-qr/extras/svg'; | ||
const mySvg = document.getElementById('my-svg'); | ||
const svg = toSvg(code, mySvg, { | ||
on: 'black', | ||
off: 'transparent', | ||
padX: 4, | ||
padY: 4, | ||
width: null, | ||
height: null, | ||
scale: 1, | ||
}); | ||
``` | ||
This will replace the image in `mySvg` with a copy of the current code. The | ||
result is always at a scale of 1 SVG unit per module (the viewBox will be | ||
resized to the correct size automatically). You can define a different | ||
size for the SVG element to scale the image. | ||
If `mySvg` is the `document` object, this will create and return a new SVG | ||
entity associated with the document (but not attached to it). | ||
If `width` / `height` is given, the root SVG element will have the explicit | ||
size applied. If these are not specified, they will be auto-calculated by | ||
multiplying the code size + padding by `scale`. You can override this for | ||
display by setting CSS properties (e.g. `mySvg.style.width = '100%'`). | ||
### `toSvgSource(code[, options])` | ||
Like `toSvg` but returns the source code for an SVG, rather than manipulating | ||
DOM nodes (can be called inside NodeJS). | ||
```javascript | ||
import { toSvgSource } from 'lean-qr/extras/svg'; | ||
const svgSource = toSvgSource(code, { | ||
on: 'black', | ||
off: 'transparent', | ||
padX: 4, | ||
padY: 4, | ||
width: null, | ||
height: null, | ||
scale: 1, | ||
}); | ||
``` | ||
Returns a complete SVG document which can be written to a standalone file or | ||
included inside a HTML document. If written to a file, you should prefix the | ||
source with `<?xml version="1.0" encoding="UTF-8" ?>`. | ||
### `toSvgPath(code)` | ||
A raw SVG path definition for the QR code. Used by `toSvg` and `toSvgSource`. | ||
```javascript | ||
import { toSvgPath } from 'lean-qr/extras/svg'; | ||
const svgPath = toSvgPath(code); | ||
// e.g. "M1 2L1 1L2 1L2 2ZM3 3L3 2L4 2L4 3Z" | ||
``` | ||
The returned path is always at a scale of 1 SVG unit to 1 module, with no | ||
padding. The path will define the whole QR code in a single string suitable for | ||
use inside `<path d="[path here]">`, and can be used with `fill-rule` of either | ||
`evenodd` or `nonzero`. The path is optimised to ensure only true edges are | ||
defined; it will not include overlapping edges (and will not have "cracks" | ||
between pixels). No other guarantees are made about the structure of this | ||
string, and the details could change in later versions. | ||
### `get(x, y)` | ||
@@ -355,2 +434,6 @@ | ||
Note that this is only well-defined for `x` and `y` inside the QR code area. | ||
In particular: providing negative `x` or `y`, or values beyond the value of | ||
`code.size`, will give undefined results. | ||
## Resources | ||
@@ -357,0 +440,0 @@ |
Sorry, the diff of this file is not supported yet
83252
8
10
433
439