@@ -138,8 +138,11 @@ var getJpegSize = function(imgData) {
138138 }
139139}
140140
141- jsPDFAPI . addImage = function ( imageData , format , x , y , w , h ) {
141+ jsPDFAPI . addImage = function ( imageData , format , x , y , w , h , alias ) {
142142 'use strict'
143+ var images = this . internal . collections [ namespace + 'images' ] ,
144+ cached_info ;
145+
143146 if ( typeof format === 'number' ) {
144147 var tmp = h ;
145148 h = w ;
@@ -148,8 +151,27 @@ jsPDFAPI.addImage = function(imageData, format, x, y, w, h) {
148151 x = format ;
149152 format = tmp || 'JPEG' ;
150153 }
151- if ( typeof imageData === 'string' && imageData . substr ( 0 , 14 ) === 'data:image/jpg' ) {
152- imageData = imageData . replace ( 'data:image/jpg' , 'data:image/jpeg' ) ;
154+ if ( typeof alias === 'undefined' ) {
155+ // TODO: Alias dynamic generation from imageData's checksum/hash
156+ }
157+
158+ if ( typeof imageData === 'string' ) {
159+ if ( imageData . charCodeAt ( 0 ) !== 0xff && imageData . substr ( 0 , 5 ) !== 'data:' ) {
160+ // This is neither raw jpeg-data nor a data uri; alias?
161+ if ( images ) {
162+
163+ for ( var e in images ) {
164+
165+ if ( imageData === images [ e ] . alias ) {
166+ cached_info = images [ e ] ;
167+ break ;
168+ }
169+ }
170+ }
171+
172+ } else if ( imageData . substr ( 0 , 14 ) === 'data:image/jpg' ) {
173+ imageData = imageData . replace ( 'data:image/jpg' , 'data:image/jpeg' ) ;
174+ }
153175 }
154176 if ( typeof imageData === 'object' && imageData . nodeType === 1 ) {
155177 var canvas = document . createElement ( 'canvas' ) ;
@@ -169,7 +191,6 @@ jsPDFAPI.addImage = function(imageData, format, x, y, w, h) {
169191 }
170192
171193 var imageIndex
172- , images = this . internal . collections [ namespace + 'images' ]
173194 , coord = this . internal . getCoordinateString
174195 , vcoord = this . internal . getVerticalCoordinateString ;
175196
@@ -195,19 +216,20 @@ jsPDFAPI.addImage = function(imageData, format, x, y, w, h) {
195216 this . internal . events . subscribe ( 'putXobjectDict' , putXObjectsDictCallback )
196217 }
197218
198- var dims = getJpegSize ( imageData ) ;
199- var info = {
200- w : dims [ 0 ] ,
201- h : dims [ 1 ] ,
202- cs : 'DeviceRGB' ,
203- bpc : 8 ,
204- f : 'DCTDecode' ,
205- i : imageIndex ,
206- data : imageData
207- // n: objectNumber will be added by putImage code
219+ var info = cached_info || ( function ( dims ) {
220+ return images [ imageIndex ] = {
221+ alias :alias ,
222+ w : dims [ 0 ] ,
223+ h : dims [ 1 ] ,
224+ cs : 'DeviceRGB' ,
225+ bpc : 8 ,
226+ f : 'DCTDecode' ,
227+ i : imageIndex ,
228+ data : imageData
229+ // n: objectNumber will be added by putImage code
230+ } ;
231+ } ) ( getJpegSize ( imageData ) ) ;
208232
209- } ;
210- images [ imageIndex ] = info
211233 if ( ! w && ! h ) {
212234 w = - 96 ;
213235 h = - 96 ;
0 commit comments