Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

For CFF fonts without proper ToUnicode/Encoding data, utilize the "charset"/"Encoding"-data from the font file to improve text-selection (issue 13260) #13277

Merged
merged 1 commit into from
Apr 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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