Skip to content

Commit

Permalink
For missing font, use a local font if it exists even if there's no st…
Browse files Browse the repository at this point in the history
…andard substitution

If the font foo is missing we just try lo load local(foo) and maybe
we'll be lucky.
  • Loading branch information
calixteman committed May 13, 2023
1 parent e738e15 commit 864c1bb
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 25 deletions.
36 changes: 18 additions & 18 deletions src/core/evaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -4197,15 +4197,15 @@ class PartialEvaluator {
if (standardFontName) {
file = await this.fetchStandardFontData(standardFontName);
properties.isInternalFont = !!file;
if (!properties.isInternalFont && this.options.useSystemFonts) {
properties.systemFontInfo = getFontSubstitution(
this.systemFontCache,
this.idFactory,
this.options.standardFontDataUrl,
baseFontName,
standardFontName
);
}
}
if (!properties.isInternalFont && this.options.useSystemFonts) {
properties.systemFontInfo = getFontSubstitution(
this.systemFontCache,
this.idFactory,
this.options.standardFontDataUrl,
baseFontName,
standardFontName
);
}
return this.extractDataStructures(dict, dict, properties).then(
newProperties => {
Expand Down Expand Up @@ -4310,15 +4310,15 @@ class PartialEvaluator {
if (standardFontName) {
fontFile = await this.fetchStandardFontData(standardFontName);
isInternalFont = !!fontFile;
if (!isInternalFont && this.options.useSystemFonts) {
systemFontInfo = getFontSubstitution(
this.systemFontCache,
this.idFactory,
this.options.standardFontDataUrl,
fontName.name,
standardFontName
);
}
}
if (!isInternalFont && this.options.useSystemFonts) {
systemFontInfo = getFontSubstitution(
this.systemFontCache,
this.idFactory,
this.options.standardFontDataUrl,
fontName.name,
standardFontName
);
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/core/font_substitutions.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,8 @@ function makeLocal(prepend, local) {
* @param {Object} idFactory The ids factory.
* @param {String} localFontPath Path to the fonts directory.
* @param {String} baseFontName The font name to be substituted.
* @param {String} standardFontName The standard font name to use if the base
* font is not available.
* @param {String|undefined} standardFontName The standard font name to use
* if the base font is not available.
* @returns an Object with the CSS, the loaded name, the src and the style.
*/
function getFontSubstitution(
Expand Down Expand Up @@ -437,7 +437,8 @@ function getFontSubstitution(
(italic && ITALIC) ||
NORMAL;
substitutionInfo = {
css: `${loadedName},sans-serif`,
css: loadedName,
mustGuessFallback: true,
loadedName,
src: `local(${baseFontName})`,
style,
Expand Down
6 changes: 5 additions & 1 deletion src/core/fonts.js
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,7 @@ class Font {
let { type, subtype } = properties;
this.type = type;
this.subtype = subtype;
this.systemFontInfo = properties.systemFontInfo;

const matches = name.match(/^InvalidPDFjsFont_(.*)_\d+$/);
this.isInvalidPDFjsFont = !!matches;
Expand All @@ -999,7 +1000,10 @@ class Font {
this.fallbackName = "sans-serif";
}

this.systemFontInfo = properties.systemFontInfo;
if (this.systemFontInfo?.mustGuessFallback) {
this.systemFontInfo.css = `${this.systemFontInfo.css},${this.fallbackName}`;
}

this.differences = properties.differences;
this.widths = properties.widths;
this.defaultWidth = properties.defaultWidth;
Expand Down
14 changes: 11 additions & 3 deletions src/display/font_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,17 @@ class FontLoader {
await fontFace.load();
this.#systemFonts.add(loadedName);
} catch {
warn(
`Cannot load system font: ${loadedName} for style ${style.style} and weight ${style.weight}.`
);
if (info.mustGuessFallback) {
// We're trying to load only one system font.
const match = src.match(/^local\((.*)\)$/);
warn(
`Cannot load system font: ${match[1]}, installing it could help to have a better PDF rendering.`
);
} else {
warn(
`Cannot load system font: ${loadedName} for style ${style.style} and weight ${style.weight}.`
);
}
this.removeNativeFontFace(fontFace);
}
return;
Expand Down

0 comments on commit 864c1bb

Please sign in to comment.