diff --git a/.github/actions/spelling/allow/fonts.txt b/.github/actions/spelling/allow/fonts.txt index a0bacd06007..e21a8736db2 100644 --- a/.github/actions/spelling/allow/fonts.txt +++ b/.github/actions/spelling/allow/fonts.txt @@ -6,5 +6,6 @@ Extralight Iosevka MDL Monofur +NSimSun Segoe wght diff --git a/src/renderer/gdi/state.cpp b/src/renderer/gdi/state.cpp index 3d1e8fb197d..e8c4fb766a5 100644 --- a/src/renderer/gdi/state.cpp +++ b/src/renderer/gdi/state.cpp @@ -679,6 +679,48 @@ GdiEngine::~GdiEngine() FontDesired.FillLegacyNameBuffer(lf.lfFaceName); + // The charset above is derived from the console code page. For East Asian code pages + // (e.g. 936 -> GB2312_CHARSET) GDI's font mapper weighs a charset mismatch higher than + // a face name match, so a font that lacks that charset (Consolas, Cascadia Mono, Nerd Fonts, ...) + // gets silently replaced with the code page's default font (e.g. NSimSun), at startup and + // whenever the font is changed. The property sheet doesn't have this problem, because it + // uses the charset GDI enumerated for the font, so its preview showed the requested font. + // If the requested face doesn't survive the font mapper, retry with DEFAULT_CHARSET, + // which honors the face name. The retry is only kept if it yields the requested face, + // so that missing fonts keep falling back exactly like they used to. + if (lf.lfCharSet != OEM_CHARSET && lf.lfCharSet != ANSI_CHARSET && lf.lfCharSet != DEFAULT_CHARSET) + { + const auto realizesRequestedFace = [&](const LOGFONTW& candidate) noexcept { + const wil::unique_hfont font{ CreateFontIndirectW(&candidate) }; + if (!font) + { + return false; + } + + const auto previous = SelectFont(hdcTemp.get(), font.get()); + if (!previous) + { + return false; + } + + wchar_t realizedFace[LF_FACESIZE]{}; + const auto length = GetTextFaceW(hdcTemp.get(), LF_FACESIZE, &realizedFace[0]); + SelectFont(hdcTemp.get(), previous); + + return length > 0 && CompareStringOrdinal(&realizedFace[0], -1, &candidate.lfFaceName[0], -1, TRUE) == CSTR_EQUAL; + }; + + if (!realizesRequestedFace(lf)) + { + auto retry = lf; + retry.lfCharSet = DEFAULT_CHARSET; + if (realizesRequestedFace(retry)) + { + lf.lfCharSet = DEFAULT_CHARSET; + } + } + } + struct FontStyle { LONG weight;