Making src/core/{image,obj,parser}.js adhere to the style guide

This commit is contained in:
Tim van der Meij 2014-03-20 20:28:22 +01:00
parent 878a123e47
commit 284288f1d0
3 changed files with 117 additions and 111 deletions

View File

@ -47,7 +47,7 @@ var PDFImage = (function PDFImageClosure() {
function decodeAndClamp(value, addend, coefficient, max) {
value = addend + value * coefficient;
// 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) {
this.image = image;
@ -89,7 +89,7 @@ var PDFImage = (function PDFImageClosure() {
if (!this.imageMask) {
var colorSpace = dict.get('ColorSpace', 'CS');
if (!colorSpace) {
warn('JPX images (which don"t require color spaces');
warn('JPX images (which do not require color spaces)');
colorSpace = Name.get('DeviceRGB');
}
this.colorSpace = ColorSpace.parse(colorSpace, xref, res);
@ -134,7 +134,7 @@ var PDFImage = (function PDFImageClosure() {
var imageDataPromise = new LegacyPromise();
var smaskPromise = 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.
Promise.all([imageDataPromise, smaskPromise, maskPromise]).then(
function(results) {
@ -184,8 +184,8 @@ var PDFImage = (function PDFImageClosure() {
PDFImage.resize = function PDFImage_resize(pixels, bpc, components,
w1, h1, w2, h2) {
var length = w2 * h2 * components;
var temp = bpc <= 8 ? new Uint8Array(length) :
bpc <= 16 ? new Uint16Array(length) : new Uint32Array(length);
var temp = (bpc <= 8 ? new Uint8Array(length) :
(bpc <= 16 ? new Uint16Array(length) : new Uint32Array(length)));
var xRatio = w1 / w2;
var yRatio = h1 / h2;
var px, py, newIndex, oldIndex;
@ -285,8 +285,8 @@ var PDFImage = (function PDFImageClosure() {
var length = width * height * numComps;
var bufferPos = 0;
var output = bpc <= 8 ? new Uint8Array(length) :
bpc <= 16 ? new Uint16Array(length) : new Uint32Array(length);
var output = (bpc <= 8 ? new Uint8Array(length) :
(bpc <= 16 ? new Uint16Array(length) : new Uint32Array(length)));
var rowComps = width * numComps;
var max = (1 << bpc) - 1;
@ -338,7 +338,7 @@ var PDFImage = (function PDFImageClosure() {
var remainingBits = bits - bpc;
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);
bits = remainingBits;
}
@ -418,7 +418,7 @@ var PDFImage = (function PDFImageClosure() {
}
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);
@ -529,14 +529,15 @@ var PDFImage = (function PDFImageClosure() {
},
fillGrayBuffer: function PDFImage_fillGrayBuffer(buffer) {
var numComps = this.numComps;
if (numComps != 1)
if (numComps != 1) {
error('Reading gray scale from a color image: ' + numComps);
}
var width = this.width;
var height = this.height;
var bpc = this.bpc;
// rows start at byte boundary;
// rows start at byte boundary
var rowBytes = (width * numComps * bpc + 7) >> 3;
var imgArray = this.getImageBytes(height * rowBytes);

View File

@ -34,7 +34,7 @@ var Name = (function NameClosure() {
Name.get = function Name_get(name) {
var nameValue = nameCache[name];
return nameValue ? nameValue : (nameCache[name] = new Name(name));
return (nameValue ? nameValue : (nameCache[name] = new Name(name)));
};
return Name;
@ -51,7 +51,7 @@ var Cmd = (function CmdClosure() {
Cmd.get = function Cmd_get(cmd) {
var cmdValue = cmdCache[cmd];
return cmdValue ? cmdValue : (cmdCache[cmd] = new Cmd(cmd));
return (cmdValue ? cmdValue : (cmdCache[cmd] = new Cmd(cmd)));
};
return Cmd;
@ -133,7 +133,7 @@ var Dict = (function DictClosure() {
var all = {};
for (var key in this.map) {
var obj = this.get(key);
all[key] = obj instanceof Dict ? obj.getAll() : obj;
all[key] = (obj instanceof Dict ? obj.getAll() : obj);
}
return all;
},
@ -167,8 +167,8 @@ var Ref = (function RefClosure() {
return Ref;
})();
// The reference is identified by number and generation,
// this structure stores only one instance of the reference.
// The reference is identified by number and generation.
// This structure stores only one instance of the reference.
var RefSet = (function RefSetClosure() {
function RefSet() {
this.dict = {};
@ -242,8 +242,8 @@ var Catalog = (function CatalogClosure() {
return shadow(this, 'metadata', null);
}
var encryptMetadata = !this.xref.encrypt ? false :
this.xref.encrypt.encryptMetadata;
var encryptMetadata = (!this.xref.encrypt ? false :
this.xref.encrypt.encryptMetadata);
var stream = this.xref.fetch(streamRef, !encryptMetadata);
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() {
var obj = this.toplevelPagesDict.get('Count');
@ -399,7 +399,7 @@ var Catalog = (function CatalogClosure() {
if (!names.hasOwnProperty(name)) {
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.
var jsDict = names[name];
if (!isDict(jsDict)) {
@ -535,7 +535,7 @@ var Catalog = (function CatalogClosure() {
var found = false;
for (var i = 0; i < kids.length; 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) {
found = true;
break;
@ -580,7 +580,6 @@ var Catalog = (function CatalogClosure() {
var XRef = (function XRefClosure() {
function XRef(stream, password) {
this.stream = stream;
this.entries = [];
this.xrefstms = {};
@ -610,8 +609,8 @@ var XRef = (function XRefClosure() {
if (encrypt) {
var ids = trailerDict.get('ID');
var fileId = (ids && ids.length) ? ids[0] : '';
this.encrypt = new CipherTransformFactory(
encrypt, fileId, this.password);
this.encrypt = new CipherTransformFactory(encrypt, fileId,
this.password);
}
// get the root dictionary (catalog) object
@ -702,10 +701,11 @@ var XRef = (function XRefClosure() {
entry.gen = parser.getObj();
var type = parser.getObj();
if (isCmd(type, 'f'))
if (isCmd(type, 'f')) {
entry.free = true;
else if (isCmd(type, 'n'))
} else if (isCmd(type, 'n')) {
entry.uncompressed = true;
}
// Validate entry obj
if (!isInt(entry.offset) || !isInt(entry.gen) ||
@ -777,7 +777,6 @@ var XRef = (function XRefClosure() {
var entryRanges = streamState.entryRanges;
while (entryRanges.length > 0) {
var first = entryRanges[0];
var n = entryRanges[1];
@ -793,9 +792,10 @@ var XRef = (function XRefClosure() {
streamState.streamPos = stream.pos;
var type = 0, offset = 0, generation = 0;
for (j = 0; j < typeFieldWidth; ++j)
for (j = 0; j < typeFieldWidth; ++j) {
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) {
type = 1;
}
@ -837,8 +837,9 @@ var XRef = (function XRefClosure() {
function readToken(data, offset) {
var token = '', ch = data[offset];
while (ch !== 13 && ch !== 10) {
if (++offset >= data.length)
if (++offset >= data.length) {
break;
}
token += String.fromCharCode(ch);
ch = data[offset];
}
@ -853,9 +854,9 @@ var XRef = (function XRefClosure() {
while (i < length && data[offset + i] == what[i]) {
++i;
}
if (i >= length)
if (i >= length) {
break; // sequence found
}
offset++;
skipped++;
}
@ -967,7 +968,6 @@ var XRef = (function XRefClosure() {
// Get dictionary
if (isCmd(obj, 'xref')) {
// Parse end-of-file XRef
dict = this.processXRefTable(parser);
if (!this.topDict) {
@ -986,7 +986,6 @@ var XRef = (function XRefClosure() {
}
}
} else if (isInt(obj)) {
// Parse in-stream XRef
if (!isInt(parser.getObj()) ||
!isCmd(parser.getObj(), 'obj') ||
@ -997,7 +996,6 @@ var XRef = (function XRefClosure() {
if (!this.topDict) {
this.topDict = dict;
}
if (!dict) {
error('Failed to read XRef stream');
}
@ -1069,8 +1067,7 @@ var XRef = (function XRefClosure() {
}
},
fetchUncompressed: function XRef_fetchUncompressed(ref,
xrefEntry,
fetchUncompressed: function XRef_fetchUncompressed(ref, xrefEntry,
suppressEncryption) {
var gen = ref.gen;
var num = ref.num;
@ -1089,7 +1086,7 @@ var XRef = (function XRefClosure() {
error('bad XRef entry');
}
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) {
num = parseInt(obj3.cmd.substring(3), 10);
if (!isNaN(num)) {
@ -1103,9 +1100,9 @@ var XRef = (function XRefClosure() {
xrefEntry = parser.getObj(this.encrypt.createCipherTransform(num,
gen));
} catch (ex) {
// almost all streams must be encrypted, but sometimes
// they are not probably due to some broken generators
// re-trying without encryption
// Almost all streams must be encrypted, but sometimes
// they are not, probably due to some broken generators.
// Retrying without encryption...
return this.fetch(ref, true);
}
} else {
@ -1195,8 +1192,8 @@ var XRef = (function XRefClosure() {
})();
/**
* A NameTree is like a Dict but has some adventagous properties, see the spec
* (7.9.6) for more details.
* A NameTree is like a Dict but has some advantageous properties, see the
* spec (7.9.6) for more details.
* TODO: implement all the Dict functions and make this more efficent.
*/
var NameTree = (function NameTreeClosure() {
@ -1259,7 +1256,6 @@ var NameTree = (function NameTreeClosure() {
* entire PDF document object graph to be traversed.
*/
var ObjectLoader = (function() {
function mayHaveChildren(value) {
return isRef(value) || isDict(value) || isArray(value) || isStream(value);
}
@ -1296,7 +1292,6 @@ var ObjectLoader = (function() {
}
ObjectLoader.prototype = {
load: function ObjectLoader_load() {
var keys = this.keys;
this.promise = new LegacyPromise();
@ -1384,7 +1379,6 @@ var ObjectLoader = (function() {
this.refSet = null;
this.promise.resolve();
}
};
return ObjectLoader;

View File

@ -24,7 +24,7 @@
var EOF = {};
function isEOF(v) {
return v == EOF;
return (v == EOF);
}
var Parser = (function ParserClosure() {
@ -62,10 +62,12 @@ var Parser = (function ParserClosure() {
if (isCmd(this.buf1, '[')) { // array
this.shift();
var array = [];
while (!isCmd(this.buf1, ']') && !isEOF(this.buf1))
while (!isCmd(this.buf1, ']') && !isEOF(this.buf1)) {
array.push(this.getObj(cipherTransform));
if (isEOF(this.buf1))
}
if (isEOF(this.buf1)) {
error('End of file inside array');
}
this.shift();
return array;
}
@ -74,25 +76,27 @@ var Parser = (function ParserClosure() {
var dict = new Dict(this.xref);
while (!isCmd(this.buf1, '>>') && !isEOF(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();
continue;
}
var key = this.buf1.name;
this.shift();
if (isEOF(this.buf1))
if (isEOF(this.buf1)) {
break;
}
dict.set(key, this.getObj(cipherTransform));
}
if (isEOF(this.buf1))
if (isEOF(this.buf1)) {
error('End of file inside dictionary');
}
// stream objects are not allowed inside content streams or
// object streams
// Stream objects are not allowed inside content streams or
// object streams.
if (isCmd(this.buf2, 'stream')) {
return this.allowStreams ?
this.makeStream(dict, cipherTransform) : dict;
return (this.allowStreams ?
this.makeStream(dict, cipherTransform) : dict);
}
this.shift();
return dict;
@ -111,8 +115,9 @@ var Parser = (function ParserClosure() {
if (isString(this.buf1)) { // string
var str = this.buf1;
this.shift();
if (cipherTransform)
if (cipherTransform) {
str = cipherTransform.decryptString(str);
}
return str;
}
@ -128,13 +133,15 @@ var Parser = (function ParserClosure() {
// parse dictionary
var dict = new Dict();
while (!isCmd(this.buf1, 'ID') && !isEOF(this.buf1)) {
if (!isName(this.buf1))
if (!isName(this.buf1)) {
error('Dictionary key must be a name object');
}
var key = this.buf1.name;
this.shift();
if (isEOF(this.buf1))
if (isEOF(this.buf1)) {
break;
}
dict.set(key, this.getObj(cipherTransform));
}
@ -158,13 +165,13 @@ var Parser = (function ParserClosure() {
break; // some binary stuff found, resetting the state
}
}
state = state === 3 ? 4 : 0;
state = (state === 3 ? 4 : 0);
break;
case 0x45:
state = 2;
break;
case 0x49:
state = state === 2 ? 3 : 0;
state = (state === 2 ? 3 : 0);
break;
default:
state = 0;
@ -224,7 +231,7 @@ var Parser = (function ParserClosure() {
},
fetchIfRef: function Parser_fetchIfRef(obj) {
// 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) {
var lexer = this.lexer;
@ -293,8 +300,9 @@ var Parser = (function ParserClosure() {
this.shift(); // 'endstream'
stream = stream.makeSubStream(pos, length, dict);
if (cipherTransform)
if (cipherTransform) {
stream = cipherTransform.createStream(stream, length);
}
stream = this.filter(stream, dict, length);
stream.dict = dict;
return stream;
@ -302,8 +310,9 @@ var Parser = (function ParserClosure() {
filter: function Parser_filter(stream, dict, length) {
var filter = this.fetchIfRef(dict.get('Filter', 'F'));
var params = this.fetchIfRef(dict.get('DecodeParms', 'DP'));
if (isName(filter))
if (isName(filter)) {
return this.makeFilter(stream, filter.name, length, params);
}
var maybeLength = length;
if (isArray(filter)) {
@ -311,12 +320,14 @@ var Parser = (function ParserClosure() {
var paramsArray = params;
for (var i = 0, ii = filterArray.length; i < ii; ++i) {
filter = filterArray[i];
if (!isName(filter))
if (!isName(filter)) {
error('Bad filter name: ' + filter);
}
params = null;
if (isArray(paramsArray) && (i in paramsArray))
if (isArray(paramsArray) && (i in paramsArray)) {
params = paramsArray[i];
}
stream = this.makeFilter(stream, filter.name, maybeLength, params);
// after the first stream the length variable is invalid
maybeLength = null;
@ -338,8 +349,9 @@ var Parser = (function ParserClosure() {
if (name == 'LZWDecode' || name == 'LZW') {
var earlyChange = 1;
if (params) {
if (params.has('EarlyChange'))
if (params.has('EarlyChange')) {
earlyChange = params.get('EarlyChange');
}
return new PredictorStream(
new LZWStream(stream, maybeLength, earlyChange),
maybeLength, params);
@ -398,8 +410,8 @@ var Lexer = (function LexerClosure() {
}
Lexer.isSpace = function Lexer_isSpace(ch) {
// space is one of the following characters: SPACE, TAB, CR, or LF
return ch === 0x20 || ch === 0x09 || ch === 0x0D || ch === 0x0A;
// Space is one of the following characters: SPACE, TAB, CR or LF.
return (ch === 0x20 || ch === 0x09 || ch === 0x0D || ch === 0x0A);
};
// A '1' in this array means the character is white space. A '1' or
@ -445,10 +457,8 @@ var Lexer = (function LexerClosure() {
var ch = this.currentChar;
var eNotation = false;
var divideBy = 0; // different from 0 if it's a floating point value
var sign = 1;
if (ch === 0x2D) { // '-'
sign = -1;
ch = this.nextChar();
@ -459,7 +469,6 @@ var Lexer = (function LexerClosure() {
divideBy = 10;
ch = this.nextChar();
}
if (ch < 0x30 || ch > 0x39) { // '0' - '9'
error('Invalid number: ' + String.fromCharCode(ch));
return 0;
@ -584,7 +593,6 @@ var Lexer = (function LexerClosure() {
x = (x << 3) + (ch & 0x0F);
}
}
strBuf.push(String.fromCharCode(x));
break;
case 0x0A: case 0x0D: // LF, CR
@ -617,8 +625,9 @@ var Lexer = (function LexerClosure() {
var x = toHexDigit(ch);
if (x != -1) {
var x2 = toHexDigit(this.nextChar());
if (x2 == -1)
if (x2 == -1) {
error('Illegal digit in hex char in name: ' + x2);
}
strBuf.push(String.fromCharCode((x << 4) | x2));
} else {
strBuf.push('#', String.fromCharCode(ch));
@ -682,8 +691,9 @@ var Lexer = (function LexerClosure() {
return EOF;
}
if (comment) {
if (ch === 0x0A || ch == 0x0D) // LF, CR
if (ch === 0x0A || ch == 0x0D) { // LF, CR
comment = false;
}
} else if (ch === 0x25) { // '%'
comment = true;
} else if (specialChars[ch] !== 1) {
@ -748,17 +758,21 @@ var Lexer = (function LexerClosure() {
if (knownCommandFound && !(possibleCommand in knownCommands)) {
break;
}
if (str.length == 128)
if (str.length == 128) {
error('Command token too long: ' + str.length);
}
str = possibleCommand;
knownCommandFound = knownCommands && (str in knownCommands);
}
if (str == 'true')
if (str == 'true') {
return true;
if (str == 'false')
}
if (str == 'false') {
return false;
if (str == 'null')
}
if (str == 'null') {
return null;
}
return Cmd.get(str);
},
skipToNextLine: function Lexer_skipToNextLine() {
@ -793,18 +807,17 @@ var Linearization = (function LinearizationClosure() {
if (isInt(obj1) && isInt(obj2) && isCmd(obj3, 'obj') &&
isDict(this.linDict)) {
var obj = this.linDict.get('Linearized');
if (!(isNum(obj) && obj > 0))
if (!(isNum(obj) && obj > 0)) {
this.linDict = null;
}
}
}
Linearization.prototype = {
getInt: function Linearization_getInt(name) {
var linDict = this.linDict;
var obj;
if (isDict(linDict) &&
isInt(obj = linDict.get(name)) &&
obj > 0) {
if (isDict(linDict) && isInt(obj = linDict.get(name)) && obj > 0) {
return obj;
}
error('"' + name + '" field in linearization table is invalid');
@ -812,18 +825,16 @@ var Linearization = (function LinearizationClosure() {
getHint: function Linearization_getHint(index) {
var linDict = this.linDict;
var obj1, obj2;
if (isDict(linDict) &&
isArray(obj1 = linDict.get('H')) &&
obj1.length >= 2 &&
isInt(obj2 = obj1[index]) &&
obj2 > 0) {
if (isDict(linDict) && isArray(obj1 = linDict.get('H')) &&
obj1.length >= 2 && isInt(obj2 = obj1[index]) && obj2 > 0) {
return obj2;
}
error('Hints table in linearization table is invalid: ' + index);
},
get length() {
if (!isDict(this.linDict))
if (!isDict(this.linDict)) {
return 0;
}
return this.getInt('L');
},
get hintsOffset() {