Skip to content

Keep the requested font on East Asian code pages in GdiEngine - #21027

Open
Jiayu Hu (hjy271828) wants to merge 1 commit into
microsoft:mainfrom
hjy271828:fix/conhost-gdi-charset-fallback
Open

Jiayu Hu (hjy271828) wants to merge 1 commit into
microsoft:mainfrom
hjy271828:fix/conhost-gdi-charset-fallback

Conversation

@hjy271828

Copy link
Copy Markdown

GdiEngine::_GetProposedFont sets lfCharSet from the console code page, e.g. GB2312_CHARSET for 936. GDI's font mapper prefers a matching charset over a matching face name, so fonts that don't cover that charset (Consolas, Cascadia Mono, Nerd Fonts, ...) were silently replaced with the code page's default font (NSimSun for 936, MS Gothic for 932). This happened at startup and when the font was changed at runtime.

If the realized face doesn't match the requested one, retry with DEFAULT_CHARSET and keep it only if that yields the requested face. Fonts that support the code page charset, and fonts that aren't installed, behave as before. Characters the font lacks (e.g. CJK text) still render through GDI's font fallback.

Closes #21018

Summary of the Pull Request

On East Asian console code pages (e.g. 936, 932), conhost's GDI renderer silently replaces TrueType fonts that don't cover the code page's charset (Consolas, Cascadia Mono, Nerd Fonts, ...) with the code page's default font (NSimSun, MS Gothic). This PR makes GdiEngine keep the requested font in that case.

References and Relevant Issues

Closes #21018

Detailed Description of the Pull Request / Additional comments

GdiEngine::_GetProposedFont sets lfCharSet from the console code page (936 → GB2312_CHARSET). GDI's font mapper prefers a matching charset over a matching face name, so CreateFontIndirectW returns a GB2312-capable font instead of the requested one. This happens at startup and whenever the font is changed at runtime.

The fix: when the code-page charset isn't OEM_CHARSET, ANSI_CHARSET or DEFAULT_CHARSET, and the realized face (GetTextFaceW) differs from the requested face, retry with DEFAULT_CHARSET. The retry is only kept if it yields the requested face.

  • Fonts that support the code-page charset (NSimSun, SimHei, MS Gothic, ...): unchanged.
  • Fonts that aren't installed: fall back exactly as before.
  • Characters missing from the chosen font (e.g. CJK text in Consolas) still render through GDI's font fallback.

The change is limited to src/renderer/gdi/state.cpp. NSimSun is added to the spelling allow list.

Validation Steps Performed

Code page 936 (Chinese Simplified locale), HKCU\Console\FaceName = MesloLGL Nerd Font Mono, OpenConsole Release x64 launched with OpenConsole.exe cmd.exe. Left: main (cbfe97a), right: this PR.

validation
Configured font Before After
MesloLGL Nerd Font Mono NSimSun MesloLGL Nerd Font Mono (incl. Nerd Font icons)
Consolas NSimSun Consolas
NSimSun NSimSun NSimSun
Font that isn't installed NSimSun NSimSun
Any, after chcp 437 requested font requested font
Consolas, changed at runtime via Properties NSimSun Consolas

CJK text renders correctly in all cases. Invoke-OpenConsoleTests -Test host and -Test interactivityWin32 pass, Invoke-CodeFormat produces no changes.

PR Checklist

AI disclosure: I used an AI assistant (Claude Code) to help investigate the issue and draft the initial patch. I reviewed the full change, built it, and performed the validation above myself.

GdiEngine::_GetProposedFont sets lfCharSet from the console code page,
e.g. GB2312_CHARSET for 936. GDI's font mapper prefers a matching
charset over a matching face name, so fonts that don't cover that
charset (Consolas, Cascadia Mono, Nerd Fonts, ...) were silently
replaced with the code page's default font (NSimSun for 936,
MS Gothic for 932). This happened at startup and when the font was
changed at runtime.

If the realized face doesn't match the requested one, retry with
DEFAULT_CHARSET and keep it only if that yields the requested face.
Fonts that support the code page charset, and fonts that aren't
installed, behave as before. Characters the font lacks (e.g. CJK text)
still render through GDI's font fallback.

Closes microsoft#21018
Copilot AI balanced review requested due to automatic review settings December 5, 2026 04:41
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The LOGFONT-, locale-, and installed-font-sensitive mapper needs human runtime checks on code page 932 and Monofur/Iosevka Extralight beyond the reported code page 936 validation.

No open issues 🎉

What changed in this PR

Addresses #21018 by preserving requested fonts in conhost’s GDI renderer when East Asian code-page charsets cause unwanted font substitution.

Changes:

  • Retry mismatched fonts with DEFAULT_CHARSET, accepting the retry only when it realizes the requested face.
  • Add NSimSun to the spelling allow list.
File Description
src/​renderer/​gdi/​state.cpp Add conditional charset retry while preserving fallback for missing fonts.
.github/​actions/​spelling/​allow/​fonts.txt Allow the font name used in the new comments.

Review effort: Balanced

@hjy271828

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

conhost: Latin-only TrueType fonts are silently replaced by NSimSun (新宋体) when the console code page is 936

2 participants