Convert the PartialEvaluator.readToUnicode method to be async

This commit is contained in:
Jonas Jenwald 2024-01-22 12:44:32 +01:00
parent f5c01188dc
commit f21a30dfb4

View File

@ -3765,70 +3765,70 @@ class PartialEvaluator {
return new IdentityToUnicodeMap(properties.firstChar, properties.lastChar); return new IdentityToUnicodeMap(properties.firstChar, properties.lastChar);
} }
readToUnicode(cmapObj) { async readToUnicode(cmapObj) {
if (!cmapObj) { if (!cmapObj) {
return Promise.resolve(null); return null;
} }
if (cmapObj instanceof Name) { if (cmapObj instanceof Name) {
return CMapFactory.create({ const cmap = await CMapFactory.create({
encoding: cmapObj, encoding: cmapObj,
fetchBuiltInCMap: this._fetchBuiltInCMapBound, fetchBuiltInCMap: this._fetchBuiltInCMapBound,
useCMap: null, useCMap: null,
}).then(function (cmap) { });
if (cmap instanceof IdentityCMap) {
return new IdentityToUnicodeMap(0, 0xffff);
}
return new ToUnicodeMap(cmap.getMap());
}
if (cmapObj instanceof BaseStream) {
try {
const cmap = await CMapFactory.create({
encoding: cmapObj,
fetchBuiltInCMap: this._fetchBuiltInCMapBound,
useCMap: null,
});
if (cmap instanceof IdentityCMap) { if (cmap instanceof IdentityCMap) {
return new IdentityToUnicodeMap(0, 0xffff); return new IdentityToUnicodeMap(0, 0xffff);
} }
return new ToUnicodeMap(cmap.getMap()); const map = new Array(cmap.length);
}); // Convert UTF-16BE
} else if (cmapObj instanceof BaseStream) { // NOTE: cmap can be a sparse array, so use forEach instead of
return CMapFactory.create({ // `for(;;)` to iterate over all keys.
encoding: cmapObj, cmap.forEach(function (charCode, token) {
fetchBuiltInCMap: this._fetchBuiltInCMapBound, // Some cmaps contain *only* CID characters (fixes issue9367.pdf).
useCMap: null, if (typeof token === "number") {
}).then( map[charCode] = String.fromCodePoint(token);
function (cmap) { return;
if (cmap instanceof IdentityCMap) {
return new IdentityToUnicodeMap(0, 0xffff);
} }
const map = new Array(cmap.length); const str = [];
// Convert UTF-16BE for (let k = 0; k < token.length; k += 2) {
// NOTE: cmap can be a sparse array, so use forEach instead of const w1 = (token.charCodeAt(k) << 8) | token.charCodeAt(k + 1);
// `for(;;)` to iterate over all keys. if ((w1 & 0xf800) !== 0xd800) {
cmap.forEach(function (charCode, token) { // w1 < 0xD800 || w1 > 0xDFFF
// Some cmaps contain *only* CID characters (fixes issue9367.pdf). str.push(w1);
if (typeof token === "number") { continue;
map[charCode] = String.fromCodePoint(token);
return;
} }
const str = []; k += 2;
for (let k = 0; k < token.length; k += 2) { const w2 = (token.charCodeAt(k) << 8) | token.charCodeAt(k + 1);
const w1 = (token.charCodeAt(k) << 8) | token.charCodeAt(k + 1); str.push(((w1 & 0x3ff) << 10) + (w2 & 0x3ff) + 0x10000);
if ((w1 & 0xf800) !== 0xd800) {
// w1 < 0xD800 || w1 > 0xDFFF
str.push(w1);
continue;
}
k += 2;
const w2 = (token.charCodeAt(k) << 8) | token.charCodeAt(k + 1);
str.push(((w1 & 0x3ff) << 10) + (w2 & 0x3ff) + 0x10000);
}
map[charCode] = String.fromCodePoint(...str);
});
return new ToUnicodeMap(map);
},
reason => {
if (reason instanceof AbortException) {
return null;
} }
if (this.options.ignoreErrors) { map[charCode] = String.fromCodePoint(...str);
warn(`readToUnicode - ignoring ToUnicode data: "${reason}".`); });
return null; return new ToUnicodeMap(map);
} } catch (reason) {
throw reason; if (reason instanceof AbortException) {
return null;
} }
); if (this.options.ignoreErrors) {
warn(`readToUnicode - ignoring ToUnicode data: "${reason}".`);
return null;
}
throw reason;
}
} }
return Promise.resolve(null); return null;
} }
readCidToGidMap(glyphsData, toUnicode) { readCidToGidMap(glyphsData, toUnicode) {