From dd306cac774eb252f05c60f485b8b7dce484df3f Mon Sep 17 00:00:00 2001 From: chris2oph Date: Wed, 9 Jan 2019 11:17:23 +0000 Subject: [PATCH] Various wxFont fixes in wxQt Implement point/pixel size accessors correctly. Implement support for strike-through fonts. Also implement DoSetNativeFontInfo() for wxQt. Make wxFont unit test pass by accounting for Qt-specific aspects. Closes https://github.com/wxWidgets/wxWidgets/pull/1113 --- include/wx/qt/font.h | 6 +++++ src/common/fontcmn.cpp | 3 +-- src/qt/font.cpp | 49 ++++++++++++++++++++++++++++++++++++++++- tests/font/fonttest.cpp | 13 ++++++++++- 4 files changed, 67 insertions(+), 4 deletions(-) diff --git a/include/wx/qt/font.h b/include/wx/qt/font.h index dd06d24356..44354e8f52 100644 --- a/include/wx/qt/font.h +++ b/include/wx/qt/font.h @@ -50,21 +50,26 @@ public: wxFontEncoding encoding = wxFONTENCODING_DEFAULT); // accessors: get the font characteristics + virtual int GetPointSize() const wxOVERRIDE; virtual float GetFractionalPointSize() const wxOVERRIDE; + virtual wxSize GetPixelSize() const wxOVERRIDE; virtual wxFontStyle GetStyle() const; virtual int GetNumericWeight() const wxOVERRIDE; virtual bool GetUnderlined() const; virtual wxString GetFaceName() const; virtual wxFontEncoding GetEncoding() const; virtual const wxNativeFontInfo *GetNativeFontInfo() const; + virtual bool GetStrikethrough() const wxOVERRIDE; // change the font characteristics virtual void SetFractionalPointSize(float pointSize) wxOVERRIDE; + virtual void SetPixelSize(const wxSize& pixelSize) wxOVERRIDE; virtual void SetFamily( wxFontFamily family ); virtual void SetStyle( wxFontStyle style ); virtual void SetNumericWeight(int weight) wxOVERRIDE; virtual bool SetFaceName(const wxString& facename); virtual void SetUnderlined( bool underlined ); + virtual void SetStrikethrough(bool strikethrough) wxOVERRIDE; virtual void SetEncoding(wxFontEncoding encoding); wxDECLARE_COMMON_FONT_METHODS(); @@ -75,6 +80,7 @@ protected: virtual wxGDIRefData *CreateGDIRefData() const; virtual wxGDIRefData *CloneGDIRefData(const wxGDIRefData *data) const; virtual wxFontFamily DoGetFamily() const; + virtual void DoSetNativeFontInfo(const wxNativeFontInfo& info) wxOVERRIDE; wxDECLARE_DYNAMIC_CLASS(wxFont); diff --git a/src/common/fontcmn.cpp b/src/common/fontcmn.cpp index 54c0698ef4..7767791433 100644 --- a/src/common/fontcmn.cpp +++ b/src/common/fontcmn.cpp @@ -439,8 +439,7 @@ bool wxFontBase::operator==(const wxFont& font) const // in wxGTK1 GetPixelSize() calls GetInternalFont() which uses // operator==() resulting in infinite recursion so we can't use it // in that port - // in wxQT, GetPixelSize is too slow to be used here -#if (!defined(__WXGTK__) || defined(__WXGTK20__)) && !defined(__WXQT__) +#if (!defined(__WXGTK__) || defined(__WXGTK20__)) GetPixelSize() == font.GetPixelSize() && #endif GetFamily() == font.GetFamily() && diff --git a/src/qt/font.cpp b/src/qt/font.cpp index 9a05705706..6600d8a90c 100644 --- a/src/qt/font.cpp +++ b/src/qt/font.cpp @@ -135,7 +135,7 @@ public: if ( info.IsUsingSizeInPixels() ) m_nativeFontInfo.SetPixelSize(info.GetPixelSize()); else - m_nativeFontInfo.SetFractionalPointSize(info.GetFractionalPointSize()); + m_nativeFontInfo.SetSizeOrDefault(info.GetFractionalPointSize()); m_nativeFontInfo.SetStyle(info.GetStyle()); m_nativeFontInfo.SetWeight(info.GetWeight()); @@ -236,11 +236,21 @@ bool wxFont::Create(wxSize size, wxFontFamily family, wxFontStyle style, return true; } +int wxFont::GetPointSize() const +{ + return M_FONTDATA.wxNativeFontInfo::GetPointSize(); +} + float wxFont::GetFractionalPointSize() const { return M_FONTDATA.GetFractionalPointSize(); } +wxSize wxFont::GetPixelSize() const +{ + return M_FONTDATA.GetPixelSize(); +} + wxFontStyle wxFont::GetStyle() const { return M_FONTDATA.GetStyle(); @@ -271,6 +281,12 @@ const wxNativeFontInfo *wxFont::GetNativeFontInfo() const return &M_FONTDATA; } +bool wxFont::GetStrikethrough() const +{ + return M_FONTDATA.GetStrikethrough(); +} + + void wxFont::SetFractionalPointSize(float pointSize) { AllocExclusive(); @@ -278,6 +294,13 @@ void wxFont::SetFractionalPointSize(float pointSize) M_FONTDATA.SetFractionalPointSize(pointSize); } +void wxFont::SetPixelSize(const wxSize& pixelSize) +{ + AllocExclusive(); + + M_FONTDATA.SetPixelSize(pixelSize); +} + bool wxFont::SetFaceName(const wxString& facename) { AllocExclusive(); @@ -313,6 +336,13 @@ void wxFont::SetUnderlined( bool underlined ) M_FONTDATA.SetUnderlined(underlined); } +void wxFont::SetStrikethrough(bool strikethrough) +{ + AllocExclusive(); + + M_FONTDATA.SetStrikethrough(strikethrough); +} + void wxFont::SetEncoding(wxFontEncoding encoding) { AllocExclusive(); @@ -320,6 +350,18 @@ void wxFont::SetEncoding(wxFontEncoding encoding) M_FONTDATA.SetEncoding(encoding); } +void wxFont::DoSetNativeFontInfo(const wxNativeFontInfo& info) +{ + SetFractionalPointSize(info.GetPointSize()); + SetFamily(info.GetFamily()); + SetStyle(info.GetStyle()); + SetNumericWeight(info.GetWeight()); + SetUnderlined(info.GetUnderlined()); + SetStrikethrough(info.GetStrikethrough()); + SetFaceName(info.GetFaceName()); + SetEncoding(info.GetEncoding()); +} + wxGDIRefData *wxFont::CreateGDIRefData() const { return new wxFontRefData; @@ -353,6 +395,11 @@ float wxNativeFontInfo::GetFractionalPointSize() const return m_qtFont.pointSizeF(); } +wxSize wxNativeFontInfo::GetPixelSize() const +{ + return wxSize(0, m_qtFont.pixelSize()); +} + wxFontStyle wxNativeFontInfo::GetStyle() const { switch (m_qtFont.style()) diff --git a/tests/font/fonttest.cpp b/tests/font/fonttest.cpp index a79b64fa51..b6cd5b60aa 100644 --- a/tests/font/fonttest.cpp +++ b/tests/font/fonttest.cpp @@ -197,7 +197,16 @@ TEST_CASE("wxFont::Weight", "[font][weight]") { wxFont font; font.SetNumericWeight(123); + + // WX to QT font weight conversions do not map directly which is why we + // check if the numeric weight is within a range rather than checking for + // an exact match. +#ifdef __WXQT__ + CHECK( ( font.GetNumericWeight() > 113 && font.GetNumericWeight() < 133 ) ); +#else CHECK( font.GetNumericWeight() == 123 ); +#endif + CHECK( font.GetWeight() == wxFONTWEIGHT_THIN ); font.SetNumericWeight(wxFONTWEIGHT_SEMIBOLD); @@ -244,8 +253,10 @@ TEST_CASE("wxFont::GetSet", "[font][getters]") // test Get/SetFaceName() +#ifndef __WXQT__ CHECK( !test.SetFaceName("a dummy face name") ); CHECK( !test.IsOk() ); +#endif // if the call to SetFaceName() below fails on your system/port, // consider adding another branch to this #if @@ -370,7 +381,7 @@ TEST_CASE("wxFont::NativeFontInfo", "[font][fontinfo]") // never returns an error at all so this assertion fails there -- and as it // doesn't seem to be possible to do anything about it maybe we should // change wxMSW and other ports to also accept any strings? -#if !defined(__WXGTK__) && !defined(__WXX11__) +#if !defined(__WXGTK__) && !defined(__WXX11__) && !defined(__WXQT__) CHECK( !font.SetNativeFontInfo("bloordyblop") ); #endif