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"
27 #include "wx/fontutil.h"
31 #include "wx/font.h" // wxFont enums
32 #include "wx/encinfo.h"
34 #include "wx/utils.h" // for wxGetDisplay()
37 #include "wx/fontmap.h"
38 #include "wx/tokenzr.h"
39 #include "wx/module.h"
43 #include "pango/pango.h"
46 #include "wx/gtk/private.h"
47 extern GtkWidget
*wxGetRootWindow();
49 #include "wx/x11/private.h"
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 void wxNativeFontInfo::Init()
62 wxNativeFontInfo::Init(const wxNativeFontInfo
& info
)
65 description
= pango_font_description_copy(info
.description
);
70 void wxNativeFontInfo::Free()
73 pango_font_description_free(description
);
76 int wxNativeFontInfo::GetPointSize() const
78 return pango_font_description_get_size( description
) / PANGO_SCALE
;
81 wxFontStyle
wxNativeFontInfo::GetStyle() const
83 wxFontStyle m_style
= wxFONTSTYLE_NORMAL
;
85 switch (pango_font_description_get_style( description
))
87 case PANGO_STYLE_NORMAL
:
88 m_style
= wxFONTSTYLE_NORMAL
;
90 case PANGO_STYLE_ITALIC
:
91 m_style
= wxFONTSTYLE_ITALIC
;
93 case PANGO_STYLE_OBLIQUE
:
94 m_style
= wxFONTSTYLE_SLANT
;
101 wxFontWeight
wxNativeFontInfo::GetWeight() const
104 // We seem to currently initialize only by string.
105 // In that case PANGO_FONT_MASK_WEIGHT is always set.
106 if (!(pango_font_description_get_set_fields(description
) & PANGO_FONT_MASK_WEIGHT
))
107 return wxFONTWEIGHT_NORMAL
;
110 PangoWeight pango_weight
= pango_font_description_get_weight( description
);
112 // Until the API can be changed the following ranges of weight values are used:
113 // wxFONTWEIGHT_LIGHT: 100 .. 349 - range of 250
114 // wxFONTWEIGHT_NORMAL: 350 .. 599 - range of 250
115 // wxFONTWEIGHT_BOLD: 600 .. 900 - range of 301 (600 is "semibold" already)
117 if (pango_weight
>= 600)
118 return wxFONTWEIGHT_BOLD
;
120 if (pango_weight
< 350)
121 return wxFONTWEIGHT_LIGHT
;
123 return wxFONTWEIGHT_NORMAL
;
126 bool wxNativeFontInfo::GetUnderlined() const
131 wxString
wxNativeFontInfo::GetFaceName() const
133 wxString tmp
= wxGTK_CONV_BACK( pango_font_description_get_family( description
) );
138 wxFontFamily
wxNativeFontInfo::GetFamily() const
140 wxFontFamily ret
= wxFONTFAMILY_DEFAULT
;
141 // note: not passing -1 as the 2nd parameter to g_ascii_strdown to work
142 // around a bug in the 64-bit glib shipped with solaris 10, -1 causes it
143 // to try to allocate 2^32 bytes.
144 const char *family_name
= pango_font_description_get_family( description
);
148 wxGtkString
family_text(g_ascii_strdown(family_name
, strlen(family_name
)));
150 // Check for some common fonts, to salvage what we can from the current win32 centric wxFont API:
151 if (strncmp( family_text
, "monospace", 9 ) == 0)
152 ret
= wxFONTFAMILY_TELETYPE
; // begins with "Monospace"
153 else if (strncmp( family_text
, "courier", 7 ) == 0)
154 ret
= wxFONTFAMILY_TELETYPE
; // begins with "Courier"
155 #if defined(__WXGTK24__) || defined(HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE)
158 if (!gtk_check_version(2,4,0))
161 PangoFontFamily
**families
;
162 PangoFontFamily
*family
= NULL
;
164 pango_context_list_families(
166 gtk_widget_get_pango_context( wxGetRootWindow() ),
168 wxTheApp
->GetPangoContext(),
170 &families
, &n_families
);
172 for (int i
= 0;i
< n_families
;++i
)
174 if (g_ascii_strcasecmp(pango_font_family_get_name( families
[i
] ), pango_font_description_get_family( description
)) == 0 )
176 family
= families
[i
];
183 // Some gtk+ systems might query for a non-existing font from wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT)
184 // on initialization, don't assert until wxSystemSettings::GetFont is checked for this - MR
185 // wxASSERT_MSG( family, wxT("wxNativeFontInfo::GetFamily() - No appropriate PangoFontFamily found for ::description") );
187 //BCI: Cache the wxFontFamily inside the class. Validate cache with
188 //BCI: g_ascii_strcasecmp(pango_font_description_get_family(description), pango_font_family_get_name(family)) == 0
190 if (family
!= NULL
&& pango_font_family_is_monospace( family
))
191 ret
= wxFONTFAMILY_TELETYPE
; // is deemed a monospace font by pango
193 #endif // gtk24 || HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE
195 if (ret
== wxFONTFAMILY_DEFAULT
)
197 if (strstr( family_text
, "sans" ) != NULL
) // checked before serif, so that "* Sans Serif" fonts are detected correctly
198 ret
= wxFONTFAMILY_SWISS
; // contains "Sans"
199 else if (strstr( family_text
, "serif" ) != NULL
)
200 ret
= wxFONTFAMILY_ROMAN
; // contains "Serif"
201 else if (strncmp( family_text
, "times", 5 ) == 0)
202 ret
= wxFONTFAMILY_ROMAN
; // begins with "Times"
203 else if (strncmp( family_text
, "old", 3 ) == 0)
204 ret
= wxFONTFAMILY_DECORATIVE
; // Begins with "Old" - "Old English", "Old Town"
210 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
212 return wxFONTENCODING_SYSTEM
;
216 void wxNativeFontInfo::SetPointSize(int pointsize
)
218 pango_font_description_set_size( description
, pointsize
* PANGO_SCALE
);
221 void wxNativeFontInfo::SetStyle(wxFontStyle style
)
225 case wxFONTSTYLE_ITALIC
:
226 pango_font_description_set_style( description
, PANGO_STYLE_ITALIC
);
228 case wxFONTSTYLE_SLANT
:
229 pango_font_description_set_style( description
, PANGO_STYLE_OBLIQUE
);
232 wxFAIL_MSG( _T("unknown font style") );
234 case wxFONTSTYLE_NORMAL
:
235 pango_font_description_set_style( description
, PANGO_STYLE_NORMAL
);
240 void wxNativeFontInfo::SetWeight(wxFontWeight weight
)
244 case wxFONTWEIGHT_BOLD
:
245 pango_font_description_set_weight(description
, PANGO_WEIGHT_BOLD
);
247 case wxFONTWEIGHT_LIGHT
:
248 pango_font_description_set_weight(description
, PANGO_WEIGHT_LIGHT
);
251 wxFAIL_MSG( _T("unknown font weight") );
253 case wxFONTWEIGHT_NORMAL
:
254 pango_font_description_set_weight(description
, PANGO_WEIGHT_NORMAL
);
258 void wxNativeFontInfo::SetUnderlined(bool WXUNUSED(underlined
))
260 wxFAIL_MSG( _T("not implemented") );
263 void wxNativeFontInfo::SetFaceName(const wxString
& facename
)
265 pango_font_description_set_family(description
, wxGTK_CONV_SYS(facename
));
268 void wxNativeFontInfo::SetFamily(wxFontFamily
WXUNUSED(family
))
270 wxFAIL_MSG( _T("not implemented") );
273 void wxNativeFontInfo::SetEncoding(wxFontEncoding
WXUNUSED(encoding
))
275 wxFAIL_MSG( _T("not implemented") );
280 bool wxNativeFontInfo::FromString(const wxString
& s
)
283 pango_font_description_free( description
);
285 // there is a bug in at least pango <= 1.13 which makes it (or its backends)
286 // segfault for very big point sizes and for negative point sizes.
287 // To workaround that bug for pango <= 1.13
288 // (see http://bugzilla.gnome.org/show_bug.cgi?id=340229)
289 // we do the check on the size here using same (arbitrary) limits used by
290 // pango > 1.13. Note that the segfault could happen also for pointsize
291 // smaller than this limit !!
293 const size_t pos
= str
.find_last_of(_T(" "));
295 if ( pos
!= wxString::npos
&& wxString(str
, pos
+ 1).ToDouble(&size
) )
300 else if ( size
>= 1E6
)
303 if ( !sizeStr
.empty() )
305 // replace the old size with the adjusted one
306 str
= wxString(s
, 0, pos
) + sizeStr
;
310 description
= pango_font_description_from_string( wxGTK_CONV_SYS( str
) );
315 wxString
wxNativeFontInfo::ToString() const
317 wxGtkString
str(pango_font_description_to_string( description
));
319 return wxGTK_CONV_BACK(str
);
322 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
324 return FromString( s
);
327 wxString
wxNativeFontInfo::ToUserString() const
332 // ----------------------------------------------------------------------------
333 // wxNativeEncodingInfo
334 // ----------------------------------------------------------------------------
336 bool wxNativeEncodingInfo::FromString(const wxString
& WXUNUSED(s
))
341 wxString
wxNativeEncodingInfo::ToString() const
343 return wxEmptyString
;
346 bool wxTestFontEncoding(const wxNativeEncodingInfo
& WXUNUSED(info
))
351 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
352 wxNativeEncodingInfo
*info
)
354 // all encodings are available in GTK+ 2 because we translate text in any
355 // encoding to UTF-8 internally anyhow
356 info
->facename
.clear();
357 info
->encoding
= encoding
;
366 #pragma message disable nosimpint
369 #include <X11/Xlib.h>
372 #pragma message enable nosimpint
375 #elif defined(__WXGTK__)
376 // we have to declare struct tm to avoid problems with first forward
377 // declaring it in C code (glib.h included from gdk.h does it) and then
378 // defining it when time.h is included from the headers below - this is
379 // known not to work at least with Sun CC 6.01
386 // ----------------------------------------------------------------------------
388 // ----------------------------------------------------------------------------
390 static wxHashTable
*g_fontHash
= (wxHashTable
*) NULL
;
392 // ----------------------------------------------------------------------------
394 // ----------------------------------------------------------------------------
396 // define the functions to create and destroy native fonts for this toolkit
398 wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
400 return XLoadQueryFont((Display
*)wxGetDisplay(), fontSpec
);
403 inline void wxFreeFont(wxNativeFont font
)
405 XFreeFont((Display
*)wxGetDisplay(), (XFontStruct
*)font
);
407 #elif defined(__WXGTK__)
408 wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
410 // VZ: we should use gdk_fontset_load() instead of gdk_font_load()
411 // here to be able to display Japanese fonts correctly (at least
412 // this is what people report) but unfortunately doing it results
413 // in tons of warnings when using GTK with "normal" European
414 // languages and so we can't always do it and I don't know enough
415 // to determine when should this be done... (FIXME)
416 return gdk_font_load( wxConvertWX2MB(fontSpec
) );
419 inline void wxFreeFont(wxNativeFont font
)
421 gdk_font_unref(font
);
424 #error "Unknown GUI toolkit"
427 static bool wxTestFontSpec(const wxString
& fontspec
);
429 static wxNativeFont
wxLoadQueryFont(int pointSize
,
434 const wxString
& facename
,
435 const wxString
& xregistry
,
436 const wxString
& xencoding
,
437 wxString
* xFontName
);
439 // ============================================================================
441 // ============================================================================
443 // ----------------------------------------------------------------------------
444 // wxNativeEncodingInfo
445 // ----------------------------------------------------------------------------
447 // convert to/from the string representation: format is
448 // encodingid;registry;encoding[;facename]
449 bool wxNativeEncodingInfo::FromString(const wxString
& s
)
451 // use ";", not "-" because it may be part of encoding name
452 wxStringTokenizer
tokenizer(s
, _T(";"));
454 wxString encid
= tokenizer
.GetNextToken();
456 if ( !encid
.ToLong(&enc
) )
458 encoding
= (wxFontEncoding
)enc
;
460 xregistry
= tokenizer
.GetNextToken();
464 xencoding
= tokenizer
.GetNextToken();
469 facename
= tokenizer
.GetNextToken();
474 wxString
wxNativeEncodingInfo::ToString() const
477 s
<< (long)encoding
<< _T(';') << xregistry
<< _T(';') << xencoding
;
478 if ( !facename
.empty() )
480 s
<< _T(';') << facename
;
486 // ----------------------------------------------------------------------------
488 // ----------------------------------------------------------------------------
490 void wxNativeFontInfo::Init()
495 bool wxNativeFontInfo::FromString(const wxString
& s
)
497 wxStringTokenizer
tokenizer(s
, _T(";"));
500 wxString token
= tokenizer
.GetNextToken();
501 if ( token
!= _T('0') )
504 xFontName
= tokenizer
.GetNextToken();
506 // this should be the end
507 if ( tokenizer
.HasMoreTokens() )
510 return FromXFontName(xFontName
);
513 wxString
wxNativeFontInfo::ToString() const
516 return wxString::Format(_T("%d;%s"), 0, GetXFontName().c_str());
519 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
521 return FromXFontName(s
);
524 wxString
wxNativeFontInfo::ToUserString() const
526 return GetXFontName();
529 bool wxNativeFontInfo::HasElements() const
531 // we suppose that the foundry is never empty, so if it is it means that we
532 // had never parsed the XLFD
533 return !fontElements
[0].empty();
536 wxString
wxNativeFontInfo::GetXFontComponent(wxXLFDField field
) const
538 wxCHECK_MSG( field
< wxXLFD_MAX
, wxEmptyString
, _T("invalid XLFD field") );
540 if ( !HasElements() )
543 if ( !((wxNativeFontInfo
*)this)->FromXFontName(xFontName
) )
544 return wxEmptyString
;
547 return fontElements
[field
];
550 bool wxNativeFontInfo::FromXFontName(const wxString
& fontname
)
552 // TODO: we should be able to handle the font aliases here, but how?
553 wxStringTokenizer
tokenizer(fontname
, _T("-"));
555 // skip the leading, usually empty field (font name registry)
556 if ( !tokenizer
.HasMoreTokens() )
559 (void)tokenizer
.GetNextToken();
561 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
563 if ( !tokenizer
.HasMoreTokens() )
565 // not enough elements in the XLFD - or maybe an alias
569 wxString field
= tokenizer
.GetNextToken();
570 if ( !field
.empty() && field
!= _T('*') )
572 // we're really initialized now
576 fontElements
[n
] = field
;
579 // this should be all
580 if ( tokenizer
.HasMoreTokens() )
586 wxString
wxNativeFontInfo::GetXFontName() const
588 if ( xFontName
.empty() )
590 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
592 // replace the non specified elements with '*' except for the
593 // additional style which is usually just omitted
594 wxString elt
= fontElements
[n
];
595 if ( elt
.empty() && n
!= wxXLFD_ADDSTYLE
)
601 ((wxNativeFontInfo
*)this)->xFontName
<< _T('-') << elt
;
609 wxNativeFontInfo::SetXFontComponent(wxXLFDField field
, const wxString
& value
)
611 wxCHECK_RET( field
< wxXLFD_MAX
, _T("invalid XLFD field") );
613 // this class should be initialized with a valid font spec first and only
614 // then the fields may be modified!
615 wxASSERT_MSG( !IsDefault(), _T("can't modify an uninitialized XLFD") );
617 if ( !HasElements() )
620 if ( !((wxNativeFontInfo
*)this)->FromXFontName(xFontName
) )
622 wxFAIL_MSG( _T("can't set font element for invalid XLFD") );
628 fontElements
[field
] = value
;
630 // invalidate the XFLD, it doesn't correspond to the font elements any more
634 void wxNativeFontInfo::SetXFontName(const wxString
& xFontName_
)
636 // invalidate the font elements, GetXFontComponent() will reparse the XLFD
637 fontElements
[0].clear();
639 xFontName
= xFontName_
;
644 int wxNativeFontInfo::GetPointSize() const
646 const wxString s
= GetXFontComponent(wxXLFD_POINTSIZE
);
648 // return -1 to indicate that the size is unknown
650 return s
.ToLong(&l
) ? l
: -1;
653 wxFontStyle
wxNativeFontInfo::GetStyle() const
655 const wxString s
= GetXFontComponent(wxXLFD_SLANT
);
657 if ( s
.length() != 1 )
659 // it is really unknown but we don't have any way to return it from
661 return wxFONTSTYLE_NORMAL
;
667 // again, unknown but consider normal by default
670 return wxFONTSTYLE_NORMAL
;
673 return wxFONTSTYLE_ITALIC
;
676 return wxFONTSTYLE_SLANT
;
680 wxFontWeight
wxNativeFontInfo::GetWeight() const
682 const wxString s
= GetXFontComponent(wxXLFD_WEIGHT
).MakeLower();
683 if ( s
.find(_T("bold")) != wxString::npos
|| s
== _T("black") )
684 return wxFONTWEIGHT_BOLD
;
685 else if ( s
== _T("light") )
686 return wxFONTWEIGHT_LIGHT
;
688 return wxFONTWEIGHT_NORMAL
;
691 bool wxNativeFontInfo::GetUnderlined() const
693 // X fonts are never underlined
697 wxString
wxNativeFontInfo::GetFaceName() const
699 // wxWidgets facename probably more accurately corresponds to X family
700 return GetXFontComponent(wxXLFD_FAMILY
);
703 wxFontFamily
wxNativeFontInfo::GetFamily() const
705 // and wxWidgets family -- to X foundry, but we have to translate it to
706 // wxFontFamily somehow...
707 wxFAIL_MSG(_T("not implemented")); // GetXFontComponent(wxXLFD_FOUNDRY);
709 return wxFONTFAMILY_DEFAULT
;
712 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
714 // we already have the code for this but need to refactor it first
715 wxFAIL_MSG( _T("not implemented") );
717 return wxFONTENCODING_MAX
;
720 void wxNativeFontInfo::SetPointSize(int pointsize
)
722 SetXFontComponent(wxXLFD_POINTSIZE
, wxString::Format(_T("%d"), pointsize
));
725 void wxNativeFontInfo::SetStyle(wxFontStyle style
)
730 case wxFONTSTYLE_ITALIC
:
734 case wxFONTSTYLE_SLANT
:
738 case wxFONTSTYLE_NORMAL
:
742 wxFAIL_MSG( _T("unknown wxFontStyle in wxNativeFontInfo::SetStyle") );
746 SetXFontComponent(wxXLFD_SLANT
, s
);
749 void wxNativeFontInfo::SetWeight(wxFontWeight weight
)
754 case wxFONTWEIGHT_BOLD
:
758 case wxFONTWEIGHT_LIGHT
:
762 case wxFONTWEIGHT_NORMAL
:
767 wxFAIL_MSG( _T("unknown wxFontWeight in wxNativeFontInfo::SetWeight") );
771 SetXFontComponent(wxXLFD_WEIGHT
, s
);
774 void wxNativeFontInfo::SetUnderlined(bool WXUNUSED(underlined
))
776 // can't do this under X
779 void wxNativeFontInfo::SetFaceName(const wxString
& facename
)
781 SetXFontComponent(wxXLFD_FAMILY
, facename
);
784 void wxNativeFontInfo::SetFamily(wxFontFamily
WXUNUSED(family
))
786 // wxFontFamily -> X foundry, anyone?
787 wxFAIL_MSG( _T("not implemented") );
789 // SetXFontComponent(wxXLFD_FOUNDRY, ...);
792 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding
)
794 wxNativeEncodingInfo info
;
795 if ( wxGetNativeFontEncoding(encoding
, &info
) )
797 SetXFontComponent(wxXLFD_ENCODING
, info
.xencoding
);
798 SetXFontComponent(wxXLFD_REGISTRY
, info
.xregistry
);
802 // ----------------------------------------------------------------------------
804 // ----------------------------------------------------------------------------
806 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
807 wxNativeEncodingInfo
*info
)
809 wxCHECK_MSG( info
, false, _T("bad pointer in wxGetNativeFontEncoding") );
811 if ( encoding
== wxFONTENCODING_DEFAULT
)
813 encoding
= wxFont::GetDefaultEncoding();
818 case wxFONTENCODING_ISO8859_1
:
819 case wxFONTENCODING_ISO8859_2
:
820 case wxFONTENCODING_ISO8859_3
:
821 case wxFONTENCODING_ISO8859_4
:
822 case wxFONTENCODING_ISO8859_5
:
823 case wxFONTENCODING_ISO8859_6
:
824 case wxFONTENCODING_ISO8859_7
:
825 case wxFONTENCODING_ISO8859_8
:
826 case wxFONTENCODING_ISO8859_9
:
827 case wxFONTENCODING_ISO8859_10
:
828 case wxFONTENCODING_ISO8859_11
:
829 case wxFONTENCODING_ISO8859_12
:
830 case wxFONTENCODING_ISO8859_13
:
831 case wxFONTENCODING_ISO8859_14
:
832 case wxFONTENCODING_ISO8859_15
:
834 int cp
= encoding
- wxFONTENCODING_ISO8859_1
+ 1;
835 info
->xregistry
= wxT("iso8859");
836 info
->xencoding
.Printf(wxT("%d"), cp
);
840 case wxFONTENCODING_UTF8
:
841 info
->xregistry
= wxT("iso10646");
842 info
->xencoding
= wxT("*");
845 case wxFONTENCODING_GB2312
:
846 info
->xregistry
= wxT("GB2312"); // or the otherway round?
847 info
->xencoding
= wxT("*");
850 case wxFONTENCODING_KOI8
:
851 case wxFONTENCODING_KOI8_U
:
852 info
->xregistry
= wxT("koi8");
854 // we don't make distinction between koi8-r, koi8-u and koi8-ru (so far)
855 info
->xencoding
= wxT("*");
858 case wxFONTENCODING_CP1250
:
859 case wxFONTENCODING_CP1251
:
860 case wxFONTENCODING_CP1252
:
861 case wxFONTENCODING_CP1253
:
862 case wxFONTENCODING_CP1254
:
863 case wxFONTENCODING_CP1255
:
864 case wxFONTENCODING_CP1256
:
865 case wxFONTENCODING_CP1257
:
867 int cp
= encoding
- wxFONTENCODING_CP1250
+ 1250;
868 info
->xregistry
= wxT("microsoft");
869 info
->xencoding
.Printf(wxT("cp%d"), cp
);
873 case wxFONTENCODING_EUC_JP
:
874 case wxFONTENCODING_SHIFT_JIS
:
875 info
->xregistry
= "jis*";
876 info
->xencoding
= "*";
879 case wxFONTENCODING_SYSTEM
:
881 info
->xencoding
= wxT("*");
885 // don't know how to translate this encoding into X fontspec
889 info
->encoding
= encoding
;
894 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
897 fontspec
.Printf(_T("-*-%s-*-*-*-*-*-*-*-*-*-*-%s-%s"),
898 !info
.facename
? _T("*") : info
.facename
.c_str(),
899 info
.xregistry
.c_str(),
900 info
.xencoding
.c_str());
902 return wxTestFontSpec(fontspec
);
905 // ----------------------------------------------------------------------------
906 // X-specific functions
907 // ----------------------------------------------------------------------------
909 wxNativeFont
wxLoadQueryNearestFont(int pointSize
,
914 const wxString
&facename
,
915 wxFontEncoding encoding
,
918 if ( encoding
== wxFONTENCODING_DEFAULT
)
920 encoding
= wxFont::GetDefaultEncoding();
923 // first determine the encoding - if the font doesn't exist at all in this
924 // encoding, it's useless to do all other approximations (i.e. size,
925 // family &c don't matter much)
926 wxNativeEncodingInfo info
;
927 if ( encoding
== wxFONTENCODING_SYSTEM
)
929 // This will always work so we don't test to save time
930 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
934 if ( !wxGetNativeFontEncoding(encoding
, &info
) ||
935 !wxTestFontEncoding(info
) )
938 if ( !wxFontMapper::Get()->GetAltForEncoding(encoding
, &info
) )
939 #endif // wxUSE_FONTMAP
941 // unspported encoding - replace it with the default
943 // NB: we can't just return 0 from here because wxGTK code doesn't
944 // check for it (i.e. it supposes that we'll always succeed),
945 // so it would provoke a crash
946 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
951 // OK, we have the correct xregistry/xencoding in info structure
952 wxNativeFont font
= 0;
954 // if we already have the X font name, try to use it
955 if( xFontName
&& !xFontName
->empty() )
958 // Make sure point size is correct for scale factor.
960 wxStringTokenizer
tokenizer(*xFontName
, _T("-"), wxTOKEN_RET_DELIMS
);
961 wxString newFontName
;
963 for(int i
= 0; i
< 8; i
++)
964 newFontName
+= tokenizer
.NextToken();
966 (void) tokenizer
.NextToken();
968 newFontName
+= wxString::Format(wxT("%d-"), pointSize
);
970 while(tokenizer
.HasMoreTokens())
971 newFontName
+= tokenizer
.GetNextToken();
973 font
= wxLoadFont(newFontName
);
976 *xFontName
= newFontName
;
981 // search up and down by stepsize 10
982 int max_size
= pointSize
+ 20 * (1 + (pointSize
/180));
983 int min_size
= pointSize
- 20 * (1 + (pointSize
/180));
985 int i
, round
; // counters
987 // first round: search for equal, then for smaller and for larger size with the given weight and style
988 int testweight
= weight
;
989 int teststyle
= style
;
991 for ( round
= 0; round
< 3; round
++ )
993 // second round: use normal weight
996 if ( testweight
!= wxNORMAL
)
998 testweight
= wxNORMAL
;
1002 ++round
; // fall through to third round
1006 // third round: ... and use normal style
1009 if ( teststyle
!= wxNORMAL
)
1011 teststyle
= wxNORMAL
;
1018 // Search for equal or smaller size (approx.)
1019 for ( i
= pointSize
; !font
&& i
>= 10 && i
>= min_size
; i
-= 10 )
1021 font
= wxLoadQueryFont(i
, family
, teststyle
, testweight
, underlined
,
1022 facename
, info
.xregistry
, info
.xencoding
,
1026 // Search for larger size (approx.)
1027 for ( i
= pointSize
+ 10; !font
&& i
<= max_size
; i
+= 10 )
1029 font
= wxLoadQueryFont(i
, family
, teststyle
, testweight
, underlined
,
1030 facename
, info
.xregistry
, info
.xencoding
,
1035 // Try default family
1036 if ( !font
&& family
!= wxDEFAULT
)
1038 font
= wxLoadQueryFont(pointSize
, wxDEFAULT
, style
, weight
,
1039 underlined
, facename
,
1040 info
.xregistry
, info
.xencoding
,
1044 // ignore size, family, style and weight but try to find font with the
1045 // given facename and encoding
1048 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1049 underlined
, facename
,
1050 info
.xregistry
, info
.xencoding
,
1053 // ignore family as well
1056 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1057 underlined
, wxEmptyString
,
1058 info
.xregistry
, info
.xencoding
,
1061 // if it still failed, try to get the font of any size but
1062 // with the requested encoding: this can happen if the
1063 // encoding is only available in one size which happens to be
1064 // different from 120
1067 font
= wxLoadQueryFont(-1, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1068 false, wxEmptyString
,
1069 info
.xregistry
, info
.xencoding
,
1072 // this should never happen as we had tested for it in the
1073 // very beginning, but if it does, do return something non
1074 // NULL or we'd crash in wxFont code
1077 wxFAIL_MSG( _T("this encoding should be available!") );
1079 font
= wxLoadQueryFont(-1,
1080 wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1081 false, wxEmptyString
,
1093 // ----------------------------------------------------------------------------
1094 // private functions
1095 // ----------------------------------------------------------------------------
1097 // returns true if there are any fonts matching this font spec
1098 static bool wxTestFontSpec(const wxString
& fontspec
)
1100 // some X servers will fail to load this font because there are too many
1101 // matches so we must test explicitly for this
1102 if ( fontspec
== _T("-*-*-*-*-*-*-*-*-*-*-*-*-*-*") )
1107 wxNativeFont test
= (wxNativeFont
) g_fontHash
->Get( fontspec
);
1113 test
= wxLoadFont(fontspec
);
1114 g_fontHash
->Put( fontspec
, (wxObject
*) test
);
1128 static wxNativeFont
wxLoadQueryFont(int pointSize
,
1132 bool WXUNUSED(underlined
),
1133 const wxString
& facename
,
1134 const wxString
& xregistry
,
1135 const wxString
& xencoding
,
1136 wxString
* xFontName
)
1141 case wxDECORATIVE
: xfamily
= wxT("lucida"); break;
1142 case wxROMAN
: xfamily
= wxT("times"); break;
1143 case wxMODERN
: xfamily
= wxT("courier"); break;
1144 case wxSWISS
: xfamily
= wxT("helvetica"); break;
1145 case wxTELETYPE
: xfamily
= wxT("lucidatypewriter"); break;
1146 case wxSCRIPT
: xfamily
= wxT("utopia"); break;
1147 default: xfamily
= wxT("*");
1155 xweight
= MWLF_WEIGHT_BOLD
;
1160 xweight
= MWLF_WEIGHT_LIGHT
;
1165 xweight
= MWLF_WEIGHT_NORMAL
;
1171 xweight
= MWLF_WEIGHT_DEFAULT
;
1175 GR_SCREEN_INFO screenInfo
;
1176 GrGetScreenInfo(& screenInfo
);
1178 int yPixelsPerCM
= screenInfo
.ydpcm
;
1180 // A point is 1/72 of an inch.
1181 // An inch is 2.541 cm.
1182 // So pixelHeight = (pointSize / 72) (inches) * 2.541 (for cm) * yPixelsPerCM (for pixels)
1183 // In fact pointSize is 10 * the normal point size so
1186 int pixelHeight
= (int) ( (((float)pointSize
) / 720.0) * 2.541 * (float) yPixelsPerCM
) ;
1188 // An alternative: assume that the screen is 72 dpi.
1189 //int pixelHeight = (int) (((float)pointSize / 720.0) * 72.0) ;
1190 //int pixelHeight = (int) ((float)pointSize / 10.0) ;
1193 logFont
.lfHeight
= pixelHeight
;
1194 logFont
.lfWidth
= 0;
1195 logFont
.lfEscapement
= 0;
1196 logFont
.lfOrientation
= 0;
1197 logFont
.lfWeight
= xweight
;
1198 logFont
.lfItalic
= (style
== wxNORMAL
? 0 : 1) ;
1199 logFont
.lfUnderline
= 0;
1200 logFont
.lfStrikeOut
= 0;
1201 logFont
.lfCharSet
= MWLF_CHARSET_DEFAULT
; // TODO: select appropriate one
1202 logFont
.lfOutPrecision
= MWLF_TYPE_DEFAULT
;
1203 logFont
.lfClipPrecision
= 0; // Not used
1204 logFont
.lfRoman
= (family
== wxROMAN
? 1 : 0) ;
1205 logFont
.lfSerif
= (family
== wxSWISS
? 0 : 1) ;
1206 logFont
.lfSansSerif
= !logFont
.lfSerif
;
1207 logFont
.lfModern
= (family
== wxMODERN
? 1 : 0) ;
1208 logFont
.lfProportional
= (family
== wxTELETYPE
? 0 : 1) ;
1209 logFont
.lfOblique
= 0;
1210 logFont
.lfSmallCaps
= 0;
1211 logFont
.lfPitch
= 0; // 0 = default
1212 strcpy(logFont
.lfFaceName
, facename
.c_str());
1214 XFontStruct
* fontInfo
= (XFontStruct
*) malloc(sizeof(XFontStruct
));
1215 fontInfo
->fid
= GrCreateFont((GR_CHAR
*) facename
.c_str(), pixelHeight
, & logFont
);
1216 GrGetFontInfo(fontInfo
->fid
, & fontInfo
->info
);
1217 return (wxNativeFont
) fontInfo
;
1221 if (!facename
.empty())
1223 fontSpec
.Printf(wxT("-*-%s-*-*-normal-*-*-*-*-*-*-*-*-*"),
1226 if ( wxTestFontSpec(fontSpec
) )
1230 //else: no such family, use default one instead
1237 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1239 if ( wxTestFontSpec(fontSpec
) )
1244 // fall through - try wxITALIC now
1247 fontSpec
.Printf(wxT("-*-%s-*-i-*-*-*-*-*-*-*-*-*-*"),
1249 if ( wxTestFontSpec(fontSpec
) )
1253 else if ( style
== wxITALIC
) // and not wxSLANT
1256 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1258 if ( wxTestFontSpec(fontSpec
) )
1264 // no italic, no slant - leave default
1271 wxFAIL_MSG(_T("unknown font style"));
1272 // fall back to normal
1284 fontSpec
.Printf(wxT("-*-%s-bold-*-*-*-*-*-*-*-*-*-*-*"),
1286 if ( wxTestFontSpec(fontSpec
) )
1288 xweight
= wxT("bold");
1291 fontSpec
.Printf(wxT("-*-%s-heavy-*-*-*-*-*-*-*-*-*-*-*"),
1293 if ( wxTestFontSpec(fontSpec
) )
1295 xweight
= wxT("heavy");
1298 fontSpec
.Printf(wxT("-*-%s-extrabold-*-*-*-*-*-*-*-*-*-*-*"),
1300 if ( wxTestFontSpec(fontSpec
) )
1302 xweight
= wxT("extrabold");
1305 fontSpec
.Printf(wxT("-*-%s-demibold-*-*-*-*-*-*-*-*-*-*-*"),
1307 if ( wxTestFontSpec(fontSpec
) )
1309 xweight
= wxT("demibold");
1312 fontSpec
.Printf(wxT("-*-%s-black-*-*-*-*-*-*-*-*-*-*-*"),
1314 if ( wxTestFontSpec(fontSpec
) )
1316 xweight
= wxT("black");
1319 fontSpec
.Printf(wxT("-*-%s-ultrablack-*-*-*-*-*-*-*-*-*-*-*"),
1321 if ( wxTestFontSpec(fontSpec
) )
1323 xweight
= wxT("ultrablack");
1330 fontSpec
.Printf(wxT("-*-%s-light-*-*-*-*-*-*-*-*-*-*-*"),
1332 if ( wxTestFontSpec(fontSpec
) )
1334 xweight
= wxT("light");
1337 fontSpec
.Printf(wxT("-*-%s-thin-*-*-*-*-*-*-*-*-*-*-*"),
1339 if ( wxTestFontSpec(fontSpec
) )
1341 xweight
= wxT("thin");
1348 fontSpec
.Printf(wxT("-*-%s-medium-*-*-*-*-*-*-*-*-*-*-*"),
1350 if ( wxTestFontSpec(fontSpec
) )
1352 xweight
= wxT("medium");
1355 fontSpec
.Printf(wxT("-*-%s-normal-*-*-*-*-*-*-*-*-*-*-*"),
1357 if ( wxTestFontSpec(fontSpec
) )
1359 xweight
= wxT("normal");
1362 fontSpec
.Printf(wxT("-*-%s-regular-*-*-*-*-*-*-*-*-*-*-*"),
1364 if ( wxTestFontSpec(fontSpec
) )
1366 xweight
= wxT("regular");
1372 default: xweight
= wxT("*"); break;
1375 // if pointSize is -1, don't specify any
1377 if ( pointSize
== -1 )
1383 sizeSpec
.Printf(_T("%d"), pointSize
);
1386 // construct the X font spec from our data
1387 fontSpec
.Printf(wxT("-*-%s-%s-%s-normal-*-*-%s-*-*-*-*-%s-%s"),
1388 xfamily
.c_str(), xweight
.c_str(), xstyle
.c_str(),
1389 sizeSpec
.c_str(), xregistry
.c_str(), xencoding
.c_str());
1392 *xFontName
= fontSpec
;
1394 return wxLoadFont(fontSpec
);
1399 // ----------------------------------------------------------------------------
1401 // ----------------------------------------------------------------------------
1403 class wxFontModule
: public wxModule
1410 DECLARE_DYNAMIC_CLASS(wxFontModule
)
1413 IMPLEMENT_DYNAMIC_CLASS(wxFontModule
, wxModule
)
1415 bool wxFontModule::OnInit()
1417 g_fontHash
= new wxHashTable( wxKEY_STRING
);
1422 void wxFontModule::OnExit()
1426 g_fontHash
= (wxHashTable
*)NULL
;
1429 #endif // GTK 2.0/1.x