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 wxNativeFontInfo::wxNativeFontInfo(const wxNativeFontInfo
& info
)
63 description
= pango_font_description_copy(info
.description
);
68 wxNativeFontInfo::~wxNativeFontInfo()
71 pango_font_description_free(description
);
74 int wxNativeFontInfo::GetPointSize() const
76 return pango_font_description_get_size( description
) / PANGO_SCALE
;
79 wxFontStyle
wxNativeFontInfo::GetStyle() const
81 wxFontStyle m_style
= wxFONTSTYLE_NORMAL
;
83 switch (pango_font_description_get_style( description
))
85 case PANGO_STYLE_NORMAL
:
86 m_style
= wxFONTSTYLE_NORMAL
;
88 case PANGO_STYLE_ITALIC
:
89 m_style
= wxFONTSTYLE_ITALIC
;
91 case PANGO_STYLE_OBLIQUE
:
92 m_style
= wxFONTSTYLE_SLANT
;
99 wxFontWeight
wxNativeFontInfo::GetWeight() const
101 wxFontWeight m_weight
= wxFONTWEIGHT_NORMAL
;
103 switch (pango_font_description_get_weight( description
))
105 case PANGO_WEIGHT_ULTRALIGHT
:
106 m_weight
= wxFONTWEIGHT_LIGHT
;
108 case PANGO_WEIGHT_LIGHT
:
109 m_weight
= wxFONTWEIGHT_LIGHT
;
111 case PANGO_WEIGHT_NORMAL
:
112 m_weight
= wxFONTWEIGHT_NORMAL
;
114 case PANGO_WEIGHT_BOLD
:
115 m_weight
= wxFONTWEIGHT_BOLD
;
117 case PANGO_WEIGHT_ULTRABOLD
:
118 m_weight
= wxFONTWEIGHT_BOLD
;
120 case PANGO_WEIGHT_HEAVY
:
121 m_weight
= wxFONTWEIGHT_BOLD
;
128 bool wxNativeFontInfo::GetUnderlined() const
133 wxString
wxNativeFontInfo::GetFaceName() const
135 wxString tmp
= wxGTK_CONV_BACK( pango_font_description_get_family( description
) );
140 wxFontFamily
wxNativeFontInfo::GetFamily() const
142 return wxFONTFAMILY_SWISS
;
145 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
147 return wxFONTENCODING_SYSTEM
;
151 void wxNativeFontInfo::SetPointSize(int WXUNUSED(pointsize
))
153 wxFAIL_MSG( _T("not implemented") );
156 void wxNativeFontInfo::SetStyle(wxFontStyle
WXUNUSED(style
))
158 wxFAIL_MSG( _T("not implemented") );
161 void wxNativeFontInfo::SetWeight(wxFontWeight
WXUNUSED(weight
))
163 wxFAIL_MSG( _T("not implemented") );
166 void wxNativeFontInfo::SetUnderlined(bool WXUNUSED(underlined
))
168 wxFAIL_MSG( _T("not implemented") );
171 void wxNativeFontInfo::SetFaceName(wxString
WXUNUSED(facename
))
173 wxFAIL_MSG( _T("not implemented") );
176 void wxNativeFontInfo::SetFamily(wxFontFamily
WXUNUSED(family
))
178 wxFAIL_MSG( _T("not implemented") );
181 void wxNativeFontInfo::SetEncoding(wxFontEncoding
WXUNUSED(encoding
))
183 wxFAIL_MSG( _T("not implemented") );
188 bool wxNativeFontInfo::FromString(const wxString
& s
)
191 pango_font_description_free( description
);
193 description
= pango_font_description_from_string( wxGTK_CONV( s
) );
198 wxString
wxNativeFontInfo::ToString() const
200 char *str
= pango_font_description_to_string( description
);
201 wxString tmp
= wxGTK_CONV_BACK( str
);
207 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
209 return FromString( s
);
212 wxString
wxNativeFontInfo::ToUserString() const
217 // ----------------------------------------------------------------------------
218 // wxNativeEncodingInfo
219 // ----------------------------------------------------------------------------
221 bool wxNativeEncodingInfo::FromString(const wxString
& s
)
226 wxString
wxNativeEncodingInfo::ToString() const
228 return wxEmptyString
;
231 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
236 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
237 wxNativeEncodingInfo
*info
)
246 #pragma message disable nosimpint
249 #include <X11/Xlib.h>
252 #pragma message enable nosimpint
255 #include "wx/utils.h" // for wxGetDisplay()
256 #elif defined(__WXGTK__)
257 // we have to declare struct tm to avoid problems with first forward
258 // declaring it in C code (glib.h included from gdk.h does it) and then
259 // defining it when time.h is included from the headers below - this is
260 // known not to work at least with Sun CC 6.01
267 // ----------------------------------------------------------------------------
269 // ----------------------------------------------------------------------------
271 static wxHashTable
*g_fontHash
= (wxHashTable
*) NULL
;
273 // ----------------------------------------------------------------------------
275 // ----------------------------------------------------------------------------
277 // define the functions to create and destroy native fonts for this toolkit
279 wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
281 return XLoadQueryFont((Display
*)wxGetDisplay(), fontSpec
);
284 inline void wxFreeFont(wxNativeFont font
)
286 XFreeFont((Display
*)wxGetDisplay(), (XFontStruct
*)font
);
288 #elif defined(__WXGTK__)
289 wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
291 // VZ: we should use gdk_fontset_load() instead of gdk_font_load()
292 // here to be able to display Japanese fonts correctly (at least
293 // this is what people report) but unfortunately doing it results
294 // in tons of warnings when using GTK with "normal" European
295 // languages and so we can't always do it and I don't know enough
296 // to determine when should this be done... (FIXME)
297 return gdk_font_load( wxConvertWX2MB(fontSpec
) );
300 inline void wxFreeFont(wxNativeFont font
)
302 gdk_font_unref(font
);
305 #error "Unknown GUI toolkit"
308 static bool wxTestFontSpec(const wxString
& fontspec
);
310 static wxNativeFont
wxLoadQueryFont(int pointSize
,
315 const wxString
& facename
,
316 const wxString
& xregistry
,
317 const wxString
& xencoding
,
318 wxString
* xFontName
);
320 // ============================================================================
322 // ============================================================================
324 // ----------------------------------------------------------------------------
325 // wxNativeEncodingInfo
326 // ----------------------------------------------------------------------------
328 // convert to/from the string representation: format is
329 // encodingid;registry;encoding[;facename]
330 bool wxNativeEncodingInfo::FromString(const wxString
& s
)
332 // use ";", not "-" because it may be part of encoding name
333 wxStringTokenizer
tokenizer(s
, _T(";"));
335 wxString encid
= tokenizer
.GetNextToken();
337 if ( !encid
.ToLong(&enc
) )
339 encoding
= (wxFontEncoding
)enc
;
341 xregistry
= tokenizer
.GetNextToken();
345 xencoding
= tokenizer
.GetNextToken();
350 facename
= tokenizer
.GetNextToken();
355 wxString
wxNativeEncodingInfo::ToString() const
358 s
<< (long)encoding
<< _T(';') << xregistry
<< _T(';') << xencoding
;
361 s
<< _T(';') << facename
;
367 // ----------------------------------------------------------------------------
369 // ----------------------------------------------------------------------------
371 void wxNativeFontInfo::Init()
376 bool wxNativeFontInfo::FromString(const wxString
& s
)
378 wxStringTokenizer
tokenizer(s
, _T(";"));
381 wxString token
= tokenizer
.GetNextToken();
382 if ( token
!= _T('0') )
385 xFontName
= tokenizer
.GetNextToken();
387 // this should be the end
388 if ( tokenizer
.HasMoreTokens() )
391 return FromXFontName(xFontName
);
394 wxString
wxNativeFontInfo::ToString() const
397 return wxString::Format(_T("%d;%s"), 0, GetXFontName().c_str());
400 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
402 return FromXFontName(s
);
405 wxString
wxNativeFontInfo::ToUserString() const
407 return GetXFontName();
410 bool wxNativeFontInfo::HasElements() const
412 // we suppose that the foundry is never empty, so if it is it means that we
413 // had never parsed the XLFD
414 return !fontElements
[0].empty();
417 wxString
wxNativeFontInfo::GetXFontComponent(wxXLFDField field
) const
419 wxCHECK_MSG( field
< wxXLFD_MAX
, _T(""), _T("invalid XLFD field") );
421 if ( !HasElements() )
424 if ( !((wxNativeFontInfo
*)this)->FromXFontName(xFontName
) )
428 return fontElements
[field
];
431 bool wxNativeFontInfo::FromXFontName(const wxString
& fontname
)
433 // TODO: we should be able to handle the font aliases here, but how?
434 wxStringTokenizer
tokenizer(fontname
, _T("-"));
436 // skip the leading, usually empty field (font name registry)
437 if ( !tokenizer
.HasMoreTokens() )
440 (void)tokenizer
.GetNextToken();
442 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
444 if ( !tokenizer
.HasMoreTokens() )
446 // not enough elements in the XLFD - or maybe an alias
450 wxString field
= tokenizer
.GetNextToken();
451 if ( !field
.empty() && field
!= _T('*') )
453 // we're really initialized now
457 fontElements
[n
] = field
;
460 // this should be all
461 if ( tokenizer
.HasMoreTokens() )
467 wxString
wxNativeFontInfo::GetXFontName() const
469 if ( xFontName
.empty() )
471 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
473 // replace the non specified elements with '*' except for the
474 // additional style which is usually just omitted
475 wxString elt
= fontElements
[n
];
476 if ( elt
.empty() && n
!= wxXLFD_ADDSTYLE
)
482 ((wxNativeFontInfo
*)this)->xFontName
<< _T('-') << elt
;
490 wxNativeFontInfo::SetXFontComponent(wxXLFDField field
, const wxString
& value
)
492 wxCHECK_RET( field
< wxXLFD_MAX
, _T("invalid XLFD field") );
494 // this class should be initialized with a valid font spec first and only
495 // then the fields may be modified!
496 wxASSERT_MSG( !IsDefault(), _T("can't modify an uninitialized XLFD") );
498 if ( !HasElements() )
501 if ( !((wxNativeFontInfo
*)this)->FromXFontName(xFontName
) )
503 wxFAIL_MSG( _T("can't set font element for invalid XLFD") );
509 fontElements
[field
] = value
;
511 // invalidate the XFLD, it doesn't correspond to the font elements any more
515 void wxNativeFontInfo::SetXFontName(const wxString
& xFontName_
)
517 // invalidate the font elements, GetXFontComponent() will reparse the XLFD
518 fontElements
[0].clear();
520 xFontName
= xFontName_
;
525 int wxNativeFontInfo::GetPointSize() const
527 const wxString s
= GetXFontComponent(wxXLFD_POINTSIZE
);
529 // return -1 to indicate that the size is unknown
531 return s
.ToLong(&l
) ? l
: -1;
534 wxFontStyle
wxNativeFontInfo::GetStyle() const
536 const wxString s
= GetXFontComponent(wxXLFD_SLANT
);
538 if ( s
.length() != 1 )
540 // it is really unknown but we don't have any way to return it from
542 return wxFONTSTYLE_NORMAL
;
548 // again, unknown but consider normal by default
551 return wxFONTSTYLE_NORMAL
;
554 return wxFONTSTYLE_ITALIC
;
557 return wxFONTSTYLE_SLANT
;
561 wxFontWeight
wxNativeFontInfo::GetWeight() const
563 const wxString s
= GetXFontComponent(wxXLFD_WEIGHT
).MakeLower();
564 if ( s
.find(_T("bold")) != wxString::npos
|| s
== _T("black") )
565 return wxFONTWEIGHT_BOLD
;
566 else if ( s
== _T("light") )
567 return wxFONTWEIGHT_LIGHT
;
569 return wxFONTWEIGHT_NORMAL
;
572 bool wxNativeFontInfo::GetUnderlined() const
574 // X fonts are never underlined
578 wxString
wxNativeFontInfo::GetFaceName() const
580 // wxWidgets facename probably more accurately corresponds to X family
581 return GetXFontComponent(wxXLFD_FAMILY
);
584 wxFontFamily
wxNativeFontInfo::GetFamily() const
586 // and wxWidgets family -- to X foundry, but we have to translate it to
587 // wxFontFamily somehow...
588 wxFAIL_MSG(_T("not implemented")); // GetXFontComponent(wxXLFD_FOUNDRY);
590 return wxFONTFAMILY_DEFAULT
;
593 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
595 // we already have the code for this but need to refactor it first
596 wxFAIL_MSG( _T("not implemented") );
598 return wxFONTENCODING_MAX
;
601 void wxNativeFontInfo::SetPointSize(int pointsize
)
603 SetXFontComponent(wxXLFD_POINTSIZE
, wxString::Format(_T("%d"), pointsize
));
606 void wxNativeFontInfo::SetStyle(wxFontStyle style
)
611 case wxFONTSTYLE_ITALIC
:
615 case wxFONTSTYLE_SLANT
:
619 case wxFONTSTYLE_NORMAL
:
623 wxFAIL_MSG( _T("unknown wxFontStyle in wxNativeFontInfo::SetStyle") );
627 SetXFontComponent(wxXLFD_SLANT
, s
);
630 void wxNativeFontInfo::SetWeight(wxFontWeight weight
)
635 case wxFONTWEIGHT_BOLD
:
639 case wxFONTWEIGHT_LIGHT
:
643 case wxFONTWEIGHT_NORMAL
:
648 wxFAIL_MSG( _T("unknown wxFontWeight in wxNativeFontInfo::SetWeight") );
652 SetXFontComponent(wxXLFD_WEIGHT
, s
);
655 void wxNativeFontInfo::SetUnderlined(bool WXUNUSED(underlined
))
657 // can't do this under X
660 void wxNativeFontInfo::SetFaceName(wxString facename
)
662 SetXFontComponent(wxXLFD_FAMILY
, facename
);
665 void wxNativeFontInfo::SetFamily(wxFontFamily family
)
667 // wxFontFamily -> X foundry, anyone?
668 wxFAIL_MSG( _T("not implemented") );
670 // SetXFontComponent(wxXLFD_FOUNDRY, ...);
673 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding
)
675 wxNativeEncodingInfo info
;
676 if ( wxGetNativeFontEncoding(encoding
, &info
) )
678 SetXFontComponent(wxXLFD_ENCODING
, info
.xencoding
);
679 SetXFontComponent(wxXLFD_REGISTRY
, info
.xregistry
);
683 // ----------------------------------------------------------------------------
685 // ----------------------------------------------------------------------------
687 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
688 wxNativeEncodingInfo
*info
)
690 wxCHECK_MSG( info
, FALSE
, _T("bad pointer in wxGetNativeFontEncoding") );
692 if ( encoding
== wxFONTENCODING_DEFAULT
)
694 encoding
= wxFont::GetDefaultEncoding();
699 case wxFONTENCODING_ISO8859_1
:
700 case wxFONTENCODING_ISO8859_2
:
701 case wxFONTENCODING_ISO8859_3
:
702 case wxFONTENCODING_ISO8859_4
:
703 case wxFONTENCODING_ISO8859_5
:
704 case wxFONTENCODING_ISO8859_6
:
705 case wxFONTENCODING_ISO8859_7
:
706 case wxFONTENCODING_ISO8859_8
:
707 case wxFONTENCODING_ISO8859_9
:
708 case wxFONTENCODING_ISO8859_10
:
709 case wxFONTENCODING_ISO8859_11
:
710 case wxFONTENCODING_ISO8859_12
:
711 case wxFONTENCODING_ISO8859_13
:
712 case wxFONTENCODING_ISO8859_14
:
713 case wxFONTENCODING_ISO8859_15
:
715 int cp
= encoding
- wxFONTENCODING_ISO8859_1
+ 1;
716 info
->xregistry
= wxT("iso8859");
717 info
->xencoding
.Printf(wxT("%d"), cp
);
721 case wxFONTENCODING_UTF8
:
722 info
->xregistry
= wxT("iso10646");
723 info
->xencoding
= wxT("*");
726 case wxFONTENCODING_GB2312
:
727 info
->xregistry
= wxT("GB2312"); // or the otherway round?
728 info
->xencoding
= wxT("*");
731 case wxFONTENCODING_KOI8
:
732 case wxFONTENCODING_KOI8_U
:
733 info
->xregistry
= wxT("koi8");
735 // we don't make distinction between koi8-r, koi8-u and koi8-ru (so far)
736 info
->xencoding
= wxT("*");
739 case wxFONTENCODING_CP1250
:
740 case wxFONTENCODING_CP1251
:
741 case wxFONTENCODING_CP1252
:
742 case wxFONTENCODING_CP1253
:
743 case wxFONTENCODING_CP1254
:
744 case wxFONTENCODING_CP1255
:
745 case wxFONTENCODING_CP1256
:
746 case wxFONTENCODING_CP1257
:
748 int cp
= encoding
- wxFONTENCODING_CP1250
+ 1250;
749 info
->xregistry
= wxT("microsoft");
750 info
->xencoding
.Printf(wxT("cp%d"), cp
);
754 case wxFONTENCODING_SYSTEM
:
756 info
->xencoding
= wxT("*");
760 // don't know how to translate this encoding into X fontspec
764 info
->encoding
= encoding
;
769 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
772 fontspec
.Printf(_T("-*-%s-*-*-*-*-*-*-*-*-*-*-%s-%s"),
773 !info
.facename
? _T("*") : info
.facename
.c_str(),
774 info
.xregistry
.c_str(),
775 info
.xencoding
.c_str());
777 return wxTestFontSpec(fontspec
);
780 // ----------------------------------------------------------------------------
781 // X-specific functions
782 // ----------------------------------------------------------------------------
784 wxNativeFont
wxLoadQueryNearestFont(int pointSize
,
789 const wxString
&facename
,
790 wxFontEncoding encoding
,
793 if ( encoding
== wxFONTENCODING_DEFAULT
)
795 encoding
= wxFont::GetDefaultEncoding();
798 // first determine the encoding - if the font doesn't exist at all in this
799 // encoding, it's useless to do all other approximations (i.e. size,
800 // family &c don't matter much)
801 wxNativeEncodingInfo info
;
802 if ( encoding
== wxFONTENCODING_SYSTEM
)
804 // This will always work so we don't test to save time
805 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
809 if ( !wxGetNativeFontEncoding(encoding
, &info
) ||
810 !wxTestFontEncoding(info
) )
813 if ( !wxFontMapper::Get()->GetAltForEncoding(encoding
, &info
) )
814 #endif // wxUSE_FONTMAP
816 // unspported encoding - replace it with the default
818 // NB: we can't just return 0 from here because wxGTK code doesn't
819 // check for it (i.e. it supposes that we'll always succeed),
820 // so it would provoke a crash
821 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
826 // OK, we have the correct xregistry/xencoding in info structure
827 wxNativeFont font
= 0;
829 // if we already have the X font name, try to use it
830 if( xFontName
&& !xFontName
->IsEmpty() )
833 // Make sure point size is correct for scale factor.
835 wxStringTokenizer
tokenizer(*xFontName
, _T("-"), wxTOKEN_RET_DELIMS
);
836 wxString newFontName
;
838 for(int i
= 0; i
< 8; i
++)
839 newFontName
+= tokenizer
.NextToken();
841 (void) tokenizer
.NextToken();
843 newFontName
+= wxString::Format(wxT("%d-"), pointSize
);
845 while(tokenizer
.HasMoreTokens())
846 newFontName
+= tokenizer
.GetNextToken();
848 font
= wxLoadFont(newFontName
);
851 *xFontName
= newFontName
;
856 // search up and down by stepsize 10
857 int max_size
= pointSize
+ 20 * (1 + (pointSize
/180));
858 int min_size
= pointSize
- 20 * (1 + (pointSize
/180));
860 int i
, round
; // counters
862 // first round: search for equal, then for smaller and for larger size with the given weight and style
863 int testweight
= weight
;
864 int teststyle
= style
;
866 for ( round
= 0; round
< 3; round
++ )
868 // second round: use normal weight
871 if ( testweight
!= wxNORMAL
)
873 testweight
= wxNORMAL
;
877 ++round
; // fall through to third round
881 // third round: ... and use normal style
884 if ( teststyle
!= wxNORMAL
)
886 teststyle
= wxNORMAL
;
893 // Search for equal or smaller size (approx.)
894 for ( i
= pointSize
; !font
&& i
>= 10 && i
>= min_size
; i
-= 10 )
896 font
= wxLoadQueryFont(i
, family
, teststyle
, testweight
, underlined
,
897 facename
, info
.xregistry
, info
.xencoding
,
901 // Search for larger size (approx.)
902 for ( i
= pointSize
+ 10; !font
&& i
<= max_size
; i
+= 10 )
904 font
= wxLoadQueryFont(i
, family
, teststyle
, testweight
, underlined
,
905 facename
, info
.xregistry
, info
.xencoding
,
910 // Try default family
911 if ( !font
&& family
!= wxDEFAULT
)
913 font
= wxLoadQueryFont(pointSize
, wxDEFAULT
, style
, weight
,
914 underlined
, facename
,
915 info
.xregistry
, info
.xencoding
,
919 // ignore size, family, style and weight but try to find font with the
920 // given facename and encoding
923 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
924 underlined
, facename
,
925 info
.xregistry
, info
.xencoding
,
928 // ignore family as well
931 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
932 underlined
, wxEmptyString
,
933 info
.xregistry
, info
.xencoding
,
936 // if it still failed, try to get the font of any size but
937 // with the requested encoding: this can happen if the
938 // encoding is only available in one size which happens to be
939 // different from 120
942 font
= wxLoadQueryFont(-1, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
943 FALSE
, wxEmptyString
,
944 info
.xregistry
, info
.xencoding
,
947 // this should never happen as we had tested for it in the
948 // very beginning, but if it does, do return something non
949 // NULL or we'd crash in wxFont code
952 wxFAIL_MSG( _T("this encoding should be available!") );
954 font
= wxLoadQueryFont(-1,
955 wxDEFAULT
, wxNORMAL
, wxNORMAL
,
956 FALSE
, wxEmptyString
,
968 // ----------------------------------------------------------------------------
970 // ----------------------------------------------------------------------------
972 // returns TRUE if there are any fonts matching this font spec
973 static bool wxTestFontSpec(const wxString
& fontspec
)
975 // some X servers will fail to load this font because there are too many
976 // matches so we must test explicitly for this
977 if ( fontspec
== _T("-*-*-*-*-*-*-*-*-*-*-*-*-*-*") )
982 wxNativeFont test
= (wxNativeFont
) g_fontHash
->Get( fontspec
);
988 test
= wxLoadFont(fontspec
);
989 g_fontHash
->Put( fontspec
, (wxObject
*) test
);
1003 static wxNativeFont
wxLoadQueryFont(int pointSize
,
1007 bool WXUNUSED(underlined
),
1008 const wxString
& facename
,
1009 const wxString
& xregistry
,
1010 const wxString
& xencoding
,
1011 wxString
* xFontName
)
1016 case wxDECORATIVE
: xfamily
= wxT("lucida"); break;
1017 case wxROMAN
: xfamily
= wxT("times"); break;
1018 case wxMODERN
: xfamily
= wxT("courier"); break;
1019 case wxSWISS
: xfamily
= wxT("helvetica"); break;
1020 case wxTELETYPE
: xfamily
= wxT("lucidatypewriter"); break;
1021 case wxSCRIPT
: xfamily
= wxT("utopia"); break;
1022 default: xfamily
= wxT("*");
1030 xweight
= MWLF_WEIGHT_BOLD
;
1035 xweight
= MWLF_WEIGHT_LIGHT
;
1040 xweight
= MWLF_WEIGHT_NORMAL
;
1046 xweight
= MWLF_WEIGHT_DEFAULT
;
1050 GR_SCREEN_INFO screenInfo
;
1051 GrGetScreenInfo(& screenInfo
);
1053 int yPixelsPerCM
= screenInfo
.ydpcm
;
1055 // A point is 1/72 of an inch.
1056 // An inch is 2.541 cm.
1057 // So pixelHeight = (pointSize / 72) (inches) * 2.541 (for cm) * yPixelsPerCM (for pixels)
1058 // In fact pointSize is 10 * the normal point size so
1061 int pixelHeight
= (int) ( (((float)pointSize
) / 720.0) * 2.541 * (float) yPixelsPerCM
) ;
1063 // An alternative: assume that the screen is 72 dpi.
1064 //int pixelHeight = (int) (((float)pointSize / 720.0) * 72.0) ;
1065 //int pixelHeight = (int) ((float)pointSize / 10.0) ;
1068 logFont
.lfHeight
= pixelHeight
;
1069 logFont
.lfWidth
= 0;
1070 logFont
.lfEscapement
= 0;
1071 logFont
.lfOrientation
= 0;
1072 logFont
.lfWeight
= xweight
;
1073 logFont
.lfItalic
= (style
== wxNORMAL
? 0 : 1) ;
1074 logFont
.lfUnderline
= 0;
1075 logFont
.lfStrikeOut
= 0;
1076 logFont
.lfCharSet
= MWLF_CHARSET_DEFAULT
; // TODO: select appropriate one
1077 logFont
.lfOutPrecision
= MWLF_TYPE_DEFAULT
;
1078 logFont
.lfClipPrecision
= 0; // Not used
1079 logFont
.lfRoman
= (family
== wxROMAN
? 1 : 0) ;
1080 logFont
.lfSerif
= (family
== wxSWISS
? 0 : 1) ;
1081 logFont
.lfSansSerif
= !logFont
.lfSerif
;
1082 logFont
.lfModern
= (family
== wxMODERN
? 1 : 0) ;
1083 logFont
.lfProportional
= (family
== wxTELETYPE
? 0 : 1) ;
1084 logFont
.lfOblique
= 0;
1085 logFont
.lfSmallCaps
= 0;
1086 logFont
.lfPitch
= 0; // 0 = default
1087 strcpy(logFont
.lfFaceName
, facename
.c_str());
1089 XFontStruct
* fontInfo
= (XFontStruct
*) malloc(sizeof(XFontStruct
));
1090 fontInfo
->fid
= GrCreateFont((GR_CHAR
*) facename
.c_str(), pixelHeight
, & logFont
);
1091 GrGetFontInfo(fontInfo
->fid
, & fontInfo
->info
);
1092 return (wxNativeFont
) fontInfo
;
1096 if (!facename
.IsEmpty())
1098 fontSpec
.Printf(wxT("-*-%s-*-*-normal-*-*-*-*-*-*-*-*-*"),
1101 if ( wxTestFontSpec(fontSpec
) )
1105 //else: no such family, use default one instead
1112 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1114 if ( wxTestFontSpec(fontSpec
) )
1119 // fall through - try wxITALIC now
1122 fontSpec
.Printf(wxT("-*-%s-*-i-*-*-*-*-*-*-*-*-*-*"),
1124 if ( wxTestFontSpec(fontSpec
) )
1128 else if ( style
== wxITALIC
) // and not wxSLANT
1131 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1133 if ( wxTestFontSpec(fontSpec
) )
1139 // no italic, no slant - leave default
1146 wxFAIL_MSG(_T("unknown font style"));
1147 // fall back to normal
1159 fontSpec
.Printf(wxT("-*-%s-bold-*-*-*-*-*-*-*-*-*-*-*"),
1161 if ( wxTestFontSpec(fontSpec
) )
1163 xweight
= wxT("bold");
1166 fontSpec
.Printf(wxT("-*-%s-heavy-*-*-*-*-*-*-*-*-*-*-*"),
1168 if ( wxTestFontSpec(fontSpec
) )
1170 xweight
= wxT("heavy");
1173 fontSpec
.Printf(wxT("-*-%s-extrabold-*-*-*-*-*-*-*-*-*-*-*"),
1175 if ( wxTestFontSpec(fontSpec
) )
1177 xweight
= wxT("extrabold");
1180 fontSpec
.Printf(wxT("-*-%s-demibold-*-*-*-*-*-*-*-*-*-*-*"),
1182 if ( wxTestFontSpec(fontSpec
) )
1184 xweight
= wxT("demibold");
1187 fontSpec
.Printf(wxT("-*-%s-black-*-*-*-*-*-*-*-*-*-*-*"),
1189 if ( wxTestFontSpec(fontSpec
) )
1191 xweight
= wxT("black");
1194 fontSpec
.Printf(wxT("-*-%s-ultrablack-*-*-*-*-*-*-*-*-*-*-*"),
1196 if ( wxTestFontSpec(fontSpec
) )
1198 xweight
= wxT("ultrablack");
1205 fontSpec
.Printf(wxT("-*-%s-light-*-*-*-*-*-*-*-*-*-*-*"),
1207 if ( wxTestFontSpec(fontSpec
) )
1209 xweight
= wxT("light");
1212 fontSpec
.Printf(wxT("-*-%s-thin-*-*-*-*-*-*-*-*-*-*-*"),
1214 if ( wxTestFontSpec(fontSpec
) )
1216 xweight
= wxT("thin");
1223 fontSpec
.Printf(wxT("-*-%s-medium-*-*-*-*-*-*-*-*-*-*-*"),
1225 if ( wxTestFontSpec(fontSpec
) )
1227 xweight
= wxT("medium");
1230 fontSpec
.Printf(wxT("-*-%s-normal-*-*-*-*-*-*-*-*-*-*-*"),
1232 if ( wxTestFontSpec(fontSpec
) )
1234 xweight
= wxT("normal");
1237 fontSpec
.Printf(wxT("-*-%s-regular-*-*-*-*-*-*-*-*-*-*-*"),
1239 if ( wxTestFontSpec(fontSpec
) )
1241 xweight
= wxT("regular");
1247 default: xweight
= wxT("*"); break;
1250 // if pointSize is -1, don't specify any
1252 if ( pointSize
== -1 )
1258 sizeSpec
.Printf(_T("%d"), pointSize
);
1261 // construct the X font spec from our data
1262 fontSpec
.Printf(wxT("-*-%s-%s-%s-normal-*-*-%s-*-*-*-*-%s-%s"),
1263 xfamily
.c_str(), xweight
.c_str(), xstyle
.c_str(),
1264 sizeSpec
.c_str(), xregistry
.c_str(), xencoding
.c_str());
1267 *xFontName
= fontSpec
;
1269 return wxLoadFont(fontSpec
);
1274 // ----------------------------------------------------------------------------
1276 // ----------------------------------------------------------------------------
1278 class wxFontModule
: public wxModule
1285 DECLARE_DYNAMIC_CLASS(wxFontModule
)
1288 IMPLEMENT_DYNAMIC_CLASS(wxFontModule
, wxModule
)
1290 bool wxFontModule::OnInit()
1292 g_fontHash
= new wxHashTable( wxKEY_STRING
);
1297 void wxFontModule::OnExit()
1301 g_fontHash
= (wxHashTable
*)NULL
;
1304 #endif // GTK 2.0/1.x