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).
This commit is contained in:
@@ -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<wxFontWeight>(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
|
||||
|
Reference in New Issue
Block a user