Making src/core/{image,obj,parser}.js adhere to the style guide
This commit is contained in:
parent
878a123e47
commit
284288f1d0
@ -47,7 +47,7 @@ var PDFImage = (function PDFImageClosure() {
|
|||||||
function decodeAndClamp(value, addend, coefficient, max) {
|
function decodeAndClamp(value, addend, coefficient, max) {
|
||||||
value = addend + value * coefficient;
|
value = addend + value * coefficient;
|
||||||
// Clamp the value to the range
|
// Clamp the value to the range
|
||||||
return value < 0 ? 0 : value > max ? max : value;
|
return (value < 0 ? 0 : (value > max ? max : value));
|
||||||
}
|
}
|
||||||
function PDFImage(xref, res, image, inline, smask, mask, isMask) {
|
function PDFImage(xref, res, image, inline, smask, mask, isMask) {
|
||||||
this.image = image;
|
this.image = image;
|
||||||
@ -89,7 +89,7 @@ var PDFImage = (function PDFImageClosure() {
|
|||||||
if (!this.imageMask) {
|
if (!this.imageMask) {
|
||||||
var colorSpace = dict.get('ColorSpace', 'CS');
|
var colorSpace = dict.get('ColorSpace', 'CS');
|
||||||
if (!colorSpace) {
|
if (!colorSpace) {
|
||||||
warn('JPX images (which don"t require color spaces');
|
warn('JPX images (which do not require color spaces)');
|
||||||
colorSpace = Name.get('DeviceRGB');
|
colorSpace = Name.get('DeviceRGB');
|
||||||
}
|
}
|
||||||
this.colorSpace = ColorSpace.parse(colorSpace, xref, res);
|
this.colorSpace = ColorSpace.parse(colorSpace, xref, res);
|
||||||
@ -134,7 +134,7 @@ var PDFImage = (function PDFImageClosure() {
|
|||||||
var imageDataPromise = new LegacyPromise();
|
var imageDataPromise = new LegacyPromise();
|
||||||
var smaskPromise = new LegacyPromise();
|
var smaskPromise = new LegacyPromise();
|
||||||
var maskPromise = new LegacyPromise();
|
var maskPromise = new LegacyPromise();
|
||||||
// The image data and smask data may not be ready yet, wait till both are
|
// The image data and smask data may not be ready yet, wait until both are
|
||||||
// resolved.
|
// resolved.
|
||||||
Promise.all([imageDataPromise, smaskPromise, maskPromise]).then(
|
Promise.all([imageDataPromise, smaskPromise, maskPromise]).then(
|
||||||
function(results) {
|
function(results) {
|
||||||
@ -170,7 +170,7 @@ var PDFImage = (function PDFImageClosure() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Resize an image using the nearest neighbor algorithm. Currently only
|
* Resize an image using the nearest neighbor algorithm. Currently only
|
||||||
* supports one and three component images.
|
* supports one and three component images.
|
||||||
* @param {TypedArray} pixels The original image with one component.
|
* @param {TypedArray} pixels The original image with one component.
|
||||||
* @param {Number} bpc Number of bits per component.
|
* @param {Number} bpc Number of bits per component.
|
||||||
@ -184,8 +184,8 @@ var PDFImage = (function PDFImageClosure() {
|
|||||||
PDFImage.resize = function PDFImage_resize(pixels, bpc, components,
|
PDFImage.resize = function PDFImage_resize(pixels, bpc, components,
|
||||||
w1, h1, w2, h2) {
|
w1, h1, w2, h2) {
|
||||||
var length = w2 * h2 * components;
|
var length = w2 * h2 * components;
|
||||||
var temp = bpc <= 8 ? new Uint8Array(length) :
|
var temp = (bpc <= 8 ? new Uint8Array(length) :
|
||||||
bpc <= 16 ? new Uint16Array(length) : new Uint32Array(length);
|
(bpc <= 16 ? new Uint16Array(length) : new Uint32Array(length)));
|
||||||
var xRatio = w1 / w2;
|
var xRatio = w1 / w2;
|
||||||
var yRatio = h1 / h2;
|
var yRatio = h1 / h2;
|
||||||
var px, py, newIndex, oldIndex;
|
var px, py, newIndex, oldIndex;
|
||||||
@ -266,7 +266,7 @@ var PDFImage = (function PDFImageClosure() {
|
|||||||
for (var i = 0, ii = this.width * this.height; i < ii; i++) {
|
for (var i = 0, ii = this.width * this.height; i < ii; i++) {
|
||||||
for (var j = 0; j < numComps; j++) {
|
for (var j = 0; j < numComps; j++) {
|
||||||
buffer[index] = decodeAndClamp(buffer[index], decodeAddends[j],
|
buffer[index] = decodeAndClamp(buffer[index], decodeAddends[j],
|
||||||
decodeCoefficients[j], max);
|
decodeCoefficients[j], max);
|
||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -285,8 +285,8 @@ var PDFImage = (function PDFImageClosure() {
|
|||||||
|
|
||||||
var length = width * height * numComps;
|
var length = width * height * numComps;
|
||||||
var bufferPos = 0;
|
var bufferPos = 0;
|
||||||
var output = bpc <= 8 ? new Uint8Array(length) :
|
var output = (bpc <= 8 ? new Uint8Array(length) :
|
||||||
bpc <= 16 ? new Uint16Array(length) : new Uint32Array(length);
|
(bpc <= 16 ? new Uint16Array(length) : new Uint32Array(length)));
|
||||||
var rowComps = width * numComps;
|
var rowComps = width * numComps;
|
||||||
|
|
||||||
var max = (1 << bpc) - 1;
|
var max = (1 << bpc) - 1;
|
||||||
@ -338,7 +338,7 @@ var PDFImage = (function PDFImageClosure() {
|
|||||||
|
|
||||||
var remainingBits = bits - bpc;
|
var remainingBits = bits - bpc;
|
||||||
var value = buf >> remainingBits;
|
var value = buf >> remainingBits;
|
||||||
output[i] = value < 0 ? 0 : value > max ? max : value;
|
output[i] = (value < 0 ? 0 : (value > max ? max : value));
|
||||||
buf = buf & ((1 << remainingBits) - 1);
|
buf = buf & ((1 << remainingBits) - 1);
|
||||||
bits = remainingBits;
|
bits = remainingBits;
|
||||||
}
|
}
|
||||||
@ -418,7 +418,7 @@ var PDFImage = (function PDFImageClosure() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function clamp(value) {
|
function clamp(value) {
|
||||||
return (value < 0 ? 0 : value > 255 ? 255 : value) | 0;
|
return (value < 0 ? 0 : (value > 255 ? 255 : value)) | 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
var matteRgb = this.colorSpace.getRgb(matte, 0);
|
var matteRgb = this.colorSpace.getRgb(matte, 0);
|
||||||
@ -442,7 +442,7 @@ var PDFImage = (function PDFImageClosure() {
|
|||||||
createImageData: function PDFImage_createImageData(forceRGBA) {
|
createImageData: function PDFImage_createImageData(forceRGBA) {
|
||||||
var drawWidth = this.drawWidth;
|
var drawWidth = this.drawWidth;
|
||||||
var drawHeight = this.drawHeight;
|
var drawHeight = this.drawHeight;
|
||||||
var imgData = { // other fields are filled in below
|
var imgData = { // other fields are filled in below
|
||||||
width: drawWidth,
|
width: drawWidth,
|
||||||
height: drawHeight
|
height: drawHeight
|
||||||
};
|
};
|
||||||
@ -529,14 +529,15 @@ var PDFImage = (function PDFImageClosure() {
|
|||||||
},
|
},
|
||||||
fillGrayBuffer: function PDFImage_fillGrayBuffer(buffer) {
|
fillGrayBuffer: function PDFImage_fillGrayBuffer(buffer) {
|
||||||
var numComps = this.numComps;
|
var numComps = this.numComps;
|
||||||
if (numComps != 1)
|
if (numComps != 1) {
|
||||||
error('Reading gray scale from a color image: ' + numComps);
|
error('Reading gray scale from a color image: ' + numComps);
|
||||||
|
}
|
||||||
|
|
||||||
var width = this.width;
|
var width = this.width;
|
||||||
var height = this.height;
|
var height = this.height;
|
||||||
var bpc = this.bpc;
|
var bpc = this.bpc;
|
||||||
|
|
||||||
// rows start at byte boundary;
|
// rows start at byte boundary
|
||||||
var rowBytes = (width * numComps * bpc + 7) >> 3;
|
var rowBytes = (width * numComps * bpc + 7) >> 3;
|
||||||
var imgArray = this.getImageBytes(height * rowBytes);
|
var imgArray = this.getImageBytes(height * rowBytes);
|
||||||
|
|
||||||
|
@ -34,7 +34,7 @@ var Name = (function NameClosure() {
|
|||||||
|
|
||||||
Name.get = function Name_get(name) {
|
Name.get = function Name_get(name) {
|
||||||
var nameValue = nameCache[name];
|
var nameValue = nameCache[name];
|
||||||
return nameValue ? nameValue : (nameCache[name] = new Name(name));
|
return (nameValue ? nameValue : (nameCache[name] = new Name(name)));
|
||||||
};
|
};
|
||||||
|
|
||||||
return Name;
|
return Name;
|
||||||
@ -51,7 +51,7 @@ var Cmd = (function CmdClosure() {
|
|||||||
|
|
||||||
Cmd.get = function Cmd_get(cmd) {
|
Cmd.get = function Cmd_get(cmd) {
|
||||||
var cmdValue = cmdCache[cmd];
|
var cmdValue = cmdCache[cmd];
|
||||||
return cmdValue ? cmdValue : (cmdCache[cmd] = new Cmd(cmd));
|
return (cmdValue ? cmdValue : (cmdCache[cmd] = new Cmd(cmd)));
|
||||||
};
|
};
|
||||||
|
|
||||||
return Cmd;
|
return Cmd;
|
||||||
@ -133,7 +133,7 @@ var Dict = (function DictClosure() {
|
|||||||
var all = {};
|
var all = {};
|
||||||
for (var key in this.map) {
|
for (var key in this.map) {
|
||||||
var obj = this.get(key);
|
var obj = this.get(key);
|
||||||
all[key] = obj instanceof Dict ? obj.getAll() : obj;
|
all[key] = (obj instanceof Dict ? obj.getAll() : obj);
|
||||||
}
|
}
|
||||||
return all;
|
return all;
|
||||||
},
|
},
|
||||||
@ -167,8 +167,8 @@ var Ref = (function RefClosure() {
|
|||||||
return Ref;
|
return Ref;
|
||||||
})();
|
})();
|
||||||
|
|
||||||
// The reference is identified by number and generation,
|
// The reference is identified by number and generation.
|
||||||
// this structure stores only one instance of the reference.
|
// This structure stores only one instance of the reference.
|
||||||
var RefSet = (function RefSetClosure() {
|
var RefSet = (function RefSetClosure() {
|
||||||
function RefSet() {
|
function RefSet() {
|
||||||
this.dict = {};
|
this.dict = {};
|
||||||
@ -242,8 +242,8 @@ var Catalog = (function CatalogClosure() {
|
|||||||
return shadow(this, 'metadata', null);
|
return shadow(this, 'metadata', null);
|
||||||
}
|
}
|
||||||
|
|
||||||
var encryptMetadata = !this.xref.encrypt ? false :
|
var encryptMetadata = (!this.xref.encrypt ? false :
|
||||||
this.xref.encrypt.encryptMetadata;
|
this.xref.encrypt.encryptMetadata);
|
||||||
|
|
||||||
var stream = this.xref.fetch(streamRef, !encryptMetadata);
|
var stream = this.xref.fetch(streamRef, !encryptMetadata);
|
||||||
var metadata;
|
var metadata;
|
||||||
@ -340,7 +340,7 @@ var Catalog = (function CatalogClosure() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return root.items.length > 0 ? root.items : null;
|
return (root.items.length > 0 ? root.items : null);
|
||||||
},
|
},
|
||||||
get numPages() {
|
get numPages() {
|
||||||
var obj = this.toplevelPagesDict.get('Count');
|
var obj = this.toplevelPagesDict.get('Count');
|
||||||
@ -399,7 +399,7 @@ var Catalog = (function CatalogClosure() {
|
|||||||
if (!names.hasOwnProperty(name)) {
|
if (!names.hasOwnProperty(name)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// We don't really use the JavaScript right now so this code is
|
// We don't really use the JavaScript right now. This code is
|
||||||
// defensive so we don't cause errors on document load.
|
// defensive so we don't cause errors on document load.
|
||||||
var jsDict = names[name];
|
var jsDict = names[name];
|
||||||
if (!isDict(jsDict)) {
|
if (!isDict(jsDict)) {
|
||||||
@ -535,7 +535,7 @@ var Catalog = (function CatalogClosure() {
|
|||||||
var found = false;
|
var found = false;
|
||||||
for (var i = 0; i < kids.length; i++) {
|
for (var i = 0; i < kids.length; i++) {
|
||||||
var kid = kids[i];
|
var kid = kids[i];
|
||||||
assert(isRef(kid), 'kids must be an ref');
|
assert(isRef(kid), 'kids must be a ref');
|
||||||
if (kid.num == kidRef.num) {
|
if (kid.num == kidRef.num) {
|
||||||
found = true;
|
found = true;
|
||||||
break;
|
break;
|
||||||
@ -580,7 +580,6 @@ var Catalog = (function CatalogClosure() {
|
|||||||
|
|
||||||
var XRef = (function XRefClosure() {
|
var XRef = (function XRefClosure() {
|
||||||
function XRef(stream, password) {
|
function XRef(stream, password) {
|
||||||
|
|
||||||
this.stream = stream;
|
this.stream = stream;
|
||||||
this.entries = [];
|
this.entries = [];
|
||||||
this.xrefstms = {};
|
this.xrefstms = {};
|
||||||
@ -610,8 +609,8 @@ var XRef = (function XRefClosure() {
|
|||||||
if (encrypt) {
|
if (encrypt) {
|
||||||
var ids = trailerDict.get('ID');
|
var ids = trailerDict.get('ID');
|
||||||
var fileId = (ids && ids.length) ? ids[0] : '';
|
var fileId = (ids && ids.length) ? ids[0] : '';
|
||||||
this.encrypt = new CipherTransformFactory(
|
this.encrypt = new CipherTransformFactory(encrypt, fileId,
|
||||||
encrypt, fileId, this.password);
|
this.password);
|
||||||
}
|
}
|
||||||
|
|
||||||
// get the root dictionary (catalog) object
|
// get the root dictionary (catalog) object
|
||||||
@ -702,16 +701,17 @@ var XRef = (function XRefClosure() {
|
|||||||
entry.gen = parser.getObj();
|
entry.gen = parser.getObj();
|
||||||
var type = parser.getObj();
|
var type = parser.getObj();
|
||||||
|
|
||||||
if (isCmd(type, 'f'))
|
if (isCmd(type, 'f')) {
|
||||||
entry.free = true;
|
entry.free = true;
|
||||||
else if (isCmd(type, 'n'))
|
} else if (isCmd(type, 'n')) {
|
||||||
entry.uncompressed = true;
|
entry.uncompressed = true;
|
||||||
|
}
|
||||||
|
|
||||||
// Validate entry obj
|
// Validate entry obj
|
||||||
if (!isInt(entry.offset) || !isInt(entry.gen) ||
|
if (!isInt(entry.offset) || !isInt(entry.gen) ||
|
||||||
!(entry.free || entry.uncompressed)) {
|
!(entry.free || entry.uncompressed)) {
|
||||||
console.log(entry.offset, entry.gen, entry.free,
|
console.log(entry.offset, entry.gen, entry.free,
|
||||||
entry.uncompressed);
|
entry.uncompressed);
|
||||||
error('Invalid entry in XRef subsection: ' + first + ', ' + count);
|
error('Invalid entry in XRef subsection: ' + first + ', ' + count);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -777,7 +777,6 @@ var XRef = (function XRefClosure() {
|
|||||||
|
|
||||||
var entryRanges = streamState.entryRanges;
|
var entryRanges = streamState.entryRanges;
|
||||||
while (entryRanges.length > 0) {
|
while (entryRanges.length > 0) {
|
||||||
|
|
||||||
var first = entryRanges[0];
|
var first = entryRanges[0];
|
||||||
var n = entryRanges[1];
|
var n = entryRanges[1];
|
||||||
|
|
||||||
@ -793,9 +792,10 @@ var XRef = (function XRefClosure() {
|
|||||||
streamState.streamPos = stream.pos;
|
streamState.streamPos = stream.pos;
|
||||||
|
|
||||||
var type = 0, offset = 0, generation = 0;
|
var type = 0, offset = 0, generation = 0;
|
||||||
for (j = 0; j < typeFieldWidth; ++j)
|
for (j = 0; j < typeFieldWidth; ++j) {
|
||||||
type = (type << 8) | stream.getByte();
|
type = (type << 8) | stream.getByte();
|
||||||
// if type field is absent, its default value = 1
|
}
|
||||||
|
// if type field is absent, its default value is 1
|
||||||
if (typeFieldWidth === 0) {
|
if (typeFieldWidth === 0) {
|
||||||
type = 1;
|
type = 1;
|
||||||
}
|
}
|
||||||
@ -837,8 +837,9 @@ var XRef = (function XRefClosure() {
|
|||||||
function readToken(data, offset) {
|
function readToken(data, offset) {
|
||||||
var token = '', ch = data[offset];
|
var token = '', ch = data[offset];
|
||||||
while (ch !== 13 && ch !== 10) {
|
while (ch !== 13 && ch !== 10) {
|
||||||
if (++offset >= data.length)
|
if (++offset >= data.length) {
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
token += String.fromCharCode(ch);
|
token += String.fromCharCode(ch);
|
||||||
ch = data[offset];
|
ch = data[offset];
|
||||||
}
|
}
|
||||||
@ -853,9 +854,9 @@ var XRef = (function XRefClosure() {
|
|||||||
while (i < length && data[offset + i] == what[i]) {
|
while (i < length && data[offset + i] == what[i]) {
|
||||||
++i;
|
++i;
|
||||||
}
|
}
|
||||||
if (i >= length)
|
if (i >= length) {
|
||||||
break; // sequence found
|
break; // sequence found
|
||||||
|
}
|
||||||
offset++;
|
offset++;
|
||||||
skipped++;
|
skipped++;
|
||||||
}
|
}
|
||||||
@ -967,7 +968,6 @@ var XRef = (function XRefClosure() {
|
|||||||
|
|
||||||
// Get dictionary
|
// Get dictionary
|
||||||
if (isCmd(obj, 'xref')) {
|
if (isCmd(obj, 'xref')) {
|
||||||
|
|
||||||
// Parse end-of-file XRef
|
// Parse end-of-file XRef
|
||||||
dict = this.processXRefTable(parser);
|
dict = this.processXRefTable(parser);
|
||||||
if (!this.topDict) {
|
if (!this.topDict) {
|
||||||
@ -986,7 +986,6 @@ var XRef = (function XRefClosure() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (isInt(obj)) {
|
} else if (isInt(obj)) {
|
||||||
|
|
||||||
// Parse in-stream XRef
|
// Parse in-stream XRef
|
||||||
if (!isInt(parser.getObj()) ||
|
if (!isInt(parser.getObj()) ||
|
||||||
!isCmd(parser.getObj(), 'obj') ||
|
!isCmd(parser.getObj(), 'obj') ||
|
||||||
@ -997,7 +996,6 @@ var XRef = (function XRefClosure() {
|
|||||||
if (!this.topDict) {
|
if (!this.topDict) {
|
||||||
this.topDict = dict;
|
this.topDict = dict;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!dict) {
|
if (!dict) {
|
||||||
error('Failed to read XRef stream');
|
error('Failed to read XRef stream');
|
||||||
}
|
}
|
||||||
@ -1069,8 +1067,7 @@ var XRef = (function XRefClosure() {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
fetchUncompressed: function XRef_fetchUncompressed(ref,
|
fetchUncompressed: function XRef_fetchUncompressed(ref, xrefEntry,
|
||||||
xrefEntry,
|
|
||||||
suppressEncryption) {
|
suppressEncryption) {
|
||||||
var gen = ref.gen;
|
var gen = ref.gen;
|
||||||
var num = ref.num;
|
var num = ref.num;
|
||||||
@ -1089,7 +1086,7 @@ var XRef = (function XRefClosure() {
|
|||||||
error('bad XRef entry');
|
error('bad XRef entry');
|
||||||
}
|
}
|
||||||
if (!isCmd(obj3, 'obj')) {
|
if (!isCmd(obj3, 'obj')) {
|
||||||
// some bad pdfs use "obj1234" and really mean 1234
|
// some bad PDFs use "obj1234" and really mean 1234
|
||||||
if (obj3.cmd.indexOf('obj') === 0) {
|
if (obj3.cmd.indexOf('obj') === 0) {
|
||||||
num = parseInt(obj3.cmd.substring(3), 10);
|
num = parseInt(obj3.cmd.substring(3), 10);
|
||||||
if (!isNaN(num)) {
|
if (!isNaN(num)) {
|
||||||
@ -1103,9 +1100,9 @@ var XRef = (function XRefClosure() {
|
|||||||
xrefEntry = parser.getObj(this.encrypt.createCipherTransform(num,
|
xrefEntry = parser.getObj(this.encrypt.createCipherTransform(num,
|
||||||
gen));
|
gen));
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
// almost all streams must be encrypted, but sometimes
|
// Almost all streams must be encrypted, but sometimes
|
||||||
// they are not probably due to some broken generators
|
// they are not, probably due to some broken generators.
|
||||||
// re-trying without encryption
|
// Retrying without encryption...
|
||||||
return this.fetch(ref, true);
|
return this.fetch(ref, true);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -1195,8 +1192,8 @@ var XRef = (function XRefClosure() {
|
|||||||
})();
|
})();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A NameTree is like a Dict but has some adventagous properties, see the spec
|
* A NameTree is like a Dict but has some advantageous properties, see the
|
||||||
* (7.9.6) for more details.
|
* spec (7.9.6) for more details.
|
||||||
* TODO: implement all the Dict functions and make this more efficent.
|
* TODO: implement all the Dict functions and make this more efficent.
|
||||||
*/
|
*/
|
||||||
var NameTree = (function NameTreeClosure() {
|
var NameTree = (function NameTreeClosure() {
|
||||||
@ -1259,7 +1256,6 @@ var NameTree = (function NameTreeClosure() {
|
|||||||
* entire PDF document object graph to be traversed.
|
* entire PDF document object graph to be traversed.
|
||||||
*/
|
*/
|
||||||
var ObjectLoader = (function() {
|
var ObjectLoader = (function() {
|
||||||
|
|
||||||
function mayHaveChildren(value) {
|
function mayHaveChildren(value) {
|
||||||
return isRef(value) || isDict(value) || isArray(value) || isStream(value);
|
return isRef(value) || isDict(value) || isArray(value) || isStream(value);
|
||||||
}
|
}
|
||||||
@ -1296,7 +1292,6 @@ var ObjectLoader = (function() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ObjectLoader.prototype = {
|
ObjectLoader.prototype = {
|
||||||
|
|
||||||
load: function ObjectLoader_load() {
|
load: function ObjectLoader_load() {
|
||||||
var keys = this.keys;
|
var keys = this.keys;
|
||||||
this.promise = new LegacyPromise();
|
this.promise = new LegacyPromise();
|
||||||
@ -1384,7 +1379,6 @@ var ObjectLoader = (function() {
|
|||||||
this.refSet = null;
|
this.refSet = null;
|
||||||
this.promise.resolve();
|
this.promise.resolve();
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return ObjectLoader;
|
return ObjectLoader;
|
||||||
|
@ -24,7 +24,7 @@
|
|||||||
var EOF = {};
|
var EOF = {};
|
||||||
|
|
||||||
function isEOF(v) {
|
function isEOF(v) {
|
||||||
return v == EOF;
|
return (v == EOF);
|
||||||
}
|
}
|
||||||
|
|
||||||
var Parser = (function ParserClosure() {
|
var Parser = (function ParserClosure() {
|
||||||
@ -62,10 +62,12 @@ var Parser = (function ParserClosure() {
|
|||||||
if (isCmd(this.buf1, '[')) { // array
|
if (isCmd(this.buf1, '[')) { // array
|
||||||
this.shift();
|
this.shift();
|
||||||
var array = [];
|
var array = [];
|
||||||
while (!isCmd(this.buf1, ']') && !isEOF(this.buf1))
|
while (!isCmd(this.buf1, ']') && !isEOF(this.buf1)) {
|
||||||
array.push(this.getObj(cipherTransform));
|
array.push(this.getObj(cipherTransform));
|
||||||
if (isEOF(this.buf1))
|
}
|
||||||
|
if (isEOF(this.buf1)) {
|
||||||
error('End of file inside array');
|
error('End of file inside array');
|
||||||
|
}
|
||||||
this.shift();
|
this.shift();
|
||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
@ -74,25 +76,27 @@ var Parser = (function ParserClosure() {
|
|||||||
var dict = new Dict(this.xref);
|
var dict = new Dict(this.xref);
|
||||||
while (!isCmd(this.buf1, '>>') && !isEOF(this.buf1)) {
|
while (!isCmd(this.buf1, '>>') && !isEOF(this.buf1)) {
|
||||||
if (!isName(this.buf1)) {
|
if (!isName(this.buf1)) {
|
||||||
info('Malformed dictionary, key must be a name object');
|
info('Malformed dictionary: key must be a name object');
|
||||||
this.shift();
|
this.shift();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
var key = this.buf1.name;
|
var key = this.buf1.name;
|
||||||
this.shift();
|
this.shift();
|
||||||
if (isEOF(this.buf1))
|
if (isEOF(this.buf1)) {
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
dict.set(key, this.getObj(cipherTransform));
|
dict.set(key, this.getObj(cipherTransform));
|
||||||
}
|
}
|
||||||
if (isEOF(this.buf1))
|
if (isEOF(this.buf1)) {
|
||||||
error('End of file inside dictionary');
|
error('End of file inside dictionary');
|
||||||
|
}
|
||||||
|
|
||||||
// stream objects are not allowed inside content streams or
|
// Stream objects are not allowed inside content streams or
|
||||||
// object streams
|
// object streams.
|
||||||
if (isCmd(this.buf2, 'stream')) {
|
if (isCmd(this.buf2, 'stream')) {
|
||||||
return this.allowStreams ?
|
return (this.allowStreams ?
|
||||||
this.makeStream(dict, cipherTransform) : dict;
|
this.makeStream(dict, cipherTransform) : dict);
|
||||||
}
|
}
|
||||||
this.shift();
|
this.shift();
|
||||||
return dict;
|
return dict;
|
||||||
@ -111,8 +115,9 @@ var Parser = (function ParserClosure() {
|
|||||||
if (isString(this.buf1)) { // string
|
if (isString(this.buf1)) { // string
|
||||||
var str = this.buf1;
|
var str = this.buf1;
|
||||||
this.shift();
|
this.shift();
|
||||||
if (cipherTransform)
|
if (cipherTransform) {
|
||||||
str = cipherTransform.decryptString(str);
|
str = cipherTransform.decryptString(str);
|
||||||
|
}
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -128,13 +133,15 @@ var Parser = (function ParserClosure() {
|
|||||||
// parse dictionary
|
// parse dictionary
|
||||||
var dict = new Dict();
|
var dict = new Dict();
|
||||||
while (!isCmd(this.buf1, 'ID') && !isEOF(this.buf1)) {
|
while (!isCmd(this.buf1, 'ID') && !isEOF(this.buf1)) {
|
||||||
if (!isName(this.buf1))
|
if (!isName(this.buf1)) {
|
||||||
error('Dictionary key must be a name object');
|
error('Dictionary key must be a name object');
|
||||||
|
}
|
||||||
|
|
||||||
var key = this.buf1.name;
|
var key = this.buf1.name;
|
||||||
this.shift();
|
this.shift();
|
||||||
if (isEOF(this.buf1))
|
if (isEOF(this.buf1)) {
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
dict.set(key, this.getObj(cipherTransform));
|
dict.set(key, this.getObj(cipherTransform));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -158,13 +165,13 @@ var Parser = (function ParserClosure() {
|
|||||||
break; // some binary stuff found, resetting the state
|
break; // some binary stuff found, resetting the state
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
state = state === 3 ? 4 : 0;
|
state = (state === 3 ? 4 : 0);
|
||||||
break;
|
break;
|
||||||
case 0x45:
|
case 0x45:
|
||||||
state = 2;
|
state = 2;
|
||||||
break;
|
break;
|
||||||
case 0x49:
|
case 0x49:
|
||||||
state = state === 2 ? 3 : 0;
|
state = (state === 2 ? 3 : 0);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
state = 0;
|
state = 0;
|
||||||
@ -224,7 +231,7 @@ var Parser = (function ParserClosure() {
|
|||||||
},
|
},
|
||||||
fetchIfRef: function Parser_fetchIfRef(obj) {
|
fetchIfRef: function Parser_fetchIfRef(obj) {
|
||||||
// not relying on the xref.fetchIfRef -- xref might not be set
|
// not relying on the xref.fetchIfRef -- xref might not be set
|
||||||
return isRef(obj) ? this.xref.fetch(obj) : obj;
|
return (isRef(obj) ? this.xref.fetch(obj) : obj);
|
||||||
},
|
},
|
||||||
makeStream: function Parser_makeStream(dict, cipherTransform) {
|
makeStream: function Parser_makeStream(dict, cipherTransform) {
|
||||||
var lexer = this.lexer;
|
var lexer = this.lexer;
|
||||||
@ -293,8 +300,9 @@ var Parser = (function ParserClosure() {
|
|||||||
this.shift(); // 'endstream'
|
this.shift(); // 'endstream'
|
||||||
|
|
||||||
stream = stream.makeSubStream(pos, length, dict);
|
stream = stream.makeSubStream(pos, length, dict);
|
||||||
if (cipherTransform)
|
if (cipherTransform) {
|
||||||
stream = cipherTransform.createStream(stream, length);
|
stream = cipherTransform.createStream(stream, length);
|
||||||
|
}
|
||||||
stream = this.filter(stream, dict, length);
|
stream = this.filter(stream, dict, length);
|
||||||
stream.dict = dict;
|
stream.dict = dict;
|
||||||
return stream;
|
return stream;
|
||||||
@ -302,8 +310,9 @@ var Parser = (function ParserClosure() {
|
|||||||
filter: function Parser_filter(stream, dict, length) {
|
filter: function Parser_filter(stream, dict, length) {
|
||||||
var filter = this.fetchIfRef(dict.get('Filter', 'F'));
|
var filter = this.fetchIfRef(dict.get('Filter', 'F'));
|
||||||
var params = this.fetchIfRef(dict.get('DecodeParms', 'DP'));
|
var params = this.fetchIfRef(dict.get('DecodeParms', 'DP'));
|
||||||
if (isName(filter))
|
if (isName(filter)) {
|
||||||
return this.makeFilter(stream, filter.name, length, params);
|
return this.makeFilter(stream, filter.name, length, params);
|
||||||
|
}
|
||||||
|
|
||||||
var maybeLength = length;
|
var maybeLength = length;
|
||||||
if (isArray(filter)) {
|
if (isArray(filter)) {
|
||||||
@ -311,12 +320,14 @@ var Parser = (function ParserClosure() {
|
|||||||
var paramsArray = params;
|
var paramsArray = params;
|
||||||
for (var i = 0, ii = filterArray.length; i < ii; ++i) {
|
for (var i = 0, ii = filterArray.length; i < ii; ++i) {
|
||||||
filter = filterArray[i];
|
filter = filterArray[i];
|
||||||
if (!isName(filter))
|
if (!isName(filter)) {
|
||||||
error('Bad filter name: ' + filter);
|
error('Bad filter name: ' + filter);
|
||||||
|
}
|
||||||
|
|
||||||
params = null;
|
params = null;
|
||||||
if (isArray(paramsArray) && (i in paramsArray))
|
if (isArray(paramsArray) && (i in paramsArray)) {
|
||||||
params = paramsArray[i];
|
params = paramsArray[i];
|
||||||
|
}
|
||||||
stream = this.makeFilter(stream, filter.name, maybeLength, params);
|
stream = this.makeFilter(stream, filter.name, maybeLength, params);
|
||||||
// after the first stream the length variable is invalid
|
// after the first stream the length variable is invalid
|
||||||
maybeLength = null;
|
maybeLength = null;
|
||||||
@ -338,11 +349,12 @@ var Parser = (function ParserClosure() {
|
|||||||
if (name == 'LZWDecode' || name == 'LZW') {
|
if (name == 'LZWDecode' || name == 'LZW') {
|
||||||
var earlyChange = 1;
|
var earlyChange = 1;
|
||||||
if (params) {
|
if (params) {
|
||||||
if (params.has('EarlyChange'))
|
if (params.has('EarlyChange')) {
|
||||||
earlyChange = params.get('EarlyChange');
|
earlyChange = params.get('EarlyChange');
|
||||||
|
}
|
||||||
return new PredictorStream(
|
return new PredictorStream(
|
||||||
new LZWStream(stream, maybeLength, earlyChange),
|
new LZWStream(stream, maybeLength, earlyChange),
|
||||||
maybeLength, params);
|
maybeLength, params);
|
||||||
}
|
}
|
||||||
return new LZWStream(stream, maybeLength, earlyChange);
|
return new LZWStream(stream, maybeLength, earlyChange);
|
||||||
}
|
}
|
||||||
@ -398,29 +410,29 @@ var Lexer = (function LexerClosure() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Lexer.isSpace = function Lexer_isSpace(ch) {
|
Lexer.isSpace = function Lexer_isSpace(ch) {
|
||||||
// space is one of the following characters: SPACE, TAB, CR, or LF
|
// Space is one of the following characters: SPACE, TAB, CR or LF.
|
||||||
return ch === 0x20 || ch === 0x09 || ch === 0x0D || ch === 0x0A;
|
return (ch === 0x20 || ch === 0x09 || ch === 0x0D || ch === 0x0A);
|
||||||
};
|
};
|
||||||
|
|
||||||
// A '1' in this array means the character is white space. A '1' or
|
// A '1' in this array means the character is white space. A '1' or
|
||||||
// '2' means the character ends a name or command.
|
// '2' means the character ends a name or command.
|
||||||
var specialChars = [
|
var specialChars = [
|
||||||
1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, // 0x
|
1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, // 0x
|
||||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x
|
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
|
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, 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, 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, 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, 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, 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, // 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, // 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, // 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, // 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, // 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, // 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, // ex
|
||||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // fx
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // fx
|
||||||
];
|
];
|
||||||
|
|
||||||
function toHexDigit(ch) {
|
function toHexDigit(ch) {
|
||||||
@ -445,10 +457,8 @@ var Lexer = (function LexerClosure() {
|
|||||||
var ch = this.currentChar;
|
var ch = this.currentChar;
|
||||||
var eNotation = false;
|
var eNotation = false;
|
||||||
var divideBy = 0; // different from 0 if it's a floating point value
|
var divideBy = 0; // different from 0 if it's a floating point value
|
||||||
|
|
||||||
var sign = 1;
|
var sign = 1;
|
||||||
|
|
||||||
|
|
||||||
if (ch === 0x2D) { // '-'
|
if (ch === 0x2D) { // '-'
|
||||||
sign = -1;
|
sign = -1;
|
||||||
ch = this.nextChar();
|
ch = this.nextChar();
|
||||||
@ -459,7 +469,6 @@ var Lexer = (function LexerClosure() {
|
|||||||
divideBy = 10;
|
divideBy = 10;
|
||||||
ch = this.nextChar();
|
ch = this.nextChar();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ch < 0x30 || ch > 0x39) { // '0' - '9'
|
if (ch < 0x30 || ch > 0x39) { // '0' - '9'
|
||||||
error('Invalid number: ' + String.fromCharCode(ch));
|
error('Invalid number: ' + String.fromCharCode(ch));
|
||||||
return 0;
|
return 0;
|
||||||
@ -584,7 +593,6 @@ var Lexer = (function LexerClosure() {
|
|||||||
x = (x << 3) + (ch & 0x0F);
|
x = (x << 3) + (ch & 0x0F);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
strBuf.push(String.fromCharCode(x));
|
strBuf.push(String.fromCharCode(x));
|
||||||
break;
|
break;
|
||||||
case 0x0A: case 0x0D: // LF, CR
|
case 0x0A: case 0x0D: // LF, CR
|
||||||
@ -617,8 +625,9 @@ var Lexer = (function LexerClosure() {
|
|||||||
var x = toHexDigit(ch);
|
var x = toHexDigit(ch);
|
||||||
if (x != -1) {
|
if (x != -1) {
|
||||||
var x2 = toHexDigit(this.nextChar());
|
var x2 = toHexDigit(this.nextChar());
|
||||||
if (x2 == -1)
|
if (x2 == -1) {
|
||||||
error('Illegal digit in hex char in name: ' + x2);
|
error('Illegal digit in hex char in name: ' + x2);
|
||||||
|
}
|
||||||
strBuf.push(String.fromCharCode((x << 4) | x2));
|
strBuf.push(String.fromCharCode((x << 4) | x2));
|
||||||
} else {
|
} else {
|
||||||
strBuf.push('#', String.fromCharCode(ch));
|
strBuf.push('#', String.fromCharCode(ch));
|
||||||
@ -682,8 +691,9 @@ var Lexer = (function LexerClosure() {
|
|||||||
return EOF;
|
return EOF;
|
||||||
}
|
}
|
||||||
if (comment) {
|
if (comment) {
|
||||||
if (ch === 0x0A || ch == 0x0D) // LF, CR
|
if (ch === 0x0A || ch == 0x0D) { // LF, CR
|
||||||
comment = false;
|
comment = false;
|
||||||
|
}
|
||||||
} else if (ch === 0x25) { // '%'
|
} else if (ch === 0x25) { // '%'
|
||||||
comment = true;
|
comment = true;
|
||||||
} else if (specialChars[ch] !== 1) {
|
} else if (specialChars[ch] !== 1) {
|
||||||
@ -748,17 +758,21 @@ var Lexer = (function LexerClosure() {
|
|||||||
if (knownCommandFound && !(possibleCommand in knownCommands)) {
|
if (knownCommandFound && !(possibleCommand in knownCommands)) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (str.length == 128)
|
if (str.length == 128) {
|
||||||
error('Command token too long: ' + str.length);
|
error('Command token too long: ' + str.length);
|
||||||
|
}
|
||||||
str = possibleCommand;
|
str = possibleCommand;
|
||||||
knownCommandFound = knownCommands && (str in knownCommands);
|
knownCommandFound = knownCommands && (str in knownCommands);
|
||||||
}
|
}
|
||||||
if (str == 'true')
|
if (str == 'true') {
|
||||||
return true;
|
return true;
|
||||||
if (str == 'false')
|
}
|
||||||
|
if (str == 'false') {
|
||||||
return false;
|
return false;
|
||||||
if (str == 'null')
|
}
|
||||||
|
if (str == 'null') {
|
||||||
return null;
|
return null;
|
||||||
|
}
|
||||||
return Cmd.get(str);
|
return Cmd.get(str);
|
||||||
},
|
},
|
||||||
skipToNextLine: function Lexer_skipToNextLine() {
|
skipToNextLine: function Lexer_skipToNextLine() {
|
||||||
@ -793,8 +807,9 @@ var Linearization = (function LinearizationClosure() {
|
|||||||
if (isInt(obj1) && isInt(obj2) && isCmd(obj3, 'obj') &&
|
if (isInt(obj1) && isInt(obj2) && isCmd(obj3, 'obj') &&
|
||||||
isDict(this.linDict)) {
|
isDict(this.linDict)) {
|
||||||
var obj = this.linDict.get('Linearized');
|
var obj = this.linDict.get('Linearized');
|
||||||
if (!(isNum(obj) && obj > 0))
|
if (!(isNum(obj) && obj > 0)) {
|
||||||
this.linDict = null;
|
this.linDict = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -802,9 +817,7 @@ var Linearization = (function LinearizationClosure() {
|
|||||||
getInt: function Linearization_getInt(name) {
|
getInt: function Linearization_getInt(name) {
|
||||||
var linDict = this.linDict;
|
var linDict = this.linDict;
|
||||||
var obj;
|
var obj;
|
||||||
if (isDict(linDict) &&
|
if (isDict(linDict) && isInt(obj = linDict.get(name)) && obj > 0) {
|
||||||
isInt(obj = linDict.get(name)) &&
|
|
||||||
obj > 0) {
|
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
error('"' + name + '" field in linearization table is invalid');
|
error('"' + name + '" field in linearization table is invalid');
|
||||||
@ -812,18 +825,16 @@ var Linearization = (function LinearizationClosure() {
|
|||||||
getHint: function Linearization_getHint(index) {
|
getHint: function Linearization_getHint(index) {
|
||||||
var linDict = this.linDict;
|
var linDict = this.linDict;
|
||||||
var obj1, obj2;
|
var obj1, obj2;
|
||||||
if (isDict(linDict) &&
|
if (isDict(linDict) && isArray(obj1 = linDict.get('H')) &&
|
||||||
isArray(obj1 = linDict.get('H')) &&
|
obj1.length >= 2 && isInt(obj2 = obj1[index]) && obj2 > 0) {
|
||||||
obj1.length >= 2 &&
|
|
||||||
isInt(obj2 = obj1[index]) &&
|
|
||||||
obj2 > 0) {
|
|
||||||
return obj2;
|
return obj2;
|
||||||
}
|
}
|
||||||
error('Hints table in linearization table is invalid: ' + index);
|
error('Hints table in linearization table is invalid: ' + index);
|
||||||
},
|
},
|
||||||
get length() {
|
get length() {
|
||||||
if (!isDict(this.linDict))
|
if (!isDict(this.linDict)) {
|
||||||
return 0;
|
return 0;
|
||||||
|
}
|
||||||
return this.getInt('L');
|
return this.getInt('L');
|
||||||
},
|
},
|
||||||
get hintsOffset() {
|
get hintsOffset() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user