forked from jeromeetienne/jquery-qrcode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprototype.qrcode.min.js
1 lines (1 loc) · 15.9 KB
/
prototype.qrcode.min.js
1
function QR8bitByte(a){this.mode=QRMode.MODE_8BIT_BYTE;this.data=a}function QRCode(a,b){this.typeNumber=a;this.errorCorrectLevel=b;this.modules=null;this.moduleCount=0;this.dataCache=null;this.dataList=new Array}function QRPolynomial(a,b){if(a.length==undefined){throw new Error(a.length+"/"+b)}var c=0;while(c<a.length&&a[c]==0){c++}this.num=new Array(a.length-c+b);for(var d=0;d<a.length-c;d++){this.num[d]=a[d+c]}}function QRRSBlock(a,b){this.totalCount=a;this.dataCount=b}function QRBitBuffer(){this.buffer=new Array;this.length=0}QR8bitByte.prototype={getLength:function(a){return this.data.length},write:function(a){for(var b=0;b<this.data.length;b++){a.put(this.data.charCodeAt(b),8)}}};QRCode.prototype={addData:function(a){var b=new QR8bitByte(a);this.dataList.push(b);this.dataCache=null},isDark:function(a,b){if(a<0||this.moduleCount<=a||b<0||this.moduleCount<=b){throw new Error(a+","+b)}return this.modules[a][b]},getModuleCount:function(){return this.moduleCount},make:function(){if(this.typeNumber<1){var a=1;for(a=1;a<40;a++){var b=QRRSBlock.getRSBlocks(a,this.errorCorrectLevel);var c=new QRBitBuffer;var d=0;for(var e=0;e<b.length;e++){d+=b[e].dataCount}for(var e=0;e<this.dataList.length;e++){var f=this.dataList[e];c.put(f.mode,4);c.put(f.getLength(),QRUtil.getLengthInBits(f.mode,a));f.write(c)}if(c.getLengthInBits()<=d*8)break}this.typeNumber=a}this.makeImpl(false,this.getBestMaskPattern())},makeImpl:function(a,b){this.moduleCount=this.typeNumber*4+17;this.modules=new Array(this.moduleCount);for(var c=0;c<this.moduleCount;c++){this.modules[c]=new Array(this.moduleCount);for(var d=0;d<this.moduleCount;d++){this.modules[c][d]=null}}this.setupPositionProbePattern(0,0);this.setupPositionProbePattern(this.moduleCount-7,0);this.setupPositionProbePattern(0,this.moduleCount-7);this.setupPositionAdjustPattern();this.setupTimingPattern();this.setupTypeInfo(a,b);if(this.typeNumber>=7){this.setupTypeNumber(a)}if(this.dataCache==null){this.dataCache=QRCode.createData(this.typeNumber,this.errorCorrectLevel,this.dataList)}this.mapData(this.dataCache,b)},setupPositionProbePattern:function(a,b){for(var c=-1;c<=7;c++){if(a+c<=-1||this.moduleCount<=a+c)continue;for(var d=-1;d<=7;d++){if(b+d<=-1||this.moduleCount<=b+d)continue;if(0<=c&&c<=6&&(d==0||d==6)||0<=d&&d<=6&&(c==0||c==6)||2<=c&&c<=4&&2<=d&&d<=4){this.modules[a+c][b+d]=true}else{this.modules[a+c][b+d]=false}}}},getBestMaskPattern:function(){var a=0;var b=0;for(var c=0;c<8;c++){this.makeImpl(true,c);var d=QRUtil.getLostPoint(this);if(c==0||a>d){a=d;b=c}}return b},createMovieClip:function(a,b,c){var d=a.createEmptyMovieClip(b,c);var e=1;this.make();for(var f=0;f<this.modules.length;f++){var g=f*e;for(var h=0;h<this.modules[f].length;h++){var i=h*e;var j=this.modules[f][h];if(j){d.beginFill(0,100);d.moveTo(i,g);d.lineTo(i+e,g);d.lineTo(i+e,g+e);d.lineTo(i,g+e);d.endFill()}}}return d},setupTimingPattern:function(){for(var a=8;a<this.moduleCount-8;a++){if(this.modules[a][6]!=null){continue}this.modules[a][6]=a%2==0}for(var b=8;b<this.moduleCount-8;b++){if(this.modules[6][b]!=null){continue}this.modules[6][b]=b%2==0}},setupPositionAdjustPattern:function(){var a=QRUtil.getPatternPosition(this.typeNumber);for(var b=0;b<a.length;b++){for(var c=0;c<a.length;c++){var d=a[b];var e=a[c];if(this.modules[d][e]!=null){continue}for(var f=-2;f<=2;f++){for(var g=-2;g<=2;g++){if(f==-2||f==2||g==-2||g==2||f==0&&g==0){this.modules[d+f][e+g]=true}else{this.modules[d+f][e+g]=false}}}}}},setupTypeNumber:function(a){var b=QRUtil.getBCHTypeNumber(this.typeNumber);for(var c=0;c<18;c++){var d=!a&&(b>>c&1)==1;this.modules[Math.floor(c/3)][c%3+this.moduleCount-8-3]=d}for(var c=0;c<18;c++){var d=!a&&(b>>c&1)==1;this.modules[c%3+this.moduleCount-8-3][Math.floor(c/3)]=d}},setupTypeInfo:function(a,b){var c=this.errorCorrectLevel<<3|b;var d=QRUtil.getBCHTypeInfo(c);for(var e=0;e<15;e++){var f=!a&&(d>>e&1)==1;if(e<6){this.modules[e][8]=f}else if(e<8){this.modules[e+1][8]=f}else{this.modules[this.moduleCount-15+e][8]=f}}for(var e=0;e<15;e++){var f=!a&&(d>>e&1)==1;if(e<8){this.modules[8][this.moduleCount-e-1]=f}else if(e<9){this.modules[8][15-e-1+1]=f}else{this.modules[8][15-e-1]=f}}this.modules[this.moduleCount-8][8]=!a},mapData:function(a,b){var c=-1;var d=this.moduleCount-1;var e=7;var f=0;for(var g=this.moduleCount-1;g>0;g-=2){if(g==6)g--;while(true){for(var h=0;h<2;h++){if(this.modules[d][g-h]==null){var i=false;if(f<a.length){i=(a[f]>>>e&1)==1}var j=QRUtil.getMask(b,d,g-h);if(j){i=!i}this.modules[d][g-h]=i;e--;if(e==-1){f++;e=7}}}d+=c;if(d<0||this.moduleCount<=d){d-=c;c=-c;break}}}}};QRCode.PAD0=236;QRCode.PAD1=17;QRCode.createData=function(a,b,c){var d=QRRSBlock.getRSBlocks(a,b);var e=new QRBitBuffer;for(var f=0;f<c.length;f++){var g=c[f];e.put(g.mode,4);e.put(g.getLength(),QRUtil.getLengthInBits(g.mode,a));g.write(e)}var h=0;for(var f=0;f<d.length;f++){h+=d[f].dataCount}if(e.getLengthInBits()>h*8){throw new Error("code length overflow. ("+e.getLengthInBits()+">"+h*8+")")}if(e.getLengthInBits()+4<=h*8){e.put(0,4)}while(e.getLengthInBits()%8!=0){e.putBit(false)}while(true){if(e.getLengthInBits()>=h*8){break}e.put(QRCode.PAD0,8);if(e.getLengthInBits()>=h*8){break}e.put(QRCode.PAD1,8)}return QRCode.createBytes(e,d)};QRCode.createBytes=function(a,b){var c=0;var d=0;var e=0;var f=new Array(b.length);var g=new Array(b.length);for(var h=0;h<b.length;h++){var i=b[h].dataCount;var j=b[h].totalCount-i;d=Math.max(d,i);e=Math.max(e,j);f[h]=new Array(i);for(var k=0;k<f[h].length;k++){f[h][k]=255&a.buffer[k+c]}c+=i;var l=QRUtil.getErrorCorrectPolynomial(j);var m=new QRPolynomial(f[h],l.getLength()-1);var n=m.mod(l);g[h]=new Array(l.getLength()-1);for(var k=0;k<g[h].length;k++){var o=k+n.getLength()-g[h].length;g[h][k]=o>=0?n.get(o):0}}var p=0;for(var k=0;k<b.length;k++){p+=b[k].totalCount}var q=new Array(p);var r=0;for(var k=0;k<d;k++){for(var h=0;h<b.length;h++){if(k<f[h].length){q[r++]=f[h][k]}}}for(var k=0;k<e;k++){for(var h=0;h<b.length;h++){if(k<g[h].length){q[r++]=g[h][k]}}}return q};var QRMode={MODE_NUMBER:1<<0,MODE_ALPHA_NUM:1<<1,MODE_8BIT_BYTE:1<<2,MODE_KANJI:1<<3};var QRErrorCorrectLevel={L:1,M:0,Q:3,H:2};var QRMaskPattern={PATTERN000:0,PATTERN001:1,PATTERN010:2,PATTERN011:3,PATTERN100:4,PATTERN101:5,PATTERN110:6,PATTERN111:7};var QRUtil={PATTERN_POSITION_TABLE:[[],[6,18],[6,22],[6,26],[6,30],[6,34],[6,22,38],[6,24,42],[6,26,46],[6,28,50],[6,30,54],[6,32,58],[6,34,62],[6,26,46,66],[6,26,48,70],[6,26,50,74],[6,30,54,78],[6,30,56,82],[6,30,58,86],[6,34,62,90],[6,28,50,72,94],[6,26,50,74,98],[6,30,54,78,102],[6,28,54,80,106],[6,32,58,84,110],[6,30,58,86,114],[6,34,62,90,118],[6,26,50,74,98,122],[6,30,54,78,102,126],[6,26,52,78,104,130],[6,30,56,82,108,134],[6,34,60,86,112,138],[6,30,58,86,114,142],[6,34,62,90,118,146],[6,30,54,78,102,126,150],[6,24,50,76,102,128,154],[6,28,54,80,106,132,158],[6,32,58,84,110,136,162],[6,26,54,82,110,138,166],[6,30,58,86,114,142,170]],G15:1<<10|1<<8|1<<5|1<<4|1<<2|1<<1|1<<0,G18:1<<12|1<<11|1<<10|1<<9|1<<8|1<<5|1<<2|1<<0,G15_MASK:1<<14|1<<12|1<<10|1<<4|1<<1,getBCHTypeInfo:function(a){var b=a<<10;while(QRUtil.getBCHDigit(b)-QRUtil.getBCHDigit(QRUtil.G15)>=0){b^=QRUtil.G15<<QRUtil.getBCHDigit(b)-QRUtil.getBCHDigit(QRUtil.G15)}return(a<<10|b)^QRUtil.G15_MASK},getBCHTypeNumber:function(a){var b=a<<12;while(QRUtil.getBCHDigit(b)-QRUtil.getBCHDigit(QRUtil.G18)>=0){b^=QRUtil.G18<<QRUtil.getBCHDigit(b)-QRUtil.getBCHDigit(QRUtil.G18)}return a<<12|b},getBCHDigit:function(a){var b=0;while(a!=0){b++;a>>>=1}return b},getPatternPosition:function(a){return QRUtil.PATTERN_POSITION_TABLE[a-1]},getMask:function(a,b,c){switch(a){case QRMaskPattern.PATTERN000:return(b+c)%2==0;case QRMaskPattern.PATTERN001:return b%2==0;case QRMaskPattern.PATTERN010:return c%3==0;case QRMaskPattern.PATTERN011:return(b+c)%3==0;case QRMaskPattern.PATTERN100:return(Math.floor(b/2)+Math.floor(c/3))%2==0;case QRMaskPattern.PATTERN101:return b*c%2+b*c%3==0;case QRMaskPattern.PATTERN110:return(b*c%2+b*c%3)%2==0;case QRMaskPattern.PATTERN111:return(b*c%3+(b+c)%2)%2==0;default:throw new Error("bad maskPattern:"+a)}},getErrorCorrectPolynomial:function(a){var b=new QRPolynomial([1],0);for(var c=0;c<a;c++){b=b.multiply(new QRPolynomial([1,QRMath.gexp(c)],0))}return b},getLengthInBits:function(a,b){if(1<=b&&b<10){switch(a){case QRMode.MODE_NUMBER:return 10;case QRMode.MODE_ALPHA_NUM:return 9;case QRMode.MODE_8BIT_BYTE:return 8;case QRMode.MODE_KANJI:return 8;default:throw new Error("mode:"+a)}}else if(b<27){switch(a){case QRMode.MODE_NUMBER:return 12;case QRMode.MODE_ALPHA_NUM:return 11;case QRMode.MODE_8BIT_BYTE:return 16;case QRMode.MODE_KANJI:return 10;default:throw new Error("mode:"+a)}}else if(b<41){switch(a){case QRMode.MODE_NUMBER:return 14;case QRMode.MODE_ALPHA_NUM:return 13;case QRMode.MODE_8BIT_BYTE:return 16;case QRMode.MODE_KANJI:return 12;default:throw new Error("mode:"+a)}}else{throw new Error("type:"+b)}},getLostPoint:function(a){var b=a.getModuleCount();var c=0;for(var d=0;d<b;d++){for(var e=0;e<b;e++){var f=0;var g=a.isDark(d,e);for(var h=-1;h<=1;h++){if(d+h<0||b<=d+h){continue}for(var i=-1;i<=1;i++){if(e+i<0||b<=e+i){continue}if(h==0&&i==0){continue}if(g==a.isDark(d+h,e+i)){f++}}}if(f>5){c+=3+f-5}}}for(var d=0;d<b-1;d++){for(var e=0;e<b-1;e++){var j=0;if(a.isDark(d,e))j++;if(a.isDark(d+1,e))j++;if(a.isDark(d,e+1))j++;if(a.isDark(d+1,e+1))j++;if(j==0||j==4){c+=3}}}for(var d=0;d<b;d++){for(var e=0;e<b-6;e++){if(a.isDark(d,e)&&!a.isDark(d,e+1)&&a.isDark(d,e+2)&&a.isDark(d,e+3)&&a.isDark(d,e+4)&&!a.isDark(d,e+5)&&a.isDark(d,e+6)){c+=40}}}for(var e=0;e<b;e++){for(var d=0;d<b-6;d++){if(a.isDark(d,e)&&!a.isDark(d+1,e)&&a.isDark(d+2,e)&&a.isDark(d+3,e)&&a.isDark(d+4,e)&&!a.isDark(d+5,e)&&a.isDark(d+6,e)){c+=40}}}var k=0;for(var e=0;e<b;e++){for(var d=0;d<b;d++){if(a.isDark(d,e)){k++}}}var l=Math.abs(100*k/b/b-50)/5;c+=l*10;return c}};var QRMath={glog:function(a){if(a<1){throw new Error("glog("+a+")")}return QRMath.LOG_TABLE[a]},gexp:function(a){while(a<0){a+=255}while(a>=256){a-=255}return QRMath.EXP_TABLE[a]},EXP_TABLE:new Array(256),LOG_TABLE:new Array(256)};for(var i=0;i<8;i++){QRMath.EXP_TABLE[i]=1<<i}for(var i=8;i<256;i++){QRMath.EXP_TABLE[i]=QRMath.EXP_TABLE[i-4]^QRMath.EXP_TABLE[i-5]^QRMath.EXP_TABLE[i-6]^QRMath.EXP_TABLE[i-8]}for(var i=0;i<255;i++){QRMath.LOG_TABLE[QRMath.EXP_TABLE[i]]=i}QRPolynomial.prototype={get:function(a){return this.num[a]},getLength:function(){return this.num.length},multiply:function(a){var b=new Array(this.getLength()+a.getLength()-1);for(var c=0;c<this.getLength();c++){for(var d=0;d<a.getLength();d++){b[c+d]^=QRMath.gexp(QRMath.glog(this.get(c))+QRMath.glog(a.get(d)))}}return new QRPolynomial(b,0)},mod:function(a){if(this.getLength()-a.getLength()<0){return this}var b=QRMath.glog(this.get(0))-QRMath.glog(a.get(0));var c=new Array(this.getLength());for(var d=0;d<this.getLength();d++){c[d]=this.get(d)}for(var d=0;d<a.getLength();d++){c[d]^=QRMath.gexp(QRMath.glog(a.get(d))+b)}return(new QRPolynomial(c,0)).mod(a)}};QRRSBlock.RS_BLOCK_TABLE=[[1,26,19],[1,26,16],[1,26,13],[1,26,9],[1,44,34],[1,44,28],[1,44,22],[1,44,16],[1,70,55],[1,70,44],[2,35,17],[2,35,13],[1,100,80],[2,50,32],[2,50,24],[4,25,9],[1,134,108],[2,67,43],[2,33,15,2,34,16],[2,33,11,2,34,12],[2,86,68],[4,43,27],[4,43,19],[4,43,15],[2,98,78],[4,49,31],[2,32,14,4,33,15],[4,39,13,1,40,14],[2,121,97],[2,60,38,2,61,39],[4,40,18,2,41,19],[4,40,14,2,41,15],[2,146,116],[3,58,36,2,59,37],[4,36,16,4,37,17],[4,36,12,4,37,13],[2,86,68,2,87,69],[4,69,43,1,70,44],[6,43,19,2,44,20],[6,43,15,2,44,16],[4,101,81],[1,80,50,4,81,51],[4,50,22,4,51,23],[3,36,12,8,37,13],[2,116,92,2,117,93],[6,58,36,2,59,37],[4,46,20,6,47,21],[7,42,14,4,43,15],[4,133,107],[8,59,37,1,60,38],[8,44,20,4,45,21],[12,33,11,4,34,12],[3,145,115,1,146,116],[4,64,40,5,65,41],[11,36,16,5,37,17],[11,36,12,5,37,13],[5,109,87,1,110,88],[5,65,41,5,66,42],[5,54,24,7,55,25],[11,36,12],[5,122,98,1,123,99],[7,73,45,3,74,46],[15,43,19,2,44,20],[3,45,15,13,46,16],[1,135,107,5,136,108],[10,74,46,1,75,47],[1,50,22,15,51,23],[2,42,14,17,43,15],[5,150,120,1,151,121],[9,69,43,4,70,44],[17,50,22,1,51,23],[2,42,14,19,43,15],[3,141,113,4,142,114],[3,70,44,11,71,45],[17,47,21,4,48,22],[9,39,13,16,40,14],[3,135,107,5,136,108],[3,67,41,13,68,42],[15,54,24,5,55,25],[15,43,15,10,44,16],[4,144,116,4,145,117],[17,68,42],[17,50,22,6,51,23],[19,46,16,6,47,17],[2,139,111,7,140,112],[17,74,46],[7,54,24,16,55,25],[34,37,13],[4,151,121,5,152,122],[4,75,47,14,76,48],[11,54,24,14,55,25],[16,45,15,14,46,16],[6,147,117,4,148,118],[6,73,45,14,74,46],[11,54,24,16,55,25],[30,46,16,2,47,17],[8,132,106,4,133,107],[8,75,47,13,76,48],[7,54,24,22,55,25],[22,45,15,13,46,16],[10,142,114,2,143,115],[19,74,46,4,75,47],[28,50,22,6,51,23],[33,46,16,4,47,17],[8,152,122,4,153,123],[22,73,45,3,74,46],[8,53,23,26,54,24],[12,45,15,28,46,16],[3,147,117,10,148,118],[3,73,45,23,74,46],[4,54,24,31,55,25],[11,45,15,31,46,16],[7,146,116,7,147,117],[21,73,45,7,74,46],[1,53,23,37,54,24],[19,45,15,26,46,16],[5,145,115,10,146,116],[19,75,47,10,76,48],[15,54,24,25,55,25],[23,45,15,25,46,16],[13,145,115,3,146,116],[2,74,46,29,75,47],[42,54,24,1,55,25],[23,45,15,28,46,16],[17,145,115],[10,74,46,23,75,47],[10,54,24,35,55,25],[19,45,15,35,46,16],[17,145,115,1,146,116],[14,74,46,21,75,47],[29,54,24,19,55,25],[11,45,15,46,46,16],[13,145,115,6,146,116],[14,74,46,23,75,47],[44,54,24,7,55,25],[59,46,16,1,47,17],[12,151,121,7,152,122],[12,75,47,26,76,48],[39,54,24,14,55,25],[22,45,15,41,46,16],[6,151,121,14,152,122],[6,75,47,34,76,48],[46,54,24,10,55,25],[2,45,15,64,46,16],[17,152,122,4,153,123],[29,74,46,14,75,47],[49,54,24,10,55,25],[24,45,15,46,46,16],[4,152,122,18,153,123],[13,74,46,32,75,47],[48,54,24,14,55,25],[42,45,15,32,46,16],[20,147,117,4,148,118],[40,75,47,7,76,48],[43,54,24,22,55,25],[10,45,15,67,46,16],[19,148,118,6,149,119],[18,75,47,31,76,48],[34,54,24,34,55,25],[20,45,15,61,46,16]];QRRSBlock.getRSBlocks=function(a,b){var c=QRRSBlock.getRsBlockTable(a,b);if(c==undefined){throw new Error("bad rs block @ typeNumber:"+a+"/errorCorrectLevel:"+b)}var d=c.length/3;var e=new Array;for(var f=0;f<d;f++){var g=c[f*3+0];var h=c[f*3+1];var i=c[f*3+2];for(var j=0;j<g;j++){e.push(new QRRSBlock(h,i))}}return e};QRRSBlock.getRsBlockTable=function(a,b){switch(b){case QRErrorCorrectLevel.L:return QRRSBlock.RS_BLOCK_TABLE[(a-1)*4+0];case QRErrorCorrectLevel.M:return QRRSBlock.RS_BLOCK_TABLE[(a-1)*4+1];case QRErrorCorrectLevel.Q:return QRRSBlock.RS_BLOCK_TABLE[(a-1)*4+2];case QRErrorCorrectLevel.H:return QRRSBlock.RS_BLOCK_TABLE[(a-1)*4+3];default:return undefined}};QRBitBuffer.prototype={get:function(a){var b=Math.floor(a/8);return(this.buffer[b]>>>7-a%8&1)==1},put:function(a,b){for(var c=0;c<b;c++){this.putBit((a>>>b-c-1&1)==1)}},getLengthInBits:function(){return this.length},putBit:function(a){var b=Math.floor(this.length/8);if(this.buffer.length<=b){this.buffer.push(0)}if(a){this.buffer[b]|=128>>>this.length%8}this.length++}};Element.addMethods({qrcode:function(a,b){if(Object.isString(b))b={text:b};b=Object.extend({render:"canvas",width:256,height:256,typeNumber:-1,correctLevel:QRErrorCorrectLevel.H,background:"#ffffff",foreground:"#000000"},b);var c=function(){var a=new QRCode(b.typeNumber,b.correctLevel);a.addData(b.text);a.make();var c=document.createElement("canvas");c.width=b.width;c.height=b.height;var d=c.getContext("2d");var e=b.width/a.getModuleCount();var f=b.height/a.getModuleCount();for(var g=0;g<a.getModuleCount();g++){for(var h=0;h<a.getModuleCount();h++){d.fillStyle=a.isDark(g,h)?b.foreground:b.background;var i=Math.ceil((h+1)*e)-Math.floor(h*e);var j=Math.ceil((g+1)*e)-Math.floor(g*e);d.fillRect(Math.round(h*e),Math.round(g*f),i,j)}}return c};var d=function(){var a=new QRCode(b.typeNumber,b.correctLevel);a.addData(b.text);a.make();var c=(new Element("table")).setStyle({width:b.width+"px",height:b.height+"px",border:"0px",borderCollapse:"collapse",backgroundColor:b.background});var d=b.width/a.getModuleCount();var e=b.height/a.getModuleCount();for(var f=0;f<a.getModuleCount();f++){c.insert($tr=new Element("tr",{style:{height:e+"px"}}));for(var g=0;g<a.getModuleCount();g++){$tr.insert((new Element("td")).setStyle({width:d+"px",backgroundColor:a.isDark(f,g)?b.foreground:b.background}))}}return c};a.insert($return=b.render=="canvas"?c():d());return $return}});