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 (strncasecmp( family_text
, "monospace", 9 ) == 0)
163 ret
= wxFONTFAMILY_TELETYPE
; // begins with "Monospace"
164 else if (strncasecmp( family_text
, "courier", 7 ) == 0)
165 ret
= wxFONTFAMILY_TELETYPE
; // begins with "Courier"
166 #if defined(__WXGTK20__) || defined(HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE)
169 if (!gtk_check_version(2,4,0))
172 PangoFontFamily
**families
;
173 PangoFontFamily
*family
= NULL
;
175 pango_context_list_families(
177 gtk_widget_get_pango_context( wxGetRootWindow() ),
179 wxTheApp
->GetPangoContext(),
181 &families
, &n_families
);
183 for (int i
= 0; i
< n_families
; ++i
)
185 if (g_ascii_strcasecmp(pango_font_family_get_name( families
[i
] ),
186 pango_font_description_get_family( description
)) == 0 )
188 family
= families
[i
];
195 // Some gtk+ systems might query for a non-existing font from
196 // wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT) on initialization,
197 // don't assert until wxSystemSettings::GetFont is checked for this - MR
198 // wxASSERT_MSG( family, "No appropriate PangoFontFamily found for ::description" );
200 if (family
!= NULL
&& pango_font_family_is_monospace( family
))
201 ret
= wxFONTFAMILY_TELETYPE
; // is deemed a monospace font by pango
203 #endif // GTK+ 2 || HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE
205 if (ret
== wxFONTFAMILY_UNKNOWN
)
207 if (strstr( family_text
, "sans" ) != NULL
|| strstr( family_text
, "Sans" ) != NULL
)
208 // checked before serif, so that "* Sans Serif" fonts are detected correctly
209 ret
= wxFONTFAMILY_SWISS
; // contains "Sans"
210 else if (strstr( family_text
, "serif" ) != NULL
|| strstr( family_text
, "Serif" ) != NULL
)
211 ret
= wxFONTFAMILY_ROMAN
; // contains "Serif"
212 else if (strncasecmp( family_text
, "times", 5 ) == 0)
213 ret
= wxFONTFAMILY_ROMAN
; // begins with "Times"
214 else if (strncasecmp( family_text
, "old", 3 ) == 0)
215 ret
= wxFONTFAMILY_DECORATIVE
; // begins with "Old" - "Old English", "Old Town"
221 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
223 return wxFONTENCODING_SYSTEM
;
226 void wxNativeFontInfo::SetPointSize(int pointsize
)
228 pango_font_description_set_size( description
, pointsize
* PANGO_SCALE
);
231 void wxNativeFontInfo::SetStyle(wxFontStyle style
)
235 case wxFONTSTYLE_ITALIC
:
236 pango_font_description_set_style( description
, PANGO_STYLE_ITALIC
);
238 case wxFONTSTYLE_SLANT
:
239 pango_font_description_set_style( description
, PANGO_STYLE_OBLIQUE
);
242 wxFAIL_MSG( "unknown font style" );
244 case wxFONTSTYLE_NORMAL
:
245 pango_font_description_set_style( description
, PANGO_STYLE_NORMAL
);
250 void wxNativeFontInfo::SetWeight(wxFontWeight weight
)
254 case wxFONTWEIGHT_BOLD
:
255 pango_font_description_set_weight(description
, PANGO_WEIGHT_BOLD
);
257 case wxFONTWEIGHT_LIGHT
:
258 pango_font_description_set_weight(description
, PANGO_WEIGHT_LIGHT
);
261 wxFAIL_MSG( "unknown font weight" );
263 case wxFONTWEIGHT_NORMAL
:
264 pango_font_description_set_weight(description
, PANGO_WEIGHT_NORMAL
);
268 void wxNativeFontInfo::SetUnderlined(bool WXUNUSED(underlined
))
270 // wxWindowDCImpl::DoDrawText will take care of rendering font with
271 // the underline attribute!
272 wxFAIL_MSG( "not implemented" );
275 void wxNativeFontInfo::SetStrikethrough(bool WXUNUSED(strikethrough
))
277 wxFAIL_MSG( "not implemented" );
280 bool wxNativeFontInfo::SetFaceName(const wxString
& facename
)
282 pango_font_description_set_family(description
, wxPANGO_CONV(facename
));
284 // we return true because Pango doesn't tell us if the call failed or not;
285 // instead on wxGTK wxFont::SetFaceName() will call wxFontBase::SetFaceName()
286 // which does the check
290 void wxNativeFontInfo::SetFamily(wxFontFamily family
)
292 wxArrayString facename
;
294 // the list of fonts associated with a family was partially
295 // taken from http://www.codestyle.org/css/font-family
299 case wxFONTFAMILY_SCRIPT
:
300 // corresponds to the cursive font family in the page linked above
301 facename
.Add(wxS("URW Chancery L"));
302 facename
.Add(wxS("Comic Sans MS"));
305 case wxFONTFAMILY_DECORATIVE
:
306 // corresponds to the fantasy font family in the page linked above
307 facename
.Add(wxS("Impact"));
310 case wxFONTFAMILY_ROMAN
:
311 // corresponds to the serif font family in the page linked above
312 facename
.Add(wxS("Century Schoolbook L"));
313 facename
.Add(wxS("URW Bookman L"));
314 facename
.Add(wxS("URW Palladio L"));
315 facename
.Add(wxS("DejaVu Serif"));
316 facename
.Add(wxS("FreeSerif"));
317 facename
.Add(wxS("Times New Roman"));
318 facename
.Add(wxS("Times"));
321 case wxFONTFAMILY_TELETYPE
:
322 case wxFONTFAMILY_MODERN
:
323 // corresponds to the monospace font family in the page linked above
324 facename
.Add(wxS("DejaVu Sans Mono"));
325 facename
.Add(wxS("Nimbus Mono L"));
326 facename
.Add(wxS("Bitstream Vera Sans Mono"));
327 facename
.Add(wxS("Andale Mono"));
328 facename
.Add(wxS("Lucida Sans Typewriter"));
329 facename
.Add(wxS("FreeMono"));
330 facename
.Add(wxS("Courier New"));
331 facename
.Add(wxS("Courier"));
334 case wxFONTFAMILY_SWISS
:
335 case wxFONTFAMILY_DEFAULT
:
337 // corresponds to the sans-serif font family in the page linked above
338 facename
.Add(wxS("DejaVu Sans"));
339 facename
.Add(wxS("URW Gothic L"));
340 facename
.Add(wxS("Nimbus Sans L"));
341 facename
.Add(wxS("Bitstream Vera Sans"));
342 facename
.Add(wxS("Lucida Sans"));
343 facename
.Add(wxS("Arial"));
344 facename
.Add(wxS("FreeSans"));
348 SetFaceName(facename
);
351 void wxNativeFontInfo::SetEncoding(wxFontEncoding
WXUNUSED(encoding
))
353 wxFAIL_MSG( "not implemented: Pango encoding is always UTF8" );
356 bool wxNativeFontInfo::FromString(const wxString
& s
)
359 pango_font_description_free( description
);
361 // there is a bug in at least pango <= 1.13 which makes it (or its backends)
362 // segfault for very big point sizes and for negative point sizes.
363 // To workaround that bug for pango <= 1.13
364 // (see http://bugzilla.gnome.org/show_bug.cgi?id=340229)
365 // we do the check on the size here using same (arbitrary) limits used by
366 // pango > 1.13. Note that the segfault could happen also for pointsize
367 // smaller than this limit !!
369 const size_t pos
= str
.find_last_of(wxS(" "));
371 if ( pos
!= wxString::npos
&& wxString(str
, pos
+ 1).ToDouble(&size
) )
376 else if ( size
>= 1E6
)
377 sizeStr
= wxS("1E6");
379 if ( !sizeStr
.empty() )
381 // replace the old size with the adjusted one
382 str
= wxString(s
, 0, pos
) + sizeStr
;
386 description
= pango_font_description_from_string(wxPANGO_CONV(str
));
389 // ensure a valid facename is selected
390 if (!wxFontEnumerator::IsValidFacename(GetFaceName()))
391 SetFaceName(wxNORMAL_FONT
->GetFaceName());
392 #endif // wxUSE_FONTENUM
397 wxString
wxNativeFontInfo::ToString() const
399 wxGtkString
str(pango_font_description_to_string( description
));
401 return wxPANGO_CONV_BACK(str
);
404 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
406 return FromString( s
);
409 wxString
wxNativeFontInfo::ToUserString() const
418 #pragma message disable nosimpint
421 #include <X11/Xlib.h>
424 #pragma message enable nosimpint
427 #elif defined(__WXGTK__)
428 // we have to declare struct tm to avoid problems with first forward
429 // declaring it in C code (glib.h included from gdk.h does it) and then
430 // defining it when time.h is included from the headers below - this is
431 // known not to work at least with Sun CC 6.01
438 // ----------------------------------------------------------------------------
440 // ----------------------------------------------------------------------------
442 static wxHashTable
*g_fontHash
= NULL
;
444 // ----------------------------------------------------------------------------
446 // ----------------------------------------------------------------------------
448 // define the functions to create and destroy native fonts for this toolkit
450 wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
452 return XLoadQueryFont((Display
*)wxGetDisplay(), fontSpec
);
455 inline void wxFreeFont(wxNativeFont font
)
457 XFreeFont((Display
*)wxGetDisplay(), (XFontStruct
*)font
);
459 #elif defined(__WXGTK__)
460 wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
462 // VZ: we should use gdk_fontset_load() instead of gdk_font_load()
463 // here to be able to display Japanese fonts correctly (at least
464 // this is what people report) but unfortunately doing it results
465 // in tons of warnings when using GTK with "normal" European
466 // languages and so we can't always do it and I don't know enough
467 // to determine when should this be done... (FIXME)
468 return gdk_font_load( wxConvertWX2MB(fontSpec
) );
471 inline void wxFreeFont(wxNativeFont font
)
473 gdk_font_unref(font
);
476 #error "Unknown GUI toolkit"
479 static bool wxTestFontSpec(const wxString
& fontspec
);
481 static wxNativeFont
wxLoadQueryFont(int pointSize
,
486 const wxString
& facename
,
487 const wxString
& xregistry
,
488 const wxString
& xencoding
,
489 wxString
* xFontName
);
491 // ============================================================================
493 // ============================================================================
495 // ----------------------------------------------------------------------------
496 // wxNativeEncodingInfo
497 // ----------------------------------------------------------------------------
499 // convert to/from the string representation: format is
500 // encodingid;registry;encoding[;facename]
501 bool wxNativeEncodingInfo::FromString(const wxString
& s
)
503 // use ";", not "-" because it may be part of encoding name
504 wxStringTokenizer
tokenizer(s
, wxT(";"));
506 wxString encid
= tokenizer
.GetNextToken();
508 if ( !encid
.ToLong(&enc
) )
510 encoding
= (wxFontEncoding
)enc
;
512 xregistry
= tokenizer
.GetNextToken();
516 xencoding
= tokenizer
.GetNextToken();
521 facename
= tokenizer
.GetNextToken();
526 wxString
wxNativeEncodingInfo::ToString() const
529 s
<< (long)encoding
<< wxT(';') << xregistry
<< wxT(';') << xencoding
;
530 if ( !facename
.empty() )
532 s
<< wxT(';') << facename
;
538 // ----------------------------------------------------------------------------
540 // ----------------------------------------------------------------------------
542 void wxNativeFontInfo::Init()
547 bool wxNativeFontInfo::FromString(const wxString
& s
)
549 wxStringTokenizer
tokenizer(s
, wxT(";"));
552 wxString token
= tokenizer
.GetNextToken();
553 if ( token
!= wxT('0') )
556 xFontName
= tokenizer
.GetNextToken();
558 // this should be the end
559 if ( tokenizer
.HasMoreTokens() )
562 return FromXFontName(xFontName
);
565 wxString
wxNativeFontInfo::ToString() const
568 return wxString::Format(wxT("%d;%s"), 0, GetXFontName().c_str());
571 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
573 return FromXFontName(s
);
576 wxString
wxNativeFontInfo::ToUserString() const
578 return GetXFontName();
581 bool wxNativeFontInfo::HasElements() const
583 // we suppose that the foundry is never empty, so if it is it means that we
584 // had never parsed the XLFD
585 return !fontElements
[0].empty();
588 wxString
wxNativeFontInfo::GetXFontComponent(wxXLFDField field
) const
590 wxCHECK_MSG( field
< wxXLFD_MAX
, wxEmptyString
, wxT("invalid XLFD field") );
592 if ( !HasElements() )
594 if ( !const_cast<wxNativeFontInfo
*>(this)->FromXFontName(xFontName
) )
595 return wxEmptyString
;
598 return fontElements
[field
];
601 bool wxNativeFontInfo::FromXFontName(const wxString
& fontname
)
603 // TODO: we should be able to handle the font aliases here, but how?
604 wxStringTokenizer
tokenizer(fontname
, wxT("-"));
606 // skip the leading, usually empty field (font name registry)
607 if ( !tokenizer
.HasMoreTokens() )
610 (void)tokenizer
.GetNextToken();
612 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
614 if ( !tokenizer
.HasMoreTokens() )
616 // not enough elements in the XLFD - or maybe an alias
620 wxString field
= tokenizer
.GetNextToken();
621 if ( !field
.empty() && field
!= wxT('*') )
623 // we're really initialized now
627 fontElements
[n
] = field
;
630 // this should be all
631 if ( tokenizer
.HasMoreTokens() )
637 wxString
wxNativeFontInfo::GetXFontName() const
639 if ( xFontName
.empty() )
641 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
643 // replace the non specified elements with '*' except for the
644 // additional style which is usually just omitted
645 wxString elt
= fontElements
[n
];
646 if ( elt
.empty() && n
!= wxXLFD_ADDSTYLE
)
651 const_cast<wxNativeFontInfo
*>(this)->xFontName
<< wxT('-') << elt
;
659 wxNativeFontInfo::SetXFontComponent(wxXLFDField field
, const wxString
& value
)
661 wxCHECK_RET( field
< wxXLFD_MAX
, wxT("invalid XLFD field") );
663 // this class should be initialized with a valid font spec first and only
664 // then the fields may be modified!
665 wxASSERT_MSG( !IsDefault(), wxT("can't modify an uninitialized XLFD") );
667 if ( !HasElements() )
669 if ( !const_cast<wxNativeFontInfo
*>(this)->FromXFontName(xFontName
) )
671 wxFAIL_MSG( wxT("can't set font element for invalid XLFD") );
677 fontElements
[field
] = value
;
679 // invalidate the XFLD, it doesn't correspond to the font elements any more
683 void wxNativeFontInfo::SetXFontName(const wxString
& xFontName_
)
685 // invalidate the font elements, GetXFontComponent() will reparse the XLFD
686 fontElements
[0].clear();
688 xFontName
= xFontName_
;
693 int wxNativeFontInfo::GetPointSize() const
695 const wxString s
= GetXFontComponent(wxXLFD_POINTSIZE
);
697 // return -1 to indicate that the size is unknown
699 return s
.ToLong(&l
) ? l
: -1;
702 wxFontStyle
wxNativeFontInfo::GetStyle() const
704 const wxString s
= GetXFontComponent(wxXLFD_SLANT
);
706 if ( s
.length() != 1 )
708 // it is really unknown but we don't have any way to return it from
710 return wxFONTSTYLE_NORMAL
;
713 switch ( s
[0].GetValue() )
716 // again, unknown but consider normal by default
719 return wxFONTSTYLE_NORMAL
;
722 return wxFONTSTYLE_ITALIC
;
725 return wxFONTSTYLE_SLANT
;
729 wxFontWeight
wxNativeFontInfo::GetWeight() const
731 const wxString s
= GetXFontComponent(wxXLFD_WEIGHT
).MakeLower();
732 if ( s
.find(wxT("bold")) != wxString::npos
|| s
== wxT("black") )
733 return wxFONTWEIGHT_BOLD
;
734 else if ( s
== wxT("light") )
735 return wxFONTWEIGHT_LIGHT
;
737 return wxFONTWEIGHT_NORMAL
;
740 bool wxNativeFontInfo::GetUnderlined() const
742 // X fonts are never underlined
746 wxString
wxNativeFontInfo::GetFaceName() const
748 // wxWidgets facename probably more accurately corresponds to X family
749 return GetXFontComponent(wxXLFD_FAMILY
);
752 wxFontFamily
wxNativeFontInfo::GetFamily() const
754 // and wxWidgets family -- to X foundry, but we have to translate it to
755 // wxFontFamily somehow...
756 wxFAIL_MSG(wxT("not implemented")); // GetXFontComponent(wxXLFD_FOUNDRY);
758 return wxFONTFAMILY_DEFAULT
;
761 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
763 // we already have the code for this but need to refactor it first
764 wxFAIL_MSG( wxT("not implemented") );
766 return wxFONTENCODING_MAX
;
769 void wxNativeFontInfo::SetPointSize(int pointsize
)
771 SetXFontComponent(wxXLFD_POINTSIZE
, wxString::Format(wxT("%d"), pointsize
));
774 void wxNativeFontInfo::SetStyle(wxFontStyle style
)
779 case wxFONTSTYLE_ITALIC
:
783 case wxFONTSTYLE_SLANT
:
787 case wxFONTSTYLE_NORMAL
:
791 wxFAIL_MSG( wxT("unknown wxFontStyle in wxNativeFontInfo::SetStyle") );
795 SetXFontComponent(wxXLFD_SLANT
, s
);
798 void wxNativeFontInfo::SetWeight(wxFontWeight weight
)
803 case wxFONTWEIGHT_BOLD
:
807 case wxFONTWEIGHT_LIGHT
:
811 case wxFONTWEIGHT_NORMAL
:
816 wxFAIL_MSG( wxT("unknown wxFontWeight in wxNativeFontInfo::SetWeight") );
820 SetXFontComponent(wxXLFD_WEIGHT
, s
);
823 void wxNativeFontInfo::SetUnderlined(bool WXUNUSED(underlined
))
825 // can't do this under X
828 void wxNativeFontInfo::SetStrikethrough(bool WXUNUSED(strikethrough
))
830 // this is not supported by Pango fonts neither
833 bool wxNativeFontInfo::SetFaceName(const wxString
& facename
)
835 SetXFontComponent(wxXLFD_FAMILY
, facename
);
839 void wxNativeFontInfo::SetFamily(wxFontFamily
WXUNUSED(family
))
841 // wxFontFamily -> X foundry, anyone?
842 wxFAIL_MSG( wxT("not implemented") );
844 // SetXFontComponent(wxXLFD_FOUNDRY, ...);
847 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding
)
849 wxNativeEncodingInfo info
;
850 if ( wxGetNativeFontEncoding(encoding
, &info
) )
852 SetXFontComponent(wxXLFD_ENCODING
, info
.xencoding
);
853 SetXFontComponent(wxXLFD_REGISTRY
, info
.xregistry
);
857 // ----------------------------------------------------------------------------
859 // ----------------------------------------------------------------------------
861 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
862 wxNativeEncodingInfo
*info
)
864 wxCHECK_MSG( info
, false, wxT("bad pointer in wxGetNativeFontEncoding") );
866 if ( encoding
== wxFONTENCODING_DEFAULT
)
868 encoding
= wxFont::GetDefaultEncoding();
873 case wxFONTENCODING_ISO8859_1
:
874 case wxFONTENCODING_ISO8859_2
:
875 case wxFONTENCODING_ISO8859_3
:
876 case wxFONTENCODING_ISO8859_4
:
877 case wxFONTENCODING_ISO8859_5
:
878 case wxFONTENCODING_ISO8859_6
:
879 case wxFONTENCODING_ISO8859_7
:
880 case wxFONTENCODING_ISO8859_8
:
881 case wxFONTENCODING_ISO8859_9
:
882 case wxFONTENCODING_ISO8859_10
:
883 case wxFONTENCODING_ISO8859_11
:
884 case wxFONTENCODING_ISO8859_12
:
885 case wxFONTENCODING_ISO8859_13
:
886 case wxFONTENCODING_ISO8859_14
:
887 case wxFONTENCODING_ISO8859_15
:
889 int cp
= encoding
- wxFONTENCODING_ISO8859_1
+ 1;
890 info
->xregistry
= wxT("iso8859");
891 info
->xencoding
.Printf(wxT("%d"), cp
);
895 case wxFONTENCODING_UTF8
:
896 info
->xregistry
= wxT("iso10646");
897 info
->xencoding
= wxT("*");
900 case wxFONTENCODING_GB2312
:
901 info
->xregistry
= wxT("GB2312"); // or the otherway round?
902 info
->xencoding
= wxT("*");
905 case wxFONTENCODING_KOI8
:
906 case wxFONTENCODING_KOI8_U
:
907 info
->xregistry
= wxT("koi8");
909 // we don't make distinction between koi8-r, koi8-u and koi8-ru (so far)
910 info
->xencoding
= wxT("*");
913 case wxFONTENCODING_CP1250
:
914 case wxFONTENCODING_CP1251
:
915 case wxFONTENCODING_CP1252
:
916 case wxFONTENCODING_CP1253
:
917 case wxFONTENCODING_CP1254
:
918 case wxFONTENCODING_CP1255
:
919 case wxFONTENCODING_CP1256
:
920 case wxFONTENCODING_CP1257
:
922 int cp
= encoding
- wxFONTENCODING_CP1250
+ 1250;
923 info
->xregistry
= wxT("microsoft");
924 info
->xencoding
.Printf(wxT("cp%d"), cp
);
928 case wxFONTENCODING_EUC_JP
:
929 case wxFONTENCODING_SHIFT_JIS
:
930 info
->xregistry
= "jis*";
931 info
->xencoding
= "*";
934 case wxFONTENCODING_SYSTEM
:
936 info
->xencoding
= wxT("*");
940 // don't know how to translate this encoding into X fontspec
944 info
->encoding
= encoding
;
949 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
952 fontspec
.Printf(wxT("-*-%s-*-*-*-*-*-*-*-*-*-*-%s-%s"),
953 !info
.facename
? wxT("*") : info
.facename
.c_str(),
954 info
.xregistry
.c_str(),
955 info
.xencoding
.c_str());
957 return wxTestFontSpec(fontspec
);
960 // ----------------------------------------------------------------------------
961 // X-specific functions
962 // ----------------------------------------------------------------------------
964 wxNativeFont
wxLoadQueryNearestFont(int pointSize
,
969 const wxString
&facename
,
970 wxFontEncoding encoding
,
973 if ( encoding
== wxFONTENCODING_DEFAULT
)
975 encoding
= wxFont::GetDefaultEncoding();
978 // first determine the encoding - if the font doesn't exist at all in this
979 // encoding, it's useless to do all other approximations (i.e. size,
980 // family &c don't matter much)
981 wxNativeEncodingInfo info
;
982 if ( encoding
== wxFONTENCODING_SYSTEM
)
984 // This will always work so we don't test to save time
985 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
989 if ( !wxGetNativeFontEncoding(encoding
, &info
) ||
990 !wxTestFontEncoding(info
) )
993 if ( !wxFontMapper::Get()->GetAltForEncoding(encoding
, &info
) )
994 #endif // wxUSE_FONTMAP
996 // unspported encoding - replace it with the default
998 // NB: we can't just return 0 from here because wxGTK code doesn't
999 // check for it (i.e. it supposes that we'll always succeed),
1000 // so it would provoke a crash
1001 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
1006 // OK, we have the correct xregistry/xencoding in info structure
1007 wxNativeFont font
= 0;
1009 // if we already have the X font name, try to use it
1010 if( xFontName
&& !xFontName
->empty() )
1013 // Make sure point size is correct for scale factor.
1015 wxStringTokenizer
tokenizer(*xFontName
, wxT("-"), wxTOKEN_RET_DELIMS
);
1016 wxString newFontName
;
1018 for(int i
= 0; i
< 8; i
++)
1019 newFontName
+= tokenizer
.NextToken();
1021 (void) tokenizer
.NextToken();
1023 newFontName
+= wxString::Format(wxT("%d-"), pointSize
);
1025 while(tokenizer
.HasMoreTokens())
1026 newFontName
+= tokenizer
.GetNextToken();
1028 font
= wxLoadFont(newFontName
);
1031 *xFontName
= newFontName
;
1036 // search up and down by stepsize 10
1037 int max_size
= pointSize
+ 20 * (1 + (pointSize
/180));
1038 int min_size
= pointSize
- 20 * (1 + (pointSize
/180));
1040 int i
, round
; // counters
1042 // first round: search for equal, then for smaller and for larger size
1043 // with the given weight and style
1044 int testweight
= weight
;
1045 int teststyle
= style
;
1047 for ( round
= 0; round
< 3; round
++ )
1049 // second round: use normal weight
1052 if ( testweight
!= wxNORMAL
)
1054 testweight
= wxNORMAL
;
1058 ++round
; // fall through to third round
1062 // third round: ... and use normal style
1065 if ( teststyle
!= wxNORMAL
)
1067 teststyle
= wxNORMAL
;
1074 // Search for equal or smaller size (approx.)
1075 for ( i
= pointSize
; !font
&& i
>= 10 && i
>= min_size
; i
-= 10 )
1077 font
= wxLoadQueryFont(i
, family
, teststyle
, testweight
, underlined
,
1078 facename
, info
.xregistry
, info
.xencoding
,
1082 // Search for larger size (approx.)
1083 for ( i
= pointSize
+ 10; !font
&& i
<= max_size
; i
+= 10 )
1085 font
= wxLoadQueryFont(i
, family
, teststyle
, testweight
, underlined
,
1086 facename
, info
.xregistry
, info
.xencoding
,
1091 // Try default family
1092 if ( !font
&& family
!= wxDEFAULT
)
1094 font
= wxLoadQueryFont(pointSize
, wxDEFAULT
, style
, weight
,
1095 underlined
, facename
,
1096 info
.xregistry
, info
.xencoding
,
1100 // ignore size, family, style and weight but try to find font with the
1101 // given facename and encoding
1104 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1105 underlined
, facename
,
1106 info
.xregistry
, info
.xencoding
,
1109 // ignore family as well
1112 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1113 underlined
, wxEmptyString
,
1114 info
.xregistry
, info
.xencoding
,
1117 // if it still failed, try to get the font of any size but
1118 // with the requested encoding: this can happen if the
1119 // encoding is only available in one size which happens to be
1120 // different from 120
1123 font
= wxLoadQueryFont(-1, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1124 false, wxEmptyString
,
1125 info
.xregistry
, info
.xencoding
,
1128 // this should never happen as we had tested for it in the
1129 // very beginning, but if it does, do return something non
1130 // NULL or we'd crash in wxFont code
1133 wxFAIL_MSG( wxT("this encoding should be available!") );
1135 font
= wxLoadQueryFont(-1,
1136 wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1137 false, wxEmptyString
,
1149 // ----------------------------------------------------------------------------
1150 // private functions
1151 // ----------------------------------------------------------------------------
1153 // returns true if there are any fonts matching this font spec
1154 static bool wxTestFontSpec(const wxString
& fontspec
)
1156 // some X servers will fail to load this font because there are too many
1157 // matches so we must test explicitly for this
1158 if ( fontspec
== wxT("-*-*-*-*-*-*-*-*-*-*-*-*-*-*") )
1163 wxNativeFont test
= (wxNativeFont
) g_fontHash
->Get( fontspec
);
1169 test
= wxLoadFont(fontspec
);
1170 g_fontHash
->Put( fontspec
, (wxObject
*) test
);
1184 static wxNativeFont
wxLoadQueryFont(int pointSize
,
1188 bool WXUNUSED(underlined
),
1189 const wxString
& facename
,
1190 const wxString
& xregistry
,
1191 const wxString
& xencoding
,
1192 wxString
* xFontName
)
1197 case wxDECORATIVE
: xfamily
= wxT("lucida"); break;
1198 case wxROMAN
: xfamily
= wxT("times"); break;
1199 case wxMODERN
: xfamily
= wxT("courier"); break;
1200 case wxSWISS
: xfamily
= wxT("helvetica"); break;
1201 case wxTELETYPE
: xfamily
= wxT("lucidatypewriter"); break;
1202 case wxSCRIPT
: xfamily
= wxT("utopia"); break;
1203 default: xfamily
= wxT("*");
1211 xweight
= MWLF_WEIGHT_BOLD
;
1216 xweight
= MWLF_WEIGHT_LIGHT
;
1221 xweight
= MWLF_WEIGHT_NORMAL
;
1227 xweight
= MWLF_WEIGHT_DEFAULT
;
1231 GR_SCREEN_INFO screenInfo
;
1232 GrGetScreenInfo(& screenInfo
);
1234 int yPixelsPerCM
= screenInfo
.ydpcm
;
1236 // A point is 1/72 of an inch.
1237 // An inch is 2.541 cm.
1238 // So pixelHeight = (pointSize / 72) (inches) * 2.541 (for cm) * yPixelsPerCM (for pixels)
1239 // In fact pointSize is 10 * the normal point size so
1242 int pixelHeight
= (int) ( (((float)pointSize
) / 720.0) * 2.541 * (float) yPixelsPerCM
) ;
1244 // An alternative: assume that the screen is 72 dpi.
1245 //int pixelHeight = (int) (((float)pointSize / 720.0) * 72.0) ;
1246 //int pixelHeight = (int) ((float)pointSize / 10.0) ;
1249 logFont
.lfHeight
= pixelHeight
;
1250 logFont
.lfWidth
= 0;
1251 logFont
.lfEscapement
= 0;
1252 logFont
.lfOrientation
= 0;
1253 logFont
.lfWeight
= xweight
;
1254 logFont
.lfItalic
= (style
== wxNORMAL
? 0 : 1) ;
1255 logFont
.lfUnderline
= 0;
1256 logFont
.lfStrikeOut
= 0;
1257 logFont
.lfCharSet
= MWLF_CHARSET_DEFAULT
; // TODO: select appropriate one
1258 logFont
.lfOutPrecision
= MWLF_TYPE_DEFAULT
;
1259 logFont
.lfClipPrecision
= 0; // Not used
1260 logFont
.lfRoman
= (family
== wxROMAN
? 1 : 0) ;
1261 logFont
.lfSerif
= (family
== wxSWISS
? 0 : 1) ;
1262 logFont
.lfSansSerif
= !logFont
.lfSerif
;
1263 logFont
.lfModern
= (family
== wxMODERN
? 1 : 0) ;
1264 logFont
.lfProportional
= (family
== wxTELETYPE
? 0 : 1) ;
1265 logFont
.lfOblique
= 0;
1266 logFont
.lfSmallCaps
= 0;
1267 logFont
.lfPitch
= 0; // 0 = default
1268 strcpy(logFont
.lfFaceName
, facename
.c_str());
1270 XFontStruct
* fontInfo
= (XFontStruct
*) malloc(sizeof(XFontStruct
));
1271 fontInfo
->fid
= GrCreateFont((GR_CHAR
*) facename
.c_str(), pixelHeight
, & logFont
);
1272 GrGetFontInfo(fontInfo
->fid
, & fontInfo
->info
);
1273 return (wxNativeFont
) fontInfo
;
1277 if (!facename
.empty())
1279 fontSpec
.Printf(wxT("-*-%s-*-*-normal-*-*-*-*-*-*-*-*-*"),
1282 if ( wxTestFontSpec(fontSpec
) )
1286 //else: no such family, use default one instead
1293 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1295 if ( wxTestFontSpec(fontSpec
) )
1300 // fall through - try wxITALIC now
1303 fontSpec
.Printf(wxT("-*-%s-*-i-*-*-*-*-*-*-*-*-*-*"),
1305 if ( wxTestFontSpec(fontSpec
) )
1309 else if ( style
== wxITALIC
) // and not wxSLANT
1312 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1314 if ( wxTestFontSpec(fontSpec
) )
1320 // no italic, no slant - leave default
1327 wxFAIL_MSG(wxT("unknown font style"));
1328 // fall back to normal
1340 fontSpec
.Printf(wxT("-*-%s-bold-*-*-*-*-*-*-*-*-*-*-*"),
1342 if ( wxTestFontSpec(fontSpec
) )
1344 xweight
= wxT("bold");
1347 fontSpec
.Printf(wxT("-*-%s-heavy-*-*-*-*-*-*-*-*-*-*-*"),
1349 if ( wxTestFontSpec(fontSpec
) )
1351 xweight
= wxT("heavy");
1354 fontSpec
.Printf(wxT("-*-%s-extrabold-*-*-*-*-*-*-*-*-*-*-*"),
1356 if ( wxTestFontSpec(fontSpec
) )
1358 xweight
= wxT("extrabold");
1361 fontSpec
.Printf(wxT("-*-%s-demibold-*-*-*-*-*-*-*-*-*-*-*"),
1363 if ( wxTestFontSpec(fontSpec
) )
1365 xweight
= wxT("demibold");
1368 fontSpec
.Printf(wxT("-*-%s-black-*-*-*-*-*-*-*-*-*-*-*"),
1370 if ( wxTestFontSpec(fontSpec
) )
1372 xweight
= wxT("black");
1375 fontSpec
.Printf(wxT("-*-%s-ultrablack-*-*-*-*-*-*-*-*-*-*-*"),
1377 if ( wxTestFontSpec(fontSpec
) )
1379 xweight
= wxT("ultrablack");
1386 fontSpec
.Printf(wxT("-*-%s-light-*-*-*-*-*-*-*-*-*-*-*"),
1388 if ( wxTestFontSpec(fontSpec
) )
1390 xweight
= wxT("light");
1393 fontSpec
.Printf(wxT("-*-%s-thin-*-*-*-*-*-*-*-*-*-*-*"),
1395 if ( wxTestFontSpec(fontSpec
) )
1397 xweight
= wxT("thin");
1404 fontSpec
.Printf(wxT("-*-%s-medium-*-*-*-*-*-*-*-*-*-*-*"),
1406 if ( wxTestFontSpec(fontSpec
) )
1408 xweight
= wxT("medium");
1411 fontSpec
.Printf(wxT("-*-%s-normal-*-*-*-*-*-*-*-*-*-*-*"),
1413 if ( wxTestFontSpec(fontSpec
) )
1415 xweight
= wxT("normal");
1418 fontSpec
.Printf(wxT("-*-%s-regular-*-*-*-*-*-*-*-*-*-*-*"),
1420 if ( wxTestFontSpec(fontSpec
) )
1422 xweight
= wxT("regular");
1428 default: xweight
= wxT("*"); break;
1431 // if pointSize is -1, don't specify any
1433 if ( pointSize
== -1 )
1435 sizeSpec
= wxT('*');
1439 sizeSpec
.Printf(wxT("%d"), pointSize
);
1442 // construct the X font spec from our data
1443 fontSpec
.Printf(wxT("-*-%s-%s-%s-normal-*-*-%s-*-*-*-*-%s-%s"),
1444 xfamily
.c_str(), xweight
.c_str(), xstyle
.c_str(),
1445 sizeSpec
.c_str(), xregistry
.c_str(), xencoding
.c_str());
1448 *xFontName
= fontSpec
;
1450 return wxLoadFont(fontSpec
);
1455 // ----------------------------------------------------------------------------
1457 // ----------------------------------------------------------------------------
1459 class wxFontModule
: public wxModule
1466 DECLARE_DYNAMIC_CLASS(wxFontModule
)
1469 IMPLEMENT_DYNAMIC_CLASS(wxFontModule
, wxModule
)
1471 bool wxFontModule::OnInit()
1473 g_fontHash
= new wxHashTable( wxKEY_STRING
);
1478 void wxFontModule::OnExit()
1480 wxDELETE(g_fontHash
);
1483 #endif // GTK 2.0/1.x