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(facename
) );
262 void wxNativeFontInfo::SetFamily(wxFontFamily
WXUNUSED(family
))
264 wxFAIL_MSG( _T("not implemented") );
267 void wxNativeFontInfo::SetEncoding(wxFontEncoding
WXUNUSED(encoding
))
269 wxFAIL_MSG( _T("not implemented") );
274 bool wxNativeFontInfo::FromString(const wxString
& s
)
277 pango_font_description_free( description
);
279 description
= pango_font_description_from_string( wxGTK_CONV( s
) );
284 wxString
wxNativeFontInfo::ToString() const
286 char *str
= pango_font_description_to_string( description
);
287 wxString tmp
= wxGTK_CONV_BACK( str
);
293 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
295 return FromString( s
);
298 wxString
wxNativeFontInfo::ToUserString() const
303 // ----------------------------------------------------------------------------
304 // wxNativeEncodingInfo
305 // ----------------------------------------------------------------------------
307 bool wxNativeEncodingInfo::FromString(const wxString
& 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_UTF8
:
337 info
->encoding
= wxFONTENCODING_UTF8
;
341 // everything else must be converted to UTF-8
350 #pragma message disable nosimpint
353 #include <X11/Xlib.h>
356 #pragma message enable nosimpint
359 #include "wx/utils.h" // for wxGetDisplay()
360 #elif defined(__WXGTK__)
361 // we have to declare struct tm to avoid problems with first forward
362 // declaring it in C code (glib.h included from gdk.h does it) and then
363 // defining it when time.h is included from the headers below - this is
364 // known not to work at least with Sun CC 6.01
371 // ----------------------------------------------------------------------------
373 // ----------------------------------------------------------------------------
375 static wxHashTable
*g_fontHash
= (wxHashTable
*) NULL
;
377 // ----------------------------------------------------------------------------
379 // ----------------------------------------------------------------------------
381 // define the functions to create and destroy native fonts for this toolkit
383 wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
385 return XLoadQueryFont((Display
*)wxGetDisplay(), fontSpec
);
388 inline void wxFreeFont(wxNativeFont font
)
390 XFreeFont((Display
*)wxGetDisplay(), (XFontStruct
*)font
);
392 #elif defined(__WXGTK__)
393 wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
395 // VZ: we should use gdk_fontset_load() instead of gdk_font_load()
396 // here to be able to display Japanese fonts correctly (at least
397 // this is what people report) but unfortunately doing it results
398 // in tons of warnings when using GTK with "normal" European
399 // languages and so we can't always do it and I don't know enough
400 // to determine when should this be done... (FIXME)
401 return gdk_font_load( wxConvertWX2MB(fontSpec
) );
404 inline void wxFreeFont(wxNativeFont font
)
406 gdk_font_unref(font
);
409 #error "Unknown GUI toolkit"
412 static bool wxTestFontSpec(const wxString
& fontspec
);
414 static wxNativeFont
wxLoadQueryFont(int pointSize
,
419 const wxString
& facename
,
420 const wxString
& xregistry
,
421 const wxString
& xencoding
,
422 wxString
* xFontName
);
424 // ============================================================================
426 // ============================================================================
428 // ----------------------------------------------------------------------------
429 // wxNativeEncodingInfo
430 // ----------------------------------------------------------------------------
432 // convert to/from the string representation: format is
433 // encodingid;registry;encoding[;facename]
434 bool wxNativeEncodingInfo::FromString(const wxString
& s
)
436 // use ";", not "-" because it may be part of encoding name
437 wxStringTokenizer
tokenizer(s
, _T(";"));
439 wxString encid
= tokenizer
.GetNextToken();
441 if ( !encid
.ToLong(&enc
) )
443 encoding
= (wxFontEncoding
)enc
;
445 xregistry
= tokenizer
.GetNextToken();
449 xencoding
= tokenizer
.GetNextToken();
454 facename
= tokenizer
.GetNextToken();
459 wxString
wxNativeEncodingInfo::ToString() const
462 s
<< (long)encoding
<< _T(';') << xregistry
<< _T(';') << xencoding
;
463 if ( !facename
.empty() )
465 s
<< _T(';') << facename
;
471 // ----------------------------------------------------------------------------
473 // ----------------------------------------------------------------------------
475 void wxNativeFontInfo::Init()
480 bool wxNativeFontInfo::FromString(const wxString
& s
)
482 wxStringTokenizer
tokenizer(s
, _T(";"));
485 wxString token
= tokenizer
.GetNextToken();
486 if ( token
!= _T('0') )
489 xFontName
= tokenizer
.GetNextToken();
491 // this should be the end
492 if ( tokenizer
.HasMoreTokens() )
495 return FromXFontName(xFontName
);
498 wxString
wxNativeFontInfo::ToString() const
501 return wxString::Format(_T("%d;%s"), 0, GetXFontName().c_str());
504 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
506 return FromXFontName(s
);
509 wxString
wxNativeFontInfo::ToUserString() const
511 return GetXFontName();
514 bool wxNativeFontInfo::HasElements() const
516 // we suppose that the foundry is never empty, so if it is it means that we
517 // had never parsed the XLFD
518 return !fontElements
[0].empty();
521 wxString
wxNativeFontInfo::GetXFontComponent(wxXLFDField field
) const
523 wxCHECK_MSG( field
< wxXLFD_MAX
, wxEmptyString
, _T("invalid XLFD field") );
525 if ( !HasElements() )
528 if ( !((wxNativeFontInfo
*)this)->FromXFontName(xFontName
) )
529 return wxEmptyString
;
532 return fontElements
[field
];
535 bool wxNativeFontInfo::FromXFontName(const wxString
& fontname
)
537 // TODO: we should be able to handle the font aliases here, but how?
538 wxStringTokenizer
tokenizer(fontname
, _T("-"));
540 // skip the leading, usually empty field (font name registry)
541 if ( !tokenizer
.HasMoreTokens() )
544 (void)tokenizer
.GetNextToken();
546 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
548 if ( !tokenizer
.HasMoreTokens() )
550 // not enough elements in the XLFD - or maybe an alias
554 wxString field
= tokenizer
.GetNextToken();
555 if ( !field
.empty() && field
!= _T('*') )
557 // we're really initialized now
561 fontElements
[n
] = field
;
564 // this should be all
565 if ( tokenizer
.HasMoreTokens() )
571 wxString
wxNativeFontInfo::GetXFontName() const
573 if ( xFontName
.empty() )
575 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
577 // replace the non specified elements with '*' except for the
578 // additional style which is usually just omitted
579 wxString elt
= fontElements
[n
];
580 if ( elt
.empty() && n
!= wxXLFD_ADDSTYLE
)
586 ((wxNativeFontInfo
*)this)->xFontName
<< _T('-') << elt
;
594 wxNativeFontInfo::SetXFontComponent(wxXLFDField field
, const wxString
& value
)
596 wxCHECK_RET( field
< wxXLFD_MAX
, _T("invalid XLFD field") );
598 // this class should be initialized with a valid font spec first and only
599 // then the fields may be modified!
600 wxASSERT_MSG( !IsDefault(), _T("can't modify an uninitialized XLFD") );
602 if ( !HasElements() )
605 if ( !((wxNativeFontInfo
*)this)->FromXFontName(xFontName
) )
607 wxFAIL_MSG( _T("can't set font element for invalid XLFD") );
613 fontElements
[field
] = value
;
615 // invalidate the XFLD, it doesn't correspond to the font elements any more
619 void wxNativeFontInfo::SetXFontName(const wxString
& xFontName_
)
621 // invalidate the font elements, GetXFontComponent() will reparse the XLFD
622 fontElements
[0].clear();
624 xFontName
= xFontName_
;
629 int wxNativeFontInfo::GetPointSize() const
631 const wxString s
= GetXFontComponent(wxXLFD_POINTSIZE
);
633 // return -1 to indicate that the size is unknown
635 return s
.ToLong(&l
) ? l
: -1;
638 wxFontStyle
wxNativeFontInfo::GetStyle() const
640 const wxString s
= GetXFontComponent(wxXLFD_SLANT
);
642 if ( s
.length() != 1 )
644 // it is really unknown but we don't have any way to return it from
646 return wxFONTSTYLE_NORMAL
;
652 // again, unknown but consider normal by default
655 return wxFONTSTYLE_NORMAL
;
658 return wxFONTSTYLE_ITALIC
;
661 return wxFONTSTYLE_SLANT
;
665 wxFontWeight
wxNativeFontInfo::GetWeight() const
667 const wxString s
= GetXFontComponent(wxXLFD_WEIGHT
).MakeLower();
668 if ( s
.find(_T("bold")) != wxString::npos
|| s
== _T("black") )
669 return wxFONTWEIGHT_BOLD
;
670 else if ( s
== _T("light") )
671 return wxFONTWEIGHT_LIGHT
;
673 return wxFONTWEIGHT_NORMAL
;
676 bool wxNativeFontInfo::GetUnderlined() const
678 // X fonts are never underlined
682 wxString
wxNativeFontInfo::GetFaceName() const
684 // wxWidgets facename probably more accurately corresponds to X family
685 return GetXFontComponent(wxXLFD_FAMILY
);
688 wxFontFamily
wxNativeFontInfo::GetFamily() const
690 // and wxWidgets family -- to X foundry, but we have to translate it to
691 // wxFontFamily somehow...
692 wxFAIL_MSG(_T("not implemented")); // GetXFontComponent(wxXLFD_FOUNDRY);
694 return wxFONTFAMILY_DEFAULT
;
697 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
699 // we already have the code for this but need to refactor it first
700 wxFAIL_MSG( _T("not implemented") );
702 return wxFONTENCODING_MAX
;
705 void wxNativeFontInfo::SetPointSize(int pointsize
)
707 SetXFontComponent(wxXLFD_POINTSIZE
, wxString::Format(_T("%d"), pointsize
));
710 void wxNativeFontInfo::SetStyle(wxFontStyle style
)
715 case wxFONTSTYLE_ITALIC
:
719 case wxFONTSTYLE_SLANT
:
723 case wxFONTSTYLE_NORMAL
:
727 wxFAIL_MSG( _T("unknown wxFontStyle in wxNativeFontInfo::SetStyle") );
731 SetXFontComponent(wxXLFD_SLANT
, s
);
734 void wxNativeFontInfo::SetWeight(wxFontWeight weight
)
739 case wxFONTWEIGHT_BOLD
:
743 case wxFONTWEIGHT_LIGHT
:
747 case wxFONTWEIGHT_NORMAL
:
752 wxFAIL_MSG( _T("unknown wxFontWeight in wxNativeFontInfo::SetWeight") );
756 SetXFontComponent(wxXLFD_WEIGHT
, s
);
759 void wxNativeFontInfo::SetUnderlined(bool WXUNUSED(underlined
))
761 // can't do this under X
764 void wxNativeFontInfo::SetFaceName(const wxString
& facename
)
766 SetXFontComponent(wxXLFD_FAMILY
, facename
);
769 void wxNativeFontInfo::SetFamily(wxFontFamily
WXUNUSED(family
))
771 // wxFontFamily -> X foundry, anyone?
772 wxFAIL_MSG( _T("not implemented") );
774 // SetXFontComponent(wxXLFD_FOUNDRY, ...);
777 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding
)
779 wxNativeEncodingInfo info
;
780 if ( wxGetNativeFontEncoding(encoding
, &info
) )
782 SetXFontComponent(wxXLFD_ENCODING
, info
.xencoding
);
783 SetXFontComponent(wxXLFD_REGISTRY
, info
.xregistry
);
787 // ----------------------------------------------------------------------------
789 // ----------------------------------------------------------------------------
791 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
792 wxNativeEncodingInfo
*info
)
794 wxCHECK_MSG( info
, false, _T("bad pointer in wxGetNativeFontEncoding") );
796 if ( encoding
== wxFONTENCODING_DEFAULT
)
798 encoding
= wxFont::GetDefaultEncoding();
803 case wxFONTENCODING_ISO8859_1
:
804 case wxFONTENCODING_ISO8859_2
:
805 case wxFONTENCODING_ISO8859_3
:
806 case wxFONTENCODING_ISO8859_4
:
807 case wxFONTENCODING_ISO8859_5
:
808 case wxFONTENCODING_ISO8859_6
:
809 case wxFONTENCODING_ISO8859_7
:
810 case wxFONTENCODING_ISO8859_8
:
811 case wxFONTENCODING_ISO8859_9
:
812 case wxFONTENCODING_ISO8859_10
:
813 case wxFONTENCODING_ISO8859_11
:
814 case wxFONTENCODING_ISO8859_12
:
815 case wxFONTENCODING_ISO8859_13
:
816 case wxFONTENCODING_ISO8859_14
:
817 case wxFONTENCODING_ISO8859_15
:
819 int cp
= encoding
- wxFONTENCODING_ISO8859_1
+ 1;
820 info
->xregistry
= wxT("iso8859");
821 info
->xencoding
.Printf(wxT("%d"), cp
);
825 case wxFONTENCODING_UTF8
:
826 info
->xregistry
= wxT("iso10646");
827 info
->xencoding
= wxT("*");
830 case wxFONTENCODING_GB2312
:
831 info
->xregistry
= wxT("GB2312"); // or the otherway round?
832 info
->xencoding
= wxT("*");
835 case wxFONTENCODING_KOI8
:
836 case wxFONTENCODING_KOI8_U
:
837 info
->xregistry
= wxT("koi8");
839 // we don't make distinction between koi8-r, koi8-u and koi8-ru (so far)
840 info
->xencoding
= wxT("*");
843 case wxFONTENCODING_CP1250
:
844 case wxFONTENCODING_CP1251
:
845 case wxFONTENCODING_CP1252
:
846 case wxFONTENCODING_CP1253
:
847 case wxFONTENCODING_CP1254
:
848 case wxFONTENCODING_CP1255
:
849 case wxFONTENCODING_CP1256
:
850 case wxFONTENCODING_CP1257
:
852 int cp
= encoding
- wxFONTENCODING_CP1250
+ 1250;
853 info
->xregistry
= wxT("microsoft");
854 info
->xencoding
.Printf(wxT("cp%d"), cp
);
858 case wxFONTENCODING_EUC_JP
:
859 case wxFONTENCODING_SHIFT_JIS
:
860 info
->xregistry
= "jis*";
861 info
->xencoding
= "*";
864 case wxFONTENCODING_SYSTEM
:
866 info
->xencoding
= wxT("*");
870 // don't know how to translate this encoding into X fontspec
874 info
->encoding
= encoding
;
879 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
882 fontspec
.Printf(_T("-*-%s-*-*-*-*-*-*-*-*-*-*-%s-%s"),
883 !info
.facename
? _T("*") : info
.facename
.c_str(),
884 info
.xregistry
.c_str(),
885 info
.xencoding
.c_str());
887 return wxTestFontSpec(fontspec
);
890 // ----------------------------------------------------------------------------
891 // X-specific functions
892 // ----------------------------------------------------------------------------
894 wxNativeFont
wxLoadQueryNearestFont(int pointSize
,
899 const wxString
&facename
,
900 wxFontEncoding encoding
,
903 if ( encoding
== wxFONTENCODING_DEFAULT
)
905 encoding
= wxFont::GetDefaultEncoding();
908 // first determine the encoding - if the font doesn't exist at all in this
909 // encoding, it's useless to do all other approximations (i.e. size,
910 // family &c don't matter much)
911 wxNativeEncodingInfo info
;
912 if ( encoding
== wxFONTENCODING_SYSTEM
)
914 // This will always work so we don't test to save time
915 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
919 if ( !wxGetNativeFontEncoding(encoding
, &info
) ||
920 !wxTestFontEncoding(info
) )
923 if ( !wxFontMapper::Get()->GetAltForEncoding(encoding
, &info
) )
924 #endif // wxUSE_FONTMAP
926 // unspported encoding - replace it with the default
928 // NB: we can't just return 0 from here because wxGTK code doesn't
929 // check for it (i.e. it supposes that we'll always succeed),
930 // so it would provoke a crash
931 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
936 // OK, we have the correct xregistry/xencoding in info structure
937 wxNativeFont font
= 0;
939 // if we already have the X font name, try to use it
940 if( xFontName
&& !xFontName
->empty() )
943 // Make sure point size is correct for scale factor.
945 wxStringTokenizer
tokenizer(*xFontName
, _T("-"), wxTOKEN_RET_DELIMS
);
946 wxString newFontName
;
948 for(int i
= 0; i
< 8; i
++)
949 newFontName
+= tokenizer
.NextToken();
951 (void) tokenizer
.NextToken();
953 newFontName
+= wxString::Format(wxT("%d-"), pointSize
);
955 while(tokenizer
.HasMoreTokens())
956 newFontName
+= tokenizer
.GetNextToken();
958 font
= wxLoadFont(newFontName
);
961 *xFontName
= newFontName
;
966 // search up and down by stepsize 10
967 int max_size
= pointSize
+ 20 * (1 + (pointSize
/180));
968 int min_size
= pointSize
- 20 * (1 + (pointSize
/180));
970 int i
, round
; // counters
972 // first round: search for equal, then for smaller and for larger size with the given weight and style
973 int testweight
= weight
;
974 int teststyle
= style
;
976 for ( round
= 0; round
< 3; round
++ )
978 // second round: use normal weight
981 if ( testweight
!= wxNORMAL
)
983 testweight
= wxNORMAL
;
987 ++round
; // fall through to third round
991 // third round: ... and use normal style
994 if ( teststyle
!= wxNORMAL
)
996 teststyle
= wxNORMAL
;
1003 // Search for equal or smaller size (approx.)
1004 for ( i
= pointSize
; !font
&& i
>= 10 && i
>= min_size
; i
-= 10 )
1006 font
= wxLoadQueryFont(i
, family
, teststyle
, testweight
, underlined
,
1007 facename
, info
.xregistry
, info
.xencoding
,
1011 // Search for larger size (approx.)
1012 for ( i
= pointSize
+ 10; !font
&& i
<= max_size
; i
+= 10 )
1014 font
= wxLoadQueryFont(i
, family
, teststyle
, testweight
, underlined
,
1015 facename
, info
.xregistry
, info
.xencoding
,
1020 // Try default family
1021 if ( !font
&& family
!= wxDEFAULT
)
1023 font
= wxLoadQueryFont(pointSize
, wxDEFAULT
, style
, weight
,
1024 underlined
, facename
,
1025 info
.xregistry
, info
.xencoding
,
1029 // ignore size, family, style and weight but try to find font with the
1030 // given facename and encoding
1033 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1034 underlined
, facename
,
1035 info
.xregistry
, info
.xencoding
,
1038 // ignore family as well
1041 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1042 underlined
, wxEmptyString
,
1043 info
.xregistry
, info
.xencoding
,
1046 // if it still failed, try to get the font of any size but
1047 // with the requested encoding: this can happen if the
1048 // encoding is only available in one size which happens to be
1049 // different from 120
1052 font
= wxLoadQueryFont(-1, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1053 false, wxEmptyString
,
1054 info
.xregistry
, info
.xencoding
,
1057 // this should never happen as we had tested for it in the
1058 // very beginning, but if it does, do return something non
1059 // NULL or we'd crash in wxFont code
1062 wxFAIL_MSG( _T("this encoding should be available!") );
1064 font
= wxLoadQueryFont(-1,
1065 wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1066 false, wxEmptyString
,
1078 // ----------------------------------------------------------------------------
1079 // private functions
1080 // ----------------------------------------------------------------------------
1082 // returns true if there are any fonts matching this font spec
1083 static bool wxTestFontSpec(const wxString
& fontspec
)
1085 // some X servers will fail to load this font because there are too many
1086 // matches so we must test explicitly for this
1087 if ( fontspec
== _T("-*-*-*-*-*-*-*-*-*-*-*-*-*-*") )
1092 wxNativeFont test
= (wxNativeFont
) g_fontHash
->Get( fontspec
);
1098 test
= wxLoadFont(fontspec
);
1099 g_fontHash
->Put( fontspec
, (wxObject
*) test
);
1113 static wxNativeFont
wxLoadQueryFont(int pointSize
,
1117 bool WXUNUSED(underlined
),
1118 const wxString
& facename
,
1119 const wxString
& xregistry
,
1120 const wxString
& xencoding
,
1121 wxString
* xFontName
)
1126 case wxDECORATIVE
: xfamily
= wxT("lucida"); break;
1127 case wxROMAN
: xfamily
= wxT("times"); break;
1128 case wxMODERN
: xfamily
= wxT("courier"); break;
1129 case wxSWISS
: xfamily
= wxT("helvetica"); break;
1130 case wxTELETYPE
: xfamily
= wxT("lucidatypewriter"); break;
1131 case wxSCRIPT
: xfamily
= wxT("utopia"); break;
1132 default: xfamily
= wxT("*");
1140 xweight
= MWLF_WEIGHT_BOLD
;
1145 xweight
= MWLF_WEIGHT_LIGHT
;
1150 xweight
= MWLF_WEIGHT_NORMAL
;
1156 xweight
= MWLF_WEIGHT_DEFAULT
;
1160 GR_SCREEN_INFO screenInfo
;
1161 GrGetScreenInfo(& screenInfo
);
1163 int yPixelsPerCM
= screenInfo
.ydpcm
;
1165 // A point is 1/72 of an inch.
1166 // An inch is 2.541 cm.
1167 // So pixelHeight = (pointSize / 72) (inches) * 2.541 (for cm) * yPixelsPerCM (for pixels)
1168 // In fact pointSize is 10 * the normal point size so
1171 int pixelHeight
= (int) ( (((float)pointSize
) / 720.0) * 2.541 * (float) yPixelsPerCM
) ;
1173 // An alternative: assume that the screen is 72 dpi.
1174 //int pixelHeight = (int) (((float)pointSize / 720.0) * 72.0) ;
1175 //int pixelHeight = (int) ((float)pointSize / 10.0) ;
1178 logFont
.lfHeight
= pixelHeight
;
1179 logFont
.lfWidth
= 0;
1180 logFont
.lfEscapement
= 0;
1181 logFont
.lfOrientation
= 0;
1182 logFont
.lfWeight
= xweight
;
1183 logFont
.lfItalic
= (style
== wxNORMAL
? 0 : 1) ;
1184 logFont
.lfUnderline
= 0;
1185 logFont
.lfStrikeOut
= 0;
1186 logFont
.lfCharSet
= MWLF_CHARSET_DEFAULT
; // TODO: select appropriate one
1187 logFont
.lfOutPrecision
= MWLF_TYPE_DEFAULT
;
1188 logFont
.lfClipPrecision
= 0; // Not used
1189 logFont
.lfRoman
= (family
== wxROMAN
? 1 : 0) ;
1190 logFont
.lfSerif
= (family
== wxSWISS
? 0 : 1) ;
1191 logFont
.lfSansSerif
= !logFont
.lfSerif
;
1192 logFont
.lfModern
= (family
== wxMODERN
? 1 : 0) ;
1193 logFont
.lfProportional
= (family
== wxTELETYPE
? 0 : 1) ;
1194 logFont
.lfOblique
= 0;
1195 logFont
.lfSmallCaps
= 0;
1196 logFont
.lfPitch
= 0; // 0 = default
1197 strcpy(logFont
.lfFaceName
, facename
.c_str());
1199 XFontStruct
* fontInfo
= (XFontStruct
*) malloc(sizeof(XFontStruct
));
1200 fontInfo
->fid
= GrCreateFont((GR_CHAR
*) facename
.c_str(), pixelHeight
, & logFont
);
1201 GrGetFontInfo(fontInfo
->fid
, & fontInfo
->info
);
1202 return (wxNativeFont
) fontInfo
;
1206 if (!facename
.empty())
1208 fontSpec
.Printf(wxT("-*-%s-*-*-normal-*-*-*-*-*-*-*-*-*"),
1211 if ( wxTestFontSpec(fontSpec
) )
1215 //else: no such family, use default one instead
1222 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1224 if ( wxTestFontSpec(fontSpec
) )
1229 // fall through - try wxITALIC now
1232 fontSpec
.Printf(wxT("-*-%s-*-i-*-*-*-*-*-*-*-*-*-*"),
1234 if ( wxTestFontSpec(fontSpec
) )
1238 else if ( style
== wxITALIC
) // and not wxSLANT
1241 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1243 if ( wxTestFontSpec(fontSpec
) )
1249 // no italic, no slant - leave default
1256 wxFAIL_MSG(_T("unknown font style"));
1257 // fall back to normal
1269 fontSpec
.Printf(wxT("-*-%s-bold-*-*-*-*-*-*-*-*-*-*-*"),
1271 if ( wxTestFontSpec(fontSpec
) )
1273 xweight
= wxT("bold");
1276 fontSpec
.Printf(wxT("-*-%s-heavy-*-*-*-*-*-*-*-*-*-*-*"),
1278 if ( wxTestFontSpec(fontSpec
) )
1280 xweight
= wxT("heavy");
1283 fontSpec
.Printf(wxT("-*-%s-extrabold-*-*-*-*-*-*-*-*-*-*-*"),
1285 if ( wxTestFontSpec(fontSpec
) )
1287 xweight
= wxT("extrabold");
1290 fontSpec
.Printf(wxT("-*-%s-demibold-*-*-*-*-*-*-*-*-*-*-*"),
1292 if ( wxTestFontSpec(fontSpec
) )
1294 xweight
= wxT("demibold");
1297 fontSpec
.Printf(wxT("-*-%s-black-*-*-*-*-*-*-*-*-*-*-*"),
1299 if ( wxTestFontSpec(fontSpec
) )
1301 xweight
= wxT("black");
1304 fontSpec
.Printf(wxT("-*-%s-ultrablack-*-*-*-*-*-*-*-*-*-*-*"),
1306 if ( wxTestFontSpec(fontSpec
) )
1308 xweight
= wxT("ultrablack");
1315 fontSpec
.Printf(wxT("-*-%s-light-*-*-*-*-*-*-*-*-*-*-*"),
1317 if ( wxTestFontSpec(fontSpec
) )
1319 xweight
= wxT("light");
1322 fontSpec
.Printf(wxT("-*-%s-thin-*-*-*-*-*-*-*-*-*-*-*"),
1324 if ( wxTestFontSpec(fontSpec
) )
1326 xweight
= wxT("thin");
1333 fontSpec
.Printf(wxT("-*-%s-medium-*-*-*-*-*-*-*-*-*-*-*"),
1335 if ( wxTestFontSpec(fontSpec
) )
1337 xweight
= wxT("medium");
1340 fontSpec
.Printf(wxT("-*-%s-normal-*-*-*-*-*-*-*-*-*-*-*"),
1342 if ( wxTestFontSpec(fontSpec
) )
1344 xweight
= wxT("normal");
1347 fontSpec
.Printf(wxT("-*-%s-regular-*-*-*-*-*-*-*-*-*-*-*"),
1349 if ( wxTestFontSpec(fontSpec
) )
1351 xweight
= wxT("regular");
1357 default: xweight
= wxT("*"); break;
1360 // if pointSize is -1, don't specify any
1362 if ( pointSize
== -1 )
1368 sizeSpec
.Printf(_T("%d"), pointSize
);
1371 // construct the X font spec from our data
1372 fontSpec
.Printf(wxT("-*-%s-%s-%s-normal-*-*-%s-*-*-*-*-%s-%s"),
1373 xfamily
.c_str(), xweight
.c_str(), xstyle
.c_str(),
1374 sizeSpec
.c_str(), xregistry
.c_str(), xencoding
.c_str());
1377 *xFontName
= fontSpec
;
1379 return wxLoadFont(fontSpec
);
1384 // ----------------------------------------------------------------------------
1386 // ----------------------------------------------------------------------------
1388 class wxFontModule
: public wxModule
1395 DECLARE_DYNAMIC_CLASS(wxFontModule
)
1398 IMPLEMENT_DYNAMIC_CLASS(wxFontModule
, wxModule
)
1400 bool wxFontModule::OnInit()
1402 g_fontHash
= new wxHashTable( wxKEY_STRING
);
1407 void wxFontModule::OnExit()
1411 g_fontHash
= (wxHashTable
*)NULL
;
1414 #endif // GTK 2.0/1.x