Add wxFontInfo ctor taking fractional font size in points

And implement support for it in the major ports.

Also add wxFontInfo::GetFractionalPointSize().
This commit is contained in:
Vadim Zeitlin
2018-09-12 17:50:06 +02:00
parent 2823e289d8
commit 27434db911
5 changed files with 51 additions and 27 deletions

View File

@@ -129,14 +129,23 @@ public:
// Default ctor uses the default font size appropriate for the current // Default ctor uses the default font size appropriate for the current
// platform. // platform.
wxFontInfo() wxFontInfo()
{ InitPointSize(-1); } { InitPointSize(-1.0f); }
// These ctors specify the font size, either in points or in pixels. // These ctors specify the font size, either in points or in pixels.
explicit wxFontInfo(int pointSize) // Point size is a floating point number, however we also accept all
// integer sizes using the simple template ctor below.
explicit wxFontInfo(float pointSize)
{ InitPointSize(pointSize); } { InitPointSize(pointSize); }
explicit wxFontInfo(const wxSize& pixelSize) : m_pixelSize(pixelSize) explicit wxFontInfo(const wxSize& pixelSize) : m_pixelSize(pixelSize)
{ Init(); } { Init(); }
// Need to define this one to avoid casting double to int too.
explicit wxFontInfo(double pointSize)
{ InitPointSize(pointSize); }
template <typename T>
explicit wxFontInfo(T pointSize)
{ InitPointSize(ToFloatPointSize(pointSize)); }
// Setters for the various attributes. All of them return the object itself // Setters for the various attributes. All of them return the object itself
// so that the calls to them could be chained. // so that the calls to them could be chained.
wxFontInfo& Family(wxFontFamily family) wxFontInfo& Family(wxFontFamily family)
@@ -174,7 +183,8 @@ public:
// various pieces of the font description. // various pieces of the font description.
bool IsUsingSizeInPixels() const { return m_pixelSize != wxDefaultSize; } bool IsUsingSizeInPixels() const { return m_pixelSize != wxDefaultSize; }
int GetPointSize() const { return m_pointSize; } float GetFractionalPointSize() const { return m_pointSize; }
int GetPointSize() const { return ToIntPointSize(m_pointSize); }
wxSize GetPixelSize() const { return m_pixelSize; } wxSize GetPixelSize() const { return m_pixelSize; }
wxFontFamily GetFamily() const { return m_family; } wxFontFamily GetFamily() const { return m_family; }
const wxString& GetFaceName() const { return m_faceName; } const wxString& GetFaceName() const { return m_faceName; }
@@ -245,7 +255,7 @@ private:
m_encoding = wxFONTENCODING_DEFAULT; m_encoding = wxFONTENCODING_DEFAULT;
} }
void InitPointSize(int pointSize) void InitPointSize(float pointSize)
{ {
Init(); Init();
@@ -264,10 +274,9 @@ private:
} }
// The size information: if m_pixelSize is valid (!= wxDefaultSize), then // The size information: if m_pixelSize is valid (!= wxDefaultSize), then
// it is used. Otherwise m_pointSize is used, taking into account that if // it is used. Otherwise m_pointSize is used, except if it is < 0, which
// it is == -1, it means that the platform dependent font size should be // means that the platform dependent font size should be used instead.
// used. float m_pointSize;
int m_pointSize;
wxSize m_pixelSize; wxSize m_pixelSize;
wxFontFamily m_family; wxFontFamily m_family;

View File

