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"
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_SYS(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_SYS( 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
& WXUNUSED(s
))
312 wxString
wxNativeEncodingInfo::ToString() const
314 return wxEmptyString
;
317 bool wxTestFontEncoding(const wxNativeEncodingInfo
& WXUNUSED(info
))
322 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
323 wxNativeEncodingInfo
*info
)
325 info
->facename
.clear();
329 // we *must* return true for default encodings as otherwise wxFontMapper
330 // considers that we can't load any font and aborts with wxLogFatalError!
331 case wxFONTENCODING_DEFAULT
:
332 case wxFONTENCODING_SYSTEM
:
333 info
->encoding
= wxFONTENCODING_SYSTEM
;
336 case wxFONTENCODING_ISO8859_1
:
337 case wxFONTENCODING_UTF8
:
338 // these are always supported by GTK+ 2
339 info
->encoding
= encoding
;
343 // everything else must be converted to UTF-8
352 #pragma message disable nosimpint
355 #include <X11/Xlib.h>
358 #pragma message enable nosimpint
361 #include "wx/utils.h" // for wxGetDisplay()
362 #elif defined(__WXGTK__)
363 // we have to declare struct tm to avoid problems with first forward
364 // declaring it in C code (glib.h included from gdk.h does it) and then
365 // defining it when time.h is included from the headers below - this is
366 // known not to work at least with Sun CC 6.01
373 // ----------------------------------------------------------------------------
375 // ----------------------------------------------------------------------------
377 static wxHashTable
*g_fontHash
= (wxHashTable
*) NULL
;
379 // ----------------------------------------------------------------------------
381 // ----------------------------------------------------------------------------
383 // define the functions to create and destroy native fonts for this toolkit
385 wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
387 return XLoadQueryFont((Display
*)wxGetDisplay(), fontSpec
);
390 inline void wxFreeFont(wxNativeFont font
)
392 XFreeFont((Display
*)wxGetDisplay(), (XFontStruct
*)font
);
394 #elif defined(__WXGTK__)
395 wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
397 // VZ: we should use gdk_fontset_load() instead of gdk_font_load()
398 // here to be able to display Japanese fonts correctly (at least
399 // this is what people report) but unfortunately doing it results
400 // in tons of warnings when using GTK with "normal" European
401 // languages and so we can't always do it and I don't know enough
402 // to determine when should this be done... (FIXME)
403 return gdk_font_load( wxConvertWX2MB(fontSpec
) );
406 inline void wxFreeFont(wxNativeFont font
)
408 gdk_font_unref(font
);
411 #error "Unknown GUI toolkit"
414 static bool wxTestFontSpec(const wxString
& fontspec
);
416 static wxNativeFont
wxLoadQueryFont(int pointSize
,
421 const wxString
& facename
,
422 const wxString
& xregistry
,
423 const wxString
& xencoding
,
424 wxString
* xFontName
);
426 // ============================================================================
428 // ============================================================================
430 // ----------------------------------------------------------------------------
431 // wxNativeEncodingInfo
432 // ----------------------------------------------------------------------------
434 // convert to/from the string representation: format is
435 // encodingid;registry;encoding[;facename]
436 bool wxNativeEncodingInfo::FromString(const wxString
& s
)
438 // use ";", not "-" because it may be part of encoding name
439 wxStringTokenizer
tokenizer(s
, _T(";"));
441 wxString encid
= tokenizer
.GetNextToken();
443 if ( !encid
.ToLong(&enc
) )
445 encoding
= (wxFontEncoding
)enc
;
447 xregistry
= tokenizer
.GetNextToken();
451 xencoding
= tokenizer
.GetNextToken();
456 facename
= tokenizer
.GetNextToken();
461 wxString
wxNativeEncodingInfo::ToString() const
464 s
<< (long)encoding
<< _T(';') << xregistry
<< _T(';') << xencoding
;
465 if ( !facename
.empty() )
467 s
<< _T(';') << facename
;
473 // ----------------------------------------------------------------------------
475 // ----------------------------------------------------------------------------
477 void wxNativeFontInfo::Init()
482 bool wxNativeFontInfo::FromString(const wxString
& s
)
484 wxStringTokenizer
tokenizer(s
, _T(";"));
487 wxString token
= tokenizer
.GetNextToken();
488 if ( token
!= _T('0') )
491 xFontName
= tokenizer
.GetNextToken();
493 // this should be the end
494 if ( tokenizer
.HasMoreTokens() )
497 return FromXFontName(xFontName
);
500 wxString
wxNativeFontInfo::ToString() const
503 return wxString::Format(_T("%d;%s"), 0, GetXFontName().c_str());
506 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
508 return FromXFontName(s
);
511 wxString
wxNativeFontInfo::ToUserString() const
513 return GetXFontName();
516 bool wxNativeFontInfo::HasElements() const
518 // we suppose that the foundry is never empty, so if it is it means that we
519 // had never parsed the XLFD
520 return !fontElements
[0].empty();
523 wxString
wxNativeFontInfo::GetXFontComponent(wxXLFDField field
) const
525 wxCHECK_MSG( field
< wxXLFD_MAX
, wxEmptyString
, _T("invalid XLFD field") );
527 if ( !HasElements() )
530 if ( !((wxNativeFontInfo
*)this)->FromXFontName(xFontName
) )
531 return wxEmptyString
;
534 return fontElements
[field
];
537 bool wxNativeFontInfo::FromXFontName(const wxString
& fontname
)
539 // TODO: we should be able to handle the font aliases here, but how?
540 wxStringTokenizer
tokenizer(fontname
, _T("-"));
542 // skip the leading, usually empty field (font name registry)
543 if ( !tokenizer
.HasMoreTokens() )
546 (void)tokenizer
.GetNextToken();
548 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
550 if ( !tokenizer
.HasMoreTokens() )
552 // not enough elements in the XLFD - or maybe an alias
556 wxString field
= tokenizer
.GetNextToken();
557 if ( !field
.empty() && field
!= _T('*') )
559 // we're really initialized now
563 fontElements
[n
] = field
;
566 // this should be all
567 if ( tokenizer
.HasMoreTokens() )
573 wxString
wxNativeFontInfo::GetXFontName() const
575 if ( xFontName
.empty() )
577 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
579 // replace the non specified elements with '*' except for the
580 // additional style which is usually just omitted
581 wxString elt
= fontElements
[n
];
582 if ( elt
.empty() && n
!= wxXLFD_ADDSTYLE
)
588 ((wxNativeFontInfo
*)this)->xFontName
<< _T('-') << elt
;
596 wxNativeFontInfo::SetXFontComponent(wxXLFDField field
, const wxString
& value
)
598 wxCHECK_RET( field
< wxXLFD_MAX
, _T("invalid XLFD field") );
600 // this class should be initialized with a valid font spec first and only
601 // then the fields may be modified!
602 wxASSERT_MSG( !IsDefault(), _T("can't modify an uninitialized XLFD") );
604 if ( !HasElements() )
607 if ( !((wxNativeFontInfo
*)this)->FromXFontName(xFontName
) )
609 wxFAIL_MSG( _T("can't set font element for invalid XLFD") );
615 fontElements
[field
] = value
;
617 // invalidate the XFLD, it doesn't correspond to the font elements any more
621 void wxNativeFontInfo::SetXFontName(const wxString
& xFontName_
)
623 // invalidate the font elements, GetXFontComponent() will reparse the XLFD
624 fontElements
[0].clear();
626 xFontName
= xFontName_
;
631 int wxNativeFontInfo::GetPointSize() const
633 const wxString s
= GetXFontComponent(wxXLFD_POINTSIZE
);
635 // return -1 to indicate that the size is unknown
637 return s
.ToLong(&l
) ? l
: -1;
640 wxFontStyle
wxNativeFontInfo::GetStyle() const
642 const wxString s
= GetXFontComponent(wxXLFD_SLANT
);
644 if ( s
.length() != 1 )
646 // it is really unknown but we don't have any way to return it from
648 return wxFONTSTYLE_NORMAL
;
654 // again, unknown but consider normal by default
657 return wxFONTSTYLE_NORMAL
;
660 return wxFONTSTYLE_ITALIC
;
663 return wxFONTSTYLE_SLANT
;
667 wxFontWeight
wxNativeFontInfo::GetWeight() const
669 const wxString s
= GetXFontComponent(wxXLFD_WEIGHT
).MakeLower();
670 if ( s
.find(_T("bold")) != wxString::npos
|| s
== _T("black") )
671 return wxFONTWEIGHT_BOLD
;
672 else if ( s
== _T("light") )
673 return wxFONTWEIGHT_LIGHT
;
675 return wxFONTWEIGHT_NORMAL
;
678 bool wxNativeFontInfo::GetUnderlined() const
680 // X fonts are never underlined
684 wxString
wxNativeFontInfo::GetFaceName() const
686 // wxWidgets facename probably more accurately corresponds to X family
687 return GetXFontComponent(wxXLFD_FAMILY
);
690 wxFontFamily
wxNativeFontInfo::GetFamily() const
692 // and wxWidgets family -- to X foundry, but we have to translate it to
693 // wxFontFamily somehow...
694 wxFAIL_MSG(_T("not implemented")); // GetXFontComponent(wxXLFD_FOUNDRY);
696 return wxFONTFAMILY_DEFAULT
;
699 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
701 // we already have the code for this but need to refactor it first
702 wxFAIL_MSG( _T("not implemented") );
704 return wxFONTENCODING_MAX
;
707 void wxNativeFontInfo::SetPointSize(int pointsize
)
709 SetXFontComponent(wxXLFD_POINTSIZE
, wxString::Format(_T("%d"), pointsize
));
712 void wxNativeFontInfo::SetStyle(wxFontStyle style
)
717 case wxFONTSTYLE_ITALIC
:
721 case wxFONTSTYLE_SLANT
:
725 case wxFONTSTYLE_NORMAL
:
729 wxFAIL_MSG( _T("unknown wxFontStyle in wxNativeFontInfo::SetStyle") );
733 SetXFontComponent(wxXLFD_SLANT
, s
);
736 void wxNativeFontInfo::SetWeight(wxFontWeight weight
)
741 case wxFONTWEIGHT_BOLD
:
745 case wxFONTWEIGHT_LIGHT
:
749 case wxFONTWEIGHT_NORMAL
:
754 wxFAIL_MSG( _T("unknown wxFontWeight in wxNativeFontInfo::SetWeight") );
758 SetXFontComponent(wxXLFD_WEIGHT
, s
);
761 void wxNativeFontInfo::SetUnderlined(bool WXUNUSED(underlined
))
763 // can't do this under X
766 void wxNativeFontInfo::SetFaceName(const wxString
& facename
)
768 SetXFontComponent(wxXLFD_FAMILY
, facename
);
771 void wxNativeFontInfo::SetFamily(wxFontFamily
WXUNUSED(family
))
773 // wxFontFamily -> X foundry, anyone?
774 wxFAIL_MSG( _T("not implemented") );
776 // SetXFontComponent(wxXLFD_FOUNDRY, ...);
779 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding
)
781 wxNativeEncodingInfo info
;
782 if ( wxGetNativeFontEncoding(encoding
, &info
) )
784 SetXFontComponent(wxXLFD_ENCODING
, info
.xencoding
);
785 SetXFontComponent(wxXLFD_REGISTRY
, info
.xregistry
);
789 // ----------------------------------------------------------------------------
791 // ----------------------------------------------------------------------------
793 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
794 wxNativeEncodingInfo
*info
)
796 wxCHECK_MSG( info
, false, _T("bad pointer in wxGetNativeFontEncoding") );
798 if ( encoding
== wxFONTENCODING_DEFAULT
)
800 encoding
= wxFont::GetDefaultEncoding();
805 case wxFONTENCODING_ISO8859_1
:
806 case wxFONTENCODING_ISO8859_2
:
807 case wxFONTENCODING_ISO8859_3
:
808 case wxFONTENCODING_ISO8859_4
:
809 case wxFONTENCODING_ISO8859_5
:
810 case wxFONTENCODING_ISO8859_6
:
811 case wxFONTENCODING_ISO8859_7
:
812 case wxFONTENCODING_ISO8859_8
:
813 case wxFONTENCODING_ISO8859_9
:
814 case wxFONTENCODING_ISO8859_10
:
815 case wxFONTENCODING_ISO8859_11
:
816 case wxFONTENCODING_ISO8859_12
:
817 case wxFONTENCODING_ISO8859_13
:
818 case wxFONTENCODING_ISO8859_14
:
819 case wxFONTENCODING_ISO8859_15
:
821 int cp
= encoding
- wxFONTENCODING_ISO8859_1
+ 1;
822 info
->xregistry
= wxT("iso8859");
823 info
->xencoding
.Printf(wxT("%d"), cp
);
827 case wxFONTENCODING_UTF8
:
828 info
->xregistry
= wxT("iso10646");
829 info
->xencoding
= wxT("*");
832 case wxFONTENCODING_GB2312
:
833 info
->xregistry
= wxT("GB2312"); // or the otherway round?
834 info
->xencoding
= wxT("*");
837 case wxFONTENCODING_KOI8
:
838 case wxFONTENCODING_KOI8_U
:
839 info
->xregistry
= wxT("koi8");
841 // we don't make distinction between koi8-r, koi8-u and koi8-ru (so far)
842 info
->xencoding
= wxT("*");
845 case wxFONTENCODING_CP1250
:
846 case wxFONTENCODING_CP1251
:
847 case wxFONTENCODING_CP1252
:
848 case wxFONTENCODING_CP1253
:
849 case wxFONTENCODING_CP1254
:
850 case wxFONTENCODING_CP1255
:
851 case wxFONTENCODING_CP1256
:
852 case wxFONTENCODING_CP1257
:
854 int cp
= encoding
- wxFONTENCODING_CP1250
+ 1250;
855 info
->xregistry
= wxT("microsoft");
856 info
->xencoding
.Printf(wxT("cp%d"), cp
);
860 case wxFONTENCODING_EUC_JP
:
861 case wxFONTENCODING_SHIFT_JIS
:
862 info
->xregistry
= "jis*";
863 info
->xencoding
= "*";
866 case wxFONTENCODING_SYSTEM
:
868 info
->xencoding
= wxT("*");
872 // don't know how to translate this encoding into X fontspec
876 info
->encoding
= encoding
;
881 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
884 fontspec
.Printf(_T("-*-%s-*-*-*-*-*-*-*-*-*-*-%s-%s"),
885 !info
.facename
? _T("*") : info
.facename
.c_str(),
886 info
.xregistry
.c_str(),
887 info
.xencoding
.c_str());
889 return wxTestFontSpec(fontspec
);
892 // ----------------------------------------------------------------------------
893 // X-specific functions
894 // ----------------------------------------------------------------------------
896 wxNativeFont
wxLoadQueryNearestFont(int pointSize
,
901 const wxString
&facename
,
902 wxFontEncoding encoding
,
905 if ( encoding
== wxFONTENCODING_DEFAULT
)
907 encoding
= wxFont::GetDefaultEncoding();
910 // first determine the encoding - if the font doesn't exist at all in this
911 // encoding, it's useless to do all other approximations (i.e. size,
912 // family &c don't matter much)
913 wxNativeEncodingInfo info
;
914 if ( encoding
== wxFONTENCODING_SYSTEM
)
916 // This will always work so we don't test to save time
917 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
921 if ( !wxGetNativeFontEncoding(encoding
, &info
) ||
922 !wxTestFontEncoding(info
) )
925 if ( !wxFontMapper::Get()->GetAltForEncoding(encoding
, &info
) )
926 #endif // wxUSE_FONTMAP
928 // unspported encoding - replace it with the default
930 // NB: we can't just return 0 from here because wxGTK code doesn't
931 // check for it (i.e. it supposes that we'll always succeed),
932 // so it would provoke a crash
933 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
938 // OK, we have the correct xregistry/xencoding in info structure
939 wxNativeFont font
= 0;
941 // if we already have the X font name, try to use it
942 if( xFontName
&& !xFontName
->empty() )
945 // Make sure point size is correct for scale factor.
947 wxStringTokenizer
tokenizer(*xFontName
, _T("-"), wxTOKEN_RET_DELIMS
);
948 wxString newFontName
;
950 for(int i
= 0; i
< 8; i
++)
951 newFontName
+= tokenizer
.NextToken();
953 (void) tokenizer
.NextToken();
955 newFontName
+= wxString::Format(wxT("%d-"), pointSize
);
957 while(tokenizer
.HasMoreTokens())
958 newFontName
+= tokenizer
.GetNextToken();
960 font
= wxLoadFont(newFontName
);
963 *xFontName
= newFontName
;
968 // search up and down by stepsize 10
969 int max_size
= pointSize
+ 20 * (1 + (pointSize
/180));
970 int min_size
= pointSize
- 20 * (1 + (pointSize
/180));
972 int i
, round
; // counters
974 // first round: search for equal, then for smaller and for larger size with the given weight and style
975 int testweight
= weight
;
976 int teststyle
= style
;
978 for ( round
= 0; round
< 3; round
++ )
980 // second round: use normal weight
983 if ( testweight
!= wxNORMAL
)
985 testweight
= wxNORMAL
;
989 ++round
; // fall through to third round
993 // third round: ... and use normal style
996 if ( teststyle
!= wxNORMAL
)
998 teststyle
= wxNORMAL
;
1005 // Search for equal or smaller size (approx.)
1006 for ( i
= pointSize
; !font
&& i
>= 10 && i
>= min_size
; i
-= 10 )
1008 font
= wxLoadQueryFont(i
, family
, teststyle
, testweight
, underlined
,
1009 facename
, info
.xregistry
, info
.xencoding
,
1013 // Search for larger size (approx.)
1014 for ( i
= pointSize
+ 10; !font
&& i
<= max_size
; i
+= 10 )
1016 font
= wxLoadQueryFont(i
, family
, teststyle
, testweight
, underlined
,
1017 facename
, info
.xregistry
, info
.xencoding
,
1022 // Try default family
1023 if ( !font
&& family
!= wxDEFAULT
)
1025 font
= wxLoadQueryFont(pointSize
, wxDEFAULT
, style
, weight
,
1026 underlined
, facename
,
1027 info
.xregistry
, info
.xencoding
,
1031 // ignore size, family, style and weight but try to find font with the
1032 // given facename and encoding
1035 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1036 underlined
, facename
,
1037 info
.xregistry
, info
.xencoding
,
1040 // ignore family as well
1043 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1044 underlined
, wxEmptyString
,
1045 info
.xregistry
, info
.xencoding
,
1048 // if it still failed, try to get the font of any size but
1049 // with the requested encoding: this can happen if the
1050 // encoding is only available in one size which happens to be
1051 // different from 120
1054 font
= wxLoadQueryFont(-1, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1055 false, wxEmptyString
,
1056 info
.xregistry
, info
.xencoding
,
1059 // this should never happen as we had tested for it in the
1060 // very beginning, but if it does, do return something non
1061 // NULL or we'd crash in wxFont code
1064 wxFAIL_MSG( _T("this encoding should be available!") );
1066 font
= wxLoadQueryFont(-1,
1067 wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1068 false, wxEmptyString
,
1080 // ----------------------------------------------------------------------------
1081 // private functions
1082 // ----------------------------------------------------------------------------
1084 // returns true if there are any fonts matching this font spec
1085 static bool wxTestFontSpec(const wxString
& fontspec
)
1087 // some X servers will fail to load this font because there are too many
1088 // matches so we must test explicitly for this
1089 if ( fontspec
== _T("-*-*-*-*-*-*-*-*-*-*-*-*-*-*") )
1094 wxNativeFont test
= (wxNativeFont
) g_fontHash
->Get( fontspec
);
1100 test
= wxLoadFont(fontspec
);
1101 g_fontHash
->Put( fontspec
, (wxObject
*) test
);
1115 static wxNativeFont
wxLoadQueryFont(int pointSize
,
1119 bool WXUNUSED(underlined
),
1120 const wxString
& facename
,
1121 const wxString
& xregistry
,
1122 const wxString
& xencoding
,
1123 wxString
* xFontName
)
1128 case wxDECORATIVE
: xfamily
= wxT("lucida"); break;
1129 case wxROMAN
: xfamily
= wxT("times"); break;
1130 case wxMODERN
: xfamily
= wxT("courier"); break;
1131 case wxSWISS
: xfamily
= wxT("helvetica"); break;
1132 case wxTELETYPE
: xfamily
= wxT("lucidatypewriter"); break;
1133 case wxSCRIPT
: xfamily
= wxT("utopia"); break;
1134 default: xfamily
= wxT("*");
1142 xweight
= MWLF_WEIGHT_BOLD
;
1147 xweight
= MWLF_WEIGHT_LIGHT
;
1152 xweight
= MWLF_WEIGHT_NORMAL
;
1158 xweight
= MWLF_WEIGHT_DEFAULT
;
1162 GR_SCREEN_INFO screenInfo
;
1163 GrGetScreenInfo(& screenInfo
);
1165 int yPixelsPerCM
= screenInfo
.ydpcm
;
1167 // A point is 1/72 of an inch.
1168 // An inch is 2.541 cm.
1169 // So pixelHeight = (pointSize / 72) (inches) * 2.541 (for cm) * yPixelsPerCM (for pixels)
1170 // In fact pointSize is 10 * the normal point size so
1173 int pixelHeight
= (int) ( (((float)pointSize
) / 720.0) * 2.541 * (float) yPixelsPerCM
) ;
1175 // An alternative: assume that the screen is 72 dpi.
1176 //int pixelHeight = (int) (((float)pointSize / 720.0) * 72.0) ;
1177 //int pixelHeight = (int) ((float)pointSize / 10.0) ;
1180 logFont
.lfHeight
= pixelHeight
;
1181 logFont
.lfWidth
= 0;
1182 logFont
.lfEscapement
= 0;
1183 logFont
.lfOrientation
= 0;
1184 logFont
.lfWeight
= xweight
;
1185 logFont
.lfItalic
= (style
== wxNORMAL
? 0 : 1) ;
1186 logFont
.lfUnderline
= 0;
1187 logFont
.lfStrikeOut
= 0;
1188 logFont
.lfCharSet
= MWLF_CHARSET_DEFAULT
; // TODO: select appropriate one
1189 logFont
.lfOutPrecision
= MWLF_TYPE_DEFAULT
;
1190 logFont
.lfClipPrecision
= 0; // Not used
1191 logFont
.lfRoman
= (family
== wxROMAN
? 1 : 0) ;
1192 logFont
.lfSerif
= (family
== wxSWISS
? 0 : 1) ;
1193 logFont
.lfSansSerif
= !logFont
.lfSerif
;
1194 logFont
.lfModern
= (family
== wxMODERN
? 1 : 0) ;
1195 logFont
.lfProportional
= (family
== wxTELETYPE
? 0 : 1) ;
1196 logFont
.lfOblique
= 0;
1197 logFont
.lfSmallCaps
= 0;
1198 logFont
.lfPitch
= 0; // 0 = default
1199 strcpy(logFont
.lfFaceName
, facename
.c_str());
1201 XFontStruct
* fontInfo
= (XFontStruct
*) malloc(sizeof(XFontStruct
));
1202 fontInfo
->fid
= GrCreateFont((GR_CHAR
*) facename
.c_str(), pixelHeight
, & logFont
);
1203 GrGetFontInfo(fontInfo
->fid
, & fontInfo
->info
);
1204 return (wxNativeFont
) fontInfo
;
1208 if (!facename
.empty())
1210 fontSpec
.Printf(wxT("-*-%s-*-*-normal-*-*-*-*-*-*-*-*-*"),
1213 if ( wxTestFontSpec(fontSpec
) )
1217 //else: no such family, use default one instead
1224 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1226 if ( wxTestFontSpec(fontSpec
) )
1231 // fall through - try wxITALIC now
1234 fontSpec
.Printf(wxT("-*-%s-*-i-*-*-*-*-*-*-*-*-*-*"),
1236 if ( wxTestFontSpec(fontSpec
) )
1240 else if ( style
== wxITALIC
) // and not wxSLANT
1243 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1245 if ( wxTestFontSpec(fontSpec
) )
1251 // no italic, no slant - leave default
1258 wxFAIL_MSG(_T("unknown font style"));
1259 // fall back to normal
1271 fontSpec
.Printf(wxT("-*-%s-bold-*-*-*-*-*-*-*-*-*-*-*"),
1273 if ( wxTestFontSpec(fontSpec
) )
1275 xweight
= wxT("bold");
1278 fontSpec
.Printf(wxT("-*-%s-heavy-*-*-*-*-*-*-*-*-*-*-*"),
1280 if ( wxTestFontSpec(fontSpec
) )
1282 xweight
= wxT("heavy");
1285 fontSpec
.Printf(wxT("-*-%s-extrabold-*-*-*-*-*-*-*-*-*-*-*"),
1287 if ( wxTestFontSpec(fontSpec
) )
1289 xweight
= wxT("extrabold");
1292 fontSpec
.Printf(wxT("-*-%s-demibold-*-*-*-*-*-*-*-*-*-*-*"),
1294 if ( wxTestFontSpec(fontSpec
) )
1296 xweight
= wxT("demibold");
1299 fontSpec
.Printf(wxT("-*-%s-black-*-*-*-*-*-*-*-*-*-*-*"),
1301 if ( wxTestFontSpec(fontSpec
) )
1303 xweight
= wxT("black");
1306 fontSpec
.Printf(wxT("-*-%s-ultrablack-*-*-*-*-*-*-*-*-*-*-*"),
1308 if ( wxTestFontSpec(fontSpec
) )
1310 xweight
= wxT("ultrablack");
1317 fontSpec
.Printf(wxT("-*-%s-light-*-*-*-*-*-*-*-*-*-*-*"),
1319 if ( wxTestFontSpec(fontSpec
) )
1321 xweight
= wxT("light");
1324 fontSpec
.Printf(wxT("-*-%s-thin-*-*-*-*-*-*-*-*-*-*-*"),
1326 if ( wxTestFontSpec(fontSpec
) )
1328 xweight
= wxT("thin");
1335 fontSpec
.Printf(wxT("-*-%s-medium-*-*-*-*-*-*-*-*-*-*-*"),
1337 if ( wxTestFontSpec(fontSpec
) )
1339 xweight
= wxT("medium");
1342 fontSpec
.Printf(wxT("-*-%s-normal-*-*-*-*-*-*-*-*-*-*-*"),
1344 if ( wxTestFontSpec(fontSpec
) )
1346 xweight
= wxT("normal");
1349 fontSpec
.Printf(wxT("-*-%s-regular-*-*-*-*-*-*-*-*-*-*-*"),
1351 if ( wxTestFontSpec(fontSpec
) )
1353 xweight
= wxT("regular");
1359 default: xweight
= wxT("*"); break;
1362 // if pointSize is -1, don't specify any
1364 if ( pointSize
== -1 )
1370 sizeSpec
.Printf(_T("%d"), pointSize
);
1373 // construct the X font spec from our data
1374 fontSpec
.Printf(wxT("-*-%s-%s-%s-normal-*-*-%s-*-*-*-*-%s-%s"),
1375 xfamily
.c_str(), xweight
.c_str(), xstyle
.c_str(),
1376 sizeSpec
.c_str(), xregistry
.c_str(), xencoding
.c_str());
1379 *xFontName
= fontSpec
;
1381 return wxLoadFont(fontSpec
);
1386 // ----------------------------------------------------------------------------
1388 // ----------------------------------------------------------------------------
1390 class wxFontModule
: public wxModule
1397 DECLARE_DYNAMIC_CLASS(wxFontModule
)
1400 IMPLEMENT_DYNAMIC_CLASS(wxFontModule
, wxModule
)
1402 bool wxFontModule::OnInit()
1404 g_fontHash
= new wxHashTable( wxKEY_STRING
);
1409 void wxFontModule::OnExit()
1413 g_fontHash
= (wxHashTable
*)NULL
;
1416 #endif // GTK 2.0/1.x