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) {
return translated;
}
return translated
.loadType3Data(this, resources, task)
.then(function () {
// Add the dependencies to the parent operatorList so they are
// resolved before Type3 operatorLists are executed synchronously.
operatorList.addDependencies(translated.type3Dependencies);
return translated; if (translated.font.isType3Font) {
}) try {
.catch( await translated.loadType3Data(this, resources, task);
reason => // Add the dependencies to the parent operatorList so they are
new TranslatedFont({ // resolved before Type3 operatorLists are executed synchronously.
loadedName: "g_font_error", operatorList.addDependencies(translated.type3Dependencies);
font: new ErrorFont(`Type3 font load error: ${reason}`), } catch (reason) {
dict: translated.font, translated = new TranslatedFont({
evaluatorOptions: this.options, loadedName: "g_font_error",
}) font: new ErrorFont(`Type3 font load error: ${reason}`),
); dict: translated.font,
}) evaluatorOptions: this.options,
.then(translated => { });
state.font = translated.font; }
translated.send(this.handler); }
return translated.loadedName;
}); state.font = translated.font;
translated.send(this.handler);
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 // Ignore Type3-parsing errors, since we only use `loadType3Data`
.loadType3Data(self, resources, task) // here to ensure that we'll always obtain a useful /FontBBox.
.catch(function () { }
// Ignore Type3-parsing errors, since we only use `loadType3Data` }
// here to ensure that we'll always obtain a useful /FontBBox.
}) textState.loadedName = translated.loadedName;
.then(function () { textState.font = translated.font;
return translated; textState.fontMatrix = translated.font.fontMatrix || FONT_IDENTITY_MATRIX;
});
})
.then(function (translated) {
textState.loadedName = translated.loadedName;
textState.font = translated.font;
textState.fontMatrix =
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,24 +4231,26 @@ class PartialEvaluator {
standardFontName standardFontName
); );
} }
return this.extractDataStructures(dict, dict, properties).then(
newProperties => { const newProperties = await this.extractDataStructures(
if (widths) { dict,
const glyphWidths = []; dict,
let j = firstChar; properties
for (const width of widths) {
glyphWidths[j++] = this.xref.fetchIfRef(width);
}
newProperties.widths = glyphWidths;
} else {
newProperties.widths = this.buildCharCodeToWidth(
metrics.widths,
newProperties
);
}
return new Font(baseFontName, file, newProperties);
}
); );
if (widths) {
const glyphWidths = [];
let j = firstChar;
for (const width of widths) {
glyphWidths[j++] = this.xref.fetchIfRef(width);
}
newProperties.widths = glyphWidths;
} else {
newProperties.widths = this.buildCharCodeToWidth(
metrics.widths,
newProperties
);
}
return new Font(baseFontName, file, newProperties);
} }
} }
@ -4371,7 +4354,7 @@ class PartialEvaluator {
} }
} }
properties = { const properties = {
type, type,
name: fontName.name, name: fontName.name,
subtype, subtype,
@ -4414,13 +4397,14 @@ 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,
this.extractWidths(dict, descriptor, newProperties); baseDict,
properties
return new Font(fontName.name, fontFile, newProperties);
}
); );
this.extractWidths(dict, descriptor, newProperties);
return new Font(fontName.name, fontFile, newProperties);
} }
static buildFontPaths(font, glyphs, handler, evaluatorOptions) { static buildFontPaths(font, glyphs, handler, evaluatorOptions) {