@@ -38,7 +38,7 @@ public:
wxFont(const wxFontInfo& info) wxFont(const wxFontInfo& info)
{ {
Create(info.GetPointSize(), Create(info.GetFractionalPointSize(),
info.GetFamily(), info.GetFamily(),
info.GetStyle(), info.GetStyle(),
info.GetWeight(), info.GetWeight(),

View File

@@ -322,9 +322,16 @@ public:
/** /**
Constructor setting the font size in points to use. Constructor setting the font size in points to use.
The canonical type of @a pointSize argument is @c float, however any
other integer type, as well as @c double, is also accepted for
compatibility.
Notice that until wxWidgets 3.1.2, the type could only be @c int.
@see wxFont::SetPointSize() @see wxFont::SetPointSize()
*/ */
explicit wxFontInfo(int pointSize); template <typename T>
explicit wxFontInfo(T pointSize);
/** /**
Constructor setting the font size in pixels to use. Constructor setting the font size in pixels to use.

View File

@@ -47,7 +47,7 @@ class wxFontRefData : public wxGDIRefData
{ {
public: public:
// from broken down font parameters, also default ctor // from broken down font parameters, also default ctor
wxFontRefData(int size = -1, wxFontRefData(float size = -1.0f,
wxFontFamily family = wxFONTFAMILY_DEFAULT, wxFontFamily family = wxFONTFAMILY_DEFAULT,
wxFontStyle style = wxFONTSTYLE_NORMAL, wxFontStyle style = wxFONTSTYLE_NORMAL,
wxFontWeight weight = wxFONTWEIGHT_NORMAL, wxFontWeight weight = wxFONTWEIGHT_NORMAL,
@@ -80,7 +80,7 @@ public:
protected: protected:
// common part of all ctors // common part of all ctors
void Init(int pointSize, void Init(float pointSize,
wxFontFamily family, wxFontFamily family,
wxFontStyle style, wxFontStyle style,
wxFontWeight weight, wxFontWeight weight,
@@ -106,7 +106,7 @@ private:
// wxFontRefData // wxFontRefData
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
void wxFontRefData::Init(int pointSize, void wxFontRefData::Init(float pointSize,
wxFontFamily family, wxFontFamily family,
wxFontStyle style, wxFontStyle style,
wxFontWeight weight, wxFontWeight weight,
@@ -133,8 +133,11 @@ void wxFontRefData::Init(int pointSize,
} }
SetStyle( style ); SetStyle( style );
m_nativeFontInfo.SetPointSize( pointSize == -1 ? wxDEFAULT_FONT_SIZE m_nativeFontInfo.SetFractionalPointSize
: pointSize ); (
pointSize < 0 ? static_cast<float>(wxDEFAULT_FONT_SIZE)
: pointSize
);
SetWeight( weight ); SetWeight( weight );
SetUnderlined( underlined ); SetUnderlined( underlined );
SetStrikethrough( strikethrough ); SetStrikethrough( strikethrough );
@@ -157,7 +160,7 @@ wxFontRefData::wxFontRefData( const wxFontRefData& data )
{ {
} }
wxFontRefData::wxFontRefData(int size, wxFontFamily family, wxFontStyle style, wxFontRefData::wxFontRefData(float size, wxFontFamily family, wxFontStyle style,
wxFontWeight weight, bool underlined, bool strikethrough, wxFontWeight weight, bool underlined, bool strikethrough,
const wxString& faceName, const wxString& faceName,
wxFontEncoding encoding) wxFontEncoding encoding)
@@ -282,7 +285,7 @@ wxFont::wxFont(const wxNativeFontInfo& info)
wxFont::wxFont(const wxFontInfo& info) wxFont::wxFont(const wxFontInfo& info)
{ {
m_refData = new wxFontRefData(info.GetPointSize(), m_refData = new wxFontRefData(info.GetFractionalPointSize(),
info.GetFamily(), info.GetFamily(),
info.GetStyle(), info.GetStyle(),
info.GetWeight(), info.GetWeight(),
@@ -308,7 +311,8 @@ bool wxFont::Create( int pointSize,
AccountForCompatValues(pointSize, style, weight); AccountForCompatValues(pointSize, style, weight);
m_refData = new wxFontRefData(pointSize, family, style, weight, m_refData = new wxFontRefData(wxFontInfo::ToFloatPointSize(pointSize),
family, style, weight,
underlined, false, face, encoding); underlined, false, face, encoding);
return true; return true;

View File

@@ -60,12 +60,12 @@ public:
// constructors // constructors
wxFontRefData() wxFontRefData()
{ {
Init(-1, wxSize(0,0), false, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, Init(-1.0f, wxSize(0,0), false, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL,
wxFONTWEIGHT_NORMAL, false, false, wxEmptyString, wxFONTWEIGHT_NORMAL, false, false, wxEmptyString,
wxFONTENCODING_DEFAULT); wxFONTENCODING_DEFAULT);
} }
wxFontRefData(int size, wxFontRefData(float size,
const wxSize& pixelSize, const wxSize& pixelSize,
bool sizeUsingPixels, bool sizeUsingPixels,
wxFontFamily family, wxFontFamily family,
@@ -267,7 +267,7 @@ public:
protected: protected:
// common part of all ctors // common part of all ctors
void Init(int size, void Init(float size,
const wxSize& pixelSize, const wxSize& pixelSize,
bool sizeUsingPixels, bool sizeUsingPixels,
wxFontFamily family, wxFontFamily family,
@@ -342,7 +342,7 @@ protected:
// wxFontRefData // wxFontRefData
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
void wxFontRefData::Init(int pointSize, void wxFontRefData::Init(float pointSize,
const wxSize& pixelSize, const wxSize& pixelSize,
bool sizeUsingPixels, bool sizeUsingPixels,
wxFontFamily family, wxFontFamily family,
@@ -362,9 +362,12 @@ void wxFontRefData::Init(int pointSize,
} }
else else
{ {
m_nativeFontInfo.SetPointSize(pointSize == -1 m_nativeFontInfo.SetFractionalPointSize
? wxNORMAL_FONT->GetPointSize() (
: pointSize); pointSize < 0
? wxNORMAL_FONT->GetPointSize()
: pointSize
);
} }
SetStyle(style); SetStyle(style);
@@ -793,7 +796,7 @@ wxFont::wxFont(const wxString& fontdesc)
wxFont::wxFont(const wxFontInfo& info) wxFont::wxFont(const wxFontInfo& info)
{ {
m_refData = new wxFontRefData(info.GetPointSize(), m_refData = new wxFontRefData(info.GetFractionalPointSize(),
info.GetPixelSize(), info.GetPixelSize(),
info.IsUsingSizeInPixels(), info.IsUsingSizeInPixels(),
info.GetFamily(), info.GetFamily(),
@@ -828,7 +831,8 @@ bool wxFont::DoCreate(int pointSize,
AccountForCompatValues(pointSize, style, weight); AccountForCompatValues(pointSize, style, weight);
m_refData = new wxFontRefData(pointSize, pixelSize, sizeUsingPixels, m_refData = new wxFontRefData(wxFontInfo::ToFloatPointSize(pointSize),
pixelSize, sizeUsingPixels,
family, style, weight, family, style, weight,
underlined, false, faceName, encoding); underlined, false, faceName, encoding);