1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/unix/fontutil.cpp
3 // Purpose: Font helper functions for X11 (GDK/X)
4 // Author: Vadim Zeitlin
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #include "wx/fontutil.h"
31 #include "wx/font.h" // wxFont enums
32 #include "wx/encinfo.h"
34 #include "wx/utils.h" // for wxGetDisplay()
37 #include "wx/fontmap.h"
38 #include "wx/tokenzr.h"
39 #include "wx/module.h"
40 #include "wx/fontenum.h"
44 #include "pango/pango.h"
47 #include "wx/gtk/private.h"
48 extern GtkWidget
*wxGetRootWindow();
50 #include "wx/x11/private.h"
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 void wxNativeFontInfo::Init()
63 wxNativeFontInfo::Init(const wxNativeFontInfo
& info
)
66 description
= pango_font_description_copy(info
.description
);
71 void wxNativeFontInfo::Free()
74 pango_font_description_free(description
);
77 int wxNativeFontInfo::GetPointSize() const
79 return pango_font_description_get_size( description
) / PANGO_SCALE
;
82 wxFontStyle
wxNativeFontInfo::GetStyle() const
84 wxFontStyle m_style
= wxFONTSTYLE_NORMAL
;
86 switch (pango_font_description_get_style( description
))
88 case PANGO_STYLE_NORMAL
:
89 m_style
= wxFONTSTYLE_NORMAL
;
91 case PANGO_STYLE_ITALIC
:
92 m_style
= wxFONTSTYLE_ITALIC
;
94 case PANGO_STYLE_OBLIQUE
:
95 m_style
= wxFONTSTYLE_SLANT
;
102 wxFontWeight
wxNativeFontInfo::GetWeight() const
105 // We seem to currently initialize only by string.
106 // In that case PANGO_FONT_MASK_WEIGHT is always set.
107 if (!(pango_font_description_get_set_fields(description
) & PANGO_FONT_MASK_WEIGHT
))
108 return wxFONTWEIGHT_NORMAL
;
111 PangoWeight pango_weight
= pango_font_description_get_weight( description
);
113 // Until the API can be changed the following ranges of weight values are used:
114 // wxFONTWEIGHT_LIGHT: 100 .. 349 - range of 250
115 // wxFONTWEIGHT_NORMAL: 350 .. 599 - range of 250
116 // wxFONTWEIGHT_BOLD: 600 .. 900 - range of 301 (600 is "semibold" already)
118 if (pango_weight
>= 600)
119 return wxFONTWEIGHT_BOLD
;
121 if (pango_weight
< 350)
122 return wxFONTWEIGHT_LIGHT
;
124 return wxFONTWEIGHT_NORMAL
;
127 bool wxNativeFontInfo::GetUnderlined() const
132 wxString
wxNativeFontInfo::GetFaceName() const
134 wxString tmp
= wxGTK_CONV_BACK( pango_font_description_get_family( description
) );
139 wxFontFamily
wxNativeFontInfo::GetFamily() const
141 wxFontFamily ret
= wxFONTFAMILY_DEFAULT
;
142 // note: not passing -1 as the 2nd parameter to g_ascii_strdown to work
143 // around a bug in the 64-bit glib shipped with solaris 10, -1 causes it
144 // to try to allocate 2^32 bytes.
145 const char *family_name
= pango_font_description_get_family( description
);
149 wxGtkString
family_text(g_ascii_strdown(family_name
, strlen(family_name
)));
151 // Check for some common fonts, to salvage what we can from the current win32 centric wxFont API:
152 if (strncmp( family_text
, "monospace", 9 ) == 0)
153 ret
= wxFONTFAMILY_TELETYPE
; // begins with "Monospace"
154 else if (strncmp( family_text
, "courier", 7 ) == 0)
155 ret
= wxFONTFAMILY_TELETYPE
; // begins with "Courier"
156 #if defined(__WXGTK24__) || defined(HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE)
159 if (!gtk_check_version(2,4,0))
162 PangoFontFamily
**families
;
163 PangoFontFamily
*family
= NULL
;
165 pango_context_list_families(
167 gtk_widget_get_pango_context( wxGetRootWindow() ),
169 wxTheApp
->GetPangoContext(),
171 &families
, &n_families
);
173 for (int i
= 0;i
< n_families
;++i
)
175 if (g_ascii_strcasecmp(pango_font_family_get_name( families
[i
] ), pango_font_description_get_family( description
)) == 0 )
177 family
= families
[i
];
184 // Some gtk+ systems might query for a non-existing font from wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT)
185 // on initialization, don't assert until wxSystemSettings::GetFont is checked for this - MR
186 // wxASSERT_MSG( family, wxT("wxNativeFontInfo::GetFamily() - No appropriate PangoFontFamily found for ::description") );
188 //BCI: Cache the wxFontFamily inside the class. Validate cache with
189 //BCI: g_ascii_strcasecmp(pango_font_description_get_family(description), pango_font_family_get_name(family)) == 0
191 if (family
!= NULL
&& pango_font_family_is_monospace( family
))
192 ret
= wxFONTFAMILY_TELETYPE
; // is deemed a monospace font by pango
194 #endif // gtk24 || HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE
196 if (ret
== wxFONTFAMILY_DEFAULT
)
198 if (strstr( family_text
, "sans" ) != NULL
) // checked before serif, so that "* Sans Serif" fonts are detected correctly
199 ret
= wxFONTFAMILY_SWISS
; // contains "Sans"
200 else if (strstr( family_text
, "serif" ) != NULL
)
201 ret
= wxFONTFAMILY_ROMAN
; // contains "Serif"
202 else if (strncmp( family_text
, "times", 5 ) == 0)
203 ret
= wxFONTFAMILY_ROMAN
; // begins with "Times"
204 else if (strncmp( family_text
, "old", 3 ) == 0)
205 ret
= wxFONTFAMILY_DECORATIVE
; // Begins with "Old" - "Old English", "Old Town"
211 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
213 return wxFONTENCODING_SYSTEM
;
217 void wxNativeFontInfo::SetPointSize(int pointsize
)
219 pango_font_description_set_size( description
, pointsize
* PANGO_SCALE
);
222 void wxNativeFontInfo::SetStyle(wxFontStyle style
)
226 case wxFONTSTYLE_ITALIC
:
227 pango_font_description_set_style( description
, PANGO_STYLE_ITALIC
);
229 case wxFONTSTYLE_SLANT
:
230 pango_font_description_set_style( description
, PANGO_STYLE_OBLIQUE
);
233 wxFAIL_MSG( _T("unknown font style") );
235 case wxFONTSTYLE_NORMAL
:
236 pango_font_description_set_style( description
, PANGO_STYLE_NORMAL
);
241 void wxNativeFontInfo::SetWeight(wxFontWeight weight
)
245 case wxFONTWEIGHT_BOLD
:
246 pango_font_description_set_weight(description
, PANGO_WEIGHT_BOLD
);
248 case wxFONTWEIGHT_LIGHT
:
249 pango_font_description_set_weight(description
, PANGO_WEIGHT_LIGHT
);
252 wxFAIL_MSG( _T("unknown font weight") );
254 case wxFONTWEIGHT_NORMAL
:
255 pango_font_description_set_weight(description
, PANGO_WEIGHT_NORMAL
);
259 void wxNativeFontInfo::SetUnderlined(bool WXUNUSED(underlined
))
261 wxFAIL_MSG( _T("not implemented") );
264 bool wxNativeFontInfo::SetFaceName(const wxString
& facename
)
266 pango_font_description_set_family(description
, wxGTK_CONV_SYS(facename
));
270 void wxNativeFontInfo::SetFamily(wxFontFamily
WXUNUSED(family
))
272 wxFAIL_MSG( _T("not implemented") );
275 void wxNativeFontInfo::SetEncoding(wxFontEncoding
WXUNUSED(encoding
))
277 wxFAIL_MSG( _T("not implemented") );
282 bool wxNativeFontInfo::FromString(const wxString
& s
)
285 pango_font_description_free( description
);
287 // there is a bug in at least pango <= 1.13 which makes it (or its backends)
288 // segfault for very big point sizes and for negative point sizes.
289 // To workaround that bug for pango <= 1.13
290 // (see http://bugzilla.gnome.org/show_bug.cgi?id=340229)
291 // we do the check on the size here using same (arbitrary) limits used by
292 // pango > 1.13. Note that the segfault could happen also for pointsize
293 // smaller than this limit !!
295 const size_t pos
= str
.find_last_of(_T(" "));
297 if ( pos
!= wxString::npos
&& wxString(str
, pos
+ 1).ToDouble(&size
) )
302 else if ( size
>= 1E6
)
305 if ( !sizeStr
.empty() )
307 // replace the old size with the adjusted one
308 str
= wxString(s
, 0, pos
) + sizeStr
;
312 description
= pango_font_description_from_string( wxGTK_CONV_SYS( str
) );
314 // ensure a valid facename is selected
315 if (!wxFontEnumerator::IsValidFacename(GetFaceName()))
316 SetFaceName(wxNORMAL_FONT
->GetFaceName());
321 wxString
wxNativeFontInfo::ToString() const
323 wxGtkString
str(pango_font_description_to_string( description
));
325 return wxGTK_CONV_BACK(str
);
328 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
330 return FromString( s
);
333 wxString
wxNativeFontInfo::ToUserString() const
338 // ----------------------------------------------------------------------------
339 // wxNativeEncodingInfo
340 // ----------------------------------------------------------------------------
342 bool wxNativeEncodingInfo::FromString(const wxString
& WXUNUSED(s
))
347 wxString
wxNativeEncodingInfo::ToString() const
349 return wxEmptyString
;
352 bool wxTestFontEncoding(const wxNativeEncodingInfo
& WXUNUSED(info
))
357 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
358 wxNativeEncodingInfo
*info
)
360 // all encodings are available in GTK+ 2 because we translate text in any
361 // encoding to UTF-8 internally anyhow
362 info
->facename
.clear();
363 info
->encoding
= encoding
;
372 #pragma message disable nosimpint
375 #include <X11/Xlib.h>
378 #pragma message enable nosimpint
381 #elif defined(__WXGTK__)
382 // we have to declare struct tm to avoid problems with first forward
383 // declaring it in C code (glib.h included from gdk.h does it) and then
384 // defining it when time.h is included from the headers below - this is
385 // known not to work at least with Sun CC 6.01
392 // ----------------------------------------------------------------------------
394 // ----------------------------------------------------------------------------
396 static wxHashTable
*g_fontHash
= (wxHashTable
*) NULL
;
398 // ----------------------------------------------------------------------------
400 // ----------------------------------------------------------------------------
402 // define the functions to create and destroy native fonts for this toolkit
404 wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
406 return XLoadQueryFont((Display
*)wxGetDisplay(), fontSpec
);
409 inline void wxFreeFont(wxNativeFont font
)
411 XFreeFont((Display
*)wxGetDisplay(), (XFontStruct
*)font
);
413 #elif defined(__WXGTK__)
414 wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
416 // VZ: we should use gdk_fontset_load() instead of gdk_font_load()
417 // here to be able to display Japanese fonts correctly (at least
418 // this is what people report) but unfortunately doing it results
419 // in tons of warnings when using GTK with "normal" European
420 // languages and so we can't always do it and I don't know enough
421 // to determine when should this be done... (FIXME)
422 return gdk_font_load( wxConvertWX2MB(fontSpec
) );
425 inline void wxFreeFont(wxNativeFont font
)
427 gdk_font_unref(font
);
430 #error "Unknown GUI toolkit"
433 static bool wxTestFontSpec(const wxString
& fontspec
);
435 static wxNativeFont
wxLoadQueryFont(int pointSize
,
440 const wxString
& facename
,
441 const wxString
& xregistry
,
442 const wxString
& xencoding
,
443 wxString
* xFontName
);
445 // ============================================================================
447 // ============================================================================
449 // ----------------------------------------------------------------------------
450 // wxNativeEncodingInfo
451 // ----------------------------------------------------------------------------
453 // convert to/from the string representation: format is
454 // encodingid;registry;encoding[;facename]
455 bool wxNativeEncodingInfo::FromString(const wxString
& s
)
457 // use ";", not "-" because it may be part of encoding name
458 wxStringTokenizer
tokenizer(s
, _T(";"));
460 wxString encid
= tokenizer
.GetNextToken();
462 if ( !encid
.ToLong(&enc
) )
464 encoding
= (wxFontEncoding
)enc
;
466 xregistry
= tokenizer
.GetNextToken();
470 xencoding
= tokenizer
.GetNextToken();
475 facename
= tokenizer
.GetNextToken();
480 wxString
wxNativeEncodingInfo::ToString() const
483 s
<< (long)encoding
<< _T(';') << xregistry
<< _T(';') << xencoding
;
484 if ( !facename
.empty() )
486 s
<< _T(';') << facename
;
492 // ----------------------------------------------------------------------------
494 // ----------------------------------------------------------------------------
496 void wxNativeFontInfo::Init()
501 bool wxNativeFontInfo::FromString(const wxString
& s
)
503 wxStringTokenizer
tokenizer(s
, _T(";"));
506 wxString token
= tokenizer
.GetNextToken();
507 if ( token
!= _T('0') )
510 xFontName
= tokenizer
.GetNextToken();
512 // this should be the end
513 if ( tokenizer
.HasMoreTokens() )
516 return FromXFontName(xFontName
);
519 wxString
wxNativeFontInfo::ToString() const
522 return wxString::Format(_T("%d;%s"), 0, GetXFontName().c_str());
525 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
527 return FromXFontName(s
);
530 wxString
wxNativeFontInfo::ToUserString() const
532 return GetXFontName();
535 bool wxNativeFontInfo::HasElements() const
537 // we suppose that the foundry is never empty, so if it is it means that we
538 // had never parsed the XLFD
539 return !fontElements
[0].empty();
542 wxString
wxNativeFontInfo::GetXFontComponent(wxXLFDField field
) const
544 wxCHECK_MSG( field
< wxXLFD_MAX
, wxEmptyString
, _T("invalid XLFD field") );
546 if ( !HasElements() )
549 if ( !((wxNativeFontInfo
*)this)->FromXFontName(xFontName
) )
550 return wxEmptyString
;
553 return fontElements
[field
];
556 bool wxNativeFontInfo::FromXFontName(const wxString
& fontname
)
558 // TODO: we should be able to handle the font aliases here, but how?
559 wxStringTokenizer
tokenizer(fontname
, _T("-"));
561 // skip the leading, usually empty field (font name registry)
562 if ( !tokenizer
.HasMoreTokens() )
565 (void)tokenizer
.GetNextToken();
567 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
569 if ( !tokenizer
.HasMoreTokens() )
571 // not enough elements in the XLFD - or maybe an alias
575 wxString field
= tokenizer
.GetNextToken();
576 if ( !field
.empty() && field
!= _T('*') )
578 // we're really initialized now
582 fontElements
[n
] = field
;
585 // this should be all
586 if ( tokenizer
.HasMoreTokens() )
592 wxString
wxNativeFontInfo::GetXFontName() const
594 if ( xFontName
.empty() )
596 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
598 // replace the non specified elements with '*' except for the
599 // additional style which is usually just omitted
600 wxString elt
= fontElements
[n
];
601 if ( elt
.empty() && n
!= wxXLFD_ADDSTYLE
)
607 ((wxNativeFontInfo
*)this)->xFontName
<< _T('-') << elt
;
615 wxNativeFontInfo::SetXFontComponent(wxXLFDField field
, const wxString
& value
)
617 wxCHECK_RET( field
< wxXLFD_MAX
, _T("invalid XLFD field") );
619 // this class should be initialized with a valid font spec first and only
620 // then the fields may be modified!
621 wxASSERT_MSG( !IsDefault(), _T("can't modify an uninitialized XLFD") );
623 if ( !HasElements() )
626 if ( !((wxNativeFontInfo
*)this)->FromXFontName(xFontName
) )
628 wxFAIL_MSG( _T("can't set font element for invalid XLFD") );
634 fontElements
[field
] = value
;
636 // invalidate the XFLD, it doesn't correspond to the font elements any more
640 void wxNativeFontInfo::SetXFontName(const wxString
& xFontName_
)
642 // invalidate the font elements, GetXFontComponent() will reparse the XLFD
643 fontElements
[0].clear();
645 xFontName
= xFontName_
;
650 int wxNativeFontInfo::GetPointSize() const
652 const wxString s
= GetXFontComponent(wxXLFD_POINTSIZE
);
654 // return -1 to indicate that the size is unknown
656 return s
.ToLong(&l
) ? l
: -1;
659 wxFontStyle
wxNativeFontInfo::GetStyle() const
661 const wxString s
= GetXFontComponent(wxXLFD_SLANT
);
663 if ( s
.length() != 1 )
665 // it is really unknown but we don't have any way to return it from
667 return wxFONTSTYLE_NORMAL
;
673 // again, unknown but consider normal by default
676 return wxFONTSTYLE_NORMAL
;
679 return wxFONTSTYLE_ITALIC
;
682 return wxFONTSTYLE_SLANT
;
686 wxFontWeight
wxNativeFontInfo::GetWeight() const
688 const wxString s
= GetXFontComponent(wxXLFD_WEIGHT
).MakeLower();
689 if ( s
.find(_T("bold")) != wxString::npos
|| s
== _T("black") )
690 return wxFONTWEIGHT_BOLD
;
691 else if ( s
== _T("light") )
692 return wxFONTWEIGHT_LIGHT
;
694 return wxFONTWEIGHT_NORMAL
;
697 bool wxNativeFontInfo::GetUnderlined() const
699 // X fonts are never underlined
703 wxString
wxNativeFontInfo::GetFaceName() const
705 // wxWidgets facename probably more accurately corresponds to X family
706 return GetXFontComponent(wxXLFD_FAMILY
);
709 wxFontFamily
wxNativeFontInfo::GetFamily() const
711 // and wxWidgets family -- to X foundry, but we have to translate it to
712 // wxFontFamily somehow...
713 wxFAIL_MSG(_T("not implemented")); // GetXFontComponent(wxXLFD_FOUNDRY);
715 return wxFONTFAMILY_DEFAULT
;
718 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
720 // we already have the code for this but need to refactor it first
721 wxFAIL_MSG( _T("not implemented") );
723 return wxFONTENCODING_MAX
;
726 void wxNativeFontInfo::SetPointSize(int pointsize
)
728 SetXFontComponent(wxXLFD_POINTSIZE
, wxString::Format(_T("%d"), pointsize
));
731 void wxNativeFontInfo::SetStyle(wxFontStyle style
)
736 case wxFONTSTYLE_ITALIC
:
740 case wxFONTSTYLE_SLANT
:
744 case wxFONTSTYLE_NORMAL
:
748 wxFAIL_MSG( _T("unknown wxFontStyle in wxNativeFontInfo::SetStyle") );
752 SetXFontComponent(wxXLFD_SLANT
, s
);
755 void wxNativeFontInfo::SetWeight(wxFontWeight weight
)
760 case wxFONTWEIGHT_BOLD
:
764 case wxFONTWEIGHT_LIGHT
:
768 case wxFONTWEIGHT_NORMAL
:
773 wxFAIL_MSG( _T("unknown wxFontWeight in wxNativeFontInfo::SetWeight") );
777 SetXFontComponent(wxXLFD_WEIGHT
, s
);
780 void wxNativeFontInfo::SetUnderlined(bool WXUNUSED(underlined
))
782 // can't do this under X
785 bool wxNativeFontInfo::SetFaceName(const wxString
& facename
)
787 SetXFontComponent(wxXLFD_FAMILY
, facename
);
791 void wxNativeFontInfo::SetFamily(wxFontFamily
WXUNUSED(family
))
793 // wxFontFamily -> X foundry, anyone?
794 wxFAIL_MSG( _T("not implemented") );
796 // SetXFontComponent(wxXLFD_FOUNDRY, ...);
799 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding
)
801 wxNativeEncodingInfo info
;
802 if ( wxGetNativeFontEncoding(encoding
, &info
) )
804 SetXFontComponent(wxXLFD_ENCODING
, info
.xencoding
);
805 SetXFontComponent(wxXLFD_REGISTRY
, info
.xregistry
);
809 // ----------------------------------------------------------------------------
811 // ----------------------------------------------------------------------------
813 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
814 wxNativeEncodingInfo
*info
)
816 wxCHECK_MSG( info
, false, _T("bad pointer in wxGetNativeFontEncoding") );
818 if ( encoding
== wxFONTENCODING_DEFAULT
)
820 encoding
= wxFont::GetDefaultEncoding();
825 case wxFONTENCODING_ISO8859_1
:
826 case wxFONTENCODING_ISO8859_2
:
827 case wxFONTENCODING_ISO8859_3
:
828 case wxFONTENCODING_ISO8859_4
:
829 case wxFONTENCODING_ISO8859_5
:
830 case wxFONTENCODING_ISO8859_6
:
831 case wxFONTENCODING_ISO8859_7
:
832 case wxFONTENCODING_ISO8859_8
:
833 case wxFONTENCODING_ISO8859_9
:
834 case wxFONTENCODING_ISO8859_10
:
835 case wxFONTENCODING_ISO8859_11
:
836 case wxFONTENCODING_ISO8859_12
:
837 case wxFONTENCODING_ISO8859_13
:
838 case wxFONTENCODING_ISO8859_14
:
839 case wxFONTENCODING_ISO8859_15
:
841 int cp
= encoding
- wxFONTENCODING_ISO8859_1
+ 1;
842 info
->xregistry
= wxT("iso8859");
843 info
->xencoding
.Printf(wxT("%d"), cp
);
847 case wxFONTENCODING_UTF8
:
848 info
->xregistry
= wxT("iso10646");
849 info
->xencoding
= wxT("*");
852 case wxFONTENCODING_GB2312
:
853 info
->xregistry
= wxT("GB2312"); // or the otherway round?
854 info
->xencoding
= wxT("*");
857 case wxFONTENCODING_KOI8
:
858 case wxFONTENCODING_KOI8_U
:
859 info
->xregistry
= wxT("koi8");
861 // we don't make distinction between koi8-r, koi8-u and koi8-ru (so far)
862 info
->xencoding
= wxT("*");
865 case wxFONTENCODING_CP1250
:
866 case wxFONTENCODING_CP1251
:
867 case wxFONTENCODING_CP1252
:
868 case wxFONTENCODING_CP1253
:
869 case wxFONTENCODING_CP1254
:
870 case wxFONTENCODING_CP1255
:
871 case wxFONTENCODING_CP1256
:
872 case wxFONTENCODING_CP1257
:
874 int cp
= encoding
- wxFONTENCODING_CP1250
+ 1250;
875 info
->xregistry
= wxT("microsoft");
876 info
->xencoding
.Printf(wxT("cp%d"), cp
);
880 case wxFONTENCODING_EUC_JP
:
881 case wxFONTENCODING_SHIFT_JIS
:
882 info
->xregistry
= "jis*";
883 info
->xencoding
= "*";
886 case wxFONTENCODING_SYSTEM
:
888 info
->xencoding
= wxT("*");
892 // don't know how to translate this encoding into X fontspec
896 info
->encoding
= encoding
;
901 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
904 fontspec
.Printf(_T("-*-%s-*-*-*-*-*-*-*-*-*-*-%s-%s"),
905 !info
.facename
? _T("*") : info
.facename
.c_str(),
906 info
.xregistry
.c_str(),
907 info
.xencoding
.c_str());
909 return wxTestFontSpec(fontspec
);
912 // ----------------------------------------------------------------------------
913 // X-specific functions
914 // ----------------------------------------------------------------------------
916 wxNativeFont
wxLoadQueryNearestFont(int pointSize
,
921 const wxString
&facename
,
922 wxFontEncoding encoding
,
925 if ( encoding
== wxFONTENCODING_DEFAULT
)
927 encoding
= wxFont::GetDefaultEncoding();
930 // first determine the encoding - if the font doesn't exist at all in this
931 // encoding, it's useless to do all other approximations (i.e. size,
932 // family &c don't matter much)
933 wxNativeEncodingInfo info
;
934 if ( encoding
== wxFONTENCODING_SYSTEM
)
936 // This will always work so we don't test to save time
937 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
941 if ( !wxGetNativeFontEncoding(encoding
, &info
) ||
942 !wxTestFontEncoding(info
) )
945 if ( !wxFontMapper::Get()->GetAltForEncoding(encoding
, &info
) )
946 #endif // wxUSE_FONTMAP
948 // unspported encoding - replace it with the default
950 // NB: we can't just return 0 from here because wxGTK code doesn't
951 // check for it (i.e. it supposes that we'll always succeed),
952 // so it would provoke a crash
953 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
958 // OK, we have the correct xregistry/xencoding in info structure
959 wxNativeFont font
= 0;
961 // if we already have the X font name, try to use it
962 if( xFontName
&& !xFontName
->empty() )
965 // Make sure point size is correct for scale factor.
967 wxStringTokenizer
tokenizer(*xFontName
, _T("-"), wxTOKEN_RET_DELIMS
);
968 wxString newFontName
;
970 for(int i
= 0; i
< 8; i
++)
971 newFontName
+= tokenizer
.NextToken();
973 (void) tokenizer
.NextToken();
975 newFontName
+= wxString::Format(wxT("%d-"), pointSize
);
977 while(tokenizer
.HasMoreTokens())
978 newFontName
+= tokenizer
.GetNextToken();
980 font
= wxLoadFont(newFontName
);
983 *xFontName
= newFontName
;
988 // search up and down by stepsize 10
989 int max_size
= pointSize
+ 20 * (1 + (pointSize
/180));
990 int min_size
= pointSize
- 20 * (1 + (pointSize
/180));
992 int i
, round
; // counters
994 // first round: search for equal, then for smaller and for larger size with the given weight and style
995 int testweight
= weight
;
996 int teststyle
= style
;
998 for ( round
= 0; round
< 3; round
++ )
1000 // second round: use normal weight
1003 if ( testweight
!= wxNORMAL
)
1005 testweight
= wxNORMAL
;
1009 ++round
; // fall through to third round
1013 // third round: ... and use normal style
1016 if ( teststyle
!= wxNORMAL
)
1018 teststyle
= wxNORMAL
;
1025 // Search for equal or smaller size (approx.)
1026 for ( i
= pointSize
; !font
&& i
>= 10 && i
>= min_size
; i
-= 10 )
1028 font
= wxLoadQueryFont(i
, family
, teststyle
, testweight
, underlined
,
1029 facename
, info
.xregistry
, info
.xencoding
,
1033 // Search for larger size (approx.)
1034 for ( i
= pointSize
+ 10; !font
&& i
<= max_size
; i
+= 10 )
1036 font
= wxLoadQueryFont(i
, family
, teststyle
, testweight
, underlined
,
1037 facename
, info
.xregistry
, info
.xencoding
,
1042 // Try default family
1043 if ( !font
&& family
!= wxDEFAULT
)
1045 font
= wxLoadQueryFont(pointSize
, wxDEFAULT
, style
, weight
,
1046 underlined
, facename
,
1047 info
.xregistry
, info
.xencoding
,
1051 // ignore size, family, style and weight but try to find font with the
1052 // given facename and encoding
1055 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1056 underlined
, facename
,
1057 info
.xregistry
, info
.xencoding
,
1060 // ignore family as well
1063 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1064 underlined
, wxEmptyString
,
1065 info
.xregistry
, info
.xencoding
,
1068 // if it still failed, try to get the font of any size but
1069 // with the requested encoding: this can happen if the
1070 // encoding is only available in one size which happens to be
1071 // different from 120
1074 font
= wxLoadQueryFont(-1, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1075 false, wxEmptyString
,
1076 info
.xregistry
, info
.xencoding
,
1079 // this should never happen as we had tested for it in the
1080 // very beginning, but if it does, do return something non
1081 // NULL or we'd crash in wxFont code
1084 wxFAIL_MSG( _T("this encoding should be available!") );
1086 font
= wxLoadQueryFont(-1,
1087 wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1088 false, wxEmptyString
,
1100 // ----------------------------------------------------------------------------
1101 // private functions
1102 // ----------------------------------------------------------------------------
1104 // returns true if there are any fonts matching this font spec
1105 static bool wxTestFontSpec(const wxString
& fontspec
)
1107 // some X servers will fail to load this font because there are too many
1108 // matches so we must test explicitly for this
1109 if ( fontspec
== _T("-*-*-*-*-*-*-*-*-*-*-*-*-*-*") )
1114 wxNativeFont test
= (wxNativeFont
) g_fontHash
->Get( fontspec
);
1120 test
= wxLoadFont(fontspec
);
1121 g_fontHash
->Put( fontspec
, (wxObject
*) test
);
1135 static wxNativeFont
wxLoadQueryFont(int pointSize
,
1139 bool WXUNUSED(underlined
),
1140 const wxString
& facename
,
1141 const wxString
& xregistry
,
1142 const wxString
& xencoding
,
1143 wxString
* xFontName
)
1148 case wxDECORATIVE
: xfamily
= wxT("lucida"); break;
1149 case wxROMAN
: xfamily
= wxT("times"); break;
1150 case wxMODERN
: xfamily
= wxT("courier"); break;
1151 case wxSWISS
: xfamily
= wxT("helvetica"); break;
1152 case wxTELETYPE
: xfamily
= wxT("lucidatypewriter"); break;
1153 case wxSCRIPT
: xfamily
= wxT("utopia"); break;
1154 default: xfamily
= wxT("*");
1162 xweight
= MWLF_WEIGHT_BOLD
;
1167 xweight
= MWLF_WEIGHT_LIGHT
;
1172 xweight
= MWLF_WEIGHT_NORMAL
;
1178 xweight
= MWLF_WEIGHT_DEFAULT
;
1182 GR_SCREEN_INFO screenInfo
;
1183 GrGetScreenInfo(& screenInfo
);
1185 int yPixelsPerCM
= screenInfo
.ydpcm
;
1187 // A point is 1/72 of an inch.
1188 // An inch is 2.541 cm.
1189 // So pixelHeight = (pointSize / 72) (inches) * 2.541 (for cm) * yPixelsPerCM (for pixels)
1190 // In fact pointSize is 10 * the normal point size so
1193 int pixelHeight
= (int) ( (((float)pointSize
) / 720.0) * 2.541 * (float) yPixelsPerCM
) ;
1195 // An alternative: assume that the screen is 72 dpi.
1196 //int pixelHeight = (int) (((float)pointSize / 720.0) * 72.0) ;
1197 //int pixelHeight = (int) ((float)pointSize / 10.0) ;
1200 logFont
.lfHeight
= pixelHeight
;
1201 logFont
.lfWidth
= 0;
1202 logFont
.lfEscapement
= 0;
1203 logFont
.lfOrientation
= 0;
1204 logFont
.lfWeight
= xweight
;
1205 logFont
.lfItalic
= (style
== wxNORMAL
? 0 : 1) ;
1206 logFont
.lfUnderline
= 0;
1207 logFont
.lfStrikeOut
= 0;
1208 logFont
.lfCharSet
= MWLF_CHARSET_DEFAULT
; // TODO: select appropriate one
1209 logFont
.lfOutPrecision
= MWLF_TYPE_DEFAULT
;
1210 logFont
.lfClipPrecision
= 0; // Not used
1211 logFont
.lfRoman
= (family
== wxROMAN
? 1 : 0) ;
1212 logFont
.lfSerif
= (family
== wxSWISS
? 0 : 1) ;
1213 logFont
.lfSansSerif
= !logFont
.lfSerif
;
1214 logFont
.lfModern
= (family
== wxMODERN
? 1 : 0) ;
1215 logFont
.lfProportional
= (family
== wxTELETYPE
? 0 : 1) ;
1216 logFont
.lfOblique
= 0;
1217 logFont
.lfSmallCaps
= 0;
1218 logFont
.lfPitch
= 0; // 0 = default
1219 strcpy(logFont
.lfFaceName
, facename
.c_str());
1221 XFontStruct
* fontInfo
= (XFontStruct
*) malloc(sizeof(XFontStruct
));
1222 fontInfo
->fid
= GrCreateFont((GR_CHAR
*) facename
.c_str(), pixelHeight
, & logFont
);
1223 GrGetFontInfo(fontInfo
->fid
, & fontInfo
->info
);
1224 return (wxNativeFont
) fontInfo
;
1228 if (!facename
.empty())
1230 fontSpec
.Printf(wxT("-*-%s-*-*-normal-*-*-*-*-*-*-*-*-*"),
1233 if ( wxTestFontSpec(fontSpec
) )
1237 //else: no such family, use default one instead
1244 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1246 if ( wxTestFontSpec(fontSpec
) )
1251 // fall through - try wxITALIC now
1254 fontSpec
.Printf(wxT("-*-%s-*-i-*-*-*-*-*-*-*-*-*-*"),
1256 if ( wxTestFontSpec(fontSpec
) )
1260 else if ( style
== wxITALIC
) // and not wxSLANT
1263 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1265 if ( wxTestFontSpec(fontSpec
) )
1271 // no italic, no slant - leave default
1278 wxFAIL_MSG(_T("unknown font style"));
1279 // fall back to normal
1291 fontSpec
.Printf(wxT("-*-%s-bold-*-*-*-*-*-*-*-*-*-*-*"),
1293 if ( wxTestFontSpec(fontSpec
) )
1295 xweight
= wxT("bold");
1298 fontSpec
.Printf(wxT("-*-%s-heavy-*-*-*-*-*-*-*-*-*-*-*"),
1300 if ( wxTestFontSpec(fontSpec
) )
1302 xweight
= wxT("heavy");
1305 fontSpec
.Printf(wxT("-*-%s-extrabold-*-*-*-*-*-*-*-*-*-*-*"),
1307 if ( wxTestFontSpec(fontSpec
) )
1309 xweight
= wxT("extrabold");
1312 fontSpec
.Printf(wxT("-*-%s-demibold-*-*-*-*-*-*-*-*-*-*-*"),
1314 if ( wxTestFontSpec(fontSpec
) )
1316 xweight
= wxT("demibold");
1319 fontSpec
.Printf(wxT("-*-%s-black-*-*-*-*-*-*-*-*-*-*-*"),
1321 if ( wxTestFontSpec(fontSpec
) )
1323 xweight
= wxT("black");
1326 fontSpec
.Printf(wxT("-*-%s-ultrablack-*-*-*-*-*-*-*-*-*-*-*"),
1328 if ( wxTestFontSpec(fontSpec
) )
1330 xweight
= wxT("ultrablack");
1337 fontSpec
.Printf(wxT("-*-%s-light-*-*-*-*-*-*-*-*-*-*-*"),
1339 if ( wxTestFontSpec(fontSpec
) )
1341 xweight
= wxT("light");
1344 fontSpec
.Printf(wxT("-*-%s-thin-*-*-*-*-*-*-*-*-*-*-*"),
1346 if ( wxTestFontSpec(fontSpec
) )
1348 xweight
= wxT("thin");
1355 fontSpec
.Printf(wxT("-*-%s-medium-*-*-*-*-*-*-*-*-*-*-*"),
1357 if ( wxTestFontSpec(fontSpec
) )
1359 xweight
= wxT("medium");
1362 fontSpec
.Printf(wxT("-*-%s-normal-*-*-*-*-*-*-*-*-*-*-*"),
1364 if ( wxTestFontSpec(fontSpec
) )
1366 xweight
= wxT("normal");
1369 fontSpec
.Printf(wxT("-*-%s-regular-*-*-*-*-*-*-*-*-*-*-*"),
1371 if ( wxTestFontSpec(fontSpec
) )
1373 xweight
= wxT("regular");
1379 default: xweight
= wxT("*"); break;
1382 // if pointSize is -1, don't specify any
1384 if ( pointSize
== -1 )
1390 sizeSpec
.Printf(_T("%d"), pointSize
);
1393 // construct the X font spec from our data
1394 fontSpec
.Printf(wxT("-*-%s-%s-%s-normal-*-*-%s-*-*-*-*-%s-%s"),
1395 xfamily
.c_str(), xweight
.c_str(), xstyle
.c_str(),
1396 sizeSpec
.c_str(), xregistry
.c_str(), xencoding
.c_str());
1399 *xFontName
= fontSpec
;
1401 return wxLoadFont(fontSpec
);
1406 // ----------------------------------------------------------------------------
1408 // ----------------------------------------------------------------------------
1410 class wxFontModule
: public wxModule
1417 DECLARE_DYNAMIC_CLASS(wxFontModule
)
1420 IMPLEMENT_DYNAMIC_CLASS(wxFontModule
, wxModule
)
1422 bool wxFontModule::OnInit()
1424 g_fontHash
= new wxHashTable( wxKEY_STRING
);
1429 void wxFontModule::OnExit()
1433 g_fontHash
= (wxHashTable
*)NULL
;
1436 #endif // GTK 2.0/1.x