Skip to content

Commit 127bb03

Browse files
Merge pull request #12486 from Snuffleupagus/font-Dict-cacheKey
Stop caching the *parsed* Font data on its `Dict` object (PR 7347 follow-up)
2 parents b710fbc + f956d0a commit 127bb03

File tree

2 files changed

+19
-24
lines changed

2 files changed

+19
-24
lines changed

src/core/evaluator.js

+18-23
Original file line numberDiff line numberDiff line change
@@ -979,15 +979,13 @@ class PartialEvaluator {
979979
}
980980

981981
loadFont(fontName, font, resources) {
982-
const errorFont = () => {
983-
return Promise.resolve(
984-
new TranslatedFont({
985-
loadedName: "g_font_error",
986-
font: new ErrorFont(`Font "${fontName}" is not available.`),
987-
dict: font,
988-
extraProperties: this.options.fontExtraProperties,
989-
})
990-
);
982+
const errorFont = async () => {
983+
return new TranslatedFont({
984+
loadedName: "g_font_error",
985+
font: new ErrorFont(`Font "${fontName}" is not available.`),
986+
dict: font,
987+
extraProperties: this.options.fontExtraProperties,
988+
});
991989
};
992990

993991
var fontRef,
@@ -1034,10 +1032,10 @@ class PartialEvaluator {
10341032
return errorFont();
10351033
}
10361034

1037-
// We are holding `font.translated` references just for `fontRef`s that
1035+
// We are holding `font.cacheKey` references only for `fontRef`s that
10381036
// are not actually `Ref`s, but rather `Dict`s. See explanation below.
1039-
if (font.translated) {
1040-
return font.translated;
1037+
if (font.cacheKey && this.fontCache.has(font.cacheKey)) {
1038+
return this.fontCache.get(font.cacheKey);
10411039
}
10421040

10431041
var fontCapability = createPromiseCapability();
@@ -1077,18 +1075,16 @@ class PartialEvaluator {
10771075

10781076
// Workaround for bad PDF generators that reference fonts incorrectly,
10791077
// where `fontRef` is a `Dict` rather than a `Ref` (fixes bug946506.pdf).
1080-
// In this case we should not put the font into `this.fontCache` (which is
1081-
// a `RefSetCache`), since it's not meaningful to use a `Dict` as a key.
1078+
// In this case we cannot put the font into `this.fontCache` (which is
1079+
// a `RefSetCache`), since it's not possible to use a `Dict` as a key.
10821080
//
10831081
// However, if we don't cache the font it's not possible to remove it
10841082
// when `cleanup` is triggered from the API, which causes issues on
1085-
// subsequent rendering operations (see issue7403.pdf).
1086-
// A simple workaround would be to just not hold `font.translated`
1087-
// references in this case, but this would force us to unnecessarily load
1088-
// the same fonts over and over.
1083+
// subsequent rendering operations (see issue7403.pdf) and would force us
1084+
// to unnecessarily load the same fonts over and over.
10891085
//
1090-
// Instead, we cheat a bit by attempting to use a modified `fontID` as a
1091-
// key in `this.fontCache`, to allow the font to be cached.
1086+
// Instead, we cheat a bit by using a modified `fontID` as a key in
1087+
// `this.fontCache`, to allow the font to be cached.
10921088
// NOTE: This works because `RefSetCache` calls `toString()` on provided
10931089
// keys. Also, since `fontRef` is used when getting cached fonts,
10941090
// we'll not accidentally match fonts cached with the `fontID`.
@@ -1098,7 +1094,8 @@ class PartialEvaluator {
10981094
if (!fontID) {
10991095
fontID = this.idFactory.createFontId();
11001096
}
1101-
this.fontCache.put(`id_${fontID}`, fontCapability.promise);
1097+
font.cacheKey = `cacheKey_${fontID}`;
1098+
this.fontCache.put(font.cacheKey, fontCapability.promise);
11021099
}
11031100
assert(
11041101
fontID && fontID.startsWith("f"),
@@ -1109,8 +1106,6 @@ class PartialEvaluator {
11091106
// load them asynchronously before calling display on a page.
11101107
font.loadedName = `${this.idFactory.getDocId()}_${fontID}`;
11111108

1112-
font.translated = fontCapability.promise;
1113-
11141109
this.translateFont(preEvaluatedFont)
11151110
.then(translatedFont => {
11161111
if (translatedFont.fontType !== undefined) {

src/core/obj.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -908,7 +908,7 @@ class Catalog {
908908

909909
return Promise.all(promises).then(translatedFonts => {
910910
for (const { dict } of translatedFonts) {
911-
delete dict.translated;
911+
delete dict.cacheKey;
912912
}
913913
this.fontCache.clear();
914914
this.builtInCMapCache.clear();

0 commit comments

Comments
 (0)