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 bool wxNativeFontInfo::SetFaceName(const wxString
& facename
)
277 pango_font_description_set_family(description
, wxPANGO_CONV(facename
));
279 // we return true because Pango doesn't tell us if the call failed or not;
280 // instead on wxGTK wxFont::SetFaceName() will call wxFontBase::SetFaceName()
281 // which does the check
285 void wxNativeFontInfo::SetFamily(wxFontFamily family
)
287 wxArrayString facename
;
289 // the list of fonts associated with a family was partially
290 // taken from http://www.codestyle.org/css/font-family
294 case wxFONTFAMILY_SCRIPT
:
295 // corresponds to the cursive font family in the page linked above
296 facename
.Add(wxS("URW Chancery L"));
297 facename
.Add(wxS("Comic Sans MS"));
300 case wxFONTFAMILY_DECORATIVE
:
301 // corresponds to the fantasy font family in the page linked above
302 facename
.Add(wxS("Impact"));
305 case wxFONTFAMILY_ROMAN
:
306 // corresponds to the serif font family in the page linked above
307 facename
.Add(wxS("Century Schoolbook L"));
308 facename
.Add(wxS("URW Bookman L"));
309 facename
.Add(wxS("URW Palladio L"));
310 facename
.Add(wxS("DejaVu Serif"));
311 facename
.Add(wxS("FreeSerif"));
312 facename
.Add(wxS("Times New Roman"));
313 facename
.Add(wxS("Times"));
316 case wxFONTFAMILY_TELETYPE
:
317 case wxFONTFAMILY_MODERN
:
318 // corresponds to the monospace font family in the page linked above
319 facename
.Add(wxS("DejaVu Sans Mono"));
320 facename
.Add(wxS("Nimbus Mono L"));
321 facename
.Add(wxS("Bitstream Vera Sans Mono"));
322 facename
.Add(wxS("Andale Mono"));
323 facename
.Add(wxS("Lucida Sans Typewriter"));
324 facename
.Add(wxS("FreeMono"));
325 facename
.Add(wxS("Courier New"));
326 facename
.Add(wxS("Courier"));
329 case wxFONTFAMILY_SWISS
:
330 case wxFONTFAMILY_DEFAULT
:
332 // corresponds to the sans-serif font family in the page linked above
333 facename
.Add(wxS("DejaVu Sans"));
334 facename
.Add(wxS("URW Gothic L"));
335 facename
.Add(wxS("Nimbus Sans L"));
336 facename
.Add(wxS("Bitstream Vera Sans"));
337 facename
.Add(wxS("Lucida Sans"));
338 facename
.Add(wxS("Arial"));
339 facename
.Add(wxS("FreeSans"));
343 SetFaceName(facename
);
346 void wxNativeFontInfo::SetEncoding(wxFontEncoding
WXUNUSED(encoding
))
348 wxFAIL_MSG( "not implemented: Pango encoding is always UTF8" );
351 bool wxNativeFontInfo::FromString(const wxString
& s
)
354 pango_font_description_free( description
);
356 // there is a bug in at least pango <= 1.13 which makes it (or its backends)
357 // segfault for very big point sizes and for negative point sizes.
358 // To workaround that bug for pango <= 1.13
359 // (see http://bugzilla.gnome.org/show_bug.cgi?id=340229)
360 // we do the check on the size here using same (arbitrary) limits used by
361 // pango > 1.13. Note that the segfault could happen also for pointsize
362 // smaller than this limit !!
364 const size_t pos
= str
.find_last_of(wxS(" "));
366 if ( pos
!= wxString::npos
&& wxString(str
, pos
+ 1).ToDouble(&size
) )
371 else if ( size
>= 1E6
)
372 sizeStr
= wxS("1E6");
374 if ( !sizeStr
.empty() )
376 // replace the old size with the adjusted one
377 str
= wxString(s
, 0, pos
) + sizeStr
;
381 description
= pango_font_description_from_string(wxPANGO_CONV(str
));
384 // ensure a valid facename is selected
385 if (!wxFontEnumerator::IsValidFacename(GetFaceName()))
386 SetFaceName(wxNORMAL_FONT
->GetFaceName());
387 #endif // wxUSE_FONTENUM
392 wxString
wxNativeFontInfo::ToString() const
394 wxGtkString
str(pango_font_description_to_string( description
));
396 return wxPANGO_CONV_BACK(str
);
399 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
401 return FromString( s
);
404 wxString
wxNativeFontInfo::ToUserString() const
413 #pragma message disable nosimpint
416 #include <X11/Xlib.h>
419 #pragma message enable nosimpint
422 #elif defined(__WXGTK__)
423 // we have to declare struct tm to avoid problems with first forward
424 // declaring it in C code (glib.h included from gdk.h does it) and then
425 // defining it when time.h is included from the headers below - this is
426 // known not to work at least with Sun CC 6.01
433 // ----------------------------------------------------------------------------
435 // ----------------------------------------------------------------------------
437 static wxHashTable
*g_fontHash
= NULL
;
439 // ----------------------------------------------------------------------------
441 // ----------------------------------------------------------------------------
443 // define the functions to create and destroy native fonts for this toolkit
445 wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
447 return XLoadQueryFont((Display
*)wxGetDisplay(), fontSpec
);
450 inline void wxFreeFont(wxNativeFont font
)
452 XFreeFont((Display
*)wxGetDisplay(), (XFontStruct
*)font
);
454 #elif defined(__WXGTK__)
455 wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
457 // VZ: we should use gdk_fontset_load() instead of gdk_font_load()
458 // here to be able to display Japanese fonts correctly (at least
459 // this is what people report) but unfortunately doing it results
460 // in tons of warnings when using GTK with "normal" European
461 // languages and so we can't always do it and I don't know enough
462 // to determine when should this be done... (FIXME)
463 return gdk_font_load( wxConvertWX2MB(fontSpec
) );
466 inline void wxFreeFont(wxNativeFont font
)
468 gdk_font_unref(font
);
471 #error "Unknown GUI toolkit"
474 static bool wxTestFontSpec(const wxString
& fontspec
);
476 static wxNativeFont
wxLoadQueryFont(int pointSize
,
481 const wxString
& facename
,
482 const wxString
& xregistry
,
483 const wxString
& xencoding
,
484 wxString
* xFontName
);
486 // ============================================================================
488 // ============================================================================
490 // ----------------------------------------------------------------------------
491 // wxNativeEncodingInfo
492 // ----------------------------------------------------------------------------
494 // convert to/from the string representation: format is
495 // encodingid;registry;encoding[;facename]
496 bool wxNativeEncodingInfo::FromString(const wxString
& s
)
498 // use ";", not "-" because it may be part of encoding name
499 wxStringTokenizer
tokenizer(s
, wxT(";"));
501 wxString encid
= tokenizer
.GetNextToken();
503 if ( !encid
.ToLong(&enc
) )
505 encoding
= (wxFontEncoding
)enc
;
507 xregistry
= tokenizer
.GetNextToken();
511 xencoding
= tokenizer
.GetNextToken();
516 facename
= tokenizer
.GetNextToken();
521 wxString
wxNativeEncodingInfo::ToString() const
524 s
<< (long)encoding
<< wxT(';') << xregistry
<< wxT(';') << xencoding
;
525 if ( !facename
.empty() )
527 s
<< wxT(';') << facename
;
533 // ----------------------------------------------------------------------------
535 // ----------------------------------------------------------------------------
537 void wxNativeFontInfo::Init()
542 bool wxNativeFontInfo::FromString(const wxString
& s
)
544 wxStringTokenizer
tokenizer(s
, wxT(";"));
547 wxString token
= tokenizer
.GetNextToken();
548 if ( token
!= wxT('0') )
551 xFontName
= tokenizer
.GetNextToken();
553 // this should be the end
554 if ( tokenizer
.HasMoreTokens() )
557 return FromXFontName(xFontName
);
560 wxString
wxNativeFontInfo::ToString() const
563 return wxString::Format(wxT("%d;%s"), 0, GetXFontName().c_str());
566 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
568 return FromXFontName(s
);
571 wxString
wxNativeFontInfo::ToUserString() const
573 return GetXFontName();
576 bool wxNativeFontInfo::HasElements() const
578 // we suppose that the foundry is never empty, so if it is it means that we
579 // had never parsed the XLFD
580 return !fontElements
[0].empty();
583 wxString
wxNativeFontInfo::GetXFontComponent(wxXLFDField field
) const
585 wxCHECK_MSG( field
< wxXLFD_MAX
, wxEmptyString
, wxT("invalid XLFD field") );
587 if ( !HasElements() )
589 if ( !const_cast<wxNativeFontInfo
*>(this)->FromXFontName(xFontName
) )
590 return wxEmptyString
;
593 return fontElements
[field
];
596 bool wxNativeFontInfo::FromXFontName(const wxString
& fontname
)
598 // TODO: we should be able to handle the font aliases here, but how?
599 wxStringTokenizer
tokenizer(fontname
, wxT("-"));
601 // skip the leading, usually empty field (font name registry)
602 if ( !tokenizer
.HasMoreTokens() )
605 (void)tokenizer
.GetNextToken();
607 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
609 if ( !tokenizer
.HasMoreTokens() )
611 // not enough elements in the XLFD - or maybe an alias
615 wxString field
= tokenizer
.GetNextToken();
616 if ( !field
.empty() && field
!= wxT('*') )
618 // we're really initialized now
622 fontElements
[n
] = field
;
625 // this should be all
626 if ( tokenizer
.HasMoreTokens() )
632 wxString
wxNativeFontInfo::GetXFontName() const
634 if ( xFontName
.empty() )
636 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
638 // replace the non specified elements with '*' except for the
639 // additional style which is usually just omitted
640 wxString elt
= fontElements
[n
];
641 if ( elt
.empty() && n
!= wxXLFD_ADDSTYLE
)
646 const_cast<wxNativeFontInfo
*>(this)->xFontName
<< wxT('-') << elt
;
654 wxNativeFontInfo::SetXFontComponent(wxXLFDField field
, const wxString
& value
)
656 wxCHECK_RET( field
< wxXLFD_MAX
, wxT("invalid XLFD field") );
658 // this class should be initialized with a valid font spec first and only
659 // then the fields may be modified!
660 wxASSERT_MSG( !IsDefault(), wxT("can't modify an uninitialized XLFD") );
662 if ( !HasElements() )
664 if ( !const_cast<wxNativeFontInfo
*>(this)->FromXFontName(xFontName
) )
666 wxFAIL_MSG( wxT("can't set font element for invalid XLFD") );
672 fontElements
[field
] = value
;
674 // invalidate the XFLD, it doesn't correspond to the font elements any more
678 void wxNativeFontInfo::SetXFontName(const wxString
& xFontName_
)
680 // invalidate the font elements, GetXFontComponent() will reparse the XLFD
681 fontElements
[0].clear();
683 xFontName
= xFontName_
;
688 int wxNativeFontInfo::GetPointSize() const
690 const wxString s
= GetXFontComponent(wxXLFD_POINTSIZE
);
692 // return -1 to indicate that the size is unknown
694 return s
.ToLong(&l
) ? l
: -1;
697 wxFontStyle
wxNativeFontInfo::GetStyle() const
699 const wxString s
= GetXFontComponent(wxXLFD_SLANT
);
701 if ( s
.length() != 1 )
703 // it is really unknown but we don't have any way to return it from
705 return wxFONTSTYLE_NORMAL
;
708 switch ( s
[0].GetValue() )
711 // again, unknown but consider normal by default
714 return wxFONTSTYLE_NORMAL
;
717 return wxFONTSTYLE_ITALIC
;
720 return wxFONTSTYLE_SLANT
;
724 wxFontWeight
wxNativeFontInfo::GetWeight() const
726 const wxString s
= GetXFontComponent(wxXLFD_WEIGHT
).MakeLower();
727 if ( s
.find(wxT("bold")) != wxString::npos
|| s
== wxT("black") )
728 return wxFONTWEIGHT_BOLD
;
729 else if ( s
== wxT("light") )
730 return wxFONTWEIGHT_LIGHT
;
732 return wxFONTWEIGHT_NORMAL
;
735 bool wxNativeFontInfo::GetUnderlined() const
737 // X fonts are never underlined
741 wxString
wxNativeFontInfo::GetFaceName() const
743 // wxWidgets facename probably more accurately corresponds to X family
744 return GetXFontComponent(wxXLFD_FAMILY
);
747 wxFontFamily
wxNativeFontInfo::GetFamily() const
749 // and wxWidgets family -- to X foundry, but we have to translate it to
750 // wxFontFamily somehow...
751 wxFAIL_MSG(wxT("not implemented")); // GetXFontComponent(wxXLFD_FOUNDRY);
753 return wxFONTFAMILY_DEFAULT
;
756 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
758 // we already have the code for this but need to refactor it first
759 wxFAIL_MSG( wxT("not implemented") );
761 return wxFONTENCODING_MAX
;
764 void wxNativeFontInfo::SetPointSize(int pointsize
)
766 SetXFontComponent(wxXLFD_POINTSIZE
, wxString::Format(wxT("%d"), pointsize
));
769 void wxNativeFontInfo::SetStyle(wxFontStyle style
)
774 case wxFONTSTYLE_ITALIC
:
778 case wxFONTSTYLE_SLANT
:
782 case wxFONTSTYLE_NORMAL
:
786 wxFAIL_MSG( wxT("unknown wxFontStyle in wxNativeFontInfo::SetStyle") );
790 SetXFontComponent(wxXLFD_SLANT
, s
);
793 void wxNativeFontInfo::SetWeight(wxFontWeight weight
)
798 case wxFONTWEIGHT_BOLD
:
802 case wxFONTWEIGHT_LIGHT
:
806 case wxFONTWEIGHT_NORMAL
:
811 wxFAIL_MSG( wxT("unknown wxFontWeight in wxNativeFontInfo::SetWeight") );
815 SetXFontComponent(wxXLFD_WEIGHT
, s
);
818 void wxNativeFontInfo::SetUnderlined(bool WXUNUSED(underlined
))
820 // can't do this under X
823 void wxNativeFontInfo::SetStrikethrough(bool WXUNUSED(strikethrough
))
825 // this is not supported by Pango fonts neither
828 bool wxNativeFontInfo::SetFaceName(const wxString
& facename
)
830 SetXFontComponent(wxXLFD_FAMILY
, facename
);
834 void wxNativeFontInfo::SetFamily(wxFontFamily
WXUNUSED(family
))
836 // wxFontFamily -> X foundry, anyone?
837 wxFAIL_MSG( wxT("not implemented") );
839 // SetXFontComponent(wxXLFD_FOUNDRY, ...);
842 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding
)
844 wxNativeEncodingInfo info
;
845 if ( wxGetNativeFontEncoding(encoding
, &info
) )
847 SetXFontComponent(wxXLFD_ENCODING
, info
.xencoding
);
848 SetXFontComponent(wxXLFD_REGISTRY
, info
.xregistry
);
852 // ----------------------------------------------------------------------------
854 // ----------------------------------------------------------------------------
856 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
857 wxNativeEncodingInfo
*info
)
859 wxCHECK_MSG( info
, false, wxT("bad pointer in wxGetNativeFontEncoding") );
861 if ( encoding
== wxFONTENCODING_DEFAULT
)
863 encoding
= wxFont::GetDefaultEncoding();
868 case wxFONTENCODING_ISO8859_1
:
869 case wxFONTENCODING_ISO8859_2
:
870 case wxFONTENCODING_ISO8859_3
:
871 case wxFONTENCODING_ISO8859_4
:
872 case wxFONTENCODING_ISO8859_5
:
873 case wxFONTENCODING_ISO8859_6
:
874 case wxFONTENCODING_ISO8859_7
:
875 case wxFONTENCODING_ISO8859_8
:
876 case wxFONTENCODING_ISO8859_9
:
877 case wxFONTENCODING_ISO8859_10
:
878 case wxFONTENCODING_ISO8859_11
:
879 case wxFONTENCODING_ISO8859_12
:
880 case wxFONTENCODING_ISO8859_13
:
881 case wxFONTENCODING_ISO8859_14
:
882 case wxFONTENCODING_ISO8859_15
:
884 int cp
= encoding
- wxFONTENCODING_ISO8859_1
+ 1;
885 info
->xregistry
= wxT("iso8859");
886 info
->xencoding
.Printf(wxT("%d"), cp
);
890 case wxFONTENCODING_UTF8
:
891 info
->xregistry
= wxT("iso10646");
892 info
->xencoding
= wxT("*");
895 case wxFONTENCODING_GB2312
:
896 info
->xregistry
= wxT("GB2312"); // or the otherway round?
897 info
->xencoding
= wxT("*");
900 case wxFONTENCODING_KOI8
:
901 case wxFONTENCODING_KOI8_U
:
902 info
->xregistry
= wxT("koi8");
904 // we don't make distinction between koi8-r, koi8-u and koi8-ru (so far)
905 info
->xencoding
= wxT("*");
908 case wxFONTENCODING_CP1250
:
909 case wxFONTENCODING_CP1251
:
910 case wxFONTENCODING_CP1252
:
911 case wxFONTENCODING_CP1253
:
912 case wxFONTENCODING_CP1254
:
913 case wxFONTENCODING_CP1255
:
914 case wxFONTENCODING_CP1256
:
915 case wxFONTENCODING_CP1257
:
917 int cp
= encoding
- wxFONTENCODING_CP1250
+ 1250;
918 info
->xregistry
= wxT("microsoft");
919 info
->xencoding
.Printf(wxT("cp%d"), cp
);
923 case wxFONTENCODING_EUC_JP
:
924 case wxFONTENCODING_SHIFT_JIS
:
925 info
->xregistry
= "jis*";
926 info
->xencoding
= "*";
929 case wxFONTENCODING_SYSTEM
:
931 info
->xencoding
= wxT("*");
935 // don't know how to translate this encoding into X fontspec
939 info
->encoding
= encoding
;
944 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
947 fontspec
.Printf(wxT("-*-%s-*-*-*-*-*-*-*-*-*-*-%s-%s"),
948 !info
.facename
? wxT("*") : info
.facename
.c_str(),
949 info
.xregistry
.c_str(),
950 info
.xencoding
.c_str());
952 return wxTestFontSpec(fontspec
);
955 // ----------------------------------------------------------------------------
956 // X-specific functions
957 // ----------------------------------------------------------------------------
959 wxNativeFont
wxLoadQueryNearestFont(int pointSize
,
964 const wxString
&facename
,
965 wxFontEncoding encoding
,
968 if ( encoding
== wxFONTENCODING_DEFAULT
)
970 encoding
= wxFont::GetDefaultEncoding();
973 // first determine the encoding - if the font doesn't exist at all in this
974 // encoding, it's useless to do all other approximations (i.e. size,
975 // family &c don't matter much)
976 wxNativeEncodingInfo info
;
977 if ( encoding
== wxFONTENCODING_SYSTEM
)
979 // This will always work so we don't test to save time
980 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
984 if ( !wxGetNativeFontEncoding(encoding
, &info
) ||
985 !wxTestFontEncoding(info
) )
988 if ( !wxFontMapper::Get()->GetAltForEncoding(encoding
, &info
) )
989 #endif // wxUSE_FONTMAP
991 // unspported encoding - replace it with the default
993 // NB: we can't just return 0 from here because wxGTK code doesn't
994 // check for it (i.e. it supposes that we'll always succeed),
995 // so it would provoke a crash
996 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
1001 // OK, we have the correct xregistry/xencoding in info structure
1002 wxNativeFont font
= 0;
1004 // if we already have the X font name, try to use it
1005 if( xFontName
&& !xFontName
->empty() )
1008 // Make sure point size is correct for scale factor.
1010 wxStringTokenizer
tokenizer(*xFontName
, wxT("-"), wxTOKEN_RET_DELIMS
);
1011 wxString newFontName
;
1013 for(int i
= 0; i
< 8; i
++)
1014 newFontName
+= tokenizer
.NextToken();
1016 (void) tokenizer
.NextToken();
1018 newFontName
+= wxString::Format(wxT("%d-"), pointSize
);
1020 while(tokenizer
.HasMoreTokens())
1021 newFontName
+= tokenizer
.GetNextToken();
1023 font
= wxLoadFont(newFontName
);
1026 *xFontName
= newFontName
;
1031 // search up and down by stepsize 10
1032 int max_size
= pointSize
+ 20 * (1 + (pointSize
/180));
1033 int min_size
= pointSize
- 20 * (1 + (pointSize
/180));
1035 int i
, round
; // counters
1037 // first round: search for equal, then for smaller and for larger size
1038 // with the given weight and style
1039 int testweight
= weight
;
1040 int teststyle
= style
;
1042 for ( round
= 0; round
< 3; round
++ )
1044 // second round: use normal weight
1047 if ( testweight
!= wxNORMAL
)
1049 testweight
= wxNORMAL
;
1053 ++round
; // fall through to third round
1057 // third round: ... and use normal style
1060 if ( teststyle
!= wxNORMAL
)
1062 teststyle
= wxNORMAL
;
1069 // Search for equal or smaller size (approx.)
1070 for ( i
= pointSize
; !font
&& i
>= 10 && i
>= min_size
; i
-= 10 )
1072 font
= wxLoadQueryFont(i
, family
, teststyle
, testweight
, underlined
,
1073 facename
, info
.xregistry
, info
.xencoding
,
1077 // Search for larger size (approx.)
1078 for ( i
= pointSize
+ 10; !font
&& i
<= max_size
; i
+= 10 )
1080 font
= wxLoadQueryFont(i
, family
, teststyle
, testweight
, underlined
,
1081 facename
, info
.xregistry
, info
.xencoding
,
1086 // Try default family
1087 if ( !font
&& family
!= wxDEFAULT
)
1089 font
= wxLoadQueryFont(pointSize
, wxDEFAULT
, style
, weight
,
1090 underlined
, facename
,
1091 info
.xregistry
, info
.xencoding
,
1095 // ignore size, family, style and weight but try to find font with the
1096 // given facename and encoding
1099 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1100 underlined
, facename
,
1101 info
.xregistry
, info
.xencoding
,
1104 // ignore family as well
1107 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1108 underlined
, wxEmptyString
,
1109 info
.xregistry
, info
.xencoding
,
1112 // if it still failed, try to get the font of any size but
1113 // with the requested encoding: this can happen if the
1114 // encoding is only available in one size which happens to be
1115 // different from 120
1118 font
= wxLoadQueryFont(-1, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1119 false, wxEmptyString
,
1120 info
.xregistry
, info
.xencoding
,
1123 // this should never happen as we had tested for it in the
1124 // very beginning, but if it does, do return something non
1125 // NULL or we'd crash in wxFont code
1128 wxFAIL_MSG( wxT("this encoding should be available!") );
1130 font
= wxLoadQueryFont(-1,
1131 wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1132 false, wxEmptyString
,
1144 // ----------------------------------------------------------------------------
1145 // private functions
1146 // ----------------------------------------------------------------------------
1148 // returns true if there are any fonts matching this font spec
1149 static bool wxTestFontSpec(const wxString
& fontspec
)
1151 // some X servers will fail to load this font because there are too many
1152 // matches so we must test explicitly for this
1153 if ( fontspec
== wxT("-*-*-*-*-*-*-*-*-*-*-*-*-*-*") )
1158 wxNativeFont test
= (wxNativeFont
) g_fontHash
->Get( fontspec
);
1164 test
= wxLoadFont(fontspec
);
1165 g_fontHash
->Put( fontspec
, (wxObject
*) test
);
1179 static wxNativeFont
wxLoadQueryFont(int pointSize
,
1183 bool WXUNUSED(underlined
),
1184 const wxString
& facename
,
1185 const wxString
& xregistry
,
1186 const wxString
& xencoding
,
1187 wxString
* xFontName
)
1192 case wxDECORATIVE
: xfamily
= wxT("lucida"); break;
1193 case wxROMAN
: xfamily
= wxT("times"); break;
1194 case wxMODERN
: xfamily
= wxT("courier"); break;
1195 case wxSWISS
: xfamily
= wxT("helvetica"); break;
1196 case wxTELETYPE
: xfamily
= wxT("lucidatypewriter"); break;
1197 case wxSCRIPT
: xfamily
= wxT("utopia"); break;
1198 default: xfamily
= wxT("*");
1206 xweight
= MWLF_WEIGHT_BOLD
;
1211 xweight
= MWLF_WEIGHT_LIGHT
;
1216 xweight
= MWLF_WEIGHT_NORMAL
;
1222 xweight
= MWLF_WEIGHT_DEFAULT
;
1226 GR_SCREEN_INFO screenInfo
;
1227 GrGetScreenInfo(& screenInfo
);
1229 int yPixelsPerCM
= screenInfo
.ydpcm
;
1231 // A point is 1/72 of an inch.
1232 // An inch is 2.541 cm.
1233 // So pixelHeight = (pointSize / 72) (inches) * 2.541 (for cm) * yPixelsPerCM (for pixels)
1234 // In fact pointSize is 10 * the normal point size so
1237 int pixelHeight
= (int) ( (((float)pointSize
) / 720.0) * 2.541 * (float) yPixelsPerCM
) ;
1239 // An alternative: assume that the screen is 72 dpi.
1240 //int pixelHeight = (int) (((float)pointSize / 720.0) * 72.0) ;
1241 //int pixelHeight = (int) ((float)pointSize / 10.0) ;
1244 logFont
.lfHeight
= pixelHeight
;
1245 logFont
.lfWidth
= 0;
1246 logFont
.lfEscapement
= 0;
1247 logFont
.lfOrientation
= 0;
1248 logFont
.lfWeight
= xweight
;
1249 logFont
.lfItalic
= (style
== wxNORMAL
? 0 : 1) ;
1250 logFont
.lfUnderline
= 0;
1251 logFont
.lfStrikeOut
= 0;
1252 logFont
.lfCharSet
= MWLF_CHARSET_DEFAULT
; // TODO: select appropriate one
1253 logFont
.lfOutPrecision
= MWLF_TYPE_DEFAULT
;
1254 logFont
.lfClipPrecision
= 0; // Not used
1255 logFont
.lfRoman
= (family
== wxROMAN
? 1 : 0) ;
1256 logFont
.lfSerif
= (family
== wxSWISS
? 0 : 1) ;
1257 logFont
.lfSansSerif
= !logFont
.lfSerif
;
1258 logFont
.lfModern
= (family
== wxMODERN
? 1 : 0) ;
1259 logFont
.lfProportional
= (family
== wxTELETYPE
? 0 : 1) ;
1260 logFont
.lfOblique
= 0;
1261 logFont
.lfSmallCaps
= 0;
1262 logFont
.lfPitch
= 0; // 0 = default
1263 strcpy(logFont
.lfFaceName
, facename
.c_str());
1265 XFontStruct
* fontInfo
= (XFontStruct
*) malloc(sizeof(XFontStruct
));
1266 fontInfo
->fid
= GrCreateFont((GR_CHAR
*) facename
.c_str(), pixelHeight
, & logFont
);
1267 GrGetFontInfo(fontInfo
->fid
, & fontInfo
->info
);
1268 return (wxNativeFont
) fontInfo
;
1272 if (!facename
.empty())
1274 fontSpec
.Printf(wxT("-*-%s-*-*-normal-*-*-*-*-*-*-*-*-*"),
1277 if ( wxTestFontSpec(fontSpec
) )
1281 //else: no such family, use default one instead
1288 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1290 if ( wxTestFontSpec(fontSpec
) )
1295 // fall through - try wxITALIC now
1298 fontSpec
.Printf(wxT("-*-%s-*-i-*-*-*-*-*-*-*-*-*-*"),
1300 if ( wxTestFontSpec(fontSpec
) )
1304 else if ( style
== wxITALIC
) // and not wxSLANT
1307 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1309 if ( wxTestFontSpec(fontSpec
) )
1315 // no italic, no slant - leave default
1322 wxFAIL_MSG(wxT("unknown font style"));
1323 // fall back to normal
1335 fontSpec
.Printf(wxT("-*-%s-bold-*-*-*-*-*-*-*-*-*-*-*"),
1337 if ( wxTestFontSpec(fontSpec
) )
1339 xweight
= wxT("bold");
1342 fontSpec
.Printf(wxT("-*-%s-heavy-*-*-*-*-*-*-*-*-*-*-*"),
1344 if ( wxTestFontSpec(fontSpec
) )
1346 xweight
= wxT("heavy");
1349 fontSpec
.Printf(wxT("-*-%s-extrabold-*-*-*-*-*-*-*-*-*-*-*"),
1351 if ( wxTestFontSpec(fontSpec
) )
1353 xweight
= wxT("extrabold");
1356 fontSpec
.Printf(wxT("-*-%s-demibold-*-*-*-*-*-*-*-*-*-*-*"),
1358 if ( wxTestFontSpec(fontSpec
) )
1360 xweight
= wxT("demibold");
1363 fontSpec
.Printf(wxT("-*-%s-black-*-*-*-*-*-*-*-*-*-*-*"),
1365 if ( wxTestFontSpec(fontSpec
) )
1367 xweight
= wxT("black");
1370 fontSpec
.Printf(wxT("-*-%s-ultrablack-*-*-*-*-*-*-*-*-*-*-*"),
1372 if ( wxTestFontSpec(fontSpec
) )
1374 xweight
= wxT("ultrablack");
1381 fontSpec
.Printf(wxT("-*-%s-light-*-*-*-*-*-*-*-*-*-*-*"),
1383 if ( wxTestFontSpec(fontSpec
) )
1385 xweight
= wxT("light");
1388 fontSpec
.Printf(wxT("-*-%s-thin-*-*-*-*-*-*-*-*-*-*-*"),
1390 if ( wxTestFontSpec(fontSpec
) )
1392 xweight
= wxT("thin");
1399 fontSpec
.Printf(wxT("-*-%s-medium-*-*-*-*-*-*-*-*-*-*-*"),
1401 if ( wxTestFontSpec(fontSpec
) )
1403 xweight
= wxT("medium");
1406 fontSpec
.Printf(wxT("-*-%s-normal-*-*-*-*-*-*-*-*-*-*-*"),
1408 if ( wxTestFontSpec(fontSpec
) )
1410 xweight
= wxT("normal");
1413 fontSpec
.Printf(wxT("-*-%s-regular-*-*-*-*-*-*-*-*-*-*-*"),
1415 if ( wxTestFontSpec(fontSpec
) )
1417 xweight
= wxT("regular");
1423 default: xweight
= wxT("*"); break;
1426 // if pointSize is -1, don't specify any
1428 if ( pointSize
== -1 )
1430 sizeSpec
= wxT('*');
1434 sizeSpec
.Printf(wxT("%d"), pointSize
);
1437 // construct the X font spec from our data
1438 fontSpec
.Printf(wxT("-*-%s-%s-%s-normal-*-*-%s-*-*-*-*-%s-%s"),
1439 xfamily
.c_str(), xweight
.c_str(), xstyle
.c_str(),
1440 sizeSpec
.c_str(), xregistry
.c_str(), xencoding
.c_str());
1443 *xFontName
= fontSpec
;
1445 return wxLoadFont(fontSpec
);
1450 // ----------------------------------------------------------------------------
1452 // ----------------------------------------------------------------------------
1454 class wxFontModule
: public wxModule
1461 DECLARE_DYNAMIC_CLASS(wxFontModule
)
1464 IMPLEMENT_DYNAMIC_CLASS(wxFontModule
, wxModule
)
1466 bool wxFontModule::OnInit()
1468 g_fontHash
= new wxHashTable( wxKEY_STRING
);
1473 void wxFontModule::OnExit()
1475 wxDELETE(g_fontHash
);
1478 #endif // GTK 2.0/1.x