fix(fonts): restore legacy local font names via metric-compatible ali… (#3057)

* fix(fonts): restore legacy local font names via metric-compatible aliases

Closes #2989.

In v5.0.x the Puppeteer renderer resolved fonts like 'Times New Roman'
or 'Arial' through the browser's font stack. The v5.1 migration to
@react-pdf/renderer requires every font to be Font.register()-ed; the
legacy local-font names were not carried over, so resumes upgraded
from v5.0.x had their typography silently replaced with IBM Plex Serif,
changing line breaks, page counts and overall layout.

This adds a render-time alias layer mapping the old names to
metric-compatible web fonts already shipped in the webfont list:

  Times New Roman → Tinos
  Cambria         → Tinos
  Arial           → Arimo
  Garamond        → EB Garamond
  Calibri         → Source Sans 3

- packages/fonts:
  - new `legacyFontAliases` map and `resolveLegacyFontAlias` helper.
  - `getFont` falls back to the alias map when the direct lookup misses,
    so any caller that asked 'is this a known family?' now answers
    truthfully for the legacy names.
  - `getFontDisplayName` is intentionally unchanged: the typography
    sidebar keeps showing the user's original choice ('Times New Roman'),
    while the renderer transparently swaps in the alias target.

- packages/pdf/use-register-fonts:
  - `resolvePdfFontFamily` returns the alias target when one applies,
    so `Font.register` runs against the right web font and templates
    receive a family name they can actually render.

Backwards compatible: families that were never aliased (Roboto, IBM
Plex Serif, the standard PDF fonts, ...) take exactly the same code
path as before. The CJK glyph fallback added in #2986 / PR #3013
continues to apply on top of the resolved primary family.

* fix(fonts): use Carlito (not Source Sans 3) as Calibri alias

Per maintainer review feedback: Carlito is metric-compatible with
Calibri, while Source Sans 3 only matches visually. Switching gives
upgraded resumes the same line widths, line breaks and page counts
they had under v5.0.x.

- packages/fonts/webfontlist.json: add Carlito (Google Fonts, weights
  400/700 + italics) so it's a registerable target.
- packages/scripts/fonts/generate.ts: add a getMetricCompatibleFonts
  helper and merge it into the output, mirroring how Computer Modern
  fonts are appended. This way regenerating the list (`pnpm generate`)
  re-emits Carlito automatically and dedupes if it ever enters the
  Google Fonts popularity slice.
- packages/fonts/src/index.ts: alias `Calibri → Carlito`.
- packages/fonts/src/index.test.ts: update alias test cases.
This commit is contained in:
JamesGoslings
2026-05-14 11:36:15 +02:00
committed by GitHub
parent affa1d6646
commit 22c60c64b6
5 changed files with 120 additions and 4 deletions
+43
View File
@@ -11,6 +11,7 @@ import {
getWebFont,
getWebFontSource,
isStandardPdfFontFamily,
resolveLegacyFontAlias,
standardFontList,
webFontList,
webFontMap,
@@ -257,3 +258,45 @@ describe("buildResumeFontFamily", () => {
expect(result).toContain("Bob\\'s Font");
});
});
describe("legacy font compatibility (#2989)", () => {
it.each([
["Times New Roman", "Tinos"],
["Arial", "Arimo"],
["Garamond", "EB Garamond"],
["Calibri", "Carlito"],
["Cambria", "Tinos"],
])("aliases %s → %s", (legacy, target) => {
expect(resolveLegacyFontAlias(legacy)).toBe(target);
});
it("returns null for non-aliased families", () => {
expect(resolveLegacyFontAlias("Roboto")).toBeNull();
expect(resolveLegacyFontAlias("IBM Plex Serif")).toBeNull();
expect(resolveLegacyFontAlias("UnknownFont")).toBeNull();
});
it("every alias target is actually registered as a known font", () => {
const aliasTargets = ["Tinos", "Arimo", "EB Garamond", "Carlito"];
for (const target of aliasTargets) {
expect(getFont(target), `alias target ${target} must be a known font`).toBeDefined();
}
});
it("getFont resolves a legacy family to its alias target", () => {
const tnr = getFont("Times New Roman");
expect(tnr).toBeDefined();
expect(tnr?.family).toBe("Tinos");
});
it("getFont still returns the direct font when both legacy and direct lookup would succeed", () => {
// Sanity check: for non-aliased families the direct path is used.
expect(getFont("Roboto")?.family).toBe("Roboto");
});
it("getFontDisplayName preserves the legacy family name (UI is not rewritten)", () => {
// Users who picked "Times New Roman" should keep seeing that label
// in the typography sidebar — the alias is a render-time concern only.
expect(getFontDisplayName("Times New Roman")).toBe("Times New Roman");
});
});