From fc62eec9017721af1dae51336a1190223ba37008 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 21 Jan 2024 17:32:05 +0100 Subject: [PATCH 1/3] Convert the `handleSetFont` methods, in `src/core/evaluator.js`, to be async --- src/core/evaluator.js | 90 ++++++++++++++++++------------------------- 1 file changed, 37 insertions(+), 53 deletions(-) diff --git a/src/core/evaluator.js b/src/core/evaluator.js index 374c903d3..3673df23f 100644 --- a/src/core/evaluator.js +++ b/src/core/evaluator.js @@ -1007,7 +1007,7 @@ class PartialEvaluator { }); } - handleSetFont( + async handleSetFont( resources, fontArgs, fontRef, @@ -1019,41 +1019,33 @@ class PartialEvaluator { ) { const fontName = fontArgs?.[0] instanceof Name ? fontArgs[0].name : null; - return this.loadFont( + let translated = await this.loadFont( fontName, fontRef, resources, fallbackFontDict, cssFontInfo - ) - .then(translated => { - if (!translated.font.isType3Font) { - return translated; - } - return translated - .loadType3Data(this, resources, task) - .then(function () { - // Add the dependencies to the parent operatorList so they are - // resolved before Type3 operatorLists are executed synchronously. - operatorList.addDependencies(translated.type3Dependencies); + ); - return translated; - }) - .catch( - reason => - new TranslatedFont({ - loadedName: "g_font_error", - font: new ErrorFont(`Type3 font load error: ${reason}`), - dict: translated.font, - evaluatorOptions: this.options, - }) - ); - }) - .then(translated => { - state.font = translated.font; - translated.send(this.handler); - return translated.loadedName; - }); + if (translated.font.isType3Font) { + try { + await translated.loadType3Data(this, resources, task); + // Add the dependencies to the parent operatorList so they are + // resolved before Type3 operatorLists are executed synchronously. + operatorList.addDependencies(translated.type3Dependencies); + } catch (reason) { + translated = new TranslatedFont({ + loadedName: "g_font_error", + font: new ErrorFont(`Type3 font load error: ${reason}`), + dict: translated.font, + evaluatorOptions: this.options, + }); + } + } + + state.font = translated.font; + translated.send(this.handler); + return translated.loadedName; } handleText(chars, state) { @@ -2560,29 +2552,21 @@ class PartialEvaluator { }; } - function handleSetFont(fontName, fontRef) { - return self - .loadFont(fontName, fontRef, resources) - .then(function (translated) { - if (!translated.font.isType3Font) { - return translated; - } - return translated - .loadType3Data(self, resources, task) - .catch(function () { - // Ignore Type3-parsing errors, since we only use `loadType3Data` - // here to ensure that we'll always obtain a useful /FontBBox. - }) - .then(function () { - return translated; - }); - }) - .then(function (translated) { - textState.loadedName = translated.loadedName; - textState.font = translated.font; - textState.fontMatrix = - translated.font.fontMatrix || FONT_IDENTITY_MATRIX; - }); + async function handleSetFont(fontName, fontRef) { + const translated = await self.loadFont(fontName, fontRef, resources); + + if (translated.font.isType3Font) { + try { + await translated.loadType3Data(self, resources, task); + } catch { + // Ignore Type3-parsing errors, since we only use `loadType3Data` + // here to ensure that we'll always obtain a useful /FontBBox. + } + } + + textState.loadedName = translated.loadedName; + textState.font = translated.font; + textState.fontMatrix = translated.font.fontMatrix || FONT_IDENTITY_MATRIX; } function applyInverseRotation(x, y, matrix) { From d1bef8cb8660a5da1e250ed4888c3b8f7779bd4a Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 21 Jan 2024 17:36:50 +0100 Subject: [PATCH 2/3] Use `await` consistently in the `PartialEvaluator.translateFont` method --- src/core/evaluator.js | 54 ++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/src/core/evaluator.js b/src/core/evaluator.js index 3673df23f..e0e9ea85a 100644 --- a/src/core/evaluator.js +++ b/src/core/evaluator.js @@ -4169,7 +4169,6 @@ class PartialEvaluator { cssFontInfo, }) { const isType3Font = type === "Type3"; - let properties; if (!descriptor) { if (isType3Font) { @@ -4200,7 +4199,7 @@ class PartialEvaluator { ? FontFlags.Symbolic : FontFlags.Nonsymbolic); - properties = { + const properties = { type, name: baseFontName, loadedName: baseDict.loadedName, @@ -4234,24 +4233,26 @@ class PartialEvaluator { standardFontName ); } - return this.extractDataStructures(dict, dict, properties).then( - newProperties => { - if (widths) { - const glyphWidths = []; - let j = firstChar; - for (const width of widths) { - glyphWidths[j++] = this.xref.fetchIfRef(width); - } - newProperties.widths = glyphWidths; - } else { - newProperties.widths = this.buildCharCodeToWidth( - metrics.widths, - newProperties - ); - } - return new Font(baseFontName, file, newProperties); - } + + const newProperties = await this.extractDataStructures( + dict, + dict, + properties ); + if (widths) { + const glyphWidths = []; + let j = firstChar; + for (const width of widths) { + glyphWidths[j++] = this.xref.fetchIfRef(width); + } + newProperties.widths = glyphWidths; + } else { + newProperties.widths = this.buildCharCodeToWidth( + metrics.widths, + newProperties + ); + } + return new Font(baseFontName, file, newProperties); } } @@ -4355,7 +4356,7 @@ class PartialEvaluator { } } - properties = { + const properties = { type, name: fontName.name, subtype, @@ -4398,13 +4399,14 @@ class PartialEvaluator { properties.vertical = properties.cMap.vertical; } - return this.extractDataStructures(dict, baseDict, properties).then( - newProperties => { - this.extractWidths(dict, descriptor, newProperties); - - return new Font(fontName.name, fontFile, newProperties); - } + const newProperties = await this.extractDataStructures( + dict, + baseDict, + properties ); + this.extractWidths(dict, descriptor, newProperties); + + return new Font(fontName.name, fontFile, newProperties); } static buildFontPaths(font, glyphs, handler, evaluatorOptions) { From 3c2c0ecd8851bf59c49796251d50c3863d0cb2d2 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 21 Jan 2024 17:41:25 +0100 Subject: [PATCH 3/3] Use the ESLint `arrow-body-style` rule in more spots in `src/core/evaluator.js` --- src/core/evaluator.js | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/core/evaluator.js b/src/core/evaluator.js index e0e9ea85a..93f6da1f2 100644 --- a/src/core/evaluator.js +++ b/src/core/evaluator.js @@ -1122,9 +1122,8 @@ class PartialEvaluator { case "Font": isSimpleGState = false; - // eslint-disable-next-line arrow-body-style - promise = promise.then(() => { - return this.handleSetFont( + promise = promise.then(() => + this.handleSetFont( resources, null, value[0], @@ -1134,8 +1133,8 @@ class PartialEvaluator { ).then(function (loadedName) { operatorList.addDependency(loadedName); gStateObj.push([key, [loadedName, value[1]]]); - }); - }); + }) + ); break; case "BM": gStateObj.push([key, normalizeBlendMode(value)]); @@ -1148,17 +1147,16 @@ class PartialEvaluator { if (value instanceof Dict) { isSimpleGState = false; - // eslint-disable-next-line arrow-body-style - promise = promise.then(() => { - return this.handleSMask( + promise = promise.then(() => + this.handleSMask( value, resources, operatorList, task, stateManager, localColorSpaceCache - ); - }); + ) + ); gStateObj.push([key, true]); } else { warn("Unsupported SMask type");