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 // setters: all of them also take care to modify m_nativeFontInfo if we
90 // have it so as to not lose the information not carried by our fields
91 void SetPointSize(int pointSize
);
92 void SetFamily(int family
);
93 void SetStyle(int style
);
94 void SetWeight(int weight
);
95 void SetUnderlined(bool underlined
);
96 void SetFaceName(const wxString
& facename
);
97 void SetEncoding(wxFontEncoding encoding
);
99 void SetNoAntiAliasing( bool no
= TRUE
) { m_noAA
= no
; }
100 bool GetNoAntiAliasing() { return m_noAA
; }
102 // and this one also modifies all the other font data fields
103 void SetNativeFontInfo(const wxNativeFontInfo
& info
);
105 // debugger helper: shows what the font really is
107 // VZ: I need this as my gdb either shows wildly wrong values or crashes
108 // when I ask it to "p fontRefData" :-(
109 #if defined(__WXDEBUG__) && !defined(__WXGTK20__)
112 wxPrintf(_T("%s-%s-%s-%d-%d\n"),
114 m_weight
== wxFONTWEIGHT_NORMAL
116 : m_weight
== wxFONTWEIGHT_BOLD
119 m_style
== wxFONTSTYLE_NORMAL
? _T("regular") : _T("italic"),
126 // common part of all ctors
127 void Init(int pointSize
,
132 const wxString
& faceName
,
133 wxFontEncoding encoding
);
135 // set all fields from (already initialized and valid) m_nativeFontInfo
136 void InitFromNative();
139 // clear m_scaled_xfonts if any
140 void ClearGdkFonts();
143 // the map of font sizes to "GdkFont *"
144 wxScaledFontList m_scaled_xfonts
;
145 #endif // GTK 2.0/1.x
153 wxFontEncoding m_encoding
; // Unused under GTK 2.0
154 bool m_noAA
; // No anti-aliasing
156 // The native font info, basicly an XFLD under GTK 1.2 and
157 // the pango font description under GTK 2.0.
158 wxNativeFontInfo m_nativeFontInfo
;
163 // ----------------------------------------------------------------------------
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
;
195 // Create native font info
196 m_nativeFontInfo
.description
= pango_font_description_new();
198 // And set its values
199 if (!m_faceName
.empty())
201 pango_font_description_set_family( m_nativeFontInfo
.description
, wxGTK_CONV(m_faceName
) );
207 case wxFONTFAMILY_MODERN
:
208 case wxFONTFAMILY_TELETYPE
:
209 pango_font_description_set_family( m_nativeFontInfo
.description
, "monospace" );
211 case wxFONTFAMILY_ROMAN
:
212 pango_font_description_set_family( m_nativeFontInfo
.description
, "serif" );
214 case wxFONTFAMILY_SWISS
:
215 // SWISS = sans serif
217 pango_font_description_set_family( m_nativeFontInfo
.description
, "sans" );
223 SetPointSize( m_pointSize
);
224 SetWeight( m_weight
);
228 void wxFontRefData::InitFromNative()
234 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
237 m_faceName
= wxGTK_CONV_BACK( pango_font_description_get_family( desc
) );
239 // Pango sometimes needs to have a size
240 int pango_size
= pango_font_description_get_size( desc
);
242 pango_font_description_set_size( desc
, 12 * PANGO_SCALE
);
244 m_pointSize
= pango_font_description_get_size( desc
) / PANGO_SCALE
;
246 switch (pango_font_description_get_style( desc
))
248 case PANGO_STYLE_NORMAL
:
249 m_style
= wxFONTSTYLE_NORMAL
;
251 case PANGO_STYLE_ITALIC
:
252 m_style
= wxFONTSTYLE_ITALIC
;
254 case PANGO_STYLE_OBLIQUE
:
255 m_style
= wxFONTSTYLE_SLANT
;
259 switch (pango_font_description_get_weight( desc
))
261 case PANGO_WEIGHT_ULTRALIGHT
:
262 m_weight
= wxFONTWEIGHT_LIGHT
;
264 case PANGO_WEIGHT_LIGHT
:
265 m_weight
= wxFONTWEIGHT_LIGHT
;
267 case PANGO_WEIGHT_NORMAL
:
268 m_weight
= wxFONTWEIGHT_NORMAL
;
270 case PANGO_WEIGHT_BOLD
:
271 m_weight
= wxFONTWEIGHT_BOLD
;
273 case PANGO_WEIGHT_ULTRABOLD
:
274 m_weight
= wxFONTWEIGHT_BOLD
;
276 case PANGO_WEIGHT_HEAVY
:
277 m_weight
= wxFONTWEIGHT_BOLD
;
281 if (m_faceName
== wxT("monospace"))
283 m_family
= wxFONTFAMILY_TELETYPE
;
285 else if (m_faceName
== wxT("sans"))
287 m_family
= wxFONTFAMILY_SWISS
;
289 else if (m_faceName
== wxT("serif"))
291 m_family
= wxFONTFAMILY_ROMAN
;
295 m_family
= wxFONTFAMILY_UNKNOWN
;
298 // Pango description are never underlined (?)
299 m_underlined
= FALSE
;
301 // Cannot we choose that
302 m_encoding
= wxFONTENCODING_SYSTEM
;
304 // get the font parameters from the XLFD
305 // -------------------------------------
307 m_faceName
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_FAMILY
);
309 m_weight
= wxFONTWEIGHT_NORMAL
;
311 wxString w
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_WEIGHT
).Upper();
312 if ( !w
.empty() && w
!= _T('*') )
314 // the test below catches all of BOLD, EXTRABOLD, DEMIBOLD, ULTRABOLD
316 if ( ((w
[0u] == _T('B') && (!wxStrcmp(w
.c_str() + 1, wxT("OLD")) ||
317 !wxStrcmp(w
.c_str() + 1, wxT("LACK"))))) ||
318 wxStrstr(w
.c_str() + 1, _T("BOLD")) )
320 m_weight
= wxFONTWEIGHT_BOLD
;
322 else if ( w
== _T("LIGHT") || w
== _T("THIN") )
324 m_weight
= wxFONTWEIGHT_LIGHT
;
328 switch ( wxToupper(*m_nativeFontInfo
.
329 GetXFontComponent(wxXLFD_SLANT
).c_str()) )
331 case _T('I'): // italique
332 m_style
= wxFONTSTYLE_ITALIC
;
335 case _T('O'): // oblique
336 m_style
= wxFONTSTYLE_SLANT
;
340 m_style
= wxFONTSTYLE_NORMAL
;
344 if ( m_nativeFontInfo
.GetXFontComponent(wxXLFD_POINTSIZE
).ToLong(&ptSize
) )
346 // size in XLFD is in 10 point units
347 m_pointSize
= (int)(ptSize
/ 10);
351 m_pointSize
= wxDEFAULT_FONT_SIZE
;
354 // examine the spacing: if the font is monospaced, assume wxTELETYPE
355 // family for compatibility with the old code which used it instead of
357 if ( m_nativeFontInfo
.GetXFontComponent(wxXLFD_SPACING
).Upper() == _T('M') )
359 m_family
= wxFONTFAMILY_TELETYPE
;
361 else // not monospaceed
363 // don't even try guessing it, it doesn't work for too many fonts
365 m_family
= wxFONTFAMILY_UNKNOWN
;
368 // X fonts are never underlined...
369 m_underlined
= FALSE
;
371 // deal with font encoding
373 registry
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_REGISTRY
).Upper(),
374 encoding
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_ENCODING
).Upper();
376 if ( registry
== _T("ISO8859") )
379 if ( wxSscanf(encoding
, wxT("%d"), &cp
) == 1 )
381 m_encoding
= (wxFontEncoding
)(wxFONTENCODING_ISO8859_1
+ cp
- 1);
384 else if ( registry
== _T("MICROSOFT") )
387 if ( wxSscanf(encoding
, wxT("cp125%d"), &cp
) == 1 )
389 m_encoding
= (wxFontEncoding
)(wxFONTENCODING_CP1250
+ cp
);
392 else if ( registry
== _T("KOI8") )
394 m_encoding
= wxFONTENCODING_KOI8
;
396 else // unknown encoding
398 // may be give a warning here? or use wxFontMapper?
399 m_encoding
= wxFONTENCODING_SYSTEM
;
401 #endif // GTK 2.0/1.x
404 wxFontRefData::wxFontRefData( const wxFontRefData
& data
)
407 m_pointSize
= data
.m_pointSize
;
408 m_family
= data
.m_family
;
409 m_style
= data
.m_style
;
410 m_weight
= data
.m_weight
;
412 m_underlined
= data
.m_underlined
;
414 m_faceName
= data
.m_faceName
;
415 m_encoding
= data
.m_encoding
;
417 m_noAA
= data
.m_noAA
;
419 // Forces a copy of the internal data. wxNativeFontInfo should probably
420 // have a copy ctor and assignment operator to fix this properly but that
421 // would break binary compatibility...
422 m_nativeFontInfo
.FromString(data
.m_nativeFontInfo
.ToString());
425 wxFontRefData::wxFontRefData(int size
, int family
, int style
,
426 int weight
, bool underlined
,
427 const wxString
& faceName
,
428 wxFontEncoding encoding
)
430 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
433 wxFontRefData::wxFontRefData(const wxString
& fontname
)
435 // VZ: FromString() should really work in both cases, doesn't it?
437 m_nativeFontInfo
.FromString( fontname
);
439 m_nativeFontInfo
.SetXFontName(fontname
);
440 #endif // GTK 2.0/1.x
445 void wxFontRefData::ClearGdkFonts()
448 for ( wxScaledFontList::iterator i
= m_scaled_xfonts
.begin();
449 i
!= m_scaled_xfonts
.end();
452 GdkFont
*font
= i
->second
;
453 gdk_font_unref( font
);
456 m_scaled_xfonts
.clear();
460 wxFontRefData::~wxFontRefData()
465 // ----------------------------------------------------------------------------
466 // wxFontRefData SetXXX()
467 // ----------------------------------------------------------------------------
469 void wxFontRefData::SetPointSize(int pointSize
)
471 m_pointSize
= pointSize
;
475 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
477 pango_font_description_set_size( desc
, m_pointSize
* PANGO_SCALE
);
479 if ( HasNativeFont() )
482 if ( pointSize
== -1 )
485 size
.Printf(_T("%d"), 10*pointSize
);
487 m_nativeFontInfo
.SetXFontComponent(wxXLFD_POINTSIZE
, size
);
492 void wxFontRefData::SetFamily(int family
)
496 // TODO: what are we supposed to do with m_nativeFontInfo here?
499 void wxFontRefData::SetStyle(int style
)
505 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
509 case wxFONTSTYLE_ITALIC
:
510 pango_font_description_set_style( desc
, PANGO_STYLE_ITALIC
);
512 case wxFONTSTYLE_SLANT
:
513 pango_font_description_set_style( desc
, PANGO_STYLE_OBLIQUE
);
516 wxFAIL_MSG( _T("unknown font style") );
518 case wxFONTSTYLE_NORMAL
:
519 pango_font_description_set_style( desc
, PANGO_STYLE_NORMAL
);
523 if ( HasNativeFont() )
528 case wxFONTSTYLE_ITALIC
:
532 case wxFONTSTYLE_SLANT
:
537 wxFAIL_MSG( _T("unknown font style") );
540 case wxFONTSTYLE_NORMAL
:
544 m_nativeFontInfo
.SetXFontComponent(wxXLFD_SLANT
, slant
);
549 void wxFontRefData::SetWeight(int weight
)
554 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
557 case wxFONTWEIGHT_BOLD
:
558 pango_font_description_set_weight(desc
, PANGO_WEIGHT_BOLD
);
561 case wxFONTWEIGHT_LIGHT
:
562 pango_font_description_set_weight(desc
, PANGO_WEIGHT_LIGHT
);
566 wxFAIL_MSG( _T("unknown font weight") );
569 case wxFONTWEIGHT_NORMAL
:
571 pango_font_description_set_weight(desc
, PANGO_WEIGHT_NORMAL
);
574 if ( HasNativeFont() )
579 case wxFONTWEIGHT_BOLD
:
580 boldness
= _T("bold");
583 case wxFONTWEIGHT_LIGHT
:
584 boldness
= _T("light");
588 wxFAIL_MSG( _T("unknown font weight") );
591 case wxFONTWEIGHT_NORMAL
:
593 boldness
= _T("medium");
596 m_nativeFontInfo
.SetXFontComponent(wxXLFD_WEIGHT
, boldness
);
601 void wxFontRefData::SetUnderlined(bool underlined
)
603 m_underlined
= underlined
;
605 // the XLFD doesn't have "underlined" field anyhow
608 void wxFontRefData::SetFaceName(const wxString
& facename
)
610 m_faceName
= facename
;
613 if ( HasNativeFont() )
615 m_nativeFontInfo
.SetXFontComponent(wxXLFD_FAMILY
, facename
);
620 void wxFontRefData::SetEncoding(wxFontEncoding encoding
)
622 m_encoding
= encoding
;
625 if ( HasNativeFont() )
627 wxNativeEncodingInfo info
;
628 if ( wxGetNativeFontEncoding(encoding
, &info
) )
630 m_nativeFontInfo
.SetXFontComponent(wxXLFD_REGISTRY
, info
.xregistry
);
631 m_nativeFontInfo
.SetXFontComponent(wxXLFD_ENCODING
, info
.xencoding
);
637 void wxFontRefData::SetNativeFontInfo(const wxNativeFontInfo
& info
)
639 // previously cached fonts shouldn't be used
642 m_nativeFontInfo
= info
;
644 // set all the other font parameters from the native font info
648 // ----------------------------------------------------------------------------
650 // ----------------------------------------------------------------------------
652 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
658 wxFont::wxFont(const wxNativeFontInfo
& info
)
663 Create( info
.GetPointSize(),
667 info
.GetUnderlined(),
669 info
.GetEncoding() );
671 (void) Create(info
.GetXFontName());
675 bool wxFont::Create( int pointSize
,
680 const wxString
& face
,
681 wxFontEncoding encoding
)
685 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
686 underlined
, face
, encoding
);
691 bool wxFont::Create(const wxString
& fontname
)
693 // VZ: does this really happen?
694 if ( fontname
.empty() )
696 *this = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
701 m_refData
= new wxFontRefData(fontname
);
706 void wxFont::Unshare()
710 m_refData
= new wxFontRefData();
714 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
724 // ----------------------------------------------------------------------------
726 // ----------------------------------------------------------------------------
728 int wxFont::GetPointSize() const
730 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
732 return M_FONTDATA
->m_pointSize
;
735 wxString
wxFont::GetFaceName() const
737 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
739 return M_FONTDATA
->m_faceName
;
742 int wxFont::GetFamily() const
744 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
746 return M_FONTDATA
->m_family
;
749 int wxFont::GetStyle() const
751 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
753 return M_FONTDATA
->m_style
;
756 int wxFont::GetWeight() const
758 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
760 return M_FONTDATA
->m_weight
;
763 bool wxFont::GetUnderlined() const
765 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid font") );
767 return M_FONTDATA
->m_underlined
;
770 wxFontEncoding
wxFont::GetEncoding() const
772 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
774 return M_FONTDATA
->m_encoding
;
777 bool wxFont::GetNoAntiAliasing()
779 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
781 return M_FONTDATA
->m_noAA
;
784 const wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
786 wxCHECK_MSG( Ok(), (wxNativeFontInfo
*)NULL
, wxT("invalid font") );
789 if ( M_FONTDATA
->m_nativeFontInfo
.GetXFontName().empty() )
793 return &(M_FONTDATA
->m_nativeFontInfo
);
796 bool wxFont::IsFixedWidth() const
798 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid font") );
801 if ( M_FONTDATA
->HasNativeFont() )
803 // the monospace fonts are supposed to have "M" in the spacing field
804 wxString spacing
= M_FONTDATA
->
805 m_nativeFontInfo
.GetXFontComponent(wxXLFD_SPACING
);
807 return spacing
.Upper() == _T('M');
811 return wxFontBase::IsFixedWidth();
814 // ----------------------------------------------------------------------------
815 // change font attributes
816 // ----------------------------------------------------------------------------
818 void wxFont::SetPointSize(int pointSize
)
822 M_FONTDATA
->SetPointSize(pointSize
);
825 void wxFont::SetFamily(int family
)
829 M_FONTDATA
->SetFamily(family
);
832 void wxFont::SetStyle(int style
)
836 M_FONTDATA
->SetStyle(style
);
839 void wxFont::SetWeight(int weight
)
843 M_FONTDATA
->SetWeight(weight
);
846 void wxFont::SetFaceName(const wxString
& faceName
)
850 M_FONTDATA
->SetFaceName(faceName
);
853 void wxFont::SetUnderlined(bool underlined
)
857 M_FONTDATA
->SetUnderlined(underlined
);
860 void wxFont::SetEncoding(wxFontEncoding encoding
)
864 M_FONTDATA
->SetEncoding(encoding
);
867 void wxFont::DoSetNativeFontInfo( const wxNativeFontInfo
& info
)
871 M_FONTDATA
->SetNativeFontInfo( info
);
874 void wxFont::SetNoAntiAliasing( bool no
)
878 M_FONTDATA
->SetNoAntiAliasing( no
);
881 // ----------------------------------------------------------------------------
882 // get internal representation of font
883 // ----------------------------------------------------------------------------
886 static GdkFont
*g_systemDefaultGuiFont
= (GdkFont
*) NULL
;
888 // this is also used from tbargtk.cpp and tooltip.cpp, hence extern
889 extern GdkFont
*GtkGetDefaultGuiFont()
891 if (!g_systemDefaultGuiFont
)
893 GtkWidget
*widget
= gtk_button_new();
894 GtkStyle
*def
= gtk_rc_get_style( widget
);
897 g_systemDefaultGuiFont
= gdk_font_ref( def
->font
);
901 def
= gtk_widget_get_default_style();
903 g_systemDefaultGuiFont
= gdk_font_ref( def
->font
);
905 gtk_widget_destroy( widget
);
909 // already have it, but ref it once more before returning
910 gdk_font_ref(g_systemDefaultGuiFont
);
913 return g_systemDefaultGuiFont
;
916 GdkFont
*wxFont::GetInternalFont( float scale
) const
918 GdkFont
*font
= (GdkFont
*) NULL
;
920 wxCHECK_MSG( Ok(), font
, wxT("invalid font") )
922 long int_scale
= long(scale
* 100.0 + 0.5); // key for fontlist
923 int point_scale
= (int)((M_FONTDATA
->m_pointSize
* 10 * int_scale
) / 100);
925 wxScaledFontList
& list
= M_FONTDATA
->m_scaled_xfonts
;
926 wxScaledFontList::iterator i
= list
.find(int_scale
);
927 if ( i
!= list
.end() )
931 else // we don't have this font in this size yet
933 if (*this == wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
))
935 font
= GtkGetDefaultGuiFont();
940 // do we have the XLFD?
941 if ( M_FONTDATA
->HasNativeFont() )
943 font
= wxLoadFont(M_FONTDATA
->m_nativeFontInfo
.GetXFontName());
946 // no XLFD of no exact match - try the approximate one now
950 font
= wxLoadQueryNearestFont( point_scale
,
951 M_FONTDATA
->m_family
,
953 M_FONTDATA
->m_weight
,
954 M_FONTDATA
->m_underlined
,
955 M_FONTDATA
->m_faceName
,
956 M_FONTDATA
->m_encoding
,
963 list
[int_scale
] = font
;
967 // it's quite useless to make it a wxCHECK because we're going to crash
969 wxASSERT_MSG( font
, wxT("could not load any font?") );
973 #endif // not GTK 2.0