diff --git a/src/features/terminal/terminal.test.ts b/src/features/terminal/terminal.test.ts index 7153870ee2..051f76705e 100644 --- a/src/features/terminal/terminal.test.ts +++ b/src/features/terminal/terminal.test.ts @@ -467,6 +467,18 @@ describe("terminalFont", () => { expect(family).toContain('"PingFang SC", "Noto Sans CJK SC", "Noto Sans SC", "Source Han Sans SC"'); expect(family.endsWith(', "Source Han Sans SC", monospace')).toBe(true); }); + it("names Latin monospace faces before the CJK ones, whose Latin glyphs are proportional", () => { + // A Linux box without the chosen font: Latin must stop at a monospace face, not Noto Sans CJK. + for (const font of [ + terminalFont(t, editor, BASE), + terminalFont({ ...t, useEditorFont: false, fontFamily: "Missing Mono" }, editor, BASE), + ]) { + const at = (face: string) => font.family.indexOf(`"${face}"`); + expect(at("DejaVu Sans Mono")).toBeGreaterThan(at("Symbols Nerd Font")); + expect(at("Consolas")).toBeLessThan(at("Microsoft YaHei")); + expect(at("DejaVu Sans Mono")).toBeLessThan(at("Noto Sans CJK SC")); + } + }); }); describe("terminal tab titles", () => { diff --git a/src/features/terminal/theme.ts b/src/features/terminal/theme.ts index c3d9c8491d..5fce2f2095 100644 --- a/src/features/terminal/theme.ts +++ b/src/features/terminal/theme.ts @@ -8,6 +8,7 @@ import type { ITheme, Terminal } from "@xterm/xterm"; import { fontWeightOf } from "@/platform/editorSettings"; import { CJK_FALLBACK_FACES, + LATIN_MONO_FALLBACK_FACES, fontsLoaded, monoStack, nerdFontFaces, @@ -186,13 +187,16 @@ export interface TerminalFont { } /** - * Nerd Fonts before the generic family, so per-glyph fallback finds prompt icons, then the CJK - * faces: a wide character xterm only finds through the webview's own fallback measures one way and - * draws another (see `CJK_FALLBACK_FACES`), which drifts any row that mixes CJK with a background. + * Nerd Fonts before the generic family, so per-glyph fallback finds prompt icons, then common + * Latin monospace faces (the CJK faces' Latin glyphs are proportional, see + * `LATIN_MONO_FALLBACK_FACES`), then the CJK faces: a wide character xterm only finds through the + * webview's own fallback measures one way and draws another (see `CJK_FALLBACK_FACES`), which + * drifts any row that mixes CJK with a background. */ function withNerdFonts(family: string): string { + const latin = LATIN_MONO_FALLBACK_FACES.map(quoteFontFamily).join(", "); const cjk = CJK_FALLBACK_FACES.map(quoteFontFamily).join(", "); - return `${family.replace(/,\s*monospace\s*$/i, "")}, ${nerdFontFaces().join(", ")}, ${cjk}, monospace`; + return `${family.replace(/,\s*monospace\s*$/i, "")}, ${nerdFontFaces().join(", ")}, ${latin}, ${cjk}, monospace`; } /** diff --git a/src/platform/fonts.ts b/src/platform/fonts.ts index 6a02bbdfde..5093f80dc7 100644 --- a/src/platform/fonts.ts +++ b/src/platform/fonts.ts @@ -139,6 +139,20 @@ export const CJK_FALLBACK_FACES = [ "Noto Sans SC", "Source Han Sans SC", ]; +/** + * Monospace faces Linux and Windows ship, named before `CJK_FALLBACK_FACES` on a grid's font + * stack. Those CJK faces carry proportional Latin glyphs: when the chosen family and the + * `--font-mono` stack (macOS-only past the bundled font) don't take, plain text would be drawn by + * one of them inside cells measured for its widest glyph, every letter adrift in a wide cell. + * None of these has CJK glyphs, so wide characters still reach the named CJK faces. + */ +export const LATIN_MONO_FALLBACK_FACES = [ + "DejaVu Sans Mono", + "Liberation Mono", + "Noto Sans Mono", + "Cascadia Mono", + "Consolas", +]; /** In running text the proportional faces give icons their own width instead of one cell's. */ const UI_NERD_FONTS = [ "JetBrainsMono Nerd Font Propo",