From 7ff223001bc9d68de26a2596fb3d9f0259a22fc0 Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Mon, 30 Nov 2026 18:25:17 +0800 Subject: [PATCH] fix(fonts): keep Latin text monospaced when the chosen font isn't installed (#7013) With a fallback font set and a primary family that isn't installed, every glyph came from the fallback. A CJK fallback such as PingFang SC has proportional Latin letters, and the terminal sized its cells for the widest of them, so text came out spread apart. Once the native font catalog shows the family is missing, the bundled JetBrains Mono now stands in for it ahead of the fallback, which still covers the glyphs it lacks. Applies to every monospace stack built from the editor or terminal font settings. Fixes #6974 --- src/platform/fonts.test.ts | 23 +++++++++++++++++++++++ src/platform/fonts.ts | 22 ++++++++++++++++++++-- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/platform/fonts.test.ts b/src/platform/fonts.test.ts index eaf8d7c58c..01cfbcb5a7 100644 --- a/src/platform/fonts.test.ts +++ b/src/platform/fonts.test.ts @@ -101,6 +101,29 @@ describe("resolveMonoFont", () => { }); }); +describe("a chosen family that isn't installed", () => { + afterEach(() => { + vi.resetModules(); + }); + + it("puts the bundled face before the fallback, so Latin text doesn't take the fallback's proportional letters", async () => { + const fonts = await import("./fonts"); + // Before the native catalog loads nothing is known to be missing. + expect(fonts.monoFontFamily("Fira Code", "PingFang SC", BASE)).toBe(`"Fira Code", "PingFang SC", ${BASE}`); + fonts.noteInstalledFonts(["PingFang SC", "Menlo"]); + expect(fonts.monoFontFamily("Fira Code", "PingFang SC", BASE)).toBe( + `"Fira Code", "JetBrains Mono", "JetBrains Mono Variable", "PingFang SC", ${BASE}`, + ); + // Installed, bundled and generic families keep the fallback right after them; so does no fallback. + expect(fonts.monoFontFamily("menlo", "PingFang SC", BASE)).toBe(`"menlo", "PingFang SC", ${BASE}`); + expect(fonts.monoFontFamily("Inter", "PingFang SC", BASE)).toBe( + `"Inter", "Inter Variable", "PingFang SC", ${BASE}`, + ); + expect(fonts.monoFontFamily("monospace", "PingFang SC", BASE)).toBe(`"monospace", "PingFang SC", ${BASE}`); + expect(fonts.monoFontFamily("Fira Code", "", BASE)).toBe(`"Fira Code", ${BASE}`); + }); +}); + describe("discovered Nerd Font fallbacks (#3723)", () => { afterEach(() => { vi.resetModules(); diff --git a/src/platform/fonts.ts b/src/platform/fonts.ts index 9990f2ae89..f9cc7a1c7a 100644 --- a/src/platform/fonts.ts +++ b/src/platform/fonts.ts @@ -98,12 +98,15 @@ async function loadUserFont(name: string, alias: string): Promise { * face is named alone unless the native catalog lists an installed JetBrains Mono. */ let monoInstalled: boolean | null = null; +/** Lower-cased installed family names from the native catalog, once it has loaded. */ +let installedFamilies: ReadonlySet | null = null; const bundledMonoOnly = () => guessOs() === "linux" && monoInstalled !== true; const BUNDLED_MONO_STACK = '"JetBrains Mono Variable", ui-monospace, monospace'; /** Records whether the system has JetBrains Mono installed (`installed`: the native catalog, not the bundled list). */ export function noteInstalledFonts(installed: readonly string[], root?: HTMLElement): void { - monoInstalled = installed.some((name) => name.trim().toLowerCase() === BUNDLED_MONO_FAMILY.toLowerCase()); + installedFamilies = new Set(installed.map((name) => name.trim().toLowerCase())); + monoInstalled = installedFamilies.has(BUNDLED_MONO_FAMILY.toLowerCase()); installMonoStack(root); } @@ -256,7 +259,22 @@ export function monoFontFamily(family: string | undefined, fallback = "", base = const name = familyName(family ?? ""); const fb = familyName(fallback); const head = familyFaces(name || BUNDLED_MONO_FAMILY); - return [...head, ...(fb ? familyFaces(fb) : []), base].join(", "); + // A chosen family that isn't installed leaves every glyph to the fallback, often a CJK face + // whose Latin letters are proportional: a grid measures its cells by the widest of them and + // the text comes out spread apart. The bundled face stands in for the missing one, and the + // fallback still covers what that lacks. + const standIn = fb && name && familyMissing(name) ? familyFaces(BUNDLED_MONO_FAMILY) : []; + return [...head, ...standIn, ...(fb ? familyFaces(fb) : []), base].join(", "); +} + +const GENERIC_FAMILIES = + /^(?:monospace|ui-monospace|sans-serif|serif|system-ui|ui-sans-serif|ui-serif|cursive|fantasy)$/i; + +/** Whether the native catalog has loaded and doesn't list `name` (bundled and generic families always count as present). */ +function familyMissing(name: string): boolean { + if (!installedFamilies) return false; + const key = name.toLowerCase(); + return !installedFamilies.has(key) && !BUNDLED_FACES.has(key) && !GENERIC_FAMILIES.test(name); } /** Tools › Terminal › Font, shared by the terminal and the Run console. */