From 36fb5c1e2bb453469145af130bc8c16040bd9661 Mon Sep 17 00:00:00 2001 From: Samuel Yuan Date: Thu, 3 Nov 2022 16:19:23 -0700 Subject: [PATCH] Propagate the translated font name to TextContentItems. This allows font data for system fonts to be looked up in the PDFObjects. --- src/core/evaluator.js | 7 ++++--- test/unit/api_spec.js | 7 ++++++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/core/evaluator.js b/src/core/evaluator.js index 2f982b575..6104b85a7 100644 --- a/src/core/evaluator.js +++ b/src/core/evaluator.js @@ -2430,8 +2430,7 @@ class PartialEvaluator { if (textContentItem.initialized) { return textContentItem; } - const font = textState.font, - loadedName = font.loadedName; + const { font, loadedName } = textState; if (!seenStyles.has(loadedName)) { seenStyles.add(loadedName); @@ -2544,6 +2543,7 @@ class PartialEvaluator { }); }) .then(function (translated) { + textState.loadedName = translated.loadedName; textState.font = translated.font; textState.fontMatrix = translated.font.fontMatrix || FONT_IDENTITY_MATRIX; @@ -2877,7 +2877,7 @@ class PartialEvaluator { width: 0, height: 0, transform: getCurrentTextTransform(), - fontName: textState.font.loadedName, + fontName: textState.loadedName, hasEOL: true, }); } @@ -4607,6 +4607,7 @@ class TextState { this.ctm = new Float32Array(IDENTITY_MATRIX); this.fontName = null; this.fontSize = 0; + this.loadedName = null; this.font = null; this.fontMatrix = FONT_IDENTITY_MATRIX; this.textMatrix = IDENTITY_MATRIX.slice(); diff --git a/test/unit/api_spec.js b/test/unit/api_spec.js index 16c12be66..b1f1161c9 100644 --- a/test/unit/api_spec.js +++ b/test/unit/api_spec.js @@ -2244,7 +2244,7 @@ page 1 / 3`); const pdfPage = await pdfDoc.getPage(1); const { items, styles } = await pdfPage.getTextContent(); expect(items.length).toEqual(1); - // Font name will a random object id. + // Font name will be a random object id. const fontName = items[0].fontName; expect(Object.keys(styles)).toEqual([fontName]); @@ -2266,6 +2266,11 @@ page 1 / 3`); vertical: false, }); + // Wait for font data to be loaded so we can check that the font names + // match. + await pdfPage.getOperatorList(); + expect(pdfPage.commonObjs.has(fontName)).toEqual(true); + await loadingTask.destroy(); });