Merge pull request #17562 from Snuffleupagus/evaluator-more-async

Add more `async` code in the `PartialEvaluator`
This commit is contained in:
Jonas Jenwald 2024-01-23 10:35:27 +01:00 committed by GitHub
commit 8b24722113
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -526,23 +526,22 @@ class PartialEvaluator {
const args = group ? [matrix, null] : [matrix, bbox]; const args = group ? [matrix, null] : [matrix, bbox];
operatorList.addOp(OPS.paintFormXObjectBegin, args); operatorList.addOp(OPS.paintFormXObjectBegin, args);
return this.getOperatorList({ await this.getOperatorList({
stream: xobj, stream: xobj,
task, task,
resources: dict.get("Resources") || resources, resources: dict.get("Resources") || resources,
operatorList, operatorList,
initialState, initialState,
}).then(function () {
operatorList.addOp(OPS.paintFormXObjectEnd, []);
if (group) {
operatorList.addOp(OPS.endGroup, [groupOptions]);
}
if (optionalContent !== undefined) {
operatorList.addOp(OPS.endMarkedContent, []);
}
}); });
operatorList.addOp(OPS.paintFormXObjectEnd, []);
if (group) {
operatorList.addOp(OPS.endGroup, [groupOptions]);
}
if (optionalContent !== undefined) {
operatorList.addOp(OPS.endMarkedContent, []);
}
} }
_sendImgData(objId, imgData, cacheGlobally = false) { _sendImgData(objId, imgData, cacheGlobally = false) {
@ -1189,15 +1188,15 @@ class PartialEvaluator {
break; break;
} }
} }
return promise.then(function () { await promise;
if (gStateObj.length > 0) {
operatorList.addOp(OPS.setGState, [gStateObj]);
}
if (isSimpleGState) { if (gStateObj.length > 0) {
localGStateCache.set(cacheKey, gStateRef, gStateObj); operatorList.addOp(OPS.setGState, [gStateObj]);
} }
});
if (isSimpleGState) {
localGStateCache.set(cacheKey, gStateRef, gStateObj);
}
} }
loadFont( loadFont(
@ -3429,13 +3428,11 @@ class PartialEvaluator {
}); });
} }
extractDataStructures(dict, baseDict, properties) { async extractDataStructures(dict, properties) {
const xref = this.xref; const xref = this.xref;
let cidToGidBytes; let cidToGidBytes;
// 9.10.2 // 9.10.2
const toUnicodePromise = this.readToUnicode( const toUnicodePromise = this.readToUnicode(properties.toUnicode);
properties.toUnicode || dict.get("ToUnicode") || baseDict.get("ToUnicode")
);
if (properties.composite) { if (properties.composite) {
// CIDSystemInfo helps to match CID to glyphs // CIDSystemInfo helps to match CID to glyphs
@ -3555,21 +3552,19 @@ class PartialEvaluator {
properties.baseEncodingName = baseEncodingName; properties.baseEncodingName = baseEncodingName;
properties.hasEncoding = !!baseEncodingName || differences.length > 0; properties.hasEncoding = !!baseEncodingName || differences.length > 0;
properties.dict = dict; properties.dict = dict;
return toUnicodePromise
.then(readToUnicode => { properties.toUnicode = await toUnicodePromise;
properties.toUnicode = readToUnicode;
return this.buildToUnicode(properties); const builtToUnicode = await this.buildToUnicode(properties);
}) properties.toUnicode = builtToUnicode;
.then(builtToUnicode => {
properties.toUnicode = builtToUnicode; if (cidToGidBytes) {
if (cidToGidBytes) { properties.cidToGidMap = this.readCidToGidMap(
properties.cidToGidMap = this.readCidToGidMap( cidToGidBytes,
cidToGidBytes, builtToUnicode
builtToUnicode );
); }
} return properties;
return properties;
});
} }
/** /**
@ -3768,70 +3763,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) {
@ -4020,7 +4015,7 @@ class PartialEvaluator {
} }
let composite = false; let composite = false;
let hash, toUnicode; let hash;
if (type.name === "Type0") { if (type.name === "Type0") {
// If font is a composite // If font is a composite
// - get the descendant font // - get the descendant font
@ -4045,6 +4040,8 @@ class PartialEvaluator {
const firstChar = dict.get("FirstChar") || 0, const firstChar = dict.get("FirstChar") || 0,
lastChar = dict.get("LastChar") || (composite ? 0xffff : 0xff); lastChar = dict.get("LastChar") || (composite ? 0xffff : 0xff);
const descriptor = dict.get("FontDescriptor"); const descriptor = dict.get("FontDescriptor");
const toUnicode = dict.get("ToUnicode") || baseDict.get("ToUnicode");
if (descriptor) { if (descriptor) {
hash = new MurmurHash3_64(); hash = new MurmurHash3_64();
@ -4082,7 +4079,6 @@ class PartialEvaluator {
hash.update(`${firstChar}-${lastChar}`); // Fixes issue10665_reduced.pdf hash.update(`${firstChar}-${lastChar}`); // Fixes issue10665_reduced.pdf
toUnicode = dict.get("ToUnicode") || baseDict.get("ToUnicode");
if (toUnicode instanceof BaseStream) { if (toUnicode instanceof BaseStream) {
const stream = toUnicode.str || toUnicode; const stream = toUnicode.str || toUnicode;
const uint8array = stream.buffer const uint8array = stream.buffer
@ -4233,7 +4229,6 @@ class PartialEvaluator {
} }
const newProperties = await this.extractDataStructures( const newProperties = await this.extractDataStructures(
dict,
dict, dict,
properties properties
); );
@ -4397,11 +4392,7 @@ class PartialEvaluator {
properties.vertical = properties.cMap.vertical; properties.vertical = properties.cMap.vertical;
} }
const newProperties = await this.extractDataStructures( const newProperties = await this.extractDataStructures(dict, properties);
dict,
baseDict,
properties
);
this.extractWidths(dict, descriptor, newProperties); this.extractWidths(dict, descriptor, newProperties);
return new Font(fontName.name, fontFile, newProperties); return new Font(fontName.name, fontFile, newProperties);