A couple of small scripting/XFA-related tweaks in the worker-code
- Use `PDFManager.ensureDoc`, rather than `PDFManager.ensure`, in a couple of spots in the code. If there exists a short-hand format, we should obviously use it whenever possible. - Fix a unit-test helper, to account for the previous changes. (Also, converts a function to be `async` instead.) - Add one more exists-check in `PDFDocument.loadXfaFonts`, which I missed to suggest in PR 13146, to prevent any possible errors if the method is ever called in a situation where it shouldn't be. Also, print a warning if the actual font-loading fails since that could help future debugging. (Finally, reduce overall indentation in the loop.) - Slightly unrelated, but make a small tweak of a comment in `src/core/fonts.js` to reduce possible confusion.
This commit is contained in:
parent
ac3fa1e3d7
commit
f560fe6875
@ -865,7 +865,9 @@ class PDFDocument {
|
|||||||
|
|
||||||
async loadXfaFonts(handler, task) {
|
async loadXfaFonts(handler, task) {
|
||||||
const acroForm = await this.pdfManager.ensureCatalog("acroForm");
|
const acroForm = await this.pdfManager.ensureCatalog("acroForm");
|
||||||
|
if (!acroForm) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
const resources = await acroForm.getAsync("DR");
|
const resources = await acroForm.getAsync("DR");
|
||||||
if (!(resources instanceof Dict)) {
|
if (!(resources instanceof Dict)) {
|
||||||
return;
|
return;
|
||||||
@ -902,28 +904,34 @@ class PDFDocument {
|
|||||||
|
|
||||||
for (const [fontName, font] of fonts) {
|
for (const [fontName, font] of fonts) {
|
||||||
const descriptor = font.get("FontDescriptor");
|
const descriptor = font.get("FontDescriptor");
|
||||||
if (descriptor instanceof Dict) {
|
if (!(descriptor instanceof Dict)) {
|
||||||
const fontFamily = descriptor.get("FontFamily");
|
continue;
|
||||||
const fontWeight = descriptor.get("FontWeight");
|
|
||||||
const italicAngle = descriptor.get("ItalicAngle");
|
|
||||||
const cssFontInfo = { fontFamily, fontWeight, italicAngle };
|
|
||||||
|
|
||||||
if (!validateCSSFont(cssFontInfo)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const promise = partialEvaluator.handleSetFont(
|
|
||||||
resources,
|
|
||||||
[Name.get(fontName), 1],
|
|
||||||
/* fontRef = */ null,
|
|
||||||
operatorList,
|
|
||||||
task,
|
|
||||||
initialState,
|
|
||||||
/* fallbackFontDict = */ null,
|
|
||||||
/* cssFontInfo = */ cssFontInfo
|
|
||||||
);
|
|
||||||
promises.push(promise.catch(() => {}));
|
|
||||||
}
|
}
|
||||||
|
const fontFamily = descriptor.get("FontFamily");
|
||||||
|
const fontWeight = descriptor.get("FontWeight");
|
||||||
|
const italicAngle = descriptor.get("ItalicAngle");
|
||||||
|
const cssFontInfo = { fontFamily, fontWeight, italicAngle };
|
||||||
|
|
||||||
|
if (!validateCSSFont(cssFontInfo)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
promises.push(
|
||||||
|
partialEvaluator
|
||||||
|
.handleSetFont(
|
||||||
|
resources,
|
||||||
|
[Name.get(fontName), 1],
|
||||||
|
/* fontRef = */ null,
|
||||||
|
operatorList,
|
||||||
|
task,
|
||||||
|
initialState,
|
||||||
|
/* fallbackFontDict = */ null,
|
||||||
|
/* cssFontInfo = */ cssFontInfo
|
||||||
|
)
|
||||||
|
.catch(function (reason) {
|
||||||
|
warn(`loadXfaFonts: "${reason}".`);
|
||||||
|
return null;
|
||||||
|
})
|
||||||
|
);
|
||||||
}
|
}
|
||||||
await Promise.all(promises);
|
await Promise.all(promises);
|
||||||
}
|
}
|
||||||
@ -1257,7 +1265,7 @@ class PDFDocument {
|
|||||||
}
|
}
|
||||||
|
|
||||||
get hasJSActions() {
|
get hasJSActions() {
|
||||||
const promise = this.pdfManager.ensure(this, "_parseHasJSActions");
|
const promise = this.pdfManager.ensureDoc("_parseHasJSActions");
|
||||||
return shadow(this, "hasJSActions", promise);
|
return shadow(this, "hasJSActions", promise);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1267,7 +1275,7 @@ class PDFDocument {
|
|||||||
async _parseHasJSActions() {
|
async _parseHasJSActions() {
|
||||||
const [catalogJsActions, fieldObjects] = await Promise.all([
|
const [catalogJsActions, fieldObjects] = await Promise.all([
|
||||||
this.pdfManager.ensureCatalog("jsActions"),
|
this.pdfManager.ensureCatalog("jsActions"),
|
||||||
this.pdfManager.ensure(this, "fieldObjects"),
|
this.pdfManager.ensureDoc("fieldObjects"),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if (catalogJsActions) {
|
if (catalogJsActions) {
|
||||||
|
@ -2966,7 +2966,7 @@ var Font = (function FontClosure() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// When `cssFontInfo` is set, the font is used to render text in the HTML
|
// When `cssFontInfo` is set, the font is used to render text in the HTML
|
||||||
// view (e.g. with Xfa) so nothing must be moved in the private area use.
|
// view (e.g. with Xfa) so nothing must be moved in the private use area.
|
||||||
if (!properties.cssFontInfo) {
|
if (!properties.cssFontInfo) {
|
||||||
// Converting glyphs and ids into font's cmap table
|
// Converting glyphs and ids into font's cmap table
|
||||||
var newMapping = adjustMapping(
|
var newMapping = adjustMapping(
|
||||||
|
@ -195,7 +195,7 @@ class WorkerMessageHandler {
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
if (isPureXfa) {
|
if (isPureXfa) {
|
||||||
const task = new WorkerTask("Load fonts for Xfa");
|
const task = new WorkerTask("loadXfaFonts");
|
||||||
startWorkerTask(task);
|
startWorkerTask(task);
|
||||||
await pdfManager
|
await pdfManager
|
||||||
.loadXfaFonts(handler, task)
|
.loadXfaFonts(handler, task)
|
||||||
|
@ -53,18 +53,18 @@ describe("document", function () {
|
|||||||
get docId() {
|
get docId() {
|
||||||
return "d0";
|
return "d0";
|
||||||
},
|
},
|
||||||
|
ensureDoc(prop, args) {
|
||||||
|
return pdfManager.ensure(pdfDocument, prop, args);
|
||||||
|
},
|
||||||
ensureCatalog(prop, args) {
|
ensureCatalog(prop, args) {
|
||||||
return pdfManager.ensure(catalog, prop, args);
|
return pdfManager.ensure(catalog, prop, args);
|
||||||
},
|
},
|
||||||
ensure(obj, prop, args) {
|
async ensure(obj, prop, args) {
|
||||||
return new Promise(function (resolve) {
|
const value = obj[prop];
|
||||||
const value = obj[prop];
|
if (typeof value === "function") {
|
||||||
if (typeof value === "function") {
|
return value.apply(obj, args);
|
||||||
resolve(value.apply(obj, args));
|
}
|
||||||
} else {
|
return value;
|
||||||
resolve(value);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
const pdfDocument = new PDFDocument(pdfManager, stream);
|
const pdfDocument = new PDFDocument(pdfManager, stream);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user