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()
60 int wxNativeFontInfo::GetPointSize() const
62 return pango_font_description_get_size( description
) / PANGO_SCALE
;
65 wxFontStyle
wxNativeFontInfo::GetStyle() const
67 wxFontStyle m_style
= wxFONTSTYLE_NORMAL
;
69 switch (pango_font_description_get_style( description
))
71 case PANGO_STYLE_NORMAL
:
72 m_style
= wxFONTSTYLE_NORMAL
;
74 case PANGO_STYLE_ITALIC
:
75 m_style
= wxFONTSTYLE_ITALIC
;
77 case PANGO_STYLE_OBLIQUE
:
78 m_style
= wxFONTSTYLE_SLANT
;
85 wxFontWeight
wxNativeFontInfo::GetWeight() const
87 wxFontWeight m_weight
= wxFONTWEIGHT_NORMAL
;
89 switch (pango_font_description_get_weight( description
))
91 case PANGO_WEIGHT_ULTRALIGHT
:
92 m_weight
= wxFONTWEIGHT_LIGHT
;
94 case PANGO_WEIGHT_LIGHT
:
95 m_weight
= wxFONTWEIGHT_LIGHT
;
97 case PANGO_WEIGHT_NORMAL
:
98 m_weight
= wxFONTWEIGHT_NORMAL
;
100 case PANGO_WEIGHT_BOLD
:
101 m_weight
= wxFONTWEIGHT_BOLD
;
103 case PANGO_WEIGHT_ULTRABOLD
:
104 m_weight
= wxFONTWEIGHT_BOLD
;
106 case PANGO_WEIGHT_HEAVY
:
107 m_weight
= wxFONTWEIGHT_BOLD
;
114 bool wxNativeFontInfo::GetUnderlined() const
119 wxString
wxNativeFontInfo::GetFaceName() const
121 wxString tmp
= wxGTK_CONV_BACK( pango_font_description_get_family( description
) );
126 wxFontFamily
wxNativeFontInfo::GetFamily() const
128 return wxFONTFAMILY_SWISS
;
131 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
133 return wxFONTENCODING_SYSTEM
;
137 void wxNativeFontInfo::SetPointSize(int WXUNUSED(pointsize
))
139 wxFAIL_MSG( _T("not implemented") );
142 void wxNativeFontInfo::SetStyle(wxFontStyle
WXUNUSED(style
))
144 wxFAIL_MSG( _T("not implemented") );
147 void wxNativeFontInfo::SetWeight(wxFontWeight
WXUNUSED(weight
))
149 wxFAIL_MSG( _T("not implemented") );
152 void wxNativeFontInfo::SetUnderlined(bool WXUNUSED(underlined
))
154 wxFAIL_MSG( _T("not implemented") );
157 void wxNativeFontInfo::SetFaceName(wxString
WXUNUSED(facename
))
159 wxFAIL_MSG( _T("not implemented") );
162 void wxNativeFontInfo::SetFamily(wxFontFamily
WXUNUSED(family
))
164 wxFAIL_MSG( _T("not implemented") );
167 void wxNativeFontInfo::SetEncoding(wxFontEncoding
WXUNUSED(encoding
))
169 wxFAIL_MSG( _T("not implemented") );
174 bool wxNativeFontInfo::FromString(const wxString
& s
)
177 pango_font_description_free( description
);
179 description
= pango_font_description_from_string( wxGTK_CONV( s
) );
184 wxString
wxNativeFontInfo::ToString() const
186 char *str
= pango_font_description_to_string( description
);
187 wxString tmp
= wxGTK_CONV_BACK( str
);
193 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
195 return FromString( s
);
198 wxString
wxNativeFontInfo::ToUserString() const
203 // ----------------------------------------------------------------------------
204 // wxNativeEncodingInfo
205 // ----------------------------------------------------------------------------
207 bool wxNativeEncodingInfo::FromString(const wxString
& s
)
212 wxString
wxNativeEncodingInfo::ToString() const
214 return wxEmptyString
;
217 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
222 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
223 wxNativeEncodingInfo
*info
)
232 #pragma message disable nosimpint
235 #include <X11/Xlib.h>
238 #pragma message enable nosimpint
241 #include "wx/utils.h" // for wxGetDisplay()
242 #elif defined(__WXGTK__)
243 // we have to declare struct tm to avoid problems with first forward
244 // declaring it in C code (glib.h included from gdk.h does it) and then
245 // defining it when time.h is included from the headers below - this is
246 // known not to work at least with Sun CC 6.01
253 // ----------------------------------------------------------------------------
255 // ----------------------------------------------------------------------------
257 static wxHashTable
*g_fontHash
= (wxHashTable
*) NULL
;
259 // ----------------------------------------------------------------------------
261 // ----------------------------------------------------------------------------
263 // define the functions to create and destroy native fonts for this toolkit
265 wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
267 return XLoadQueryFont((Display
*)wxGetDisplay(), fontSpec
);
270 inline void wxFreeFont(wxNativeFont font
)
272 XFreeFont((Display
*)wxGetDisplay(), (XFontStruct
*)font
);
274 #elif defined(__WXGTK__)
275 wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
277 // VZ: we should use gdk_fontset_load() instead of gdk_font_load()
278 // here to be able to display Japanese fonts correctly (at least
279 // this is what people report) but unfortunately doing it results
280 // in tons of warnings when using GTK with "normal" European
281 // languages and so we can't always do it and I don't know enough
282 // to determine when should this be done... (FIXME)
283 return gdk_font_load( wxConvertWX2MB(fontSpec
) );
286 inline void wxFreeFont(wxNativeFont font
)
288 gdk_font_unref(font
);
291 #error "Unknown GUI toolkit"
294 static bool wxTestFontSpec(const wxString
& fontspec
);
296 static wxNativeFont
wxLoadQueryFont(int pointSize
,
301 const wxString
& facename
,
302 const wxString
& xregistry
,
303 const wxString
& xencoding
,
304 wxString
* xFontName
);
306 // ============================================================================
308 // ============================================================================
310 // ----------------------------------------------------------------------------
311 // wxNativeEncodingInfo
312 // ----------------------------------------------------------------------------
314 // convert to/from the string representation: format is
315 // encodingid;registry;encoding[;facename]
316 bool wxNativeEncodingInfo::FromString(const wxString
& s
)
318 // use ";", not "-" because it may be part of encoding name
319 wxStringTokenizer
tokenizer(s
, _T(";"));
321 wxString encid
= tokenizer
.GetNextToken();
323 if ( !encid
.ToLong(&enc
) )
325 encoding
= (wxFontEncoding
)enc
;
327 xregistry
= tokenizer
.GetNextToken();
331 xencoding
= tokenizer
.GetNextToken();
336 facename
= tokenizer
.GetNextToken();
341 wxString
wxNativeEncodingInfo::ToString() const
344 s
<< (long)encoding
<< _T(';') << xregistry
<< _T(';') << xencoding
;
347 s
<< _T(';') << facename
;
353 // ----------------------------------------------------------------------------
355 // ----------------------------------------------------------------------------
357 void wxNativeFontInfo::Init()
362 bool wxNativeFontInfo::FromString(const wxString
& s
)
364 wxStringTokenizer
tokenizer(s
, _T(";"));
367 wxString token
= tokenizer
.GetNextToken();
368 if ( token
!= _T('0') )
371 xFontName
= tokenizer
.GetNextToken();
373 // this should be the end
374 if ( tokenizer
.HasMoreTokens() )
377 return FromXFontName(xFontName
);
380 wxString
wxNativeFontInfo::ToString() const
383 return wxString::Format(_T("%d;%s"), 0, GetXFontName().c_str());
386 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
388 return FromXFontName(s
);
391 wxString
wxNativeFontInfo::ToUserString() const
393 return GetXFontName();
396 bool wxNativeFontInfo::HasElements() const
398 // we suppose that the foundry is never empty, so if it is it means that we
399 // had never parsed the XLFD
400 return !fontElements
[0].empty();
403 wxString
wxNativeFontInfo::GetXFontComponent(wxXLFDField field
) const
405 wxCHECK_MSG( field
< wxXLFD_MAX
, _T(""), _T("invalid XLFD field") );
407 if ( !HasElements() )
410 if ( !((wxNativeFontInfo
*)this)->FromXFontName(xFontName
) )
414 return fontElements
[field
];
417 bool wxNativeFontInfo::FromXFontName(const wxString
& fontname
)
419 // TODO: we should be able to handle the font aliases here, but how?
420 wxStringTokenizer
tokenizer(fontname
, _T("-"));
422 // skip the leading, usually empty field (font name registry)
423 if ( !tokenizer
.HasMoreTokens() )
426 (void)tokenizer
.GetNextToken();
428 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
430 if ( !tokenizer
.HasMoreTokens() )
432 // not enough elements in the XLFD - or maybe an alias
436 wxString field
= tokenizer
.GetNextToken();
437 if ( !field
.empty() && field
!= _T('*') )
439 // we're really initialized now
443 fontElements
[n
] = field
;
446 // this should be all
447 if ( tokenizer
.HasMoreTokens() )
453 wxString
wxNativeFontInfo::GetXFontName() const
455 if ( xFontName
.empty() )
457 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
459 // replace the non specified elements with '*' except for the
460 // additional style which is usually just omitted
461 wxString elt
= fontElements
[n
];
462 if ( elt
.empty() && n
!= wxXLFD_ADDSTYLE
)
468 ((wxNativeFontInfo
*)this)->xFontName
<< _T('-') << elt
;
476 wxNativeFontInfo::SetXFontComponent(wxXLFDField field
, const wxString
& value
)
478 wxCHECK_RET( field
< wxXLFD_MAX
, _T("invalid XLFD field") );
480 // this class should be initialized with a valid font spec first and only
481 // then the fields may be modified!
482 wxASSERT_MSG( !IsDefault(), _T("can't modify an uninitialized XLFD") );
484 if ( !HasElements() )
487 if ( !((wxNativeFontInfo
*)this)->FromXFontName(xFontName
) )
489 wxFAIL_MSG( _T("can't set font element for invalid XLFD") );
495 fontElements
[field
] = value
;
497 // invalidate the XFLD, it doesn't correspond to the font elements any more
501 void wxNativeFontInfo::SetXFontName(const wxString
& xFontName_
)
503 // invalidate the font elements, GetXFontComponent() will reparse the XLFD
504 fontElements
[0].clear();
506 xFontName
= xFontName_
;
511 int wxNativeFontInfo::GetPointSize() const
513 const wxString s
= GetXFontComponent(wxXLFD_POINTSIZE
);
515 // return -1 to indicate that the size is unknown
517 return s
.ToLong(&l
) ? l
: -1;
520 wxFontStyle
wxNativeFontInfo::GetStyle() const
522 const wxString s
= GetXFontComponent(wxXLFD_SLANT
);
524 if ( s
.length() != 1 )
526 // it is really unknown but we don't have any way to return it from
528 return wxFONTSTYLE_NORMAL
;
534 // again, unknown but consider normal by default
537 return wxFONTSTYLE_NORMAL
;
540 return wxFONTSTYLE_ITALIC
;
543 return wxFONTSTYLE_SLANT
;
547 wxFontWeight
wxNativeFontInfo::GetWeight() const
549 const wxString s
= GetXFontComponent(wxXLFD_WEIGHT
).MakeLower();
550 if ( s
.find(_T("bold")) != wxString::npos
|| s
== _T("black") )
551 return wxFONTWEIGHT_BOLD
;
552 else if ( s
== _T("light") )
553 return wxFONTWEIGHT_LIGHT
;
555 return wxFONTWEIGHT_NORMAL
;
558 bool wxNativeFontInfo::GetUnderlined() const
560 // X fonts are never underlined
564 wxString
wxNativeFontInfo::GetFaceName() const
566 // wxWindows facename probably more accurately corresponds to X family
567 return GetXFontComponent(wxXLFD_FAMILY
);
570 wxFontFamily
wxNativeFontInfo::GetFamily() const
572 // and wxWindows family -- to X foundry, but we have to translate it to
573 // wxFontFamily somehow...
574 wxFAIL_MSG(_T("not implemented")); // GetXFontComponent(wxXLFD_FOUNDRY);
576 return wxFONTFAMILY_DEFAULT
;
579 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
581 // we already have the code for this but need to refactor it first
582 wxFAIL_MSG( _T("not implemented") );
584 return wxFONTENCODING_MAX
;
587 void wxNativeFontInfo::SetPointSize(int pointsize
)
589 SetXFontComponent(wxXLFD_POINTSIZE
, wxString::Format(_T("%d"), pointsize
));
592 void wxNativeFontInfo::SetStyle(wxFontStyle style
)
597 case wxFONTSTYLE_ITALIC
:
601 case wxFONTSTYLE_SLANT
:
605 case wxFONTSTYLE_NORMAL
:
609 wxFAIL_MSG( _T("unknown wxFontStyle in wxNativeFontInfo::SetStyle") );
613 SetXFontComponent(wxXLFD_SLANT
, s
);
616 void wxNativeFontInfo::SetWeight(wxFontWeight weight
)
621 case wxFONTWEIGHT_BOLD
:
625 case wxFONTWEIGHT_LIGHT
:
629 case wxFONTWEIGHT_NORMAL
:
634 wxFAIL_MSG( _T("unknown wxFontWeight in wxNativeFontInfo::SetWeight") );
638 SetXFontComponent(wxXLFD_WEIGHT
, s
);
641 void wxNativeFontInfo::SetUnderlined(bool WXUNUSED(underlined
))
643 // can't do this under X
646 void wxNativeFontInfo::SetFaceName(wxString facename
)
648 SetXFontComponent(wxXLFD_FAMILY
, facename
);
651 void wxNativeFontInfo::SetFamily(wxFontFamily family
)
653 // wxFontFamily -> X foundry, anyone?
654 wxFAIL_MSG( _T("not implemented") );
656 // SetXFontComponent(wxXLFD_FOUNDRY, ...);
659 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding
)
661 wxNativeEncodingInfo info
;
662 if ( wxGetNativeFontEncoding(encoding
, &info
) )
664 SetXFontComponent(wxXLFD_ENCODING
, info
.xencoding
);
665 SetXFontComponent(wxXLFD_REGISTRY
, info
.xregistry
);
669 // ----------------------------------------------------------------------------
671 // ----------------------------------------------------------------------------
673 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
674 wxNativeEncodingInfo
*info
)
676 wxCHECK_MSG( info
, FALSE
, _T("bad pointer in wxGetNativeFontEncoding") );
678 if ( encoding
== wxFONTENCODING_DEFAULT
)
680 encoding
= wxFont::GetDefaultEncoding();
685 case wxFONTENCODING_ISO8859_1
:
686 case wxFONTENCODING_ISO8859_2
:
687 case wxFONTENCODING_ISO8859_3
:
688 case wxFONTENCODING_ISO8859_4
:
689 case wxFONTENCODING_ISO8859_5
:
690 case wxFONTENCODING_ISO8859_6
:
691 case wxFONTENCODING_ISO8859_7
:
692 case wxFONTENCODING_ISO8859_8
:
693 case wxFONTENCODING_ISO8859_9
:
694 case wxFONTENCODING_ISO8859_10
:
695 case wxFONTENCODING_ISO8859_11
:
696 case wxFONTENCODING_ISO8859_12
:
697 case wxFONTENCODING_ISO8859_13
:
698 case wxFONTENCODING_ISO8859_14
:
699 case wxFONTENCODING_ISO8859_15
:
701 int cp
= encoding
- wxFONTENCODING_ISO8859_1
+ 1;
702 info
->xregistry
= wxT("iso8859");
703 info
->xencoding
.Printf(wxT("%d"), cp
);
707 case wxFONTENCODING_UTF8
:
708 info
->xregistry
= wxT("iso10646");
709 info
->xencoding
= wxT("*");
712 case wxFONTENCODING_GB2312
:
713 info
->xregistry
= wxT("GB2312"); // or the otherway round?
714 info
->xencoding
= wxT("*");
717 case wxFONTENCODING_KOI8
:
718 info
->xregistry
= wxT("koi8");
720 // we don't make distinction between koi8-r, koi8-u and koi8-ru (so far)
721 info
->xencoding
= wxT("*");
724 case wxFONTENCODING_CP1250
:
725 case wxFONTENCODING_CP1251
:
726 case wxFONTENCODING_CP1252
:
727 case wxFONTENCODING_CP1253
:
728 case wxFONTENCODING_CP1254
:
729 case wxFONTENCODING_CP1255
:
730 case wxFONTENCODING_CP1256
:
731 case wxFONTENCODING_CP1257
:
733 int cp
= encoding
- wxFONTENCODING_CP1250
+ 1250;
734 info
->xregistry
= wxT("microsoft");
735 info
->xencoding
.Printf(wxT("cp%d"), cp
);
739 case wxFONTENCODING_SYSTEM
:
741 info
->xencoding
= wxT("*");
745 // don't know how to translate this encoding into X fontspec
749 info
->encoding
= encoding
;
754 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
757 fontspec
.Printf(_T("-*-%s-*-*-*-*-*-*-*-*-*-*-%s-%s"),
758 !info
.facename
? _T("*") : info
.facename
.c_str(),
759 info
.xregistry
.c_str(),
760 info
.xencoding
.c_str());
762 return wxTestFontSpec(fontspec
);
765 // ----------------------------------------------------------------------------
766 // X-specific functions
767 // ----------------------------------------------------------------------------
769 wxNativeFont
wxLoadQueryNearestFont(int pointSize
,
774 const wxString
&facename
,
775 wxFontEncoding encoding
,
778 if ( encoding
== wxFONTENCODING_DEFAULT
)
780 encoding
= wxFont::GetDefaultEncoding();
783 // first determine the encoding - if the font doesn't exist at all in this
784 // encoding, it's useless to do all other approximations (i.e. size,
785 // family &c don't matter much)
786 wxNativeEncodingInfo info
;
787 if ( encoding
== wxFONTENCODING_SYSTEM
)
789 // This will always work so we don't test to save time
790 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
794 if ( !wxGetNativeFontEncoding(encoding
, &info
) ||
795 !wxTestFontEncoding(info
) )
798 if ( !wxFontMapper::Get()->GetAltForEncoding(encoding
, &info
) )
799 #endif // wxUSE_FONTMAP
801 // unspported encoding - replace it with the default
803 // NB: we can't just return 0 from here because wxGTK code doesn't
804 // check for it (i.e. it supposes that we'll always succeed),
805 // so it would provoke a crash
806 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
811 // OK, we have the correct xregistry/xencoding in info structure
812 wxNativeFont font
= 0;
814 // if we already have the X font name, try to use it
815 if( xFontName
&& !xFontName
->IsEmpty() )
818 // Make sure point size is correct for scale factor.
820 wxStringTokenizer
tokenizer(*xFontName
, _T("-"), wxTOKEN_RET_DELIMS
);
821 wxString newFontName
;
823 for(int i
= 0; i
< 8; i
++)
824 newFontName
+= tokenizer
.NextToken();
826 (void) tokenizer
.NextToken();
828 newFontName
+= wxString::Format(wxT("%d-"), pointSize
);
830 while(tokenizer
.HasMoreTokens())
831 newFontName
+= tokenizer
.GetNextToken();
833 font
= wxLoadFont(newFontName
);
836 *xFontName
= newFontName
;
841 // search up and down by stepsize 10
842 int max_size
= pointSize
+ 20 * (1 + (pointSize
/180));
843 int min_size
= pointSize
- 20 * (1 + (pointSize
/180));
845 int i
, round
; // counters
847 // first round: search for equal, then for smaller and for larger size with the given weight and style
848 int testweight
= weight
;
849 int teststyle
= style
;
851 for ( round
= 0; round
< 3; round
++ )
853 // second round: use normal weight
856 if ( testweight
!= wxNORMAL
)
858 testweight
= wxNORMAL
;
862 ++round
; // fall through to third round
866 // third round: ... and use normal style
869 if ( teststyle
!= wxNORMAL
)
871 teststyle
= wxNORMAL
;
878 // Search for equal or smaller size (approx.)
879 for ( i
= pointSize
; !font
&& i
>= 10 && i
>= min_size
; i
-= 10 )
881 font
= wxLoadQueryFont(i
, family
, teststyle
, testweight
, underlined
,
882 facename
, info
.xregistry
, info
.xencoding
,
886 // Search for larger size (approx.)
887 for ( i
= pointSize
+ 10; !font
&& i
<= max_size
; i
+= 10 )
889 font
= wxLoadQueryFont(i
, family
, teststyle
, testweight
, underlined
,
890 facename
, info
.xregistry
, info
.xencoding
,
895 // Try default family
896 if ( !font
&& family
!= wxDEFAULT
)
898 font
= wxLoadQueryFont(pointSize
, wxDEFAULT
, style
, weight
,
899 underlined
, facename
,
900 info
.xregistry
, info
.xencoding
,
904 // ignore size, family, style and weight but try to find font with the
905 // given facename and encoding
908 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
909 underlined
, facename
,
910 info
.xregistry
, info
.xencoding
,
913 // ignore family as well
916 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
917 underlined
, wxEmptyString
,
918 info
.xregistry
, info
.xencoding
,
921 // if it still failed, try to get the font of any size but
922 // with the requested encoding: this can happen if the
923 // encoding is only available in one size which happens to be
924 // different from 120
927 font
= wxLoadQueryFont(-1, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
928 FALSE
, wxEmptyString
,
929 info
.xregistry
, info
.xencoding
,
932 // this should never happen as we had tested for it in the
933 // very beginning, but if it does, do return something non
934 // NULL or we'd crash in wxFont code
937 wxFAIL_MSG( _T("this encoding should be available!") );
939 font
= wxLoadQueryFont(-1,
940 wxDEFAULT
, wxNORMAL
, wxNORMAL
,
941 FALSE
, wxEmptyString
,
953 // ----------------------------------------------------------------------------
955 // ----------------------------------------------------------------------------
957 // returns TRUE if there are any fonts matching this font spec
958 static bool wxTestFontSpec(const wxString
& fontspec
)
960 // some X servers will fail to load this font because there are too many
961 // matches so we must test explicitly for this
962 if ( fontspec
== _T("-*-*-*-*-*-*-*-*-*-*-*-*-*-*") )
967 wxNativeFont test
= (wxNativeFont
) g_fontHash
->Get( fontspec
);
973 test
= wxLoadFont(fontspec
);
974 g_fontHash
->Put( fontspec
, (wxObject
*) test
);
988 static wxNativeFont
wxLoadQueryFont(int pointSize
,
992 bool WXUNUSED(underlined
),
993 const wxString
& facename
,
994 const wxString
& xregistry
,
995 const wxString
& xencoding
,
1001 case wxDECORATIVE
: xfamily
= wxT("lucida"); break;
1002 case wxROMAN
: xfamily
= wxT("times"); break;
1003 case wxMODERN
: xfamily
= wxT("courier"); break;
1004 case wxSWISS
: xfamily
= wxT("helvetica"); break;
1005 case wxTELETYPE
: xfamily
= wxT("lucidatypewriter"); break;
1006 case wxSCRIPT
: xfamily
= wxT("utopia"); break;
1007 default: xfamily
= wxT("*");
1015 xweight
= MWLF_WEIGHT_BOLD
;
1020 xweight
= MWLF_WEIGHT_LIGHT
;
1025 xweight
= MWLF_WEIGHT_NORMAL
;
1031 xweight
= MWLF_WEIGHT_DEFAULT
;
1035 GR_SCREEN_INFO screenInfo
;
1036 GrGetScreenInfo(& screenInfo
);
1038 int yPixelsPerCM
= screenInfo
.ydpcm
;
1040 // A point is 1/72 of an inch.
1041 // An inch is 2.541 cm.
1042 // So pixelHeight = (pointSize / 72) (inches) * 2.541 (for cm) * yPixelsPerCM (for pixels)
1043 // In fact pointSize is 10 * the normal point size so
1046 int pixelHeight
= (int) ( (((float)pointSize
) / 720.0) * 2.541 * (float) yPixelsPerCM
) ;
1048 // An alternative: assume that the screen is 72 dpi.
1049 //int pixelHeight = (int) (((float)pointSize / 720.0) * 72.0) ;
1050 //int pixelHeight = (int) ((float)pointSize / 10.0) ;
1053 logFont
.lfHeight
= pixelHeight
;
1054 logFont
.lfWidth
= 0;
1055 logFont
.lfEscapement
= 0;
1056 logFont
.lfOrientation
= 0;
1057 logFont
.lfWeight
= xweight
;
1058 logFont
.lfItalic
= (style
== wxNORMAL
? 0 : 1) ;
1059 logFont
.lfUnderline
= 0;
1060 logFont
.lfStrikeOut
= 0;
1061 logFont
.lfCharSet
= MWLF_CHARSET_DEFAULT
; // TODO: select appropriate one
1062 logFont
.lfOutPrecision
= MWLF_TYPE_DEFAULT
;
1063 logFont
.lfClipPrecision
= 0; // Not used
1064 logFont
.lfRoman
= (family
== wxROMAN
? 1 : 0) ;
1065 logFont
.lfSerif
= (family
== wxSWISS
? 0 : 1) ;
1066 logFont
.lfSansSerif
= !logFont
.lfSerif
;
1067 logFont
.lfModern
= (family
== wxMODERN
? 1 : 0) ;
1068 logFont
.lfProportional
= (family
== wxTELETYPE
? 0 : 1) ;
1069 logFont
.lfOblique
= 0;
1070 logFont
.lfSmallCaps
= 0;
1071 logFont
.lfPitch
= 0; // 0 = default
1072 strcpy(logFont
.lfFaceName
, facename
.c_str());
1074 XFontStruct
* fontInfo
= (XFontStruct
*) malloc(sizeof(XFontStruct
));
1075 fontInfo
->fid
= GrCreateFont((GR_CHAR
*) facename
.c_str(), pixelHeight
, & logFont
);
1076 GrGetFontInfo(fontInfo
->fid
, & fontInfo
->info
);
1077 return (wxNativeFont
) fontInfo
;
1081 if (!facename
.IsEmpty())
1083 fontSpec
.Printf(wxT("-*-%s-*-*-normal-*-*-*-*-*-*-*-*-*"),
1086 if ( wxTestFontSpec(fontSpec
) )
1090 //else: no such family, use default one instead
1097 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1099 if ( wxTestFontSpec(fontSpec
) )
1104 // fall through - try wxITALIC now
1107 fontSpec
.Printf(wxT("-*-%s-*-i-*-*-*-*-*-*-*-*-*-*"),
1109 if ( wxTestFontSpec(fontSpec
) )
1113 else if ( style
== wxITALIC
) // and not wxSLANT
1116 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1118 if ( wxTestFontSpec(fontSpec
) )
1124 // no italic, no slant - leave default
1131 wxFAIL_MSG(_T("unknown font style"));
1132 // fall back to normal
1144 fontSpec
.Printf(wxT("-*-%s-bold-*-*-*-*-*-*-*-*-*-*-*"),
1146 if ( wxTestFontSpec(fontSpec
) )
1148 xweight
= wxT("bold");
1151 fontSpec
.Printf(wxT("-*-%s-heavy-*-*-*-*-*-*-*-*-*-*-*"),
1153 if ( wxTestFontSpec(fontSpec
) )
1155 xweight
= wxT("heavy");
1158 fontSpec
.Printf(wxT("-*-%s-extrabold-*-*-*-*-*-*-*-*-*-*-*"),
1160 if ( wxTestFontSpec(fontSpec
) )
1162 xweight
= wxT("extrabold");
1165 fontSpec
.Printf(wxT("-*-%s-demibold-*-*-*-*-*-*-*-*-*-*-*"),
1167 if ( wxTestFontSpec(fontSpec
) )
1169 xweight
= wxT("demibold");
1172 fontSpec
.Printf(wxT("-*-%s-black-*-*-*-*-*-*-*-*-*-*-*"),
1174 if ( wxTestFontSpec(fontSpec
) )
1176 xweight
= wxT("black");
1179 fontSpec
.Printf(wxT("-*-%s-ultrablack-*-*-*-*-*-*-*-*-*-*-*"),
1181 if ( wxTestFontSpec(fontSpec
) )
1183 xweight
= wxT("ultrablack");
1190 fontSpec
.Printf(wxT("-*-%s-light-*-*-*-*-*-*-*-*-*-*-*"),
1192 if ( wxTestFontSpec(fontSpec
) )
1194 xweight
= wxT("light");
1197 fontSpec
.Printf(wxT("-*-%s-thin-*-*-*-*-*-*-*-*-*-*-*"),
1199 if ( wxTestFontSpec(fontSpec
) )
1201 xweight
= wxT("thin");
1208 fontSpec
.Printf(wxT("-*-%s-medium-*-*-*-*-*-*-*-*-*-*-*"),
1210 if ( wxTestFontSpec(fontSpec
) )
1212 xweight
= wxT("medium");
1215 fontSpec
.Printf(wxT("-*-%s-normal-*-*-*-*-*-*-*-*-*-*-*"),
1217 if ( wxTestFontSpec(fontSpec
) )
1219 xweight
= wxT("normal");
1222 fontSpec
.Printf(wxT("-*-%s-regular-*-*-*-*-*-*-*-*-*-*-*"),
1224 if ( wxTestFontSpec(fontSpec
) )
1226 xweight
= wxT("regular");
1232 default: xweight
= wxT("*"); break;
1235 // if pointSize is -1, don't specify any
1237 if ( pointSize
== -1 )
1243 sizeSpec
.Printf(_T("%d"), pointSize
);
1246 // construct the X font spec from our data
1247 fontSpec
.Printf(wxT("-*-%s-%s-%s-normal-*-*-%s-*-*-*-*-%s-%s"),
1248 xfamily
.c_str(), xweight
.c_str(), xstyle
.c_str(),
1249 sizeSpec
.c_str(), xregistry
.c_str(), xencoding
.c_str());
1252 *xFontName
= fontSpec
;
1254 return wxLoadFont(fontSpec
);
1259 // ----------------------------------------------------------------------------
1261 // ----------------------------------------------------------------------------
1263 class wxFontModule
: public wxModule
1270 DECLARE_DYNAMIC_CLASS(wxFontModule
)
1273 IMPLEMENT_DYNAMIC_CLASS(wxFontModule
, wxModule
)
1275 bool wxFontModule::OnInit()
1277 g_fontHash
= new wxHashTable( wxKEY_STRING
);
1282 void wxFontModule::OnExit()
1286 g_fontHash
= (wxHashTable
*)NULL
;
1289 #endif // GTK 2.0/1.x