1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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"
28 #include "wx/font.h" // wxFont enums
29 #include "wx/encinfo.h"
32 #include "wx/fontutil.h"
33 #include "wx/fontmap.h"
34 #include "wx/tokenzr.h"
36 #include "wx/module.h"
40 #include "pango/pango.h"
43 #include "wx/gtk/private.h"
44 extern GtkWidget
*wxGetRootWindow();
46 #include "wx/x11/private.h"
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 void wxNativeFontInfo::Init()
59 wxNativeFontInfo::Init(const wxNativeFontInfo
& info
)
62 description
= pango_font_description_copy(info
.description
);
67 void wxNativeFontInfo::Free()
70 pango_font_description_free(description
);
73 int wxNativeFontInfo::GetPointSize() const
75 return pango_font_description_get_size( description
) / PANGO_SCALE
;
78 wxFontStyle
wxNativeFontInfo::GetStyle() const
80 wxFontStyle m_style
= wxFONTSTYLE_NORMAL
;
82 switch (pango_font_description_get_style( description
))
84 case PANGO_STYLE_NORMAL
:
85 m_style
= wxFONTSTYLE_NORMAL
;
87 case PANGO_STYLE_ITALIC
:
88 m_style
= wxFONTSTYLE_ITALIC
;
90 case PANGO_STYLE_OBLIQUE
:
91 m_style
= wxFONTSTYLE_SLANT
;
98 wxFontWeight
wxNativeFontInfo::GetWeight() const
101 // We seem to currently initialize only by string.
102 // In that case PANGO_FONT_MASK_WEIGHT is always set.
103 if (!(pango_font_description_get_set_fields(description
) & PANGO_FONT_MASK_WEIGHT
))
104 return wxFONTWEIGHT_NORMAL
;
107 PangoWeight pango_weight
= pango_font_description_get_weight( description
);
109 // Until the API can be changed the following ranges of weight values are used:
110 // wxFONTWEIGHT_LIGHT: 100 .. 349 - range of 250
111 // wxFONTWEIGHT_NORMAL: 350 .. 599 - range of 250
112 // wxFONTWEIGHT_BOLD: 600 .. 900 - range of 301 (600 is "semibold" already)
114 if (pango_weight
>= 600)
115 return wxFONTWEIGHT_BOLD
;
117 if (pango_weight
< 350)
118 return wxFONTWEIGHT_LIGHT
;
120 return wxFONTWEIGHT_NORMAL
;
123 bool wxNativeFontInfo::GetUnderlined() const
128 wxString
wxNativeFontInfo::GetFaceName() const
130 wxString tmp
= wxGTK_CONV_BACK( pango_font_description_get_family( description
) );
135 wxFontFamily
wxNativeFontInfo::GetFamily() const
137 wxFontFamily ret
= wxFONTFAMILY_DEFAULT
;
138 // note: not passing -1 as the 2nd parameter to g_ascii_strdown to work
139 // around a bug in the 64-bit glib shipped with solaris 10, -1 causes it
140 // to try to allocate 2^32 bytes.
141 const char *family_name
= pango_font_description_get_family( description
);
142 char *family_text
= g_ascii_strdown( family_name
, family_name
? strlen( family_name
) : 0 );
143 // Check for some common fonts, to salvage what we can from the current win32 centric wxFont API:
144 if (strncmp( family_text
, "monospace", 9 ) == 0)
145 ret
= wxFONTFAMILY_TELETYPE
; // begins with "Monospace"
146 else if (strncmp( family_text
, "courier", 7 ) == 0)
147 ret
= wxFONTFAMILY_TELETYPE
; // begins with "Courier"
148 #if defined(__WXGTK24__) || defined(HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE)
151 if (!gtk_check_version(2,4,0))
154 PangoFontFamily
**families
;
155 PangoFontFamily
*family
= NULL
;
157 pango_context_list_families(
159 gtk_widget_get_pango_context( wxGetRootWindow() ),
161 wxTheApp
->GetPangoContext(),
163 &families
, &n_families
);
165 for (int i
= 0;i
< n_families
;++i
)
167 if (g_ascii_strcasecmp(pango_font_family_get_name( families
[i
] ), pango_font_description_get_family( description
)) == 0 )
169 family
= families
[i
];
176 // Some gtk+ systems might query for a non-existing font from wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT)
177 // on initialization, don't assert until wxSystemSettings::GetFont is checked for this - MR
178 // wxASSERT_MSG( family, wxT("wxNativeFontInfo::GetFamily() - No appropriate PangoFontFamily found for ::description") );
180 //BCI: Cache the wxFontFamily inside the class. Validate cache with
181 //BCI: g_ascii_strcasecmp(pango_font_description_get_family(description), pango_font_family_get_name(family)) == 0
183 if (family
!= NULL
&& pango_font_family_is_monospace( family
))
184 ret
= wxFONTFAMILY_TELETYPE
; // is deemed a monospace font by pango
186 #endif // gtk24 || HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE
188 if (ret
== wxFONTFAMILY_DEFAULT
)
190 if (strstr( family_text
, "sans" ) != NULL
) // checked before serif, so that "* Sans Serif" fonts are detected correctly
191 ret
= wxFONTFAMILY_SWISS
; // contains "Sans"
192 else if (strstr( family_text
, "serif" ) != NULL
)
193 ret
= wxFONTFAMILY_ROMAN
; // contains "Serif"
194 else if (strncmp( family_text
, "times", 5 ) == 0)
195 ret
= wxFONTFAMILY_ROMAN
; // begins with "Times"
196 else if (strncmp( family_text
, "old", 3 ) == 0)
197 ret
= wxFONTFAMILY_DECORATIVE
; // Begins with "Old" - "Old English", "Old Town"
204 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
206 return wxFONTENCODING_SYSTEM
;
210 void wxNativeFontInfo::SetPointSize(int pointsize
)
212 pango_font_description_set_size( description
, pointsize
* PANGO_SCALE
);
215 void wxNativeFontInfo::SetStyle(wxFontStyle style
)
219 case wxFONTSTYLE_ITALIC
:
220 pango_font_description_set_style( description
, PANGO_STYLE_ITALIC
);
222 case wxFONTSTYLE_SLANT
:
223 pango_font_description_set_style( description
, PANGO_STYLE_OBLIQUE
);
226 wxFAIL_MSG( _T("unknown font style") );
228 case wxFONTSTYLE_NORMAL
:
229 pango_font_description_set_style( description
, PANGO_STYLE_NORMAL
);
234 void wxNativeFontInfo::SetWeight(wxFontWeight weight
)
238 case wxFONTWEIGHT_BOLD
:
239 pango_font_description_set_weight(description
, PANGO_WEIGHT_BOLD
);
241 case wxFONTWEIGHT_LIGHT
:
242 pango_font_description_set_weight(description
, PANGO_WEIGHT_LIGHT
);
245 wxFAIL_MSG( _T("unknown font weight") );
247 case wxFONTWEIGHT_NORMAL
:
248 pango_font_description_set_weight(description
, PANGO_WEIGHT_NORMAL
);
252 void wxNativeFontInfo::SetUnderlined(bool WXUNUSED(underlined
))
254 wxFAIL_MSG( _T("not implemented") );
257 void wxNativeFontInfo::SetFaceName(const wxString
& facename
)
259 pango_font_description_set_family( description
, wxGTK_CONV(facename
) );
262 void wxNativeFontInfo::SetFamily(wxFontFamily
WXUNUSED(family
))
264 wxFAIL_MSG( _T("not implemented") );
267 void wxNativeFontInfo::SetEncoding(wxFontEncoding
WXUNUSED(encoding
))
269 wxFAIL_MSG( _T("not implemented") );
274 bool wxNativeFontInfo::FromString(const wxString
& s
)
277 pango_font_description_free( description
);
279 description
= pango_font_description_from_string( wxGTK_CONV( s
) );
284 wxString
wxNativeFontInfo::ToString() const
286 char *str
= pango_font_description_to_string( description
);
287 wxString tmp
= wxGTK_CONV_BACK( str
);
293 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
295 return FromString( s
);
298 wxString
wxNativeFontInfo::ToUserString() const
303 // ----------------------------------------------------------------------------
304 // wxNativeEncodingInfo
305 // ----------------------------------------------------------------------------
307 bool wxNativeEncodingInfo::FromString(const wxString
& s
)
312 wxString
wxNativeEncodingInfo::ToString() const
314 return wxEmptyString
;
317 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
322 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
323 wxNativeEncodingInfo
*info
)
325 // we *must* return true for default encoding as otherwise wxFontMapper
326 // considers that we can't load any font and aborts with wxLogFatalError!
327 if ( encoding
== wxFONTENCODING_SYSTEM
)
329 info
->facename
.clear();
330 info
->encoding
= wxFONTENCODING_SYSTEM
;
333 // pretend that we support everything, it's better than to always return
334 // false as the old code did
342 #pragma message disable nosimpint
345 #include <X11/Xlib.h>
348 #pragma message enable nosimpint
351 #include "wx/utils.h" // for wxGetDisplay()
352 #elif defined(__WXGTK__)
353 // we have to declare struct tm to avoid problems with first forward
354 // declaring it in C code (glib.h included from gdk.h does it) and then
355 // defining it when time.h is included from the headers below - this is
356 // known not to work at least with Sun CC 6.01
363 // ----------------------------------------------------------------------------
365 // ----------------------------------------------------------------------------
367 static wxHashTable
*g_fontHash
= (wxHashTable
*) NULL
;
369 // ----------------------------------------------------------------------------
371 // ----------------------------------------------------------------------------
373 // define the functions to create and destroy native fonts for this toolkit
375 wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
377 return XLoadQueryFont((Display
*)wxGetDisplay(), fontSpec
);
380 inline void wxFreeFont(wxNativeFont font
)
382 XFreeFont((Display
*)wxGetDisplay(), (XFontStruct
*)font
);
384 #elif defined(__WXGTK__)
385 wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
387 // VZ: we should use gdk_fontset_load() instead of gdk_font_load()
388 // here to be able to display Japanese fonts correctly (at least
389 // this is what people report) but unfortunately doing it results
390 // in tons of warnings when using GTK with "normal" European
391 // languages and so we can't always do it and I don't know enough
392 // to determine when should this be done... (FIXME)
393 return gdk_font_load( wxConvertWX2MB(fontSpec
) );
396 inline void wxFreeFont(wxNativeFont font
)
398 gdk_font_unref(font
);
401 #error "Unknown GUI toolkit"
404 static bool wxTestFontSpec(const wxString
& fontspec
);
406 static wxNativeFont
wxLoadQueryFont(int pointSize
,
411 const wxString
& facename
,
412 const wxString
& xregistry
,
413 const wxString
& xencoding
,
414 wxString
* xFontName
);
416 // ============================================================================
418 // ============================================================================
420 // ----------------------------------------------------------------------------
421 // wxNativeEncodingInfo
422 // ----------------------------------------------------------------------------
424 // convert to/from the string representation: format is
425 // encodingid;registry;encoding[;facename]
426 bool wxNativeEncodingInfo::FromString(const wxString
& s
)
428 // use ";", not "-" because it may be part of encoding name
429 wxStringTokenizer
tokenizer(s
, _T(";"));
431 wxString encid
= tokenizer
.GetNextToken();
433 if ( !encid
.ToLong(&enc
) )
435 encoding
= (wxFontEncoding
)enc
;
437 xregistry
= tokenizer
.GetNextToken();
441 xencoding
= tokenizer
.GetNextToken();
446 facename
= tokenizer
.GetNextToken();
451 wxString
wxNativeEncodingInfo::ToString() const
454 s
<< (long)encoding
<< _T(';') << xregistry
<< _T(';') << xencoding
;
457 s
<< _T(';') << facename
;
463 // ----------------------------------------------------------------------------
465 // ----------------------------------------------------------------------------
467 void wxNativeFontInfo::Init()
472 bool wxNativeFontInfo::FromString(const wxString
& s
)
474 wxStringTokenizer
tokenizer(s
, _T(";"));
477 wxString token
= tokenizer
.GetNextToken();
478 if ( token
!= _T('0') )
481 xFontName
= tokenizer
.GetNextToken();
483 // this should be the end
484 if ( tokenizer
.HasMoreTokens() )
487 return FromXFontName(xFontName
);
490 wxString
wxNativeFontInfo::ToString() const
493 return wxString::Format(_T("%d;%s"), 0, GetXFontName().c_str());
496 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
498 return FromXFontName(s
);
501 wxString
wxNativeFontInfo::ToUserString() const
503 return GetXFontName();
506 bool wxNativeFontInfo::HasElements() const
508 // we suppose that the foundry is never empty, so if it is it means that we
509 // had never parsed the XLFD
510 return !fontElements
[0].empty();
513 wxString
wxNativeFontInfo::GetXFontComponent(wxXLFDField field
) const
515 wxCHECK_MSG( field
< wxXLFD_MAX
, _T(""), _T("invalid XLFD field") );
517 if ( !HasElements() )
520 if ( !((wxNativeFontInfo
*)this)->FromXFontName(xFontName
) )
524 return fontElements
[field
];
527 bool wxNativeFontInfo::FromXFontName(const wxString
& fontname
)
529 // TODO: we should be able to handle the font aliases here, but how?
530 wxStringTokenizer
tokenizer(fontname
, _T("-"));
532 // skip the leading, usually empty field (font name registry)
533 if ( !tokenizer
.HasMoreTokens() )
536 (void)tokenizer
.GetNextToken();
538 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
540 if ( !tokenizer
.HasMoreTokens() )
542 // not enough elements in the XLFD - or maybe an alias
546 wxString field
= tokenizer
.GetNextToken();
547 if ( !field
.empty() && field
!= _T('*') )
549 // we're really initialized now
553 fontElements
[n
] = field
;
556 // this should be all
557 if ( tokenizer
.HasMoreTokens() )
563 wxString
wxNativeFontInfo::GetXFontName() const
565 if ( xFontName
.empty() )
567 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
569 // replace the non specified elements with '*' except for the
570 // additional style which is usually just omitted
571 wxString elt
= fontElements
[n
];
572 if ( elt
.empty() && n
!= wxXLFD_ADDSTYLE
)
578 ((wxNativeFontInfo
*)this)->xFontName
<< _T('-') << elt
;
586 wxNativeFontInfo::SetXFontComponent(wxXLFDField field
, const wxString
& value
)
588 wxCHECK_RET( field
< wxXLFD_MAX
, _T("invalid XLFD field") );
590 // this class should be initialized with a valid font spec first and only
591 // then the fields may be modified!
592 wxASSERT_MSG( !IsDefault(), _T("can't modify an uninitialized XLFD") );
594 if ( !HasElements() )
597 if ( !((wxNativeFontInfo
*)this)->FromXFontName(xFontName
) )
599 wxFAIL_MSG( _T("can't set font element for invalid XLFD") );
605 fontElements
[field
] = value
;
607 // invalidate the XFLD, it doesn't correspond to the font elements any more
611 void wxNativeFontInfo::SetXFontName(const wxString
& xFontName_
)
613 // invalidate the font elements, GetXFontComponent() will reparse the XLFD
614 fontElements
[0].clear();
616 xFontName
= xFontName_
;
621 int wxNativeFontInfo::GetPointSize() const
623 const wxString s
= GetXFontComponent(wxXLFD_POINTSIZE
);
625 // return -1 to indicate that the size is unknown
627 return s
.ToLong(&l
) ? l
: -1;
630 wxFontStyle
wxNativeFontInfo::GetStyle() const
632 const wxString s
= GetXFontComponent(wxXLFD_SLANT
);
634 if ( s
.length() != 1 )
636 // it is really unknown but we don't have any way to return it from
638 return wxFONTSTYLE_NORMAL
;
644 // again, unknown but consider normal by default
647 return wxFONTSTYLE_NORMAL
;
650 return wxFONTSTYLE_ITALIC
;
653 return wxFONTSTYLE_SLANT
;
657 wxFontWeight
wxNativeFontInfo::GetWeight() const
659 const wxString s
= GetXFontComponent(wxXLFD_WEIGHT
).MakeLower();
660 if ( s
.find(_T("bold")) != wxString::npos
|| s
== _T("black") )
661 return wxFONTWEIGHT_BOLD
;
662 else if ( s
== _T("light") )
663 return wxFONTWEIGHT_LIGHT
;
665 return wxFONTWEIGHT_NORMAL
;
668 bool wxNativeFontInfo::GetUnderlined() const
670 // X fonts are never underlined
674 wxString
wxNativeFontInfo::GetFaceName() const
676 // wxWidgets facename probably more accurately corresponds to X family
677 return GetXFontComponent(wxXLFD_FAMILY
);
680 wxFontFamily
wxNativeFontInfo::GetFamily() const
682 // and wxWidgets family -- to X foundry, but we have to translate it to
683 // wxFontFamily somehow...
684 wxFAIL_MSG(_T("not implemented")); // GetXFontComponent(wxXLFD_FOUNDRY);
686 return wxFONTFAMILY_DEFAULT
;
689 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
691 // we already have the code for this but need to refactor it first
692 wxFAIL_MSG( _T("not implemented") );
694 return wxFONTENCODING_MAX
;
697 void wxNativeFontInfo::SetPointSize(int pointsize
)
699 SetXFontComponent(wxXLFD_POINTSIZE
, wxString::Format(_T("%d"), pointsize
));
702 void wxNativeFontInfo::SetStyle(wxFontStyle style
)
707 case wxFONTSTYLE_ITALIC
:
711 case wxFONTSTYLE_SLANT
:
715 case wxFONTSTYLE_NORMAL
:
719 wxFAIL_MSG( _T("unknown wxFontStyle in wxNativeFontInfo::SetStyle") );
723 SetXFontComponent(wxXLFD_SLANT
, s
);
726 void wxNativeFontInfo::SetWeight(wxFontWeight weight
)
731 case wxFONTWEIGHT_BOLD
:
735 case wxFONTWEIGHT_LIGHT
:
739 case wxFONTWEIGHT_NORMAL
:
744 wxFAIL_MSG( _T("unknown wxFontWeight in wxNativeFontInfo::SetWeight") );
748 SetXFontComponent(wxXLFD_WEIGHT
, s
);
751 void wxNativeFontInfo::SetUnderlined(bool WXUNUSED(underlined
))
753 // can't do this under X
756 void wxNativeFontInfo::SetFaceName(const wxString
& facename
)
758 SetXFontComponent(wxXLFD_FAMILY
, facename
);
761 void wxNativeFontInfo::SetFamily(wxFontFamily family
)
763 // wxFontFamily -> X foundry, anyone?
764 wxFAIL_MSG( _T("not implemented") );
766 // SetXFontComponent(wxXLFD_FOUNDRY, ...);
769 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding
)
771 wxNativeEncodingInfo info
;
772 if ( wxGetNativeFontEncoding(encoding
, &info
) )
774 SetXFontComponent(wxXLFD_ENCODING
, info
.xencoding
);
775 SetXFontComponent(wxXLFD_REGISTRY
, info
.xregistry
);
779 // ----------------------------------------------------------------------------
781 // ----------------------------------------------------------------------------
783 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
784 wxNativeEncodingInfo
*info
)
786 wxCHECK_MSG( info
, FALSE
, _T("bad pointer in wxGetNativeFontEncoding") );
788 if ( encoding
== wxFONTENCODING_DEFAULT
)
790 encoding
= wxFont::GetDefaultEncoding();
795 case wxFONTENCODING_ISO8859_1
:
796 case wxFONTENCODING_ISO8859_2
:
797 case wxFONTENCODING_ISO8859_3
:
798 case wxFONTENCODING_ISO8859_4
:
799 case wxFONTENCODING_ISO8859_5
:
800 case wxFONTENCODING_ISO8859_6
:
801 case wxFONTENCODING_ISO8859_7
:
802 case wxFONTENCODING_ISO8859_8
:
803 case wxFONTENCODING_ISO8859_9
:
804 case wxFONTENCODING_ISO8859_10
:
805 case wxFONTENCODING_ISO8859_11
:
806 case wxFONTENCODING_ISO8859_12
:
807 case wxFONTENCODING_ISO8859_13
:
808 case wxFONTENCODING_ISO8859_14
:
809 case wxFONTENCODING_ISO8859_15
:
811 int cp
= encoding
- wxFONTENCODING_ISO8859_1
+ 1;
812 info
->xregistry
= wxT("iso8859");
813 info
->xencoding
.Printf(wxT("%d"), cp
);
817 case wxFONTENCODING_UTF8
:
818 info
->xregistry
= wxT("iso10646");
819 info
->xencoding
= wxT("*");
822 case wxFONTENCODING_GB2312
:
823 info
->xregistry
= wxT("GB2312"); // or the otherway round?
824 info
->xencoding
= wxT("*");
827 case wxFONTENCODING_KOI8
:
828 case wxFONTENCODING_KOI8_U
:
829 info
->xregistry
= wxT("koi8");
831 // we don't make distinction between koi8-r, koi8-u and koi8-ru (so far)
832 info
->xencoding
= wxT("*");
835 case wxFONTENCODING_CP1250
:
836 case wxFONTENCODING_CP1251
:
837 case wxFONTENCODING_CP1252
:
838 case wxFONTENCODING_CP1253
:
839 case wxFONTENCODING_CP1254
:
840 case wxFONTENCODING_CP1255
:
841 case wxFONTENCODING_CP1256
:
842 case wxFONTENCODING_CP1257
:
844 int cp
= encoding
- wxFONTENCODING_CP1250
+ 1250;
845 info
->xregistry
= wxT("microsoft");
846 info
->xencoding
.Printf(wxT("cp%d"), cp
);
850 case wxFONTENCODING_EUC_JP
:
851 case wxFONTENCODING_SHIFT_JIS
:
852 info
->xregistry
= "jis*";
853 info
->xencoding
= "*";
856 case wxFONTENCODING_SYSTEM
:
858 info
->xencoding
= wxT("*");
862 // don't know how to translate this encoding into X fontspec
866 info
->encoding
= encoding
;
871 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
874 fontspec
.Printf(_T("-*-%s-*-*-*-*-*-*-*-*-*-*-%s-%s"),
875 !info
.facename
? _T("*") : info
.facename
.c_str(),
876 info
.xregistry
.c_str(),
877 info
.xencoding
.c_str());
879 return wxTestFontSpec(fontspec
);
882 // ----------------------------------------------------------------------------
883 // X-specific functions
884 // ----------------------------------------------------------------------------
886 wxNativeFont
wxLoadQueryNearestFont(int pointSize
,
891 const wxString
&facename
,
892 wxFontEncoding encoding
,
895 if ( encoding
== wxFONTENCODING_DEFAULT
)
897 encoding
= wxFont::GetDefaultEncoding();
900 // first determine the encoding - if the font doesn't exist at all in this
901 // encoding, it's useless to do all other approximations (i.e. size,
902 // family &c don't matter much)
903 wxNativeEncodingInfo info
;
904 if ( encoding
== wxFONTENCODING_SYSTEM
)
906 // This will always work so we don't test to save time
907 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
911 if ( !wxGetNativeFontEncoding(encoding
, &info
) ||
912 !wxTestFontEncoding(info
) )
915 if ( !wxFontMapper::Get()->GetAltForEncoding(encoding
, &info
) )
916 #endif // wxUSE_FONTMAP
918 // unspported encoding - replace it with the default
920 // NB: we can't just return 0 from here because wxGTK code doesn't
921 // check for it (i.e. it supposes that we'll always succeed),
922 // so it would provoke a crash
923 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
928 // OK, we have the correct xregistry/xencoding in info structure
929 wxNativeFont font
= 0;
931 // if we already have the X font name, try to use it
932 if( xFontName
&& !xFontName
->IsEmpty() )
935 // Make sure point size is correct for scale factor.
937 wxStringTokenizer
tokenizer(*xFontName
, _T("-"), wxTOKEN_RET_DELIMS
);
938 wxString newFontName
;
940 for(int i
= 0; i
< 8; i
++)
941 newFontName
+= tokenizer
.NextToken();
943 (void) tokenizer
.NextToken();
945 newFontName
+= wxString::Format(wxT("%d-"), pointSize
);
947 while(tokenizer
.HasMoreTokens())
948 newFontName
+= tokenizer
.GetNextToken();
950 font
= wxLoadFont(newFontName
);
953 *xFontName
= newFontName
;
958 // search up and down by stepsize 10
959 int max_size
= pointSize
+ 20 * (1 + (pointSize
/180));
960 int min_size
= pointSize
- 20 * (1 + (pointSize
/180));
962 int i
, round
; // counters
964 // first round: search for equal, then for smaller and for larger size with the given weight and style
965 int testweight
= weight
;
966 int teststyle
= style
;
968 for ( round
= 0; round
< 3; round
++ )
970 // second round: use normal weight
973 if ( testweight
!= wxNORMAL
)
975 testweight
= wxNORMAL
;
979 ++round
; // fall through to third round
983 // third round: ... and use normal style
986 if ( teststyle
!= wxNORMAL
)
988 teststyle
= wxNORMAL
;
995 // Search for equal or smaller size (approx.)
996 for ( i
= pointSize
; !font
&& i
>= 10 && i
>= min_size
; i
-= 10 )
998 font
= wxLoadQueryFont(i
, family
, teststyle
, testweight
, underlined
,
999 facename
, info
.xregistry
, info
.xencoding
,
1003 // Search for larger size (approx.)
1004 for ( i
= pointSize
+ 10; !font
&& i
<= max_size
; i
+= 10 )
1006 font
= wxLoadQueryFont(i
, family
, teststyle
, testweight
, underlined
,
1007 facename
, info
.xregistry
, info
.xencoding
,
1012 // Try default family
1013 if ( !font
&& family
!= wxDEFAULT
)
1015 font
= wxLoadQueryFont(pointSize
, wxDEFAULT
, style
, weight
,
1016 underlined
, facename
,
1017 info
.xregistry
, info
.xencoding
,
1021 // ignore size, family, style and weight but try to find font with the
1022 // given facename and encoding
1025 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1026 underlined
, facename
,
1027 info
.xregistry
, info
.xencoding
,
1030 // ignore family as well
1033 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1034 underlined
, wxEmptyString
,
1035 info
.xregistry
, info
.xencoding
,
1038 // if it still failed, try to get the font of any size but
1039 // with the requested encoding: this can happen if the
1040 // encoding is only available in one size which happens to be
1041 // different from 120
1044 font
= wxLoadQueryFont(-1, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1045 FALSE
, wxEmptyString
,
1046 info
.xregistry
, info
.xencoding
,
1049 // this should never happen as we had tested for it in the
1050 // very beginning, but if it does, do return something non
1051 // NULL or we'd crash in wxFont code
1054 wxFAIL_MSG( _T("this encoding should be available!") );
1056 font
= wxLoadQueryFont(-1,
1057 wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1058 FALSE
, wxEmptyString
,
1070 // ----------------------------------------------------------------------------
1071 // private functions
1072 // ----------------------------------------------------------------------------
1074 // returns TRUE if there are any fonts matching this font spec
1075 static bool wxTestFontSpec(const wxString
& fontspec
)
1077 // some X servers will fail to load this font because there are too many
1078 // matches so we must test explicitly for this
1079 if ( fontspec
== _T("-*-*-*-*-*-*-*-*-*-*-*-*-*-*") )
1084 wxNativeFont test
= (wxNativeFont
) g_fontHash
->Get( fontspec
);
1090 test
= wxLoadFont(fontspec
);
1091 g_fontHash
->Put( fontspec
, (wxObject
*) test
);
1105 static wxNativeFont
wxLoadQueryFont(int pointSize
,
1109 bool WXUNUSED(underlined
),
1110 const wxString
& facename
,
1111 const wxString
& xregistry
,
1112 const wxString
& xencoding
,
1113 wxString
* xFontName
)
1118 case wxDECORATIVE
: xfamily
= wxT("lucida"); break;
1119 case wxROMAN
: xfamily
= wxT("times"); break;
1120 case wxMODERN
: xfamily
= wxT("courier"); break;
1121 case wxSWISS
: xfamily
= wxT("helvetica"); break;
1122 case wxTELETYPE
: xfamily
= wxT("lucidatypewriter"); break;
1123 case wxSCRIPT
: xfamily
= wxT("utopia"); break;
1124 default: xfamily
= wxT("*");
1132 xweight
= MWLF_WEIGHT_BOLD
;
1137 xweight
= MWLF_WEIGHT_LIGHT
;
1142 xweight
= MWLF_WEIGHT_NORMAL
;
1148 xweight
= MWLF_WEIGHT_DEFAULT
;
1152 GR_SCREEN_INFO screenInfo
;
1153 GrGetScreenInfo(& screenInfo
);
1155 int yPixelsPerCM
= screenInfo
.ydpcm
;
1157 // A point is 1/72 of an inch.
1158 // An inch is 2.541 cm.
1159 // So pixelHeight = (pointSize / 72) (inches) * 2.541 (for cm) * yPixelsPerCM (for pixels)
1160 // In fact pointSize is 10 * the normal point size so
1163 int pixelHeight
= (int) ( (((float)pointSize
) / 720.0) * 2.541 * (float) yPixelsPerCM
) ;
1165 // An alternative: assume that the screen is 72 dpi.
1166 //int pixelHeight = (int) (((float)pointSize / 720.0) * 72.0) ;
1167 //int pixelHeight = (int) ((float)pointSize / 10.0) ;
1170 logFont
.lfHeight
= pixelHeight
;
1171 logFont
.lfWidth
= 0;
1172 logFont
.lfEscapement
= 0;
1173 logFont
.lfOrientation
= 0;
1174 logFont
.lfWeight
= xweight
;
1175 logFont
.lfItalic
= (style
== wxNORMAL
? 0 : 1) ;
1176 logFont
.lfUnderline
= 0;
1177 logFont
.lfStrikeOut
= 0;
1178 logFont
.lfCharSet
= MWLF_CHARSET_DEFAULT
; // TODO: select appropriate one
1179 logFont
.lfOutPrecision
= MWLF_TYPE_DEFAULT
;
1180 logFont
.lfClipPrecision
= 0; // Not used
1181 logFont
.lfRoman
= (family
== wxROMAN
? 1 : 0) ;
1182 logFont
.lfSerif
= (family
== wxSWISS
? 0 : 1) ;
1183 logFont
.lfSansSerif
= !logFont
.lfSerif
;
1184 logFont
.lfModern
= (family
== wxMODERN
? 1 : 0) ;
1185 logFont
.lfProportional
= (family
== wxTELETYPE
? 0 : 1) ;
1186 logFont
.lfOblique
= 0;
1187 logFont
.lfSmallCaps
= 0;
1188 logFont
.lfPitch
= 0; // 0 = default
1189 strcpy(logFont
.lfFaceName
, facename
.c_str());
1191 XFontStruct
* fontInfo
= (XFontStruct
*) malloc(sizeof(XFontStruct
));
1192 fontInfo
->fid
= GrCreateFont((GR_CHAR
*) facename
.c_str(), pixelHeight
, & logFont
);
1193 GrGetFontInfo(fontInfo
->fid
, & fontInfo
->info
);
1194 return (wxNativeFont
) fontInfo
;
1198 if (!facename
.IsEmpty())
1200 fontSpec
.Printf(wxT("-*-%s-*-*-normal-*-*-*-*-*-*-*-*-*"),
1203 if ( wxTestFontSpec(fontSpec
) )
1207 //else: no such family, use default one instead
1214 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1216 if ( wxTestFontSpec(fontSpec
) )
1221 // fall through - try wxITALIC now
1224 fontSpec
.Printf(wxT("-*-%s-*-i-*-*-*-*-*-*-*-*-*-*"),
1226 if ( wxTestFontSpec(fontSpec
) )
1230 else if ( style
== wxITALIC
) // and not wxSLANT
1233 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1235 if ( wxTestFontSpec(fontSpec
) )
1241 // no italic, no slant - leave default
1248 wxFAIL_MSG(_T("unknown font style"));
1249 // fall back to normal
1261 fontSpec
.Printf(wxT("-*-%s-bold-*-*-*-*-*-*-*-*-*-*-*"),
1263 if ( wxTestFontSpec(fontSpec
) )
1265 xweight
= wxT("bold");
1268 fontSpec
.Printf(wxT("-*-%s-heavy-*-*-*-*-*-*-*-*-*-*-*"),
1270 if ( wxTestFontSpec(fontSpec
) )
1272 xweight
= wxT("heavy");
1275 fontSpec
.Printf(wxT("-*-%s-extrabold-*-*-*-*-*-*-*-*-*-*-*"),
1277 if ( wxTestFontSpec(fontSpec
) )
1279 xweight
= wxT("extrabold");
1282 fontSpec
.Printf(wxT("-*-%s-demibold-*-*-*-*-*-*-*-*-*-*-*"),
1284 if ( wxTestFontSpec(fontSpec
) )
1286 xweight
= wxT("demibold");
1289 fontSpec
.Printf(wxT("-*-%s-black-*-*-*-*-*-*-*-*-*-*-*"),
1291 if ( wxTestFontSpec(fontSpec
) )
1293 xweight
= wxT("black");
1296 fontSpec
.Printf(wxT("-*-%s-ultrablack-*-*-*-*-*-*-*-*-*-*-*"),
1298 if ( wxTestFontSpec(fontSpec
) )
1300 xweight
= wxT("ultrablack");
1307 fontSpec
.Printf(wxT("-*-%s-light-*-*-*-*-*-*-*-*-*-*-*"),
1309 if ( wxTestFontSpec(fontSpec
) )
1311 xweight
= wxT("light");
1314 fontSpec
.Printf(wxT("-*-%s-thin-*-*-*-*-*-*-*-*-*-*-*"),
1316 if ( wxTestFontSpec(fontSpec
) )
1318 xweight
= wxT("thin");
1325 fontSpec
.Printf(wxT("-*-%s-medium-*-*-*-*-*-*-*-*-*-*-*"),
1327 if ( wxTestFontSpec(fontSpec
) )
1329 xweight
= wxT("medium");
1332 fontSpec
.Printf(wxT("-*-%s-normal-*-*-*-*-*-*-*-*-*-*-*"),
1334 if ( wxTestFontSpec(fontSpec
) )
1336 xweight
= wxT("normal");
1339 fontSpec
.Printf(wxT("-*-%s-regular-*-*-*-*-*-*-*-*-*-*-*"),
1341 if ( wxTestFontSpec(fontSpec
) )
1343 xweight
= wxT("regular");
1349 default: xweight
= wxT("*"); break;
1352 // if pointSize is -1, don't specify any
1354 if ( pointSize
== -1 )
1360 sizeSpec
.Printf(_T("%d"), pointSize
);
1363 // construct the X font spec from our data
1364 fontSpec
.Printf(wxT("-*-%s-%s-%s-normal-*-*-%s-*-*-*-*-%s-%s"),
1365 xfamily
.c_str(), xweight
.c_str(), xstyle
.c_str(),
1366 sizeSpec
.c_str(), xregistry
.c_str(), xencoding
.c_str());
1369 *xFontName
= fontSpec
;
1371 return wxLoadFont(fontSpec
);
1376 // ----------------------------------------------------------------------------
1378 // ----------------------------------------------------------------------------
1380 class wxFontModule
: public wxModule
1387 DECLARE_DYNAMIC_CLASS(wxFontModule
)
1390 IMPLEMENT_DYNAMIC_CLASS(wxFontModule
, wxModule
)
1392 bool wxFontModule::OnInit()
1394 g_fontHash
= new wxHashTable( wxKEY_STRING
);
1399 void wxFontModule::OnExit()
1403 g_fontHash
= (wxHashTable
*)NULL
;
1406 #endif // GTK 2.0/1.x