From d428db63c347200fa88d8b5e48fafb34e9c2cc7f Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Mon, 24 Feb 2025 00:17:23 +0100 Subject: [PATCH] Improve the "FontFallback" handling on the worker-thread Remove the `Catalog.prototype.fontFallback` method, and move its code into `PDFDocument.prototype.fontFallback` instead, to reduce the indirection a little bit. Pass the `evaluatorOptions` directly to the `TranslatedFont.prototype.fallback` method, since nothing else in the `TranslatedFont`-class needs it now. --- src/core/catalog.js | 11 ----------- src/core/document.js | 11 +++++++++-- src/core/evaluator.js | 11 +++-------- 3 files changed, 12 insertions(+), 21 deletions(-) diff --git a/src/core/catalog.js b/src/core/catalog.js index 1274f6f5c..c5e481efd 100644 --- a/src/core/catalog.js +++ b/src/core/catalog.js @@ -1169,17 +1169,6 @@ class Catalog { return shadow(this, "jsActions", actions); } - async fontFallback(id, handler) { - const translatedFonts = await Promise.all(this.fontCache); - - for (const translatedFont of translatedFonts) { - if (translatedFont.loadedName === id) { - translatedFont.fallback(handler); - return; - } - } - } - async cleanup(manuallyTriggered = false) { clearGlobalCaches(); this.globalImageCache.clear(/* onlyData = */ manuallyTriggered); diff --git a/src/core/document.js b/src/core/document.js index b3a550c4e..8b98a51c6 100644 --- a/src/core/document.js +++ b/src/core/document.js @@ -1745,8 +1745,15 @@ class PDFDocument { } } - fontFallback(id, handler) { - return this.catalog.fontFallback(id, handler); + async fontFallback(id, handler) { + const { catalog, pdfManager } = this; + + for (const translatedFont of await Promise.all(catalog.fontCache)) { + if (translatedFont.loadedName === id) { + translatedFont.fallback(handler, pdfManager.evaluatorOptions); + return; + } + } } async cleanup(manuallyTriggered = false) { diff --git a/src/core/evaluator.js b/src/core/evaluator.js index 4c63dc7d7..66024a7d3 100644 --- a/src/core/evaluator.js +++ b/src/core/evaluator.js @@ -1065,7 +1065,6 @@ class PartialEvaluator { loadedName: "g_font_error", font: new ErrorFont(`Type3 font load error: ${reason}`), dict: translated.font, - evaluatorOptions: this.options, }); } } @@ -1237,7 +1236,6 @@ class PartialEvaluator { loadedName: "g_font_error", font: new ErrorFont(`Font "${fontName}" is not available.`), dict: font, - evaluatorOptions: this.options, }); let fontRef; @@ -1364,7 +1362,6 @@ class PartialEvaluator { loadedName: font.loadedName, font: translatedFont, dict: font, - evaluatorOptions: this.options, }) ); }) @@ -1379,7 +1376,6 @@ class PartialEvaluator { reason instanceof Error ? reason.message : reason ), dict: font, - evaluatorOptions: this.options, }) ); }); @@ -4606,11 +4602,10 @@ class PartialEvaluator { } class TranslatedFont { - constructor({ loadedName, font, dict, evaluatorOptions }) { + constructor({ loadedName, font, dict }) { this.loadedName = loadedName; this.font = font; this.dict = dict; - this._evaluatorOptions = evaluatorOptions || DefaultPartialEvaluatorOptions; this.type3Loaded = null; this.type3Dependencies = font.isType3Font ? new Set() : null; this.sent = false; @@ -4629,7 +4624,7 @@ class TranslatedFont { ]); } - fallback(handler) { + fallback(handler, evaluatorOptions) { if (!this.font.data) { return; } @@ -4645,7 +4640,7 @@ class TranslatedFont { this.font, /* glyphs = */ this.font.glyphCacheValues, handler, - this._evaluatorOptions + evaluatorOptions ); }