Порівняти коміти

...
Автор SHA1 Повідомлення Дата
l0ng-ai 4e9dc45d68 fix(fonts): keep Latin text monospaced when the chosen font isn't installed
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
2026-11-30 18:24:48 +08:00
2 змінених файлів з 43 додано та 2 видалено

No files matched your search

+23
Переглянути файл
@@ -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();
+20 -2
Переглянути файл
@@ -98,12 +98,15 @@ async function loadUserFont(name: string, alias: string): Promise<void> {
* 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<string> | 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. */