1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/unix/fontutil.cpp
3 // Purpose: Font helper functions for X11 (GDK/X)
4 // Author: Vadim Zeitlin
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
28 #include "wx/font.h" // wxFont enums
29 #include "wx/encinfo.h"
31 #include "wx/utils.h" // for wxGetDisplay()
34 #include "wx/fontutil.h"
35 #include "wx/fontmap.h"
36 #include "wx/tokenzr.h"
37 #include "wx/module.h"
41 #include "pango/pango.h"
44 #include "wx/gtk/private.h"
45 extern GtkWidget
*wxGetRootWindow();
47 #include "wx/x11/private.h"
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 void wxNativeFontInfo::Init()
60 wxNativeFontInfo::Init(const wxNativeFontInfo
& info
)
63 description
= pango_font_description_copy(info
.description
);
68 void wxNativeFontInfo::Free()
71 pango_font_description_free(description
);
74 int wxNativeFontInfo::GetPointSize() const
76 return pango_font_description_get_size( description
) / PANGO_SCALE
;
79 wxFontStyle
wxNativeFontInfo::GetStyle() const
81 wxFontStyle m_style
= wxFONTSTYLE_NORMAL
;
83 switch (pango_font_description_get_style( description
))
85 case PANGO_STYLE_NORMAL
:
86 m_style
= wxFONTSTYLE_NORMAL
;
88 case PANGO_STYLE_ITALIC
:
89 m_style
= wxFONTSTYLE_ITALIC
;
91 case PANGO_STYLE_OBLIQUE
:
92 m_style
= wxFONTSTYLE_SLANT
;
99 wxFontWeight
wxNativeFontInfo::GetWeight() const
102 // We seem to currently initialize only by string.
103 // In that case PANGO_FONT_MASK_WEIGHT is always set.
104 if (!(pango_font_description_get_set_fields(description
) & PANGO_FONT_MASK_WEIGHT
))
105 return wxFONTWEIGHT_NORMAL
;
108 PangoWeight pango_weight
= pango_font_description_get_weight( description
);
110 // Until the API can be changed the following ranges of weight values are used:
111 // wxFONTWEIGHT_LIGHT: 100 .. 349 - range of 250
112 // wxFONTWEIGHT_NORMAL: 350 .. 599 - range of 250
113 // wxFONTWEIGHT_BOLD: 600 .. 900 - range of 301 (600 is "semibold" already)
115 if (pango_weight
>= 600)
116 return wxFONTWEIGHT_BOLD
;
118 if (pango_weight
< 350)
119 return wxFONTWEIGHT_LIGHT
;
121 return wxFONTWEIGHT_NORMAL
;
124 bool wxNativeFontInfo::GetUnderlined() const
129 wxString
wxNativeFontInfo::GetFaceName() const
131 wxString tmp
= wxGTK_CONV_BACK( pango_font_description_get_family( description
) );
136 wxFontFamily
wxNativeFontInfo::GetFamily() const
138 wxFontFamily ret
= wxFONTFAMILY_DEFAULT
;
139 // note: not passing -1 as the 2nd parameter to g_ascii_strdown to work
140 // around a bug in the 64-bit glib shipped with solaris 10, -1 causes it
141 // to try to allocate 2^32 bytes.
142 const char *family_name
= pango_font_description_get_family( description
);
143 char *family_text
= g_ascii_strdown( family_name
, family_name
? strlen( family_name
) : 0 );
144 // Check for some common fonts, to salvage what we can from the current win32 centric wxFont API:
145 if (strncmp( family_text
, "monospace", 9 ) == 0)
146 ret
= wxFONTFAMILY_TELETYPE
; // begins with "Monospace"
147 else if (strncmp( family_text
, "courier", 7 ) == 0)
148 ret
= wxFONTFAMILY_TELETYPE
; // begins with "Courier"
149 #if defined(__WXGTK24__) || defined(HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE)
152 if (!gtk_check_version(2,4,0))
155 PangoFontFamily
**families
;
156 PangoFontFamily
*family
= NULL
;
158 pango_context_list_families(
160 gtk_widget_get_pango_context( wxGetRootWindow() ),
162 wxTheApp
->GetPangoContext(),
164 &families
, &n_families
);
166 for (int i
= 0;i
< n_families
;++i
)
168 if (g_ascii_strcasecmp(pango_font_family_get_name( families
[i
] ), pango_font_description_get_family( description
)) == 0 )
170 family
= families
[i
];
177 // Some gtk+ systems might query for a non-existing font from wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT)
178 // on initialization, don't assert until wxSystemSettings::GetFont is checked for this - MR
179 // wxASSERT_MSG( family, wxT("wxNativeFontInfo::GetFamily() - No appropriate PangoFontFamily found for ::description") );
181 //BCI: Cache the wxFontFamily inside the class. Validate cache with
182 //BCI: g_ascii_strcasecmp(pango_font_description_get_family(description), pango_font_family_get_name(family)) == 0
184 if (family
!= NULL
&& pango_font_family_is_monospace( family
))
185 ret
= wxFONTFAMILY_TELETYPE
; // is deemed a monospace font by pango
187 #endif // gtk24 || HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE
189 if (ret
== wxFONTFAMILY_DEFAULT
)
191 if (strstr( family_text
, "sans" ) != NULL
) // checked before serif, so that "* Sans Serif" fonts are detected correctly
192 ret
= wxFONTFAMILY_SWISS
; // contains "Sans"
193 else if (strstr( family_text
, "serif" ) != NULL
)
194 ret
= wxFONTFAMILY_ROMAN
; // contains "Serif"
195 else if (strncmp( family_text
, "times", 5 ) == 0)
196 ret
= wxFONTFAMILY_ROMAN
; // begins with "Times"
197 else if (strncmp( family_text
, "old", 3 ) == 0)
198 ret
= wxFONTFAMILY_DECORATIVE
; // Begins with "Old" - "Old English", "Old Town"
205 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
207 return wxFONTENCODING_SYSTEM
;
211 void wxNativeFontInfo::SetPointSize(int pointsize
)
213 pango_font_description_set_size( description
, pointsize
* PANGO_SCALE
);
216 void wxNativeFontInfo::SetStyle(wxFontStyle style
)
220 case wxFONTSTYLE_ITALIC
:
221 pango_font_description_set_style( description
, PANGO_STYLE_ITALIC
);
223 case wxFONTSTYLE_SLANT
:
224 pango_font_description_set_style( description
, PANGO_STYLE_OBLIQUE
);
227 wxFAIL_MSG( _T("unknown font style") );
229 case wxFONTSTYLE_NORMAL
:
230 pango_font_description_set_style( description
, PANGO_STYLE_NORMAL
);
235 void wxNativeFontInfo::SetWeight(wxFontWeight weight
)
239 case wxFONTWEIGHT_BOLD
:
240 pango_font_description_set_weight(description
, PANGO_WEIGHT_BOLD
);
242 case wxFONTWEIGHT_LIGHT
:
243 pango_font_description_set_weight(description
, PANGO_WEIGHT_LIGHT
);
246 wxFAIL_MSG( _T("unknown font weight") );
248 case wxFONTWEIGHT_NORMAL
:
249 pango_font_description_set_weight(description
, PANGO_WEIGHT_NORMAL
);
253 void wxNativeFontInfo::SetUnderlined(bool WXUNUSED(underlined
))
255 wxFAIL_MSG( _T("not implemented") );
258 void wxNativeFontInfo::SetFaceName(const wxString
& facename
)
260 pango_font_description_set_family(description
, wxGTK_CONV_SYS(facename
));
263 void wxNativeFontInfo::SetFamily(wxFontFamily
WXUNUSED(family
))
265 wxFAIL_MSG( _T("not implemented") );
268 void wxNativeFontInfo::SetEncoding(wxFontEncoding
WXUNUSED(encoding
))
270 wxFAIL_MSG( _T("not implemented") );
275 bool wxNativeFontInfo::FromString(const wxString
& s
)
278 pango_font_description_free( description
);
280 description
= pango_font_description_from_string( wxGTK_CONV_SYS( s
) );
285 wxString
wxNativeFontInfo::ToString() const
287 char *str
= pango_font_description_to_string( description
);
288 wxString tmp
= wxGTK_CONV_BACK( str
);
294 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
296 return FromString( s
);
299 wxString
wxNativeFontInfo::ToUserString() const
304 // ----------------------------------------------------------------------------
305 // wxNativeEncodingInfo
306 // ----------------------------------------------------------------------------
308 bool wxNativeEncodingInfo::FromString(const wxString
& WXUNUSED(s
))
313 wxString
wxNativeEncodingInfo::ToString() const
315 return wxEmptyString
;
318 bool wxTestFontEncoding(const wxNativeEncodingInfo
& WXUNUSED(info
))
323 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
324 wxNativeEncodingInfo
*info
)
326 // all encodings are available in GTK+ 2 because we translate text in any
327 // encoding to UTF-8 internally anyhow
328 info
->facename
.clear();
329 info
->encoding
= encoding
;
338 #pragma message disable nosimpint
341 #include <X11/Xlib.h>
344 #pragma message enable nosimpint
347 #elif defined(__WXGTK__)
348 // we have to declare struct tm to avoid problems with first forward
349 // declaring it in C code (glib.h included from gdk.h does it) and then
350 // defining it when time.h is included from the headers below - this is
351 // known not to work at least with Sun CC 6.01
358 // ----------------------------------------------------------------------------
360 // ----------------------------------------------------------------------------
362 static wxHashTable
*g_fontHash
= (wxHashTable
*) NULL
;
364 // ----------------------------------------------------------------------------
366 // ----------------------------------------------------------------------------
368 // define the functions to create and destroy native fonts for this toolkit
370 wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
372 return XLoadQueryFont((Display
*)wxGetDisplay(), fontSpec
);
375 inline void wxFreeFont(wxNativeFont font
)
377 XFreeFont((Display
*)wxGetDisplay(), (XFontStruct
*)font
);
379 #elif defined(__WXGTK__)
380 wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
382 // VZ: we should use gdk_fontset_load() instead of gdk_font_load()
383 // here to be able to display Japanese fonts correctly (at least
384 // this is what people report) but unfortunately doing it results
385 // in tons of warnings when using GTK with "normal" European
386 // languages and so we can't always do it and I don't know enough
387 // to determine when should this be done... (FIXME)
388 return gdk_font_load( wxConvertWX2MB(fontSpec
) );
391 inline void wxFreeFont(wxNativeFont font
)
393 gdk_font_unref(font
);
396 #error "Unknown GUI toolkit"
399 static bool wxTestFontSpec(const wxString
& fontspec
);
401 static wxNativeFont
wxLoadQueryFont(int pointSize
,
406 const wxString
& facename
,
407 const wxString
& xregistry
,
408 const wxString
& xencoding
,
409 wxString
* xFontName
);
411 // ============================================================================
413 // ============================================================================
415 // ----------------------------------------------------------------------------
416 // wxNativeEncodingInfo
417 // ----------------------------------------------------------------------------
419 // convert to/from the string representation: format is
420 // encodingid;registry;encoding[;facename]
421 bool wxNativeEncodingInfo::FromString(const wxString
& s
)
423 // use ";", not "-" because it may be part of encoding name
424 wxStringTokenizer
tokenizer(s
, _T(";"));
426 wxString encid
= tokenizer
.GetNextToken();
428 if ( !encid
.ToLong(&enc
) )
430 encoding
= (wxFontEncoding
)enc
;
432 xregistry
= tokenizer
.GetNextToken();
436 xencoding
= tokenizer
.GetNextToken();
441 facename
= tokenizer
.GetNextToken();
446 wxString
wxNativeEncodingInfo::ToString() const
449 s
<< (long)encoding
<< _T(';') << xregistry
<< _T(';') << xencoding
;
450 if ( !facename
.empty() )
452 s
<< _T(';') << facename
;
458 // ----------------------------------------------------------------------------
460 // ----------------------------------------------------------------------------
462 void wxNativeFontInfo::Init()
467 bool wxNativeFontInfo::FromString(const wxString
& s
)
469 wxStringTokenizer
tokenizer(s
, _T(";"));
472 wxString token
= tokenizer
.GetNextToken();
473 if ( token
!= _T('0') )
476 xFontName
= tokenizer
.GetNextToken();
478 // this should be the end
479 if ( tokenizer
.HasMoreTokens() )
482 return FromXFontName(xFontName
);
485 wxString
wxNativeFontInfo::ToString() const
488 return wxString::Format(_T("%d;%s"), 0, GetXFontName().c_str());
491 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
493 return FromXFontName(s
);
496 wxString
wxNativeFontInfo::ToUserString() const
498 return GetXFontName();
501 bool wxNativeFontInfo::HasElements() const
503 // we suppose that the foundry is never empty, so if it is it means that we
504 // had never parsed the XLFD
505 return !fontElements
[0].empty();
508 wxString
wxNativeFontInfo::GetXFontComponent(wxXLFDField field
) const
510 wxCHECK_MSG( field
< wxXLFD_MAX
, wxEmptyString
, _T("invalid XLFD field") );
512 if ( !HasElements() )
515 if ( !((wxNativeFontInfo
*)this)->FromXFontName(xFontName
) )
516 return wxEmptyString
;
519 return fontElements
[field
];
522 bool wxNativeFontInfo::FromXFontName(const wxString
& fontname
)
524 // TODO: we should be able to handle the font aliases here, but how?
525 wxStringTokenizer
tokenizer(fontname
, _T("-"));
527 // skip the leading, usually empty field (font name registry)
528 if ( !tokenizer
.HasMoreTokens() )
531 (void)tokenizer
.GetNextToken();
533 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
535 if ( !tokenizer
.HasMoreTokens() )
537 // not enough elements in the XLFD - or maybe an alias
541 wxString field
= tokenizer
.GetNextToken();
542 if ( !field
.empty() && field
!= _T('*') )
544 // we're really initialized now
548 fontElements
[n
] = field
;
551 // this should be all
552 if ( tokenizer
.HasMoreTokens() )
558 wxString
wxNativeFontInfo::GetXFontName() const
560 if ( xFontName
.empty() )
562 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
564 // replace the non specified elements with '*' except for the
565 // additional style which is usually just omitted
566 wxString elt
= fontElements
[n
];
567 if ( elt
.empty() && n
!= wxXLFD_ADDSTYLE
)
573 ((wxNativeFontInfo
*)this)->xFontName
<< _T('-') << elt
;
581 wxNativeFontInfo::SetXFontComponent(wxXLFDField field
, const wxString
& value
)
583 wxCHECK_RET( field
< wxXLFD_MAX
, _T("invalid XLFD field") );
585 // this class should be initialized with a valid font spec first and only
586 // then the fields may be modified!
587 wxASSERT_MSG( !IsDefault(), _T("can't modify an uninitialized XLFD") );
589 if ( !HasElements() )
592 if ( !((wxNativeFontInfo
*)this)->FromXFontName(xFontName
) )
594 wxFAIL_MSG( _T("can't set font element for invalid XLFD") );
600 fontElements
[field
] = value
;
602 // invalidate the XFLD, it doesn't correspond to the font elements any more
606 void wxNativeFontInfo::SetXFontName(const wxString
& xFontName_
)
608 // invalidate the font elements, GetXFontComponent() will reparse the XLFD
609 fontElements
[0].clear();
611 xFontName
= xFontName_
;
616 int wxNativeFontInfo::GetPointSize() const
618 const wxString s
= GetXFontComponent(wxXLFD_POINTSIZE
);
620 // return -1 to indicate that the size is unknown
622 return s
.ToLong(&l
) ? l
: -1;
625 wxFontStyle
wxNativeFontInfo::GetStyle() const
627 const wxString s
= GetXFontComponent(wxXLFD_SLANT
);
629 if ( s
.length() != 1 )
631 // it is really unknown but we don't have any way to return it from
633 return wxFONTSTYLE_NORMAL
;
639 // again, unknown but consider normal by default
642 return wxFONTSTYLE_NORMAL
;
645 return wxFONTSTYLE_ITALIC
;
648 return wxFONTSTYLE_SLANT
;
652 wxFontWeight
wxNativeFontInfo::GetWeight() const
654 const wxString s
= GetXFontComponent(wxXLFD_WEIGHT
).MakeLower();
655 if ( s
.find(_T("bold")) != wxString::npos
|| s
== _T("black") )
656 return wxFONTWEIGHT_BOLD
;
657 else if ( s
== _T("light") )
658 return wxFONTWEIGHT_LIGHT
;
660 return wxFONTWEIGHT_NORMAL
;
663 bool wxNativeFontInfo::GetUnderlined() const
665 // X fonts are never underlined
669 wxString
wxNativeFontInfo::GetFaceName() const
671 // wxWidgets facename probably more accurately corresponds to X family
672 return GetXFontComponent(wxXLFD_FAMILY
);
675 wxFontFamily
wxNativeFontInfo::GetFamily() const
677 // and wxWidgets family -- to X foundry, but we have to translate it to
678 // wxFontFamily somehow...
679 wxFAIL_MSG(_T("not implemented")); // GetXFontComponent(wxXLFD_FOUNDRY);
681 return wxFONTFAMILY_DEFAULT
;
684 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
686 // we already have the code for this but need to refactor it first
687 wxFAIL_MSG( _T("not implemented") );
689 return wxFONTENCODING_MAX
;
692 void wxNativeFontInfo::SetPointSize(int pointsize
)
694 SetXFontComponent(wxXLFD_POINTSIZE
, wxString::Format(_T("%d"), pointsize
));
697 void wxNativeFontInfo::SetStyle(wxFontStyle style
)
702 case wxFONTSTYLE_ITALIC
:
706 case wxFONTSTYLE_SLANT
:
710 case wxFONTSTYLE_NORMAL
:
714 wxFAIL_MSG( _T("unknown wxFontStyle in wxNativeFontInfo::SetStyle") );
718 SetXFontComponent(wxXLFD_SLANT
, s
);
721 void wxNativeFontInfo::SetWeight(wxFontWeight weight
)
726 case wxFONTWEIGHT_BOLD
:
730 case wxFONTWEIGHT_LIGHT
:
734 case wxFONTWEIGHT_NORMAL
:
739 wxFAIL_MSG( _T("unknown wxFontWeight in wxNativeFontInfo::SetWeight") );
743 SetXFontComponent(wxXLFD_WEIGHT
, s
);
746 void wxNativeFontInfo::SetUnderlined(bool WXUNUSED(underlined
))
748 // can't do this under X
751 void wxNativeFontInfo::SetFaceName(const wxString
& facename
)
753 SetXFontComponent(wxXLFD_FAMILY
, facename
);
756 void wxNativeFontInfo::SetFamily(wxFontFamily
WXUNUSED(family
))
758 // wxFontFamily -> X foundry, anyone?
759 wxFAIL_MSG( _T("not implemented") );
761 // SetXFontComponent(wxXLFD_FOUNDRY, ...);
764 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding
)
766 wxNativeEncodingInfo info
;
767 if ( wxGetNativeFontEncoding(encoding
, &info
) )
769 SetXFontComponent(wxXLFD_ENCODING
, info
.xencoding
);
770 SetXFontComponent(wxXLFD_REGISTRY
, info
.xregistry
);
774 // ----------------------------------------------------------------------------
776 // ----------------------------------------------------------------------------
778 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
779 wxNativeEncodingInfo
*info
)
781 wxCHECK_MSG( info
, false, _T("bad pointer in wxGetNativeFontEncoding") );
783 if ( encoding
== wxFONTENCODING_DEFAULT
)
785 encoding
= wxFont::GetDefaultEncoding();
790 case wxFONTENCODING_ISO8859_1
:
791 case wxFONTENCODING_ISO8859_2
:
792 case wxFONTENCODING_ISO8859_3
:
793 case wxFONTENCODING_ISO8859_4
:
794 case wxFONTENCODING_ISO8859_5
:
795 case wxFONTENCODING_ISO8859_6
:
796 case wxFONTENCODING_ISO8859_7
:
797 case wxFONTENCODING_ISO8859_8
:
798 case wxFONTENCODING_ISO8859_9
:
799 case wxFONTENCODING_ISO8859_10
:
800 case wxFONTENCODING_ISO8859_11
:
801 case wxFONTENCODING_ISO8859_12
:
802 case wxFONTENCODING_ISO8859_13
:
803 case wxFONTENCODING_ISO8859_14
:
804 case wxFONTENCODING_ISO8859_15
:
806 int cp
= encoding
- wxFONTENCODING_ISO8859_1
+ 1;
807 info
->xregistry
= wxT("iso8859");
808 info
->xencoding
.Printf(wxT("%d"), cp
);
812 case wxFONTENCODING_UTF8
:
813 info
->xregistry
= wxT("iso10646");
814 info
->xencoding
= wxT("*");
817 case wxFONTENCODING_GB2312
:
818 info
->xregistry
= wxT("GB2312"); // or the otherway round?
819 info
->xencoding
= wxT("*");
822 case wxFONTENCODING_KOI8
:
823 case wxFONTENCODING_KOI8_U
:
824 info
->xregistry
= wxT("koi8");
826 // we don't make distinction between koi8-r, koi8-u and koi8-ru (so far)
827 info
->xencoding
= wxT("*");
830 case wxFONTENCODING_CP1250
:
831 case wxFONTENCODING_CP1251
:
832 case wxFONTENCODING_CP1252
:
833 case wxFONTENCODING_CP1253
:
834 case wxFONTENCODING_CP1254
:
835 case wxFONTENCODING_CP1255
:
836 case wxFONTENCODING_CP1256
:
837 case wxFONTENCODING_CP1257
:
839 int cp
= encoding
- wxFONTENCODING_CP1250
+ 1250;
840 info
->xregistry
= wxT("microsoft");
841 info
->xencoding
.Printf(wxT("cp%d"), cp
);
845 case wxFONTENCODING_EUC_JP
:
846 case wxFONTENCODING_SHIFT_JIS
:
847 info
->xregistry
= "jis*";
848 info
->xencoding
= "*";
851 case wxFONTENCODING_SYSTEM
:
853 info
->xencoding
= wxT("*");
857 // don't know how to translate this encoding into X fontspec
861 info
->encoding
= encoding
;
866 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
869 fontspec
.Printf(_T("-*-%s-*-*-*-*-*-*-*-*-*-*-%s-%s"),
870 !info
.facename
? _T("*") : info
.facename
.c_str(),
871 info
.xregistry
.c_str(),
872 info
.xencoding
.c_str());
874 return wxTestFontSpec(fontspec
);
877 // ----------------------------------------------------------------------------
878 // X-specific functions
879 // ----------------------------------------------------------------------------
881 wxNativeFont
wxLoadQueryNearestFont(int pointSize
,
886 const wxString
&facename
,
887 wxFontEncoding encoding
,
890 if ( encoding
== wxFONTENCODING_DEFAULT
)
892 encoding
= wxFont::GetDefaultEncoding();
895 // first determine the encoding - if the font doesn't exist at all in this
896 // encoding, it's useless to do all other approximations (i.e. size,
897 // family &c don't matter much)
898 wxNativeEncodingInfo info
;
899 if ( encoding
== wxFONTENCODING_SYSTEM
)
901 // This will always work so we don't test to save time
902 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
906 if ( !wxGetNativeFontEncoding(encoding
, &info
) ||
907 !wxTestFontEncoding(info
) )
910 if ( !wxFontMapper::Get()->GetAltForEncoding(encoding
, &info
) )
911 #endif // wxUSE_FONTMAP
913 // unspported encoding - replace it with the default
915 // NB: we can't just return 0 from here because wxGTK code doesn't
916 // check for it (i.e. it supposes that we'll always succeed),
917 // so it would provoke a crash
918 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
923 // OK, we have the correct xregistry/xencoding in info structure
924 wxNativeFont font
= 0;
926 // if we already have the X font name, try to use it
927 if( xFontName
&& !xFontName
->empty() )
930 // Make sure point size is correct for scale factor.
932 wxStringTokenizer
tokenizer(*xFontName
, _T("-"), wxTOKEN_RET_DELIMS
);
933 wxString newFontName
;
935 for(int i
= 0; i
< 8; i
++)
936 newFontName
+= tokenizer
.NextToken();
938 (void) tokenizer
.NextToken();
940 newFontName
+= wxString::Format(wxT("%d-"), pointSize
);
942 while(tokenizer
.HasMoreTokens())
943 newFontName
+= tokenizer
.GetNextToken();
945 font
= wxLoadFont(newFontName
);
948 *xFontName
= newFontName
;
953 // search up and down by stepsize 10
954 int max_size
= pointSize
+ 20 * (1 + (pointSize
/180));
955 int min_size
= pointSize
- 20 * (1 + (pointSize
/180));
957 int i
, round
; // counters
959 // first round: search for equal, then for smaller and for larger size with the given weight and style
960 int testweight
= weight
;
961 int teststyle
= style
;
963 for ( round
= 0; round
< 3; round
++ )
965 // second round: use normal weight
968 if ( testweight
!= wxNORMAL
)
970 testweight
= wxNORMAL
;
974 ++round
; // fall through to third round
978 // third round: ... and use normal style
981 if ( teststyle
!= wxNORMAL
)
983 teststyle
= wxNORMAL
;
990 // Search for equal or smaller size (approx.)
991 for ( i
= pointSize
; !font
&& i
>= 10 && i
>= min_size
; i
-= 10 )
993 font
= wxLoadQueryFont(i
, family
, teststyle
, testweight
, underlined
,
994 facename
, info
.xregistry
, info
.xencoding
,
998 // Search for larger size (approx.)
999 for ( i
= pointSize
+ 10; !font
&& i
<= max_size
; i
+= 10 )
1001 font
= wxLoadQueryFont(i
, family
, teststyle
, testweight
, underlined
,
1002 facename
, info
.xregistry
, info
.xencoding
,
1007 // Try default family
1008 if ( !font
&& family
!= wxDEFAULT
)
1010 font
= wxLoadQueryFont(pointSize
, wxDEFAULT
, style
, weight
,
1011 underlined
, facename
,
1012 info
.xregistry
, info
.xencoding
,
1016 // ignore size, family, style and weight but try to find font with the
1017 // given facename and encoding
1020 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1021 underlined
, facename
,
1022 info
.xregistry
, info
.xencoding
,
1025 // ignore family as well
1028 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1029 underlined
, wxEmptyString
,
1030 info
.xregistry
, info
.xencoding
,
1033 // if it still failed, try to get the font of any size but
1034 // with the requested encoding: this can happen if the
1035 // encoding is only available in one size which happens to be
1036 // different from 120
1039 font
= wxLoadQueryFont(-1, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1040 false, wxEmptyString
,
1041 info
.xregistry
, info
.xencoding
,
1044 // this should never happen as we had tested for it in the
1045 // very beginning, but if it does, do return something non
1046 // NULL or we'd crash in wxFont code
1049 wxFAIL_MSG( _T("this encoding should be available!") );
1051 font
= wxLoadQueryFont(-1,
1052 wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1053 false, wxEmptyString
,
1065 // ----------------------------------------------------------------------------
1066 // private functions
1067 // ----------------------------------------------------------------------------
1069 // returns true if there are any fonts matching this font spec
1070 static bool wxTestFontSpec(const wxString
& fontspec
)
1072 // some X servers will fail to load this font because there are too many
1073 // matches so we must test explicitly for this
1074 if ( fontspec
== _T("-*-*-*-*-*-*-*-*-*-*-*-*-*-*") )
1079 wxNativeFont test
= (wxNativeFont
) g_fontHash
->Get( fontspec
);
1085 test
= wxLoadFont(fontspec
);
1086 g_fontHash
->Put( fontspec
, (wxObject
*) test
);
1100 static wxNativeFont
wxLoadQueryFont(int pointSize
,
1104 bool WXUNUSED(underlined
),
1105 const wxString
& facename
,
1106 const wxString
& xregistry
,
1107 const wxString
& xencoding
,
1108 wxString
* xFontName
)
1113 case wxDECORATIVE
: xfamily
= wxT("lucida"); break;
1114 case wxROMAN
: xfamily
= wxT("times"); break;
1115 case wxMODERN
: xfamily
= wxT("courier"); break;
1116 case wxSWISS
: xfamily
= wxT("helvetica"); break;
1117 case wxTELETYPE
: xfamily
= wxT("lucidatypewriter"); break;
1118 case wxSCRIPT
: xfamily
= wxT("utopia"); break;
1119 default: xfamily
= wxT("*");
1127 xweight
= MWLF_WEIGHT_BOLD
;
1132 xweight
= MWLF_WEIGHT_LIGHT
;
1137 xweight
= MWLF_WEIGHT_NORMAL
;
1143 xweight
= MWLF_WEIGHT_DEFAULT
;
1147 GR_SCREEN_INFO screenInfo
;
1148 GrGetScreenInfo(& screenInfo
);
1150 int yPixelsPerCM
= screenInfo
.ydpcm
;
1152 // A point is 1/72 of an inch.
1153 // An inch is 2.541 cm.
1154 // So pixelHeight = (pointSize / 72) (inches) * 2.541 (for cm) * yPixelsPerCM (for pixels)
1155 // In fact pointSize is 10 * the normal point size so
1158 int pixelHeight
= (int) ( (((float)pointSize
) / 720.0) * 2.541 * (float) yPixelsPerCM
) ;
1160 // An alternative: assume that the screen is 72 dpi.
1161 //int pixelHeight = (int) (((float)pointSize / 720.0) * 72.0) ;
1162 //int pixelHeight = (int) ((float)pointSize / 10.0) ;
1165 logFont
.lfHeight
= pixelHeight
;
1166 logFont
.lfWidth
= 0;
1167 logFont
.lfEscapement
= 0;
1168 logFont
.lfOrientation
= 0;
1169 logFont
.lfWeight
= xweight
;
1170 logFont
.lfItalic
= (style
== wxNORMAL
? 0 : 1) ;
1171 logFont
.lfUnderline
= 0;
1172 logFont
.lfStrikeOut
= 0;
1173 logFont
.lfCharSet
= MWLF_CHARSET_DEFAULT
; // TODO: select appropriate one
1174 logFont
.lfOutPrecision
= MWLF_TYPE_DEFAULT
;
1175 logFont
.lfClipPrecision
= 0; // Not used
1176 logFont
.lfRoman
= (family
== wxROMAN
? 1 : 0) ;
1177 logFont
.lfSerif
= (family
== wxSWISS
? 0 : 1) ;
1178 logFont
.lfSansSerif
= !logFont
.lfSerif
;
1179 logFont
.lfModern
= (family
== wxMODERN
? 1 : 0) ;
1180 logFont
.lfProportional
= (family
== wxTELETYPE
? 0 : 1) ;
1181 logFont
.lfOblique
= 0;
1182 logFont
.lfSmallCaps
= 0;
1183 logFont
.lfPitch
= 0; // 0 = default
1184 strcpy(logFont
.lfFaceName
, facename
.c_str());
1186 XFontStruct
* fontInfo
= (XFontStruct
*) malloc(sizeof(XFontStruct
));
1187 fontInfo
->fid
= GrCreateFont((GR_CHAR
*) facename
.c_str(), pixelHeight
, & logFont
);
1188 GrGetFontInfo(fontInfo
->fid
, & fontInfo
->info
);
1189 return (wxNativeFont
) fontInfo
;
1193 if (!facename
.empty())
1195 fontSpec
.Printf(wxT("-*-%s-*-*-normal-*-*-*-*-*-*-*-*-*"),
1198 if ( wxTestFontSpec(fontSpec
) )
1202 //else: no such family, use default one instead
1209 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1211 if ( wxTestFontSpec(fontSpec
) )
1216 // fall through - try wxITALIC now
1219 fontSpec
.Printf(wxT("-*-%s-*-i-*-*-*-*-*-*-*-*-*-*"),
1221 if ( wxTestFontSpec(fontSpec
) )
1225 else if ( style
== wxITALIC
) // and not wxSLANT
1228 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1230 if ( wxTestFontSpec(fontSpec
) )
1236 // no italic, no slant - leave default
1243 wxFAIL_MSG(_T("unknown font style"));
1244 // fall back to normal
1256 fontSpec
.Printf(wxT("-*-%s-bold-*-*-*-*-*-*-*-*-*-*-*"),
1258 if ( wxTestFontSpec(fontSpec
) )
1260 xweight
= wxT("bold");
1263 fontSpec
.Printf(wxT("-*-%s-heavy-*-*-*-*-*-*-*-*-*-*-*"),
1265 if ( wxTestFontSpec(fontSpec
) )
1267 xweight
= wxT("heavy");
1270 fontSpec
.Printf(wxT("-*-%s-extrabold-*-*-*-*-*-*-*-*-*-*-*"),
1272 if ( wxTestFontSpec(fontSpec
) )
1274 xweight
= wxT("extrabold");
1277 fontSpec
.Printf(wxT("-*-%s-demibold-*-*-*-*-*-*-*-*-*-*-*"),
1279 if ( wxTestFontSpec(fontSpec
) )
1281 xweight
= wxT("demibold");
1284 fontSpec
.Printf(wxT("-*-%s-black-*-*-*-*-*-*-*-*-*-*-*"),
1286 if ( wxTestFontSpec(fontSpec
) )
1288 xweight
= wxT("black");
1291 fontSpec
.Printf(wxT("-*-%s-ultrablack-*-*-*-*-*-*-*-*-*-*-*"),
1293 if ( wxTestFontSpec(fontSpec
) )
1295 xweight
= wxT("ultrablack");
1302 fontSpec
.Printf(wxT("-*-%s-light-*-*-*-*-*-*-*-*-*-*-*"),
1304 if ( wxTestFontSpec(fontSpec
) )
1306 xweight
= wxT("light");
1309 fontSpec
.Printf(wxT("-*-%s-thin-*-*-*-*-*-*-*-*-*-*-*"),
1311 if ( wxTestFontSpec(fontSpec
) )
1313 xweight
= wxT("thin");
1320 fontSpec
.Printf(wxT("-*-%s-medium-*-*-*-*-*-*-*-*-*-*-*"),
1322 if ( wxTestFontSpec(fontSpec
) )
1324 xweight
= wxT("medium");
1327 fontSpec
.Printf(wxT("-*-%s-normal-*-*-*-*-*-*-*-*-*-*-*"),
1329 if ( wxTestFontSpec(fontSpec
) )
1331 xweight
= wxT("normal");
1334 fontSpec
.Printf(wxT("-*-%s-regular-*-*-*-*-*-*-*-*-*-*-*"),
1336 if ( wxTestFontSpec(fontSpec
) )
1338 xweight
= wxT("regular");
1344 default: xweight
= wxT("*"); break;
1347 // if pointSize is -1, don't specify any
1349 if ( pointSize
== -1 )
1355 sizeSpec
.Printf(_T("%d"), pointSize
);
1358 // construct the X font spec from our data
1359 fontSpec
.Printf(wxT("-*-%s-%s-%s-normal-*-*-%s-*-*-*-*-%s-%s"),
1360 xfamily
.c_str(), xweight
.c_str(), xstyle
.c_str(),
1361 sizeSpec
.c_str(), xregistry
.c_str(), xencoding
.c_str());
1364 *xFontName
= fontSpec
;
1366 return wxLoadFont(fontSpec
);
1371 // ----------------------------------------------------------------------------
1373 // ----------------------------------------------------------------------------
1375 class wxFontModule
: public wxModule
1382 DECLARE_DYNAMIC_CLASS(wxFontModule
)
1385 IMPLEMENT_DYNAMIC_CLASS(wxFontModule
, wxModule
)
1387 bool wxFontModule::OnInit()
1389 g_fontHash
= new wxHashTable( wxKEY_STRING
);
1394 void wxFontModule::OnExit()
1398 g_fontHash
= (wxHashTable
*)NULL
;
1401 #endif // GTK 2.0/1.x