1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling and Julian Smart
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // ============================================================================
12 // ============================================================================
14 // ----------------------------------------------------------------------------
16 // ----------------------------------------------------------------------------
18 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
19 #pragma implementation "font.h"
22 // For compilers that support precompilation, includes "wx.h".
23 #include "wx/wxprec.h"
26 #include "wx/fontutil.h"
27 #include "wx/cmndata.h"
30 #include "wx/gdicmn.h"
31 #include "wx/tokenzr.h"
32 #include "wx/settings.h"
36 #include "wx/gtk/private.h"
37 #include <gdk/gdkprivate.h>
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 // the default size (in points) for the fonts
44 static const int wxDEFAULT_FONT_SIZE
= 12;
46 // ----------------------------------------------------------------------------
47 // wxScaledFontList: maps the font sizes to the GDK fonts for the given font
48 // ----------------------------------------------------------------------------
50 WX_DECLARE_HASH_MAP(int, GdkFont
*, wxIntegerHash
, wxIntegerEqual
,
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 class wxFontRefData
: public wxObjectRefData
60 // from broken down font parameters, also default ctor
61 wxFontRefData(int size
= -1,
62 int family
= wxFONTFAMILY_DEFAULT
,
63 int style
= wxFONTSTYLE_NORMAL
,
64 int weight
= wxFONTWEIGHT_NORMAL
,
65 bool underlined
= FALSE
,
66 const wxString
& faceName
= wxEmptyString
,
67 wxFontEncoding encoding
= wxFONTENCODING_DEFAULT
);
70 wxFontRefData(const wxString
& fontname
);
73 wxFontRefData( const wxFontRefData
& data
);
75 virtual ~wxFontRefData();
77 // do we have the native font info?
78 bool HasNativeFont() const
81 // we always have a Pango font description
84 // only use m_nativeFontInfo if it had been initialized
85 return !m_nativeFontInfo
.IsDefault();
89 // reinitilize the font with the gived XFLD
90 void ReInit(const wxString
& fontname
);
92 // setters: all of them also take care to modify m_nativeFontInfo if we
93 // have it so as to not lose the information not carried by our fields
94 void SetPointSize(int pointSize
);
95 void SetFamily(int family
);
96 void SetStyle(int style
);
97 void SetWeight(int weight
);
98 void SetUnderlined(bool underlined
);
99 void SetFaceName(const wxString
& facename
);
100 void SetEncoding(wxFontEncoding encoding
);
102 void SetNoAntiAliasing( bool no
= TRUE
) { m_noAA
= no
; }
103 bool GetNoAntiAliasing() { return m_noAA
; }
105 // and this one also modifies all the other font data fields
106 void SetNativeFontInfo(const wxNativeFontInfo
& info
);
108 // debugger helper: shows what the font really is
110 // VZ: I need this as my gdb either shows wildly wrong values or crashes
111 // when I ask it to "p fontRefData" :-(
112 #if defined(__WXDEBUG__) && !defined(__WXGTK20__)
115 wxPrintf(_T("%s-%s-%s-%d-%d\n"),
117 m_weight
== wxFONTWEIGHT_NORMAL
119 : m_weight
== wxFONTWEIGHT_BOLD
122 m_style
== wxFONTSTYLE_NORMAL
? _T("regular") : _T("italic"),
129 // common part of all ctors
130 void Init(int pointSize
,
135 const wxString
& faceName
,
136 wxFontEncoding encoding
);
138 // set all fields from (already initialized and valid) m_nativeFontInfo
139 void InitFromNative();
142 // clear m_scaled_xfonts if any
143 void ClearGdkFonts();
146 // the map of font sizes to "GdkFont *"
147 wxScaledFontList m_scaled_xfonts
;
148 #endif // GTK 2.0/1.x
156 wxFontEncoding m_encoding
; // Unused under GTK 2.0
157 bool m_noAA
; // No anti-aliasing
159 // The native font info, basicly an XFLD under GTK 1.2 and
160 // the pango font description under GTK 2.0.
161 wxNativeFontInfo m_nativeFontInfo
;
166 // ----------------------------------------------------------------------------
168 // ----------------------------------------------------------------------------
170 void wxFontRefData::Init(int pointSize
,
175 const wxString
& faceName
,
176 wxFontEncoding encoding
)
178 m_family
= family
== wxFONTFAMILY_DEFAULT
? wxFONTFAMILY_SWISS
: family
;
180 m_faceName
= faceName
;
182 // we accept both wxDEFAULT and wxNORMAL here - should we?
183 m_style
= style
== wxDEFAULT
? wxFONTSTYLE_NORMAL
: style
;
184 m_weight
= weight
== wxDEFAULT
? wxFONTWEIGHT_NORMAL
: weight
;
186 // and here, do we really want to forbid creation of the font of the size
187 // 90 (the value of wxDEFAULT)??
188 m_pointSize
= pointSize
== wxDEFAULT
|| pointSize
== -1
189 ? wxDEFAULT_FONT_SIZE
192 m_underlined
= underlined
;
193 m_encoding
= encoding
;
198 // Create native font info
199 m_nativeFontInfo
.description
= pango_font_description_new();
201 // And set its values
202 if (!m_faceName
.empty())
204 pango_font_description_set_family( m_nativeFontInfo
.description
, wxGTK_CONV(m_faceName
) );
210 case wxFONTFAMILY_MODERN
:
211 case wxFONTFAMILY_TELETYPE
:
212 pango_font_description_set_family( m_nativeFontInfo
.description
, "monospace" );
214 case wxFONTFAMILY_ROMAN
:
215 pango_font_description_set_family( m_nativeFontInfo
.description
, "serif" );
217 case wxFONTFAMILY_SWISS
:
218 // SWISS = sans serif
220 pango_font_description_set_family( m_nativeFontInfo
.description
, "sans" );
226 SetPointSize( m_pointSize
);
227 SetWeight( m_weight
);
231 void wxFontRefData::InitFromNative()
237 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
240 m_faceName
= wxGTK_CONV_BACK( pango_font_description_get_family( desc
) );
242 m_pointSize
= pango_font_description_get_size( desc
) / PANGO_SCALE
;
244 switch (pango_font_description_get_style( desc
))
246 case PANGO_STYLE_NORMAL
:
247 m_style
= wxFONTSTYLE_NORMAL
;
249 case PANGO_STYLE_ITALIC
:
250 m_style
= wxFONTSTYLE_ITALIC
;
252 case PANGO_STYLE_OBLIQUE
:
253 m_style
= wxFONTSTYLE_SLANT
;
257 switch (pango_font_description_get_weight( desc
))
259 case PANGO_WEIGHT_ULTRALIGHT
:
260 m_weight
= wxFONTWEIGHT_LIGHT
;
262 case PANGO_WEIGHT_LIGHT
:
263 m_weight
= wxFONTWEIGHT_LIGHT
;
265 case PANGO_WEIGHT_NORMAL
:
266 m_weight
= wxFONTWEIGHT_NORMAL
;
268 case PANGO_WEIGHT_BOLD
:
269 m_weight
= wxFONTWEIGHT_BOLD
;
271 case PANGO_WEIGHT_ULTRABOLD
:
272 m_weight
= wxFONTWEIGHT_BOLD
;
274 case PANGO_WEIGHT_HEAVY
:
275 m_weight
= wxFONTWEIGHT_BOLD
;
279 if (m_faceName
== wxT("monospace"))
281 m_family
= wxFONTFAMILY_TELETYPE
;
283 else if (m_faceName
== wxT("sans"))
285 m_family
= wxFONTFAMILY_SWISS
;
287 else if (m_faceName
== wxT("serif"))
289 m_family
= wxFONTFAMILY_ROMAN
;
293 m_family
= wxFONTFAMILY_UNKNOWN
;
296 // Pango description are never underlined (?)
297 m_underlined
= FALSE
;
299 // Cannot we choose that
300 m_encoding
= wxFONTENCODING_SYSTEM
;
302 // get the font parameters from the XLFD
303 // -------------------------------------
305 m_faceName
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_FAMILY
);
307 m_weight
= wxFONTWEIGHT_NORMAL
;
309 wxString w
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_WEIGHT
).Upper();
310 if ( !w
.empty() && w
!= _T('*') )
312 // the test below catches all of BOLD, EXTRABOLD, DEMIBOLD, ULTRABOLD
314 if ( ((w
[0u] == _T('B') && (!wxStrcmp(w
.c_str() + 1, wxT("OLD")) ||
315 !wxStrcmp(w
.c_str() + 1, wxT("LACK"))))) ||
316 wxStrstr(w
.c_str() + 1, _T("BOLD")) )
318 m_weight
= wxFONTWEIGHT_BOLD
;
320 else if ( w
== _T("LIGHT") || w
== _T("THIN") )
322 m_weight
= wxFONTWEIGHT_LIGHT
;
326 switch ( wxToupper(*m_nativeFontInfo
.
327 GetXFontComponent(wxXLFD_SLANT
).c_str()) )
329 case _T('I'): // italique
330 m_style
= wxFONTSTYLE_ITALIC
;
333 case _T('O'): // oblique
334 m_style
= wxFONTSTYLE_SLANT
;
338 m_style
= wxFONTSTYLE_NORMAL
;
342 if ( m_nativeFontInfo
.GetXFontComponent(wxXLFD_POINTSIZE
).ToLong(&ptSize
) )
344 // size in XLFD is in 10 point units
345 m_pointSize
= (int)(ptSize
/ 10);
349 m_pointSize
= wxDEFAULT_FONT_SIZE
;
352 // examine the spacing: if the font is monospaced, assume wxTELETYPE
353 // family for compatibility with the old code which used it instead of
355 if ( m_nativeFontInfo
.GetXFontComponent(wxXLFD_SPACING
).Upper() == _T('M') )
357 m_family
= wxFONTFAMILY_TELETYPE
;
359 else // not monospaceed
361 // don't even try guessing it, it doesn't work for too many fonts
363 m_family
= wxFONTFAMILY_UNKNOWN
;
366 // X fonts are never underlined...
367 m_underlined
= FALSE
;
369 // deal with font encoding
371 registry
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_REGISTRY
).Upper(),
372 encoding
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_ENCODING
).Upper();
374 if ( registry
== _T("ISO8859") )
377 if ( wxSscanf(encoding
, wxT("%d"), &cp
) == 1 )
379 m_encoding
= (wxFontEncoding
)(wxFONTENCODING_ISO8859_1
+ cp
- 1);
382 else if ( registry
== _T("MICROSOFT") )
385 if ( wxSscanf(encoding
, wxT("cp125%d"), &cp
) == 1 )
387 m_encoding
= (wxFontEncoding
)(wxFONTENCODING_CP1250
+ cp
);
390 else if ( registry
== _T("KOI8") )
392 m_encoding
= wxFONTENCODING_KOI8
;
394 else // unknown encoding
396 // may be give a warning here? or use wxFontMapper?
397 m_encoding
= wxFONTENCODING_SYSTEM
;
399 #endif // GTK 2.0/1.x
402 wxFontRefData::wxFontRefData( const wxFontRefData
& data
)
405 m_pointSize
= data
.m_pointSize
;
406 m_family
= data
.m_family
;
407 m_style
= data
.m_style
;
408 m_weight
= data
.m_weight
;
410 m_underlined
= data
.m_underlined
;
412 m_faceName
= data
.m_faceName
;
413 m_encoding
= data
.m_encoding
;
415 m_noAA
= data
.m_noAA
;
417 // Forces a copy of the internal data. wxNativeFontInfo should probably
418 // have a copy ctor and assignment operator to fix this properly but that
419 // would break binary compatibility...
420 m_nativeFontInfo
.FromString(data
.m_nativeFontInfo
.ToString());
423 wxFontRefData::wxFontRefData(int size
, int family
, int style
,
424 int weight
, bool underlined
,
425 const wxString
& faceName
,
426 wxFontEncoding encoding
)
428 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
431 wxFontRefData::wxFontRefData(const wxString
& fontname
)
433 // VZ: FromString() should really work in both cases, doesn't it?
435 m_nativeFontInfo
.FromString( fontname
);
437 m_nativeFontInfo
.SetXFontName(fontname
);
438 #endif // GTK 2.0/1.x
443 void wxFontRefData::ReInit(const wxString
& fontname
)
445 m_nativeFontInfo
.SetXFontName(fontname
);
450 void wxFontRefData::ClearGdkFonts()
453 for ( wxScaledFontList::iterator i
= m_scaled_xfonts
.begin();
454 i
!= m_scaled_xfonts
.end();
457 GdkFont
*font
= i
->second
;
458 gdk_font_unref( font
);
461 m_scaled_xfonts
.clear();
465 wxFontRefData::~wxFontRefData()
470 // ----------------------------------------------------------------------------
471 // wxFontRefData SetXXX()
472 // ----------------------------------------------------------------------------
474 void wxFontRefData::SetPointSize(int pointSize
)
476 m_pointSize
= pointSize
;
480 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
482 pango_font_description_set_size( desc
, m_pointSize
* PANGO_SCALE
);
484 if ( HasNativeFont() )
487 if ( pointSize
== -1 )
490 size
.Printf(_T("%d"), 10*pointSize
);
492 m_nativeFontInfo
.SetXFontComponent(wxXLFD_POINTSIZE
, size
);
497 void wxFontRefData::SetFamily(int family
)
501 // TODO: what are we supposed to do with m_nativeFontInfo here?
504 void wxFontRefData::SetStyle(int style
)
510 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
514 case wxFONTSTYLE_ITALIC
:
515 pango_font_description_set_style( desc
, PANGO_STYLE_ITALIC
);
517 case wxFONTSTYLE_SLANT
:
518 pango_font_description_set_style( desc
, PANGO_STYLE_OBLIQUE
);
521 wxFAIL_MSG( _T("unknown font style") );
523 case wxFONTSTYLE_NORMAL
:
524 pango_font_description_set_style( desc
, PANGO_STYLE_NORMAL
);
528 if ( HasNativeFont() )
533 case wxFONTSTYLE_ITALIC
:
537 case wxFONTSTYLE_SLANT
:
542 wxFAIL_MSG( _T("unknown font style") );
545 case wxFONTSTYLE_NORMAL
:
549 m_nativeFontInfo
.SetXFontComponent(wxXLFD_SLANT
, slant
);
554 void wxFontRefData::SetWeight(int weight
)
559 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
562 case wxFONTWEIGHT_BOLD
:
563 pango_font_description_set_weight(desc
, PANGO_WEIGHT_BOLD
);
566 case wxFONTWEIGHT_LIGHT
:
567 pango_font_description_set_weight(desc
, PANGO_WEIGHT_LIGHT
);
571 wxFAIL_MSG( _T("unknown font weight") );
574 case wxFONTWEIGHT_NORMAL
:
576 pango_font_description_set_weight(desc
, PANGO_WEIGHT_NORMAL
);
579 if ( HasNativeFont() )
584 case wxFONTWEIGHT_BOLD
:
585 boldness
= _T("bold");
588 case wxFONTWEIGHT_LIGHT
:
589 boldness
= _T("light");
593 wxFAIL_MSG( _T("unknown font weight") );
596 case wxFONTWEIGHT_NORMAL
:
598 boldness
= _T("medium");
601 m_nativeFontInfo
.SetXFontComponent(wxXLFD_WEIGHT
, boldness
);
606 void wxFontRefData::SetUnderlined(bool underlined
)
608 m_underlined
= underlined
;
610 // the XLFD doesn't have "underlined" field anyhow
613 void wxFontRefData::SetFaceName(const wxString
& facename
)
615 m_faceName
= facename
;
618 if ( HasNativeFont() )
620 m_nativeFontInfo
.SetXFontComponent(wxXLFD_FAMILY
, facename
);
625 void wxFontRefData::SetEncoding(wxFontEncoding encoding
)
627 m_encoding
= encoding
;
630 if ( HasNativeFont() )
632 wxNativeEncodingInfo info
;
633 if ( wxGetNativeFontEncoding(encoding
, &info
) )
635 m_nativeFontInfo
.SetXFontComponent(wxXLFD_REGISTRY
, info
.xregistry
);
636 m_nativeFontInfo
.SetXFontComponent(wxXLFD_ENCODING
, info
.xencoding
);
642 void wxFontRefData::SetNativeFontInfo(const wxNativeFontInfo
& info
)
644 // previously cached fonts shouldn't be used
647 m_nativeFontInfo
= info
;
649 // set all the other font parameters from the native font info
653 // ----------------------------------------------------------------------------
655 // ----------------------------------------------------------------------------
657 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
663 wxFont::wxFont(const wxNativeFontInfo
& info
)
668 Create( info
.GetPointSize(),
672 info
.GetUnderlined(),
674 info
.GetEncoding() );
676 (void) Create(info
.GetXFontName());
680 bool wxFont::Create( int pointSize
,
685 const wxString
& face
,
686 wxFontEncoding encoding
)
690 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
691 underlined
, face
, encoding
);
696 bool wxFont::Create(const wxString
& fontname
)
698 // VZ: does this really happen?
699 if ( fontname
.empty() )
701 *this = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
706 m_refData
= new wxFontRefData(fontname
);
711 void wxFont::Unshare()
715 m_refData
= new wxFontRefData();
719 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
729 // ----------------------------------------------------------------------------
731 // ----------------------------------------------------------------------------
733 int wxFont::GetPointSize() const
735 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
737 return M_FONTDATA
->m_pointSize
;
740 wxString
wxFont::GetFaceName() const
742 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
744 return M_FONTDATA
->m_faceName
;
747 int wxFont::GetFamily() const
749 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
751 return M_FONTDATA
->m_family
;
754 int wxFont::GetStyle() const
756 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
758 return M_FONTDATA
->m_style
;
761 int wxFont::GetWeight() const
763 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
765 return M_FONTDATA
->m_weight
;
768 bool wxFont::GetUnderlined() const
770 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid font") );
772 return M_FONTDATA
->m_underlined
;
775 wxFontEncoding
wxFont::GetEncoding() const
777 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
779 return M_FONTDATA
->m_encoding
;
782 bool wxFont::GetNoAntiAliasing()
784 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
786 return M_FONTDATA
->m_noAA
;
789 wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
791 wxCHECK_MSG( Ok(), (wxNativeFontInfo
*)NULL
, wxT("invalid font") );
794 if ( M_FONTDATA
->m_nativeFontInfo
.GetXFontName().empty() )
798 return new wxNativeFontInfo(M_FONTDATA
->m_nativeFontInfo
);
801 bool wxFont::IsFixedWidth() const
803 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid font") );
806 if ( M_FONTDATA
->HasNativeFont() )
808 // the monospace fonts are supposed to have "M" in the spacing field
809 wxString spacing
= M_FONTDATA
->
810 m_nativeFontInfo
.GetXFontComponent(wxXLFD_SPACING
);
812 return spacing
.Upper() == _T('M');
816 return wxFontBase::IsFixedWidth();
819 // ----------------------------------------------------------------------------
820 // change font attributes
821 // ----------------------------------------------------------------------------
823 void wxFont::SetPointSize(int pointSize
)
827 M_FONTDATA
->SetPointSize(pointSize
);
830 void wxFont::SetFamily(int family
)
834 M_FONTDATA
->SetFamily(family
);
837 void wxFont::SetStyle(int style
)
841 M_FONTDATA
->SetStyle(style
);
844 void wxFont::SetWeight(int weight
)
848 M_FONTDATA
->SetWeight(weight
);
851 void wxFont::SetFaceName(const wxString
& faceName
)
855 M_FONTDATA
->SetFaceName(faceName
);
858 void wxFont::SetUnderlined(bool underlined
)
862 M_FONTDATA
->SetUnderlined(underlined
);
865 void wxFont::SetEncoding(wxFontEncoding encoding
)
869 M_FONTDATA
->SetEncoding(encoding
);
872 void wxFont::DoSetNativeFontInfo( const wxNativeFontInfo
& info
)
876 M_FONTDATA
->SetNativeFontInfo( info
);
879 void wxFont::SetNoAntiAliasing( bool no
)
883 M_FONTDATA
->SetNoAntiAliasing( no
);
886 // ----------------------------------------------------------------------------
887 // get internal representation of font
888 // ----------------------------------------------------------------------------
891 static GdkFont
*g_systemDefaultGuiFont
= (GdkFont
*) NULL
;
893 // this is also used from tbargtk.cpp and tooltip.cpp, hence extern
894 extern GdkFont
*GtkGetDefaultGuiFont()
896 if (!g_systemDefaultGuiFont
)
898 GtkWidget
*widget
= gtk_button_new();
899 GtkStyle
*def
= gtk_rc_get_style( widget
);
902 g_systemDefaultGuiFont
= gdk_font_ref( def
->font
);
906 def
= gtk_widget_get_default_style();
908 g_systemDefaultGuiFont
= gdk_font_ref( def
->font
);
910 gtk_widget_destroy( widget
);
914 // already have it, but ref it once more before returning
915 gdk_font_ref(g_systemDefaultGuiFont
);
918 return g_systemDefaultGuiFont
;
921 GdkFont
*wxFont::GetInternalFont( float scale
) const
923 GdkFont
*font
= (GdkFont
*) NULL
;
925 wxCHECK_MSG( Ok(), font
, wxT("invalid font") )
927 long int_scale
= long(scale
* 100.0 + 0.5); // key for fontlist
928 int point_scale
= (int)((M_FONTDATA
->m_pointSize
* 10 * int_scale
) / 100);
930 wxScaledFontList
& list
= M_FONTDATA
->m_scaled_xfonts
;
931 wxScaledFontList::iterator i
= list
.find(int_scale
);
932 if ( i
!= list
.end() )
936 else // we don't have this font in this size yet
938 if (*this == wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
))
940 font
= GtkGetDefaultGuiFont();
945 // do we have the XLFD?
946 if ( M_FONTDATA
->HasNativeFont() )
948 font
= wxLoadFont(M_FONTDATA
->m_nativeFontInfo
.GetXFontName());
951 // no XLFD of no exact match - try the approximate one now
955 font
= wxLoadQueryNearestFont( point_scale
,
956 M_FONTDATA
->m_family
,
958 M_FONTDATA
->m_weight
,
959 M_FONTDATA
->m_underlined
,
960 M_FONTDATA
->m_faceName
,
961 M_FONTDATA
->m_encoding
,
965 M_FONTDATA
->ReInit(xfontname
);
972 list
[int_scale
] = font
;
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?") );
982 #endif // not GTK 2.0