Skip to content

Commit

Permalink
Merge pull request #13277 from Snuffleupagus/adjustToUnicode-cff
Browse files Browse the repository at this point in the history
For CFF fonts without proper `ToUnicode`/`Encoding` data, utilize the "charset"/"Encoding"-data from the font file to improve text-selection (issue 13260)
  • Loading branch information
brendandahl committed Apr 21, 2021
2 parents 5231d92 + 7fab73e commit 066cbcf
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/core/evaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
36 changes: 33 additions & 3 deletions src/core/fonts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}
Expand All @@ -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) {
Expand Down Expand Up @@ -4012,6 +4015,7 @@ var CFFFont = (function CFFFontClosure() {
// anyway and hope the font loaded.
this.data = file;
}
this._createBuiltInEncoding();
}

CFFFont.prototype = {
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 066cbcf

Please sign in to comment.