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.
This commit is contained in:
Vadim Zeitlin
2018-09-06 01:40:47 +02:00
parent e05a732666
commit c98879e379
9 changed files with 52 additions and 26 deletions

View File

@@ -368,7 +368,8 @@ public:
wxString GetNativeFontInfoUserDesc() const; wxString GetNativeFontInfoUserDesc() const;
// change the font characteristics // 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 SetPixelSize( const wxSize& pixelSize );
virtual void SetFamily( wxFontFamily family ) = 0; virtual void SetFamily( wxFontFamily family ) = 0;
virtual void SetStyle( wxFontStyle style ) = 0; virtual void SetStyle( wxFontStyle style ) = 0;

View File

@@ -74,7 +74,7 @@ public:
virtual const wxNativeFontInfo *GetNativeFontInfo() const wxOVERRIDE; virtual const wxNativeFontInfo *GetNativeFontInfo() const wxOVERRIDE;
virtual bool IsFixedWidth() 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 SetFamily(wxFontFamily family) wxOVERRIDE;
virtual void SetStyle(wxFontStyle style) wxOVERRIDE; virtual void SetStyle(wxFontStyle style) wxOVERRIDE;
virtual void SetNumericWeight(int weight) wxOVERRIDE; virtual void SetNumericWeight(int weight) wxOVERRIDE;

View File

@@ -96,7 +96,7 @@ public:
virtual wxFontEncoding GetEncoding() const wxOVERRIDE; virtual wxFontEncoding GetEncoding() const wxOVERRIDE;
virtual const wxNativeFontInfo *GetNativeFontInfo() 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 SetPixelSize(const wxSize& pixelSize) wxOVERRIDE;
virtual void SetFamily(wxFontFamily family) wxOVERRIDE; virtual void SetFamily(wxFontFamily family) wxOVERRIDE;
virtual void SetStyle(wxFontStyle style) wxOVERRIDE; virtual void SetStyle(wxFontStyle style) wxOVERRIDE;

View File

@@ -131,7 +131,7 @@ public:
virtual bool IsFixedWidth() 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 SetFamily(wxFontFamily family) wxOVERRIDE;
virtual void SetStyle(wxFontStyle style) wxOVERRIDE; virtual void SetStyle(wxFontStyle style) wxOVERRIDE;
virtual void SetNumericWeight(int weight) wxOVERRIDE; virtual void SetNumericWeight(int weight) wxOVERRIDE;

View File

@@ -1062,19 +1062,29 @@ public:
void SetNativeFontInfo(const wxNativeFontInfo& info); 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 <em>point size</em> is defined as 1/72 of the Anglo-Saxon inch The <em>point size</em> is defined as 1/72 of the Anglo-Saxon inch
(25.4 mm): it is approximately 0.0139 inch or 352.8 um. (25.4 mm): it is approximately 0.0139 inch or 352.8 um.
@param pointSize @param pointSize
Size in points. This can also be a fractional point size like 11.5. 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. Sets the pixel size.

View File

