Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1 | espaco | 1 | /** @preserve |
| 2 | jsPDF addImage plugin (JPEG only at this time) |
||
| 3 | Copyright (c) 2012 https://github.com/siefkenj/ |
||
| 4 | */ |
||
| 5 | |||
| 6 | /** |
||
| 7 | * Permission is hereby granted, free of charge, to any person obtaining |
||
| 8 | * a copy of this software and associated documentation files (the |
||
| 9 | * "Software"), to deal in the Software without restriction, including |
||
| 10 | * without limitation the rights to use, copy, modify, merge, publish, |
||
| 11 | * distribute, sublicense, and/or sell copies of the Software, and to |
||
| 12 | * permit persons to whom the Software is furnished to do so, subject to |
||
| 13 | * the following conditions: |
||
| 14 | * |
||
| 15 | * The above copyright notice and this permission notice shall be |
||
| 16 | * included in all copies or substantial portions of the Software. |
||
| 17 | * |
||
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
||
| 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
||
| 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
||
| 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
||
| 22 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
||
| 23 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
||
| 24 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
||
| 25 | * ==================================================================== |
||
| 26 | */ |
||
| 27 | |||
| 28 | ;(function(jsPDFAPI) { |
||
| 29 | 'use strict' |
||
| 30 | |||
| 31 | var namespace = 'addImage_' |
||
| 32 | |||
| 33 | // takes a string imgData containing the raw bytes of |
||
| 34 | // a jpeg image and returns [width, height] |
||
| 35 | // Algorithm from: http://www.64lines.com/jpeg-width-height |
||
| 36 | var getJpegSize = function(imgData) { |
||
| 37 | 'use strict' |
||
| 38 | var width, height; |
||
| 39 | // Verify we have a valid jpeg header 0xff,0xd8,0xff,0xe0,?,?,'J','F','I','F',0x00 |
||
| 40 | if (!imgData.charCodeAt(0) === 0xff || |
||
| 41 | !imgData.charCodeAt(1) === 0xd8 || |
||
| 42 | !imgData.charCodeAt(2) === 0xff || |
||
| 43 | !imgData.charCodeAt(3) === 0xe0 || |
||
| 44 | !imgData.charCodeAt(6) === 'J'.charCodeAt(0) || |
||
| 45 | !imgData.charCodeAt(7) === 'F'.charCodeAt(0) || |
||
| 46 | !imgData.charCodeAt(8) === 'I'.charCodeAt(0) || |
||
| 47 | !imgData.charCodeAt(9) === 'F'.charCodeAt(0) || |
||
| 48 | !imgData.charCodeAt(10) === 0x00) { |
||
| 49 | throw new Error('getJpegSize requires a binary jpeg file') |
||
| 50 | } |
||
| 51 | var blockLength = imgData.charCodeAt(4)*256 + imgData.charCodeAt(5); |
||
| 52 | var i = 4, len = imgData.length; |
||
| 53 | while ( i < len ) { |
||
| 54 | i += blockLength; |
||
| 55 | if (imgData.charCodeAt(i) !== 0xff) { |
||
| 56 | throw new Error('getJpegSize could not find the size of the image'); |
||
| 57 | } |
||
| 58 | if (imgData.charCodeAt(i+1) === 0xc0 || //(SOF) Huffman - Baseline DCT |
||
| 59 | imgData.charCodeAt(i+1) === 0xc1 || //(SOF) Huffman - Extended sequential DCT |
||
| 60 | imgData.charCodeAt(i+1) === 0xc2 || // Progressive DCT (SOF2) |
||
| 61 | imgData.charCodeAt(i+1) === 0xc3 || // Spatial (sequential) lossless (SOF3) |
||
| 62 | imgData.charCodeAt(i+1) === 0xc4 || // Differential sequential DCT (SOF5) |
||
| 63 | imgData.charCodeAt(i+1) === 0xc5 || // Differential progressive DCT (SOF6) |
||
| 64 | imgData.charCodeAt(i+1) === 0xc6 || // Differential spatial (SOF7) |
||
| 65 | imgData.charCodeAt(i+1) === 0xc7) { |
||
| 66 | height = imgData.charCodeAt(i+5)*256 + imgData.charCodeAt(i+6); |
||
| 67 | width = imgData.charCodeAt(i+7)*256 + imgData.charCodeAt(i+8); |
||
| 68 | return [width, height]; |
||
| 69 | } else { |
||
| 70 | i += 2; |
||
| 71 | blockLength = imgData.charCodeAt(i)*256 + imgData.charCodeAt(i+1) |
||
| 72 | } |
||
| 73 | } |
||
| 74 | } |
||
| 75 | // Image functionality ported from pdf.js |
||
| 76 | , putImage = function(img) { |
||
| 77 | var objectNumber = this.internal.newObject() |
||
| 78 | , out = this.internal.write |
||
| 79 | , putStream = this.internal.putStream |
||
| 80 | |||
| 81 | img['n'] = objectNumber |
||
| 82 | |||
| 83 | out('<</Type /XObject') |
||
| 84 | out('/Subtype /Image') |
||
| 85 | out('/Width ' + img['w']) |
||
| 86 | out('/Height ' + img['h']) |
||
| 87 | if (img['cs'] === 'Indexed') { |
||
| 88 | out('/ColorSpace [/Indexed /DeviceRGB ' |
||
| 89 | + (img['pal'].length / 3 - 1) + ' ' + (objectNumber + 1) |
||
| 90 | + ' 0 R]'); |
||
| 91 | } else { |
||
| 92 | out('/ColorSpace /' + img['cs']); |
||
| 93 | if (img['cs'] === 'DeviceCMYK') { |
||
| 94 | out('/Decode [1 0 1 0 1 0 1 0]'); |
||
| 95 | } |
||
| 96 | } |
||
| 97 | out('/BitsPerComponent ' + img['bpc']); |
||
| 98 | if ('f' in img) { |
||
| 99 | out('/Filter /' + img['f']); |
||
| 100 | } |
||
| 101 | if ('dp' in img) { |
||
| 102 | out('/DecodeParms <<' + img['dp'] + '>>'); |
||
| 103 | } |
||
| 104 | if ('trns' in img && img['trns'].constructor == Array) { |
||
| 105 | var trns = ''; |
||
| 106 | for ( var i = 0; i < img['trns'].length; i++) { |
||
| 107 | trns += (img[trns][i] + ' ' + img['trns'][i] + ' '); |
||
| 108 | out('/Mask [' + trns + ']'); |
||
| 109 | } |
||
| 110 | } |
||
| 111 | if ('smask' in img) { |
||
| 112 | out('/SMask ' + (objectNumber + 1) + ' 0 R'); |
||
| 113 | } |
||
| 114 | out('/Length ' + img['data'].length + '>>'); |
||
| 115 | |||
| 116 | putStream(img['data']); |
||
| 117 | |||
| 118 | out('endobj'); |
||
| 119 | } |
||
| 120 | , putResourcesCallback = function() { |
||
| 121 | var images = this.internal.collections[namespace + 'images'] |
||
| 122 | for ( var i in images ) { |
||
| 123 | putImage.call(this, images[i]) |
||
| 124 | } |
||
| 125 | } |
||
| 126 | , putXObjectsDictCallback = function(){ |
||
| 127 | var images = this.internal.collections[namespace + 'images'] |
||
| 128 | , out = this.internal.write |
||
| 129 | , image |
||
| 130 | for (var i in images) { |
||
| 131 | image = images[i] |
||
| 132 | out( |
||
| 133 | '/I' + image['i'] |
||
| 134 | , image['n'] |
||
| 135 | , '0' |
||
| 136 | , 'R' |
||
| 137 | ) |
||
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | jsPDFAPI.addImage = function(imageData, format, x, y, w, h) { |
||
| 142 | 'use strict' |
||
| 143 | if (typeof imageData === 'object' && imageData.nodeType === 1) { |
||
| 144 | var canvas = document.createElement('canvas'); |
||
| 145 | canvas.width = imageData.clientWidth; |
||
| 146 | canvas.height = imageData.clientHeight; |
||
| 147 | |||
| 148 | var ctx = canvas.getContext('2d'); |
||
| 149 | if (!ctx) { |
||
| 150 | throw ('addImage requires canvas to be supported by browser.'); |
||
| 151 | } |
||
| 152 | ctx.drawImage(imageData, 0, 0, canvas.width, canvas.height); |
||
| 153 | imageData = canvas.toDataURL('image/jpeg'); |
||
| 154 | format = "JPEG"; |
||
| 155 | } |
||
| 156 | if (format.toUpperCase() !== 'JPEG') { |
||
| 157 | throw new Error('addImage currently only supports format \'JPEG\', not \''+format+'\''); |
||
| 158 | } |
||
| 159 | |||
| 160 | var imageIndex |
||
| 161 | , images = this.internal.collections[namespace + 'images'] |
||
| 162 | , coord = this.internal.getCoordinateString |
||
| 163 | , vcoord = this.internal.getVerticalCoordinateString; |
||
| 164 | |||
| 165 | // Detect if the imageData is raw binary or Data URL |
||
| 166 | if (imageData.substring(0, 23) === 'data:image/jpeg;base64,') { |
||
| 167 | imageData = atob(imageData.replace('data:image/jpeg;base64,', '')); |
||
| 168 | } |
||
| 169 | |||
| 170 | if (images){ |
||
| 171 | // this is NOT the first time this method is ran on this instance of jsPDF object. |
||
| 172 | imageIndex = Object.keys ? |
||
| 173 | Object.keys(images).length : |
||
| 174 | (function(o){ |
||
| 175 | var i = 0 |
||
| 176 | for (var e in o){if(o.hasOwnProperty(e)){ i++ }} |
||
| 177 | return i |
||
| 178 | })(images) |
||
| 179 | } else { |
||
| 180 | // this is the first time this method is ran on this instance of jsPDF object. |
||
| 181 | imageIndex = 0 |
||
| 182 | this.internal.collections[namespace + 'images'] = images = {} |
||
| 183 | this.internal.events.subscribe('putResources', putResourcesCallback) |
||
| 184 | this.internal.events.subscribe('putXobjectDict', putXObjectsDictCallback) |
||
| 185 | } |
||
| 186 | |||
| 187 | var dims = getJpegSize(imageData); |
||
| 188 | var info = { |
||
| 189 | w : dims[0], |
||
| 190 | h : dims[1], |
||
| 191 | cs : 'DeviceRGB', |
||
| 192 | bpc : 8, |
||
| 193 | f : 'DCTDecode', |
||
| 194 | i : imageIndex, |
||
| 195 | data : imageData |
||
| 196 | // n: objectNumber will be added by putImage code |
||
| 197 | |||
| 198 | }; |
||
| 199 | images[imageIndex] = info |
||
| 200 | if (!w && !h) { |
||
| 201 | w = -96; |
||
| 202 | h = -96; |
||
| 203 | } |
||
| 204 | if (w < 0) { |
||
| 205 | w = (-1) * info['w'] * 72 / w / this.internal.scaleFactor; |
||
| 206 | } |
||
| 207 | if (h < 0) { |
||
| 208 | h = (-1) * info['h'] * 72 / h / this.internal.scaleFactor; |
||
| 209 | } |
||
| 210 | if (w === 0) { |
||
| 211 | w = h * info['w'] / info['h']; |
||
| 212 | } |
||
| 213 | if (h === 0) { |
||
| 214 | h = w * info['h'] / info['w']; |
||
| 215 | } |
||
| 216 | |||
| 217 | this.internal.write( |
||
| 218 | 'q' |
||
| 219 | , coord(w) |
||
| 220 | , '0 0' |
||
| 221 | , coord(h) // TODO: check if this should be shifted by vcoord |
||
| 222 | , coord(x) |
||
| 223 | , vcoord(y + h) |
||
| 224 | , 'cm /I'+info['i'] |
||
| 225 | , 'Do Q' |
||
| 226 | ) |
||
| 227 | |||
| 228 | return this |
||
| 229 | } |
||
| 230 | })(jsPDF.API) |