pdf.js/pdf.js

1442 lines
43 KiB
JavaScript
Raw Normal View History

/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- /
/* vim: set shiftwidth=4 tabstop=8 autoindent cindent expandtab: */
2011-05-03 14:50:55 +09:00
var Stream = (function() {
function constructor(arrayBuffer) {
this.bytes = Uint8Array(arrayBuffer);
this.pos = 0;
}
constructor.prototype = {
2011-05-06 17:16:09 +09:00
get length() {
return this.bytes.length;
},
2011-05-03 14:50:55 +09:00
reset: function() {
this.pos = 0;
},
lookChar: function() {
var bytes = this.bytes;
if (this.pos >= bytes.length)
2011-05-03 18:17:17 +09:00
return;
2011-05-03 14:50:55 +09:00
return String.fromCharCode(bytes[this.pos]);
},
getChar: function() {
var ch = this.lookChar();
2011-05-07 15:37:49 +09:00
if (!ch)
return ch;
2011-05-03 14:50:55 +09:00
this.pos++;
return ch;
2011-05-03 14:50:55 +09:00
},
putBack: function() {
this.pos--;
},
skipChar: function() {
this.pos++;
},
2011-05-06 17:16:09 +09:00
skip: function(n) {
this.pos += n;
},
moveStart: function() {
this.bytes = Uint8Array(this.bytes, this.pos);
2011-05-06 17:16:09 +09:00
this.pos = 0;
},
find: function(needle, limit, backwards) {
2011-05-06 17:16:09 +09:00
var length = this.bytes.length;
var pos = this.pos;
var str = "";
if (pos + limit > length)
limit = length - pos;
for (var n = 0; n < limit; ++n)
str += this.getChar();
this.pos = pos;
var index = backwards ? str.lastIndexOf(needle) : str.indexOf(needle);
2011-05-06 17:16:09 +09:00
if (index == -1)
return false; /* not found */
this.pos += index;
return true; /* found */
},
asString: function() {
var str = "";
var ch;
while (!!(ch = this.getChar()))
str += ch;
return str;
},
makeSubStream: function(pos, length) {
return new Stream(new Uint8Array(this.bytes, pos, length));
2011-05-03 14:50:55 +09:00
}
};
return constructor;
})();
2011-05-03 14:50:55 +09:00
var StringStream = (function () {
function constructor(str) {
var length = str.length;
var bytes = new Uint8Array(length);
for (var n = 0; n < length; ++n)
bytes[n] = str.charCodeAt(n);
this.Stream(bytes);
}
constructor.prototype = Stream.prototype;
return constructor;
})();
var DecryptStream = (function () {
function constructor(str, fileKey, encAlgorithm, keyLength) {
// TODO
}
constructor.prototype = Stream.prototype;
return constructor;
})();
2011-05-07 09:27:27 +09:00
var Name = (function() {
function constructor(name) {
this.name = name;
}
constructor.prototype = {
};
2011-05-07 09:27:27 +09:00
return constructor;
})();
2011-05-07 09:27:27 +09:00
var Cmd = (function() {
function constructor(cmd) {
this.cmd = cmd;
}
2011-05-07 09:27:27 +09:00
constructor.prototype = {
};
return constructor;
})();
var Dict = (function() {
function constructor() {
}
2011-05-07 09:27:27 +09:00
constructor.prototype = {
get: function(key) {
return this["$" + key];
},
set: function(key, value) {
this["$" + key] = value;
},
contains: function(key) {
return ("$" + key) in this;
}
2011-05-07 09:27:27 +09:00
};
return constructor;
})();
var Ref = (function() {
function constructor(num, ref) {
this.num = num;
this.ref = ref;
}
2011-05-07 09:27:27 +09:00
constructor.prototype = {
};
return constructor;
})();
2011-05-07 09:27:27 +09:00
function IsBool(v) {
return typeof v == "boolean";
}
function IsInt(v) {
return typeof v == "number" && ((v|0) == v);
}
function IsNum(v) {
return typeof v == "number";
}
function IsString(v) {
return typeof v == "string";
}
function IsNull(v) {
return v == null;
}
function IsName(v) {
return v instanceof Name;
}
function IsCmd(v, cmd) {
return v instanceof Cmd && (!cmd || v.cmd == cmd);
}
function IsDict(v) {
return v instanceof Dict;
}
function IsArray(v) {
return v instanceof Array;
}
function IsStream(v) {
return v instanceof Stream;
}
function IsRef(v) {
return v instanceof Ref;
}
var EOF = {};
function IsEOF(v) {
return v == EOF;
}
var Error = {};
function IsError(v) {
return v == Error;
}
var None = {};
function IsNone(v) {
return v == None;
}
var Lexer = (function() {
2011-05-03 14:50:55 +09:00
function constructor(stream) {
this.stream = stream;
}
// A '1' in this array means the character is white space. A '1' or
// '2' means the character ends a name or command.
var specialChars = [
1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, // 0x
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x
1, 0, 0, 0, 0, 2, 0, 0, 2, 2, 0, 0, 0, 0, 0, 2, // 2x
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, // 3x
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 4x
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 0, // 5x
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 6x
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 0, // 7x
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // ax
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // bx
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // cx
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // dx
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // ex
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // fx
];
const MIN_INT = (1<<31) | 0;
const MAX_INT = (MIN_INT - 1) | 0;
const MIN_UINT = 0;
const MAX_UINT = ((1<<30) * 4) - 1;
function ToHexDigit(ch) {
if (ch >= "0" && ch <= "9")
return ch - "0";
ch = ch.toLowerCase();
if (ch >= "a" && ch <= "f")
return ch - "a";
return -1;
}
constructor.prototype = {
error: function(msg) {
2011-05-03 14:50:55 +09:00
// TODO
2011-05-07 08:38:16 +09:00
print(msg);
},
getNumber: function(ch) {
var floating = false;
var str = ch;
2011-05-03 14:50:55 +09:00
var stream = this.stream;
do {
2011-05-03 14:50:55 +09:00
ch = stream.getChar();
if (ch == "." && !floating) {
str += ch;
floating = true;
} else if (ch == "-") {
// ignore minus signs in the middle of numbers to match
// Adobe's behavior
this.error("Badly formated number");
} else if (ch >= "0" && ch <= "9") {
str += ch;
} else if (ch == "e" || ch == "E") {
floating = true;
} else {
// put back the last character, it doesn't belong to us
2011-05-03 14:50:55 +09:00
stream.putBack();
break;
}
} while (true);
var value = parseFloat(str);
if (isNaN(value))
2011-05-07 09:27:27 +09:00
return Error;
return value;
},
getString: function(ch) {
var n = 0;
var numParent = 1;
var done = false;
var str = ch;
2011-05-03 14:50:55 +09:00
var stream = this.stream;
do {
2011-05-03 14:50:55 +09:00
switch (ch = stream.getChar()) {
2011-05-03 18:17:17 +09:00
case undefined:
this.error("Unterminated string");
done = true;
break;
case '(':
++numParen;
str += ch;
break;
case ')':
if (--numParen == 0) {
done = true;
} else {
str += ch;
}
break;
case '\\':
2011-05-03 14:50:55 +09:00
switch (ch = stream.getChar()) {
2011-05-03 18:17:17 +09:00
case undefined:
this.error("Unterminated string");
done = true;
break;
case 'n':
str += '\n';
break;
case 'r':
str += '\r';
break;
case 't':
str += '\t';
break;
case 'b':
str += '\b';
break;
case 'f':
str += '\f';
break;
case '\\':
case '(':
case ')':
str += c;
break;
case '0': case '1': case '2': case '3':
case '4': case '5': case '6': case '7':
var x = ch - '0';
2011-05-03 14:50:55 +09:00
ch = stream.lookChar();
if (ch >= '0' && ch <= '7') {
this.getChar();
x = (x << 3) + (x - '0');
2011-05-03 14:50:55 +09:00
ch = stream.lookChar();
if (ch >= '0' && ch <= '7') {
2011-05-03 14:50:55 +09:00
stream.getChar();
x = (x << 3) + (x - '0');
}
}
str += String.fromCharCode(x);
break;
case '\r':
2011-05-03 14:50:55 +09:00
ch = stream.lookChar();
if (ch == '\n')
stream.getChar();
break;
case '\n':
break;
default:
str += ch;
break;
}
break;
default:
str += ch;
break;
}
} while (!done);
if (!str.length)
2011-05-07 09:27:27 +09:00
return EOF;
return str;
},
getName: function(ch) {
var str = "";
2011-05-03 14:50:55 +09:00
var stream = this.stream;
while (!!(ch = stream.lookChar()) && !specialChars[ch.charCodeAt(0)]) {
2011-05-03 14:50:55 +09:00
stream.getChar();
if (ch == "#") {
2011-05-03 14:50:55 +09:00
ch = stream.lookChar();
var x = ToHexDigit(ch);
if (x != -1) {
2011-05-03 14:50:55 +09:00
stream.getChar();
var x2 = ToHexDigit(stream.getChar());
if (x2 == -1)
this.error("Illegal digit in hex char in name");
str += String.fromCharCode((x << 4) | x2);
} else {
str += "#";
str += ch;
}
} else {
str += ch;
}
}
if (str.length > 128)
this.error("Warning: name token is longer than allowed by the specification");
2011-05-07 09:27:27 +09:00
return new Name(str);
},
getHexString: function(ch) {
var str = "";
2011-05-03 14:50:55 +09:00
var stream = this.stream;
while (1) {
2011-05-03 14:50:55 +09:00
ch = stream.getChar();
if (ch == '>') {
break;
2011-05-03 18:17:17 +09:00
} else if (!ch) {
this.error("Unterminated hex string");
break;
2011-05-07 15:37:49 +09:00
} else if (specialChars[ch.charCodeAt(0)] != 1) {
var x, x2;
if (((x = ToHexDigit(ch)) == -1) ||
2011-05-07 15:37:49 +09:00
((x2 = ToHexDigit(stream.getChar())) == -1)) {
this.error("Illegal character in hex string");
break;
}
str += String.fromCharCode((x << 4) | x2);
}
}
2011-05-07 09:27:27 +09:00
return str;
},
getObj: function() {
// skip whitespace and comments
var comment = false;
2011-05-03 14:50:55 +09:00
var stream = this.stream;
var ch;
while (true) {
2011-05-03 18:17:17 +09:00
if (!(ch = stream.getChar()))
2011-05-07 09:27:27 +09:00
return EOF;
if (comment) {
if (ch == '\r' || ch == '\n')
comment = false;
} else if (ch == '%') {
comment = true;
} else if (specialChars[ch.charCodeAt(0)] != 1) {
break;
}
}
// start reading token
switch (ch) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
case '+': case '-': case '.':
return this.getNumber(ch);
case '(':
return this.getString(ch);
case '/':
return this.getName(ch);
// array punctuation
case '[':
case ']':
2011-05-07 09:27:27 +09:00
return new Cmd(ch);
// hex string or dict punctuation
case '<':
2011-05-03 14:50:55 +09:00
ch = stream.lookChar();
if (ch == '<') {
// dict punctuation
2011-05-03 14:50:55 +09:00
stream.getChar();
2011-05-07 14:44:01 +09:00
return new Cmd("<<");
}
return this.getHexString(ch);
// dict punctuation
case '>':
2011-05-03 14:50:55 +09:00
ch = stream.lookChar();
if (ch == '>') {
2011-05-03 14:50:55 +09:00
stream.getChar();
2011-05-07 14:44:01 +09:00
return new Cmd(">>");
}
// fall through
case ')':
case '{':
case '}':
this.error("Illegal character");
2011-05-07 09:27:27 +09:00
return Error;
}
// command
var str = ch;
while (!!(ch = stream.lookChar()) && !specialChars[ch.charCodeAt(0)]) {
2011-05-03 14:50:55 +09:00
stream.getChar();
if (str.length == 128) {
this.error("Command token too long");
break;
}
str += ch;
}
if (str == "true")
2011-05-07 09:27:27 +09:00
return true;
if (str == "false")
2011-05-07 09:27:27 +09:00
return false;
if (str == "null")
2011-05-07 09:27:27 +09:00
return null;
return new Cmd(str);
2011-05-07 15:37:49 +09:00
},
skipToNextLine: function() {
var stream = this.stream;
while (true) {
var ch = stream.getChar();
if (!ch || ch == "\n")
return;
if (ch == "\r") {
if ((ch = stream.lookChar()) == "\n")
stream.getChar();
return;
}
}
}
};
return constructor;
})();
var Parser = (function() {
function constructor(lexer, allowStreams) {
this.lexer = lexer;
this.allowStreams = allowStreams;
this.inlineImg = 0;
this.refill();
}
constructor.prototype = {
refill: function() {
this.buf1 = this.lexer.getObj();
this.buf2 = this.lexer.getObj();
2011-05-03 07:16:03 +09:00
},
shift: function() {
if (this.inlineImg > 0) {
if (this.inlineImg < 2) {
this.inlineImg++;
} else {
// in a damaged content stream, if 'ID' shows up in the middle
// of a dictionary, we need to reset
this.inlineImg = 0;
}
2011-05-07 09:27:27 +09:00
} else if (IsCmd(this.buf2, "ID")) {
this.lexer.skipChar(); // skip char after 'ID' command
this.inlineImg = 1;
}
this.buf1 = this.buf2;
// don't buffer inline image data
2011-05-07 09:27:27 +09:00
this.buf2 = (this.inlineImg > 0) ? null : this.lexer.getObj();
},
getObj: function() {
// refill buffer after inline image data
if (this.inlineImg == 2)
this.refill();
2011-05-07 09:27:27 +09:00
if (IsCmd(this.buf1, "[")) { // array
2011-05-07 14:44:01 +09:00
this.shift();
2011-05-07 09:27:27 +09:00
var array = [];
while (!IsCmd(this.buf1, "]") && !IsEOF(this.buf1))
array.push(this.getObj());
if (IsEOF(this.buf1))
this.error("End of file inside array");
this.shift();
2011-05-07 09:27:27 +09:00
return array;
} else if (IsCmd(this.buf1, "<<")) { // dictionary or stream
this.shift();
2011-05-07 09:27:27 +09:00
var dict = new Dict();
while (!IsCmd(this.buf1, ">>") && !IsEOF(this.buf1)) {
if (!IsName(this.buf1)) {
this.error("Dictionary key must be a name object");
shift();
} else {
2011-05-07 14:44:01 +09:00
var key = this.buf1.name;
this.shift();
2011-05-07 09:27:27 +09:00
if (IsEOF(this.buf1) || IsError(this.buf1))
break;
2011-05-07 09:27:27 +09:00
dict.set(key, this.getObj());
}
}
2011-05-07 09:27:27 +09:00
if (IsEOF(this.buf1))
this.error("End of file inside dictionary");
// stream objects are not allowed inside content streams or
// object streams
2011-05-07 09:27:27 +09:00
if (this.allowStreams && IsCmd(this.buf2, "stream")) {
2011-05-07 15:37:49 +09:00
return this.makeStream(dict);
} else {
this.shift();
}
2011-05-07 09:27:27 +09:00
return dict;
2011-05-07 09:27:27 +09:00
} else if (IsInt(this.buf1)) { // indirect reference or integer
var num = this.buf1;
this.shift();
2011-05-07 09:27:27 +09:00
if (IsInt(this.buf1) && IsCmd(this.buf2, "R")) {
var ref = new Ref(num, this.buf1);
this.shift();
this.shift();
2011-05-07 09:27:27 +09:00
return ref;
}
2011-05-07 09:27:27 +09:00
return num;
} else if (IsString(this.buf1)) { // string
var str = this.buf1;
this.shift();
if (this.fileKey) {
var decrypt = new DecryptStream(new StringStream(str),
this.fileKey,
this.encAlgorithm,
this.keyLength);
str = decrypt.asString();
}
2011-05-07 09:27:27 +09:00
return str;
}
// simple object
var obj = this.buf1;
this.shift();
return obj;
},
2011-05-07 15:37:49 +09:00
makeStream: function(dict) {
var lexer = this.lexer;
var stream = lexer.stream;
// get stream start position
lexer.skipToNextLine();
var pos = stream.pos;
// get length
var length;
if (!IsInt(length = dict.get("Length"))) {
this.error("Bad 'Length' attribute in stream");
2011-05-07 15:37:49 +09:00
lenght = 0;
}
// skip over the stream data
stream.pos = pos + length;
this.shift(); // '>>'
this.shift(); // 'stream'
if (!IsCmd(this.buf1, "endstream"))
this.error("Missing 'endstream'");
this.shift();
stream = stream.makeSubStream(pos, length);
if (this.fileKey) {
stream = new DecryptStream(stream,
this.fileKey,
this.encAlgorithm,
this.keyLength);
}
return this.filter(stream, dict);
},
filter: function(stream, dict) {
var filter = dict.get("Filter") || dict.get("F");
var params = dict.get("DecodeParms") || dict.get("DP");
if (IsName(filter))
return this.makeFilter(stream, filter.name, params);
if (IsArray(filter)) {
var filterArray = filter;
var paramsArray = params;
for (filter in filterArray) {
if (!IsName(filter))
this.error("Bad filter name");
else {
params = null;
if (IsArray(paramsArray) && (i in paramsArray))
params = paramsArray[i];
stream = this.makeFilter(stream, filter.name, params);
}
}
}
return stream;
},
makeFilter: function(stream, name, params) {
// TODO
return stream;
}
};
return constructor;
})();
var Linearization = (function () {
2011-05-03 14:50:55 +09:00
function constructor(stream) {
this.parser = new Parser(new Lexer(stream), false);
var obj1 = this.parser.getObj();
var obj2 = this.parser.getObj();
var obj3 = this.parser.getObj();
this.linDict = this.parser.getObj();
2011-05-07 09:27:27 +09:00
if (IsInt(obj1) && IsInt(obj2) && IsCmd(obj3, "obj") && IsDict(this.linDict)) {
2011-05-07 14:44:01 +09:00
var obj = this.linDict.get("Linearized");
2011-05-07 09:27:27 +09:00
if (!(IsNum(obj) && obj > 0))
this.linDict = null;
}
}
constructor.prototype = {
2011-05-03 07:16:03 +09:00
getInt: function(name) {
var linDict = this.linDict;
var obj;
2011-05-07 09:27:27 +09:00
if (IsDict(linDict) &&
2011-05-07 14:44:01 +09:00
IsInt(obj = linDict.get(name)) &&
2011-05-07 09:27:27 +09:00
obj > 0) {
2011-05-07 14:44:01 +09:00
return obj;
}
this.error("'" + name + "' field in linearization table is invalid");
return 0;
},
2011-05-03 07:16:03 +09:00
getHint: function(index) {
var linDict = this.linDict;
var obj1, obj2;
2011-05-07 09:27:27 +09:00
if (IsDict(linDict) &&
2011-05-07 14:44:01 +09:00
IsArray(obj1 = linDict.get("H")) &&
2011-05-07 09:27:27 +09:00
obj1.length >= 2 &&
IsInt(obj2 = obj1[index]) &&
obj2 > 0) {
return obj2;
}
this.error("Hints table in linearization table is invalid");
return 0;
},
get length() {
2011-05-07 09:27:27 +09:00
if (!IsDict(this.linDict))
return 0;
return this.getInt("L");
},
get hintsOffset() {
return this.getHint(0);
},
get hintsLength() {
return this.getHint(1);
},
get hintsOffset2() {
return this.getHint(2);
},
get hintsLenth2() {
return this.getHint(3);
},
get objectNumberFirst() {
return this.getInt("O");
},
get endFirst() {
return this.getInt("E");
},
get numPages() {
return this.getInt("N");
},
get mainXRefEntriesOffset() {
return this.getInt("T");
},
get pageFirst() {
return this.getInt("P");
}
};
return constructor;
})();
2011-05-07 04:12:57 +09:00
var XRef = (function () {
function constructor(stream, startXRef, mainXRefEntriesOffset) {
2011-05-07 15:37:49 +09:00
this.stream = stream;
2011-05-07 08:38:16 +09:00
this.entries = [];
2011-05-07 15:37:49 +09:00
this.xrefstms = {};
this.readXRef(startXRef);
2011-05-07 04:12:57 +09:00
}
constructor.prototype = {
2011-05-07 08:18:13 +09:00
readXRefTable: function(parser) {
2011-05-07 15:37:49 +09:00
var obj;
2011-05-07 08:38:16 +09:00
while (true) {
2011-05-07 09:27:27 +09:00
if (IsCmd(obj = parser.getObj(), "trailer"))
2011-05-07 08:38:16 +09:00
break;
2011-05-07 09:27:27 +09:00
if (!IsInt(obj))
2011-05-07 08:38:16 +09:00
return false;
2011-05-07 09:27:27 +09:00
var first = obj;
if (!IsInt(obj = parser.getObj()))
2011-05-07 08:38:16 +09:00
return false;
2011-05-07 09:27:27 +09:00
var n = obj;
2011-05-07 08:38:16 +09:00
if (first < 0 || n < 0 || (first + n) != ((first + n) | 0))
return false;
for (var i = first; i < first + n; ++i) {
var entry = {};
2011-05-07 09:27:27 +09:00
if (!IsInt(obj = parser.getObj()))
2011-05-07 08:38:16 +09:00
return false;
2011-05-07 09:27:27 +09:00
entry.offset = obj;
if (!IsInt(obj = parser.getObj()))
2011-05-07 08:38:16 +09:00
return false;
2011-05-07 09:27:27 +09:00
entry.gen = obj;
2011-05-07 08:38:16 +09:00
obj = parser.getObj();
2011-05-07 09:27:27 +09:00
if (IsCmd(obj, "n")) {
2011-05-07 08:38:16 +09:00
entry.uncompressed = true;
2011-05-07 09:27:27 +09:00
} else if (IsCmd(obj, "f")) {
2011-05-07 08:38:16 +09:00
entry.free = true;
} else {
return false;
}
if (!this.entries[i]) {
// In some buggy PDF files the xref table claims to start at 1
// instead of 0.
if (i == 1 && first == 1 &&
entry.offset == 0 && entry.gen == 65535 && entry.free) {
i = first = 0;
}
this.entries[i] = entry;
}
}
}
2011-05-07 15:37:49 +09:00
2011-05-07 08:38:16 +09:00
// read the trailer dictionary
2011-05-07 15:37:49 +09:00
var dict;
if (!IsDict(dict = parser.getObj()))
return false;
// get the 'Prev' pointer
var more = false;
obj = dict.get("Prev");
if (IsInt(obj)) {
this.prev = obj;
more = true;
} else if (IsRef(obj)) {
// certain buggy PDF generators generate "/Prev NNN 0 R" instead
// of "/Prev NNN"
this.prev = obj.num;
more = true;
}
if (!this.trailer)
this.trailer = dict;
// check for 'XRefStm' key
if (IsInt(obj = dict.get("XRefStm"))) {
var pos = obj;
if (pos in this.xrefstms)
return false;
this.xrefstms[pos] = 1; // avoid infinite recursion
this.readXRef(pos);
} else {
this.ok = true;
}
return more;
2011-05-07 08:18:13 +09:00
},
readXRefStream: function(parser) {
2011-05-07 08:38:16 +09:00
// TODO
2011-05-07 08:18:13 +09:00
this.ok = true;
return true;
},
2011-05-07 15:37:49 +09:00
readXRef: function(startXRef) {
var stream = this.stream;
2011-05-07 08:18:13 +09:00
stream.pos = startXRef;
2011-05-07 15:37:49 +09:00
var parser = new Parser(new Lexer(stream), true);
2011-05-07 08:18:13 +09:00
var obj = parser.getObj();
// parse an old-style xref table
2011-05-07 09:27:27 +09:00
if (IsCmd(obj, "xref"))
2011-05-07 08:18:13 +09:00
return this.readXRefTable(parser);
// parse an xref stream
2011-05-07 09:27:27 +09:00
if (IsInt(obj)) {
if (!IsInt(parser.getObj()) ||
!IsCmd(parser.getObj(), "obj") ||
!IsStream(obj = parser.getObj())) {
2011-05-07 08:18:13 +09:00
return false;
}
2011-05-07 09:27:27 +09:00
return this.readXRefStream(obj);
2011-05-07 08:18:13 +09:00
}
return false;
}
2011-05-07 04:12:57 +09:00
};
return constructor;
})();
2011-05-03 07:34:59 +09:00
var PDFDoc = (function () {
2011-05-03 14:50:55 +09:00
function constructor(stream) {
this.stream = stream;
this.setup();
}
2011-05-03 07:34:59 +09:00
constructor.prototype = {
get linearization() {
var length = this.stream.length;
var linearization = false;
if (length) {
linearization = new Linearization(this.stream);
if (linearization.length != length)
linearization = false;
}
2011-05-06 17:16:09 +09:00
// shadow the prototype getter with a data property
return this.linearization = linearization;
2011-05-03 07:34:59 +09:00
},
get startXRef() {
var stream = this.stream;
2011-05-06 17:16:09 +09:00
var startXRef = 0;
var linearization = this.linearization;
if (linearization) {
2011-05-06 17:16:09 +09:00
// Find end of first obj.
stream.reset();
if (stream.find("endobj", 1024))
startXRef = stream.pos + 6;
} else {
2011-05-06 17:16:09 +09:00
// Find startxref at the end of the file.
var start = stream.length - 1024;
if (start < 0)
start = 0;
stream.pos = start;
if (stream.find("startxref", 1024, true)) {
stream.skip(9);
var ch;
while ((ch = stream.getChar()) == " " || ch == "\t")
;
var str = "";
while ((ch - "0") <= 9) {
str += ch;
ch = stream.getChar();
}
startXRef = parseInt(str);
2011-05-06 17:16:09 +09:00
if (isNaN(startXRef))
startXRef = 0;
}
}
2011-05-06 17:16:09 +09:00
// shadow the prototype getter with a data property
return this.startXRef = startXRef;
2011-05-03 07:34:59 +09:00
},
2011-05-07 04:12:57 +09:00
get mainXRefEntriesOffset() {
var mainXRefEntriesOffset = 0;
var linearization = this.linearization;
if (linearization)
mainXRefEntriesOffset = linearization.mainXRefEntriesOffset;
// shadow the prototype getter with a data property
return this.mainXRefEntriesOffset = mainXRefEntriesOffset;
},
2011-05-03 07:34:59 +09:00
// Find the header, remove leading garbage and setup the stream
// starting from the header.
checkHeader: function() {
var stream = this.stream;
2011-05-03 14:50:55 +09:00
stream.reset();
2011-05-06 17:16:09 +09:00
if (stream.find("%PDF-", 1024)) {
2011-05-03 07:34:59 +09:00
// Found the header, trim off any garbage before it.
2011-05-06 17:16:09 +09:00
stream.moveStart();
return;
2011-05-03 07:34:59 +09:00
}
// May not be a PDF file, continue anyway.
},
2011-05-07 08:18:13 +09:00
setup: function(ownerPassword, userPassword) {
this.checkHeader();
2011-05-07 04:12:57 +09:00
this.xref = new XRef(this.stream,
this.startXRef,
this.mainXRefEntriesOffset);
this.ok = this.xref.ok;
2011-05-03 07:34:59 +09:00
}
};
return constructor;
2011-05-03 07:34:59 +09:00
})();
var Interpreter = (function() {
function constructor(xref, resources, catalog, gfx) {
this.xref = xref;
2011-05-06 02:28:37 +09:00
this.res = resources;
this.catalog = catalog;
this.gfx = gfx;
this.map = {
// Graphics state
w: gfx.setLineWidth,
d: gfx.setDash,
q: gfx.save,
Q: gfx.restore,
cm: gfx.transform,
// Path
m: gfx.moveTo,
l: gfx.lineTo,
c: gfx.curveTo,
h: gfx.closePath,
re: gfx.rectangle,
S: gfx.stroke,
f: gfx.fill,
B: gfx.fillStroke,
b: gfx.closeFillStroke,
// Clipping
// Text
BT: gfx.beginText,
ET: gfx.endText,
Tf: gfx.setFont,
Td: gfx.moveText,
Tj: gfx.showText,
// Type3 fonts
// Color
g: gfx.setFillGray,
RG: gfx.setStrokeRGBColor,
rg: gfx.setFillRGBColor,
// Shading
// Images
// XObjects
// Marked content
// Compatibility
};
}
constructor.prototype = {
2011-05-07 10:15:51 +09:00
compile: function(parser) {
},
interpret: function(obj) {
return this.interpretHelper(new Parser(new Lexer(obj), true));
},
2011-05-06 13:46:54 +09:00
interpretHelper: function(mediaBox, parser) {
this.gfx.beginDrawing({ x: mediaBox[0], y: mediaBox[1],
width: mediaBox[2] - mediaBox[0],
height: mediaBox[3] - mediaBox[1] });
2011-05-07 10:15:51 +09:00
var args = [];
var gfx = this.gfx;
var map = this.map;
var obj;
2011-05-07 09:27:27 +09:00
while (!IsEOF(obj = parser.getObj())) {
if (IsCmd(obj)) {
var cmd = obj.cmd;
var fn = map[cmd];
if (fn) {
2011-05-07 10:15:51 +09:00
if (fn.length != args.length)
this.error("Invalid number of arguments '" + cmd + "'");
fn.apply(gfx, args);
} else
this.error("Unknown command '" + cmd + "'");
2011-05-05 13:53:55 +09:00
args.length = 0;
} else {
2011-05-07 15:37:49 +09:00
if (args.length > 33)
this.error("Too many arguments '" + cmd + "'");
2011-05-07 10:15:51 +09:00
args.push(obj);
}
}
this.gfx.endDrawing();
},
error: function(what) {
throw new Error(what);
},
};
return constructor;
})();
var EchoGraphics = (function() {
function constructor() {
this.out = "";
this.indentation = 0;
this.indentationStr = "";
}
constructor.prototype = {
2011-05-06 13:46:54 +09:00
beginDrawing: function(mediaBox) {
this.printdentln("/MediaBox ["+
mediaBox.x +" "+ mediaBox.y +" "+
mediaBox.width +" "+ mediaBox.height +" ]");
},
2011-05-06 13:46:54 +09:00
endDrawing: function() {
},
// Graphics state
setLineWidth: function(width) {
this.printdentln(width +" w");
},
setDash: function(dashArray, dashPhase) {
this.printdentln(""+ dashArray +" "+ dashPhase +" d");
},
save: function() {
this.printdentln("q");
},
restore: function() {
this.printdentln("Q");
},
transform: function(a, b, c, d, e, f) {
2011-05-05 13:34:59 +09:00
this.printdentln(""+ a +" "+ b +" "+ c +
" "+d +" "+ e +" "+ f + " cm");
},
// Path
moveTo: function(x, y) {
this.printdentln(""+ x +" "+ y +" m");
},
lineTo: function(x, y) {
this.printdentln(""+ x +" "+ y +" l");
},
curveTo: function(x1, y1, x2, y2, x3, y3) {
this.printdentln(""+ x1 +" "+ y1 +
" "+ x2 +" "+ y2 +
" "+ x3 +" "+ y3 + " c");
},
closePath: function() {
2011-05-05 13:34:59 +09:00
this.printdentln("h");
},
rectangle: function(x, y, width, height) {
this.printdentln(""+ x +" "+ y + " "+ width +" "+ height +" re");
},
stroke: function() {
this.printdentln("S");
},
fill: function() {
2011-05-05 13:34:59 +09:00
this.printdentln("f");
},
fillStroke: function() {
this.printdentln("B");
},
closeFillStroke: function() {
this.printdentln("b");
},
// Clipping
// Text
beginText: function() {
this.printdentln("BT");
this.indent();
},
endText: function() {
this.dedent();
this.printdentln("ET");
},
setFont: function(font, size) {
2011-05-07 10:15:51 +09:00
this.printdentln("/"+ font.name +" "+ size +" Tf");
},
moveText: function (x, y) {
this.printdentln(""+ x +" "+ y +" Td");
},
showText: function(text) {
this.printdentln("( "+ text +" ) Tj");
},
// Type3 fonts
// Color
setFillGray: function(gray) {
this.printdentln(""+ gray +" g");
},
setStrokeRGBColor: function(r, g, b) {
this.printdentln(""+ r +" "+ g +" "+ b +" RG");
},
setFillRGBColor: function(r, g, b) {
this.printdentln(""+ r +" "+ g +" "+ b +" rg");
},
// Shading
// Images
// XObjects
// Marked content
// Compatibility
// Output state
print: function(str) {
this.out += str;
},
println: function(str) {
this.print(str);
this.out += "\n";
},
printdentln: function(str) {
this.print(this.indentationStr);
this.println(str);
},
indent: function() {
this.indentation += 2;
this.indentationStr += " ";
},
dedent: function() {
this.indentation -= 2;
this.indentationStr = this.indentationStr.slice(0, -2);
},
};
return constructor;
})();
2011-05-06 02:40:34 +09:00
// <canvas> contexts store most of the state we need natively.
// However, PDF needs a bit more state, which we store here.
var CanvasExtraState = (function() {
function constructor() {
// Current text position (in text coordinates)
this.lineX = 0.0;
this.lineY = 0.0;
}
constructor.prototype = {
};
return constructor;
})();
var CanvasGraphics = (function() {
function constructor(canvasCtx) {
this.ctx = canvasCtx;
2011-05-06 02:40:34 +09:00
this.current = new CanvasExtraState();
this.stateStack = [ ];
}
constructor.prototype = {
2011-05-06 13:46:54 +09:00
beginDrawing: function(mediaBox) {
var cw = this.ctx.canvas.width, ch = this.ctx.canvas.height;
this.ctx.save();
2011-05-06 13:46:54 +09:00
this.ctx.scale(cw / mediaBox.width, -ch / mediaBox.height);
this.ctx.translate(0, -mediaBox.height);
},
endDrawing: function () {
this.ctx.restore();
},
// Graphics state
setLineWidth: function(width) {
this.ctx.lineWidth = width;
},
setDash: function(dashArray, dashPhase) {
2011-05-07 10:15:51 +09:00
// TODO
},
save: function() {
this.ctx.save();
2011-05-06 02:40:34 +09:00
this.stateStack.push(this.current);
this.current = new CanvasExtraState();
},
restore: function() {
2011-05-06 02:40:34 +09:00
this.current = this.stateStack.pop();
this.ctx.restore();
},
transform: function(a, b, c, d, e, f) {
2011-05-05 13:34:59 +09:00
this.ctx.transform(a, b, c, d, e, f);
},
// Path
moveTo: function(x, y) {
this.ctx.moveTo(x, y);
},
lineTo: function(x, y) {
this.ctx.lineTo(x, y);
},
curveTo: function(x1, y1, x2, y2, x3, y3) {
this.ctx.bezierCurveTo(x1, y1, x2, y2, x3, y3);
},
closePath: function() {
2011-05-05 13:34:59 +09:00
this.ctx.closePath();
},
rectangle: function(x, y, width, height) {
this.ctx.rect(x, y, width, height);
},
stroke: function() {
this.ctx.stroke();
this.consumePath();
},
fill: function() {
2011-05-05 13:34:59 +09:00
this.ctx.fill();
this.consumePath();
2011-05-05 13:34:59 +09:00
},
fillStroke: function() {
this.ctx.fill();
this.ctx.stroke();
this.consumePath();
},
closeFillStroke: function() {
return this.fillStroke();
},
// Clipping
2011-05-05 10:08:52 +09:00
// Text
beginText: function() {
2011-05-07 10:15:51 +09:00
// TODO
2011-05-05 10:08:52 +09:00
},
endText: function() {
2011-05-07 10:15:51 +09:00
// TODO
2011-05-05 10:08:52 +09:00
},
setFont: function(font, size) {
2011-05-06 02:28:37 +09:00
this.ctx.font = size +'px '+ font.BaseFont;
2011-05-05 10:08:52 +09:00
},
moveText: function (x, y) {
2011-05-06 02:40:34 +09:00
this.current.lineX = x;
this.current.lineY = y;
2011-05-05 10:08:52 +09:00
},
showText: function(text) {
this.ctx.save();
this.ctx.translate(0, 2 * this.current.lineY);
this.ctx.scale(1, -1);
2011-05-06 02:40:34 +09:00
this.ctx.fillText(text, this.current.lineX, this.current.lineY);
this.ctx.restore();
2011-05-05 10:08:52 +09:00
},
// Type3 fonts
// Color
setFillGray: function(gray) {
this.setFillRGBColor(gray, gray, gray);
},
setStrokeRGBColor: function(r, g, b) {
this.ctx.strokeStyle = this.makeCssRgb(r, g, b);
},
setFillRGBColor: function(r, g, b) {
this.ctx.fillStyle = this.makeCssRgb(r, g, b);
},
// Helper functions
2011-05-07 10:15:51 +09:00
consumePath: function() {
this.ctx.beginPath();
},
makeCssRgb: function(r, g, b) {
var ri = (255 * r) | 0, gi = (255 * g) | 0, bi = (255 * b) | 0;
return "rgb("+ ri +","+ gi +","+ bi +")";
},
};
return constructor;
})();
//var PostscriptGraphics
//var SVGGraphics
var MockParser = (function() {
function constructor(objs) {
2011-05-05 10:08:52 +09:00
this.objs = objs.slice(0);
}
constructor.prototype = {
getObj: function() {
return this.objs.shift();
}
};
return constructor;
})();
2011-05-07 09:27:27 +09:00
function cmd(c) { return new Cmd(c); }
function name(n) { return new Name(n); }
function int(i) { return i; }
function string(s) { return s; }
function eof() { return EOF; }
function array(a) { return a; }
function real(r) { return r; }
var tests = [
{ name: "Hello world",
2011-05-06 02:28:37 +09:00
res: {
// XXX not structured correctly
Font: {
F1: { Type: "Font",
Subtype: "Type1",
Name: "F1",
2011-05-06 13:46:54 +09:00
BaseFont: "Helvetica",
2011-05-06 02:28:37 +09:00
Encoding: "MacRomanEncoding"
},
}
},
2011-05-06 13:46:54 +09:00
mediaBox: [ 0, 0, 612, 792 ],
objs: [
cmd("BT"),
name("F1"), int(24), cmd("Tf"),
int(100), int(100), cmd("Td"),
string("Hello World"), cmd("Tj"),
cmd("ET"),
eof()
2011-05-06 13:46:54 +09:00
]
},
{ name: "Simple graphics",
2011-05-06 02:28:37 +09:00
res: { },
2011-05-06 13:46:54 +09:00
mediaBox: [ 0, 0, 612, 792 ],
objs: [
int(150), int(250), cmd("m"),
int(150), int(350), cmd("l"),
cmd("S"),
int(4), cmd("w"),
array([int(4), int(6)]), int(0), cmd("d"),
int(150), int(250), cmd("m"),
int(400), int(250), cmd("l"),
cmd("S"),
array([]), int(0), cmd("d"),
int(1), cmd("w"),
real(1.0), real(0.0), real(0.0), cmd("RG"),
real(0.5), real(0.75), real(1.0), cmd("rg"),
int(200), int(300), int(50), int(75), cmd("re"),
cmd("B"),
real(0.5), real(0.1), real(0.2), cmd("RG"),
real(0.7), cmd("g"),
int(300), int(300), cmd("m"),
int(300), int(400), int(400), int(400), int(400), int(300), cmd("c"),
cmd("b"),
eof()
]
},
2011-05-05 13:34:59 +09:00
{ name: "Heart",
2011-05-06 02:28:37 +09:00
res: { },
2011-05-06 13:46:54 +09:00
mediaBox: [ 0, 0, 612, 792 ],
2011-05-05 13:34:59 +09:00
objs: [
cmd("q"),
real(0.9), real(0.0), real(0.0), cmd("rg"),
int(75), int(40), cmd("m"),
int(75), int(37), int(70), int(25), int(50), int(25), cmd("c"),
int(20), int(25), int(20), real(62.5), int(20), real(62.5), cmd("c"),
int(20), int(80), int(40), int(102), int(75), int(120), cmd("c"),
int(110), int(102), int(130), int(80), int(130), real(62.5), cmd("c"),
int(130), real(62.5), int(130), int(25), int(100), int(25), cmd("c"),
int(85), int(25), int(75), int(37), int(75), int(40), cmd("c"),
cmd("f"),
cmd("Q"),
eof()
]
},
{ name: "Rectangle",
2011-05-06 02:28:37 +09:00
res: { },
2011-05-06 13:46:54 +09:00
mediaBox: [ 0, 0, 612, 792 ],
2011-05-05 13:34:59 +09:00
objs: [
int(1), int(0), int(0), int(1), int(80), int(80), cmd("cm"),
int(0), int(72), cmd("m"),
int(72), int(0), cmd("l"),
int(0), int(-72), cmd("l"),
int(-72), int(0), cmd("l"),
int(4), cmd("w"),
cmd("h"), cmd("S"),
eof()
2011-05-06 13:46:54 +09:00
]
2011-05-05 13:34:59 +09:00
},
];
2011-05-05 13:34:59 +09:00
function runEchoTests() {
tests.forEach(function(test) {
putstr("Running echo test '"+ test.name +"'... ");
var output = "";
var gfx = new EchoGraphics(output);
2011-05-06 02:28:37 +09:00
var i = new Interpreter(null, test.res, null, gfx);
2011-05-06 13:46:54 +09:00
i.interpretHelper(test.mediaBox, new MockParser(test.objs));
print("done. Output:");
print(gfx.out);
});
}
function runParseTests() {
2011-05-07 14:44:01 +09:00
//var data = snarf("simple_graphics.pdf", "binary");
var data = snarf("/tmp/pdf_reference_1-7.pdf", "binary");
var pdf = new PDFDoc(new Stream(data));
}
if ("arguments" in this) {
const cmds = {
"-e": runEchoTests,
"-p": runParseTests
}
for (n in arguments) {
var fn = cmds[arguments[n]];
if (fn)
fn();
}
}