Skip to content

Commit d88c833

Browse files
committed
addImage: Implemented generateAliasFromData - Related to parallax#99 and parallax#300
1 parent 8f9d6cd commit d88c833

6 files changed

Lines changed: 145 additions & 106 deletions

File tree

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jspdf",
3-
"version": "1.0.203",
3+
"version": "1.0.209",
44
"homepage": "https://github.com/mrrio/jspdf",
55
"description": "PDF Document creation from JavaScript",
66
"main": "dist/jspdf.min.js",

dist/jspdf.debug.js

Lines changed: 85 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/** @preserve
22
* jsPDF - PDF Document creation from JavaScript
3-
* Version 1.0.203-git Built on 2014-07-22T04:20
4-
* CommitID ed1c917abb
3+
* Version 1.0.209-git Built on 2014-07-28T16:27
4+
* CommitID 8f9d6cd5cb
55
*
66
* Copyright (c) 2010-2014 James Hall, https://github.com/MrRio/jsPDF
77
* 2010 Aaron Spike, https://github.com/acspike
@@ -1703,7 +1703,7 @@ var jsPDF = (function(global) {
17031703
* pdfdoc.mymethod() // <- !!!!!!
17041704
*/
17051705
jsPDF.API = {events:[]};
1706-
jsPDF.version = "1.0.203-debug 2014-07-22T04:20:diegocr";
1706+
jsPDF.version = "1.0.209-debug 2014-07-28T16:27:diegocr";
17071707

