miscellaneous wxFont enhancements (patch 1496606):

- made SetFaceName() bool and return false if the face name is not available
- corrected To/FromUserString() to complement each other


git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@39411 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2006-05-29 00:03:36 +00:00
parent 7aa7d2d407
commit 85ab460e7d
31 changed files with 378 additions and 104 deletions

View File

@@ -41,6 +41,7 @@
#include "wx/fontutil.h" // for wxNativeFontInfo
#include "wx/fontmap.h"
#include "wx/fontenum.h"
#include "wx/tokenzr.h"
@@ -266,6 +267,11 @@ wxString wxFontBase::GetNativeFontInfoDesc() const
if ( fontInfo )
{
fontDesc = fontInfo->ToString();
wxASSERT_MSG(!fontDesc.IsEmpty(), wxT("This should be a non-empty string!"));
}
else
{
wxASSERT_MSG(0, wxT("Derived class should have created the wxNativeFontInfo!"));
}
return fontDesc;
@@ -278,27 +284,40 @@ wxString wxFontBase::GetNativeFontInfoUserDesc() const
if ( fontInfo )
{
fontDesc = fontInfo->ToUserString();
wxASSERT_MSG(!fontDesc.IsEmpty(), wxT("This should be a non-empty string!"));
}
else
{
wxASSERT_MSG(0, wxT("Derived class should have created the wxNativeFontInfo!"));
}
return fontDesc;
}
void wxFontBase::SetNativeFontInfo(const wxString& info)
bool wxFontBase::SetNativeFontInfo(const wxString& info)
{
wxNativeFontInfo fontInfo;
if ( !info.empty() && fontInfo.FromString(info) )
{
SetNativeFontInfo(fontInfo);
return true;
}
UnRef();
return false;
}
void wxFontBase::SetNativeFontInfoUserDesc(const wxString& info)
bool wxFontBase::SetNativeFontInfoUserDesc(const wxString& info)
{
wxNativeFontInfo fontInfo;
if ( !info.empty() && fontInfo.FromUserString(info) )
{
SetNativeFontInfo(fontInfo);
return true;
}
UnRef();
return false;
}
bool wxFontBase::operator==(const wxFont& font) const
@@ -313,7 +332,7 @@ bool wxFontBase::operator==(const wxFont& font) const
GetStyle() == font.GetStyle() &&
GetWeight() == font.GetWeight() &&
GetUnderlined() == font.GetUnderlined() &&
GetFaceName() == font.GetFaceName() &&
GetFaceName().IsSameAs(font.GetFaceName(), false) &&
GetEncoding() == font.GetEncoding()
);
}
@@ -365,10 +384,41 @@ wxString wxFontBase::GetWeightString() const
}
}
bool wxFontBase::SetFaceName(const wxString &facename)
{
if (!wxFontEnumerator::IsValidFacename(facename))
{
UnRef(); // make Ok() return false
return false;
}
return true;
}
// ----------------------------------------------------------------------------
// wxNativeFontInfo
// ----------------------------------------------------------------------------
// Up to now, there are no native implementations of this function:
void wxNativeFontInfo::SetFaceName(const wxArrayString &facenames)
{
for (size_t i=0; i < facenames.GetCount(); i++)
{
if (wxFontEnumerator::IsValidFacename(facenames[i]))
{
SetFaceName(facenames[i]);
return;
}
}
// set the first valid facename we can find on this system
wxString validfacename = wxFontEnumerator::GetFacenames().Item(0);
wxLogTrace(wxT("font"), wxT("Falling back to '%s'"), validfacename.c_str());
SetFaceName(validfacename);
}
#ifdef wxNO_NATIVE_FONTINFO
// These are the generic forms of FromString()/ToString.
@@ -510,9 +560,10 @@ void wxNativeFontInfo::SetUnderlined(bool underlined_)
underlined = underlined_;
}
void wxNativeFontInfo::SetFaceName(const wxString& facename_)
bool wxNativeFontInfo::SetFaceName(const wxString& facename_)
{
faceName = facename_;
return true;
}
void wxNativeFontInfo::SetFamily(wxFontFamily family_)
@@ -542,7 +593,7 @@ wxString wxNativeFontInfo::ToUserString() const
// but what else can we do?
if ( GetUnderlined() )
{
desc << _("underlined ");
desc << _("underlined");
}
switch ( GetWeight() )
@@ -555,11 +606,11 @@ wxString wxNativeFontInfo::ToUserString() const
break;
case wxFONTWEIGHT_LIGHT:
desc << _("light ");
desc << _(" light");
break;
case wxFONTWEIGHT_BOLD:
desc << _("bold ");
desc << _(" bold");
break;
}
@@ -575,7 +626,7 @@ wxString wxNativeFontInfo::ToUserString() const
// we don't distinguish between the two for now anyhow...
case wxFONTSTYLE_ITALIC:
case wxFONTSTYLE_SLANT:
desc << _("italic");
desc << _(" italic");
break;
}
@@ -599,7 +650,7 @@ wxString wxNativeFontInfo::ToUserString() const
}
#endif // wxUSE_FONTMAP
return desc;
return desc.Strip(wxString::both).MakeLower();
}
bool wxNativeFontInfo::FromUserString(const wxString& s)
@@ -614,10 +665,7 @@ bool wxNativeFontInfo::FromUserString(const wxString& s)
wxString face;
unsigned long size;
#if wxUSE_FONTMAP
wxFontEncoding encoding;
#endif // wxUSE_FONTMAP
bool weightfound = false, pointsizefound = false, encodingfound = false;
while ( tokenizer.HasMoreTokens() )
{
@@ -634,10 +682,12 @@ bool wxNativeFontInfo::FromUserString(const wxString& s)
else if ( token == _T("light") || token == _("light") )
{
SetWeight(wxFONTWEIGHT_LIGHT);
weightfound = true;
}
else if ( token == _T("bold") || token == _("bold") )
{
SetWeight(wxFONTWEIGHT_BOLD);
weightfound = true;
}
else if ( token == _T("italic") || token == _("italic") )
{
@@ -646,16 +696,24 @@ bool wxNativeFontInfo::FromUserString(const wxString& s)
else if ( token.ToULong(&size) )
{
SetPointSize(size);
pointsizefound = true;
}
else
{
#if wxUSE_FONTMAP
else if ( (encoding = wxFontMapper::Get()->CharsetToEncoding(token, false))
!= wxFONTENCODING_DEFAULT )
// try to interpret this as an encoding
wxFontEncoding encoding = wxFontMapper::Get()->CharsetToEncoding(token, false);
if ( encoding != wxFONTENCODING_DEFAULT &&
encoding != wxFONTENCODING_SYSTEM ) // returned when the recognition failed
{
SetEncoding(encoding);
encodingfound = true;
}
#endif // wxUSE_FONTMAP
else // assume it is the face name
else
{
#endif // wxUSE_FONTMAP
// assume it is the face name
if ( !face.empty() )
{
face += _T(' ');
@@ -665,6 +723,10 @@ bool wxNativeFontInfo::FromUserString(const wxString& s)
// skip the code which resets face below
continue;
#if wxUSE_FONTMAP
}
#endif // wxUSE_FONTMAP
}
// if we had had the facename, we shouldn't continue appending tokens
@@ -672,7 +734,12 @@ bool wxNativeFontInfo::FromUserString(const wxString& s)
// bar")
if ( !face.empty() )
{
SetFaceName(face);
// NB: the check on the facename is implemented in wxFontBase::SetFaceName
// and not in wxNativeFontInfo::SetFaceName thus we need to explicitely
// call here wxFontEnumerator::IsValidFacename
if (!wxFontEnumerator::IsValidFacename(face) ||
!SetFaceName(face))
SetFaceName(wxNORMAL_FONT->GetFaceName());
face.clear();
}
}
@@ -680,9 +747,28 @@ bool wxNativeFontInfo::FromUserString(const wxString& s)
// we might not have flushed it inside the loop
if ( !face.empty() )
{
SetFaceName(face);
// NB: the check on the facename is implemented in wxFontBase::SetFaceName
// and not in wxNativeFontInfo::SetFaceName thus we need to explicitely
// call here wxFontEnumerator::IsValidFacename
if (!wxFontEnumerator::IsValidFacename(face) ||
!SetFaceName(face))
SetFaceName(wxNORMAL_FONT->GetFaceName());
}
// set point size to default value if size was not given
if ( !pointsizefound )
SetPointSize(wxNORMAL_FONT->GetPointSize());
// set font weight to default value if weight was not given
if ( !weightfound )
SetWeight(wxFONTWEIGHT_NORMAL);
#if wxUSE_FONTMAP
// set font encoding to default value if encoding was not given
if ( !encodingfound )
SetEncoding(wxFONTENCODING_SYSTEM);
#endif // wxUSE_FONTMAP
return true;
}