+#include "wx/fontmap.h"
+#include "wx/tokenzr.h"
+#include "wx/fontenum.h"
+
+#if wxUSE_PANGO
+
+#include "pango/pango.h"
+
+#ifdef __WXGTK20__
+#include "wx/gtk/private.h"
+extern GtkWidget *wxGetRootWindow();
+#else
+#include "wx/x11/private.h"
+#endif
+
+// ----------------------------------------------------------------------------
+// wxNativeFontInfo
+// ----------------------------------------------------------------------------
+
+void wxNativeFontInfo::Init()
+{
+ description = NULL;
+}
+
+void
+wxNativeFontInfo::Init(const wxNativeFontInfo& info)
+{
+ if (info.description)
+ description = pango_font_description_copy(info.description);
+ else
+ description = NULL;
+}
+
+void wxNativeFontInfo::Free()
+{
+ if (description)
+ pango_font_description_free(description);
+}
+
+int wxNativeFontInfo::GetPointSize() const
+{
+ return pango_font_description_get_size( description ) / PANGO_SCALE;
+}
+
+wxFontStyle wxNativeFontInfo::GetStyle() const
+{
+ wxFontStyle m_style = wxFONTSTYLE_NORMAL;
+
+ switch (pango_font_description_get_style( description ))
+ {
+ case PANGO_STYLE_NORMAL:
+ m_style = wxFONTSTYLE_NORMAL;
+ break;
+ case PANGO_STYLE_ITALIC:
+ m_style = wxFONTSTYLE_ITALIC;
+ break;
+ case PANGO_STYLE_OBLIQUE:
+ m_style = wxFONTSTYLE_SLANT;
+ break;
+ }
+
+ return m_style;
+}
+
+wxFontWeight wxNativeFontInfo::GetWeight() const
+{
+#if 0
+ // We seem to currently initialize only by string.
+ // In that case PANGO_FONT_MASK_WEIGHT is always set.
+ if (!(pango_font_description_get_set_fields(description) & PANGO_FONT_MASK_WEIGHT))
+ return wxFONTWEIGHT_NORMAL;
+#endif
+
+ PangoWeight pango_weight = pango_font_description_get_weight( description );
+
+ // Until the API can be changed the following ranges of weight values are used:
+ // wxFONTWEIGHT_LIGHT: 100 .. 349 - range of 250
+ // wxFONTWEIGHT_NORMAL: 350 .. 599 - range of 250
+ // wxFONTWEIGHT_BOLD: 600 .. 900 - range of 301 (600 is "semibold" already)
+
+ if (pango_weight >= 600)
+ return wxFONTWEIGHT_BOLD;
+
+ if (pango_weight < 350)
+ return wxFONTWEIGHT_LIGHT;
+
+ return wxFONTWEIGHT_NORMAL;
+}
+
+bool wxNativeFontInfo::GetUnderlined() const
+{
+ return false;
+}
+
+wxString wxNativeFontInfo::GetFaceName() const
+{
+ wxString tmp = wxGTK_CONV_BACK( pango_font_description_get_family( description ) );
+
+ return tmp;
+}
+
+wxFontFamily wxNativeFontInfo::GetFamily() const
+{
+ wxFontFamily ret = wxFONTFAMILY_DEFAULT;
+ // note: not passing -1 as the 2nd parameter to g_ascii_strdown to work
+ // around a bug in the 64-bit glib shipped with solaris 10, -1 causes it
+ // to try to allocate 2^32 bytes.
+ const char *family_name = pango_font_description_get_family( description );
+ if ( !family_name )
+ return ret;
+
+ wxGtkString family_text(g_ascii_strdown(family_name, strlen(family_name)));
+
+ // Check for some common fonts, to salvage what we can from the current win32 centric wxFont API:
+ if (strncmp( family_text, "monospace", 9 ) == 0)
+ ret = wxFONTFAMILY_TELETYPE; // begins with "Monospace"
+ else if (strncmp( family_text, "courier", 7 ) == 0)
+ ret = wxFONTFAMILY_TELETYPE; // begins with "Courier"
+#if defined(__WXGTK24__) || defined(HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE)
+ else
+#ifdef __WXGTK24__
+ if (!gtk_check_version(2,4,0))
+#endif
+ {
+ PangoFontFamily **families;
+ PangoFontFamily *family = NULL;
+ int n_families;
+ pango_context_list_families(
+#ifdef __WXGTK20__
+ gtk_widget_get_pango_context( wxGetRootWindow() ),
+#else
+ wxTheApp->GetPangoContext(),
+#endif
+ &families, &n_families);
+
+ for (int i = 0;i < n_families;++i)
+ {
+ if (g_ascii_strcasecmp(pango_font_family_get_name( families[i] ), pango_font_description_get_family( description )) == 0 )
+ {
+ family = families[i];
+ break;
+ }
+ }
+
+ g_free(families);
+
+ // Some gtk+ systems might query for a non-existing font from wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT)
+ // on initialization, don't assert until wxSystemSettings::GetFont is checked for this - MR
+ // wxASSERT_MSG( family, wxT("wxNativeFontInfo::GetFamily() - No appropriate PangoFontFamily found for ::description") );
+
+ //BCI: Cache the wxFontFamily inside the class. Validate cache with
+ //BCI: g_ascii_strcasecmp(pango_font_description_get_family(description), pango_font_family_get_name(family)) == 0
+
+ if (family != NULL && pango_font_family_is_monospace( family ))
+ ret = wxFONTFAMILY_TELETYPE; // is deemed a monospace font by pango
+ }
+#endif // gtk24 || HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE
+
+ if (ret == wxFONTFAMILY_DEFAULT)
+ {
+ if (strstr( family_text, "sans" ) != NULL) // checked before serif, so that "* Sans Serif" fonts are detected correctly
+ ret = wxFONTFAMILY_SWISS; // contains "Sans"
+ else if (strstr( family_text, "serif" ) != NULL)
+ ret = wxFONTFAMILY_ROMAN; // contains "Serif"
+ else if (strncmp( family_text, "times", 5 ) == 0)
+ ret = wxFONTFAMILY_ROMAN; // begins with "Times"
+ else if (strncmp( family_text, "old", 3 ) == 0)
+ ret = wxFONTFAMILY_DECORATIVE; // Begins with "Old" - "Old English", "Old Town"
+ }
+
+ return ret;
+}
+
+wxFontEncoding wxNativeFontInfo::GetEncoding() const
+{
+ return wxFONTENCODING_SYSTEM;
+}
+
+
+void wxNativeFontInfo::SetPointSize(int pointsize)
+{
+ pango_font_description_set_size( description, pointsize * PANGO_SCALE );
+}
+
+void wxNativeFontInfo::SetStyle(wxFontStyle style)
+{
+ switch (style)
+ {
+ case wxFONTSTYLE_ITALIC:
+ pango_font_description_set_style( description, PANGO_STYLE_ITALIC );
+ break;
+ case wxFONTSTYLE_SLANT:
+ pango_font_description_set_style( description, PANGO_STYLE_OBLIQUE );
+ break;
+ default:
+ wxFAIL_MSG( _T("unknown font style") );
+ // fall through
+ case wxFONTSTYLE_NORMAL:
+ pango_font_description_set_style( description, PANGO_STYLE_NORMAL );
+ break;
+ }
+}
+
+void wxNativeFontInfo::SetWeight(wxFontWeight weight)
+{
+ switch (weight)
+ {
+ case wxFONTWEIGHT_BOLD:
+ pango_font_description_set_weight(description, PANGO_WEIGHT_BOLD);
+ break;
+ case wxFONTWEIGHT_LIGHT:
+ pango_font_description_set_weight(description, PANGO_WEIGHT_LIGHT);
+ break;
+ default:
+ wxFAIL_MSG( _T("unknown font weight") );
+ // fall through
+ case wxFONTWEIGHT_NORMAL:
+ pango_font_description_set_weight(description, PANGO_WEIGHT_NORMAL);
+ }
+}
+
+void wxNativeFontInfo::SetUnderlined(bool WXUNUSED(underlined))
+{
+ wxFAIL_MSG( _T("not implemented") );
+}
+
+bool wxNativeFontInfo::SetFaceName(const wxString& facename)
+{
+ pango_font_description_set_family(description, wxGTK_CONV_SYS(facename));
+ return true;
+}
+
+void wxNativeFontInfo::SetFamily(wxFontFamily WXUNUSED(family))
+{
+ wxFAIL_MSG( _T("not implemented") );
+}
+
+void wxNativeFontInfo::SetEncoding(wxFontEncoding WXUNUSED(encoding))
+{
+ wxFAIL_MSG( _T("not implemented") );
+}
+
+
+
+bool wxNativeFontInfo::FromString(const wxString& s)
+{
+ if (description)
+ pango_font_description_free( description );
+
+ // there is a bug in at least pango <= 1.13 which makes it (or its backends)
+ // segfault for very big point sizes and for negative point sizes.
+ // To workaround that bug for pango <= 1.13
+ // (see http://bugzilla.gnome.org/show_bug.cgi?id=340229)
+ // we do the check on the size here using same (arbitrary) limits used by
+ // pango > 1.13. Note that the segfault could happen also for pointsize
+ // smaller than this limit !!
+ wxString str(s);
+ const size_t pos = str.find_last_of(_T(" "));
+ double size;
+ if ( pos != wxString::npos && wxString(str, pos + 1).ToDouble(&size) )
+ {
+ wxString sizeStr;
+ if ( size < 1 )
+ sizeStr = _T("1");
+ else if ( size >= 1E6 )
+ sizeStr = _T("1E6");
+
+ if ( !sizeStr.empty() )
+ {
+ // replace the old size with the adjusted one
+ str = wxString(s, 0, pos) + sizeStr;
+ }
+ }
+
+ description = pango_font_description_from_string( wxGTK_CONV_SYS( str ) );
+
+ // ensure a valid facename is selected
+ if (!wxFontEnumerator::IsValidFacename(GetFaceName()))
+ SetFaceName(wxNORMAL_FONT->GetFaceName());
+
+ return true;
+}
+
+wxString wxNativeFontInfo::ToString() const
+{
+ wxGtkString str(pango_font_description_to_string( description ));
+
+ return wxGTK_CONV_BACK(str);
+}
+
+bool wxNativeFontInfo::FromUserString(const wxString& s)
+{
+ return FromString( s );
+}
+
+wxString wxNativeFontInfo::ToUserString() const
+{
+ return ToString();
+}
+
+// ----------------------------------------------------------------------------
+// wxNativeEncodingInfo
+// ----------------------------------------------------------------------------
+
+bool wxNativeEncodingInfo::FromString(const wxString& WXUNUSED(s))
+{
+ return false;
+}
+
+wxString wxNativeEncodingInfo::ToString() const
+{
+ return wxEmptyString;
+}
+
+bool wxTestFontEncoding(const wxNativeEncodingInfo& WXUNUSED(info))
+{
+ return true;
+}
+
+bool wxGetNativeFontEncoding(wxFontEncoding encoding,
+ wxNativeEncodingInfo *info)
+{
+ // all encodings are available in GTK+ 2 because we translate text in any
+ // encoding to UTF-8 internally anyhow
+ info->facename.clear();
+ info->encoding = encoding;
+
+ return true;
+}
+
+#else // GTK+ 1.x
+