From c98879e3796905fd2d6b3058b83383a0ad4adfe9 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Thu, 6 Sep 2018 01:40:47 +0200 Subject: [PATCH] Add wxFont::SetFractionalPointSize() Changing SetPointSize() argument type from int to float wasn't 100% backwards-compatible as it notably started resulting in warnings (from at least MSVC) about conversions from int to float in the existing code. To avoid these warnings and for symmetry with GetFractionalPointSize(), add SetFractionalPointSize() taking float argument and preserve the argument of type int in SetPointSize() for compatibility. SetPointSize() is now just a wrapper forwarding to the more general SetFractionalPointSize(). Notice that the other ports still remain broken, this commit only updates the currently working wxGTK, wxMac and wxMSW. --- include/wx/font.h | 3 ++- include/wx/gtk/font.h | 2 +- include/wx/msw/font.h | 2 +- include/wx/osx/font.h | 2 +- interface/wx/font.h | 20 +++++++++++++++----- src/common/fontcmn.cpp | 10 ++++++++++ src/gtk/font.cpp | 11 ++++++----- src/msw/font.cpp | 16 +++++++++++----- src/osx/carbon/font.cpp | 12 +++++------- 9 files changed, 52 insertions(+), 26 deletions(-) diff --git a/include/wx/font.h b/include/wx/font.h index c732f45ca4..5f9f70b2d0 100644 --- a/include/wx/font.h +++ b/include/wx/font.h @@ -368,7 +368,8 @@ public: wxString GetNativeFontInfoUserDesc() const; // change the font characteristics - virtual void SetPointSize( float pointSize ) = 0; + virtual void SetPointSize( int pointSize ); + virtual void SetFractionalPointSize( float pointSize ) = 0; virtual void SetPixelSize( const wxSize& pixelSize ); virtual void SetFamily( wxFontFamily family ) = 0; virtual void SetStyle( wxFontStyle style ) = 0; diff --git a/include/wx/gtk/font.h b/include/wx/gtk/font.h index 149be51b84..51076e4b12 100644 --- a/include/wx/gtk/font.h +++ b/include/wx/gtk/font.h @@ -74,7 +74,7 @@ public: virtual const wxNativeFontInfo *GetNativeFontInfo() const wxOVERRIDE; virtual bool IsFixedWidth() const wxOVERRIDE; - virtual void SetPointSize(float pointSize) wxOVERRIDE; + virtual void SetFractionalPointSize(float pointSize) wxOVERRIDE; virtual void SetFamily(wxFontFamily family) wxOVERRIDE; virtual void SetStyle(wxFontStyle style) wxOVERRIDE; virtual void SetNumericWeight(int weight) wxOVERRIDE; diff --git a/include/wx/msw/font.h b/include/wx/msw/font.h index 000d840c32..044b6e2c8e 100644 --- a/include/wx/msw/font.h +++ b/include/wx/msw/font.h @@ -96,7 +96,7 @@ public: virtual wxFontEncoding GetEncoding() const wxOVERRIDE; virtual const wxNativeFontInfo *GetNativeFontInfo() const wxOVERRIDE; - virtual void SetPointSize(float pointSize) wxOVERRIDE; + virtual void SetFractionalPointSize(float pointSize) wxOVERRIDE; virtual void SetPixelSize(const wxSize& pixelSize) wxOVERRIDE; virtual void SetFamily(wxFontFamily family) wxOVERRIDE; virtual void SetStyle(wxFontStyle style) wxOVERRIDE; diff --git a/include/wx/osx/font.h b/include/wx/osx/font.h index 2494d78604..593d2c3f7e 100644 --- a/include/wx/osx/font.h +++ b/include/wx/osx/font.h @@ -131,7 +131,7 @@ public: virtual bool IsFixedWidth() const wxOVERRIDE; - virtual void SetPointSize(float pointSize) wxOVERRIDE; + virtual void SetFractionalPointSize(float pointSize) wxOVERRIDE; virtual void SetFamily(wxFontFamily family) wxOVERRIDE; virtual void SetStyle(wxFontStyle style) wxOVERRIDE; virtual void SetNumericWeight(int weight) wxOVERRIDE; diff --git a/interface/wx/font.h b/interface/wx/font.h index 1ada127136..f36d2a7de8 100644 --- a/interface/wx/font.h +++ b/interface/wx/font.h @@ -1062,19 +1062,29 @@ public: void SetNativeFontInfo(const wxNativeFontInfo& info); /** - Sets the point size. + Sets the font size in points to an integer value. + + This is a legacy version of the function only supporting integer point + sizes. It can still be used, but to avoid unnecessarily restricting the + font size in points to integer values, consider using the new (added in + wxWidgets 3.1.2) SetFractionalPointSize() function instead. + */ + virtual void SetPointSize(int pointSize); + + /** + Sets the font size in points. The point size is defined as 1/72 of the Anglo-Saxon inch (25.4 mm): it is approximately 0.0139 inch or 352.8 um. @param pointSize Size in points. This can also be a fractional point size like 11.5. - Note that until wxWidgets 3.1.2, the size had to be an integer number - (and the type of this parameter was @c int). - @see GetPointSize() + @see GetFractionalPointSize(), SetPointSize() + + @since 3.1.2 */ - virtual void SetPointSize(float pointSize); + virtual void SetFractionalPointSize(float pointSize); /** Sets the pixel size. diff --git a/src/common/fontcmn.cpp b/src/common/fontcmn.cpp index 66db8ebb47..14d1c2bc31 100644 --- a/src/common/fontcmn.cpp +++ b/src/common/fontcmn.cpp @@ -45,6 +45,8 @@ #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) @@ -306,6 +308,14 @@ bool wxFontBase::IsUsingSizeInPixels() const return false; } +void wxFontBase::SetPointSize(int pointSize) +{ + wxCHECK_RET( pointSize >= 0 && static_cast(pointSize) < FLT_MAX, + "Invalid font point size" ); + + SetFractionalPointSize(static_cast(pointSize)); +} + void wxFontBase::SetPixelSize( const wxSize& pixelSize ) { wxCHECK_RET( pixelSize.GetWidth() >= 0 && pixelSize.GetHeight() > 0, diff --git a/src/gtk/font.cpp b/src/gtk/font.cpp index b4fe2a4d6e..2082bf2614 100644 --- a/src/gtk/font.cpp +++ b/src/gtk/font.cpp @@ -65,7 +65,7 @@ public: // setters: all of them also take care to modify m_nativeFontInfo if we // have it so as to not lose the information not carried by our fields - void SetPointSize(float pointSize); + void SetFractionalPointSize(float pointSize); void SetFamily(wxFontFamily family); void SetStyle(wxFontStyle style); void SetWeight(wxFontWeight weight); @@ -133,7 +133,8 @@ void wxFontRefData::Init(int pointSize, } SetStyle( style ); - SetPointSize( pointSize == -1 ? wxDEFAULT_FONT_SIZE : pointSize ); + m_nativeFontInfo.SetPointSize( pointSize == -1 ? wxDEFAULT_FONT_SIZE + : pointSize ); SetWeight( weight ); SetUnderlined( underlined ); SetStrikethrough( strikethrough ); @@ -179,7 +180,7 @@ wxFontRefData::~wxFontRefData() // wxFontRefData SetXXX() // ---------------------------------------------------------------------------- -void wxFontRefData::SetPointSize(float pointSize) +void wxFontRefData::SetFractionalPointSize(float pointSize) { m_nativeFontInfo.SetFractionalPointSize(pointSize); } @@ -409,11 +410,11 @@ bool wxFont::IsFixedWidth() const // change font attributes // ---------------------------------------------------------------------------- -void wxFont::SetPointSize(float pointSize) +void wxFont::SetFractionalPointSize(float pointSize) { AllocExclusive(); - M_FONTDATA->SetPointSize(pointSize); + M_FONTDATA->SetFractionalPointSize(pointSize); } void wxFont::SetFamily(wxFontFamily family) diff --git a/src/msw/font.cpp b/src/msw/font.cpp index 6702f97851..f8ae3aaabb 100644 --- a/src/msw/font.cpp +++ b/src/msw/font.cpp @@ -176,7 +176,7 @@ public: // ... and setters: notice that all of them invalidate the currently // allocated HFONT, if any, so that the next call to GetHFONT() recreates a // new one - void SetPointSize(float pointSize) + void SetFractionalPointSize(float pointSize) { Free(); @@ -357,9 +357,15 @@ void wxFontRefData::Init(int pointSize, m_sizeUsingPixels = sizeUsingPixels; if ( m_sizeUsingPixels ) - SetPixelSize(pixelSize); + { + m_nativeFontInfo.SetPixelSize(pixelSize); + } else - SetPointSize(pointSize == -1 ? wxNORMAL_FONT->GetPointSize() : pointSize); + { + m_nativeFontInfo.SetPointSize(pointSize == -1 + ? wxNORMAL_FONT->GetPointSize() + : pointSize); + } SetStyle(style); m_nativeFontInfo.SetWeight(weight); @@ -877,12 +883,12 @@ bool wxFont::IsFree() const // change font attribute: we recreate font when doing it // ---------------------------------------------------------------------------- -void wxFont::SetPointSize(float pointSize) +void wxFont::SetFractionalPointSize(float pointSize) { AllocExclusive(); M_FONTDATA->Free(); - M_FONTDATA->SetPointSize(pointSize); + M_FONTDATA->SetFractionalPointSize(pointSize); } void wxFont::SetPixelSize(const wxSize& pixelSize) diff --git a/src/osx/carbon/font.cpp b/src/osx/carbon/font.cpp index d813e57335..abf4df5248 100644 --- a/src/osx/carbon/font.cpp +++ b/src/osx/carbon/font.cpp @@ -92,7 +92,7 @@ public: const wxNativeFontInfo& GetNativeFontInfo() const; - void SetPointSize(float size) + void SetFractionalPointSize(float size) { if (GetFractionalPointSize() != size) { @@ -300,7 +300,8 @@ void wxFontRefData::Init(float size, SetFaceName(faceName); else SetFamily(family); - SetPointSize(size < 0 ? wxNORMAL_FONT->GetFractionalPointSize() : size); + + SetFractionalPointSize(size < 0 ? wxNORMAL_FONT->GetFractionalPointSize() : size); SetNumericWeight(weight); SetStyle(style); SetUnderlined(underlined); @@ -613,14 +614,11 @@ wxGDIRefData* wxFont::CloneGDIRefData(const wxGDIRefData* data) const return new wxFontRefData(*static_cast(data)); } -void wxFont::SetPointSize(float pointSize) +void wxFont::SetFractionalPointSize(float pointSize) { - if (IsOk() && M_FONTDATA->GetFractionalPointSize() == pointSize) - return; - AllocExclusive(); - M_FONTDATA->SetPointSize(pointSize); + M_FONTDATA->SetFractionalPointSize(pointSize); } void wxFont::SetFamily(wxFontFamily family)