17081708
if (typeof define === 'function' && define.amd) {
17091709
define('jsPDF', function() {
@@ -1990,8 +1990,8 @@ var jsPDF = (function(global) {
19901990
return typeof value === 'undefined' || value === null;
19911991
}
19921992
, generateAliasFromData = function(data) {
1993-
// TODO: Alias dynamic generation from imageData's checksum/hash
1994-
return undefined;
1993+
return typeof data === 'string' && Array.prototype.reduce &&
1994+
data.split("").reduce(function(a,b){a=((a<<5)-a)+b.charCodeAt(0);return a&a},0);
19951995
}
19961996
, doesNotSupportImageType = function(type) {
19971997
return supported_image_types.indexOf(type) === -1;
@@ -2029,11 +2029,11 @@ var jsPDF = (function(global) {
20292029

20302030
return canvas.toDataURL((''+format).toLowerCase() == 'png' ? 'image/png' : 'image/jpeg');
20312031
}
2032-
,checkImagesForAlias = function(imageData, images) {
2032+
,checkImagesForAlias = function(alias, images) {
20332033
var cached_info;
20342034
if(images) {
20352035
for(var e in images) {
2036-
if(imageData === images[e].alias) {
2036+
if(alias === images[e].alias) {
20372037
cached_info = images[e];
20382038
break;
20392039
}
@@ -2321,61 +2321,67 @@ var jsPDF = (function(global) {
23212321
format = tmp;
23222322
}
23232323

2324-
var images = getImages.call(this),//initalises internals and events on first run
2325-
dataAsBinaryString;
2324+
var images = getImages.call(this), info;
23262325

2327-
compression = checkCompressValue(compression);
2326+
if (!(info = checkImagesForAlias(imageData, images))) {
2327+
var dataAsBinaryString;
23282328

2329-
if(notDefined(alias))
2330-
alias = generateAliasFromData(imageData);
2329+
if(isDOMElement(imageData))
2330+
imageData = createDataURIFromElement(imageData, format);
23312331

2332-
if(isDOMElement(imageData))
2333-
imageData = createDataURIFromElement(imageData, format);
2332+
if(notDefined(alias))
2333+
alias = generateAliasFromData(imageData);
23342334

2335-
if(this.isString(imageData)) {
2335+
if (!(info = checkImagesForAlias(alias, images))) {
23362336

2337-
var base64Info = this.extractInfoFromBase64DataURI(imageData);
2337+
if(this.isString(imageData)) {
23382338

2339-
if(base64Info) {
2339+
var base64Info = this.extractInfoFromBase64DataURI(imageData);
23402340

2341-
format = base64Info[2];
2342-
imageData = atob(base64Info[3]);//convert to binary string
2341+
if(base64Info) {
23432342

2344-
} else {
2343+
format = base64Info[2];
2344+
imageData = atob(base64Info[3]);//convert to binary string
23452345

2346-
if (imageData.charCodeAt(0) === 0x89 &&
2347-
imageData.charCodeAt(1) === 0x50 &&
2348-
imageData.charCodeAt(2) === 0x4e &&
2349-
imageData.charCodeAt(3) === 0x47 ) format = 'png';
2350-
}
2351-
}
2352-
format = (format || 'JPEG').toLowerCase();
2346+
} else {
23532347

2354-
if(doesNotSupportImageType(format))
2355-
throw new Error('addImage currently only supports formats ' + supported_image_types + ', not \''+format+'\'');
2348+
if (imageData.charCodeAt(0) === 0x89 &&
2349+
imageData.charCodeAt(1) === 0x50 &&
2350+
imageData.charCodeAt(2) === 0x4e &&
2351+
imageData.charCodeAt(3) === 0x47 ) format = 'png';
2352+
}
2353+
}
2354+
format = (format || 'JPEG').toLowerCase();
23562355

2357-
if(processMethodNotEnabled(format))
2358-
throw new Error('please ensure that the plugin for \''+format+'\' support is added');
2356+
if(doesNotSupportImageType(format))
2357+
throw new Error('addImage currently only supports formats ' + supported_image_types + ', not \''+format+'\'');
23592358

2360-
/*
2361-
* need to test if it's more efficent to convert all binary strings
2362-
* to TypedArray - or should we just leave and process as string?
2363-
*/
2364-
if(this.supportsArrayBuffer()) {
2365-
dataAsBinaryString = imageData;
2366-
imageData = this.binaryStringToUint8Array(imageData);
2367-
}
2359+
if(processMethodNotEnabled(format))
2360+
throw new Error('please ensure that the plugin for \''+format+'\' support is added');
23682361

2369-
var imageIndex = getImageIndex(images),
2370-
info = checkImagesForAlias(dataAsBinaryString || imageData, images);
2362+
/**
2363+
* need to test if it's more efficent to convert all binary strings
2364+
* to TypedArray - or should we just leave and process as string?
2365+
*/
2366+
if(this.supportsArrayBuffer()) {
2367+
dataAsBinaryString = imageData;
2368+
imageData = this.binaryStringToUint8Array(imageData);
2369+
}
23712370

2372-
if(!info)
2373-
info = this['process' + format.toUpperCase()](imageData, imageIndex, alias, compression, dataAsBinaryString);
2371+
info = this['process' + format.toUpperCase()](
2372+
imageData,
2373+
getImageIndex(images),
2374+
alias,
2375+
checkCompressValue(compression),
2376+
dataAsBinaryString
2377+
);
23742378

2375-
if(!info)
2376-
throw new Error('An unkwown error occurred whilst processing the image');
2379+
if(!info)
2380+
throw new Error('An unkwown error occurred whilst processing the image');
2381+
}
2382+
}
23772383

2378-
writeImageToPDF.call(this, x, y, w, h, info, imageIndex, images);
2384+
writeImageToPDF.call(this, x, y, w, h, info, info.i, images);
23792385

23802386
return this
23812387
};
@@ -5415,7 +5421,7 @@ jsPDFAPI.putTotalPages = function(pageExpression) {
54155421
})(jsPDF.API);
54165422
/* Blob.js
54175423
* A Blob implementation.
5418-
* 2014-07-01
5424+
* 2014-07-24
54195425
*
54205426
* By Eli Grey, http://eligrey.com
54215427
* By Devin Samarin, https://github.com/eboyjr
@@ -5477,13 +5483,31 @@ jsPDFAPI.putTotalPages = function(pageExpression) {
54775483

54785484
, ArrayBuffer = view.ArrayBuffer
54795485
, Uint8Array = view.Uint8Array
5486+
5487+
, origin = /^[\w-]+:\/*\[?[\w\.:-]+\]?(?::[0-9]+)?/
54805488
;
54815489
FakeBlob.fake = FB_proto.fake = true;
54825490
while (file_ex_code--) {
54835491
FileException.prototype[file_ex_codes[file_ex_code]] = file_ex_code + 1;
54845492
}
5493+
// Polyfill URL
54855494
if (!real_URL.createObjectURL) {
5486-
URL = view.URL = {};
5495+
URL = view.URL = function(uri) {
5496+
var
5497+
uri_info = document.createElementNS("http://www.w3.org/1999/xhtml", "a")
5498+
, uri_origin
5499+
;
5500+
uri_info.href = uri;
5501+
if (!("origin" in uri_info)) {
5502+
if (uri_info.protocol.toLowerCase() === "data:") {
5503+
uri_info.origin = null;
5504+
} else {
5505+
uri_origin = uri.match(origin);
5506+
uri_info.origin = uri_origin && uri_origin[1];
5507+
}
5508+
}
5509+
return uri_info;
5510+
};
54875511
}
54885512
URL.createObjectURL = function(blob) {
54895513
var
@@ -5594,7 +5618,7 @@ jsPDFAPI.putTotalPages = function(pageExpression) {
55945618
}(typeof self !== "undefined" && self || typeof window !== "undefined" && window || this.content || this));
55955619
/* FileSaver.js
55965620
* A saveAs() FileSaver implementation.
5597-
* 2014-07-21
5621+
* 2014-07-25
55985622
*
55995623
* By Eli Grey, http://eligrey.com
56005624
* License: X11/MIT
@@ -5647,13 +5671,18 @@ var saveAs = saveAs
56475671
// the reasoning behind the timeout and revocation flow
56485672
, arbitrary_revoke_timeout = 10
56495673
, revoke = function(file) {
5650-
setTimeout(function() {
5674+
var revoker = function() {
56515675
if (typeof file === "string") { // file is an object URL
56525676
get_URL().revokeObjectURL(file);
56535677
} else { // file is a File
56545678
file.remove();
56555679
}
5656-
}, arbitrary_revoke_timeout);
5680+
};
5681+
if (view.chrome) {
5682+
revoker();
5683+
} else {
5684+
setTimeout(revoker, arbitrary_revoke_timeout);
5685+
}
56575686
}
56585687
, dispatch = function(filesaver, event_types, event) {
56595688
event_types = [].concat(event_types);
@@ -5689,7 +5718,11 @@ var saveAs = saveAs
56895718
if (target_view) {
56905719
target_view.location.href = object_url;
56915720
} else {
5692-
view.open(object_url, "_blank");
5721+
var new_tab = view.open(object_url, "_blank");
5722+
if (new_tab == undefined && typeof safari !== "undefined") {
5723+
//Apple do not allow window.open, see http://bit.ly/1kZffRI
5724+
view.location.href = object_url
5725+
}
56935726
}
56945727
filesaver.readyState = filesaver.DONE;
56955728
dispatch_all();

dist/jspdf.min.js

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jspdf.plugin.addimage.js

Lines changed: 49 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@
158158
return typeof value === 'undefined' || value === null;
159159
}
160160
, generateAliasFromData = function(data) {
161-
// TODO: Alias dynamic generation from imageData's checksum/hash
162-
return undefined;
161+
return typeof data === 'string' && Array.prototype.reduce &&
162+
data.split("").reduce(function(a,b){a=((a<<5)-a)+b.charCodeAt(0);return a&a},0);
163163
}
164164
, doesNotSupportImageType = function(type) {
165165
return supported_image_types.indexOf(type) === -1;
@@ -197,11 +197,11 @@
197197

198198
return canvas.toDataURL((''+format).toLowerCase() == 'png' ? 'image/png' : 'image/jpeg');
199199
}
200-
,checkImagesForAlias = function(imageData, images) {
200+
,checkImagesForAlias = function(alias, images) {
201201
var cached_info;
202202
if(images) {
203203
for(var e in images) {
204-
if(imageData === images[e].alias) {
204+
if(alias === images[e].alias) {
205205
cached_info = images[e];
206206
break;
207207
}
@@ -489,61 +489,67 @@
489489
format = tmp;
490490
}
491491

492-
var images = getImages.call(this),//initalises internals and events on first run
493-
dataAsBinaryString;
492+
var images = getImages.call(this), info;
494493

495-
compression = checkCompressValue(compression);
494+
if (!(info = checkImagesForAlias(imageData, images))) {
495+
var dataAsBinaryString;
496496

497-
if(notDefined(alias))
498-
alias = generateAliasFromData(imageData);
497+
if(isDOMElement(imageData))
498+
imageData = createDataURIFromElement(imageData, format);
499499

500-
if(isDOMElement(imageData))
501-
imageData = createDataURIFromElement(imageData, format);
500+
if(notDefined(alias))
501+
alias = generateAliasFromData(imageData);
502502

503-
if(this.isString(imageData)) {
503+
if (!(info = checkImagesForAlias(alias, images))) {
504504

505-
var base64Info = this.extractInfoFromBase64DataURI(imageData);
505+
if(this.isString(imageData)) {
506506

507-
if(base64Info) {
507+
var base64Info = this.extractInfoFromBase64DataURI(imageData);
508508

509-
format = base64Info[2];
510-
imageData = atob(base64Info[3]);//convert to binary string
509+
if(base64Info) {
511510

512-
} else {
511+
format = base64Info[2];
512+
imageData = atob(base64Info[3]);//convert to binary string
513513

514-
if (imageData.charCodeAt(0) === 0x89 &&
515-
imageData.charCodeAt(1) === 0x50 &&
516-
imageData.charCodeAt(2) === 0x4e &&
517-
imageData.charCodeAt(3) === 0x47 ) format = 'png';
518-
}
519-
}
520-
format = (format || 'JPEG').toLowerCase();
514+
} else {
521515

522-
if(doesNotSupportImageType(format))
523-
throw new Error('addImage currently only supports formats ' + supported_image_types + ', not \''+format+'\'');
516+
if (imageData.charCodeAt(0) === 0x89 &&
517+
imageData.charCodeAt(1) === 0x50 &&
518+
imageData.charCodeAt(2) === 0x4e &&
519+
imageData.charCodeAt(3) === 0x47 ) format = 'png';
520+
}
521+
}
522+
format = (format || 'JPEG').toLowerCase();
524523

525-
if(processMethodNotEnabled(format))
526-
throw new Error('please ensure that the plugin for \''+format+'\' support is added');
524+
if(doesNotSupportImageType(format))
525+
throw new Error('addImage currently only supports formats ' + supported_image_types + ', not \''+format+'\'');
527526

528-
/*
529-
* need to test if it's more efficent to convert all binary strings
530-
* to TypedArray - or should we just leave and process as string?
531-
*/
532-
if(this.supportsArrayBuffer()) {
533-
dataAsBinaryString = imageData;
534-
imageData = this.binaryStringToUint8Array(imageData);
535-
}
527+
if(processMethodNotEnabled(format))
528+
throw new Error('please ensure that the plugin for \''+format+'\' support is added');
536529

537-
var imageIndex = getImageIndex(images),
538-
info = checkImagesForAlias(dataAsBinaryString || imageData, images);
530+
/**
531+
* need to test if it's more efficent to convert all binary strings
532+
* to TypedArray - or should we just leave and process as string?
533+
*/
534+
if(this.supportsArrayBuffer()) {
535+
dataAsBinaryString = imageData;
536+
imageData = this.binaryStringToUint8Array(imageData);
537+
}
539538

540-
if(!info)
541-
info = this['process' + format.toUpperCase()](imageData, imageIndex, alias, compression, dataAsBinaryString);
539+
info = this['process' + format.toUpperCase()](
540+
imageData,
541+
getImageIndex(images),
542+
alias,
543+
checkCompressValue(compression),
544+
dataAsBinaryString
545+
);
542546

543-
if(!info)
544-
throw new Error('An unkwown error occurred whilst processing the image');
547+
if(!info)
548+
throw new Error('An unkwown error occurred whilst processing the image');
549+
}
550+
}
545551

546-
writeImageToPDF.call(this, x, y, w, h, info, imageIndex, images);
552+
writeImageToPDF.call(this, x, y, w, h, info, info.i, images);
547553

548554
return this
549555
};

libs/Blob.js

Submodule Blob.js updated 1 file

libs/FileSaver.js

0 commit comments

Comments
 (0)