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 // wxWidgets facename probably more accurately corresponds to X family
567 return GetXFontComponent(wxXLFD_FAMILY
);
570 wxFontFamily
wxNativeFontInfo::GetFamily() const
572 // and wxWidgets 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 case wxFONTENCODING_KOI8_U
:
719 info
->xregistry
= wxT("koi8");
721 // we don't make distinction between koi8-r, koi8-u and koi8-ru (so far)
722 info
->xencoding
= wxT("*");
725 case wxFONTENCODING_CP1250
:
726 case wxFONTENCODING_CP1251
:
727 case wxFONTENCODING_CP1252
:
728 case wxFONTENCODING_CP1253
:
729 case wxFONTENCODING_CP1254
:
730 case wxFONTENCODING_CP1255
:
731 case wxFONTENCODING_CP1256
:
732 case wxFONTENCODING_CP1257
:
734 int cp
= encoding
- wxFONTENCODING_CP1250
+ 1250;
735 info
->xregistry
= wxT("microsoft");
736 info
->xencoding
.Printf(wxT("cp%d"), cp
);
740 case wxFONTENCODING_SYSTEM
:
742 info
->xencoding
= wxT("*");
746 // don't know how to translate this encoding into X fontspec
750 info
->encoding
= encoding
;
755 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
758 fontspec
.Printf(_T("-*-%s-*-*-*-*-*-*-*-*-*-*-%s-%s"),
759 !info
.facename
? _T("*") : info
.facename
.c_str(),
760 info
.xregistry
.c_str(),
761 info
.xencoding
.c_str());
763 return wxTestFontSpec(fontspec
);
766 // ----------------------------------------------------------------------------
767 // X-specific functions
768 // ----------------------------------------------------------------------------
770 wxNativeFont
wxLoadQueryNearestFont(int pointSize
,
775 const wxString
&facename
,
776 wxFontEncoding encoding
,
779 if ( encoding
== wxFONTENCODING_DEFAULT
)
781 encoding
= wxFont::GetDefaultEncoding();
784 // first determine the encoding - if the font doesn't exist at all in this
785 // encoding, it's useless to do all other approximations (i.e. size,
786 // family &c don't matter much)
787 wxNativeEncodingInfo info
;
788 if ( encoding
== wxFONTENCODING_SYSTEM
)
790 // This will always work so we don't test to save time
791 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
795 if ( !wxGetNativeFontEncoding(encoding
, &info
) ||
796 !wxTestFontEncoding(info
) )
799 if ( !wxFontMapper::Get()->GetAltForEncoding(encoding
, &info
) )
800 #endif // wxUSE_FONTMAP
802 // unspported encoding - replace it with the default
804 // NB: we can't just return 0 from here because wxGTK code doesn't
805 // check for it (i.e. it supposes that we'll always succeed),
806 // so it would provoke a crash
807 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
812 // OK, we have the correct xregistry/xencoding in info structure
813 wxNativeFont font
= 0;
815 // if we already have the X font name, try to use it
816 if( xFontName
&& !xFontName
->IsEmpty() )
819 // Make sure point size is correct for scale factor.
821 wxStringTokenizer
tokenizer(*xFontName
, _T("-"), wxTOKEN_RET_DELIMS
);
822 wxString newFontName
;
824 for(int i
= 0; i
< 8; i
++)
825 newFontName
+= tokenizer
.NextToken();
827 (void) tokenizer
.NextToken();
829 newFontName
+= wxString::Format(wxT("%d-"), pointSize
);
831 while(tokenizer
.HasMoreTokens())
832 newFontName
+= tokenizer
.GetNextToken();
834 font
= wxLoadFont(newFontName
);
837 *xFontName
= newFontName
;
842 // search up and down by stepsize 10
843 int max_size
= pointSize
+ 20 * (1 + (pointSize
/180));
844 int min_size
= pointSize
- 20 * (1 + (pointSize
/180));
846 int i
, round
; // counters
848 // first round: search for equal, then for smaller and for larger size with the given weight and style
849 int testweight
= weight
;
850 int teststyle
= style
;
852 for ( round
= 0; round
< 3; round
++ )
854 // second round: use normal weight
857 if ( testweight
!= wxNORMAL
)
859 testweight
= wxNORMAL
;
863 ++round
; // fall through to third round
867 // third round: ... and use normal style
870 if ( teststyle
!= wxNORMAL
)
872 teststyle
= wxNORMAL
;
879 // Search for equal or smaller size (approx.)
880 for ( i
= pointSize
; !font
&& i
>= 10 && i
>= min_size
; i
-= 10 )
882 font
= wxLoadQueryFont(i
, family
, teststyle
, testweight
, underlined
,
883 facename
, info
.xregistry
, info
.xencoding
,
887 // Search for larger size (approx.)
888 for ( i
= pointSize
+ 10; !font
&& i
<= max_size
; i
+= 10 )
890 font
= wxLoadQueryFont(i
, family
, teststyle
, testweight
, underlined
,
891 facename
, info
.xregistry
, info
.xencoding
,
896 // Try default family
897 if ( !font
&& family
!= wxDEFAULT
)
899 font
= wxLoadQueryFont(pointSize
, wxDEFAULT
, style
, weight
,
900 underlined
, facename
,
901 info
.xregistry
, info
.xencoding
,
905 // ignore size, family, style and weight but try to find font with the
906 // given facename and encoding
909 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
910 underlined
, facename
,
911 info
.xregistry
, info
.xencoding
,
914 // ignore family as well
917 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
918 underlined
, wxEmptyString
,
919 info
.xregistry
, info
.xencoding
,
922 // if it still failed, try to get the font of any size but
923 // with the requested encoding: this can happen if the
924 // encoding is only available in one size which happens to be
925 // different from 120
928 font
= wxLoadQueryFont(-1, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
929 FALSE
, wxEmptyString
,
930 info
.xregistry
, info
.xencoding
,
933 // this should never happen as we had tested for it in the
934 // very beginning, but if it does, do return something non
935 // NULL or we'd crash in wxFont code
938 wxFAIL_MSG( _T("this encoding should be available!") );
940 font
= wxLoadQueryFont(-1,
941 wxDEFAULT
, wxNORMAL
, wxNORMAL
,
942 FALSE
, wxEmptyString
,
954 // ----------------------------------------------------------------------------
956 // ----------------------------------------------------------------------------
958 // returns TRUE if there are any fonts matching this font spec
959 static bool wxTestFontSpec(const wxString
& fontspec
)
961 // some X servers will fail to load this font because there are too many
962 // matches so we must test explicitly for this
963 if ( fontspec
== _T("-*-*-*-*-*-*-*-*-*-*-*-*-*-*") )
968 wxNativeFont test
= (wxNativeFont
) g_fontHash
->Get( fontspec
);
974 test
= wxLoadFont(fontspec
);
975 g_fontHash
->Put( fontspec
, (wxObject
*) test
);
989 static wxNativeFont
wxLoadQueryFont(int pointSize
,
993 bool WXUNUSED(underlined
),
994 const wxString
& facename
,
995 const wxString
& xregistry
,
996 const wxString
& xencoding
,
1002 case wxDECORATIVE
: xfamily
= wxT("lucida"); break;
1003 case wxROMAN
: xfamily
= wxT("times"); break;
1004 case wxMODERN
: xfamily
= wxT("courier"); break;
1005 case wxSWISS
: xfamily
= wxT("helvetica"); break;
1006 case wxTELETYPE
: xfamily
= wxT("lucidatypewriter"); break;
1007 case wxSCRIPT
: xfamily
= wxT("utopia"); break;
1008 default: xfamily
= wxT("*");
1016 xweight
= MWLF_WEIGHT_BOLD
;
1021 xweight
= MWLF_WEIGHT_LIGHT
;
1026 xweight
= MWLF_WEIGHT_NORMAL
;
1032 xweight
= MWLF_WEIGHT_DEFAULT
;
1036 GR_SCREEN_INFO screenInfo
;
1037 GrGetScreenInfo(& screenInfo
);
1039 int yPixelsPerCM
= screenInfo
.ydpcm
;
1041 // A point is 1/72 of an inch.
1042 // An inch is 2.541 cm.
1043 // So pixelHeight = (pointSize / 72) (inches) * 2.541 (for cm) * yPixelsPerCM (for pixels)
1044 // In fact pointSize is 10 * the normal point size so
1047 int pixelHeight
= (int) ( (((float)pointSize
) / 720.0) * 2.541 * (float) yPixelsPerCM
) ;
1049 // An alternative: assume that the screen is 72 dpi.
1050 //int pixelHeight = (int) (((float)pointSize / 720.0) * 72.0) ;
1051 //int pixelHeight = (int) ((float)pointSize / 10.0) ;
1054 logFont
.lfHeight
= pixelHeight
;
1055 logFont
.lfWidth
= 0;
1056 logFont
.lfEscapement
= 0;
1057 logFont
.lfOrientation
= 0;
1058 logFont
.lfWeight
= xweight
;
1059 logFont
.lfItalic
= (style
== wxNORMAL
? 0 : 1) ;
1060 logFont
.lfUnderline
= 0;
1061 logFont
.lfStrikeOut
= 0;
1062 logFont
.lfCharSet
= MWLF_CHARSET_DEFAULT
; // TODO: select appropriate one
1063 logFont
.lfOutPrecision
= MWLF_TYPE_DEFAULT
;
1064 logFont
.lfClipPrecision
= 0; // Not used
1065 logFont
.lfRoman
= (family
== wxROMAN
? 1 : 0) ;
1066 logFont
.lfSerif
= (family
== wxSWISS
? 0 : 1) ;
1067 logFont
.lfSansSerif
= !logFont
.lfSerif
;
1068 logFont
.lfModern
= (family
== wxMODERN
? 1 : 0) ;
1069 logFont
.lfProportional
= (family
== wxTELETYPE
? 0 : 1) ;
1070 logFont
.lfOblique
= 0;
1071 logFont
.lfSmallCaps
= 0;
1072 logFont
.lfPitch
= 0; // 0 = default
1073 strcpy(logFont
.lfFaceName
, facename
.c_str());
1075 XFontStruct
* fontInfo
= (XFontStruct
*) malloc(sizeof(XFontStruct
));
1076 fontInfo
->fid
= GrCreateFont((GR_CHAR
*) facename
.c_str(), pixelHeight
, & logFont
);
1077 GrGetFontInfo(fontInfo
->fid
, & fontInfo
->info
);
1078 return (wxNativeFont
) fontInfo
;
1082 if (!facename
.IsEmpty())
1084 fontSpec
.Printf(wxT("-*-%s-*-*-normal-*-*-*-*-*-*-*-*-*"),
1087 if ( wxTestFontSpec(fontSpec
) )
1091 //else: no such family, use default one instead
1098 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1100 if ( wxTestFontSpec(fontSpec
) )
1105 // fall through - try wxITALIC now
1108 fontSpec
.Printf(wxT("-*-%s-*-i-*-*-*-*-*-*-*-*-*-*"),
1110 if ( wxTestFontSpec(fontSpec
) )
1114 else if ( style
== wxITALIC
) // and not wxSLANT
1117 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1119 if ( wxTestFontSpec(fontSpec
) )
1125 // no italic, no slant - leave default
1132 wxFAIL_MSG(_T("unknown font style"));
1133 // fall back to normal
1145 fontSpec
.Printf(wxT("-*-%s-bold-*-*-*-*-*-*-*-*-*-*-*"),
1147 if ( wxTestFontSpec(fontSpec
) )
1149 xweight
= wxT("bold");
1152 fontSpec
.Printf(wxT("-*-%s-heavy-*-*-*-*-*-*-*-*-*-*-*"),
1154 if ( wxTestFontSpec(fontSpec
) )
1156 xweight
= wxT("heavy");
1159 fontSpec
.Printf(wxT("-*-%s-extrabold-*-*-*-*-*-*-*-*-*-*-*"),
1161 if ( wxTestFontSpec(fontSpec
) )
1163 xweight
= wxT("extrabold");
1166 fontSpec
.Printf(wxT("-*-%s-demibold-*-*-*-*-*-*-*-*-*-*-*"),
1168 if ( wxTestFontSpec(fontSpec
) )
1170 xweight
= wxT("demibold");
1173 fontSpec
.Printf(wxT("-*-%s-black-*-*-*-*-*-*-*-*-*-*-*"),
1175 if ( wxTestFontSpec(fontSpec
) )
1177 xweight
= wxT("black");
1180 fontSpec
.Printf(wxT("-*-%s-ultrablack-*-*-*-*-*-*-*-*-*-*-*"),
1182 if ( wxTestFontSpec(fontSpec
) )
1184 xweight
= wxT("ultrablack");
1191 fontSpec
.Printf(wxT("-*-%s-light-*-*-*-*-*-*-*-*-*-*-*"),
1193 if ( wxTestFontSpec(fontSpec
) )
1195 xweight
= wxT("light");
1198 fontSpec
.Printf(wxT("-*-%s-thin-*-*-*-*-*-*-*-*-*-*-*"),
1200 if ( wxTestFontSpec(fontSpec
) )
1202 xweight
= wxT("thin");
1209 fontSpec
.Printf(wxT("-*-%s-medium-*-*-*-*-*-*-*-*-*-*-*"),
1211 if ( wxTestFontSpec(fontSpec
) )
1213 xweight
= wxT("medium");
1216 fontSpec
.Printf(wxT("-*-%s-normal-*-*-*-*-*-*-*-*-*-*-*"),
1218 if ( wxTestFontSpec(fontSpec
) )
1220 xweight
= wxT("normal");
1223 fontSpec
.Printf(wxT("-*-%s-regular-*-*-*-*-*-*-*-*-*-*-*"),
1225 if ( wxTestFontSpec(fontSpec
) )
1227 xweight
= wxT("regular");
1233 default: xweight
= wxT("*"); break;
1236 // if pointSize is -1, don't specify any
1238 if ( pointSize
== -1 )
1244 sizeSpec
.Printf(_T("%d"), pointSize
);
1247 // construct the X font spec from our data
1248 fontSpec
.Printf(wxT("-*-%s-%s-%s-normal-*-*-%s-*-*-*-*-%s-%s"),
1249 xfamily
.c_str(), xweight
.c_str(), xstyle
.c_str(),
1250 sizeSpec
.c_str(), xregistry
.c_str(), xencoding
.c_str());
1253 *xFontName
= fontSpec
;
1255 return wxLoadFont(fontSpec
);
1260 // ----------------------------------------------------------------------------
1262 // ----------------------------------------------------------------------------
1264 class wxFontModule
: public wxModule
1271 DECLARE_DYNAMIC_CLASS(wxFontModule
)
1274 IMPLEMENT_DYNAMIC_CLASS(wxFontModule
, wxModule
)
1276 bool wxFontModule::OnInit()
1278 g_fontHash
= new wxHashTable( wxKEY_STRING
);
1283 void wxFontModule::OnExit()
1287 g_fontHash
= (wxHashTable
*)NULL
;
1290 #endif // GTK 2.0/1.x