From 5507f8eebc3f5a11a2dbf118f964d8163ff5e58c Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 16 Jul 2019 14:33:36 +0200 Subject: [PATCH 1/2] Add wxNativeFontInfo::GetPointSizeFromLogFontHeight() helper No real changes, just refactor the code previously present in both wxNativeFontInfo ctor and SetPixelSize() in a single function and reuse it in both places. --- include/wx/fontutil.h | 9 ++++++++- src/msw/font.cpp | 9 ++++----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/include/wx/fontutil.h b/include/wx/fontutil.h index 38f473654e..983f29ebf8 100644 --- a/include/wx/fontutil.h +++ b/include/wx/fontutil.h @@ -118,7 +118,14 @@ public: // set the XFLD void SetXFontName(const wxString& xFontName); #elif defined(__WXMSW__) - wxNativeFontInfo(const LOGFONT& lf_); + wxNativeFontInfo(const LOGFONT& lf_) + : lf(lf_), + pointSize(GetPointSizeFromLogFontHeight(lf.lfHeight)) + { + } + + // MSW-specific: get point size from LOGFONT height using the default DPI. + static float GetPointSizeFromLogFontHeight(int height); // MSW-specific: get the height value in pixels using LOGFONT convention // (i.e. negative) corresponding to the given size in points and DPI. diff --git a/src/msw/font.cpp b/src/msw/font.cpp index 255aafcdae..ed6e2c8c2c 100644 --- a/src/msw/font.cpp +++ b/src/msw/font.cpp @@ -402,13 +402,13 @@ void wxFontRefData::Free() // wxNativeFontInfo // ---------------------------------------------------------------------------- -wxNativeFontInfo::wxNativeFontInfo(const LOGFONT& lf_) - : lf(lf_) +/* static */ +float wxNativeFontInfo::GetPointSizeFromLogFontHeight(int height) { // Determine the size in points using the primary screen DPI as we don't // have anything else here. const float ppi = ::GetDeviceCaps(ScreenHDC(), LOGPIXELSY); - pointSize = 72.0f * abs(lf.lfHeight) / ppi; + return 72.0f * abs(height) / ppi; } void wxNativeFontInfo::Init() @@ -535,8 +535,7 @@ void wxNativeFontInfo::SetPixelSize(const wxSize& pixelSize) // We don't have the right DPI to use here neither, but we need to update // the point size too, so fall back to the default. - const float ppi = ::GetDeviceCaps(ScreenHDC(), LOGPIXELSY); - pointSize = 72.0f * pixelSize.GetHeight() / ppi; + pointSize = GetPointSizeFromLogFontHeight(lf.lfHeight); } void wxNativeFontInfo::SetStyle(wxFontStyle style) From 228cd926e27589ee514682513cc8d89aabc9a603 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 16 Jul 2019 14:37:19 +0200 Subject: [PATCH 2/2] Initialize point size correctly from v0 native font info strings Reading native font info strings in v0 format, used by the previous wxWidgets versions, resulted in creation of fonts with 0 point size, which resulted in suboptimal user experience when such a font was used to display text. Fix this by initializing point size to the value corresponding to the font height in pixels using the default DPI, just as we already do when creating wxNativeFontInfo from a LOGFONT. --- src/msw/font.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/msw/font.cpp b/src/msw/font.cpp index ed6e2c8c2c..3d7d7e0f33 100644 --- a/src/msw/font.cpp +++ b/src/msw/font.cpp @@ -658,11 +658,13 @@ bool wxNativeFontInfo::FromString(const wxString& s) if ( !token.ToLong(&l) ) return false; + bool setPointSizeFromHeight = false; switch ( l ) { case 0: - // Fractional point size is not present in this version. - pointSize = 0.0f; + // Fractional point size is not present in this version, it will be + // set from lfHeight below in this case. + setPointSizeFromHeight = true; break; case 1: @@ -685,6 +687,8 @@ bool wxNativeFontInfo::FromString(const wxString& s) if ( !token.ToLong(&l) ) return false; lf.lfHeight = l; + if ( setPointSizeFromHeight ) + pointSize = GetPointSizeFromLogFontHeight(l); token = tokenizer.GetNextToken(); if ( !token.ToLong(&l) )