@@ -45,6 +45,8 @@
#include "wx/tokenzr.h" #include "wx/tokenzr.h"
#include <float.h> // for FLT_MAX
// debugger helper: this function can be called from a debugger to show what // debugger helper: this function can be called from a debugger to show what
// the date really is // the date really is
extern const char *wxDumpFont(const wxFont *font) extern const char *wxDumpFont(const wxFont *font)
@@ -306,6 +308,14 @@ bool wxFontBase::IsUsingSizeInPixels() const
return false; return false;
} }
void wxFontBase::SetPointSize(int pointSize)
{
wxCHECK_RET( pointSize >= 0 && static_cast<float>(pointSize) < FLT_MAX,
"Invalid font point size" );
SetFractionalPointSize(static_cast<float>(pointSize));
}
void wxFontBase::SetPixelSize( const wxSize& pixelSize ) void wxFontBase::SetPixelSize( const wxSize& pixelSize )
{ {
wxCHECK_RET( pixelSize.GetWidth() >= 0 && pixelSize.GetHeight() > 0, wxCHECK_RET( pixelSize.GetWidth() >= 0 && pixelSize.GetHeight() > 0,

View File

@@ -65,7 +65,7 @@ public:
// setters: all of them also take care to modify m_nativeFontInfo if we // 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 // 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 SetFamily(wxFontFamily family);
void SetStyle(wxFontStyle style); void SetStyle(wxFontStyle style);
void SetWeight(wxFontWeight weight); void SetWeight(wxFontWeight weight);
@@ -133,7 +133,8 @@ void wxFontRefData::Init(int pointSize,
} }
SetStyle( style ); SetStyle( style );
SetPointSize( pointSize == -1 ? wxDEFAULT_FONT_SIZE : pointSize ); m_nativeFontInfo.SetPointSize( pointSize == -1 ? wxDEFAULT_FONT_SIZE
: pointSize );
SetWeight( weight ); SetWeight( weight );
SetUnderlined( underlined ); SetUnderlined( underlined );
SetStrikethrough( strikethrough ); SetStrikethrough( strikethrough );
@@ -179,7 +180,7 @@ wxFontRefData::~wxFontRefData()
// wxFontRefData SetXXX() // wxFontRefData SetXXX()
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
void wxFontRefData::SetPointSize(float pointSize) void wxFontRefData::SetFractionalPointSize(float pointSize)
{ {
m_nativeFontInfo.SetFractionalPointSize(pointSize); m_nativeFontInfo.SetFractionalPointSize(pointSize);
} }
@@ -409,11 +410,11 @@ bool wxFont::IsFixedWidth() const
// change font attributes // change font attributes
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
void wxFont::SetPointSize(float pointSize) void wxFont::SetFractionalPointSize(float pointSize)
{ {
AllocExclusive(); AllocExclusive();
M_FONTDATA->SetPointSize(pointSize); M_FONTDATA->SetFractionalPointSize(pointSize);
} }
void wxFont::SetFamily(wxFontFamily family) void wxFont::SetFamily(wxFontFamily family)

View File

@@ -176,7 +176,7 @@ public:
// ... and setters: notice that all of them invalidate the currently // ... and setters: notice that all of them invalidate the currently
// allocated HFONT, if any, so that the next call to GetHFONT() recreates a // allocated HFONT, if any, so that the next call to GetHFONT() recreates a
// new one // new one
void SetPointSize(float pointSize) void SetFractionalPointSize(float pointSize)
{ {
Free(); Free();
@@ -357,9 +357,15 @@ void wxFontRefData::Init(int pointSize,
m_sizeUsingPixels = sizeUsingPixels; m_sizeUsingPixels = sizeUsingPixels;
if ( m_sizeUsingPixels ) if ( m_sizeUsingPixels )
SetPixelSize(pixelSize); {
m_nativeFontInfo.SetPixelSize(pixelSize);
}
else else
SetPointSize(pointSize == -1 ? wxNORMAL_FONT->GetPointSize() : pointSize); {
m_nativeFontInfo.SetPointSize(pointSize == -1
? wxNORMAL_FONT->GetPointSize()
: pointSize);
}
SetStyle(style); SetStyle(style);
m_nativeFontInfo.SetWeight(weight); m_nativeFontInfo.SetWeight(weight);
@@ -877,12 +883,12 @@ bool wxFont::IsFree() const
// change font attribute: we recreate font when doing it // change font attribute: we recreate font when doing it
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
void wxFont::SetPointSize(float pointSize) void wxFont::SetFractionalPointSize(float pointSize)
{ {
AllocExclusive(); AllocExclusive();
M_FONTDATA->Free(); M_FONTDATA->Free();
M_FONTDATA->SetPointSize(pointSize); M_FONTDATA->SetFractionalPointSize(pointSize);
} }
void wxFont::SetPixelSize(const wxSize& pixelSize) void wxFont::SetPixelSize(const wxSize& pixelSize)

View File

@@ -92,7 +92,7 @@ public:
const wxNativeFontInfo& GetNativeFontInfo() const; const wxNativeFontInfo& GetNativeFontInfo() const;
void SetPointSize(float size) void SetFractionalPointSize(float size)
{ {
if (GetFractionalPointSize() != size) if (GetFractionalPointSize() != size)
{ {
@@ -300,7 +300,8 @@ void wxFontRefData::Init(float size,
SetFaceName(faceName); SetFaceName(faceName);
else else
SetFamily(family); SetFamily(family);
SetPointSize(size < 0 ? wxNORMAL_FONT->GetFractionalPointSize() : size);
SetFractionalPointSize(size < 0 ? wxNORMAL_FONT->GetFractionalPointSize() : size);
SetNumericWeight(weight); SetNumericWeight(weight);
SetStyle(style); SetStyle(style);
SetUnderlined(underlined); SetUnderlined(underlined);
@@ -613,14 +614,11 @@ wxGDIRefData* wxFont::CloneGDIRefData(const wxGDIRefData* data) const
return new wxFontRefData(*static_cast<const wxFontRefData*>(data)); return new wxFontRefData(*static_cast<const wxFontRefData*>(data));
} }
void wxFont::SetPointSize(float pointSize) void wxFont::SetFractionalPointSize(float pointSize)
{ {
if (IsOk() && M_FONTDATA->GetFractionalPointSize() == pointSize)
return;
AllocExclusive(); AllocExclusive();
M_FONTDATA->SetPointSize(pointSize); M_FONTDATA->SetFractionalPointSize(pointSize);
} }
void wxFont::SetFamily(wxFontFamily family) void wxFont::SetFamily(wxFontFamily family)