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"
31 #include "wx/utils.h" // for wxGetDisplay()
34 #include "wx/fontutil.h"
35 #include "wx/fontmap.h"
36 #include "wx/tokenzr.h"
37 #include "wx/module.h"
41 #include "pango/pango.h"
44 #include "wx/gtk/private.h"
45 extern GtkWidget
*wxGetRootWindow();
47 #include "wx/x11/private.h"
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 void wxNativeFontInfo::Init()
60 wxNativeFontInfo::Init(const wxNativeFontInfo
& info
)
63 description
= pango_font_description_copy(info
.description
);
68 void wxNativeFontInfo::Free()
71 pango_font_description_free(description
);
74 int wxNativeFontInfo::GetPointSize() const
76 return pango_font_description_get_size( description
) / PANGO_SCALE
;
79 wxFontStyle
wxNativeFontInfo::GetStyle() const
81 wxFontStyle m_style
= wxFONTSTYLE_NORMAL
;
83 switch (pango_font_description_get_style( description
))
85 case PANGO_STYLE_NORMAL
:
86 m_style
= wxFONTSTYLE_NORMAL
;
88 case PANGO_STYLE_ITALIC
:
89 m_style
= wxFONTSTYLE_ITALIC
;
91 case PANGO_STYLE_OBLIQUE
:
92 m_style
= wxFONTSTYLE_SLANT
;
99 wxFontWeight
wxNativeFontInfo::GetWeight() const
102 // We seem to currently initialize only by string.
103 // In that case PANGO_FONT_MASK_WEIGHT is always set.
104 if (!(pango_font_description_get_set_fields(description
) & PANGO_FONT_MASK_WEIGHT
))
105 return wxFONTWEIGHT_NORMAL
;
108 PangoWeight pango_weight
= pango_font_description_get_weight( description
);
110 // Until the API can be changed the following ranges of weight values are used:
111 // wxFONTWEIGHT_LIGHT: 100 .. 349 - range of 250
112 // wxFONTWEIGHT_NORMAL: 350 .. 599 - range of 250
113 // wxFONTWEIGHT_BOLD: 600 .. 900 - range of 301 (600 is "semibold" already)
115 if (pango_weight
>= 600)
116 return wxFONTWEIGHT_BOLD
;
118 if (pango_weight
< 350)
119 return wxFONTWEIGHT_LIGHT
;
121 return wxFONTWEIGHT_NORMAL
;
124 bool wxNativeFontInfo::GetUnderlined() const
129 wxString
wxNativeFontInfo::GetFaceName() const
131 wxString tmp
= wxGTK_CONV_BACK( pango_font_description_get_family( description
) );
136 wxFontFamily
wxNativeFontInfo::GetFamily() const
138 wxFontFamily ret
= wxFONTFAMILY_DEFAULT
;
139 // note: not passing -1 as the 2nd parameter to g_ascii_strdown to work
140 // around a bug in the 64-bit glib shipped with solaris 10, -1 causes it
141 // to try to allocate 2^32 bytes.
142 const char *family_name
= pango_font_description_get_family( description
);
143 char *family_text
= g_ascii_strdown( family_name
, family_name
? strlen( family_name
) : 0 );
144 // Check for some common fonts, to salvage what we can from the current win32 centric wxFont API:
145 if (strncmp( family_text
, "monospace", 9 ) == 0)
146 ret
= wxFONTFAMILY_TELETYPE
; // begins with "Monospace"
147 else if (strncmp( family_text
, "courier", 7 ) == 0)
148 ret
= wxFONTFAMILY_TELETYPE
; // begins with "Courier"
149 #if defined(__WXGTK24__) || defined(HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE)
152 if (!gtk_check_version(2,4,0))
155 PangoFontFamily
**families
;
156 PangoFontFamily
*family
= NULL
;
158 pango_context_list_families(
160 gtk_widget_get_pango_context( wxGetRootWindow() ),
162 wxTheApp
->GetPangoContext(),
164 &families
, &n_families
);
166 for (int i
= 0;i
< n_families
;++i
)
168 if (g_ascii_strcasecmp(pango_font_family_get_name( families
[i
] ), pango_font_description_get_family( description
)) == 0 )
170 family
= families
[i
];
177 // Some gtk+ systems might query for a non-existing font from wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT)
178 // on initialization, don't assert until wxSystemSettings::GetFont is checked for this - MR
179 // wxASSERT_MSG( family, wxT("wxNativeFontInfo::GetFamily() - No appropriate PangoFontFamily found for ::description") );
181 //BCI: Cache the wxFontFamily inside the class. Validate cache with
182 //BCI: g_ascii_strcasecmp(pango_font_description_get_family(description), pango_font_family_get_name(family)) == 0
184 if (family
!= NULL
&& pango_font_family_is_monospace( family
))
185 ret
= wxFONTFAMILY_TELETYPE
; // is deemed a monospace font by pango
187 #endif // gtk24 || HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE
189 if (ret
== wxFONTFAMILY_DEFAULT
)
191 if (strstr( family_text
, "sans" ) != NULL
) // checked before serif, so that "* Sans Serif" fonts are detected correctly
192 ret
= wxFONTFAMILY_SWISS
; // contains "Sans"
193 else if (strstr( family_text
, "serif" ) != NULL
)
194 ret
= wxFONTFAMILY_ROMAN
; // contains "Serif"
195 else if (strncmp( family_text
, "times", 5 ) == 0)
196 ret
= wxFONTFAMILY_ROMAN
; // begins with "Times"
197 else if (strncmp( family_text
, "old", 3 ) == 0)
198 ret
= wxFONTFAMILY_DECORATIVE
; // Begins with "Old" - "Old English", "Old Town"
205 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
207 return wxFONTENCODING_SYSTEM
;
211 void wxNativeFontInfo::SetPointSize(int pointsize
)
213 pango_font_description_set_size( description
, pointsize
* PANGO_SCALE
);
216 void wxNativeFontInfo::SetStyle(wxFontStyle style
)
220 case wxFONTSTYLE_ITALIC
:
221 pango_font_description_set_style( description
, PANGO_STYLE_ITALIC
);
223 case wxFONTSTYLE_SLANT
:
224 pango_font_description_set_style( description
, PANGO_STYLE_OBLIQUE
);
227 wxFAIL_MSG( _T("unknown font style") );
229 case wxFONTSTYLE_NORMAL
:
230 pango_font_description_set_style( description
, PANGO_STYLE_NORMAL
);
235 void wxNativeFontInfo::SetWeight(wxFontWeight weight
)
239 case wxFONTWEIGHT_BOLD
:
240 pango_font_description_set_weight(description
, PANGO_WEIGHT_BOLD
);
242 case wxFONTWEIGHT_LIGHT
:
243 pango_font_description_set_weight(description
, PANGO_WEIGHT_LIGHT
);
246 wxFAIL_MSG( _T("unknown font weight") );
248 case wxFONTWEIGHT_NORMAL
:
249 pango_font_description_set_weight(description
, PANGO_WEIGHT_NORMAL
);
253 void wxNativeFontInfo::SetUnderlined(bool WXUNUSED(underlined
))
255 wxFAIL_MSG( _T("not implemented") );
258 void wxNativeFontInfo::SetFaceName(const wxString
& facename
)
260 pango_font_description_set_family(description
, wxGTK_CONV_SYS(facename
));
263 void wxNativeFontInfo::SetFamily(wxFontFamily
WXUNUSED(family
))
265 wxFAIL_MSG( _T("not implemented") );
268 void wxNativeFontInfo::SetEncoding(wxFontEncoding
WXUNUSED(encoding
))
270 wxFAIL_MSG( _T("not implemented") );
275 bool wxNativeFontInfo::FromString(const wxString
& s
)
278 pango_font_description_free( description
);
280 // there is a bug in at least pango <= 1.13 which makes it (or its backends)
281 // segfault for very big point sizes and for negative point sizes.
282 // To workaround that bug for pango <= 1.13
283 // (see http://bugzilla.gnome.org/show_bug.cgi?id=340229)
284 // we do the check on the size here using same (arbitrary) limits used by
285 // pango > 1.13. Note that the segfault could happen also for pointsize
286 // smaller than this limit !!
288 const size_t pos
= str
.find_last_of(_T(" "));
290 if ( pos
!= wxString::npos
&& wxString(str
, pos
+ 1).ToDouble(&size
) )
298 if ( !sizeStr
.empty() )
300 // replace the old size with the adjusted one
301 str
= wxString(s
, 0, pos
) + sizeStr
;
305 description
= pango_font_description_from_string( wxGTK_CONV_SYS( str
) );
310 wxString
wxNativeFontInfo::ToString() const
312 char *str
= pango_font_description_to_string( description
);
313 wxString tmp
= wxGTK_CONV_BACK( str
);
319 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
321 return FromString( s
);
324 wxString
wxNativeFontInfo::ToUserString() const
329 // ----------------------------------------------------------------------------
330 // wxNativeEncodingInfo
331 // ----------------------------------------------------------------------------
333 bool wxNativeEncodingInfo::FromString(const wxString
& WXUNUSED(s
))
338 wxString
wxNativeEncodingInfo::ToString() const
340 return wxEmptyString
;
343 bool wxTestFontEncoding(const wxNativeEncodingInfo
& WXUNUSED(info
))
348 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
349 wxNativeEncodingInfo
*info
)
351 // all encodings are available in GTK+ 2 because we translate text in any
352 // encoding to UTF-8 internally anyhow
353 info
->facename
.clear();
354 info
->encoding
= encoding
;
363 #pragma message disable nosimpint
366 #include <X11/Xlib.h>
369 #pragma message enable nosimpint
372 #elif defined(__WXGTK__)
373 // we have to declare struct tm to avoid problems with first forward
374 // declaring it in C code (glib.h included from gdk.h does it) and then
375 // defining it when time.h is included from the headers below - this is
376 // known not to work at least with Sun CC 6.01
383 // ----------------------------------------------------------------------------
385 // ----------------------------------------------------------------------------
387 static wxHashTable
*g_fontHash
= (wxHashTable
*) NULL
;
389 // ----------------------------------------------------------------------------
391 // ----------------------------------------------------------------------------
393 // define the functions to create and destroy native fonts for this toolkit
395 wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
397 return XLoadQueryFont((Display
*)wxGetDisplay(), fontSpec
);
400 inline void wxFreeFont(wxNativeFont font
)
402 XFreeFont((Display
*)wxGetDisplay(), (XFontStruct
*)font
);
404 #elif defined(__WXGTK__)
405 wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
407 // VZ: we should use gdk_fontset_load() instead of gdk_font_load()
408 // here to be able to display Japanese fonts correctly (at least
409 // this is what people report) but unfortunately doing it results
410 // in tons of warnings when using GTK with "normal" European
411 // languages and so we can't always do it and I don't know enough
412 // to determine when should this be done... (FIXME)
413 return gdk_font_load( wxConvertWX2MB(fontSpec
) );
416 inline void wxFreeFont(wxNativeFont font
)
418 gdk_font_unref(font
);
421 #error "Unknown GUI toolkit"
424 static bool wxTestFontSpec(const wxString
& fontspec
);
426 static wxNativeFont
wxLoadQueryFont(int pointSize
,
431 const wxString
& facename
,
432 const wxString
& xregistry
,
433 const wxString
& xencoding
,
434 wxString
* xFontName
);
436 // ============================================================================
438 // ============================================================================
440 // ----------------------------------------------------------------------------
441 // wxNativeEncodingInfo
442 // ----------------------------------------------------------------------------
444 // convert to/from the string representation: format is
445 // encodingid;registry;encoding[;facename]
446 bool wxNativeEncodingInfo::FromString(const wxString
& s
)
448 // use ";", not "-" because it may be part of encoding name
449 wxStringTokenizer
tokenizer(s
, _T(";"));
451 wxString encid
= tokenizer
.GetNextToken();
453 if ( !encid
.ToLong(&enc
) )
455 encoding
= (wxFontEncoding
)enc
;
457 xregistry
= tokenizer
.GetNextToken();
461 xencoding
= tokenizer
.GetNextToken();
466 facename
= tokenizer
.GetNextToken();
471 wxString
wxNativeEncodingInfo::ToString() const
474 s
<< (long)encoding
<< _T(';') << xregistry
<< _T(';') << xencoding
;
475 if ( !facename
.empty() )
477 s
<< _T(';') << facename
;
483 // ----------------------------------------------------------------------------
485 // ----------------------------------------------------------------------------
487 void wxNativeFontInfo::Init()
492 bool wxNativeFontInfo::FromString(const wxString
& s
)
494 wxStringTokenizer
tokenizer(s
, _T(";"));
497 wxString token
= tokenizer
.GetNextToken();
498 if ( token
!= _T('0') )
501 xFontName
= tokenizer
.GetNextToken();
503 // this should be the end
504 if ( tokenizer
.HasMoreTokens() )
507 return FromXFontName(xFontName
);
510 wxString
wxNativeFontInfo::ToString() const
513 return wxString::Format(_T("%d;%s"), 0, GetXFontName().c_str());
516 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
518 return FromXFontName(s
);
521 wxString
wxNativeFontInfo::ToUserString() const
523 return GetXFontName();
526 bool wxNativeFontInfo::HasElements() const
528 // we suppose that the foundry is never empty, so if it is it means that we
529 // had never parsed the XLFD
530 return !fontElements
[0].empty();
533 wxString
wxNativeFontInfo::GetXFontComponent(wxXLFDField field
) const
535 wxCHECK_MSG( field
< wxXLFD_MAX
, wxEmptyString
, _T("invalid XLFD field") );
537 if ( !HasElements() )
540 if ( !((wxNativeFontInfo
*)this)->FromXFontName(xFontName
) )
541 return wxEmptyString
;
544 return fontElements
[field
];
547 bool wxNativeFontInfo::FromXFontName(const wxString
& fontname
)
549 // TODO: we should be able to handle the font aliases here, but how?
550 wxStringTokenizer
tokenizer(fontname
, _T("-"));
552 // skip the leading, usually empty field (font name registry)
553 if ( !tokenizer
.HasMoreTokens() )
556 (void)tokenizer
.GetNextToken();
558 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
560 if ( !tokenizer
.HasMoreTokens() )
562 // not enough elements in the XLFD - or maybe an alias
566 wxString field
= tokenizer
.GetNextToken();
567 if ( !field
.empty() && field
!= _T('*') )
569 // we're really initialized now
573 fontElements
[n
] = field
;
576 // this should be all
577 if ( tokenizer
.HasMoreTokens() )
583 wxString
wxNativeFontInfo::GetXFontName() const
585 if ( xFontName
.empty() )
587 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
589 // replace the non specified elements with '*' except for the
590 // additional style which is usually just omitted
591 wxString elt
= fontElements
[n
];
592 if ( elt
.empty() && n
!= wxXLFD_ADDSTYLE
)
598 ((wxNativeFontInfo
*)this)->xFontName
<< _T('-') << elt
;
606 wxNativeFontInfo::SetXFontComponent(wxXLFDField field
, const wxString
& value
)
608 wxCHECK_RET( field
< wxXLFD_MAX
, _T("invalid XLFD field") );
610 // this class should be initialized with a valid font spec first and only
611 // then the fields may be modified!
612 wxASSERT_MSG( !IsDefault(), _T("can't modify an uninitialized XLFD") );
614 if ( !HasElements() )
617 if ( !((wxNativeFontInfo
*)this)->FromXFontName(xFontName
) )
619 wxFAIL_MSG( _T("can't set font element for invalid XLFD") );
625 fontElements
[field
] = value
;
627 // invalidate the XFLD, it doesn't correspond to the font elements any more
631 void wxNativeFontInfo::SetXFontName(const wxString
& xFontName_
)
633 // invalidate the font elements, GetXFontComponent() will reparse the XLFD
634 fontElements
[0].clear();
636 xFontName
= xFontName_
;
641 int wxNativeFontInfo::GetPointSize() const
643 const wxString s
= GetXFontComponent(wxXLFD_POINTSIZE
);
645 // return -1 to indicate that the size is unknown
647 return s
.ToLong(&l
) ? l
: -1;
650 wxFontStyle
wxNativeFontInfo::GetStyle() const
652 const wxString s
= GetXFontComponent(wxXLFD_SLANT
);
654 if ( s
.length() != 1 )
656 // it is really unknown but we don't have any way to return it from
658 return wxFONTSTYLE_NORMAL
;
664 // again, unknown but consider normal by default
667 return wxFONTSTYLE_NORMAL
;
670 return wxFONTSTYLE_ITALIC
;
673 return wxFONTSTYLE_SLANT
;
677 wxFontWeight
wxNativeFontInfo::GetWeight() const
679 const wxString s
= GetXFontComponent(wxXLFD_WEIGHT
).MakeLower();
680 if ( s
.find(_T("bold")) != wxString::npos
|| s
== _T("black") )
681 return wxFONTWEIGHT_BOLD
;
682 else if ( s
== _T("light") )
683 return wxFONTWEIGHT_LIGHT
;
685 return wxFONTWEIGHT_NORMAL
;
688 bool wxNativeFontInfo::GetUnderlined() const
690 // X fonts are never underlined
694 wxString
wxNativeFontInfo::GetFaceName() const
696 // wxWidgets facename probably more accurately corresponds to X family
697 return GetXFontComponent(wxXLFD_FAMILY
);
700 wxFontFamily
wxNativeFontInfo::GetFamily() const
702 // and wxWidgets family -- to X foundry, but we have to translate it to
703 // wxFontFamily somehow...
704 wxFAIL_MSG(_T("not implemented")); // GetXFontComponent(wxXLFD_FOUNDRY);
706 return wxFONTFAMILY_DEFAULT
;
709 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
711 // we already have the code for this but need to refactor it first
712 wxFAIL_MSG( _T("not implemented") );
714 return wxFONTENCODING_MAX
;
717 void wxNativeFontInfo::SetPointSize(int pointsize
)
719 SetXFontComponent(wxXLFD_POINTSIZE
, wxString::Format(_T("%d"), pointsize
));
722 void wxNativeFontInfo::SetStyle(wxFontStyle style
)
727 case wxFONTSTYLE_ITALIC
:
731 case wxFONTSTYLE_SLANT
:
735 case wxFONTSTYLE_NORMAL
:
739 wxFAIL_MSG( _T("unknown wxFontStyle in wxNativeFontInfo::SetStyle") );
743 SetXFontComponent(wxXLFD_SLANT
, s
);
746 void wxNativeFontInfo::SetWeight(wxFontWeight weight
)
751 case wxFONTWEIGHT_BOLD
:
755 case wxFONTWEIGHT_LIGHT
:
759 case wxFONTWEIGHT_NORMAL
:
764 wxFAIL_MSG( _T("unknown wxFontWeight in wxNativeFontInfo::SetWeight") );
768 SetXFontComponent(wxXLFD_WEIGHT
, s
);
771 void wxNativeFontInfo::SetUnderlined(bool WXUNUSED(underlined
))
773 // can't do this under X
776 void wxNativeFontInfo::SetFaceName(const wxString
& facename
)
778 SetXFontComponent(wxXLFD_FAMILY
, facename
);
781 void wxNativeFontInfo::SetFamily(wxFontFamily
WXUNUSED(family
))
783 // wxFontFamily -> X foundry, anyone?
784 wxFAIL_MSG( _T("not implemented") );
786 // SetXFontComponent(wxXLFD_FOUNDRY, ...);
789 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding
)
791 wxNativeEncodingInfo info
;
792 if ( wxGetNativeFontEncoding(encoding
, &info
) )
794 SetXFontComponent(wxXLFD_ENCODING
, info
.xencoding
);
795 SetXFontComponent(wxXLFD_REGISTRY
, info
.xregistry
);
799 // ----------------------------------------------------------------------------
801 // ----------------------------------------------------------------------------
803 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
804 wxNativeEncodingInfo
*info
)
806 wxCHECK_MSG( info
, false, _T("bad pointer in wxGetNativeFontEncoding") );
808 if ( encoding
== wxFONTENCODING_DEFAULT
)
810 encoding
= wxFont::GetDefaultEncoding();
815 case wxFONTENCODING_ISO8859_1
:
816 case wxFONTENCODING_ISO8859_2
:
817 case wxFONTENCODING_ISO8859_3
:
818 case wxFONTENCODING_ISO8859_4
:
819 case wxFONTENCODING_ISO8859_5
:
820 case wxFONTENCODING_ISO8859_6
:
821 case wxFONTENCODING_ISO8859_7
:
822 case wxFONTENCODING_ISO8859_8
:
823 case wxFONTENCODING_ISO8859_9
:
824 case wxFONTENCODING_ISO8859_10
:
825 case wxFONTENCODING_ISO8859_11
:
826 case wxFONTENCODING_ISO8859_12
:
827 case wxFONTENCODING_ISO8859_13
:
828 case wxFONTENCODING_ISO8859_14
:
829 case wxFONTENCODING_ISO8859_15
:
831 int cp
= encoding
- wxFONTENCODING_ISO8859_1
+ 1;
832 info
->xregistry
= wxT("iso8859");
833 info
->xencoding
.Printf(wxT("%d"), cp
);
837 case wxFONTENCODING_UTF8
:
838 info
->xregistry
= wxT("iso10646");
839 info
->xencoding
= wxT("*");
842 case wxFONTENCODING_GB2312
:
843 info
->xregistry
= wxT("GB2312"); // or the otherway round?
844 info
->xencoding
= wxT("*");
847 case wxFONTENCODING_KOI8
:
848 case wxFONTENCODING_KOI8_U
:
849 info
->xregistry
= wxT("koi8");
851 // we don't make distinction between koi8-r, koi8-u and koi8-ru (so far)
852 info
->xencoding
= wxT("*");
855 case wxFONTENCODING_CP1250
:
856 case wxFONTENCODING_CP1251
:
857 case wxFONTENCODING_CP1252
:
858 case wxFONTENCODING_CP1253
:
859 case wxFONTENCODING_CP1254
:
860 case wxFONTENCODING_CP1255
:
861 case wxFONTENCODING_CP1256
:
862 case wxFONTENCODING_CP1257
:
864 int cp
= encoding
- wxFONTENCODING_CP1250
+ 1250;
865 info
->xregistry
= wxT("microsoft");
866 info
->xencoding
.Printf(wxT("cp%d"), cp
);
870 case wxFONTENCODING_EUC_JP
:
871 case wxFONTENCODING_SHIFT_JIS
:
872 info
->xregistry
= "jis*";
873 info
->xencoding
= "*";
876 case wxFONTENCODING_SYSTEM
:
878 info
->xencoding
= wxT("*");
882 // don't know how to translate this encoding into X fontspec
886 info
->encoding
= encoding
;
891 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
894 fontspec
.Printf(_T("-*-%s-*-*-*-*-*-*-*-*-*-*-%s-%s"),
895 !info
.facename
? _T("*") : info
.facename
.c_str(),
896 info
.xregistry
.c_str(),
897 info
.xencoding
.c_str());
899 return wxTestFontSpec(fontspec
);
902 // ----------------------------------------------------------------------------
903 // X-specific functions
904 // ----------------------------------------------------------------------------
906 wxNativeFont
wxLoadQueryNearestFont(int pointSize
,
911 const wxString
&facename
,
912 wxFontEncoding encoding
,
915 if ( encoding
== wxFONTENCODING_DEFAULT
)
917 encoding
= wxFont::GetDefaultEncoding();
920 // first determine the encoding - if the font doesn't exist at all in this
921 // encoding, it's useless to do all other approximations (i.e. size,
922 // family &c don't matter much)
923 wxNativeEncodingInfo info
;
924 if ( encoding
== wxFONTENCODING_SYSTEM
)
926 // This will always work so we don't test to save time
927 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
931 if ( !wxGetNativeFontEncoding(encoding
, &info
) ||
932 !wxTestFontEncoding(info
) )
935 if ( !wxFontMapper::Get()->GetAltForEncoding(encoding
, &info
) )
936 #endif // wxUSE_FONTMAP
938 // unspported encoding - replace it with the default
940 // NB: we can't just return 0 from here because wxGTK code doesn't
941 // check for it (i.e. it supposes that we'll always succeed),
942 // so it would provoke a crash
943 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
948 // OK, we have the correct xregistry/xencoding in info structure
949 wxNativeFont font
= 0;
951 // if we already have the X font name, try to use it
952 if( xFontName
&& !xFontName
->empty() )
955 // Make sure point size is correct for scale factor.
957 wxStringTokenizer
tokenizer(*xFontName
, _T("-"), wxTOKEN_RET_DELIMS
);
958 wxString newFontName
;
960 for(int i
= 0; i
< 8; i
++)
961 newFontName
+= tokenizer
.NextToken();
963 (void) tokenizer
.NextToken();
965 newFontName
+= wxString::Format(wxT("%d-"), pointSize
);
967 while(tokenizer
.HasMoreTokens())
968 newFontName
+= tokenizer
.GetNextToken();
970 font
= wxLoadFont(newFontName
);
973 *xFontName
= newFontName
;
978 // search up and down by stepsize 10
979 int max_size
= pointSize
+ 20 * (1 + (pointSize
/180));
980 int min_size
= pointSize
- 20 * (1 + (pointSize
/180));
982 int i
, round
; // counters
984 // first round: search for equal, then for smaller and for larger size with the given weight and style
985 int testweight
= weight
;
986 int teststyle
= style
;
988 for ( round
= 0; round
< 3; round
++ )
990 // second round: use normal weight
993 if ( testweight
!= wxNORMAL
)
995 testweight
= wxNORMAL
;
999 ++round
; // fall through to third round
1003 // third round: ... and use normal style
1006 if ( teststyle
!= wxNORMAL
)
1008 teststyle
= wxNORMAL
;
1015 // Search for equal or smaller size (approx.)
1016 for ( i
= pointSize
; !font
&& i
>= 10 && i
>= min_size
; i
-= 10 )
1018 font
= wxLoadQueryFont(i
, family
, teststyle
, testweight
, underlined
,
1019 facename
, info
.xregistry
, info
.xencoding
,
1023 // Search for larger size (approx.)
1024 for ( i
= pointSize
+ 10; !font
&& i
<= max_size
; i
+= 10 )
1026 font
= wxLoadQueryFont(i
, family
, teststyle
, testweight
, underlined
,
1027 facename
, info
.xregistry
, info
.xencoding
,
1032 // Try default family
1033 if ( !font
&& family
!= wxDEFAULT
)
1035 font
= wxLoadQueryFont(pointSize
, wxDEFAULT
, style
, weight
,
1036 underlined
, facename
,
1037 info
.xregistry
, info
.xencoding
,
1041 // ignore size, family, style and weight but try to find font with the
1042 // given facename and encoding
1045 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1046 underlined
, facename
,
1047 info
.xregistry
, info
.xencoding
,
1050 // ignore family as well
1053 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1054 underlined
, wxEmptyString
,
1055 info
.xregistry
, info
.xencoding
,
1058 // if it still failed, try to get the font of any size but
1059 // with the requested encoding: this can happen if the
1060 // encoding is only available in one size which happens to be
1061 // different from 120
1064 font
= wxLoadQueryFont(-1, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1065 false, wxEmptyString
,
1066 info
.xregistry
, info
.xencoding
,
1069 // this should never happen as we had tested for it in the
1070 // very beginning, but if it does, do return something non
1071 // NULL or we'd crash in wxFont code
1074 wxFAIL_MSG( _T("this encoding should be available!") );
1076 font
= wxLoadQueryFont(-1,
1077 wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1078 false, wxEmptyString
,
1090 // ----------------------------------------------------------------------------
1091 // private functions
1092 // ----------------------------------------------------------------------------
1094 // returns true if there are any fonts matching this font spec
1095 static bool wxTestFontSpec(const wxString
& fontspec
)
1097 // some X servers will fail to load this font because there are too many
1098 // matches so we must test explicitly for this
1099 if ( fontspec
== _T("-*-*-*-*-*-*-*-*-*-*-*-*-*-*") )
1104 wxNativeFont test
= (wxNativeFont
) g_fontHash
->Get( fontspec
);
1110 test
= wxLoadFont(fontspec
);
1111 g_fontHash
->Put( fontspec
, (wxObject
*) test
);
1125 static wxNativeFont
wxLoadQueryFont(int pointSize
,
1129 bool WXUNUSED(underlined
),
1130 const wxString
& facename
,
1131 const wxString
& xregistry
,
1132 const wxString
& xencoding
,
1133 wxString
* xFontName
)
1138 case wxDECORATIVE
: xfamily
= wxT("lucida"); break;
1139 case wxROMAN
: xfamily
= wxT("times"); break;
1140 case wxMODERN
: xfamily
= wxT("courier"); break;
1141 case wxSWISS
: xfamily
= wxT("helvetica"); break;
1142 case wxTELETYPE
: xfamily
= wxT("lucidatypewriter"); break;
1143 case wxSCRIPT
: xfamily
= wxT("utopia"); break;
1144 default: xfamily
= wxT("*");
1152 xweight
= MWLF_WEIGHT_BOLD
;
1157 xweight
= MWLF_WEIGHT_LIGHT
;
1162 xweight
= MWLF_WEIGHT_NORMAL
;
1168 xweight
= MWLF_WEIGHT_DEFAULT
;
1172 GR_SCREEN_INFO screenInfo
;
1173 GrGetScreenInfo(& screenInfo
);
1175 int yPixelsPerCM
= screenInfo
.ydpcm
;
1177 // A point is 1/72 of an inch.
1178 // An inch is 2.541 cm.
1179 // So pixelHeight = (pointSize / 72) (inches) * 2.541 (for cm) * yPixelsPerCM (for pixels)
1180 // In fact pointSize is 10 * the normal point size so
1183 int pixelHeight
= (int) ( (((float)pointSize
) / 720.0) * 2.541 * (float) yPixelsPerCM
) ;
1185 // An alternative: assume that the screen is 72 dpi.
1186 //int pixelHeight = (int) (((float)pointSize / 720.0) * 72.0) ;
1187 //int pixelHeight = (int) ((float)pointSize / 10.0) ;
1190 logFont
.lfHeight
= pixelHeight
;
1191 logFont
.lfWidth
= 0;
1192 logFont
.lfEscapement
= 0;
1193 logFont
.lfOrientation
= 0;
1194 logFont
.lfWeight
= xweight
;
1195 logFont
.lfItalic
= (style
== wxNORMAL
? 0 : 1) ;
1196 logFont
.lfUnderline
= 0;
1197 logFont
.lfStrikeOut
= 0;
1198 logFont
.lfCharSet
= MWLF_CHARSET_DEFAULT
; // TODO: select appropriate one
1199 logFont
.lfOutPrecision
= MWLF_TYPE_DEFAULT
;
1200 logFont
.lfClipPrecision
= 0; // Not used
1201 logFont
.lfRoman
= (family
== wxROMAN
? 1 : 0) ;
1202 logFont
.lfSerif
= (family
== wxSWISS
? 0 : 1) ;
1203 logFont
.lfSansSerif
= !logFont
.lfSerif
;
1204 logFont
.lfModern
= (family
== wxMODERN
? 1 : 0) ;
1205 logFont
.lfProportional
= (family
== wxTELETYPE
? 0 : 1) ;
1206 logFont
.lfOblique
= 0;
1207 logFont
.lfSmallCaps
= 0;
1208 logFont
.lfPitch
= 0; // 0 = default
1209 strcpy(logFont
.lfFaceName
, facename
.c_str());
1211 XFontStruct
* fontInfo
= (XFontStruct
*) malloc(sizeof(XFontStruct
));
1212 fontInfo
->fid
= GrCreateFont((GR_CHAR
*) facename
.c_str(), pixelHeight
, & logFont
);
1213 GrGetFontInfo(fontInfo
->fid
, & fontInfo
->info
);
1214 return (wxNativeFont
) fontInfo
;
1218 if (!facename
.empty())
1220 fontSpec
.Printf(wxT("-*-%s-*-*-normal-*-*-*-*-*-*-*-*-*"),
1223 if ( wxTestFontSpec(fontSpec
) )
1227 //else: no such family, use default one instead
1234 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1236 if ( wxTestFontSpec(fontSpec
) )
1241 // fall through - try wxITALIC now
1244 fontSpec
.Printf(wxT("-*-%s-*-i-*-*-*-*-*-*-*-*-*-*"),
1246 if ( wxTestFontSpec(fontSpec
) )
1250 else if ( style
== wxITALIC
) // and not wxSLANT
1253 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1255 if ( wxTestFontSpec(fontSpec
) )
1261 // no italic, no slant - leave default
1268 wxFAIL_MSG(_T("unknown font style"));
1269 // fall back to normal
1281 fontSpec
.Printf(wxT("-*-%s-bold-*-*-*-*-*-*-*-*-*-*-*"),
1283 if ( wxTestFontSpec(fontSpec
) )
1285 xweight
= wxT("bold");
1288 fontSpec
.Printf(wxT("-*-%s-heavy-*-*-*-*-*-*-*-*-*-*-*"),
1290 if ( wxTestFontSpec(fontSpec
) )
1292 xweight
= wxT("heavy");
1295 fontSpec
.Printf(wxT("-*-%s-extrabold-*-*-*-*-*-*-*-*-*-*-*"),
1297 if ( wxTestFontSpec(fontSpec
) )
1299 xweight
= wxT("extrabold");
1302 fontSpec
.Printf(wxT("-*-%s-demibold-*-*-*-*-*-*-*-*-*-*-*"),
1304 if ( wxTestFontSpec(fontSpec
) )
1306 xweight
= wxT("demibold");
1309 fontSpec
.Printf(wxT("-*-%s-black-*-*-*-*-*-*-*-*-*-*-*"),
1311 if ( wxTestFontSpec(fontSpec
) )
1313 xweight
= wxT("black");
1316 fontSpec
.Printf(wxT("-*-%s-ultrablack-*-*-*-*-*-*-*-*-*-*-*"),
1318 if ( wxTestFontSpec(fontSpec
) )
1320 xweight
= wxT("ultrablack");
1327 fontSpec
.Printf(wxT("-*-%s-light-*-*-*-*-*-*-*-*-*-*-*"),
1329 if ( wxTestFontSpec(fontSpec
) )
1331 xweight
= wxT("light");
1334 fontSpec
.Printf(wxT("-*-%s-thin-*-*-*-*-*-*-*-*-*-*-*"),
1336 if ( wxTestFontSpec(fontSpec
) )
1338 xweight
= wxT("thin");
1345 fontSpec
.Printf(wxT("-*-%s-medium-*-*-*-*-*-*-*-*-*-*-*"),
1347 if ( wxTestFontSpec(fontSpec
) )
1349 xweight
= wxT("medium");
1352 fontSpec
.Printf(wxT("-*-%s-normal-*-*-*-*-*-*-*-*-*-*-*"),
1354 if ( wxTestFontSpec(fontSpec
) )
1356 xweight
= wxT("normal");
1359 fontSpec
.Printf(wxT("-*-%s-regular-*-*-*-*-*-*-*-*-*-*-*"),
1361 if ( wxTestFontSpec(fontSpec
) )
1363 xweight
= wxT("regular");
1369 default: xweight
= wxT("*"); break;
1372 // if pointSize is -1, don't specify any
1374 if ( pointSize
== -1 )
1380 sizeSpec
.Printf(_T("%d"), pointSize
);
1383 // construct the X font spec from our data
1384 fontSpec
.Printf(wxT("-*-%s-%s-%s-normal-*-*-%s-*-*-*-*-%s-%s"),
1385 xfamily
.c_str(), xweight
.c_str(), xstyle
.c_str(),
1386 sizeSpec
.c_str(), xregistry
.c_str(), xencoding
.c_str());
1389 *xFontName
= fontSpec
;
1391 return wxLoadFont(fontSpec
);
1396 // ----------------------------------------------------------------------------
1398 // ----------------------------------------------------------------------------
1400 class wxFontModule
: public wxModule
1407 DECLARE_DYNAMIC_CLASS(wxFontModule
)
1410 IMPLEMENT_DYNAMIC_CLASS(wxFontModule
, wxModule
)
1412 bool wxFontModule::OnInit()
1414 g_fontHash
= new wxHashTable( wxKEY_STRING
);
1419 void wxFontModule::OnExit()
1423 g_fontHash
= (wxHashTable
*)NULL
;
1426 #endif // GTK 2.0/1.x