1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/unix/fontutil.cpp
3 // Purpose: Font helper functions for wxX11, wxGTK, wxMotif
4 // Author: Vadim Zeitlin
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #include "wx/fontutil.h"
31 #include "wx/font.h" // wxFont enums
32 #include "wx/encinfo.h"
34 #include "wx/utils.h" // for wxGetDisplay()
35 #include "wx/module.h"
38 #include "wx/fontmap.h"
39 #include "wx/tokenzr.h"
40 #include "wx/fontenum.h"
44 #include "pango/pango.h"
47 #include "wx/gtk/private.h"
48 extern GtkWidget
*wxGetRootWindow();
50 #define wxPANGO_CONV wxGTK_CONV_SYS
51 #define wxPANGO_CONV_BACK wxGTK_CONV_BACK_SYS
53 #include "wx/x11/private.h"
54 #include "wx/gtk/private/string.h"
56 #define wxPANGO_CONV(s) (wxConvUTF8.cWX2MB((s)))
57 #define wxPANGO_CONV_BACK(s) (wxConvUTF8.cMB2WX((s)))
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
64 void wxNativeFontInfo::Init()
70 wxNativeFontInfo::Init(const wxNativeFontInfo
& info
)
73 description
= pango_font_description_copy(info
.description
);
78 void wxNativeFontInfo::Free()
81 pango_font_description_free(description
);
84 int wxNativeFontInfo::GetPointSize() const
86 return pango_font_description_get_size( description
) / PANGO_SCALE
;
89 wxFontStyle
wxNativeFontInfo::GetStyle() const
91 wxFontStyle m_style
= wxFONTSTYLE_NORMAL
;
93 switch (pango_font_description_get_style( description
))
95 case PANGO_STYLE_NORMAL
:
96 m_style
= wxFONTSTYLE_NORMAL
;
98 case PANGO_STYLE_ITALIC
:
99 m_style
= wxFONTSTYLE_ITALIC
;
101 case PANGO_STYLE_OBLIQUE
:
102 m_style
= wxFONTSTYLE_SLANT
;
109 wxFontWeight
wxNativeFontInfo::GetWeight() const
112 // We seem to currently initialize only by string.
113 // In that case PANGO_FONT_MASK_WEIGHT is always set.
114 if (!(pango_font_description_get_set_fields(description
) & PANGO_FONT_MASK_WEIGHT
))
115 return wxFONTWEIGHT_NORMAL
;
118 PangoWeight pango_weight
= pango_font_description_get_weight( description
);
120 // Until the API can be changed the following ranges of weight values are used:
121 // wxFONTWEIGHT_LIGHT: 100 .. 349 - range of 250
122 // wxFONTWEIGHT_NORMAL: 350 .. 599 - range of 250
123 // wxFONTWEIGHT_BOLD: 600 .. 900 - range of 301 (600 is "semibold" already)
125 if (pango_weight
>= 600)
126 return wxFONTWEIGHT_BOLD
;
128 if (pango_weight
< 350)
129 return wxFONTWEIGHT_LIGHT
;
131 return wxFONTWEIGHT_NORMAL
;
134 bool wxNativeFontInfo::GetUnderlined() const
139 wxString
wxNativeFontInfo::GetFaceName() const
141 return wxPANGO_CONV_BACK(pango_font_description_get_family(description
));
144 wxFontFamily
wxNativeFontInfo::GetFamily() const
146 wxFontFamily ret
= wxFONTFAMILY_DEFAULT
;
147 // note: not passing -1 as the 2nd parameter to g_ascii_strdown to work
148 // around a bug in the 64-bit glib shipped with solaris 10, -1 causes it
149 // to try to allocate 2^32 bytes.
150 const char *family_name
= pango_font_description_get_family( description
);
154 wxGtkString
family_text(g_ascii_strdown(family_name
, strlen(family_name
)));
156 // Check for some common fonts, to salvage what we can from the current win32 centric wxFont API:
157 if (strncmp( family_text
, "monospace", 9 ) == 0)
158 ret
= wxFONTFAMILY_TELETYPE
; // begins with "Monospace"
159 else if (strncmp( family_text
, "courier", 7 ) == 0)
160 ret
= wxFONTFAMILY_TELETYPE
; // begins with "Courier"
161 #if defined(__WXGTK20__) || defined(HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE)
164 if (!gtk_check_version(2,4,0))
167 PangoFontFamily
**families
;
168 PangoFontFamily
*family
= NULL
;
170 pango_context_list_families(
172 gtk_widget_get_pango_context( wxGetRootWindow() ),
174 wxTheApp
->GetPangoContext(),
176 &families
, &n_families
);
178 for (int i
= 0;i
< n_families
;++i
)
180 if (g_ascii_strcasecmp(pango_font_family_get_name( families
[i
] ), pango_font_description_get_family( description
)) == 0 )
182 family
= families
[i
];
189 // Some gtk+ systems might query for a non-existing font from wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT)
190 // on initialization, don't assert until wxSystemSettings::GetFont is checked for this - MR
191 // wxASSERT_MSG( family, wxT("wxNativeFontInfo::GetFamily() - No appropriate PangoFontFamily found for ::description") );
193 //BCI: Cache the wxFontFamily inside the class. Validate cache with
194 //BCI: g_ascii_strcasecmp(pango_font_description_get_family(description), pango_font_family_get_name(family)) == 0
196 if (family
!= NULL
&& pango_font_family_is_monospace( family
))
197 ret
= wxFONTFAMILY_TELETYPE
; // is deemed a monospace font by pango
199 #endif // GTK+ 2 || HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE
201 if (ret
== wxFONTFAMILY_DEFAULT
)
203 if (strstr( family_text
, "sans" ) != NULL
) // checked before serif, so that "* Sans Serif" fonts are detected correctly
204 ret
= wxFONTFAMILY_SWISS
; // contains "Sans"
205 else if (strstr( family_text
, "serif" ) != NULL
)
206 ret
= wxFONTFAMILY_ROMAN
; // contains "Serif"
207 else if (strncmp( family_text
, "times", 5 ) == 0)
208 ret
= wxFONTFAMILY_ROMAN
; // begins with "Times"
209 else if (strncmp( family_text
, "old", 3 ) == 0)
210 ret
= wxFONTFAMILY_DECORATIVE
; // Begins with "Old" - "Old English", "Old Town"
216 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
218 return wxFONTENCODING_SYSTEM
;
221 void wxNativeFontInfo::SetPointSize(int pointsize
)
223 pango_font_description_set_size( description
, pointsize
* PANGO_SCALE
);
226 void wxNativeFontInfo::SetStyle(wxFontStyle style
)
230 case wxFONTSTYLE_ITALIC
:
231 pango_font_description_set_style( description
, PANGO_STYLE_ITALIC
);
233 case wxFONTSTYLE_SLANT
:
234 pango_font_description_set_style( description
, PANGO_STYLE_OBLIQUE
);
237 wxFAIL_MSG( _T("unknown font style") );
239 case wxFONTSTYLE_NORMAL
:
240 pango_font_description_set_style( description
, PANGO_STYLE_NORMAL
);
245 void wxNativeFontInfo::SetWeight(wxFontWeight weight
)
249 case wxFONTWEIGHT_BOLD
:
250 pango_font_description_set_weight(description
, PANGO_WEIGHT_BOLD
);
252 case wxFONTWEIGHT_LIGHT
:
253 pango_font_description_set_weight(description
, PANGO_WEIGHT_LIGHT
);
256 wxFAIL_MSG( _T("unknown font weight") );
258 case wxFONTWEIGHT_NORMAL
:
259 pango_font_description_set_weight(description
, PANGO_WEIGHT_NORMAL
);
263 void wxNativeFontInfo::SetUnderlined(bool WXUNUSED(underlined
))
265 wxFAIL_MSG( _T("not implemented") );
268 bool wxNativeFontInfo::SetFaceName(const wxString
& facename
)
270 pango_font_description_set_family(description
, wxPANGO_CONV(facename
));
274 void wxNativeFontInfo::SetFamily(wxFontFamily
WXUNUSED(family
))
276 wxFAIL_MSG( _T("not implemented") );
279 void wxNativeFontInfo::SetEncoding(wxFontEncoding
WXUNUSED(encoding
))
281 wxFAIL_MSG( _T("not implemented") );
286 bool wxNativeFontInfo::FromString(const wxString
& s
)
289 pango_font_description_free( description
);
291 // there is a bug in at least pango <= 1.13 which makes it (or its backends)
292 // segfault for very big point sizes and for negative point sizes.
293 // To workaround that bug for pango <= 1.13
294 // (see http://bugzilla.gnome.org/show_bug.cgi?id=340229)
295 // we do the check on the size here using same (arbitrary) limits used by
296 // pango > 1.13. Note that the segfault could happen also for pointsize
297 // smaller than this limit !!
299 const size_t pos
= str
.find_last_of(_T(" "));
301 if ( pos
!= wxString::npos
&& wxString(str
, pos
+ 1).ToDouble(&size
) )
306 else if ( size
>= 1E6
)
309 if ( !sizeStr
.empty() )
311 // replace the old size with the adjusted one
312 str
= wxString(s
, 0, pos
) + sizeStr
;
316 description
= pango_font_description_from_string(wxPANGO_CONV(str
));
319 // ensure a valid facename is selected
320 if (!wxFontEnumerator::IsValidFacename(GetFaceName()))
321 SetFaceName(wxNORMAL_FONT
->GetFaceName());
322 #endif // wxUSE_FONTENUM
327 wxString
wxNativeFontInfo::ToString() const
329 wxGtkString
str(pango_font_description_to_string( description
));
331 return wxPANGO_CONV_BACK(str
);
334 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
336 return FromString( s
);
339 wxString
wxNativeFontInfo::ToUserString() const
348 #pragma message disable nosimpint
351 #include <X11/Xlib.h>
354 #pragma message enable nosimpint
357 #elif defined(__WXGTK__)
358 // we have to declare struct tm to avoid problems with first forward
359 // declaring it in C code (glib.h included from gdk.h does it) and then
360 // defining it when time.h is included from the headers below - this is
361 // known not to work at least with Sun CC 6.01
368 // ----------------------------------------------------------------------------
370 // ----------------------------------------------------------------------------
372 static wxHashTable
*g_fontHash
= NULL
;
374 // ----------------------------------------------------------------------------
376 // ----------------------------------------------------------------------------
378 // define the functions to create and destroy native fonts for this toolkit
380 wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
382 return XLoadQueryFont((Display
*)wxGetDisplay(), fontSpec
);
385 inline void wxFreeFont(wxNativeFont font
)
387 XFreeFont((Display
*)wxGetDisplay(), (XFontStruct
*)font
);
389 #elif defined(__WXGTK__)
390 wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
392 // VZ: we should use gdk_fontset_load() instead of gdk_font_load()
393 // here to be able to display Japanese fonts correctly (at least
394 // this is what people report) but unfortunately doing it results
395 // in tons of warnings when using GTK with "normal" European
396 // languages and so we can't always do it and I don't know enough
397 // to determine when should this be done... (FIXME)
398 return gdk_font_load( wxConvertWX2MB(fontSpec
) );
401 inline void wxFreeFont(wxNativeFont font
)
403 gdk_font_unref(font
);
406 #error "Unknown GUI toolkit"
409 static bool wxTestFontSpec(const wxString
& fontspec
);
411 static wxNativeFont
wxLoadQueryFont(int pointSize
,
416 const wxString
& facename
,
417 const wxString
& xregistry
,
418 const wxString
& xencoding
,
419 wxString
* xFontName
);
421 // ============================================================================
423 // ============================================================================
425 // ----------------------------------------------------------------------------
426 // wxNativeEncodingInfo
427 // ----------------------------------------------------------------------------
429 // convert to/from the string representation: format is
430 // encodingid;registry;encoding[;facename]
431 bool wxNativeEncodingInfo::FromString(const wxString
& s
)
433 // use ";", not "-" because it may be part of encoding name
434 wxStringTokenizer
tokenizer(s
, _T(";"));
436 wxString encid
= tokenizer
.GetNextToken();
438 if ( !encid
.ToLong(&enc
) )
440 encoding
= (wxFontEncoding
)enc
;
442 xregistry
= tokenizer
.GetNextToken();
446 xencoding
= tokenizer
.GetNextToken();
451 facename
= tokenizer
.GetNextToken();
456 wxString
wxNativeEncodingInfo::ToString() const
459 s
<< (long)encoding
<< _T(';') << xregistry
<< _T(';') << xencoding
;
460 if ( !facename
.empty() )
462 s
<< _T(';') << facename
;
468 // ----------------------------------------------------------------------------
470 // ----------------------------------------------------------------------------
472 void wxNativeFontInfo::Init()
477 bool wxNativeFontInfo::FromString(const wxString
& s
)
479 wxStringTokenizer
tokenizer(s
, _T(";"));
482 wxString token
= tokenizer
.GetNextToken();
483 if ( token
!= _T('0') )
486 xFontName
= tokenizer
.GetNextToken();
488 // this should be the end
489 if ( tokenizer
.HasMoreTokens() )
492 return FromXFontName(xFontName
);
495 wxString
wxNativeFontInfo::ToString() const
498 return wxString::Format(_T("%d;%s"), 0, GetXFontName().c_str());
501 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
503 return FromXFontName(s
);
506 wxString
wxNativeFontInfo::ToUserString() const
508 return GetXFontName();
511 bool wxNativeFontInfo::HasElements() const
513 // we suppose that the foundry is never empty, so if it is it means that we
514 // had never parsed the XLFD
515 return !fontElements
[0].empty();
518 wxString
wxNativeFontInfo::GetXFontComponent(wxXLFDField field
) const
520 wxCHECK_MSG( field
< wxXLFD_MAX
, wxEmptyString
, _T("invalid XLFD field") );
522 if ( !HasElements() )
525 if ( !((wxNativeFontInfo
*)this)->FromXFontName(xFontName
) )
526 return wxEmptyString
;
529 return fontElements
[field
];
532 bool wxNativeFontInfo::FromXFontName(const wxString
& fontname
)
534 // TODO: we should be able to handle the font aliases here, but how?
535 wxStringTokenizer
tokenizer(fontname
, _T("-"));
537 // skip the leading, usually empty field (font name registry)
538 if ( !tokenizer
.HasMoreTokens() )
541 (void)tokenizer
.GetNextToken();
543 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
545 if ( !tokenizer
.HasMoreTokens() )
547 // not enough elements in the XLFD - or maybe an alias
551 wxString field
= tokenizer
.GetNextToken();
552 if ( !field
.empty() && field
!= _T('*') )
554 // we're really initialized now
558 fontElements
[n
] = field
;
561 // this should be all
562 if ( tokenizer
.HasMoreTokens() )
568 wxString
wxNativeFontInfo::GetXFontName() const
570 if ( xFontName
.empty() )
572 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
574 // replace the non specified elements with '*' except for the
575 // additional style which is usually just omitted
576 wxString elt
= fontElements
[n
];
577 if ( elt
.empty() && n
!= wxXLFD_ADDSTYLE
)
583 ((wxNativeFontInfo
*)this)->xFontName
<< _T('-') << elt
;
591 wxNativeFontInfo::SetXFontComponent(wxXLFDField field
, const wxString
& value
)
593 wxCHECK_RET( field
< wxXLFD_MAX
, _T("invalid XLFD field") );
595 // this class should be initialized with a valid font spec first and only
596 // then the fields may be modified!
597 wxASSERT_MSG( !IsDefault(), _T("can't modify an uninitialized XLFD") );
599 if ( !HasElements() )
602 if ( !((wxNativeFontInfo
*)this)->FromXFontName(xFontName
) )
604 wxFAIL_MSG( _T("can't set font element for invalid XLFD") );
610 fontElements
[field
] = value
;
612 // invalidate the XFLD, it doesn't correspond to the font elements any more
616 void wxNativeFontInfo::SetXFontName(const wxString
& xFontName_
)
618 // invalidate the font elements, GetXFontComponent() will reparse the XLFD
619 fontElements
[0].clear();
621 xFontName
= xFontName_
;
626 int wxNativeFontInfo::GetPointSize() const
628 const wxString s
= GetXFontComponent(wxXLFD_POINTSIZE
);
630 // return -1 to indicate that the size is unknown
632 return s
.ToLong(&l
) ? l
: -1;
635 wxFontStyle
wxNativeFontInfo::GetStyle() const
637 const wxString s
= GetXFontComponent(wxXLFD_SLANT
);
639 if ( s
.length() != 1 )
641 // it is really unknown but we don't have any way to return it from
643 return wxFONTSTYLE_NORMAL
;
646 switch ( s
[0].GetValue() )
649 // again, unknown but consider normal by default
652 return wxFONTSTYLE_NORMAL
;
655 return wxFONTSTYLE_ITALIC
;
658 return wxFONTSTYLE_SLANT
;
662 wxFontWeight
wxNativeFontInfo::GetWeight() const
664 const wxString s
= GetXFontComponent(wxXLFD_WEIGHT
).MakeLower();
665 if ( s
.find(_T("bold")) != wxString::npos
|| s
== _T("black") )
666 return wxFONTWEIGHT_BOLD
;
667 else if ( s
== _T("light") )
668 return wxFONTWEIGHT_LIGHT
;
670 return wxFONTWEIGHT_NORMAL
;
673 bool wxNativeFontInfo::GetUnderlined() const
675 // X fonts are never underlined
679 wxString
wxNativeFontInfo::GetFaceName() const
681 // wxWidgets facename probably more accurately corresponds to X family
682 return GetXFontComponent(wxXLFD_FAMILY
);
685 wxFontFamily
wxNativeFontInfo::GetFamily() const
687 // and wxWidgets family -- to X foundry, but we have to translate it to
688 // wxFontFamily somehow...
689 wxFAIL_MSG(_T("not implemented")); // GetXFontComponent(wxXLFD_FOUNDRY);
691 return wxFONTFAMILY_DEFAULT
;
694 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
696 // we already have the code for this but need to refactor it first
697 wxFAIL_MSG( _T("not implemented") );
699 return wxFONTENCODING_MAX
;
702 void wxNativeFontInfo::SetPointSize(int pointsize
)
704 SetXFontComponent(wxXLFD_POINTSIZE
, wxString::Format(_T("%d"), pointsize
));
707 void wxNativeFontInfo::SetStyle(wxFontStyle style
)
712 case wxFONTSTYLE_ITALIC
:
716 case wxFONTSTYLE_SLANT
:
720 case wxFONTSTYLE_NORMAL
:
724 wxFAIL_MSG( _T("unknown wxFontStyle in wxNativeFontInfo::SetStyle") );
728 SetXFontComponent(wxXLFD_SLANT
, s
);
731 void wxNativeFontInfo::SetWeight(wxFontWeight weight
)
736 case wxFONTWEIGHT_BOLD
:
740 case wxFONTWEIGHT_LIGHT
:
744 case wxFONTWEIGHT_NORMAL
:
749 wxFAIL_MSG( _T("unknown wxFontWeight in wxNativeFontInfo::SetWeight") );
753 SetXFontComponent(wxXLFD_WEIGHT
, s
);
756 void wxNativeFontInfo::SetUnderlined(bool WXUNUSED(underlined
))
758 // can't do this under X
761 bool wxNativeFontInfo::SetFaceName(const wxString
& facename
)
763 SetXFontComponent(wxXLFD_FAMILY
, facename
);
767 void wxNativeFontInfo::SetFamily(wxFontFamily
WXUNUSED(family
))
769 // wxFontFamily -> X foundry, anyone?
770 wxFAIL_MSG( _T("not implemented") );
772 // SetXFontComponent(wxXLFD_FOUNDRY, ...);
775 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding
)
777 wxNativeEncodingInfo info
;
778 if ( wxGetNativeFontEncoding(encoding
, &info
) )
780 SetXFontComponent(wxXLFD_ENCODING
, info
.xencoding
);
781 SetXFontComponent(wxXLFD_REGISTRY
, info
.xregistry
);
785 // ----------------------------------------------------------------------------
787 // ----------------------------------------------------------------------------
789 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
790 wxNativeEncodingInfo
*info
)
792 wxCHECK_MSG( info
, false, _T("bad pointer in wxGetNativeFontEncoding") );
794 if ( encoding
== wxFONTENCODING_DEFAULT
)
796 encoding
= wxFont::GetDefaultEncoding();
801 case wxFONTENCODING_ISO8859_1
:
802 case wxFONTENCODING_ISO8859_2
:
803 case wxFONTENCODING_ISO8859_3
:
804 case wxFONTENCODING_ISO8859_4
:
805 case wxFONTENCODING_ISO8859_5
:
806 case wxFONTENCODING_ISO8859_6
:
807 case wxFONTENCODING_ISO8859_7
:
808 case wxFONTENCODING_ISO8859_8
:
809 case wxFONTENCODING_ISO8859_9
:
810 case wxFONTENCODING_ISO8859_10
:
811 case wxFONTENCODING_ISO8859_11
:
812 case wxFONTENCODING_ISO8859_12
:
813 case wxFONTENCODING_ISO8859_13
:
814 case wxFONTENCODING_ISO8859_14
:
815 case wxFONTENCODING_ISO8859_15
:
817 int cp
= encoding
- wxFONTENCODING_ISO8859_1
+ 1;
818 info
->xregistry
= wxT("iso8859");
819 info
->xencoding
.Printf(wxT("%d"), cp
);
823 case wxFONTENCODING_UTF8
:
824 info
->xregistry
= wxT("iso10646");
825 info
->xencoding
= wxT("*");
828 case wxFONTENCODING_GB2312
:
829 info
->xregistry
= wxT("GB2312"); // or the otherway round?
830 info
->xencoding
= wxT("*");
833 case wxFONTENCODING_KOI8
:
834 case wxFONTENCODING_KOI8_U
:
835 info
->xregistry
= wxT("koi8");
837 // we don't make distinction between koi8-r, koi8-u and koi8-ru (so far)
838 info
->xencoding
= wxT("*");
841 case wxFONTENCODING_CP1250
:
842 case wxFONTENCODING_CP1251
:
843 case wxFONTENCODING_CP1252
:
844 case wxFONTENCODING_CP1253
:
845 case wxFONTENCODING_CP1254
:
846 case wxFONTENCODING_CP1255
:
847 case wxFONTENCODING_CP1256
:
848 case wxFONTENCODING_CP1257
:
850 int cp
= encoding
- wxFONTENCODING_CP1250
+ 1250;
851 info
->xregistry
= wxT("microsoft");
852 info
->xencoding
.Printf(wxT("cp%d"), cp
);
856 case wxFONTENCODING_EUC_JP
:
857 case wxFONTENCODING_SHIFT_JIS
:
858 info
->xregistry
= "jis*";
859 info
->xencoding
= "*";
862 case wxFONTENCODING_SYSTEM
:
864 info
->xencoding
= wxT("*");
868 // don't know how to translate this encoding into X fontspec
872 info
->encoding
= encoding
;
877 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
880 fontspec
.Printf(_T("-*-%s-*-*-*-*-*-*-*-*-*-*-%s-%s"),
881 !info
.facename
? _T("*") : info
.facename
.c_str(),
882 info
.xregistry
.c_str(),
883 info
.xencoding
.c_str());
885 return wxTestFontSpec(fontspec
);
888 // ----------------------------------------------------------------------------
889 // X-specific functions
890 // ----------------------------------------------------------------------------
892 wxNativeFont
wxLoadQueryNearestFont(int pointSize
,
897 const wxString
&facename
,
898 wxFontEncoding encoding
,
901 if ( encoding
== wxFONTENCODING_DEFAULT
)
903 encoding
= wxFont::GetDefaultEncoding();
906 // first determine the encoding - if the font doesn't exist at all in this
907 // encoding, it's useless to do all other approximations (i.e. size,
908 // family &c don't matter much)
909 wxNativeEncodingInfo info
;
910 if ( encoding
== wxFONTENCODING_SYSTEM
)
912 // This will always work so we don't test to save time
913 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
917 if ( !wxGetNativeFontEncoding(encoding
, &info
) ||
918 !wxTestFontEncoding(info
) )
921 if ( !wxFontMapper::Get()->GetAltForEncoding(encoding
, &info
) )
922 #endif // wxUSE_FONTMAP
924 // unspported encoding - replace it with the default
926 // NB: we can't just return 0 from here because wxGTK code doesn't
927 // check for it (i.e. it supposes that we'll always succeed),
928 // so it would provoke a crash
929 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
934 // OK, we have the correct xregistry/xencoding in info structure
935 wxNativeFont font
= 0;
937 // if we already have the X font name, try to use it
938 if( xFontName
&& !xFontName
->empty() )
941 // Make sure point size is correct for scale factor.
943 wxStringTokenizer
tokenizer(*xFontName
, _T("-"), wxTOKEN_RET_DELIMS
);
944 wxString newFontName
;
946 for(int i
= 0; i
< 8; i
++)
947 newFontName
+= tokenizer
.NextToken();
949 (void) tokenizer
.NextToken();
951 newFontName
+= wxString::Format(wxT("%d-"), pointSize
);
953 while(tokenizer
.HasMoreTokens())
954 newFontName
+= tokenizer
.GetNextToken();
956 font
= wxLoadFont(newFontName
);
959 *xFontName
= newFontName
;
964 // search up and down by stepsize 10
965 int max_size
= pointSize
+ 20 * (1 + (pointSize
/180));
966 int min_size
= pointSize
- 20 * (1 + (pointSize
/180));
968 int i
, round
; // counters
970 // first round: search for equal, then for smaller and for larger size with the given weight and style
971 int testweight
= weight
;
972 int teststyle
= style
;
974 for ( round
= 0; round
< 3; round
++ )
976 // second round: use normal weight
979 if ( testweight
!= wxNORMAL
)
981 testweight
= wxNORMAL
;
985 ++round
; // fall through to third round
989 // third round: ... and use normal style
992 if ( teststyle
!= wxNORMAL
)
994 teststyle
= wxNORMAL
;
1001 // Search for equal or smaller size (approx.)
1002 for ( i
= pointSize
; !font
&& i
>= 10 && i
>= min_size
; i
-= 10 )
1004 font
= wxLoadQueryFont(i
, family
, teststyle
, testweight
, underlined
,
1005 facename
, info
.xregistry
, info
.xencoding
,
1009 // Search for larger size (approx.)
1010 for ( i
= pointSize
+ 10; !font
&& i
<= max_size
; i
+= 10 )
1012 font
= wxLoadQueryFont(i
, family
, teststyle
, testweight
, underlined
,
1013 facename
, info
.xregistry
, info
.xencoding
,
1018 // Try default family
1019 if ( !font
&& family
!= wxDEFAULT
)
1021 font
= wxLoadQueryFont(pointSize
, wxDEFAULT
, style
, weight
,
1022 underlined
, facename
,
1023 info
.xregistry
, info
.xencoding
,
1027 // ignore size, family, style and weight but try to find font with the
1028 // given facename and encoding
1031 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1032 underlined
, facename
,
1033 info
.xregistry
, info
.xencoding
,
1036 // ignore family as well
1039 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1040 underlined
, wxEmptyString
,
1041 info
.xregistry
, info
.xencoding
,
1044 // if it still failed, try to get the font of any size but
1045 // with the requested encoding: this can happen if the
1046 // encoding is only available in one size which happens to be
1047 // different from 120
1050 font
= wxLoadQueryFont(-1, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1051 false, wxEmptyString
,
1052 info
.xregistry
, info
.xencoding
,
1055 // this should never happen as we had tested for it in the
1056 // very beginning, but if it does, do return something non
1057 // NULL or we'd crash in wxFont code
1060 wxFAIL_MSG( _T("this encoding should be available!") );
1062 font
= wxLoadQueryFont(-1,
1063 wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1064 false, wxEmptyString
,
1076 // ----------------------------------------------------------------------------
1077 // private functions
1078 // ----------------------------------------------------------------------------
1080 // returns true if there are any fonts matching this font spec
1081 static bool wxTestFontSpec(const wxString
& fontspec
)
1083 // some X servers will fail to load this font because there are too many
1084 // matches so we must test explicitly for this
1085 if ( fontspec
== _T("-*-*-*-*-*-*-*-*-*-*-*-*-*-*") )
1090 wxNativeFont test
= (wxNativeFont
) g_fontHash
->Get( fontspec
);
1096 test
= wxLoadFont(fontspec
);
1097 g_fontHash
->Put( fontspec
, (wxObject
*) test
);
1111 static wxNativeFont
wxLoadQueryFont(int pointSize
,
1115 bool WXUNUSED(underlined
),
1116 const wxString
& facename
,
1117 const wxString
& xregistry
,
1118 const wxString
& xencoding
,
1119 wxString
* xFontName
)
1124 case wxDECORATIVE
: xfamily
= wxT("lucida"); break;
1125 case wxROMAN
: xfamily
= wxT("times"); break;
1126 case wxMODERN
: xfamily
= wxT("courier"); break;
1127 case wxSWISS
: xfamily
= wxT("helvetica"); break;
1128 case wxTELETYPE
: xfamily
= wxT("lucidatypewriter"); break;
1129 case wxSCRIPT
: xfamily
= wxT("utopia"); break;
1130 default: xfamily
= wxT("*");
1138 xweight
= MWLF_WEIGHT_BOLD
;
1143 xweight
= MWLF_WEIGHT_LIGHT
;
1148 xweight
= MWLF_WEIGHT_NORMAL
;
1154 xweight
= MWLF_WEIGHT_DEFAULT
;
1158 GR_SCREEN_INFO screenInfo
;
1159 GrGetScreenInfo(& screenInfo
);
1161 int yPixelsPerCM
= screenInfo
.ydpcm
;
1163 // A point is 1/72 of an inch.
1164 // An inch is 2.541 cm.
1165 // So pixelHeight = (pointSize / 72) (inches) * 2.541 (for cm) * yPixelsPerCM (for pixels)
1166 // In fact pointSize is 10 * the normal point size so
1169 int pixelHeight
= (int) ( (((float)pointSize
) / 720.0) * 2.541 * (float) yPixelsPerCM
) ;
1171 // An alternative: assume that the screen is 72 dpi.
1172 //int pixelHeight = (int) (((float)pointSize / 720.0) * 72.0) ;
1173 //int pixelHeight = (int) ((float)pointSize / 10.0) ;
1176 logFont
.lfHeight
= pixelHeight
;
1177 logFont
.lfWidth
= 0;
1178 logFont
.lfEscapement
= 0;
1179 logFont
.lfOrientation
= 0;
1180 logFont
.lfWeight
= xweight
;
1181 logFont
.lfItalic
= (style
== wxNORMAL
? 0 : 1) ;
1182 logFont
.lfUnderline
= 0;
1183 logFont
.lfStrikeOut
= 0;
1184 logFont
.lfCharSet
= MWLF_CHARSET_DEFAULT
; // TODO: select appropriate one
1185 logFont
.lfOutPrecision
= MWLF_TYPE_DEFAULT
;
1186 logFont
.lfClipPrecision
= 0; // Not used
1187 logFont
.lfRoman
= (family
== wxROMAN
? 1 : 0) ;
1188 logFont
.lfSerif
= (family
== wxSWISS
? 0 : 1) ;
1189 logFont
.lfSansSerif
= !logFont
.lfSerif
;
1190 logFont
.lfModern
= (family
== wxMODERN
? 1 : 0) ;
1191 logFont
.lfProportional
= (family
== wxTELETYPE
? 0 : 1) ;
1192 logFont
.lfOblique
= 0;
1193 logFont
.lfSmallCaps
= 0;
1194 logFont
.lfPitch
= 0; // 0 = default
1195 strcpy(logFont
.lfFaceName
, facename
.c_str());
1197 XFontStruct
* fontInfo
= (XFontStruct
*) malloc(sizeof(XFontStruct
));
1198 fontInfo
->fid
= GrCreateFont((GR_CHAR
*) facename
.c_str(), pixelHeight
, & logFont
);
1199 GrGetFontInfo(fontInfo
->fid
, & fontInfo
->info
);
1200 return (wxNativeFont
) fontInfo
;
1204 if (!facename
.empty())
1206 fontSpec
.Printf(wxT("-*-%s-*-*-normal-*-*-*-*-*-*-*-*-*"),
1209 if ( wxTestFontSpec(fontSpec
) )
1213 //else: no such family, use default one instead
1220 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1222 if ( wxTestFontSpec(fontSpec
) )
1227 // fall through - try wxITALIC now
1230 fontSpec
.Printf(wxT("-*-%s-*-i-*-*-*-*-*-*-*-*-*-*"),
1232 if ( wxTestFontSpec(fontSpec
) )
1236 else if ( style
== wxITALIC
) // and not wxSLANT
1239 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1241 if ( wxTestFontSpec(fontSpec
) )
1247 // no italic, no slant - leave default
1254 wxFAIL_MSG(_T("unknown font style"));
1255 // fall back to normal
1267 fontSpec
.Printf(wxT("-*-%s-bold-*-*-*-*-*-*-*-*-*-*-*"),
1269 if ( wxTestFontSpec(fontSpec
) )
1271 xweight
= wxT("bold");
1274 fontSpec
.Printf(wxT("-*-%s-heavy-*-*-*-*-*-*-*-*-*-*-*"),
1276 if ( wxTestFontSpec(fontSpec
) )
1278 xweight
= wxT("heavy");
1281 fontSpec
.Printf(wxT("-*-%s-extrabold-*-*-*-*-*-*-*-*-*-*-*"),
1283 if ( wxTestFontSpec(fontSpec
) )
1285 xweight
= wxT("extrabold");
1288 fontSpec
.Printf(wxT("-*-%s-demibold-*-*-*-*-*-*-*-*-*-*-*"),
1290 if ( wxTestFontSpec(fontSpec
) )
1292 xweight
= wxT("demibold");
1295 fontSpec
.Printf(wxT("-*-%s-black-*-*-*-*-*-*-*-*-*-*-*"),
1297 if ( wxTestFontSpec(fontSpec
) )
1299 xweight
= wxT("black");
1302 fontSpec
.Printf(wxT("-*-%s-ultrablack-*-*-*-*-*-*-*-*-*-*-*"),
1304 if ( wxTestFontSpec(fontSpec
) )
1306 xweight
= wxT("ultrablack");
1313 fontSpec
.Printf(wxT("-*-%s-light-*-*-*-*-*-*-*-*-*-*-*"),
1315 if ( wxTestFontSpec(fontSpec
) )
1317 xweight
= wxT("light");
1320 fontSpec
.Printf(wxT("-*-%s-thin-*-*-*-*-*-*-*-*-*-*-*"),
1322 if ( wxTestFontSpec(fontSpec
) )
1324 xweight
= wxT("thin");
1331 fontSpec
.Printf(wxT("-*-%s-medium-*-*-*-*-*-*-*-*-*-*-*"),
1333 if ( wxTestFontSpec(fontSpec
) )
1335 xweight
= wxT("medium");
1338 fontSpec
.Printf(wxT("-*-%s-normal-*-*-*-*-*-*-*-*-*-*-*"),
1340 if ( wxTestFontSpec(fontSpec
) )
1342 xweight
= wxT("normal");
1345 fontSpec
.Printf(wxT("-*-%s-regular-*-*-*-*-*-*-*-*-*-*-*"),
1347 if ( wxTestFontSpec(fontSpec
) )
1349 xweight
= wxT("regular");
1355 default: xweight
= wxT("*"); break;
1358 // if pointSize is -1, don't specify any
1360 if ( pointSize
== -1 )
1366 sizeSpec
.Printf(_T("%d"), pointSize
);
1369 // construct the X font spec from our data
1370 fontSpec
.Printf(wxT("-*-%s-%s-%s-normal-*-*-%s-*-*-*-*-%s-%s"),
1371 xfamily
.c_str(), xweight
.c_str(), xstyle
.c_str(),
1372 sizeSpec
.c_str(), xregistry
.c_str(), xencoding
.c_str());
1375 *xFontName
= fontSpec
;
1377 return wxLoadFont(fontSpec
);
1382 // ----------------------------------------------------------------------------
1384 // ----------------------------------------------------------------------------
1386 class wxFontModule
: public wxModule
1393 DECLARE_DYNAMIC_CLASS(wxFontModule
)
1396 IMPLEMENT_DYNAMIC_CLASS(wxFontModule
, wxModule
)
1398 bool wxFontModule::OnInit()
1400 g_fontHash
= new wxHashTable( wxKEY_STRING
);
1405 void wxFontModule::OnExit()
1412 #endif // GTK 2.0/1.x