Centralize backwards compatibility code in wxFont

Add wxFontBase::AccountForCompatValues() and use it in all ports instead
of redoing the same comparison with wxDEFAULT in all of them.

This is done not so much to avoid the code duplication, which was
minimal anyhow, but to make the code more clear and make it easier to
remove it from all ports at once in the bright (but remote) future when
we don't need these compatibility hacks any more.

Also document that wxDEFAULT and wxNORMAL are only handled specially in
the old-style ctor taking the individual font components and not the new
one using wxFontInfo and extend the unit test to check this.
This commit is contained in:
Vadim Zeitlin
2018-09-06 02:32:53 +02:00
parent b84db46feb
commit 08e5acedcc
10 changed files with 54 additions and 42 deletions

View File

@@ -492,6 +492,14 @@ protected:
return (flags & wxFONTFLAG_STRIKETHROUGH) != 0;
}
// For compatibility reasons, we continue to accept wxDEFAULT as meaning
// "default font size" and wxNORMAL and similar deprecated constants
// instead of wxFONT{WEIGHT,STYLE}_NORMAL. This function modifies its
// parameters to account for this if necessary.
static void AccountForCompatValues(int& pointSize,
wxFontStyle& style,
wxFontWeight& weight);
private:
// the currently default encoding: by default, it's the default system
// encoding, but may be changed by the application using

View File

@@ -503,7 +503,10 @@ public:
historical reasons, the value 70 here is interpreted at @c
wxDEFAULT and results in creation of the font with the default size
and not of a font with the size of 70pt. If you really need the
latter, please use SetPointSize(70).
latter, please use SetPointSize(70). Note that this constructor and
the matching Create() method overload are the only places in wxFont
API handling @c wxDEFAULT specially: neither SetPointSize() nor the
constructor taking wxFontInfo handle this value in this way.
@param family
The font family: a generic portable way of referring to fonts without specifying a
facename. This parameter must be one of the ::wxFontFamily enumeration values.

View File

