Skip to content

Commit 0948d14

Browse files
committed
Merge pull request parallax#82 from lifof/master
PDF compression with less fat
2 parents 13b4be8 + 70a6217 commit 0948d14

5 files changed

Lines changed: 2254 additions & 5 deletions

File tree

jspdf.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ PubSub implementation
431431

432432
// outToPages = false as set in endDocument(). out() writes to content.
433433

434-
var n, p;
434+
var n, p, arr, uint, i, deflater, adler32;
435435
for (n = 1; n <= page; n++) {
436436
newObject();
437437
out('<</Type /Page');
@@ -442,9 +442,24 @@ PubSub implementation
442442

443443
// Page content
444444
p = pages[n].join('\n');
445+
adler32 = adler32cs.from(p);
445446
newObject();
446447
if (compress) {
447-
p = zpipe.deflate(p);
448+
arr = [];
449+
for (i = 0; i < p.length; ++i) {
450+
arr[i] = p.charCodeAt(i);
451+
}
452+
deflater = new Deflater(6);
453+
deflater.append(new Uint8Array(arr));
454+
p = deflater.flush();
455+
arr = [new Uint8Array([120, 156]), new Uint8Array(p),
456+
new Uint8Array([adler32 & 0xFF, (adler32 >> 8) & 0xFF, (adler32 >> 16) & 0xFF, (adler32 >> 24) & 0xFF])];
457+
p = '';
458+
for (i in arr) {
459+
if (arr.hasOwnProperty(i)) {
460+
p += String.fromCharCode.apply(null, arr[i]);
461+
}
462+
}
448463
out('<</Length ' + p.length + ' /Filter [/FlateDecode]>>');
449464
} else {
450465
out('<</Length ' + p.length + '>>');
@@ -938,7 +953,7 @@ PubSub implementation
938953
case undef:
939954
return buildDocument();
940955
case 'save':
941-
if (navigator.getUserMedia || Blob.prototype.slice === undefined) {
956+
if (navigator.getUserMedia) {
942957
if (window.URL === undefined) {
943958
return API.output('dataurlnewwindow');
944959
} else if (window.URL.createObjectURL === undefined) {

libs/Blob.js

Submodule Blob.js updated 1 file

libs/Deflate/adler32cs.js

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
/*
2+
* Copyright (c) 2012 chick307 <chick307@gmail.com>
3+
*
4+
* Licensed under the MIT License.
5+
* http://opensource.org/licenses/mit-license
6+
*/
7+
8+
void function(global, callback) {
9+
if (typeof module === 'object') {
10+
module.exports = callback();
11+
} else if (typeof define === 'function') {
12+
define(callback);
13+
} else {
14+
global.adler32cs = callback();
15+
}
16+
}(this, function() {
17+
var _hasArrayBuffer = typeof ArrayBuffer === 'function' &&
18+
typeof Uint8Array === 'function';
19+
20+
var _Buffer = null, _isBuffer = (function() {
21+
if (!_hasArrayBuffer)
22+
return function _isBuffer() { return false };
23+
24+
try {
25+
var buffer = require('buffer');
26+
if (typeof buffer.Buffer === 'function')
27+
_Buffer = buffer.Buffer;
28+
} catch (error) {}
29+
30+
return function _isBuffer(value) {
31+
return value instanceof ArrayBuffer ||
32+
_Buffer !== null && value instanceof _Buffer;
33+
};
34+
}());
35+
36+
var _utf8ToBinary = (function() {
37+
if (_Buffer !== null) {
38+
return function _utf8ToBinary(utf8String) {
39+
return new _Buffer(utf8String, 'utf8').toString('binary');
40+
};
41+
} else {
42+
return function _utf8ToBinary(utf8String) {
43+
return unescape(encodeURIComponent(utf8String));
44+
};
45+
}
46+
}());
47+
48+
var MOD = 65521;
49+
50+
var _update = function _update(checksum, binaryString) {
51+
var a = checksum & 0xFFFF, b = checksum >>> 16;
52+
for (var i = 0, length = binaryString.length; i < length; i++) {
53+
a = (a + (binaryString.charCodeAt(i) & 0xFF)) % MOD;
54+
b = (b + a) % MOD;
55+
}
56+
return (b << 16 | a) >>> 0;
57+
};
58+
59+
var _updateUint8Array = function _updateUint8Array(checksum, uint8Array) {
60+
var a = checksum & 0xFFFF, b = checksum >>> 16;
61+
for (var i = 0, length = uint8Array.length, x; i < length; i++) {
62+
a = (a + uint8Array[i]) % MOD;
63+
b = (b + a) % MOD;
64+
}
65+
return (b << 16 | a) >>> 0
66+
};
67+
68+
var exports = {};
69+
70+
var Adler32 = exports.Adler32 = (function() {
71+
var ctor = function Adler32(checksum) {
72+
if (!(this instanceof ctor)) {
73+
throw new TypeError(
74+
'Constructor cannot called be as a function.');
75+
}
76+
if (!isFinite(checksum = checksum == null ? 1 : +checksum)) {
77+
throw new Error(
78+
'First arguments needs to be a finite number.');
79+
}
80+
this.checksum = checksum >>> 0;
81+
};
82+
83+
var proto = ctor.prototype = {};
84+
proto.constructor = ctor;
85+
86+
ctor.from = function(from) {
87+
from.prototype = proto;
88+
return from;
89+
}(function from(binaryString) {
90+
if (!(this instanceof ctor)) {
91+
throw new TypeError(
92+
'Constructor cannot called be as a function.');
93+
}
94+
if (binaryString == null)
95+
throw new Error('First argument needs to be a string.');
96+
this.checksum = _update(1, binaryString.toString());
97+
});
98+
99+
ctor.fromUtf8 = function(fromUtf8) {
100+
fromUtf8.prototype = proto;
101+
return fromUtf8;
102+
}(function fromUtf8(utf8String) {
103+
if (!(this instanceof ctor)) {
104+
throw new TypeError(
105+
'Constructor cannot called be as a function.');
106+
}
107+
if (utf8String == null)
108+
throw new Error('First argument needs to be a string.');
109+
var binaryString = _utf8ToBinary(utf8String.toString());
110+
this.checksum = _update(1, binaryString);
111+
});
112+
113+
if (_hasArrayBuffer) {
114+
ctor.fromBuffer = function(fromBuffer) {
115+
fromBuffer.prototype = proto;
116+
return fromBuffer;
117+
}(function fromBuffer(buffer) {
118+
if (!(this instanceof ctor)) {
119+
throw new TypeError(
120+
'Constructor cannot called be as a function.');
121+
}
122+
if (!_isBuffer(buffer))
123+
throw new Error('First argument needs to be ArrayBuffer.');
124+
var array = new Uint8Array(buffer);
125+
return this.checksum = _updateUint8Array(1, array);
126+
});
127+
}
128+
129+
proto.update = function update(binaryString) {
130+
if (binaryString == null)
131+
throw new Error('First argument needs to be a string.');
132+
binaryString = binaryString.toString();
133+
return this.checksum = _update(this.checksum, binaryString);
134+
};
135+
136+
proto.updateUtf8 = function updateUtf8(utf8String) {
137+
if (utf8String == null)
138+
throw new Error('First argument needs to be a string.');
139+
var binaryString = _utf8ToBinary(utf8String.toString());
140+
return this.checksum = _update(this.checksum, binaryString);
141+
};
142+
143+
if (_hasArrayBuffer) {
144+
proto.updateBuffer = function updateBuffer(buffer) {
145+
if (!_isBuffer(buffer))
146+
throw new Error('First argument needs to be ArrayBuffer.');
147+
var array = new Uint8Array(buffer);
148+
return this.checksum = _updateUint8Array(this.checksum, array);
149+
};
150+
}
151+
152+
proto.clone = function clone() {
153+
return new Adler32(this.checksum);
154+
};
155+
156+
return ctor;
157+
}());
158+
159+
exports.from = function from(binaryString) {
160+
if (binaryString == null)
161+
throw new Error('First argument needs to be a string.');
162+
return _update(1, binaryString.toString());
163+
};
164+
165+
exports.fromUtf8 = function fromUtf8(utf8String) {
166+
if (utf8String == null)
167+
throw new Error('First argument needs to be a string.');
168+
var binaryString = _utf8ToBinary(utf8String.toString());
169+
return _update(1, binaryString);
170+
};
171+
172+
if (_hasArrayBuffer) {
173+
exports.fromBuffer = function fromBuffer(buffer) {
174+
if (!_isBuffer(buffer))
175+
throw new Error('First argument need to be ArrayBuffer.');
176+
var array = new Uint8Array(buffer);
177+
return _updateUint8Array(1, array);
178+
};
179+
}
180+
181+
return exports;
182+
});

0 commit comments

Comments
 (0)