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
;
136 bool wxNativeFontInfo::FromString(const wxString
& s
)
139 pango_font_description_free( description
);
141 description
= pango_font_description_from_string( wxGTK_CONV( s
) );
146 wxString
wxNativeFontInfo::ToString() const
148 char *str
= pango_font_description_to_string( description
);
149 wxString tmp
= wxGTK_CONV_BACK( str
);
155 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
157 return FromString( s
);
160 wxString
wxNativeFontInfo::ToUserString() const
165 // ----------------------------------------------------------------------------
166 // wxNativeEncodingInfo
167 // ----------------------------------------------------------------------------
169 bool wxNativeEncodingInfo::FromString(const wxString
& s
)
174 wxString
wxNativeEncodingInfo::ToString() const
176 return wxEmptyString
;
179 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
184 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
185 wxNativeEncodingInfo
*info
)
194 #pragma message disable nosimpint
197 #include <X11/Xlib.h>
200 #pragma message enable nosimpint
203 #include "wx/utils.h" // for wxGetDisplay()
204 #elif defined(__WXGTK__)
205 // we have to declare struct tm to avoid problems with first forward
206 // declaring it in C code (glib.h included from gdk.h does it) and then
207 // defining it when time.h is included from the headers below - this is
208 // known not to work at least with Sun CC 6.01
215 // ----------------------------------------------------------------------------
217 // ----------------------------------------------------------------------------
219 static wxHashTable
*g_fontHash
= (wxHashTable
*) NULL
;
221 // ----------------------------------------------------------------------------
223 // ----------------------------------------------------------------------------
225 // define the functions to create and destroy native fonts for this toolkit
227 wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
229 return XLoadQueryFont((Display
*)wxGetDisplay(), fontSpec
);
232 inline void wxFreeFont(wxNativeFont font
)
234 XFreeFont((Display
*)wxGetDisplay(), (XFontStruct
*)font
);
236 #elif defined(__WXGTK__)
237 wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
239 // VZ: we should use gdk_fontset_load() instead of gdk_font_load()
240 // here to be able to display Japanese fonts correctly (at least
241 // this is what people report) but unfortunately doing it results
242 // in tons of warnings when using GTK with "normal" European
243 // languages and so we can't always do it and I don't know enough
244 // to determine when should this be done... (FIXME)
245 return gdk_font_load( wxConvertWX2MB(fontSpec
) );
248 inline void wxFreeFont(wxNativeFont font
)
250 gdk_font_unref(font
);
253 #error "Unknown GUI toolkit"
256 static bool wxTestFontSpec(const wxString
& fontspec
);
258 static wxNativeFont
wxLoadQueryFont(int pointSize
,
263 const wxString
& facename
,
264 const wxString
& xregistry
,
265 const wxString
& xencoding
,
266 wxString
* xFontName
);
268 // ============================================================================
270 // ============================================================================
272 // ----------------------------------------------------------------------------
273 // wxNativeEncodingInfo
274 // ----------------------------------------------------------------------------
276 // convert to/from the string representation: format is
277 // encodingid;registry;encoding[;facename]
278 bool wxNativeEncodingInfo::FromString(const wxString
& s
)
280 // use ";", not "-" because it may be part of encoding name
281 wxStringTokenizer
tokenizer(s
, _T(";"));
283 wxString encid
= tokenizer
.GetNextToken();
285 if ( !encid
.ToLong(&enc
) )
287 encoding
= (wxFontEncoding
)enc
;
289 xregistry
= tokenizer
.GetNextToken();
293 xencoding
= tokenizer
.GetNextToken();
298 facename
= tokenizer
.GetNextToken();
303 wxString
wxNativeEncodingInfo::ToString() const
306 s
<< (long)encoding
<< _T(';') << xregistry
<< _T(';') << xencoding
;
309 s
<< _T(';') << facename
;
315 // ----------------------------------------------------------------------------
317 // ----------------------------------------------------------------------------
319 void wxNativeFontInfo::Init()
324 bool wxNativeFontInfo::FromString(const wxString
& s
)
326 wxStringTokenizer
tokenizer(s
, _T(";"));
329 wxString token
= tokenizer
.GetNextToken();
330 if ( token
!= _T('0') )
333 xFontName
= tokenizer
.GetNextToken();
335 // this should be the end
336 if ( tokenizer
.HasMoreTokens() )
339 return FromXFontName(xFontName
);
342 wxString
wxNativeFontInfo::ToString() const
345 return wxString::Format(_T("%d;%s"), 0, GetXFontName().c_str());
348 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
350 return FromXFontName(s
);
353 wxString
wxNativeFontInfo::ToUserString() const
355 return GetXFontName();
358 bool wxNativeFontInfo::HasElements() const
360 // we suppose that the foundry is never empty, so if it is it means that we
361 // had never parsed the XLFD
362 return !fontElements
[0].empty();
365 wxString
wxNativeFontInfo::GetXFontComponent(wxXLFDField field
) const
367 wxCHECK_MSG( field
< wxXLFD_MAX
, _T(""), _T("invalid XLFD field") );
369 if ( !HasElements() )
372 if ( !((wxNativeFontInfo
*)this)->FromXFontName(xFontName
) )
376 return fontElements
[field
];
379 bool wxNativeFontInfo::FromXFontName(const wxString
& fontname
)
381 // TODO: we should be able to handle the font aliases here, but how?
382 wxStringTokenizer
tokenizer(fontname
, _T("-"));
384 // skip the leading, usually empty field (font name registry)
385 if ( !tokenizer
.HasMoreTokens() )
388 (void)tokenizer
.GetNextToken();
390 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
392 if ( !tokenizer
.HasMoreTokens() )
394 // not enough elements in the XLFD - or maybe an alias
398 wxString field
= tokenizer
.GetNextToken();
399 if ( !field
.empty() && field
!= _T('*') )
401 // we're really initialized now
405 fontElements
[n
] = field
;
408 // this should be all
409 if ( tokenizer
.HasMoreTokens() )
415 wxString
wxNativeFontInfo::GetXFontName() const
417 if ( xFontName
.empty() )
419 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
421 // replace the non specified elements with '*' except for the
422 // additional style which is usually just omitted
423 wxString elt
= fontElements
[n
];
424 if ( elt
.empty() && n
!= wxXLFD_ADDSTYLE
)
430 ((wxNativeFontInfo
*)this)->xFontName
<< _T('-') << elt
;
438 wxNativeFontInfo::SetXFontComponent(wxXLFDField field
, const wxString
& value
)
440 wxCHECK_RET( field
< wxXLFD_MAX
, _T("invalid XLFD field") );
442 // this class should be initialized with a valid font spec first and only
443 // then the fields may be modified!
444 wxASSERT_MSG( !IsDefault(), _T("can't modify an uninitialized XLFD") );
446 if ( !HasElements() )
449 if ( !((wxNativeFontInfo
*)this)->FromXFontName(xFontName
) )
451 wxFAIL_MSG( _T("can't set font element for invalid XLFD") );
457 fontElements
[field
] = value
;
459 // invalidate the XFLD, it doesn't correspond to the font elements any more
463 void wxNativeFontInfo::SetXFontName(const wxString
& xFontName_
)
465 // invalidate the font elements, GetXFontComponent() will reparse the XLFD
466 fontElements
[0].clear();
468 xFontName
= xFontName_
;
473 int wxNativeFontInfo::GetPointSize() const
475 const wxString s
= GetXFontComponent(wxXLFD_POINTSIZE
);
477 // return -1 to indicate that the size is unknown
479 return s
.ToLong(&l
) ? l
: -1;
482 wxFontStyle
wxNativeFontInfo::GetStyle() const
484 const wxString s
= GetXFontComponent(wxXLFD_SLANT
);
486 if ( s
.length() != 1 )
488 // it is really unknown but we don't have any way to return it from
490 return wxFONTSTYLE_NORMAL
;
496 // again, unknown but consider normal by default
499 return wxFONTSTYLE_NORMAL
;
502 return wxFONTSTYLE_ITALIC
;
505 return wxFONTSTYLE_SLANT
;
509 wxFontWeight
wxNativeFontInfo::GetWeight() const
511 const wxString s
= GetXFontComponent(wxXLFD_WEIGHT
).MakeLower();
512 if ( s
.find(_T("bold")) != wxString::npos
|| s
== _T("black") )
513 return wxFONTWEIGHT_BOLD
;
514 else if ( s
== _T("light") )
515 return wxFONTWEIGHT_LIGHT
;
517 return wxFONTWEIGHT_NORMAL
;
520 bool wxNativeFontInfo::GetUnderlined() const
522 // X fonts are never underlined
526 wxString
wxNativeFontInfo::GetFaceName() const
528 // wxWindows facename probably more accurately corresponds to X family
529 return GetXFontComponent(wxXLFD_FAMILY
);
532 wxFontFamily
wxNativeFontInfo::GetFamily() const
534 // and wxWindows family -- to X foundry, but we have to translate it to
535 // wxFontFamily somehow...
536 wxFAIL_MSG(_T("not implemented")); // GetXFontComponent(wxXLFD_FOUNDRY);
538 return wxFONTFAMILY_DEFAULT
;
541 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
543 // we already have the code for this but need to refactor it first
544 wxFAIL_MSG( _T("not implemented") );
546 return wxFONTENCODING_MAX
;
549 void wxNativeFontInfo::SetPointSize(int pointsize
)
551 SetXFontComponent(wxXLFD_POINTSIZE
, wxString::Format(_T("%d"), pointsize
));
554 void wxNativeFontInfo::SetStyle(wxFontStyle style
)
559 case wxFONTSTYLE_ITALIC
:
563 case wxFONTSTYLE_SLANT
:
567 case wxFONTSTYLE_NORMAL
:
571 wxFAIL_MSG( _T("unknown wxFontStyle in wxNativeFontInfo::SetStyle") );
575 SetXFontComponent(wxXLFD_SLANT
, s
);
578 void wxNativeFontInfo::SetWeight(wxFontWeight weight
)
583 case wxFONTWEIGHT_BOLD
:
587 case wxFONTWEIGHT_LIGHT
:
591 case wxFONTWEIGHT_NORMAL
:
596 wxFAIL_MSG( _T("unknown wxFontWeight in wxNativeFontInfo::SetWeight") );
600 SetXFontComponent(wxXLFD_WEIGHT
, s
);
603 void wxNativeFontInfo::SetUnderlined(bool WXUNUSED(underlined
))
605 // can't do this under X
608 void wxNativeFontInfo::SetFaceName(wxString facename
)
610 SetXFontComponent(wxXLFD_FAMILY
, facename
);
613 void wxNativeFontInfo::SetFamily(wxFontFamily family
)
615 // wxFontFamily -> X foundry, anyone?
616 wxFAIL_MSG( _T("not implemented") );
618 // SetXFontComponent(wxXLFD_FOUNDRY, ...);
621 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding
)
623 wxNativeEncodingInfo info
;
624 if ( wxGetNativeFontEncoding(encoding
, &info
) )
626 SetXFontComponent(wxXLFD_ENCODING
, info
.xencoding
);
627 SetXFontComponent(wxXLFD_REGISTRY
, info
.xregistry
);
631 // ----------------------------------------------------------------------------
633 // ----------------------------------------------------------------------------
635 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
636 wxNativeEncodingInfo
*info
)
638 wxCHECK_MSG( info
, FALSE
, _T("bad pointer in wxGetNativeFontEncoding") );
640 if ( encoding
== wxFONTENCODING_DEFAULT
)
642 encoding
= wxFont::GetDefaultEncoding();
647 case wxFONTENCODING_ISO8859_1
:
648 case wxFONTENCODING_ISO8859_2
:
649 case wxFONTENCODING_ISO8859_3
:
650 case wxFONTENCODING_ISO8859_4
:
651 case wxFONTENCODING_ISO8859_5
:
652 case wxFONTENCODING_ISO8859_6
:
653 case wxFONTENCODING_ISO8859_7
:
654 case wxFONTENCODING_ISO8859_8
:
655 case wxFONTENCODING_ISO8859_9
:
656 case wxFONTENCODING_ISO8859_10
:
657 case wxFONTENCODING_ISO8859_11
:
658 case wxFONTENCODING_ISO8859_12
:
659 case wxFONTENCODING_ISO8859_13
:
660 case wxFONTENCODING_ISO8859_14
:
661 case wxFONTENCODING_ISO8859_15
:
663 int cp
= encoding
- wxFONTENCODING_ISO8859_1
+ 1;
664 info
->xregistry
= wxT("iso8859");
665 info
->xencoding
.Printf(wxT("%d"), cp
);
669 case wxFONTENCODING_UTF8
:
670 info
->xregistry
= wxT("iso10646");
671 info
->xencoding
= wxT("*");
674 case wxFONTENCODING_GB2312
:
675 info
->xregistry
= wxT("GB2312"); // or the otherway round?
676 info
->xencoding
= wxT("*");
679 case wxFONTENCODING_KOI8
:
680 info
->xregistry
= wxT("koi8");
682 // we don't make distinction between koi8-r, koi8-u and koi8-ru (so far)
683 info
->xencoding
= wxT("*");
686 case wxFONTENCODING_CP1250
:
687 case wxFONTENCODING_CP1251
:
688 case wxFONTENCODING_CP1252
:
689 case wxFONTENCODING_CP1253
:
690 case wxFONTENCODING_CP1254
:
691 case wxFONTENCODING_CP1255
:
692 case wxFONTENCODING_CP1256
:
693 case wxFONTENCODING_CP1257
:
695 int cp
= encoding
- wxFONTENCODING_CP1250
+ 1250;
696 info
->xregistry
= wxT("microsoft");
697 info
->xencoding
.Printf(wxT("cp%d"), cp
);
701 case wxFONTENCODING_SYSTEM
:
703 info
->xencoding
= wxT("*");
707 // don't know how to translate this encoding into X fontspec
711 info
->encoding
= encoding
;
716 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
719 fontspec
.Printf(_T("-*-%s-*-*-*-*-*-*-*-*-*-*-%s-%s"),
720 !info
.facename
? _T("*") : info
.facename
.c_str(),
721 info
.xregistry
.c_str(),
722 info
.xencoding
.c_str());
724 return wxTestFontSpec(fontspec
);
727 // ----------------------------------------------------------------------------
728 // X-specific functions
729 // ----------------------------------------------------------------------------
731 wxNativeFont
wxLoadQueryNearestFont(int pointSize
,
736 const wxString
&facename
,
737 wxFontEncoding encoding
,
740 if ( encoding
== wxFONTENCODING_DEFAULT
)
742 encoding
= wxFont::GetDefaultEncoding();
745 // first determine the encoding - if the font doesn't exist at all in this
746 // encoding, it's useless to do all other approximations (i.e. size,
747 // family &c don't matter much)
748 wxNativeEncodingInfo info
;
749 if ( encoding
== wxFONTENCODING_SYSTEM
)
751 // This will always work so we don't test to save time
752 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
756 if ( !wxGetNativeFontEncoding(encoding
, &info
) ||
757 !wxTestFontEncoding(info
) )
760 if ( !wxFontMapper::Get()->GetAltForEncoding(encoding
, &info
) )
761 #endif // wxUSE_FONTMAP
763 // unspported encoding - replace it with the default
765 // NB: we can't just return 0 from here because wxGTK code doesn't
766 // check for it (i.e. it supposes that we'll always succeed),
767 // so it would provoke a crash
768 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
773 // OK, we have the correct xregistry/xencoding in info structure
774 wxNativeFont font
= 0;
776 // if we already have the X font name, try to use it
777 if( xFontName
&& !xFontName
->IsEmpty() )
780 // Make sure point size is correct for scale factor.
782 wxStringTokenizer
tokenizer(*xFontName
, _T("-"), wxTOKEN_RET_DELIMS
);
783 wxString newFontName
;
785 for(int i
= 0; i
< 8; i
++)
786 newFontName
+= tokenizer
.NextToken();
788 (void) tokenizer
.NextToken();
790 newFontName
+= wxString::Format(wxT("%d-"), pointSize
);
792 while(tokenizer
.HasMoreTokens())
793 newFontName
+= tokenizer
.GetNextToken();
795 font
= wxLoadFont(newFontName
);
798 *xFontName
= newFontName
;
803 // search up and down by stepsize 10
804 int max_size
= pointSize
+ 20 * (1 + (pointSize
/180));
805 int min_size
= pointSize
- 20 * (1 + (pointSize
/180));
807 int i
, round
; // counters
809 // first round: search for equal, then for smaller and for larger size with the given weight and style
810 int testweight
= weight
;
811 int teststyle
= style
;
813 for ( round
= 0; round
< 3; round
++ )
815 // second round: use normal weight
818 if ( testweight
!= wxNORMAL
)
820 testweight
= wxNORMAL
;
824 ++round
; // fall through to third round
828 // third round: ... and use normal style
831 if ( teststyle
!= wxNORMAL
)
833 teststyle
= wxNORMAL
;
840 // Search for equal or smaller size (approx.)
841 for ( i
= pointSize
; !font
&& i
>= 10 && i
>= min_size
; i
-= 10 )
843 font
= wxLoadQueryFont(i
, family
, teststyle
, testweight
, underlined
,
844 facename
, info
.xregistry
, info
.xencoding
,
848 // Search for larger size (approx.)
849 for ( i
= pointSize
+ 10; !font
&& i
<= max_size
; i
+= 10 )
851 font
= wxLoadQueryFont(i
, family
, teststyle
, testweight
, underlined
,
852 facename
, info
.xregistry
, info
.xencoding
,
857 // Try default family
858 if ( !font
&& family
!= wxDEFAULT
)
860 font
= wxLoadQueryFont(pointSize
, wxDEFAULT
, style
, weight
,
861 underlined
, facename
,
862 info
.xregistry
, info
.xencoding
,
866 // ignore size, family, style and weight but try to find font with the
867 // given facename and encoding
870 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
871 underlined
, facename
,
872 info
.xregistry
, info
.xencoding
,
875 // ignore family as well
878 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
879 underlined
, wxEmptyString
,
880 info
.xregistry
, info
.xencoding
,
883 // if it still failed, try to get the font of any size but
884 // with the requested encoding: this can happen if the
885 // encoding is only available in one size which happens to be
886 // different from 120
889 font
= wxLoadQueryFont(-1, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
890 FALSE
, wxEmptyString
,
891 info
.xregistry
, info
.xencoding
,
894 // this should never happen as we had tested for it in the
895 // very beginning, but if it does, do return something non
896 // NULL or we'd crash in wxFont code
899 wxFAIL_MSG( _T("this encoding should be available!") );
901 font
= wxLoadQueryFont(-1,
902 wxDEFAULT
, wxNORMAL
, wxNORMAL
,
903 FALSE
, wxEmptyString
,
915 // ----------------------------------------------------------------------------
917 // ----------------------------------------------------------------------------
919 // returns TRUE if there are any fonts matching this font spec
920 static bool wxTestFontSpec(const wxString
& fontspec
)
922 // some X servers will fail to load this font because there are too many
923 // matches so we must test explicitly for this
924 if ( fontspec
== _T("-*-*-*-*-*-*-*-*-*-*-*-*-*-*") )
929 wxNativeFont test
= (wxNativeFont
) g_fontHash
->Get( fontspec
);
935 test
= wxLoadFont(fontspec
);
936 g_fontHash
->Put( fontspec
, (wxObject
*) test
);
950 static wxNativeFont
wxLoadQueryFont(int pointSize
,
954 bool WXUNUSED(underlined
),
955 const wxString
& facename
,
956 const wxString
& xregistry
,
957 const wxString
& xencoding
,
963 case wxDECORATIVE
: xfamily
= wxT("lucida"); break;
964 case wxROMAN
: xfamily
= wxT("times"); break;
965 case wxMODERN
: xfamily
= wxT("courier"); break;
966 case wxSWISS
: xfamily
= wxT("helvetica"); break;
967 case wxTELETYPE
: xfamily
= wxT("lucidatypewriter"); break;
968 case wxSCRIPT
: xfamily
= wxT("utopia"); break;
969 default: xfamily
= wxT("*");
977 xweight
= MWLF_WEIGHT_BOLD
;
982 xweight
= MWLF_WEIGHT_LIGHT
;
987 xweight
= MWLF_WEIGHT_NORMAL
;
993 xweight
= MWLF_WEIGHT_DEFAULT
;
997 GR_SCREEN_INFO screenInfo
;
998 GrGetScreenInfo(& screenInfo
);
1000 int yPixelsPerCM
= screenInfo
.ydpcm
;
1002 // A point is 1/72 of an inch.
1003 // An inch is 2.541 cm.
1004 // So pixelHeight = (pointSize / 72) (inches) * 2.541 (for cm) * yPixelsPerCM (for pixels)
1005 // In fact pointSize is 10 * the normal point size so
1008 int pixelHeight
= (int) ( (((float)pointSize
) / 720.0) * 2.541 * (float) yPixelsPerCM
) ;
1010 // An alternative: assume that the screen is 72 dpi.
1011 //int pixelHeight = (int) (((float)pointSize / 720.0) * 72.0) ;
1012 //int pixelHeight = (int) ((float)pointSize / 10.0) ;
1015 logFont
.lfHeight
= pixelHeight
;
1016 logFont
.lfWidth
= 0;
1017 logFont
.lfEscapement
= 0;
1018 logFont
.lfOrientation
= 0;
1019 logFont
.lfWeight
= xweight
;
1020 logFont
.lfItalic
= (style
== wxNORMAL
? 0 : 1) ;
1021 logFont
.lfUnderline
= 0;
1022 logFont
.lfStrikeOut
= 0;
1023 logFont
.lfCharSet
= MWLF_CHARSET_DEFAULT
; // TODO: select appropriate one
1024 logFont
.lfOutPrecision
= MWLF_TYPE_DEFAULT
;
1025 logFont
.lfClipPrecision
= 0; // Not used
1026 logFont
.lfRoman
= (family
== wxROMAN
? 1 : 0) ;
1027 logFont
.lfSerif
= (family
== wxSWISS
? 0 : 1) ;
1028 logFont
.lfSansSerif
= !logFont
.lfSerif
;
1029 logFont
.lfModern
= (family
== wxMODERN
? 1 : 0) ;
1030 logFont
.lfProportional
= (family
== wxTELETYPE
? 0 : 1) ;
1031 logFont
.lfOblique
= 0;
1032 logFont
.lfSmallCaps
= 0;
1033 logFont
.lfPitch
= 0; // 0 = default
1034 strcpy(logFont
.lfFaceName
, facename
.c_str());
1036 XFontStruct
* fontInfo
= (XFontStruct
*) malloc(sizeof(XFontStruct
));
1037 fontInfo
->fid
= GrCreateFont((GR_CHAR
*) facename
.c_str(), pixelHeight
, & logFont
);
1038 GrGetFontInfo(fontInfo
->fid
, & fontInfo
->info
);
1039 return (wxNativeFont
) fontInfo
;
1043 if (!facename
.IsEmpty())
1045 fontSpec
.Printf(wxT("-*-%s-*-*-normal-*-*-*-*-*-*-*-*-*"),
1048 if ( wxTestFontSpec(fontSpec
) )
1052 //else: no such family, use default one instead
1059 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1061 if ( wxTestFontSpec(fontSpec
) )
1066 // fall through - try wxITALIC now
1069 fontSpec
.Printf(wxT("-*-%s-*-i-*-*-*-*-*-*-*-*-*-*"),
1071 if ( wxTestFontSpec(fontSpec
) )
1075 else if ( style
== wxITALIC
) // and not wxSLANT
1078 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1080 if ( wxTestFontSpec(fontSpec
) )
1086 // no italic, no slant - leave default
1093 wxFAIL_MSG(_T("unknown font style"));
1094 // fall back to normal
1106 fontSpec
.Printf(wxT("-*-%s-bold-*-*-*-*-*-*-*-*-*-*-*"),
1108 if ( wxTestFontSpec(fontSpec
) )
1110 xweight
= wxT("bold");
1113 fontSpec
.Printf(wxT("-*-%s-heavy-*-*-*-*-*-*-*-*-*-*-*"),
1115 if ( wxTestFontSpec(fontSpec
) )
1117 xweight
= wxT("heavy");
1120 fontSpec
.Printf(wxT("-*-%s-extrabold-*-*-*-*-*-*-*-*-*-*-*"),
1122 if ( wxTestFontSpec(fontSpec
) )
1124 xweight
= wxT("extrabold");
1127 fontSpec
.Printf(wxT("-*-%s-demibold-*-*-*-*-*-*-*-*-*-*-*"),
1129 if ( wxTestFontSpec(fontSpec
) )
1131 xweight
= wxT("demibold");
1134 fontSpec
.Printf(wxT("-*-%s-black-*-*-*-*-*-*-*-*-*-*-*"),
1136 if ( wxTestFontSpec(fontSpec
) )
1138 xweight
= wxT("black");
1141 fontSpec
.Printf(wxT("-*-%s-ultrablack-*-*-*-*-*-*-*-*-*-*-*"),
1143 if ( wxTestFontSpec(fontSpec
) )
1145 xweight
= wxT("ultrablack");
1152 fontSpec
.Printf(wxT("-*-%s-light-*-*-*-*-*-*-*-*-*-*-*"),
1154 if ( wxTestFontSpec(fontSpec
) )
1156 xweight
= wxT("light");
1159 fontSpec
.Printf(wxT("-*-%s-thin-*-*-*-*-*-*-*-*-*-*-*"),
1161 if ( wxTestFontSpec(fontSpec
) )
1163 xweight
= wxT("thin");
1170 fontSpec
.Printf(wxT("-*-%s-medium-*-*-*-*-*-*-*-*-*-*-*"),
1172 if ( wxTestFontSpec(fontSpec
) )
1174 xweight
= wxT("medium");
1177 fontSpec
.Printf(wxT("-*-%s-normal-*-*-*-*-*-*-*-*-*-*-*"),
1179 if ( wxTestFontSpec(fontSpec
) )
1181 xweight
= wxT("normal");
1184 fontSpec
.Printf(wxT("-*-%s-regular-*-*-*-*-*-*-*-*-*-*-*"),
1186 if ( wxTestFontSpec(fontSpec
) )
1188 xweight
= wxT("regular");
1194 default: xweight
= wxT("*"); break;
1197 // if pointSize is -1, don't specify any
1199 if ( pointSize
== -1 )
1205 sizeSpec
.Printf(_T("%d"), pointSize
);
1208 // construct the X font spec from our data
1209 fontSpec
.Printf(wxT("-*-%s-%s-%s-normal-*-*-%s-*-*-*-*-%s-%s"),
1210 xfamily
.c_str(), xweight
.c_str(), xstyle
.c_str(),
1211 sizeSpec
.c_str(), xregistry
.c_str(), xencoding
.c_str());
1214 *xFontName
= fontSpec
;
1216 return wxLoadFont(fontSpec
);
1221 // ----------------------------------------------------------------------------
1223 // ----------------------------------------------------------------------------
1225 class wxFontModule
: public wxModule
1232 DECLARE_DYNAMIC_CLASS(wxFontModule
)
1235 IMPLEMENT_DYNAMIC_CLASS(wxFontModule
, wxModule
)
1237 bool wxFontModule::OnInit()
1239 g_fontHash
= new wxHashTable( wxKEY_STRING
);
1244 void wxFontModule::OnExit()
1248 g_fontHash
= (wxHashTable
*)NULL
;
1251 #endif // GTK 2.0/1.x