From f560fe68756728ab33f9253e2f6da7c84b4b364f Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sat, 17 Apr 2021 10:13:42 +0200 Subject: [PATCH] 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. --- src/core/document.js | 56 ++++++++++++++++++++++---------------- src/core/fonts.js | 2 +- src/core/worker.js | 2 +- test/unit/document_spec.js | 18 ++++++------ 4 files changed, 43 insertions(+), 35 deletions(-) diff --git a/src/core/document.js b/src/core/document.js index bd38b47ca..606742842 100644 --- a/src/core/document.js +++ b/src/core/document.js @@ -865,7 +865,9 @@ class PDFDocument { async loadXfaFonts(handler, task) { const acroForm = await this.pdfManager.ensureCatalog("acroForm"); - + if (!acroForm) { + return; + } const resources = await acroForm.getAsync("DR"); if (!(resources instanceof Dict)) { return; @@ -902,28 +904,34 @@ class PDFDocument { for (const [fontName, font] of fonts) { const descriptor = font.get("FontDescriptor"); - if (descriptor instanceof Dict) { - const fontFamily = descriptor.get("FontFamily"); - 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(() => {})); + if (!(descriptor instanceof Dict)) { + continue; } + 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); } @@ -1257,7 +1265,7 @@ class PDFDocument { } get hasJSActions() { - const promise = this.pdfManager.ensure(this, "_parseHasJSActions"); + const promise = this.pdfManager.ensureDoc("_parseHasJSActions"); return shadow(this, "hasJSActions", promise); } @@ -1267,7 +1275,7 @@ class PDFDocument { async _parseHasJSActions() { const [catalogJsActions, fieldObjects] = await Promise.all([ this.pdfManager.ensureCatalog("jsActions"), - this.pdfManager.ensure(this, "fieldObjects"), + this.pdfManager.ensureDoc("fieldObjects"), ]); if (catalogJsActions) { diff --git a/src/core/fonts.js b/src/core/fonts.js index e38553c6c..e54f57558 100644 --- a/src/core/fonts.js +++ b/src/core/fonts.js @@ -2966,7 +2966,7 @@ var Font = (function FontClosure() { } // 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) { // Converting glyphs and ids into font's cmap table var newMapping = adjustMapping( diff --git a/src/core/worker.js b/src/core/worker.js index d75b79372..95032c204 100644 --- a/src/core/worker.js +++ b/src/core/worker.js @@ -195,7 +195,7 @@ class WorkerMessageHandler { ]); if (isPureXfa) { - const task = new WorkerTask("Load fonts for Xfa"); + const task = new WorkerTask("loadXfaFonts"); startWorkerTask(task); await pdfManager .loadXfaFonts(handler, task) diff --git a/test/unit/document_spec.js b/test/unit/document_spec.js index 91001e0e5..077f0d0d0 100644 --- a/test/unit/document_spec.js +++ b/test/unit/document_spec.js @@ -53,18 +53,18 @@ describe("document", function () { get docId() { return "d0"; }, + ensureDoc(prop, args) { + return pdfManager.ensure(pdfDocument, prop, args); + }, ensureCatalog(prop, args) { return pdfManager.ensure(catalog, prop, args); }, - ensure(obj, prop, args) { - return new Promise(function (resolve) { - const value = obj[prop]; - if (typeof value === "function") { - resolve(value.apply(obj, args)); - } else { - resolve(value); - } - }); + async ensure(obj, prop, args) { + const value = obj[prop]; + if (typeof value === "function") { + return value.apply(obj, args); + } + return value; }, }; const pdfDocument = new PDFDocument(pdfManager, stream);