1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "fontutil.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
32 #include "wx/font.h" // wxFont enums
33 #include "wx/encinfo.h"
36 #include "wx/fontutil.h"
37 #include "wx/fontmap.h"
38 #include "wx/tokenzr.h"
40 #include "wx/module.h"
44 #include "pango/pango.h"
47 #include "wx/gtk/private.h"
48 extern GtkWidget
*wxGetRootWindow();
50 #include "wx/x11/private.h"
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 void wxNativeFontInfo::Init()
63 wxNativeFontInfo::Init(const wxNativeFontInfo
& info
)
66 description
= pango_font_description_copy(info
.description
);
71 void wxNativeFontInfo::Free()
74 pango_font_description_free(description
);
77 int wxNativeFontInfo::GetPointSize() const
79 return pango_font_description_get_size( description
) / PANGO_SCALE
;
82 wxFontStyle
wxNativeFontInfo::GetStyle() const
84 wxFontStyle m_style
= wxFONTSTYLE_NORMAL
;
86 switch (pango_font_description_get_style( description
))
88 case PANGO_STYLE_NORMAL
:
89 m_style
= wxFONTSTYLE_NORMAL
;
91 case PANGO_STYLE_ITALIC
:
92 m_style
= wxFONTSTYLE_ITALIC
;
94 case PANGO_STYLE_OBLIQUE
:
95 m_style
= wxFONTSTYLE_SLANT
;
102 wxFontWeight
wxNativeFontInfo::GetWeight() const
105 // We seem to currently initialize only by string.
106 // In that case PANGO_FONT_MASK_WEIGHT is always set.
107 if (!(pango_font_description_get_set_fields(description
) & PANGO_FONT_MASK_WEIGHT
))
108 return wxFONTWEIGHT_NORMAL
;
111 PangoWeight pango_weight
= pango_font_description_get_weight( description
);
113 // Until the API can be changed the following ranges of weight values are used:
114 // wxFONTWEIGHT_LIGHT: 100 .. 349 - range of 250
115 // wxFONTWEIGHT_NORMAL: 350 .. 599 - range of 250
116 // wxFONTWEIGHT_BOLD: 600 .. 900 - range of 301 (600 is "semibold" already)
118 if (pango_weight
>= 600)
119 return wxFONTWEIGHT_BOLD
;
121 if (pango_weight
< 350)
122 return wxFONTWEIGHT_LIGHT
;
124 return wxFONTWEIGHT_NORMAL
;
127 bool wxNativeFontInfo::GetUnderlined() const
132 wxString
wxNativeFontInfo::GetFaceName() const
134 wxString tmp
= wxGTK_CONV_BACK( pango_font_description_get_family( description
) );
139 wxFontFamily
wxNativeFontInfo::GetFamily() const
141 #ifndef HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE
142 if (g_ascii_strcasecmp( pango_font_description_get_family( description
), "monospace" ) == 0)
143 return wxFONTFAMILY_TELETYPE
;
146 PangoFontFamily
**families
;
147 PangoFontFamily
*family
;
149 pango_context_list_families(
151 gtk_widget_get_pango_context( wxGetRootWindow() ),
153 wxTheApp
->GetPangoContext(),
155 &families
, &n_families
);
157 for (int i
= 0;i
< n_families
;++i
)
159 if (g_ascii_strcasecmp(pango_font_family_get_name( families
[i
] ), pango_font_description_get_family( description
)) == 0 )
161 family
= families
[i
];
168 wxASSERT_MSG( family
, wxT("wxNativeFontInfo::GetFamily() - No appropriate PangoFontFamily found for ::description") );
170 //BCI: Cache the wxFontFamily inside the class. Validate cache with
171 //BCI: g_ascii_strcasecmp(pango_font_description_get_family(description), pango_font_family_get_name(family)) == 0
173 if (pango_font_family_is_monospace( family
))
174 return wxFONTFAMILY_TELETYPE
;
175 #endif // pango_font_family_is_monospace
177 return wxFONTFAMILY_SWISS
;
180 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
182 return wxFONTENCODING_SYSTEM
;
186 void wxNativeFontInfo::SetPointSize(int WXUNUSED(pointsize
))
188 wxFAIL_MSG( _T("not implemented") );
191 void wxNativeFontInfo::SetStyle(wxFontStyle
WXUNUSED(style
))
193 wxFAIL_MSG( _T("not implemented") );
196 void wxNativeFontInfo::SetWeight(wxFontWeight
WXUNUSED(weight
))
198 wxFAIL_MSG( _T("not implemented") );
201 void wxNativeFontInfo::SetUnderlined(bool WXUNUSED(underlined
))
203 wxFAIL_MSG( _T("not implemented") );
206 void wxNativeFontInfo::SetFaceName(wxString
WXUNUSED(facename
))
208 wxFAIL_MSG( _T("not implemented") );
211 void wxNativeFontInfo::SetFamily(wxFontFamily
WXUNUSED(family
))
213 wxFAIL_MSG( _T("not implemented") );
216 void wxNativeFontInfo::SetEncoding(wxFontEncoding
WXUNUSED(encoding
))
218 wxFAIL_MSG( _T("not implemented") );
223 bool wxNativeFontInfo::FromString(const wxString
& s
)
226 pango_font_description_free( description
);
228 description
= pango_font_description_from_string( wxGTK_CONV( s
) );
233 wxString
wxNativeFontInfo::ToString() const
235 char *str
= pango_font_description_to_string( description
);
236 wxString tmp
= wxGTK_CONV_BACK( str
);
242 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
244 return FromString( s
);
247 wxString
wxNativeFontInfo::ToUserString() const
252 // ----------------------------------------------------------------------------
253 // wxNativeEncodingInfo
254 // ----------------------------------------------------------------------------
256 bool wxNativeEncodingInfo::FromString(const wxString
& s
)
261 wxString
wxNativeEncodingInfo::ToString() const
263 return wxEmptyString
;
266 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
271 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
272 wxNativeEncodingInfo
*info
)
274 // we *must* return true for default encoding as otherwise wxFontMapper
275 // considers that we can't load any font and aborts with wxLogFatalError!
276 if ( encoding
== wxFONTENCODING_SYSTEM
)
278 info
->facename
.clear();
279 info
->encoding
= wxFONTENCODING_SYSTEM
;
282 // pretend that we support everything, it's better than to always return
283 // false as the old code did
291 #pragma message disable nosimpint
294 #include <X11/Xlib.h>
297 #pragma message enable nosimpint
300 #include "wx/utils.h" // for wxGetDisplay()
301 #elif defined(__WXGTK__)
302 // we have to declare struct tm to avoid problems with first forward
303 // declaring it in C code (glib.h included from gdk.h does it) and then
304 // defining it when time.h is included from the headers below - this is
305 // known not to work at least with Sun CC 6.01
312 // ----------------------------------------------------------------------------
314 // ----------------------------------------------------------------------------
316 static wxHashTable
*g_fontHash
= (wxHashTable
*) NULL
;
318 // ----------------------------------------------------------------------------
320 // ----------------------------------------------------------------------------
322 // define the functions to create and destroy native fonts for this toolkit
324 wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
326 return XLoadQueryFont((Display
*)wxGetDisplay(), fontSpec
);
329 inline void wxFreeFont(wxNativeFont font
)
331 XFreeFont((Display
*)wxGetDisplay(), (XFontStruct
*)font
);
333 #elif defined(__WXGTK__)
334 wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
336 // VZ: we should use gdk_fontset_load() instead of gdk_font_load()
337 // here to be able to display Japanese fonts correctly (at least
338 // this is what people report) but unfortunately doing it results
339 // in tons of warnings when using GTK with "normal" European
340 // languages and so we can't always do it and I don't know enough
341 // to determine when should this be done... (FIXME)
342 return gdk_font_load( wxConvertWX2MB(fontSpec
) );
345 inline void wxFreeFont(wxNativeFont font
)
347 gdk_font_unref(font
);
350 #error "Unknown GUI toolkit"
353 static bool wxTestFontSpec(const wxString
& fontspec
);
355 static wxNativeFont
wxLoadQueryFont(int pointSize
,
360 const wxString
& facename
,
361 const wxString
& xregistry
,
362 const wxString
& xencoding
,
363 wxString
* xFontName
);
365 // ============================================================================
367 // ============================================================================
369 // ----------------------------------------------------------------------------
370 // wxNativeEncodingInfo
371 // ----------------------------------------------------------------------------
373 // convert to/from the string representation: format is
374 // encodingid;registry;encoding[;facename]
375 bool wxNativeEncodingInfo::FromString(const wxString
& s
)
377 // use ";", not "-" because it may be part of encoding name
378 wxStringTokenizer
tokenizer(s
, _T(";"));
380 wxString encid
= tokenizer
.GetNextToken();
382 if ( !encid
.ToLong(&enc
) )
384 encoding
= (wxFontEncoding
)enc
;
386 xregistry
= tokenizer
.GetNextToken();
390 xencoding
= tokenizer
.GetNextToken();
395 facename
= tokenizer
.GetNextToken();
400 wxString
wxNativeEncodingInfo::ToString() const
403 s
<< (long)encoding
<< _T(';') << xregistry
<< _T(';') << xencoding
;
406 s
<< _T(';') << facename
;
412 // ----------------------------------------------------------------------------
414 // ----------------------------------------------------------------------------
416 void wxNativeFontInfo::Init()
421 bool wxNativeFontInfo::FromString(const wxString
& s
)
423 wxStringTokenizer
tokenizer(s
, _T(";"));
426 wxString token
= tokenizer
.GetNextToken();
427 if ( token
!= _T('0') )
430 xFontName
= tokenizer
.GetNextToken();
432 // this should be the end
433 if ( tokenizer
.HasMoreTokens() )
436 return FromXFontName(xFontName
);
439 wxString
wxNativeFontInfo::ToString() const
442 return wxString::Format(_T("%d;%s"), 0, GetXFontName().c_str());
445 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
447 return FromXFontName(s
);
450 wxString
wxNativeFontInfo::ToUserString() const
452 return GetXFontName();
455 bool wxNativeFontInfo::HasElements() const
457 // we suppose that the foundry is never empty, so if it is it means that we
458 // had never parsed the XLFD
459 return !fontElements
[0].empty();
462 wxString
wxNativeFontInfo::GetXFontComponent(wxXLFDField field
) const
464 wxCHECK_MSG( field
< wxXLFD_MAX
, _T(""), _T("invalid XLFD field") );
466 if ( !HasElements() )
469 if ( !((wxNativeFontInfo
*)this)->FromXFontName(xFontName
) )
473 return fontElements
[field
];
476 bool wxNativeFontInfo::FromXFontName(const wxString
& fontname
)
478 // TODO: we should be able to handle the font aliases here, but how?
479 wxStringTokenizer
tokenizer(fontname
, _T("-"));
481 // skip the leading, usually empty field (font name registry)
482 if ( !tokenizer
.HasMoreTokens() )
485 (void)tokenizer
.GetNextToken();
487 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
489 if ( !tokenizer
.HasMoreTokens() )
491 // not enough elements in the XLFD - or maybe an alias
495 wxString field
= tokenizer
.GetNextToken();
496 if ( !field
.empty() && field
!= _T('*') )
498 // we're really initialized now
502 fontElements
[n
] = field
;
505 // this should be all
506 if ( tokenizer
.HasMoreTokens() )
512 wxString
wxNativeFontInfo::GetXFontName() const
514 if ( xFontName
.empty() )
516 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
518 // replace the non specified elements with '*' except for the
519 // additional style which is usually just omitted
520 wxString elt
= fontElements
[n
];
521 if ( elt
.empty() && n
!= wxXLFD_ADDSTYLE
)
527 ((wxNativeFontInfo
*)this)->xFontName
<< _T('-') << elt
;
535 wxNativeFontInfo::SetXFontComponent(wxXLFDField field
, const wxString
& value
)
537 wxCHECK_RET( field
< wxXLFD_MAX
, _T("invalid XLFD field") );
539 // this class should be initialized with a valid font spec first and only
540 // then the fields may be modified!
541 wxASSERT_MSG( !IsDefault(), _T("can't modify an uninitialized XLFD") );
543 if ( !HasElements() )
546 if ( !((wxNativeFontInfo
*)this)->FromXFontName(xFontName
) )
548 wxFAIL_MSG( _T("can't set font element for invalid XLFD") );
554 fontElements
[field
] = value
;
556 // invalidate the XFLD, it doesn't correspond to the font elements any more
560 void wxNativeFontInfo::SetXFontName(const wxString
& xFontName_
)
562 // invalidate the font elements, GetXFontComponent() will reparse the XLFD
563 fontElements
[0].clear();
565 xFontName
= xFontName_
;
570 int wxNativeFontInfo::GetPointSize() const
572 const wxString s
= GetXFontComponent(wxXLFD_POINTSIZE
);
574 // return -1 to indicate that the size is unknown
576 return s
.ToLong(&l
) ? l
: -1;
579 wxFontStyle
wxNativeFontInfo::GetStyle() const
581 const wxString s
= GetXFontComponent(wxXLFD_SLANT
);
583 if ( s
.length() != 1 )
585 // it is really unknown but we don't have any way to return it from
587 return wxFONTSTYLE_NORMAL
;
593 // again, unknown but consider normal by default
596 return wxFONTSTYLE_NORMAL
;
599 return wxFONTSTYLE_ITALIC
;
602 return wxFONTSTYLE_SLANT
;
606 wxFontWeight
wxNativeFontInfo::GetWeight() const
608 const wxString s
= GetXFontComponent(wxXLFD_WEIGHT
).MakeLower();
609 if ( s
.find(_T("bold")) != wxString::npos
|| s
== _T("black") )
610 return wxFONTWEIGHT_BOLD
;
611 else if ( s
== _T("light") )
612 return wxFONTWEIGHT_LIGHT
;
614 return wxFONTWEIGHT_NORMAL
;
617 bool wxNativeFontInfo::GetUnderlined() const
619 // X fonts are never underlined
623 wxString
wxNativeFontInfo::GetFaceName() const
625 // wxWidgets facename probably more accurately corresponds to X family
626 return GetXFontComponent(wxXLFD_FAMILY
);
629 wxFontFamily
wxNativeFontInfo::GetFamily() const
631 // and wxWidgets family -- to X foundry, but we have to translate it to
632 // wxFontFamily somehow...
633 wxFAIL_MSG(_T("not implemented")); // GetXFontComponent(wxXLFD_FOUNDRY);
635 return wxFONTFAMILY_DEFAULT
;
638 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
640 // we already have the code for this but need to refactor it first
641 wxFAIL_MSG( _T("not implemented") );
643 return wxFONTENCODING_MAX
;
646 void wxNativeFontInfo::SetPointSize(int pointsize
)
648 SetXFontComponent(wxXLFD_POINTSIZE
, wxString::Format(_T("%d"), pointsize
));
651 void wxNativeFontInfo::SetStyle(wxFontStyle style
)
656 case wxFONTSTYLE_ITALIC
:
660 case wxFONTSTYLE_SLANT
:
664 case wxFONTSTYLE_NORMAL
:
668 wxFAIL_MSG( _T("unknown wxFontStyle in wxNativeFontInfo::SetStyle") );
672 SetXFontComponent(wxXLFD_SLANT
, s
);
675 void wxNativeFontInfo::SetWeight(wxFontWeight weight
)
680 case wxFONTWEIGHT_BOLD
:
684 case wxFONTWEIGHT_LIGHT
:
688 case wxFONTWEIGHT_NORMAL
:
693 wxFAIL_MSG( _T("unknown wxFontWeight in wxNativeFontInfo::SetWeight") );
697 SetXFontComponent(wxXLFD_WEIGHT
, s
);
700 void wxNativeFontInfo::SetUnderlined(bool WXUNUSED(underlined
))
702 // can't do this under X
705 void wxNativeFontInfo::SetFaceName(wxString facename
)
707 SetXFontComponent(wxXLFD_FAMILY
, facename
);
710 void wxNativeFontInfo::SetFamily(wxFontFamily family
)
712 // wxFontFamily -> X foundry, anyone?
713 wxFAIL_MSG( _T("not implemented") );
715 // SetXFontComponent(wxXLFD_FOUNDRY, ...);
718 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding
)
720 wxNativeEncodingInfo info
;
721 if ( wxGetNativeFontEncoding(encoding
, &info
) )
723 SetXFontComponent(wxXLFD_ENCODING
, info
.xencoding
);
724 SetXFontComponent(wxXLFD_REGISTRY
, info
.xregistry
);
728 // ----------------------------------------------------------------------------
730 // ----------------------------------------------------------------------------
732 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
733 wxNativeEncodingInfo
*info
)
735 wxCHECK_MSG( info
, FALSE
, _T("bad pointer in wxGetNativeFontEncoding") );
737 if ( encoding
== wxFONTENCODING_DEFAULT
)
739 encoding
= wxFont::GetDefaultEncoding();
744 case wxFONTENCODING_ISO8859_1
:
745 case wxFONTENCODING_ISO8859_2
:
746 case wxFONTENCODING_ISO8859_3
:
747 case wxFONTENCODING_ISO8859_4
:
748 case wxFONTENCODING_ISO8859_5
:
749 case wxFONTENCODING_ISO8859_6
:
750 case wxFONTENCODING_ISO8859_7
:
751 case wxFONTENCODING_ISO8859_8
:
752 case wxFONTENCODING_ISO8859_9
:
753 case wxFONTENCODING_ISO8859_10
:
754 case wxFONTENCODING_ISO8859_11
:
755 case wxFONTENCODING_ISO8859_12
:
756 case wxFONTENCODING_ISO8859_13
:
757 case wxFONTENCODING_ISO8859_14
:
758 case wxFONTENCODING_ISO8859_15
:
760 int cp
= encoding
- wxFONTENCODING_ISO8859_1
+ 1;
761 info
->xregistry
= wxT("iso8859");
762 info
->xencoding
.Printf(wxT("%d"), cp
);
766 case wxFONTENCODING_UTF8
:
767 info
->xregistry
= wxT("iso10646");
768 info
->xencoding
= wxT("*");
771 case wxFONTENCODING_GB2312
:
772 info
->xregistry
= wxT("GB2312"); // or the otherway round?
773 info
->xencoding
= wxT("*");
776 case wxFONTENCODING_KOI8
:
777 case wxFONTENCODING_KOI8_U
:
778 info
->xregistry
= wxT("koi8");
780 // we don't make distinction between koi8-r, koi8-u and koi8-ru (so far)
781 info
->xencoding
= wxT("*");
784 case wxFONTENCODING_CP1250
:
785 case wxFONTENCODING_CP1251
:
786 case wxFONTENCODING_CP1252
:
787 case wxFONTENCODING_CP1253
:
788 case wxFONTENCODING_CP1254
:
789 case wxFONTENCODING_CP1255
:
790 case wxFONTENCODING_CP1256
:
791 case wxFONTENCODING_CP1257
:
793 int cp
= encoding
- wxFONTENCODING_CP1250
+ 1250;
794 info
->xregistry
= wxT("microsoft");
795 info
->xencoding
.Printf(wxT("cp%d"), cp
);
799 case wxFONTENCODING_EUC_JP
:
800 case wxFONTENCODING_SHIFT_JIS
:
801 info
->xregistry
= "jis*";
802 info
->xencoding
= "*";
805 case wxFONTENCODING_SYSTEM
:
807 info
->xencoding
= wxT("*");
811 // don't know how to translate this encoding into X fontspec
815 info
->encoding
= encoding
;
820 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
823 fontspec
.Printf(_T("-*-%s-*-*-*-*-*-*-*-*-*-*-%s-%s"),
824 !info
.facename
? _T("*") : info
.facename
.c_str(),
825 info
.xregistry
.c_str(),
826 info
.xencoding
.c_str());
828 return wxTestFontSpec(fontspec
);
831 // ----------------------------------------------------------------------------
832 // X-specific functions
833 // ----------------------------------------------------------------------------
835 wxNativeFont
wxLoadQueryNearestFont(int pointSize
,
840 const wxString
&facename
,
841 wxFontEncoding encoding
,
844 if ( encoding
== wxFONTENCODING_DEFAULT
)
846 encoding
= wxFont::GetDefaultEncoding();
849 // first determine the encoding - if the font doesn't exist at all in this
850 // encoding, it's useless to do all other approximations (i.e. size,
851 // family &c don't matter much)
852 wxNativeEncodingInfo info
;
853 if ( encoding
== wxFONTENCODING_SYSTEM
)
855 // This will always work so we don't test to save time
856 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
860 if ( !wxGetNativeFontEncoding(encoding
, &info
) ||
861 !wxTestFontEncoding(info
) )
864 if ( !wxFontMapper::Get()->GetAltForEncoding(encoding
, &info
) )
865 #endif // wxUSE_FONTMAP
867 // unspported encoding - replace it with the default
869 // NB: we can't just return 0 from here because wxGTK code doesn't
870 // check for it (i.e. it supposes that we'll always succeed),
871 // so it would provoke a crash
872 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
877 // OK, we have the correct xregistry/xencoding in info structure
878 wxNativeFont font
= 0;
880 // if we already have the X font name, try to use it
881 if( xFontName
&& !xFontName
->IsEmpty() )
884 // Make sure point size is correct for scale factor.
886 wxStringTokenizer
tokenizer(*xFontName
, _T("-"), wxTOKEN_RET_DELIMS
);
887 wxString newFontName
;
889 for(int i
= 0; i
< 8; i
++)
890 newFontName
+= tokenizer
.NextToken();
892 (void) tokenizer
.NextToken();
894 newFontName
+= wxString::Format(wxT("%d-"), pointSize
);
896 while(tokenizer
.HasMoreTokens())
897 newFontName
+= tokenizer
.GetNextToken();
899 font
= wxLoadFont(newFontName
);
902 *xFontName
= newFontName
;
907 // search up and down by stepsize 10
908 int max_size
= pointSize
+ 20 * (1 + (pointSize
/180));
909 int min_size
= pointSize
- 20 * (1 + (pointSize
/180));
911 int i
, round
; // counters
913 // first round: search for equal, then for smaller and for larger size with the given weight and style
914 int testweight
= weight
;
915 int teststyle
= style
;
917 for ( round
= 0; round
< 3; round
++ )
919 // second round: use normal weight
922 if ( testweight
!= wxNORMAL
)
924 testweight
= wxNORMAL
;
928 ++round
; // fall through to third round
932 // third round: ... and use normal style
935 if ( teststyle
!= wxNORMAL
)
937 teststyle
= wxNORMAL
;
944 // Search for equal or smaller size (approx.)
945 for ( i
= pointSize
; !font
&& i
>= 10 && i
>= min_size
; i
-= 10 )
947 font
= wxLoadQueryFont(i
, family
, teststyle
, testweight
, underlined
,
948 facename
, info
.xregistry
, info
.xencoding
,
952 // Search for larger size (approx.)
953 for ( i
= pointSize
+ 10; !font
&& i
<= max_size
; i
+= 10 )
955 font
= wxLoadQueryFont(i
, family
, teststyle
, testweight
, underlined
,
956 facename
, info
.xregistry
, info
.xencoding
,
961 // Try default family
962 if ( !font
&& family
!= wxDEFAULT
)
964 font
= wxLoadQueryFont(pointSize
, wxDEFAULT
, style
, weight
,
965 underlined
, facename
,
966 info
.xregistry
, info
.xencoding
,
970 // ignore size, family, style and weight but try to find font with the
971 // given facename and encoding
974 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
975 underlined
, facename
,
976 info
.xregistry
, info
.xencoding
,
979 // ignore family as well
982 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
983 underlined
, wxEmptyString
,
984 info
.xregistry
, info
.xencoding
,
987 // if it still failed, try to get the font of any size but
988 // with the requested encoding: this can happen if the
989 // encoding is only available in one size which happens to be
990 // different from 120
993 font
= wxLoadQueryFont(-1, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
994 FALSE
, wxEmptyString
,
995 info
.xregistry
, info
.xencoding
,
998 // this should never happen as we had tested for it in the
999 // very beginning, but if it does, do return something non
1000 // NULL or we'd crash in wxFont code
1003 wxFAIL_MSG( _T("this encoding should be available!") );
1005 font
= wxLoadQueryFont(-1,
1006 wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1007 FALSE
, wxEmptyString
,
1019 // ----------------------------------------------------------------------------
1020 // private functions
1021 // ----------------------------------------------------------------------------
1023 // returns TRUE if there are any fonts matching this font spec
1024 static bool wxTestFontSpec(const wxString
& fontspec
)
1026 // some X servers will fail to load this font because there are too many
1027 // matches so we must test explicitly for this
1028 if ( fontspec
== _T("-*-*-*-*-*-*-*-*-*-*-*-*-*-*") )
1033 wxNativeFont test
= (wxNativeFont
) g_fontHash
->Get( fontspec
);
1039 test
= wxLoadFont(fontspec
);
1040 g_fontHash
->Put( fontspec
, (wxObject
*) test
);
1054 static wxNativeFont
wxLoadQueryFont(int pointSize
,
1058 bool WXUNUSED(underlined
),
1059 const wxString
& facename
,
1060 const wxString
& xregistry
,
1061 const wxString
& xencoding
,
1062 wxString
* xFontName
)
1067 case wxDECORATIVE
: xfamily
= wxT("lucida"); break;
1068 case wxROMAN
: xfamily
= wxT("times"); break;
1069 case wxMODERN
: xfamily
= wxT("courier"); break;
1070 case wxSWISS
: xfamily
= wxT("helvetica"); break;
1071 case wxTELETYPE
: xfamily
= wxT("lucidatypewriter"); break;
1072 case wxSCRIPT
: xfamily
= wxT("utopia"); break;
1073 default: xfamily
= wxT("*");
1081 xweight
= MWLF_WEIGHT_BOLD
;
1086 xweight
= MWLF_WEIGHT_LIGHT
;
1091 xweight
= MWLF_WEIGHT_NORMAL
;
1097 xweight
= MWLF_WEIGHT_DEFAULT
;
1101 GR_SCREEN_INFO screenInfo
;
1102 GrGetScreenInfo(& screenInfo
);
1104 int yPixelsPerCM
= screenInfo
.ydpcm
;
1106 // A point is 1/72 of an inch.
1107 // An inch is 2.541 cm.
1108 // So pixelHeight = (pointSize / 72) (inches) * 2.541 (for cm) * yPixelsPerCM (for pixels)
1109 // In fact pointSize is 10 * the normal point size so
1112 int pixelHeight
= (int) ( (((float)pointSize
) / 720.0) * 2.541 * (float) yPixelsPerCM
) ;
1114 // An alternative: assume that the screen is 72 dpi.
1115 //int pixelHeight = (int) (((float)pointSize / 720.0) * 72.0) ;
1116 //int pixelHeight = (int) ((float)pointSize / 10.0) ;
1119 logFont
.lfHeight
= pixelHeight
;
1120 logFont
.lfWidth
= 0;
1121 logFont
.lfEscapement
= 0;
1122 logFont
.lfOrientation
= 0;
1123 logFont
.lfWeight
= xweight
;
1124 logFont
.lfItalic
= (style
== wxNORMAL
? 0 : 1) ;
1125 logFont
.lfUnderline
= 0;
1126 logFont
.lfStrikeOut
= 0;
1127 logFont
.lfCharSet
= MWLF_CHARSET_DEFAULT
; // TODO: select appropriate one
1128 logFont
.lfOutPrecision
= MWLF_TYPE_DEFAULT
;
1129 logFont
.lfClipPrecision
= 0; // Not used
1130 logFont
.lfRoman
= (family
== wxROMAN
? 1 : 0) ;
1131 logFont
.lfSerif
= (family
== wxSWISS
? 0 : 1) ;
1132 logFont
.lfSansSerif
= !logFont
.lfSerif
;
1133 logFont
.lfModern
= (family
== wxMODERN
? 1 : 0) ;
1134 logFont
.lfProportional
= (family
== wxTELETYPE
? 0 : 1) ;
1135 logFont
.lfOblique
= 0;
1136 logFont
.lfSmallCaps
= 0;
1137 logFont
.lfPitch
= 0; // 0 = default
1138 strcpy(logFont
.lfFaceName
, facename
.c_str());
1140 XFontStruct
* fontInfo
= (XFontStruct
*) malloc(sizeof(XFontStruct
));
1141 fontInfo
->fid
= GrCreateFont((GR_CHAR
*) facename
.c_str(), pixelHeight
, & logFont
);
1142 GrGetFontInfo(fontInfo
->fid
, & fontInfo
->info
);
1143 return (wxNativeFont
) fontInfo
;
1147 if (!facename
.IsEmpty())
1149 fontSpec
.Printf(wxT("-*-%s-*-*-normal-*-*-*-*-*-*-*-*-*"),
1152 if ( wxTestFontSpec(fontSpec
) )
1156 //else: no such family, use default one instead
1163 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1165 if ( wxTestFontSpec(fontSpec
) )
1170 // fall through - try wxITALIC now
1173 fontSpec
.Printf(wxT("-*-%s-*-i-*-*-*-*-*-*-*-*-*-*"),
1175 if ( wxTestFontSpec(fontSpec
) )
1179 else if ( style
== wxITALIC
) // and not wxSLANT
1182 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1184 if ( wxTestFontSpec(fontSpec
) )
1190 // no italic, no slant - leave default
1197 wxFAIL_MSG(_T("unknown font style"));
1198 // fall back to normal
1210 fontSpec
.Printf(wxT("-*-%s-bold-*-*-*-*-*-*-*-*-*-*-*"),
1212 if ( wxTestFontSpec(fontSpec
) )
1214 xweight
= wxT("bold");
1217 fontSpec
.Printf(wxT("-*-%s-heavy-*-*-*-*-*-*-*-*-*-*-*"),
1219 if ( wxTestFontSpec(fontSpec
) )
1221 xweight
= wxT("heavy");
1224 fontSpec
.Printf(wxT("-*-%s-extrabold-*-*-*-*-*-*-*-*-*-*-*"),
1226 if ( wxTestFontSpec(fontSpec
) )
1228 xweight
= wxT("extrabold");
1231 fontSpec
.Printf(wxT("-*-%s-demibold-*-*-*-*-*-*-*-*-*-*-*"),
1233 if ( wxTestFontSpec(fontSpec
) )
1235 xweight
= wxT("demibold");
1238 fontSpec
.Printf(wxT("-*-%s-black-*-*-*-*-*-*-*-*-*-*-*"),
1240 if ( wxTestFontSpec(fontSpec
) )
1242 xweight
= wxT("black");
1245 fontSpec
.Printf(wxT("-*-%s-ultrablack-*-*-*-*-*-*-*-*-*-*-*"),
1247 if ( wxTestFontSpec(fontSpec
) )
1249 xweight
= wxT("ultrablack");
1256 fontSpec
.Printf(wxT("-*-%s-light-*-*-*-*-*-*-*-*-*-*-*"),
1258 if ( wxTestFontSpec(fontSpec
) )
1260 xweight
= wxT("light");
1263 fontSpec
.Printf(wxT("-*-%s-thin-*-*-*-*-*-*-*-*-*-*-*"),
1265 if ( wxTestFontSpec(fontSpec
) )
1267 xweight
= wxT("thin");
1274 fontSpec
.Printf(wxT("-*-%s-medium-*-*-*-*-*-*-*-*-*-*-*"),
1276 if ( wxTestFontSpec(fontSpec
) )
1278 xweight
= wxT("medium");
1281 fontSpec
.Printf(wxT("-*-%s-normal-*-*-*-*-*-*-*-*-*-*-*"),
1283 if ( wxTestFontSpec(fontSpec
) )
1285 xweight
= wxT("normal");
1288 fontSpec
.Printf(wxT("-*-%s-regular-*-*-*-*-*-*-*-*-*-*-*"),
1290 if ( wxTestFontSpec(fontSpec
) )
1292 xweight
= wxT("regular");
1298 default: xweight
= wxT("*"); break;
1301 // if pointSize is -1, don't specify any
1303 if ( pointSize
== -1 )
1309 sizeSpec
.Printf(_T("%d"), pointSize
);
1312 // construct the X font spec from our data
1313 fontSpec
.Printf(wxT("-*-%s-%s-%s-normal-*-*-%s-*-*-*-*-%s-%s"),
1314 xfamily
.c_str(), xweight
.c_str(), xstyle
.c_str(),
1315 sizeSpec
.c_str(), xregistry
.c_str(), xencoding
.c_str());
1318 *xFontName
= fontSpec
;
1320 return wxLoadFont(fontSpec
);
1325 // ----------------------------------------------------------------------------
1327 // ----------------------------------------------------------------------------
1329 class wxFontModule
: public wxModule
1336 DECLARE_DYNAMIC_CLASS(wxFontModule
)
1339 IMPLEMENT_DYNAMIC_CLASS(wxFontModule
, wxModule
)
1341 bool wxFontModule::OnInit()
1343 g_fontHash
= new wxHashTable( wxKEY_STRING
);
1348 void wxFontModule::OnExit()
1352 g_fontHash
= (wxHashTable
*)NULL
;
1355 #endif // GTK 2.0/1.x