Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/actions/spelling/allow/fonts.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ Extralight
Iosevka
MDL
Monofur
NSimSun
Segoe
wght
42 changes: 42 additions & 0 deletions src/renderer/gdi/state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down