Skip to content

Commit

Permalink
Merge pull request #16408 from calixteman/local_font_cache
Browse files Browse the repository at this point in the history
Add a cache to avoid to load several times a local font
  • Loading branch information
calixteman authored May 10, 2023
2 parents 2d2f7b3 + cfb908c commit d520754
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/core/catalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class Catalog {
this.pageKidsCountCache = new RefSetCache();
this.pageIndexCache = new RefSetCache();
this.nonBlendModesSet = new RefSet();
this.systemFontCache = new Map();
}

get version() {
Expand Down Expand Up @@ -1062,6 +1063,7 @@ class Catalog {
this.fontCache.clear();
this.builtInCMapCache.clear();
this.standardFontDataCache.clear();
this.systemFontCache.clear();
}

async getPageDict(pageIndex) {
Expand Down
9 changes: 9 additions & 0 deletions src/core/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class Page {
builtInCMapCache,
standardFontDataCache,
globalImageCache,
systemFontCache,
nonBlendModesSet,
xfaFactory,
}) {
Expand All @@ -86,6 +87,7 @@ class Page {
this.builtInCMapCache = builtInCMapCache;
this.standardFontDataCache = standardFontDataCache;
this.globalImageCache = globalImageCache;
this.systemFontCache = systemFontCache;
this.nonBlendModesSet = nonBlendModesSet;
this.evaluatorOptions = pdfManager.evaluatorOptions;
this.resourcesPromise = null;
Expand Down Expand Up @@ -270,6 +272,7 @@ class Page {
builtInCMapCache: this.builtInCMapCache,
standardFontDataCache: this.standardFontDataCache,
globalImageCache: this.globalImageCache,
systemFontCache: this.systemFontCache,
options: this.evaluatorOptions,
});

Expand Down Expand Up @@ -321,6 +324,7 @@ class Page {
builtInCMapCache: this.builtInCMapCache,
standardFontDataCache: this.standardFontDataCache,
globalImageCache: this.globalImageCache,
systemFontCache: this.systemFontCache,
options: this.evaluatorOptions,
});

Expand Down Expand Up @@ -390,6 +394,7 @@ class Page {
builtInCMapCache: this.builtInCMapCache,
standardFontDataCache: this.standardFontDataCache,
globalImageCache: this.globalImageCache,
systemFontCache: this.systemFontCache,
options: this.evaluatorOptions,
});

Expand Down Expand Up @@ -533,6 +538,7 @@ class Page {
builtInCMapCache: this.builtInCMapCache,
standardFontDataCache: this.standardFontDataCache,
globalImageCache: this.globalImageCache,
systemFontCache: this.systemFontCache,
options: this.evaluatorOptions,
});

Expand Down Expand Up @@ -602,6 +608,7 @@ class Page {
builtInCMapCache: this.builtInCMapCache,
standardFontDataCache: this.standardFontDataCache,
globalImageCache: this.globalImageCache,
systemFontCache: this.systemFontCache,
options: this.evaluatorOptions,
});

