Merge pull request #8536 from Snuffleupagus/refactor-ObjectLoader

Refactor `ObjectLoader` to use `Dict`s correctly, rather than abusing their internal properties
This commit is contained in:
Tim van der Meij 2017-06-18 22:17:40 +02:00 committed by GitHub
commit db52e4fb73
5 changed files with 70 additions and 78 deletions

View File

@ -390,9 +390,8 @@ var Annotation = (function AnnotationClosure() {
if (!resources) { if (!resources) {
return; return;
} }
var objectLoader = new ObjectLoader(resources.map, let objectLoader = new ObjectLoader(resources, keys, resources.xref);
keys,
resources.xref);
return objectLoader.load().then(function() { return objectLoader.load().then(function() {
return resources; return resources;
}); });

View File

@ -186,9 +186,8 @@ var Page = (function PageClosure() {
this.resourcesPromise = this.pdfManager.ensure(this, 'resources'); this.resourcesPromise = this.pdfManager.ensure(this, 'resources');
} }
return this.resourcesPromise.then(() => { return this.resourcesPromise.then(() => {
var objectLoader = new ObjectLoader(this.resources.map, let objectLoader = new ObjectLoader(this.resources, keys, this.xref);
keys,
this.xref);
return objectLoader.load(); return objectLoader.load();
}); });
}, },

View File

