diff --git a/include/wx/font.h b/include/wx/font.h index 5f9f70b2d0..4dc01f55e1 100644 --- a/include/wx/font.h +++ b/include/wx/font.h @@ -413,6 +413,10 @@ public: static wxFontEncoding GetDefaultEncoding() { return ms_encodingDefault; } static void SetDefaultEncoding(wxFontEncoding encoding); + // Convert between symbolic and numeric font weights. + static int GetNumericWeightOf(wxFontWeight weight); + static wxFontWeight GetWeightClosestToNumericValue(int numWeight); + // this doesn't do anything and is kept for compatibility only #if WXWIN_COMPATIBILITY_2_8 wxDEPRECATED_INLINE(void SetNoAntiAliasing(bool no = true), wxUnusedVar(no);) diff --git a/interface/wx/font.h b/interface/wx/font.h index f36d2a7de8..23cc879498 100644 --- a/interface/wx/font.h +++ b/interface/wx/font.h @@ -1221,6 +1221,34 @@ public: */ static void SetDefaultEncoding(wxFontEncoding encoding); + /** + Get the raw weight value corresponding to the given symbolic constant. + + For compatibility, this function handles the values @c wxNORMAL, @c + wxLIGHT and @c wxBOLD, that have values 90, 91 and 92, specially and + converts them to the corresponding @c wxFONTWEIGHT_XXX weight value. + + @param weight + A valid element of wxFontWeight enum, i.e. this argument can't have + value ::wxFONTWEIGHT_INVALID. + @return Numeric weight, between 1 and 1000. + + @since 3.1.2 + */ + static int GetNumericWeightOf(wxFontWeight weight); + + /** + Get the symbolic weight closest to the given raw weight value. + + @param numWeight + A valid raw weight value, i.e. a value in the range 1 to 1000, + inclusive. + @return A valid element of wxFontWeight enum. + + @since 3.1.2 + */ + static wxFontWeight GetWeightClosestToNumericValue(int numWeight); + //@{ /** This function takes the same parameters as the relative diff --git a/src/common/fontcmn.cpp b/src/common/fontcmn.cpp index a0ef87bee7..66451ced9d 100644 --- a/src/common/fontcmn.cpp +++ b/src/common/fontcmn.cpp @@ -138,55 +138,6 @@ wxEMPTY_HANDLERS_TABLE(wxFont) // implementation // ============================================================================ -// ---------------------------------------------------------------------------- -// Local helpers -// ---------------------------------------------------------------------------- - -namespace -{ - -// Convert to/from wxFontWeight enum elements and numeric weight values. - -// This function interprets wxNORMAL, wxLIGHT and wxBOLD values in addition to -// the actual wxFontWeight enum element values for compatibility. It does check -// that it gets either the one or the other. -int GetNumericWeightOf(wxFontWeight weight) -{ - // deal with compatibility constants - if (weight >= 90 && weight <= 92) - { - if (weight == 90 /* wxNORMAL */) - weight = wxFONTWEIGHT_NORMAL; - else if (weight == 91 /* wxLIGHT */) - weight = wxFONTWEIGHT_LIGHT; - else if (weight == 92 /* wxBOLD */) - weight = wxFONTWEIGHT_BOLD; - } - - wxASSERT(weight > wxFONTWEIGHT_INVALID); - wxASSERT(weight <= wxFONTWEIGHT_MAX); - wxASSERT(weight % 100 == 0); - - return weight; -} - -// As its name indicates, this function returns the closest wxFontWeight enum -// element, even if its value doesn't correspond to the given weight exactly. -wxFontWeight GetWeightClosestToNumericValue(int numWeight) -{ - // round to nearest hundredth = wxFONTWEIGHT_ constant - int weight = ((numWeight + 50) / 100) * 100; - - if (weight < wxFONTWEIGHT_THIN) - weight = wxFONTWEIGHT_THIN; - if (weight > wxFONTWEIGHT_MAX) - weight = wxFONTWEIGHT_MAX; - - return static_cast(weight); -} - -} // anonymous namespace - // ---------------------------------------------------------------------------- // wxFontBase // ---------------------------------------------------------------------------- @@ -283,6 +234,53 @@ bool wxFontBase::IsFixedWidth() const return GetFamily() == wxFONTFAMILY_TELETYPE; } + +// Convert to/from wxFontWeight enum elements and numeric weight values. + +// This function interprets wxNORMAL, wxLIGHT and wxBOLD values in addition to +// the actual wxFontWeight enum element values for compatibility. It does check +// that it gets either the one or the other. +/* static */ +int wxFontBase::GetNumericWeightOf(wxFontWeight weight) +{ + // deal with compatibility constants + if (weight >= 90 && weight <= 92) + { + if (weight == 90 /* wxNORMAL */) + weight = wxFONTWEIGHT_NORMAL; + else if (weight == 91 /* wxLIGHT */) + weight = wxFONTWEIGHT_LIGHT; + else if (weight == 92 /* wxBOLD */) + weight = wxFONTWEIGHT_BOLD; + } + + wxASSERT(weight > wxFONTWEIGHT_INVALID); + wxASSERT(weight <= wxFONTWEIGHT_MAX); + wxASSERT(weight % 100 == 0); + + return weight; +} + +// As its name indicates, this function returns the closest wxFontWeight enum +// element, even if its value doesn't correspond to the given weight exactly. +/* static */ +wxFontWeight wxFontBase::GetWeightClosestToNumericValue(int numWeight) +{ + wxASSERT(numWeight > 0); + wxASSERT(numWeight <= 1000); + + // round to nearest hundredth = wxFONTWEIGHT_ constant + int weight = ((numWeight + 50) / 100) * 100; + + if (weight < wxFONTWEIGHT_THIN) + weight = wxFONTWEIGHT_THIN; + if (weight > wxFONTWEIGHT_MAX) + weight = wxFONTWEIGHT_MAX; + + return static_cast(weight); +} + + int wxFontBase::GetPointSize() const { return wxRound(GetFractionalPointSize()); @@ -1332,12 +1330,12 @@ bool wxNativeFontInfo::FromUserString(const wxString& s) wxFontWeight wxNativeFontInfo::GetWeight() const { - return GetWeightClosestToNumericValue(GetNumericWeight()); + return wxFontBase::GetWeightClosestToNumericValue(GetNumericWeight()); } void wxNativeFontInfo::SetWeight(wxFontWeight weight) { - const int numWeight = GetNumericWeightOf(weight); + const int numWeight = wxFontBase::GetNumericWeightOf(weight); if ( numWeight != GetNumericWeight() ) SetNumericWeight(numWeight); }