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

@@ -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;
}