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
);
146 wxGtkString
family_text(g_ascii_strdown(family_name
, strlen(family_name
)));
148 // Check for some common fonts, to salvage what we can from the current win32 centric wxFont API:
149 if (strncmp( family_text
, "monospace", 9 ) == 0)
150 ret
= wxFONTFAMILY_TELETYPE
; // begins with "Monospace"
151 else if (strncmp( family_text
, "courier", 7 ) == 0)
152 ret
= wxFONTFAMILY_TELETYPE
; // begins with "Courier"
153 #if defined(__WXGTK24__) || defined(HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE)
156 if (!gtk_check_version(2,4,0))
159 PangoFontFamily
**families
;
160 PangoFontFamily
*family
= NULL
;
162 pango_context_list_families(
164 gtk_widget_get_pango_context( wxGetRootWindow() ),
166 wxTheApp
->GetPangoContext(),
168 &families
, &n_families
);
170 for (int i
= 0;i
< n_families
;++i
)
172 if (g_ascii_strcasecmp(pango_font_family_get_name( families
[i
] ), pango_font_description_get_family( description
)) == 0 )
174 family
= families
[i
];
181 // Some gtk+ systems might query for a non-existing font from wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT)
182 // on initialization, don't assert until wxSystemSettings::GetFont is checked for this - MR
183 // wxASSERT_MSG( family, wxT("wxNativeFontInfo::GetFamily() - No appropriate PangoFontFamily found for ::description") );
185 //BCI: Cache the wxFontFamily inside the class. Validate cache with
186 //BCI: g_ascii_strcasecmp(pango_font_description_get_family(description), pango_font_family_get_name(family)) == 0
188 if (family
!= NULL
&& pango_font_family_is_monospace( family
))
189 ret
= wxFONTFAMILY_TELETYPE
; // is deemed a monospace font by pango
191 #endif // gtk24 || HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE
193 if (ret
== wxFONTFAMILY_DEFAULT
)
195 if (strstr( family_text
, "sans" ) != NULL
) // checked before serif, so that "* Sans Serif" fonts are detected correctly
196 ret
= wxFONTFAMILY_SWISS
; // contains "Sans"
197 else if (strstr( family_text
, "serif" ) != NULL
)
198 ret
= wxFONTFAMILY_ROMAN
; // contains "Serif"
199 else if (strncmp( family_text
, "times", 5 ) == 0)
200 ret
= wxFONTFAMILY_ROMAN
; // begins with "Times"
201 else if (strncmp( family_text
, "old", 3 ) == 0)
202 ret
= wxFONTFAMILY_DECORATIVE
; // Begins with "Old" - "Old English", "Old Town"
208 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
210 return wxFONTENCODING_SYSTEM
;
214 void wxNativeFontInfo::SetPointSize(int pointsize
)
216 pango_font_description_set_size( description
, pointsize
* PANGO_SCALE
);
219 void wxNativeFontInfo::SetStyle(wxFontStyle style
)
223 case wxFONTSTYLE_ITALIC
:
224 pango_font_description_set_style( description
, PANGO_STYLE_ITALIC
);
226 case wxFONTSTYLE_SLANT
:
227 pango_font_description_set_style( description
, PANGO_STYLE_OBLIQUE
);
230 wxFAIL_MSG( _T("unknown font style") );
232 case wxFONTSTYLE_NORMAL
:
233 pango_font_description_set_style( description
, PANGO_STYLE_NORMAL
);
238 void wxNativeFontInfo::SetWeight(wxFontWeight weight
)
242 case wxFONTWEIGHT_BOLD
:
243 pango_font_description_set_weight(description
, PANGO_WEIGHT_BOLD
);
245 case wxFONTWEIGHT_LIGHT
:
246 pango_font_description_set_weight(description
, PANGO_WEIGHT_LIGHT
);
249 wxFAIL_MSG( _T("unknown font weight") );
251 case wxFONTWEIGHT_NORMAL
:
252 pango_font_description_set_weight(description
, PANGO_WEIGHT_NORMAL
);
256 void wxNativeFontInfo::SetUnderlined(bool WXUNUSED(underlined
))
258 wxFAIL_MSG( _T("not implemented") );
261 void wxNativeFontInfo::SetFaceName(const wxString
& facename
)
263 pango_font_description_set_family(description
, wxGTK_CONV_SYS(facename
));
266 void wxNativeFontInfo::SetFamily(wxFontFamily
WXUNUSED(family
))
268 wxFAIL_MSG( _T("not implemented") );
271 void wxNativeFontInfo::SetEncoding(wxFontEncoding
WXUNUSED(encoding
))
273 wxFAIL_MSG( _T("not implemented") );
278 bool wxNativeFontInfo::FromString(const wxString
& s
)
281 pango_font_description_free( description
);
283 // there is a bug in at least pango <= 1.13 which makes it (or its backends)
284 // segfault for very big point sizes and for negative point sizes.
285 // To workaround that bug for pango <= 1.13
286 // (see http://bugzilla.gnome.org/show_bug.cgi?id=340229)
287 // we do the check on the size here using same (arbitrary) limits used by
288 // pango > 1.13. Note that the segfault could happen also for pointsize
289 // smaller than this limit !!
291 const size_t pos
= str
.find_last_of(_T(" "));
293 if ( pos
!= wxString::npos
&& wxString(str
, pos
+ 1).ToDouble(&size
) )
298 else if ( size
>= 1E6
)
301 if ( !sizeStr
.empty() )
303 // replace the old size with the adjusted one
304 str
= wxString(s
, 0, pos
) + sizeStr
;
308 description
= pango_font_description_from_string( wxGTK_CONV_SYS( str
) );
313 wxString
wxNativeFontInfo::ToString() const
315 wxGtkString
str(pango_font_description_to_string( description
));
317 return wxGTK_CONV_BACK(str
);
320 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
322 return FromString( s
);
325 wxString
wxNativeFontInfo::ToUserString() const
330 // ----------------------------------------------------------------------------
331 // wxNativeEncodingInfo
332 // ----------------------------------------------------------------------------
334 bool wxNativeEncodingInfo::FromString(const wxString
& WXUNUSED(s
))
339 wxString
wxNativeEncodingInfo::ToString() const
341 return wxEmptyString
;
344 bool wxTestFontEncoding(const wxNativeEncodingInfo
& WXUNUSED(info
))
349 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
350 wxNativeEncodingInfo
*info
)
352 // all encodings are available in GTK+ 2 because we translate text in any
353 // encoding to UTF-8 internally anyhow
354 info
->facename
.clear();
355 info
->encoding
= encoding
;
364 #pragma message disable nosimpint
367 #include <X11/Xlib.h>
370 #pragma message enable nosimpint
373 #elif defined(__WXGTK__)
374 // we have to declare struct tm to avoid problems with first forward
375 // declaring it in C code (glib.h included from gdk.h does it) and then
376 // defining it when time.h is included from the headers below - this is
377 // known not to work at least with Sun CC 6.01
384 // ----------------------------------------------------------------------------
386 // ----------------------------------------------------------------------------
388 static wxHashTable
*g_fontHash
= (wxHashTable
*) NULL
;
390 // ----------------------------------------------------------------------------
392 // ----------------------------------------------------------------------------
394 // define the functions to create and destroy native fonts for this toolkit
396 wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
398 return XLoadQueryFont((Display
*)wxGetDisplay(), fontSpec
);
401 inline void wxFreeFont(wxNativeFont font
)
403 XFreeFont((Display
*)wxGetDisplay(), (XFontStruct
*)font
);
405 #elif defined(__WXGTK__)
406 wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
408 // VZ: we should use gdk_fontset_load() instead of gdk_font_load()
409 // here to be able to display Japanese fonts correctly (at least
410 // this is what people report) but unfortunately doing it results
411 // in tons of warnings when using GTK with "normal" European
412 // languages and so we can't always do it and I don't know enough
413 // to determine when should this be done... (FIXME)
414 return gdk_font_load( wxConvertWX2MB(fontSpec
) );
417 inline void wxFreeFont(wxNativeFont font
)
419 gdk_font_unref(font
);
422 #error "Unknown GUI toolkit"
425 static bool wxTestFontSpec(const wxString
& fontspec
);
427 static wxNativeFont
wxLoadQueryFont(int pointSize
,
432 const wxString
& facename
,
433 const wxString
& xregistry
,
434 const wxString
& xencoding
,
435 wxString
* xFontName
);
437 // ============================================================================
439 // ============================================================================
441 // ----------------------------------------------------------------------------
442 // wxNativeEncodingInfo
443 // ----------------------------------------------------------------------------
445 // convert to/from the string representation: format is
446 // encodingid;registry;encoding[;facename]
447 bool wxNativeEncodingInfo::FromString(const wxString
& s
)
449 // use ";", not "-" because it may be part of encoding name
450 wxStringTokenizer
tokenizer(s
, _T(";"));
452 wxString encid
= tokenizer
.GetNextToken();
454 if ( !encid
.ToLong(&enc
) )
456 encoding
= (wxFontEncoding
)enc
;
458 xregistry
= tokenizer
.GetNextToken();
462 xencoding
= tokenizer
.GetNextToken();
467 facename
= tokenizer
.GetNextToken();
472 wxString
wxNativeEncodingInfo::ToString() const
475 s
<< (long)encoding
<< _T(';') << xregistry
<< _T(';') << xencoding
;
476 if ( !facename
.empty() )
478 s
<< _T(';') << facename
;
484 // ----------------------------------------------------------------------------
486 // ----------------------------------------------------------------------------
488 void wxNativeFontInfo::Init()
493 bool wxNativeFontInfo::FromString(const wxString
& s
)
495 wxStringTokenizer
tokenizer(s
, _T(";"));
498 wxString token
= tokenizer
.GetNextToken();
499 if ( token
!= _T('0') )
502 xFontName
= tokenizer
.GetNextToken();
504 // this should be the end
505 if ( tokenizer
.HasMoreTokens() )
508 return FromXFontName(xFontName
);
511 wxString
wxNativeFontInfo::ToString() const
514 return wxString::Format(_T("%d;%s"), 0, GetXFontName().c_str());
517 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
519 return FromXFontName(s
);
522 wxString
wxNativeFontInfo::ToUserString() const
524 return GetXFontName();
527 bool wxNativeFontInfo::HasElements() const
529 // we suppose that the foundry is never empty, so if it is it means that we
530 // had never parsed the XLFD
531 return !fontElements
[0].empty();
534 wxString
wxNativeFontInfo::GetXFontComponent(wxXLFDField field
) const
536 wxCHECK_MSG( field
< wxXLFD_MAX
, wxEmptyString
, _T("invalid XLFD field") );
538 if ( !HasElements() )
541 if ( !((wxNativeFontInfo
*)this)->FromXFontName(xFontName
) )
542 return wxEmptyString
;
545 return fontElements
[field
];
548 bool wxNativeFontInfo::FromXFontName(const wxString
& fontname
)
550 // TODO: we should be able to handle the font aliases here, but how?
551 wxStringTokenizer
tokenizer(fontname
, _T("-"));
553 // skip the leading, usually empty field (font name registry)
554 if ( !tokenizer
.HasMoreTokens() )
557 (void)tokenizer
.GetNextToken();
559 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
561 if ( !tokenizer
.HasMoreTokens() )
563 // not enough elements in the XLFD - or maybe an alias
567 wxString field
= tokenizer
.GetNextToken();
568 if ( !field
.empty() && field
!= _T('*') )
570 // we're really initialized now
574 fontElements
[n
] = field
;
577 // this should be all
578 if ( tokenizer
.HasMoreTokens() )
584 wxString
wxNativeFontInfo::GetXFontName() const
586 if ( xFontName
.empty() )
588 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
590 // replace the non specified elements with '*' except for the
591 // additional style which is usually just omitted
592 wxString elt
= fontElements
[n
];
593 if ( elt
.empty() && n
!= wxXLFD_ADDSTYLE
)
599 ((wxNativeFontInfo
*)this)->xFontName
<< _T('-') << elt
;
607 wxNativeFontInfo::SetXFontComponent(wxXLFDField field
, const wxString
& value
)
609 wxCHECK_RET( field
< wxXLFD_MAX
, _T("invalid XLFD field") );
611 // this class should be initialized with a valid font spec first and only
612 // then the fields may be modified!
613 wxASSERT_MSG( !IsDefault(), _T("can't modify an uninitialized XLFD") );
615 if ( !HasElements() )
618 if ( !((wxNativeFontInfo
*)this)->FromXFontName(xFontName
) )
620 wxFAIL_MSG( _T("can't set font element for invalid XLFD") );
626 fontElements
[field
] = value
;
628 // invalidate the XFLD, it doesn't correspond to the font elements any more
632 void wxNativeFontInfo::SetXFontName(const wxString
& xFontName_
)
634 // invalidate the font elements, GetXFontComponent() will reparse the XLFD
635 fontElements
[0].clear();
637 xFontName
= xFontName_
;
642 int wxNativeFontInfo::GetPointSize() const
644 const wxString s
= GetXFontComponent(wxXLFD_POINTSIZE
);
646 // return -1 to indicate that the size is unknown
648 return s
.ToLong(&l
) ? l
: -1;
651 wxFontStyle
wxNativeFontInfo::GetStyle() const
653 const wxString s
= GetXFontComponent(wxXLFD_SLANT
);
655 if ( s
.length() != 1 )
657 // it is really unknown but we don't have any way to return it from
659 return wxFONTSTYLE_NORMAL
;
665 // again, unknown but consider normal by default
668 return wxFONTSTYLE_NORMAL
;
671 return wxFONTSTYLE_ITALIC
;
674 return wxFONTSTYLE_SLANT
;
678 wxFontWeight
wxNativeFontInfo::GetWeight() const
680 const wxString s
= GetXFontComponent(wxXLFD_WEIGHT
).MakeLower();
681 if ( s
.find(_T("bold")) != wxString::npos
|| s
== _T("black") )
682 return wxFONTWEIGHT_BOLD
;
683 else if ( s
== _T("light") )
684 return wxFONTWEIGHT_LIGHT
;
686 return wxFONTWEIGHT_NORMAL
;
689 bool wxNativeFontInfo::GetUnderlined() const
691 // X fonts are never underlined
695 wxString
wxNativeFontInfo::GetFaceName() const
697 // wxWidgets facename probably more accurately corresponds to X family
698 return GetXFontComponent(wxXLFD_FAMILY
);
701 wxFontFamily
wxNativeFontInfo::GetFamily() const
703 // and wxWidgets family -- to X foundry, but we have to translate it to
704 // wxFontFamily somehow...
705 wxFAIL_MSG(_T("not implemented")); // GetXFontComponent(wxXLFD_FOUNDRY);
707 return wxFONTFAMILY_DEFAULT
;
710 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
712 // we already have the code for this but need to refactor it first
713 wxFAIL_MSG( _T("not implemented") );
715 return wxFONTENCODING_MAX
;
718 void wxNativeFontInfo::SetPointSize(int pointsize
)
720 SetXFontComponent(wxXLFD_POINTSIZE
, wxString::Format(_T("%d"), pointsize
));
723 void wxNativeFontInfo::SetStyle(wxFontStyle style
)
728 case wxFONTSTYLE_ITALIC
:
732 case wxFONTSTYLE_SLANT
:
736 case wxFONTSTYLE_NORMAL
:
740 wxFAIL_MSG( _T("unknown wxFontStyle in wxNativeFontInfo::SetStyle") );
744 SetXFontComponent(wxXLFD_SLANT
, s
);
747 void wxNativeFontInfo::SetWeight(wxFontWeight weight
)
752 case wxFONTWEIGHT_BOLD
:
756 case wxFONTWEIGHT_LIGHT
:
760 case wxFONTWEIGHT_NORMAL
:
765 wxFAIL_MSG( _T("unknown wxFontWeight in wxNativeFontInfo::SetWeight") );
769 SetXFontComponent(wxXLFD_WEIGHT
, s
);
772 void wxNativeFontInfo::SetUnderlined(bool WXUNUSED(underlined
))
774 // can't do this under X
777 void wxNativeFontInfo::SetFaceName(const wxString
& facename
)
779 SetXFontComponent(wxXLFD_FAMILY
, facename
);
782 void wxNativeFontInfo::SetFamily(wxFontFamily
WXUNUSED(family
))
784 // wxFontFamily -> X foundry, anyone?
785 wxFAIL_MSG( _T("not implemented") );
787 // SetXFontComponent(wxXLFD_FOUNDRY, ...);
790 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding
)
792 wxNativeEncodingInfo info
;
793 if ( wxGetNativeFontEncoding(encoding
, &info
) )
795 SetXFontComponent(wxXLFD_ENCODING
, info
.xencoding
);
796 SetXFontComponent(wxXLFD_REGISTRY
, info
.xregistry
);
800 // ----------------------------------------------------------------------------
802 // ----------------------------------------------------------------------------
804 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
805 wxNativeEncodingInfo
*info
)
807 wxCHECK_MSG( info
, false, _T("bad pointer in wxGetNativeFontEncoding") );
809 if ( encoding
== wxFONTENCODING_DEFAULT
)
811 encoding
= wxFont::GetDefaultEncoding();
816 case wxFONTENCODING_ISO8859_1
:
817 case wxFONTENCODING_ISO8859_2
:
818 case wxFONTENCODING_ISO8859_3
:
819 case wxFONTENCODING_ISO8859_4
:
820 case wxFONTENCODING_ISO8859_5
:
821 case wxFONTENCODING_ISO8859_6
:
822 case wxFONTENCODING_ISO8859_7
:
823 case wxFONTENCODING_ISO8859_8
:
824 case wxFONTENCODING_ISO8859_9
:
825 case wxFONTENCODING_ISO8859_10
:
826 case wxFONTENCODING_ISO8859_11
:
827 case wxFONTENCODING_ISO8859_12
:
828 case wxFONTENCODING_ISO8859_13
:
829 case wxFONTENCODING_ISO8859_14
:
830 case wxFONTENCODING_ISO8859_15
:
832 int cp
= encoding
- wxFONTENCODING_ISO8859_1
+ 1;
833 info
->xregistry
= wxT("iso8859");
834 info
->xencoding
.Printf(wxT("%d"), cp
);
838 case wxFONTENCODING_UTF8
:
839 info
->xregistry
= wxT("iso10646");
840 info
->xencoding
= wxT("*");
843 case wxFONTENCODING_GB2312
:
844 info
->xregistry
= wxT("GB2312"); // or the otherway round?
845 info
->xencoding
= wxT("*");
848 case wxFONTENCODING_KOI8
:
849 case wxFONTENCODING_KOI8_U
:
850 info
->xregistry
= wxT("koi8");
852 // we don't make distinction between koi8-r, koi8-u and koi8-ru (so far)
853 info
->xencoding
= wxT("*");
856 case wxFONTENCODING_CP1250
:
857 case wxFONTENCODING_CP1251
:
858 case wxFONTENCODING_CP1252
:
859 case wxFONTENCODING_CP1253
:
860 case wxFONTENCODING_CP1254
:
861 case wxFONTENCODING_CP1255
:
862 case wxFONTENCODING_CP1256
:
863 case wxFONTENCODING_CP1257
:
865 int cp
= encoding
- wxFONTENCODING_CP1250
+ 1250;
866 info
->xregistry
= wxT("microsoft");
867 info
->xencoding
.Printf(wxT("cp%d"), cp
);
871 case wxFONTENCODING_EUC_JP
:
872 case wxFONTENCODING_SHIFT_JIS
:
873 info
->xregistry
= "jis*";
874 info
->xencoding
= "*";
877 case wxFONTENCODING_SYSTEM
:
879 info
->xencoding
= wxT("*");
883 // don't know how to translate this encoding into X fontspec
887 info
->encoding
= encoding
;
892 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
895 fontspec
.Printf(_T("-*-%s-*-*-*-*-*-*-*-*-*-*-%s-%s"),
896 !info
.facename
? _T("*") : info
.facename
.c_str(),
897 info
.xregistry
.c_str(),
898 info
.xencoding
.c_str());
900 return wxTestFontSpec(fontspec
);
903 // ----------------------------------------------------------------------------
904 // X-specific functions
905 // ----------------------------------------------------------------------------
907 wxNativeFont
wxLoadQueryNearestFont(int pointSize
,
912 const wxString
&facename
,
913 wxFontEncoding encoding
,
916 if ( encoding
== wxFONTENCODING_DEFAULT
)
918 encoding
= wxFont::GetDefaultEncoding();
921 // first determine the encoding - if the font doesn't exist at all in this
922 // encoding, it's useless to do all other approximations (i.e. size,
923 // family &c don't matter much)
924 wxNativeEncodingInfo info
;
925 if ( encoding
== wxFONTENCODING_SYSTEM
)
927 // This will always work so we don't test to save time
928 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
932 if ( !wxGetNativeFontEncoding(encoding
, &info
) ||
933 !wxTestFontEncoding(info
) )
936 if ( !wxFontMapper::Get()->GetAltForEncoding(encoding
, &info
) )
937 #endif // wxUSE_FONTMAP
939 // unspported encoding - replace it with the default
941 // NB: we can't just return 0 from here because wxGTK code doesn't
942 // check for it (i.e. it supposes that we'll always succeed),
943 // so it would provoke a crash
944 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
949 // OK, we have the correct xregistry/xencoding in info structure
950 wxNativeFont font
= 0;
952 // if we already have the X font name, try to use it
953 if( xFontName
&& !xFontName
->empty() )
956 // Make sure point size is correct for scale factor.
958 wxStringTokenizer
tokenizer(*xFontName
, _T("-"), wxTOKEN_RET_DELIMS
);
959 wxString newFontName
;
961 for(int i
= 0; i
< 8; i
++)
962 newFontName
+= tokenizer
.NextToken();
964 (void) tokenizer
.NextToken();
966 newFontName
+= wxString::Format(wxT("%d-"), pointSize
);
968 while(tokenizer
.HasMoreTokens())
969 newFontName
+= tokenizer
.GetNextToken();
971 font
= wxLoadFont(newFontName
);
974 *xFontName
= newFontName
;
979 // search up and down by stepsize 10
980 int max_size
= pointSize
+ 20 * (1 + (pointSize
/180));
981 int min_size
= pointSize
- 20 * (1 + (pointSize
/180));
983 int i
, round
; // counters
985 // first round: search for equal, then for smaller and for larger size with the given weight and style
986 int testweight
= weight
;
987 int teststyle
= style
;
989 for ( round
= 0; round
< 3; round
++ )
991 // second round: use normal weight
994 if ( testweight
!= wxNORMAL
)
996 testweight
= wxNORMAL
;
1000 ++round
; // fall through to third round
1004 // third round: ... and use normal style
1007 if ( teststyle
!= wxNORMAL
)
1009 teststyle
= wxNORMAL
;
1016 // Search for equal or smaller size (approx.)
1017 for ( i
= pointSize
; !font
&& i
>= 10 && i
>= min_size
; i
-= 10 )
1019 font
= wxLoadQueryFont(i
, family
, teststyle
, testweight
, underlined
,
1020 facename
, info
.xregistry
, info
.xencoding
,
1024 // Search for larger size (approx.)
1025 for ( i
= pointSize
+ 10; !font
&& i
<= max_size
; i
+= 10 )
1027 font
= wxLoadQueryFont(i
, family
, teststyle
, testweight
, underlined
,
1028 facename
, info
.xregistry
, info
.xencoding
,
1033 // Try default family
1034 if ( !font
&& family
!= wxDEFAULT
)
1036 font
= wxLoadQueryFont(pointSize
, wxDEFAULT
, style
, weight
,
1037 underlined
, facename
,
1038 info
.xregistry
, info
.xencoding
,
1042 // ignore size, family, style and weight but try to find font with the
1043 // given facename and encoding
1046 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1047 underlined
, facename
,
1048 info
.xregistry
, info
.xencoding
,
1051 // ignore family as well
1054 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1055 underlined
, wxEmptyString
,
1056 info
.xregistry
, info
.xencoding
,
1059 // if it still failed, try to get the font of any size but
1060 // with the requested encoding: this can happen if the
1061 // encoding is only available in one size which happens to be
1062 // different from 120
1065 font
= wxLoadQueryFont(-1, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1066 false, wxEmptyString
,
1067 info
.xregistry
, info
.xencoding
,
1070 // this should never happen as we had tested for it in the
1071 // very beginning, but if it does, do return something non
1072 // NULL or we'd crash in wxFont code
1075 wxFAIL_MSG( _T("this encoding should be available!") );
1077 font
= wxLoadQueryFont(-1,
1078 wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1079 false, wxEmptyString
,
1091 // ----------------------------------------------------------------------------
1092 // private functions
1093 // ----------------------------------------------------------------------------
1095 // returns true if there are any fonts matching this font spec
1096 static bool wxTestFontSpec(const wxString
& fontspec
)
1098 // some X servers will fail to load this font because there are too many
1099 // matches so we must test explicitly for this
1100 if ( fontspec
== _T("-*-*-*-*-*-*-*-*-*-*-*-*-*-*") )
1105 wxNativeFont test
= (wxNativeFont
) g_fontHash
->Get( fontspec
);
1111 test
= wxLoadFont(fontspec
);
1112 g_fontHash
->Put( fontspec
, (wxObject
*) test
);
1126 static wxNativeFont
wxLoadQueryFont(int pointSize
,
1130 bool WXUNUSED(underlined
),
1131 const wxString
& facename
,
1132 const wxString
& xregistry
,
1133 const wxString
& xencoding
,
1134 wxString
* xFontName
)
1139 case wxDECORATIVE
: xfamily
= wxT("lucida"); break;
1140 case wxROMAN
: xfamily
= wxT("times"); break;
1141 case wxMODERN
: xfamily
= wxT("courier"); break;
1142 case wxSWISS
: xfamily
= wxT("helvetica"); break;
1143 case wxTELETYPE
: xfamily
= wxT("lucidatypewriter"); break;
1144 case wxSCRIPT
: xfamily
= wxT("utopia"); break;
1145 default: xfamily
= wxT("*");
1153 xweight
= MWLF_WEIGHT_BOLD
;
1158 xweight
= MWLF_WEIGHT_LIGHT
;
1163 xweight
= MWLF_WEIGHT_NORMAL
;
1169 xweight
= MWLF_WEIGHT_DEFAULT
;
1173 GR_SCREEN_INFO screenInfo
;
1174 GrGetScreenInfo(& screenInfo
);
1176 int yPixelsPerCM
= screenInfo
.ydpcm
;
1178 // A point is 1/72 of an inch.
1179 // An inch is 2.541 cm.
1180 // So pixelHeight = (pointSize / 72) (inches) * 2.541 (for cm) * yPixelsPerCM (for pixels)
1181 // In fact pointSize is 10 * the normal point size so
1184 int pixelHeight
= (int) ( (((float)pointSize
) / 720.0) * 2.541 * (float) yPixelsPerCM
) ;
1186 // An alternative: assume that the screen is 72 dpi.
1187 //int pixelHeight = (int) (((float)pointSize / 720.0) * 72.0) ;
1188 //int pixelHeight = (int) ((float)pointSize / 10.0) ;
1191 logFont
.lfHeight
= pixelHeight
;
1192 logFont
.lfWidth
= 0;
1193 logFont
.lfEscapement
= 0;
1194 logFont
.lfOrientation
= 0;
1195 logFont
.lfWeight
= xweight
;
1196 logFont
.lfItalic
= (style
== wxNORMAL
? 0 : 1) ;
1197 logFont
.lfUnderline
= 0;
1198 logFont
.lfStrikeOut
= 0;
1199 logFont
.lfCharSet
= MWLF_CHARSET_DEFAULT
; // TODO: select appropriate one
1200 logFont
.lfOutPrecision
= MWLF_TYPE_DEFAULT
;
1201 logFont
.lfClipPrecision
= 0; // Not used
1202 logFont
.lfRoman
= (family
== wxROMAN
? 1 : 0) ;
1203 logFont
.lfSerif
= (family
== wxSWISS
? 0 : 1) ;
1204 logFont
.lfSansSerif
= !logFont
.lfSerif
;
1205 logFont
.lfModern
= (family
== wxMODERN
? 1 : 0) ;
1206 logFont
.lfProportional
= (family
== wxTELETYPE
? 0 : 1) ;
1207 logFont
.lfOblique
= 0;
1208 logFont
.lfSmallCaps
= 0;
1209 logFont
.lfPitch
= 0; // 0 = default
1210 strcpy(logFont
.lfFaceName
, facename
.c_str());
1212 XFontStruct
* fontInfo
= (XFontStruct
*) malloc(sizeof(XFontStruct
));
1213 fontInfo
->fid
= GrCreateFont((GR_CHAR
*) facename
.c_str(), pixelHeight
, & logFont
);
1214 GrGetFontInfo(fontInfo
->fid
, & fontInfo
->info
);
1215 return (wxNativeFont
) fontInfo
;
1219 if (!facename
.empty())
1221 fontSpec
.Printf(wxT("-*-%s-*-*-normal-*-*-*-*-*-*-*-*-*"),
1224 if ( wxTestFontSpec(fontSpec
) )
1228 //else: no such family, use default one instead
1235 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1237 if ( wxTestFontSpec(fontSpec
) )
1242 // fall through - try wxITALIC now
1245 fontSpec
.Printf(wxT("-*-%s-*-i-*-*-*-*-*-*-*-*-*-*"),
1247 if ( wxTestFontSpec(fontSpec
) )
1251 else if ( style
== wxITALIC
) // and not wxSLANT
1254 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1256 if ( wxTestFontSpec(fontSpec
) )
1262 // no italic, no slant - leave default
1269 wxFAIL_MSG(_T("unknown font style"));
1270 // fall back to normal
1282 fontSpec
.Printf(wxT("-*-%s-bold-*-*-*-*-*-*-*-*-*-*-*"),
1284 if ( wxTestFontSpec(fontSpec
) )
1286 xweight
= wxT("bold");
1289 fontSpec
.Printf(wxT("-*-%s-heavy-*-*-*-*-*-*-*-*-*-*-*"),
1291 if ( wxTestFontSpec(fontSpec
) )
1293 xweight
= wxT("heavy");
1296 fontSpec
.Printf(wxT("-*-%s-extrabold-*-*-*-*-*-*-*-*-*-*-*"),
1298 if ( wxTestFontSpec(fontSpec
) )
1300 xweight
= wxT("extrabold");
1303 fontSpec
.Printf(wxT("-*-%s-demibold-*-*-*-*-*-*-*-*-*-*-*"),
1305 if ( wxTestFontSpec(fontSpec
) )
1307 xweight
= wxT("demibold");
1310 fontSpec
.Printf(wxT("-*-%s-black-*-*-*-*-*-*-*-*-*-*-*"),
1312 if ( wxTestFontSpec(fontSpec
) )
1314 xweight
= wxT("black");
1317 fontSpec
.Printf(wxT("-*-%s-ultrablack-*-*-*-*-*-*-*-*-*-*-*"),
1319 if ( wxTestFontSpec(fontSpec
) )
1321 xweight
= wxT("ultrablack");
1328 fontSpec
.Printf(wxT("-*-%s-light-*-*-*-*-*-*-*-*-*-*-*"),
1330 if ( wxTestFontSpec(fontSpec
) )
1332 xweight
= wxT("light");
1335 fontSpec
.Printf(wxT("-*-%s-thin-*-*-*-*-*-*-*-*-*-*-*"),
1337 if ( wxTestFontSpec(fontSpec
) )
1339 xweight
= wxT("thin");
1346 fontSpec
.Printf(wxT("-*-%s-medium-*-*-*-*-*-*-*-*-*-*-*"),
1348 if ( wxTestFontSpec(fontSpec
) )
1350 xweight
= wxT("medium");
1353 fontSpec
.Printf(wxT("-*-%s-normal-*-*-*-*-*-*-*-*-*-*-*"),
1355 if ( wxTestFontSpec(fontSpec
) )
1357 xweight
= wxT("normal");
1360 fontSpec
.Printf(wxT("-*-%s-regular-*-*-*-*-*-*-*-*-*-*-*"),
1362 if ( wxTestFontSpec(fontSpec
) )
1364 xweight
= wxT("regular");
1370 default: xweight
= wxT("*"); break;
1373 // if pointSize is -1, don't specify any
1375 if ( pointSize
== -1 )
1381 sizeSpec
.Printf(_T("%d"), pointSize
);
1384 // construct the X font spec from our data
1385 fontSpec
.Printf(wxT("-*-%s-%s-%s-normal-*-*-%s-*-*-*-*-%s-%s"),
1386 xfamily
.c_str(), xweight
.c_str(), xstyle
.c_str(),
1387 sizeSpec
.c_str(), xregistry
.c_str(), xencoding
.c_str());
1390 *xFontName
= fontSpec
;
1392 return wxLoadFont(fontSpec
);
1397 // ----------------------------------------------------------------------------
1399 // ----------------------------------------------------------------------------
1401 class wxFontModule
: public wxModule
1408 DECLARE_DYNAMIC_CLASS(wxFontModule
)
1411 IMPLEMENT_DYNAMIC_CLASS(wxFontModule
, wxModule
)
1413 bool wxFontModule::OnInit()
1415 g_fontHash
= new wxHashTable( wxKEY_STRING
);
1420 void wxFontModule::OnExit()
1424 g_fontHash
= (wxHashTable
*)NULL
;
1427 #endif // GTK 2.0/1.x