/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- /
/* vim: set shiftwidth=4 tabstop=8 autoindent cindent expandtab: */
+function warn(msg) {
+ if (console && console.log)
+ console.log(msg);
+ if (print)
+ print(msg);
+}
+
+function error(msg) {
+ throw new Error(msg);
+}
+
var Stream = (function() {
function constructor(arrayBuffer) {
this.bytes = Uint8Array(arrayBuffer);
const lengthDecode = [
[0, 3],
[0, 4],
- [0, 5},
- [0, 6},
- [0, 7},
- [0, 8},
- [0, 9},
+ [0, 5],
+ [0, 6],
+ [0, 7],
+ [0, 8],
+ [0, 9],
[0, 10],
[1, 11],
[1, 13],
return v == EOF;
}
-var Error = {};
-
-function IsError(v) {
- return v == Error;
-}
-
var None = {};
function IsNone(v) {
}
constructor.prototype = {
- error: function(msg) {
- // TODO
- print(msg);
- },
getNumber: function(ch) {
var floating = false;
var str = ch;
} else if (ch == "-") {
// ignore minus signs in the middle of numbers to match
// Adobe's behavior
- this.error("Badly formated number");
+ warn("Badly formated number");
} else if (ch >= "0" && ch <= "9") {
str += ch;
} else if (ch == "e" || ch == "E") {
} while (true);
var value = parseFloat(str);
if (isNaN(value))
- return Error;
+ error("Invalid floating point number");
return value;
},
getString: function(ch) {
do {
switch (ch = stream.getChar()) {
case undefined:
- this.error("Unterminated string");
+ warn("Unterminated string");
done = true;
break;
case '(':
case '\\':
switch (ch = stream.getChar()) {
case undefined:
- this.error("Unterminated string");
+ warn("Unterminated string");
done = true;
break;
case 'n':
stream.getChar();
var x2 = ToHexDigit(stream.getChar());
if (x2 == -1)
- this.error("Illegal digit in hex char in name");
+ error("Illegal digit in hex char in name");
str += String.fromCharCode((x << 4) | x2);
} else {
str += "#";
}
}
if (str.length > 128)
- this.error("Warning: name token is longer than allowed by the specification");
+ error("Warning: name token is longer than allowed by the specification");
return new Name(str);
},
getHexString: function(ch) {
if (ch == '>') {
break;
} else if (!ch) {
- this.error("Unterminated hex string");
+ warn("Unterminated hex string");
break;
} else if (specialChars[ch.charCodeAt(0)] != 1) {
var x, x2;
if (((x = ToHexDigit(ch)) == -1) ||
((x2 = ToHexDigit(stream.getChar())) == -1)) {
- this.error("Illegal character in hex string");
+ error("Illegal character in hex string");
break;
}
str += String.fromCharCode((x << 4) | x2);
case ')':
case '{':
case '}':
- this.error("Illegal character");
+ error("Illegal character");
return Error;
}
while (!!(ch = stream.lookChar()) && !specialChars[ch.charCodeAt(0)]) {
stream.getChar();
if (str.length == 128) {
- this.error("Command token too long");
+ error("Command token too long");
break;
}
str += ch;
while (!IsCmd(this.buf1, "]") && !IsEOF(this.buf1))
array.push(this.getObj());
if (IsEOF(this.buf1))
- this.error("End of file inside array");
+ error("End of file inside array");
this.shift();
return array;
} else if (IsCmd(this.buf1, "<<")) { // dictionary or stream
var dict = new Dict();
while (!IsCmd(this.buf1, ">>") && !IsEOF(this.buf1)) {
if (!IsName(this.buf1)) {
- this.error("Dictionary key must be a name object");
+ error("Dictionary key must be a name object");
shift();
} else {
var key = this.buf1.name;
this.shift();
- if (IsEOF(this.buf1) || IsError(this.buf1))
+ if (IsEOF(this.buf1))
break;
dict.set(key, this.getObj());
}
}
if (IsEOF(this.buf1))
- this.error("End of file inside dictionary");
+ error("End of file inside dictionary");
// stream objects are not allowed inside content streams or
// object streams
// get length
var length;
if (!IsInt(length = dict.get("Length"))) {
- this.error("Bad 'Length' attribute in stream");
+ error("Bad 'Length' attribute in stream");
lenght = 0;
}
this.shift(); // '>>'
this.shift(); // 'stream'
if (!IsCmd(this.buf1, "endstream"))
- this.error("Missing 'endstream'");
+ error("Missing 'endstream'");
this.shift();
stream = stream.makeSubStream(pos, length);
var paramsArray = params;
for (filter in filterArray) {
if (!IsName(filter))
- this.error("Bad filter name");
+ error("Bad filter name");
else {
params = null;
if (IsArray(paramsArray) && (i in paramsArray))
obj > 0) {
return obj;
}
- this.error("'" + name + "' field in linearization table is invalid");
+ error("'" + name + "' field in linearization table is invalid");
return 0;
},
getHint: function(index) {
obj2 > 0) {
return obj2;
}
- this.error("Hints table in linearization table is invalid");
+ error("Hints table in linearization table is invalid");
return 0;
},
get length() {
var fn = map[cmd];
if (fn) {
if (fn.length != args.length)
- this.error("Invalid number of arguments '" + cmd + "'");
+ error("Invalid number of arguments '" + cmd + "'");
fn.apply(gfx, args);
} else
- this.error("Unknown command '" + cmd + "'");
+ error("Unknown command '" + cmd + "'");
args.length = 0;
} else {
if (args.length > 33)
- this.error("Too many arguments '" + cmd + "'");
+ error("Too many arguments '" + cmd + "'");
args.push(obj);
}
}
this.gfx.endDrawing();
- },
- error: function(what) {
- throw new Error(what);
- },
+ }
};
return constructor;
} else if (IsString(e)) {
this.showText(e);
} else {
- this.error("Unexpected element in TJ array");
+ error("Unexpected element in TJ array");
}
}
},