Merge pull request #11312 from Snuffleupagus/async-getPageDict
Convert `Catalog.getPageDict` to an `async` method
This commit is contained in:
commit
b1440a11c1
@ -14,10 +14,9 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import {
|
import {
|
||||||
assert, bytesToString, createPromiseCapability, createValidAbsoluteUrl,
|
assert, bytesToString, createValidAbsoluteUrl, FormatError, info,
|
||||||
FormatError, info, InvalidPDFException, isBool, isNum, isString,
|
InvalidPDFException, isBool, isNum, isString, PermissionFlag, shadow,
|
||||||
PermissionFlag, shadow, stringToPDFString, stringToUTF8String, unreachable,
|
stringToPDFString, stringToUTF8String, unreachable, warn
|
||||||
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,
|
||||||
@ -680,26 +679,25 @@ class Catalog {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
getPageDict(pageIndex) {
|
async 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 (isRef(currentNode)) {
|
if (currentNode instanceof Ref) {
|
||||||
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);
|
||||||
|
|
||||||
xref.fetchAsync(currentNode).then(function(obj) {
|
if ((obj instanceof Dict) && (isName(obj.get('Type'), 'Page') ||
|
||||||
if (isDict(obj, 'Page') || (isDict(obj) && !obj.has('Kids'))) {
|
(!obj.has('Type') && !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
|
||||||
@ -707,24 +705,19 @@ class Catalog {
|
|||||||
if (currentNode && !pageKidsCountCache.has(currentNode)) {
|
if (currentNode && !pageKidsCountCache.has(currentNode)) {
|
||||||
pageKidsCountCache.put(currentNode, 1);
|
pageKidsCountCache.put(currentNode, 1);
|
||||||
}
|
}
|
||||||
capability.resolve([obj, currentNode]);
|
return [obj, currentNode];
|
||||||
} else {
|
|
||||||
currentPageIndex++;
|
|
||||||
next();
|
|
||||||
}
|
}
|
||||||
return;
|
currentPageIndex++;
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
nodesToVisit.push(obj);
|
nodesToVisit.push(obj);
|
||||||
next();
|
continue;
|
||||||
}, capability.reject);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Must be a child page dictionary.
|
// Must be a child page dictionary.
|
||||||
if (!isDict(currentNode)) {
|
if (!(currentNode instanceof Dict)) {
|
||||||
capability.reject(new FormatError(
|
throw 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');
|
||||||
@ -750,16 +743,12 @@ 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) {
|
||||||
capability.resolve([currentNode, null]);
|
return [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
|
||||||
@ -769,10 +758,7 @@ class Catalog {
|
|||||||
nodesToVisit.push(kids[last]);
|
nodesToVisit.push(kids[last]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
capability.reject(new Error(`Page index ${pageIndex} not found.`));
|
throw new Error(`Page index ${pageIndex} not found.`);
|
||||||
}
|
|
||||||
next();
|
|
||||||
return capability.promise;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getPageIndex(pageRef) {
|
getPageIndex(pageRef) {
|
||||||
|
Loading…
Reference in New Issue
Block a user