1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // ============================================================================
12 // ============================================================================
14 // ----------------------------------------------------------------------------
16 // ----------------------------------------------------------------------------
19 #pragma implementation "font.h"
23 #include "wx/fontutil.h"
24 #include "wx/cmndata.h"
27 #include "wx/gdicmn.h"
28 #include "wx/tokenzr.h"
29 #include "wx/settings.h"
33 #include "wx/gtk/private.h"
34 #include <gdk/gdkprivate.h>
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 // the default size (in points) for the fonts
41 static const int wxDEFAULT_FONT_SIZE
= 12;
43 // ----------------------------------------------------------------------------
44 // wxScaledFontList: maps the font sizes to the GDK fonts for the given font
45 // ----------------------------------------------------------------------------
47 WX_DECLARE_HASH_MAP(int, GdkFont
*, wxIntegerHash
, wxIntegerEqual
,
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 class wxFontRefData
: public wxObjectRefData
57 // from broken down font parameters, also default ctor
58 wxFontRefData(int size
= -1,
59 int family
= wxFONTFAMILY_DEFAULT
,
60 int style
= wxFONTSTYLE_NORMAL
,
61 int weight
= wxFONTWEIGHT_NORMAL
,
62 bool underlined
= FALSE
,
63 const wxString
& faceName
= wxEmptyString
,
64 wxFontEncoding encoding
= wxFONTENCODING_DEFAULT
);
67 wxFontRefData(const wxString
& fontname
);
70 wxFontRefData( const wxFontRefData
& data
);
72 virtual ~wxFontRefData();
74 // do we have the native font info?
75 bool HasNativeFont() const
78 // we always have a Pango font description
81 // only use m_nativeFontInfo if it had been initialized
82 return !m_nativeFontInfo
.IsDefault();
86 // setters: all of them also take care to modify m_nativeFontInfo if we
87 // have it so as to not lose the information not carried by our fields
88 void SetPointSize(int pointSize
);
89 void SetFamily(int family
);
90 void SetStyle(int style
);
91 void SetWeight(int weight
);
92 void SetUnderlined(bool underlined
);
93 void SetFaceName(const wxString
& facename
);
94 void SetEncoding(wxFontEncoding encoding
);
96 // and this one also modifies all the other font data fields
97 void SetNativeFontInfo(const wxNativeFontInfo
& info
);
99 // debugger helper: shows what the font really is
101 // VZ: I need this as my gdb either shows wildly wrong values or crashes
102 // when I ask it to "p fontRefData" :-(
103 #if defined(__WXDEBUG__) && !defined(__WXGTK20__)
106 wxPrintf(_T("%s-%s-%s-%d-%d\n"),
108 m_weight
== wxFONTWEIGHT_NORMAL
110 : m_weight
== wxFONTWEIGHT_BOLD
113 m_style
== wxFONTSTYLE_NORMAL
? _T("regular") : _T("italic"),
120 // common part of all ctors
121 void Init(int pointSize
,
126 const wxString
& faceName
,
127 wxFontEncoding encoding
);
129 // set all fields from (already initialized and valid) m_nativeFontInfo
130 void InitFromNative();
134 void ClearGdkFonts() { }
136 // clear m_scaled_xfonts
137 void ClearGdkFonts();
139 // the map of font sizes to "GdkFont *"
140 wxScaledFontList m_scaled_xfonts
;
141 #endif // GTK 2.0/1.x
143 // the broken down font parameters
150 wxFontEncoding m_encoding
; // Unused under GTK 2.0
152 // The native font info, basicly an XFLD under GTK 1.2 and
153 // the pango font description under GTK 2.0.
154 wxNativeFontInfo m_nativeFontInfo
;
159 // ============================================================================
160 // wxFontRefData implementation
161 // ============================================================================
163 // ----------------------------------------------------------------------------
164 // wxFontRefData creation
165 // ----------------------------------------------------------------------------
167 void wxFontRefData::Init(int pointSize
,
172 const wxString
& faceName
,
173 wxFontEncoding encoding
)
175 m_family
= family
== wxFONTFAMILY_DEFAULT
? wxFONTFAMILY_SWISS
: family
;
177 m_faceName
= faceName
;
179 // we accept both wxDEFAULT and wxNORMAL here - should we?
180 m_style
= style
== wxDEFAULT
? wxFONTSTYLE_NORMAL
: style
;
181 m_weight
= weight
== wxDEFAULT
? wxFONTWEIGHT_NORMAL
: weight
;
183 // and here, do we really want to forbid creation of the font of the size
184 // 90 (the value of wxDEFAULT)??
185 m_pointSize
= pointSize
== wxDEFAULT
|| pointSize
== -1
186 ? wxDEFAULT_FONT_SIZE
189 m_underlined
= underlined
;
190 m_encoding
= encoding
;
193 // Create native font info
194 m_nativeFontInfo
.description
= pango_font_description_new();
196 // And set its values
199 case wxFONTFAMILY_MODERN
:
200 case wxFONTFAMILY_TELETYPE
:
201 pango_font_description_set_family( m_nativeFontInfo
.description
, "monospace" );
203 case wxFONTFAMILY_SWISS
:
204 pango_font_description_set_family( m_nativeFontInfo
.description
, "serif" );
207 pango_font_description_set_family( m_nativeFontInfo
.description
, "sans" );
211 SetPointSize( m_pointSize
);
212 SetWeight( m_weight
);
216 void wxFontRefData::InitFromNative()
220 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
223 m_faceName
= wxGTK_CONV_BACK( pango_font_description_get_family( desc
) );
225 m_pointSize
= pango_font_description_get_size( desc
) / PANGO_SCALE
;
227 switch (pango_font_description_get_style( desc
))
229 case PANGO_STYLE_NORMAL
:
230 m_style
= wxFONTSTYLE_NORMAL
;
232 case PANGO_STYLE_ITALIC
:
233 m_style
= wxFONTSTYLE_ITALIC
;
235 case PANGO_STYLE_OBLIQUE
:
236 m_style
= wxFONTSTYLE_SLANT
;
240 switch (pango_font_description_get_weight( desc
))
242 case PANGO_WEIGHT_ULTRALIGHT
:
243 m_weight
= wxFONTWEIGHT_LIGHT
;
245 case PANGO_WEIGHT_LIGHT
:
246 m_weight
= wxFONTWEIGHT_LIGHT
;
248 case PANGO_WEIGHT_NORMAL
:
249 m_weight
= wxFONTWEIGHT_NORMAL
;
251 case PANGO_WEIGHT_BOLD
:
252 m_weight
= wxFONTWEIGHT_BOLD
;
254 case PANGO_WEIGHT_ULTRABOLD
:
255 m_weight
= wxFONTWEIGHT_BOLD
;
257 case PANGO_WEIGHT_HEAVY
:
258 m_weight
= wxFONTWEIGHT_BOLD
;
262 if (m_faceName
== wxT("monospace"))
264 m_family
= wxFONTFAMILY_TELETYPE
;
266 else if (m_faceName
== wxT("sans"))
268 m_family
= wxFONTFAMILY_SWISS
;
272 m_family
= wxFONTFAMILY_UNKNOWN
;
275 // Pango description are never underlined (?)
276 m_underlined
= FALSE
;
278 // Cannot we choose that
279 m_encoding
= wxFONTENCODING_SYSTEM
;
281 // get the font parameters from the XLFD
282 // -------------------------------------
284 m_faceName
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_FAMILY
);
286 m_weight
= wxFONTWEIGHT_NORMAL
;
288 wxString w
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_WEIGHT
).Upper();
289 if ( !w
.empty() && w
!= _T('*') )
291 // the test below catches all of BOLD, EXTRABOLD, DEMIBOLD, ULTRABOLD
293 if ( ((w
[0u] == _T('B') && (!wxStrcmp(w
.c_str() + 1, wxT("OLD")) ||
294 !wxStrcmp(w
.c_str() + 1, wxT("LACK"))))) ||
295 wxStrstr(w
.c_str() + 1, _T("BOLD")) )
297 m_weight
= wxFONTWEIGHT_BOLD
;
299 else if ( w
== _T("LIGHT") || w
== _T("THIN") )
301 m_weight
= wxFONTWEIGHT_LIGHT
;
305 switch ( wxToupper(*m_nativeFontInfo
.
306 GetXFontComponent(wxXLFD_SLANT
).c_str()) )
308 case _T('I'): // italique
309 m_style
= wxFONTSTYLE_ITALIC
;
312 case _T('O'): // oblique
313 m_style
= wxFONTSTYLE_SLANT
;
317 m_style
= wxFONTSTYLE_NORMAL
;
321 if ( m_nativeFontInfo
.GetXFontComponent(wxXLFD_POINTSIZE
).ToLong(&ptSize
) )
323 // size in XLFD is in 10 point units
324 m_pointSize
= (int)(ptSize
/ 10);
328 m_pointSize
= wxDEFAULT_FONT_SIZE
;
331 // examine the spacing: if the font is monospaced, assume wxTELETYPE
332 // family for compatibility with the old code which used it instead of
334 if ( m_nativeFontInfo
.GetXFontComponent(wxXLFD_SPACING
).Upper() == _T('M') )
336 m_family
= wxFONTFAMILY_TELETYPE
;
338 else // not monospaceed
340 // don't even try guessing it, it doesn't work for too many fonts
342 m_family
= wxFONTFAMILY_UNKNOWN
;
345 // X fonts are never underlined...
346 m_underlined
= FALSE
;
348 // deal with font encoding
350 registry
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_REGISTRY
).Upper(),
351 encoding
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_ENCODING
).Upper();
353 if ( registry
== _T("ISO8859") )
356 if ( wxSscanf(encoding
, wxT("%d"), &cp
) == 1 )
358 m_encoding
= (wxFontEncoding
)(wxFONTENCODING_ISO8859_1
+ cp
- 1);
361 else if ( registry
== _T("MICROSOFT") )
364 if ( wxSscanf(encoding
, wxT("cp125%d"), &cp
) == 1 )
366 m_encoding
= (wxFontEncoding
)(wxFONTENCODING_CP1250
+ cp
);
369 else if ( registry
== _T("KOI8") )
371 m_encoding
= wxFONTENCODING_KOI8
;
373 else // unknown encoding
375 // may be give a warning here? or use wxFontMapper?
376 m_encoding
= wxFONTENCODING_SYSTEM
;
378 #endif // GTK 2.0/1.x
381 wxFontRefData::wxFontRefData( const wxFontRefData
& data
)
384 m_pointSize
= data
.m_pointSize
;
385 m_family
= data
.m_family
;
386 m_style
= data
.m_style
;
387 m_weight
= data
.m_weight
;
389 m_underlined
= data
.m_underlined
;
391 m_faceName
= data
.m_faceName
;
392 m_encoding
= data
.m_encoding
;
394 m_nativeFontInfo
= data
.m_nativeFontInfo
;
397 wxFontRefData::wxFontRefData(int size
, int family
, int style
,
398 int weight
, bool underlined
,
399 const wxString
& faceName
,
400 wxFontEncoding encoding
)
402 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
405 wxFontRefData::wxFontRefData(const wxString
& fontname
)
407 // VZ: FromString() should really work in both cases, doesn't it?
409 m_nativeFontInfo
.FromString( fontname
);
411 m_nativeFontInfo
.SetXFontName(fontname
);
412 #endif // GTK 2.0/1.x
418 void wxFontRefData::ClearGdkFonts()
420 for ( wxScaledFontList::iterator i
= m_scaled_xfonts
.begin();
421 i
!= m_scaled_xfonts
.end();
424 GdkFont
*font
= i
->second
;
425 gdk_font_unref( font
);
428 m_scaled_xfonts
.clear();
432 wxFontRefData::~wxFontRefData()
437 // ----------------------------------------------------------------------------
438 // wxFontRefData SetXXX()
439 // ----------------------------------------------------------------------------
441 void wxFontRefData::SetPointSize(int pointSize
)
443 m_pointSize
= pointSize
;
447 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
449 pango_font_description_set_size( desc
, m_pointSize
* PANGO_SCALE
);
451 if ( HasNativeFont() )
454 if ( pointSize
== -1 )
457 size
.Printf(_T("%d"), 10*pointSize
);
459 m_nativeFontInfo
.SetXFontComponent(wxXLFD_POINTSIZE
, size
);
464 void wxFontRefData::SetFamily(int family
)
468 // TODO: what are we supposed to do with m_nativeFontInfo here?
471 void wxFontRefData::SetStyle(int style
)
477 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
481 case wxFONTSTYLE_ITALIC
:
482 pango_font_description_set_style( desc
, PANGO_STYLE_ITALIC
);
484 case wxFONTSTYLE_SLANT
:
485 pango_font_description_set_style( desc
, PANGO_STYLE_OBLIQUE
);
488 wxFAIL_MSG( _T("unknown font style") );
490 case wxFONTSTYLE_NORMAL
:
491 pango_font_description_set_style( desc
, PANGO_STYLE_NORMAL
);
495 if ( HasNativeFont() )
500 case wxFONTSTYLE_ITALIC
:
504 case wxFONTSTYLE_SLANT
:
509 wxFAIL_MSG( _T("unknown font style") );
512 case wxFONTSTYLE_NORMAL
:
516 m_nativeFontInfo
.SetXFontComponent(wxXLFD_SLANT
, slant
);
521 void wxFontRefData::SetWeight(int weight
)
526 if ( HasNativeFont() )
531 case wxFONTWEIGHT_BOLD
:
532 boldness
= _T("bold");
535 case wxFONTWEIGHT_LIGHT
:
536 boldness
= _T("light");
540 wxFAIL_MSG( _T("unknown font weight") );
543 case wxFONTWEIGHT_NORMAL
:
545 boldness
= _T("medium");
548 m_nativeFontInfo
.SetXFontComponent(wxXLFD_WEIGHT
, boldness
);
553 void wxFontRefData::SetUnderlined(bool underlined
)
555 m_underlined
= underlined
;
557 // the XLFD doesn't have "underlined" field anyhow
560 void wxFontRefData::SetFaceName(const wxString
& facename
)
562 m_faceName
= facename
;
565 if ( HasNativeFont() )
567 m_nativeFontInfo
.SetXFontComponent(wxXLFD_FAMILY
, facename
);
572 void wxFontRefData::SetEncoding(wxFontEncoding encoding
)
574 m_encoding
= encoding
;
577 if ( HasNativeFont() )
579 wxNativeEncodingInfo info
;
580 if ( wxGetNativeFontEncoding(encoding
, &info
) )
582 m_nativeFontInfo
.SetXFontComponent(wxXLFD_REGISTRY
, info
.xregistry
);
583 m_nativeFontInfo
.SetXFontComponent(wxXLFD_ENCODING
, info
.xencoding
);
589 void wxFontRefData::SetNativeFontInfo(const wxNativeFontInfo
& info
)
591 // previously cached fonts shouldn't be used
594 m_nativeFontInfo
= info
;
596 // set all the other font parameters from the native font info
600 // ============================================================================
601 // wxFont implementation
602 // ============================================================================
604 // ----------------------------------------------------------------------------
606 // ----------------------------------------------------------------------------
608 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
614 wxFont::wxFont(const wxNativeFontInfo
& info
)
619 Create( info
.GetPointSize(),
623 info
.GetUnderlined(),
625 info
.GetEncoding() );
627 Create(info
.GetXFontName());
631 bool wxFont::Create( int pointSize
,
636 const wxString
& face
,
637 wxFontEncoding encoding
)
639 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
640 underlined
, face
, encoding
);
645 bool wxFont::Create(const wxString
& fontname
)
647 // VZ: does this really happen?
648 if ( fontname
.empty() )
650 *this = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
655 m_refData
= new wxFontRefData(fontname
);
660 void wxFont::Unshare()
664 m_refData
= new wxFontRefData();
668 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
678 // ----------------------------------------------------------------------------
680 // ----------------------------------------------------------------------------
682 // all accessors are just forwarded to wxFontRefData which has everything we
685 int wxFont::GetPointSize() const
687 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
689 return M_FONTDATA
->m_pointSize
;
692 wxString
wxFont::GetFaceName() const
694 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
696 return M_FONTDATA
->m_faceName
;
699 int wxFont::GetFamily() const
701 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
703 return M_FONTDATA
->m_family
;
706 int wxFont::GetStyle() const
708 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
710 return M_FONTDATA
->m_style
;
713 int wxFont::GetWeight() const
715 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
717 return M_FONTDATA
->m_weight
;
720 bool wxFont::GetUnderlined() const
722 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid font") );
724 return M_FONTDATA
->m_underlined
;
727 wxFontEncoding
wxFont::GetEncoding() const
729 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
731 return M_FONTDATA
->m_encoding
;
734 wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
736 wxCHECK_MSG( Ok(), (wxNativeFontInfo
*)NULL
, wxT("invalid font") );
738 #ifndef __WXGTK20__ // ???
739 if ( M_FONTDATA
->m_nativeFontInfo
.GetXFontName().empty() )
743 return new wxNativeFontInfo(M_FONTDATA
->m_nativeFontInfo
);
746 bool wxFont::IsFixedWidth() const
748 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid font") );
751 if ( M_FONTDATA
->HasNativeFont() )
753 // the monospace fonts are supposed to have "M" in the spacing field
754 wxString spacing
= M_FONTDATA
->
755 m_nativeFontInfo
.GetXFontComponent(wxXLFD_SPACING
);
757 return spacing
.Upper() == _T('M');
761 return wxFontBase::IsFixedWidth();
764 // ----------------------------------------------------------------------------
765 // change font attributes
766 // ----------------------------------------------------------------------------
768 void wxFont::SetPointSize(int pointSize
)
772 M_FONTDATA
->SetPointSize(pointSize
);
775 void wxFont::SetFamily(int family
)
779 M_FONTDATA
->SetFamily(family
);
782 void wxFont::SetStyle(int style
)
786 M_FONTDATA
->SetStyle(style
);
789 void wxFont::SetWeight(int weight
)
793 M_FONTDATA
->SetWeight(weight
);
796 void wxFont::SetFaceName(const wxString
& faceName
)
800 M_FONTDATA
->SetFaceName(faceName
);
803 void wxFont::SetUnderlined(bool underlined
)
807 M_FONTDATA
->SetUnderlined(underlined
);
810 void wxFont::SetEncoding(wxFontEncoding encoding
)
814 M_FONTDATA
->SetEncoding(encoding
);
817 void wxFont::SetNativeFontInfo(const wxNativeFontInfo
& info
)
821 M_FONTDATA
->SetNativeFontInfo(info
);
824 // ----------------------------------------------------------------------------
825 // get internal representation of font
826 // ----------------------------------------------------------------------------
828 static GdkFont
*g_systemDefaultGuiFont
= (GdkFont
*) NULL
;
830 // this is also used from tbargtk.cpp and tooltip.cpp, hence extern
831 extern GdkFont
*GtkGetDefaultGuiFont()
833 if (!g_systemDefaultGuiFont
)
835 GtkWidget
*widget
= gtk_button_new();
836 GtkStyle
*def
= gtk_rc_get_style( widget
);
839 g_systemDefaultGuiFont
= gdk_font_ref( GET_STYLE_FONT(def
) );
843 def
= gtk_widget_get_default_style();
845 g_systemDefaultGuiFont
= gdk_font_ref( GET_STYLE_FONT(def
) );
847 gtk_widget_destroy( widget
);
851 // already have it, but ref it once more before returning
852 gdk_font_ref(g_systemDefaultGuiFont
);
855 return g_systemDefaultGuiFont
;
858 GdkFont
*wxFont::GetInternalFont( float scale
) const
860 GdkFont
*font
= (GdkFont
*) NULL
;
862 wxCHECK_MSG( Ok(), font
, wxT("invalid font") )
865 if (*this == wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
))
867 font
= GtkGetDefaultGuiFont();
871 PangoFontDescription
*
872 font_description
= GetNativeFontInfo()->description
;
874 font
= gdk_font_from_description( font_description
);
877 long int_scale
= long(scale
* 100.0 + 0.5); // key for fontlist
878 int point_scale
= (int)((M_FONTDATA
->m_pointSize
* 10 * int_scale
) / 100);
880 wxScaledFontList
& list
= M_FONTDATA
->m_scaled_xfonts
;
881 wxScaledFontList::iterator i
= list
.find(int_scale
);
882 if ( i
!= list
.end() )
886 else // we don't have this font in this size yet
888 if (*this == wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
))
890 font
= GtkGetDefaultGuiFont();
895 // do we have the XLFD?
896 if ( M_FONTDATA
->HasNativeFont() )
898 font
= wxLoadFont(M_FONTDATA
->m_nativeFontInfo
.GetXFontName());
901 // no XLFD of no exact match - try the approximate one now
905 font
= wxLoadQueryNearestFont( point_scale
,
906 M_FONTDATA
->m_family
,
908 M_FONTDATA
->m_weight
,
909 M_FONTDATA
->m_underlined
,
910 M_FONTDATA
->m_faceName
,
911 M_FONTDATA
->m_encoding
,
915 M_FONTDATA
->m_nativeFontInfo
.SetXFontName(xfontname
);
922 list
[int_scale
] = font
;
925 #endif // GTK 2.0/1.x
927 // it's quite useless to make it a wxCHECK because we're going to crash
929 wxASSERT_MSG( font
, wxT("could not load any font?") );