Revert "Convert Catalog.getPageDict to an async method"

This commit is contained in:
Jonas Jenwald 2019-11-09 22:36:23 +01:00 committed by GitHub
parent b1440a11c1
commit 0233fc07b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -14,9 +14,10 @@
*/
import {
assert, bytesToString, createValidAbsoluteUrl, FormatError, info,
InvalidPDFException, isBool, isNum, isString, PermissionFlag, shadow,
stringToPDFString, stringToUTF8String, unreachable, warn
assert, bytesToString, createPromiseCapability, createValidAbsoluteUrl,
FormatError, info, InvalidPDFException, isBool, isNum, isString,
PermissionFlag, shadow, stringToPDFString, stringToUTF8String, unreachable,
warn
} from '../shared/util';
import {
clearPrimitiveCaches, Cmd, Dict, isCmd, isDict, isName, isRef, isRefsEqual,
@ -679,25 +680,26 @@ class Catalog {
});
}
async getPageDict(pageIndex) {
getPageDict(pageIndex) {
const capability = createPromiseCapability();
const nodesToVisit = [this.catDict.getRaw('Pages')];
const xref = this.xref, pageKidsCountCache = this.pageKidsCountCache;
let count, currentPageIndex = 0;
function next() {
while (nodesToVisit.length) {
const currentNode = nodesToVisit.pop();
if (currentNode instanceof Ref) {
if (isRef(currentNode)) {
count = pageKidsCountCache.get(currentNode);
// Skip nodes where the page can't be.
if (count > 0 && currentPageIndex + count < pageIndex) {
currentPageIndex += count;
continue;
}
const obj = await xref.fetchAsync(currentNode);
if ((obj instanceof Dict) && (isName(obj.get('Type'), 'Page') ||
(!obj.has('Type') && !obj.has('Kids')))) {
xref.fetchAsync(currentNode).then(function(obj) {
if (isDict(obj, 'Page') || (isDict(obj) && !obj.has('Kids'))) {
if (pageIndex === currentPageIndex) {
// Cache the Page reference, since it can *greatly* improve
// performance by reducing redundant lookups in long documents
@ -705,19 +707,24 @@ class Catalog {
if (currentNode && !pageKidsCountCache.has(currentNode)) {
pageKidsCountCache.put(currentNode, 1);
}
return [obj, currentNode];
}
capability.resolve([obj, currentNode]);
} else {
currentPageIndex++;
continue;
next();
}
return;
}
nodesToVisit.push(obj);
continue;
next();
}, capability.reject);
return;
}
// Must be a child page dictionary.
if (!(currentNode instanceof Dict)) {
throw new FormatError(
'Page dictionary kid reference points to wrong type of object.');
if (!isDict(currentNode)) {
capability.reject(new FormatError(
'Page dictionary kid reference points to wrong type of object.'));
return;
}
count = currentNode.get('Count');
@ -743,12 +750,16 @@ class Catalog {
if (isName(currentNode.get('Type'), 'Page') ||
(!currentNode.has('Type') && currentNode.has('Contents'))) {
if (currentPageIndex === pageIndex) {
return [currentNode, null];
capability.resolve([currentNode, null]);
return;
}
currentPageIndex++;
continue;
}
throw new FormatError('Page dictionary kids object is not an array.');
capability.reject(new FormatError(
'Page dictionary kids object is not an array.'));
return;
}
// Always check all `Kids` nodes, to avoid getting stuck in an empty
@ -758,7 +769,10 @@ class Catalog {
nodesToVisit.push(kids[last]);
}
}
throw new Error(`Page index ${pageIndex} not found.`);
capability.reject(new Error(`Page index ${pageIndex} not found.`));
}
next();
return capability.promise;
}
getPageIndex(pageRef) {