Skip to content

Commit

Permalink
Merge pull request mozilla#16363 from calixteman/use_local_font
Browse files Browse the repository at this point in the history
[api-minor] Use a local font or fallback on an embedded one (if it exists) for non-embedded fonts (bug 1766039)
  • Loading branch information
calixteman committed May 10, 2023
2 parents 1a2e787 + 53134c0 commit 2d2f7b3
Show file tree
Hide file tree
Showing 12 changed files with 584 additions and 28 deletions.
Binary file removed external/standard_fonts/FoxitSans.pfb
Binary file not shown.
Binary file removed external/standard_fonts/FoxitSansBold.pfb
Binary file not shown.
Binary file removed external/standard_fonts/FoxitSansBoldItalic.pfb
Binary file not shown.
Binary file removed external/standard_fonts/FoxitSansItalic.pfb
Binary file not shown.
46 changes: 28 additions & 18 deletions src/core/core_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,31 @@ function encodeToXmlString(str) {
return buffer.join("");
}

function validateFontName(fontFamily, mustWarn = false) {
// See https://developer.mozilla.org/en-US/docs/Web/CSS/string.
const m = /^("|').*("|')$/.exec(fontFamily);
if (m && m[1] === m[2]) {
const re = new RegExp(`[^\\\\]${m[1]}`);
if (re.test(fontFamily.slice(1, -1))) {
if (mustWarn) {
warn(`FontFamily contains unescaped ${m[1]}: ${fontFamily}.`);
}
return false;
}
} else {
// See https://developer.mozilla.org/en-US/docs/Web/CSS/custom-ident.
for (const ident of fontFamily.split(/[ \t]+/)) {
if (/^(\d|(-(\d|-)))/.test(ident) || !/^[\w-\\]+$/.test(ident)) {
if (mustWarn) {
warn(`FontFamily contains invalid <custom-ident>: ${fontFamily}.`);
}
return false;
}
}
}
return true;
}

function validateCSSFont(cssFontInfo) {
// See https://developer.mozilla.org/en-US/docs/Web/CSS/font-style.
const DEFAULT_CSS_FONT_OBLIQUE = "14";
Expand All @@ -447,24 +472,8 @@ function validateCSSFont(cssFontInfo) {

const { fontFamily, fontWeight, italicAngle } = cssFontInfo;

// See https://developer.mozilla.org/en-US/docs/Web/CSS/string.
const m = /^("|').*("|')$/.exec(fontFamily);
if (m && m[1] === m[2]) {
const re = new RegExp(`[^\\\\]${m[1]}`);
if (re.test(fontFamily.slice(1, -1))) {
warn(`XFA - FontFamily contains unescaped ${m[1]}: ${fontFamily}.`);
return false;
}
} else {
// See https://developer.mozilla.org/en-US/docs/Web/CSS/custom-ident.
for (const ident of fontFamily.split(/[ \t]+/)) {
if (/^(\d|(-(\d|-)))/.test(ident) || !/^[\w-\\]+$/.test(ident)) {
warn(
`XFA - FontFamily contains invalid <custom-ident>: ${fontFamily}.`
);
return false;
}
}
if (!validateFontName(fontFamily, true)) {
return false;
}

const weight = fontWeight ? fontWeight.toString() : "";
Expand Down Expand Up @@ -617,6 +626,7 @@ export {
stringToUTF16String,
toRomanNumerals,
validateCSSFont,
validateFontName,
XRefEntryException,
XRefParseException,
};
20 changes: 20 additions & 0 deletions src/core/evaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ import { bidi } from "./bidi.js";
import { ColorSpace } from "./colorspace.js";
import { DecodeStream } from "./decode_stream.js";
import { FontFlags } from "./fonts_utils.js";
import { getFontSubstitution } from "./font_substitutions.js";
import { getGlyphsUnicode } from "./glyphlist.js";
import { getLookupTableFactory } from "./core_utils.js";
import { getMetrics } from "./metrics.js";
Expand Down Expand Up @@ -4174,6 +4175,7 @@ class PartialEvaluator {
type,
name: baseFontName,
loadedName: baseDict.loadedName,
systemFontInfo: null,
widths: metrics.widths,
defaultWidth: metrics.defaultWidth,
isSimulatedFlags: true,
Expand All @@ -4193,6 +4195,14 @@ class PartialEvaluator {
if (standardFontName) {
file = await this.fetchStandardFontData(standardFontName);
properties.isInternalFont = !!file;
if (!properties.isInternalFont && this.options.useSystemFonts) {
properties.systemFontInfo = getFontSubstitution(
this.idFactory,
this.options.standardFontDataUrl,
baseFontName,
standardFontName
);
}
}
return this.extractDataStructures(dict, dict, properties).then(
newProperties => {
Expand Down Expand Up @@ -4264,6 +4274,7 @@ class PartialEvaluator {
}
let isInternalFont = false;
let glyphScaleFactors = null;
let systemFontInfo = null;
if (fontFile) {
if (fontFile.dict) {
const subtypeEntry = fontFile.dict.get("Subtype");
Expand Down Expand Up @@ -4296,6 +4307,14 @@ class PartialEvaluator {
if (standardFontName) {
fontFile = await this.fetchStandardFontData(standardFontName);
isInternalFont = !!fontFile;
if (!isInternalFont && this.options.useSystemFonts) {
systemFontInfo = getFontSubstitution(
this.idFactory,
this.options.standardFontDataUrl,
fontName.name,
standardFontName
);
}
}
}

Expand Down Expand Up @@ -4325,6 +4344,7 @@ class PartialEvaluator {
isType3Font,
cssFontInfo,
scaleFactors: glyphScaleFactors,
systemFontInfo,
};

if (composite) {
Expand Down

0 comments on commit 2d2f7b3

Please sign in to comment.