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 m_pointSize
= pango_font_description_get_size( desc
) / PANGO_SCALE
;
241 switch (pango_font_description_get_style( desc
))
243 case PANGO_STYLE_NORMAL
:
244 m_style
= wxFONTSTYLE_NORMAL
;
246 case PANGO_STYLE_ITALIC
:
247 m_style
= wxFONTSTYLE_ITALIC
;
249 case PANGO_STYLE_OBLIQUE
:
250 m_style
= wxFONTSTYLE_SLANT
;
254 switch (pango_font_description_get_weight( desc
))
256 case PANGO_WEIGHT_ULTRALIGHT
:
257 m_weight
= wxFONTWEIGHT_LIGHT
;
259 case PANGO_WEIGHT_LIGHT
:
260 m_weight
= wxFONTWEIGHT_LIGHT
;
262 case PANGO_WEIGHT_NORMAL
:
263 m_weight
= wxFONTWEIGHT_NORMAL
;
265 case PANGO_WEIGHT_BOLD
:
266 m_weight
= wxFONTWEIGHT_BOLD
;
268 case PANGO_WEIGHT_ULTRABOLD
:
269 m_weight
= wxFONTWEIGHT_BOLD
;
271 case PANGO_WEIGHT_HEAVY
:
272 m_weight
= wxFONTWEIGHT_BOLD
;
276 if (m_faceName
== wxT("monospace"))
278 m_family
= wxFONTFAMILY_TELETYPE
;
280 else if (m_faceName
== wxT("sans"))
282 m_family
= wxFONTFAMILY_SWISS
;
284 else if (m_faceName
== wxT("serif"))
286 m_family
= wxFONTFAMILY_ROMAN
;
290 m_family
= wxFONTFAMILY_UNKNOWN
;
293 // Pango description are never underlined (?)
294 m_underlined
= FALSE
;
296 // Cannot we choose that
297 m_encoding
= wxFONTENCODING_SYSTEM
;
299 // get the font parameters from the XLFD
300 // -------------------------------------
302 m_faceName
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_FAMILY
);
304 m_weight
= wxFONTWEIGHT_NORMAL
;
306 wxString w
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_WEIGHT
).Upper();
307 if ( !w
.empty() && w
!= _T('*') )
309 // the test below catches all of BOLD, EXTRABOLD, DEMIBOLD, ULTRABOLD
311 if ( ((w
[0u] == _T('B') && (!wxStrcmp(w
.c_str() + 1, wxT("OLD")) ||
312 !wxStrcmp(w
.c_str() + 1, wxT("LACK"))))) ||
313 wxStrstr(w
.c_str() + 1, _T("BOLD")) )
315 m_weight
= wxFONTWEIGHT_BOLD
;
317 else if ( w
== _T("LIGHT") || w
== _T("THIN") )
319 m_weight
= wxFONTWEIGHT_LIGHT
;
323 switch ( wxToupper(*m_nativeFontInfo
.
324 GetXFontComponent(wxXLFD_SLANT
).c_str()) )
326 case _T('I'): // italique
327 m_style
= wxFONTSTYLE_ITALIC
;
330 case _T('O'): // oblique
331 m_style
= wxFONTSTYLE_SLANT
;
335 m_style
= wxFONTSTYLE_NORMAL
;
339 if ( m_nativeFontInfo
.GetXFontComponent(wxXLFD_POINTSIZE
).ToLong(&ptSize
) )
341 // size in XLFD is in 10 point units
342 m_pointSize
= (int)(ptSize
/ 10);
346 m_pointSize
= wxDEFAULT_FONT_SIZE
;
349 // examine the spacing: if the font is monospaced, assume wxTELETYPE
350 // family for compatibility with the old code which used it instead of
352 if ( m_nativeFontInfo
.GetXFontComponent(wxXLFD_SPACING
).Upper() == _T('M') )
354 m_family
= wxFONTFAMILY_TELETYPE
;
356 else // not monospaceed
358 // don't even try guessing it, it doesn't work for too many fonts
360 m_family
= wxFONTFAMILY_UNKNOWN
;
363 // X fonts are never underlined...
364 m_underlined
= FALSE
;
366 // deal with font encoding
368 registry
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_REGISTRY
).Upper(),
369 encoding
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_ENCODING
).Upper();
371 if ( registry
== _T("ISO8859") )
374 if ( wxSscanf(encoding
, wxT("%d"), &cp
) == 1 )
376 m_encoding
= (wxFontEncoding
)(wxFONTENCODING_ISO8859_1
+ cp
- 1);
379 else if ( registry
== _T("MICROSOFT") )
382 if ( wxSscanf(encoding
, wxT("cp125%d"), &cp
) == 1 )
384 m_encoding
= (wxFontEncoding
)(wxFONTENCODING_CP1250
+ cp
);
387 else if ( registry
== _T("KOI8") )
389 m_encoding
= wxFONTENCODING_KOI8
;
391 else // unknown encoding
393 // may be give a warning here? or use wxFontMapper?
394 m_encoding
= wxFONTENCODING_SYSTEM
;
396 #endif // GTK 2.0/1.x
399 wxFontRefData::wxFontRefData( const wxFontRefData
& data
)
402 m_pointSize
= data
.m_pointSize
;
403 m_family
= data
.m_family
;
404 m_style
= data
.m_style
;
405 m_weight
= data
.m_weight
;
407 m_underlined
= data
.m_underlined
;
409 m_faceName
= data
.m_faceName
;
410 m_encoding
= data
.m_encoding
;
412 m_noAA
= data
.m_noAA
;
414 // Forces a copy of the internal data. wxNativeFontInfo should probably
415 // have a copy ctor and assignment operator to fix this properly but that
416 // would break binary compatibility...
417 m_nativeFontInfo
.FromString(data
.m_nativeFontInfo
.ToString());
420 wxFontRefData::wxFontRefData(int size
, int family
, int style
,
421 int weight
, bool underlined
,
422 const wxString
& faceName
,
423 wxFontEncoding encoding
)
425 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
428 wxFontRefData::wxFontRefData(const wxString
& fontname
)
430 // VZ: FromString() should really work in both cases, doesn't it?
432 m_nativeFontInfo
.FromString( fontname
);
434 m_nativeFontInfo
.SetXFontName(fontname
);
435 #endif // GTK 2.0/1.x
440 void wxFontRefData::ClearGdkFonts()
443 for ( wxScaledFontList::iterator i
= m_scaled_xfonts
.begin();
444 i
!= m_scaled_xfonts
.end();
447 GdkFont
*font
= i
->second
;
448 gdk_font_unref( font
);
451 m_scaled_xfonts
.clear();
455 wxFontRefData::~wxFontRefData()
460 // ----------------------------------------------------------------------------
461 // wxFontRefData SetXXX()
462 // ----------------------------------------------------------------------------
464 void wxFontRefData::SetPointSize(int pointSize
)
466 m_pointSize
= pointSize
;
470 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
472 pango_font_description_set_size( desc
, m_pointSize
* PANGO_SCALE
);
474 if ( HasNativeFont() )
477 if ( pointSize
== -1 )
480 size
.Printf(_T("%d"), 10*pointSize
);
482 m_nativeFontInfo
.SetXFontComponent(wxXLFD_POINTSIZE
, size
);
487 void wxFontRefData::SetFamily(int family
)
491 // TODO: what are we supposed to do with m_nativeFontInfo here?
494 void wxFontRefData::SetStyle(int style
)
500 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
504 case wxFONTSTYLE_ITALIC
:
505 pango_font_description_set_style( desc
, PANGO_STYLE_ITALIC
);
507 case wxFONTSTYLE_SLANT
:
508 pango_font_description_set_style( desc
, PANGO_STYLE_OBLIQUE
);
511 wxFAIL_MSG( _T("unknown font style") );
513 case wxFONTSTYLE_NORMAL
:
514 pango_font_description_set_style( desc
, PANGO_STYLE_NORMAL
);
518 if ( HasNativeFont() )
523 case wxFONTSTYLE_ITALIC
:
527 case wxFONTSTYLE_SLANT
:
532 wxFAIL_MSG( _T("unknown font style") );
535 case wxFONTSTYLE_NORMAL
:
539 m_nativeFontInfo
.SetXFontComponent(wxXLFD_SLANT
, slant
);
544 void wxFontRefData::SetWeight(int weight
)
549 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
552 case wxFONTWEIGHT_BOLD
:
553 pango_font_description_set_weight(desc
, PANGO_WEIGHT_BOLD
);
556 case wxFONTWEIGHT_LIGHT
:
557 pango_font_description_set_weight(desc
, PANGO_WEIGHT_LIGHT
);
561 wxFAIL_MSG( _T("unknown font weight") );
564 case wxFONTWEIGHT_NORMAL
:
566 pango_font_description_set_weight(desc
, PANGO_WEIGHT_NORMAL
);
569 if ( HasNativeFont() )
574 case wxFONTWEIGHT_BOLD
:
575 boldness
= _T("bold");
578 case wxFONTWEIGHT_LIGHT
:
579 boldness
= _T("light");
583 wxFAIL_MSG( _T("unknown font weight") );
586 case wxFONTWEIGHT_NORMAL
:
588 boldness
= _T("medium");
591 m_nativeFontInfo
.SetXFontComponent(wxXLFD_WEIGHT
, boldness
);
596 void wxFontRefData::SetUnderlined(bool underlined
)
598 m_underlined
= underlined
;
600 // the XLFD doesn't have "underlined" field anyhow
603 void wxFontRefData::SetFaceName(const wxString
& facename
)
605 m_faceName
= facename
;
608 if ( HasNativeFont() )
610 m_nativeFontInfo
.SetXFontComponent(wxXLFD_FAMILY
, facename
);
615 void wxFontRefData::SetEncoding(wxFontEncoding encoding
)
617 m_encoding
= encoding
;
620 if ( HasNativeFont() )
622 wxNativeEncodingInfo info
;
623 if ( wxGetNativeFontEncoding(encoding
, &info
) )
625 m_nativeFontInfo
.SetXFontComponent(wxXLFD_REGISTRY
, info
.xregistry
);
626 m_nativeFontInfo
.SetXFontComponent(wxXLFD_ENCODING
, info
.xencoding
);
632 void wxFontRefData::SetNativeFontInfo(const wxNativeFontInfo
& info
)
634 // previously cached fonts shouldn't be used
637 m_nativeFontInfo
= info
;
639 // set all the other font parameters from the native font info
643 // ----------------------------------------------------------------------------
645 // ----------------------------------------------------------------------------
647 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
653 wxFont::wxFont(const wxNativeFontInfo
& info
)
658 Create( info
.GetPointSize(),
662 info
.GetUnderlined(),
664 info
.GetEncoding() );
666 (void) Create(info
.GetXFontName());
670 bool wxFont::Create( int pointSize
,
675 const wxString
& face
,
676 wxFontEncoding encoding
)
680 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
681 underlined
, face
, encoding
);
686 bool wxFont::Create(const wxString
& fontname
)
688 // VZ: does this really happen?
689 if ( fontname
.empty() )
691 *this = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
696 m_refData
= new wxFontRefData(fontname
);
701 void wxFont::Unshare()
705 m_refData
= new wxFontRefData();
709 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
719 // ----------------------------------------------------------------------------
721 // ----------------------------------------------------------------------------
723 int wxFont::GetPointSize() const
725 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
727 return M_FONTDATA
->m_pointSize
;
730 wxString
wxFont::GetFaceName() const
732 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
734 return M_FONTDATA
->m_faceName
;
737 int wxFont::GetFamily() const
739 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
741 return M_FONTDATA
->m_family
;
744 int wxFont::GetStyle() const
746 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
748 return M_FONTDATA
->m_style
;
751 int wxFont::GetWeight() const
753 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
755 return M_FONTDATA
->m_weight
;
758 bool wxFont::GetUnderlined() const
760 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid font") );
762 return M_FONTDATA
->m_underlined
;
765 wxFontEncoding
wxFont::GetEncoding() const
767 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
769 return M_FONTDATA
->m_encoding
;
772 bool wxFont::GetNoAntiAliasing()
774 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
776 return M_FONTDATA
->m_noAA
;
779 wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
781 wxCHECK_MSG( Ok(), (wxNativeFontInfo
*)NULL
, wxT("invalid font") );
784 if ( M_FONTDATA
->m_nativeFontInfo
.GetXFontName().empty() )
788 return new wxNativeFontInfo(M_FONTDATA
->m_nativeFontInfo
);
791 bool wxFont::IsFixedWidth() const
793 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid font") );
796 if ( M_FONTDATA
->HasNativeFont() )
798 // the monospace fonts are supposed to have "M" in the spacing field
799 wxString spacing
= M_FONTDATA
->
800 m_nativeFontInfo
.GetXFontComponent(wxXLFD_SPACING
);
802 return spacing
.Upper() == _T('M');
806 return wxFontBase::IsFixedWidth();
809 // ----------------------------------------------------------------------------
810 // change font attributes
811 // ----------------------------------------------------------------------------
813 void wxFont::SetPointSize(int pointSize
)
817 M_FONTDATA
->SetPointSize(pointSize
);
820 void wxFont::SetFamily(int family
)
824 M_FONTDATA
->SetFamily(family
);
827 void wxFont::SetStyle(int style
)
831 M_FONTDATA
->SetStyle(style
);
834 void wxFont::SetWeight(int weight
)
838 M_FONTDATA
->SetWeight(weight
);
841 void wxFont::SetFaceName(const wxString
& faceName
)
845 M_FONTDATA
->SetFaceName(faceName
);
848 void wxFont::SetUnderlined(bool underlined
)
852 M_FONTDATA
->SetUnderlined(underlined
);
855 void wxFont::SetEncoding(wxFontEncoding encoding
)
859 M_FONTDATA
->SetEncoding(encoding
);
862 void wxFont::DoSetNativeFontInfo( const wxNativeFontInfo
& info
)
866 M_FONTDATA
->SetNativeFontInfo( info
);
869 void wxFont::SetNoAntiAliasing( bool no
)
873 M_FONTDATA
->SetNoAntiAliasing( no
);
876 // ----------------------------------------------------------------------------
877 // get internal representation of font
878 // ----------------------------------------------------------------------------
881 static GdkFont
*g_systemDefaultGuiFont
= (GdkFont
*) NULL
;
883 // this is also used from tbargtk.cpp and tooltip.cpp, hence extern
884 extern GdkFont
*GtkGetDefaultGuiFont()
886 if (!g_systemDefaultGuiFont
)
888 GtkWidget
*widget
= gtk_button_new();
889 GtkStyle
*def
= gtk_rc_get_style( widget
);
892 g_systemDefaultGuiFont
= gdk_font_ref( def
->font
);
896 def
= gtk_widget_get_default_style();
898 g_systemDefaultGuiFont
= gdk_font_ref( def
->font
);
900 gtk_widget_destroy( widget
);
904 // already have it, but ref it once more before returning
905 gdk_font_ref(g_systemDefaultGuiFont
);
908 return g_systemDefaultGuiFont
;
911 GdkFont
*wxFont::GetInternalFont( float scale
) const
913 GdkFont
*font
= (GdkFont
*) NULL
;
915 wxCHECK_MSG( Ok(), font
, wxT("invalid font") )
917 long int_scale
= long(scale
* 100.0 + 0.5); // key for fontlist
918 int point_scale
= (int)((M_FONTDATA
->m_pointSize
* 10 * int_scale
) / 100);
920 wxScaledFontList
& list
= M_FONTDATA
->m_scaled_xfonts
;
921 wxScaledFontList::iterator i
= list
.find(int_scale
);
922 if ( i
!= list
.end() )
926 else // we don't have this font in this size yet
928 if (*this == wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
))
930 font
= GtkGetDefaultGuiFont();
935 // do we have the XLFD?
936 if ( M_FONTDATA
->HasNativeFont() )
938 font
= wxLoadFont(M_FONTDATA
->m_nativeFontInfo
.GetXFontName());
941 // no XLFD of no exact match - try the approximate one now
945 font
= wxLoadQueryNearestFont( point_scale
,
946 M_FONTDATA
->m_family
,
948 M_FONTDATA
->m_weight
,
949 M_FONTDATA
->m_underlined
,
950 M_FONTDATA
->m_faceName
,
951 M_FONTDATA
->m_encoding
,
955 M_FONTDATA
->m_nativeFontInfo
.SetXFontName(xfontname
);
962 list
[int_scale
] = font
;
966 // it's quite useless to make it a wxCHECK because we're going to crash
968 wxASSERT_MSG( font
, wxT("could not load any font?") );
972 #endif // not GTK 2.0