From 5a6b7681d86d369811efb0c88a0b1db0e85ae579 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 12 Sep 2018 14:46:00 +0200 Subject: [PATCH] Refactor float to int point size conversions once again They will be also needed in wxFontInfo soon, so move them there and use these functions from both wxFont and wxNativeFontInfo, as they can depend on wxFontInfo but not the other way round. No real changes. --- include/wx/font.h | 20 ++++++++++++++++++++ src/common/fontcmn.cpp | 14 ++++---------- src/osx/carbon/font.cpp | 3 ++- 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/include/wx/font.h b/include/wx/font.h index 4dc01f55e1..76eb570015 100644 --- a/include/wx/font.h +++ b/include/wx/font.h @@ -19,6 +19,7 @@ #include "wx/fontenc.h" // the font encoding constants #include "wx/gdiobj.h" // the base class #include "wx/gdicmn.h" // for wxGDIObjListBase +#include "wx/math.h" // for wxRound() // ---------------------------------------------------------------------------- // forward declarations @@ -216,6 +217,25 @@ public: // Default copy ctor, assignment operator and dtor are OK. + + // Helper functions for converting between integer and fractional sizes. + static int ToIntPointSize(float pointSize) { return wxRound(pointSize); } + static float ToFloatPointSize(int pointSize) + { + wxCHECK_MSG( pointSize == -1 || pointSize >= 0, + -1, "Invalid font point size" ); + + // Huge values are not exactly representable as floats, so don't accept + // those neither as they can only be due to a mistake anyhow: nobody + // could possibly need a font of size 16777217pt (which is the first + // value for which this fails). + const float f = static_cast(pointSize); + wxCHECK_MSG( static_cast(f) == pointSize, + -1, "Font point size out of range" ); + + return f; + } + private: void Init() { diff --git a/src/common/fontcmn.cpp b/src/common/fontcmn.cpp index 66451ced9d..998c3c1caa 100644 --- a/src/common/fontcmn.cpp +++ b/src/common/fontcmn.cpp @@ -28,7 +28,6 @@ #ifndef WX_PRECOMP #include "wx/dc.h" #include "wx/intl.h" - #include "wx/math.h" #include "wx/dcscreen.h" #include "wx/log.h" #include "wx/gdicmn.h" @@ -45,8 +44,6 @@ #include "wx/tokenzr.h" -#include // for FLT_MAX - // debugger helper: this function can be called from a debugger to show what // the date really is extern const char *wxDumpFont(const wxFont *font) @@ -283,7 +280,7 @@ wxFontWeight wxFontBase::GetWeightClosestToNumericValue(int numWeight) int wxFontBase::GetPointSize() const { - return wxRound(GetFractionalPointSize()); + return wxFontInfo::ToIntPointSize(GetFractionalPointSize()); } @@ -308,10 +305,7 @@ bool wxFontBase::IsUsingSizeInPixels() const void wxFontBase::SetPointSize(int pointSize) { - wxCHECK_RET( pointSize >= 0 && static_cast(pointSize) < FLT_MAX, - "Invalid font point size" ); - - SetFractionalPointSize(static_cast(pointSize)); + SetFractionalPointSize(wxFontInfo::ToFloatPointSize(pointSize)); } void wxFontBase::SetPixelSize( const wxSize& pixelSize ) @@ -710,12 +704,12 @@ void wxNativeFontInfo::SetFaceName(const wxArrayString& facenames) int wxNativeFontInfo::GetPointSize() const { - return wxRound(GetFractionalPointSize()); + return wxFontInfo::ToIntPointSize(GetFractionalPointSize()); } void wxNativeFontInfo::SetPointSize(int pointsize) { - SetFractionalPointSize(static_cast(pointsize)); + SetFractionalPointSize(wxFontInfo::ToFloatPointSize(pointsize)); } #ifdef wxNO_NATIVE_FONTINFO diff --git a/src/osx/carbon/font.cpp b/src/osx/carbon/font.cpp index abf4df5248..d2e1967eaa 100644 --- a/src/osx/carbon/font.cpp +++ b/src/osx/carbon/font.cpp @@ -578,7 +578,8 @@ bool wxFont::Create(int pointSize, { AccountForCompatValues(pointSize, style, weight); - return Create((float)pointSize, family, style, weight, underlined, faceName, encoding); + return Create(wxFontInfo::ToFloatPointSize(pointSize), + family, style, weight, underlined, faceName, encoding); } wxFont::~wxFont()