1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/unix/fontutil.cpp
3 // Purpose: Font helper functions for wxX11, wxGTK, wxMotif
4 // Author: Vadim Zeitlin
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #include "wx/fontutil.h"
31 #include "wx/font.h" // wxFont enums
32 #include "wx/encinfo.h"
34 #include "wx/utils.h" // for wxGetDisplay()
35 #include "wx/module.h"
38 #include "wx/fontmap.h"
39 #include "wx/tokenzr.h"
40 #include "wx/fontenum.h"
44 #include "pango/pango.h"
47 #include "wx/gtk/private.h"
48 extern GtkWidget
*wxGetRootWindow();
50 #define wxPANGO_CONV wxGTK_CONV_SYS
51 #define wxPANGO_CONV_BACK wxGTK_CONV_BACK_SYS
53 #include "wx/x11/private.h"
54 #include "wx/gtk/private/string.h"
56 #define wxPANGO_CONV(s) s.utf8_str()
57 #define wxPANGO_CONV_BACK(s) wxString::FromUTF8Unchecked(s)
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
64 void wxNativeFontInfo::Init()
69 void wxNativeFontInfo::Init(const wxNativeFontInfo
& info
)
72 description
= pango_font_description_copy(info
.description
);
77 void wxNativeFontInfo::Free()
80 pango_font_description_free(description
);
83 int wxNativeFontInfo::GetPointSize() const
85 return pango_font_description_get_size( description
) / PANGO_SCALE
;
88 wxFontStyle
wxNativeFontInfo::GetStyle() const
90 wxFontStyle m_style
= wxFONTSTYLE_NORMAL
;
92 switch (pango_font_description_get_style( description
))
94 case PANGO_STYLE_NORMAL
:
95 m_style
= wxFONTSTYLE_NORMAL
;
97 case PANGO_STYLE_ITALIC
:
98 m_style
= wxFONTSTYLE_ITALIC
;
100 case PANGO_STYLE_OBLIQUE
:
101 m_style
= wxFONTSTYLE_SLANT
;
108 wxFontWeight
wxNativeFontInfo::GetWeight() const
110 // We seem to currently initialize only by string.
111 // In that case PANGO_FONT_MASK_WEIGHT is always set.
112 // if (!(pango_font_description_get_set_fields(description) & PANGO_FONT_MASK_WEIGHT))
113 // return wxFONTWEIGHT_NORMAL;
115 PangoWeight pango_weight
= pango_font_description_get_weight( description
);
117 // Until the API can be changed the following ranges of weight values are used:
118 // wxFONTWEIGHT_LIGHT: 100 .. 349 - range of 250
119 // wxFONTWEIGHT_NORMAL: 350 .. 599 - range of 250
120 // wxFONTWEIGHT_BOLD: 600 .. 900 - range of 301 (600 is "semibold" already)
122 if (pango_weight
>= 600)
123 return wxFONTWEIGHT_BOLD
;
125 if (pango_weight
< 350)
126 return wxFONTWEIGHT_LIGHT
;
128 return wxFONTWEIGHT_NORMAL
;
131 bool wxNativeFontInfo::GetUnderlined() const
136 bool wxNativeFontInfo::GetStrikethrough() const
141 wxString
wxNativeFontInfo::GetFaceName() const
143 // the Pango "family" is the wx "face name"
144 return wxPANGO_CONV_BACK(pango_font_description_get_family(description
));
147 wxFontFamily
wxNativeFontInfo::GetFamily() const
149 wxFontFamily ret
= wxFONTFAMILY_UNKNOWN
;
151 const char *family_name
= pango_font_description_get_family( description
);
153 // note: not passing -1 as the 2nd parameter to g_ascii_strdown to work
154 // around a bug in the 64-bit glib shipped with solaris 10, -1 causes it
155 // to try to allocate 2^32 bytes.
158 wxGtkString
family_text(g_ascii_strdown(family_name
, strlen(family_name
)));
160 // Check for some common fonts, to salvage what we can from the current
161 // win32 centric wxFont API:
162 if (wxStrnicmp( family_text
, "monospace", 9 ) == 0)
163 ret
= wxFONTFAMILY_TELETYPE
; // begins with "Monospace"
164 else if (wxStrnicmp( family_text
, "courier", 7 ) == 0)
165 ret
= wxFONTFAMILY_TELETYPE
; // begins with "Courier"
166 #if defined(__WXGTK20__) || defined(HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE)
169 PangoFontFamily
**families
;
170 PangoFontFamily
*family
= NULL
;
172 pango_context_list_families(
174 gtk_widget_get_pango_context( wxGetRootWindow() ),
176 wxTheApp
->GetPangoContext(),
178 &families
, &n_families
);
180 for (int i
= 0; i
< n_families
; ++i
)
182 if (g_ascii_strcasecmp(pango_font_family_get_name( families
[i
] ),
183 pango_font_description_get_family( description
)) == 0 )
185 family
= families
[i
];
192 // Some gtk+ systems might query for a non-existing font from
193 // wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT) on initialization,
194 // don't assert until wxSystemSettings::GetFont is checked for this - MR
195 // wxASSERT_MSG( family, "No appropriate PangoFontFamily found for ::description" );
197 if (family
!= NULL
&& pango_font_family_is_monospace( family
))
198 ret
= wxFONTFAMILY_TELETYPE
; // is deemed a monospace font by pango
200 #endif // GTK+ 2 || HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE
202 if (ret
== wxFONTFAMILY_UNKNOWN
)
204 if (strstr( family_text
, "sans" ) != NULL
|| strstr( family_text
, "Sans" ) != NULL
)
205 // checked before serif, so that "* Sans Serif" fonts are detected correctly
206 ret
= wxFONTFAMILY_SWISS
; // contains "Sans"
207 else if (strstr( family_text
, "serif" ) != NULL
|| strstr( family_text
, "Serif" ) != NULL
)
208 ret
= wxFONTFAMILY_ROMAN
; // contains "Serif"
209 else if (wxStrnicmp( family_text
, "times", 5 ) == 0)
210 ret
= wxFONTFAMILY_ROMAN
; // begins with "Times"
211 else if (wxStrnicmp( family_text
, "old", 3 ) == 0)
212 ret
= wxFONTFAMILY_DECORATIVE
; // begins with "Old" - "Old English", "Old Town"
218 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
220 return wxFONTENCODING_SYSTEM
;
223 void wxNativeFontInfo::SetPointSize(int pointsize
)
225 pango_font_description_set_size( description
, pointsize
* PANGO_SCALE
);
228 void wxNativeFontInfo::SetStyle(wxFontStyle style
)
232 case wxFONTSTYLE_ITALIC
:
233 pango_font_description_set_style( description
, PANGO_STYLE_ITALIC
);
235 case wxFONTSTYLE_SLANT
:
236 pango_font_description_set_style( description
, PANGO_STYLE_OBLIQUE
);
239 wxFAIL_MSG( "unknown font style" );
241 case wxFONTSTYLE_NORMAL
:
242 pango_font_description_set_style( description
, PANGO_STYLE_NORMAL
);
247 void wxNativeFontInfo::SetWeight(wxFontWeight weight
)
251 case wxFONTWEIGHT_BOLD
:
252 pango_font_description_set_weight(description
, PANGO_WEIGHT_BOLD
);
254 case wxFONTWEIGHT_LIGHT
:
255 pango_font_description_set_weight(description
, PANGO_WEIGHT_LIGHT
);
258 wxFAIL_MSG( "unknown font weight" );
260 case wxFONTWEIGHT_NORMAL
:
261 pango_font_description_set_weight(description
, PANGO_WEIGHT_NORMAL
);
265 void wxNativeFontInfo::SetUnderlined(bool WXUNUSED(underlined
))
267 // wxWindowDCImpl::DoDrawText will take care of rendering font with
268 // the underline attribute!
269 wxFAIL_MSG( "not implemented" );
272 void wxNativeFontInfo::SetStrikethrough(bool WXUNUSED(strikethrough
))
274 wxFAIL_MSG( "not implemented" );
277 bool wxNativeFontInfo::SetFaceName(const wxString
& facename
)
279 pango_font_description_set_family(description
, wxPANGO_CONV(facename
));
281 // we return true because Pango doesn't tell us if the call failed or not;
282 // instead on wxGTK wxFont::SetFaceName() will call wxFontBase::SetFaceName()
283 // which does the check
287 void wxNativeFontInfo::SetFamily(wxFontFamily family
)
289 wxArrayString facename
;
291 // the list of fonts associated with a family was partially
292 // taken from http://www.codestyle.org/css/font-family
296 case wxFONTFAMILY_SCRIPT
:
297 // corresponds to the cursive font family in the page linked above
298 facename
.Add(wxS("URW Chancery L"));
299 facename
.Add(wxS("Comic Sans MS"));
302 case wxFONTFAMILY_DECORATIVE
:
303 // corresponds to the fantasy font family in the page linked above
304 facename
.Add(wxS("Impact"));
307 case wxFONTFAMILY_ROMAN
:
308 // corresponds to the serif font family in the page linked above
309 facename
.Add(wxS("Century Schoolbook L"));
310 facename
.Add(wxS("URW Bookman L"));
311 facename
.Add(wxS("URW Palladio L"));
312 facename
.Add(wxS("DejaVu Serif"));
313 facename
.Add(wxS("FreeSerif"));
314 facename
.Add(wxS("Times New Roman"));
315 facename
.Add(wxS("Times"));
318 case wxFONTFAMILY_TELETYPE
:
319 case wxFONTFAMILY_MODERN
:
320 // corresponds to the monospace font family in the page linked above
321 facename
.Add(wxS("DejaVu Sans Mono"));
322 facename
.Add(wxS("Nimbus Mono L"));
323 facename
.Add(wxS("Bitstream Vera Sans Mono"));
324 facename
.Add(wxS("Andale Mono"));
325 facename
.Add(wxS("Lucida Sans Typewriter"));
326 facename
.Add(wxS("FreeMono"));
327 facename
.Add(wxS("Courier New"));
328 facename
.Add(wxS("Courier"));
331 case wxFONTFAMILY_SWISS
:
332 case wxFONTFAMILY_DEFAULT
:
334 // corresponds to the sans-serif font family in the page linked above
335 facename
.Add(wxS("DejaVu Sans"));
336 facename
.Add(wxS("URW Gothic L"));
337 facename
.Add(wxS("Nimbus Sans L"));
338 facename
.Add(wxS("Bitstream Vera Sans"));
339 facename
.Add(wxS("Lucida Sans"));
340 facename
.Add(wxS("Arial"));
341 facename
.Add(wxS("FreeSans"));
345 SetFaceName(facename
);
348 void wxNativeFontInfo::SetEncoding(wxFontEncoding
WXUNUSED(encoding
))
350 wxFAIL_MSG( "not implemented: Pango encoding is always UTF8" );
353 bool wxNativeFontInfo::FromString(const wxString
& s
)
356 pango_font_description_free( description
);
358 // there is a bug in at least pango <= 1.13 which makes it (or its backends)
359 // segfault for very big point sizes and for negative point sizes.
360 // To workaround that bug for pango <= 1.13
361 // (see http://bugzilla.gnome.org/show_bug.cgi?id=340229)
362 // we do the check on the size here using same (arbitrary) limits used by
363 // pango > 1.13. Note that the segfault could happen also for pointsize
364 // smaller than this limit !!
366 const size_t pos
= str
.find_last_of(wxS(" "));
368 if ( pos
!= wxString::npos
&& wxString(str
, pos
+ 1).ToDouble(&size
) )
373 else if ( size
>= 1E6
)
374 sizeStr
= wxS("1E6");
376 if ( !sizeStr
.empty() )
378 // replace the old size with the adjusted one
379 str
= wxString(s
, 0, pos
) + sizeStr
;
383 description
= pango_font_description_from_string(wxPANGO_CONV(str
));
386 // ensure a valid facename is selected
387 if (!wxFontEnumerator::IsValidFacename(GetFaceName()))
388 SetFaceName(wxNORMAL_FONT
->GetFaceName());
389 #endif // wxUSE_FONTENUM
394 wxString
wxNativeFontInfo::ToString() const
396 wxGtkString
str(pango_font_description_to_string( description
));
398 return wxPANGO_CONV_BACK(str
);
401 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
403 return FromString( s
);
406 wxString
wxNativeFontInfo::ToUserString() const
415 #pragma message disable nosimpint
418 #include <X11/Xlib.h>
421 #pragma message enable nosimpint
424 #elif defined(__WXGTK__)
425 // we have to declare struct tm to avoid problems with first forward
426 // declaring it in C code (glib.h included from gdk.h does it) and then
427 // defining it when time.h is included from the headers below - this is
428 // known not to work at least with Sun CC 6.01
435 // ----------------------------------------------------------------------------
437 // ----------------------------------------------------------------------------
439 static wxHashTable
*g_fontHash
= NULL
;
441 // ----------------------------------------------------------------------------
443 // ----------------------------------------------------------------------------
445 // define the functions to create and destroy native fonts for this toolkit
447 wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
449 return XLoadQueryFont((Display
*)wxGetDisplay(), fontSpec
);
452 inline void wxFreeFont(wxNativeFont font
)
454 XFreeFont((Display
*)wxGetDisplay(), (XFontStruct
*)font
);
456 #elif defined(__WXGTK__)
457 wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
459 // VZ: we should use gdk_fontset_load() instead of gdk_font_load()
460 // here to be able to display Japanese fonts correctly (at least
461 // this is what people report) but unfortunately doing it results
462 // in tons of warnings when using GTK with "normal" European
463 // languages and so we can't always do it and I don't know enough
464 // to determine when should this be done... (FIXME)
465 return gdk_font_load( wxConvertWX2MB(fontSpec
) );
468 inline void wxFreeFont(wxNativeFont font
)
470 gdk_font_unref(font
);
473 #error "Unknown GUI toolkit"
476 static bool wxTestFontSpec(const wxString
& fontspec
);
478 static wxNativeFont
wxLoadQueryFont(int pointSize
,
483 const wxString
& facename
,
484 const wxString
& xregistry
,
485 const wxString
& xencoding
,
486 wxString
* xFontName
);
488 // ============================================================================
490 // ============================================================================
492 // ----------------------------------------------------------------------------
493 // wxNativeEncodingInfo
494 // ----------------------------------------------------------------------------
496 // convert to/from the string representation: format is
497 // encodingid;registry;encoding[;facename]
498 bool wxNativeEncodingInfo::FromString(const wxString
& s
)
500 // use ";", not "-" because it may be part of encoding name
501 wxStringTokenizer
tokenizer(s
, wxT(";"));
503 wxString encid
= tokenizer
.GetNextToken();
505 if ( !encid
.ToLong(&enc
) )
507 encoding
= (wxFontEncoding
)enc
;
509 xregistry
= tokenizer
.GetNextToken();
513 xencoding
= tokenizer
.GetNextToken();
518 facename
= tokenizer
.GetNextToken();
523 wxString
wxNativeEncodingInfo::ToString() const
526 s
<< (long)encoding
<< wxT(';') << xregistry
<< wxT(';') << xencoding
;
527 if ( !facename
.empty() )
529 s
<< wxT(';') << facename
;
535 // ----------------------------------------------------------------------------
537 // ----------------------------------------------------------------------------
539 void wxNativeFontInfo::Init()
544 bool wxNativeFontInfo::FromString(const wxString
& s
)
546 wxStringTokenizer
tokenizer(s
, wxT(";"));
549 wxString token
= tokenizer
.GetNextToken();
550 if ( token
!= wxT('0') )
553 xFontName
= tokenizer
.GetNextToken();
555 // this should be the end
556 if ( tokenizer
.HasMoreTokens() )
559 return FromXFontName(xFontName
);
562 wxString
wxNativeFontInfo::ToString() const
565 return wxString::Format(wxT("%d;%s"), 0, GetXFontName().c_str());
568 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
570 return FromXFontName(s
);
573 wxString
wxNativeFontInfo::ToUserString() const
575 return GetXFontName();
578 bool wxNativeFontInfo::HasElements() const
580 // we suppose that the foundry is never empty, so if it is it means that we
581 // had never parsed the XLFD
582 return !fontElements
[0].empty();
585 wxString
wxNativeFontInfo::GetXFontComponent(wxXLFDField field
) const
587 wxCHECK_MSG( field
< wxXLFD_MAX
, wxEmptyString
, wxT("invalid XLFD field") );
589 if ( !HasElements() )
591 if ( !const_cast<wxNativeFontInfo
*>(this)->FromXFontName(xFontName
) )
592 return wxEmptyString
;
595 return fontElements
[field
];
598 bool wxNativeFontInfo::FromXFontName(const wxString
& fontname
)
600 // TODO: we should be able to handle the font aliases here, but how?
601 wxStringTokenizer
tokenizer(fontname
, wxT("-"));
603 // skip the leading, usually empty field (font name registry)
604 if ( !tokenizer
.HasMoreTokens() )
607 (void)tokenizer
.GetNextToken();
609 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
611 if ( !tokenizer
.HasMoreTokens() )
613 // not enough elements in the XLFD - or maybe an alias
617 wxString field
= tokenizer
.GetNextToken();
618 if ( !field
.empty() && field
!= wxT('*') )
620 // we're really initialized now
624 fontElements
[n
] = field
;
627 // this should be all
628 if ( tokenizer
.HasMoreTokens() )
634 wxString
wxNativeFontInfo::GetXFontName() const
636 if ( xFontName
.empty() )
638 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
640 // replace the non specified elements with '*' except for the
641 // additional style which is usually just omitted
642 wxString elt
= fontElements
[n
];
643 if ( elt
.empty() && n
!= wxXLFD_ADDSTYLE
)
648 const_cast<wxNativeFontInfo
*>(this)->xFontName
<< wxT('-') << elt
;
656 wxNativeFontInfo::SetXFontComponent(wxXLFDField field
, const wxString
& value
)
658 wxCHECK_RET( field
< wxXLFD_MAX
, wxT("invalid XLFD field") );
660 // this class should be initialized with a valid font spec first and only
661 // then the fields may be modified!
662 wxASSERT_MSG( !IsDefault(), wxT("can't modify an uninitialized XLFD") );
664 if ( !HasElements() )
666 if ( !const_cast<wxNativeFontInfo
*>(this)->FromXFontName(xFontName
) )
668 wxFAIL_MSG( wxT("can't set font element for invalid XLFD") );
674 fontElements
[field
] = value
;
676 // invalidate the XFLD, it doesn't correspond to the font elements any more
680 void wxNativeFontInfo::SetXFontName(const wxString
& xFontName_
)
682 // invalidate the font elements, GetXFontComponent() will reparse the XLFD
683 fontElements
[0].clear();
685 xFontName
= xFontName_
;
690 int wxNativeFontInfo::GetPointSize() const
692 const wxString s
= GetXFontComponent(wxXLFD_POINTSIZE
);
694 // return -1 to indicate that the size is unknown
696 return s
.ToLong(&l
) ? l
: -1;
699 wxFontStyle
wxNativeFontInfo::GetStyle() const
701 const wxString s
= GetXFontComponent(wxXLFD_SLANT
);
703 if ( s
.length() != 1 )
705 // it is really unknown but we don't have any way to return it from
707 return wxFONTSTYLE_NORMAL
;
710 switch ( s
[0].GetValue() )
713 // again, unknown but consider normal by default
716 return wxFONTSTYLE_NORMAL
;
719 return wxFONTSTYLE_ITALIC
;
722 return wxFONTSTYLE_SLANT
;
726 wxFontWeight
wxNativeFontInfo::GetWeight() const
728 const wxString s
= GetXFontComponent(wxXLFD_WEIGHT
).MakeLower();
729 if ( s
.find(wxT("bold")) != wxString::npos
|| s
== wxT("black") )
730 return wxFONTWEIGHT_BOLD
;
731 else if ( s
== wxT("light") )
732 return wxFONTWEIGHT_LIGHT
;
734 return wxFONTWEIGHT_NORMAL
;
737 bool wxNativeFontInfo::GetUnderlined() const
739 // X fonts are never underlined
743 wxString
wxNativeFontInfo::GetFaceName() const
745 // wxWidgets facename probably more accurately corresponds to X family
746 return GetXFontComponent(wxXLFD_FAMILY
);
749 wxFontFamily
wxNativeFontInfo::GetFamily() const
751 // and wxWidgets family -- to X foundry, but we have to translate it to
752 // wxFontFamily somehow...
753 wxFAIL_MSG(wxT("not implemented")); // GetXFontComponent(wxXLFD_FOUNDRY);
755 return wxFONTFAMILY_DEFAULT
;
758 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
760 // we already have the code for this but need to refactor it first
761 wxFAIL_MSG( wxT("not implemented") );
763 return wxFONTENCODING_MAX
;
766 void wxNativeFontInfo::SetPointSize(int pointsize
)
768 SetXFontComponent(wxXLFD_POINTSIZE
, wxString::Format(wxT("%d"), pointsize
));
771 void wxNativeFontInfo::SetStyle(wxFontStyle style
)
776 case wxFONTSTYLE_ITALIC
:
780 case wxFONTSTYLE_SLANT
:
784 case wxFONTSTYLE_NORMAL
:
788 wxFAIL_MSG( wxT("unknown wxFontStyle in wxNativeFontInfo::SetStyle") );
792 SetXFontComponent(wxXLFD_SLANT
, s
);
795 void wxNativeFontInfo::SetWeight(wxFontWeight weight
)
800 case wxFONTWEIGHT_BOLD
:
804 case wxFONTWEIGHT_LIGHT
:
808 case wxFONTWEIGHT_NORMAL
:
813 wxFAIL_MSG( wxT("unknown wxFontWeight in wxNativeFontInfo::SetWeight") );
817 SetXFontComponent(wxXLFD_WEIGHT
, s
);
820 void wxNativeFontInfo::SetUnderlined(bool WXUNUSED(underlined
))
822 // can't do this under X
825 void wxNativeFontInfo::SetStrikethrough(bool WXUNUSED(strikethrough
))
827 // this is not supported by Pango fonts neither
830 bool wxNativeFontInfo::SetFaceName(const wxString
& facename
)
832 SetXFontComponent(wxXLFD_FAMILY
, facename
);
836 void wxNativeFontInfo::SetFamily(wxFontFamily
WXUNUSED(family
))
838 // wxFontFamily -> X foundry, anyone?
839 wxFAIL_MSG( wxT("not implemented") );
841 // SetXFontComponent(wxXLFD_FOUNDRY, ...);
844 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding
)
846 wxNativeEncodingInfo info
;
847 if ( wxGetNativeFontEncoding(encoding
, &info
) )
849 SetXFontComponent(wxXLFD_ENCODING
, info
.xencoding
);
850 SetXFontComponent(wxXLFD_REGISTRY
, info
.xregistry
);
854 // ----------------------------------------------------------------------------
856 // ----------------------------------------------------------------------------
858 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
859 wxNativeEncodingInfo
*info
)
861 wxCHECK_MSG( info
, false, wxT("bad pointer in wxGetNativeFontEncoding") );
863 if ( encoding
== wxFONTENCODING_DEFAULT
)
865 encoding
= wxFont::GetDefaultEncoding();
870 case wxFONTENCODING_ISO8859_1
:
871 case wxFONTENCODING_ISO8859_2
:
872 case wxFONTENCODING_ISO8859_3
:
873 case wxFONTENCODING_ISO8859_4
:
874 case wxFONTENCODING_ISO8859_5
:
875 case wxFONTENCODING_ISO8859_6
:
876 case wxFONTENCODING_ISO8859_7
:
877 case wxFONTENCODING_ISO8859_8
:
878 case wxFONTENCODING_ISO8859_9
:
879 case wxFONTENCODING_ISO8859_10
:
880 case wxFONTENCODING_ISO8859_11
:
881 case wxFONTENCODING_ISO8859_12
:
882 case wxFONTENCODING_ISO8859_13
:
883 case wxFONTENCODING_ISO8859_14
:
884 case wxFONTENCODING_ISO8859_15
:
886 int cp
= encoding
- wxFONTENCODING_ISO8859_1
+ 1;
887 info
->xregistry
= wxT("iso8859");
888 info
->xencoding
.Printf(wxT("%d"), cp
);
892 case wxFONTENCODING_UTF8
:
893 info
->xregistry
= wxT("iso10646");
894 info
->xencoding
= wxT("*");
897 case wxFONTENCODING_GB2312
:
898 info
->xregistry
= wxT("GB2312"); // or the otherway round?
899 info
->xencoding
= wxT("*");
902 case wxFONTENCODING_KOI8
:
903 case wxFONTENCODING_KOI8_U
:
904 info
->xregistry
= wxT("koi8");
906 // we don't make distinction between koi8-r, koi8-u and koi8-ru (so far)
907 info
->xencoding
= wxT("*");
910 case wxFONTENCODING_CP1250
:
911 case wxFONTENCODING_CP1251
:
912 case wxFONTENCODING_CP1252
:
913 case wxFONTENCODING_CP1253
:
914 case wxFONTENCODING_CP1254
:
915 case wxFONTENCODING_CP1255
:
916 case wxFONTENCODING_CP1256
:
917 case wxFONTENCODING_CP1257
:
919 int cp
= encoding
- wxFONTENCODING_CP1250
+ 1250;
920 info
->xregistry
= wxT("microsoft");
921 info
->xencoding
.Printf(wxT("cp%d"), cp
);
925 case wxFONTENCODING_EUC_JP
:
926 case wxFONTENCODING_SHIFT_JIS
:
927 info
->xregistry
= "jis*";
928 info
->xencoding
= "*";
931 case wxFONTENCODING_SYSTEM
:
933 info
->xencoding
= wxT("*");
937 // don't know how to translate this encoding into X fontspec
941 info
->encoding
= encoding
;
946 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
949 fontspec
.Printf(wxT("-*-%s-*-*-*-*-*-*-*-*-*-*-%s-%s"),
950 !info
.facename
? wxT("*") : info
.facename
.c_str(),
951 info
.xregistry
.c_str(),
952 info
.xencoding
.c_str());
954 return wxTestFontSpec(fontspec
);
957 // ----------------------------------------------------------------------------
958 // X-specific functions
959 // ----------------------------------------------------------------------------
961 wxNativeFont
wxLoadQueryNearestFont(int pointSize
,
966 const wxString
&facename
,
967 wxFontEncoding encoding
,
970 if ( encoding
== wxFONTENCODING_DEFAULT
)
972 encoding
= wxFont::GetDefaultEncoding();
975 // first determine the encoding - if the font doesn't exist at all in this
976 // encoding, it's useless to do all other approximations (i.e. size,
977 // family &c don't matter much)
978 wxNativeEncodingInfo info
;
979 if ( encoding
== wxFONTENCODING_SYSTEM
)
981 // This will always work so we don't test to save time
982 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
986 if ( !wxGetNativeFontEncoding(encoding
, &info
) ||
987 !wxTestFontEncoding(info
) )
990 if ( !wxFontMapper::Get()->GetAltForEncoding(encoding
, &info
) )
991 #endif // wxUSE_FONTMAP
993 // unspported encoding - replace it with the default
995 // NB: we can't just return 0 from here because wxGTK code doesn't
996 // check for it (i.e. it supposes that we'll always succeed),
997 // so it would provoke a crash
998 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
1003 // OK, we have the correct xregistry/xencoding in info structure
1004 wxNativeFont font
= 0;
1006 // if we already have the X font name, try to use it
1007 if( xFontName
&& !xFontName
->empty() )
1010 // Make sure point size is correct for scale factor.
1012 wxStringTokenizer
tokenizer(*xFontName
, wxT("-"), wxTOKEN_RET_DELIMS
);
1013 wxString newFontName
;
1015 for(int i
= 0; i
< 8; i
++)
1016 newFontName
+= tokenizer
.NextToken();
1018 (void) tokenizer
.NextToken();
1020 newFontName
+= wxString::Format(wxT("%d-"), pointSize
);
1022 while(tokenizer
.HasMoreTokens())
1023 newFontName
+= tokenizer
.GetNextToken();
1025 font
= wxLoadFont(newFontName
);
1028 *xFontName
= newFontName
;
1033 // search up and down by stepsize 10
1034 int max_size
= pointSize
+ 20 * (1 + (pointSize
/180));
1035 int min_size
= pointSize
- 20 * (1 + (pointSize
/180));
1037 int i
, round
; // counters
1039 // first round: search for equal, then for smaller and for larger size
1040 // with the given weight and style
1041 int testweight
= weight
;
1042 int teststyle
= style
;
1044 for ( round
= 0; round
< 3; round
++ )
1046 // second round: use normal weight
1049 if ( testweight
!= wxNORMAL
)
1051 testweight
= wxNORMAL
;
1055 ++round
; // fall through to third round
1059 // third round: ... and use normal style
1062 if ( teststyle
!= wxNORMAL
)
1064 teststyle
= wxNORMAL
;
1071 // Search for equal or smaller size (approx.)
1072 for ( i
= pointSize
; !font
&& i
>= 10 && i
>= min_size
; i
-= 10 )
1074 font
= wxLoadQueryFont(i
, family
, teststyle
, testweight
, underlined
,
1075 facename
, info
.xregistry
, info
.xencoding
,
1079 // Search for larger size (approx.)
1080 for ( i
= pointSize
+ 10; !font
&& i
<= max_size
; i
+= 10 )
1082 font
= wxLoadQueryFont(i
, family
, teststyle
, testweight
, underlined
,
1083 facename
, info
.xregistry
, info
.xencoding
,
1088 // Try default family
1089 if ( !font
&& family
!= wxDEFAULT
)
1091 font
= wxLoadQueryFont(pointSize
, wxDEFAULT
, style
, weight
,
1092 underlined
, facename
,
1093 info
.xregistry
, info
.xencoding
,
1097 // ignore size, family, style and weight but try to find font with the
1098 // given facename and encoding
1101 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1102 underlined
, facename
,
1103 info
.xregistry
, info
.xencoding
,
1106 // ignore family as well
1109 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1110 underlined
, wxEmptyString
,
1111 info
.xregistry
, info
.xencoding
,
1114 // if it still failed, try to get the font of any size but
1115 // with the requested encoding: this can happen if the
1116 // encoding is only available in one size which happens to be
1117 // different from 120
1120 font
= wxLoadQueryFont(-1, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1121 false, wxEmptyString
,
1122 info
.xregistry
, info
.xencoding
,
1125 // this should never happen as we had tested for it in the
1126 // very beginning, but if it does, do return something non
1127 // NULL or we'd crash in wxFont code
1130 wxFAIL_MSG( wxT("this encoding should be available!") );
1132 font
= wxLoadQueryFont(-1,
1133 wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1134 false, wxEmptyString
,
1146 // ----------------------------------------------------------------------------
1147 // private functions
1148 // ----------------------------------------------------------------------------
1150 // returns true if there are any fonts matching this font spec
1151 static bool wxTestFontSpec(const wxString
& fontspec
)
1153 // some X servers will fail to load this font because there are too many
1154 // matches so we must test explicitly for this
1155 if ( fontspec
== wxT("-*-*-*-*-*-*-*-*-*-*-*-*-*-*") )
1160 wxNativeFont test
= (wxNativeFont
) g_fontHash
->Get( fontspec
);
1166 test
= wxLoadFont(fontspec
);
1167 g_fontHash
->Put( fontspec
, (wxObject
*) test
);
1181 static wxNativeFont
wxLoadQueryFont(int pointSize
,
1185 bool WXUNUSED(underlined
),
1186 const wxString
& facename
,
1187 const wxString
& xregistry
,
1188 const wxString
& xencoding
,
1189 wxString
* xFontName
)
1194 case wxDECORATIVE
: xfamily
= wxT("lucida"); break;
1195 case wxROMAN
: xfamily
= wxT("times"); break;
1196 case wxMODERN
: xfamily
= wxT("courier"); break;
1197 case wxSWISS
: xfamily
= wxT("helvetica"); break;
1198 case wxTELETYPE
: xfamily
= wxT("lucidatypewriter"); break;
1199 case wxSCRIPT
: xfamily
= wxT("utopia"); break;
1200 default: xfamily
= wxT("*");
1208 xweight
= MWLF_WEIGHT_BOLD
;
1213 xweight
= MWLF_WEIGHT_LIGHT
;
1218 xweight
= MWLF_WEIGHT_NORMAL
;
1224 xweight
= MWLF_WEIGHT_DEFAULT
;
1228 GR_SCREEN_INFO screenInfo
;
1229 GrGetScreenInfo(& screenInfo
);
1231 int yPixelsPerCM
= screenInfo
.ydpcm
;
1233 // A point is 1/72 of an inch.
1234 // An inch is 2.541 cm.
1235 // So pixelHeight = (pointSize / 72) (inches) * 2.541 (for cm) * yPixelsPerCM (for pixels)
1236 // In fact pointSize is 10 * the normal point size so
1239 int pixelHeight
= (int) ( (((float)pointSize
) / 720.0) * 2.541 * (float) yPixelsPerCM
) ;
1241 // An alternative: assume that the screen is 72 dpi.
1242 //int pixelHeight = (int) (((float)pointSize / 720.0) * 72.0) ;
1243 //int pixelHeight = (int) ((float)pointSize / 10.0) ;
1246 logFont
.lfHeight
= pixelHeight
;
1247 logFont
.lfWidth
= 0;
1248 logFont
.lfEscapement
= 0;
1249 logFont
.lfOrientation
= 0;
1250 logFont
.lfWeight
= xweight
;
1251 logFont
.lfItalic
= (style
== wxNORMAL
? 0 : 1) ;
1252 logFont
.lfUnderline
= 0;
1253 logFont
.lfStrikeOut
= 0;
1254 logFont
.lfCharSet
= MWLF_CHARSET_DEFAULT
; // TODO: select appropriate one
1255 logFont
.lfOutPrecision
= MWLF_TYPE_DEFAULT
;
1256 logFont
.lfClipPrecision
= 0; // Not used
1257 logFont
.lfRoman
= (family
== wxROMAN
? 1 : 0) ;
1258 logFont
.lfSerif
= (family
== wxSWISS
? 0 : 1) ;
1259 logFont
.lfSansSerif
= !logFont
.lfSerif
;
1260 logFont
.lfModern
= (family
== wxMODERN
? 1 : 0) ;
1261 logFont
.lfProportional
= (family
== wxTELETYPE
? 0 : 1) ;
1262 logFont
.lfOblique
= 0;
1263 logFont
.lfSmallCaps
= 0;
1264 logFont
.lfPitch
= 0; // 0 = default
1265 strcpy(logFont
.lfFaceName
, facename
.c_str());
1267 XFontStruct
* fontInfo
= (XFontStruct
*) malloc(sizeof(XFontStruct
));
1268 fontInfo
->fid
= GrCreateFont((GR_CHAR
*) facename
.c_str(), pixelHeight
, & logFont
);
1269 GrGetFontInfo(fontInfo
->fid
, & fontInfo
->info
);
1270 return (wxNativeFont
) fontInfo
;
1274 if (!facename
.empty())
1276 fontSpec
.Printf(wxT("-*-%s-*-*-normal-*-*-*-*-*-*-*-*-*"),
1279 if ( wxTestFontSpec(fontSpec
) )
1283 //else: no such family, use default one instead
1290 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1292 if ( wxTestFontSpec(fontSpec
) )
1297 // fall through - try wxITALIC now
1300 fontSpec
.Printf(wxT("-*-%s-*-i-*-*-*-*-*-*-*-*-*-*"),
1302 if ( wxTestFontSpec(fontSpec
) )
1306 else if ( style
== wxITALIC
) // and not wxSLANT
1309 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1311 if ( wxTestFontSpec(fontSpec
) )
1317 // no italic, no slant - leave default
1324 wxFAIL_MSG(wxT("unknown font style"));
1325 // fall back to normal
1337 fontSpec
.Printf(wxT("-*-%s-bold-*-*-*-*-*-*-*-*-*-*-*"),
1339 if ( wxTestFontSpec(fontSpec
) )
1341 xweight
= wxT("bold");
1344 fontSpec
.Printf(wxT("-*-%s-heavy-*-*-*-*-*-*-*-*-*-*-*"),
1346 if ( wxTestFontSpec(fontSpec
) )
1348 xweight
= wxT("heavy");
1351 fontSpec
.Printf(wxT("-*-%s-extrabold-*-*-*-*-*-*-*-*-*-*-*"),
1353 if ( wxTestFontSpec(fontSpec
) )
1355 xweight
= wxT("extrabold");
1358 fontSpec
.Printf(wxT("-*-%s-demibold-*-*-*-*-*-*-*-*-*-*-*"),
1360 if ( wxTestFontSpec(fontSpec
) )
1362 xweight
= wxT("demibold");
1365 fontSpec
.Printf(wxT("-*-%s-black-*-*-*-*-*-*-*-*-*-*-*"),
1367 if ( wxTestFontSpec(fontSpec
) )
1369 xweight
= wxT("black");
1372 fontSpec
.Printf(wxT("-*-%s-ultrablack-*-*-*-*-*-*-*-*-*-*-*"),
1374 if ( wxTestFontSpec(fontSpec
) )
1376 xweight
= wxT("ultrablack");
1383 fontSpec
.Printf(wxT("-*-%s-light-*-*-*-*-*-*-*-*-*-*-*"),
1385 if ( wxTestFontSpec(fontSpec
) )
1387 xweight
= wxT("light");
1390 fontSpec
.Printf(wxT("-*-%s-thin-*-*-*-*-*-*-*-*-*-*-*"),
1392 if ( wxTestFontSpec(fontSpec
) )
1394 xweight
= wxT("thin");
1401 fontSpec
.Printf(wxT("-*-%s-medium-*-*-*-*-*-*-*-*-*-*-*"),
1403 if ( wxTestFontSpec(fontSpec
) )
1405 xweight
= wxT("medium");
1408 fontSpec
.Printf(wxT("-*-%s-normal-*-*-*-*-*-*-*-*-*-*-*"),
1410 if ( wxTestFontSpec(fontSpec
) )
1412 xweight
= wxT("normal");
1415 fontSpec
.Printf(wxT("-*-%s-regular-*-*-*-*-*-*-*-*-*-*-*"),
1417 if ( wxTestFontSpec(fontSpec
) )
1419 xweight
= wxT("regular");
1425 default: xweight
= wxT("*"); break;
1428 // if pointSize is -1, don't specify any
1430 if ( pointSize
== -1 )
1432 sizeSpec
= wxT('*');
1436 sizeSpec
.Printf(wxT("%d"), pointSize
);
1439 // construct the X font spec from our data
1440 fontSpec
.Printf(wxT("-*-%s-%s-%s-normal-*-*-%s-*-*-*-*-%s-%s"),
1441 xfamily
.c_str(), xweight
.c_str(), xstyle
.c_str(),
1442 sizeSpec
.c_str(), xregistry
.c_str(), xencoding
.c_str());
1445 *xFontName
= fontSpec
;
1447 return wxLoadFont(fontSpec
);
1452 // ----------------------------------------------------------------------------
1454 // ----------------------------------------------------------------------------
1456 class wxFontModule
: public wxModule
1463 DECLARE_DYNAMIC_CLASS(wxFontModule
)
1466 IMPLEMENT_DYNAMIC_CLASS(wxFontModule
, wxModule
)
1468 bool wxFontModule::OnInit()
1470 g_fontHash
= new wxHashTable( wxKEY_STRING
);
1475 void wxFontModule::OnExit()
1477 wxDELETE(g_fontHash
);
1480 #endif // GTK 2.0/1.x