#include "wx/fontutil.h" // for wxNativeFontInfo
#include "wx/fontmap.h"
+#include "wx/fontenum.h"
#include "wx/tokenzr.h"
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;
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
GetStyle() == font.GetStyle() &&
GetWeight() == font.GetWeight() &&
GetUnderlined() == font.GetUnderlined() &&
- GetFaceName() == font.GetFaceName() &&
+ GetFaceName().IsSameAs(font.GetFaceName(), false) &&
GetEncoding() == font.GetEncoding()
);
}
}
}
+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.
underlined = underlined_;
}
-void wxNativeFontInfo::SetFaceName(const wxString& facename_)
+bool wxNativeFontInfo::SetFaceName(const wxString& facename_)
{
faceName = facename_;
+ return true;
}
void wxNativeFontInfo::SetFamily(wxFontFamily family_)
// but what else can we do?
if ( GetUnderlined() )
{
- desc << _("underlined ");
+ desc << _("underlined");
}
switch ( GetWeight() )
break;
case wxFONTWEIGHT_LIGHT:
- desc << _("light ");
+ desc << _(" light");
break;
case wxFONTWEIGHT_BOLD:
- desc << _("bold ");
+ desc << _(" bold");
break;
}
// we don't distinguish between the two for now anyhow...
case wxFONTSTYLE_ITALIC:
case wxFONTSTYLE_SLANT:
- desc << _("italic");
+ desc << _(" italic");
break;
}
}
#endif // wxUSE_FONTMAP
- return desc;
+ return desc.Strip(wxString::both).MakeLower();
}
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() )
{
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") )
{
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(' ');
// 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
// 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();
}
}
// 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;
}