Skip to content

Commit 664db78

Browse files
committed
Add DEFLATE compression using zpipe : https://github.com/richardassar/zpipe
1 parent 48a9f7e commit 664db78

11 files changed

Lines changed: 14049 additions & 2 deletions

File tree

jspdf.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,18 +307,20 @@ var PubSub = function(context){
307307
@constructor
308308
@private
309309
*/
310-
function jsPDF(/** String */ orientation, /** String */ unit, /** String */ format){
310+
function jsPDF(/** String */ orientation, /** String */ unit, /** String */ format, /** Boolean **/ compressPdf){
311311

312312
// Default parameter values
313313
if (typeof orientation === 'undefined') orientation = 'p'
314314
else orientation = orientation.toString().toLowerCase()
315315
if (typeof unit === 'undefined') unit = 'mm'
316316
if (typeof format === 'undefined') format = 'a4'
317+
if (typeof compressPdf === 'undefined' && typeof zpipe === 'undefined') compressPdf = false
317318

318319
var format_as_string = format.toString().toLowerCase()
319320
, version = '20120619'
320321
, content = []
321322
, content_length = 0
323+
, compress = compressPdf
322324

323325
, pdfVersion = '1.3' // PDF Version
324326
, pageFormats = { // Size in pt of various paper formats
@@ -449,7 +451,13 @@ function jsPDF(/** String */ orientation, /** String */ unit, /** String */ form
449451
// Page content
450452
p = pages[n].join('\n')
451453
newObject()
452-
out('<</Length ' + p.length + '>>')
454+
if(compress){
455+
p = zpipe.deflate( p)
456+
out('<</Length ' + p.length +' /Filter [/FlateDecode]>>')
457+
}
458+
else{
459+
out('<</Length ' + p.length + '>>')
460+
}
453461
putStream(p)
454462
out('endobj')
455463
}

libs/zpipe/Makefile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
all: dist/zpipe.min.js
2+
3+
dist:
4+
mkdir -p dist
5+
6+
dist/zpipe.min.js: dist src/zpipe.js
7+
@echo "Building zpipe.min.js"
8+
cat src/header.js src/zpipe.js src/footer.js | java -jar ~/closure-compiler/compiler.jar > dist/zpipe.min.js
9+
10+
clean:
11+
rm -rf dist/
12+
13+
test: dist/zpipe.min.js
14+
@./node_modules/mocha/bin/mocha --reporter spec
15+
16+
.PHONY: test clean

libs/zpipe/README.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Z'PIPE!
2+
3+
zpipe is **not** a pipe.
4+
5+
!["Ceci n'est pas une pipe"](http://upload.wikimedia.org/wikipedia/en/thumb/b/b9/MagrittePipe.jpg/300px-MagrittePipe.jpg "Ceci n'est pas une pipe")
6+
7+
>The famous pipe. How people reproached me for it! And yet, could you stuff my pipe? No, it's just a representation, is it not? So if I had written on my picture "This is a pipe," I'd have been lying!
8+
9+
## About
10+
11+
zpipe exposes an interface to the [DEFLATE](http://www.ietf.org/rfc/rfc1951.txt) algorithm of the [ZLib](http://zlib.net/) compression library, it has been cross-compiled to JavaScript with [Emscripten](https://github.com/kripken/emscripten).
12+
13+
## Motivation
14+
15+
* Currently no compression API exposed in browsers
16+
* Help users suffering from poor upload bandwidth
17+
18+
## Usage
19+
20+
Regular `<script>` include ...
21+
22+
``` html
23+
<script type="text/javascript" src="zpipe.min.js"></script>
24+
25+
<script>
26+
var deflated = zpipe.deflate("the balloon");
27+
28+
var inflated = zpipe.inflate(deflated); // "the balloon"
29+
</script>
30+
```
31+
32+
With require() ...
33+
34+
``` js
35+
var zpipe = require("zpipe");
36+
37+
var deflated = zpipe.deflate("the balloon");
38+
39+
var inflated = zpipe.inflate(deflated); // "the balloon"
40+
```
41+
## Browser support
42+
43+
zpipe is supported in the following browsers:
44+
45+
* Internet Explorer 7+ (**Note**: Use [zpipe-native](https://github.com/richardassar/zpipe-native/))
46+
* Google Chrome
47+
* Mozilla Firefox
48+
* Opera
49+
* Safari
50+
51+
## Installation
52+
53+
Install the package with **npm**
54+
55+
$ npm install zpipe
56+
57+
and bundle it with **Browserify**.
58+
59+
$ browserify example.js -o bundle.js
60+
61+
Alternatively just add it to your **Ender** bundle.
62+
63+
$ ender add zpipe
64+
65+
## But it's so big!
66+
67+
Ok 201 KB for `zpipe.min.js` is big, however it comes in at **57.6 KB** gzipped. This is acceptable.
68+
69+
## Tests
70+
71+
Test against node zlib bindings:
72+
73+
$ make test
74+
75+
Run the test in the browser by pointing your browser to `test/test.html` and `test/test-native.html`.
76+
77+
## Character encoding
78+
79+
zpipe operates on octet strings only, multi-byte characters will have their high byte masked. If you want to handle multi-byte characters then you must convert your strings to UTF-8 prior to calling `deflate()` and then convert them back after calling `inflate()`
80+
81+
You could use [utf8](https://github.com/ryanmcgrath/node-utf8) or [jshashes](https://github.com/h2non/jsHashes)' `Helpers.utf8Encode()` function for this, for example.
82+
83+
## TODO
84+
85+
* Support stream compression through workers
86+
* Benchmarks

libs/zpipe/dist/zpipe.min.js

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

libs/zpipe/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require("./dist/zpipe.min.js");

libs/zpipe/package.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name" : "zpipe",
3+
"description" : "JavaScript port of ZLib DEFLATE for the browser",
4+
"keywords" : ["compression", "zlib", "deflate"],
5+
"version" : "1.0.2",
6+
"author" : "Richard Assar <richardassar@googlemail.com>",
7+
"main" : "./dist/zpipe.min.js",
8+
"scripts" : {
9+
"test" : "make test"
10+
},
11+
"repository" : {
12+
"type" : "git",
13+
"url" : "git://github.com/richardassar/zpipe"
14+
},
15+
"devDependencies" : {
16+
"mocha" : "*",
17+
"chai" : "*"
18+
}
19+
}

libs/zpipe/src/footer.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
function run(args) {
2+
if (Module["preRun"]) {
3+
Module["preRun"]();
4+
}
5+
6+
args = args || Module["arguments"];
7+
initRuntime();
8+
var ret = null;
9+
if (Module["_main"]) {
10+
ret = Module.callMain(args);
11+
exitRuntime();
12+
}
13+
14+
if (Module["postRun"]) {
15+
Module["postRun"]();
16+
}
17+
18+
return ret;
19+
};
20+
21+
Module['run'] = run;
22+
23+
// Make z'pipe!
24+
var old = context[name];
25+
26+
var zpipe = (function() {
27+
var data;
28+
var i;
29+
var output;
30+
31+
// Set up iostreams
32+
FS.init(function() {
33+
// Stdin
34+
if(i < data.length) {
35+
var val = data.charCodeAt(i) & 0xFF;
36+
37+
i++;
38+
39+
return val;
40+
} else {
41+
return null;
42+
}
43+
}, function(data) {
44+
// Stdout
45+
output += String.fromCharCode(data & 0xFF);
46+
});
47+
48+
// Export interface
49+
return {
50+
'inflate' : function(string) {
51+
//
52+
data = string;
53+
i = 0;
54+
output = '';
55+
56+
//
57+
if(run(['-d']) != 0) {
58+
throw new Error("Could not inflate string");
59+
}
60+
61+
return output;
62+
},
63+
'deflate' : function(string) {
64+
data = string;
65+
i = 0;
66+
output = '';
67+
68+
if(run([]) != 0) {
69+
throw new Error("Could not deflate string");
70+
}
71+
72+
return output;
73+
},
74+
'noConflict': function () {
75+
if(old === undefined) {
76+
delete context[name];
77+
} else {
78+
context[name] = old;
79+
}
80+
81+
return this;
82+
}
83+
};
84+
})();
85+
86+
return zpipe;
87+
88+
});

libs/zpipe/src/header.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
!function (name, context, definition) {
2+
if (typeof module !== 'undefined') module.exports = definition(name, context);
3+
else if (typeof define === 'function' && typeof define.amd === 'object') define(definition);
4+
else context[name] = definition(name, context);
5+
}("zpipe", this, function(name, context) {

0 commit comments

Comments
 (0)