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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "fontutil.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
32 #include "wx/font.h" // wxFont enums
33 #include "wx/encinfo.h"
36 #include "wx/fontutil.h"
37 #include "wx/fontmap.h"
38 #include "wx/tokenzr.h"
40 #include "wx/module.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 char *family_text
= g_ascii_strdown( pango_font_description_get_family( description
), -1 );
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 wxASSERT_MSG( family
, wxT("wxNativeFontInfo::GetFamily() - No appropriate PangoFontFamily found for ::description") );
178 //BCI: Cache the wxFontFamily inside the class. Validate cache with
179 //BCI: g_ascii_strcasecmp(pango_font_description_get_family(description), pango_font_family_get_name(family)) == 0
181 if (family
!= NULL
&& pango_font_family_is_monospace( family
))
182 ret
= wxFONTFAMILY_TELETYPE
; // is deemed a monospace font by pango
184 #endif // gtk24 || HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE
186 if (ret
== wxFONTFAMILY_DEFAULT
)
188 if (strstr( family_text
, "sans" ) != NULL
) // checked before serif, so that "* Sans Serif" fonts are detected correctly
189 ret
= wxFONTFAMILY_SWISS
; // contains "Sans"
190 else if (strstr( family_text
, "serif" ) != NULL
)
191 ret
= wxFONTFAMILY_ROMAN
; // contains "Serif"
192 else if (strncmp( family_text
, "times", 5 ) == 0)
193 ret
= wxFONTFAMILY_ROMAN
; // begins with "Times"
194 else if (strncmp( family_text
, "old", 3 ) == 0)
195 ret
= wxFONTFAMILY_DECORATIVE
; // Begins with "Old" - "Old English", "Old Town"
202 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
204 return wxFONTENCODING_SYSTEM
;
208 void wxNativeFontInfo::SetPointSize(int pointsize
)
210 pango_font_description_set_size( description
, pointsize
* PANGO_SCALE
);
213 void wxNativeFontInfo::SetStyle(wxFontStyle style
)
217 case wxFONTSTYLE_ITALIC
:
218 pango_font_description_set_style( description
, PANGO_STYLE_ITALIC
);
220 case wxFONTSTYLE_SLANT
:
221 pango_font_description_set_style( description
, PANGO_STYLE_OBLIQUE
);
224 wxFAIL_MSG( _T("unknown font style") );
226 case wxFONTSTYLE_NORMAL
:
227 pango_font_description_set_style( description
, PANGO_STYLE_NORMAL
);
232 void wxNativeFontInfo::SetWeight(wxFontWeight weight
)
236 case wxFONTWEIGHT_BOLD
:
237 pango_font_description_set_weight(description
, PANGO_WEIGHT_BOLD
);
239 case wxFONTWEIGHT_LIGHT
:
240 pango_font_description_set_weight(description
, PANGO_WEIGHT_LIGHT
);
243 wxFAIL_MSG( _T("unknown font weight") );
245 case wxFONTWEIGHT_NORMAL
:
246 pango_font_description_set_weight(description
, PANGO_WEIGHT_NORMAL
);
250 void wxNativeFontInfo::SetUnderlined(bool WXUNUSED(underlined
))
252 wxFAIL_MSG( _T("not implemented") );
255 void wxNativeFontInfo::SetFaceName(wxString facename
)
257 pango_font_description_set_family( description
, wxGTK_CONV(facename
) );
260 void wxNativeFontInfo::SetFamily(wxFontFamily
WXUNUSED(family
))
262 wxFAIL_MSG( _T("not implemented") );
265 void wxNativeFontInfo::SetEncoding(wxFontEncoding
WXUNUSED(encoding
))
267 wxFAIL_MSG( _T("not implemented") );
272 bool wxNativeFontInfo::FromString(const wxString
& s
)
275 pango_font_description_free( description
);
277 description
= pango_font_description_from_string( wxGTK_CONV( s
) );
282 wxString
wxNativeFontInfo::ToString() const
284 char *str
= pango_font_description_to_string( description
);
285 wxString tmp
= wxGTK_CONV_BACK( str
);
291 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
293 return FromString( s
);
296 wxString
wxNativeFontInfo::ToUserString() const
301 // ----------------------------------------------------------------------------
302 // wxNativeEncodingInfo
303 // ----------------------------------------------------------------------------
305 bool wxNativeEncodingInfo::FromString(const wxString
& s
)
310 wxString
wxNativeEncodingInfo::ToString() const
312 return wxEmptyString
;
315 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
320 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
321 wxNativeEncodingInfo
*info
)
323 // we *must* return true for default encoding as otherwise wxFontMapper
324 // considers that we can't load any font and aborts with wxLogFatalError!
325 if ( encoding
== wxFONTENCODING_SYSTEM
)
327 info
->facename
.clear();
328 info
->encoding
= wxFONTENCODING_SYSTEM
;
331 // pretend that we support everything, it's better than to always return
332 // false as the old code did
340 #pragma message disable nosimpint
343 #include <X11/Xlib.h>
346 #pragma message enable nosimpint
349 #include "wx/utils.h" // for wxGetDisplay()
350 #elif defined(__WXGTK__)
351 // we have to declare struct tm to avoid problems with first forward
352 // declaring it in C code (glib.h included from gdk.h does it) and then
353 // defining it when time.h is included from the headers below - this is
354 // known not to work at least with Sun CC 6.01
361 // ----------------------------------------------------------------------------
363 // ----------------------------------------------------------------------------
365 static wxHashTable
*g_fontHash
= (wxHashTable
*) NULL
;
367 // ----------------------------------------------------------------------------
369 // ----------------------------------------------------------------------------
371 // define the functions to create and destroy native fonts for this toolkit
373 wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
375 return XLoadQueryFont((Display
*)wxGetDisplay(), fontSpec
);
378 inline void wxFreeFont(wxNativeFont font
)
380 XFreeFont((Display
*)wxGetDisplay(), (XFontStruct
*)font
);
382 #elif defined(__WXGTK__)
383 wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
385 // VZ: we should use gdk_fontset_load() instead of gdk_font_load()
386 // here to be able to display Japanese fonts correctly (at least
387 // this is what people report) but unfortunately doing it results
388 // in tons of warnings when using GTK with "normal" European
389 // languages and so we can't always do it and I don't know enough
390 // to determine when should this be done... (FIXME)
391 return gdk_font_load( wxConvertWX2MB(fontSpec
) );
394 inline void wxFreeFont(wxNativeFont font
)
396 gdk_font_unref(font
);
399 #error "Unknown GUI toolkit"
402 static bool wxTestFontSpec(const wxString
& fontspec
);
404 static wxNativeFont
wxLoadQueryFont(int pointSize
,
409 const wxString
& facename
,
410 const wxString
& xregistry
,
411 const wxString
& xencoding
,
412 wxString
* xFontName
);
414 // ============================================================================
416 // ============================================================================
418 // ----------------------------------------------------------------------------
419 // wxNativeEncodingInfo
420 // ----------------------------------------------------------------------------
422 // convert to/from the string representation: format is
423 // encodingid;registry;encoding[;facename]
424 bool wxNativeEncodingInfo::FromString(const wxString
& s
)
426 // use ";", not "-" because it may be part of encoding name
427 wxStringTokenizer
tokenizer(s
, _T(";"));
429 wxString encid
= tokenizer
.GetNextToken();
431 if ( !encid
.ToLong(&enc
) )
433 encoding
= (wxFontEncoding
)enc
;
435 xregistry
= tokenizer
.GetNextToken();
439 xencoding
= tokenizer
.GetNextToken();
444 facename
= tokenizer
.GetNextToken();
449 wxString
wxNativeEncodingInfo::ToString() const
452 s
<< (long)encoding
<< _T(';') << xregistry
<< _T(';') << xencoding
;
455 s
<< _T(';') << facename
;
461 // ----------------------------------------------------------------------------
463 // ----------------------------------------------------------------------------
465 void wxNativeFontInfo::Init()
470 bool wxNativeFontInfo::FromString(const wxString
& s
)
472 wxStringTokenizer
tokenizer(s
, _T(";"));
475 wxString token
= tokenizer
.GetNextToken();
476 if ( token
!= _T('0') )
479 xFontName
= tokenizer
.GetNextToken();
481 // this should be the end
482 if ( tokenizer
.HasMoreTokens() )
485 return FromXFontName(xFontName
);
488 wxString
wxNativeFontInfo::ToString() const
491 return wxString::Format(_T("%d;%s"), 0, GetXFontName().c_str());
494 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
496 return FromXFontName(s
);
499 wxString
wxNativeFontInfo::ToUserString() const
501 return GetXFontName();
504 bool wxNativeFontInfo::HasElements() const
506 // we suppose that the foundry is never empty, so if it is it means that we
507 // had never parsed the XLFD
508 return !fontElements
[0].empty();
511 wxString
wxNativeFontInfo::GetXFontComponent(wxXLFDField field
) const
513 wxCHECK_MSG( field
< wxXLFD_MAX
, _T(""), _T("invalid XLFD field") );
515 if ( !HasElements() )
518 if ( !((wxNativeFontInfo
*)this)->FromXFontName(xFontName
) )
522 return fontElements
[field
];
525 bool wxNativeFontInfo::FromXFontName(const wxString
& fontname
)
527 // TODO: we should be able to handle the font aliases here, but how?
528 wxStringTokenizer
tokenizer(fontname
, _T("-"));
530 // skip the leading, usually empty field (font name registry)
531 if ( !tokenizer
.HasMoreTokens() )
534 (void)tokenizer
.GetNextToken();
536 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
538 if ( !tokenizer
.HasMoreTokens() )
540 // not enough elements in the XLFD - or maybe an alias
544 wxString field
= tokenizer
.GetNextToken();
545 if ( !field
.empty() && field
!= _T('*') )
547 // we're really initialized now
551 fontElements
[n
] = field
;
554 // this should be all
555 if ( tokenizer
.HasMoreTokens() )
561 wxString
wxNativeFontInfo::GetXFontName() const
563 if ( xFontName
.empty() )
565 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
567 // replace the non specified elements with '*' except for the
568 // additional style which is usually just omitted
569 wxString elt
= fontElements
[n
];
570 if ( elt
.empty() && n
!= wxXLFD_ADDSTYLE
)
576 ((wxNativeFontInfo
*)this)->xFontName
<< _T('-') << elt
;
584 wxNativeFontInfo::SetXFontComponent(wxXLFDField field
, const wxString
& value
)
586 wxCHECK_RET( field
< wxXLFD_MAX
, _T("invalid XLFD field") );
588 // this class should be initialized with a valid font spec first and only
589 // then the fields may be modified!
590 wxASSERT_MSG( !IsDefault(), _T("can't modify an uninitialized XLFD") );
592 if ( !HasElements() )
595 if ( !((wxNativeFontInfo
*)this)->FromXFontName(xFontName
) )
597 wxFAIL_MSG( _T("can't set font element for invalid XLFD") );
603 fontElements
[field
] = value
;
605 // invalidate the XFLD, it doesn't correspond to the font elements any more
609 void wxNativeFontInfo::SetXFontName(const wxString
& xFontName_
)
611 // invalidate the font elements, GetXFontComponent() will reparse the XLFD
612 fontElements
[0].clear();
614 xFontName
= xFontName_
;
619 int wxNativeFontInfo::GetPointSize() const
621 const wxString s
= GetXFontComponent(wxXLFD_POINTSIZE
);
623 // return -1 to indicate that the size is unknown
625 return s
.ToLong(&l
) ? l
: -1;
628 wxFontStyle
wxNativeFontInfo::GetStyle() const
630 const wxString s
= GetXFontComponent(wxXLFD_SLANT
);
632 if ( s
.length() != 1 )
634 // it is really unknown but we don't have any way to return it from
636 return wxFONTSTYLE_NORMAL
;
642 // again, unknown but consider normal by default
645 return wxFONTSTYLE_NORMAL
;
648 return wxFONTSTYLE_ITALIC
;
651 return wxFONTSTYLE_SLANT
;
655 wxFontWeight
wxNativeFontInfo::GetWeight() const
657 const wxString s
= GetXFontComponent(wxXLFD_WEIGHT
).MakeLower();
658 if ( s
.find(_T("bold")) != wxString::npos
|| s
== _T("black") )
659 return wxFONTWEIGHT_BOLD
;
660 else if ( s
== _T("light") )
661 return wxFONTWEIGHT_LIGHT
;
663 return wxFONTWEIGHT_NORMAL
;
666 bool wxNativeFontInfo::GetUnderlined() const
668 // X fonts are never underlined
672 wxString
wxNativeFontInfo::GetFaceName() const
674 // wxWidgets facename probably more accurately corresponds to X family
675 return GetXFontComponent(wxXLFD_FAMILY
);
678 wxFontFamily
wxNativeFontInfo::GetFamily() const
680 // and wxWidgets family -- to X foundry, but we have to translate it to
681 // wxFontFamily somehow...
682 wxFAIL_MSG(_T("not implemented")); // GetXFontComponent(wxXLFD_FOUNDRY);
684 return wxFONTFAMILY_DEFAULT
;
687 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
689 // we already have the code for this but need to refactor it first
690 wxFAIL_MSG( _T("not implemented") );
692 return wxFONTENCODING_MAX
;
695 void wxNativeFontInfo::SetPointSize(int pointsize
)
697 SetXFontComponent(wxXLFD_POINTSIZE
, wxString::Format(_T("%d"), pointsize
));
700 void wxNativeFontInfo::SetStyle(wxFontStyle style
)
705 case wxFONTSTYLE_ITALIC
:
709 case wxFONTSTYLE_SLANT
:
713 case wxFONTSTYLE_NORMAL
:
717 wxFAIL_MSG( _T("unknown wxFontStyle in wxNativeFontInfo::SetStyle") );
721 SetXFontComponent(wxXLFD_SLANT
, s
);
724 void wxNativeFontInfo::SetWeight(wxFontWeight weight
)
729 case wxFONTWEIGHT_BOLD
:
733 case wxFONTWEIGHT_LIGHT
:
737 case wxFONTWEIGHT_NORMAL
:
742 wxFAIL_MSG( _T("unknown wxFontWeight in wxNativeFontInfo::SetWeight") );
746 SetXFontComponent(wxXLFD_WEIGHT
, s
);
749 void wxNativeFontInfo::SetUnderlined(bool WXUNUSED(underlined
))
751 // can't do this under X
754 void wxNativeFontInfo::SetFaceName(wxString facename
)
756 SetXFontComponent(wxXLFD_FAMILY
, facename
);
759 void wxNativeFontInfo::SetFamily(wxFontFamily family
)
761 // wxFontFamily -> X foundry, anyone?
762 wxFAIL_MSG( _T("not implemented") );
764 // SetXFontComponent(wxXLFD_FOUNDRY, ...);
767 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding
)
769 wxNativeEncodingInfo info
;
770 if ( wxGetNativeFontEncoding(encoding
, &info
) )
772 SetXFontComponent(wxXLFD_ENCODING
, info
.xencoding
);
773 SetXFontComponent(wxXLFD_REGISTRY
, info
.xregistry
);
777 // ----------------------------------------------------------------------------
779 // ----------------------------------------------------------------------------
781 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
782 wxNativeEncodingInfo
*info
)
784 wxCHECK_MSG( info
, FALSE
, _T("bad pointer in wxGetNativeFontEncoding") );
786 if ( encoding
== wxFONTENCODING_DEFAULT
)
788 encoding
= wxFont::GetDefaultEncoding();
793 case wxFONTENCODING_ISO8859_1
:
794 case wxFONTENCODING_ISO8859_2
:
795 case wxFONTENCODING_ISO8859_3
:
796 case wxFONTENCODING_ISO8859_4
:
797 case wxFONTENCODING_ISO8859_5
:
798 case wxFONTENCODING_ISO8859_6
:
799 case wxFONTENCODING_ISO8859_7
:
800 case wxFONTENCODING_ISO8859_8
:
801 case wxFONTENCODING_ISO8859_9
:
802 case wxFONTENCODING_ISO8859_10
:
803 case wxFONTENCODING_ISO8859_11
:
804 case wxFONTENCODING_ISO8859_12
:
805 case wxFONTENCODING_ISO8859_13
:
806 case wxFONTENCODING_ISO8859_14
:
807 case wxFONTENCODING_ISO8859_15
:
809 int cp
= encoding
- wxFONTENCODING_ISO8859_1
+ 1;
810 info
->xregistry
= wxT("iso8859");
811 info
->xencoding
.Printf(wxT("%d"), cp
);
815 case wxFONTENCODING_UTF8
:
816 info
->xregistry
= wxT("iso10646");
817 info
->xencoding
= wxT("*");
820 case wxFONTENCODING_GB2312
:
821 info
->xregistry
= wxT("GB2312"); // or the otherway round?
822 info
->xencoding
= wxT("*");
825 case wxFONTENCODING_KOI8
:
826 case wxFONTENCODING_KOI8_U
:
827 info
->xregistry
= wxT("koi8");
829 // we don't make distinction between koi8-r, koi8-u and koi8-ru (so far)
830 info
->xencoding
= wxT("*");
833 case wxFONTENCODING_CP1250
:
834 case wxFONTENCODING_CP1251
:
835 case wxFONTENCODING_CP1252
:
836 case wxFONTENCODING_CP1253
:
837 case wxFONTENCODING_CP1254
:
838 case wxFONTENCODING_CP1255
:
839 case wxFONTENCODING_CP1256
:
840 case wxFONTENCODING_CP1257
:
842 int cp
= encoding
- wxFONTENCODING_CP1250
+ 1250;
843 info
->xregistry
= wxT("microsoft");
844 info
->xencoding
.Printf(wxT("cp%d"), cp
);
848 case wxFONTENCODING_EUC_JP
:
849 case wxFONTENCODING_SHIFT_JIS
:
850 info
->xregistry
= "jis*";
851 info
->xencoding
= "*";
854 case wxFONTENCODING_SYSTEM
:
856 info
->xencoding
= wxT("*");
860 // don't know how to translate this encoding into X fontspec
864 info
->encoding
= encoding
;
869 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
872 fontspec
.Printf(_T("-*-%s-*-*-*-*-*-*-*-*-*-*-%s-%s"),
873 !info
.facename
? _T("*") : info
.facename
.c_str(),
874 info
.xregistry
.c_str(),
875 info
.xencoding
.c_str());
877 return wxTestFontSpec(fontspec
);
880 // ----------------------------------------------------------------------------
881 // X-specific functions
882 // ----------------------------------------------------------------------------
884 wxNativeFont
wxLoadQueryNearestFont(int pointSize
,
889 const wxString
&facename
,
890 wxFontEncoding encoding
,
893 if ( encoding
== wxFONTENCODING_DEFAULT
)
895 encoding
= wxFont::GetDefaultEncoding();
898 // first determine the encoding - if the font doesn't exist at all in this
899 // encoding, it's useless to do all other approximations (i.e. size,
900 // family &c don't matter much)
901 wxNativeEncodingInfo info
;
902 if ( encoding
== wxFONTENCODING_SYSTEM
)
904 // This will always work so we don't test to save time
905 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
909 if ( !wxGetNativeFontEncoding(encoding
, &info
) ||
910 !wxTestFontEncoding(info
) )
913 if ( !wxFontMapper::Get()->GetAltForEncoding(encoding
, &info
) )
914 #endif // wxUSE_FONTMAP
916 // unspported encoding - replace it with the default
918 // NB: we can't just return 0 from here because wxGTK code doesn't
919 // check for it (i.e. it supposes that we'll always succeed),
920 // so it would provoke a crash
921 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
926 // OK, we have the correct xregistry/xencoding in info structure
927 wxNativeFont font
= 0;
929 // if we already have the X font name, try to use it
930 if( xFontName
&& !xFontName
->IsEmpty() )
933 // Make sure point size is correct for scale factor.
935 wxStringTokenizer
tokenizer(*xFontName
, _T("-"), wxTOKEN_RET_DELIMS
);
936 wxString newFontName
;
938 for(int i
= 0; i
< 8; i
++)
939 newFontName
+= tokenizer
.NextToken();
941 (void) tokenizer
.NextToken();
943 newFontName
+= wxString::Format(wxT("%d-"), pointSize
);
945 while(tokenizer
.HasMoreTokens())
946 newFontName
+= tokenizer
.GetNextToken();
948 font
= wxLoadFont(newFontName
);
951 *xFontName
= newFontName
;
956 // search up and down by stepsize 10
957 int max_size
= pointSize
+ 20 * (1 + (pointSize
/180));
958 int min_size
= pointSize
- 20 * (1 + (pointSize
/180));
960 int i
, round
; // counters
962 // first round: search for equal, then for smaller and for larger size with the given weight and style
963 int testweight
= weight
;
964 int teststyle
= style
;
966 for ( round
= 0; round
< 3; round
++ )
968 // second round: use normal weight
971 if ( testweight
!= wxNORMAL
)
973 testweight
= wxNORMAL
;
977 ++round
; // fall through to third round
981 // third round: ... and use normal style
984 if ( teststyle
!= wxNORMAL
)
986 teststyle
= wxNORMAL
;
993 // Search for equal or smaller size (approx.)
994 for ( i
= pointSize
; !font
&& i
>= 10 && i
>= min_size
; i
-= 10 )
996 font
= wxLoadQueryFont(i
, family
, teststyle
, testweight
, underlined
,
997 facename
, info
.xregistry
, info
.xencoding
,
1001 // Search for larger size (approx.)
1002 for ( i
= pointSize
+ 10; !font
&& i
<= max_size
; i
+= 10 )
1004 font
= wxLoadQueryFont(i
, family
, teststyle
, testweight
, underlined
,
1005 facename
, info
.xregistry
, info
.xencoding
,
1010 // Try default family
1011 if ( !font
&& family
!= wxDEFAULT
)
1013 font
= wxLoadQueryFont(pointSize
, wxDEFAULT
, style
, weight
,
1014 underlined
, facename
,
1015 info
.xregistry
, info
.xencoding
,
1019 // ignore size, family, style and weight but try to find font with the
1020 // given facename and encoding
1023 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1024 underlined
, facename
,
1025 info
.xregistry
, info
.xencoding
,
1028 // ignore family as well
1031 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1032 underlined
, wxEmptyString
,
1033 info
.xregistry
, info
.xencoding
,
1036 // if it still failed, try to get the font of any size but
1037 // with the requested encoding: this can happen if the
1038 // encoding is only available in one size which happens to be
1039 // different from 120
1042 font
= wxLoadQueryFont(-1, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1043 FALSE
, wxEmptyString
,
1044 info
.xregistry
, info
.xencoding
,
1047 // this should never happen as we had tested for it in the
1048 // very beginning, but if it does, do return something non
1049 // NULL or we'd crash in wxFont code
1052 wxFAIL_MSG( _T("this encoding should be available!") );
1054 font
= wxLoadQueryFont(-1,
1055 wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1056 FALSE
, wxEmptyString
,
1068 // ----------------------------------------------------------------------------
1069 // private functions
1070 // ----------------------------------------------------------------------------
1072 // returns TRUE if there are any fonts matching this font spec
1073 static bool wxTestFontSpec(const wxString
& fontspec
)
1075 // some X servers will fail to load this font because there are too many
1076 // matches so we must test explicitly for this
1077 if ( fontspec
== _T("-*-*-*-*-*-*-*-*-*-*-*-*-*-*") )
1082 wxNativeFont test
= (wxNativeFont
) g_fontHash
->Get( fontspec
);
1088 test
= wxLoadFont(fontspec
);
1089 g_fontHash
->Put( fontspec
, (wxObject
*) test
);
1103 static wxNativeFont
wxLoadQueryFont(int pointSize
,
1107 bool WXUNUSED(underlined
),
1108 const wxString
& facename
,
1109 const wxString
& xregistry
,
1110 const wxString
& xencoding
,
1111 wxString
* xFontName
)
1116 case wxDECORATIVE
: xfamily
= wxT("lucida"); break;
1117 case wxROMAN
: xfamily
= wxT("times"); break;
1118 case wxMODERN
: xfamily
= wxT("courier"); break;
1119 case wxSWISS
: xfamily
= wxT("helvetica"); break;
1120 case wxTELETYPE
: xfamily
= wxT("lucidatypewriter"); break;
1121 case wxSCRIPT
: xfamily
= wxT("utopia"); break;
1122 default: xfamily
= wxT("*");
1130 xweight
= MWLF_WEIGHT_BOLD
;
1135 xweight
= MWLF_WEIGHT_LIGHT
;
1140 xweight
= MWLF_WEIGHT_NORMAL
;
1146 xweight
= MWLF_WEIGHT_DEFAULT
;
1150 GR_SCREEN_INFO screenInfo
;
1151 GrGetScreenInfo(& screenInfo
);
1153 int yPixelsPerCM
= screenInfo
.ydpcm
;
1155 // A point is 1/72 of an inch.
1156 // An inch is 2.541 cm.
1157 // So pixelHeight = (pointSize / 72) (inches) * 2.541 (for cm) * yPixelsPerCM (for pixels)
1158 // In fact pointSize is 10 * the normal point size so
1161 int pixelHeight
= (int) ( (((float)pointSize
) / 720.0) * 2.541 * (float) yPixelsPerCM
) ;
1163 // An alternative: assume that the screen is 72 dpi.
1164 //int pixelHeight = (int) (((float)pointSize / 720.0) * 72.0) ;
1165 //int pixelHeight = (int) ((float)pointSize / 10.0) ;
1168 logFont
.lfHeight
= pixelHeight
;
1169 logFont
.lfWidth
= 0;
1170 logFont
.lfEscapement
= 0;
1171 logFont
.lfOrientation
= 0;
1172 logFont
.lfWeight
= xweight
;
1173 logFont
.lfItalic
= (style
== wxNORMAL
? 0 : 1) ;
1174 logFont
.lfUnderline
= 0;
1175 logFont
.lfStrikeOut
= 0;
1176 logFont
.lfCharSet
= MWLF_CHARSET_DEFAULT
; // TODO: select appropriate one
1177 logFont
.lfOutPrecision
= MWLF_TYPE_DEFAULT
;
1178 logFont
.lfClipPrecision
= 0; // Not used
1179 logFont
.lfRoman
= (family
== wxROMAN
? 1 : 0) ;
1180 logFont
.lfSerif
= (family
== wxSWISS
? 0 : 1) ;
1181 logFont
.lfSansSerif
= !logFont
.lfSerif
;
1182 logFont
.lfModern
= (family
== wxMODERN
? 1 : 0) ;
1183 logFont
.lfProportional
= (family
== wxTELETYPE
? 0 : 1) ;
1184 logFont
.lfOblique
= 0;
1185 logFont
.lfSmallCaps
= 0;
1186 logFont
.lfPitch
= 0; // 0 = default
1187 strcpy(logFont
.lfFaceName
, facename
.c_str());
1189 XFontStruct
* fontInfo
= (XFontStruct
*) malloc(sizeof(XFontStruct
));
1190 fontInfo
->fid
= GrCreateFont((GR_CHAR
*) facename
.c_str(), pixelHeight
, & logFont
);
1191 GrGetFontInfo(fontInfo
->fid
, & fontInfo
->info
);
1192 return (wxNativeFont
) fontInfo
;
1196 if (!facename
.IsEmpty())
1198 fontSpec
.Printf(wxT("-*-%s-*-*-normal-*-*-*-*-*-*-*-*-*"),
1201 if ( wxTestFontSpec(fontSpec
) )
1205 //else: no such family, use default one instead
1212 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1214 if ( wxTestFontSpec(fontSpec
) )
1219 // fall through - try wxITALIC now
1222 fontSpec
.Printf(wxT("-*-%s-*-i-*-*-*-*-*-*-*-*-*-*"),
1224 if ( wxTestFontSpec(fontSpec
) )
1228 else if ( style
== wxITALIC
) // and not wxSLANT
1231 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1233 if ( wxTestFontSpec(fontSpec
) )
1239 // no italic, no slant - leave default
1246 wxFAIL_MSG(_T("unknown font style"));
1247 // fall back to normal
1259 fontSpec
.Printf(wxT("-*-%s-bold-*-*-*-*-*-*-*-*-*-*-*"),
1261 if ( wxTestFontSpec(fontSpec
) )
1263 xweight
= wxT("bold");
1266 fontSpec
.Printf(wxT("-*-%s-heavy-*-*-*-*-*-*-*-*-*-*-*"),
1268 if ( wxTestFontSpec(fontSpec
) )
1270 xweight
= wxT("heavy");
1273 fontSpec
.Printf(wxT("-*-%s-extrabold-*-*-*-*-*-*-*-*-*-*-*"),
1275 if ( wxTestFontSpec(fontSpec
) )
1277 xweight
= wxT("extrabold");
1280 fontSpec
.Printf(wxT("-*-%s-demibold-*-*-*-*-*-*-*-*-*-*-*"),
1282 if ( wxTestFontSpec(fontSpec
) )
1284 xweight
= wxT("demibold");
1287 fontSpec
.Printf(wxT("-*-%s-black-*-*-*-*-*-*-*-*-*-*-*"),
1289 if ( wxTestFontSpec(fontSpec
) )
1291 xweight
= wxT("black");
1294 fontSpec
.Printf(wxT("-*-%s-ultrablack-*-*-*-*-*-*-*-*-*-*-*"),
1296 if ( wxTestFontSpec(fontSpec
) )
1298 xweight
= wxT("ultrablack");
1305 fontSpec
.Printf(wxT("-*-%s-light-*-*-*-*-*-*-*-*-*-*-*"),
1307 if ( wxTestFontSpec(fontSpec
) )
1309 xweight
= wxT("light");
1312 fontSpec
.Printf(wxT("-*-%s-thin-*-*-*-*-*-*-*-*-*-*-*"),
1314 if ( wxTestFontSpec(fontSpec
) )
1316 xweight
= wxT("thin");
1323 fontSpec
.Printf(wxT("-*-%s-medium-*-*-*-*-*-*-*-*-*-*-*"),
1325 if ( wxTestFontSpec(fontSpec
) )
1327 xweight
= wxT("medium");
1330 fontSpec
.Printf(wxT("-*-%s-normal-*-*-*-*-*-*-*-*-*-*-*"),
1332 if ( wxTestFontSpec(fontSpec
) )
1334 xweight
= wxT("normal");
1337 fontSpec
.Printf(wxT("-*-%s-regular-*-*-*-*-*-*-*-*-*-*-*"),
1339 if ( wxTestFontSpec(fontSpec
) )
1341 xweight
= wxT("regular");
1347 default: xweight
= wxT("*"); break;
1350 // if pointSize is -1, don't specify any
1352 if ( pointSize
== -1 )
1358 sizeSpec
.Printf(_T("%d"), pointSize
);
1361 // construct the X font spec from our data
1362 fontSpec
.Printf(wxT("-*-%s-%s-%s-normal-*-*-%s-*-*-*-*-%s-%s"),
1363 xfamily
.c_str(), xweight
.c_str(), xstyle
.c_str(),
1364 sizeSpec
.c_str(), xregistry
.c_str(), xencoding
.c_str());
1367 *xFontName
= fontSpec
;
1369 return wxLoadFont(fontSpec
);
1374 // ----------------------------------------------------------------------------
1376 // ----------------------------------------------------------------------------
1378 class wxFontModule
: public wxModule
1385 DECLARE_DYNAMIC_CLASS(wxFontModule
)
1388 IMPLEMENT_DYNAMIC_CLASS(wxFontModule
, wxModule
)
1390 bool wxFontModule::OnInit()
1392 g_fontHash
= new wxHashTable( wxKEY_STRING
);
1397 void wxFontModule::OnExit()
1401 g_fontHash
= (wxHashTable
*)NULL
;
1404 #endif // GTK 2.0/1.x