1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/unix/fontutil.cpp
3 // Purpose: Font helper functions for wxX11, wxGTK, wxMotif
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()
35 #include "wx/module.h"
38 #include "wx/fontmap.h"
39 #include "wx/tokenzr.h"
40 #include "wx/fontenum.h"
44 #include "pango/pango.h"
47 #include "wx/gtk/private.h"
48 extern GtkWidget
*wxGetRootWindow();
50 #define wxPANGO_CONV wxGTK_CONV_SYS
51 #define wxPANGO_CONV_BACK wxGTK_CONV_BACK_SYS
53 #include "wx/x11/private.h"
54 #include "wx/gtk/private/string.h"
56 #define wxPANGO_CONV(s) (wxConvUTF8.cWX2MB((s)))
57 #define wxPANGO_CONV_BACK(s) (wxConvUTF8.cMB2WX((s)))
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
64 void wxNativeFontInfo::Init()
69 void wxNativeFontInfo::Init(const wxNativeFontInfo
& info
)
72 description
= pango_font_description_copy(info
.description
);
77 void wxNativeFontInfo::Free()
80 pango_font_description_free(description
);
83 int wxNativeFontInfo::GetPointSize() const
85 return pango_font_description_get_size( description
) / PANGO_SCALE
;
88 wxFontStyle
wxNativeFontInfo::GetStyle() const
90 wxFontStyle m_style
= wxFONTSTYLE_NORMAL
;
92 switch (pango_font_description_get_style( description
))
94 case PANGO_STYLE_NORMAL
:
95 m_style
= wxFONTSTYLE_NORMAL
;
97 case PANGO_STYLE_ITALIC
:
98 m_style
= wxFONTSTYLE_ITALIC
;
100 case PANGO_STYLE_OBLIQUE
:
101 m_style
= wxFONTSTYLE_SLANT
;
108 wxFontWeight
wxNativeFontInfo::GetWeight() const
111 // We seem to currently initialize only by string.
112 // In that case PANGO_FONT_MASK_WEIGHT is always set.
113 if (!(pango_font_description_get_set_fields(description
) & PANGO_FONT_MASK_WEIGHT
))
114 return wxFONTWEIGHT_NORMAL
;
117 PangoWeight pango_weight
= pango_font_description_get_weight( description
);
119 // Until the API can be changed the following ranges of weight values are used:
120 // wxFONTWEIGHT_LIGHT: 100 .. 349 - range of 250
121 // wxFONTWEIGHT_NORMAL: 350 .. 599 - range of 250
122 // wxFONTWEIGHT_BOLD: 600 .. 900 - range of 301 (600 is "semibold" already)
124 if (pango_weight
>= 600)
125 return wxFONTWEIGHT_BOLD
;
127 if (pango_weight
< 350)
128 return wxFONTWEIGHT_LIGHT
;
130 return wxFONTWEIGHT_NORMAL
;
133 bool wxNativeFontInfo::GetUnderlined() const
138 wxString
wxNativeFontInfo::GetFaceName() const
140 // the Pango "family" is the wx "face name"
141 return wxPANGO_CONV_BACK(pango_font_description_get_family(description
));
144 wxFontFamily
wxNativeFontInfo::GetFamily() const
146 wxFontFamily ret
= wxFONTFAMILY_DEFAULT
;
148 const char *family_name
= pango_font_description_get_family( description
);
150 // note: not passing -1 as the 2nd parameter to g_ascii_strdown to work
151 // around a bug in the 64-bit glib shipped with solaris 10, -1 causes it
152 // to try to allocate 2^32 bytes.
155 wxGtkString
family_text(g_ascii_strdown(family_name
, strlen(family_name
)));
157 // Check for some common fonts, to salvage what we can from the current win32 centric wxFont API:
158 if (strncmp( family_text
, "monospace", 9 ) == 0)
159 ret
= wxFONTFAMILY_TELETYPE
; // begins with "Monospace"
160 else if (strncmp( family_text
, "courier", 7 ) == 0)
161 ret
= wxFONTFAMILY_TELETYPE
; // begins with "Courier"
162 #if defined(__WXGTK20__) || defined(HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE)
165 if (!gtk_check_version(2,4,0))
168 PangoFontFamily
**families
;
169 PangoFontFamily
*family
= NULL
;
171 pango_context_list_families(
173 gtk_widget_get_pango_context( wxGetRootWindow() ),
175 wxTheApp
->GetPangoContext(),
177 &families
, &n_families
);
179 for (int i
= 0; i
< n_families
; ++i
)
181 if (g_ascii_strcasecmp(pango_font_family_get_name( families
[i
] ),
182 pango_font_description_get_family( description
)) == 0 )
184 family
= families
[i
];
191 // Some gtk+ systems might query for a non-existing font from
192 // wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT) on initialization,
193 // don't assert until wxSystemSettings::GetFont is checked for this - MR
194 // wxASSERT_MSG( family,
195 // "wxNativeFontInfo::GetFamily() - No appropriate PangoFontFamily found for ::description" );
197 //BCI: Cache the wxFontFamily inside the class. Validate cache with
198 //BCI: g_ascii_strcasecmp(pango_font_description_get_family(description),
199 // pango_font_family_get_name(family)) == 0
201 if (family
!= NULL
&& pango_font_family_is_monospace( family
))
202 ret
= wxFONTFAMILY_TELETYPE
; // is deemed a monospace font by pango
204 #endif // GTK+ 2 || HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE
206 if (ret
== wxFONTFAMILY_DEFAULT
)
208 if (strstr( family_text
, "sans" ) != NULL
)
209 // checked before serif, so that "* Sans Serif" fonts are detected correctly
210 ret
= wxFONTFAMILY_SWISS
; // contains "Sans"
211 else if (strstr( family_text
, "serif" ) != NULL
)
212 ret
= wxFONTFAMILY_ROMAN
; // contains "Serif"
213 else if (strncmp( family_text
, "times", 5 ) == 0)
214 ret
= wxFONTFAMILY_ROMAN
; // begins with "Times"
215 else if (strncmp( family_text
, "old", 3 ) == 0)
216 ret
= wxFONTFAMILY_DECORATIVE
; // begins with "Old" - "Old English", "Old Town"
222 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
224 return wxFONTENCODING_SYSTEM
;
227 void wxNativeFontInfo::SetPointSize(int pointsize
)
229 pango_font_description_set_size( description
, pointsize
* PANGO_SCALE
);
232 void wxNativeFontInfo::SetStyle(wxFontStyle style
)
236 case wxFONTSTYLE_ITALIC
:
237 pango_font_description_set_style( description
, PANGO_STYLE_ITALIC
);
239 case wxFONTSTYLE_SLANT
:
240 pango_font_description_set_style( description
, PANGO_STYLE_OBLIQUE
);
243 wxFAIL_MSG( _T("unknown font style") );
245 case wxFONTSTYLE_NORMAL
:
246 pango_font_description_set_style( description
, PANGO_STYLE_NORMAL
);
251 void wxNativeFontInfo::SetWeight(wxFontWeight weight
)
255 case wxFONTWEIGHT_BOLD
:
256 pango_font_description_set_weight(description
, PANGO_WEIGHT_BOLD
);
258 case wxFONTWEIGHT_LIGHT
:
259 pango_font_description_set_weight(description
, PANGO_WEIGHT_LIGHT
);
262 wxFAIL_MSG( _T("unknown font weight") );
264 case wxFONTWEIGHT_NORMAL
:
265 pango_font_description_set_weight(description
, PANGO_WEIGHT_NORMAL
);
269 void wxNativeFontInfo::SetUnderlined(bool WXUNUSED(underlined
))
271 // wxWindowDCImpl::DoDrawText will take care of rendering font with
272 // the underline attribute
273 wxFAIL_MSG( _T("not implemented") );
276 bool wxNativeFontInfo::SetFaceName(const wxString
& facename
)
278 pango_font_description_set_family(description
, wxPANGO_CONV(facename
));
280 // we return true because Pango doesn't tell us if the call failed or not;
281 // instead on wxGTK wxFont::SetFaceName() will call wxFontBase::SetFaceName()
282 // which does the check
286 void wxNativeFontInfo::SetFamily(wxFontFamily
WXUNUSED(family
))
288 wxFAIL_MSG( _T("not implemented") );
291 void wxNativeFontInfo::SetEncoding(wxFontEncoding
WXUNUSED(encoding
))
293 wxFAIL_MSG( _T("not implemented: Pango encoding is always UTF8") );
296 bool wxNativeFontInfo::FromString(const wxString
& s
)
299 pango_font_description_free( description
);
301 // there is a bug in at least pango <= 1.13 which makes it (or its backends)
302 // segfault for very big point sizes and for negative point sizes.
303 // To workaround that bug for pango <= 1.13
304 // (see http://bugzilla.gnome.org/show_bug.cgi?id=340229)
305 // we do the check on the size here using same (arbitrary) limits used by
306 // pango > 1.13. Note that the segfault could happen also for pointsize
307 // smaller than this limit !!
309 const size_t pos
= str
.find_last_of(_T(" "));
311 if ( pos
!= wxString::npos
&& wxString(str
, pos
+ 1).ToDouble(&size
) )
316 else if ( size
>= 1E6
)
319 if ( !sizeStr
.empty() )
321 // replace the old size with the adjusted one
322 str
= wxString(s
, 0, pos
) + sizeStr
;
326 description
= pango_font_description_from_string(wxPANGO_CONV(str
));
329 // ensure a valid facename is selected
330 if (!wxFontEnumerator::IsValidFacename(GetFaceName()))
331 SetFaceName(wxNORMAL_FONT
->GetFaceName());
332 #endif // wxUSE_FONTENUM
337 wxString
wxNativeFontInfo::ToString() const
339 wxGtkString
str(pango_font_description_to_string( description
));
341 return wxPANGO_CONV_BACK(str
);
344 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
346 return FromString( s
);
349 wxString
wxNativeFontInfo::ToUserString() const
358 #pragma message disable nosimpint
361 #include <X11/Xlib.h>
364 #pragma message enable nosimpint
367 #elif defined(__WXGTK__)
368 // we have to declare struct tm to avoid problems with first forward
369 // declaring it in C code (glib.h included from gdk.h does it) and then
370 // defining it when time.h is included from the headers below - this is
371 // known not to work at least with Sun CC 6.01
378 // ----------------------------------------------------------------------------
380 // ----------------------------------------------------------------------------
382 static wxHashTable
*g_fontHash
= NULL
;
384 // ----------------------------------------------------------------------------
386 // ----------------------------------------------------------------------------
388 // define the functions to create and destroy native fonts for this toolkit
390 wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
392 return XLoadQueryFont((Display
*)wxGetDisplay(), fontSpec
);
395 inline void wxFreeFont(wxNativeFont font
)
397 XFreeFont((Display
*)wxGetDisplay(), (XFontStruct
*)font
);
399 #elif defined(__WXGTK__)
400 wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
402 // VZ: we should use gdk_fontset_load() instead of gdk_font_load()
403 // here to be able to display Japanese fonts correctly (at least
404 // this is what people report) but unfortunately doing it results
405 // in tons of warnings when using GTK with "normal" European
406 // languages and so we can't always do it and I don't know enough
407 // to determine when should this be done... (FIXME)
408 return gdk_font_load( wxConvertWX2MB(fontSpec
) );
411 inline void wxFreeFont(wxNativeFont font
)
413 gdk_font_unref(font
);
416 #error "Unknown GUI toolkit"
419 static bool wxTestFontSpec(const wxString
& fontspec
);
421 static wxNativeFont
wxLoadQueryFont(int pointSize
,
426 const wxString
& facename
,
427 const wxString
& xregistry
,
428 const wxString
& xencoding
,
429 wxString
* xFontName
);
431 // ============================================================================
433 // ============================================================================
435 // ----------------------------------------------------------------------------
436 // wxNativeEncodingInfo
437 // ----------------------------------------------------------------------------
439 // convert to/from the string representation: format is
440 // encodingid;registry;encoding[;facename]
441 bool wxNativeEncodingInfo::FromString(const wxString
& s
)
443 // use ";", not "-" because it may be part of encoding name
444 wxStringTokenizer
tokenizer(s
, _T(";"));
446 wxString encid
= tokenizer
.GetNextToken();
448 if ( !encid
.ToLong(&enc
) )
450 encoding
= (wxFontEncoding
)enc
;
452 xregistry
= tokenizer
.GetNextToken();
456 xencoding
= tokenizer
.GetNextToken();
461 facename
= tokenizer
.GetNextToken();
466 wxString
wxNativeEncodingInfo::ToString() const
469 s
<< (long)encoding
<< _T(';') << xregistry
<< _T(';') << xencoding
;
470 if ( !facename
.empty() )
472 s
<< _T(';') << facename
;
478 // ----------------------------------------------------------------------------
480 // ----------------------------------------------------------------------------
482 void wxNativeFontInfo::Init()
487 bool wxNativeFontInfo::FromString(const wxString
& s
)
489 wxStringTokenizer
tokenizer(s
, _T(";"));
492 wxString token
= tokenizer
.GetNextToken();
493 if ( token
!= _T('0') )
496 xFontName
= tokenizer
.GetNextToken();
498 // this should be the end
499 if ( tokenizer
.HasMoreTokens() )
502 return FromXFontName(xFontName
);
505 wxString
wxNativeFontInfo::ToString() const
508 return wxString::Format(_T("%d;%s"), 0, GetXFontName().c_str());
511 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
513 return FromXFontName(s
);
516 wxString
wxNativeFontInfo::ToUserString() const
518 return GetXFontName();
521 bool wxNativeFontInfo::HasElements() const
523 // we suppose that the foundry is never empty, so if it is it means that we
524 // had never parsed the XLFD
525 return !fontElements
[0].empty();
528 wxString
wxNativeFontInfo::GetXFontComponent(wxXLFDField field
) const
530 wxCHECK_MSG( field
< wxXLFD_MAX
, wxEmptyString
, _T("invalid XLFD field") );
532 if ( !HasElements() )
535 if ( !((wxNativeFontInfo
*)this)->FromXFontName(xFontName
) )
536 return wxEmptyString
;
539 return fontElements
[field
];
542 bool wxNativeFontInfo::FromXFontName(const wxString
& fontname
)
544 // TODO: we should be able to handle the font aliases here, but how?
545 wxStringTokenizer
tokenizer(fontname
, _T("-"));
547 // skip the leading, usually empty field (font name registry)
548 if ( !tokenizer
.HasMoreTokens() )
551 (void)tokenizer
.GetNextToken();
553 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
555 if ( !tokenizer
.HasMoreTokens() )
557 // not enough elements in the XLFD - or maybe an alias
561 wxString field
= tokenizer
.GetNextToken();
562 if ( !field
.empty() && field
!= _T('*') )
564 // we're really initialized now
568 fontElements
[n
] = field
;
571 // this should be all
572 if ( tokenizer
.HasMoreTokens() )
578 wxString
wxNativeFontInfo::GetXFontName() const
580 if ( xFontName
.empty() )
582 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
584 // replace the non specified elements with '*' except for the
585 // additional style which is usually just omitted
586 wxString elt
= fontElements
[n
];
587 if ( elt
.empty() && n
!= wxXLFD_ADDSTYLE
)
593 ((wxNativeFontInfo
*)this)->xFontName
<< _T('-') << elt
;
601 wxNativeFontInfo::SetXFontComponent(wxXLFDField field
, const wxString
& value
)
603 wxCHECK_RET( field
< wxXLFD_MAX
, _T("invalid XLFD field") );
605 // this class should be initialized with a valid font spec first and only
606 // then the fields may be modified!
607 wxASSERT_MSG( !IsDefault(), _T("can't modify an uninitialized XLFD") );
609 if ( !HasElements() )
612 if ( !((wxNativeFontInfo
*)this)->FromXFontName(xFontName
) )
614 wxFAIL_MSG( _T("can't set font element for invalid XLFD") );
620 fontElements
[field
] = value
;
622 // invalidate the XFLD, it doesn't correspond to the font elements any more
626 void wxNativeFontInfo::SetXFontName(const wxString
& xFontName_
)
628 // invalidate the font elements, GetXFontComponent() will reparse the XLFD
629 fontElements
[0].clear();
631 xFontName
= xFontName_
;
636 int wxNativeFontInfo::GetPointSize() const
638 const wxString s
= GetXFontComponent(wxXLFD_POINTSIZE
);
640 // return -1 to indicate that the size is unknown
642 return s
.ToLong(&l
) ? l
: -1;
645 wxFontStyle
wxNativeFontInfo::GetStyle() const
647 const wxString s
= GetXFontComponent(wxXLFD_SLANT
);
649 if ( s
.length() != 1 )
651 // it is really unknown but we don't have any way to return it from
653 return wxFONTSTYLE_NORMAL
;
656 switch ( s
[0].GetValue() )
659 // again, unknown but consider normal by default
662 return wxFONTSTYLE_NORMAL
;
665 return wxFONTSTYLE_ITALIC
;
668 return wxFONTSTYLE_SLANT
;
672 wxFontWeight
wxNativeFontInfo::GetWeight() const
674 const wxString s
= GetXFontComponent(wxXLFD_WEIGHT
).MakeLower();
675 if ( s
.find(_T("bold")) != wxString::npos
|| s
== _T("black") )
676 return wxFONTWEIGHT_BOLD
;
677 else if ( s
== _T("light") )
678 return wxFONTWEIGHT_LIGHT
;
680 return wxFONTWEIGHT_NORMAL
;
683 bool wxNativeFontInfo::GetUnderlined() const
685 // X fonts are never underlined
689 wxString
wxNativeFontInfo::GetFaceName() const
691 // wxWidgets facename probably more accurately corresponds to X family
692 return GetXFontComponent(wxXLFD_FAMILY
);
695 wxFontFamily
wxNativeFontInfo::GetFamily() const
697 // and wxWidgets family -- to X foundry, but we have to translate it to
698 // wxFontFamily somehow...
699 wxFAIL_MSG(_T("not implemented")); // GetXFontComponent(wxXLFD_FOUNDRY);
701 return wxFONTFAMILY_DEFAULT
;
704 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
706 // we already have the code for this but need to refactor it first
707 wxFAIL_MSG( _T("not implemented") );
709 return wxFONTENCODING_MAX
;
712 void wxNativeFontInfo::SetPointSize(int pointsize
)
714 SetXFontComponent(wxXLFD_POINTSIZE
, wxString::Format(_T("%d"), pointsize
));
717 void wxNativeFontInfo::SetStyle(wxFontStyle style
)
722 case wxFONTSTYLE_ITALIC
:
726 case wxFONTSTYLE_SLANT
:
730 case wxFONTSTYLE_NORMAL
:
734 wxFAIL_MSG( _T("unknown wxFontStyle in wxNativeFontInfo::SetStyle") );
738 SetXFontComponent(wxXLFD_SLANT
, s
);
741 void wxNativeFontInfo::SetWeight(wxFontWeight weight
)
746 case wxFONTWEIGHT_BOLD
:
750 case wxFONTWEIGHT_LIGHT
:
754 case wxFONTWEIGHT_NORMAL
:
759 wxFAIL_MSG( _T("unknown wxFontWeight in wxNativeFontInfo::SetWeight") );
763 SetXFontComponent(wxXLFD_WEIGHT
, s
);
766 void wxNativeFontInfo::SetUnderlined(bool WXUNUSED(underlined
))
768 // can't do this under X
771 bool wxNativeFontInfo::SetFaceName(const wxString
& facename
)
773 SetXFontComponent(wxXLFD_FAMILY
, facename
);
777 void wxNativeFontInfo::SetFamily(wxFontFamily
WXUNUSED(family
))
779 // wxFontFamily -> X foundry, anyone?
780 wxFAIL_MSG( _T("not implemented") );
782 // SetXFontComponent(wxXLFD_FOUNDRY, ...);
785 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding
)
787 wxNativeEncodingInfo info
;
788 if ( wxGetNativeFontEncoding(encoding
, &info
) )
790 SetXFontComponent(wxXLFD_ENCODING
, info
.xencoding
);
791 SetXFontComponent(wxXLFD_REGISTRY
, info
.xregistry
);
795 // ----------------------------------------------------------------------------
797 // ----------------------------------------------------------------------------
799 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
800 wxNativeEncodingInfo
*info
)
802 wxCHECK_MSG( info
, false, _T("bad pointer in wxGetNativeFontEncoding") );
804 if ( encoding
== wxFONTENCODING_DEFAULT
)
806 encoding
= wxFont::GetDefaultEncoding();
811 case wxFONTENCODING_ISO8859_1
:
812 case wxFONTENCODING_ISO8859_2
:
813 case wxFONTENCODING_ISO8859_3
:
814 case wxFONTENCODING_ISO8859_4
:
815 case wxFONTENCODING_ISO8859_5
:
816 case wxFONTENCODING_ISO8859_6
:
817 case wxFONTENCODING_ISO8859_7
:
818 case wxFONTENCODING_ISO8859_8
:
819 case wxFONTENCODING_ISO8859_9
:
820 case wxFONTENCODING_ISO8859_10
:
821 case wxFONTENCODING_ISO8859_11
:
822 case wxFONTENCODING_ISO8859_12
:
823 case wxFONTENCODING_ISO8859_13
:
824 case wxFONTENCODING_ISO8859_14
:
825 case wxFONTENCODING_ISO8859_15
:
827 int cp
= encoding
- wxFONTENCODING_ISO8859_1
+ 1;
828 info
->xregistry
= wxT("iso8859");
829 info
->xencoding
.Printf(wxT("%d"), cp
);
833 case wxFONTENCODING_UTF8
:
834 info
->xregistry
= wxT("iso10646");
835 info
->xencoding
= wxT("*");
838 case wxFONTENCODING_GB2312
:
839 info
->xregistry
= wxT("GB2312"); // or the otherway round?
840 info
->xencoding
= wxT("*");
843 case wxFONTENCODING_KOI8
:
844 case wxFONTENCODING_KOI8_U
:
845 info
->xregistry
= wxT("koi8");
847 // we don't make distinction between koi8-r, koi8-u and koi8-ru (so far)
848 info
->xencoding
= wxT("*");
851 case wxFONTENCODING_CP1250
:
852 case wxFONTENCODING_CP1251
:
853 case wxFONTENCODING_CP1252
:
854 case wxFONTENCODING_CP1253
:
855 case wxFONTENCODING_CP1254
:
856 case wxFONTENCODING_CP1255
:
857 case wxFONTENCODING_CP1256
:
858 case wxFONTENCODING_CP1257
:
860 int cp
= encoding
- wxFONTENCODING_CP1250
+ 1250;
861 info
->xregistry
= wxT("microsoft");
862 info
->xencoding
.Printf(wxT("cp%d"), cp
);
866 case wxFONTENCODING_EUC_JP
:
867 case wxFONTENCODING_SHIFT_JIS
:
868 info
->xregistry
= "jis*";
869 info
->xencoding
= "*";
872 case wxFONTENCODING_SYSTEM
:
874 info
->xencoding
= wxT("*");
878 // don't know how to translate this encoding into X fontspec
882 info
->encoding
= encoding
;
887 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
890 fontspec
.Printf(_T("-*-%s-*-*-*-*-*-*-*-*-*-*-%s-%s"),
891 !info
.facename
? _T("*") : info
.facename
.c_str(),
892 info
.xregistry
.c_str(),
893 info
.xencoding
.c_str());
895 return wxTestFontSpec(fontspec
);
898 // ----------------------------------------------------------------------------
899 // X-specific functions
900 // ----------------------------------------------------------------------------
902 wxNativeFont
wxLoadQueryNearestFont(int pointSize
,
907 const wxString
&facename
,
908 wxFontEncoding encoding
,
911 if ( encoding
== wxFONTENCODING_DEFAULT
)
913 encoding
= wxFont::GetDefaultEncoding();
916 // first determine the encoding - if the font doesn't exist at all in this
917 // encoding, it's useless to do all other approximations (i.e. size,
918 // family &c don't matter much)
919 wxNativeEncodingInfo info
;
920 if ( encoding
== wxFONTENCODING_SYSTEM
)
922 // This will always work so we don't test to save time
923 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
927 if ( !wxGetNativeFontEncoding(encoding
, &info
) ||
928 !wxTestFontEncoding(info
) )
931 if ( !wxFontMapper::Get()->GetAltForEncoding(encoding
, &info
) )
932 #endif // wxUSE_FONTMAP
934 // unspported encoding - replace it with the default
936 // NB: we can't just return 0 from here because wxGTK code doesn't
937 // check for it (i.e. it supposes that we'll always succeed),
938 // so it would provoke a crash
939 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
944 // OK, we have the correct xregistry/xencoding in info structure
945 wxNativeFont font
= 0;
947 // if we already have the X font name, try to use it
948 if( xFontName
&& !xFontName
->empty() )
951 // Make sure point size is correct for scale factor.
953 wxStringTokenizer
tokenizer(*xFontName
, _T("-"), wxTOKEN_RET_DELIMS
);
954 wxString newFontName
;
956 for(int i
= 0; i
< 8; i
++)
957 newFontName
+= tokenizer
.NextToken();
959 (void) tokenizer
.NextToken();
961 newFontName
+= wxString::Format(wxT("%d-"), pointSize
);
963 while(tokenizer
.HasMoreTokens())
964 newFontName
+= tokenizer
.GetNextToken();
966 font
= wxLoadFont(newFontName
);
969 *xFontName
= newFontName
;
974 // search up and down by stepsize 10
975 int max_size
= pointSize
+ 20 * (1 + (pointSize
/180));
976 int min_size
= pointSize
- 20 * (1 + (pointSize
/180));
978 int i
, round
; // counters
980 // first round: search for equal, then for smaller and for larger size
981 // with the given weight and style
982 int testweight
= weight
;
983 int teststyle
= style
;
985 for ( round
= 0; round
< 3; round
++ )
987 // second round: use normal weight
990 if ( testweight
!= wxNORMAL
)
992 testweight
= wxNORMAL
;
996 ++round
; // fall through to third round
1000 // third round: ... and use normal style
1003 if ( teststyle
!= wxNORMAL
)
1005 teststyle
= wxNORMAL
;
1012 // Search for equal or smaller size (approx.)
1013 for ( i
= pointSize
; !font
&& i
>= 10 && i
>= min_size
; i
-= 10 )
1015 font
= wxLoadQueryFont(i
, family
, teststyle
, testweight
, underlined
,
1016 facename
, info
.xregistry
, info
.xencoding
,
1020 // Search for larger size (approx.)
1021 for ( i
= pointSize
+ 10; !font
&& i
<= max_size
; i
+= 10 )
1023 font
= wxLoadQueryFont(i
, family
, teststyle
, testweight
, underlined
,
1024 facename
, info
.xregistry
, info
.xencoding
,
1029 // Try default family
1030 if ( !font
&& family
!= wxDEFAULT
)
1032 font
= wxLoadQueryFont(pointSize
, wxDEFAULT
, style
, weight
,
1033 underlined
, facename
,
1034 info
.xregistry
, info
.xencoding
,
1038 // ignore size, family, style and weight but try to find font with the
1039 // given facename and encoding
1042 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1043 underlined
, facename
,
1044 info
.xregistry
, info
.xencoding
,
1047 // ignore family as well
1050 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1051 underlined
, wxEmptyString
,
1052 info
.xregistry
, info
.xencoding
,
1055 // if it still failed, try to get the font of any size but
1056 // with the requested encoding: this can happen if the
1057 // encoding is only available in one size which happens to be
1058 // different from 120
1061 font
= wxLoadQueryFont(-1, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1062 false, wxEmptyString
,
1063 info
.xregistry
, info
.xencoding
,
1066 // this should never happen as we had tested for it in the
1067 // very beginning, but if it does, do return something non
1068 // NULL or we'd crash in wxFont code
1071 wxFAIL_MSG( _T("this encoding should be available!") );
1073 font
= wxLoadQueryFont(-1,
1074 wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1075 false, wxEmptyString
,
1087 // ----------------------------------------------------------------------------
1088 // private functions
1089 // ----------------------------------------------------------------------------
1091 // returns true if there are any fonts matching this font spec
1092 static bool wxTestFontSpec(const wxString
& fontspec
)
1094 // some X servers will fail to load this font because there are too many
1095 // matches so we must test explicitly for this
1096 if ( fontspec
== _T("-*-*-*-*-*-*-*-*-*-*-*-*-*-*") )
1101 wxNativeFont test
= (wxNativeFont
) g_fontHash
->Get( fontspec
);
1107 test
= wxLoadFont(fontspec
);
1108 g_fontHash
->Put( fontspec
, (wxObject
*) test
);
1122 static wxNativeFont
wxLoadQueryFont(int pointSize
,
1126 bool WXUNUSED(underlined
),
1127 const wxString
& facename
,
1128 const wxString
& xregistry
,
1129 const wxString
& xencoding
,
1130 wxString
* xFontName
)
1135 case wxDECORATIVE
: xfamily
= wxT("lucida"); break;
1136 case wxROMAN
: xfamily
= wxT("times"); break;
1137 case wxMODERN
: xfamily
= wxT("courier"); break;
1138 case wxSWISS
: xfamily
= wxT("helvetica"); break;
1139 case wxTELETYPE
: xfamily
= wxT("lucidatypewriter"); break;
1140 case wxSCRIPT
: xfamily
= wxT("utopia"); break;
1141 default: xfamily
= wxT("*");
1149 xweight
= MWLF_WEIGHT_BOLD
;
1154 xweight
= MWLF_WEIGHT_LIGHT
;
1159 xweight
= MWLF_WEIGHT_NORMAL
;
1165 xweight
= MWLF_WEIGHT_DEFAULT
;
1169 GR_SCREEN_INFO screenInfo
;
1170 GrGetScreenInfo(& screenInfo
);
1172 int yPixelsPerCM
= screenInfo
.ydpcm
;
1174 // A point is 1/72 of an inch.
1175 // An inch is 2.541 cm.
1176 // So pixelHeight = (pointSize / 72) (inches) * 2.541 (for cm) * yPixelsPerCM (for pixels)
1177 // In fact pointSize is 10 * the normal point size so
1180 int pixelHeight
= (int) ( (((float)pointSize
) / 720.0) * 2.541 * (float) yPixelsPerCM
) ;
1182 // An alternative: assume that the screen is 72 dpi.
1183 //int pixelHeight = (int) (((float)pointSize / 720.0) * 72.0) ;
1184 //int pixelHeight = (int) ((float)pointSize / 10.0) ;
1187 logFont
.lfHeight
= pixelHeight
;
1188 logFont
.lfWidth
= 0;
1189 logFont
.lfEscapement
= 0;
1190 logFont
.lfOrientation
= 0;
1191 logFont
.lfWeight
= xweight
;
1192 logFont
.lfItalic
= (style
== wxNORMAL
? 0 : 1) ;
1193 logFont
.lfUnderline
= 0;
1194 logFont
.lfStrikeOut
= 0;
1195 logFont
.lfCharSet
= MWLF_CHARSET_DEFAULT
; // TODO: select appropriate one
1196 logFont
.lfOutPrecision
= MWLF_TYPE_DEFAULT
;
1197 logFont
.lfClipPrecision
= 0; // Not used
1198 logFont
.lfRoman
= (family
== wxROMAN
? 1 : 0) ;
1199 logFont
.lfSerif
= (family
== wxSWISS
? 0 : 1) ;
1200 logFont
.lfSansSerif
= !logFont
.lfSerif
;
1201 logFont
.lfModern
= (family
== wxMODERN
? 1 : 0) ;
1202 logFont
.lfProportional
= (family
== wxTELETYPE
? 0 : 1) ;
1203 logFont
.lfOblique
= 0;
1204 logFont
.lfSmallCaps
= 0;
1205 logFont
.lfPitch
= 0; // 0 = default
1206 strcpy(logFont
.lfFaceName
, facename
.c_str());
1208 XFontStruct
* fontInfo
= (XFontStruct
*) malloc(sizeof(XFontStruct
));
1209 fontInfo
->fid
= GrCreateFont((GR_CHAR
*) facename
.c_str(), pixelHeight
, & logFont
);
1210 GrGetFontInfo(fontInfo
->fid
, & fontInfo
->info
);
1211 return (wxNativeFont
) fontInfo
;
1215 if (!facename
.empty())
1217 fontSpec
.Printf(wxT("-*-%s-*-*-normal-*-*-*-*-*-*-*-*-*"),
1220 if ( wxTestFontSpec(fontSpec
) )
1224 //else: no such family, use default one instead
1231 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1233 if ( wxTestFontSpec(fontSpec
) )
1238 // fall through - try wxITALIC now
1241 fontSpec
.Printf(wxT("-*-%s-*-i-*-*-*-*-*-*-*-*-*-*"),
1243 if ( wxTestFontSpec(fontSpec
) )
1247 else if ( style
== wxITALIC
) // and not wxSLANT
1250 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1252 if ( wxTestFontSpec(fontSpec
) )
1258 // no italic, no slant - leave default
1265 wxFAIL_MSG(_T("unknown font style"));
1266 // fall back to normal
1278 fontSpec
.Printf(wxT("-*-%s-bold-*-*-*-*-*-*-*-*-*-*-*"),
1280 if ( wxTestFontSpec(fontSpec
) )
1282 xweight
= wxT("bold");
1285 fontSpec
.Printf(wxT("-*-%s-heavy-*-*-*-*-*-*-*-*-*-*-*"),
1287 if ( wxTestFontSpec(fontSpec
) )
1289 xweight
= wxT("heavy");
1292 fontSpec
.Printf(wxT("-*-%s-extrabold-*-*-*-*-*-*-*-*-*-*-*"),
1294 if ( wxTestFontSpec(fontSpec
) )
1296 xweight
= wxT("extrabold");
1299 fontSpec
.Printf(wxT("-*-%s-demibold-*-*-*-*-*-*-*-*-*-*-*"),
1301 if ( wxTestFontSpec(fontSpec
) )
1303 xweight
= wxT("demibold");
1306 fontSpec
.Printf(wxT("-*-%s-black-*-*-*-*-*-*-*-*-*-*-*"),
1308 if ( wxTestFontSpec(fontSpec
) )
1310 xweight
= wxT("black");
1313 fontSpec
.Printf(wxT("-*-%s-ultrablack-*-*-*-*-*-*-*-*-*-*-*"),
1315 if ( wxTestFontSpec(fontSpec
) )
1317 xweight
= wxT("ultrablack");
1324 fontSpec
.Printf(wxT("-*-%s-light-*-*-*-*-*-*-*-*-*-*-*"),
1326 if ( wxTestFontSpec(fontSpec
) )
1328 xweight
= wxT("light");
1331 fontSpec
.Printf(wxT("-*-%s-thin-*-*-*-*-*-*-*-*-*-*-*"),
1333 if ( wxTestFontSpec(fontSpec
) )
1335 xweight
= wxT("thin");
1342 fontSpec
.Printf(wxT("-*-%s-medium-*-*-*-*-*-*-*-*-*-*-*"),
1344 if ( wxTestFontSpec(fontSpec
) )
1346 xweight
= wxT("medium");
1349 fontSpec
.Printf(wxT("-*-%s-normal-*-*-*-*-*-*-*-*-*-*-*"),
1351 if ( wxTestFontSpec(fontSpec
) )
1353 xweight
= wxT("normal");
1356 fontSpec
.Printf(wxT("-*-%s-regular-*-*-*-*-*-*-*-*-*-*-*"),
1358 if ( wxTestFontSpec(fontSpec
) )
1360 xweight
= wxT("regular");
1366 default: xweight
= wxT("*"); break;
1369 // if pointSize is -1, don't specify any
1371 if ( pointSize
== -1 )
1377 sizeSpec
.Printf(_T("%d"), pointSize
);
1380 // construct the X font spec from our data
1381 fontSpec
.Printf(wxT("-*-%s-%s-%s-normal-*-*-%s-*-*-*-*-%s-%s"),
1382 xfamily
.c_str(), xweight
.c_str(), xstyle
.c_str(),
1383 sizeSpec
.c_str(), xregistry
.c_str(), xencoding
.c_str());
1386 *xFontName
= fontSpec
;
1388 return wxLoadFont(fontSpec
);
1393 // ----------------------------------------------------------------------------
1395 // ----------------------------------------------------------------------------
1397 class wxFontModule
: public wxModule
1404 DECLARE_DYNAMIC_CLASS(wxFontModule
)
1407 IMPLEMENT_DYNAMIC_CLASS(wxFontModule
, wxModule
)
1409 bool wxFontModule::OnInit()
1411 g_fontHash
= new wxHashTable( wxKEY_STRING
);
1416 void wxFontModule::OnExit()
1423 #endif // GTK 2.0/1.x