Skip to content

Commit

Permalink
Merge pull request #12486 from Snuffleupagus/font-Dict-cacheKey
Browse files Browse the repository at this point in the history
Stop caching the *parsed* Font data on its `Dict` object (PR 7347 follow-up)
  • Loading branch information
timvandermeij authored Oct 16, 2020
2 parents b710fbc + f956d0a commit 127bb03
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 24 deletions.
41 changes: 18 additions & 23 deletions src/core/evaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -979,15 +979,13 @@ class PartialEvaluator {
}

loadFont(fontName, font, resources) {
const errorFont = () => {
return Promise.resolve(
new TranslatedFont({
loadedName: "g_font_error",
font: new ErrorFont(`Font "${fontName}" is not available.`),
dict: font,
extraProperties: this.options.fontExtraProperties,
})
);
const errorFont = async () => {
return new TranslatedFont({
loadedName: "g_font_error",
font: new ErrorFont(`Font "${fontName}" is not available.`),
dict: font,
extraProperties: this.options.fontExtraProperties,
});
};

var fontRef,
Expand Down Expand Up @@ -1034,10 +1032,10 @@ class PartialEvaluator {
return errorFont();
}

// We are holding `font.translated` references just for `fontRef`s that
// We are holding `font.cacheKey` references only for `fontRef`s that
// are not actually `Ref`s, but rather `Dict`s. See explanation below.
if (font.translated) {
return font.translated;
if (font.cacheKey && this.fontCache.has(font.cacheKey)) {
return this.fontCache.get(font.cacheKey);
}

var fontCapability = createPromiseCapability();
Expand Down Expand Up @@ -1077,18 +1075,16 @@ class PartialEvaluator {

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

font.translated = fontCapability.promise;

this.translateFont(preEvaluatedFont)
.then(translatedFont => {
if (translatedFont.fontType !== undefined) {
Expand Down
2 changes: 1 addition & 1 deletion src/core/obj.js
Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,7 @@ class Catalog {

return Promise.all(promises).then(translatedFonts => {
for (const { dict } of translatedFonts) {
delete dict.translated;
delete dict.cacheKey;
}
this.fontCache.clear();
this.builtInCMapCache.clear();
Expand Down

0 comments on commit 127bb03

Please sign in to comment.