ColorSpace.parseToIR = function colorSpaceParseToIR(cs, xref, res) {
if (isName(cs)) {
- var colorSpaces = xref.fetchIfRef(res.get('ColorSpace'));
+ var colorSpaces = res.get('ColorSpace');
if (isDict(colorSpaces)) {
var refcs = colorSpaces.get(cs.name);
if (refcs)
var tintFnIR = PDFFunction.getIR(xref, xref.fetchIfRef(cs[3]));
return ['AlternateCS', numComps, alt, tintFnIR];
case 'Lab':
- var params = cs[1].map;
+ var params = cs[1].getAll();
return ['LabCS', params];
default:
error('unimplemented color space object "' + mode + '"');
Page.prototype = {
getPageProp: function pageGetPageProp(key) {
- return this.xref.fetchIfRef(this.pageDict.get(key));
+ return this.pageDict.get(key);
},
inheritPageProp: function pageInheritPageProp(key) {
var dict = this.pageDict;
var obj = dict.get(key);
while (obj === undefined) {
- dict = this.xref.fetchIfRef(dict.get('Parent'));
+ dict = dict.get('Parent');
if (!dict)
break;
obj = dict.get(key);
this.stats.time('Build IR Queue');
var xref = this.xref;
- var content = xref.fetchIfRef(this.content);
- var resources = xref.fetchIfRef(this.resources);
+ var content = this.content;
+ var resources = this.resources;
if (isArray(content)) {
// fetching items
var i, n = content.length;
var stats = this.stats;
stats.time('Rendering');
var xref = this.xref;
- var resources = xref.fetchIfRef(this.resources);
- var mediaBox = xref.fetchIfRef(this.mediaBox);
+ var resources = this.resources;
+ var mediaBox = this.mediaBox;
assertWellFormed(isDict(resources), 'invalid page resources');
gfx.xref = xref;
function getInheritableProperty(annotation, name) {
var item = annotation;
while (item && !item.has(name)) {
- item = xref.fetchIfRef(item.get('Parent'));
+ item = item.get('Parent');
}
if (!item)
return null;
}
}
- var annotations = xref.fetchIfRef(this.annotations) || [];
+ var annotations = this.annotations || [];
var i, n = annotations.length;
var items = [];
for (i = 0; i < n; ++i) {
item.height = Math.abs(topLeftCorner.y - bottomRightCorner.y);
switch (subtype.name) {
case 'Link':
- var a = this.xref.fetchIfRef(annotation.get('A'));
+ var a = annotation.get('A');
if (a) {
switch (a.get('S').name) {
case 'URI':
var fieldName = [];
var namedItem = annotation, ref = annotationRef;
while (namedItem) {
- var parentRef = namedItem.get('Parent');
- var parent = xref.fetchIfRef(parentRef);
+ var parent = namedItem.get('Parent');
+ var parentRef = namedItem.getRaw('Parent');
var name = namedItem.get('T');
- if (name)
+ if (name) {
fieldName.unshift(stringToPDFString(name));
- else {
+ } else {
// The field name is absent, that means more than one field
// with the same name may exist. Replacing the empty name
// with the '`' plus index in the parent's 'Kids' array.
// This is not in the PDF spec but necessary to id the
// the input controls.
- var kids = xref.fetchIfRef(parent.get('Kids'));
+ var kids = parent.get('Kids');
var j, jj;
for (j = 0, jj = kids.length; j < jj; j++) {
- if (kids[j].num == ref.num && kids[j].gen == ref.gen)
+ var kidRef = kids[j];
+ if (kidRef.num == ref.num && kidRef.gen == ref.gen)
break;
}
fieldName.unshift('`' + j);
assertWellFormed(stream.length > 0, 'stream must have data');
this.stream = stream;
this.setup();
- this.acroForm = this.xref.fetchIfRef(this.catalog.catDict.get('AcroForm'));
+ this.acroForm = this.catalog.catDict.get('AcroForm');
}
function find(stream, needle, limit, backwards) {
getDocumentInfo: function pdfDocGetDocumentInfo() {
var info;
if (this.xref.trailer.has('Info'))
- info = this.xref.fetch(this.xref.trailer.get('Info'));
+ info = this.xref.trailer.get('Info');
return shadow(this, 'getDocumentInfo', info);
},
var xref = this.xref, fileID;
if (xref.trailer.has('ID')) {
fileID = '';
- var id = xref.fetchIfRef(xref.trailer.get('ID'))[0];
+ var id = xref.trailer.get('ID')[0];
id.split('').forEach(function(el) {
fileID += Number(el.charCodeAt(0)).toString(16);
});
}
}
- function handleSetFont(fontName, fontRef) {
+ function handleSetFont(fontName, font) {
var loadedName = null;
var fontRes = resources.get('Font');
assert(fontRes, 'fontRes not available');
- fontRes = xref.fetchIfRef(fontRes);
- fontRef = fontRef || fontRes.get(fontName);
- var font = xref.fetchIfRef(fontRef);
+ font = xref.fetchIfRef(font) || fontRes.get(fontName);
assertWellFormed(isDict(font));
if (!font.translated) {
font.translated = self.translateFont(font, xref, resources,
var fnArray = queue.fnArray, argsArray = queue.argsArray;
var dependencyArray = dependency || [];
- resources = xref.fetchIfRef(resources) || new Dict();
- var xobjs = xref.fetchIfRef(resources.get('XObject')) || new Dict();
- var patterns = xref.fetchIfRef(resources.get('Pattern')) || new Dict();
- var parser = new Parser(new Lexer(stream), false);
+ resources = resources || new Dict();
+ var xobjs = resources.get('XObject') || new Dict();
+ var patterns = resources.get('Pattern') || new Dict();
+ var parser = new Parser(new Lexer(stream), false, xref);
var res = resources;
var args = [], obj;
var getObjBt = function getObjBt() {
var patternName = args[args.length - 1];
// SCN/scn applies patterns along with normal colors
if (isName(patternName)) {
- var pattern = xref.fetchIfRef(patterns.get(patternName.name));
+ var pattern = patterns.get(patternName.name);
if (pattern) {
var dict = isStream(pattern) ? pattern.dict : pattern;
var typeNum = dict.get('PatternType');
args = TilingPattern.getIR(operatorList, dict, args);
}
else if (typeNum == SHADING_PATTERN) {
- var shading = xref.fetchIfRef(dict.get('Shading'));
+ var shading = dict.get('Shading');
var matrix = dict.get('Matrix');
var pattern = Pattern.parseShading(shading, matrix, xref, res,
null /*ctx*/);
var name = args[0].name;
var xobj = xobjs.get(name);
if (xobj) {
- xobj = xref.fetchIfRef(xobj);
assertWellFormed(isStream(xobj), 'XObject should be a stream');
var type = xobj.dict.get('Subtype');
args = [ColorSpace.parseToIR(args[0], xref, resources)];
break;
case 'shadingFill':
- var shadingRes = xref.fetchIfRef(res.get('Shading'));
+ var shadingRes = res.get('Shading');
if (!shadingRes)
error('No shading resource found');
- var shading = xref.fetchIfRef(shadingRes.get(args[0].name));
+ var shading = shadingRes.get(args[0].name);
if (!shading)
error('No shading object found');
break;
case 'setGState':
var dictName = args[0];
- var extGState = xref.fetchIfRef(resources.get('ExtGState'));
+ var extGState = resources.get('ExtGState');
if (!isDict(extGState) || !extGState.has(dictName.name))
break;
- var gsState = xref.fetchIfRef(extGState.get(dictName.name));
+ var gsState = extGState.get(dictName.name);
// This array holds the converted/processed state data.
var gsStateObj = [];
if (properties.composite) {
// CIDSystemInfo helps to match CID to glyphs
- var cidSystemInfo = xref.fetchIfRef(dict.get('CIDSystemInfo'));
+ var cidSystemInfo = dict.get('CIDSystemInfo');
if (isDict(cidSystemInfo)) {
properties.cidSystemInfo = {
registry: cidSystemInfo.get('Registry'),
};
}
- var cidToGidMap = xref.fetchIfRef(dict.get('CIDToGIDMap'));
+ var cidToGidMap = dict.get('CIDToGIDMap');
if (isStream(cidToGidMap))
properties.cidToGidMap = this.readCidToGidMap(cidToGidMap);
}
Encodings.symbolsEncoding : Encodings.StandardEncoding;
var hasEncoding = dict.has('Encoding');
if (hasEncoding) {
- var encoding = xref.fetchIfRef(dict.get('Encoding'));
+ var encoding = dict.get('Encoding');
if (isDict(encoding)) {
var baseName = encoding.get('BaseEncoding');
if (baseName)
readToUnicode:
function partialEvaluatorReadToUnicode(toUnicode, xref) {
- var cmapObj = xref.fetchIfRef(toUnicode);
+ var cmapObj = toUnicode;
var charToUnicode = [];
if (isName(cmapObj)) {
var isIdentityMap = cmapObj.name.substr(0, 9) == 'Identity-';
var glyphsWidths = [];
var defaultWidth = 0;
if (properties.composite) {
- defaultWidth = xref.fetchIfRef(dict.get('DW')) || 1000;
+ defaultWidth = dict.get('DW') || 1000;
- var widths = xref.fetchIfRef(dict.get('W'));
+ var widths = dict.get('W');
if (widths) {
var start = 0, end = 0;
for (var i = 0, ii = widths.length; i < ii; i++) {
}
} else {
var firstChar = properties.firstChar;
- var widths = xref.fetchIfRef(dict.get('Widths'));
+ var widths = dict.get('Widths');
if (widths) {
var j = firstChar;
for (var i = 0, ii = widths.length; i < ii; i++)
if (!df)
return null;
- if (isRef(df))
- df = xref.fetch(df);
-
- dict = xref.fetchIfRef(isRef(df) ? df : df[0]);
+ dict = isArray(df) ? xref.fetchIfRef(df[0]) : df;
type = dict.get('Subtype');
assertWellFormed(isName(type), 'invalid font Subtype');
}
var maxCharIndex = composite ? 0xFFFF : 0xFF;
- var descriptor = xref.fetchIfRef(dict.get('FontDescriptor'));
+ var descriptor = dict.get('FontDescriptor');
if (!descriptor) {
if (type.name == 'Type3') {
// FontDescriptor is only required for Type3 fonts when the document
// to ignore this rule when a variant of a standart font is used.
// TODO Fill the width array depending on which of the base font this is
// a variant.
- var firstChar = xref.fetchIfRef(dict.get('FirstChar')) || 0;
- var lastChar = xref.fetchIfRef(dict.get('LastChar')) || maxCharIndex;
- var fontName = xref.fetchIfRef(descriptor.get('FontName'));
+ var firstChar = dict.get('FirstChar') || 0;
+ var lastChar = dict.get('LastChar') || maxCharIndex;
+ var fontName = descriptor.get('FontName');
// Some bad pdf's have a string as the font name.
if (isString(fontName))
fontName = new Name(fontName);
var fontFile = descriptor.get('FontFile', 'FontFile2', 'FontFile3');
if (fontFile) {
- fontFile = xref.fetchIfRef(fontFile);
if (fontFile.dict) {
var subtype = fontFile.dict.get('Subtype');
if (subtype)
subtype = subtype.name;
var length1 = fontFile.dict.get('Length1');
- if (!isInt(length1))
- length1 = xref.fetchIfRef(length1);
var length2 = fontFile.dict.get('Length2');
- if (!isInt(length2))
- length2 = xref.fetchIfRef(length2);
}
}
if (type.name === 'Type3') {
properties.coded = true;
- var charProcs = xref.fetchIfRef(dict.get('CharProcs'));
- var fontResources = xref.fetchIfRef(dict.get('Resources')) || resources;
+ var charProcs = dict.get('CharProcs').getAll();
+ var fontResources = dict.get('Resources') || resources;
properties.resources = fontResources;
properties.charProcOperatorList = {};
- for (var key in charProcs.map) {
- var glyphStream = xref.fetchIfRef(charProcs.map[key]);
+ for (var key in charProcs) {
+ var glyphStream = charProcs[key];
properties.charProcOperatorList[key] =
this.getOperatorList(glyphStream, fontResources, dependency);
}
if (inputSize != 1)
error('Bad domain for stiched function');
- var fnRefs = xref.fetchIfRef(dict.get('Functions'));
+ var fnRefs = dict.get('Functions');
var fns = [];
for (var i = 0, ii = fnRefs.length; i < ii; ++i)
fns.push(PDFFunction.getIR(xref, xref.fetchIfRef(fnRefs[i])));
- var bounds = xref.fetchIfRef(dict.get('Bounds'));
- var encode = xref.fetchIfRef(dict.get('Encode'));
+ var bounds = dict.get('Bounds');
+ var encode = dict.get('Encode');
return [CONSTRUCT_STICHED, domain, bounds, encode, fns];
},
}
}
- var mask = xref.fetchIfRef(dict.get('Mask'));
+ var mask = dict.get('Mask');
if (mask) {
TODO('masked images');
handleImageData(handler, xref, res, image, imageDataPromise);
- var smask = xref.fetchIfRef(image.dict.get('SMask'));
+ var smask = image.dict.get('SMask');
if (smask)
handleImageData(handler, xref, res, smask, smaskPromise);
else
})();
var Dict = (function DictClosure() {
- function Dict() {
+ // xref is optional
+ function Dict(xref) {
+ // Map should only be used internally, use functions below to access.
this.map = Object.create(null);
+ this.xref = xref;
}
Dict.prototype = {
+ // automatically dereferences Ref objects
get: function dictGet(key1, key2, key3) {
var value;
+ var xref = this.xref;
if (typeof (value = this.map[key1]) != 'undefined' || key1 in this.map ||
typeof key2 == 'undefined') {
- return value;
+ return xref ? this.xref.fetchIfRef(value) : value;
}
if (typeof (value = this.map[key2]) != 'undefined' || key2 in this.map ||
typeof key3 == 'undefined') {
- return value;
+ return xref ? this.xref.fetchIfRef(value) : value;
}
-
- return this.map[key3] || null;
+ value = this.map[key3] || null;
+ return xref ? this.xref.fetchIfRef(value) : value;
+ },
+ // no dereferencing
+ getRaw: function dictGetRaw(key) {
+ return this.map[key];
+ },
+ // creates new map and dereferences all Refs
+ getAll: function dictGetAll() {
+ var all = {};
+ for (var key in this.map)
+ all[key] = this.get(key);
+ return all;
},
set: function dictSet(key, value) {
forEach: function dictForEach(callback) {
for (var key in this.map) {
- callback(key, this.map[key]);
+ callback(key, this.get(key));
}
}
};
Catalog.prototype = {
get metadata() {
- var ref = this.catDict.get('Metadata');
- var stream = this.xref.fetchIfRef(ref);
+ var stream = this.catDict.get('Metadata');
var metadata;
if (stream && isDict(stream.dict)) {
var type = stream.dict.get('Type');
},
get toplevelPagesDict() {
var pagesObj = this.catDict.get('Pages');
- assertWellFormed(isRef(pagesObj), 'invalid top-level pages reference');
- var xrefObj = this.xref.fetch(pagesObj);
- assertWellFormed(isDict(xrefObj), 'invalid top-level pages dictionary');
+ assertWellFormed(isDict(pagesObj), 'invalid top-level pages dictionary');
// shadow the prototype getter
- return shadow(this, 'toplevelPagesDict', xrefObj);
+ return shadow(this, 'toplevelPagesDict', pagesObj);
},
get documentOutline() {
var xref = this.xref;
- var obj = xref.fetchIfRef(this.catDict.get('Outlines'));
+ var obj = this.catDict.get('Outlines');
var root = { items: [] };
if (isDict(obj)) {
+ var ref = obj.getRaw('First');
obj = obj.get('First');
var processed = new RefSet();
- if (isRef(obj)) {
+ if (isRef(ref)) {
var queue = [{obj: obj, parent: root}];
// to avoid recursion keeping track of the items
// in the processed dictionary
- processed.put(obj);
+ processed.put(ref);
while (queue.length > 0) {
var i = queue.shift();
- var outlineDict = xref.fetch(i.obj);
+ var outlineDict = i.obj;
if (outlineDict === null)
continue;
if (!outlineDict.has('Title'))
error('Invalid outline item');
var dest = outlineDict.get('A');
if (dest)
- dest = xref.fetchIfRef(dest).get('D');
+ dest = dest.get('D');
else if (outlineDict.has('Dest')) {
- dest = outlineDict.get('Dest');
+ dest = outlineDict.getRaw('Dest');
if (isName(dest))
dest = dest.name;
}
- var title = xref.fetchIfRef(outlineDict.get('Title'));
+ var title = outlineDict.get('Title');
var outlineItem = {
dest: dest,
title: stringToPDFString(title),
items: []
};
i.parent.items.push(outlineItem);
- obj = outlineDict.get('First');
- if (isRef(obj) && !processed.has(obj)) {
+ ref = outlineDict.getRaw('First');
+ if (isRef(ref) && !processed.has(ref)) {
+ obj = outlineDict.get('First');
queue.push({obj: obj, parent: outlineItem});
- processed.put(obj);
+ processed.put(ref);
}
- obj = outlineDict.get('Next');
- if (isRef(obj) && !processed.has(obj)) {
+ ref = outlineDict.getRaw('Next');
+ if (isRef(ref) && !processed.has(ref)) {
+ obj = outlineDict.get('Next');
queue.push({obj: obj, parent: i.parent});
- processed.put(obj);
+ processed.put(ref);
}
}
}
for (var i = 0, ii = kids.length; i < ii; ++i) {
var kid = kids[i];
assertWellFormed(isRef(kid),
- 'page dictionary kid is not a reference');
+ '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, kid));
},
get destinations() {
function fetchDestination(xref, ref) {
- var dest = xref.fetchIfRef(ref);
+ var dest = ref;
return isDict(dest) ? dest.get('D') : dest;
}
var dests = {}, nameTreeRef, nameDictionaryRef;
var obj = this.catDict.get('Names');
if (obj)
- nameTreeRef = xref.fetchIfRef(obj).get('Dests');
+ nameTreeRef = obj.getRaw('Dests');
else if (this.catDict.has('Dests'))
nameDictionaryRef = this.catDict.get('Dests');
if (nameDictionaryRef) {
// reading simple destination dictionary
- obj = xref.fetchIfRef(nameDictionaryRef);
+ obj = nameDictionaryRef;
obj.forEach(function catalogForEach(key, value) {
if (!value) return;
dests[key] = fetchDestination(xref, value);
this.entries = [];
this.xrefstms = {};
var trailerDict = this.readXRef(startXRef);
+ trailerDict.xref = this;
this.trailer = trailerDict;
// prepare the XRef cache
this.cache = [];
var encrypt = trailerDict.get('Encrypt');
if (encrypt) {
var fileId = trailerDict.get('ID');
- this.encrypt = new CipherTransformFactory(this.fetch(encrypt),
+ this.encrypt = new CipherTransformFactory(encrypt,
fileId[0] /*, password */);
}
// get the root dictionary (catalog) object
- if (!isRef(this.root = trailerDict.get('Root')))
+ if (!(this.root = trailerDict.get('Root')))
error('Invalid root reference');
}
var dict;
for (var i = 0, ii = trailers.length; i < ii; ++i) {
stream.pos = trailers[i];
- var parser = new Parser(new Lexer(stream), true);
+ var parser = new Parser(new Lexer(stream), true, null);
var obj = parser.getObj();
if (!isCmd(obj, 'trailer'))
continue;
stream.pos = startXRef;
try {
- var parser = new Parser(new Lexer(stream), true);
+ var parser = new Parser(new Lexer(stream), true, null);
var obj = parser.getObj();
var dict;
return this.fetch(obj);
},
fetch: function xRefFetch(ref, suppressEncryption) {
+ assertWellFormed(isRef(ref), 'ref object is not a reference');
var num = ref.num;
if (num in this.cache)
return this.cache[num];
if (!isInt(first) || !isInt(n)) {
error('invalid first and n parameters for ObjStm stream');
}
- parser = new Parser(new Lexer(stream), false);
+ parser = new Parser(new Lexer(stream), false, this);
var i, entries = [], nums = [];
// read the object numbers to populate cache
for (i = 0; i < n; ++i) {
return e;
},
getCatalogObj: function xRefGetCatalogObj() {
- return this.fetch(this.root);
+ return this.root;
}
};
}
if (isCmd(this.buf1, '<<')) { // dictionary or stream
this.shift();
- var dict = new Dict();
+ var dict = new Dict(this.xref);
while (!isCmd(this.buf1, '>>') && !isEOF(this.buf1)) {
if (!isName(this.buf1))
error('Dictionary key must be a name object');
var Linearization = (function LinearizationClosure() {
function Linearization(stream) {
- this.parser = new Parser(new Lexer(stream), false);
+ this.parser = new Parser(new Lexer(stream), false, null);
var obj1 = this.parser.getObj();
var obj2 = this.parser.getObj();
var obj3 = this.parser.getObj();
this.extendEnd = extendEnd;
var fnObj = dict.get('Function');
- fnObj = xref.fetchIfRef(fnObj);
if (isArray(fnObj))
error('No support for array of functions');
if (!isPDFFunction(fnObj))