Skip to content

Commit 066cbcf

Browse files
authored
Merge pull request #13277 from Snuffleupagus/adjustToUnicode-cff
For CFF fonts without proper `ToUnicode`/`Encoding` data, utilize the "charset"/"Encoding"-data from the font file to improve text-selection (issue 13260)
2 parents 5231d92 + 7fab73e commit 066cbcf

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

src/core/evaluator.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2853,7 +2853,7 @@ class PartialEvaluator {
28532853
}
28542854

28552855
if (baseEncodingName) {
2856-
properties.defaultEncoding = getEncoding(baseEncodingName).slice();
2856+
properties.defaultEncoding = getEncoding(baseEncodingName);
28572857
} else {
28582858
var isSymbolicFont = !!(properties.flags & FontFlags.Symbolic);
28592859
var isNonsymbolicFont = !!(properties.flags & FontFlags.Nonsymbolic);

src/core/fonts.js

+33-3
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,6 @@ function adjustToUnicode(properties, builtInEncoding) {
200200
if (properties.hasIncludedToUnicodeMap) {
201201
return; // The font dictionary has a `ToUnicode` entry.
202202
}
203-
if (properties.hasEncoding) {
204-
return; // The font dictionary has an `Encoding` entry.
205-
}
206203
if (builtInEncoding === properties.defaultEncoding) {
207204
return; // No point in trying to adjust `toUnicode` if the encodings match.
208205
}
@@ -212,6 +209,12 @@ function adjustToUnicode(properties, builtInEncoding) {
212209
var toUnicode = [],
213210
glyphsUnicodeMap = getGlyphsUnicode();
214211
for (var charCode in builtInEncoding) {
212+
if (
213+
properties.hasEncoding &&
214+
properties.differences[charCode] !== undefined
215+
) {
216+
continue; // The font dictionary has an `Encoding`/`Differences` entry.
217+
}
215218
var glyphName = builtInEncoding[charCode];
216219
var unicode = getUnicodeForGlyph(glyphName, glyphsUnicodeMap);
217220
if (unicode !== -1) {
@@ -4012,6 +4015,7 @@ var CFFFont = (function CFFFontClosure() {
40124015
// anyway and hope the font loaded.
40134016
this.data = file;
40144017
}
4018+
this._createBuiltInEncoding();
40154019
}
40164020

40174021
CFFFont.prototype = {
@@ -4057,6 +4061,32 @@ var CFFFont = (function CFFFontClosure() {
40574061
hasGlyphId: function CFFFont_hasGlyphID(id) {
40584062
return this.cff.hasGlyphId(id);
40594063
},
4064+
4065+
/**
4066+
* @private
4067+
*/
4068+
_createBuiltInEncoding() {
4069+
const { charset, encoding } = this.cff;
4070+
if (!charset || !encoding) {
4071+
return;
4072+
}
4073+
const charsets = charset.charset,
4074+
encodings = encoding.encoding;
4075+
const map = [];
4076+
4077+
for (const charCode in encodings) {
4078+
const glyphId = encodings[charCode];
4079+
if (glyphId >= 0) {
4080+
const glyphName = charsets[glyphId];
4081+
if (glyphName) {
4082+
map[charCode] = glyphName;
4083+
}
4084+
}
4085+
}
4086+
if (map.length > 0) {
4087+
this.properties.builtInEncoding = map;
4088+
}
4089+
},
40604090
};
40614091

40624092
return CFFFont;

0 commit comments

Comments
 (0)