1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/unix/fontutil.cpp
3 // Purpose: Font helper functions for wxX11, wxGTK, wxMotif
4 // Author: Vadim Zeitlin
7 // Copyright: (c) Vadim Zeitlin
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
26 #include "wx/fontutil.h"
30 #include "wx/font.h" // wxFont enums
32 #include "wx/utils.h" // for wxGetDisplay()
33 #include "wx/module.h"
36 #include "wx/encinfo.h"
37 #include "wx/fontmap.h"
38 #include "wx/tokenzr.h"
39 #include "wx/fontenum.h"
43 #include "pango/pango.h"
46 #include "wx/gtk/private.h"
47 extern GtkWidget
*wxGetRootWindow();
49 #define wxPANGO_CONV wxGTK_CONV_SYS
50 #define wxPANGO_CONV_BACK wxGTK_CONV_BACK_SYS
52 #include "wx/x11/private.h"
53 #include "wx/gtk/private/string.h"
55 #define wxPANGO_CONV(s) s.utf8_str()
56 #define wxPANGO_CONV_BACK(s) wxString::FromUTF8Unchecked(s)
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 void wxNativeFontInfo::Init()
67 m_strikethrough
= false;
70 void wxNativeFontInfo::Init(const wxNativeFontInfo
& info
)
74 description
= pango_font_description_copy(info
.description
);
75 m_underlined
= info
.GetUnderlined();
76 m_strikethrough
= info
.GetStrikethrough();
82 m_strikethrough
= false;
86 void wxNativeFontInfo::Free()
89 pango_font_description_free(description
);
92 int wxNativeFontInfo::GetPointSize() const
94 return pango_font_description_get_size( description
) / PANGO_SCALE
;
97 wxFontStyle
wxNativeFontInfo::GetStyle() const
99 wxFontStyle m_style
= wxFONTSTYLE_NORMAL
;
101 switch (pango_font_description_get_style( description
))
103 case PANGO_STYLE_NORMAL
:
104 m_style
= wxFONTSTYLE_NORMAL
;
106 case PANGO_STYLE_ITALIC
:
107 m_style
= wxFONTSTYLE_ITALIC
;
109 case PANGO_STYLE_OBLIQUE
:
110 m_style
= wxFONTSTYLE_SLANT
;
117 wxFontWeight
wxNativeFontInfo::GetWeight() const
119 // We seem to currently initialize only by string.
120 // In that case PANGO_FONT_MASK_WEIGHT is always set.
121 // if (!(pango_font_description_get_set_fields(description) & PANGO_FONT_MASK_WEIGHT))
122 // return wxFONTWEIGHT_NORMAL;
124 PangoWeight pango_weight
= pango_font_description_get_weight( description
);
126 // Until the API can be changed the following ranges of weight values are used:
127 // wxFONTWEIGHT_LIGHT: 100 .. 349 - range of 250
128 // wxFONTWEIGHT_NORMAL: 350 .. 599 - range of 250
129 // wxFONTWEIGHT_BOLD: 600 .. 900 - range of 301 (600 is "semibold" already)
131 if (pango_weight
>= 600)
132 return wxFONTWEIGHT_BOLD
;
134 if (pango_weight
< 350)
135 return wxFONTWEIGHT_LIGHT
;
137 return wxFONTWEIGHT_NORMAL
;
140 bool wxNativeFontInfo::GetUnderlined() const
145 bool wxNativeFontInfo::GetStrikethrough() const
147 return m_strikethrough
;
150 wxString
wxNativeFontInfo::GetFaceName() const
152 // the Pango "family" is the wx "face name"
153 return wxPANGO_CONV_BACK(pango_font_description_get_family(description
));
156 wxFontFamily
wxNativeFontInfo::GetFamily() const
158 wxFontFamily ret
= wxFONTFAMILY_UNKNOWN
;
160 const char *family_name
= pango_font_description_get_family( description
);
162 // note: not passing -1 as the 2nd parameter to g_ascii_strdown to work
163 // around a bug in the 64-bit glib shipped with solaris 10, -1 causes it
164 // to try to allocate 2^32 bytes.
167 wxGtkString
family_text(g_ascii_strdown(family_name
, strlen(family_name
)));
169 // Check for some common fonts, to salvage what we can from the current
170 // win32 centric wxFont API:
171 if (wxStrnicmp( family_text
, "monospace", 9 ) == 0)
172 ret
= wxFONTFAMILY_TELETYPE
; // begins with "Monospace"
173 else if (wxStrnicmp( family_text
, "courier", 7 ) == 0)
174 ret
= wxFONTFAMILY_TELETYPE
; // begins with "Courier"
175 #if defined(__WXGTK20__) || defined(HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE)
178 PangoFontFamily
**families
;
179 PangoFontFamily
*family
= NULL
;
181 pango_context_list_families(
183 gtk_widget_get_pango_context( wxGetRootWindow() ),
185 wxTheApp
->GetPangoContext(),
187 &families
, &n_families
);
189 for (int i
= 0; i
< n_families
; ++i
)
191 if (g_ascii_strcasecmp(pango_font_family_get_name( families
[i
] ),
192 pango_font_description_get_family( description
)) == 0 )
194 family
= families
[i
];
201 // Some gtk+ systems might query for a non-existing font from
202 // wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT) on initialization,
203 // don't assert until wxSystemSettings::GetFont is checked for this - MR
204 // wxASSERT_MSG( family, "No appropriate PangoFontFamily found for ::description" );
206 if (family
!= NULL
&& pango_font_family_is_monospace( family
))
207 ret
= wxFONTFAMILY_TELETYPE
; // is deemed a monospace font by pango
209 #endif // GTK+ 2 || HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE
211 if (ret
== wxFONTFAMILY_UNKNOWN
)
213 if (strstr( family_text
, "sans" ) != NULL
|| strstr( family_text
, "Sans" ) != NULL
)
214 // checked before serif, so that "* Sans Serif" fonts are detected correctly
215 ret
= wxFONTFAMILY_SWISS
; // contains "Sans"
216 else if (strstr( family_text
, "serif" ) != NULL
|| strstr( family_text
, "Serif" ) != NULL
)
217 ret
= wxFONTFAMILY_ROMAN
; // contains "Serif"
218 else if (wxStrnicmp( family_text
, "times", 5 ) == 0)
219 ret
= wxFONTFAMILY_ROMAN
; // begins with "Times"
220 else if (wxStrnicmp( family_text
, "old", 3 ) == 0)
221 ret
= wxFONTFAMILY_DECORATIVE
; // begins with "Old" - "Old English", "Old Town"
227 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
229 return wxFONTENCODING_SYSTEM
;
232 void wxNativeFontInfo::SetPointSize(int pointsize
)
234 pango_font_description_set_size( description
, pointsize
* PANGO_SCALE
);
237 void wxNativeFontInfo::SetStyle(wxFontStyle style
)
241 case wxFONTSTYLE_ITALIC
:
242 pango_font_description_set_style( description
, PANGO_STYLE_ITALIC
);
244 case wxFONTSTYLE_SLANT
:
245 pango_font_description_set_style( description
, PANGO_STYLE_OBLIQUE
);
248 wxFAIL_MSG( "unknown font style" );
250 case wxFONTSTYLE_NORMAL
:
251 pango_font_description_set_style( description
, PANGO_STYLE_NORMAL
);
256 void wxNativeFontInfo::SetWeight(wxFontWeight weight
)
260 case wxFONTWEIGHT_BOLD
:
261 pango_font_description_set_weight(description
, PANGO_WEIGHT_BOLD
);
263 case wxFONTWEIGHT_LIGHT
:
264 pango_font_description_set_weight(description
, PANGO_WEIGHT_LIGHT
);
267 wxFAIL_MSG( "unknown font weight" );
269 case wxFONTWEIGHT_NORMAL
:
270 pango_font_description_set_weight(description
, PANGO_WEIGHT_NORMAL
);
274 void wxNativeFontInfo::SetUnderlined(bool underlined
)
276 // Pango doesn't have the underlined attribute so we store it separately
277 // (and handle it specially in wxWindowDCImpl::DoDrawText()).
278 m_underlined
= underlined
;
281 void wxNativeFontInfo::SetStrikethrough(bool strikethrough
)
283 // As with the underlined attribute above, we handle this one separately as
284 // Pango doesn't support it as part of the font description.
285 m_strikethrough
= strikethrough
;
288 bool wxNativeFontInfo::SetFaceName(const wxString
& facename
)
290 pango_font_description_set_family(description
, wxPANGO_CONV(facename
));
292 // we return true because Pango doesn't tell us if the call failed or not;
293 // instead on wxGTK wxFont::SetFaceName() will call wxFontBase::SetFaceName()
294 // which does the check
298 void wxNativeFontInfo::SetFamily(wxFontFamily family
)
300 wxArrayString facename
;
302 // the list of fonts associated with a family was partially
303 // taken from http://www.codestyle.org/css/font-family
307 case wxFONTFAMILY_SCRIPT
:
308 // corresponds to the cursive font family in the page linked above
309 facename
.Add(wxS("URW Chancery L"));
310 facename
.Add(wxS("Comic Sans MS"));
313 case wxFONTFAMILY_DECORATIVE
:
314 // corresponds to the fantasy font family in the page linked above
315 facename
.Add(wxS("Impact"));
318 case wxFONTFAMILY_ROMAN
:
319 // corresponds to the serif font family in the page linked above
320 facename
.Add(wxS("Serif"));
321 facename
.Add(wxS("DejaVu Serif"));
322 facename
.Add(wxS("DejaVu LGC Serif"));
323 facename
.Add(wxS("Bitstream Vera Serif"));
324 facename
.Add(wxS("Liberation Serif"));
325 facename
.Add(wxS("FreeSerif"));
326 facename
.Add(wxS("Luxi Serif"));
327 facename
.Add(wxS("Times New Roman"));
328 facename
.Add(wxS("Century Schoolbook L"));
329 facename
.Add(wxS("URW Bookman L"));
330 facename
.Add(wxS("URW Palladio L"));
331 facename
.Add(wxS("Times"));
334 case wxFONTFAMILY_TELETYPE
:
335 case wxFONTFAMILY_MODERN
:
336 // corresponds to the monospace font family in the page linked above
337 facename
.Add(wxS("Monospace"));
338 facename
.Add(wxS("DejaVu Sans Mono"));
339 facename
.Add(wxS("DejaVu LGC Sans Mono"));
340 facename
.Add(wxS("Bitstream Vera Sans Mono"));
341 facename
.Add(wxS("Liberation Mono"));
342 facename
.Add(wxS("FreeMono"));
343 facename
.Add(wxS("Luxi Mono"));
344 facename
.Add(wxS("Courier New"));
345 facename
.Add(wxS("Lucida Sans Typewriter"));
346 facename
.Add(wxS("Nimbus Mono L"));
347 facename
.Add(wxS("Andale Mono"));
348 facename
.Add(wxS("Courier"));
351 case wxFONTFAMILY_SWISS
:
352 case wxFONTFAMILY_DEFAULT
:
354 // corresponds to the sans-serif font family in the page linked above
355 facename
.Add(wxS("Sans"));
356 facename
.Add(wxS("DejaVu Sans"));
357 facename
.Add(wxS("DejaVu LGC Sans"));
358 facename
.Add(wxS("Bitstream Vera Sans"));
359 facename
.Add(wxS("Liberation Sans"));
360 facename
.Add(wxS("FreeSans"));
361 facename
.Add(wxS("Luxi Sans"));
362 facename
.Add(wxS("Arial"));
363 facename
.Add(wxS("Lucida Sans"));
364 facename
.Add(wxS("Nimbus Sans L"));
365 facename
.Add(wxS("URW Gothic L"));
369 SetFaceName(facename
);
372 void wxNativeFontInfo::SetEncoding(wxFontEncoding
WXUNUSED(encoding
))
374 wxFAIL_MSG( "not implemented: Pango encoding is always UTF8" );
377 bool wxNativeFontInfo::FromString(const wxString
& s
)
381 // Pango font description doesn't have 'underlined' or 'strikethrough'
382 // attributes, so we handle them specially by extracting them from the
383 // string before passing it to Pango.
384 m_underlined
= str
.StartsWith(wxS("underlined "), &str
);
385 m_strikethrough
= str
.StartsWith(wxS("strikethrough "), &str
);
388 pango_font_description_free( description
);
390 // there is a bug in at least pango <= 1.13 which makes it (or its backends)
391 // segfault for very big point sizes and for negative point sizes.
392 // To workaround that bug for pango <= 1.13
393 // (see http://bugzilla.gnome.org/show_bug.cgi?id=340229)
394 // we do the check on the size here using same (arbitrary) limits used by
395 // pango > 1.13. Note that the segfault could happen also for pointsize
396 // smaller than this limit !!
397 const size_t pos
= str
.find_last_of(wxS(" "));
399 if ( pos
!= wxString::npos
&& wxString(str
, pos
+ 1).ToDouble(&size
) )
404 else if ( size
>= 1E6
)
405 sizeStr
= wxS("1E6");
407 if ( !sizeStr
.empty() )
409 // replace the old size with the adjusted one
410 str
= wxString(s
, 0, pos
) + sizeStr
;
414 description
= pango_font_description_from_string(wxPANGO_CONV(str
));
417 // ensure a valid facename is selected
418 if (!wxFontEnumerator::IsValidFacename(GetFaceName()))
419 SetFaceName(wxNORMAL_FONT
->GetFaceName());
420 #endif // wxUSE_FONTENUM
425 wxString
wxNativeFontInfo::ToString() const
427 wxGtkString
str(pango_font_description_to_string( description
));
428 wxString desc
= wxPANGO_CONV_BACK(str
);
430 // Augment the string with the attributes not handled by Pango.
432 // Notice that we must add them in the same order they are extracted in
433 // FromString() above.
435 desc
.insert(0, wxS("strikethrough "));
437 desc
.insert(0, wxS("underlined "));
442 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
444 return FromString( s
);
447 wxString
wxNativeFontInfo::ToUserString() const
456 #pragma message disable nosimpint
459 #include <X11/Xlib.h>
462 #pragma message enable nosimpint
465 #elif defined(__WXGTK__)
466 // we have to declare struct tm to avoid problems with first forward
467 // declaring it in C code (glib.h included from gdk.h does it) and then
468 // defining it when time.h is included from the headers below - this is
469 // known not to work at least with Sun CC 6.01
476 // ----------------------------------------------------------------------------
478 // ----------------------------------------------------------------------------
480 static wxHashTable
*g_fontHash
= NULL
;
482 // ----------------------------------------------------------------------------
484 // ----------------------------------------------------------------------------
486 // define the functions to create and destroy native fonts for this toolkit
488 wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
490 return XLoadQueryFont((Display
*)wxGetDisplay(), fontSpec
);
493 inline void wxFreeFont(wxNativeFont font
)
495 XFreeFont((Display
*)wxGetDisplay(), (XFontStruct
*)font
);
497 #elif defined(__WXGTK__)
498 wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
500 // VZ: we should use gdk_fontset_load() instead of gdk_font_load()
501 // here to be able to display Japanese fonts correctly (at least
502 // this is what people report) but unfortunately doing it results
503 // in tons of warnings when using GTK with "normal" European
504 // languages and so we can't always do it and I don't know enough
505 // to determine when should this be done... (FIXME)
506 return gdk_font_load( wxConvertWX2MB(fontSpec
) );
509 inline void wxFreeFont(wxNativeFont font
)
511 gdk_font_unref(font
);
514 #error "Unknown GUI toolkit"
517 static bool wxTestFontSpec(const wxString
& fontspec
);
519 static wxNativeFont
wxLoadQueryFont(int pointSize
,
524 const wxString
& facename
,
525 const wxString
& xregistry
,
526 const wxString
& xencoding
,
527 wxString
* xFontName
);
529 // ============================================================================
531 // ============================================================================
533 // ----------------------------------------------------------------------------
534 // wxNativeEncodingInfo
535 // ----------------------------------------------------------------------------
537 // convert to/from the string representation: format is
538 // encodingid;registry;encoding[;facename]
539 bool wxNativeEncodingInfo::FromString(const wxString
& s
)
541 // use ";", not "-" because it may be part of encoding name
542 wxStringTokenizer
tokenizer(s
, wxT(";"));
544 wxString encid
= tokenizer
.GetNextToken();
546 if ( !encid
.ToLong(&enc
) )
548 encoding
= (wxFontEncoding
)enc
;
550 xregistry
= tokenizer
.GetNextToken();
554 xencoding
= tokenizer
.GetNextToken();
559 facename
= tokenizer
.GetNextToken();
564 wxString
wxNativeEncodingInfo::ToString() const
567 s
<< (long)encoding
<< wxT(';') << xregistry
<< wxT(';') << xencoding
;
568 if ( !facename
.empty() )
570 s
<< wxT(';') << facename
;
576 // ----------------------------------------------------------------------------
578 // ----------------------------------------------------------------------------
580 void wxNativeFontInfo::Init()
585 bool wxNativeFontInfo::FromString(const wxString
& s
)
587 wxStringTokenizer
tokenizer(s
, wxT(";"));
590 wxString token
= tokenizer
.GetNextToken();
591 if ( token
!= wxT('0') )
594 xFontName
= tokenizer
.GetNextToken();
596 // this should be the end
597 if ( tokenizer
.HasMoreTokens() )
600 return FromXFontName(xFontName
);
603 wxString
wxNativeFontInfo::ToString() const
606 return wxString::Format(wxT("%d;%s"), 0, GetXFontName().c_str());
609 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
611 return FromXFontName(s
);
614 wxString
wxNativeFontInfo::ToUserString() const
616 return GetXFontName();
619 bool wxNativeFontInfo::HasElements() const
621 // we suppose that the foundry is never empty, so if it is it means that we
622 // had never parsed the XLFD
623 return !fontElements
[0].empty();
626 wxString
wxNativeFontInfo::GetXFontComponent(wxXLFDField field
) const
628 wxCHECK_MSG( field
< wxXLFD_MAX
, wxEmptyString
, wxT("invalid XLFD field") );
630 if ( !HasElements() )
632 if ( !const_cast<wxNativeFontInfo
*>(this)->FromXFontName(xFontName
) )
633 return wxEmptyString
;
636 return fontElements
[field
];
639 bool wxNativeFontInfo::FromXFontName(const wxString
& fontname
)
641 // TODO: we should be able to handle the font aliases here, but how?
642 wxStringTokenizer
tokenizer(fontname
, wxT("-"));
644 // skip the leading, usually empty field (font name registry)
645 if ( !tokenizer
.HasMoreTokens() )
648 (void)tokenizer
.GetNextToken();
650 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
652 if ( !tokenizer
.HasMoreTokens() )
654 // not enough elements in the XLFD - or maybe an alias
658 wxString field
= tokenizer
.GetNextToken();
659 if ( !field
.empty() && field
!= wxT('*') )
661 // we're really initialized now
665 fontElements
[n
] = field
;
668 // this should be all
669 if ( tokenizer
.HasMoreTokens() )
675 wxString
wxNativeFontInfo::GetXFontName() const
677 if ( xFontName
.empty() )
679 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
681 // replace the non specified elements with '*' except for the
682 // additional style which is usually just omitted
683 wxString elt
= fontElements
[n
];
684 if ( elt
.empty() && n
!= wxXLFD_ADDSTYLE
)
689 const_cast<wxNativeFontInfo
*>(this)->xFontName
<< wxT('-') << elt
;
697 wxNativeFontInfo::SetXFontComponent(wxXLFDField field
, const wxString
& value
)
699 wxCHECK_RET( field
< wxXLFD_MAX
, wxT("invalid XLFD field") );
701 // this class should be initialized with a valid font spec first and only
702 // then the fields may be modified!
703 wxASSERT_MSG( !IsDefault(), wxT("can't modify an uninitialized XLFD") );
705 if ( !HasElements() )
707 if ( !const_cast<wxNativeFontInfo
*>(this)->FromXFontName(xFontName
) )
709 wxFAIL_MSG( wxT("can't set font element for invalid XLFD") );
715 fontElements
[field
] = value
;
717 // invalidate the XFLD, it doesn't correspond to the font elements any more
721 void wxNativeFontInfo::SetXFontName(const wxString
& xFontName_
)
723 // invalidate the font elements, GetXFontComponent() will reparse the XLFD
724 fontElements
[0].clear();
726 xFontName
= xFontName_
;
731 int wxNativeFontInfo::GetPointSize() const
733 const wxString s
= GetXFontComponent(wxXLFD_POINTSIZE
);
735 // return -1 to indicate that the size is unknown
737 return s
.ToLong(&l
) ? l
: -1;
740 wxFontStyle
wxNativeFontInfo::GetStyle() const
742 const wxString s
= GetXFontComponent(wxXLFD_SLANT
);
744 if ( s
.length() != 1 )
746 // it is really unknown but we don't have any way to return it from
748 return wxFONTSTYLE_NORMAL
;
751 switch ( s
[0].GetValue() )
754 // again, unknown but consider normal by default
757 return wxFONTSTYLE_NORMAL
;
760 return wxFONTSTYLE_ITALIC
;
763 return wxFONTSTYLE_SLANT
;
767 wxFontWeight
wxNativeFontInfo::GetWeight() const
769 const wxString s
= GetXFontComponent(wxXLFD_WEIGHT
).MakeLower();
770 if ( s
.find(wxT("bold")) != wxString::npos
|| s
== wxT("black") )
771 return wxFONTWEIGHT_BOLD
;
772 else if ( s
== wxT("light") )
773 return wxFONTWEIGHT_LIGHT
;
775 return wxFONTWEIGHT_NORMAL
;
778 bool wxNativeFontInfo::GetUnderlined() const
780 // X fonts are never underlined
784 wxString
wxNativeFontInfo::GetFaceName() const
786 // wxWidgets facename probably more accurately corresponds to X family
787 return GetXFontComponent(wxXLFD_FAMILY
);
790 wxFontFamily
wxNativeFontInfo::GetFamily() const
792 // and wxWidgets family -- to X foundry, but we have to translate it to
793 // wxFontFamily somehow...
794 wxFAIL_MSG(wxT("not implemented")); // GetXFontComponent(wxXLFD_FOUNDRY);
796 return wxFONTFAMILY_DEFAULT
;
799 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
801 // we already have the code for this but need to refactor it first
802 wxFAIL_MSG( wxT("not implemented") );
804 return wxFONTENCODING_MAX
;
807 void wxNativeFontInfo::SetPointSize(int pointsize
)
809 SetXFontComponent(wxXLFD_POINTSIZE
, wxString::Format(wxT("%d"), pointsize
));
812 void wxNativeFontInfo::SetStyle(wxFontStyle style
)
817 case wxFONTSTYLE_ITALIC
:
821 case wxFONTSTYLE_SLANT
:
825 case wxFONTSTYLE_NORMAL
:
829 wxFAIL_MSG( wxT("unknown wxFontStyle in wxNativeFontInfo::SetStyle") );
833 SetXFontComponent(wxXLFD_SLANT
, s
);
836 void wxNativeFontInfo::SetWeight(wxFontWeight weight
)
841 case wxFONTWEIGHT_BOLD
:
845 case wxFONTWEIGHT_LIGHT
:
849 case wxFONTWEIGHT_NORMAL
:
854 wxFAIL_MSG( wxT("unknown wxFontWeight in wxNativeFontInfo::SetWeight") );
858 SetXFontComponent(wxXLFD_WEIGHT
, s
);
861 void wxNativeFontInfo::SetUnderlined(bool WXUNUSED(underlined
))
863 // can't do this under X
866 void wxNativeFontInfo::SetStrikethrough(bool WXUNUSED(strikethrough
))
868 // this is not supported by Pango fonts neither
871 bool wxNativeFontInfo::SetFaceName(const wxString
& facename
)
873 SetXFontComponent(wxXLFD_FAMILY
, facename
);
877 void wxNativeFontInfo::SetFamily(wxFontFamily
WXUNUSED(family
))
879 // wxFontFamily -> X foundry, anyone?
880 wxFAIL_MSG( wxT("not implemented") );
882 // SetXFontComponent(wxXLFD_FOUNDRY, ...);
885 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding
)
887 wxNativeEncodingInfo info
;
888 if ( wxGetNativeFontEncoding(encoding
, &info
) )
890 SetXFontComponent(wxXLFD_ENCODING
, info
.xencoding
);
891 SetXFontComponent(wxXLFD_REGISTRY
, info
.xregistry
);
895 // ----------------------------------------------------------------------------
897 // ----------------------------------------------------------------------------
899 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
900 wxNativeEncodingInfo
*info
)
902 wxCHECK_MSG( info
, false, wxT("bad pointer in wxGetNativeFontEncoding") );
904 if ( encoding
== wxFONTENCODING_DEFAULT
)
906 encoding
= wxFont::GetDefaultEncoding();
911 case wxFONTENCODING_ISO8859_1
:
912 case wxFONTENCODING_ISO8859_2
:
913 case wxFONTENCODING_ISO8859_3
:
914 case wxFONTENCODING_ISO8859_4
:
915 case wxFONTENCODING_ISO8859_5
:
916 case wxFONTENCODING_ISO8859_6
:
917 case wxFONTENCODING_ISO8859_7
:
918 case wxFONTENCODING_ISO8859_8
:
919 case wxFONTENCODING_ISO8859_9
:
920 case wxFONTENCODING_ISO8859_10
:
921 case wxFONTENCODING_ISO8859_11
:
922 case wxFONTENCODING_ISO8859_12
:
923 case wxFONTENCODING_ISO8859_13
:
924 case wxFONTENCODING_ISO8859_14
:
925 case wxFONTENCODING_ISO8859_15
:
927 int cp
= encoding
- wxFONTENCODING_ISO8859_1
+ 1;
928 info
->xregistry
= wxT("iso8859");
929 info
->xencoding
.Printf(wxT("%d"), cp
);
933 case wxFONTENCODING_UTF8
:
934 info
->xregistry
= wxT("iso10646");
935 info
->xencoding
= wxT("*");
938 case wxFONTENCODING_GB2312
:
939 info
->xregistry
= wxT("GB2312"); // or the otherway round?
940 info
->xencoding
= wxT("*");
943 case wxFONTENCODING_KOI8
:
944 case wxFONTENCODING_KOI8_U
:
945 info
->xregistry
= wxT("koi8");
947 // we don't make distinction between koi8-r, koi8-u and koi8-ru (so far)
948 info
->xencoding
= wxT("*");
951 case wxFONTENCODING_CP1250
:
952 case wxFONTENCODING_CP1251
:
953 case wxFONTENCODING_CP1252
:
954 case wxFONTENCODING_CP1253
:
955 case wxFONTENCODING_CP1254
:
956 case wxFONTENCODING_CP1255
:
957 case wxFONTENCODING_CP1256
:
958 case wxFONTENCODING_CP1257
:
960 int cp
= encoding
- wxFONTENCODING_CP1250
+ 1250;
961 info
->xregistry
= wxT("microsoft");
962 info
->xencoding
.Printf(wxT("cp%d"), cp
);
966 case wxFONTENCODING_EUC_JP
:
967 case wxFONTENCODING_SHIFT_JIS
:
968 info
->xregistry
= "jis*";
969 info
->xencoding
= "*";
972 case wxFONTENCODING_SYSTEM
:
974 info
->xencoding
= wxT("*");
978 // don't know how to translate this encoding into X fontspec
982 info
->encoding
= encoding
;
987 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
990 fontspec
.Printf(wxT("-*-%s-*-*-*-*-*-*-*-*-*-*-%s-%s"),
991 info
.facename
.empty() ? wxString("*") : info
.facename
,
995 return wxTestFontSpec(fontspec
);
998 // ----------------------------------------------------------------------------
999 // X-specific functions
1000 // ----------------------------------------------------------------------------
1002 wxNativeFont
wxLoadQueryNearestFont(int pointSize
,
1007 const wxString
&facename
,
1008 wxFontEncoding encoding
,
1009 wxString
* xFontName
)
1011 if ( encoding
== wxFONTENCODING_DEFAULT
)
1013 encoding
= wxFont::GetDefaultEncoding();
1016 // first determine the encoding - if the font doesn't exist at all in this
1017 // encoding, it's useless to do all other approximations (i.e. size,
1018 // family &c don't matter much)
1019 wxNativeEncodingInfo info
;
1020 if ( encoding
== wxFONTENCODING_SYSTEM
)
1022 // This will always work so we don't test to save time
1023 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
1027 if ( !wxGetNativeFontEncoding(encoding
, &info
) ||
1028 !wxTestFontEncoding(info
) )
1031 if ( !wxFontMapper::Get()->GetAltForEncoding(encoding
, &info
) )
1032 #endif // wxUSE_FONTMAP
1034 // unspported encoding - replace it with the default
1036 // NB: we can't just return 0 from here because wxGTK code doesn't
1037 // check for it (i.e. it supposes that we'll always succeed),
1038 // so it would provoke a crash
1039 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
1044 // OK, we have the correct xregistry/xencoding in info structure
1045 wxNativeFont font
= 0;
1047 // if we already have the X font name, try to use it
1048 if( xFontName
&& !xFontName
->empty() )
1051 // Make sure point size is correct for scale factor.
1053 wxStringTokenizer
tokenizer(*xFontName
, wxT("-"), wxTOKEN_RET_DELIMS
);
1054 wxString newFontName
;
1056 for(int i
= 0; i
< 8; i
++)
1057 newFontName
+= tokenizer
.NextToken();
1059 (void) tokenizer
.NextToken();
1061 newFontName
+= wxString::Format(wxT("%d-"), pointSize
);
1063 while(tokenizer
.HasMoreTokens())
1064 newFontName
+= tokenizer
.GetNextToken();
1066 font
= wxLoadFont(newFontName
);
1069 *xFontName
= newFontName
;
1074 // search up and down by stepsize 10
1075 int max_size
= pointSize
+ 20 * (1 + (pointSize
/180));
1076 int min_size
= pointSize
- 20 * (1 + (pointSize
/180));
1078 int i
, round
; // counters
1080 // first round: search for equal, then for smaller and for larger size
1081 // with the given weight and style
1082 int testweight
= weight
;
1083 int teststyle
= style
;
1085 for ( round
= 0; round
< 3; round
++ )
1087 // second round: use normal weight
1090 if ( testweight
!= wxNORMAL
)
1092 testweight
= wxNORMAL
;
1096 ++round
; // fall through to third round
1100 // third round: ... and use normal style
1103 if ( teststyle
!= wxNORMAL
)
1105 teststyle
= wxNORMAL
;
1112 // Search for equal or smaller size (approx.)
1113 for ( i
= pointSize
; !font
&& i
>= 10 && i
>= min_size
; i
-= 10 )
1115 font
= wxLoadQueryFont(i
, family
, teststyle
, testweight
, underlined
,
1116 facename
, info
.xregistry
, info
.xencoding
,
1120 // Search for larger size (approx.)
1121 for ( i
= pointSize
+ 10; !font
&& i
<= max_size
; i
+= 10 )
1123 font
= wxLoadQueryFont(i
, family
, teststyle
, testweight
, underlined
,
1124 facename
, info
.xregistry
, info
.xencoding
,
1129 // Try default family
1130 if ( !font
&& family
!= wxDEFAULT
)
1132 font
= wxLoadQueryFont(pointSize
, wxDEFAULT
, style
, weight
,
1133 underlined
, facename
,
1134 info
.xregistry
, info
.xencoding
,
1138 // ignore size, family, style and weight but try to find font with the
1139 // given facename and encoding
1142 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1143 underlined
, facename
,
1144 info
.xregistry
, info
.xencoding
,
1147 // ignore family as well
1150 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1151 underlined
, wxEmptyString
,
1152 info
.xregistry
, info
.xencoding
,
1155 // if it still failed, try to get the font of any size but
1156 // with the requested encoding: this can happen if the
1157 // encoding is only available in one size which happens to be
1158 // different from 120
1161 font
= wxLoadQueryFont(-1, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1162 false, wxEmptyString
,
1163 info
.xregistry
, info
.xencoding
,
1166 // this should never happen as we had tested for it in the
1167 // very beginning, but if it does, do return something non
1168 // NULL or we'd crash in wxFont code
1171 wxFAIL_MSG( wxT("this encoding should be available!") );
1173 font
= wxLoadQueryFont(-1,
1174 wxDEFAULT
, wxNORMAL
, wxNORMAL
,
1175 false, wxEmptyString
,
1187 // ----------------------------------------------------------------------------
1188 // private functions
1189 // ----------------------------------------------------------------------------
1191 // returns true if there are any fonts matching this font spec
1192 static bool wxTestFontSpec(const wxString
& fontspec
)
1194 // some X servers will fail to load this font because there are too many
1195 // matches so we must test explicitly for this
1196 if ( fontspec
== wxT("-*-*-*-*-*-*-*-*-*-*-*-*-*-*") )
1201 wxNativeFont test
= (wxNativeFont
) g_fontHash
->Get( fontspec
);
1207 test
= wxLoadFont(fontspec
);
1208 g_fontHash
->Put( fontspec
, (wxObject
*) test
);
1222 static wxNativeFont
wxLoadQueryFont(int pointSize
,
1226 bool WXUNUSED(underlined
),
1227 const wxString
& facename
,
1228 const wxString
& xregistry
,
1229 const wxString
& xencoding
,
1230 wxString
* xFontName
)
1235 case wxDECORATIVE
: xfamily
= wxT("lucida"); break;
1236 case wxROMAN
: xfamily
= wxT("times"); break;
1237 case wxMODERN
: xfamily
= wxT("courier"); break;
1238 case wxSWISS
: xfamily
= wxT("helvetica"); break;
1239 case wxTELETYPE
: xfamily
= wxT("lucidatypewriter"); break;
1240 case wxSCRIPT
: xfamily
= wxT("utopia"); break;
1241 default: xfamily
= wxT("*");
1249 xweight
= MWLF_WEIGHT_BOLD
;
1254 xweight
= MWLF_WEIGHT_LIGHT
;
1259 xweight
= MWLF_WEIGHT_NORMAL
;
1265 xweight
= MWLF_WEIGHT_DEFAULT
;
1269 GR_SCREEN_INFO screenInfo
;
1270 GrGetScreenInfo(& screenInfo
);
1272 int yPixelsPerCM
= screenInfo
.ydpcm
;
1274 // A point is 1/72 of an inch.
1275 // An inch is 2.541 cm.
1276 // So pixelHeight = (pointSize / 72) (inches) * 2.541 (for cm) * yPixelsPerCM (for pixels)
1277 // In fact pointSize is 10 * the normal point size so
1280 int pixelHeight
= (int) ( (((float)pointSize
) / 720.0) * 2.541 * (float) yPixelsPerCM
) ;
1282 // An alternative: assume that the screen is 72 dpi.
1283 //int pixelHeight = (int) (((float)pointSize / 720.0) * 72.0) ;
1284 //int pixelHeight = (int) ((float)pointSize / 10.0) ;
1287 logFont
.lfHeight
= pixelHeight
;
1288 logFont
.lfWidth
= 0;
1289 logFont
.lfEscapement
= 0;
1290 logFont
.lfOrientation
= 0;
1291 logFont
.lfWeight
= xweight
;
1292 logFont
.lfItalic
= (style
== wxNORMAL
? 0 : 1) ;
1293 logFont
.lfUnderline
= 0;
1294 logFont
.lfStrikeOut
= 0;
1295 logFont
.lfCharSet
= MWLF_CHARSET_DEFAULT
; // TODO: select appropriate one
1296 logFont
.lfOutPrecision
= MWLF_TYPE_DEFAULT
;
1297 logFont
.lfClipPrecision
= 0; // Not used
1298 logFont
.lfRoman
= (family
== wxROMAN
? 1 : 0) ;
1299 logFont
.lfSerif
= (family
== wxSWISS
? 0 : 1) ;
1300 logFont
.lfSansSerif
= !logFont
.lfSerif
;
1301 logFont
.lfModern
= (family
== wxMODERN
? 1 : 0) ;
1302 logFont
.lfProportional
= (family
== wxTELETYPE
? 0 : 1) ;
1303 logFont
.lfOblique
= 0;
1304 logFont
.lfSmallCaps
= 0;
1305 logFont
.lfPitch
= 0; // 0 = default
1306 strcpy(logFont
.lfFaceName
, facename
.c_str());
1308 XFontStruct
* fontInfo
= (XFontStruct
*) malloc(sizeof(XFontStruct
));
1309 fontInfo
->fid
= GrCreateFont((GR_CHAR
*) facename
.c_str(), pixelHeight
, & logFont
);
1310 GrGetFontInfo(fontInfo
->fid
, & fontInfo
->info
);
1311 return (wxNativeFont
) fontInfo
;
1315 if (!facename
.empty())
1317 fontSpec
.Printf(wxT("-*-%s-*-*-normal-*-*-*-*-*-*-*-*-*"),
1320 if ( wxTestFontSpec(fontSpec
) )
1324 //else: no such family, use default one instead
1331 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1333 if ( wxTestFontSpec(fontSpec
) )
1338 // fall through - try wxITALIC now
1341 fontSpec
.Printf(wxT("-*-%s-*-i-*-*-*-*-*-*-*-*-*-*"),
1343 if ( wxTestFontSpec(fontSpec
) )
1347 else if ( style
== wxITALIC
) // and not wxSLANT
1350 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1352 if ( wxTestFontSpec(fontSpec
) )
1358 // no italic, no slant - leave default
1365 wxFAIL_MSG(wxT("unknown font style"));
1366 // fall back to normal
1378 fontSpec
.Printf(wxT("-*-%s-bold-*-*-*-*-*-*-*-*-*-*-*"),
1380 if ( wxTestFontSpec(fontSpec
) )
1382 xweight
= wxT("bold");
1385 fontSpec
.Printf(wxT("-*-%s-heavy-*-*-*-*-*-*-*-*-*-*-*"),
1387 if ( wxTestFontSpec(fontSpec
) )
1389 xweight
= wxT("heavy");
1392 fontSpec
.Printf(wxT("-*-%s-extrabold-*-*-*-*-*-*-*-*-*-*-*"),
1394 if ( wxTestFontSpec(fontSpec
) )
1396 xweight
= wxT("extrabold");
1399 fontSpec
.Printf(wxT("-*-%s-demibold-*-*-*-*-*-*-*-*-*-*-*"),
1401 if ( wxTestFontSpec(fontSpec
) )
1403 xweight
= wxT("demibold");
1406 fontSpec
.Printf(wxT("-*-%s-black-*-*-*-*-*-*-*-*-*-*-*"),
1408 if ( wxTestFontSpec(fontSpec
) )
1410 xweight
= wxT("black");
1413 fontSpec
.Printf(wxT("-*-%s-ultrablack-*-*-*-*-*-*-*-*-*-*-*"),
1415 if ( wxTestFontSpec(fontSpec
) )
1417 xweight
= wxT("ultrablack");
1424 fontSpec
.Printf(wxT("-*-%s-light-*-*-*-*-*-*-*-*-*-*-*"),
1426 if ( wxTestFontSpec(fontSpec
) )
1428 xweight
= wxT("light");
1431 fontSpec
.Printf(wxT("-*-%s-thin-*-*-*-*-*-*-*-*-*-*-*"),
1433 if ( wxTestFontSpec(fontSpec
) )
1435 xweight
= wxT("thin");
1442 fontSpec
.Printf(wxT("-*-%s-medium-*-*-*-*-*-*-*-*-*-*-*"),
1444 if ( wxTestFontSpec(fontSpec
) )
1446 xweight
= wxT("medium");
1449 fontSpec
.Printf(wxT("-*-%s-normal-*-*-*-*-*-*-*-*-*-*-*"),
1451 if ( wxTestFontSpec(fontSpec
) )
1453 xweight
= wxT("normal");
1456 fontSpec
.Printf(wxT("-*-%s-regular-*-*-*-*-*-*-*-*-*-*-*"),
1458 if ( wxTestFontSpec(fontSpec
) )
1460 xweight
= wxT("regular");
1466 default: xweight
= wxT("*"); break;
1469 // if pointSize is -1, don't specify any
1471 if ( pointSize
== -1 )
1473 sizeSpec
= wxT('*');
1477 sizeSpec
.Printf(wxT("%d"), pointSize
);
1480 // construct the X font spec from our data
1481 fontSpec
.Printf(wxT("-*-%s-%s-%s-normal-*-*-%s-*-*-*-*-%s-%s"),
1482 xfamily
.c_str(), xweight
.c_str(), xstyle
.c_str(),
1483 sizeSpec
.c_str(), xregistry
.c_str(), xencoding
.c_str());
1486 *xFontName
= fontSpec
;
1488 return wxLoadFont(fontSpec
);
1493 // ----------------------------------------------------------------------------
1495 // ----------------------------------------------------------------------------
1497 class wxFontModule
: public wxModule
1504 DECLARE_DYNAMIC_CLASS(wxFontModule
)
1507 IMPLEMENT_DYNAMIC_CLASS(wxFontModule
, wxModule
)
1509 bool wxFontModule::OnInit()
1511 g_fontHash
= new wxHashTable( wxKEY_STRING
);
1516 void wxFontModule::OnExit()
1518 wxDELETE(g_fontHash
);
1521 #endif // GTK 2.0/1.x