jspdf.plugin.addimage.min.js 8.1 KB

1
  1. (function(p){var j="addImage_",b=["jpeg","jpg","png"];var t=function(A){var B=this.internal.newObject(),y=this.internal.write,x=this.internal.putStream;A.n=B;y("<</Type /XObject");y("/Subtype /Image");y("/Width "+A.w);y("/Height "+A.h);if(A.cs===this.color_spaces.INDEXED){y("/ColorSpace [/Indexed /DeviceRGB "+(A.pal.length/3-1)+" "+("smask" in A?B+2:B+1)+" 0 R]")}else{y("/ColorSpace /"+A.cs);if(A.cs===this.color_spaces.DEVICE_CMYK){y("/Decode [1 0 1 0 1 0 1 0]")}}y("/BitsPerComponent "+A.bpc);if("f" in A){y("/Filter /"+A.f)}if("dp" in A){y("/DecodeParms <<"+A.dp+">>")}if("trns" in A&&A.trns.constructor==Array){var v="",z=0,C=A.trns.length;for(;z<C;z++){v+=(A.trns[z]+" "+A.trns[z]+" ")}y("/Mask ["+v+"]")}if("smask" in A){y("/SMask "+(B+1)+" 0 R")}y("/Length "+A.data.length+">>");x(A.data);y("endobj");if("smask" in A){var w="/Predictor 15 /Colors 1 /BitsPerComponent "+A.bpc+" /Columns "+A.w;var D={w:A.w,h:A.h,cs:"DeviceGray",bpc:A.bpc,dp:w,data:A.smask};if("f" in A){D.f=A.f}t.call(this,D)}if(A.cs===this.color_spaces.INDEXED){this.internal.newObject();y("<< /Length "+A.pal.length+">>");x(this.arrayBufferToBinaryString(new Uint8Array(A.pal)));y("endobj")}},c=function(){var v=this.internal.collections[j+"images"];for(var w in v){t.call(this,v[w])}},f=function(){var v=this.internal.collections[j+"images"],w=this.internal.write,y;for(var x in v){y=v[x];w("/I"+y.i,y.n,"0","R")}},r=function(v){if(v&&typeof v==="string"){v=v.toUpperCase()}return v in p.image_compression?v:p.image_compression.NONE},g=function(){var v=this.internal.collections[j+"images"];if(!v){this.internal.collections[j+"images"]=v={};this.internal.events.subscribe("putResources",c);this.internal.events.subscribe("putXobjectDict",f)}return v},e=function(w){var v=0;if(w){v=Object.keys?Object.keys(w).length:(function(z){var x=0;for(var y in z){if(z.hasOwnProperty(y)){x++}}return x})(w)}return v},s=function(v){return typeof v==="undefined"||v===null},o=function(v){return undefined},a=function(v){return b.indexOf(v)===-1},i=function(v){return typeof p["process"+v.toUpperCase()]!=="function"},k=function(v){return typeof v==="object"&&v.nodeType===1},u=function(x,y){if(x.nodeName==="IMG"&&x.hasAttribute("src")){var z=""+x.getAttribute("src");if(z.indexOf("data:image/")===0){return z}if(!y&&/\.png(?:[?#].*)?$/i.test(z)){y="png"}}if(x.nodeName==="CANVAS"){var w=x}else{var w=document.createElement("canvas");w.width=x.clientWidth||x.width;w.height=x.clientHeight||x.height;var v=w.getContext("2d");if(!v){throw ("addImage requires canvas to be supported by browser.")}v.drawImage(x,0,0,w.width,w.height)}return w.toDataURL((""+y).toLowerCase()=="png"?"image/png":"image/jpeg")},m=function(y,w){var v;if(w){for(var x in w){if(y===w[x].alias){v=w[x];break}}}return v},n=function(v,x,y){if(!v&&!x){v=-96;x=-96}if(v<0){v=(-1)*y.w*72/v/this.internal.scaleFactor}if(x<0){x=(-1)*y.h*72/x/this.internal.scaleFactor}if(v===0){v=x*y.w/y.h}if(x===0){x=v*y.h/y.w}return[v,x]},d=function(F,E,H,z,v,B,C){var G=n.call(this,H,z,v),A=this.internal.getCoordinateString,D=this.internal.getVerticalCoordinateString;H=G[0];z=G[1];C[B]=v;this.internal.write("q",A(H),"0 0",A(z),A(F),D(E+z),"cm /I"+v.i,"Do Q")};p.color_spaces={DEVICE_RGB:"DeviceRGB",DEVICE_GRAY:"DeviceGray",DEVICE_CMYK:"DeviceCMYK",CAL_GREY:"CalGray",CAL_RGB:"CalRGB",LAB:"Lab",ICC_BASED:"ICCBased",INDEXED:"Indexed",PATTERN:"Pattern",SEPERATION:"Seperation",DEVICE_N:"DeviceN"};p.decode={DCT_DECODE:"DCTDecode",FLATE_DECODE:"FlateDecode",LZW_DECODE:"LZWDecode",JPX_DECODE:"JPXDecode",JBIG2_DECODE:"JBIG2Decode",ASCII85_DECODE:"ASCII85Decode",ASCII_HEX_DECODE:"ASCIIHexDecode",RUN_LENGTH_DECODE:"RunLengthDecode",CCITT_FAX_DECODE:"CCITTFaxDecode"};p.image_compression={NONE:"NONE",FAST:"FAST",MEDIUM:"MEDIUM",SLOW:"SLOW"};p.isString=function(v){return typeof v==="string"};p.extractInfoFromBase64DataURI=function(v){return/^data:([\w]+?\/([\w]+?));base64,(.+?)$/g.exec(v)};p.supportsArrayBuffer=function(){return typeof ArrayBuffer!=="undefined"&&typeof Uint8Array!=="undefined"};p.isArrayBuffer=function(v){if(!this.supportsArrayBuffer()){return false}return v instanceof ArrayBuffer};p.isArrayBufferView=function(v){if(!this.supportsArrayBuffer()){return false}if(typeof Uint32Array==="undefined"){return false}return(v instanceof Int8Array||v instanceof Uint8Array||(typeof Uint8ClampedArray!=="undefined"&&v instanceof Uint8ClampedArray)||v instanceof Int16Array||v instanceof Uint16Array||v instanceof Int32Array||v instanceof Uint32Array||v instanceof Float32Array||v instanceof Float64Array)};p.binaryStringToUint8Array=function(x){var v=x.length;var w=new Uint8Array(v);for(var y=0;y<v;y++){w[y]=x.charCodeAt(y)}return w};p.arrayBufferToBinaryString=function(w){if(this.isArrayBuffer(w)){w=new Uint8Array(w)}var x="";var v=w.byteLength;for(var y=0;y<v;y++){x+=String.fromCharCode(w[y])}return x};p.arrayBufferToBase64=function(y){var x="";var A="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";var H=new Uint8Array(y);var F=H.byteLength;var w=F%3;var G=F-w;var E,C,B,z;var D;for(var v=0;v<G;v=v+3){D=(H[v]<<16)|(H[v+1]<<8)|H[v+2];E=(D&16515072)>>18;C=(D&258048)>>12;B=(D&4032)>>6;z=D&63;x+=A[E]+A[C]+A[B]+A[z]}if(w==1){D=H[G];E=(D&252)>>2;C=(D&3)<<4;x+=A[E]+A[C]+"=="}else{if(w==2){D=(H[G]<<8)|H[G+1];E=(D&64512)>>10;C=(D&1008)>>4;B=(D&15)<<2;x+=A[E]+A[C]+A[B]+"="}}return x};p.createImageInfo=function(B,z,F,E,v,D,w,C,A,y,G,H){var x={alias:C,w:z,h:F,cs:E,bpc:v,i:w,data:B};if(D){x.f=D}if(A){x.dp=A}if(y){x.trns=y}if(G){x.pal=G}if(H){x.smask=H}return x};p.addImage=function(v,H,G,F,I,C,D,K){if(typeof H==="number"){var B=C;C=I;I=F;F=G;G=H;H=B}var E=g.call(this),J;K=r(K);if(s(D)){D=o(v)}if(k(v)){v=u(v,H)}if(this.isString(v)){var L=this.extractInfoFromBase64DataURI(v);if(L){H=L[2];v=atob(L[3])}else{if(imgData.charCodeAt(0)===137&&imgData.charCodeAt(1)===80&&imgData.charCodeAt(2)===78&&imgData.charCodeAt(3)===71){H="png"}}}H=(H||"JPEG").toLowerCase();if(a(H)){throw new Error("addImage currently only supports formats "+b+", not '"+H+"'")}if(i(H)){throw new Error("please ensure that the plugin for '"+H+"' support is added")}if(this.supportsArrayBuffer()){J=v;v=this.binaryStringToUint8Array(v)}var z=e(E),A=m(J||v,E);if(!A){A=this["process"+H.toUpperCase()](v,z,D,K,J)}if(!A){throw new Error("An unkwown error occurred whilst processing the image")}d.call(this,G,F,I,C,A,z,E);return this};var h=function(B){var z,w,A;if(!B.charCodeAt(0)===255||!B.charCodeAt(1)===216||!B.charCodeAt(2)===255||!B.charCodeAt(3)===224||!B.charCodeAt(6)==="J".charCodeAt(0)||!B.charCodeAt(7)==="F".charCodeAt(0)||!B.charCodeAt(8)==="I".charCodeAt(0)||!B.charCodeAt(9)==="F".charCodeAt(0)||!B.charCodeAt(10)===0){throw new Error("getJpegSize requires a binary string jpeg file")}var x=B.charCodeAt(4)*256+B.charCodeAt(5);var y=4,v=B.length;while(y<v){y+=x;if(B.charCodeAt(y)!==255){throw new Error("getJpegSize could not find the size of the image")}if(B.charCodeAt(y+1)===192||B.charCodeAt(y+1)===193||B.charCodeAt(y+1)===194||B.charCodeAt(y+1)===195||B.charCodeAt(y+1)===196||B.charCodeAt(y+1)===197||B.charCodeAt(y+1)===198||B.charCodeAt(y+1)===199){w=B.charCodeAt(y+5)*256+B.charCodeAt(y+6);z=B.charCodeAt(y+7)*256+B.charCodeAt(y+8);A=B.charCodeAt(y+9);return[z,w,A]}else{y+=2;x=B.charCodeAt(y)*256+B.charCodeAt(y+1)}}},q=function(y){var B=(y[0]<<8)|y[1];if(B!==65496){throw new Error("Supplied data is not a JPEG")}var z=y.length,x=(y[4]<<8)+y[5],A=4,D,v,C,w;while(A<z){A+=x;D=l(y,A);x=(D[2]<<8)+D[3];if((D[1]===192||D[1]===194)&&D[0]===255&&x>7){D=l(y,A+5);v=(D[2]<<8)+D[3];C=(D[0]<<8)+D[1];w=D[4];return{width:v,height:C,numcomponents:w}}A+=2}throw new Error("getJpegSizeFromBytes could not find the size of the image")},l=function(v,w){return v.subarray(w,w+5)};p.processJPEG=function(y,A,z,D,C){var x=this.color_spaces.DEVICE_RGB,w=this.decode.DCT_DECODE,v=8,B;if(this.isString(y)){B=h(y);return this.createImageInfo(y,B[0],B[1],B[3]==1?this.color_spaces.DEVICE_GRAY:x,v,w,A,z)}if(this.isArrayBuffer(y)){y=new Uint8Array(y)}if(this.isArrayBufferView(y)){B=q(y);y=C||this.arrayBufferToBinaryString(y);return this.createImageInfo(y,B.width,B.height,B.numcomponents==1?this.color_spaces.DEVICE_GRAY:x,v,w,A,z)}return null};p.processJPG=function(){return this.processJPEG.apply(this,arguments)}})(jsPDF.API);