From dcb12e633be5da54a9681e9ef6a0413945cc0267 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 5 Sep 2018 02:02:56 +0200 Subject: [PATCH] Extract helper functions for wxFontWeight <-> int conversions Make these functions available for the upcoming reuse in wxFont. This is almost a pure refactoring, except that assert checking for the symbolic weight validity was corrected (it was always true before due to wrong use of "||" instead of "&&", so split it in 2 asserts to ensure that this doesn't happen any more). --- src/common/fontcmn.cpp | 79 ++++++++++++++++++++++++++++-------------- 1 file changed, 53 insertions(+), 26 deletions(-) diff --git a/src/common/fontcmn.cpp b/src/common/fontcmn.cpp index 1d8038e85b..2e5f9784e5 100644 --- a/src/common/fontcmn.cpp +++ b/src/common/fontcmn.cpp @@ -136,6 +136,55 @@ 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 // ---------------------------------------------------------------------------- @@ -1234,36 +1283,14 @@ bool wxNativeFontInfo::FromUserString(const wxString& s) wxFontWeight wxNativeFontInfo::GetWeight() const { - // round to nearest hundredth = wxFONTWEIGHT_ constant - int weight = ((GetNumericWeight() + 50) / 100) * 100; - - if (weight < wxFONTWEIGHT_THIN) - weight = wxFONTWEIGHT_THIN; - if (weight > wxFONTWEIGHT_MAX) - weight = wxFONTWEIGHT_MAX; - - return (wxFontWeight)weight; + return GetWeightClosestToNumericValue(GetNumericWeight()); } void wxNativeFontInfo::SetWeight(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 || weight <= wxFONTWEIGHT_MAX); - wxASSERT(weight % 100 == 0); - - wxFontWeight formerWeight = GetWeight(); - if (formerWeight != weight) - SetNumericWeight(weight); + const int numWeight = GetNumericWeightOf(weight); + if ( numWeight != GetNumericWeight() ) + SetNumericWeight(numWeight); } // wxFont <-> wxString utilities, used by wxConfig