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/font.h" // wxFont enums
33 #include "wx/encinfo.h"
36 #include "wx/fontutil.h"
37 #include "wx/fontmap.h"
38 #include "wx/tokenzr.h"
40 #include "wx/module.h"
44 #include "pango/pango.h"
47 #include "wx/gtk/private.h"
48 extern GtkWidget
*wxGetRootWindow();
50 #include "wx/x11/private.h"
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 void wxNativeFontInfo::Init()
63 wxNativeFontInfo::Init(const wxNativeFontInfo
& info
)
66 description
= pango_font_description_copy(info
.description
);
71 void wxNativeFontInfo::Free()
74 pango_font_description_free(description
);
77 int wxNativeFontInfo::GetPointSize() const
79 return pango_font_description_get_size( description
) / PANGO_SCALE
;
82 wxFontStyle
wxNativeFontInfo::GetStyle() const
84 wxFontStyle m_style
= wxFONTSTYLE_NORMAL
;
86 switch (pango_font_description_get_style( description
))
88 case PANGO_STYLE_NORMAL
:
89 m_style
= wxFONTSTYLE_NORMAL
;
91 case PANGO_STYLE_ITALIC
:
92 m_style
= wxFONTSTYLE_ITALIC
;
94 case PANGO_STYLE_OBLIQUE
:
95 m_style
= wxFONTSTYLE_SLANT
;
102 wxFontWeight
wxNativeFontInfo::GetWeight() const
105 // We seem to currently initialize only by string.
106 // In that case PANGO_FONT_MASK_WEIGHT is always set.
107 if (!(pango_font_description_get_set_fields(description
) & PANGO_FONT_MASK_WEIGHT
))
108 return wxFONTWEIGHT_NORMAL
;
111 PangoWeight pango_weight
= pango_font_description_get_weight( description
);
113 // Until the API can be changed the following ranges of weight values are used:
114 // wxFONTWEIGHT_LIGHT: 100 .. 349 - range of 250
115 // wxFONTWEIGHT_NORMAL: 350 .. 599 - range of 250
116 // wxFONTWEIGHT_BOLD: 600 .. 900 - range of 301 (600 is "semibold" already)
118 if (pango_weight
>= 600)
119 return wxFONTWEIGHT_BOLD
;
121 if (pango_weight
< 350)
122 return wxFONTWEIGHT_LIGHT
;
124 return wxFONTWEIGHT_NORMAL
;
127 bool wxNativeFontInfo::GetUnderlined() const
132 wxString
wxNativeFontInfo::GetFaceName() const
134 wxString tmp
= wxGTK_CONV_BACK( pango_font_description_get_family( description
) );
139 wxFontFamily
wxNativeFontInfo::GetFamily() const
141 wxFontFamily ret
= wxFONTFAMILY_DEFAULT
;
142 char *family_text
= g_ascii_strdown( pango_font_description_get_family( description
), -1 );
143 // Check for some common fonts, to salvage what we can from the current win32 centric wxFont API:
144 if (strncmp( family_text
, "monospace", 9 ) == 0)
145 ret
= wxFONTFAMILY_TELETYPE
; // begins with "Monospace"
146 else if (strncmp( family_text
, "courier", 7 ) == 0)
147 ret
= wxFONTFAMILY_TELETYPE
; // begins with "Courier"
148 #ifdef HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE
151 PangoFontFamily
**families
;
152 PangoFontFamily
*family
;
154 pango_context_list_families(
156 gtk_widget_get_pango_context( wxGetRootWindow() ),
158 wxTheApp
->GetPangoContext(),
160 &families
, &n_families
);
162 for (int i
= 0;i
< n_families
;++i
)
164 if (g_ascii_strcasecmp(pango_font_family_get_name( families
[i
] ), pango_font_description_get_family( description
)) == 0 )
166 family
= families
[i
];
173 wxASSERT_MSG( family
, wxT("wxNativeFontInfo::GetFamily() - No appropriate PangoFontFamily found for ::description") );
175 //BCI: Cache the wxFontFamily inside the class. Validate cache with
176 //BCI: g_ascii_strcasecmp(pango_font_description_get_family(description), pango_font_family_get_name(family)) == 0
178 if (pango_font_family_is_monospace( family
))
179 ret
= wxFONTFAMILY_TELETYPE
; // is deemed a monospace font by pango
181 #endif // pango_font_family_is_monospace
183 if (ret
== wxFONTFAMILY_DEFAULT
)
185 if (strstr( family_text
, "sans" ) != NULL
) // checked before serif, so that "* Sans Serif" fonts are detected correctly
186 ret
= wxFONTFAMILY_SWISS
; // contains "Sans"
187 else if (strstr( family_text
, "serif" ) != NULL
)
188 ret
= wxFONTFAMILY_ROMAN
; // contains "Serif"
189 else if (strncmp( family_text
, "times", 5 ) == 0)
190 ret
= wxFONTFAMILY_ROMAN
; // begins with "Times"
191 else if (strncmp( family_text
, "old", 3 ) == 0)
192 ret
= wxFONTFAMILY_DECORATIVE
; // Begins with "Old" - "Old English", "Old Town"
199 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
201 return wxFONTENCODING_SYSTEM
;
205 void wxNativeFontInfo::SetPointSize(int WXUNUSED(pointsize
))
207 wxFAIL_MSG( _T("not implemented") );
210 void wxNativeFontInfo::SetStyle(wxFontStyle
WXUNUSED(style
))
212 wxFAIL_MSG( _T("not implemented") );
215 void wxNativeFontInfo::SetWeight(wxFontWeight
WXUNUSED(weight
))
217 wxFAIL_MSG( _T("not implemented") );
220 void wxNativeFontInfo::SetUnderlined(bool WXUNUSED(underlined
))
222 wxFAIL_MSG( _T("not implemented") );
225 void wxNativeFontInfo::SetFaceName(wxString
WXUNUSED(facename
))
227 wxFAIL_MSG( _T("not implemented") );
230 void wxNativeFontInfo::SetFamily(wxFontFamily
WXUNUSED(family
))
232 wxFAIL_MSG( _T("not implemented") );
235 void wxNativeFontInfo::SetEncoding(wxFontEncoding
WXUNUSED(encoding
))
237 wxFAIL_MSG( _T("not implemented") );
242 bool wxNativeFontInfo::FromString(const wxString
& s
)
245 pango_font_description_free( description
);
247 description
= pango_font_description_from_string( wxGTK_CONV( s
) );
252 wxString
wxNativeFontInfo::ToString() const
254 char *str
= pango_font_description_to_string( description
);
255 wxString tmp
= wxGTK_CONV_BACK( str
);
261 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
263 return FromString( s
);
266 wxString
wxNativeFontInfo::ToUserString() const
271 // ----------------------------------------------------------------------------
272 // wxNativeEncodingInfo
273 // ----------------------------------------------------------------------------
275 bool wxNativeEncodingInfo::FromString(const wxString
& s
)
280 wxString
wxNativeEncodingInfo::ToString() const
282 return wxEmptyString
;
285 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
290 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
291 wxNativeEncodingInfo
*info
)
293 // we *must* return true for default encoding as otherwise wxFontMapper
294 // considers that we can't load any font and aborts with wxLogFatalError!
295 if ( encoding
== wxFONTENCODING_SYSTEM
)
297 info
->facename
.clear();
298 info
->encoding
= wxFONTENCODING_SYSTEM
;
301 // pretend that we support everything, it's better than to always return
302 // false as the old code did
310 #pragma message disable nosimpint
313 #include <X11/Xlib.h>
316 #pragma message enable nosimpint
319 #include "wx/utils.h" // for wxGetDisplay()
320 #elif defined(__WXGTK__)
321 // we have to declare struct tm to avoid problems with first forward
322 // declaring it in C code (glib.h included from gdk.h does it) and then
323 // defining it when time.h is included from the headers below - this is
324 // known not to work at least with Sun CC 6.01
331 // ----------------------------------------------------------------------------
333 // ----------------------------------------------------------------------------
335 static wxHashTable
*g_fontHash
= (wxHashTable
*) NULL
;
337 // ----------------------------------------------------------------------------
339 // ----------------------------------------------------------------------------
341 // define the functions to create and destroy native fonts for this toolkit
343 wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
345 return XLoadQueryFont((Display
*)wxGetDisplay(), fontSpec
);
348 inline void wxFreeFont(wxNativeFont font
)
350 XFreeFont((Display
*)wxGetDisplay(), (XFontStruct
*)font
);
352 #elif defined(__WXGTK__)
353 wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
355 // VZ: we should use gdk_fontset_load() instead of gdk_font_load()
356 // here to be able to display Japanese fonts correctly (at least
357 // this is what people report) but unfortunately doing it results
358 // in tons of warnings when using GTK with "normal" European
359 // languages and so we can't always do it and I don't know enough
360 // to determine when should this be done... (FIXME)
361 return gdk_font_load( wxConvertWX2MB(fontSpec
) );
364 inline void wxFreeFont(wxNativeFont font
)
366 gdk_font_unref(font
);
369 #error "Unknown GUI toolkit"
372 static bool wxTestFontSpec(const wxString
& fontspec
);
374 static wxNativeFont
wxLoadQueryFont(int pointSize
,
379 const wxString
& facename
,
380 const wxString
& xregistry
,
381 const wxString
& xencoding
,
382 wxString
* xFontName
);
384 // ============================================================================
386 // ============================================================================
388 // ----------------------------------------------------------------------------
389 // wxNativeEncodingInfo
390 // ----------------------------------------------------------------------------
392 // convert to/from the string representation: format is
393 // encodingid;registry;encoding[;facename]
394 bool wxNativeEncodingInfo::FromString(const wxString
& s
)
396 // use ";", not "-" because it may be part of encoding name
397 wxStringTokenizer
tokenizer(s
, _T(";"));
399 wxString encid
= tokenizer
.GetNextToken();
401 if ( !encid
.ToLong(&enc
) )
403 encoding
= (wxFontEncoding
)enc
;
405 xregistry
= tokenizer
.GetNextToken();
409 xencoding
= tokenizer
.GetNextToken();
414 facename
= tokenizer
.GetNextToken();
419 wxString
wxNativeEncodingInfo::ToString() const
422 s
<< (long)encoding
<< _T(';') << xregistry
<< _T(';') << xencoding
;
425 s
<< _T(';') << facename
;
431 // ----------------------------------------------------------------------------
433 // ----------------------------------------------------------------------------
435 void wxNativeFontInfo::Init()
440 bool wxNativeFontInfo::FromString(const wxString
& s
)
442 wxStringTokenizer
tokenizer(s
, _T(";"));
445 wxString token
= tokenizer
.GetNextToken();
446 if ( token
!= _T('0') )
449 xFontName
= tokenizer
.GetNextToken();
451 // this should be the end
452 if ( tokenizer
.HasMoreTokens() )
455 return FromXFontName(xFontName
);
458 wxString
wxNativeFontInfo::ToString() const
461 return wxString::Format(_T("%d;%s"), 0, GetXFontName().c_str());
464 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
466 return FromXFontName(s
);
469 wxString
wxNativeFontInfo::ToUserString() const
471 return GetXFontName();
474 bool wxNativeFontInfo::HasElements() const
476 // we suppose that the foundry is never empty, so if it is it means that we
477 // had never parsed the XLFD
478 return !fontElements
[0].empty();
481 wxString
wxNativeFontInfo::GetXFontComponent(wxXLFDField field
) const
483 wxCHECK_MSG( field
< wxXLFD_MAX
, _T(""), _T("invalid XLFD field") );
485 if ( !HasElements() )
488 if ( !((wxNativeFontInfo
*)this)->FromXFontName(xFontName
) )
492 return fontElements
[field
];
495 bool wxNativeFontInfo::FromXFontName(const wxString
& fontname
)
497 // TODO: we should be able to handle the font aliases here, but how?
498 wxStringTokenizer
tokenizer(fontname
, _T("-"));
500 // skip the leading, usually empty field (font name registry)
501 if ( !tokenizer
.HasMoreTokens() )
504 (void)tokenizer
.GetNextToken();
506 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
508 if ( !tokenizer
.HasMoreTokens() )
510 // not enough elements in the XLFD - or maybe an alias
514 wxString field
= tokenizer
.GetNextToken();
515 if ( !field
.empty() && field
!= _T('*') )
517 // we're really initialized now
521 fontElements
[n
] = field
;
524 // this should be all
525 if ( tokenizer
.HasMoreTokens() )
531 wxString
wxNativeFontInfo::GetXFontName() const
533 if ( xFontName
.empty() )
535 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
537 // replace the non specified elements with '*' except for the
538 // additional style which is usually just omitted
539 wxString elt
= fontElements
[n
];
540 if ( elt
.empty() && n
!= wxXLFD_ADDSTYLE
)
546 ((wxNativeFontInfo
*)this)->xFontName
<< _T('-') << elt
;
554 wxNativeFontInfo::SetXFontComponent(wxXLFDField field
, const wxString
& value
)
556 wxCHECK_RET( field
< wxXLFD_MAX
, _T("invalid XLFD field") );
558 // this class should be initialized with a valid font spec first and only
559 // then the fields may be modified!
560 wxASSERT_MSG( !IsDefault(), _T("can't modify an uninitialized XLFD") );
562 if ( !HasElements() )
565 if ( !((wxNativeFontInfo
*)this)->FromXFontName(xFontName
) )
567 wxFAIL_MSG( _T("can't set font element for invalid XLFD") );
573 fontElements
[field
] = value
;
575 // invalidate the XFLD, it doesn't correspond to the font elements any more
579 void wxNativeFontInfo::SetXFontName(const wxString
& xFontName_
)
581 // invalidate the font elements, GetXFontComponent() will reparse the XLFD
582 fontElements
[0].clear();
584 xFontName
= xFontName_
;
589 int wxNativeFontInfo::GetPointSize() const
591 const wxString s
= GetXFontComponent(wxXLFD_POINTSIZE
);
593 // return -1 to indicate that the size is unknown
595 return s
.ToLong(&l
) ? l
: -1;
598 wxFontStyle
wxNativeFontInfo::GetStyle() const
600 const wxString s
= GetXFontComponent(wxXLFD_SLANT
);
602 if ( s
.length() != 1 )
604 // it is really unknown but we don't have any way to return it from
606 return wxFONTSTYLE_NORMAL
;
612 // again, unknown but consider normal by default
615 return wxFONTSTYLE_NORMAL
;
618 return wxFONTSTYLE_ITALIC
;
621 return wxFONTSTYLE_SLANT
;
625 wxFontWeight
wxNativeFontInfo::GetWeight() const
627 const wxString s
= GetXFontComponent(wxXLFD_WEIGHT
).MakeLower();
628 if ( s
.find(_T("bold")) != wxString::npos
|| s
== _T("black") )
629 return wxFONTWEIGHT_BOLD
;
630 else if ( s
== _T("light") )
631 return wxFONTWEIGHT_LIGHT
;
633 return wxFONTWEIGHT_NORMAL
;
636 bool wxNativeFontInfo::GetUnderlined() const
638 // X fonts are never underlined
642 wxString
wxNativeFontInfo::GetFaceName() const
644 // wxWidgets facename probably more accurately corresponds to X family
645 return GetXFontComponent(wxXLFD_FAMILY
);
648 wxFontFamily
wxNativeFontInfo::GetFamily() const
650 // and wxWidgets family -- to X foundry, but we have to translate it to
651 // wxFontFamily somehow...
652 wxFAIL_MSG(_T("not implemented")); // GetXFontComponent(wxXLFD_FOUNDRY);
654 return wxFONTFAMILY_DEFAULT
;
657 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
659 // we already have the code for this but need to refactor it first
660 wxFAIL_MSG( _T("not implemented") );
662 return wxFONTENCODING_MAX
;
665 void wxNativeFontInfo::SetPointSize(int pointsize
)
667 SetXFontComponent(wxXLFD_POINTSIZE
, wxString::Format(_T("%d"), pointsize
));
670 void wxNativeFontInfo::SetStyle(wxFontStyle style
)
675 case wxFONTSTYLE_ITALIC
:
679 case wxFONTSTYLE_SLANT
:
683 case wxFONTSTYLE_NORMAL
:
687 wxFAIL_MSG( _T("unknown wxFontStyle in wxNativeFontInfo::SetStyle") );
691 SetXFontComponent(wxXLFD_SLANT
, s
);
694 void wxNativeFontInfo::SetWeight(wxFontWeight weight
)
699 case wxFONTWEIGHT_BOLD
:
703 case wxFONTWEIGHT_LIGHT
:
707 case wxFONTWEIGHT_NORMAL
:
712 wxFAIL_MSG( _T("unknown wxFontWeight in wxNativeFontInfo::SetWeight") );
716 SetXFontComponent(wxXLFD_WEIGHT
, s
);
719 void wxNativeFontInfo::SetUnderlined(bool WXUNUSED(underlined
))
721 // can't do this under X
724 void wxNativeFontInfo::SetFaceName(wxString facename
)
726 SetXFontComponent(wxXLFD_FAMILY
, facename
);
729 void wxNativeFontInfo::SetFamily(wxFontFamily family
)
731 // wxFontFamily -> X foundry, anyone?
732 wxFAIL_MSG( _T("not implemented") );
734 // SetXFontComponent(wxXLFD_FOUNDRY, ...);
737 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding
)
739 wxNativeEncodingInfo info
;
740 if ( wxGetNativeFontEncoding(encoding
, &info
) )
742 SetXFontComponent(wxXLFD_ENCODING
, info
.xencoding
);
743 SetXFontComponent(wxXLFD_REGISTRY
, info
.xregistry
);
747 // ----------------------------------------------------------------------------
749 // ----------------------------------------------------------------------------
751 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
752 wxNativeEncodingInfo
*info
)
754 wxCHECK_MSG( info
, FALSE
, _T("bad pointer in wxGetNativeFontEncoding") );
756 if ( encoding
== wxFONTENCODING_DEFAULT
)
758 encoding
= wxFont::GetDefaultEncoding();
763 case wxFONTENCODING_ISO8859_1
:
764 case wxFONTENCODING_ISO8859_2
:
765 case wxFONTENCODING_ISO8859_3
:
766 case wxFONTENCODING_ISO8859_4
:
767 case wxFONTENCODING_ISO8859_5
:
768 case wxFONTENCODING_ISO8859_6
:
769 case wxFONTENCODING_ISO8859_7
:
770 case wxFONTENCODING_ISO8859_8
:
771 case wxFONTENCODING_ISO8859_9
:
772 case wxFONTENCODING_ISO8859_10
:
773 case wxFONTENCODING_ISO8859_11
:
774 case wxFONTENCODING_ISO8859_12
:
775 case wxFONTENCODING_ISO8859_13
:
776 case wxFONTENCODING_ISO8859_14
:
777 case wxFONTENCODING_ISO8859_15
:
779 int cp
= encoding
- wxFONTENCODING_ISO8859_1
+ 1;
780 info
->xregistry
= wxT("iso8859");
781 info
->xencoding
.Printf(wxT("%d"), cp
);
785 case wxFONTENCODING_UTF8
:
786 info
->xregistry
= wxT("iso10646");
787 info
->xencoding
= wxT("*");
790 case wxFONTENCODING_GB2312
:
791 info
->xregistry
= wxT("GB2312"); // or the otherway round?
792 info
->xencoding
= wxT("*");
795 case wxFONTENCODING_KOI8
:
796 case wxFONTENCODING_KOI8_U
:
797 info
->xregistry
= wxT("koi8");
799 // we don't make distinction between koi8-r, koi8-u and koi8-ru (so far)
800 info
->xencoding
= wxT("*");
803 case wxFONTENCODING_CP1250
:
804 case wxFONTENCODING_CP1251
:
805 case wxFONTENCODING_CP1252
:
806 case wxFONTENCODING_CP1253
:
807 case wxFONTENCODING_CP1254
:
808 case wxFONTENCODING_CP1255
:
809 case wxFONTENCODING_CP1256
:
810 case wxFONTENCODING_CP1257
:
812 int cp
= encoding
- wxFONTENCODING_CP1250
+ 1250;
813 info
->xregistry
= wxT("microsoft");
814 info
->xencoding
.Printf(wxT("cp%d"), cp
);
818 case wxFONTENCODING_EUC_JP
:
819 case wxFONTENCODING_SHIFT_JIS
:
820 info
->xregistry
= "jis*";
821 info
->xencoding
= "*";
824 case wxFONTENCODING_SYSTEM
:
826 info
->xencoding
= wxT("*");
830 // don't know how to translate this encoding into X fontspec
834 info
->encoding
= encoding
;
839 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
842 fontspec
.Printf(_T("-*-%s-*-*-*-*-*-*-*-*-*-*-%s-%s"),
843 !info
.facename
? _T("*") : info
.facename
.c_str(),
844 info
.xregistry
.c_str(),
845 info
.xencoding
.c_str());
847 return wxTestFontSpec(fontspec
);
850 // ----------------------------------------------------------------------------
851 // X-specific functions
852 // ----------------------------------------------------------------------------
854 wxNativeFont
wxLoadQueryNearestFont(int pointSize
,
859 const wxString
&facename
,
860 wxFontEncoding encoding
,
863 if ( encoding
== wxFONTENCODING_DEFAULT
)
865 encoding
= wxFont::GetDefaultEncoding();
868 // first determine the encoding - if the font doesn't exist at all in this
869 // encoding, it's useless to do all other approximations (i.e. size,
870 // family &c don't matter much)
871 wxNativeEncodingInfo info
;
872 if ( encoding
== wxFONTENCODING_SYSTEM
)
874 // This will always work so we don't test to save time
875 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
879 if ( !wxGetNativeFontEncoding(encoding
, &info
) ||
880 !wxTestFontEncoding(info
) )
883 if ( !wxFontMapper::Get()->GetAltForEncoding(encoding
, &info
) )
884 #endif // wxUSE_FONTMAP
886 // unspported encoding - replace it with the default
888 // NB: we can't just return 0 from here because wxGTK code doesn't
889 // check for it (i.e. it supposes that we'll always succeed),
890 // so it would provoke a crash
891 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
896 // OK, we have the correct xregistry/xencoding in info structure
897 wxNativeFont font
= 0;
899 // if we already have the X font name, try to use it
900 if( xFontName
&& !xFontName
->IsEmpty() )
903 // Make sure point size is correct for scale factor.
905 wxStringTokenizer
tokenizer(*xFontName
, _T("-"), wxTOKEN_RET_DELIMS
);
906 wxString newFontName
;
908 for(int i
= 0; i
< 8; i
++)
909 newFontName
+= tokenizer
.NextToken();
911 (void) tokenizer
.NextToken();
913 newFontName
+= wxString::Format(wxT("%d-"), pointSize
);
915 while(tokenizer
.HasMoreTokens())
916 newFontName
+= tokenizer
.GetNextToken();
918 font
= wxLoadFont(newFontName
);
921 *xFontName
= newFontName
;
926 // search up and down by stepsize 10
927 int max_size
= pointSize
+ 20 * (1 + (pointSize
/180));
928 int min_size
= pointSize
- 20 * (1 + (pointSize
/180));
930 int i
, round
; // counters
932 // first round: search for equal, then for smaller and for larger size with the given weight and style
933 int testweight
= weight
;
934 int teststyle
= style
;
936 for ( round
= 0; round
< 3; round
++ )
938 // second round: use normal weight
941 if ( testweight
!= wxNORMAL
)
943 testweight
= wxNORMAL
;
947 ++round
; // fall through to third round
951 // third round: ... and use normal style
954 if ( teststyle
!= wxNORMAL
)
956 teststyle
= wxNORMAL
;
963 // Search for equal or smaller size (approx.)
964 for ( i
= pointSize
; !font
&& i
>= 10 && i
>= min_size
; i
-= 10 )
966 font
= wxLoadQueryFont(i
, family
, teststyle
, testweight
, underlined
,
967 facename
, info
.xregistry
, info
.xencoding
,
971 // Search for larger size (approx.)
972 for ( i
= pointSize
+ 10; !font
&& i
<= max_size
; i
+= 10 )
974 font
= wxLoadQueryFont(i
, family
, teststyle
, testweight
, underlined
,
975 facename
, info
.xregistry
, info
.xencoding
,
980 // Try default family
981 if ( !font
&& family
!= wxDEFAULT
)
983 font
= wxLoadQueryFont(pointSize
, wxDEFAULT
, style
, weight
,
984 underlined
, facename
,
985 info
.xregistry
, info
.xencoding
,
989 // ignore size, family, style and weight but try to find font with the
990 // given facename and encoding
993 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
994 underlined
, facename
,
995 info
.xregistry
, info
.xencoding
,
998 // ignore family as well
1001 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1002 underlined
, wxEmptyString
,
1003 info
.xregistry
, info
.xencoding
,
1006 // if it still failed, try to get the font of any size but
1007 // with the requested encoding: this can happen if the
1008 // encoding is only available in one size which happens to be
1009 // different from 120
1012 font
= wxLoadQueryFont(-1, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1013 FALSE
, wxEmptyString
,
1014 info
.xregistry
, info
.xencoding
,
1017 // this should never happen as we had tested for it in the
1018 // very beginning, but if it does, do return something non
1019 // NULL or we'd crash in wxFont code
1022 wxFAIL_MSG( _T("this encoding should be available!") );
1024 font
= wxLoadQueryFont(-1,
1025 wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1026 FALSE
, wxEmptyString
,
1038 // ----------------------------------------------------------------------------
1039 // private functions
1040 // ----------------------------------------------------------------------------
1042 // returns TRUE if there are any fonts matching this font spec
1043 static bool wxTestFontSpec(const wxString
& fontspec
)
1045 // some X servers will fail to load this font because there are too many
1046 // matches so we must test explicitly for this
1047 if ( fontspec
== _T("-*-*-*-*-*-*-*-*-*-*-*-*-*-*") )
1052 wxNativeFont test
= (wxNativeFont
) g_fontHash
->Get( fontspec
);
1058 test
= wxLoadFont(fontspec
);
1059 g_fontHash
->Put( fontspec
, (wxObject
*) test
);
1073 static wxNativeFont
wxLoadQueryFont(int pointSize
,
1077 bool WXUNUSED(underlined
),
1078 const wxString
& facename
,
1079 const wxString
& xregistry
,
1080 const wxString
& xencoding
,
1081 wxString
* xFontName
)
1086 case wxDECORATIVE
: xfamily
= wxT("lucida"); break;
1087 case wxROMAN
: xfamily
= wxT("times"); break;
1088 case wxMODERN
: xfamily
= wxT("courier"); break;
1089 case wxSWISS
: xfamily
= wxT("helvetica"); break;
1090 case wxTELETYPE
: xfamily
= wxT("lucidatypewriter"); break;
1091 case wxSCRIPT
: xfamily
= wxT("utopia"); break;
1092 default: xfamily
= wxT("*");
1100 xweight
= MWLF_WEIGHT_BOLD
;
1105 xweight
= MWLF_WEIGHT_LIGHT
;
1110 xweight
= MWLF_WEIGHT_NORMAL
;
1116 xweight
= MWLF_WEIGHT_DEFAULT
;
1120 GR_SCREEN_INFO screenInfo
;
1121 GrGetScreenInfo(& screenInfo
);
1123 int yPixelsPerCM
= screenInfo
.ydpcm
;
1125 // A point is 1/72 of an inch.
1126 // An inch is 2.541 cm.
1127 // So pixelHeight = (pointSize / 72) (inches) * 2.541 (for cm) * yPixelsPerCM (for pixels)
1128 // In fact pointSize is 10 * the normal point size so
1131 int pixelHeight
= (int) ( (((float)pointSize
) / 720.0) * 2.541 * (float) yPixelsPerCM
) ;
1133 // An alternative: assume that the screen is 72 dpi.
1134 //int pixelHeight = (int) (((float)pointSize / 720.0) * 72.0) ;
1135 //int pixelHeight = (int) ((float)pointSize / 10.0) ;
1138 logFont
.lfHeight
= pixelHeight
;
1139 logFont
.lfWidth
= 0;
1140 logFont
.lfEscapement
= 0;
1141 logFont
.lfOrientation
= 0;
1142 logFont
.lfWeight
= xweight
;
1143 logFont
.lfItalic
= (style
== wxNORMAL
? 0 : 1) ;
1144 logFont
.lfUnderline
= 0;
1145 logFont
.lfStrikeOut
= 0;
1146 logFont
.lfCharSet
= MWLF_CHARSET_DEFAULT
; // TODO: select appropriate one
1147 logFont
.lfOutPrecision
= MWLF_TYPE_DEFAULT
;
1148 logFont
.lfClipPrecision
= 0; // Not used
1149 logFont
.lfRoman
= (family
== wxROMAN
? 1 : 0) ;
1150 logFont
.lfSerif
= (family
== wxSWISS
? 0 : 1) ;
1151 logFont
.lfSansSerif
= !logFont
.lfSerif
;
1152 logFont
.lfModern
= (family
== wxMODERN
? 1 : 0) ;
1153 logFont
.lfProportional
= (family
== wxTELETYPE
? 0 : 1) ;
1154 logFont
.lfOblique
= 0;
1155 logFont
.lfSmallCaps
= 0;
1156 logFont
.lfPitch
= 0; // 0 = default
1157 strcpy(logFont
.lfFaceName
, facename
.c_str());
1159 XFontStruct
* fontInfo
= (XFontStruct
*) malloc(sizeof(XFontStruct
));
1160 fontInfo
->fid
= GrCreateFont((GR_CHAR
*) facename
.c_str(), pixelHeight
, & logFont
);
1161 GrGetFontInfo(fontInfo
->fid
, & fontInfo
->info
);
1162 return (wxNativeFont
) fontInfo
;
1166 if (!facename
.IsEmpty())
1168 fontSpec
.Printf(wxT("-*-%s-*-*-normal-*-*-*-*-*-*-*-*-*"),
1171 if ( wxTestFontSpec(fontSpec
) )
1175 //else: no such family, use default one instead
1182 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1184 if ( wxTestFontSpec(fontSpec
) )
1189 // fall through - try wxITALIC now
1192 fontSpec
.Printf(wxT("-*-%s-*-i-*-*-*-*-*-*-*-*-*-*"),
1194 if ( wxTestFontSpec(fontSpec
) )
1198 else if ( style
== wxITALIC
) // and not wxSLANT
1201 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1203 if ( wxTestFontSpec(fontSpec
) )
1209 // no italic, no slant - leave default
1216 wxFAIL_MSG(_T("unknown font style"));
1217 // fall back to normal
1229 fontSpec
.Printf(wxT("-*-%s-bold-*-*-*-*-*-*-*-*-*-*-*"),
1231 if ( wxTestFontSpec(fontSpec
) )
1233 xweight
= wxT("bold");
1236 fontSpec
.Printf(wxT("-*-%s-heavy-*-*-*-*-*-*-*-*-*-*-*"),
1238 if ( wxTestFontSpec(fontSpec
) )
1240 xweight
= wxT("heavy");
1243 fontSpec
.Printf(wxT("-*-%s-extrabold-*-*-*-*-*-*-*-*-*-*-*"),
1245 if ( wxTestFontSpec(fontSpec
) )
1247 xweight
= wxT("extrabold");
1250 fontSpec
.Printf(wxT("-*-%s-demibold-*-*-*-*-*-*-*-*-*-*-*"),
1252 if ( wxTestFontSpec(fontSpec
) )
1254 xweight
= wxT("demibold");
1257 fontSpec
.Printf(wxT("-*-%s-black-*-*-*-*-*-*-*-*-*-*-*"),
1259 if ( wxTestFontSpec(fontSpec
) )
1261 xweight
= wxT("black");
1264 fontSpec
.Printf(wxT("-*-%s-ultrablack-*-*-*-*-*-*-*-*-*-*-*"),
1266 if ( wxTestFontSpec(fontSpec
) )
1268 xweight
= wxT("ultrablack");
1275 fontSpec
.Printf(wxT("-*-%s-light-*-*-*-*-*-*-*-*-*-*-*"),
1277 if ( wxTestFontSpec(fontSpec
) )
1279 xweight
= wxT("light");
1282 fontSpec
.Printf(wxT("-*-%s-thin-*-*-*-*-*-*-*-*-*-*-*"),
1284 if ( wxTestFontSpec(fontSpec
) )
1286 xweight
= wxT("thin");
1293 fontSpec
.Printf(wxT("-*-%s-medium-*-*-*-*-*-*-*-*-*-*-*"),
1295 if ( wxTestFontSpec(fontSpec
) )
1297 xweight
= wxT("medium");
1300 fontSpec
.Printf(wxT("-*-%s-normal-*-*-*-*-*-*-*-*-*-*-*"),
1302 if ( wxTestFontSpec(fontSpec
) )
1304 xweight
= wxT("normal");
1307 fontSpec
.Printf(wxT("-*-%s-regular-*-*-*-*-*-*-*-*-*-*-*"),
1309 if ( wxTestFontSpec(fontSpec
) )
1311 xweight
= wxT("regular");
1317 default: xweight
= wxT("*"); break;
1320 // if pointSize is -1, don't specify any
1322 if ( pointSize
== -1 )
1328 sizeSpec
.Printf(_T("%d"), pointSize
);
1331 // construct the X font spec from our data
1332 fontSpec
.Printf(wxT("-*-%s-%s-%s-normal-*-*-%s-*-*-*-*-%s-%s"),
1333 xfamily
.c_str(), xweight
.c_str(), xstyle
.c_str(),
1334 sizeSpec
.c_str(), xregistry
.c_str(), xencoding
.c_str());
1337 *xFontName
= fontSpec
;
1339 return wxLoadFont(fontSpec
);
1344 // ----------------------------------------------------------------------------
1346 // ----------------------------------------------------------------------------
1348 class wxFontModule
: public wxModule
1355 DECLARE_DYNAMIC_CLASS(wxFontModule
)
1358 IMPLEMENT_DYNAMIC_CLASS(wxFontModule
, wxModule
)
1360 bool wxFontModule::OnInit()
1362 g_fontHash
= new wxHashTable( wxKEY_STRING
);
1367 void wxFontModule::OnExit()
1371 g_fontHash
= (wxHashTable
*)NULL
;
1374 #endif // GTK 2.0/1.x