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
));
282 void wxNativeFontInfo::SetFamily(wxFontFamily
WXUNUSED(family
))
284 wxFAIL_MSG( _T("not implemented") );
287 void wxNativeFontInfo::SetEncoding(wxFontEncoding
WXUNUSED(encoding
))
289 wxFAIL_MSG( _T("not implemented: Pango encoding is always UTF8") );
292 bool wxNativeFontInfo::FromString(const wxString
& s
)
295 pango_font_description_free( description
);
297 // there is a bug in at least pango <= 1.13 which makes it (or its backends)
298 // segfault for very big point sizes and for negative point sizes.
299 // To workaround that bug for pango <= 1.13
300 // (see http://bugzilla.gnome.org/show_bug.cgi?id=340229)
301 // we do the check on the size here using same (arbitrary) limits used by
302 // pango > 1.13. Note that the segfault could happen also for pointsize
303 // smaller than this limit !!
305 const size_t pos
= str
.find_last_of(_T(" "));
307 if ( pos
!= wxString::npos
&& wxString(str
, pos
+ 1).ToDouble(&size
) )
312 else if ( size
>= 1E6
)
315 if ( !sizeStr
.empty() )
317 // replace the old size with the adjusted one
318 str
= wxString(s
, 0, pos
) + sizeStr
;
322 description
= pango_font_description_from_string(wxPANGO_CONV(str
));
325 // ensure a valid facename is selected
326 if (!wxFontEnumerator::IsValidFacename(GetFaceName()))
327 SetFaceName(wxNORMAL_FONT
->GetFaceName());
328 #endif // wxUSE_FONTENUM
333 wxString
wxNativeFontInfo::ToString() const
335 wxGtkString
str(pango_font_description_to_string( description
));
337 return wxPANGO_CONV_BACK(str
);
340 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
342 return FromString( s
);
345 wxString
wxNativeFontInfo::ToUserString() const
354 #pragma message disable nosimpint
357 #include <X11/Xlib.h>
360 #pragma message enable nosimpint
363 #elif defined(__WXGTK__)
364 // we have to declare struct tm to avoid problems with first forward
365 // declaring it in C code (glib.h included from gdk.h does it) and then
366 // defining it when time.h is included from the headers below - this is
367 // known not to work at least with Sun CC 6.01
374 // ----------------------------------------------------------------------------
376 // ----------------------------------------------------------------------------
378 static wxHashTable
*g_fontHash
= NULL
;
380 // ----------------------------------------------------------------------------
382 // ----------------------------------------------------------------------------
384 // define the functions to create and destroy native fonts for this toolkit
386 wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
388 return XLoadQueryFont((Display
*)wxGetDisplay(), fontSpec
);
391 inline void wxFreeFont(wxNativeFont font
)
393 XFreeFont((Display
*)wxGetDisplay(), (XFontStruct
*)font
);
395 #elif defined(__WXGTK__)
396 wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
398 // VZ: we should use gdk_fontset_load() instead of gdk_font_load()
399 // here to be able to display Japanese fonts correctly (at least
400 // this is what people report) but unfortunately doing it results
401 // in tons of warnings when using GTK with "normal" European
402 // languages and so we can't always do it and I don't know enough
403 // to determine when should this be done... (FIXME)
404 return gdk_font_load( wxConvertWX2MB(fontSpec
) );
407 inline void wxFreeFont(wxNativeFont font
)
409 gdk_font_unref(font
);
412 #error "Unknown GUI toolkit"
415 static bool wxTestFontSpec(const wxString
& fontspec
);
417 static wxNativeFont
wxLoadQueryFont(int pointSize
,
422 const wxString
& facename
,
423 const wxString
& xregistry
,
424 const wxString
& xencoding
,
425 wxString
* xFontName
);
427 // ============================================================================
429 // ============================================================================
431 // ----------------------------------------------------------------------------
432 // wxNativeEncodingInfo
433 // ----------------------------------------------------------------------------
435 // convert to/from the string representation: format is
436 // encodingid;registry;encoding[;facename]
437 bool wxNativeEncodingInfo::FromString(const wxString
& s
)
439 // use ";", not "-" because it may be part of encoding name
440 wxStringTokenizer
tokenizer(s
, _T(";"));
442 wxString encid
= tokenizer
.GetNextToken();
444 if ( !encid
.ToLong(&enc
) )
446 encoding
= (wxFontEncoding
)enc
;
448 xregistry
= tokenizer
.GetNextToken();
452 xencoding
= tokenizer
.GetNextToken();
457 facename
= tokenizer
.GetNextToken();
462 wxString
wxNativeEncodingInfo::ToString() const
465 s
<< (long)encoding
<< _T(';') << xregistry
<< _T(';') << xencoding
;
466 if ( !facename
.empty() )
468 s
<< _T(';') << facename
;
474 // ----------------------------------------------------------------------------
476 // ----------------------------------------------------------------------------
478 void wxNativeFontInfo::Init()
483 bool wxNativeFontInfo::FromString(const wxString
& s
)
485 wxStringTokenizer
tokenizer(s
, _T(";"));
488 wxString token
= tokenizer
.GetNextToken();
489 if ( token
!= _T('0') )
492 xFontName
= tokenizer
.GetNextToken();
494 // this should be the end
495 if ( tokenizer
.HasMoreTokens() )
498 return FromXFontName(xFontName
);
501 wxString
wxNativeFontInfo::ToString() const
504 return wxString::Format(_T("%d;%s"), 0, GetXFontName().c_str());
507 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
509 return FromXFontName(s
);
512 wxString
wxNativeFontInfo::ToUserString() const
514 return GetXFontName();
517 bool wxNativeFontInfo::HasElements() const
519 // we suppose that the foundry is never empty, so if it is it means that we
520 // had never parsed the XLFD
521 return !fontElements
[0].empty();
524 wxString
wxNativeFontInfo::GetXFontComponent(wxXLFDField field
) const
526 wxCHECK_MSG( field
< wxXLFD_MAX
, wxEmptyString
, _T("invalid XLFD field") );
528 if ( !HasElements() )
531 if ( !((wxNativeFontInfo
*)this)->FromXFontName(xFontName
) )
532 return wxEmptyString
;
535 return fontElements
[field
];
538 bool wxNativeFontInfo::FromXFontName(const wxString
& fontname
)
540 // TODO: we should be able to handle the font aliases here, but how?
541 wxStringTokenizer
tokenizer(fontname
, _T("-"));
543 // skip the leading, usually empty field (font name registry)
544 if ( !tokenizer
.HasMoreTokens() )
547 (void)tokenizer
.GetNextToken();
549 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
551 if ( !tokenizer
.HasMoreTokens() )
553 // not enough elements in the XLFD - or maybe an alias
557 wxString field
= tokenizer
.GetNextToken();
558 if ( !field
.empty() && field
!= _T('*') )
560 // we're really initialized now
564 fontElements
[n
] = field
;
567 // this should be all
568 if ( tokenizer
.HasMoreTokens() )
574 wxString
wxNativeFontInfo::GetXFontName() const
576 if ( xFontName
.empty() )
578 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
580 // replace the non specified elements with '*' except for the
581 // additional style which is usually just omitted
582 wxString elt
= fontElements
[n
];
583 if ( elt
.empty() && n
!= wxXLFD_ADDSTYLE
)
589 ((wxNativeFontInfo
*)this)->xFontName
<< _T('-') << elt
;
597 wxNativeFontInfo::SetXFontComponent(wxXLFDField field
, const wxString
& value
)
599 wxCHECK_RET( field
< wxXLFD_MAX
, _T("invalid XLFD field") );
601 // this class should be initialized with a valid font spec first and only
602 // then the fields may be modified!
603 wxASSERT_MSG( !IsDefault(), _T("can't modify an uninitialized XLFD") );
605 if ( !HasElements() )
608 if ( !((wxNativeFontInfo
*)this)->FromXFontName(xFontName
) )
610 wxFAIL_MSG( _T("can't set font element for invalid XLFD") );
616 fontElements
[field
] = value
;
618 // invalidate the XFLD, it doesn't correspond to the font elements any more
622 void wxNativeFontInfo::SetXFontName(const wxString
& xFontName_
)
624 // invalidate the font elements, GetXFontComponent() will reparse the XLFD
625 fontElements
[0].clear();
627 xFontName
= xFontName_
;
632 int wxNativeFontInfo::GetPointSize() const
634 const wxString s
= GetXFontComponent(wxXLFD_POINTSIZE
);
636 // return -1 to indicate that the size is unknown
638 return s
.ToLong(&l
) ? l
: -1;
641 wxFontStyle
wxNativeFontInfo::GetStyle() const
643 const wxString s
= GetXFontComponent(wxXLFD_SLANT
);
645 if ( s
.length() != 1 )
647 // it is really unknown but we don't have any way to return it from
649 return wxFONTSTYLE_NORMAL
;
652 switch ( s
[0].GetValue() )
655 // again, unknown but consider normal by default
658 return wxFONTSTYLE_NORMAL
;
661 return wxFONTSTYLE_ITALIC
;
664 return wxFONTSTYLE_SLANT
;
668 wxFontWeight
wxNativeFontInfo::GetWeight() const
670 const wxString s
= GetXFontComponent(wxXLFD_WEIGHT
).MakeLower();
671 if ( s
.find(_T("bold")) != wxString::npos
|| s
== _T("black") )
672 return wxFONTWEIGHT_BOLD
;
673 else if ( s
== _T("light") )
674 return wxFONTWEIGHT_LIGHT
;
676 return wxFONTWEIGHT_NORMAL
;
679 bool wxNativeFontInfo::GetUnderlined() const
681 // X fonts are never underlined
685 wxString
wxNativeFontInfo::GetFaceName() const
687 // wxWidgets facename probably more accurately corresponds to X family
688 return GetXFontComponent(wxXLFD_FAMILY
);
691 wxFontFamily
wxNativeFontInfo::GetFamily() const
693 // and wxWidgets family -- to X foundry, but we have to translate it to
694 // wxFontFamily somehow...
695 wxFAIL_MSG(_T("not implemented")); // GetXFontComponent(wxXLFD_FOUNDRY);
697 return wxFONTFAMILY_DEFAULT
;
700 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
702 // we already have the code for this but need to refactor it first
703 wxFAIL_MSG( _T("not implemented") );
705 return wxFONTENCODING_MAX
;
708 void wxNativeFontInfo::SetPointSize(int pointsize
)
710 SetXFontComponent(wxXLFD_POINTSIZE
, wxString::Format(_T("%d"), pointsize
));
713 void wxNativeFontInfo::SetStyle(wxFontStyle style
)
718 case wxFONTSTYLE_ITALIC
:
722 case wxFONTSTYLE_SLANT
:
726 case wxFONTSTYLE_NORMAL
:
730 wxFAIL_MSG( _T("unknown wxFontStyle in wxNativeFontInfo::SetStyle") );
734 SetXFontComponent(wxXLFD_SLANT
, s
);
737 void wxNativeFontInfo::SetWeight(wxFontWeight weight
)
742 case wxFONTWEIGHT_BOLD
:
746 case wxFONTWEIGHT_LIGHT
:
750 case wxFONTWEIGHT_NORMAL
:
755 wxFAIL_MSG( _T("unknown wxFontWeight in wxNativeFontInfo::SetWeight") );
759 SetXFontComponent(wxXLFD_WEIGHT
, s
);
762 void wxNativeFontInfo::SetUnderlined(bool WXUNUSED(underlined
))
764 // can't do this under X
767 bool wxNativeFontInfo::SetFaceName(const wxString
& facename
)
769 SetXFontComponent(wxXLFD_FAMILY
, facename
);
773 void wxNativeFontInfo::SetFamily(wxFontFamily
WXUNUSED(family
))
775 // wxFontFamily -> X foundry, anyone?
776 wxFAIL_MSG( _T("not implemented") );
778 // SetXFontComponent(wxXLFD_FOUNDRY, ...);
781 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding
)
783 wxNativeEncodingInfo info
;
784 if ( wxGetNativeFontEncoding(encoding
, &info
) )
786 SetXFontComponent(wxXLFD_ENCODING
, info
.xencoding
);
787 SetXFontComponent(wxXLFD_REGISTRY
, info
.xregistry
);
791 // ----------------------------------------------------------------------------
793 // ----------------------------------------------------------------------------
795 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
796 wxNativeEncodingInfo
*info
)
798 wxCHECK_MSG( info
, false, _T("bad pointer in wxGetNativeFontEncoding") );
800 if ( encoding
== wxFONTENCODING_DEFAULT
)
802 encoding
= wxFont::GetDefaultEncoding();
807 case wxFONTENCODING_ISO8859_1
:
808 case wxFONTENCODING_ISO8859_2
:
809 case wxFONTENCODING_ISO8859_3
:
810 case wxFONTENCODING_ISO8859_4
:
811 case wxFONTENCODING_ISO8859_5
:
812 case wxFONTENCODING_ISO8859_6
:
813 case wxFONTENCODING_ISO8859_7
:
814 case wxFONTENCODING_ISO8859_8
:
815 case wxFONTENCODING_ISO8859_9
:
816 case wxFONTENCODING_ISO8859_10
:
817 case wxFONTENCODING_ISO8859_11
:
818 case wxFONTENCODING_ISO8859_12
:
819 case wxFONTENCODING_ISO8859_13
:
820 case wxFONTENCODING_ISO8859_14
:
821 case wxFONTENCODING_ISO8859_15
:
823 int cp
= encoding
- wxFONTENCODING_ISO8859_1
+ 1;
824 info
->xregistry
= wxT("iso8859");
825 info
->xencoding
.Printf(wxT("%d"), cp
);
829 case wxFONTENCODING_UTF8
:
830 info
->xregistry
= wxT("iso10646");
831 info
->xencoding
= wxT("*");
834 case wxFONTENCODING_GB2312
:
835 info
->xregistry
= wxT("GB2312"); // or the otherway round?
836 info
->xencoding
= wxT("*");
839 case wxFONTENCODING_KOI8
:
840 case wxFONTENCODING_KOI8_U
:
841 info
->xregistry
= wxT("koi8");
843 // we don't make distinction between koi8-r, koi8-u and koi8-ru (so far)
844 info
->xencoding
= wxT("*");
847 case wxFONTENCODING_CP1250
:
848 case wxFONTENCODING_CP1251
:
849 case wxFONTENCODING_CP1252
:
850 case wxFONTENCODING_CP1253
:
851 case wxFONTENCODING_CP1254
:
852 case wxFONTENCODING_CP1255
:
853 case wxFONTENCODING_CP1256
:
854 case wxFONTENCODING_CP1257
:
856 int cp
= encoding
- wxFONTENCODING_CP1250
+ 1250;
857 info
->xregistry
= wxT("microsoft");
858 info
->xencoding
.Printf(wxT("cp%d"), cp
);
862 case wxFONTENCODING_EUC_JP
:
863 case wxFONTENCODING_SHIFT_JIS
:
864 info
->xregistry
= "jis*";
865 info
->xencoding
= "*";
868 case wxFONTENCODING_SYSTEM
:
870 info
->xencoding
= wxT("*");
874 // don't know how to translate this encoding into X fontspec
878 info
->encoding
= encoding
;
883 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
886 fontspec
.Printf(_T("-*-%s-*-*-*-*-*-*-*-*-*-*-%s-%s"),
887 !info
.facename
? _T("*") : info
.facename
.c_str(),
888 info
.xregistry
.c_str(),
889 info
.xencoding
.c_str());
891 return wxTestFontSpec(fontspec
);
894 // ----------------------------------------------------------------------------
895 // X-specific functions
896 // ----------------------------------------------------------------------------
898 wxNativeFont
wxLoadQueryNearestFont(int pointSize
,
903 const wxString
&facename
,
904 wxFontEncoding encoding
,
907 if ( encoding
== wxFONTENCODING_DEFAULT
)
909 encoding
= wxFont::GetDefaultEncoding();
912 // first determine the encoding - if the font doesn't exist at all in this
913 // encoding, it's useless to do all other approximations (i.e. size,
914 // family &c don't matter much)
915 wxNativeEncodingInfo info
;
916 if ( encoding
== wxFONTENCODING_SYSTEM
)
918 // This will always work so we don't test to save time
919 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
923 if ( !wxGetNativeFontEncoding(encoding
, &info
) ||
924 !wxTestFontEncoding(info
) )
927 if ( !wxFontMapper::Get()->GetAltForEncoding(encoding
, &info
) )
928 #endif // wxUSE_FONTMAP
930 // unspported encoding - replace it with the default
932 // NB: we can't just return 0 from here because wxGTK code doesn't
933 // check for it (i.e. it supposes that we'll always succeed),
934 // so it would provoke a crash
935 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
940 // OK, we have the correct xregistry/xencoding in info structure
941 wxNativeFont font
= 0;
943 // if we already have the X font name, try to use it
944 if( xFontName
&& !xFontName
->empty() )
947 // Make sure point size is correct for scale factor.
949 wxStringTokenizer
tokenizer(*xFontName
, _T("-"), wxTOKEN_RET_DELIMS
);
950 wxString newFontName
;
952 for(int i
= 0; i
< 8; i
++)
953 newFontName
+= tokenizer
.NextToken();
955 (void) tokenizer
.NextToken();
957 newFontName
+= wxString::Format(wxT("%d-"), pointSize
);
959 while(tokenizer
.HasMoreTokens())
960 newFontName
+= tokenizer
.GetNextToken();
962 font
= wxLoadFont(newFontName
);
965 *xFontName
= newFontName
;
970 // search up and down by stepsize 10
971 int max_size
= pointSize
+ 20 * (1 + (pointSize
/180));
972 int min_size
= pointSize
- 20 * (1 + (pointSize
/180));
974 int i
, round
; // counters
976 // first round: search for equal, then for smaller and for larger size
977 // with the given weight and style
978 int testweight
= weight
;
979 int teststyle
= style
;
981 for ( round
= 0; round
< 3; round
++ )
983 // second round: use normal weight
986 if ( testweight
!= wxNORMAL
)
988 testweight
= wxNORMAL
;
992 ++round
; // fall through to third round
996 // third round: ... and use normal style
999 if ( teststyle
!= wxNORMAL
)
1001 teststyle
= wxNORMAL
;
1008 // Search for equal or smaller size (approx.)
1009 for ( i
= pointSize
; !font
&& i
>= 10 && i
>= min_size
; i
-= 10 )
1011 font
= wxLoadQueryFont(i
, family
, teststyle
, testweight
, underlined
,
1012 facename
, info
.xregistry
, info
.xencoding
,
1016 // Search for larger size (approx.)
1017 for ( i
= pointSize
+ 10; !font
&& i
<= max_size
; i
+= 10 )
1019 font
= wxLoadQueryFont(i
, family
, teststyle
, testweight
, underlined
,
1020 facename
, info
.xregistry
, info
.xencoding
,
1025 // Try default family
1026 if ( !font
&& family
!= wxDEFAULT
)
1028 font
= wxLoadQueryFont(pointSize
, wxDEFAULT
, style
, weight
,
1029 underlined
, facename
,
1030 info
.xregistry
, info
.xencoding
,
1034 // ignore size, family, style and weight but try to find font with the
1035 // given facename and encoding
1038 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1039 underlined
, facename
,
1040 info
.xregistry
, info
.xencoding
,
1043 // ignore family as well
1046 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1047 underlined
, wxEmptyString
,
1048 info
.xregistry
, info
.xencoding
,
1051 // if it still failed, try to get the font of any size but
1052 // with the requested encoding: this can happen if the
1053 // encoding is only available in one size which happens to be
1054 // different from 120
1057 font
= wxLoadQueryFont(-1, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1058 false, wxEmptyString
,
1059 info
.xregistry
, info
.xencoding
,
1062 // this should never happen as we had tested for it in the
1063 // very beginning, but if it does, do return something non
1064 // NULL or we'd crash in wxFont code
1067 wxFAIL_MSG( _T("this encoding should be available!") );
1069 font
= wxLoadQueryFont(-1,
1070 wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1071 false, wxEmptyString
,
1083 // ----------------------------------------------------------------------------
1084 // private functions
1085 // ----------------------------------------------------------------------------
1087 // returns true if there are any fonts matching this font spec
1088 static bool wxTestFontSpec(const wxString
& fontspec
)
1090 // some X servers will fail to load this font because there are too many
1091 // matches so we must test explicitly for this
1092 if ( fontspec
== _T("-*-*-*-*-*-*-*-*-*-*-*-*-*-*") )
1097 wxNativeFont test
= (wxNativeFont
) g_fontHash
->Get( fontspec
);
1103 test
= wxLoadFont(fontspec
);
1104 g_fontHash
->Put( fontspec
, (wxObject
*) test
);
1118 static wxNativeFont
wxLoadQueryFont(int pointSize
,
1122 bool WXUNUSED(underlined
),
1123 const wxString
& facename
,
1124 const wxString
& xregistry
,
1125 const wxString
& xencoding
,
1126 wxString
* xFontName
)
1131 case wxDECORATIVE
: xfamily
= wxT("lucida"); break;
1132 case wxROMAN
: xfamily
= wxT("times"); break;
1133 case wxMODERN
: xfamily
= wxT("courier"); break;
1134 case wxSWISS
: xfamily
= wxT("helvetica"); break;
1135 case wxTELETYPE
: xfamily
= wxT("lucidatypewriter"); break;
1136 case wxSCRIPT
: xfamily
= wxT("utopia"); break;
1137 default: xfamily
= wxT("*");
1145 xweight
= MWLF_WEIGHT_BOLD
;
1150 xweight
= MWLF_WEIGHT_LIGHT
;
1155 xweight
= MWLF_WEIGHT_NORMAL
;
1161 xweight
= MWLF_WEIGHT_DEFAULT
;
1165 GR_SCREEN_INFO screenInfo
;
1166 GrGetScreenInfo(& screenInfo
);
1168 int yPixelsPerCM
= screenInfo
.ydpcm
;
1170 // A point is 1/72 of an inch.
1171 // An inch is 2.541 cm.
1172 // So pixelHeight = (pointSize / 72) (inches) * 2.541 (for cm) * yPixelsPerCM (for pixels)
1173 // In fact pointSize is 10 * the normal point size so
1176 int pixelHeight
= (int) ( (((float)pointSize
) / 720.0) * 2.541 * (float) yPixelsPerCM
) ;
1178 // An alternative: assume that the screen is 72 dpi.
1179 //int pixelHeight = (int) (((float)pointSize / 720.0) * 72.0) ;
1180 //int pixelHeight = (int) ((float)pointSize / 10.0) ;
1183 logFont
.lfHeight
= pixelHeight
;
1184 logFont
.lfWidth
= 0;
1185 logFont
.lfEscapement
= 0;
1186 logFont
.lfOrientation
= 0;
1187 logFont
.lfWeight
= xweight
;
1188 logFont
.lfItalic
= (style
== wxNORMAL
? 0 : 1) ;
1189 logFont
.lfUnderline
= 0;
1190 logFont
.lfStrikeOut
= 0;
1191 logFont
.lfCharSet
= MWLF_CHARSET_DEFAULT
; // TODO: select appropriate one
1192 logFont
.lfOutPrecision
= MWLF_TYPE_DEFAULT
;
1193 logFont
.lfClipPrecision
= 0; // Not used
1194 logFont
.lfRoman
= (family
== wxROMAN
? 1 : 0) ;
1195 logFont
.lfSerif
= (family
== wxSWISS
? 0 : 1) ;
1196 logFont
.lfSansSerif
= !logFont
.lfSerif
;
1197 logFont
.lfModern
= (family
== wxMODERN
? 1 : 0) ;
1198 logFont
.lfProportional
= (family
== wxTELETYPE
? 0 : 1) ;
1199 logFont
.lfOblique
= 0;
1200 logFont
.lfSmallCaps
= 0;
1201 logFont
.lfPitch
= 0; // 0 = default
1202 strcpy(logFont
.lfFaceName
, facename
.c_str());
1204 XFontStruct
* fontInfo
= (XFontStruct
*) malloc(sizeof(XFontStruct
));
1205 fontInfo
->fid
= GrCreateFont((GR_CHAR
*) facename
.c_str(), pixelHeight
, & logFont
);
1206 GrGetFontInfo(fontInfo
->fid
, & fontInfo
->info
);
1207 return (wxNativeFont
) fontInfo
;
1211 if (!facename
.empty())
1213 fontSpec
.Printf(wxT("-*-%s-*-*-normal-*-*-*-*-*-*-*-*-*"),
1216 if ( wxTestFontSpec(fontSpec
) )
1220 //else: no such family, use default one instead
1227 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1229 if ( wxTestFontSpec(fontSpec
) )
1234 // fall through - try wxITALIC now
1237 fontSpec
.Printf(wxT("-*-%s-*-i-*-*-*-*-*-*-*-*-*-*"),
1239 if ( wxTestFontSpec(fontSpec
) )
1243 else if ( style
== wxITALIC
) // and not wxSLANT
1246 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1248 if ( wxTestFontSpec(fontSpec
) )
1254 // no italic, no slant - leave default
1261 wxFAIL_MSG(_T("unknown font style"));
1262 // fall back to normal
1274 fontSpec
.Printf(wxT("-*-%s-bold-*-*-*-*-*-*-*-*-*-*-*"),
1276 if ( wxTestFontSpec(fontSpec
) )
1278 xweight
= wxT("bold");
1281 fontSpec
.Printf(wxT("-*-%s-heavy-*-*-*-*-*-*-*-*-*-*-*"),
1283 if ( wxTestFontSpec(fontSpec
) )
1285 xweight
= wxT("heavy");
1288 fontSpec
.Printf(wxT("-*-%s-extrabold-*-*-*-*-*-*-*-*-*-*-*"),
1290 if ( wxTestFontSpec(fontSpec
) )
1292 xweight
= wxT("extrabold");
1295 fontSpec
.Printf(wxT("-*-%s-demibold-*-*-*-*-*-*-*-*-*-*-*"),
1297 if ( wxTestFontSpec(fontSpec
) )
1299 xweight
= wxT("demibold");
1302 fontSpec
.Printf(wxT("-*-%s-black-*-*-*-*-*-*-*-*-*-*-*"),
1304 if ( wxTestFontSpec(fontSpec
) )
1306 xweight
= wxT("black");
1309 fontSpec
.Printf(wxT("-*-%s-ultrablack-*-*-*-*-*-*-*-*-*-*-*"),
1311 if ( wxTestFontSpec(fontSpec
) )
1313 xweight
= wxT("ultrablack");
1320 fontSpec
.Printf(wxT("-*-%s-light-*-*-*-*-*-*-*-*-*-*-*"),
1322 if ( wxTestFontSpec(fontSpec
) )
1324 xweight
= wxT("light");
1327 fontSpec
.Printf(wxT("-*-%s-thin-*-*-*-*-*-*-*-*-*-*-*"),
1329 if ( wxTestFontSpec(fontSpec
) )
1331 xweight
= wxT("thin");
1338 fontSpec
.Printf(wxT("-*-%s-medium-*-*-*-*-*-*-*-*-*-*-*"),
1340 if ( wxTestFontSpec(fontSpec
) )
1342 xweight
= wxT("medium");
1345 fontSpec
.Printf(wxT("-*-%s-normal-*-*-*-*-*-*-*-*-*-*-*"),
1347 if ( wxTestFontSpec(fontSpec
) )
1349 xweight
= wxT("normal");
1352 fontSpec
.Printf(wxT("-*-%s-regular-*-*-*-*-*-*-*-*-*-*-*"),
1354 if ( wxTestFontSpec(fontSpec
) )
1356 xweight
= wxT("regular");
1362 default: xweight
= wxT("*"); break;
1365 // if pointSize is -1, don't specify any
1367 if ( pointSize
== -1 )
1373 sizeSpec
.Printf(_T("%d"), pointSize
);
1376 // construct the X font spec from our data
1377 fontSpec
.Printf(wxT("-*-%s-%s-%s-normal-*-*-%s-*-*-*-*-%s-%s"),
1378 xfamily
.c_str(), xweight
.c_str(), xstyle
.c_str(),
1379 sizeSpec
.c_str(), xregistry
.c_str(), xencoding
.c_str());
1382 *xFontName
= fontSpec
;
1384 return wxLoadFont(fontSpec
);
1389 // ----------------------------------------------------------------------------
1391 // ----------------------------------------------------------------------------
1393 class wxFontModule
: public wxModule
1400 DECLARE_DYNAMIC_CLASS(wxFontModule
)
1403 IMPLEMENT_DYNAMIC_CLASS(wxFontModule
, wxModule
)
1405 bool wxFontModule::OnInit()
1407 g_fontHash
= new wxHashTable( wxKEY_STRING
);
1412 void wxFontModule::OnExit()
1419 #endif // GTK 2.0/1.x