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 void SetNoAntiAliasing( bool no
= TRUE
) { m_noAA
= no
; }
97 bool GetNoAntiAliasing() { return m_noAA
; }
99 // and this one also modifies all the other font data fields
100 void SetNativeFontInfo(const wxNativeFontInfo
& info
);
102 // debugger helper: shows what the font really is
104 // VZ: I need this as my gdb either shows wildly wrong values or crashes
105 // when I ask it to "p fontRefData" :-(
106 #if defined(__WXDEBUG__) && !defined(__WXGTK20__)
109 wxPrintf(_T("%s-%s-%s-%d-%d\n"),
111 m_weight
== wxFONTWEIGHT_NORMAL
113 : m_weight
== wxFONTWEIGHT_BOLD
116 m_style
== wxFONTSTYLE_NORMAL
? _T("regular") : _T("italic"),
123 // common part of all ctors
124 void Init(int pointSize
,
129 const wxString
& faceName
,
130 wxFontEncoding encoding
);
132 // set all fields from (already initialized and valid) m_nativeFontInfo
133 void InitFromNative();
136 // clear m_scaled_xfonts if any
137 void ClearGdkFonts();
140 // the map of font sizes to "GdkFont *"
141 wxScaledFontList m_scaled_xfonts
;
142 #endif // GTK 2.0/1.x
150 wxFontEncoding m_encoding
; // Unused under GTK 2.0
151 bool m_noAA
; // No anti-aliasing
153 // The native font info, basicly an XFLD under GTK 1.2 and
154 // the pango font description under GTK 2.0.
155 wxNativeFontInfo m_nativeFontInfo
;
160 // ----------------------------------------------------------------------------
162 // ----------------------------------------------------------------------------
164 void wxFontRefData::Init(int pointSize
,
169 const wxString
& faceName
,
170 wxFontEncoding encoding
)
172 m_family
= family
== wxFONTFAMILY_DEFAULT
? wxFONTFAMILY_SWISS
: family
;
174 m_faceName
= faceName
;
176 // we accept both wxDEFAULT and wxNORMAL here - should we?
177 m_style
= style
== wxDEFAULT
? wxFONTSTYLE_NORMAL
: style
;
178 m_weight
= weight
== wxDEFAULT
? wxFONTWEIGHT_NORMAL
: weight
;
180 // and here, do we really want to forbid creation of the font of the size
181 // 90 (the value of wxDEFAULT)??
182 m_pointSize
= pointSize
== wxDEFAULT
|| pointSize
== -1
183 ? wxDEFAULT_FONT_SIZE
186 m_underlined
= underlined
;
187 m_encoding
= encoding
;
192 // Create native font info
193 m_nativeFontInfo
.description
= pango_font_description_new();
195 // And set its values
196 if (!m_faceName
.empty())
198 pango_font_description_set_family( m_nativeFontInfo
.description
, wxGTK_CONV(m_faceName
) );
204 case wxFONTFAMILY_MODERN
:
205 case wxFONTFAMILY_TELETYPE
:
206 pango_font_description_set_family( m_nativeFontInfo
.description
, "monospace" );
208 case wxFONTFAMILY_ROMAN
:
209 pango_font_description_set_family( m_nativeFontInfo
.description
, "serif" );
211 case wxFONTFAMILY_SWISS
:
212 // SWISS = sans serif
214 pango_font_description_set_family( m_nativeFontInfo
.description
, "sans" );
220 SetPointSize( m_pointSize
);
221 SetWeight( m_weight
);
225 void wxFontRefData::InitFromNative()
231 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
234 m_faceName
= wxGTK_CONV_BACK( pango_font_description_get_family( desc
) );
236 m_pointSize
= pango_font_description_get_size( desc
) / PANGO_SCALE
;
238 switch (pango_font_description_get_style( desc
))
240 case PANGO_STYLE_NORMAL
:
241 m_style
= wxFONTSTYLE_NORMAL
;
243 case PANGO_STYLE_ITALIC
:
244 m_style
= wxFONTSTYLE_ITALIC
;
246 case PANGO_STYLE_OBLIQUE
:
247 m_style
= wxFONTSTYLE_SLANT
;
251 switch (pango_font_description_get_weight( desc
))
253 case PANGO_WEIGHT_ULTRALIGHT
:
254 m_weight
= wxFONTWEIGHT_LIGHT
;
256 case PANGO_WEIGHT_LIGHT
:
257 m_weight
= wxFONTWEIGHT_LIGHT
;
259 case PANGO_WEIGHT_NORMAL
:
260 m_weight
= wxFONTWEIGHT_NORMAL
;
262 case PANGO_WEIGHT_BOLD
:
263 m_weight
= wxFONTWEIGHT_BOLD
;
265 case PANGO_WEIGHT_ULTRABOLD
:
266 m_weight
= wxFONTWEIGHT_BOLD
;
268 case PANGO_WEIGHT_HEAVY
:
269 m_weight
= wxFONTWEIGHT_BOLD
;
273 if (m_faceName
== wxT("monospace"))
275 m_family
= wxFONTFAMILY_TELETYPE
;
277 else if (m_faceName
== wxT("sans"))
279 m_family
= wxFONTFAMILY_SWISS
;
281 else if (m_faceName
== wxT("serif"))
283 m_family
= wxFONTFAMILY_ROMAN
;
287 m_family
= wxFONTFAMILY_UNKNOWN
;
290 // Pango description are never underlined (?)
291 m_underlined
= FALSE
;
293 // Cannot we choose that
294 m_encoding
= wxFONTENCODING_SYSTEM
;
296 // get the font parameters from the XLFD
297 // -------------------------------------
299 m_faceName
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_FAMILY
);
301 m_weight
= wxFONTWEIGHT_NORMAL
;
303 wxString w
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_WEIGHT
).Upper();
304 if ( !w
.empty() && w
!= _T('*') )
306 // the test below catches all of BOLD, EXTRABOLD, DEMIBOLD, ULTRABOLD
308 if ( ((w
[0u] == _T('B') && (!wxStrcmp(w
.c_str() + 1, wxT("OLD")) ||
309 !wxStrcmp(w
.c_str() + 1, wxT("LACK"))))) ||
310 wxStrstr(w
.c_str() + 1, _T("BOLD")) )
312 m_weight
= wxFONTWEIGHT_BOLD
;
314 else if ( w
== _T("LIGHT") || w
== _T("THIN") )
316 m_weight
= wxFONTWEIGHT_LIGHT
;
320 switch ( wxToupper(*m_nativeFontInfo
.
321 GetXFontComponent(wxXLFD_SLANT
).c_str()) )
323 case _T('I'): // italique
324 m_style
= wxFONTSTYLE_ITALIC
;
327 case _T('O'): // oblique
328 m_style
= wxFONTSTYLE_SLANT
;
332 m_style
= wxFONTSTYLE_NORMAL
;
336 if ( m_nativeFontInfo
.GetXFontComponent(wxXLFD_POINTSIZE
).ToLong(&ptSize
) )
338 // size in XLFD is in 10 point units
339 m_pointSize
= (int)(ptSize
/ 10);
343 m_pointSize
= wxDEFAULT_FONT_SIZE
;
346 // examine the spacing: if the font is monospaced, assume wxTELETYPE
347 // family for compatibility with the old code which used it instead of
349 if ( m_nativeFontInfo
.GetXFontComponent(wxXLFD_SPACING
).Upper() == _T('M') )
351 m_family
= wxFONTFAMILY_TELETYPE
;
353 else // not monospaceed
355 // don't even try guessing it, it doesn't work for too many fonts
357 m_family
= wxFONTFAMILY_UNKNOWN
;
360 // X fonts are never underlined...
361 m_underlined
= FALSE
;
363 // deal with font encoding
365 registry
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_REGISTRY
).Upper(),
366 encoding
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_ENCODING
).Upper();
368 if ( registry
== _T("ISO8859") )
371 if ( wxSscanf(encoding
, wxT("%d"), &cp
) == 1 )
373 m_encoding
= (wxFontEncoding
)(wxFONTENCODING_ISO8859_1
+ cp
- 1);
376 else if ( registry
== _T("MICROSOFT") )
379 if ( wxSscanf(encoding
, wxT("cp125%d"), &cp
) == 1 )
381 m_encoding
= (wxFontEncoding
)(wxFONTENCODING_CP1250
+ cp
);
384 else if ( registry
== _T("KOI8") )
386 m_encoding
= wxFONTENCODING_KOI8
;
388 else // unknown encoding
390 // may be give a warning here? or use wxFontMapper?
391 m_encoding
= wxFONTENCODING_SYSTEM
;
393 #endif // GTK 2.0/1.x
396 wxFontRefData::wxFontRefData( const wxFontRefData
& data
)
399 m_pointSize
= data
.m_pointSize
;
400 m_family
= data
.m_family
;
401 m_style
= data
.m_style
;
402 m_weight
= data
.m_weight
;
404 m_underlined
= data
.m_underlined
;
406 m_faceName
= data
.m_faceName
;
407 m_encoding
= data
.m_encoding
;
409 m_noAA
= data
.m_noAA
;
411 // Forces a copy of the internal data. wxNativeFontInfo should probably
412 // have a copy ctor and assignment operator to fix this properly but that
413 // would break binary compatibility...
414 m_nativeFontInfo
.FromString(data
.m_nativeFontInfo
.ToString());
417 wxFontRefData::wxFontRefData(int size
, int family
, int style
,
418 int weight
, bool underlined
,
419 const wxString
& faceName
,
420 wxFontEncoding encoding
)
422 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
425 wxFontRefData::wxFontRefData(const wxString
& fontname
)
427 // VZ: FromString() should really work in both cases, doesn't it?
429 m_nativeFontInfo
.FromString( fontname
);
431 m_nativeFontInfo
.SetXFontName(fontname
);
432 #endif // GTK 2.0/1.x
437 void wxFontRefData::ClearGdkFonts()
440 for ( wxScaledFontList::iterator i
= m_scaled_xfonts
.begin();
441 i
!= m_scaled_xfonts
.end();
444 GdkFont
*font
= i
->second
;
445 gdk_font_unref( font
);
448 m_scaled_xfonts
.clear();
452 wxFontRefData::~wxFontRefData()
457 // ----------------------------------------------------------------------------
458 // wxFontRefData SetXXX()
459 // ----------------------------------------------------------------------------
461 void wxFontRefData::SetPointSize(int pointSize
)
463 m_pointSize
= pointSize
;
467 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
469 pango_font_description_set_size( desc
, m_pointSize
* PANGO_SCALE
);
471 if ( HasNativeFont() )
474 if ( pointSize
== -1 )
477 size
.Printf(_T("%d"), 10*pointSize
);
479 m_nativeFontInfo
.SetXFontComponent(wxXLFD_POINTSIZE
, size
);
484 void wxFontRefData::SetFamily(int family
)
488 // TODO: what are we supposed to do with m_nativeFontInfo here?
491 void wxFontRefData::SetStyle(int style
)
497 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
501 case wxFONTSTYLE_ITALIC
:
502 pango_font_description_set_style( desc
, PANGO_STYLE_ITALIC
);
504 case wxFONTSTYLE_SLANT
:
505 pango_font_description_set_style( desc
, PANGO_STYLE_OBLIQUE
);
508 wxFAIL_MSG( _T("unknown font style") );
510 case wxFONTSTYLE_NORMAL
:
511 pango_font_description_set_style( desc
, PANGO_STYLE_NORMAL
);
515 if ( HasNativeFont() )
520 case wxFONTSTYLE_ITALIC
:
524 case wxFONTSTYLE_SLANT
:
529 wxFAIL_MSG( _T("unknown font style") );
532 case wxFONTSTYLE_NORMAL
:
536 m_nativeFontInfo
.SetXFontComponent(wxXLFD_SLANT
, slant
);
541 void wxFontRefData::SetWeight(int weight
)
546 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
549 case wxFONTWEIGHT_BOLD
:
550 pango_font_description_set_weight(desc
, PANGO_WEIGHT_BOLD
);
553 case wxFONTWEIGHT_LIGHT
:
554 pango_font_description_set_weight(desc
, PANGO_WEIGHT_LIGHT
);
558 wxFAIL_MSG( _T("unknown font weight") );
561 case wxFONTWEIGHT_NORMAL
:
563 pango_font_description_set_weight(desc
, PANGO_WEIGHT_NORMAL
);
566 if ( HasNativeFont() )
571 case wxFONTWEIGHT_BOLD
:
572 boldness
= _T("bold");
575 case wxFONTWEIGHT_LIGHT
:
576 boldness
= _T("light");
580 wxFAIL_MSG( _T("unknown font weight") );
583 case wxFONTWEIGHT_NORMAL
:
585 boldness
= _T("medium");
588 m_nativeFontInfo
.SetXFontComponent(wxXLFD_WEIGHT
, boldness
);
593 void wxFontRefData::SetUnderlined(bool underlined
)
595 m_underlined
= underlined
;
597 // the XLFD doesn't have "underlined" field anyhow
600 void wxFontRefData::SetFaceName(const wxString
& facename
)
602 m_faceName
= facename
;
605 if ( HasNativeFont() )
607 m_nativeFontInfo
.SetXFontComponent(wxXLFD_FAMILY
, facename
);
612 void wxFontRefData::SetEncoding(wxFontEncoding encoding
)
614 m_encoding
= encoding
;
617 if ( HasNativeFont() )
619 wxNativeEncodingInfo info
;
620 if ( wxGetNativeFontEncoding(encoding
, &info
) )
622 m_nativeFontInfo
.SetXFontComponent(wxXLFD_REGISTRY
, info
.xregistry
);
623 m_nativeFontInfo
.SetXFontComponent(wxXLFD_ENCODING
, info
.xencoding
);
629 void wxFontRefData::SetNativeFontInfo(const wxNativeFontInfo
& info
)
631 // previously cached fonts shouldn't be used
634 m_nativeFontInfo
= info
;
636 // set all the other font parameters from the native font info
640 // ----------------------------------------------------------------------------
642 // ----------------------------------------------------------------------------
644 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
650 wxFont::wxFont(const wxNativeFontInfo
& info
)
655 Create( info
.GetPointSize(),
659 info
.GetUnderlined(),
661 info
.GetEncoding() );
663 (void) Create(info
.GetXFontName());
667 bool wxFont::Create( int pointSize
,
672 const wxString
& face
,
673 wxFontEncoding encoding
)
677 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
678 underlined
, face
, encoding
);
683 bool wxFont::Create(const wxString
& fontname
)
685 // VZ: does this really happen?
686 if ( fontname
.empty() )
688 *this = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
693 m_refData
= new wxFontRefData(fontname
);
698 void wxFont::Unshare()
702 m_refData
= new wxFontRefData();
706 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
716 // ----------------------------------------------------------------------------
718 // ----------------------------------------------------------------------------
720 int wxFont::GetPointSize() const
722 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
724 return M_FONTDATA
->m_pointSize
;
727 wxString
wxFont::GetFaceName() const
729 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
731 return M_FONTDATA
->m_faceName
;
734 int wxFont::GetFamily() const
736 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
738 return M_FONTDATA
->m_family
;
741 int wxFont::GetStyle() const
743 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
745 return M_FONTDATA
->m_style
;
748 int wxFont::GetWeight() const
750 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
752 return M_FONTDATA
->m_weight
;
755 bool wxFont::GetUnderlined() const
757 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid font") );
759 return M_FONTDATA
->m_underlined
;
762 wxFontEncoding
wxFont::GetEncoding() const
764 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
766 return M_FONTDATA
->m_encoding
;
769 bool wxFont::GetNoAntiAliasing()
771 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
773 return M_FONTDATA
->m_noAA
;
776 wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
778 wxCHECK_MSG( Ok(), (wxNativeFontInfo
*)NULL
, wxT("invalid font") );
780 #ifndef __WXGTK20__ // ???
781 if ( M_FONTDATA
->m_nativeFontInfo
.GetXFontName().empty() )
785 return new wxNativeFontInfo(M_FONTDATA
->m_nativeFontInfo
);
788 bool wxFont::IsFixedWidth() const
790 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid font") );
793 if ( M_FONTDATA
->HasNativeFont() )
795 // the monospace fonts are supposed to have "M" in the spacing field
796 wxString spacing
= M_FONTDATA
->
797 m_nativeFontInfo
.GetXFontComponent(wxXLFD_SPACING
);
799 return spacing
.Upper() == _T('M');
803 return wxFontBase::IsFixedWidth();
806 // ----------------------------------------------------------------------------
807 // change font attributes
808 // ----------------------------------------------------------------------------
810 void wxFont::SetPointSize(int pointSize
)
814 M_FONTDATA
->SetPointSize(pointSize
);
817 void wxFont::SetFamily(int family
)
821 M_FONTDATA
->SetFamily(family
);
824 void wxFont::SetStyle(int style
)
828 M_FONTDATA
->SetStyle(style
);
831 void wxFont::SetWeight(int weight
)
835 M_FONTDATA
->SetWeight(weight
);
838 void wxFont::SetFaceName(const wxString
& faceName
)
842 M_FONTDATA
->SetFaceName(faceName
);
845 void wxFont::SetUnderlined(bool underlined
)
849 M_FONTDATA
->SetUnderlined(underlined
);
852 void wxFont::SetEncoding(wxFontEncoding encoding
)
856 M_FONTDATA
->SetEncoding(encoding
);
859 void wxFont::SetNativeFontInfo( const wxNativeFontInfo
& info
)
863 M_FONTDATA
->SetNativeFontInfo( info
);
866 void wxFont::SetNoAntiAliasing( bool no
)
870 M_FONTDATA
->SetNoAntiAliasing( no
);
873 // ----------------------------------------------------------------------------
874 // get internal representation of font
875 // ----------------------------------------------------------------------------
877 static GdkFont
*g_systemDefaultGuiFont
= (GdkFont
*) NULL
;
879 // this is also used from tbargtk.cpp and tooltip.cpp, hence extern
880 extern GdkFont
*GtkGetDefaultGuiFont()
882 if (!g_systemDefaultGuiFont
)
884 GtkWidget
*widget
= gtk_button_new();
885 GtkStyle
*def
= gtk_rc_get_style( widget
);
888 g_systemDefaultGuiFont
= gdk_font_ref( GET_STYLE_FONT(def
) );
892 def
= gtk_widget_get_default_style();
894 g_systemDefaultGuiFont
= gdk_font_ref( GET_STYLE_FONT(def
) );
896 gtk_widget_destroy( widget
);
900 // already have it, but ref it once more before returning
901 gdk_font_ref(g_systemDefaultGuiFont
);
904 return g_systemDefaultGuiFont
;
907 GdkFont
*wxFont::GetInternalFont( float scale
) const
909 GdkFont
*font
= (GdkFont
*) NULL
;
911 wxCHECK_MSG( Ok(), font
, wxT("invalid font") )
914 if (*this == wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
))
916 font
= GtkGetDefaultGuiFont();
920 PangoFontDescription
*
921 font_description
= GetNativeFontInfo()->description
;
923 font
= gdk_font_from_description( font_description
);
926 long int_scale
= long(scale
* 100.0 + 0.5); // key for fontlist
927 int point_scale
= (int)((M_FONTDATA
->m_pointSize
* 10 * int_scale
) / 100);
929 wxScaledFontList
& list
= M_FONTDATA
->m_scaled_xfonts
;
930 wxScaledFontList::iterator i
= list
.find(int_scale
);
931 if ( i
!= list
.end() )
935 else // we don't have this font in this size yet
937 if (*this == wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
))
939 font
= GtkGetDefaultGuiFont();
944 // do we have the XLFD?
945 if ( M_FONTDATA
->HasNativeFont() )
947 font
= wxLoadFont(M_FONTDATA
->m_nativeFontInfo
.GetXFontName());
950 // no XLFD of no exact match - try the approximate one now
954 font
= wxLoadQueryNearestFont( point_scale
,
955 M_FONTDATA
->m_family
,
957 M_FONTDATA
->m_weight
,
958 M_FONTDATA
->m_underlined
,
959 M_FONTDATA
->m_faceName
,
960 M_FONTDATA
->m_encoding
,
964 M_FONTDATA
->m_nativeFontInfo
.SetXFontName(xfontname
);
971 list
[int_scale
] = font
;
974 #endif // GTK 2.0/1.x
976 // it's quite useless to make it a wxCHECK because we're going to crash
978 wxASSERT_MSG( font
, wxT("could not load any font?") );