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.
This commit is contained in:
Jonas Jenwald 2025-05-04 12:35:00 +02:00
parent 604153957a
commit d9548b1c18
3 changed files with 20 additions and 25 deletions

View File

@ -1237,7 +1237,7 @@ class PDFDocument {
return this.xfaFactory ? this.xfaFactory.getPages() : null; return this.xfaFactory ? this.xfaFactory.getPages() : null;
} }
async loadXfaImages() { async #loadXfaImages() {
const xfaImages = await this.pdfManager.ensureCatalog("xfaImages"); const xfaImages = await this.pdfManager.ensureCatalog("xfaImages");
if (!xfaImages) { if (!xfaImages) {
return; return;
@ -1245,7 +1245,7 @@ class PDFDocument {
this.xfaFactory.setImages(xfaImages); this.xfaFactory.setImages(xfaImages);
} }
async loadXfaFonts(handler, task) { async #loadXfaFonts(handler, task) {
const acroForm = await this.pdfManager.ensureCatalog("acroForm"); const acroForm = await this.pdfManager.ensureCatalog("acroForm");
if (!acroForm) { if (!acroForm) {
return; return;
@ -1264,18 +1264,19 @@ class PDFDocument {
const options = Object.assign( const options = Object.assign(
Object.create(null), Object.create(null),
this.pdfManager.evaluatorOptions this.pdfManager.evaluatorOptions,
{ useSystemFonts: false }
); );
options.useSystemFonts = false; const { builtInCMapCache, fontCache, standardFontDataCache } = this.catalog;
const partialEvaluator = new PartialEvaluator({ const partialEvaluator = new PartialEvaluator({
xref: this.xref, xref: this.xref,
handler, handler,
pageIndex: -1, pageIndex: -1,
idFactory: this._globalIdFactory, idFactory: this._globalIdFactory,
fontCache: this.catalog.fontCache, fontCache,
builtInCMapCache: this.catalog.builtInCMapCache, builtInCMapCache,
standardFontDataCache: this.catalog.standardFontDataCache, standardFontDataCache,
options, options,
}); });
const operatorList = new OperatorList(); const operatorList = new OperatorList();
@ -1383,6 +1384,15 @@ class PDFDocument {
this.xfaFactory.appendFonts(pdfFonts, reallyMissingFonts); this.xfaFactory.appendFonts(pdfFonts, reallyMissingFonts);
} }
loadXfaResources(handler, task) {
return Promise.all([
this.#loadXfaFonts(handler, task).catch(() => {
// Ignore errors, to allow the document to load.
}),
this.#loadXfaImages(),
]);
}
serializeXfaData(annotationStorage) { serializeXfaData(annotationStorage) {
return this.xfaFactory return this.xfaFactory
? this.xfaFactory.serializeData(annotationStorage) ? this.xfaFactory.serializeData(annotationStorage)

View File

@ -115,14 +115,6 @@ class BasePdfManager {
return this.pdfDocument.fontFallback(id, handler); return this.pdfDocument.fontFallback(id, handler);
} }
loadXfaFonts(handler, task) {
return this.pdfDocument.loadXfaFonts(handler, task);
}
loadXfaImages() {
return this.pdfDocument.loadXfaImages();
}
cleanup(manuallyTriggered = false) { cleanup(manuallyTriggered = false) {
return this.pdfDocument.cleanup(manuallyTriggered); return this.pdfDocument.cleanup(manuallyTriggered);
} }

View File

@ -170,18 +170,11 @@ class WorkerMessageHandler {
const isPureXfa = await pdfManager.ensureDoc("isPureXfa"); const isPureXfa = await pdfManager.ensureDoc("isPureXfa");
if (isPureXfa) { if (isPureXfa) {
const task = new WorkerTask("loadXfaFonts"); const task = new WorkerTask("loadXfaResources");
startWorkerTask(task); startWorkerTask(task);
await Promise.all([ await pdfManager.ensureDoc("loadXfaResources", [handler, task]);
pdfManager finishWorkerTask(task);
.loadXfaFonts(handler, task)
.catch(reason => {
// Ignore errors, to allow the document to load.
})
.then(() => finishWorkerTask(task)),
pdfManager.loadXfaImages(),
]);
} }
const [numPages, fingerprints] = await Promise.all([ const [numPages, fingerprints] = await Promise.all([