Skip to content

Commit d9548b1

Browse files
committed
Slightly re-factor how we pre-load fonts and images in XFA documents
Rather than "manually" invoking the methods from the `src/core/worker.js` file we introduce a single `PDFDocument`-method that handles this for us, and make the current methods private. Since this code is only invoked at most *once* per document, and only for XFA documents, we can use `BasePdfManager.prototype.ensureDoc` directly rather than needing a stand-alone method.
1 parent 6041539 commit d9548b1

File tree

3 files changed

+20
-25
lines changed

3 files changed

+20
-25
lines changed

src/core/document.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,15 +1237,15 @@ class PDFDocument {
12371237
return this.xfaFactory ? this.xfaFactory.getPages() : null;
12381238
}
12391239

1240-
async loadXfaImages() {
1240+
async #loadXfaImages() {
12411241
const xfaImages = await this.pdfManager.ensureCatalog("xfaImages");
12421242
if (!xfaImages) {
12431243
return;
12441244
}
12451245
this.xfaFactory.setImages(xfaImages);
12461246
}
12471247

1248-
async loadXfaFonts(handler, task) {
1248+
async #loadXfaFonts(handler, task) {
12491249
const acroForm = await this.pdfManager.ensureCatalog("acroForm");
12501250
if (!acroForm) {
12511251
return;
@@ -1264,18 +1264,19 @@ class PDFDocument {
12641264

12651265
const options = Object.assign(
12661266
Object.create(null),
1267-
this.pdfManager.evaluatorOptions
1267+
this.pdfManager.evaluatorOptions,
1268+
{ useSystemFonts: false }
12681269
);
1269-
options.useSystemFonts = false;
1270+
const { builtInCMapCache, fontCache, standardFontDataCache } = this.catalog;
12701271

12711272
const partialEvaluator = new PartialEvaluator({
12721273
xref: this.xref,
12731274
handler,
12741275
pageIndex: -1,
12751276
idFactory: this._globalIdFactory,
1276-
fontCache: this.catalog.fontCache,
1277-
builtInCMapCache: this.catalog.builtInCMapCache,
1278-
standardFontDataCache: this.catalog.standardFontDataCache,
1277+
fontCache,
1278+
builtInCMapCache,
1279+
standardFontDataCache,
12791280
options,
12801281
});
12811282
const operatorList = new OperatorList();
@@ -1383,6 +1384,15 @@ class PDFDocument {
13831384
this.xfaFactory.appendFonts(pdfFonts, reallyMissingFonts);
13841385
}
13851386

1387+
loadXfaResources(handler, task) {
1388+
return Promise.all([
1389+
this.#loadXfaFonts(handler, task).catch(() => {
1390+
// Ignore errors, to allow the document to load.
1391+
}),
1392+
this.#loadXfaImages(),
1393+
]);
1394+
}
1395+
13861396
serializeXfaData(annotationStorage) {
13871397
return this.xfaFactory
13881398
? this.xfaFactory.serializeData(annotationStorage)

src/core/pdf_manager.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,6 @@ class BasePdfManager {
115115
return this.pdfDocument.fontFallback(id, handler);
116116
}
117117

118-
loadXfaFonts(handler, task) {
119-
return this.pdfDocument.loadXfaFonts(handler, task);
120-
}
121-
122-
loadXfaImages() {
123-
return this.pdfDocument.loadXfaImages();
124-
}
125-
126118
cleanup(manuallyTriggered = false) {
127119
return this.pdfDocument.cleanup(manuallyTriggered);
128120
}

src/core/worker.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -170,18 +170,11 @@ class WorkerMessageHandler {
170170

171171
const isPureXfa = await pdfManager.ensureDoc("isPureXfa");
172172
if (isPureXfa) {
173-
const task = new WorkerTask("loadXfaFonts");
173+
const task = new WorkerTask("loadXfaResources");
174174
startWorkerTask(task);
175175

176-
await Promise.all([
177-
pdfManager
178-
.loadXfaFonts(handler, task)
179-
.catch(reason => {
180-
// Ignore errors, to allow the document to load.
181-
})
182-
.then(() => finishWorkerTask(task)),
183-
pdfManager.loadXfaImages(),
184-
]);
176+
await pdfManager.ensureDoc("loadXfaResources", [handler, task]);
177+
finishWorkerTask(task);
185178
}
186179

187180
const [numPages, fingerprints] = await Promise.all([

0 commit comments

Comments
 (0)