@@ -558,6 +558,24 @@ bool wxFontBase::SetFaceName(const wxString& facename)
return true;
}
/* static */
void wxFontBase::AccountForCompatValues(int& pointSize,
wxFontStyle& style,
wxFontWeight& weight)
{
// Old code specifies wxDEFAULT instead of -1 or wxNORMAL instead of the
// new type-safe wxFONTSTYLE_NORMAL or wxFONTWEIGHT_NORMAL, continue
// handling this for compatibility.
if ( pointSize == wxDEFAULT )
pointSize = -1;
if ( static_cast<int>(style) == wxDEFAULT )
style = wxFONTSTYLE_NORMAL;
if ( static_cast<int>(weight) == wxDEFAULT )
weight = wxFONTWEIGHT_NORMAL;
}
void wxFontBase::SetSymbolicSize(wxFontSymbolicSize size)
{
SetSymbolicSizeRelativeTo(size, wxNORMAL_FONT->GetPointSize());

View File

@@ -115,18 +115,6 @@ void wxFontRefData::Init(int pointSize,
const wxString& faceName,
wxFontEncoding WXUNUSED(encoding))
{
// Old code could wrongly specify wxDEFAULT instead of -1 or wxNORMAL or,
// preferably, wxFONTSTYLE_NORMAL or wxFONTWEIGHT_NORMAL, continue handling
// this for compatibility.
if ( pointSize == wxDEFAULT )
pointSize = -1;
if ( static_cast<int>(style) == wxDEFAULT )
style = wxFONTSTYLE_NORMAL;
if ( static_cast<int>(weight) == wxDEFAULT )
weight = wxFONTWEIGHT_NORMAL;
if (family == wxFONTFAMILY_DEFAULT)
family = wxFONTFAMILY_SWISS;
@@ -317,6 +305,8 @@ bool wxFont::Create( int pointSize,
{
UnRef();
AccountForCompatValues(pointSize, style, weight);
m_refData = new wxFontRefData(pointSize, family, style, weight,
underlined, false, face, encoding);

View File

@@ -147,15 +147,10 @@ void wxFontRefData::Init(int pointSize,
m_faceName = faceName;
// we accept both wxDEFAULT and wxNORMAL here - should we?
m_style = style == wxDEFAULT ? wxFONTSTYLE_NORMAL : style;
m_weight = weight == wxDEFAULT ? wxFONTWEIGHT_NORMAL : weight;
m_style = style;
m_weight = weight;
// and here, do we really want to forbid creation of the font of the size
// 90 (the value of wxDEFAULT)??
m_pointSize = pointSize == wxDEFAULT || pointSize == -1
? wxDEFAULT_FONT_SIZE
: pointSize;
m_pointSize = pointSize == -1 ? wxDEFAULT_FONT_SIZE : pointSize;
m_underlined = underlined;
m_encoding = encoding;
@@ -465,6 +460,8 @@ bool wxFont::Create( int pointSize,
{
UnRef();
AccountForCompatValues(pointSize, style, weight);
m_refData = new wxFontRefData(pointSize, family, style, weight,
underlined, face, encoding);

View File

@@ -178,17 +178,10 @@ void wxFontRefData::Init(int pointSize,
m_faceName = faceName;
if (style == wxDEFAULT)
m_style = wxFONTSTYLE_NORMAL;
else
m_style = style;
m_style = style;
m_weight = weight;
if (weight == wxDEFAULT)
m_weight = wxFONTWEIGHT_NORMAL;
else
m_weight = weight;
if (pointSize == wxDEFAULT)
if (pointSize == -1)
m_pointSize = 12;
else
m_pointSize = pointSize;
@@ -229,6 +222,9 @@ bool wxFont::Create(int pointSize,
wxFontEncoding encoding)
{
UnRef();
AccountForCompatValues(pointSize, style, weight);
m_refData = new wxFontRefData(pointSize, family, style, weight,
underlined, faceName, encoding);

View File

@@ -817,12 +817,7 @@ bool wxFont::DoCreate(int pointSize,
{
UnRef();
// wxDEFAULT is a valid value for the font size too so we must treat it
// specially here (otherwise the size would be 70 == wxDEFAULT value)
if ( pointSize == wxDEFAULT )
{
pointSize = wxNORMAL_FONT->GetPointSize();
}
AccountForCompatValues(pointSize, style, weight);
m_refData = new wxFontRefData(pointSize, pixelSize, sizeUsingPixels,
family, style, weight,

View File

@@ -575,10 +575,7 @@ bool wxFont::Create(int pointSize,
const wxString& faceName,
wxFontEncoding encoding)
{
// wxDEFAULT is a valid value for the font size too so we must treat it
// specially here (otherwise the size would be 70 == wxDEFAULT value)
if (pointSize == wxDEFAULT)
pointSize = -1;
AccountForCompatValues(pointSize, style, weight);
return Create((float)pointSize, family, style, weight, underlined, faceName, encoding);
}

View File

@@ -187,6 +187,9 @@ bool wxFont::Create(wxSize size, wxFontFamily family, wxFontStyle style,
wxFontWeight weight, bool underlined, const wxString& face,
wxFontEncoding WXUNUSED(encoding) )
{
int pointSize = size.GetHeight();
AccountForCompatValues(pointSize, style, weight);
if (!face.empty())
M_FONTDATA.SetFaceName(face);
else
@@ -195,7 +198,7 @@ bool wxFont::Create(wxSize size, wxFontFamily family, wxFontStyle style,
M_FONTDATA.SetStyle(style);
M_FONTDATA.SetWeight(weight);
M_FONTDATA.SetUnderlined(underlined);
M_FONTDATA.SetPointSize(size.GetHeight());
M_FONTDATA.SetPointSize(pointSize);
return true;
}

View File

@@ -143,6 +143,11 @@ TEST_CASE("wxFont::Size", "[font][size]")
", expected = " << size.expected);
CHECK( font.GetPointSize() == expected );
}
// Note that the compatibility hacks only apply to the old ctors, the newer
// one, taking wxFontInfo, doesn't support them.
CHECK( wxFont(wxFontInfo(70)).GetPointSize() == 70 );
CHECK( wxFont(wxFontInfo(90)).GetPointSize() == 90 );
}
TEST_CASE("wxFont::Style", "[font][style]")