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