2011-05-03 07:06:11 +09:00
|
|
|
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- /
|
|
|
|
/* vim: set shiftwidth=4 tabstop=8 autoindent cindent expandtab: */
|
|
|
|
|
2011-05-10 10:10:15 +09:00
|
|
|
function warn(msg) {
|
|
|
|
if (console && console.log)
|
|
|
|
console.log(msg);
|
|
|
|
if (print)
|
|
|
|
print(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
function error(msg) {
|
|
|
|
throw new Error(msg);
|
|
|
|
}
|
|
|
|
|
2011-05-03 14:50:55 +09:00
|
|
|
var Stream = (function() {
|
|
|
|
function constructor(arrayBuffer) {
|
|
|
|
this.bytes = Uint8Array(arrayBuffer);
|
|
|
|
this.pos = 0;
|
2011-05-09 03:35:32 +09:00
|
|
|
this.start = 0;
|
2011-05-03 14:50:55 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor.prototype = {
|
2011-05-06 17:16:09 +09:00
|
|
|
get length() {
|
|
|
|
return this.bytes.length;
|
|
|
|
},
|
2011-05-11 13:22:28 +09:00
|
|
|
getByte: function() {
|
|
|
|
var bytes = this.bytes;
|
|
|
|
if (this.pos >= bytes.length)
|
|
|
|
return -1;
|
|
|
|
return bytes[this.pos++];
|
|
|
|
},
|
2011-05-03 14:50:55 +09:00
|
|
|
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++;
|
2011-05-06 18:18:31 +09:00
|
|
|
return ch;
|
2011-05-03 14:50:55 +09:00
|
|
|
},
|
2011-05-06 17:16:09 +09:00
|
|
|
skip: function(n) {
|
2011-05-10 15:44:25 +09:00
|
|
|
if (!n)
|
|
|
|
n = 1;
|
|
|
|
this.pos += n;
|
2011-05-06 17:16:09 +09:00
|
|
|
},
|
2011-05-11 19:26:33 +09:00
|
|
|
reset: function() {
|
|
|
|
this.pos = this.start;
|
|
|
|
},
|
2011-05-06 17:16:09 +09:00
|
|
|
moveStart: function() {
|
2011-05-09 03:35:32 +09:00
|
|
|
this.start = this.pos;
|
2011-05-06 17:16:09 +09:00
|
|
|
},
|
2011-05-07 17:20:04 +09:00
|
|
|
makeSubStream: function(pos, length) {
|
2011-05-11 17:20:12 +09:00
|
|
|
var buffer = this.bytes.buffer;
|
|
|
|
if (length)
|
|
|
|
return new Stream(new Uint8Array(buffer, pos, length));
|
|
|
|
return new Stream(new Uint8Array(buffer, pos));
|
2011-05-03 14:50:55 +09:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return constructor;
|
2011-05-06 18:18:31 +09:00
|
|
|
})();
|
2011-05-03 14:50:55 +09:00
|
|
|
|
2011-05-09 03:32:53 +09:00
|
|
|
var StringStream = (function() {
|
2011-05-07 17:20:04 +09:00
|
|
|
function constructor(str) {
|
|
|
|
var length = str.length;
|
|
|
|
var bytes = new Uint8Array(length);
|
|
|
|
for (var n = 0; n < length; ++n)
|
|
|
|
bytes[n] = str.charCodeAt(n);
|
2011-05-08 14:57:04 +09:00
|
|
|
Stream.call(this, bytes);
|
2011-05-07 17:20:04 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor.prototype = Stream.prototype;
|
|
|
|
|
|
|
|
return constructor;
|
|
|
|
})();
|
|
|
|
|
2011-05-10 10:04:22 +09:00
|
|
|
var FlateStream = (function() {
|
2011-05-11 13:22:28 +09:00
|
|
|
const codeLenCodeMap = [
|
2011-05-10 10:04:22 +09:00
|
|
|
16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
|
|
|
|
];
|
2011-05-09 09:39:40 +09:00
|
|
|
|
2011-05-10 10:04:22 +09:00
|
|
|
const lengthDecode = [
|
|
|
|
[0, 3],
|
|
|
|
[0, 4],
|
2011-05-10 10:10:15 +09:00
|
|
|
[0, 5],
|
|
|
|
[0, 6],
|
|
|
|
[0, 7],
|
|
|
|
[0, 8],
|
|
|
|
[0, 9],
|
2011-05-10 10:04:22 +09:00
|
|
|
[0, 10],
|
|
|
|
[1, 11],
|
|
|
|
[1, 13],
|
|
|
|
[1, 15],
|
|
|
|
[1, 17],
|
|
|
|
[2, 19],
|
|
|
|
[2, 23],
|
|
|
|
[2, 27],
|
|
|
|
[2, 31],
|
|
|
|
[3, 35],
|
|
|
|
[3, 43],
|
|
|
|
[3, 51],
|
|
|
|
[3, 59],
|
|
|
|
[4, 67],
|
|
|
|
[4, 83],
|
|
|
|
[4, 99],
|
|
|
|
[4, 115],
|
|
|
|
[5, 131],
|
|
|
|
[5, 163],
|
|
|
|
[5, 195],
|
|
|
|
[5, 227],
|
|
|
|
[0, 258],
|
|
|
|
[0, 258],
|
|
|
|
[0, 258]
|
|
|
|
];
|
2011-05-09 09:39:40 +09:00
|
|
|
|
2011-05-12 08:30:30 +09:00
|
|
|
const distDecode = [
|
|
|
|
[0, 1],
|
|
|
|
[0, 2],
|
|
|
|
[0, 3],
|
|
|
|
[0, 4],
|
|
|
|
[1, 5],
|
|
|
|
[1, 7],
|
|
|
|
[2, 9],
|
|
|
|
[2, 13],
|
|
|
|
[3, 17],
|
|
|
|
[3, 25],
|
|
|
|
[4, 33],
|
|
|
|
[4, 49],
|
|
|
|
[5, 65],
|
|
|
|
[5, 97],
|
|
|
|
[6, 129],
|
|
|
|
[6, 193],
|
|
|
|
[7, 257],
|
|
|
|
[7, 385],
|
|
|
|
[8, 513],
|
|
|
|
[8, 769],
|
|
|
|
[9, 1025],
|
|
|
|
[9, 1537],
|
|
|
|
[10, 2049],
|
|
|
|
[10, 3073],
|
|
|
|
[11, 4097],
|
|
|
|
[11, 6145],
|
|
|
|
[12, 8193],
|
|
|
|
[12, 12289],
|
|
|
|
[13, 16385],
|
|
|
|
[13, 24577]
|
|
|
|
];
|
|
|
|
|
2011-05-11 13:22:28 +09:00
|
|
|
const fixedLitCodeTab = [[
|
2011-05-10 10:04:22 +09:00
|
|
|
[7, 0x0100],
|
|
|
|
[8, 0x0050],
|
|
|
|
[8, 0x0010],
|
|
|
|
[8, 0x0118],
|
|
|
|
[7, 0x0110],
|
|
|
|
[8, 0x0070],
|
|
|
|
[8, 0x0030],
|
|
|
|
[9, 0x00c0],
|
|
|
|
[7, 0x0108],
|
|
|
|
[8, 0x0060],
|
|
|
|
[8, 0x0020],
|
|
|
|
[9, 0x00a0],
|
|
|
|
[8, 0x0000],
|
|
|
|
[8, 0x0080],
|
|
|
|
[8, 0x0040],
|
|
|
|
[9, 0x00e0],
|
|
|
|
[7, 0x0104],
|
|
|
|
[8, 0x0058],
|
|
|
|
[8, 0x0018],
|
|
|
|
[9, 0x0090],
|
|
|
|
[7, 0x0114],
|
|
|
|
[8, 0x0078],
|
|
|
|
[8, 0x0038],
|
|
|
|
[9, 0x00d0],
|
|
|
|
[7, 0x010c],
|
|
|
|
[8, 0x0068],
|
|
|
|
[8, 0x0028],
|
|
|
|
[9, 0x00b0],
|
|
|
|
[8, 0x0008],
|
|
|
|
[8, 0x0088],
|
|
|
|
[8, 0x0048],
|
|
|
|
[9, 0x00f0],
|
|
|
|
[7, 0x0102],
|
|
|
|
[8, 0x0054],
|
|
|
|
[8, 0x0014],
|
|
|
|
[8, 0x011c],
|
|
|
|
[7, 0x0112],
|
|
|
|
[8, 0x0074],
|
|
|
|
[8, 0x0034],
|
|
|
|
[9, 0x00c8],
|
|
|
|
[7, 0x010a],
|
|
|
|
[8, 0x0064],
|
|
|
|
[8, 0x0024],
|
|
|
|
[9, 0x00a8],
|
|
|
|
[8, 0x0004],
|
|
|
|
[8, 0x0084],
|
|
|
|
[8, 0x0044],
|
|
|
|
[9, 0x00e8],
|
|
|
|
[7, 0x0106],
|
|
|
|
[8, 0x005c],
|
|
|
|
[8, 0x001c],
|
|
|
|
[9, 0x0098],
|
|
|
|
[7, 0x0116],
|
|
|
|
[8, 0x007c],
|
|
|
|
[8, 0x003c],
|
|
|
|
[9, 0x00d8],
|
|
|
|
[7, 0x010e],
|
|
|
|
[8, 0x006c],
|
|
|
|
[8, 0x002c],
|
|
|
|
[9, 0x00b8],
|
|
|
|
[8, 0x000c],
|
|
|
|
[8, 0x008c],
|
|
|
|
[8, 0x004c],
|
|
|
|
[9, 0x00f8],
|
|
|
|
[7, 0x0101],
|
|
|
|
[8, 0x0052],
|
|
|
|
[8, 0x0012],
|
|
|
|
[8, 0x011a],
|
|
|
|
[7, 0x0111],
|
|
|
|
[8, 0x0072],
|
|
|
|
[8, 0x0032],
|
|
|
|
[9, 0x00c4],
|
|
|
|
[7, 0x0109],
|
|
|
|
[8, 0x0062],
|
|
|
|
[8, 0x0022],
|
|
|
|
[9, 0x00a4],
|
|
|
|
[8, 0x0002],
|
|
|
|
[8, 0x0082],
|
|
|
|
[8, 0x0042],
|
|
|
|
[9, 0x00e4],
|
|
|
|
[7, 0x0105],
|
|
|
|
[8, 0x005a],
|
|
|
|
[8, 0x001a],
|
|
|
|
[9, 0x0094],
|
|
|
|
[7, 0x0115],
|
|
|
|
[8, 0x007a],
|
|
|
|
[8, 0x003a],
|
|
|
|
[9, 0x00d4],
|
|
|
|
[7, 0x010d],
|
|
|
|
[8, 0x006a],
|
|
|
|
[8, 0x002a],
|
|
|
|
[9, 0x00b4],
|
|
|
|
[8, 0x000a],
|
|
|
|
[8, 0x008a],
|
|
|
|
[8, 0x004a],
|
|
|
|
[9, 0x00f4],
|
|
|
|
[7, 0x0103],
|
|
|
|
[8, 0x0056],
|
|
|
|
[8, 0x0016],
|
|
|
|
[8, 0x011e],
|
|
|
|
[7, 0x0113],
|
|
|
|
[8, 0x0076],
|
|
|
|
[8, 0x0036],
|
|
|
|
[9, 0x00cc],
|
|
|
|
[7, 0x010b],
|
|
|
|
[8, 0x0066],
|
|
|
|
[8, 0x0026],
|
|
|
|
[9, 0x00ac],
|
|
|
|
[8, 0x0006],
|
|
|
|
[8, 0x0086],
|
|
|
|
[8, 0x0046],
|
|
|
|
[9, 0x00ec],
|
|
|
|
[7, 0x0107],
|
|
|
|
[8, 0x005e],
|
|
|
|
[8, 0x001e],
|
|
|
|
[9, 0x009c],
|
|
|
|
[7, 0x0117],
|
|
|
|
[8, 0x007e],
|
|
|
|
[8, 0x003e],
|
|
|
|
[9, 0x00dc],
|
|
|
|
[7, 0x010f],
|
|
|
|
[8, 0x006e],
|
|
|
|
[8, 0x002e],
|
|
|
|
[9, 0x00bc],
|
|
|
|
[8, 0x000e],
|
|
|
|
[8, 0x008e],
|
|
|
|
[8, 0x004e],
|
|
|
|
[9, 0x00fc],
|
|
|
|
[7, 0x0100],
|
|
|
|
[8, 0x0051],
|
|
|
|
[8, 0x0011],
|
|
|
|
[8, 0x0119],
|
|
|
|
[7, 0x0110],
|
|
|
|
[8, 0x0071],
|
|
|
|
[8, 0x0031],
|
|
|
|
[9, 0x00c2],
|
|
|
|
[7, 0x0108],
|
|
|
|
[8, 0x0061],
|
|
|
|
[8, 0x0021],
|
|
|
|
[9, 0x00a2],
|
|
|
|
[8, 0x0001],
|
|
|
|
[8, 0x0081],
|
|
|
|
[8, 0x0041],
|
|
|
|
[9, 0x00e2],
|
|
|
|
[7, 0x0104],
|
|
|
|
[8, 0x0059],
|
|
|
|
[8, 0x0019],
|
|
|
|
[9, 0x0092],
|
|
|
|
[7, 0x0114],
|
|
|
|
[8, 0x0079],
|
|
|
|
[8, 0x0039],
|
|
|
|
[9, 0x00d2],
|
|
|
|
[7, 0x010c],
|
|
|
|
[8, 0x0069],
|
|
|
|
[8, 0x0029],
|
|
|
|
[9, 0x00b2],
|
|
|
|
[8, 0x0009],
|
|
|
|
[8, 0x0089],
|
|
|
|
[8, 0x0049],
|
|
|
|
[9, 0x00f2],
|
|
|
|
[7, 0x0102],
|
|
|
|
[8, 0x0055],
|
|
|
|
[8, 0x0015],
|
|
|
|
[8, 0x011d],
|
|
|
|
[7, 0x0112],
|
|
|
|
[8, 0x0075],
|
|
|
|
[8, 0x0035],
|
|
|
|
[9, 0x00ca],
|
|
|
|
[7, 0x010a],
|
|
|
|
[8, 0x0065],
|
|
|
|
[8, 0x0025],
|
|
|
|
[9, 0x00aa],
|
|
|
|
[8, 0x0005],
|
|
|
|
[8, 0x0085],
|
|
|
|
[8, 0x0045],
|
|
|
|
[9, 0x00ea],
|
|
|
|
[7, 0x0106],
|
|
|
|
[8, 0x005d],
|
|
|
|
[8, 0x001d],
|
|
|
|
[9, 0x009a],
|
|
|
|
[7, 0x0116],
|
|
|
|
[8, 0x007d],
|
|
|
|
[8, 0x003d],
|
|
|
|
[9, 0x00da],
|
|
|
|
[7, 0x010e],
|
|
|
|
[8, 0x006d],
|
|
|
|
[8, 0x002d],
|
|
|
|
[9, 0x00ba],
|
|
|
|
[8, 0x000d],
|
|
|
|
[8, 0x008d],
|
|
|
|
[8, 0x004d],
|
|
|
|
[9, 0x00fa],
|
|
|
|
[7, 0x0101],
|
|
|
|
[8, 0x0053],
|
|
|
|
[8, 0x0013],
|
|
|
|
[8, 0x011b],
|
|
|
|
[7, 0x0111],
|
|
|
|
[8, 0x0073],
|
|
|
|
[8, 0x0033],
|
|
|
|
[9, 0x00c6],
|
|
|
|
[7, 0x0109],
|
|
|
|
[8, 0x0063],
|
|
|
|
[8, 0x0023],
|
|
|
|
[9, 0x00a6],
|
|
|
|
[8, 0x0003],
|
|
|
|
[8, 0x0083],
|
|
|
|
[8, 0x0043],
|
|
|
|
[9, 0x00e6],
|
|
|
|
[7, 0x0105],
|
|
|
|
[8, 0x005b],
|
|
|
|
[8, 0x001b],
|
|
|
|
[9, 0x0096],
|
|
|
|
[7, 0x0115],
|
|
|
|
[8, 0x007b],
|
|
|
|
[8, 0x003b],
|
|
|
|
[9, 0x00d6],
|
|
|
|
[7, 0x010d],
|
|
|
|
[8, 0x006b],
|
|
|
|
[8, 0x002b],
|
|
|
|
[9, 0x00b6],
|
|
|
|
[8, 0x000b],
|
|
|
|
[8, 0x008b],
|
|
|
|
[8, 0x004b],
|
|
|
|
[9, 0x00f6],
|
|
|
|
[7, 0x0103],
|
|
|
|
[8, 0x0057],
|
|
|
|
[8, 0x0017],
|
|
|
|
[8, 0x011f],
|
|
|
|
[7, 0x0113],
|
|
|
|
[8, 0x0077],
|
|
|
|
[8, 0x0037],
|
|
|
|
[9, 0x00ce],
|
|
|
|
[7, 0x010b],
|
|
|
|
[8, 0x0067],
|
|
|
|
[8, 0x0027],
|
|
|
|
[9, 0x00ae],
|
|
|
|
[8, 0x0007],
|
|
|
|
[8, 0x0087],
|
|
|
|
[8, 0x0047],
|
|
|
|
[9, 0x00ee],
|
|
|
|
[7, 0x0107],
|
|
|
|
[8, 0x005f],
|
|
|
|
[8, 0x001f],
|
|
|
|
[9, 0x009e],
|
|
|
|
[7, 0x0117],
|
|
|
|
[8, 0x007f],
|
|
|
|
[8, 0x003f],
|
|
|
|
[9, 0x00de],
|
|
|
|
[7, 0x010f],
|
|
|
|
[8, 0x006f],
|
|
|
|
[8, 0x002f],
|
|
|
|
[9, 0x00be],
|
|
|
|
[8, 0x000f],
|
|
|
|
[8, 0x008f],
|
|
|
|
[8, 0x004f],
|
|
|
|
[9, 0x00fe],
|
|
|
|
[7, 0x0100],
|
|
|
|
[8, 0x0050],
|
|
|
|
[8, 0x0010],
|
|
|
|
[8, 0x0118],
|
|
|
|
[7, 0x0110],
|
|
|
|
[8, 0x0070],
|
|
|
|
[8, 0x0030],
|
|
|
|
[9, 0x00c1],
|
|
|
|
[7, 0x0108],
|
|
|
|
[8, 0x0060],
|
|
|
|
[8, 0x0020],
|
|
|
|
[9, 0x00a1],
|
|
|
|
[8, 0x0000],
|
|
|
|
[8, 0x0080],
|
|
|
|
[8, 0x0040],
|
|
|
|
[9, 0x00e1],
|
|
|
|
[7, 0x0104],
|
|
|
|
[8, 0x0058],
|
|
|
|
[8, 0x0018],
|
|
|
|
[9, 0x0091],
|
|
|
|
[7, 0x0114],
|
|
|
|
[8, 0x0078],
|
|
|
|
[8, 0x0038],
|
|
|
|
[9, 0x00d1],
|
|
|
|
[7, 0x010c],
|
|
|
|
[8, 0x0068],
|
|
|
|
[8, 0x0028],
|
|
|
|
[9, 0x00b1],
|
|
|
|
[8, 0x0008],
|
|
|
|
[8, 0x0088],
|
|
|
|
[8, 0x0048],
|
|
|
|
[9, 0x00f1],
|
|
|
|
[7, 0x0102],
|
|
|
|
[8, 0x0054],
|
|
|
|
[8, 0x0014],
|
|
|
|
[8, 0x011c],
|
|
|
|
[7, 0x0112],
|
|
|
|
[8, 0x0074],
|
|
|
|
[8, 0x0034],
|
|
|
|
[9, 0x00c9],
|
|
|
|
[7, 0x010a],
|
|
|
|
[8, 0x0064],
|
|
|
|
[8, 0x0024],
|
|
|
|
[9, 0x00a9],
|
|
|
|
[8, 0x0004],
|
|
|
|
[8, 0x0084],
|
|
|
|
[8, 0x0044],
|
|
|
|
[9, 0x00e9],
|
|
|
|
[7, 0x0106],
|
|
|
|
[8, 0x005c],
|
|
|
|
[8, 0x001c],
|
|
|
|
[9, 0x0099],
|
|
|
|
[7, 0x0116],
|
|
|
|
[8, 0x007c],
|
|
|
|
[8, 0x003c],
|
|
|
|
[9, 0x00d9],
|
|
|
|
[7, 0x010e],
|
|
|
|
[8, 0x006c],
|
|
|
|
[8, 0x002c],
|
|
|
|
[9, 0x00b9],
|
|
|
|
[8, 0x000c],
|
|
|
|
[8, 0x008c],
|
|
|
|
[8, 0x004c],
|
|
|
|
[9, 0x00f9],
|
|
|
|
[7, 0x0101],
|
|
|
|
[8, 0x0052],
|
|
|
|
[8, 0x0012],
|
|
|
|
[8, 0x011a],
|
|
|
|
[7, 0x0111],
|
|
|
|
[8, 0x0072],
|
|
|
|
[8, 0x0032],
|
|
|
|
[9, 0x00c5],
|
|
|
|
[7, 0x0109],
|
|
|
|
[8, 0x0062],
|
|
|
|
[8, 0x0022],
|
|
|
|
[9, 0x00a5],
|
|
|
|
[8, 0x0002],
|
|
|
|
[8, 0x0082],
|
|
|
|
[8, 0x0042],
|
|
|
|
[9, 0x00e5],
|
|
|
|
[7, 0x0105],
|
|
|
|
[8, 0x005a],
|
|
|
|
[8, 0x001a],
|
|
|
|
[9, 0x0095],
|
|
|
|
[7, 0x0115],
|
|
|
|
[8, 0x007a],
|
|
|
|
[8, 0x003a],
|
|
|
|
[9, 0x00d5],
|
|
|
|
[7, 0x010d],
|
|
|
|
[8, 0x006a],
|
|
|
|
[8, 0x002a],
|
|
|
|
[9, 0x00b5],
|
|
|
|
[8, 0x000a],
|
|
|
|
[8, 0x008a],
|
|
|
|
[8, 0x004a],
|
|
|
|
[9, 0x00f5],
|
|
|
|
[7, 0x0103],
|
|
|
|
[8, 0x0056],
|
|
|
|
[8, 0x0016],
|
|
|
|
[8, 0x011e],
|
|
|
|
[7, 0x0113],
|
|
|
|
[8, 0x0076],
|
|
|
|
[8, 0x0036],
|
|
|
|
[9, 0x00cd],
|
|
|
|
[7, 0x010b],
|
|
|
|
[8, 0x0066],
|
|
|
|
[8, 0x0026],
|
|
|
|
[9, 0x00ad],
|
|
|
|
[8, 0x0006],
|
|
|
|
[8, 0x0086],
|
|
|
|
[8, 0x0046],
|
|
|
|
[9, 0x00ed],
|
|
|
|
[7, 0x0107],
|
|
|
|
[8, 0x005e],
|
|
|
|
[8, 0x001e],
|
|
|
|
[9, 0x009d],
|
|
|
|
[7, 0x0117],
|
|
|
|
[8, 0x007e],
|
|
|
|
[8, 0x003e],
|
|
|
|
[9, 0x00dd],
|
|
|
|
[7, 0x010f],
|
|
|
|
[8, 0x006e],
|
|
|
|
[8, 0x002e],
|
|
|
|
[9, 0x00bd],
|
|
|
|
[8, 0x000e],
|
|
|
|
[8, 0x008e],
|
|
|
|
[8, 0x004e],
|
|
|
|
[9, 0x00fd],
|
|
|
|
[7, 0x0100],
|
|
|
|
[8, 0x0051],
|
|
|
|
[8, 0x0011],
|
|
|
|
[8, 0x0119],
|
|
|
|
[7, 0x0110],
|
|
|
|
[8, 0x0071],
|
|
|
|
[8, 0x0031],
|
|
|
|
[9, 0x00c3],
|
|
|
|
[7, 0x0108],
|
|
|
|
[8, 0x0061],
|
|
|
|
[8, 0x0021],
|
|
|
|
[9, 0x00a3],
|
|
|
|
[8, 0x0001],
|
|
|
|
[8, 0x0081],
|
|
|
|
[8, 0x0041],
|
|
|
|
[9, 0x00e3],
|
|
|
|
[7, 0x0104],
|
|
|
|
[8, 0x0059],
|
|
|
|
[8, 0x0019],
|
|
|
|
[9, 0x0093],
|
|
|
|
[7, 0x0114],
|
|
|
|
[8, 0x0079],
|
|
|
|
[8, 0x0039],
|
|
|
|
[9, 0x00d3],
|
|
|
|
[7, 0x010c],
|
|
|
|
[8, 0x0069],
|
|
|
|
[8, 0x0029],
|
|
|
|
[9, 0x00b3],
|
|
|
|
[8, 0x0009],
|
|
|
|
[8, 0x0089],
|
|
|
|
[8, 0x0049],
|
|
|
|
[9, 0x00f3],
|
|
|
|
[7, 0x0102],
|
|
|
|
[8, 0x0055],
|
|
|
|
[8, 0x0015],
|
|
|
|
[8, 0x011d],
|
|
|
|
[7, 0x0112],
|
|
|
|
[8, 0x0075],
|
|
|
|
[8, 0x0035],
|
|
|
|
[9, 0x00cb],
|
|
|
|
[7, 0x010a],
|
|
|
|
[8, 0x0065],
|
|
|
|
[8, 0x0025],
|
|
|
|
[9, 0x00ab],
|
|
|
|
[8, 0x0005],
|
|
|
|
[8, 0x0085],
|
|
|
|
[8, 0x0045],
|
|
|
|
[9, 0x00eb],
|
|
|
|
[7, 0x0106],
|
|
|
|
[8, 0x005d],
|
|
|
|
[8, 0x001d],
|
|
|
|
[9, 0x009b],
|
|
|
|
[7, 0x0116],
|
|
|
|
[8, 0x007d],
|
|
|
|
[8, 0x003d],
|
|
|
|
[9, 0x00db],
|
|
|
|
[7, 0x010e],
|
|
|
|
[8, 0x006d],
|
|
|
|
[8, 0x002d],
|
|
|
|
[9, 0x00bb],
|
|
|
|
[8, 0x000d],
|
|
|
|
[8, 0x008d],
|
|
|
|
[8, 0x004d],
|
|
|
|
[9, 0x00fb],
|
|
|
|
[7, 0x0101],
|
|
|
|
[8, 0x0053],
|
|
|
|
[8, 0x0013],
|
|
|
|
[8, 0x011b],
|
|
|
|
[7, 0x0111],
|
|
|
|
[8, 0x0073],
|
|
|
|
[8, 0x0033],
|
|
|
|
[9, 0x00c7],
|
|
|
|
[7, 0x0109],
|
|
|
|
[8, 0x0063],
|
|
|
|
[8, 0x0023],
|
|
|
|
[9, 0x00a7],
|
|
|
|
[8, 0x0003],
|
|
|
|
[8, 0x0083],
|
|
|
|
[8, 0x0043],
|
|
|
|
[9, 0x00e7],
|
|
|
|
[7, 0x0105],
|
|
|
|
[8, 0x005b],
|
|
|
|
[8, 0x001b],
|
|
|
|
[9, 0x0097],
|
|
|
|
[7, 0x0115],
|
|
|
|
[8, 0x007b],
|
|
|
|
[8, 0x003b],
|
|
|
|
[9, 0x00d7],
|
|
|
|
[7, 0x010d],
|
|
|
|
[8, 0x006b],
|
|
|
|
[8, 0x002b],
|
|
|
|
[9, 0x00b7],
|
|
|
|
[8, 0x000b],
|
|
|
|
[8, 0x008b],
|
|
|
|
[8, 0x004b],
|
|
|
|
[9, 0x00f7],
|
|
|
|
[7, 0x0103],
|
|
|
|
[8, 0x0057],
|
|
|
|
[8, 0x0017],
|
|
|
|
[8, 0x011f],
|
|
|
|
[7, 0x0113],
|
|
|
|
[8, 0x0077],
|
|
|
|
[8, 0x0037],
|
|
|
|
[9, 0x00cf],
|
|
|
|
[7, 0x010b],
|
|
|
|
[8, 0x0067],
|
|
|
|
[8, 0x0027],
|
|
|
|
[9, 0x00af],
|
|
|
|
[8, 0x0007],
|
|
|
|
[8, 0x0087],
|
|
|
|
[8, 0x0047],
|
|
|
|
[9, 0x00ef],
|
|
|
|
[7, 0x0107],
|
|
|
|
[8, 0x005f],
|
|
|
|
[8, 0x001f],
|
|
|
|
[9, 0x009f],
|
|
|
|
[7, 0x0117],
|
|
|
|
[8, 0x007f],
|
|
|
|
[8, 0x003f],
|
|
|
|
[9, 0x00df],
|
|
|
|
[7, 0x010f],
|
|
|
|
[8, 0x006f],
|
|
|
|
[8, 0x002f],
|
|
|
|
[9, 0x00bf],
|
|
|
|
[8, 0x000f],
|
|
|
|
[8, 0x008f],
|
|
|
|
[8, 0x004f],
|
|
|
|
[9, 0x00ff]
|
2011-05-11 13:22:28 +09:00
|
|
|
], 9];
|
2011-05-10 10:04:22 +09:00
|
|
|
|
2011-05-11 13:22:28 +09:00
|
|
|
const fixedDistCodeTab = [[
|
2011-05-10 10:04:22 +09:00
|
|
|
[5, 0x0000],
|
|
|
|
[5, 0x0010],
|
|
|
|
[5, 0x0008],
|
|
|
|
[5, 0x0018],
|
|
|
|
[5, 0x0004],
|
|
|
|
[5, 0x0014],
|
|
|
|
[5, 0x000c],
|
|
|
|
[5, 0x001c],
|
|
|
|
[5, 0x0002],
|
|
|
|
[5, 0x0012],
|
|
|
|
[5, 0x000a],
|
|
|
|
[5, 0x001a],
|
|
|
|
[5, 0x0006],
|
|
|
|
[5, 0x0016],
|
|
|
|
[5, 0x000e],
|
|
|
|
[0, 0x0000],
|
|
|
|
[5, 0x0001],
|
|
|
|
[5, 0x0011],
|
|
|
|
[5, 0x0009],
|
|
|
|
[5, 0x0019],
|
|
|
|
[5, 0x0005],
|
|
|
|
[5, 0x0015],
|
|
|
|
[5, 0x000d],
|
|
|
|
[5, 0x001d],
|
|
|
|
[5, 0x0003],
|
|
|
|
[5, 0x0013],
|
|
|
|
[5, 0x000b],
|
|
|
|
[5, 0x001b],
|
|
|
|
[5, 0x0007],
|
|
|
|
[5, 0x0017],
|
|
|
|
[5, 0x000f],
|
|
|
|
[0, 0x0000]
|
2011-05-11 13:22:28 +09:00
|
|
|
], 5];
|
2011-05-09 09:39:40 +09:00
|
|
|
|
2011-05-11 19:26:33 +09:00
|
|
|
function constructor(stream) {
|
|
|
|
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;
|
2011-05-09 03:32:53 +09:00
|
|
|
}
|
2011-05-10 07:58:37 +09:00
|
|
|
|
|
|
|
constructor.prototype = {
|
2011-05-10 10:04:22 +09:00
|
|
|
getBits: function(bits) {
|
2011-05-11 13:22:28 +09:00
|
|
|
var stream = this.stream;
|
2011-05-10 10:04:22 +09:00
|
|
|
var codeSize = this.codeSize;
|
2011-05-11 13:22:28 +09:00
|
|
|
var codeBuf = this.codeBuf;
|
|
|
|
var b;
|
|
|
|
while (codeSize < bits) {
|
|
|
|
if ((b = stream.getByte()) == -1)
|
|
|
|
error("Bad encoding in flate stream");
|
|
|
|
codeBuf |= b << codeSize;
|
|
|
|
codeSize += 8;
|
|
|
|
}
|
|
|
|
b = codeBuf & ((1 << bits) - 1);
|
|
|
|
this.codeBuf = codeBuf >> bits;
|
|
|
|
this.codeSize = codeSize -= bits;
|
|
|
|
return b;
|
|
|
|
},
|
2011-05-11 15:57:03 +09:00
|
|
|
getCode: function(table) {
|
2011-05-11 19:26:33 +09:00
|
|
|
var codes = table[0];
|
|
|
|
var maxLen = table[1];
|
2011-05-11 15:57:03 +09:00
|
|
|
var codeSize = this.codeSize;
|
|
|
|
var codeBuf = this.codeBuf;
|
2011-05-11 19:26:33 +09:00
|
|
|
var stream = this.stream;
|
|
|
|
while (codeSize < maxLen) {
|
2011-05-11 15:57:03 +09:00
|
|
|
var b;
|
|
|
|
if ((b = stream.getByte()) == -1)
|
|
|
|
error("Bad encoding in flate stream");
|
|
|
|
codeBuf |= (b << codeSize);
|
|
|
|
codeSize += 8;
|
|
|
|
}
|
2011-05-11 19:26:33 +09:00
|
|
|
var code = codes[codeBuf & ((1 << maxLen) - 1)];
|
2011-05-11 15:57:03 +09:00
|
|
|
var codeLen = code[0];
|
|
|
|
var codeVal = code[1];
|
|
|
|
if (codeSize == 0|| codeSize < codeLen || codeLen == 0)
|
|
|
|
error("Bad encoding in flate stream");
|
|
|
|
this.codeBuf = (codeBuf >> codeLen);
|
|
|
|
this.codeLen = (codeLen - codeLen);
|
|
|
|
return codeVal;
|
|
|
|
},
|
2011-05-11 13:22:28 +09:00
|
|
|
ensureBuffer: function(requested, copy) {
|
|
|
|
var buffer = this.buffer;
|
|
|
|
var current = buffer ? buffer.byteLength : 0;
|
|
|
|
if (current < requested)
|
|
|
|
return buffer;
|
|
|
|
var size = 512;
|
|
|
|
while (size < requested)
|
|
|
|
size <<= 1;
|
|
|
|
var buffer2 = new Uint8Array(size);
|
|
|
|
if (copy) {
|
|
|
|
for (var i = 0; i < current; ++i)
|
|
|
|
buffer2[i] = buffer[i];
|
|
|
|
}
|
|
|
|
return this.buffer = buffer2;
|
|
|
|
},
|
2011-05-11 15:57:03 +09:00
|
|
|
lookChar: function() {
|
|
|
|
var bufferLength = this.bufferLength;
|
|
|
|
var bufferPos = this.bufferPos;
|
|
|
|
if (bufferLength == bufferPos) {
|
|
|
|
if (this.eof)
|
|
|
|
return;
|
|
|
|
this.readBlock();
|
|
|
|
}
|
|
|
|
return String.fromChar(this.buffer[bufferPos]);
|
|
|
|
},
|
|
|
|
getChar: function() {
|
|
|
|
var ch = this.lookChar();
|
|
|
|
if (!ch)
|
|
|
|
return;
|
|
|
|
this.pos++;
|
|
|
|
this.bufferPos++;
|
|
|
|
return ch;
|
|
|
|
},
|
|
|
|
skip: function(n) {
|
|
|
|
if (!n)
|
|
|
|
n = 1;
|
|
|
|
while (n-- > 0)
|
|
|
|
this.getChar();
|
2011-05-11 16:10:15 +09:00
|
|
|
},
|
2011-05-11 13:22:28 +09:00
|
|
|
generateHuffmanTable: function(lengths) {
|
|
|
|
var n = lengths.length;
|
|
|
|
|
|
|
|
// find max code length
|
|
|
|
var maxLen = 0;
|
|
|
|
for (var i = 0; i < n; ++i) {
|
|
|
|
if (lengths[i] > maxLen)
|
|
|
|
maxLen = lengths[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
// build the table
|
2011-05-11 19:26:33 +09:00
|
|
|
var size = 1 << maxLen;
|
2011-05-11 13:22:28 +09:00
|
|
|
var codes = new Array(size);
|
|
|
|
for (var len = 1, code = 0, skip = 2;
|
2011-05-12 08:30:30 +09:00
|
|
|
len <= maxLen;
|
2011-05-11 13:22:28 +09:00
|
|
|
++len, code <<= 1, skip <<= 1) {
|
|
|
|
for (var val = 0; val < n; ++val) {
|
|
|
|
if (lengths[val] == len) {
|
|
|
|
// bit-reverse the code
|
|
|
|
var code2 = 0;
|
|
|
|
var t = code;
|
|
|
|
for (var i = 0; i < len; ++i) {
|
|
|
|
code2 = (code2 << 1) | (t & 1);
|
|
|
|
t >>= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// fill the table entries
|
|
|
|
for (var i = code2; i < size; i += skip)
|
|
|
|
codes[i] = [len, val];
|
|
|
|
|
|
|
|
++code;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-11 19:26:33 +09:00
|
|
|
return [codes, maxLen];
|
2011-05-11 13:22:28 +09:00
|
|
|
},
|
|
|
|
readBlock: function() {
|
|
|
|
var stream = this.stream;
|
|
|
|
|
|
|
|
// read block header
|
|
|
|
var hdr = this.getBits(3);
|
|
|
|
if (hdr & 1)
|
|
|
|
this.eof = true;
|
|
|
|
hdr >>= 1;
|
|
|
|
|
|
|
|
var b;
|
|
|
|
if (hdr == 0) { // uncompressed block
|
|
|
|
if ((b = stream.getByte()) == -1)
|
|
|
|
error("Bad block header in flate stream");
|
|
|
|
var blockLen = b;
|
|
|
|
if ((b = stream.getByte()) == -1)
|
|
|
|
error("Bad block header in flate stream");
|
|
|
|
blockLen |= (b << 8);
|
|
|
|
if ((b = stream.getByte()) == -1)
|
|
|
|
error("Bad block header in flate stream");
|
|
|
|
var check = b;
|
|
|
|
if ((b = stream.getByte()) == -1)
|
|
|
|
error("Bad block header in flate stream");
|
|
|
|
check |= (b << 8);
|
|
|
|
if (check != (~this.blockLen & 0xffff))
|
|
|
|
error("Bad uncompressed block length in flate stream");
|
|
|
|
var buffer = this.ensureBuffer(blockLen);
|
|
|
|
this.bufferLength = blockLen;
|
2011-05-11 15:57:03 +09:00
|
|
|
this.bufferPos = 0;
|
2011-05-11 13:22:28 +09:00
|
|
|
for (var n = 0; n < blockLen; ++n) {
|
|
|
|
if ((b = stream.getByte()) == -1) {
|
|
|
|
this.eof = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
buffer[n] = b;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var litCodeTable;
|
|
|
|
var distCodeTable;
|
|
|
|
if (hdr == 1) { // compressed block, fixed codes
|
|
|
|
litCodeTable = fixedLitCodeTab;
|
|
|
|
distCodeTable = fixedDistCodeTab;
|
|
|
|
} else if (hdr == 2) { // compressed block, dynamic codes
|
|
|
|
var numLitCodes = this.getBits(5) + 257;
|
|
|
|
var numDistCodes = this.getBits(5) + 1;
|
|
|
|
var numCodeLenCodes = this.getBits(4) + 4;
|
|
|
|
|
|
|
|
// build the code lengths code table
|
|
|
|
var codeLenCodeLengths = Array(codeLenCodeMap.length);
|
|
|
|
var i = 0;
|
|
|
|
while (i < numCodeLenCodes)
|
|
|
|
codeLenCodeLengths[codeLenCodeMap[i++]] = this.getBits(3);
|
|
|
|
var codeLenCodeTab = this.generateHuffmanTable(codeLenCodeLengths);
|
|
|
|
|
|
|
|
// built the literal and distance code tables
|
|
|
|
var len = 0;
|
|
|
|
var i = 0;
|
|
|
|
var codes = numLitCodes + numDistCodes;
|
|
|
|
var codeLengths = new Array(codes);
|
|
|
|
while (i < codes) {
|
2011-05-12 08:30:30 +09:00
|
|
|
function repeat(stream, array, i, len, offset, what) {
|
|
|
|
var repeat = stream.getBits(len) + offset;
|
2011-05-11 13:22:28 +09:00
|
|
|
while (repeat-- > 0)
|
|
|
|
array[i++] = what;
|
|
|
|
}
|
|
|
|
var code = this.getCode(codeLenCodeTab);
|
|
|
|
if (code == 16) {
|
2011-05-12 08:30:30 +09:00
|
|
|
repeat(this, codeLengths, i, 2, 3, len);
|
2011-05-11 13:22:28 +09:00
|
|
|
} else if (code == 17) {
|
2011-05-12 08:30:30 +09:00
|
|
|
repeat(this, codeLengths, i, 3, 3, len = 0);
|
2011-05-11 13:22:28 +09:00
|
|
|
} else if (code == 18) {
|
2011-05-12 08:30:30 +09:00
|
|
|
repeat(this, codeLengths, i, 7, 11, len = 0);
|
2011-05-11 13:22:28 +09:00
|
|
|
} else {
|
2011-05-11 19:26:33 +09:00
|
|
|
codeLengths[i++] = len = code;
|
2011-05-11 13:22:28 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
litCodeTable = this.generateHuffmanTable(codeLengths.slice(0, numLitCodes));
|
|
|
|
distCodeTable = this.generateHuffmanTable(codeLengths.slice(numDistCodes, codes));
|
|
|
|
} else {
|
|
|
|
error("Unknown block type in flate stream");
|
|
|
|
}
|
|
|
|
|
|
|
|
var pos = 0;
|
|
|
|
while (true) {
|
|
|
|
var code1 = this.getCode(litCodeTable);
|
|
|
|
if (code1 == 256) {
|
|
|
|
this.bufferLength = pos;
|
2011-05-11 15:57:03 +09:00
|
|
|
this.bufferPos = 0;
|
2011-05-11 13:22:28 +09:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (code1 < 256) {
|
|
|
|
var buffer = this.ensureBuffer(pos + 1);
|
|
|
|
buffer[pos++] = code1;
|
|
|
|
} else {
|
|
|
|
code1 -= 257;
|
|
|
|
var code2 = lengthDecode[code1][0];
|
|
|
|
if (code2 > 0)
|
|
|
|
code2 = this.getBits(code2);
|
|
|
|
var len = lengthDecode[code1][1] + code2;
|
|
|
|
code1 = this.getCode(distCodeTable);
|
|
|
|
code2 = distDecode[code1][0];
|
|
|
|
if (code2 > 0)
|
|
|
|
code2 = this.getBits(code2);
|
|
|
|
var dist = distDecode[code1][1] + code2;
|
|
|
|
var buffer = this.ensureBuffer(pos + len);
|
|
|
|
for (var k = 0; k < len; ++k, ++pos)
|
|
|
|
buffer[pos] = buffer[pos - dist];
|
|
|
|
}
|
|
|
|
}
|
2011-05-10 10:04:22 +09:00
|
|
|
}
|
2011-05-10 07:58:37 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
return constructor;
|
2011-05-09 03:32:53 +09:00
|
|
|
})();
|
|
|
|
|
|
|
|
var DecryptStream = (function() {
|
2011-05-07 17:20:04 +09:00
|
|
|
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;
|
2011-04-26 15:33:36 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor.prototype = {
|
|
|
|
};
|
|
|
|
|
2011-05-07 09:27:27 +09:00
|
|
|
return constructor;
|
|
|
|
})();
|
2011-04-26 15:33:36 +09:00
|
|
|
|
2011-05-07 09:27:27 +09:00
|
|
|
var Cmd = (function() {
|
|
|
|
function constructor(cmd) {
|
|
|
|
this.cmd = cmd;
|
2011-05-05 06:58:51 +09:00
|
|
|
}
|
2011-05-07 09:27:27 +09:00
|
|
|
|
|
|
|
constructor.prototype = {
|
|
|
|
};
|
|
|
|
|
|
|
|
return constructor;
|
|
|
|
})();
|
|
|
|
|
|
|
|
var Dict = (function() {
|
|
|
|
function constructor() {
|
2011-05-09 06:26:08 +09:00
|
|
|
this.map = Object.create(null);
|
2011-04-26 15:33:36 +09:00
|
|
|
}
|
2011-05-07 09:27:27 +09:00
|
|
|
|
|
|
|
constructor.prototype = {
|
|
|
|
get: function(key) {
|
2011-05-09 06:26:08 +09:00
|
|
|
return this.map[key];
|
2011-05-07 09:27:27 +09:00
|
|
|
},
|
2011-05-11 19:14:08 +09:00
|
|
|
has: function(key) {
|
|
|
|
return key in this.map;
|
|
|
|
},
|
2011-05-07 09:27:27 +09:00
|
|
|
set: function(key, value) {
|
2011-05-09 06:26:08 +09:00
|
|
|
this.map[key] = value;
|
2011-05-05 07:15:26 +09:00
|
|
|
}
|
2011-05-07 09:27:27 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
return constructor;
|
|
|
|
})();
|
|
|
|
|
|
|
|
var Ref = (function() {
|
2011-05-10 07:58:37 +09:00
|
|
|
function constructor(num, gen) {
|
2011-05-07 09:27:27 +09:00
|
|
|
this.num = num;
|
2011-05-10 07:58:37 +09:00
|
|
|
this.gen = gen;
|
2011-05-05 07:15:26 +09:00
|
|
|
}
|
2011-04-26 15:33:36 +09:00
|
|
|
|
2011-05-07 09:27:27 +09:00
|
|
|
constructor.prototype = {
|
|
|
|
};
|
2011-04-26 15:33:36 +09:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2011-05-11 19:14:08 +09:00
|
|
|
function IsDict(v, type) {
|
|
|
|
return v instanceof Dict && (!type || v.get("Type").name == type);
|
2011-05-07 09:27:27 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
function IsArray(v) {
|
|
|
|
return v instanceof Array;
|
|
|
|
}
|
|
|
|
|
|
|
|
function IsStream(v) {
|
2011-05-11 19:26:33 +09:00
|
|
|
return typeof v == "object" && "getChar" in v;
|
2011-05-07 09:27:27 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
function IsRef(v) {
|
|
|
|
return v instanceof Ref;
|
|
|
|
}
|
|
|
|
|
|
|
|
var EOF = {};
|
|
|
|
|
|
|
|
function IsEOF(v) {
|
|
|
|
return v == EOF;
|
|
|
|
}
|
|
|
|
|
|
|
|
var None = {};
|
|
|
|
|
|
|
|
function IsNone(v) {
|
|
|
|
return v == None;
|
|
|
|
}
|
|
|
|
|
2011-04-26 15:33:36 +09:00
|
|
|
var Lexer = (function() {
|
2011-05-03 14:50:55 +09:00
|
|
|
function constructor(stream) {
|
|
|
|
this.stream = stream;
|
2011-04-26 15:33:36 +09:00
|
|
|
}
|
|
|
|
|
2011-05-09 03:32:53 +09:00
|
|
|
constructor.isSpace = function(ch) {
|
|
|
|
return ch == " " || ch == "\t";
|
|
|
|
}
|
|
|
|
|
2011-04-26 15:33:36 +09:00
|
|
|
// 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
|
2011-05-03 07:06:11 +09:00
|
|
|
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
|
2011-04-26 15:33:36 +09:00
|
|
|
];
|
|
|
|
|
|
|
|
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) {
|
2011-05-03 07:06:11 +09:00
|
|
|
if (ch >= "0" && ch <= "9")
|
|
|
|
return ch - "0";
|
|
|
|
ch = ch.toLowerCase();
|
|
|
|
if (ch >= "a" && ch <= "f")
|
|
|
|
return ch - "a";
|
|
|
|
return -1;
|
2011-04-26 15:33:36 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor.prototype = {
|
2011-05-03 07:06:11 +09:00
|
|
|
getNumber: function(ch) {
|
2011-04-26 15:33:36 +09:00
|
|
|
var floating = false;
|
2011-05-03 07:06:11 +09:00
|
|
|
var str = ch;
|
2011-05-03 14:50:55 +09:00
|
|
|
var stream = this.stream;
|
2011-05-03 07:06:11 +09:00
|
|
|
do {
|
2011-05-09 06:08:38 +09:00
|
|
|
ch = stream.lookChar();
|
2011-05-03 07:06:11 +09:00
|
|
|
if (ch == "." && !floating) {
|
|
|
|
str += ch;
|
|
|
|
floating = true;
|
|
|
|
} else if (ch == "-") {
|
|
|
|
// ignore minus signs in the middle of numbers to match
|
|
|
|
// Adobe's behavior
|
2011-05-10 10:10:15 +09:00
|
|
|
warn("Badly formated number");
|
2011-05-03 07:06:11 +09:00
|
|
|
} else if (ch >= "0" && ch <= "9") {
|
|
|
|
str += ch;
|
|
|
|
} else if (ch == "e" || ch == "E") {
|
|
|
|
floating = true;
|
|
|
|
} else {
|
2011-05-09 06:08:38 +09:00
|
|
|
// the last character doesn't belong to us
|
2011-05-03 07:06:11 +09:00
|
|
|
break;
|
|
|
|
}
|
2011-05-10 15:47:08 +09:00
|
|
|
stream.skip();
|
2011-05-03 07:06:11 +09:00
|
|
|
} while (true);
|
2011-05-06 18:18:31 +09:00
|
|
|
var value = parseFloat(str);
|
2011-05-03 07:06:11 +09:00
|
|
|
if (isNaN(value))
|
2011-05-10 10:10:15 +09:00
|
|
|
error("Invalid floating point number");
|
2011-05-07 09:27:27 +09:00
|
|
|
return value;
|
2011-05-03 07:06:11 +09:00
|
|
|
},
|
|
|
|
getString: function(ch) {
|
|
|
|
var n = 0;
|
2011-05-12 09:09:06 +09:00
|
|
|
var numParen = 1;
|
2011-05-03 07:06:11 +09:00
|
|
|
var done = false;
|
|
|
|
var str = ch;
|
2011-05-03 14:50:55 +09:00
|
|
|
var stream = this.stream;
|
2011-05-03 07:06:11 +09:00
|
|
|
do {
|
2011-05-03 14:50:55 +09:00
|
|
|
switch (ch = stream.getChar()) {
|
2011-05-03 18:17:17 +09:00
|
|
|
case undefined:
|
2011-05-10 10:10:15 +09:00
|
|
|
warn("Unterminated string");
|
2011-05-03 07:06:11 +09:00
|
|
|
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:
|
2011-05-10 10:10:15 +09:00
|
|
|
warn("Unterminated string");
|
2011-05-03 18:17:17 +09:00
|
|
|
done = true;
|
|
|
|
break;
|
2011-05-03 07:06:11 +09:00
|
|
|
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();
|
2011-05-03 07:06:11 +09:00
|
|
|
if (ch >= '0' && ch <= '7') {
|
2011-05-12 09:09:06 +09:00
|
|
|
stream.skip();
|
2011-05-03 07:06:11 +09:00
|
|
|
x = (x << 3) + (x - '0');
|
2011-05-03 14:50:55 +09:00
|
|
|
ch = stream.lookChar();
|
2011-05-03 07:06:11 +09:00
|
|
|
if (ch >= '0' && ch <= '7') {
|
2011-05-10 15:47:08 +09:00
|
|
|
stream.skip();
|
2011-05-03 07:06:11 +09:00
|
|
|
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')
|
2011-05-10 15:47:08 +09:00
|
|
|
stream.skip();
|
2011-05-03 07:06:11 +09:00
|
|
|
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;
|
2011-04-26 15:33:36 +09:00
|
|
|
},
|
2011-05-03 07:06:11 +09:00
|
|
|
getName: function(ch) {
|
|
|
|
var str = "";
|
2011-05-03 14:50:55 +09:00
|
|
|
var stream = this.stream;
|
2011-05-06 18:18:31 +09:00
|
|
|
while (!!(ch = stream.lookChar()) && !specialChars[ch.charCodeAt(0)]) {
|
2011-05-10 15:47:08 +09:00
|
|
|
stream.skip();
|
2011-05-03 07:06:11 +09:00
|
|
|
if (ch == "#") {
|
2011-05-03 14:50:55 +09:00
|
|
|
ch = stream.lookChar();
|
2011-05-03 07:06:11 +09:00
|
|
|
var x = ToHexDigit(ch);
|
|
|
|
if (x != -1) {
|
2011-05-10 15:47:08 +09:00
|
|
|
stream.skip();
|
2011-05-03 14:50:55 +09:00
|
|
|
var x2 = ToHexDigit(stream.getChar());
|
2011-05-03 07:06:11 +09:00
|
|
|
if (x2 == -1)
|
2011-05-10 10:10:15 +09:00
|
|
|
error("Illegal digit in hex char in name");
|
2011-05-03 07:06:11 +09:00
|
|
|
str += String.fromCharCode((x << 4) | x2);
|
|
|
|
} else {
|
|
|
|
str += "#";
|
|
|
|
str += ch;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
str += ch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (str.length > 128)
|
2011-05-10 10:10:15 +09:00
|
|
|
error("Warning: name token is longer than allowed by the specification");
|
2011-05-07 09:27:27 +09:00
|
|
|
return new Name(str);
|
2011-05-03 07:06:11 +09:00
|
|
|
},
|
|
|
|
getHexString: function(ch) {
|
|
|
|
var str = "";
|
2011-05-03 14:50:55 +09:00
|
|
|
var stream = this.stream;
|
2011-05-03 07:06:11 +09:00
|
|
|
while (1) {
|
2011-05-03 14:50:55 +09:00
|
|
|
ch = stream.getChar();
|
2011-05-03 07:06:11 +09:00
|
|
|
if (ch == '>') {
|
|
|
|
break;
|
2011-05-03 18:17:17 +09:00
|
|
|
} else if (!ch) {
|
2011-05-10 10:10:15 +09:00
|
|
|
warn("Unterminated hex string");
|
2011-05-03 07:06:11 +09:00
|
|
|
break;
|
2011-05-07 15:37:49 +09:00
|
|
|
} else if (specialChars[ch.charCodeAt(0)] != 1) {
|
2011-05-03 07:06:11 +09:00
|
|
|
var x, x2;
|
|
|
|
if (((x = ToHexDigit(ch)) == -1) ||
|
2011-05-07 15:37:49 +09:00
|
|
|
((x2 = ToHexDigit(stream.getChar())) == -1)) {
|
2011-05-10 10:10:15 +09:00
|
|
|
error("Illegal character in hex string");
|
2011-05-03 07:06:11 +09:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
str += String.fromCharCode((x << 4) | x2);
|
|
|
|
}
|
|
|
|
}
|
2011-05-07 09:27:27 +09:00
|
|
|
return str;
|
2011-05-03 07:06:11 +09:00
|
|
|
},
|
|
|
|
getObj: function() {
|
|
|
|
// skip whitespace and comments
|
|
|
|
var comment = false;
|
2011-05-03 14:50:55 +09:00
|
|
|
var stream = this.stream;
|
2011-05-06 18:18:31 +09:00
|
|
|
var ch;
|
2011-05-03 07:06:11 +09:00
|
|
|
while (true) {
|
2011-05-03 18:17:17 +09:00
|
|
|
if (!(ch = stream.getChar()))
|
2011-05-07 09:27:27 +09:00
|
|
|
return EOF;
|
2011-05-03 07:06:11 +09:00
|
|
|
if (comment) {
|
|
|
|
if (ch == '\r' || ch == '\n')
|
|
|
|
comment = false;
|
|
|
|
} else if (ch == '%') {
|
|
|
|
comment = true;
|
2011-05-06 18:18:31 +09:00
|
|
|
} else if (specialChars[ch.charCodeAt(0)] != 1) {
|
2011-05-03 07:06:11 +09:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// start reading token
|
2011-05-06 18:18:31 +09:00
|
|
|
switch (ch) {
|
2011-05-03 07:06:11 +09:00
|
|
|
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);
|
2011-05-03 07:06:11 +09:00
|
|
|
// hex string or dict punctuation
|
|
|
|
case '<':
|
2011-05-03 14:50:55 +09:00
|
|
|
ch = stream.lookChar();
|
2011-05-03 07:06:11 +09:00
|
|
|
if (ch == '<') {
|
|
|
|
// dict punctuation
|
2011-05-10 15:47:08 +09:00
|
|
|
stream.skip();
|
2011-05-07 14:44:01 +09:00
|
|
|
return new Cmd("<<");
|
2011-05-03 07:06:11 +09:00
|
|
|
}
|
|
|
|
return this.getHexString(ch);
|
|
|
|
// dict punctuation
|
|
|
|
case '>':
|
2011-05-03 14:50:55 +09:00
|
|
|
ch = stream.lookChar();
|
2011-05-03 07:06:11 +09:00
|
|
|
if (ch == '>') {
|
2011-05-10 15:47:08 +09:00
|
|
|
stream.skip();
|
2011-05-07 14:44:01 +09:00
|
|
|
return new Cmd(">>");
|
2011-05-03 07:06:11 +09:00
|
|
|
}
|
2011-04-26 15:33:36 +09:00
|
|
|
// fall through
|
2011-05-03 07:06:11 +09:00
|
|
|
case ')':
|
|
|
|
case '{':
|
|
|
|
case '}':
|
2011-05-10 10:10:15 +09:00
|
|
|
error("Illegal character");
|
2011-05-07 09:27:27 +09:00
|
|
|
return Error;
|
2011-05-03 07:06:11 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// command
|
|
|
|
var str = ch;
|
2011-05-06 18:18:31 +09:00
|
|
|
while (!!(ch = stream.lookChar()) && !specialChars[ch.charCodeAt(0)]) {
|
2011-05-10 15:47:08 +09:00
|
|
|
stream.skip();
|
2011-05-03 07:06:11 +09:00
|
|
|
if (str.length == 128) {
|
2011-05-10 10:10:15 +09:00
|
|
|
error("Command token too long");
|
2011-05-03 07:06:11 +09:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
str += ch;
|
|
|
|
}
|
|
|
|
if (str == "true")
|
2011-05-07 09:27:27 +09:00
|
|
|
return true;
|
2011-05-03 07:06:11 +09:00
|
|
|
if (str == "false")
|
2011-05-07 09:27:27 +09:00
|
|
|
return false;
|
2011-05-03 07:06:11 +09:00
|
|
|
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")
|
2011-05-10 15:47:08 +09:00
|
|
|
stream.skip();
|
2011-05-07 15:37:49 +09:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2011-05-03 07:06:11 +09:00
|
|
|
}
|
2011-04-26 15:33:36 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
return constructor;
|
|
|
|
})();
|
|
|
|
|
|
|
|
var Parser = (function() {
|
|
|
|
function constructor(lexer, allowStreams) {
|
2011-05-03 07:06:11 +09:00
|
|
|
this.lexer = lexer;
|
|
|
|
this.allowStreams = allowStreams;
|
|
|
|
this.inlineImg = 0;
|
|
|
|
this.refill();
|
2011-04-26 15:33:36 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor.prototype = {
|
2011-05-03 07:06:11 +09:00
|
|
|
refill: function() {
|
2011-05-06 18:18:31 +09:00
|
|
|
this.buf1 = this.lexer.getObj();
|
|
|
|
this.buf2 = this.lexer.getObj();
|
2011-05-03 07:16:03 +09:00
|
|
|
},
|
2011-05-03 07:06:11 +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")) {
|
2011-05-10 15:44:25 +09:00
|
|
|
this.lexer.skip(); // skip char after 'ID' command
|
2011-05-03 07:06:11 +09:00
|
|
|
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();
|
2011-05-03 07:06:11 +09:00
|
|
|
},
|
|
|
|
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))
|
2011-05-10 10:10:15 +09:00
|
|
|
error("End of file inside array");
|
2011-05-03 07:06:11 +09:00
|
|
|
this.shift();
|
2011-05-07 09:27:27 +09:00
|
|
|
return array;
|
|
|
|
} else if (IsCmd(this.buf1, "<<")) { // dictionary or stream
|
2011-05-03 07:06:11 +09:00
|
|
|
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)) {
|
2011-05-10 10:10:15 +09:00
|
|
|
error("Dictionary key must be a name object");
|
2011-05-03 07:06:11 +09:00
|
|
|
shift();
|
|
|
|
} else {
|
2011-05-07 14:44:01 +09:00
|
|
|
var key = this.buf1.name;
|
2011-05-03 07:06:11 +09:00
|
|
|
this.shift();
|
2011-05-10 10:10:15 +09:00
|
|
|
if (IsEOF(this.buf1))
|
2011-05-03 07:06:11 +09:00
|
|
|
break;
|
2011-05-07 09:27:27 +09:00
|
|
|
dict.set(key, this.getObj());
|
2011-05-03 07:06:11 +09:00
|
|
|
}
|
|
|
|
}
|
2011-05-07 09:27:27 +09:00
|
|
|
if (IsEOF(this.buf1))
|
2011-05-10 10:10:15 +09:00
|
|
|
error("End of file inside dictionary");
|
2011-05-03 07:06:11 +09:00
|
|
|
|
|
|
|
// 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);
|
2011-05-03 07:06:11 +09:00
|
|
|
} else {
|
|
|
|
this.shift();
|
|
|
|
}
|
2011-05-07 09:27:27 +09:00
|
|
|
return dict;
|
2011-05-03 07:06:11 +09:00
|
|
|
|
2011-05-07 09:27:27 +09:00
|
|
|
} else if (IsInt(this.buf1)) { // indirect reference or integer
|
|
|
|
var num = this.buf1;
|
2011-05-03 07:06:11 +09:00
|
|
|
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);
|
2011-05-03 07:06:11 +09:00
|
|
|
this.shift();
|
|
|
|
this.shift();
|
2011-05-07 09:27:27 +09:00
|
|
|
return ref;
|
2011-05-03 07:06:11 +09:00
|
|
|
}
|
2011-05-07 09:27:27 +09:00
|
|
|
return num;
|
|
|
|
} else if (IsString(this.buf1)) { // string
|
2011-05-07 17:20:04 +09:00
|
|
|
var str = this.buf1;
|
2011-05-03 07:06:11 +09:00
|
|
|
this.shift();
|
2011-05-07 17:20:04 +09:00
|
|
|
if (this.fileKey) {
|
|
|
|
var decrypt = new DecryptStream(new StringStream(str),
|
|
|
|
this.fileKey,
|
|
|
|
this.encAlgorithm,
|
|
|
|
this.keyLength);
|
2011-05-10 15:37:21 +09:00
|
|
|
var str = "";
|
|
|
|
var pos = decrypt.pos;
|
|
|
|
var length = decrypt.length;
|
|
|
|
while (pos++ > length)
|
|
|
|
str += decrypt.getChar();
|
2011-05-07 17:20:04 +09:00
|
|
|
}
|
2011-05-07 09:27:27 +09:00
|
|
|
return str;
|
2011-05-03 07:06:11 +09:00
|
|
|
}
|
2011-04-26 15:33:36 +09:00
|
|
|
|
2011-05-03 07:06:11 +09:00
|
|
|
// 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"))) {
|
2011-05-10 10:10:15 +09:00
|
|
|
error("Bad 'Length' attribute in stream");
|
2011-05-07 15:37:49 +09:00
|
|
|
lenght = 0;
|
|
|
|
}
|
|
|
|
|
2011-05-07 17:20:04 +09:00
|
|
|
// skip over the stream data
|
|
|
|
stream.pos = pos + length;
|
|
|
|
this.shift(); // '>>'
|
|
|
|
this.shift(); // 'stream'
|
|
|
|
if (!IsCmd(this.buf1, "endstream"))
|
2011-05-10 10:10:15 +09:00
|
|
|
error("Missing 'endstream'");
|
2011-05-07 17:20:04 +09:00
|
|
|
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))
|
2011-05-10 10:10:15 +09:00
|
|
|
error("Bad filter name");
|
2011-05-07 17:20:04 +09:00
|
|
|
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) {
|
2011-05-11 19:26:33 +09:00
|
|
|
if (name == "FlateDecode" || name == "Fl") {
|
|
|
|
if (params)
|
|
|
|
error("params not supported yet for FlateDecode");
|
|
|
|
return new FlateStream(stream);
|
|
|
|
} else {
|
|
|
|
error("filter '" + name + "' not supported yet");
|
|
|
|
}
|
2011-05-07 17:20:04 +09:00
|
|
|
return stream;
|
2011-05-03 07:06:11 +09:00
|
|
|
}
|
2011-04-26 15:33:36 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
return constructor;
|
|
|
|
})();
|
|
|
|
|
2011-05-09 03:32:53 +09:00
|
|
|
var Linearization = (function() {
|
2011-05-03 14:50:55 +09:00
|
|
|
function constructor(stream) {
|
|
|
|
this.parser = new Parser(new Lexer(stream), false);
|
2011-05-03 07:06:11 +09:00
|
|
|
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;
|
2011-05-03 07:06:11 +09:00
|
|
|
}
|
2011-04-26 15:33:36 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor.prototype = {
|
2011-05-03 07:16:03 +09:00
|
|
|
getInt: function(name) {
|
2011-05-03 07:06:11 +09:00
|
|
|
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;
|
2011-05-03 07:06:11 +09:00
|
|
|
}
|
2011-05-10 10:10:15 +09:00
|
|
|
error("'" + name + "' field in linearization table is invalid");
|
2011-05-03 07:06:11 +09:00
|
|
|
return 0;
|
|
|
|
},
|
2011-05-03 07:16:03 +09:00
|
|
|
getHint: function(index) {
|
2011-05-03 07:06:11 +09:00
|
|
|
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;
|
2011-05-03 07:06:11 +09:00
|
|
|
}
|
2011-05-10 10:10:15 +09:00
|
|
|
error("Hints table in linearization table is invalid");
|
2011-05-03 07:06:11 +09:00
|
|
|
return 0;
|
|
|
|
},
|
|
|
|
get length() {
|
2011-05-07 09:27:27 +09:00
|
|
|
if (!IsDict(this.linDict))
|
2011-05-06 18:18:31 +09:00
|
|
|
return 0;
|
2011-05-03 07:06:11 +09:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
};
|
2011-05-06 18:18:31 +09:00
|
|
|
|
|
|
|
return constructor;
|
2011-04-26 15:33:36 +09:00
|
|
|
})();
|
|
|
|
|
2011-05-09 03:32:53 +09:00
|
|
|
var XRef = (function() {
|
2011-05-07 04:12:57 +09:00
|
|
|
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-10 07:58:37 +09:00
|
|
|
|
|
|
|
// get the root dictionary (catalog) object
|
2011-05-11 17:50:47 +09:00
|
|
|
if (!IsRef(this.root = this.trailerDict.get("Root")))
|
|
|
|
error("Invalid root reference");
|
2011-05-11 16:10:15 +09:00
|
|
|
|
2011-05-11 17:50:47 +09:00
|
|
|
// prepare the XRef cache
|
|
|
|
this.cache = Object.create(null);
|
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-11 16:10:15 +09:00
|
|
|
error("Invalid XRef table");
|
2011-05-07 09:27:27 +09:00
|
|
|
var first = obj;
|
|
|
|
if (!IsInt(obj = parser.getObj()))
|
2011-05-11 16:10:15 +09:00
|
|
|
error("Invalid XRef table");
|
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))
|
2011-05-11 16:10:15 +09:00
|
|
|
error("Invalid XRef table");
|
2011-05-07 08:38:16 +09:00
|
|
|
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-11 16:10:15 +09:00
|
|
|
error("Invalid XRef table");
|
2011-05-07 09:27:27 +09:00
|
|
|
entry.offset = obj;
|
|
|
|
if (!IsInt(obj = parser.getObj()))
|
2011-05-11 16:10:15 +09:00
|
|
|
error("Invalid XRef table");
|
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 {
|
2011-05-11 16:10:15 +09:00
|
|
|
error("Invalid XRef table");
|
2011-05-07 08:38:16 +09:00
|
|
|
}
|
|
|
|
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()))
|
2011-05-11 16:10:15 +09:00
|
|
|
error("Invalid XRef table");
|
2011-05-07 15:37:49 +09:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
2011-05-10 07:58:37 +09:00
|
|
|
this.trailerDict = dict;
|
2011-05-07 15:37:49 +09:00
|
|
|
|
|
|
|
// check for 'XRefStm' key
|
|
|
|
if (IsInt(obj = dict.get("XRefStm"))) {
|
|
|
|
var pos = obj;
|
|
|
|
if (pos in this.xrefstms)
|
2011-05-11 16:10:15 +09:00
|
|
|
error("Invalid XRef table");
|
2011-05-07 15:37:49 +09:00
|
|
|
this.xrefstms[pos] = 1; // avoid infinite recursion
|
|
|
|
this.readXRef(pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
return more;
|
2011-05-07 08:18:13 +09:00
|
|
|
},
|
|
|
|
readXRefStream: function(parser) {
|
2011-05-11 16:10:15 +09:00
|
|
|
error("Invalid XRef stream");
|
2011-05-07 08:18:13 +09:00
|
|
|
},
|
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-11 16:10:15 +09:00
|
|
|
error("Invalid XRef stream");
|
2011-05-07 08:18:13 +09:00
|
|
|
}
|
2011-05-07 09:27:27 +09:00
|
|
|
return this.readXRefStream(obj);
|
2011-05-07 08:18:13 +09:00
|
|
|
}
|
2011-05-11 16:10:15 +09:00
|
|
|
error("Invalid XRef");
|
2011-05-11 17:20:12 +09:00
|
|
|
},
|
|
|
|
getEntry: function(i) {
|
|
|
|
var e = this.entries[i];
|
|
|
|
if (e.free)
|
|
|
|
error("reading an XRef stream not implemented yet");
|
|
|
|
return e;
|
|
|
|
},
|
2011-05-11 17:50:47 +09:00
|
|
|
fetch: function(ref) {
|
|
|
|
var num = ref.num;
|
|
|
|
var e = this.cache[num];
|
|
|
|
if (e)
|
|
|
|
return e;
|
|
|
|
e = this.getEntry(num);
|
|
|
|
var gen = ref.gen;
|
2011-05-11 17:20:12 +09:00
|
|
|
if (e.uncompressed) {
|
|
|
|
if (e.gen != gen)
|
|
|
|
throw("inconsistent generation in XRef");
|
|
|
|
var stream = this.stream.makeSubStream(e.offset);
|
2011-05-11 19:14:08 +09:00
|
|
|
var parser = new Parser(new Lexer(stream), true);
|
2011-05-11 17:20:12 +09:00
|
|
|
var obj1 = parser.getObj();
|
|
|
|
var obj2 = parser.getObj();
|
|
|
|
var obj3 = parser.getObj();
|
|
|
|
if (!IsInt(obj1) || obj1 != num ||
|
|
|
|
!IsInt(obj2) || obj2 != gen ||
|
|
|
|
!IsCmd(obj3)) {
|
|
|
|
error("bad XRef entry");
|
|
|
|
}
|
|
|
|
if (!IsCmd(obj3, "obj")) {
|
|
|
|
// some bad pdfs use "obj1234" and really mean 1234
|
|
|
|
if (obj3.cmd.indexOf("obj") == 0) {
|
|
|
|
var num = parseInt(obj3.cmd.substring(3));
|
|
|
|
if (!isNaN(num))
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
error("bad XRef entry");
|
|
|
|
}
|
2011-05-11 17:50:47 +09:00
|
|
|
return this.cache[num] = parser.getObj();
|
2011-05-11 17:20:12 +09:00
|
|
|
}
|
2011-05-11 17:50:47 +09:00
|
|
|
error("compressed entry");
|
2011-05-11 17:20:12 +09:00
|
|
|
},
|
|
|
|
getCatalogObj: function() {
|
2011-05-11 17:50:47 +09:00
|
|
|
return this.fetch(this.root);
|
2011-05-07 08:18:13 +09:00
|
|
|
}
|
2011-05-07 04:12:57 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
return constructor;
|
|
|
|
})();
|
|
|
|
|
2011-05-11 19:14:08 +09:00
|
|
|
var Page = (function() {
|
|
|
|
function constructor(xref, pageNumber, pageDict) {
|
|
|
|
this.xref = xref;
|
|
|
|
this.pageNumber = pageNumber;
|
|
|
|
this.pageDict = pageDict;
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor.prototype = {
|
|
|
|
get contents() {
|
|
|
|
var obj = this.pageDict.get("Contents");
|
|
|
|
if (IsRef(obj))
|
|
|
|
obj = this.xref.fetch(obj);
|
2011-05-12 09:09:06 +09:00
|
|
|
if (!IsStream(obj))
|
2011-05-11 19:14:08 +09:00
|
|
|
error("invalid page contents object");
|
|
|
|
return this.contents = obj;
|
2011-05-12 09:09:06 +09:00
|
|
|
},
|
|
|
|
display: function() {
|
|
|
|
var stream = this.contents;
|
|
|
|
var parser = new Parser(new Lexer(stream), false);
|
|
|
|
var obj;
|
|
|
|
while (!IsEOF(obj = parser.getObj())) {
|
|
|
|
print(uneval(obj));
|
|
|
|
}
|
2011-05-11 19:14:08 +09:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return constructor;
|
|
|
|
})();
|
|
|
|
|
2011-05-11 17:20:12 +09:00
|
|
|
var Catalog = (function() {
|
|
|
|
function constructor(xref) {
|
|
|
|
this.xref = xref;
|
2011-05-11 17:50:47 +09:00
|
|
|
var obj = xref.getCatalogObj();
|
|
|
|
if (!IsDict(obj))
|
|
|
|
error("catalog object is not a dictionary");
|
|
|
|
this.catDict = obj;
|
2011-05-11 17:20:12 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor.prototype = {
|
2011-05-11 19:14:08 +09:00
|
|
|
get toplevelPagesDict() {
|
2011-05-11 17:50:47 +09:00
|
|
|
var obj = this.catDict.get("Pages");
|
|
|
|
if (!IsRef(obj))
|
|
|
|
error("invalid top-level pages reference");
|
|
|
|
var obj = this.xref.fetch(obj);
|
|
|
|
if (!IsDict(obj))
|
|
|
|
error("invalid top-level pages dictionary");
|
|
|
|
// shadow the prototype getter
|
2011-05-11 19:14:08 +09:00
|
|
|
return this.toplevelPagesDict = obj;
|
2011-05-11 17:50:47 +09:00
|
|
|
},
|
|
|
|
get numPages() {
|
2011-05-11 19:14:08 +09:00
|
|
|
obj = this.toplevelPagesDict.get("Count");
|
2011-05-11 17:50:47 +09:00
|
|
|
if (!IsInt(obj))
|
|
|
|
error("page count in top level pages object is not an integer");
|
|
|
|
// shadow the prototype getter
|
|
|
|
return this.numPages = obj;
|
2011-05-11 19:14:08 +09:00
|
|
|
},
|
|
|
|
traverseKids: function(pagesDict) {
|
|
|
|
var pageCache = this.pageCache;
|
|
|
|
var kids = pagesDict.get("Kids");
|
|
|
|
if (!IsArray(kids))
|
|
|
|
error("page dictionary kids object is not an array");
|
|
|
|
for (var i = 0; i < kids.length; ++i) {
|
|
|
|
var kid = kids[i];
|
|
|
|
if (!IsRef(kid))
|
|
|
|
error("page dictionary kid is not a reference");
|
|
|
|
var obj = this.xref.fetch(kid);
|
|
|
|
if (IsDict(obj, "Page") || (IsDict(obj) && !obj.has("Kids"))) {
|
|
|
|
pageCache.push(new Page(this.xref, pageCache.length, obj));
|
|
|
|
} else if (IsDict(obj)) { // must be a child page dictionary
|
|
|
|
this.traverseKids(obj);
|
|
|
|
} else {
|
|
|
|
error("page dictionary kid reference points to wrong type of object");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
getPage: function(n) {
|
|
|
|
var pageCache = this.pageCache;
|
|
|
|
if (!pageCache) {
|
|
|
|
pageCache = this.pageCache = [];
|
|
|
|
this.traverseKids(this.toplevelPagesDict);
|
|
|
|
}
|
|
|
|
return this.pageCache[n];
|
2011-05-11 17:50:47 +09:00
|
|
|
}
|
2011-05-11 17:20:12 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
return constructor;
|
|
|
|
})();
|
|
|
|
|
2011-05-09 03:32:53 +09:00
|
|
|
var PDFDoc = (function() {
|
2011-05-03 14:50:55 +09:00
|
|
|
function constructor(stream) {
|
2011-05-06 18:18:31 +09:00
|
|
|
this.stream = stream;
|
|
|
|
this.setup();
|
2011-04-26 15:33:36 +09:00
|
|
|
}
|
|
|
|
|
2011-05-10 15:32:22 +09:00
|
|
|
function find(stream, needle, limit, backwards) {
|
|
|
|
var length = stream.length;
|
|
|
|
var pos = stream.pos;
|
|
|
|
var str = "";
|
|
|
|
if (pos + limit > length)
|
|
|
|
limit = length - pos;
|
|
|
|
for (var n = 0; n < limit; ++n)
|
|
|
|
str += stream.getChar();
|
|
|
|
stream.pos = pos;
|
|
|
|
var index = backwards ? str.lastIndexOf(needle) : str.indexOf(needle);
|
|
|
|
if (index == -1)
|
|
|
|
return false; /* not found */
|
|
|
|
stream.pos += index;
|
|
|
|
return true; /* found */
|
|
|
|
}
|
|
|
|
|
2011-05-03 07:34:59 +09:00
|
|
|
constructor.prototype = {
|
2011-05-03 09:20:08 +09:00
|
|
|
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
|
2011-05-03 10:26:18 +09:00
|
|
|
return this.linearization = linearization;
|
2011-05-03 07:34:59 +09:00
|
|
|
},
|
2011-05-03 09:20:08 +09:00
|
|
|
get startXRef() {
|
2011-05-06 18:18:31 +09:00
|
|
|
var stream = this.stream;
|
2011-05-06 17:16:09 +09:00
|
|
|
var startXRef = 0;
|
2011-05-03 09:20:08 +09:00
|
|
|
var linearization = this.linearization;
|
|
|
|
if (linearization) {
|
2011-05-06 17:16:09 +09:00
|
|
|
// Find end of first obj.
|
|
|
|
stream.reset();
|
2011-05-10 15:32:22 +09:00
|
|
|
if (find(stream, "endobj", 1024))
|
2011-05-06 17:16:09 +09:00
|
|
|
startXRef = stream.pos + 6;
|
2011-05-03 09:20:08 +09:00
|
|
|
} 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;
|
2011-05-10 15:32:22 +09:00
|
|
|
if (find(stream, "startxref", 1024, true)) {
|
2011-05-06 17:16:09 +09:00
|
|
|
stream.skip(9);
|
|
|
|
var ch;
|
2011-05-09 03:32:53 +09:00
|
|
|
while (Lexer.isSpace(ch = stream.getChar()))
|
2011-05-06 17:16:09 +09:00
|
|
|
;
|
|
|
|
var str = "";
|
|
|
|
while ((ch - "0") <= 9) {
|
|
|
|
str += ch;
|
|
|
|
ch = stream.getChar();
|
|
|
|
}
|
2011-05-06 18:18:31 +09:00
|
|
|
startXRef = parseInt(str);
|
2011-05-06 17:16:09 +09:00
|
|
|
if (isNaN(startXRef))
|
|
|
|
startXRef = 0;
|
|
|
|
}
|
2011-05-03 09:20:08 +09:00
|
|
|
}
|
2011-05-06 17:16:09 +09:00
|
|
|
// shadow the prototype getter with a data property
|
2011-05-03 10:26:18 +09:00
|
|
|
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.
|
2011-05-06 18:18:31 +09:00
|
|
|
checkHeader: function() {
|
|
|
|
var stream = this.stream;
|
2011-05-03 14:50:55 +09:00
|
|
|
stream.reset();
|
2011-05-10 15:32:22 +09:00
|
|
|
if (find(stream, "%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);
|
2011-05-11 17:20:12 +09:00
|
|
|
this.catalog = new Catalog(this.xref);
|
2011-05-11 17:50:47 +09:00
|
|
|
},
|
|
|
|
get numPages() {
|
|
|
|
var linearization = this.linearization;
|
|
|
|
var num = linearization
|
|
|
|
? linearization.numPages
|
|
|
|
: this.catalog.numPages;
|
|
|
|
// overwrite the prototype getter
|
|
|
|
return this.numPages = num;
|
|
|
|
},
|
2011-05-11 19:14:08 +09:00
|
|
|
getPage: function(n) {
|
|
|
|
var linearization = this.linearization;
|
|
|
|
if (linearization) {
|
|
|
|
error("linearized page access not implemented");
|
|
|
|
}
|
|
|
|
return this.catalog.getPage(n);
|
2011-05-03 07:34:59 +09:00
|
|
|
}
|
|
|
|
};
|
2011-05-06 18:18:31 +09:00
|
|
|
|
|
|
|
return constructor;
|
2011-05-03 07:34:59 +09:00
|
|
|
})();
|
2011-05-05 04:46:39 +09:00
|
|
|
|
|
|
|
var Interpreter = (function() {
|
2011-05-07 14:15:18 +09:00
|
|
|
function constructor(xref, resources, catalog, gfx) {
|
2011-05-05 04:46:39 +09:00
|
|
|
this.xref = xref;
|
2011-05-06 02:28:37 +09:00
|
|
|
this.res = resources;
|
2011-05-05 04:46:39 +09:00
|
|
|
this.catalog = catalog;
|
2011-05-07 14:15:18 +09:00
|
|
|
this.gfx = gfx;
|
2011-05-10 05:18:19 +09:00
|
|
|
var env = this;
|
2011-05-07 14:15:18 +09:00
|
|
|
this.map = {
|
|
|
|
// Graphics state
|
|
|
|
w: gfx.setLineWidth,
|
2011-05-10 08:57:23 +09:00
|
|
|
J: gfx.setLineCap,
|
2011-05-10 09:11:40 +09:00
|
|
|
j: gfx.setLineJoin,
|
2011-05-07 14:15:18 +09:00
|
|
|
d: gfx.setDash,
|
2011-05-10 10:16:06 +09:00
|
|
|
ri: gfx.setRenderingIntent,
|
|
|
|
i: gfx.setFlatness,
|
2011-05-07 14:15:18 +09:00
|
|
|
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,
|
2011-05-10 11:08:21 +09:00
|
|
|
"f*": gfx.eoFill,
|
2011-05-07 14:15:18 +09:00
|
|
|
B: gfx.fillStroke,
|
|
|
|
b: gfx.closeFillStroke,
|
2011-05-10 10:16:06 +09:00
|
|
|
n: gfx.endPath,
|
2011-05-07 14:15:18 +09:00
|
|
|
|
|
|
|
// Clipping
|
2011-05-10 11:05:33 +09:00
|
|
|
W: gfx.clip,
|
|
|
|
"W*": gfx.eoClip,
|
2011-05-07 14:15:18 +09:00
|
|
|
|
|
|
|
// Text
|
|
|
|
BT: gfx.beginText,
|
|
|
|
ET: gfx.endText,
|
2011-05-10 05:18:19 +09:00
|
|
|
Tf: function(fontRef, size) {
|
|
|
|
var font = env.res.Font[fontRef.name]
|
|
|
|
gfx.setFont(font, size);
|
|
|
|
},
|
2011-05-07 14:15:18 +09:00
|
|
|
Td: gfx.moveText,
|
2011-05-10 10:16:06 +09:00
|
|
|
Tm: gfx.setTextMatrix,
|
2011-05-07 14:15:18 +09:00
|
|
|
Tj: gfx.showText,
|
2011-05-10 08:20:04 +09:00
|
|
|
TJ: gfx.showSpacedText,
|
2011-05-07 14:15:18 +09:00
|
|
|
|
|
|
|
// Type3 fonts
|
|
|
|
|
|
|
|
// Color
|
2011-05-10 10:16:06 +09:00
|
|
|
CS: gfx.setStrokeColorSpace,
|
|
|
|
cs: gfx.setFillColorSpace,
|
|
|
|
SC: gfx.setStrokeColor,
|
2011-05-10 11:19:26 +09:00
|
|
|
SCN: gfx.setStrokeColorN,
|
2011-05-10 10:16:06 +09:00
|
|
|
sc: gfx.setFillColor,
|
2011-05-10 11:19:26 +09:00
|
|
|
scn: gfx.setFillColorN,
|
2011-05-07 14:15:18 +09:00
|
|
|
g: gfx.setFillGray,
|
|
|
|
RG: gfx.setStrokeRGBColor,
|
|
|
|
rg: gfx.setFillRGBColor,
|
|
|
|
|
|
|
|
// Shading
|
2011-05-10 11:14:09 +09:00
|
|
|
sh: gfx.shadingFill,
|
|
|
|
|
2011-05-07 14:15:18 +09:00
|
|
|
// Images
|
|
|
|
// XObjects
|
2011-05-10 10:16:06 +09:00
|
|
|
Do: gfx.paintXObject,
|
|
|
|
|
2011-05-07 14:15:18 +09:00
|
|
|
// Marked content
|
|
|
|
// Compatibility
|
|
|
|
};
|
2011-05-05 04:46:39 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor.prototype = {
|
2011-05-07 10:15:51 +09:00
|
|
|
compile: function(parser) {
|
|
|
|
},
|
2011-05-05 04:46:39 +09:00
|
|
|
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;
|
2011-05-07 14:15:18 +09:00
|
|
|
var map = this.map;
|
2011-05-05 04:46:39 +09:00
|
|
|
var obj;
|
2011-05-07 09:27:27 +09:00
|
|
|
while (!IsEOF(obj = parser.getObj())) {
|
|
|
|
if (IsCmd(obj)) {
|
|
|
|
var cmd = obj.cmd;
|
2011-05-07 14:15:18 +09:00
|
|
|
var fn = map[cmd];
|
2011-05-10 10:16:06 +09:00
|
|
|
if (fn)
|
|
|
|
// TODO figure out how to type-check vararg functions
|
2011-05-07 10:15:51 +09:00
|
|
|
fn.apply(gfx, args);
|
2011-05-10 10:16:06 +09:00
|
|
|
else
|
2011-05-10 10:10:15 +09:00
|
|
|
error("Unknown command '" + cmd + "'");
|
2011-05-05 13:53:55 +09:00
|
|
|
args.length = 0;
|
2011-05-05 04:46:39 +09:00
|
|
|
} else {
|
2011-05-07 15:37:49 +09:00
|
|
|
if (args.length > 33)
|
2011-05-10 10:10:15 +09:00
|
|
|
error("Too many arguments '" + cmd + "'");
|
2011-05-07 10:15:51 +09:00
|
|
|
args.push(obj);
|
2011-05-05 04:46:39 +09:00
|
|
|
}
|
|
|
|
}
|
2011-05-06 12:20:07 +09:00
|
|
|
this.gfx.endDrawing();
|
2011-05-10 10:10:15 +09:00
|
|
|
}
|
2011-05-05 04:46:39 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
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 12:20:07 +09:00
|
|
|
},
|
2011-05-06 13:46:54 +09:00
|
|
|
endDrawing: function() {
|
2011-05-06 12:20:07 +09:00
|
|
|
},
|
|
|
|
|
2011-05-05 04:46:39 +09:00
|
|
|
// Graphics state
|
2011-05-07 14:15:18 +09:00
|
|
|
setLineWidth: function(width) {
|
2011-05-05 06:52:52 +09:00
|
|
|
this.printdentln(width +" w");
|
|
|
|
},
|
2011-05-10 08:57:23 +09:00
|
|
|
setLineCap: function(style) {
|
|
|
|
this.printdentln(style +" J");
|
|
|
|
},
|
2011-05-10 09:11:40 +09:00
|
|
|
setLineJoin: function(style) {
|
|
|
|
this.printdentln(style +" j");
|
|
|
|
},
|
2011-05-07 14:15:18 +09:00
|
|
|
setDash: function(dashArray, dashPhase) {
|
2011-05-05 06:52:52 +09:00
|
|
|
this.printdentln(""+ dashArray +" "+ dashPhase +" d");
|
|
|
|
},
|
2011-05-10 10:16:06 +09:00
|
|
|
setRenderingIntent: function(intent) {
|
|
|
|
this.printdentln("/"+ intent.name + " ri");
|
|
|
|
},
|
|
|
|
setFlatness: function(flatness) {
|
|
|
|
this.printdentln(""+ flatness +" i");
|
|
|
|
},
|
2011-05-07 14:15:18 +09:00
|
|
|
save: function() {
|
2011-05-05 04:46:39 +09:00
|
|
|
this.printdentln("q");
|
|
|
|
},
|
2011-05-07 14:15:18 +09:00
|
|
|
restore: function() {
|
2011-05-05 04:46:39 +09:00
|
|
|
this.printdentln("Q");
|
|
|
|
},
|
2011-05-07 14:15:18 +09:00
|
|
|
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");
|
|
|
|
},
|
2011-05-05 04:46:39 +09:00
|
|
|
|
2011-05-05 06:52:52 +09:00
|
|
|
// Path
|
2011-05-07 14:15:18 +09:00
|
|
|
moveTo: function(x, y) {
|
2011-05-05 06:52:52 +09:00
|
|
|
this.printdentln(""+ x +" "+ y +" m");
|
|
|
|
},
|
2011-05-07 14:15:18 +09:00
|
|
|
lineTo: function(x, y) {
|
2011-05-05 06:52:52 +09:00
|
|
|
this.printdentln(""+ x +" "+ y +" l");
|
|
|
|
},
|
2011-05-07 14:15:18 +09:00
|
|
|
curveTo: function(x1, y1, x2, y2, x3, y3) {
|
2011-05-05 06:52:52 +09:00
|
|
|
this.printdentln(""+ x1 +" "+ y1 +
|
|
|
|
" "+ x2 +" "+ y2 +
|
|
|
|
" "+ x3 +" "+ y3 + " c");
|
|
|
|
},
|
2011-05-07 14:15:18 +09:00
|
|
|
closePath: function() {
|
2011-05-05 13:34:59 +09:00
|
|
|
this.printdentln("h");
|
|
|
|
},
|
2011-05-07 14:15:18 +09:00
|
|
|
rectangle: function(x, y, width, height) {
|
2011-05-05 06:52:52 +09:00
|
|
|
this.printdentln(""+ x +" "+ y + " "+ width +" "+ height +" re");
|
|
|
|
},
|
2011-05-07 14:15:18 +09:00
|
|
|
stroke: function() {
|
2011-05-05 06:52:52 +09:00
|
|
|
this.printdentln("S");
|
|
|
|
},
|
2011-05-07 14:15:18 +09:00
|
|
|
fill: function() {
|
2011-05-05 13:34:59 +09:00
|
|
|
this.printdentln("f");
|
|
|
|
},
|
2011-05-10 11:08:21 +09:00
|
|
|
eoFill: function() {
|
|
|
|
this.printdentln("f*");
|
|
|
|
},
|
2011-05-07 14:15:18 +09:00
|
|
|
fillStroke: function() {
|
2011-05-05 06:52:52 +09:00
|
|
|
this.printdentln("B");
|
|
|
|
},
|
2011-05-07 14:15:18 +09:00
|
|
|
closeFillStroke: function() {
|
2011-05-05 06:52:52 +09:00
|
|
|
this.printdentln("b");
|
|
|
|
},
|
2011-05-10 10:16:06 +09:00
|
|
|
endPath: function() {
|
|
|
|
this.printdentln("n");
|
|
|
|
},
|
2011-05-05 06:52:52 +09:00
|
|
|
|
|
|
|
// Clipping
|
2011-05-10 11:05:33 +09:00
|
|
|
clip: function() {
|
|
|
|
this.printdentln("W");
|
|
|
|
},
|
|
|
|
eoClip: function() {
|
|
|
|
this.printdentln("W*");
|
|
|
|
},
|
2011-05-05 06:52:52 +09:00
|
|
|
|
2011-05-05 04:46:39 +09:00
|
|
|
// Text
|
2011-05-07 14:15:18 +09:00
|
|
|
beginText: function() {
|
2011-05-05 04:46:39 +09:00
|
|
|
this.printdentln("BT");
|
|
|
|
this.indent();
|
|
|
|
},
|
2011-05-07 14:15:18 +09:00
|
|
|
endText: function() {
|
2011-05-05 06:52:52 +09:00
|
|
|
this.dedent();
|
|
|
|
this.printdentln("ET");
|
|
|
|
},
|
2011-05-07 14:15:18 +09:00
|
|
|
setFont: function(font, size) {
|
2011-05-07 10:15:51 +09:00
|
|
|
this.printdentln("/"+ font.name +" "+ size +" Tf");
|
2011-05-05 04:46:39 +09:00
|
|
|
},
|
2011-05-07 14:15:18 +09:00
|
|
|
moveText: function (x, y) {
|
2011-05-05 04:46:39 +09:00
|
|
|
this.printdentln(""+ x +" "+ y +" Td");
|
|
|
|
},
|
2011-05-10 10:16:06 +09:00
|
|
|
setTextMatrix: function(a, b, c, d, e, f) {
|
|
|
|
this.printdentln(""+ a +" "+ b +" "+ c +
|
|
|
|
" "+d +" "+ e +" "+ f + " Tm");
|
|
|
|
},
|
2011-05-07 14:15:18 +09:00
|
|
|
showText: function(text) {
|
2011-05-05 04:46:39 +09:00
|
|
|
this.printdentln("( "+ text +" ) Tj");
|
|
|
|
},
|
2011-05-10 08:20:04 +09:00
|
|
|
showSpacedText: function(arr) {
|
|
|
|
this.printdentln(""+ arr +" TJ");
|
|
|
|
},
|
2011-05-05 06:52:52 +09:00
|
|
|
|
|
|
|
// Type3 fonts
|
|
|
|
|
|
|
|
// Color
|
2011-05-10 10:16:06 +09:00
|
|
|
setStrokeColorSpace: function(space) {
|
|
|
|
this.printdentln("/"+ space.name +" CS");
|
|
|
|
},
|
|
|
|
setFillColorSpace: function(space) {
|
|
|
|
this.printdentln("/"+ space.name +" cs");
|
|
|
|
},
|
|
|
|
setStrokeColor: function(/*...*/) {
|
|
|
|
this.printdent("");
|
|
|
|
for (var i = 0; i < arguments.length; ++i)
|
|
|
|
this.print(""+ arguments[i] +" ");
|
|
|
|
this.printdentln("SC");
|
|
|
|
},
|
2011-05-10 11:19:26 +09:00
|
|
|
setStrokeColorN: function(/*...*/) {
|
|
|
|
this.printdent("");
|
|
|
|
for (var i = 0; i < arguments.length; ++i)
|
|
|
|
this.print(""+ arguments[i] +" ");
|
|
|
|
this.printdentln("SCN");
|
|
|
|
},
|
2011-05-10 10:16:06 +09:00
|
|
|
setFillColor: function(/*...*/) {
|
|
|
|
this.printdent("");
|
|
|
|
for (var i = 0; i < arguments.length; ++i)
|
|
|
|
this.print(""+ arguments[i] +" ");
|
|
|
|
this.printdentln("sc");
|
|
|
|
},
|
2011-05-10 11:19:26 +09:00
|
|
|
setFillColorN: function(/*...*/) {
|
|
|
|
this.printdent("");
|
|
|
|
for (var i = 0; i < arguments.length; ++i)
|
|
|
|
this.print(""+ arguments[i] +" ");
|
|
|
|
this.printdentln("scn");
|
|
|
|
},
|
2011-05-07 14:15:18 +09:00
|
|
|
setFillGray: function(gray) {
|
2011-05-05 06:52:52 +09:00
|
|
|
this.printdentln(""+ gray +" g");
|
|
|
|
},
|
2011-05-07 14:15:18 +09:00
|
|
|
setStrokeRGBColor: function(r, g, b) {
|
2011-05-05 06:52:52 +09:00
|
|
|
this.printdentln(""+ r +" "+ g +" "+ b +" RG");
|
|
|
|
},
|
2011-05-07 14:15:18 +09:00
|
|
|
setFillRGBColor: function(r, g, b) {
|
2011-05-05 06:52:52 +09:00
|
|
|
this.printdentln(""+ r +" "+ g +" "+ b +" rg");
|
2011-05-05 04:46:39 +09:00
|
|
|
},
|
|
|
|
|
2011-05-05 06:52:52 +09:00
|
|
|
// Shading
|
2011-05-10 11:14:09 +09:00
|
|
|
shadingFill: function(entry) {
|
|
|
|
this.printdentln("/"+ entry.name +" sh");
|
|
|
|
},
|
|
|
|
|
2011-05-05 06:52:52 +09:00
|
|
|
// Images
|
|
|
|
// XObjects
|
2011-05-10 10:16:06 +09:00
|
|
|
paintXObject: function(obj) {
|
|
|
|
this.printdentln("/"+ obj.name +" Do");
|
|
|
|
},
|
|
|
|
|
2011-05-05 06:52:52 +09:00
|
|
|
// Marked content
|
|
|
|
// Compatibility
|
|
|
|
|
2011-05-05 04:46:39 +09:00
|
|
|
// Output state
|
|
|
|
print: function(str) {
|
|
|
|
this.out += str;
|
|
|
|
},
|
|
|
|
println: function(str) {
|
|
|
|
this.print(str);
|
|
|
|
this.out += "\n";
|
|
|
|
},
|
2011-05-10 10:16:06 +09:00
|
|
|
printdent: function(str) {
|
2011-05-05 04:46:39 +09:00
|
|
|
this.print(this.indentationStr);
|
2011-05-10 10:16:06 +09:00
|
|
|
this.print(str);
|
|
|
|
},
|
|
|
|
printdentln: function(str) {
|
|
|
|
this.printdent(str);
|
|
|
|
this.println("");
|
2011-05-05 04:46:39 +09:00
|
|
|
},
|
|
|
|
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() {
|
2011-05-10 08:20:04 +09:00
|
|
|
this.fontSize = 0.0;
|
|
|
|
// Current point (in user coordinates)
|
|
|
|
this.curX = 0.0;
|
|
|
|
this.curY = 0.0;
|
|
|
|
// Start of text line (in text coordinates)
|
2011-05-06 02:40:34 +09:00
|
|
|
this.lineX = 0.0;
|
|
|
|
this.lineY = 0.0;
|
2011-05-05 04:46:39 +09:00
|
|
|
}
|
|
|
|
constructor.prototype = {
|
|
|
|
};
|
|
|
|
return constructor;
|
|
|
|
})();
|
|
|
|
|
|
|
|
var CanvasGraphics = (function() {
|
2011-05-06 12:20:07 +09:00
|
|
|
function constructor(canvasCtx) {
|
2011-05-05 04:46:39 +09:00
|
|
|
this.ctx = canvasCtx;
|
2011-05-06 02:40:34 +09:00
|
|
|
this.current = new CanvasExtraState();
|
2011-05-05 04:46:39 +09:00
|
|
|
this.stateStack = [ ];
|
2011-05-10 11:05:33 +09:00
|
|
|
this.pendingClip = null;
|
2011-05-05 04:46:39 +09:00
|
|
|
}
|
|
|
|
|
2011-05-10 08:57:23 +09:00
|
|
|
var LINE_CAP_STYLES = [ "butt", "round", "square" ];
|
2011-05-10 09:11:40 +09:00
|
|
|
var LINE_JOIN_STYLES = [ "miter", "round", "bevel" ];
|
2011-05-10 11:05:33 +09:00
|
|
|
var NORMAL_CLIP = {};
|
|
|
|
var EO_CLIP = {};
|
2011-05-10 08:57:23 +09:00
|
|
|
|
2011-05-05 04:46:39 +09:00
|
|
|
constructor.prototype = {
|
2011-05-06 13:46:54 +09:00
|
|
|
beginDrawing: function(mediaBox) {
|
|
|
|
var cw = this.ctx.canvas.width, ch = this.ctx.canvas.height;
|
2011-05-06 12:20:07 +09:00
|
|
|
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);
|
2011-05-06 12:20:07 +09:00
|
|
|
},
|
2011-05-09 03:32:53 +09:00
|
|
|
endDrawing: function() {
|
2011-05-06 12:20:07 +09:00
|
|
|
this.ctx.restore();
|
|
|
|
},
|
|
|
|
|
2011-05-05 06:52:52 +09:00
|
|
|
// Graphics state
|
2011-05-07 14:15:18 +09:00
|
|
|
setLineWidth: function(width) {
|
2011-05-05 09:32:59 +09:00
|
|
|
this.ctx.lineWidth = width;
|
|
|
|
},
|
2011-05-10 08:57:23 +09:00
|
|
|
setLineCap: function(style) {
|
|
|
|
this.ctx.lineCap = LINE_CAP_STYLES[style];
|
|
|
|
},
|
2011-05-10 09:11:40 +09:00
|
|
|
setLineJoin: function(style) {
|
|
|
|
this.ctx.lineJoin = LINE_JOIN_STYLES[style];
|
|
|
|
},
|
2011-05-07 14:15:18 +09:00
|
|
|
setDash: function(dashArray, dashPhase) {
|
2011-05-07 10:15:51 +09:00
|
|
|
// TODO
|
2011-05-05 09:32:59 +09:00
|
|
|
},
|
2011-05-10 10:16:06 +09:00
|
|
|
setRenderingIntent: function(intent) {
|
|
|
|
// TODO
|
|
|
|
},
|
|
|
|
setFlatness: function(flatness) {
|
|
|
|
// TODO
|
|
|
|
},
|
2011-05-07 14:15:18 +09:00
|
|
|
save: function() {
|
2011-05-05 04:46:39 +09:00
|
|
|
this.ctx.save();
|
2011-05-06 02:40:34 +09:00
|
|
|
this.stateStack.push(this.current);
|
|
|
|
this.current = new CanvasExtraState();
|
2011-05-05 04:46:39 +09:00
|
|
|
},
|
2011-05-07 14:15:18 +09:00
|
|
|
restore: function() {
|
2011-05-06 02:40:34 +09:00
|
|
|
this.current = this.stateStack.pop();
|
2011-05-05 04:46:39 +09:00
|
|
|
this.ctx.restore();
|
|
|
|
},
|
2011-05-07 14:15:18 +09:00
|
|
|
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);
|
|
|
|
},
|
2011-05-05 09:32:59 +09:00
|
|
|
|
|
|
|
// Path
|
2011-05-07 14:15:18 +09:00
|
|
|
moveTo: function(x, y) {
|
2011-05-05 09:32:59 +09:00
|
|
|
this.ctx.moveTo(x, y);
|
|
|
|
},
|
2011-05-07 14:15:18 +09:00
|
|
|
lineTo: function(x, y) {
|
2011-05-05 09:32:59 +09:00
|
|
|
this.ctx.lineTo(x, y);
|
|
|
|
},
|
2011-05-07 14:15:18 +09:00
|
|
|
curveTo: function(x1, y1, x2, y2, x3, y3) {
|
2011-05-05 09:32:59 +09:00
|
|
|
this.ctx.bezierCurveTo(x1, y1, x2, y2, x3, y3);
|
|
|
|
},
|
2011-05-07 14:15:18 +09:00
|
|
|
closePath: function() {
|
2011-05-05 13:34:59 +09:00
|
|
|
this.ctx.closePath();
|
|
|
|
},
|
2011-05-07 14:15:18 +09:00
|
|
|
rectangle: function(x, y, width, height) {
|
2011-05-05 09:32:59 +09:00
|
|
|
this.ctx.rect(x, y, width, height);
|
|
|
|
},
|
2011-05-07 14:15:18 +09:00
|
|
|
stroke: function() {
|
2011-05-05 09:32:59 +09:00
|
|
|
this.ctx.stroke();
|
2011-05-07 14:15:18 +09:00
|
|
|
this.consumePath();
|
2011-05-05 09:32:59 +09:00
|
|
|
},
|
2011-05-07 14:15:18 +09:00
|
|
|
fill: function() {
|
2011-05-05 13:34:59 +09:00
|
|
|
this.ctx.fill();
|
2011-05-07 14:15:18 +09:00
|
|
|
this.consumePath();
|
2011-05-05 13:34:59 +09:00
|
|
|
},
|
2011-05-10 11:08:21 +09:00
|
|
|
eoFill: function() {
|
|
|
|
// TODO: <canvas> needs to support even-odd winding rule
|
|
|
|
this.fill();
|
|
|
|
},
|
2011-05-07 14:15:18 +09:00
|
|
|
fillStroke: function() {
|
2011-05-05 09:32:59 +09:00
|
|
|
this.ctx.fill();
|
|
|
|
this.ctx.stroke();
|
2011-05-07 14:15:18 +09:00
|
|
|
this.consumePath();
|
2011-05-05 09:32:59 +09:00
|
|
|
},
|
2011-05-07 14:15:18 +09:00
|
|
|
closeFillStroke: function() {
|
|
|
|
return this.fillStroke();
|
2011-05-05 09:32:59 +09:00
|
|
|
},
|
2011-05-10 10:16:06 +09:00
|
|
|
endPath: function() {
|
|
|
|
this.consumePath();
|
|
|
|
},
|
2011-05-05 09:32:59 +09:00
|
|
|
|
|
|
|
// Clipping
|
2011-05-10 11:05:33 +09:00
|
|
|
clip: function() {
|
|
|
|
this.pendingClip = NORMAL_CLIP;
|
|
|
|
},
|
|
|
|
eoClip: function() {
|
|
|
|
this.pendingClip = EO_CLIP;
|
|
|
|
},
|
2011-05-05 10:08:52 +09:00
|
|
|
|
2011-05-05 09:32:59 +09:00
|
|
|
// Text
|
2011-05-07 14:15:18 +09:00
|
|
|
beginText: function() {
|
2011-05-07 10:15:51 +09:00
|
|
|
// TODO
|
2011-05-05 10:08:52 +09:00
|
|
|
},
|
2011-05-07 14:15:18 +09:00
|
|
|
endText: function() {
|
2011-05-07 10:15:51 +09:00
|
|
|
// TODO
|
2011-05-05 10:08:52 +09:00
|
|
|
},
|
2011-05-07 14:15:18 +09:00
|
|
|
setFont: function(font, size) {
|
2011-05-10 08:20:04 +09:00
|
|
|
this.current.fontSize = size;
|
|
|
|
this.ctx.font = this.current.fontSize +'px '+ font.BaseFont;
|
2011-05-05 10:08:52 +09:00
|
|
|
},
|
2011-05-07 14:15:18 +09:00
|
|
|
moveText: function (x, y) {
|
2011-05-10 08:20:04 +09:00
|
|
|
this.current.lineX += x;
|
|
|
|
this.current.lineY += y;
|
2011-05-10 10:16:06 +09:00
|
|
|
// TODO transform
|
2011-05-10 08:20:04 +09:00
|
|
|
this.current.curX = this.current.lineX;
|
|
|
|
this.current.curY = this.current.lineY;
|
2011-05-05 10:08:52 +09:00
|
|
|
},
|
2011-05-10 10:16:06 +09:00
|
|
|
setTextMatrix: function(a, b, c, d, e, f) {
|
|
|
|
// TODO
|
|
|
|
},
|
2011-05-07 14:15:18 +09:00
|
|
|
showText: function(text) {
|
2011-05-06 12:20:07 +09:00
|
|
|
this.ctx.save();
|
2011-05-10 08:20:04 +09:00
|
|
|
this.ctx.translate(0, 2 * this.current.curY);
|
2011-05-06 12:20:07 +09:00
|
|
|
this.ctx.scale(1, -1);
|
|
|
|
|
2011-05-10 08:20:04 +09:00
|
|
|
this.ctx.fillText(text, this.current.curX, this.current.curY);
|
|
|
|
this.current.curX += this.ctx.measureText(text).width;
|
2011-05-06 12:20:07 +09:00
|
|
|
|
|
|
|
this.ctx.restore();
|
2011-05-05 10:08:52 +09:00
|
|
|
},
|
2011-05-10 08:20:04 +09:00
|
|
|
showSpacedText: function(arr) {
|
|
|
|
for (var i = 0; i < arr.length; ++i) {
|
|
|
|
var e = arr[i];
|
|
|
|
if (IsNum(e)) {
|
|
|
|
this.current.curX -= e * 0.001 * this.current.fontSize;
|
|
|
|
} else if (IsString(e)) {
|
|
|
|
this.showText(e);
|
|
|
|
} else {
|
2011-05-10 10:10:15 +09:00
|
|
|
error("Unexpected element in TJ array");
|
2011-05-10 08:20:04 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2011-05-05 10:08:52 +09:00
|
|
|
|
2011-05-05 09:32:59 +09:00
|
|
|
// Type3 fonts
|
|
|
|
|
|
|
|
// Color
|
2011-05-10 10:16:06 +09:00
|
|
|
setStrokeColorSpace: function(space) {
|
|
|
|
// TODO
|
|
|
|
},
|
|
|
|
setFillColorSpace: function(space) {
|
|
|
|
// TODO
|
|
|
|
},
|
|
|
|
setStrokeColor: function(/*...*/) {
|
|
|
|
// TODO
|
|
|
|
},
|
2011-05-10 11:19:26 +09:00
|
|
|
setStrokeColorN: function(/*...*/) {
|
|
|
|
// TODO
|
|
|
|
},
|
2011-05-10 10:16:06 +09:00
|
|
|
setFillColor: function(/*...*/) {
|
|
|
|
// TODO
|
|
|
|
},
|
2011-05-10 11:19:26 +09:00
|
|
|
setFillColorN: function(/*...*/) {
|
|
|
|
// TODO
|
|
|
|
},
|
2011-05-07 14:15:18 +09:00
|
|
|
setFillGray: function(gray) {
|
|
|
|
this.setFillRGBColor(gray, gray, gray);
|
2011-05-05 09:32:59 +09:00
|
|
|
},
|
2011-05-07 14:15:18 +09:00
|
|
|
setStrokeRGBColor: function(r, g, b) {
|
|
|
|
this.ctx.strokeStyle = this.makeCssRgb(r, g, b);
|
2011-05-05 09:32:59 +09:00
|
|
|
},
|
2011-05-07 14:15:18 +09:00
|
|
|
setFillRGBColor: function(r, g, b) {
|
|
|
|
this.ctx.fillStyle = this.makeCssRgb(r, g, b);
|
2011-05-05 09:32:59 +09:00
|
|
|
},
|
|
|
|
|
2011-05-10 11:14:09 +09:00
|
|
|
// Shading
|
|
|
|
shadingFill: function(entry) {
|
|
|
|
// TODO
|
|
|
|
},
|
|
|
|
|
2011-05-10 10:16:06 +09:00
|
|
|
// XObjects
|
|
|
|
paintXObject: function(obj) {
|
|
|
|
// TODO
|
|
|
|
},
|
|
|
|
|
2011-05-07 14:15:18 +09:00
|
|
|
// Helper functions
|
2011-05-07 10:15:51 +09:00
|
|
|
|
2011-05-07 14:15:18 +09:00
|
|
|
consumePath: function() {
|
2011-05-10 11:05:33 +09:00
|
|
|
if (this.pendingClip) {
|
|
|
|
// TODO: <canvas> needs to support even-odd winding rule
|
|
|
|
this.ctx.clip();
|
|
|
|
this.pendingClip = null;
|
|
|
|
}
|
2011-05-05 09:32:59 +09:00
|
|
|
this.ctx.beginPath();
|
|
|
|
},
|
2011-05-07 14:15:18 +09:00
|
|
|
makeCssRgb: function(r, g, b) {
|
2011-05-05 09:32:59 +09:00
|
|
|
var ri = (255 * r) | 0, gi = (255 * g) | 0, bi = (255 * b) | 0;
|
|
|
|
return "rgb("+ ri +","+ gi +","+ bi +")";
|
|
|
|
},
|
2011-05-05 04:46:39 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
2011-05-05 04:46:39 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
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; }
|
2011-05-05 09:32:59 +09:00
|
|
|
|
|
|
|
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 ],
|
2011-05-05 09:32:59 +09:00
|
|
|
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
|
|
|
]
|
2011-05-05 09:32:59 +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 ],
|
2011-05-05 09:32:59 +09:00
|
|
|
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-10 08:20:04 +09:00
|
|
|
{ name: "TJ",
|
|
|
|
res: {
|
|
|
|
// XXX not structured correctly
|
|
|
|
Font: {
|
|
|
|
F1: { Type: "Font",
|
|
|
|
Subtype: "Type1",
|
|
|
|
Name: "F1",
|
|
|
|
BaseFont: "Georgia",
|
|
|
|
Encoding: "MacRomanEncoding"
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
mediaBox: [ 0, 0, 612, 792 ],
|
|
|
|
objs: [
|
|
|
|
cmd("BT"),
|
|
|
|
name("F1"), real(17.9328), cmd("Tf"),
|
|
|
|
|
|
|
|
real(80.5159), real(700.6706), cmd("Td"),
|
|
|
|
[ string("Trace-based Just-in-Time") ], cmd("TJ"),
|
|
|
|
|
|
|
|
int(0), int(-18), cmd("Td"),
|
|
|
|
[ string("T"), int(74), string("race-based"), int(-250), string("J"), int(15), string("ust-in-T"), int(18), string("ime") ], cmd("TJ"),
|
|
|
|
cmd("ET"),
|
|
|
|
eof()
|
|
|
|
]
|
|
|
|
},
|
2011-05-10 08:57:23 +09:00
|
|
|
{ name: "Line cap",
|
|
|
|
res: { },
|
|
|
|
mediaBox: [ 0, 0, 612, 792 ],
|
|
|
|
objs: [
|
|
|
|
int(5), cmd("w"),
|
|
|
|
|
|
|
|
int(0), cmd("J"), // butt cap
|
|
|
|
int(100), int(692), cmd("m"),
|
|
|
|
int(200), int(692), cmd("l"),
|
|
|
|
cmd("S"),
|
|
|
|
|
|
|
|
int(1), cmd("J"), // round cap
|
|
|
|
int(100), int(686), cmd("m"),
|
|
|
|
int(200), int(686), cmd("l"),
|
|
|
|
cmd("S"),
|
|
|
|
|
|
|
|
int(2), cmd("J"), // projecting square cap
|
|
|
|
int(100), int(680), cmd("m"),
|
|
|
|
int(200), int(680), cmd("l"),
|
|
|
|
cmd("S"),
|
|
|
|
|
2011-05-10 09:11:40 +09:00
|
|
|
eof()
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{ name: "Line join",
|
|
|
|
res: { },
|
|
|
|
mediaBox: [ 0, 0, 612, 792 ],
|
|
|
|
objs: [
|
|
|
|
int(20), cmd("w"),
|
|
|
|
|
|
|
|
int(0), cmd("j"), // miter join
|
|
|
|
int(100), int(692), cmd("m"),
|
|
|
|
int(150), int(642), cmd("l"),
|
|
|
|
int(200), int(692), cmd("l"),
|
|
|
|
cmd("S"),
|
|
|
|
|
|
|
|
int(1), cmd("j"), // round join
|
|
|
|
int(250), int(692), cmd("m"),
|
|
|
|
int(300), int(642), cmd("l"),
|
|
|
|
int(350), int(692), cmd("l"),
|
|
|
|
cmd("S"),
|
|
|
|
|
|
|
|
int(2), cmd("j"), // bevel join
|
|
|
|
int(400), int(692), cmd("m"),
|
|
|
|
int(450), int(642), cmd("l"),
|
|
|
|
int(500), int(692), cmd("l"),
|
|
|
|
cmd("S"),
|
|
|
|
|
2011-05-10 08:57:23 +09:00
|
|
|
eof()
|
|
|
|
],
|
|
|
|
},
|
2011-05-10 10:16:06 +09:00
|
|
|
{ name: "NYI", // check that NYI commands are no-ops
|
|
|
|
res: { },
|
|
|
|
mediaBox: [ 0, 0, 612, 792 ],
|
|
|
|
objs: [
|
|
|
|
name("Perceptual"), cmd("ri"),
|
|
|
|
int(2), cmd("i"),
|
|
|
|
int(1), int(0), int(0), int(1), int(80), int(80), cmd("Tm"),
|
|
|
|
name("DeviceRGB"), cmd("CS"),
|
|
|
|
name("DeviceGray"), cmd("cs"),
|
|
|
|
int(1), int(0), int(0), cmd("SC"),
|
2011-05-10 11:19:26 +09:00
|
|
|
int(1), int(0), int(0), cmd("SCN"),
|
2011-05-10 10:16:06 +09:00
|
|
|
int(1), cmd("sc"),
|
2011-05-10 11:19:26 +09:00
|
|
|
int(1), cmd("scn"),
|
2011-05-10 10:16:06 +09:00
|
|
|
name("object"), cmd("Do"),
|
2011-05-10 11:14:09 +09:00
|
|
|
name("shading"), cmd("sh"),
|
2011-05-10 10:16:06 +09:00
|
|
|
eof()
|
|
|
|
],
|
|
|
|
},
|
2011-05-10 11:05:33 +09:00
|
|
|
{ name: "Broken heart",
|
|
|
|
res: { },
|
|
|
|
mediaBox: [ 0, 0, 612, 792 ],
|
|
|
|
objs: [
|
|
|
|
cmd("q"),
|
|
|
|
int(20), int(20), int(60), int(60), cmd("re"),
|
|
|
|
int(60), int(60), int(60), int(60), cmd("re"),
|
|
|
|
cmd("W"), cmd("n"),
|
|
|
|
|
|
|
|
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()
|
|
|
|
]
|
|
|
|
},
|
2011-05-05 09:32:59 +09:00
|
|
|
];
|
2011-05-05 04:46:39 +09:00
|
|
|
|
2011-05-05 13:34:59 +09:00
|
|
|
|
2011-05-05 09:32:59 +09:00
|
|
|
function runEchoTests() {
|
2011-05-05 04:46:39 +09:00
|
|
|
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));
|
2011-05-05 04:46:39 +09:00
|
|
|
|
|
|
|
print("done. Output:");
|
|
|
|
print(gfx.out);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2011-05-06 17:44:17 +09:00
|
|
|
function runParseTests() {
|
2011-05-07 14:44:01 +09:00
|
|
|
//var data = snarf("simple_graphics.pdf", "binary");
|
2011-05-10 07:58:37 +09:00
|
|
|
var data = snarf("/tmp/paper.pdf", "binary");
|
2011-05-06 18:18:31 +09:00
|
|
|
var pdf = new PDFDoc(new Stream(data));
|
2011-05-11 19:14:08 +09:00
|
|
|
var page = pdf.getPage(1);
|
2011-05-12 09:09:06 +09:00
|
|
|
page.display();
|
2011-05-06 17:44:17 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
if ("arguments" in this) {
|
|
|
|
const cmds = {
|
|
|
|
"-e": runEchoTests,
|
|
|
|
"-p": runParseTests
|
|
|
|
}
|
|
|
|
for (n in arguments) {
|
|
|
|
var fn = cmds[arguments[n]];
|
|
|
|
if (fn)
|
|
|
|
fn();
|
|
|
|
}
|
|
|
|
}
|