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"
34 #include "wx/fontutil.h"
35 #include "wx/fontmap.h"
36 #include "wx/tokenzr.h"
38 #include "wx/module.h"
42 #include "pango/pango.h"
45 #include "wx/gtk/private.h"
47 #include "wx/x11/private.h"
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 void wxNativeFontInfo::Init()
59 int wxNativeFontInfo::GetPointSize() const
61 return pango_font_description_get_size( description
) / PANGO_SCALE
;
64 wxFontStyle
wxNativeFontInfo::GetStyle() const
66 wxFontStyle m_style
= wxFONTSTYLE_NORMAL
;
68 switch (pango_font_description_get_style( description
))
70 case PANGO_STYLE_NORMAL
:
71 m_style
= wxFONTSTYLE_NORMAL
;
73 case PANGO_STYLE_ITALIC
:
74 m_style
= wxFONTSTYLE_ITALIC
;
76 case PANGO_STYLE_OBLIQUE
:
77 m_style
= wxFONTSTYLE_SLANT
;
84 wxFontWeight
wxNativeFontInfo::GetWeight() const
86 wxFontWeight m_weight
= wxFONTWEIGHT_NORMAL
;
88 switch (pango_font_description_get_weight( description
))
90 case PANGO_WEIGHT_ULTRALIGHT
:
91 m_weight
= wxFONTWEIGHT_LIGHT
;
93 case PANGO_WEIGHT_LIGHT
:
94 m_weight
= wxFONTWEIGHT_LIGHT
;
96 case PANGO_WEIGHT_NORMAL
:
97 m_weight
= wxFONTWEIGHT_NORMAL
;
99 case PANGO_WEIGHT_BOLD
:
100 m_weight
= wxFONTWEIGHT_BOLD
;
102 case PANGO_WEIGHT_ULTRABOLD
:
103 m_weight
= wxFONTWEIGHT_BOLD
;
105 case PANGO_WEIGHT_HEAVY
:
106 m_weight
= wxFONTWEIGHT_BOLD
;
113 bool wxNativeFontInfo::GetUnderlined() const
118 wxString
wxNativeFontInfo::GetFaceName() const
120 wxString tmp
= wxGTK_CONV_BACK( pango_font_description_get_family( description
) );
125 wxFontFamily
wxNativeFontInfo::GetFamily() const
127 return wxFONTFAMILY_SWISS
;
130 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
132 return wxFONTENCODING_SYSTEM
;
135 bool wxNativeFontInfo::FromString(const wxString
& s
)
138 pango_font_description_free( description
);
140 description
= pango_font_description_from_string( wxGTK_CONV( s
) );
145 wxString
wxNativeFontInfo::ToString() const
147 char *str
= pango_font_description_to_string( description
);
148 wxString tmp
= wxGTK_CONV_BACK( str
);
154 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
156 return FromString( s
);
159 wxString
wxNativeFontInfo::ToUserString() const
164 // ----------------------------------------------------------------------------
165 // wxNativeEncodingInfo
166 // ----------------------------------------------------------------------------
168 bool wxNativeEncodingInfo::FromString(const wxString
& s
)
173 wxString
wxNativeEncodingInfo::ToString() const
175 return wxEmptyString
;
178 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
183 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
184 wxNativeEncodingInfo
*info
)
193 #pragma message disable nosimpint
196 #include <X11/Xlib.h>
199 #pragma message enable nosimpint
202 #include "wx/utils.h" // for wxGetDisplay()
203 #elif defined(__WXGTK__)
204 // we have to declare struct tm to avoid problems with first forward
205 // declaring it in C code (glib.h included from gdk.h does it) and then
206 // defining it when time.h is included from the headers below - this is
207 // known not to work at least with Sun CC 6.01
214 // ----------------------------------------------------------------------------
216 // ----------------------------------------------------------------------------
218 static wxHashTable
*g_fontHash
= (wxHashTable
*) NULL
;
220 // ----------------------------------------------------------------------------
222 // ----------------------------------------------------------------------------
224 // define the functions to create and destroy native fonts for this toolkit
226 wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
228 return XLoadQueryFont((Display
*)wxGetDisplay(), fontSpec
);
231 inline void wxFreeFont(wxNativeFont font
)
233 XFreeFont((Display
*)wxGetDisplay(), (XFontStruct
*)font
);
235 #elif defined(__WXGTK__)
236 wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
238 // VZ: we should use gdk_fontset_load() instead of gdk_font_load()
239 // here to be able to display Japanese fonts correctly (at least
240 // this is what people report) but unfortunately doing it results
241 // in tons of warnings when using GTK with "normal" European
242 // languages and so we can't always do it and I don't know enough
243 // to determine when should this be done... (FIXME)
244 return gdk_font_load( wxConvertWX2MB(fontSpec
) );
247 inline void wxFreeFont(wxNativeFont font
)
249 gdk_font_unref(font
);
252 #error "Unknown GUI toolkit"
255 static bool wxTestFontSpec(const wxString
& fontspec
);
257 static wxNativeFont
wxLoadQueryFont(int pointSize
,
262 const wxString
& facename
,
263 const wxString
& xregistry
,
264 const wxString
& xencoding
,
265 wxString
* xFontName
);
267 // ============================================================================
269 // ============================================================================
271 // ----------------------------------------------------------------------------
272 // wxNativeEncodingInfo
273 // ----------------------------------------------------------------------------
275 // convert to/from the string representation: format is
276 // encodingid;registry;encoding[;facename]
277 bool wxNativeEncodingInfo::FromString(const wxString
& s
)
279 // use ";", not "-" because it may be part of encoding name
280 wxStringTokenizer
tokenizer(s
, _T(";"));
282 wxString encid
= tokenizer
.GetNextToken();
284 if ( !encid
.ToLong(&enc
) )
286 encoding
= (wxFontEncoding
)enc
;
288 xregistry
= tokenizer
.GetNextToken();
292 xencoding
= tokenizer
.GetNextToken();
297 facename
= tokenizer
.GetNextToken();
302 wxString
wxNativeEncodingInfo::ToString() const
305 s
<< (long)encoding
<< _T(';') << xregistry
<< _T(';') << xencoding
;
308 s
<< _T(';') << facename
;
314 // ----------------------------------------------------------------------------
316 // ----------------------------------------------------------------------------
318 void wxNativeFontInfo::Init()
323 bool wxNativeFontInfo::FromString(const wxString
& s
)
325 wxStringTokenizer
tokenizer(s
, _T(";"));
328 wxString token
= tokenizer
.GetNextToken();
329 if ( token
!= _T('0') )
332 xFontName
= tokenizer
.GetNextToken();
334 // this should be the end
335 if ( tokenizer
.HasMoreTokens() )
338 return FromXFontName(xFontName
);
341 wxString
wxNativeFontInfo::ToString() const
344 return wxString::Format(_T("%d;%s"), 0, GetXFontName().c_str());
347 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
349 return FromXFontName(s
);
352 wxString
wxNativeFontInfo::ToUserString() const
354 return GetXFontName();
357 bool wxNativeFontInfo::HasElements() const
359 // we suppose that the foundry is never empty, so if it is it means that we
360 // had never parsed the XLFD
361 return !fontElements
[0].empty();
364 wxString
wxNativeFontInfo::GetXFontComponent(wxXLFDField field
) const
366 wxCHECK_MSG( field
< wxXLFD_MAX
, _T(""), _T("invalid XLFD field") );
368 if ( !HasElements() )
371 if ( !((wxNativeFontInfo
*)this)->FromXFontName(xFontName
) )
375 return fontElements
[field
];
378 bool wxNativeFontInfo::FromXFontName(const wxString
& fontname
)
380 // TODO: we should be able to handle the font aliases here, but how?
381 wxStringTokenizer
tokenizer(fontname
, _T("-"));
383 // skip the leading, usually empty field (font name registry)
384 if ( !tokenizer
.HasMoreTokens() )
387 (void)tokenizer
.GetNextToken();
389 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
391 if ( !tokenizer
.HasMoreTokens() )
393 // not enough elements in the XLFD - or maybe an alias
397 wxString field
= tokenizer
.GetNextToken();
398 if ( !field
.empty() && field
!= _T('*') )
400 // we're really initialized now
404 fontElements
[n
] = field
;
407 // this should be all
408 if ( tokenizer
.HasMoreTokens() )
414 wxString
wxNativeFontInfo::GetXFontName() const
416 if ( xFontName
.empty() )
418 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
420 // replace the non specified elements with '*' except for the
421 // additional style which is usually just omitted
422 wxString elt
= fontElements
[n
];
423 if ( elt
.empty() && n
!= wxXLFD_ADDSTYLE
)
429 ((wxNativeFontInfo
*)this)->xFontName
<< _T('-') << elt
;
437 wxNativeFontInfo::SetXFontComponent(wxXLFDField field
, const wxString
& value
)
439 wxCHECK_RET( field
< wxXLFD_MAX
, _T("invalid XLFD field") );
441 // this class should be initialized with a valid font spec first and only
442 // then the fields may be modified!
443 wxASSERT_MSG( !IsDefault(), _T("can't modify an uninitialized XLFD") );
445 if ( !HasElements() )
448 if ( !((wxNativeFontInfo
*)this)->FromXFontName(xFontName
) )
450 wxFAIL_MSG( _T("can't set font element for invalid XLFD") );
456 fontElements
[field
] = value
;
458 // invalidate the XFLD, it doesn't correspond to the font elements any more
462 void wxNativeFontInfo::SetXFontName(const wxString
& xFontName_
)
464 // invalidate the font elements, GetXFontComponent() will reparse the XLFD
465 fontElements
[0].clear();
467 xFontName
= xFontName_
;
472 int wxNativeFontInfo::GetPointSize() const
474 const wxString s
= GetXFontComponent(wxXLFD_POINTSIZE
);
476 // return -1 to indicate that the size is unknown
478 return s
.ToLong(&l
) ? l
: -1;
481 wxFontStyle
wxNativeFontInfo::GetStyle() const
483 const wxString s
= GetXFontComponent(wxXLFD_SLANT
);
485 if ( s
.length() != 1 )
487 // it is really unknown but we don't have any way to return it from
489 return wxFONTSTYLE_NORMAL
;
495 // again, unknown but consider normal by default
498 return wxFONTSTYLE_NORMAL
;
501 return wxFONTSTYLE_ITALIC
;
504 return wxFONTSTYLE_SLANT
;
508 wxFontWeight
wxNativeFontInfo::GetWeight() const
510 const wxString s
= GetXFontComponent(wxXLFD_WEIGHT
).MakeLower();
511 if ( s
.find(_T("bold")) != wxString::npos
|| s
== _T("black") )
512 return wxFONTWEIGHT_BOLD
;
513 else if ( s
== _T("light") )
514 return wxFONTWEIGHT_LIGHT
;
516 return wxFONTWEIGHT_NORMAL
;
519 bool wxNativeFontInfo::GetUnderlined() const
521 // X fonts are never underlined
525 wxString
wxNativeFontInfo::GetFaceName() const
527 // wxWindows facename probably more accurately corresponds to X family
528 return GetXFontComponent(wxXLFD_FAMILY
);
531 wxFontFamily
wxNativeFontInfo::GetFamily() const
533 // and wxWindows family -- to X foundry, but we have to translate it to
534 // wxFontFamily somehow...
535 wxFAIL_MSG(_T("not implemented")); // GetXFontComponent(wxXLFD_FOUNDRY);
537 return wxFONTFAMILY_DEFAULT
;
540 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
542 // we already have the code for this but need to refactor it first
543 wxFAIL_MSG( _T("not implemented") );
545 return wxFONTENCODING_MAX
;
548 void wxNativeFontInfo::SetPointSize(int pointsize
)
550 SetXFontComponent(wxXLFD_POINTSIZE
, wxString::Format(_T("%d"), pointsize
));
553 void wxNativeFontInfo::SetStyle(wxFontStyle style
)
558 case wxFONTSTYLE_ITALIC
:
562 case wxFONTSTYLE_SLANT
:
566 case wxFONTSTYLE_NORMAL
:
570 wxFAIL_MSG( _T("unknown wxFontStyle in wxNativeFontInfo::SetStyle") );
574 SetXFontComponent(wxXLFD_SLANT
, s
);
577 void wxNativeFontInfo::SetWeight(wxFontWeight weight
)
582 case wxFONTWEIGHT_BOLD
:
586 case wxFONTWEIGHT_LIGHT
:
590 case wxFONTWEIGHT_NORMAL
:
595 wxFAIL_MSG( _T("unknown wxFontWeight in wxNativeFontInfo::SetWeight") );
599 SetXFontComponent(wxXLFD_WEIGHT
, s
);
602 void wxNativeFontInfo::SetUnderlined(bool WXUNUSED(underlined
))
604 // can't do this under X
607 void wxNativeFontInfo::SetFaceName(wxString facename
)
609 SetXFontComponent(wxXLFD_FAMILY
, facename
);
612 void wxNativeFontInfo::SetFamily(wxFontFamily family
)
614 // wxFontFamily -> X foundry, anyone?
615 wxFAIL_MSG( _T("not implemented") );
617 // SetXFontComponent(wxXLFD_FOUNDRY, ...);
620 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding
)
622 wxNativeEncodingInfo info
;
623 if ( wxGetNativeFontEncoding(encoding
, &info
) )
625 SetXFontComponent(wxXLFD_ENCODING
, info
.xencoding
);
626 SetXFontComponent(wxXLFD_REGISTRY
, info
.xregistry
);
630 // ----------------------------------------------------------------------------
632 // ----------------------------------------------------------------------------
634 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
635 wxNativeEncodingInfo
*info
)
637 wxCHECK_MSG( info
, FALSE
, _T("bad pointer in wxGetNativeFontEncoding") );
639 if ( encoding
== wxFONTENCODING_DEFAULT
)
641 encoding
= wxFont::GetDefaultEncoding();
646 case wxFONTENCODING_ISO8859_1
:
647 case wxFONTENCODING_ISO8859_2
:
648 case wxFONTENCODING_ISO8859_3
:
649 case wxFONTENCODING_ISO8859_4
:
650 case wxFONTENCODING_ISO8859_5
:
651 case wxFONTENCODING_ISO8859_6
:
652 case wxFONTENCODING_ISO8859_7
:
653 case wxFONTENCODING_ISO8859_8
:
654 case wxFONTENCODING_ISO8859_9
:
655 case wxFONTENCODING_ISO8859_10
:
656 case wxFONTENCODING_ISO8859_11
:
657 case wxFONTENCODING_ISO8859_12
:
658 case wxFONTENCODING_ISO8859_13
:
659 case wxFONTENCODING_ISO8859_14
:
660 case wxFONTENCODING_ISO8859_15
:
662 int cp
= encoding
- wxFONTENCODING_ISO8859_1
+ 1;
663 info
->xregistry
= wxT("iso8859");
664 info
->xencoding
.Printf(wxT("%d"), cp
);
668 case wxFONTENCODING_UTF8
:
669 info
->xregistry
= wxT("iso10646");
670 info
->xencoding
= wxT("*");
673 case wxFONTENCODING_GB2312
:
674 info
->xregistry
= wxT("GB2312"); // or the otherway round?
675 info
->xencoding
= wxT("*");
678 case wxFONTENCODING_KOI8
:
679 info
->xregistry
= wxT("koi8");
681 // we don't make distinction between koi8-r, koi8-u and koi8-ru (so far)
682 info
->xencoding
= wxT("*");
685 case wxFONTENCODING_CP1250
:
686 case wxFONTENCODING_CP1251
:
687 case wxFONTENCODING_CP1252
:
688 case wxFONTENCODING_CP1253
:
689 case wxFONTENCODING_CP1254
:
690 case wxFONTENCODING_CP1255
:
691 case wxFONTENCODING_CP1256
:
692 case wxFONTENCODING_CP1257
:
694 int cp
= encoding
- wxFONTENCODING_CP1250
+ 1250;
695 info
->xregistry
= wxT("microsoft");
696 info
->xencoding
.Printf(wxT("cp%d"), cp
);
700 case wxFONTENCODING_SYSTEM
:
702 info
->xencoding
= wxT("*");
706 // don't know how to translate this encoding into X fontspec
710 info
->encoding
= encoding
;
715 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
718 fontspec
.Printf(_T("-*-%s-*-*-*-*-*-*-*-*-*-*-%s-%s"),
719 !info
.facename
? _T("*") : info
.facename
.c_str(),
720 info
.xregistry
.c_str(),
721 info
.xencoding
.c_str());
723 return wxTestFontSpec(fontspec
);
726 // ----------------------------------------------------------------------------
727 // X-specific functions
728 // ----------------------------------------------------------------------------
730 wxNativeFont
wxLoadQueryNearestFont(int pointSize
,
735 const wxString
&facename
,
736 wxFontEncoding encoding
,
739 if ( encoding
== wxFONTENCODING_DEFAULT
)
741 encoding
= wxFont::GetDefaultEncoding();
744 // first determine the encoding - if the font doesn't exist at all in this
745 // encoding, it's useless to do all other approximations (i.e. size,
746 // family &c don't matter much)
747 wxNativeEncodingInfo info
;
748 if ( encoding
== wxFONTENCODING_SYSTEM
)
750 // This will always work so we don't test to save time
751 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
755 if ( !wxGetNativeFontEncoding(encoding
, &info
) ||
756 !wxTestFontEncoding(info
) )
759 if ( !wxFontMapper::Get()->GetAltForEncoding(encoding
, &info
) )
760 #endif // wxUSE_FONTMAP
762 // unspported encoding - replace it with the default
764 // NB: we can't just return 0 from here because wxGTK code doesn't
765 // check for it (i.e. it supposes that we'll always succeed),
766 // so it would provoke a crash
767 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
772 // OK, we have the correct xregistry/xencoding in info structure
773 wxNativeFont font
= 0;
775 // if we already have the X font name, try to use it
776 if( xFontName
&& !xFontName
->IsEmpty() )
779 // Make sure point size is correct for scale factor.
781 wxStringTokenizer
tokenizer(*xFontName
, _T("-"), wxTOKEN_RET_DELIMS
);
782 wxString newFontName
;
784 for(int i
= 0; i
< 8; i
++)
785 newFontName
+= tokenizer
.NextToken();
787 (void) tokenizer
.NextToken();
789 newFontName
+= wxString::Format(wxT("%d-"), pointSize
);
791 while(tokenizer
.HasMoreTokens())
792 newFontName
+= tokenizer
.GetNextToken();
794 font
= wxLoadFont(newFontName
);
797 *xFontName
= newFontName
;
802 // search up and down by stepsize 10
803 int max_size
= pointSize
+ 20 * (1 + (pointSize
/180));
804 int min_size
= pointSize
- 20 * (1 + (pointSize
/180));
806 int i
, round
; // counters
808 // first round: search for equal, then for smaller and for larger size with the given weight and style
809 int testweight
= weight
;
810 int teststyle
= style
;
812 for ( round
= 0; round
< 3; round
++ )
814 // second round: use normal weight
817 if ( testweight
!= wxNORMAL
)
819 testweight
= wxNORMAL
;
823 ++round
; // fall through to third round
827 // third round: ... and use normal style
830 if ( teststyle
!= wxNORMAL
)
832 teststyle
= wxNORMAL
;
839 // Search for equal or smaller size (approx.)
840 for ( i
= pointSize
; !font
&& i
>= 10 && i
>= min_size
; i
-= 10 )
842 font
= wxLoadQueryFont(i
, family
, teststyle
, testweight
, underlined
,
843 facename
, info
.xregistry
, info
.xencoding
,
847 // Search for larger size (approx.)
848 for ( i
= pointSize
+ 10; !font
&& i
<= max_size
; i
+= 10 )
850 font
= wxLoadQueryFont(i
, family
, teststyle
, testweight
, underlined
,
851 facename
, info
.xregistry
, info
.xencoding
,
856 // Try default family
857 if ( !font
&& family
!= wxDEFAULT
)
859 font
= wxLoadQueryFont(pointSize
, wxDEFAULT
, style
, weight
,
860 underlined
, facename
,
861 info
.xregistry
, info
.xencoding
,
865 // ignore size, family, style and weight but try to find font with the
866 // given facename and encoding
869 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
870 underlined
, facename
,
871 info
.xregistry
, info
.xencoding
,
874 // ignore family as well
877 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
878 underlined
, wxEmptyString
,
879 info
.xregistry
, info
.xencoding
,
882 // if it still failed, try to get the font of any size but
883 // with the requested encoding: this can happen if the
884 // encoding is only available in one size which happens to be
885 // different from 120
888 font
= wxLoadQueryFont(-1, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
889 FALSE
, wxEmptyString
,
890 info
.xregistry
, info
.xencoding
,
893 // this should never happen as we had tested for it in the
894 // very beginning, but if it does, do return something non
895 // NULL or we'd crash in wxFont code
898 wxFAIL_MSG( _T("this encoding should be available!") );
900 font
= wxLoadQueryFont(-1,
901 wxDEFAULT
, wxNORMAL
, wxNORMAL
,
902 FALSE
, wxEmptyString
,
914 // ----------------------------------------------------------------------------
916 // ----------------------------------------------------------------------------
918 // returns TRUE if there are any fonts matching this font spec
919 static bool wxTestFontSpec(const wxString
& fontspec
)
921 // some X servers will fail to load this font because there are too many
922 // matches so we must test explicitly for this
923 if ( fontspec
== _T("-*-*-*-*-*-*-*-*-*-*-*-*-*-*") )
928 wxNativeFont test
= (wxNativeFont
) g_fontHash
->Get( fontspec
);
934 test
= wxLoadFont(fontspec
);
935 g_fontHash
->Put( fontspec
, (wxObject
*) test
);
949 static wxNativeFont
wxLoadQueryFont(int pointSize
,
953 bool WXUNUSED(underlined
),
954 const wxString
& facename
,
955 const wxString
& xregistry
,
956 const wxString
& xencoding
,
962 case wxDECORATIVE
: xfamily
= wxT("lucida"); break;
963 case wxROMAN
: xfamily
= wxT("times"); break;
964 case wxMODERN
: xfamily
= wxT("courier"); break;
965 case wxSWISS
: xfamily
= wxT("helvetica"); break;
966 case wxTELETYPE
: xfamily
= wxT("lucidatypewriter"); break;
967 case wxSCRIPT
: xfamily
= wxT("utopia"); break;
968 default: xfamily
= wxT("*");
976 xweight
= MWLF_WEIGHT_BOLD
;
981 xweight
= MWLF_WEIGHT_LIGHT
;
986 xweight
= MWLF_WEIGHT_NORMAL
;
992 xweight
= MWLF_WEIGHT_DEFAULT
;
996 GR_SCREEN_INFO screenInfo
;
997 GrGetScreenInfo(& screenInfo
);
999 int yPixelsPerCM
= screenInfo
.ydpcm
;
1001 // A point is 1/72 of an inch.
1002 // An inch is 2.541 cm.
1003 // So pixelHeight = (pointSize / 72) (inches) * 2.541 (for cm) * yPixelsPerCM (for pixels)
1004 // In fact pointSize is 10 * the normal point size so
1007 int pixelHeight
= (int) ( (((float)pointSize
) / 720.0) * 2.541 * (float) yPixelsPerCM
) ;
1009 // An alternative: assume that the screen is 72 dpi.
1010 //int pixelHeight = (int) (((float)pointSize / 720.0) * 72.0) ;
1011 //int pixelHeight = (int) ((float)pointSize / 10.0) ;
1014 logFont
.lfHeight
= pixelHeight
;
1015 logFont
.lfWidth
= 0;
1016 logFont
.lfEscapement
= 0;
1017 logFont
.lfOrientation
= 0;
1018 logFont
.lfWeight
= xweight
;
1019 logFont
.lfItalic
= (style
== wxNORMAL
? 0 : 1) ;
1020 logFont
.lfUnderline
= 0;
1021 logFont
.lfStrikeOut
= 0;
1022 logFont
.lfCharSet
= MWLF_CHARSET_DEFAULT
; // TODO: select appropriate one
1023 logFont
.lfOutPrecision
= MWLF_TYPE_DEFAULT
;
1024 logFont
.lfClipPrecision
= 0; // Not used
1025 logFont
.lfRoman
= (family
== wxROMAN
? 1 : 0) ;
1026 logFont
.lfSerif
= (family
== wxSWISS
? 0 : 1) ;
1027 logFont
.lfSansSerif
= !logFont
.lfSerif
;
1028 logFont
.lfModern
= (family
== wxMODERN
? 1 : 0) ;
1029 logFont
.lfProportional
= (family
== wxTELETYPE
? 0 : 1) ;
1030 logFont
.lfOblique
= 0;
1031 logFont
.lfSmallCaps
= 0;
1032 logFont
.lfPitch
= 0; // 0 = default
1033 strcpy(logFont
.lfFaceName
, facename
.c_str());
1035 XFontStruct
* fontInfo
= (XFontStruct
*) malloc(sizeof(XFontStruct
));
1036 fontInfo
->fid
= GrCreateFont((GR_CHAR
*) facename
.c_str(), pixelHeight
, & logFont
);
1037 GrGetFontInfo(fontInfo
->fid
, & fontInfo
->info
);
1038 return (wxNativeFont
) fontInfo
;
1042 if (!facename
.IsEmpty())
1044 fontSpec
.Printf(wxT("-*-%s-*-*-normal-*-*-*-*-*-*-*-*-*"),
1047 if ( wxTestFontSpec(fontSpec
) )
1051 //else: no such family, use default one instead
1058 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1060 if ( wxTestFontSpec(fontSpec
) )
1065 // fall through - try wxITALIC now
1068 fontSpec
.Printf(wxT("-*-%s-*-i-*-*-*-*-*-*-*-*-*-*"),
1070 if ( wxTestFontSpec(fontSpec
) )
1074 else if ( style
== wxITALIC
) // and not wxSLANT
1077 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1079 if ( wxTestFontSpec(fontSpec
) )
1085 // no italic, no slant - leave default
1092 wxFAIL_MSG(_T("unknown font style"));
1093 // fall back to normal
1105 fontSpec
.Printf(wxT("-*-%s-bold-*-*-*-*-*-*-*-*-*-*-*"),
1107 if ( wxTestFontSpec(fontSpec
) )
1109 xweight
= wxT("bold");
1112 fontSpec
.Printf(wxT("-*-%s-heavy-*-*-*-*-*-*-*-*-*-*-*"),
1114 if ( wxTestFontSpec(fontSpec
) )
1116 xweight
= wxT("heavy");
1119 fontSpec
.Printf(wxT("-*-%s-extrabold-*-*-*-*-*-*-*-*-*-*-*"),
1121 if ( wxTestFontSpec(fontSpec
) )
1123 xweight
= wxT("extrabold");
1126 fontSpec
.Printf(wxT("-*-%s-demibold-*-*-*-*-*-*-*-*-*-*-*"),
1128 if ( wxTestFontSpec(fontSpec
) )
1130 xweight
= wxT("demibold");
1133 fontSpec
.Printf(wxT("-*-%s-black-*-*-*-*-*-*-*-*-*-*-*"),
1135 if ( wxTestFontSpec(fontSpec
) )
1137 xweight
= wxT("black");
1140 fontSpec
.Printf(wxT("-*-%s-ultrablack-*-*-*-*-*-*-*-*-*-*-*"),
1142 if ( wxTestFontSpec(fontSpec
) )
1144 xweight
= wxT("ultrablack");
1151 fontSpec
.Printf(wxT("-*-%s-light-*-*-*-*-*-*-*-*-*-*-*"),
1153 if ( wxTestFontSpec(fontSpec
) )
1155 xweight
= wxT("light");
1158 fontSpec
.Printf(wxT("-*-%s-thin-*-*-*-*-*-*-*-*-*-*-*"),
1160 if ( wxTestFontSpec(fontSpec
) )
1162 xweight
= wxT("thin");
1169 fontSpec
.Printf(wxT("-*-%s-medium-*-*-*-*-*-*-*-*-*-*-*"),
1171 if ( wxTestFontSpec(fontSpec
) )
1173 xweight
= wxT("medium");
1176 fontSpec
.Printf(wxT("-*-%s-normal-*-*-*-*-*-*-*-*-*-*-*"),
1178 if ( wxTestFontSpec(fontSpec
) )
1180 xweight
= wxT("normal");
1183 fontSpec
.Printf(wxT("-*-%s-regular-*-*-*-*-*-*-*-*-*-*-*"),
1185 if ( wxTestFontSpec(fontSpec
) )
1187 xweight
= wxT("regular");
1193 default: xweight
= wxT("*"); break;
1196 // if pointSize is -1, don't specify any
1198 if ( pointSize
== -1 )
1204 sizeSpec
.Printf(_T("%d"), pointSize
);
1207 // construct the X font spec from our data
1208 fontSpec
.Printf(wxT("-*-%s-%s-%s-normal-*-*-%s-*-*-*-*-%s-%s"),
1209 xfamily
.c_str(), xweight
.c_str(), xstyle
.c_str(),
1210 sizeSpec
.c_str(), xregistry
.c_str(), xencoding
.c_str());
1213 *xFontName
= fontSpec
;
1215 return wxLoadFont(fontSpec
);
1220 // ----------------------------------------------------------------------------
1222 // ----------------------------------------------------------------------------
1224 class wxFontModule
: public wxModule
1231 DECLARE_DYNAMIC_CLASS(wxFontModule
)
1234 IMPLEMENT_DYNAMIC_CLASS(wxFontModule
, wxModule
)
1236 bool wxFontModule::OnInit()
1238 g_fontHash
= new wxHashTable( wxKEY_STRING
);
1243 void wxFontModule::OnExit()
1247 g_fontHash
= (wxHashTable
*)NULL
;
1250 #endif // GTK 2.0/1.x