From 7fab73ed23b687187301c74475894fa13f253910 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Tue, 20 Apr 2021 17:12:19 +0200 Subject: [PATCH] For CFF fonts without proper `ToUnicode`/`Encoding` data, utilize the "charset"/"Encoding"-data from the font file to improve text-selection (issue 13260) This patch extends the approach, implemented in PR 7550, to also apply to CFF fonts. --- src/core/evaluator.js | 2 +- src/core/fonts.js | 36 +++++++++++++++++++++++++++++++++--- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/core/evaluator.js b/src/core/evaluator.js index 1029538c3..678998552 100644 --- a/src/core/evaluator.js +++ b/src/core/evaluator.js @@ -2853,7 +2853,7 @@ class PartialEvaluator { } if (baseEncodingName) { - properties.defaultEncoding = getEncoding(baseEncodingName).slice(); + properties.defaultEncoding = getEncoding(baseEncodingName); } else { var isSymbolicFont = !!(properties.flags & FontFlags.Symbolic); var isNonsymbolicFont = !!(properties.flags & FontFlags.Nonsymbolic); diff --git a/src/core/fonts.js b/src/core/fonts.js index e54f57558..edc8c9950 100644 --- a/src/core/fonts.js +++ b/src/core/fonts.js @@ -200,9 +200,6 @@ function adjustToUnicode(properties, builtInEncoding) { if (properties.hasIncludedToUnicodeMap) { return; // The font dictionary has a `ToUnicode` entry. } - if (properties.hasEncoding) { - return; // The font dictionary has an `Encoding` entry. - } if (builtInEncoding === properties.defaultEncoding) { return; // No point in trying to adjust `toUnicode` if the encodings match. } @@ -212,6 +209,12 @@ function adjustToUnicode(properties, builtInEncoding) { var toUnicode = [], glyphsUnicodeMap = getGlyphsUnicode(); for (var charCode in builtInEncoding) { + if ( + properties.hasEncoding && + properties.differences[charCode] !== undefined + ) { + continue; // The font dictionary has an `Encoding`/`Differences` entry. + } var glyphName = builtInEncoding[charCode]; var unicode = getUnicodeForGlyph(glyphName, glyphsUnicodeMap); if (unicode !== -1) { @@ -4012,6 +4015,7 @@ var CFFFont = (function CFFFontClosure() { // anyway and hope the font loaded. this.data = file; } + this._createBuiltInEncoding(); } CFFFont.prototype = { @@ -4057,6 +4061,32 @@ var CFFFont = (function CFFFontClosure() { hasGlyphId: function CFFFont_hasGlyphID(id) { return this.cff.hasGlyphId(id); }, + + /** + * @private + */ + _createBuiltInEncoding() { + const { charset, encoding } = this.cff; + if (!charset || !encoding) { + return; + } + const charsets = charset.charset, + encodings = encoding.encoding; + const map = []; + + for (const charCode in encodings) { + const glyphId = encodings[charCode]; + if (glyphId >= 0) { + const glyphName = charsets[glyphId]; + if (glyphName) { + map[charCode] = glyphName; + } + } + } + if (map.length > 0) { + this.properties.builtInEncoding = map; + } + }, }; return CFFFont;