wired up decompression, not working yet

This commit is contained in:
Andreas Gal 2011-05-11 03:26:33 -07:00
parent cebd567fa1
commit 9543128796

73
pdf.js
View File

@ -23,9 +23,6 @@ var Stream = (function() {
get length() { get length() {
return this.bytes.length; return this.bytes.length;
}, },
reset: function() {
this.pos = this.start;
},
getByte: function() { getByte: function() {
var bytes = this.bytes; var bytes = this.bytes;
if (this.pos >= bytes.length) if (this.pos >= bytes.length)
@ -50,6 +47,9 @@ var Stream = (function() {
n = 1; n = 1;
this.pos += n; this.pos += n;
}, },
reset: function() {
this.pos = this.start;
},
moveStart: function() { moveStart: function() {
this.start = this.pos; this.start = this.pos;
}, },
@ -667,29 +667,26 @@ var FlateStream = (function() {
[0, 0x0000] [0, 0x0000]
], 5]; ], 5];
function constructor() { function constructor(stream) {
this.reset(); this.stream = stream;
this.eof = true;
var cmf = stream.getByte();
var flg = stream.getByte();
if (cmf == -1 || flg == -1)
error("Invalid header in flate stream");
if ((cmf & 0x0f) != 0x08)
error("Unknown compression method in flate stream");
if ((((cmf << 8) + flg) % 31) != 0)
error("Bad FCHECK in flate stream");
if (flg & 0x20)
error("FDICT bit set in flate stream");
this.eof = false;
this.codeSize = 0;
this.codeBuf = 0;
this.pos = 0;
} }
constructor.prototype = { constructor.prototype = {
reset: function() {
stream.reset();
this.eof = true;
var cmf = stream.getByte();
var flg = stream.getByte();
if (cmf == -1 || flg == -1)
return;
if ((cmd & 0x0f) != 0x08)
error("Unknown compression method in flate stream");
if ((((cmf << 8) + flg) % 31) != 0)
error("Bad FCHECK in flate stream");
if (flg & 0x20)
error("FDICT bit set in flate stream");
this.eof = false;
this.codeSize = 0;
this.codeBuf = 0;
this.pos = 0;
},
getBits: function(bits) { getBits: function(bits) {
var stream = this.stream; var stream = this.stream;
var codeSize = this.codeSize; var codeSize = this.codeSize;
@ -707,18 +704,19 @@ var FlateStream = (function() {
return b; return b;
}, },
getCode: function(table) { getCode: function(table) {
var codes = table.codes; var codes = table[0];
var maxLen = table.maxLen; var maxLen = table[1];
var codeSize = this.codeSize; var codeSize = this.codeSize;
var codeBuf = this.codeBuf; var codeBuf = this.codeBuf;
while (codeSize < maxlen) { var stream = this.stream;
while (codeSize < maxLen) {
var b; var b;
if ((b = stream.getByte()) == -1) if ((b = stream.getByte()) == -1)
error("Bad encoding in flate stream"); error("Bad encoding in flate stream");
codeBuf |= (b << codeSize); codeBuf |= (b << codeSize);
codeSize += 8; codeSize += 8;
} }
var code = table.codes[codeBuf & ((1 << table.maxLen) - 1)]; var code = codes[codeBuf & ((1 << maxLen) - 1)];
var codeLen = code[0]; var codeLen = code[0];
var codeVal = code[1]; var codeVal = code[1];
if (codeSize == 0|| codeSize < codeLen || codeLen == 0) if (codeSize == 0|| codeSize < codeLen || codeLen == 0)
@ -777,7 +775,7 @@ var FlateStream = (function() {
} }
// build the table // build the table
var size = 1 << max; var size = 1 << maxLen;
var codes = new Array(size); var codes = new Array(size);
for (var len = 1, code = 0, skip = 2; for (var len = 1, code = 0, skip = 2;
len < maxLen; len < maxLen;
@ -801,7 +799,7 @@ var FlateStream = (function() {
} }
} }
return { codes: codes, maxLen: maxLen }; return [codes, maxLen];
}, },
readBlock: function() { readBlock: function() {
var stream = this.stream; var stream = this.stream;
@ -879,7 +877,7 @@ var FlateStream = (function() {
} else if (code == 18) { } else if (code == 18) {
repeat(codeLengths, i, 7, 11, len = 0); repeat(codeLengths, i, 7, 11, len = 0);
} else { } else {
codeLenths[i++] = len = code; codeLengths[i++] = len = code;
} }
} }
@ -1023,7 +1021,7 @@ function IsArray(v) {
} }
function IsStream(v) { function IsStream(v) {
return v instanceof Stream; return typeof v == "object" && "getChar" in v;
} }
function IsRef(v) { function IsRef(v) {
@ -1497,10 +1495,13 @@ var Parser = (function() {
return stream; return stream;
}, },
makeFilter: function(stream, name, params) { makeFilter: function(stream, name, params) {
print(name); if (name == "FlateDecode" || name == "Fl") {
if (params) if (params)
error("filter params not supported yet"); error("params not supported yet for FlateDecode");
// TODO return new FlateStream(stream);
} else {
error("filter '" + name + "' not supported yet");
}
return stream; return stream;
} }
}; };
@ -2721,6 +2722,8 @@ function runParseTests() {
var pdf = new PDFDoc(new Stream(data)); var pdf = new PDFDoc(new Stream(data));
var page = pdf.getPage(1); var page = pdf.getPage(1);
var contents = page.contents; var contents = page.contents;
for (var i = 0; i < 100; ++i)
print(contents.getChar());
} }
if ("arguments" in this) { if ("arguments" in this) {