Make converters between wxFontWeight and raw values public

These functions can be useful outside of src/common/fontcmn.cpp and,
potentially, even in the user code, so make them public methods of
wxFont.

No real changes, just add asserts verifying input argument value to
GetWeightClosestToNumericValue().
This commit is contained in:
Vadim Zeitlin
2018-09-07 03:05:34 +02:00
parent 4e0b4e5939
commit 6dd9f4208e
3 changed files with 81 additions and 51 deletions

View File

@@ -413,6 +413,10 @@ public:
static wxFontEncoding GetDefaultEncoding() { return ms_encodingDefault; } static wxFontEncoding GetDefaultEncoding() { return ms_encodingDefault; }
static void SetDefaultEncoding(wxFontEncoding encoding); 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 // this doesn't do anything and is kept for compatibility only
#if WXWIN_COMPATIBILITY_2_8 #if WXWIN_COMPATIBILITY_2_8
wxDEPRECATED_INLINE(void SetNoAntiAliasing(bool no = true), wxUnusedVar(no);) wxDEPRECATED_INLINE(void SetNoAntiAliasing(bool no = true), wxUnusedVar(no);)

View File

@@ -1221,6 +1221,34 @@ public:
*/ */
static void SetDefaultEncoding(wxFontEncoding encoding); 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 This function takes the same parameters as the relative

View File

@@ -138,55 +138,6 @@ wxEMPTY_HANDLERS_TABLE(wxFont)
// implementation // 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 // wxFontBase
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@@ -283,6 +234,53 @@ bool wxFontBase::IsFixedWidth() const
return GetFamily() == wxFONTFAMILY_TELETYPE; 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<wxFontWeight>(weight);
}
int wxFontBase::GetPointSize() const int wxFontBase::GetPointSize() const
{ {
return wxRound(GetFractionalPointSize()); return wxRound(GetFractionalPointSize());
@@ -1332,12 +1330,12 @@ bool wxNativeFontInfo::FromUserString(const wxString& s)
wxFontWeight wxNativeFontInfo::GetWeight() const wxFontWeight wxNativeFontInfo::GetWeight() const
{ {
return GetWeightClosestToNumericValue(GetNumericWeight()); return wxFontBase::GetWeightClosestToNumericValue(GetNumericWeight());
} }
void wxNativeFontInfo::SetWeight(wxFontWeight weight) void wxNativeFontInfo::SetWeight(wxFontWeight weight)
{ {
const int numWeight = GetNumericWeightOf(weight); const int numWeight = wxFontBase::GetNumericWeightOf(weight);
if ( numWeight != GetNumericWeight() ) if ( numWeight != GetNumericWeight() )
SetNumericWeight(numWeight); SetNumericWeight(numWeight);
} }