Move getPage, on the worker side, from Catalog and into PDFDocument instead

Addresses an existing TODO, and avoids having to pass in a `pageFactory` when creating `Catalog` instances.
This commit is contained in:
Jonas Jenwald 2018-07-25 16:53:47 +02:00
parent 51b0e60f9b
commit fbb25ff4e2
2 changed files with 22 additions and 33 deletions

View File

@ -355,6 +355,7 @@ var PDFDocument = (function PDFDocumentClosure() {
xref: this.xref,
isEvalSupported: evaluatorOptions.isEvalSupported,
});
this._pagePromises = [];
}
function find(stream, needle, limit, backwards) {
@ -520,21 +521,7 @@ var PDFDocument = (function PDFDocumentClosure() {
},
setup: function PDFDocument_setup(recoveryMode) {
this.xref.parse(recoveryMode);
var pageFactory = {
createPage: (pageIndex, dict, ref, fontCache, builtInCMapCache) => {
return new Page({
pdfManager: this.pdfManager,
xref: this.xref,
pageIndex,
pageDict: dict,
ref,
fontCache,
builtInCMapCache,
pdfFunctionFactory: this.pdfFunctionFactory,
});
},
};
this.catalog = new Catalog(this.pdfManager, this.xref, pageFactory);
this.catalog = new Catalog(this.pdfManager, this.xref);
},
get numPages() {
var linearization = this.linearization;
@ -599,8 +586,25 @@ var PDFDocument = (function PDFDocumentClosure() {
return shadow(this, 'fingerprint', fileID);
},
getPage: function PDFDocument_getPage(pageIndex) {
return this.catalog.getPage(pageIndex);
getPage(pageIndex) {
if (this._pagePromises[pageIndex] !== undefined) {
return this._pagePromises[pageIndex];
}
const catalog = this.catalog;
return this._pagePromises[pageIndex] =
catalog.getPageDict(pageIndex).then(([pageDict, ref]) => {
return new Page({
pdfManager: this.pdfManager,
xref: this.xref,
pageIndex,
pageDict,
ref,
fontCache: catalog.fontCache,
builtInCMapCache: catalog.builtInCMapCache,
pdfFunctionFactory: this.pdfFunctionFactory,
});
});
},
cleanup: function PDFDocument_cleanup() {

View File

@ -29,7 +29,7 @@ import { CipherTransformFactory } from './crypto';
import { ColorSpace } from './colorspace';
var Catalog = (function CatalogClosure() {
function Catalog(pdfManager, xref, pageFactory) {
function Catalog(pdfManager, xref) {
this.pdfManager = pdfManager;
this.xref = xref;
this.catDict = xref.getCatalogObj();
@ -40,9 +40,6 @@ var Catalog = (function CatalogClosure() {
this.fontCache = new RefSetCache();
this.builtInCMapCache = Object.create(null);
this.pageKidsCountCache = new RefSetCache();
// TODO refactor to move getPage() to the PDFDocument.
this.pageFactory = pageFactory;
this.pagePromises = [];
}
Catalog.prototype = {
@ -453,18 +450,6 @@ var Catalog = (function CatalogClosure() {
});
},
getPage: function Catalog_getPage(pageIndex) {
if (!(pageIndex in this.pagePromises)) {
this.pagePromises[pageIndex] = this.getPageDict(pageIndex).then(
([dict, ref]) => {
return this.pageFactory.createPage(pageIndex, dict, ref,
this.fontCache,
this.builtInCMapCache);
});
}
return this.pagePromises[pageIndex];
},
getPageDict: function Catalog_getPageDict(pageIndex) {
var capability = createPromiseCapability();
var nodesToVisit = [this.catDict.getRaw('Pages')];