@ -1610,39 +1610,34 @@ var FileSpec = (function FileSpecClosure() {
})(); })();
/** /**
* A helper for loading missing data in object graphs. It traverses the graph * A helper for loading missing data in `Dict` graphs. It traverses the graph
* depth first and queues up any objects that have missing data. Once it has * depth first and queues up any objects that have missing data. Once it has
* has traversed as many objects that are available it attempts to bundle the * has traversed as many objects that are available it attempts to bundle the
* missing data requests and then resume from the nodes that weren't ready. * missing data requests and then resume from the nodes that weren't ready.
* *
* NOTE: It provides protection from circular references by keeping track of * NOTE: It provides protection from circular references by keeping track of
* of loaded references. However, you must be careful not to load any graphs * loaded references. However, you must be careful not to load any graphs
* that have references to the catalog or other pages since that will cause the * that have references to the catalog or other pages since that will cause the
* entire PDF document object graph to be traversed. * entire PDF document object graph to be traversed.
*/ */
var ObjectLoader = (function() { let 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);
} }
function addChildren(node, nodesToVisit) { function addChildren(node, nodesToVisit) {
var value;
if (isDict(node) || isStream(node)) { if (isDict(node) || isStream(node)) {
var map; let dict = isDict(node) ? node : node.dict;
if (isDict(node)) { let dictKeys = dict.getKeys();
map = node.map; for (let i = 0, ii = dictKeys.length; i < ii; i++) {
} else { let rawValue = dict.getRaw(dictKeys[i]);
map = node.dict.map; if (mayHaveChildren(rawValue)) {
} nodesToVisit.push(rawValue);
for (var key in map) {
value = map[key];
if (mayHaveChildren(value)) {
nodesToVisit.push(value);
} }
} }
} else if (isArray(node)) { } else if (isArray(node)) {
for (var i = 0, ii = node.length; i < ii; i++) { for (let i = 0, ii = node.length; i < ii; i++) {
value = node[i]; let value = node[i];
if (mayHaveChildren(value)) { if (mayHaveChildren(value)) {
nodesToVisit.push(value); nodesToVisit.push(value);
} }
@ -1650,8 +1645,8 @@ var ObjectLoader = (function() {
} }
} }
function ObjectLoader(obj, keys, xref) { function ObjectLoader(dict, keys, xref) {
this.obj = obj; this.dict = dict;
this.keys = keys; this.keys = keys;
this.xref = xref; this.xref = xref;
this.refSet = null; this.refSet = null;
@ -1659,8 +1654,7 @@ var ObjectLoader = (function() {
} }
ObjectLoader.prototype = { ObjectLoader.prototype = {
load: function ObjectLoader_load() { load() {
var keys = this.keys;
this.capability = createPromiseCapability(); this.capability = createPromiseCapability();
// Don't walk the graph if all the data is already loaded. // Don't walk the graph if all the data is already loaded.
if (!(this.xref.stream instanceof ChunkedStream) || if (!(this.xref.stream instanceof ChunkedStream) ||
@ -1669,23 +1663,28 @@ var ObjectLoader = (function() {
return this.capability.promise; return this.capability.promise;
} }
let { keys, dict, } = this;
this.refSet = new RefSet(); this.refSet = new RefSet();
// Setup the initial nodes to visit. // Setup the initial nodes to visit.
var nodesToVisit = []; let nodesToVisit = [];
for (var i = 0; i < keys.length; i++) { for (let i = 0, ii = keys.length; i < ii; i++) {
nodesToVisit.push(this.obj[keys[i]]); let rawValue = dict.getRaw(keys[i]);
// Skip nodes that are guaranteed to be empty.
if (rawValue !== undefined) {
nodesToVisit.push(rawValue);
}
} }
this._walk(nodesToVisit); this._walk(nodesToVisit);
return this.capability.promise; return this.capability.promise;
}, },
_walk: function ObjectLoader_walk(nodesToVisit) { _walk(nodesToVisit) {
var nodesToRevisit = []; let nodesToRevisit = [];
var pendingRequests = []; let pendingRequests = [];
// DFS walk of the object graph. // DFS walk of the object graph.
while (nodesToVisit.length) { while (nodesToVisit.length) {
var currentNode = nodesToVisit.pop(); let currentNode = nodesToVisit.pop();
// Only references or chunked streams can cause missing data exceptions. // Only references or chunked streams can cause missing data exceptions.
if (isRef(currentNode)) { if (isRef(currentNode)) {
@ -1694,28 +1693,24 @@ var ObjectLoader = (function() {
continue; continue;
} }
try { try {
var ref = currentNode; this.refSet.put(currentNode);
this.refSet.put(ref);
currentNode = this.xref.fetch(currentNode); currentNode = this.xref.fetch(currentNode);
} catch (e) { } catch (ex) {
if (!(e instanceof MissingDataException)) { if (!(ex instanceof MissingDataException)) {
throw e; throw ex;
} }
nodesToRevisit.push(currentNode); nodesToRevisit.push(currentNode);
pendingRequests.push({ begin: e.begin, end: e.end, }); pendingRequests.push({ begin: ex.begin, end: ex.end, });
} }
} }
if (currentNode && currentNode.getBaseStreams) { if (currentNode && currentNode.getBaseStreams) {
var baseStreams = currentNode.getBaseStreams(); let baseStreams = currentNode.getBaseStreams();
var foundMissingData = false; let foundMissingData = false;
for (var i = 0; i < baseStreams.length; i++) { for (let i = 0, ii = baseStreams.length; i < ii; i++) {
var stream = baseStreams[i]; let stream = baseStreams[i];
if (stream.getMissingChunks && stream.getMissingChunks().length) { if (stream.getMissingChunks && stream.getMissingChunks().length) {
foundMissingData = true; foundMissingData = true;
pendingRequests.push({ pendingRequests.push({ begin: stream.start, end: stream.end, });
begin: stream.start,
end: stream.end,
});
} }
} }
if (foundMissingData) { if (foundMissingData) {
@ -1728,16 +1723,15 @@ var ObjectLoader = (function() {
if (pendingRequests.length) { if (pendingRequests.length) {
this.xref.stream.manager.requestRanges(pendingRequests).then(() => { this.xref.stream.manager.requestRanges(pendingRequests).then(() => {
nodesToVisit = nodesToRevisit; for (let i = 0, ii = nodesToRevisit.length; i < ii; i++) {
for (var i = 0; i < nodesToRevisit.length; i++) { let node = nodesToRevisit[i];
var node = nodesToRevisit[i]; // Remove any reference nodes from the current `RefSet` so they
// Remove any reference nodes from the currrent refset so they
// aren't skipped when we revist them. // aren't skipped when we revist them.
if (isRef(node)) { if (isRef(node)) {
this.refSet.remove(node); this.refSet.remove(node);
} }
} }
this._walk(nodesToVisit); this._walk(nodesToRevisit);
}, this.capability.reject); }, this.capability.reject);
return; return;
} }

View File

@ -60,7 +60,7 @@ var Dict = (function DictClosure() {
// xref is optional // xref is optional
function Dict(xref) { function Dict(xref) {
// Map should only be used internally, use functions below to access. // Map should only be used internally, use functions below to access.
this.map = Object.create(null); this._map = Object.create(null);
this.xref = xref; this.xref = xref;
this.objId = null; this.objId = null;
this.suppressEncryption = false; this.suppressEncryption = false;
@ -76,15 +76,15 @@ var Dict = (function DictClosure() {
get: function Dict_get(key1, key2, key3) { get: function Dict_get(key1, key2, key3) {
var value; var value;
var xref = this.xref, suppressEncryption = this.suppressEncryption; var xref = this.xref, suppressEncryption = this.suppressEncryption;
if (typeof (value = this.map[key1]) !== 'undefined' || key1 in this.map || if (typeof (value = this._map[key1]) !== 'undefined' ||
typeof key2 === 'undefined') { key1 in this._map || typeof key2 === 'undefined') {
return xref ? xref.fetchIfRef(value, suppressEncryption) : value; return xref ? xref.fetchIfRef(value, suppressEncryption) : value;
} }
if (typeof (value = this.map[key2]) !== 'undefined' || key2 in this.map || if (typeof (value = this._map[key2]) !== 'undefined' ||
typeof key3 === 'undefined') { key2 in this._map || typeof key3 === 'undefined') {
return xref ? xref.fetchIfRef(value, suppressEncryption) : value; return xref ? xref.fetchIfRef(value, suppressEncryption) : value;
} }
value = this.map[key3] || null; value = this._map[key3] || null;
return xref ? xref.fetchIfRef(value, suppressEncryption) : value; return xref ? xref.fetchIfRef(value, suppressEncryption) : value;
}, },
@ -92,21 +92,21 @@ var Dict = (function DictClosure() {
getAsync: function Dict_getAsync(key1, key2, key3) { getAsync: function Dict_getAsync(key1, key2, key3) {
var value; var value;
var xref = this.xref, suppressEncryption = this.suppressEncryption; var xref = this.xref, suppressEncryption = this.suppressEncryption;
if (typeof (value = this.map[key1]) !== 'undefined' || key1 in this.map || if (typeof (value = this._map[key1]) !== 'undefined' ||
typeof key2 === 'undefined') { key1 in this._map || typeof key2 === 'undefined') {
if (xref) { if (xref) {
return xref.fetchIfRefAsync(value, suppressEncryption); return xref.fetchIfRefAsync(value, suppressEncryption);
} }
return Promise.resolve(value); return Promise.resolve(value);
} }
if (typeof (value = this.map[key2]) !== 'undefined' || key2 in this.map || if (typeof (value = this._map[key2]) !== 'undefined' ||
typeof key3 === 'undefined') { key2 in this._map || typeof key3 === 'undefined') {
if (xref) { if (xref) {
return xref.fetchIfRefAsync(value, suppressEncryption); return xref.fetchIfRefAsync(value, suppressEncryption);
} }
return Promise.resolve(value); return Promise.resolve(value);
} }
value = this.map[key3] || null; value = this._map[key3] || null;
if (xref) { if (xref) {
return xref.fetchIfRefAsync(value, suppressEncryption); return xref.fetchIfRefAsync(value, suppressEncryption);
} }
@ -132,23 +132,23 @@ var Dict = (function DictClosure() {
// no dereferencing // no dereferencing
getRaw: function Dict_getRaw(key) { getRaw: function Dict_getRaw(key) {
return this.map[key]; return this._map[key];
}, },
getKeys: function Dict_getKeys() { getKeys: function Dict_getKeys() {
return Object.keys(this.map); return Object.keys(this._map);
}, },
set: function Dict_set(key, value) { set: function Dict_set(key, value) {
this.map[key] = value; this._map[key] = value;
}, },
has: function Dict_has(key) { has: function Dict_has(key) {
return key in this.map; return key in this._map;
}, },
forEach: function Dict_forEach(callback) { forEach: function Dict_forEach(callback) {
for (var key in this.map) { for (var key in this._map) {
callback(key, this.get(key)); callback(key, this.get(key));
} }
}, },
@ -156,19 +156,19 @@ var Dict = (function DictClosure() {
Dict.empty = new Dict(null); Dict.empty = new Dict(null);
Dict.merge = function Dict_merge(xref, dictArray) { Dict.merge = function(xref, dictArray) {
var mergedDict = new Dict(xref); let mergedDict = new Dict(xref);
for (var i = 0, ii = dictArray.length; i < ii; i++) { for (let i = 0, ii = dictArray.length; i < ii; i++) {
var dict = dictArray[i]; let dict = dictArray[i];
if (!isDict(dict)) { if (!isDict(dict)) {
continue; continue;
} }
for (var keyName in dict.map) { for (let keyName in dict._map) {
if (mergedDict.map[keyName]) { if (mergedDict._map[keyName] !== undefined) {
continue; continue;
} }
mergedDict.map[keyName] = dict.map[keyName]; mergedDict._map[keyName] = dict._map[keyName];
} }
} }
return mergedDict; return mergedDict;

View File

@ -361,9 +361,9 @@ var Type2Parser = function type2Parser(aFilePath) {
dump('privateData:' + privateDict); dump('privateData:' + privateDict);
parseAsToken(privateDict, CFFDictPrivateDataMap); parseAsToken(privateDict, CFFDictPrivateDataMap);
for (var p in font.map) { font.forEach(function(key, value) {
dump(p + '::' + font.get(p)); dump(key + '::' + value);
} });
// Read CharStrings Index // Read CharStrings Index
var charStringsOffset = font.get('CharStrings'); var charStringsOffset = font.get('CharStrings');