Convert PartialEvaluator.buildToUnicode to an async method

This removes the need to *manually* wrap all return values in a Promise.
This commit is contained in:
Jonas Jenwald 2021-05-17 15:40:23 +02:00
parent 3660aaac85
commit edc38de37a

View File

@ -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;
@ -3297,8 +3297,7 @@ class PartialEvaluator {
properties.fallbackToUnicode =
this._buildSimpleFontToUnicode(properties);
}
return Promise.resolve(properties.toUnicode);
return properties.toUnicode;
}
// According to the spec if the font is a simple font we should only map
@ -3307,7 +3306,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 this._buildSimpleFontToUnicode(properties);
}
// If the font is a composite font that uses one of the predefined CMaps
@ -3330,42 +3329,37 @@ class PartialEvaluator {
// b) Obtain the registry and ordering of the character collection used
// by the fonts 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 registryorderingUCS2
// (for example, AdobeJapan1UCS2).
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) {