From b390c487f7ca5ec6d18d4cce8a99c9d44adc457b Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 29 Mar 2022 13:48:26 +0100 Subject: [PATCH] Fix wxMemoryDC::SetFont(wxNullFont) broken by recent changes Fix crash in this function introduced by 9e5c8a8027 (Respect bitmap content scale factor in wxMSW wxMemoryDC, 2022-03-26). Also add a unit test ensuring that this doesn't get broken again under MSW, which seems to be the only place where it works. Closes #22240. --- src/msw/dcmemory.cpp | 3 ++- tests/graphics/measuring.cpp | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/msw/dcmemory.cpp b/src/msw/dcmemory.cpp index 9d7c52b531..7218fa34eb 100644 --- a/src/msw/dcmemory.cpp +++ b/src/msw/dcmemory.cpp @@ -148,7 +148,8 @@ void wxMemoryDCImpl::SetFont(const wxFont& font) // We need to adjust the font size by the ratio between the scale factor we // use and the default/global scale factor used when creating fonts. wxFont scaledFont = font; - scaledFont.WXAdjustToPPI(wxDisplay::GetStdPPI()*m_contentScaleFactor); + if ( scaledFont.IsOk() ) + scaledFont.WXAdjustToPPI(wxDisplay::GetStdPPI()*m_contentScaleFactor); wxMSWDCImpl::SetFont(scaledFont); } diff --git a/tests/graphics/measuring.cpp b/tests/graphics/measuring.cpp index 9248db9674..284d93123a 100644 --- a/tests/graphics/measuring.cpp +++ b/tests/graphics/measuring.cpp @@ -87,6 +87,14 @@ TEST_CASE("wxMemoryDC::GetTextExtent", "[memdc][text-extent]") wxBitmap bmp(100, 100); wxMemoryDC memdc(bmp); GetTextExtentTester testMem(memdc); + + // Under MSW, this wxDC should work even without any valid font -- but + // this is not the case under wxGTK and probably neither elsewhere, so + // restrict this test to that platform only. +#ifdef __WXMSW__ + memdc.SetFont(wxNullFont); + GetTextExtentTester testMemNoFont(memdc); +#endif // __WXMSW__ } #if wxUSE_PRINTING_ARCHITECTURE && wxUSE_POSTSCRIPT