Skip to content

Commit

Permalink
Merge pull request #13393 from Snuffleupagus/adjustToUnicode-hasInclu…
Browse files Browse the repository at this point in the history
…dedToUnicodeMap

Tweak `adjustToUnicode` to allow extending a built-in /ToUnicode map
  • Loading branch information
Snuffleupagus authored Jun 16, 2021
2 parents f9a0568 + 229a49b commit 7fa61c0
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 55 deletions.
61 changes: 27 additions & 34 deletions src/core/evaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -3178,10 +3178,10 @@ class PartialEvaluator {
}

/**
* @returns {ToUnicodeMap}
* @returns {Array}
* @private
*/
_buildSimpleFontToUnicode(properties, forceGlyphs = false) {
_simpleFontToUnicode(properties, forceGlyphs = false) {
assert(!properties.composite, "Must be a simple font.");

const toUnicode = [];
Expand Down Expand Up @@ -3242,7 +3242,7 @@ class PartialEvaluator {
Number.isNaN(code) &&
Number.isInteger(parseInt(codeStr, 16))
) {
return this._buildSimpleFontToUnicode(
return this._simpleFontToUnicode(
properties,
/* forceGlyphs */ true
);
Expand Down Expand Up @@ -3275,7 +3275,7 @@ class PartialEvaluator {
}
toUnicode[charcode] = String.fromCharCode(glyphsUnicodeMap[glyphName]);
}
return new ToUnicodeMap(toUnicode);
return toUnicode;
}

/**
Expand All @@ -3284,7 +3284,7 @@ class PartialEvaluator {
* @returns {Promise} A Promise that is resolved with a
* {ToUnicodeMap|IdentityToUnicodeMap} object.
*/
buildToUnicode(properties) {
async buildToUnicode(properties) {
properties.hasIncludedToUnicodeMap =
!!properties.toUnicode && properties.toUnicode.length > 0;

Expand All @@ -3294,11 +3294,9 @@ class PartialEvaluator {
// text-extraction. For simple fonts, containing encoding information,
// use a fallback ToUnicode map to improve this (fixes issue8229.pdf).
if (!properties.composite && properties.hasEncoding) {
properties.fallbackToUnicode =
this._buildSimpleFontToUnicode(properties);
properties.fallbackToUnicode = this._simpleFontToUnicode(properties);
}

return Promise.resolve(properties.toUnicode);
return properties.toUnicode;
}

// According to the spec if the font is a simple font we should only map
Expand All @@ -3307,7 +3305,7 @@ class PartialEvaluator {
// in pratice it seems better to always try to create a toUnicode map
// based of the default encoding.
if (!properties.composite /* is simple font */) {
return Promise.resolve(this._buildSimpleFontToUnicode(properties));
return new ToUnicodeMap(this._simpleFontToUnicode(properties));
}

// If the font is a composite font that uses one of the predefined CMaps
Expand All @@ -3330,42 +3328,37 @@ class PartialEvaluator {
// b) Obtain the registry and ordering of the character collection used
// by the font’s CMap (for example, Adobe and Japan1) from its
// CIDSystemInfo dictionary.
const registry = properties.cidSystemInfo.registry;
const ordering = properties.cidSystemInfo.ordering;
const { registry, ordering } = properties.cidSystemInfo;
// c) Construct a second CMap name by concatenating the registry and
// ordering obtained in step (b) in the format registry–ordering–UCS2
// (for example, Adobe–Japan1–UCS2).
const ucs2CMapName = Name.get(registry + "-" + ordering + "-UCS2");
const ucs2CMapName = Name.get(`${registry}-${ordering}-UCS2`);
// d) Obtain the CMap with the name constructed in step (c) (available
// from the ASN Web site; see the Bibliography).
return CMapFactory.create({
const ucs2CMap = await CMapFactory.create({
encoding: ucs2CMapName,
fetchBuiltInCMap: this._fetchBuiltInCMapBound,
useCMap: null,
}).then(function (ucs2CMap) {
const cMap = properties.cMap;
const toUnicode = [];
cMap.forEach(function (charcode, cid) {
if (cid > 0xffff) {
throw new FormatError("Max size of CID is 65,535");
}
// e) Map the CID obtained in step (a) according to the CMap
// obtained in step (d), producing a Unicode value.
const ucs2 = ucs2CMap.lookup(cid);
if (ucs2) {
toUnicode[charcode] = String.fromCharCode(
(ucs2.charCodeAt(0) << 8) + ucs2.charCodeAt(1)
);
}
});
return new ToUnicodeMap(toUnicode);
});
const toUnicode = [];
properties.cMap.forEach(function (charcode, cid) {
if (cid > 0xffff) {
throw new FormatError("Max size of CID is 65,535");
}
// e) Map the CID obtained in step (a) according to the CMap
// obtained in step (d), producing a Unicode value.
const ucs2 = ucs2CMap.lookup(cid);
if (ucs2) {
toUnicode[charcode] = String.fromCharCode(
(ucs2.charCodeAt(0) << 8) + ucs2.charCodeAt(1)
);
}
});
return new ToUnicodeMap(toUnicode);
}

// The viewer's choice, just use an identity map.
return Promise.resolve(
new IdentityToUnicodeMap(properties.firstChar, properties.lastChar)
);
return new IdentityToUnicodeMap(properties.firstChar, properties.lastChar);
}

readToUnicode(cmapObj) {
Expand Down
65 changes: 44 additions & 21 deletions src/core/fonts.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,6 @@ function adjustToUnicode(properties, builtInEncoding) {
if (properties.isInternalFont) {
return;
}
if (properties.hasIncludedToUnicodeMap) {
return; // The font dictionary has a `ToUnicode` entry.
}
if (builtInEncoding === properties.defaultEncoding) {
return; // No point in trying to adjust `toUnicode` if the encodings match.
}
Expand All @@ -147,19 +144,50 @@ function adjustToUnicode(properties, builtInEncoding) {
const toUnicode = [],
glyphsUnicodeMap = getGlyphsUnicode();
for (const charCode in builtInEncoding) {
if (
properties.hasEncoding &&
properties.differences[charCode] !== undefined
) {
continue; // The font dictionary has an `Encoding`/`Differences` entry.
if (properties.hasIncludedToUnicodeMap) {
if (properties.toUnicode.has(charCode)) {
continue; // The font dictionary has a `ToUnicode` entry.
}
} else {
if (
properties.hasEncoding &&
properties.differences[charCode] !== undefined
) {
continue; // The font dictionary has an `Encoding`/`Differences` entry.
}
}
const glyphName = builtInEncoding[charCode];
const unicode = getUnicodeForGlyph(glyphName, glyphsUnicodeMap);
if (unicode !== -1) {
toUnicode[charCode] = String.fromCharCode(unicode);
}
}
properties.toUnicode.amend(toUnicode);
if (toUnicode.length > 0) {
properties.toUnicode.amend(toUnicode);
}
}

/**
* NOTE: This function should only be called at the *end* of font-parsing,
* after e.g. `adjustToUnicode` has run, to prevent any issues.
*/
function amendFallbackToUnicode(properties) {
if (!properties.fallbackToUnicode) {
return;
}
if (properties.toUnicode instanceof IdentityToUnicodeMap) {
return;
}
const toUnicode = [];
for (const charCode in properties.fallbackToUnicode) {
if (properties.toUnicode.has(charCode)) {
continue; // The font dictionary has a `ToUnicode` entry.
}
toUnicode[charCode] = properties.fallbackToUnicode[charCode];
}
if (toUnicode.length > 0) {
properties.toUnicode.amend(toUnicode);
}
}

class Glyph {
Expand Down Expand Up @@ -849,8 +877,6 @@ class Font {
this.defaultEncoding = properties.defaultEncoding;

this.toUnicode = properties.toUnicode;
this.fallbackToUnicode = properties.fallbackToUnicode || new ToUnicodeMap();

this.toFontChar = [];

if (properties.type === "Type3") {
Expand Down Expand Up @@ -936,6 +962,7 @@ class Font {
return;
}

amendFallbackToUnicode(properties);
this.data = data;
this.fontType = getFontType(type, subtype, properties.isStandardFont);

Expand Down Expand Up @@ -1094,6 +1121,8 @@ class Font {
}
this.toFontChar = map;
}

amendFallbackToUnicode(properties);
this.loadedName = fontName.split("-")[0];
this.fontType = getFontType(type, subtype, properties.isStandardFont);
}
Expand Down Expand Up @@ -2545,12 +2574,9 @@ class Font {
const glyphsUnicodeMap = getGlyphsUnicode();
for (let charCode = 0; charCode < 256; charCode++) {
let glyphName;
if (this.differences && charCode in this.differences) {
if (this.differences[charCode] !== undefined) {
glyphName = this.differences[charCode];
} else if (
charCode in baseEncoding &&
baseEncoding[charCode] !== ""
) {
} else if (baseEncoding[charCode] !== "") {
glyphName = baseEncoding[charCode];
} else {
glyphName = StandardEncoding[charCode];
Expand Down Expand Up @@ -2955,15 +2981,12 @@ class Font {
width = isNum(width) ? width : this.defaultWidth;
const vmetric = this.vmetrics && this.vmetrics[widthCode];

let unicode =
this.toUnicode.get(charcode) ||
this.fallbackToUnicode.get(charcode) ||
charcode;
let unicode = this.toUnicode.get(charcode) || charcode;
if (typeof unicode === "number") {
unicode = String.fromCharCode(unicode);
}

let isInFont = charcode in this.toFontChar;
let isInFont = this.toFontChar[charcode] !== undefined;
// First try the toFontChar map, if it's not there then try falling
// back to the char code.
fontCharCode = this.toFontChar[charcode] || charcode;
Expand Down

0 comments on commit 7fa61c0

Please sign in to comment.