Skip to content

Commit

Permalink
Merge pull request #852 from MrRio/pr/801
Browse files Browse the repository at this point in the history
Pr/801 - Fix PNG compression
  • Loading branch information
MrRio authored Sep 26, 2016
2 parents 5acfc8c + e76f566 commit 35a9bbd
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 42 deletions.
48 changes: 34 additions & 14 deletions dist/jspdf.debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

/** @preserve
* jsPDF - PDF Document creation from JavaScript
* Version 1.2.61 Built on 2016-09-25T22:32:35.622Z
* CommitID 33aab1469e
* Version 1.2.61 Built on 2016-09-26T11:22:48.682Z
* CommitID bd60b2f4b9
*
* Copyright (c) 2010-2014 James Hall <james@parall.ax>, https://github.com/MrRio/jsPDF
* 2010 Aaron Spike, https://github.com/acspike
Expand Down Expand Up @@ -2026,7 +2026,7 @@
* pdfdoc.mymethod() // <- !!!!!!
*/
jsPDF.API = { events: [] };
jsPDF.version = "1.2.61 2016-09-25T22:32:35.622Z:jameshall";
jsPDF.version = "1.2.61 2016-09-26T11:22:48.682Z:jameshall";

if (typeof define === 'function' && define.amd) {
define('jsPDF', function () {
Expand Down Expand Up @@ -3862,7 +3862,7 @@ Q\n";

// Soft mask
if ('smask' in img) {
var dp = '/Predictor 15 /Colors 1 /BitsPerComponent ' + img['bpc'] + ' /Columns ' + img['w'];
var dp = '/Predictor ' + img['p'] + ' /Colors 1 /BitsPerComponent ' + img['bpc'] + ' /Columns ' + img['w'];
var smask = { 'w': img['w'], 'h': img['h'], 'cs': 'DeviceGray', 'bpc': img['bpc'], 'dp': dp, 'data': img['smask'] };
if ('f' in img) smask.f = img['f'];
putImage.call(this, smask);
Expand Down Expand Up @@ -4178,10 +4178,10 @@ Q\n";
* Async method using Blob and FileReader could be best, but i'm not sure how to fit it into the flow?
*/
jsPDFAPI.arrayBufferToBinaryString = function (buffer) {
if ('TextDecoder' in window) {
var decoder = new TextDecoder('ascii');
return decoder.decode(buffer);
}
/*if('TextDecoder' in window){
var decoder = new TextDecoder('ascii');
return decoder.decode(buffer);
}*/

if (this.isArrayBuffer(buffer)) buffer = new Uint8Array(buffer);

Expand Down Expand Up @@ -4259,7 +4259,7 @@ Q\n";
return base64;
};

jsPDFAPI.createImageInfo = function (data, wd, ht, cs, bpc, f, imageIndex, alias, dp, trns, pal, smask) {
jsPDFAPI.createImageInfo = function (data, wd, ht, cs, bpc, f, imageIndex, alias, dp, trns, pal, smask, p) {
var info = {
alias: alias,
w: wd,
Expand All @@ -4276,6 +4276,7 @@ Q\n";
if (trns) info.trns = trns;
if (pal) info.pal = pal;
if (smask) info.smask = smask;
if (p) info.p = p; // predictor parameter for PNG compression

return info;
};
Expand Down Expand Up @@ -7145,9 +7146,9 @@ Q\n";
value = listCount++ + '. ' + value;
} else {
var fontSize = fragmentCSS["font-size"];
offsetX = (3 - fontSize * 0.75) * renderer.pdf.internal.scaleFactor;
offsetY = fontSize * 0.75 * renderer.pdf.internal.scaleFactor;
radius = fontSize * 1.74 / renderer.pdf.internal.scaleFactor;
var offsetX = (3 - fontSize * 0.75) * renderer.pdf.internal.scaleFactor;
var offsetY = fontSize * 0.75 * renderer.pdf.internal.scaleFactor;
var radius = fontSize * 1.74 / renderer.pdf.internal.scaleFactor;
cb = function cb(x, y) {
this.pdf.circle(x + offsetX, y + offsetY, radius, 'FD');
};
Expand Down Expand Up @@ -8265,6 +8266,23 @@ Q\n";
while (i < len) {
sum += Math.abs(array[i++]);
}return sum;
},
getPredictorFromCompression = function getPredictorFromCompression(compression) {
var predictor;
switch (compression) {
case jsPDFAPI.image_compression.FAST:
predictor = 11;
break;

case jsPDFAPI.image_compression.MEDIUM:
predictor = 13;
break;

case jsPDFAPI.image_compression.SLOW:
predictor = 14;
break;
}
return predictor;
},
logImg = function logImg(img) {
console.log("width: " + img.width);
Expand Down Expand Up @@ -8446,15 +8464,17 @@ Q\n";
}
}

if (decode === this.decode.FLATE_DECODE) dp = '/Predictor 15 /Colors ' + colors + ' /BitsPerComponent ' + bpc + ' /Columns ' + img.width;else
var predictor = getPredictorFromCompression(compression);

if (decode === this.decode.FLATE_DECODE) dp = '/Predictor ' + predictor + ' /Colors ' + colors + ' /BitsPerComponent ' + bpc + ' /Columns ' + img.width;else
//remove 'Predictor' as it applies to the type of png filter applied to its IDAT - we only apply with compression
dp = '/Colors ' + colors + ' /BitsPerComponent ' + bpc + ' /Columns ' + img.width;

if (this.isArrayBuffer(imageData) || this.isArrayBufferView(imageData)) imageData = this.arrayBufferToBinaryString(imageData);

if (smask && this.isArrayBuffer(smask) || this.isArrayBufferView(smask)) smask = this.arrayBufferToBinaryString(smask);

return this.createImageInfo(imageData, img.width, img.height, colorSpace, bpc, decode, imageIndex, alias, dp, trns, pal, smask);
return this.createImageInfo(imageData, img.width, img.height, colorSpace, bpc, decode, imageIndex, alias, dp, trns, pal, smask, predictor);
}

throw new Error("Unsupported PNG image data, try using JPEG instead.");
Expand Down
Loading

0 comments on commit 35a9bbd

Please sign in to comment.