Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Dict handle all the fetching of Refs. #1488

Merged
merged 3 commits into from
Apr 5, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/colorspace.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ var ColorSpace = (function ColorSpaceClosure() {

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)
Expand Down Expand Up @@ -152,7 +152,7 @@ var ColorSpace = (function ColorSpaceClosure() {
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 + '"');
Expand Down
37 changes: 19 additions & 18 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ var Page = (function PageClosure() {

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);
Expand Down Expand Up @@ -199,8 +199,8 @@ var Page = (function PageClosure() {
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;
Expand Down Expand Up @@ -242,8 +242,8 @@ var Page = (function PageClosure() {
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;
Expand Down Expand Up @@ -307,7 +307,7 @@ var Page = (function PageClosure() {
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;
Expand All @@ -330,7 +330,7 @@ var Page = (function PageClosure() {
}
}

var annotations = xref.fetchIfRef(this.annotations) || [];
var annotations = this.annotations || [];
var i, n = annotations.length;
var items = [];
for (i = 0; i < n; ++i) {
Expand All @@ -353,7 +353,7 @@ var Page = (function PageClosure() {
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':
Expand Down Expand Up @@ -386,21 +386,22 @@ var Page = (function PageClosure() {
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);
Expand Down Expand Up @@ -490,7 +491,7 @@ var PDFDocModel = (function PDFDocModelClosure() {
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) {
Expand Down Expand Up @@ -597,15 +598,15 @@ var PDFDocModel = (function PDFDocModelClosure() {
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);
},
getFingerprint: function pdfDocGetFingerprint() {
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);
});
Expand Down
67 changes: 28 additions & 39 deletions src/evaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,14 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
}
}

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,
Expand Down Expand Up @@ -250,10 +248,10 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
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() {
Expand Down Expand Up @@ -285,7 +283,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
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');
Expand All @@ -303,7 +301,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
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*/);
Expand All @@ -318,7 +316,6 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
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');
Expand Down Expand Up @@ -369,11 +366,11 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
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');

Expand All @@ -385,12 +382,12 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
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 = [];
Expand Down Expand Up @@ -469,7 +466,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {

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'),
Expand All @@ -478,7 +475,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
};
}

var cidToGidMap = xref.fetchIfRef(dict.get('CIDToGIDMap'));
var cidToGidMap = dict.get('CIDToGIDMap');
if (isStream(cidToGidMap))
properties.cidToGidMap = this.readCidToGidMap(cidToGidMap);
}
Expand All @@ -489,7 +486,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
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)
Expand Down Expand Up @@ -523,7 +520,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {

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-';
Expand Down Expand Up @@ -666,9 +663,9 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
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++) {
Expand All @@ -689,7 +686,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
}
} 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++)
Expand Down Expand Up @@ -742,18 +739,15 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
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');
composite = true;
}
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
Expand Down Expand Up @@ -802,29 +796,24 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
// 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);
assertWellFormed(isName(fontName), 'invalid font name');

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);
}
}

Expand Down Expand Up @@ -853,12 +842,12 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {

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);
}
Expand Down
6 changes: 3 additions & 3 deletions src/function.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,13 +268,13 @@ var PDFFunction = (function PDFFunctionClosure() {
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];
},
Expand Down
4 changes: 2 additions & 2 deletions src/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ var PDFImage = (function PDFImageClosure() {
}
}

var mask = xref.fetchIfRef(dict.get('Mask'));
var mask = dict.get('Mask');

if (mask) {
TODO('masked images');
Expand All @@ -120,7 +120,7 @@ var PDFImage = (function PDFImageClosure() {

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
Expand Down
Loading