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 * @returns {Promise} A Promise that is resolved with a
* {ToUnicodeMap|IdentityToUnicodeMap} object. * {ToUnicodeMap|IdentityToUnicodeMap} object.
*/ */
buildToUnicode(properties) { async buildToUnicode(properties) {
properties.hasIncludedToUnicodeMap = properties.hasIncludedToUnicodeMap =
!!properties.toUnicode && properties.toUnicode.length > 0; !!properties.toUnicode && properties.toUnicode.length > 0;
@ -3297,8 +3297,7 @@ class PartialEvaluator {
properties.fallbackToUnicode = properties.fallbackToUnicode =
this._buildSimpleFontToUnicode(properties); this._buildSimpleFontToUnicode(properties);
} }
return properties.toUnicode;
return Promise.resolve(properties.toUnicode);
} }
// According to the spec if the font is a simple font we should only map // 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 // in pratice it seems better to always try to create a toUnicode map
// based of the default encoding. // based of the default encoding.
if (!properties.composite /* is simple font */) { 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 // 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 // b) Obtain the registry and ordering of the character collection used
// by the fonts CMap (for example, Adobe and Japan1) from its // by the fonts CMap (for example, Adobe and Japan1) from its
// CIDSystemInfo dictionary. // CIDSystemInfo dictionary.
const registry = properties.cidSystemInfo.registry; const { registry, ordering } = properties.cidSystemInfo;
const ordering = properties.cidSystemInfo.ordering;
// c) Construct a second CMap name by concatenating the registry and // c) Construct a second CMap name by concatenating the registry and
// ordering obtained in step (b) in the format registryorderingUCS2 // ordering obtained in step (b) in the format registryorderingUCS2
// (for example, AdobeJapan1UCS2). // (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 // d) Obtain the CMap with the name constructed in step (c) (available
// from the ASN Web site; see the Bibliography). // from the ASN Web site; see the Bibliography).
return CMapFactory.create({ const ucs2CMap = await CMapFactory.create({
encoding: ucs2CMapName, encoding: ucs2CMapName,
fetchBuiltInCMap: this._fetchBuiltInCMapBound, fetchBuiltInCMap: this._fetchBuiltInCMapBound,
useCMap: null, 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. // The viewer's choice, just use an identity map.
return Promise.resolve( return new IdentityToUnicodeMap(properties.firstChar, properties.lastChar);
new IdentityToUnicodeMap(properties.firstChar, properties.lastChar)
);
} }
readToUnicode(cmapObj) { readToUnicode(cmapObj) {