Skip to content

Commit 06f4491

Browse files
Merge pull request #19889 from Snuffleupagus/loadXfaResources
Slightly re-factor how we pre-load fonts and images in XFA documents
2 parents 5ca57fb + d9548b1 commit 06f4491

File tree

4 files changed

+44
-61
lines changed

4 files changed

+44
-61
lines changed

src/core/document.js

Lines changed: 40 additions & 42 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();
@@ -1292,6 +1293,23 @@ class PDFDocument {
12921293
},
12931294
};
12941295

1296+
const parseFont = (fontName, fallbackFontDict, cssFontInfo) =>
1297+
partialEvaluator
1298+
.handleSetFont(
1299+
resources,
1300+
[Name.get(fontName), 1],
1301+
/* fontRef = */ null,
1302+
operatorList,
1303+
task,
1304+
initialState,
1305+
fallbackFontDict,
1306+
cssFontInfo
1307+
)
1308+
.catch(reason => {
1309+
warn(`loadXfaFonts: "${reason}".`);
1310+
return null;
1311+
});
1312+
12951313
const promises = [];
12961314
for (const [fontName, font] of fontRes) {
12971315
const descriptor = font.get("FontDescriptor");
@@ -1313,21 +1331,7 @@ class PDFDocument {
13131331
continue;
13141332
}
13151333
promises.push(
1316-
partialEvaluator
1317-
.handleSetFont(
1318-
resources,
1319-
[Name.get(fontName), 1],
1320-
/* fontRef = */ null,
1321-
operatorList,
1322-
task,
1323-
initialState,
1324-
/* fallbackFontDict = */ null,
1325-
/* cssFontInfo = */ cssFontInfo
1326-
)
1327-
.catch(function (reason) {
1328-
warn(`loadXfaFonts: "${reason}".`);
1329-
return null;
1330-
})
1334+
parseFont(fontName, /* fallbackFontDict = */ null, cssFontInfo)
13311335
);
13321336
}
13331337

@@ -1365,28 +1369,13 @@ class PDFDocument {
13651369
{ name: "BoldItalic", fontWeight: 700, italicAngle: 12 },
13661370
]) {
13671371
const name = `${missing}-${fontInfo.name}`;
1368-
const dict = getXfaFontDict(name);
13691372

13701373
promises.push(
1371-
partialEvaluator
1372-
.handleSetFont(
1373-
resources,
1374-
[Name.get(name), 1],
1375-
/* fontRef = */ null,
1376-
operatorList,
1377-
task,
1378-
initialState,
1379-
/* fallbackFontDict = */ dict,
1380-
/* cssFontInfo = */ {
1381-
fontFamily: missing,
1382-
fontWeight: fontInfo.fontWeight,
1383-
italicAngle: fontInfo.italicAngle,
1384-
}
1385-
)
1386-
.catch(function (reason) {
1387-
warn(`loadXfaFonts: "${reason}".`);
1388-
return null;
1389-
})
1374+
parseFont(name, getXfaFontDict(name), {
1375+
fontFamily: missing,
1376+
fontWeight: fontInfo.fontWeight,
1377+
italicAngle: fontInfo.italicAngle,
1378+
})
13901379
);
13911380
}
13921381
}
@@ -1395,6 +1384,15 @@ class PDFDocument {
13951384
this.xfaFactory.appendFonts(pdfFonts, reallyMissingFonts);
13961385
}
13971386

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+
13981396
serializeXfaData(annotationStorage) {
13991397
return this.xfaFactory
14001398
? 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([

src/core/xfa/factory.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class XFAFactory {
4343
}
4444

4545
isValid() {
46-
return this.root && this.form;
46+
return !!(this.root && this.form);
4747
}
4848

4949
/**

0 commit comments

Comments
 (0)