Merge pull request #17553 from Snuffleupagus/async-handleSetFont

Add more `async` code when loading fonts in the `PartialEvaluator`
This commit is contained in:
Tim van der Meij 2024-01-21 20:00:29 +01:00 committed by GitHub
commit d549c2ef4c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1007,7 +1007,7 @@ class PartialEvaluator {
}); });
} }
handleSetFont( async handleSetFont(
resources, resources,
fontArgs, fontArgs,
fontRef, fontRef,
@ -1019,41 +1019,33 @@ class PartialEvaluator {
) { ) {
const fontName = fontArgs?.[0] instanceof Name ? fontArgs[0].name : null; const fontName = fontArgs?.[0] instanceof Name ? fontArgs[0].name : null;
return this.loadFont( let translated = await this.loadFont(
fontName, fontName,
fontRef, fontRef,
resources, resources,
fallbackFontDict, fallbackFontDict,
cssFontInfo cssFontInfo
) );
.then(translated => {
if (!translated.font.isType3Font) { if (translated.font.isType3Font) {
return translated; try {
} await translated.loadType3Data(this, resources, task);
return translated
.loadType3Data(this, resources, task)
.then(function () {
// Add the dependencies to the parent operatorList so they are // Add the dependencies to the parent operatorList so they are
// resolved before Type3 operatorLists are executed synchronously. // resolved before Type3 operatorLists are executed synchronously.
operatorList.addDependencies(translated.type3Dependencies); operatorList.addDependencies(translated.type3Dependencies);
} catch (reason) {
return translated; translated = new TranslatedFont({
})
.catch(
reason =>
new TranslatedFont({
loadedName: "g_font_error", loadedName: "g_font_error",
font: new ErrorFont(`Type3 font load error: ${reason}`), font: new ErrorFont(`Type3 font load error: ${reason}`),
dict: translated.font, dict: translated.font,
evaluatorOptions: this.options, evaluatorOptions: this.options,
}) });
); }
}) }
.then(translated => {
state.font = translated.font; state.font = translated.font;
translated.send(this.handler); translated.send(this.handler);
return translated.loadedName; return translated.loadedName;
});
} }
handleText(chars, state) { handleText(chars, state) {
@ -1130,9 +1122,8 @@ class PartialEvaluator {
case "Font": case "Font":
isSimpleGState = false; isSimpleGState = false;
// eslint-disable-next-line arrow-body-style promise = promise.then(() =>
promise = promise.then(() => { this.handleSetFont(
return this.handleSetFont(
resources, resources,
null, null,
value[0], value[0],
@ -1142,8 +1133,8 @@ class PartialEvaluator {
).then(function (loadedName) { ).then(function (loadedName) {
operatorList.addDependency(loadedName); operatorList.addDependency(loadedName);
gStateObj.push([key, [loadedName, value[1]]]); gStateObj.push([key, [loadedName, value[1]]]);
}); })
}); );
break; break;
case "BM": case "BM":
gStateObj.push([key, normalizeBlendMode(value)]); gStateObj.push([key, normalizeBlendMode(value)]);
@ -1156,17 +1147,16 @@ class PartialEvaluator {
if (value instanceof Dict) { if (value instanceof Dict) {
isSimpleGState = false; isSimpleGState = false;
// eslint-disable-next-line arrow-body-style promise = promise.then(() =>
promise = promise.then(() => { this.handleSMask(
return this.handleSMask(
value, value,
resources, resources,
operatorList, operatorList,
task, task,
stateManager, stateManager,
localColorSpaceCache localColorSpaceCache
)
); );
});
gStateObj.push([key, true]); gStateObj.push([key, true]);
} else { } else {
warn("Unsupported SMask type"); warn("Unsupported SMask type");
@ -2560,29 +2550,21 @@ class PartialEvaluator {
}; };
} }
function handleSetFont(fontName, fontRef) { async function handleSetFont(fontName, fontRef) {
return self const translated = await self.loadFont(fontName, fontRef, resources);
.loadFont(fontName, fontRef, resources)
.then(function (translated) { if (translated.font.isType3Font) {
if (!translated.font.isType3Font) { try {
return translated; await translated.loadType3Data(self, resources, task);
} } catch {
return translated
.loadType3Data(self, resources, task)
.catch(function () {
// Ignore Type3-parsing errors, since we only use `loadType3Data` // Ignore Type3-parsing errors, since we only use `loadType3Data`
// here to ensure that we'll always obtain a useful /FontBBox. // here to ensure that we'll always obtain a useful /FontBBox.
}) }
.then(function () { }
return translated;
});
})
.then(function (translated) {
textState.loadedName = translated.loadedName; textState.loadedName = translated.loadedName;
textState.font = translated.font; textState.font = translated.font;
textState.fontMatrix = textState.fontMatrix = translated.font.fontMatrix || FONT_IDENTITY_MATRIX;
translated.font.fontMatrix || FONT_IDENTITY_MATRIX;
});
} }
function applyInverseRotation(x, y, matrix) { function applyInverseRotation(x, y, matrix) {
@ -4185,7 +4167,6 @@ class PartialEvaluator {
cssFontInfo, cssFontInfo,
}) { }) {
const isType3Font = type === "Type3"; const isType3Font = type === "Type3";
let properties;
if (!descriptor) { if (!descriptor) {
if (isType3Font) { if (isType3Font) {
@ -4216,7 +4197,7 @@ class PartialEvaluator {
? FontFlags.Symbolic ? FontFlags.Symbolic
: FontFlags.Nonsymbolic); : FontFlags.Nonsymbolic);
properties = { const properties = {
type, type,
name: baseFontName, name: baseFontName,
loadedName: baseDict.loadedName, loadedName: baseDict.loadedName,
@ -4250,8 +4231,12 @@ class PartialEvaluator {
standardFontName standardFontName
); );
} }
return this.extractDataStructures(dict, dict, properties).then(
newProperties => { const newProperties = await this.extractDataStructures(
dict,
dict,
properties
);
if (widths) { if (widths) {
const glyphWidths = []; const glyphWidths = [];
let j = firstChar; let j = firstChar;
@ -4267,8 +4252,6 @@ class PartialEvaluator {
} }
return new Font(baseFontName, file, newProperties); return new Font(baseFontName, file, newProperties);
} }
);
}
} }
// According to the spec if 'FontDescriptor' is declared, 'FirstChar', // According to the spec if 'FontDescriptor' is declared, 'FirstChar',
@ -4371,7 +4354,7 @@ class PartialEvaluator {
} }
} }
properties = { const properties = {
type, type,
name: fontName.name, name: fontName.name,
subtype, subtype,
@ -4414,14 +4397,15 @@ class PartialEvaluator {
properties.vertical = properties.cMap.vertical; properties.vertical = properties.cMap.vertical;
} }
return this.extractDataStructures(dict, baseDict, properties).then( const newProperties = await this.extractDataStructures(
newProperties => { 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);
} }
);
}
static buildFontPaths(font, glyphs, handler, evaluatorOptions) { static buildFontPaths(font, glyphs, handler, evaluatorOptions) {
function buildPath(fontChar) { function buildPath(fontChar) {