Skip to content

Commit 6041539

Browse files
committed
Reduce duplication when parsing fonts in loadXfaFonts
Currently we repeat virtually the same code when calling the `PartialEvaluator.prototype.handleSetFont` method, which we can avoid by introducing an inline helper function.
1 parent 2979e23 commit 6041539

1 file changed

Lines changed: 23 additions & 35 deletions

File tree

src/core/document.js

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,6 +1292,23 @@ class PDFDocument {
12921292
},
12931293
};
12941294

1295+
const parseFont = (fontName, fallbackFontDict, cssFontInfo) =>
1296+
partialEvaluator
1297+
.handleSetFont(
1298+
resources,
1299+
[Name.get(fontName), 1],
1300+
/* fontRef = */ null,
1301+
operatorList,
1302+
task,
1303+
initialState,
1304+
fallbackFontDict,
1305+
cssFontInfo
1306+
)
1307+
.catch(reason => {
1308+
warn(`loadXfaFonts: "${reason}".`);
1309+
return null;
1310+
});
1311+
12951312
const promises = [];
12961313
for (const [fontName, font] of fontRes) {
12971314
const descriptor = font.get("FontDescriptor");
@@ -1313,21 +1330,7 @@ class PDFDocument {
13131330
continue;
13141331
}
13151332
promises.push(
1316-
partialEvaluator
1317-
.handleSetFont(
1318-
resources,
1319-
[Name.get(fontName), 1],
1320-
/* fontRef = */ null,
1321-
operatorList,
1322-
task,
1323-
initialState,
1324-
/* fallbackFontDict = */ null,
1325-
/* cssFontInfo = */ cssFontInfo
1326-
)
1327-
.catch(function (reason) {
1328-
warn(`loadXfaFonts: "${reason}".`);
1329-
return null;
1330-
})
1333+
parseFont(fontName, /* fallbackFontDict = */ null, cssFontInfo)
13311334
);
13321335
}
13331336

@@ -1365,28 +1368,13 @@ class PDFDocument {
13651368
{ name: "BoldItalic", fontWeight: 700, italicAngle: 12 },
13661369
]) {
13671370
const name = `${missing}-${fontInfo.name}`;
1368-
const dict = getXfaFontDict(name);
13691371

13701372
promises.push(
1371-
partialEvaluator
1372-
.handleSetFont(
1373-
resources,
1374-
[Name.get(name), 1],
1375-
/* fontRef = */ null,
1376-
operatorList,
1377-
task,
1378-
initialState,
1379-
/* fallbackFontDict = */ dict,
1380-
/* cssFontInfo = */ {
1381-
fontFamily: missing,
1382-
fontWeight: fontInfo.fontWeight,
1383-
italicAngle: fontInfo.italicAngle,
1384-
}
1385-
)
1386-
.catch(function (reason) {
1387-
warn(`loadXfaFonts: "${reason}".`);
1388-
return null;
1389-
})
1373+
parseFont(name, getXfaFontDict(name), {
1374+
fontFamily: missing,
1375+
fontWeight: fontInfo.fontWeight,
1376+
italicAngle: fontInfo.italicAngle,
1377+
})
13901378
);
13911379
}
13921380
}

0 commit comments

Comments
 (0)