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
)
247 #pragma message disable nosimpint
250 #include <X11/Xlib.h>
253 #pragma message enable nosimpint
256 #include "wx/utils.h" // for wxGetDisplay()
257 #elif defined(__WXGTK__)
258 // we have to declare struct tm to avoid problems with first forward
259 // declaring it in C code (glib.h included from gdk.h does it) and then
260 // defining it when time.h is included from the headers below - this is
261 // known not to work at least with Sun CC 6.01
268 // ----------------------------------------------------------------------------
270 // ----------------------------------------------------------------------------
272 static wxHashTable
*g_fontHash
= (wxHashTable
*) NULL
;
274 // ----------------------------------------------------------------------------
276 // ----------------------------------------------------------------------------
278 // define the functions to create and destroy native fonts for this toolkit
280 wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
282 return XLoadQueryFont((Display
*)wxGetDisplay(), fontSpec
);
285 inline void wxFreeFont(wxNativeFont font
)
287 XFreeFont((Display
*)wxGetDisplay(), (XFontStruct
*)font
);
289 #elif defined(__WXGTK__)
290 wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
292 // VZ: we should use gdk_fontset_load() instead of gdk_font_load()
293 // here to be able to display Japanese fonts correctly (at least
294 // this is what people report) but unfortunately doing it results
295 // in tons of warnings when using GTK with "normal" European
296 // languages and so we can't always do it and I don't know enough
297 // to determine when should this be done... (FIXME)
298 return gdk_font_load( wxConvertWX2MB(fontSpec
) );
301 inline void wxFreeFont(wxNativeFont font
)
303 gdk_font_unref(font
);
306 #error "Unknown GUI toolkit"
309 static bool wxTestFontSpec(const wxString
& fontspec
);
311 static wxNativeFont
wxLoadQueryFont(int pointSize
,
316 const wxString
& facename
,
317 const wxString
& xregistry
,
318 const wxString
& xencoding
,
319 wxString
* xFontName
);
321 // ============================================================================
323 // ============================================================================
325 // ----------------------------------------------------------------------------
326 // wxNativeEncodingInfo
327 // ----------------------------------------------------------------------------
329 // convert to/from the string representation: format is
330 // encodingid;registry;encoding[;facename]
331 bool wxNativeEncodingInfo::FromString(const wxString
& s
)
333 // use ";", not "-" because it may be part of encoding name
334 wxStringTokenizer
tokenizer(s
, _T(";"));
336 wxString encid
= tokenizer
.GetNextToken();
338 if ( !encid
.ToLong(&enc
) )
340 encoding
= (wxFontEncoding
)enc
;
342 xregistry
= tokenizer
.GetNextToken();
346 xencoding
= tokenizer
.GetNextToken();
351 facename
= tokenizer
.GetNextToken();
356 wxString
wxNativeEncodingInfo::ToString() const
359 s
<< (long)encoding
<< _T(';') << xregistry
<< _T(';') << xencoding
;
362 s
<< _T(';') << facename
;
368 // ----------------------------------------------------------------------------
370 // ----------------------------------------------------------------------------
372 void wxNativeFontInfo::Init()
377 bool wxNativeFontInfo::FromString(const wxString
& s
)
379 wxStringTokenizer
tokenizer(s
, _T(";"));
382 wxString token
= tokenizer
.GetNextToken();
383 if ( token
!= _T('0') )
386 xFontName
= tokenizer
.GetNextToken();
388 // this should be the end
389 if ( tokenizer
.HasMoreTokens() )
392 return FromXFontName(xFontName
);
395 wxString
wxNativeFontInfo::ToString() const
398 return wxString::Format(_T("%d;%s"), 0, GetXFontName().c_str());
401 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
403 return FromXFontName(s
);
406 wxString
wxNativeFontInfo::ToUserString() const
408 return GetXFontName();
411 bool wxNativeFontInfo::HasElements() const
413 // we suppose that the foundry is never empty, so if it is it means that we
414 // had never parsed the XLFD
415 return !fontElements
[0].empty();
418 wxString
wxNativeFontInfo::GetXFontComponent(wxXLFDField field
) const
420 wxCHECK_MSG( field
< wxXLFD_MAX
, _T(""), _T("invalid XLFD field") );
422 if ( !HasElements() )
425 if ( !((wxNativeFontInfo
*)this)->FromXFontName(xFontName
) )
429 return fontElements
[field
];
432 bool wxNativeFontInfo::FromXFontName(const wxString
& fontname
)
434 // TODO: we should be able to handle the font aliases here, but how?
435 wxStringTokenizer
tokenizer(fontname
, _T("-"));
437 // skip the leading, usually empty field (font name registry)
438 if ( !tokenizer
.HasMoreTokens() )
441 (void)tokenizer
.GetNextToken();
443 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
445 if ( !tokenizer
.HasMoreTokens() )
447 // not enough elements in the XLFD - or maybe an alias
451 wxString field
= tokenizer
.GetNextToken();
452 if ( !field
.empty() && field
!= _T('*') )
454 // we're really initialized now
458 fontElements
[n
] = field
;
461 // this should be all
462 if ( tokenizer
.HasMoreTokens() )
468 wxString
wxNativeFontInfo::GetXFontName() const
470 if ( xFontName
.empty() )
472 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
474 // replace the non specified elements with '*' except for the
475 // additional style which is usually just omitted
476 wxString elt
= fontElements
[n
];
477 if ( elt
.empty() && n
!= wxXLFD_ADDSTYLE
)
483 ((wxNativeFontInfo
*)this)->xFontName
<< _T('-') << elt
;
491 wxNativeFontInfo::SetXFontComponent(wxXLFDField field
, const wxString
& value
)
493 wxCHECK_RET( field
< wxXLFD_MAX
, _T("invalid XLFD field") );
495 // this class should be initialized with a valid font spec first and only
496 // then the fields may be modified!
497 wxASSERT_MSG( !IsDefault(), _T("can't modify an uninitialized XLFD") );
499 if ( !HasElements() )
502 if ( !((wxNativeFontInfo
*)this)->FromXFontName(xFontName
) )
504 wxFAIL_MSG( _T("can't set font element for invalid XLFD") );
510 fontElements
[field
] = value
;
512 // invalidate the XFLD, it doesn't correspond to the font elements any more
516 void wxNativeFontInfo::SetXFontName(const wxString
& xFontName_
)
518 // invalidate the font elements, GetXFontComponent() will reparse the XLFD
519 fontElements
[0].clear();
521 xFontName
= xFontName_
;
526 int wxNativeFontInfo::GetPointSize() const
528 const wxString s
= GetXFontComponent(wxXLFD_POINTSIZE
);
530 // return -1 to indicate that the size is unknown
532 return s
.ToLong(&l
) ? l
: -1;
535 wxFontStyle
wxNativeFontInfo::GetStyle() const
537 const wxString s
= GetXFontComponent(wxXLFD_SLANT
);
539 if ( s
.length() != 1 )
541 // it is really unknown but we don't have any way to return it from
543 return wxFONTSTYLE_NORMAL
;
549 // again, unknown but consider normal by default
552 return wxFONTSTYLE_NORMAL
;
555 return wxFONTSTYLE_ITALIC
;
558 return wxFONTSTYLE_SLANT
;
562 wxFontWeight
wxNativeFontInfo::GetWeight() const
564 const wxString s
= GetXFontComponent(wxXLFD_WEIGHT
).MakeLower();
565 if ( s
.find(_T("bold")) != wxString::npos
|| s
== _T("black") )
566 return wxFONTWEIGHT_BOLD
;
567 else if ( s
== _T("light") )
568 return wxFONTWEIGHT_LIGHT
;
570 return wxFONTWEIGHT_NORMAL
;
573 bool wxNativeFontInfo::GetUnderlined() const
575 // X fonts are never underlined
579 wxString
wxNativeFontInfo::GetFaceName() const
581 // wxWidgets facename probably more accurately corresponds to X family
582 return GetXFontComponent(wxXLFD_FAMILY
);
585 wxFontFamily
wxNativeFontInfo::GetFamily() const
587 // and wxWidgets family -- to X foundry, but we have to translate it to
588 // wxFontFamily somehow...
589 wxFAIL_MSG(_T("not implemented")); // GetXFontComponent(wxXLFD_FOUNDRY);
591 return wxFONTFAMILY_DEFAULT
;
594 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
596 // we already have the code for this but need to refactor it first
597 wxFAIL_MSG( _T("not implemented") );
599 return wxFONTENCODING_MAX
;
602 void wxNativeFontInfo::SetPointSize(int pointsize
)
604 SetXFontComponent(wxXLFD_POINTSIZE
, wxString::Format(_T("%d"), pointsize
));
607 void wxNativeFontInfo::SetStyle(wxFontStyle style
)
612 case wxFONTSTYLE_ITALIC
:
616 case wxFONTSTYLE_SLANT
:
620 case wxFONTSTYLE_NORMAL
:
624 wxFAIL_MSG( _T("unknown wxFontStyle in wxNativeFontInfo::SetStyle") );
628 SetXFontComponent(wxXLFD_SLANT
, s
);
631 void wxNativeFontInfo::SetWeight(wxFontWeight weight
)
636 case wxFONTWEIGHT_BOLD
:
640 case wxFONTWEIGHT_LIGHT
:
644 case wxFONTWEIGHT_NORMAL
:
649 wxFAIL_MSG( _T("unknown wxFontWeight in wxNativeFontInfo::SetWeight") );
653 SetXFontComponent(wxXLFD_WEIGHT
, s
);
656 void wxNativeFontInfo::SetUnderlined(bool WXUNUSED(underlined
))
658 // can't do this under X
661 void wxNativeFontInfo::SetFaceName(wxString facename
)
663 SetXFontComponent(wxXLFD_FAMILY
, facename
);
666 void wxNativeFontInfo::SetFamily(wxFontFamily family
)
668 // wxFontFamily -> X foundry, anyone?
669 wxFAIL_MSG( _T("not implemented") );
671 // SetXFontComponent(wxXLFD_FOUNDRY, ...);
674 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding
)
676 wxNativeEncodingInfo info
;
677 if ( wxGetNativeFontEncoding(encoding
, &info
) )
679 SetXFontComponent(wxXLFD_ENCODING
, info
.xencoding
);
680 SetXFontComponent(wxXLFD_REGISTRY
, info
.xregistry
);
684 // ----------------------------------------------------------------------------
686 // ----------------------------------------------------------------------------
688 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
689 wxNativeEncodingInfo
*info
)
691 wxCHECK_MSG( info
, FALSE
, _T("bad pointer in wxGetNativeFontEncoding") );
693 if ( encoding
== wxFONTENCODING_DEFAULT
)
695 encoding
= wxFont::GetDefaultEncoding();
700 case wxFONTENCODING_ISO8859_1
:
701 case wxFONTENCODING_ISO8859_2
:
702 case wxFONTENCODING_ISO8859_3
:
703 case wxFONTENCODING_ISO8859_4
:
704 case wxFONTENCODING_ISO8859_5
:
705 case wxFONTENCODING_ISO8859_6
:
706 case wxFONTENCODING_ISO8859_7
:
707 case wxFONTENCODING_ISO8859_8
:
708 case wxFONTENCODING_ISO8859_9
:
709 case wxFONTENCODING_ISO8859_10
:
710 case wxFONTENCODING_ISO8859_11
:
711 case wxFONTENCODING_ISO8859_12
:
712 case wxFONTENCODING_ISO8859_13
:
713 case wxFONTENCODING_ISO8859_14
:
714 case wxFONTENCODING_ISO8859_15
:
716 int cp
= encoding
- wxFONTENCODING_ISO8859_1
+ 1;
717 info
->xregistry
= wxT("iso8859");
718 info
->xencoding
.Printf(wxT("%d"), cp
);
722 case wxFONTENCODING_UTF8
:
723 info
->xregistry
= wxT("iso10646");
724 info
->xencoding
= wxT("*");
727 case wxFONTENCODING_GB2312
:
728 info
->xregistry
= wxT("GB2312"); // or the otherway round?
729 info
->xencoding
= wxT("*");
732 case wxFONTENCODING_KOI8
:
733 case wxFONTENCODING_KOI8_U
:
734 info
->xregistry
= wxT("koi8");
736 // we don't make distinction between koi8-r, koi8-u and koi8-ru (so far)
737 info
->xencoding
= wxT("*");
740 case wxFONTENCODING_CP1250
:
741 case wxFONTENCODING_CP1251
:
742 case wxFONTENCODING_CP1252
:
743 case wxFONTENCODING_CP1253
:
744 case wxFONTENCODING_CP1254
:
745 case wxFONTENCODING_CP1255
:
746 case wxFONTENCODING_CP1256
:
747 case wxFONTENCODING_CP1257
:
749 int cp
= encoding
- wxFONTENCODING_CP1250
+ 1250;
750 info
->xregistry
= wxT("microsoft");
751 info
->xencoding
.Printf(wxT("cp%d"), cp
);
755 case wxFONTENCODING_SYSTEM
:
757 info
->xencoding
= wxT("*");
761 // don't know how to translate this encoding into X fontspec
765 info
->encoding
= encoding
;
770 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
773 fontspec
.Printf(_T("-*-%s-*-*-*-*-*-*-*-*-*-*-%s-%s"),
774 !info
.facename
? _T("*") : info
.facename
.c_str(),
775 info
.xregistry
.c_str(),
776 info
.xencoding
.c_str());
778 return wxTestFontSpec(fontspec
);
781 // ----------------------------------------------------------------------------
782 // X-specific functions
783 // ----------------------------------------------------------------------------
785 wxNativeFont
wxLoadQueryNearestFont(int pointSize
,
790 const wxString
&facename
,
791 wxFontEncoding encoding
,
794 if ( encoding
== wxFONTENCODING_DEFAULT
)
796 encoding
= wxFont::GetDefaultEncoding();
799 // first determine the encoding - if the font doesn't exist at all in this
800 // encoding, it's useless to do all other approximations (i.e. size,
801 // family &c don't matter much)
802 wxNativeEncodingInfo info
;
803 if ( encoding
== wxFONTENCODING_SYSTEM
)
805 // This will always work so we don't test to save time
806 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
810 if ( !wxGetNativeFontEncoding(encoding
, &info
) ||
811 !wxTestFontEncoding(info
) )
814 if ( !wxFontMapper::Get()->GetAltForEncoding(encoding
, &info
) )
815 #endif // wxUSE_FONTMAP
817 // unspported encoding - replace it with the default
819 // NB: we can't just return 0 from here because wxGTK code doesn't
820 // check for it (i.e. it supposes that we'll always succeed),
821 // so it would provoke a crash
822 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
827 // OK, we have the correct xregistry/xencoding in info structure
828 wxNativeFont font
= 0;
830 // if we already have the X font name, try to use it
831 if( xFontName
&& !xFontName
->IsEmpty() )
834 // Make sure point size is correct for scale factor.
836 wxStringTokenizer
tokenizer(*xFontName
, _T("-"), wxTOKEN_RET_DELIMS
);
837 wxString newFontName
;
839 for(int i
= 0; i
< 8; i
++)
840 newFontName
+= tokenizer
.NextToken();
842 (void) tokenizer
.NextToken();
844 newFontName
+= wxString::Format(wxT("%d-"), pointSize
);
846 while(tokenizer
.HasMoreTokens())
847 newFontName
+= tokenizer
.GetNextToken();
849 font
= wxLoadFont(newFontName
);
852 *xFontName
= newFontName
;
857 // search up and down by stepsize 10
858 int max_size
= pointSize
+ 20 * (1 + (pointSize
/180));
859 int min_size
= pointSize
- 20 * (1 + (pointSize
/180));
861 int i
, round
; // counters
863 // first round: search for equal, then for smaller and for larger size with the given weight and style
864 int testweight
= weight
;
865 int teststyle
= style
;
867 for ( round
= 0; round
< 3; round
++ )
869 // second round: use normal weight
872 if ( testweight
!= wxNORMAL
)
874 testweight
= wxNORMAL
;
878 ++round
; // fall through to third round
882 // third round: ... and use normal style
885 if ( teststyle
!= wxNORMAL
)
887 teststyle
= wxNORMAL
;
894 // Search for equal or smaller size (approx.)
895 for ( i
= pointSize
; !font
&& i
>= 10 && i
>= min_size
; i
-= 10 )
897 font
= wxLoadQueryFont(i
, family
, teststyle
, testweight
, underlined
,
898 facename
, info
.xregistry
, info
.xencoding
,
902 // Search for larger size (approx.)
903 for ( i
= pointSize
+ 10; !font
&& i
<= max_size
; i
+= 10 )
905 font
= wxLoadQueryFont(i
, family
, teststyle
, testweight
, underlined
,
906 facename
, info
.xregistry
, info
.xencoding
,
911 // Try default family
912 if ( !font
&& family
!= wxDEFAULT
)
914 font
= wxLoadQueryFont(pointSize
, wxDEFAULT
, style
, weight
,
915 underlined
, facename
,
916 info
.xregistry
, info
.xencoding
,
920 // ignore size, family, style and weight but try to find font with the
921 // given facename and encoding
924 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
925 underlined
, facename
,
926 info
.xregistry
, info
.xencoding
,
929 // ignore family as well
932 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
933 underlined
, wxEmptyString
,
934 info
.xregistry
, info
.xencoding
,
937 // if it still failed, try to get the font of any size but
938 // with the requested encoding: this can happen if the
939 // encoding is only available in one size which happens to be
940 // different from 120
943 font
= wxLoadQueryFont(-1, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
944 FALSE
, wxEmptyString
,
945 info
.xregistry
, info
.xencoding
,
948 // this should never happen as we had tested for it in the
949 // very beginning, but if it does, do return something non
950 // NULL or we'd crash in wxFont code
953 wxFAIL_MSG( _T("this encoding should be available!") );
955 font
= wxLoadQueryFont(-1,
956 wxDEFAULT
, wxNORMAL
, wxNORMAL
,
957 FALSE
, wxEmptyString
,
969 // ----------------------------------------------------------------------------
971 // ----------------------------------------------------------------------------
973 // returns TRUE if there are any fonts matching this font spec
974 static bool wxTestFontSpec(const wxString
& fontspec
)
976 // some X servers will fail to load this font because there are too many
977 // matches so we must test explicitly for this
978 if ( fontspec
== _T("-*-*-*-*-*-*-*-*-*-*-*-*-*-*") )
983 wxNativeFont test
= (wxNativeFont
) g_fontHash
->Get( fontspec
);
989 test
= wxLoadFont(fontspec
);
990 g_fontHash
->Put( fontspec
, (wxObject
*) test
);
1004 static wxNativeFont
wxLoadQueryFont(int pointSize
,
1008 bool WXUNUSED(underlined
),
1009 const wxString
& facename
,
1010 const wxString
& xregistry
,
1011 const wxString
& xencoding
,
1012 wxString
* xFontName
)
1017 case wxDECORATIVE
: xfamily
= wxT("lucida"); break;
1018 case wxROMAN
: xfamily
= wxT("times"); break;
1019 case wxMODERN
: xfamily
= wxT("courier"); break;
1020 case wxSWISS
: xfamily
= wxT("helvetica"); break;
1021 case wxTELETYPE
: xfamily
= wxT("lucidatypewriter"); break;
1022 case wxSCRIPT
: xfamily
= wxT("utopia"); break;
1023 default: xfamily
= wxT("*");
1031 xweight
= MWLF_WEIGHT_BOLD
;
1036 xweight
= MWLF_WEIGHT_LIGHT
;
1041 xweight
= MWLF_WEIGHT_NORMAL
;
1047 xweight
= MWLF_WEIGHT_DEFAULT
;
1051 GR_SCREEN_INFO screenInfo
;
1052 GrGetScreenInfo(& screenInfo
);
1054 int yPixelsPerCM
= screenInfo
.ydpcm
;
1056 // A point is 1/72 of an inch.
1057 // An inch is 2.541 cm.
1058 // So pixelHeight = (pointSize / 72) (inches) * 2.541 (for cm) * yPixelsPerCM (for pixels)
1059 // In fact pointSize is 10 * the normal point size so
1062 int pixelHeight
= (int) ( (((float)pointSize
) / 720.0) * 2.541 * (float) yPixelsPerCM
) ;
1064 // An alternative: assume that the screen is 72 dpi.
1065 //int pixelHeight = (int) (((float)pointSize / 720.0) * 72.0) ;
1066 //int pixelHeight = (int) ((float)pointSize / 10.0) ;
1069 logFont
.lfHeight
= pixelHeight
;
1070 logFont
.lfWidth
= 0;
1071 logFont
.lfEscapement
= 0;
1072 logFont
.lfOrientation
= 0;
1073 logFont
.lfWeight
= xweight
;
1074 logFont
.lfItalic
= (style
== wxNORMAL
? 0 : 1) ;
1075 logFont
.lfUnderline
= 0;
1076 logFont
.lfStrikeOut
= 0;
1077 logFont
.lfCharSet
= MWLF_CHARSET_DEFAULT
; // TODO: select appropriate one
1078 logFont
.lfOutPrecision
= MWLF_TYPE_DEFAULT
;
1079 logFont
.lfClipPrecision
= 0; // Not used
1080 logFont
.lfRoman
= (family
== wxROMAN
? 1 : 0) ;
1081 logFont
.lfSerif
= (family
== wxSWISS
? 0 : 1) ;
1082 logFont
.lfSansSerif
= !logFont
.lfSerif
;
1083 logFont
.lfModern
= (family
== wxMODERN
? 1 : 0) ;
1084 logFont
.lfProportional
= (family
== wxTELETYPE
? 0 : 1) ;
1085 logFont
.lfOblique
= 0;
1086 logFont
.lfSmallCaps
= 0;
1087 logFont
.lfPitch
= 0; // 0 = default
1088 strcpy(logFont
.lfFaceName
, facename
.c_str());
1090 XFontStruct
* fontInfo
= (XFontStruct
*) malloc(sizeof(XFontStruct
));
1091 fontInfo
->fid
= GrCreateFont((GR_CHAR
*) facename
.c_str(), pixelHeight
, & logFont
);
1092 GrGetFontInfo(fontInfo
->fid
, & fontInfo
->info
);
1093 return (wxNativeFont
) fontInfo
;
1097 if (!facename
.IsEmpty())
1099 fontSpec
.Printf(wxT("-*-%s-*-*-normal-*-*-*-*-*-*-*-*-*"),
1102 if ( wxTestFontSpec(fontSpec
) )
1106 //else: no such family, use default one instead
1113 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1115 if ( wxTestFontSpec(fontSpec
) )
1120 // fall through - try wxITALIC now
1123 fontSpec
.Printf(wxT("-*-%s-*-i-*-*-*-*-*-*-*-*-*-*"),
1125 if ( wxTestFontSpec(fontSpec
) )
1129 else if ( style
== wxITALIC
) // and not wxSLANT
1132 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1134 if ( wxTestFontSpec(fontSpec
) )
1140 // no italic, no slant - leave default
1147 wxFAIL_MSG(_T("unknown font style"));
1148 // fall back to normal
1160 fontSpec
.Printf(wxT("-*-%s-bold-*-*-*-*-*-*-*-*-*-*-*"),
1162 if ( wxTestFontSpec(fontSpec
) )
1164 xweight
= wxT("bold");
1167 fontSpec
.Printf(wxT("-*-%s-heavy-*-*-*-*-*-*-*-*-*-*-*"),
1169 if ( wxTestFontSpec(fontSpec
) )
1171 xweight
= wxT("heavy");
1174 fontSpec
.Printf(wxT("-*-%s-extrabold-*-*-*-*-*-*-*-*-*-*-*"),
1176 if ( wxTestFontSpec(fontSpec
) )
1178 xweight
= wxT("extrabold");
1181 fontSpec
.Printf(wxT("-*-%s-demibold-*-*-*-*-*-*-*-*-*-*-*"),
1183 if ( wxTestFontSpec(fontSpec
) )
1185 xweight
= wxT("demibold");
1188 fontSpec
.Printf(wxT("-*-%s-black-*-*-*-*-*-*-*-*-*-*-*"),
1190 if ( wxTestFontSpec(fontSpec
) )
1192 xweight
= wxT("black");
1195 fontSpec
.Printf(wxT("-*-%s-ultrablack-*-*-*-*-*-*-*-*-*-*-*"),
1197 if ( wxTestFontSpec(fontSpec
) )
1199 xweight
= wxT("ultrablack");
1206 fontSpec
.Printf(wxT("-*-%s-light-*-*-*-*-*-*-*-*-*-*-*"),
1208 if ( wxTestFontSpec(fontSpec
) )
1210 xweight
= wxT("light");
1213 fontSpec
.Printf(wxT("-*-%s-thin-*-*-*-*-*-*-*-*-*-*-*"),
1215 if ( wxTestFontSpec(fontSpec
) )
1217 xweight
= wxT("thin");
1224 fontSpec
.Printf(wxT("-*-%s-medium-*-*-*-*-*-*-*-*-*-*-*"),
1226 if ( wxTestFontSpec(fontSpec
) )
1228 xweight
= wxT("medium");
1231 fontSpec
.Printf(wxT("-*-%s-normal-*-*-*-*-*-*-*-*-*-*-*"),
1233 if ( wxTestFontSpec(fontSpec
) )
1235 xweight
= wxT("normal");
1238 fontSpec
.Printf(wxT("-*-%s-regular-*-*-*-*-*-*-*-*-*-*-*"),
1240 if ( wxTestFontSpec(fontSpec
) )
1242 xweight
= wxT("regular");
1248 default: xweight
= wxT("*"); break;
1251 // if pointSize is -1, don't specify any
1253 if ( pointSize
== -1 )
1259 sizeSpec
.Printf(_T("%d"), pointSize
);
1262 // construct the X font spec from our data
1263 fontSpec
.Printf(wxT("-*-%s-%s-%s-normal-*-*-%s-*-*-*-*-%s-%s"),
1264 xfamily
.c_str(), xweight
.c_str(), xstyle
.c_str(),
1265 sizeSpec
.c_str(), xregistry
.c_str(), xencoding
.c_str());
1268 *xFontName
= fontSpec
;
1270 return wxLoadFont(fontSpec
);
1275 // ----------------------------------------------------------------------------
1277 // ----------------------------------------------------------------------------
1279 class wxFontModule
: public wxModule
1286 DECLARE_DYNAMIC_CLASS(wxFontModule
)
1289 IMPLEMENT_DYNAMIC_CLASS(wxFontModule
, wxModule
)
1291 bool wxFontModule::OnInit()
1293 g_fontHash
= new wxHashTable( wxKEY_STRING
);
1298 void wxFontModule::OnExit()
1302 g_fontHash
= (wxHashTable
*)NULL
;
1305 #endif // GTK 2.0/1.x