Skip to content

Commit

Permalink
Fix issue #2525: Diff renders incorrectly with "Cascadia Mono" variant
Browse files Browse the repository at this point in the history
  • Loading branch information
sdottaka committed Nov 13, 2024
1 parent 938cda3 commit 9ef4cb2
Showing 1 changed file with 60 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -297,17 +297,74 @@ static D2D1_SIZE_F GetCharWidthHeight(IDWriteTextFormat *pTextFormat)
return {textMetrics.width, textMetrics.height};
}

static CComPtr<IDWriteFont> CreateFontFromLOGFONT(const LOGFONT& logFont)
{
IDWriteFactory *pDWriteFactory = AfxGetD2DState()->GetWriteFactory();
CComPtr<IDWriteGdiInterop> gdiInterop;
HRESULT hr = pDWriteFactory->GetGdiInterop(&gdiInterop);
if (FAILED(hr))
return nullptr;
CComPtr<IDWriteFont> dwriteFont;
hr = gdiInterop->CreateFontFromLOGFONT(&logFont, &dwriteFont);
if (FAILED(hr))
return nullptr;
return dwriteFont;
}

static void GetFontFamilyNameFromFont(IDWriteFont* pFont, wchar_t* fontFamilyName, size_t bufsize)
{
if (!pFont)
return;
CComPtr<IDWriteFontFamily> pFontFamily;
HRESULT hr = pFont->GetFontFamily(&pFontFamily);
if (FAILED(hr))
return;
CComPtr<IDWriteLocalizedStrings> pLocalizedStrings;
hr = pFontFamily->GetFamilyNames(&pLocalizedStrings);
if (FAILED(hr))
return;
UINT32 index = 0;
BOOL exists = FALSE;
hr = pLocalizedStrings->FindLocaleName(L"en-us", &index, &exists);
if (FAILED(hr) || !exists)
index = 0;
UINT32 length = 0;
pLocalizedStrings->GetStringLength(index, &length);
pLocalizedStrings->GetString(index, fontFamilyName, (std::max)(length + 1, static_cast<UINT32>(bufsize)));
}

static DWRITE_FONT_WEIGHT GetDWriteFontWeight(const LOGFONT& lf, bool bold)
{
DWRITE_FONT_WEIGHT result;
const long weight = bold ? (lf.lfWeight + 300) : lf.lfWeight;
if (weight <= 100) return DWRITE_FONT_WEIGHT_THIN;
else if (weight <= 200) return DWRITE_FONT_WEIGHT_EXTRA_LIGHT;
else if (weight <= 300) return DWRITE_FONT_WEIGHT_LIGHT;
else if (weight <= 350) return DWRITE_FONT_WEIGHT_SEMI_LIGHT;
else if (weight <= 400) return DWRITE_FONT_WEIGHT_NORMAL;
else if (weight <= 500) return DWRITE_FONT_WEIGHT_MEDIUM;
else if (weight <= 600) return DWRITE_FONT_WEIGHT_SEMI_BOLD;
else if (weight <= 700) return DWRITE_FONT_WEIGHT_BOLD;
else if (weight <= 800) return DWRITE_FONT_WEIGHT_EXTRA_BOLD;
else if (weight <= 900) return DWRITE_FONT_WEIGHT_BLACK;
else return DWRITE_FONT_WEIGHT_EXTRA_BLACK;
}

void CCrystalRendererDirectWrite::SetFont(const LOGFONT &lf)
{
CComPtr<IDWriteFont> pFont = CreateFontFromLOGFONT(lf);
wchar_t fontFamilyName[256]{};
GetFontFamilyNameFromFont(pFont, fontFamilyName, std::size(fontFamilyName));

m_lfBaseFont = lf;
for (int nIndex = 0; nIndex < 4; ++nIndex)
{
bool bold = (nIndex & 1) != 0;
bool italic = (nIndex & 2) != 0;
m_pTextFormat[nIndex].reset(new CD2DTextFormat(&m_renderTarget,
lf.lfFaceName[0] ? lf.lfFaceName : _T("Courier New"),
fontFamilyName[0] ? fontFamilyName : _T("Courier New"),
static_cast<FLOAT>(abs(lf.lfHeight == 0 ? 11 : lf.lfHeight)),
bold ? DWRITE_FONT_WEIGHT_BOLD : DWRITE_FONT_WEIGHT_NORMAL,
GetDWriteFontWeight(lf, bold),
italic ? DWRITE_FONT_STYLE_ITALIC : DWRITE_FONT_STYLE_NORMAL));
IDWriteTextFormat *pTextFormat = m_pTextFormat[nIndex]->Get();
pTextFormat->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_LEADING);
Expand All @@ -316,7 +373,7 @@ void CCrystalRendererDirectWrite::SetFont(const LOGFONT &lf)
}
m_pCurrentTextFormat = m_pTextFormat[0].get();
m_pFont.reset();
m_charSize = ::GetCharWidthHeight(m_pTextFormat[3]->Get());
m_charSize = ::GetCharWidthHeight(m_pTextFormat[0]->Get());
}

void CCrystalRendererDirectWrite::SwitchFont(bool italic, bool bold)
Expand Down

0 comments on commit 9ef4cb2

Please sign in to comment.