1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/unix/fontutil.cpp
3 // Purpose: Font helper functions for X11 (GDK/X)
4 // Author: Vadim Zeitlin
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #include "wx/fontutil.h"
31 #include "wx/font.h" // wxFont enums
32 #include "wx/encinfo.h"
34 #include "wx/utils.h" // for wxGetDisplay()
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
52 #include "wx/x11/private.h"
53 #include "wx/gtk/private/string.h"
55 #define wxPANGO_CONV(s) (wxConvUTF8.cWX2MB((s)))
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 void wxNativeFontInfo::Init()
68 wxNativeFontInfo::Init(const wxNativeFontInfo
& info
)
71 description
= pango_font_description_copy(info
.description
);
76 void wxNativeFontInfo::Free()
79 pango_font_description_free(description
);
82 int wxNativeFontInfo::GetPointSize() const
84 return pango_font_description_get_size( description
) / PANGO_SCALE
;
87 wxFontStyle
wxNativeFontInfo::GetStyle() const
89 wxFontStyle m_style
= wxFONTSTYLE_NORMAL
;
91 switch (pango_font_description_get_style( description
))
93 case PANGO_STYLE_NORMAL
:
94 m_style
= wxFONTSTYLE_NORMAL
;
96 case PANGO_STYLE_ITALIC
:
97 m_style
= wxFONTSTYLE_ITALIC
;
99 case PANGO_STYLE_OBLIQUE
:
100 m_style
= wxFONTSTYLE_SLANT
;
107 wxFontWeight
wxNativeFontInfo::GetWeight() const
110 // We seem to currently initialize only by string.
111 // In that case PANGO_FONT_MASK_WEIGHT is always set.
112 if (!(pango_font_description_get_set_fields(description
) & PANGO_FONT_MASK_WEIGHT
))
113 return wxFONTWEIGHT_NORMAL
;
116 PangoWeight pango_weight
= pango_font_description_get_weight( description
);
118 // Until the API can be changed the following ranges of weight values are used:
119 // wxFONTWEIGHT_LIGHT: 100 .. 349 - range of 250
120 // wxFONTWEIGHT_NORMAL: 350 .. 599 - range of 250
121 // wxFONTWEIGHT_BOLD: 600 .. 900 - range of 301 (600 is "semibold" already)
123 if (pango_weight
>= 600)
124 return wxFONTWEIGHT_BOLD
;
126 if (pango_weight
< 350)
127 return wxFONTWEIGHT_LIGHT
;
129 return wxFONTWEIGHT_NORMAL
;
132 bool wxNativeFontInfo::GetUnderlined() const
137 wxString
wxNativeFontInfo::GetFaceName() const
139 return wxGTK_CONV_BACK_SYS(pango_font_description_get_family(description
));
142 wxFontFamily
wxNativeFontInfo::GetFamily() const
144 wxFontFamily ret
= wxFONTFAMILY_DEFAULT
;
145 // note: not passing -1 as the 2nd parameter to g_ascii_strdown to work
146 // around a bug in the 64-bit glib shipped with solaris 10, -1 causes it
147 // to try to allocate 2^32 bytes.
148 const char *family_name
= pango_font_description_get_family( description
);
152 wxGtkString
family_text(g_ascii_strdown(family_name
, strlen(family_name
)));
154 // Check for some common fonts, to salvage what we can from the current win32 centric wxFont API:
155 if (strncmp( family_text
, "monospace", 9 ) == 0)
156 ret
= wxFONTFAMILY_TELETYPE
; // begins with "Monospace"
157 else if (strncmp( family_text
, "courier", 7 ) == 0)
158 ret
= wxFONTFAMILY_TELETYPE
; // begins with "Courier"
159 #if defined(__WXGTK24__) || defined(HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE)
162 if (!gtk_check_version(2,4,0))
165 PangoFontFamily
**families
;
166 PangoFontFamily
*family
= NULL
;
168 pango_context_list_families(
170 gtk_widget_get_pango_context( wxGetRootWindow() ),
172 wxTheApp
->GetPangoContext(),
174 &families
, &n_families
);
176 for (int i
= 0;i
< n_families
;++i
)
178 if (g_ascii_strcasecmp(pango_font_family_get_name( families
[i
] ), pango_font_description_get_family( description
)) == 0 )
180 family
= families
[i
];
187 // Some gtk+ systems might query for a non-existing font from wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT)
188 // on initialization, don't assert until wxSystemSettings::GetFont is checked for this - MR
189 // wxASSERT_MSG( family, wxT("wxNativeFontInfo::GetFamily() - No appropriate PangoFontFamily found for ::description") );
191 //BCI: Cache the wxFontFamily inside the class. Validate cache with
192 //BCI: g_ascii_strcasecmp(pango_font_description_get_family(description), pango_font_family_get_name(family)) == 0
194 if (family
!= NULL
&& pango_font_family_is_monospace( family
))
195 ret
= wxFONTFAMILY_TELETYPE
; // is deemed a monospace font by pango
197 #endif // gtk24 || HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE
199 if (ret
== wxFONTFAMILY_DEFAULT
)
201 if (strstr( family_text
, "sans" ) != NULL
) // checked before serif, so that "* Sans Serif" fonts are detected correctly
202 ret
= wxFONTFAMILY_SWISS
; // contains "Sans"
203 else if (strstr( family_text
, "serif" ) != NULL
)
204 ret
= wxFONTFAMILY_ROMAN
; // contains "Serif"
205 else if (strncmp( family_text
, "times", 5 ) == 0)
206 ret
= wxFONTFAMILY_ROMAN
; // begins with "Times"
207 else if (strncmp( family_text
, "old", 3 ) == 0)
208 ret
= wxFONTFAMILY_DECORATIVE
; // Begins with "Old" - "Old English", "Old Town"
214 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
216 return wxFONTENCODING_SYSTEM
;
220 void wxNativeFontInfo::SetPointSize(int pointsize
)
222 pango_font_description_set_size( description
, pointsize
* PANGO_SCALE
);
225 void wxNativeFontInfo::SetStyle(wxFontStyle style
)
229 case wxFONTSTYLE_ITALIC
:
230 pango_font_description_set_style( description
, PANGO_STYLE_ITALIC
);
232 case wxFONTSTYLE_SLANT
:
233 pango_font_description_set_style( description
, PANGO_STYLE_OBLIQUE
);
236 wxFAIL_MSG( _T("unknown font style") );
238 case wxFONTSTYLE_NORMAL
:
239 pango_font_description_set_style( description
, PANGO_STYLE_NORMAL
);
244 void wxNativeFontInfo::SetWeight(wxFontWeight weight
)
248 case wxFONTWEIGHT_BOLD
:
249 pango_font_description_set_weight(description
, PANGO_WEIGHT_BOLD
);
251 case wxFONTWEIGHT_LIGHT
:
252 pango_font_description_set_weight(description
, PANGO_WEIGHT_LIGHT
);
255 wxFAIL_MSG( _T("unknown font weight") );
257 case wxFONTWEIGHT_NORMAL
:
258 pango_font_description_set_weight(description
, PANGO_WEIGHT_NORMAL
);
262 void wxNativeFontInfo::SetUnderlined(bool WXUNUSED(underlined
))
264 wxFAIL_MSG( _T("not implemented") );
267 bool wxNativeFontInfo::SetFaceName(const wxString
& facename
)
269 pango_font_description_set_family(description
, wxPANGO_CONV(facename
));
273 void wxNativeFontInfo::SetFamily(wxFontFamily
WXUNUSED(family
))
275 wxFAIL_MSG( _T("not implemented") );
278 void wxNativeFontInfo::SetEncoding(wxFontEncoding
WXUNUSED(encoding
))
280 wxFAIL_MSG( _T("not implemented") );
285 bool wxNativeFontInfo::FromString(const wxString
& s
)
288 pango_font_description_free( description
);
290 // there is a bug in at least pango <= 1.13 which makes it (or its backends)
291 // segfault for very big point sizes and for negative point sizes.
292 // To workaround that bug for pango <= 1.13
293 // (see http://bugzilla.gnome.org/show_bug.cgi?id=340229)
294 // we do the check on the size here using same (arbitrary) limits used by
295 // pango > 1.13. Note that the segfault could happen also for pointsize
296 // smaller than this limit !!
298 const size_t pos
= str
.find_last_of(_T(" "));
300 if ( pos
!= wxString::npos
&& wxString(str
, pos
+ 1).ToDouble(&size
) )
305 else if ( size
>= 1E6
)
308 if ( !sizeStr
.empty() )
310 // replace the old size with the adjusted one
311 str
= wxString(s
, 0, pos
) + sizeStr
;
315 description
= pango_font_description_from_string(wxPANGO_CONV(str
));
317 // ensure a valid facename is selected
318 if (!wxFontEnumerator::IsValidFacename(GetFaceName()))
319 SetFaceName(wxNORMAL_FONT
->GetFaceName());
324 wxString
wxNativeFontInfo::ToString() const
326 wxGtkString
str(pango_font_description_to_string( description
));
328 return wxGTK_CONV_BACK_SYS(str
);
331 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
333 return FromString( s
);
336 wxString
wxNativeFontInfo::ToUserString() const
345 #pragma message disable nosimpint
348 #include <X11/Xlib.h>
351 #pragma message enable nosimpint
354 #elif defined(__WXGTK__)
355 // we have to declare struct tm to avoid problems with first forward
356 // declaring it in C code (glib.h included from gdk.h does it) and then
357 // defining it when time.h is included from the headers below - this is
358 // known not to work at least with Sun CC 6.01
365 // ----------------------------------------------------------------------------
367 // ----------------------------------------------------------------------------
369 static wxHashTable
*g_fontHash
= (wxHashTable
*) NULL
;
371 // ----------------------------------------------------------------------------
373 // ----------------------------------------------------------------------------
375 // define the functions to create and destroy native fonts for this toolkit
377 wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
379 return XLoadQueryFont((Display
*)wxGetDisplay(), fontSpec
);
382 inline void wxFreeFont(wxNativeFont font
)
384 XFreeFont((Display
*)wxGetDisplay(), (XFontStruct
*)font
);
386 #elif defined(__WXGTK__)
387 wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
389 // VZ: we should use gdk_fontset_load() instead of gdk_font_load()
390 // here to be able to display Japanese fonts correctly (at least
391 // this is what people report) but unfortunately doing it results
392 // in tons of warnings when using GTK with "normal" European
393 // languages and so we can't always do it and I don't know enough
394 // to determine when should this be done... (FIXME)
395 return gdk_font_load( wxConvertWX2MB(fontSpec
) );
398 inline void wxFreeFont(wxNativeFont font
)
400 gdk_font_unref(font
);
403 #error "Unknown GUI toolkit"
406 static bool wxTestFontSpec(const wxString
& fontspec
);
408 static wxNativeFont
wxLoadQueryFont(int pointSize
,
413 const wxString
& facename
,
414 const wxString
& xregistry
,
415 const wxString
& xencoding
,
416 wxString
* xFontName
);
418 // ============================================================================
420 // ============================================================================
422 // ----------------------------------------------------------------------------
423 // wxNativeEncodingInfo
424 // ----------------------------------------------------------------------------
426 // convert to/from the string representation: format is
427 // encodingid;registry;encoding[;facename]
428 bool wxNativeEncodingInfo::FromString(const wxString
& s
)
430 // use ";", not "-" because it may be part of encoding name
431 wxStringTokenizer
tokenizer(s
, _T(";"));
433 wxString encid
= tokenizer
.GetNextToken();
435 if ( !encid
.ToLong(&enc
) )
437 encoding
= (wxFontEncoding
)enc
;
439 xregistry
= tokenizer
.GetNextToken();
443 xencoding
= tokenizer
.GetNextToken();
448 facename
= tokenizer
.GetNextToken();
453 wxString
wxNativeEncodingInfo::ToString() const
456 s
<< (long)encoding
<< _T(';') << xregistry
<< _T(';') << xencoding
;
457 if ( !facename
.empty() )
459 s
<< _T(';') << facename
;
465 // ----------------------------------------------------------------------------
467 // ----------------------------------------------------------------------------
469 void wxNativeFontInfo::Init()
474 bool wxNativeFontInfo::FromString(const wxString
& s
)
476 wxStringTokenizer
tokenizer(s
, _T(";"));
479 wxString token
= tokenizer
.GetNextToken();
480 if ( token
!= _T('0') )
483 xFontName
= tokenizer
.GetNextToken();
485 // this should be the end
486 if ( tokenizer
.HasMoreTokens() )
489 return FromXFontName(xFontName
);
492 wxString
wxNativeFontInfo::ToString() const
495 return wxString::Format(_T("%d;%s"), 0, GetXFontName().c_str());
498 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
500 return FromXFontName(s
);
503 wxString
wxNativeFontInfo::ToUserString() const
505 return GetXFontName();
508 bool wxNativeFontInfo::HasElements() const
510 // we suppose that the foundry is never empty, so if it is it means that we
511 // had never parsed the XLFD
512 return !fontElements
[0].empty();
515 wxString
wxNativeFontInfo::GetXFontComponent(wxXLFDField field
) const
517 wxCHECK_MSG( field
< wxXLFD_MAX
, wxEmptyString
, _T("invalid XLFD field") );
519 if ( !HasElements() )
522 if ( !((wxNativeFontInfo
*)this)->FromXFontName(xFontName
) )
523 return wxEmptyString
;
526 return fontElements
[field
];
529 bool wxNativeFontInfo::FromXFontName(const wxString
& fontname
)
531 // TODO: we should be able to handle the font aliases here, but how?
532 wxStringTokenizer
tokenizer(fontname
, _T("-"));
534 // skip the leading, usually empty field (font name registry)
535 if ( !tokenizer
.HasMoreTokens() )
538 (void)tokenizer
.GetNextToken();
540 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
542 if ( !tokenizer
.HasMoreTokens() )
544 // not enough elements in the XLFD - or maybe an alias
548 wxString field
= tokenizer
.GetNextToken();
549 if ( !field
.empty() && field
!= _T('*') )
551 // we're really initialized now
555 fontElements
[n
] = field
;
558 // this should be all
559 if ( tokenizer
.HasMoreTokens() )
565 wxString
wxNativeFontInfo::GetXFontName() const
567 if ( xFontName
.empty() )
569 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
571 // replace the non specified elements with '*' except for the
572 // additional style which is usually just omitted
573 wxString elt
= fontElements
[n
];
574 if ( elt
.empty() && n
!= wxXLFD_ADDSTYLE
)
580 ((wxNativeFontInfo
*)this)->xFontName
<< _T('-') << elt
;
588 wxNativeFontInfo::SetXFontComponent(wxXLFDField field
, const wxString
& value
)
590 wxCHECK_RET( field
< wxXLFD_MAX
, _T("invalid XLFD field") );
592 // this class should be initialized with a valid font spec first and only
593 // then the fields may be modified!
594 wxASSERT_MSG( !IsDefault(), _T("can't modify an uninitialized XLFD") );
596 if ( !HasElements() )
599 if ( !((wxNativeFontInfo
*)this)->FromXFontName(xFontName
) )
601 wxFAIL_MSG( _T("can't set font element for invalid XLFD") );
607 fontElements
[field
] = value
;
609 // invalidate the XFLD, it doesn't correspond to the font elements any more
613 void wxNativeFontInfo::SetXFontName(const wxString
& xFontName_
)
615 // invalidate the font elements, GetXFontComponent() will reparse the XLFD
616 fontElements
[0].clear();
618 xFontName
= xFontName_
;
623 int wxNativeFontInfo::GetPointSize() const
625 const wxString s
= GetXFontComponent(wxXLFD_POINTSIZE
);
627 // return -1 to indicate that the size is unknown
629 return s
.ToLong(&l
) ? l
: -1;
632 wxFontStyle
wxNativeFontInfo::GetStyle() const
634 const wxString s
= GetXFontComponent(wxXLFD_SLANT
);
636 if ( s
.length() != 1 )
638 // it is really unknown but we don't have any way to return it from
640 return wxFONTSTYLE_NORMAL
;
646 // again, unknown but consider normal by default
649 return wxFONTSTYLE_NORMAL
;
652 return wxFONTSTYLE_ITALIC
;
655 return wxFONTSTYLE_SLANT
;
659 wxFontWeight
wxNativeFontInfo::GetWeight() const
661 const wxString s
= GetXFontComponent(wxXLFD_WEIGHT
).MakeLower();
662 if ( s
.find(_T("bold")) != wxString::npos
|| s
== _T("black") )
663 return wxFONTWEIGHT_BOLD
;
664 else if ( s
== _T("light") )
665 return wxFONTWEIGHT_LIGHT
;
667 return wxFONTWEIGHT_NORMAL
;
670 bool wxNativeFontInfo::GetUnderlined() const
672 // X fonts are never underlined
676 wxString
wxNativeFontInfo::GetFaceName() const
678 // wxWidgets facename probably more accurately corresponds to X family
679 return GetXFontComponent(wxXLFD_FAMILY
);
682 wxFontFamily
wxNativeFontInfo::GetFamily() const
684 // and wxWidgets family -- to X foundry, but we have to translate it to
685 // wxFontFamily somehow...
686 wxFAIL_MSG(_T("not implemented")); // GetXFontComponent(wxXLFD_FOUNDRY);
688 return wxFONTFAMILY_DEFAULT
;
691 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
693 // we already have the code for this but need to refactor it first
694 wxFAIL_MSG( _T("not implemented") );
696 return wxFONTENCODING_MAX
;
699 void wxNativeFontInfo::SetPointSize(int pointsize
)
701 SetXFontComponent(wxXLFD_POINTSIZE
, wxString::Format(_T("%d"), pointsize
));
704 void wxNativeFontInfo::SetStyle(wxFontStyle style
)
709 case wxFONTSTYLE_ITALIC
:
713 case wxFONTSTYLE_SLANT
:
717 case wxFONTSTYLE_NORMAL
:
721 wxFAIL_MSG( _T("unknown wxFontStyle in wxNativeFontInfo::SetStyle") );
725 SetXFontComponent(wxXLFD_SLANT
, s
);
728 void wxNativeFontInfo::SetWeight(wxFontWeight weight
)
733 case wxFONTWEIGHT_BOLD
:
737 case wxFONTWEIGHT_LIGHT
:
741 case wxFONTWEIGHT_NORMAL
:
746 wxFAIL_MSG( _T("unknown wxFontWeight in wxNativeFontInfo::SetWeight") );
750 SetXFontComponent(wxXLFD_WEIGHT
, s
);
753 void wxNativeFontInfo::SetUnderlined(bool WXUNUSED(underlined
))
755 // can't do this under X
758 bool wxNativeFontInfo::SetFaceName(const wxString
& facename
)
760 SetXFontComponent(wxXLFD_FAMILY
, facename
);
764 void wxNativeFontInfo::SetFamily(wxFontFamily
WXUNUSED(family
))
766 // wxFontFamily -> X foundry, anyone?
767 wxFAIL_MSG( _T("not implemented") );
769 // SetXFontComponent(wxXLFD_FOUNDRY, ...);
772 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding
)
774 wxNativeEncodingInfo info
;
775 if ( wxGetNativeFontEncoding(encoding
, &info
) )
777 SetXFontComponent(wxXLFD_ENCODING
, info
.xencoding
);
778 SetXFontComponent(wxXLFD_REGISTRY
, info
.xregistry
);
782 // ----------------------------------------------------------------------------
784 // ----------------------------------------------------------------------------
786 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
787 wxNativeEncodingInfo
*info
)
789 wxCHECK_MSG( info
, false, _T("bad pointer in wxGetNativeFontEncoding") );
791 if ( encoding
== wxFONTENCODING_DEFAULT
)
793 encoding
= wxFont::GetDefaultEncoding();
798 case wxFONTENCODING_ISO8859_1
:
799 case wxFONTENCODING_ISO8859_2
:
800 case wxFONTENCODING_ISO8859_3
:
801 case wxFONTENCODING_ISO8859_4
:
802 case wxFONTENCODING_ISO8859_5
:
803 case wxFONTENCODING_ISO8859_6
:
804 case wxFONTENCODING_ISO8859_7
:
805 case wxFONTENCODING_ISO8859_8
:
806 case wxFONTENCODING_ISO8859_9
:
807 case wxFONTENCODING_ISO8859_10
:
808 case wxFONTENCODING_ISO8859_11
:
809 case wxFONTENCODING_ISO8859_12
:
810 case wxFONTENCODING_ISO8859_13
:
811 case wxFONTENCODING_ISO8859_14
:
812 case wxFONTENCODING_ISO8859_15
:
814 int cp
= encoding
- wxFONTENCODING_ISO8859_1
+ 1;
815 info
->xregistry
= wxT("iso8859");
816 info
->xencoding
.Printf(wxT("%d"), cp
);
820 case wxFONTENCODING_UTF8
:
821 info
->xregistry
= wxT("iso10646");
822 info
->xencoding
= wxT("*");
825 case wxFONTENCODING_GB2312
:
826 info
->xregistry
= wxT("GB2312"); // or the otherway round?
827 info
->xencoding
= wxT("*");
830 case wxFONTENCODING_KOI8
:
831 case wxFONTENCODING_KOI8_U
:
832 info
->xregistry
= wxT("koi8");
834 // we don't make distinction between koi8-r, koi8-u and koi8-ru (so far)
835 info
->xencoding
= wxT("*");
838 case wxFONTENCODING_CP1250
:
839 case wxFONTENCODING_CP1251
:
840 case wxFONTENCODING_CP1252
:
841 case wxFONTENCODING_CP1253
:
842 case wxFONTENCODING_CP1254
:
843 case wxFONTENCODING_CP1255
:
844 case wxFONTENCODING_CP1256
:
845 case wxFONTENCODING_CP1257
:
847 int cp
= encoding
- wxFONTENCODING_CP1250
+ 1250;
848 info
->xregistry
= wxT("microsoft");
849 info
->xencoding
.Printf(wxT("cp%d"), cp
);
853 case wxFONTENCODING_EUC_JP
:
854 case wxFONTENCODING_SHIFT_JIS
:
855 info
->xregistry
= "jis*";
856 info
->xencoding
= "*";
859 case wxFONTENCODING_SYSTEM
:
861 info
->xencoding
= wxT("*");
865 // don't know how to translate this encoding into X fontspec
869 info
->encoding
= encoding
;
874 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
877 fontspec
.Printf(_T("-*-%s-*-*-*-*-*-*-*-*-*-*-%s-%s"),
878 !info
.facename
? _T("*") : info
.facename
.c_str(),
879 info
.xregistry
.c_str(),
880 info
.xencoding
.c_str());
882 return wxTestFontSpec(fontspec
);
885 // ----------------------------------------------------------------------------
886 // X-specific functions
887 // ----------------------------------------------------------------------------
889 wxNativeFont
wxLoadQueryNearestFont(int pointSize
,
894 const wxString
&facename
,
895 wxFontEncoding encoding
,
898 if ( encoding
== wxFONTENCODING_DEFAULT
)
900 encoding
= wxFont::GetDefaultEncoding();
903 // first determine the encoding - if the font doesn't exist at all in this
904 // encoding, it's useless to do all other approximations (i.e. size,
905 // family &c don't matter much)
906 wxNativeEncodingInfo info
;
907 if ( encoding
== wxFONTENCODING_SYSTEM
)
909 // This will always work so we don't test to save time
910 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
914 if ( !wxGetNativeFontEncoding(encoding
, &info
) ||
915 !wxTestFontEncoding(info
) )
918 if ( !wxFontMapper::Get()->GetAltForEncoding(encoding
, &info
) )
919 #endif // wxUSE_FONTMAP
921 // unspported encoding - replace it with the default
923 // NB: we can't just return 0 from here because wxGTK code doesn't
924 // check for it (i.e. it supposes that we'll always succeed),
925 // so it would provoke a crash
926 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
931 // OK, we have the correct xregistry/xencoding in info structure
932 wxNativeFont font
= 0;
934 // if we already have the X font name, try to use it
935 if( xFontName
&& !xFontName
->empty() )
938 // Make sure point size is correct for scale factor.
940 wxStringTokenizer
tokenizer(*xFontName
, _T("-"), wxTOKEN_RET_DELIMS
);
941 wxString newFontName
;
943 for(int i
= 0; i
< 8; i
++)
944 newFontName
+= tokenizer
.NextToken();
946 (void) tokenizer
.NextToken();
948 newFontName
+= wxString::Format(wxT("%d-"), pointSize
);
950 while(tokenizer
.HasMoreTokens())
951 newFontName
+= tokenizer
.GetNextToken();
953 font
= wxLoadFont(newFontName
);
956 *xFontName
= newFontName
;
961 // search up and down by stepsize 10
962 int max_size
= pointSize
+ 20 * (1 + (pointSize
/180));
963 int min_size
= pointSize
- 20 * (1 + (pointSize
/180));
965 int i
, round
; // counters
967 // first round: search for equal, then for smaller and for larger size with the given weight and style
968 int testweight
= weight
;
969 int teststyle
= style
;
971 for ( round
= 0; round
< 3; round
++ )
973 // second round: use normal weight
976 if ( testweight
!= wxNORMAL
)
978 testweight
= wxNORMAL
;
982 ++round
; // fall through to third round
986 // third round: ... and use normal style
989 if ( teststyle
!= wxNORMAL
)
991 teststyle
= wxNORMAL
;
998 // Search for equal or smaller size (approx.)
999 for ( i
= pointSize
; !font
&& i
>= 10 && i
>= min_size
; i
-= 10 )
1001 font
= wxLoadQueryFont(i
, family
, teststyle
, testweight
, underlined
,
1002 facename
, info
.xregistry
, info
.xencoding
,
1006 // Search for larger size (approx.)
1007 for ( i
= pointSize
+ 10; !font
&& i
<= max_size
; i
+= 10 )
1009 font
= wxLoadQueryFont(i
, family
, teststyle
, testweight
, underlined
,
1010 facename
, info
.xregistry
, info
.xencoding
,
1015 // Try default family
1016 if ( !font
&& family
!= wxDEFAULT
)
1018 font
= wxLoadQueryFont(pointSize
, wxDEFAULT
, style
, weight
,
1019 underlined
, facename
,
1020 info
.xregistry
, info
.xencoding
,
1024 // ignore size, family, style and weight but try to find font with the
1025 // given facename and encoding
1028 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1029 underlined
, facename
,
1030 info
.xregistry
, info
.xencoding
,
1033 // ignore family as well
1036 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1037 underlined
, wxEmptyString
,
1038 info
.xregistry
, info
.xencoding
,
1041 // if it still failed, try to get the font of any size but
1042 // with the requested encoding: this can happen if the
1043 // encoding is only available in one size which happens to be
1044 // different from 120
1047 font
= wxLoadQueryFont(-1, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1048 false, wxEmptyString
,
1049 info
.xregistry
, info
.xencoding
,
1052 // this should never happen as we had tested for it in the
1053 // very beginning, but if it does, do return something non
1054 // NULL or we'd crash in wxFont code
1057 wxFAIL_MSG( _T("this encoding should be available!") );
1059 font
= wxLoadQueryFont(-1,
1060 wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1061 false, wxEmptyString
,
1073 // ----------------------------------------------------------------------------
1074 // private functions
1075 // ----------------------------------------------------------------------------
1077 // returns true if there are any fonts matching this font spec
1078 static bool wxTestFontSpec(const wxString
& fontspec
)
1080 // some X servers will fail to load this font because there are too many
1081 // matches so we must test explicitly for this
1082 if ( fontspec
== _T("-*-*-*-*-*-*-*-*-*-*-*-*-*-*") )
1087 wxNativeFont test
= (wxNativeFont
) g_fontHash
->Get( fontspec
);
1093 test
= wxLoadFont(fontspec
);
1094 g_fontHash
->Put( fontspec
, (wxObject
*) test
);
1108 static wxNativeFont
wxLoadQueryFont(int pointSize
,
1112 bool WXUNUSED(underlined
),
1113 const wxString
& facename
,
1114 const wxString
& xregistry
,
1115 const wxString
& xencoding
,
1116 wxString
* xFontName
)
1121 case wxDECORATIVE
: xfamily
= wxT("lucida"); break;
1122 case wxROMAN
: xfamily
= wxT("times"); break;
1123 case wxMODERN
: xfamily
= wxT("courier"); break;
1124 case wxSWISS
: xfamily
= wxT("helvetica"); break;
1125 case wxTELETYPE
: xfamily
= wxT("lucidatypewriter"); break;
1126 case wxSCRIPT
: xfamily
= wxT("utopia"); break;
1127 default: xfamily
= wxT("*");
1135 xweight
= MWLF_WEIGHT_BOLD
;
1140 xweight
= MWLF_WEIGHT_LIGHT
;
1145 xweight
= MWLF_WEIGHT_NORMAL
;
1151 xweight
= MWLF_WEIGHT_DEFAULT
;
1155 GR_SCREEN_INFO screenInfo
;
1156 GrGetScreenInfo(& screenInfo
);
1158 int yPixelsPerCM
= screenInfo
.ydpcm
;
1160 // A point is 1/72 of an inch.
1161 // An inch is 2.541 cm.
1162 // So pixelHeight = (pointSize / 72) (inches) * 2.541 (for cm) * yPixelsPerCM (for pixels)
1163 // In fact pointSize is 10 * the normal point size so
1166 int pixelHeight
= (int) ( (((float)pointSize
) / 720.0) * 2.541 * (float) yPixelsPerCM
) ;
1168 // An alternative: assume that the screen is 72 dpi.
1169 //int pixelHeight = (int) (((float)pointSize / 720.0) * 72.0) ;
1170 //int pixelHeight = (int) ((float)pointSize / 10.0) ;
1173 logFont
.lfHeight
= pixelHeight
;
1174 logFont
.lfWidth
= 0;
1175 logFont
.lfEscapement
= 0;
1176 logFont
.lfOrientation
= 0;
1177 logFont
.lfWeight
= xweight
;
1178 logFont
.lfItalic
= (style
== wxNORMAL
? 0 : 1) ;
1179 logFont
.lfUnderline
= 0;
1180 logFont
.lfStrikeOut
= 0;
1181 logFont
.lfCharSet
= MWLF_CHARSET_DEFAULT
; // TODO: select appropriate one
1182 logFont
.lfOutPrecision
= MWLF_TYPE_DEFAULT
;
1183 logFont
.lfClipPrecision
= 0; // Not used
1184 logFont
.lfRoman
= (family
== wxROMAN
? 1 : 0) ;
1185 logFont
.lfSerif
= (family
== wxSWISS
? 0 : 1) ;
1186 logFont
.lfSansSerif
= !logFont
.lfSerif
;
1187 logFont
.lfModern
= (family
== wxMODERN
? 1 : 0) ;
1188 logFont
.lfProportional
= (family
== wxTELETYPE
? 0 : 1) ;
1189 logFont
.lfOblique
= 0;
1190 logFont
.lfSmallCaps
= 0;
1191 logFont
.lfPitch
= 0; // 0 = default
1192 strcpy(logFont
.lfFaceName
, facename
.c_str());
1194 XFontStruct
* fontInfo
= (XFontStruct
*) malloc(sizeof(XFontStruct
));
1195 fontInfo
->fid
= GrCreateFont((GR_CHAR
*) facename
.c_str(), pixelHeight
, & logFont
);
1196 GrGetFontInfo(fontInfo
->fid
, & fontInfo
->info
);
1197 return (wxNativeFont
) fontInfo
;
1201 if (!facename
.empty())
1203 fontSpec
.Printf(wxT("-*-%s-*-*-normal-*-*-*-*-*-*-*-*-*"),
1206 if ( wxTestFontSpec(fontSpec
) )
1210 //else: no such family, use default one instead
1217 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1219 if ( wxTestFontSpec(fontSpec
) )
1224 // fall through - try wxITALIC now
1227 fontSpec
.Printf(wxT("-*-%s-*-i-*-*-*-*-*-*-*-*-*-*"),
1229 if ( wxTestFontSpec(fontSpec
) )
1233 else if ( style
== wxITALIC
) // and not wxSLANT
1236 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1238 if ( wxTestFontSpec(fontSpec
) )
1244 // no italic, no slant - leave default
1251 wxFAIL_MSG(_T("unknown font style"));
1252 // fall back to normal
1264 fontSpec
.Printf(wxT("-*-%s-bold-*-*-*-*-*-*-*-*-*-*-*"),
1266 if ( wxTestFontSpec(fontSpec
) )
1268 xweight
= wxT("bold");
1271 fontSpec
.Printf(wxT("-*-%s-heavy-*-*-*-*-*-*-*-*-*-*-*"),
1273 if ( wxTestFontSpec(fontSpec
) )
1275 xweight
= wxT("heavy");
1278 fontSpec
.Printf(wxT("-*-%s-extrabold-*-*-*-*-*-*-*-*-*-*-*"),
1280 if ( wxTestFontSpec(fontSpec
) )
1282 xweight
= wxT("extrabold");
1285 fontSpec
.Printf(wxT("-*-%s-demibold-*-*-*-*-*-*-*-*-*-*-*"),
1287 if ( wxTestFontSpec(fontSpec
) )
1289 xweight
= wxT("demibold");
1292 fontSpec
.Printf(wxT("-*-%s-black-*-*-*-*-*-*-*-*-*-*-*"),
1294 if ( wxTestFontSpec(fontSpec
) )
1296 xweight
= wxT("black");
1299 fontSpec
.Printf(wxT("-*-%s-ultrablack-*-*-*-*-*-*-*-*-*-*-*"),
1301 if ( wxTestFontSpec(fontSpec
) )
1303 xweight
= wxT("ultrablack");
1310 fontSpec
.Printf(wxT("-*-%s-light-*-*-*-*-*-*-*-*-*-*-*"),
1312 if ( wxTestFontSpec(fontSpec
) )
1314 xweight
= wxT("light");
1317 fontSpec
.Printf(wxT("-*-%s-thin-*-*-*-*-*-*-*-*-*-*-*"),
1319 if ( wxTestFontSpec(fontSpec
) )
1321 xweight
= wxT("thin");
1328 fontSpec
.Printf(wxT("-*-%s-medium-*-*-*-*-*-*-*-*-*-*-*"),
1330 if ( wxTestFontSpec(fontSpec
) )
1332 xweight
= wxT("medium");
1335 fontSpec
.Printf(wxT("-*-%s-normal-*-*-*-*-*-*-*-*-*-*-*"),
1337 if ( wxTestFontSpec(fontSpec
) )
1339 xweight
= wxT("normal");
1342 fontSpec
.Printf(wxT("-*-%s-regular-*-*-*-*-*-*-*-*-*-*-*"),
1344 if ( wxTestFontSpec(fontSpec
) )
1346 xweight
= wxT("regular");
1352 default: xweight
= wxT("*"); break;
1355 // if pointSize is -1, don't specify any
1357 if ( pointSize
== -1 )
1363 sizeSpec
.Printf(_T("%d"), pointSize
);
1366 // construct the X font spec from our data
1367 fontSpec
.Printf(wxT("-*-%s-%s-%s-normal-*-*-%s-*-*-*-*-%s-%s"),
1368 xfamily
.c_str(), xweight
.c_str(), xstyle
.c_str(),
1369 sizeSpec
.c_str(), xregistry
.c_str(), xencoding
.c_str());
1372 *xFontName
= fontSpec
;
1374 return wxLoadFont(fontSpec
);
1379 // ----------------------------------------------------------------------------
1381 // ----------------------------------------------------------------------------
1383 class wxFontModule
: public wxModule
1390 DECLARE_DYNAMIC_CLASS(wxFontModule
)
1393 IMPLEMENT_DYNAMIC_CLASS(wxFontModule
, wxModule
)
1395 bool wxFontModule::OnInit()
1397 g_fontHash
= new wxHashTable( wxKEY_STRING
);
1402 void wxFontModule::OnExit()
1406 g_fontHash
= (wxHashTable
*)NULL
;
1409 #endif // GTK 2.0/1.x