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/encinfo.h"
35 #include "wx/fontutil.h"
36 #include "wx/fontmap.h"
37 #include "wx/tokenzr.h"
39 #include "wx/module.h"
43 #include "pango/pango.h"
46 #include "wx/gtk/private.h"
48 #include "wx/x11/private.h"
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 void wxNativeFontInfo::Init()
61 wxNativeFontInfo::Init(const wxNativeFontInfo
& info
)
64 description
= pango_font_description_copy(info
.description
);
69 void wxNativeFontInfo::Free()
72 pango_font_description_free(description
);
75 int wxNativeFontInfo::GetPointSize() const
77 return pango_font_description_get_size( description
) / PANGO_SCALE
;
80 wxFontStyle
wxNativeFontInfo::GetStyle() const
82 wxFontStyle m_style
= wxFONTSTYLE_NORMAL
;
84 switch (pango_font_description_get_style( description
))
86 case PANGO_STYLE_NORMAL
:
87 m_style
= wxFONTSTYLE_NORMAL
;
89 case PANGO_STYLE_ITALIC
:
90 m_style
= wxFONTSTYLE_ITALIC
;
92 case PANGO_STYLE_OBLIQUE
:
93 m_style
= wxFONTSTYLE_SLANT
;
100 wxFontWeight
wxNativeFontInfo::GetWeight() const
102 wxFontWeight m_weight
= wxFONTWEIGHT_NORMAL
;
104 switch (pango_font_description_get_weight( description
))
106 case PANGO_WEIGHT_ULTRALIGHT
:
107 m_weight
= wxFONTWEIGHT_LIGHT
;
109 case PANGO_WEIGHT_LIGHT
:
110 m_weight
= wxFONTWEIGHT_LIGHT
;
112 case PANGO_WEIGHT_NORMAL
:
113 m_weight
= wxFONTWEIGHT_NORMAL
;
115 case PANGO_WEIGHT_BOLD
:
116 m_weight
= wxFONTWEIGHT_BOLD
;
118 case PANGO_WEIGHT_ULTRABOLD
:
119 m_weight
= wxFONTWEIGHT_BOLD
;
121 case PANGO_WEIGHT_HEAVY
:
122 m_weight
= wxFONTWEIGHT_BOLD
;
129 bool wxNativeFontInfo::GetUnderlined() const
134 wxString
wxNativeFontInfo::GetFaceName() const
136 wxString tmp
= wxGTK_CONV_BACK( pango_font_description_get_family( description
) );
141 wxFontFamily
wxNativeFontInfo::GetFamily() const
143 return wxFONTFAMILY_SWISS
;
146 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
148 return wxFONTENCODING_SYSTEM
;
152 void wxNativeFontInfo::SetPointSize(int WXUNUSED(pointsize
))
154 wxFAIL_MSG( _T("not implemented") );
157 void wxNativeFontInfo::SetStyle(wxFontStyle
WXUNUSED(style
))
159 wxFAIL_MSG( _T("not implemented") );
162 void wxNativeFontInfo::SetWeight(wxFontWeight
WXUNUSED(weight
))
164 wxFAIL_MSG( _T("not implemented") );
167 void wxNativeFontInfo::SetUnderlined(bool WXUNUSED(underlined
))
169 wxFAIL_MSG( _T("not implemented") );
172 void wxNativeFontInfo::SetFaceName(wxString
WXUNUSED(facename
))
174 wxFAIL_MSG( _T("not implemented") );
177 void wxNativeFontInfo::SetFamily(wxFontFamily
WXUNUSED(family
))
179 wxFAIL_MSG( _T("not implemented") );
182 void wxNativeFontInfo::SetEncoding(wxFontEncoding
WXUNUSED(encoding
))
184 wxFAIL_MSG( _T("not implemented") );
189 bool wxNativeFontInfo::FromString(const wxString
& s
)
192 pango_font_description_free( description
);
194 description
= pango_font_description_from_string( wxGTK_CONV( s
) );
199 wxString
wxNativeFontInfo::ToString() const
201 char *str
= pango_font_description_to_string( description
);
202 wxString tmp
= wxGTK_CONV_BACK( str
);
208 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
210 return FromString( s
);
213 wxString
wxNativeFontInfo::ToUserString() const
218 // ----------------------------------------------------------------------------
219 // wxNativeEncodingInfo
220 // ----------------------------------------------------------------------------
222 bool wxNativeEncodingInfo::FromString(const wxString
& s
)
227 wxString
wxNativeEncodingInfo::ToString() const
229 return wxEmptyString
;
232 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
237 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
238 wxNativeEncodingInfo
*info
)
240 // we *must* return true for default encoding as otherwise wxFontMapper
241 // considers that we can't load any font and aborts with wxLogFatalError!
242 if ( encoding
== wxFONTENCODING_SYSTEM
)
244 info
->facename
.clear();
245 info
->encoding
= wxFONTENCODING_SYSTEM
;
248 // pretend that we support everything, it's better than to always return
249 // false as the old code did
257 #pragma message disable nosimpint
260 #include <X11/Xlib.h>
263 #pragma message enable nosimpint
266 #include "wx/utils.h" // for wxGetDisplay()
267 #elif defined(__WXGTK__)
268 // we have to declare struct tm to avoid problems with first forward
269 // declaring it in C code (glib.h included from gdk.h does it) and then
270 // defining it when time.h is included from the headers below - this is
271 // known not to work at least with Sun CC 6.01
278 // ----------------------------------------------------------------------------
280 // ----------------------------------------------------------------------------
282 static wxHashTable
*g_fontHash
= (wxHashTable
*) NULL
;
284 // ----------------------------------------------------------------------------
286 // ----------------------------------------------------------------------------
288 // define the functions to create and destroy native fonts for this toolkit
290 wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
292 return XLoadQueryFont((Display
*)wxGetDisplay(), fontSpec
);
295 inline void wxFreeFont(wxNativeFont font
)
297 XFreeFont((Display
*)wxGetDisplay(), (XFontStruct
*)font
);
299 #elif defined(__WXGTK__)
300 wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
302 // VZ: we should use gdk_fontset_load() instead of gdk_font_load()
303 // here to be able to display Japanese fonts correctly (at least
304 // this is what people report) but unfortunately doing it results
305 // in tons of warnings when using GTK with "normal" European
306 // languages and so we can't always do it and I don't know enough
307 // to determine when should this be done... (FIXME)
308 return gdk_font_load( wxConvertWX2MB(fontSpec
) );
311 inline void wxFreeFont(wxNativeFont font
)
313 gdk_font_unref(font
);
316 #error "Unknown GUI toolkit"
319 static bool wxTestFontSpec(const wxString
& fontspec
);
321 static wxNativeFont
wxLoadQueryFont(int pointSize
,
326 const wxString
& facename
,
327 const wxString
& xregistry
,
328 const wxString
& xencoding
,
329 wxString
* xFontName
);
331 // ============================================================================
333 // ============================================================================
335 // ----------------------------------------------------------------------------
336 // wxNativeEncodingInfo
337 // ----------------------------------------------------------------------------
339 // convert to/from the string representation: format is
340 // encodingid;registry;encoding[;facename]
341 bool wxNativeEncodingInfo::FromString(const wxString
& s
)
343 // use ";", not "-" because it may be part of encoding name
344 wxStringTokenizer
tokenizer(s
, _T(";"));
346 wxString encid
= tokenizer
.GetNextToken();
348 if ( !encid
.ToLong(&enc
) )
350 encoding
= (wxFontEncoding
)enc
;
352 xregistry
= tokenizer
.GetNextToken();
356 xencoding
= tokenizer
.GetNextToken();
361 facename
= tokenizer
.GetNextToken();
366 wxString
wxNativeEncodingInfo::ToString() const
369 s
<< (long)encoding
<< _T(';') << xregistry
<< _T(';') << xencoding
;
372 s
<< _T(';') << facename
;
378 // ----------------------------------------------------------------------------
380 // ----------------------------------------------------------------------------
382 void wxNativeFontInfo::Init()
387 bool wxNativeFontInfo::FromString(const wxString
& s
)
389 wxStringTokenizer
tokenizer(s
, _T(";"));
392 wxString token
= tokenizer
.GetNextToken();
393 if ( token
!= _T('0') )
396 xFontName
= tokenizer
.GetNextToken();
398 // this should be the end
399 if ( tokenizer
.HasMoreTokens() )
402 return FromXFontName(xFontName
);
405 wxString
wxNativeFontInfo::ToString() const
408 return wxString::Format(_T("%d;%s"), 0, GetXFontName().c_str());
411 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
413 return FromXFontName(s
);
416 wxString
wxNativeFontInfo::ToUserString() const
418 return GetXFontName();
421 bool wxNativeFontInfo::HasElements() const
423 // we suppose that the foundry is never empty, so if it is it means that we
424 // had never parsed the XLFD
425 return !fontElements
[0].empty();
428 wxString
wxNativeFontInfo::GetXFontComponent(wxXLFDField field
) const
430 wxCHECK_MSG( field
< wxXLFD_MAX
, _T(""), _T("invalid XLFD field") );
432 if ( !HasElements() )
435 if ( !((wxNativeFontInfo
*)this)->FromXFontName(xFontName
) )
439 return fontElements
[field
];
442 bool wxNativeFontInfo::FromXFontName(const wxString
& fontname
)
444 // TODO: we should be able to handle the font aliases here, but how?
445 wxStringTokenizer
tokenizer(fontname
, _T("-"));
447 // skip the leading, usually empty field (font name registry)
448 if ( !tokenizer
.HasMoreTokens() )
451 (void)tokenizer
.GetNextToken();
453 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
455 if ( !tokenizer
.HasMoreTokens() )
457 // not enough elements in the XLFD - or maybe an alias
461 wxString field
= tokenizer
.GetNextToken();
462 if ( !field
.empty() && field
!= _T('*') )
464 // we're really initialized now
468 fontElements
[n
] = field
;
471 // this should be all
472 if ( tokenizer
.HasMoreTokens() )
478 wxString
wxNativeFontInfo::GetXFontName() const
480 if ( xFontName
.empty() )
482 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
484 // replace the non specified elements with '*' except for the
485 // additional style which is usually just omitted
486 wxString elt
= fontElements
[n
];
487 if ( elt
.empty() && n
!= wxXLFD_ADDSTYLE
)
493 ((wxNativeFontInfo
*)this)->xFontName
<< _T('-') << elt
;
501 wxNativeFontInfo::SetXFontComponent(wxXLFDField field
, const wxString
& value
)
503 wxCHECK_RET( field
< wxXLFD_MAX
, _T("invalid XLFD field") );
505 // this class should be initialized with a valid font spec first and only
506 // then the fields may be modified!
507 wxASSERT_MSG( !IsDefault(), _T("can't modify an uninitialized XLFD") );
509 if ( !HasElements() )
512 if ( !((wxNativeFontInfo
*)this)->FromXFontName(xFontName
) )
514 wxFAIL_MSG( _T("can't set font element for invalid XLFD") );
520 fontElements
[field
] = value
;
522 // invalidate the XFLD, it doesn't correspond to the font elements any more
526 void wxNativeFontInfo::SetXFontName(const wxString
& xFontName_
)
528 // invalidate the font elements, GetXFontComponent() will reparse the XLFD
529 fontElements
[0].clear();
531 xFontName
= xFontName_
;
536 int wxNativeFontInfo::GetPointSize() const
538 const wxString s
= GetXFontComponent(wxXLFD_POINTSIZE
);
540 // return -1 to indicate that the size is unknown
542 return s
.ToLong(&l
) ? l
: -1;
545 wxFontStyle
wxNativeFontInfo::GetStyle() const
547 const wxString s
= GetXFontComponent(wxXLFD_SLANT
);
549 if ( s
.length() != 1 )
551 // it is really unknown but we don't have any way to return it from
553 return wxFONTSTYLE_NORMAL
;
559 // again, unknown but consider normal by default
562 return wxFONTSTYLE_NORMAL
;
565 return wxFONTSTYLE_ITALIC
;
568 return wxFONTSTYLE_SLANT
;
572 wxFontWeight
wxNativeFontInfo::GetWeight() const
574 const wxString s
= GetXFontComponent(wxXLFD_WEIGHT
).MakeLower();
575 if ( s
.find(_T("bold")) != wxString::npos
|| s
== _T("black") )
576 return wxFONTWEIGHT_BOLD
;
577 else if ( s
== _T("light") )
578 return wxFONTWEIGHT_LIGHT
;
580 return wxFONTWEIGHT_NORMAL
;
583 bool wxNativeFontInfo::GetUnderlined() const
585 // X fonts are never underlined
589 wxString
wxNativeFontInfo::GetFaceName() const
591 // wxWidgets facename probably more accurately corresponds to X family
592 return GetXFontComponent(wxXLFD_FAMILY
);
595 wxFontFamily
wxNativeFontInfo::GetFamily() const
597 // and wxWidgets family -- to X foundry, but we have to translate it to
598 // wxFontFamily somehow...
599 wxFAIL_MSG(_T("not implemented")); // GetXFontComponent(wxXLFD_FOUNDRY);
601 return wxFONTFAMILY_DEFAULT
;
604 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
606 // we already have the code for this but need to refactor it first
607 wxFAIL_MSG( _T("not implemented") );
609 return wxFONTENCODING_MAX
;
612 void wxNativeFontInfo::SetPointSize(int pointsize
)
614 SetXFontComponent(wxXLFD_POINTSIZE
, wxString::Format(_T("%d"), pointsize
));
617 void wxNativeFontInfo::SetStyle(wxFontStyle style
)
622 case wxFONTSTYLE_ITALIC
:
626 case wxFONTSTYLE_SLANT
:
630 case wxFONTSTYLE_NORMAL
:
634 wxFAIL_MSG( _T("unknown wxFontStyle in wxNativeFontInfo::SetStyle") );
638 SetXFontComponent(wxXLFD_SLANT
, s
);
641 void wxNativeFontInfo::SetWeight(wxFontWeight weight
)
646 case wxFONTWEIGHT_BOLD
:
650 case wxFONTWEIGHT_LIGHT
:
654 case wxFONTWEIGHT_NORMAL
:
659 wxFAIL_MSG( _T("unknown wxFontWeight in wxNativeFontInfo::SetWeight") );
663 SetXFontComponent(wxXLFD_WEIGHT
, s
);
666 void wxNativeFontInfo::SetUnderlined(bool WXUNUSED(underlined
))
668 // can't do this under X
671 void wxNativeFontInfo::SetFaceName(wxString facename
)
673 SetXFontComponent(wxXLFD_FAMILY
, facename
);
676 void wxNativeFontInfo::SetFamily(wxFontFamily family
)
678 // wxFontFamily -> X foundry, anyone?
679 wxFAIL_MSG( _T("not implemented") );
681 // SetXFontComponent(wxXLFD_FOUNDRY, ...);
684 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding
)
686 wxNativeEncodingInfo info
;
687 if ( wxGetNativeFontEncoding(encoding
, &info
) )
689 SetXFontComponent(wxXLFD_ENCODING
, info
.xencoding
);
690 SetXFontComponent(wxXLFD_REGISTRY
, info
.xregistry
);
694 // ----------------------------------------------------------------------------
696 // ----------------------------------------------------------------------------
698 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
699 wxNativeEncodingInfo
*info
)
701 wxCHECK_MSG( info
, FALSE
, _T("bad pointer in wxGetNativeFontEncoding") );
703 if ( encoding
== wxFONTENCODING_DEFAULT
)
705 encoding
= wxFont::GetDefaultEncoding();
710 case wxFONTENCODING_ISO8859_1
:
711 case wxFONTENCODING_ISO8859_2
:
712 case wxFONTENCODING_ISO8859_3
:
713 case wxFONTENCODING_ISO8859_4
:
714 case wxFONTENCODING_ISO8859_5
:
715 case wxFONTENCODING_ISO8859_6
:
716 case wxFONTENCODING_ISO8859_7
:
717 case wxFONTENCODING_ISO8859_8
:
718 case wxFONTENCODING_ISO8859_9
:
719 case wxFONTENCODING_ISO8859_10
:
720 case wxFONTENCODING_ISO8859_11
:
721 case wxFONTENCODING_ISO8859_12
:
722 case wxFONTENCODING_ISO8859_13
:
723 case wxFONTENCODING_ISO8859_14
:
724 case wxFONTENCODING_ISO8859_15
:
726 int cp
= encoding
- wxFONTENCODING_ISO8859_1
+ 1;
727 info
->xregistry
= wxT("iso8859");
728 info
->xencoding
.Printf(wxT("%d"), cp
);
732 case wxFONTENCODING_UTF8
:
733 info
->xregistry
= wxT("iso10646");
734 info
->xencoding
= wxT("*");
737 case wxFONTENCODING_GB2312
:
738 info
->xregistry
= wxT("GB2312"); // or the otherway round?
739 info
->xencoding
= wxT("*");
742 case wxFONTENCODING_KOI8
:
743 case wxFONTENCODING_KOI8_U
:
744 info
->xregistry
= wxT("koi8");
746 // we don't make distinction between koi8-r, koi8-u and koi8-ru (so far)
747 info
->xencoding
= wxT("*");
750 case wxFONTENCODING_CP1250
:
751 case wxFONTENCODING_CP1251
:
752 case wxFONTENCODING_CP1252
:
753 case wxFONTENCODING_CP1253
:
754 case wxFONTENCODING_CP1254
:
755 case wxFONTENCODING_CP1255
:
756 case wxFONTENCODING_CP1256
:
757 case wxFONTENCODING_CP1257
:
759 int cp
= encoding
- wxFONTENCODING_CP1250
+ 1250;
760 info
->xregistry
= wxT("microsoft");
761 info
->xencoding
.Printf(wxT("cp%d"), cp
);
765 case wxFONTENCODING_SYSTEM
:
767 info
->xencoding
= wxT("*");
771 // don't know how to translate this encoding into X fontspec
775 info
->encoding
= encoding
;
780 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
783 fontspec
.Printf(_T("-*-%s-*-*-*-*-*-*-*-*-*-*-%s-%s"),
784 !info
.facename
? _T("*") : info
.facename
.c_str(),
785 info
.xregistry
.c_str(),
786 info
.xencoding
.c_str());
788 return wxTestFontSpec(fontspec
);
791 // ----------------------------------------------------------------------------
792 // X-specific functions
793 // ----------------------------------------------------------------------------
795 wxNativeFont
wxLoadQueryNearestFont(int pointSize
,
800 const wxString
&facename
,
801 wxFontEncoding encoding
,
804 if ( encoding
== wxFONTENCODING_DEFAULT
)
806 encoding
= wxFont::GetDefaultEncoding();
809 // first determine the encoding - if the font doesn't exist at all in this
810 // encoding, it's useless to do all other approximations (i.e. size,
811 // family &c don't matter much)
812 wxNativeEncodingInfo info
;
813 if ( encoding
== wxFONTENCODING_SYSTEM
)
815 // This will always work so we don't test to save time
816 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
820 if ( !wxGetNativeFontEncoding(encoding
, &info
) ||
821 !wxTestFontEncoding(info
) )
824 if ( !wxFontMapper::Get()->GetAltForEncoding(encoding
, &info
) )
825 #endif // wxUSE_FONTMAP
827 // unspported encoding - replace it with the default
829 // NB: we can't just return 0 from here because wxGTK code doesn't
830 // check for it (i.e. it supposes that we'll always succeed),
831 // so it would provoke a crash
832 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
837 // OK, we have the correct xregistry/xencoding in info structure
838 wxNativeFont font
= 0;
840 // if we already have the X font name, try to use it
841 if( xFontName
&& !xFontName
->IsEmpty() )
844 // Make sure point size is correct for scale factor.
846 wxStringTokenizer
tokenizer(*xFontName
, _T("-"), wxTOKEN_RET_DELIMS
);
847 wxString newFontName
;
849 for(int i
= 0; i
< 8; i
++)
850 newFontName
+= tokenizer
.NextToken();
852 (void) tokenizer
.NextToken();
854 newFontName
+= wxString::Format(wxT("%d-"), pointSize
);
856 while(tokenizer
.HasMoreTokens())
857 newFontName
+= tokenizer
.GetNextToken();
859 font
= wxLoadFont(newFontName
);
862 *xFontName
= newFontName
;
867 // search up and down by stepsize 10
868 int max_size
= pointSize
+ 20 * (1 + (pointSize
/180));
869 int min_size
= pointSize
- 20 * (1 + (pointSize
/180));
871 int i
, round
; // counters
873 // first round: search for equal, then for smaller and for larger size with the given weight and style
874 int testweight
= weight
;
875 int teststyle
= style
;
877 for ( round
= 0; round
< 3; round
++ )
879 // second round: use normal weight
882 if ( testweight
!= wxNORMAL
)
884 testweight
= wxNORMAL
;
888 ++round
; // fall through to third round
892 // third round: ... and use normal style
895 if ( teststyle
!= wxNORMAL
)
897 teststyle
= wxNORMAL
;
904 // Search for equal or smaller size (approx.)
905 for ( i
= pointSize
; !font
&& i
>= 10 && i
>= min_size
; i
-= 10 )
907 font
= wxLoadQueryFont(i
, family
, teststyle
, testweight
, underlined
,
908 facename
, info
.xregistry
, info
.xencoding
,
912 // Search for larger size (approx.)
913 for ( i
= pointSize
+ 10; !font
&& i
<= max_size
; i
+= 10 )
915 font
= wxLoadQueryFont(i
, family
, teststyle
, testweight
, underlined
,
916 facename
, info
.xregistry
, info
.xencoding
,
921 // Try default family
922 if ( !font
&& family
!= wxDEFAULT
)
924 font
= wxLoadQueryFont(pointSize
, wxDEFAULT
, style
, weight
,
925 underlined
, facename
,
926 info
.xregistry
, info
.xencoding
,
930 // ignore size, family, style and weight but try to find font with the
931 // given facename and encoding
934 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
935 underlined
, facename
,
936 info
.xregistry
, info
.xencoding
,
939 // ignore family as well
942 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
943 underlined
, wxEmptyString
,
944 info
.xregistry
, info
.xencoding
,
947 // if it still failed, try to get the font of any size but
948 // with the requested encoding: this can happen if the
949 // encoding is only available in one size which happens to be
950 // different from 120
953 font
= wxLoadQueryFont(-1, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
954 FALSE
, wxEmptyString
,
955 info
.xregistry
, info
.xencoding
,
958 // this should never happen as we had tested for it in the
959 // very beginning, but if it does, do return something non
960 // NULL or we'd crash in wxFont code
963 wxFAIL_MSG( _T("this encoding should be available!") );
965 font
= wxLoadQueryFont(-1,
966 wxDEFAULT
, wxNORMAL
, wxNORMAL
,
967 FALSE
, wxEmptyString
,
979 // ----------------------------------------------------------------------------
981 // ----------------------------------------------------------------------------
983 // returns TRUE if there are any fonts matching this font spec
984 static bool wxTestFontSpec(const wxString
& fontspec
)
986 // some X servers will fail to load this font because there are too many
987 // matches so we must test explicitly for this
988 if ( fontspec
== _T("-*-*-*-*-*-*-*-*-*-*-*-*-*-*") )
993 wxNativeFont test
= (wxNativeFont
) g_fontHash
->Get( fontspec
);
999 test
= wxLoadFont(fontspec
);
1000 g_fontHash
->Put( fontspec
, (wxObject
*) test
);
1014 static wxNativeFont
wxLoadQueryFont(int pointSize
,
1018 bool WXUNUSED(underlined
),
1019 const wxString
& facename
,
1020 const wxString
& xregistry
,
1021 const wxString
& xencoding
,
1022 wxString
* xFontName
)
1027 case wxDECORATIVE
: xfamily
= wxT("lucida"); break;
1028 case wxROMAN
: xfamily
= wxT("times"); break;
1029 case wxMODERN
: xfamily
= wxT("courier"); break;
1030 case wxSWISS
: xfamily
= wxT("helvetica"); break;
1031 case wxTELETYPE
: xfamily
= wxT("lucidatypewriter"); break;
1032 case wxSCRIPT
: xfamily
= wxT("utopia"); break;
1033 default: xfamily
= wxT("*");
1041 xweight
= MWLF_WEIGHT_BOLD
;
1046 xweight
= MWLF_WEIGHT_LIGHT
;
1051 xweight
= MWLF_WEIGHT_NORMAL
;
1057 xweight
= MWLF_WEIGHT_DEFAULT
;
1061 GR_SCREEN_INFO screenInfo
;
1062 GrGetScreenInfo(& screenInfo
);
1064 int yPixelsPerCM
= screenInfo
.ydpcm
;
1066 // A point is 1/72 of an inch.
1067 // An inch is 2.541 cm.
1068 // So pixelHeight = (pointSize / 72) (inches) * 2.541 (for cm) * yPixelsPerCM (for pixels)
1069 // In fact pointSize is 10 * the normal point size so
1072 int pixelHeight
= (int) ( (((float)pointSize
) / 720.0) * 2.541 * (float) yPixelsPerCM
) ;
1074 // An alternative: assume that the screen is 72 dpi.
1075 //int pixelHeight = (int) (((float)pointSize / 720.0) * 72.0) ;
1076 //int pixelHeight = (int) ((float)pointSize / 10.0) ;
1079 logFont
.lfHeight
= pixelHeight
;
1080 logFont
.lfWidth
= 0;
1081 logFont
.lfEscapement
= 0;
1082 logFont
.lfOrientation
= 0;
1083 logFont
.lfWeight
= xweight
;
1084 logFont
.lfItalic
= (style
== wxNORMAL
? 0 : 1) ;
1085 logFont
.lfUnderline
= 0;
1086 logFont
.lfStrikeOut
= 0;
1087 logFont
.lfCharSet
= MWLF_CHARSET_DEFAULT
; // TODO: select appropriate one
1088 logFont
.lfOutPrecision
= MWLF_TYPE_DEFAULT
;
1089 logFont
.lfClipPrecision
= 0; // Not used
1090 logFont
.lfRoman
= (family
== wxROMAN
? 1 : 0) ;
1091 logFont
.lfSerif
= (family
== wxSWISS
? 0 : 1) ;
1092 logFont
.lfSansSerif
= !logFont
.lfSerif
;
1093 logFont
.lfModern
= (family
== wxMODERN
? 1 : 0) ;
1094 logFont
.lfProportional
= (family
== wxTELETYPE
? 0 : 1) ;
1095 logFont
.lfOblique
= 0;
1096 logFont
.lfSmallCaps
= 0;
1097 logFont
.lfPitch
= 0; // 0 = default
1098 strcpy(logFont
.lfFaceName
, facename
.c_str());
1100 XFontStruct
* fontInfo
= (XFontStruct
*) malloc(sizeof(XFontStruct
));
1101 fontInfo
->fid
= GrCreateFont((GR_CHAR
*) facename
.c_str(), pixelHeight
, & logFont
);
1102 GrGetFontInfo(fontInfo
->fid
, & fontInfo
->info
);
1103 return (wxNativeFont
) fontInfo
;
1107 if (!facename
.IsEmpty())
1109 fontSpec
.Printf(wxT("-*-%s-*-*-normal-*-*-*-*-*-*-*-*-*"),
1112 if ( wxTestFontSpec(fontSpec
) )
1116 //else: no such family, use default one instead
1123 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1125 if ( wxTestFontSpec(fontSpec
) )
1130 // fall through - try wxITALIC now
1133 fontSpec
.Printf(wxT("-*-%s-*-i-*-*-*-*-*-*-*-*-*-*"),
1135 if ( wxTestFontSpec(fontSpec
) )
1139 else if ( style
== wxITALIC
) // and not wxSLANT
1142 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1144 if ( wxTestFontSpec(fontSpec
) )
1150 // no italic, no slant - leave default
1157 wxFAIL_MSG(_T("unknown font style"));
1158 // fall back to normal
1170 fontSpec
.Printf(wxT("-*-%s-bold-*-*-*-*-*-*-*-*-*-*-*"),
1172 if ( wxTestFontSpec(fontSpec
) )
1174 xweight
= wxT("bold");
1177 fontSpec
.Printf(wxT("-*-%s-heavy-*-*-*-*-*-*-*-*-*-*-*"),
1179 if ( wxTestFontSpec(fontSpec
) )
1181 xweight
= wxT("heavy");
1184 fontSpec
.Printf(wxT("-*-%s-extrabold-*-*-*-*-*-*-*-*-*-*-*"),
1186 if ( wxTestFontSpec(fontSpec
) )
1188 xweight
= wxT("extrabold");
1191 fontSpec
.Printf(wxT("-*-%s-demibold-*-*-*-*-*-*-*-*-*-*-*"),
1193 if ( wxTestFontSpec(fontSpec
) )
1195 xweight
= wxT("demibold");
1198 fontSpec
.Printf(wxT("-*-%s-black-*-*-*-*-*-*-*-*-*-*-*"),
1200 if ( wxTestFontSpec(fontSpec
) )
1202 xweight
= wxT("black");
1205 fontSpec
.Printf(wxT("-*-%s-ultrablack-*-*-*-*-*-*-*-*-*-*-*"),
1207 if ( wxTestFontSpec(fontSpec
) )
1209 xweight
= wxT("ultrablack");
1216 fontSpec
.Printf(wxT("-*-%s-light-*-*-*-*-*-*-*-*-*-*-*"),
1218 if ( wxTestFontSpec(fontSpec
) )
1220 xweight
= wxT("light");
1223 fontSpec
.Printf(wxT("-*-%s-thin-*-*-*-*-*-*-*-*-*-*-*"),
1225 if ( wxTestFontSpec(fontSpec
) )
1227 xweight
= wxT("thin");
1234 fontSpec
.Printf(wxT("-*-%s-medium-*-*-*-*-*-*-*-*-*-*-*"),
1236 if ( wxTestFontSpec(fontSpec
) )
1238 xweight
= wxT("medium");
1241 fontSpec
.Printf(wxT("-*-%s-normal-*-*-*-*-*-*-*-*-*-*-*"),
1243 if ( wxTestFontSpec(fontSpec
) )
1245 xweight
= wxT("normal");
1248 fontSpec
.Printf(wxT("-*-%s-regular-*-*-*-*-*-*-*-*-*-*-*"),
1250 if ( wxTestFontSpec(fontSpec
) )
1252 xweight
= wxT("regular");
1258 default: xweight
= wxT("*"); break;
1261 // if pointSize is -1, don't specify any
1263 if ( pointSize
== -1 )
1269 sizeSpec
.Printf(_T("%d"), pointSize
);
1272 // construct the X font spec from our data
1273 fontSpec
.Printf(wxT("-*-%s-%s-%s-normal-*-*-%s-*-*-*-*-%s-%s"),
1274 xfamily
.c_str(), xweight
.c_str(), xstyle
.c_str(),
1275 sizeSpec
.c_str(), xregistry
.c_str(), xencoding
.c_str());
1278 *xFontName
= fontSpec
;
1280 return wxLoadFont(fontSpec
);
1285 // ----------------------------------------------------------------------------
1287 // ----------------------------------------------------------------------------
1289 class wxFontModule
: public wxModule
1296 DECLARE_DYNAMIC_CLASS(wxFontModule
)
1299 IMPLEMENT_DYNAMIC_CLASS(wxFontModule
, wxModule
)
1301 bool wxFontModule::OnInit()
1303 g_fontHash
= new wxHashTable( wxKEY_STRING
);
1308 void wxFontModule::OnExit()
1312 g_fontHash
= (wxHashTable
*)NULL
;
1315 #endif // GTK 2.0/1.x