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 // note: not passing -1 as the 2nd parameter to g_ascii_strdown to work
143 // around a bug in the 64-bit glib shipped with solaris 10, -1 causes it
144 // to try to allocate 2^32 bytes.
145 const char *family_name
= pango_font_description_get_family( description
);
146 char *family_text
= g_ascii_strdown( family_name
, family_name
? strlen( family_name
) : 0 );
147 // Check for some common fonts, to salvage what we can from the current win32 centric wxFont API:
148 if (strncmp( family_text
, "monospace", 9 ) == 0)
149 ret
= wxFONTFAMILY_TELETYPE
; // begins with "Monospace"
150 else if (strncmp( family_text
, "courier", 7 ) == 0)
151 ret
= wxFONTFAMILY_TELETYPE
; // begins with "Courier"
152 #if defined(__WXGTK24__) || defined(HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE)
155 if (!gtk_check_version(2,4,0))
158 PangoFontFamily
**families
;
159 PangoFontFamily
*family
= NULL
;
161 pango_context_list_families(
163 gtk_widget_get_pango_context( wxGetRootWindow() ),
165 wxTheApp
->GetPangoContext(),
167 &families
, &n_families
);
169 for (int i
= 0;i
< n_families
;++i
)
171 if (g_ascii_strcasecmp(pango_font_family_get_name( families
[i
] ), pango_font_description_get_family( description
)) == 0 )
173 family
= families
[i
];
180 // Some gtk+ systems might query for a non-existing font from wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT)
181 // on initialization, don't assert until wxSystemSettings::GetFont is checked for this - MR
182 // wxASSERT_MSG( family, wxT("wxNativeFontInfo::GetFamily() - No appropriate PangoFontFamily found for ::description") );
184 //BCI: Cache the wxFontFamily inside the class. Validate cache with
185 //BCI: g_ascii_strcasecmp(pango_font_description_get_family(description), pango_font_family_get_name(family)) == 0
187 if (family
!= NULL
&& pango_font_family_is_monospace( family
))
188 ret
= wxFONTFAMILY_TELETYPE
; // is deemed a monospace font by pango
190 #endif // gtk24 || HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE
192 if (ret
== wxFONTFAMILY_DEFAULT
)
194 if (strstr( family_text
, "sans" ) != NULL
) // checked before serif, so that "* Sans Serif" fonts are detected correctly
195 ret
= wxFONTFAMILY_SWISS
; // contains "Sans"
196 else if (strstr( family_text
, "serif" ) != NULL
)
197 ret
= wxFONTFAMILY_ROMAN
; // contains "Serif"
198 else if (strncmp( family_text
, "times", 5 ) == 0)
199 ret
= wxFONTFAMILY_ROMAN
; // begins with "Times"
200 else if (strncmp( family_text
, "old", 3 ) == 0)
201 ret
= wxFONTFAMILY_DECORATIVE
; // Begins with "Old" - "Old English", "Old Town"
208 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
210 return wxFONTENCODING_SYSTEM
;
214 void wxNativeFontInfo::SetPointSize(int pointsize
)
216 pango_font_description_set_size( description
, pointsize
* PANGO_SCALE
);
219 void wxNativeFontInfo::SetStyle(wxFontStyle style
)
223 case wxFONTSTYLE_ITALIC
:
224 pango_font_description_set_style( description
, PANGO_STYLE_ITALIC
);
226 case wxFONTSTYLE_SLANT
:
227 pango_font_description_set_style( description
, PANGO_STYLE_OBLIQUE
);
230 wxFAIL_MSG( _T("unknown font style") );
232 case wxFONTSTYLE_NORMAL
:
233 pango_font_description_set_style( description
, PANGO_STYLE_NORMAL
);
238 void wxNativeFontInfo::SetWeight(wxFontWeight weight
)
242 case wxFONTWEIGHT_BOLD
:
243 pango_font_description_set_weight(description
, PANGO_WEIGHT_BOLD
);
245 case wxFONTWEIGHT_LIGHT
:
246 pango_font_description_set_weight(description
, PANGO_WEIGHT_LIGHT
);
249 wxFAIL_MSG( _T("unknown font weight") );
251 case wxFONTWEIGHT_NORMAL
:
252 pango_font_description_set_weight(description
, PANGO_WEIGHT_NORMAL
);
256 void wxNativeFontInfo::SetUnderlined(bool WXUNUSED(underlined
))
258 wxFAIL_MSG( _T("not implemented") );
261 void wxNativeFontInfo::SetFaceName(wxString facename
)
263 pango_font_description_set_family( description
, wxGTK_CONV(facename
) );
266 void wxNativeFontInfo::SetFamily(wxFontFamily
WXUNUSED(family
))
268 wxFAIL_MSG( _T("not implemented") );
271 void wxNativeFontInfo::SetEncoding(wxFontEncoding
WXUNUSED(encoding
))
273 wxFAIL_MSG( _T("not implemented") );
278 bool wxNativeFontInfo::FromString(const wxString
& s
)
281 pango_font_description_free( description
);
283 description
= pango_font_description_from_string( wxGTK_CONV( s
) );
288 wxString
wxNativeFontInfo::ToString() const
290 char *str
= pango_font_description_to_string( description
);
291 wxString tmp
= wxGTK_CONV_BACK( str
);
297 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
299 return FromString( s
);
302 wxString
wxNativeFontInfo::ToUserString() const
307 // ----------------------------------------------------------------------------
308 // wxNativeEncodingInfo
309 // ----------------------------------------------------------------------------
311 bool wxNativeEncodingInfo::FromString(const wxString
& s
)
316 wxString
wxNativeEncodingInfo::ToString() const
318 return wxEmptyString
;
321 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
326 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
327 wxNativeEncodingInfo
*info
)
329 // we *must* return true for default encoding as otherwise wxFontMapper
330 // considers that we can't load any font and aborts with wxLogFatalError!
331 if ( encoding
== wxFONTENCODING_SYSTEM
)
333 info
->facename
.clear();
334 info
->encoding
= wxFONTENCODING_SYSTEM
;
337 // pretend that we support everything, it's better than to always return
338 // false as the old code did
346 #pragma message disable nosimpint
349 #include <X11/Xlib.h>
352 #pragma message enable nosimpint
355 #include "wx/utils.h" // for wxGetDisplay()
356 #elif defined(__WXGTK__)
357 // we have to declare struct tm to avoid problems with first forward
358 // declaring it in C code (glib.h included from gdk.h does it) and then
359 // defining it when time.h is included from the headers below - this is
360 // known not to work at least with Sun CC 6.01
367 // ----------------------------------------------------------------------------
369 // ----------------------------------------------------------------------------
371 static wxHashTable
*g_fontHash
= (wxHashTable
*) NULL
;
373 // ----------------------------------------------------------------------------
375 // ----------------------------------------------------------------------------
377 // define the functions to create and destroy native fonts for this toolkit
379 wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
381 return XLoadQueryFont((Display
*)wxGetDisplay(), fontSpec
);
384 inline void wxFreeFont(wxNativeFont font
)
386 XFreeFont((Display
*)wxGetDisplay(), (XFontStruct
*)font
);
388 #elif defined(__WXGTK__)
389 wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
391 // VZ: we should use gdk_fontset_load() instead of gdk_font_load()
392 // here to be able to display Japanese fonts correctly (at least
393 // this is what people report) but unfortunately doing it results
394 // in tons of warnings when using GTK with "normal" European
395 // languages and so we can't always do it and I don't know enough
396 // to determine when should this be done... (FIXME)
397 return gdk_font_load( wxConvertWX2MB(fontSpec
) );
400 inline void wxFreeFont(wxNativeFont font
)
402 gdk_font_unref(font
);
405 #error "Unknown GUI toolkit"
408 static bool wxTestFontSpec(const wxString
& fontspec
);
410 static wxNativeFont
wxLoadQueryFont(int pointSize
,
415 const wxString
& facename
,
416 const wxString
& xregistry
,
417 const wxString
& xencoding
,
418 wxString
* xFontName
);
420 // ============================================================================
422 // ============================================================================
424 // ----------------------------------------------------------------------------
425 // wxNativeEncodingInfo
426 // ----------------------------------------------------------------------------
428 // convert to/from the string representation: format is
429 // encodingid;registry;encoding[;facename]
430 bool wxNativeEncodingInfo::FromString(const wxString
& s
)
432 // use ";", not "-" because it may be part of encoding name
433 wxStringTokenizer
tokenizer(s
, _T(";"));
435 wxString encid
= tokenizer
.GetNextToken();
437 if ( !encid
.ToLong(&enc
) )
439 encoding
= (wxFontEncoding
)enc
;
441 xregistry
= tokenizer
.GetNextToken();
445 xencoding
= tokenizer
.GetNextToken();
450 facename
= tokenizer
.GetNextToken();
455 wxString
wxNativeEncodingInfo::ToString() const
458 s
<< (long)encoding
<< _T(';') << xregistry
<< _T(';') << xencoding
;
461 s
<< _T(';') << facename
;
467 // ----------------------------------------------------------------------------
469 // ----------------------------------------------------------------------------
471 void wxNativeFontInfo::Init()
476 bool wxNativeFontInfo::FromString(const wxString
& s
)
478 wxStringTokenizer
tokenizer(s
, _T(";"));
481 wxString token
= tokenizer
.GetNextToken();
482 if ( token
!= _T('0') )
485 xFontName
= tokenizer
.GetNextToken();
487 // this should be the end
488 if ( tokenizer
.HasMoreTokens() )
491 return FromXFontName(xFontName
);
494 wxString
wxNativeFontInfo::ToString() const
497 return wxString::Format(_T("%d;%s"), 0, GetXFontName().c_str());
500 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
502 return FromXFontName(s
);
505 wxString
wxNativeFontInfo::ToUserString() const
507 return GetXFontName();
510 bool wxNativeFontInfo::HasElements() const
512 // we suppose that the foundry is never empty, so if it is it means that we
513 // had never parsed the XLFD
514 return !fontElements
[0].empty();
517 wxString
wxNativeFontInfo::GetXFontComponent(wxXLFDField field
) const
519 wxCHECK_MSG( field
< wxXLFD_MAX
, _T(""), _T("invalid XLFD field") );
521 if ( !HasElements() )
524 if ( !((wxNativeFontInfo
*)this)->FromXFontName(xFontName
) )
528 return fontElements
[field
];
531 bool wxNativeFontInfo::FromXFontName(const wxString
& fontname
)
533 // TODO: we should be able to handle the font aliases here, but how?
534 wxStringTokenizer
tokenizer(fontname
, _T("-"));
536 // skip the leading, usually empty field (font name registry)
537 if ( !tokenizer
.HasMoreTokens() )
540 (void)tokenizer
.GetNextToken();
542 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
544 if ( !tokenizer
.HasMoreTokens() )
546 // not enough elements in the XLFD - or maybe an alias
550 wxString field
= tokenizer
.GetNextToken();
551 if ( !field
.empty() && field
!= _T('*') )
553 // we're really initialized now
557 fontElements
[n
] = field
;
560 // this should be all
561 if ( tokenizer
.HasMoreTokens() )
567 wxString
wxNativeFontInfo::GetXFontName() const
569 if ( xFontName
.empty() )
571 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
573 // replace the non specified elements with '*' except for the
574 // additional style which is usually just omitted
575 wxString elt
= fontElements
[n
];
576 if ( elt
.empty() && n
!= wxXLFD_ADDSTYLE
)
582 ((wxNativeFontInfo
*)this)->xFontName
<< _T('-') << elt
;
590 wxNativeFontInfo::SetXFontComponent(wxXLFDField field
, const wxString
& value
)
592 wxCHECK_RET( field
< wxXLFD_MAX
, _T("invalid XLFD field") );
594 // this class should be initialized with a valid font spec first and only
595 // then the fields may be modified!
596 wxASSERT_MSG( !IsDefault(), _T("can't modify an uninitialized XLFD") );
598 if ( !HasElements() )
601 if ( !((wxNativeFontInfo
*)this)->FromXFontName(xFontName
) )
603 wxFAIL_MSG( _T("can't set font element for invalid XLFD") );
609 fontElements
[field
] = value
;
611 // invalidate the XFLD, it doesn't correspond to the font elements any more
615 void wxNativeFontInfo::SetXFontName(const wxString
& xFontName_
)
617 // invalidate the font elements, GetXFontComponent() will reparse the XLFD
618 fontElements
[0].clear();
620 xFontName
= xFontName_
;
625 int wxNativeFontInfo::GetPointSize() const
627 const wxString s
= GetXFontComponent(wxXLFD_POINTSIZE
);
629 // return -1 to indicate that the size is unknown
631 return s
.ToLong(&l
) ? l
: -1;
634 wxFontStyle
wxNativeFontInfo::GetStyle() const
636 const wxString s
= GetXFontComponent(wxXLFD_SLANT
);
638 if ( s
.length() != 1 )
640 // it is really unknown but we don't have any way to return it from
642 return wxFONTSTYLE_NORMAL
;
648 // again, unknown but consider normal by default
651 return wxFONTSTYLE_NORMAL
;
654 return wxFONTSTYLE_ITALIC
;
657 return wxFONTSTYLE_SLANT
;
661 wxFontWeight
wxNativeFontInfo::GetWeight() const
663 const wxString s
= GetXFontComponent(wxXLFD_WEIGHT
).MakeLower();
664 if ( s
.find(_T("bold")) != wxString::npos
|| s
== _T("black") )
665 return wxFONTWEIGHT_BOLD
;
666 else if ( s
== _T("light") )
667 return wxFONTWEIGHT_LIGHT
;
669 return wxFONTWEIGHT_NORMAL
;
672 bool wxNativeFontInfo::GetUnderlined() const
674 // X fonts are never underlined
678 wxString
wxNativeFontInfo::GetFaceName() const
680 // wxWidgets facename probably more accurately corresponds to X family
681 return GetXFontComponent(wxXLFD_FAMILY
);
684 wxFontFamily
wxNativeFontInfo::GetFamily() const
686 // and wxWidgets family -- to X foundry, but we have to translate it to
687 // wxFontFamily somehow...
688 wxFAIL_MSG(_T("not implemented")); // GetXFontComponent(wxXLFD_FOUNDRY);
690 return wxFONTFAMILY_DEFAULT
;
693 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
695 // we already have the code for this but need to refactor it first
696 wxFAIL_MSG( _T("not implemented") );
698 return wxFONTENCODING_MAX
;
701 void wxNativeFontInfo::SetPointSize(int pointsize
)
703 SetXFontComponent(wxXLFD_POINTSIZE
, wxString::Format(_T("%d"), pointsize
));
706 void wxNativeFontInfo::SetStyle(wxFontStyle style
)
711 case wxFONTSTYLE_ITALIC
:
715 case wxFONTSTYLE_SLANT
:
719 case wxFONTSTYLE_NORMAL
:
723 wxFAIL_MSG( _T("unknown wxFontStyle in wxNativeFontInfo::SetStyle") );
727 SetXFontComponent(wxXLFD_SLANT
, s
);
730 void wxNativeFontInfo::SetWeight(wxFontWeight weight
)
735 case wxFONTWEIGHT_BOLD
:
739 case wxFONTWEIGHT_LIGHT
:
743 case wxFONTWEIGHT_NORMAL
:
748 wxFAIL_MSG( _T("unknown wxFontWeight in wxNativeFontInfo::SetWeight") );
752 SetXFontComponent(wxXLFD_WEIGHT
, s
);
755 void wxNativeFontInfo::SetUnderlined(bool WXUNUSED(underlined
))
757 // can't do this under X
760 void wxNativeFontInfo::SetFaceName(wxString facename
)
762 SetXFontComponent(wxXLFD_FAMILY
, facename
);
765 void wxNativeFontInfo::SetFamily(wxFontFamily family
)
767 // wxFontFamily -> X foundry, anyone?
768 wxFAIL_MSG( _T("not implemented") );
770 // SetXFontComponent(wxXLFD_FOUNDRY, ...);
773 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding
)
775 wxNativeEncodingInfo info
;
776 if ( wxGetNativeFontEncoding(encoding
, &info
) )
778 SetXFontComponent(wxXLFD_ENCODING
, info
.xencoding
);
779 SetXFontComponent(wxXLFD_REGISTRY
, info
.xregistry
);
783 // ----------------------------------------------------------------------------
785 // ----------------------------------------------------------------------------
787 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
788 wxNativeEncodingInfo
*info
)
790 wxCHECK_MSG( info
, FALSE
, _T("bad pointer in wxGetNativeFontEncoding") );
792 if ( encoding
== wxFONTENCODING_DEFAULT
)
794 encoding
= wxFont::GetDefaultEncoding();
799 case wxFONTENCODING_ISO8859_1
:
800 case wxFONTENCODING_ISO8859_2
:
801 case wxFONTENCODING_ISO8859_3
:
802 case wxFONTENCODING_ISO8859_4
:
803 case wxFONTENCODING_ISO8859_5
:
804 case wxFONTENCODING_ISO8859_6
:
805 case wxFONTENCODING_ISO8859_7
:
806 case wxFONTENCODING_ISO8859_8
:
807 case wxFONTENCODING_ISO8859_9
:
808 case wxFONTENCODING_ISO8859_10
:
809 case wxFONTENCODING_ISO8859_11
:
810 case wxFONTENCODING_ISO8859_12
:
811 case wxFONTENCODING_ISO8859_13
:
812 case wxFONTENCODING_ISO8859_14
:
813 case wxFONTENCODING_ISO8859_15
:
815 int cp
= encoding
- wxFONTENCODING_ISO8859_1
+ 1;
816 info
->xregistry
= wxT("iso8859");
817 info
->xencoding
.Printf(wxT("%d"), cp
);
821 case wxFONTENCODING_UTF8
:
822 info
->xregistry
= wxT("iso10646");
823 info
->xencoding
= wxT("*");
826 case wxFONTENCODING_GB2312
:
827 info
->xregistry
= wxT("GB2312"); // or the otherway round?
828 info
->xencoding
= wxT("*");
831 case wxFONTENCODING_KOI8
:
832 case wxFONTENCODING_KOI8_U
:
833 info
->xregistry
= wxT("koi8");
835 // we don't make distinction between koi8-r, koi8-u and koi8-ru (so far)
836 info
->xencoding
= wxT("*");
839 case wxFONTENCODING_CP1250
:
840 case wxFONTENCODING_CP1251
:
841 case wxFONTENCODING_CP1252
:
842 case wxFONTENCODING_CP1253
:
843 case wxFONTENCODING_CP1254
:
844 case wxFONTENCODING_CP1255
:
845 case wxFONTENCODING_CP1256
:
846 case wxFONTENCODING_CP1257
:
848 int cp
= encoding
- wxFONTENCODING_CP1250
+ 1250;
849 info
->xregistry
= wxT("microsoft");
850 info
->xencoding
.Printf(wxT("cp%d"), cp
);
854 case wxFONTENCODING_EUC_JP
:
855 case wxFONTENCODING_SHIFT_JIS
:
856 info
->xregistry
= "jis*";
857 info
->xencoding
= "*";
860 case wxFONTENCODING_SYSTEM
:
862 info
->xencoding
= wxT("*");
866 // don't know how to translate this encoding into X fontspec
870 info
->encoding
= encoding
;
875 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
878 fontspec
.Printf(_T("-*-%s-*-*-*-*-*-*-*-*-*-*-%s-%s"),
879 !info
.facename
? _T("*") : info
.facename
.c_str(),
880 info
.xregistry
.c_str(),
881 info
.xencoding
.c_str());
883 return wxTestFontSpec(fontspec
);
886 // ----------------------------------------------------------------------------
887 // X-specific functions
888 // ----------------------------------------------------------------------------
890 wxNativeFont
wxLoadQueryNearestFont(int pointSize
,
895 const wxString
&facename
,
896 wxFontEncoding encoding
,
899 if ( encoding
== wxFONTENCODING_DEFAULT
)
901 encoding
= wxFont::GetDefaultEncoding();
904 // first determine the encoding - if the font doesn't exist at all in this
905 // encoding, it's useless to do all other approximations (i.e. size,
906 // family &c don't matter much)
907 wxNativeEncodingInfo info
;
908 if ( encoding
== wxFONTENCODING_SYSTEM
)
910 // This will always work so we don't test to save time
911 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
915 if ( !wxGetNativeFontEncoding(encoding
, &info
) ||
916 !wxTestFontEncoding(info
) )
919 if ( !wxFontMapper::Get()->GetAltForEncoding(encoding
, &info
) )
920 #endif // wxUSE_FONTMAP
922 // unspported encoding - replace it with the default
924 // NB: we can't just return 0 from here because wxGTK code doesn't
925 // check for it (i.e. it supposes that we'll always succeed),
926 // so it would provoke a crash
927 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
932 // OK, we have the correct xregistry/xencoding in info structure
933 wxNativeFont font
= 0;
935 // if we already have the X font name, try to use it
936 if( xFontName
&& !xFontName
->IsEmpty() )
939 // Make sure point size is correct for scale factor.
941 wxStringTokenizer
tokenizer(*xFontName
, _T("-"), wxTOKEN_RET_DELIMS
);
942 wxString newFontName
;
944 for(int i
= 0; i
< 8; i
++)
945 newFontName
+= tokenizer
.NextToken();
947 (void) tokenizer
.NextToken();
949 newFontName
+= wxString::Format(wxT("%d-"), pointSize
);
951 while(tokenizer
.HasMoreTokens())
952 newFontName
+= tokenizer
.GetNextToken();
954 font
= wxLoadFont(newFontName
);
957 *xFontName
= newFontName
;
962 // search up and down by stepsize 10
963 int max_size
= pointSize
+ 20 * (1 + (pointSize
/180));
964 int min_size
= pointSize
- 20 * (1 + (pointSize
/180));
966 int i
, round
; // counters
968 // first round: search for equal, then for smaller and for larger size with the given weight and style
969 int testweight
= weight
;
970 int teststyle
= style
;
972 for ( round
= 0; round
< 3; round
++ )
974 // second round: use normal weight
977 if ( testweight
!= wxNORMAL
)
979 testweight
= wxNORMAL
;
983 ++round
; // fall through to third round
987 // third round: ... and use normal style
990 if ( teststyle
!= wxNORMAL
)
992 teststyle
= wxNORMAL
;
999 // Search for equal or smaller size (approx.)
1000 for ( i
= pointSize
; !font
&& i
>= 10 && i
>= min_size
; i
-= 10 )
1002 font
= wxLoadQueryFont(i
, family
, teststyle
, testweight
, underlined
,
1003 facename
, info
.xregistry
, info
.xencoding
,
1007 // Search for larger size (approx.)
1008 for ( i
= pointSize
+ 10; !font
&& i
<= max_size
; i
+= 10 )
1010 font
= wxLoadQueryFont(i
, family
, teststyle
, testweight
, underlined
,
1011 facename
, info
.xregistry
, info
.xencoding
,
1016 // Try default family
1017 if ( !font
&& family
!= wxDEFAULT
)
1019 font
= wxLoadQueryFont(pointSize
, wxDEFAULT
, style
, weight
,
1020 underlined
, facename
,
1021 info
.xregistry
, info
.xencoding
,
1025 // ignore size, family, style and weight but try to find font with the
1026 // given facename and encoding
1029 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1030 underlined
, facename
,
1031 info
.xregistry
, info
.xencoding
,
1034 // ignore family as well
1037 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1038 underlined
, wxEmptyString
,
1039 info
.xregistry
, info
.xencoding
,
1042 // if it still failed, try to get the font of any size but
1043 // with the requested encoding: this can happen if the
1044 // encoding is only available in one size which happens to be
1045 // different from 120
1048 font
= wxLoadQueryFont(-1, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1049 FALSE
, wxEmptyString
,
1050 info
.xregistry
, info
.xencoding
,
1053 // this should never happen as we had tested for it in the
1054 // very beginning, but if it does, do return something non
1055 // NULL or we'd crash in wxFont code
1058 wxFAIL_MSG( _T("this encoding should be available!") );
1060 font
= wxLoadQueryFont(-1,
1061 wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1062 FALSE
, wxEmptyString
,
1074 // ----------------------------------------------------------------------------
1075 // private functions
1076 // ----------------------------------------------------------------------------
1078 // returns TRUE if there are any fonts matching this font spec
1079 static bool wxTestFontSpec(const wxString
& fontspec
)
1081 // some X servers will fail to load this font because there are too many
1082 // matches so we must test explicitly for this
1083 if ( fontspec
== _T("-*-*-*-*-*-*-*-*-*-*-*-*-*-*") )
1088 wxNativeFont test
= (wxNativeFont
) g_fontHash
->Get( fontspec
);
1094 test
= wxLoadFont(fontspec
);
1095 g_fontHash
->Put( fontspec
, (wxObject
*) test
);
1109 static wxNativeFont
wxLoadQueryFont(int pointSize
,
1113 bool WXUNUSED(underlined
),
1114 const wxString
& facename
,
1115 const wxString
& xregistry
,
1116 const wxString
& xencoding
,
1117 wxString
* xFontName
)
1122 case wxDECORATIVE
: xfamily
= wxT("lucida"); break;
1123 case wxROMAN
: xfamily
= wxT("times"); break;
1124 case wxMODERN
: xfamily
= wxT("courier"); break;
1125 case wxSWISS
: xfamily
= wxT("helvetica"); break;
1126 case wxTELETYPE
: xfamily
= wxT("lucidatypewriter"); break;
1127 case wxSCRIPT
: xfamily
= wxT("utopia"); break;
1128 default: xfamily
= wxT("*");
1136 xweight
= MWLF_WEIGHT_BOLD
;
1141 xweight
= MWLF_WEIGHT_LIGHT
;
1146 xweight
= MWLF_WEIGHT_NORMAL
;
1152 xweight
= MWLF_WEIGHT_DEFAULT
;
1156 GR_SCREEN_INFO screenInfo
;
1157 GrGetScreenInfo(& screenInfo
);
1159 int yPixelsPerCM
= screenInfo
.ydpcm
;
1161 // A point is 1/72 of an inch.
1162 // An inch is 2.541 cm.
1163 // So pixelHeight = (pointSize / 72) (inches) * 2.541 (for cm) * yPixelsPerCM (for pixels)
1164 // In fact pointSize is 10 * the normal point size so
1167 int pixelHeight
= (int) ( (((float)pointSize
) / 720.0) * 2.541 * (float) yPixelsPerCM
) ;
1169 // An alternative: assume that the screen is 72 dpi.
1170 //int pixelHeight = (int) (((float)pointSize / 720.0) * 72.0) ;
1171 //int pixelHeight = (int) ((float)pointSize / 10.0) ;
1174 logFont
.lfHeight
= pixelHeight
;
1175 logFont
.lfWidth
= 0;
1176 logFont
.lfEscapement
= 0;
1177 logFont
.lfOrientation
= 0;
1178 logFont
.lfWeight
= xweight
;
1179 logFont
.lfItalic
= (style
== wxNORMAL
? 0 : 1) ;
1180 logFont
.lfUnderline
= 0;
1181 logFont
.lfStrikeOut
= 0;
1182 logFont
.lfCharSet
= MWLF_CHARSET_DEFAULT
; // TODO: select appropriate one
1183 logFont
.lfOutPrecision
= MWLF_TYPE_DEFAULT
;
1184 logFont
.lfClipPrecision
= 0; // Not used
1185 logFont
.lfRoman
= (family
== wxROMAN
? 1 : 0) ;
1186 logFont
.lfSerif
= (family
== wxSWISS
? 0 : 1) ;
1187 logFont
.lfSansSerif
= !logFont
.lfSerif
;
1188 logFont
.lfModern
= (family
== wxMODERN
? 1 : 0) ;
1189 logFont
.lfProportional
= (family
== wxTELETYPE
? 0 : 1) ;
1190 logFont
.lfOblique
= 0;
1191 logFont
.lfSmallCaps
= 0;
1192 logFont
.lfPitch
= 0; // 0 = default
1193 strcpy(logFont
.lfFaceName
, facename
.c_str());
1195 XFontStruct
* fontInfo
= (XFontStruct
*) malloc(sizeof(XFontStruct
));
1196 fontInfo
->fid
= GrCreateFont((GR_CHAR
*) facename
.c_str(), pixelHeight
, & logFont
);
1197 GrGetFontInfo(fontInfo
->fid
, & fontInfo
->info
);
1198 return (wxNativeFont
) fontInfo
;
1202 if (!facename
.IsEmpty())
1204 fontSpec
.Printf(wxT("-*-%s-*-*-normal-*-*-*-*-*-*-*-*-*"),
1207 if ( wxTestFontSpec(fontSpec
) )
1211 //else: no such family, use default one instead
1218 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1220 if ( wxTestFontSpec(fontSpec
) )
1225 // fall through - try wxITALIC now
1228 fontSpec
.Printf(wxT("-*-%s-*-i-*-*-*-*-*-*-*-*-*-*"),
1230 if ( wxTestFontSpec(fontSpec
) )
1234 else if ( style
== wxITALIC
) // and not wxSLANT
1237 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1239 if ( wxTestFontSpec(fontSpec
) )
1245 // no italic, no slant - leave default
1252 wxFAIL_MSG(_T("unknown font style"));
1253 // fall back to normal
1265 fontSpec
.Printf(wxT("-*-%s-bold-*-*-*-*-*-*-*-*-*-*-*"),
1267 if ( wxTestFontSpec(fontSpec
) )
1269 xweight
= wxT("bold");
1272 fontSpec
.Printf(wxT("-*-%s-heavy-*-*-*-*-*-*-*-*-*-*-*"),
1274 if ( wxTestFontSpec(fontSpec
) )
1276 xweight
= wxT("heavy");
1279 fontSpec
.Printf(wxT("-*-%s-extrabold-*-*-*-*-*-*-*-*-*-*-*"),
1281 if ( wxTestFontSpec(fontSpec
) )
1283 xweight
= wxT("extrabold");
1286 fontSpec
.Printf(wxT("-*-%s-demibold-*-*-*-*-*-*-*-*-*-*-*"),
1288 if ( wxTestFontSpec(fontSpec
) )
1290 xweight
= wxT("demibold");
1293 fontSpec
.Printf(wxT("-*-%s-black-*-*-*-*-*-*-*-*-*-*-*"),
1295 if ( wxTestFontSpec(fontSpec
) )
1297 xweight
= wxT("black");
1300 fontSpec
.Printf(wxT("-*-%s-ultrablack-*-*-*-*-*-*-*-*-*-*-*"),
1302 if ( wxTestFontSpec(fontSpec
) )
1304 xweight
= wxT("ultrablack");
1311 fontSpec
.Printf(wxT("-*-%s-light-*-*-*-*-*-*-*-*-*-*-*"),
1313 if ( wxTestFontSpec(fontSpec
) )
1315 xweight
= wxT("light");
1318 fontSpec
.Printf(wxT("-*-%s-thin-*-*-*-*-*-*-*-*-*-*-*"),
1320 if ( wxTestFontSpec(fontSpec
) )
1322 xweight
= wxT("thin");
1329 fontSpec
.Printf(wxT("-*-%s-medium-*-*-*-*-*-*-*-*-*-*-*"),
1331 if ( wxTestFontSpec(fontSpec
) )
1333 xweight
= wxT("medium");
1336 fontSpec
.Printf(wxT("-*-%s-normal-*-*-*-*-*-*-*-*-*-*-*"),
1338 if ( wxTestFontSpec(fontSpec
) )
1340 xweight
= wxT("normal");
1343 fontSpec
.Printf(wxT("-*-%s-regular-*-*-*-*-*-*-*-*-*-*-*"),
1345 if ( wxTestFontSpec(fontSpec
) )
1347 xweight
= wxT("regular");
1353 default: xweight
= wxT("*"); break;
1356 // if pointSize is -1, don't specify any
1358 if ( pointSize
== -1 )
1364 sizeSpec
.Printf(_T("%d"), pointSize
);
1367 // construct the X font spec from our data
1368 fontSpec
.Printf(wxT("-*-%s-%s-%s-normal-*-*-%s-*-*-*-*-%s-%s"),
1369 xfamily
.c_str(), xweight
.c_str(), xstyle
.c_str(),
1370 sizeSpec
.c_str(), xregistry
.c_str(), xencoding
.c_str());
1373 *xFontName
= fontSpec
;
1375 return wxLoadFont(fontSpec
);
1380 // ----------------------------------------------------------------------------
1382 // ----------------------------------------------------------------------------
1384 class wxFontModule
: public wxModule
1391 DECLARE_DYNAMIC_CLASS(wxFontModule
)
1394 IMPLEMENT_DYNAMIC_CLASS(wxFontModule
, wxModule
)
1396 bool wxFontModule::OnInit()
1398 g_fontHash
= new wxHashTable( wxKEY_STRING
);
1403 void wxFontModule::OnExit()
1407 g_fontHash
= (wxHashTable
*)NULL
;
1410 #endif // GTK 2.0/1.x