From de6d7472a2f5a8d19c5a6135379a731277b5ffce Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 30 Dec 2018 23:53:25 +0100 Subject: [PATCH] Add static GetLogFontHeightAtPPI() overload taking point size This will be useful elsewhere too and makes SetFractionalPointSize() implementation less fragile as we don't have to be careful about changing pointSize member before setting lf.lfHeight any more. --- include/wx/fontutil.h | 11 ++++++++++- src/msw/font.cpp | 27 +++++---------------------- 2 files changed, 15 insertions(+), 23 deletions(-) diff --git a/include/wx/fontutil.h b/include/wx/fontutil.h index f7ba39237e..38f473654e 100644 --- a/include/wx/fontutil.h +++ b/include/wx/fontutil.h @@ -122,7 +122,16 @@ public: // MSW-specific: get the height value in pixels using LOGFONT convention // (i.e. negative) corresponding to the given size in points and DPI. - int GetLogFontHeightAtPPI(int ppi) const; + static int GetLogFontHeightAtPPI(float size, int ppi) + { + return -wxRound(size * ppi / 72.0); + } + + // And the same thing for the size of this font. + int GetLogFontHeightAtPPI(int ppi) const + { + return GetLogFontHeightAtPPI(pointSize, ppi); + } LOGFONT lf; diff --git a/src/msw/font.cpp b/src/msw/font.cpp index aa18cc8bd4..255aafcdae 100644 --- a/src/msw/font.cpp +++ b/src/msw/font.cpp @@ -509,31 +509,14 @@ wxFontEncoding wxNativeFontInfo::GetEncoding() const return wxGetFontEncFromCharSet(lf.lfCharSet); } -int wxNativeFontInfo::GetLogFontHeightAtPPI(int ppi) const -{ - return -wxRound(pointSize * ppi / 72.0); -} - void wxNativeFontInfo::SetFractionalPointSize(float pointSizeNew) { - if ( pointSize == 0.0f ) - { - // Do this before calling GetLogFontHeightAtPPI() below. - pointSize = pointSizeNew; + // We don't have the correct DPI to use here, so use that of the + // primary screen. + const int ppi = ::GetDeviceCaps(ScreenHDC(), LOGPIXELSY); + lf.lfHeight = GetLogFontHeightAtPPI(pointSizeNew, ppi); - // We don't have the correct DPI to use here, so use that of the - // primary screen. - const int ppi = ::GetDeviceCaps(ScreenHDC(), LOGPIXELSY); - lf.lfHeight = GetLogFontHeightAtPPI(ppi); - } - else // Changing the size of a valid font. - { - // Scale the font using the ratio of sizes, to ensure that we use the - // same DPI as before. - lf.lfHeight = -wxRound(abs(lf.lfHeight) * pointSizeNew / pointSize); - - pointSize = pointSizeNew; - } + pointSize = pointSizeNew; } void wxNativeFontInfo::SetPixelSize(const wxSize& pixelSize)