Expand Down Expand Up @@ -1476,6 +1483,7 @@ class PDFDocument {
builtInCMapCache: catalog.builtInCMapCache,
standardFontDataCache: catalog.standardFontDataCache,
globalImageCache: catalog.globalImageCache,
systemFontCache: catalog.systemFontCache,
nonBlendModesSet: catalog.nonBlendModesSet,
xfaFactory,
});
Expand Down Expand Up @@ -1574,6 +1582,7 @@ class PDFDocument {
builtInCMapCache: catalog.builtInCMapCache,
standardFontDataCache: catalog.standardFontDataCache,
globalImageCache: catalog.globalImageCache,
systemFontCache: catalog.systemFontCache,
nonBlendModesSet: catalog.nonBlendModesSet,
xfaFactory: null,
})
Expand Down
4 changes: 4 additions & 0 deletions src/core/evaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ class PartialEvaluator {
builtInCMapCache,
standardFontDataCache,
globalImageCache,
systemFontCache,
options = null,
}) {
this.xref = xref;
Expand All @@ -225,6 +226,7 @@ class PartialEvaluator {
this.builtInCMapCache = builtInCMapCache;
this.standardFontDataCache = standardFontDataCache;
this.globalImageCache = globalImageCache;
this.systemFontCache = systemFontCache;
this.options = options || DefaultPartialEvaluatorOptions;
this.parsingType3Font = false;

Expand Down Expand Up @@ -4197,6 +4199,7 @@ class PartialEvaluator {
properties.isInternalFont = !!file;
if (!properties.isInternalFont && this.options.useSystemFonts) {
properties.systemFontInfo = getFontSubstitution(
this.systemFontCache,
this.idFactory,
this.options.standardFontDataUrl,
baseFontName,
Expand Down Expand Up @@ -4309,6 +4312,7 @@ class PartialEvaluator {
isInternalFont = !!fontFile;
if (!isInternalFont && this.options.useSystemFonts) {
systemFontInfo = getFontSubstitution(
this.systemFontCache,
this.idFactory,
this.options.standardFontDataUrl,
fontName.name,
Expand Down
17 changes: 15 additions & 2 deletions src/core/font_substitutions.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ function makeLocal(prepend, local) {
* }
* or use the FontFace API.
*
* @param {Map} systemFontCache The cache of local fonts.
* @param {Object} idFactory The ids factory.
* @param {String} localFontPath Path to the fonts directory.
* @param {String} baseFontName The font name to be substituted.
Expand All @@ -382,6 +383,7 @@ function makeLocal(prepend, local) {
* @returns an Object with the CSS, the loaded name, the src and the style.
*/
function getFontSubstitution(
systemFontCache,
idFactory,
localFontPath,
baseFontName,
Expand All @@ -393,6 +395,12 @@ function getFontSubstitution(
// just replace them by a dash.
baseFontName = normalizeFontName(baseFontName);

const key = baseFontName;
let substitutionInfo = systemFontCache.get(key);
if (substitutionInfo) {
return substitutionInfo;
}

// First, check if we've a substitution for the base font.
let substitution = substitutionMap.get(baseFontName);
if (!substitution) {
Expand All @@ -416,6 +424,7 @@ function getFontSubstitution(
const loadedName = `${idFactory.getDocId()}_sf_${idFactory.createFontId()}`;
if (!substitution) {
if (!validateFontName(baseFontName)) {
systemFontCache.set(key, null);
// If the baseFontName is not valid we don't want to use it.
return null;
}
Expand All @@ -427,12 +436,14 @@ function getFontSubstitution(
(bold && BOLD) ||
(italic && ITALIC) ||
NORMAL;
return {
substitutionInfo = {
css: `${loadedName},sans-serif`,
loadedName,
src: `local(${baseFontName})`,
style,
};
systemFontCache.set(key, substitutionInfo);
return substitutionInfo;
}

while (substitution.alias) {
Expand Down Expand Up @@ -467,12 +478,14 @@ function getFontSubstitution(
src = `local(${baseFontName}),${src}`;
}

return {
substitutionInfo = {
css: `${loadedName},${ultimate}`,
loadedName,
src,
style,
};
systemFontCache.set(key, substitutionInfo);
return substitutionInfo;
}

export { getFontSubstitution };
7 changes: 7 additions & 0 deletions src/display/font_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import {
import { isNodeJS } from "../shared/is_node.js";

class FontLoader {
#systemFonts = new Set();

constructor({
ownerDocument = globalThis.document,
styleElement = null, // For testing only.
Expand Down Expand Up @@ -69,6 +71,7 @@ class FontLoader {
this._document.fonts.delete(nativeFontFace);
}
this.nativeFontFaces.clear();
this.#systemFonts.clear();

if (this.styleElement) {
// Note: ChildNode.remove doesn't throw if the parentNode is undefined.
Expand All @@ -78,6 +81,9 @@ class FontLoader {
}

async loadSystemFont(info) {
if (!info || this.#systemFonts.has(info.loadedName)) {
return;
}
assert(
!this.disableFontFace,
"loadSystemFont shouldn't be called when `disableFontFace` is set."
Expand All @@ -89,6 +95,7 @@ class FontLoader {
this.addNativeFontFace(fontFace);
try {
await fontFace.load();
this.#systemFonts.add(loadedName);
} catch {
warn(
`Cannot load system font: ${loadedName} for style ${style.style} and weight ${style.weight}.`
Expand Down
1 change: 1 addition & 0 deletions test/unit/annotation_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ describe("annotation", function () {
fontCache: new RefSetCache(),
builtInCMapCache,
standardFontDataCache: new Map(),
systemFontCache: new Map(),
});
});

Expand Down

0 comments on commit d520754

Please sign in to comment.