From 08e5acedcc6acb74e67fc16391979871f135c30b Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Thu, 6 Sep 2018 02:32:53 +0200 Subject: [PATCH] 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. --- include/wx/font.h | 8 ++++++++ interface/wx/font.h | 5 ++++- src/common/fontcmn.cpp | 18 ++++++++++++++++++ src/gtk/font.cpp | 14 ++------------ src/gtk1/font.cpp | 13 +++++-------- src/motif/font.cpp | 16 ++++++---------- src/msw/font.cpp | 7 +------ src/osx/carbon/font.cpp | 5 +---- src/qt/font.cpp | 5 ++++- tests/font/fonttest.cpp | 5 +++++ 10 files changed, 54 insertions(+), 42 deletions(-) diff --git a/include/wx/font.h b/include/wx/font.h index d325e5c959..c732f45ca4 100644 --- a/include/wx/font.h +++ b/include/wx/font.h @@ -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 diff --git a/interface/wx/font.h b/interface/wx/font.h index acbb5d0500..1ada127136 100644 --- a/interface/wx/font.h +++ b/interface/wx/font.h @@ -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. diff --git a/src/common/fontcmn.cpp b/src/common/fontcmn.cpp index 5c168279d6..a18b9f6f07 100644 --- a/src/common/fontcmn.cpp +++ b/src/common/fontcmn.cpp @@ -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(style) == wxDEFAULT ) + style = wxFONTSTYLE_NORMAL; + + if ( static_cast(weight) == wxDEFAULT ) + weight = wxFONTWEIGHT_NORMAL; +} + void wxFontBase::SetSymbolicSize(wxFontSymbolicSize size) { SetSymbolicSizeRelativeTo(size, wxNORMAL_FONT->GetPointSize()); diff --git a/src/gtk/font.cpp b/src/gtk/font.cpp index a3d3c5b1dd..5883d46815 100644 --- a/src/gtk/font.cpp +++ b/src/gtk/font.cpp @@ -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(style) == wxDEFAULT ) - style = wxFONTSTYLE_NORMAL; - - if ( static_cast(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); diff --git a/src/gtk1/font.cpp b/src/gtk1/font.cpp index 57ba87c846..4fbfe8c92a 100644 --- a/src/gtk1/font.cpp +++ b/src/gtk1/font.cpp @@ -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); diff --git a/src/motif/font.cpp b/src/motif/font.cpp index 8558a4ed97..76c63d3c9c 100644 --- a/src/motif/font.cpp +++ b/src/motif/font.cpp @@ -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); diff --git a/src/msw/font.cpp b/src/msw/font.cpp index dc7a8c723a..efedb1d0d7 100644 --- a/src/msw/font.cpp +++ b/src/msw/font.cpp @@ -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, diff --git a/src/osx/carbon/font.cpp b/src/osx/carbon/font.cpp index cc680674e9..5ebef216cf 100644 --- a/src/osx/carbon/font.cpp +++ b/src/osx/carbon/font.cpp @@ -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); } diff --git a/src/qt/font.cpp b/src/qt/font.cpp index cd01b207ec..dc467ea0b7 100644 --- a/src/qt/font.cpp +++ b/src/qt/font.cpp @@ -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; } diff --git a/tests/font/fonttest.cpp b/tests/font/fonttest.cpp index 620041b3d7..480278c2b5 100644 --- a/tests/font/fonttest.cpp +++ b/tests/font/fonttest.cpp @@ -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]")