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();
90 // reinitilize the font with the gived XFLD
91 void ReInit(const wxString
& fontname
);
94 // setters: all of them also take care to modify m_nativeFontInfo if we
95 // have it so as to not lose the information not carried by our fields
96 void SetPointSize(int pointSize
);
97 void SetFamily(int family
);
98 void SetStyle(int style
);
99 void SetWeight(int weight
);
100 void SetUnderlined(bool underlined
);
101 void SetFaceName(const wxString
& facename
);
102 void SetEncoding(wxFontEncoding encoding
);
104 void SetNoAntiAliasing( bool no
= TRUE
) { m_noAA
= no
; }
105 bool GetNoAntiAliasing() { return m_noAA
; }
107 // and this one also modifies all the other font data fields
108 void SetNativeFontInfo(const wxNativeFontInfo
& info
);
110 // debugger helper: shows what the font really is
112 // VZ: I need this as my gdb either shows wildly wrong values or crashes
113 // when I ask it to "p fontRefData" :-(
114 #if defined(__WXDEBUG__) && !defined(__WXGTK20__)
117 wxPrintf(_T("%s-%s-%s-%d-%d\n"),
119 m_weight
== wxFONTWEIGHT_NORMAL
121 : m_weight
== wxFONTWEIGHT_BOLD
124 m_style
== wxFONTSTYLE_NORMAL
? _T("regular") : _T("italic"),
131 // common part of all ctors
132 void Init(int pointSize
,
137 const wxString
& faceName
,
138 wxFontEncoding encoding
);
140 // set all fields from (already initialized and valid) m_nativeFontInfo
141 void InitFromNative();
144 // clear m_scaled_xfonts if any
145 void ClearGdkFonts();
148 // the map of font sizes to "GdkFont *"
149 wxScaledFontList m_scaled_xfonts
;
150 #endif // GTK 2.0/1.x
158 wxFontEncoding m_encoding
; // Unused under GTK 2.0
159 bool m_noAA
; // No anti-aliasing
161 // The native font info, basicly an XFLD under GTK 1.2 and
162 // the pango font description under GTK 2.0.
163 wxNativeFontInfo m_nativeFontInfo
;
168 // ----------------------------------------------------------------------------
170 // ----------------------------------------------------------------------------
172 void wxFontRefData::Init(int pointSize
,
177 const wxString
& faceName
,
178 wxFontEncoding encoding
)
180 m_family
= family
== wxFONTFAMILY_DEFAULT
? wxFONTFAMILY_SWISS
: family
;
182 m_faceName
= faceName
;
184 // we accept both wxDEFAULT and wxNORMAL here - should we?
185 m_style
= style
== wxDEFAULT
? wxFONTSTYLE_NORMAL
: style
;
186 m_weight
= weight
== wxDEFAULT
? wxFONTWEIGHT_NORMAL
: weight
;
188 // and here, do we really want to forbid creation of the font of the size
189 // 90 (the value of wxDEFAULT)??
190 m_pointSize
= pointSize
== wxDEFAULT
|| pointSize
== -1
191 ? wxDEFAULT_FONT_SIZE
194 m_underlined
= underlined
;
195 m_encoding
= encoding
;
200 // Create native font info
201 m_nativeFontInfo
.description
= pango_font_description_new();
203 // And set its values
204 if (!m_faceName
.empty())
206 pango_font_description_set_family( m_nativeFontInfo
.description
, wxGTK_CONV(m_faceName
) );
212 case wxFONTFAMILY_MODERN
:
213 case wxFONTFAMILY_TELETYPE
:
214 pango_font_description_set_family( m_nativeFontInfo
.description
, "monospace" );
216 case wxFONTFAMILY_ROMAN
:
217 pango_font_description_set_family( m_nativeFontInfo
.description
, "serif" );
219 case wxFONTFAMILY_SWISS
:
220 // SWISS = sans serif
222 pango_font_description_set_family( m_nativeFontInfo
.description
, "sans" );
228 SetPointSize( m_pointSize
);
229 SetWeight( m_weight
);
233 void wxFontRefData::InitFromNative()
239 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
242 m_faceName
= wxGTK_CONV_BACK( pango_font_description_get_family( desc
) );
244 // Pango sometimes needs to have a size
245 int pango_size
= pango_font_description_get_size( desc
);
247 pango_font_description_set_size( desc
, 12 * PANGO_SCALE
);
249 m_pointSize
= pango_font_description_get_size( desc
) / PANGO_SCALE
;
251 switch (pango_font_description_get_style( desc
))
253 case PANGO_STYLE_NORMAL
:
254 m_style
= wxFONTSTYLE_NORMAL
;
256 case PANGO_STYLE_ITALIC
:
257 m_style
= wxFONTSTYLE_ITALIC
;
259 case PANGO_STYLE_OBLIQUE
:
260 m_style
= wxFONTSTYLE_SLANT
;
264 switch (pango_font_description_get_weight( desc
))
266 case PANGO_WEIGHT_ULTRALIGHT
:
267 m_weight
= wxFONTWEIGHT_LIGHT
;
269 case PANGO_WEIGHT_LIGHT
:
270 m_weight
= wxFONTWEIGHT_LIGHT
;
272 case PANGO_WEIGHT_NORMAL
:
273 m_weight
= wxFONTWEIGHT_NORMAL
;
275 case PANGO_WEIGHT_BOLD
:
276 m_weight
= wxFONTWEIGHT_BOLD
;
278 case PANGO_WEIGHT_ULTRABOLD
:
279 m_weight
= wxFONTWEIGHT_BOLD
;
281 case PANGO_WEIGHT_HEAVY
:
282 m_weight
= wxFONTWEIGHT_BOLD
;
286 if (m_faceName
== wxT("monospace"))
288 m_family
= wxFONTFAMILY_TELETYPE
;
290 else if (m_faceName
== wxT("sans"))
292 m_family
= wxFONTFAMILY_SWISS
;
294 else if (m_faceName
== wxT("serif"))
296 m_family
= wxFONTFAMILY_ROMAN
;
300 m_family
= wxFONTFAMILY_UNKNOWN
;
303 // Pango description are never underlined (?)
304 m_underlined
= FALSE
;
306 // Cannot we choose that
307 m_encoding
= wxFONTENCODING_SYSTEM
;
309 // get the font parameters from the XLFD
310 // -------------------------------------
312 m_faceName
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_FAMILY
);
314 m_weight
= wxFONTWEIGHT_NORMAL
;
316 wxString w
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_WEIGHT
).Upper();
317 if ( !w
.empty() && w
!= _T('*') )
319 // the test below catches all of BOLD, EXTRABOLD, DEMIBOLD, ULTRABOLD
321 if ( ((w
[0u] == _T('B') && (!wxStrcmp(w
.c_str() + 1, wxT("OLD")) ||
322 !wxStrcmp(w
.c_str() + 1, wxT("LACK"))))) ||
323 wxStrstr(w
.c_str() + 1, _T("BOLD")) )
325 m_weight
= wxFONTWEIGHT_BOLD
;
327 else if ( w
== _T("LIGHT") || w
== _T("THIN") )
329 m_weight
= wxFONTWEIGHT_LIGHT
;
333 switch ( wxToupper(*m_nativeFontInfo
.
334 GetXFontComponent(wxXLFD_SLANT
).c_str()) )
336 case _T('I'): // italique
337 m_style
= wxFONTSTYLE_ITALIC
;
340 case _T('O'): // oblique
341 m_style
= wxFONTSTYLE_SLANT
;
345 m_style
= wxFONTSTYLE_NORMAL
;
349 if ( m_nativeFontInfo
.GetXFontComponent(wxXLFD_POINTSIZE
).ToLong(&ptSize
) )
351 // size in XLFD is in 10 point units
352 m_pointSize
= (int)(ptSize
/ 10);
356 m_pointSize
= wxDEFAULT_FONT_SIZE
;
359 // examine the spacing: if the font is monospaced, assume wxTELETYPE
360 // family for compatibility with the old code which used it instead of
362 if ( m_nativeFontInfo
.GetXFontComponent(wxXLFD_SPACING
).Upper() == _T('M') )
364 m_family
= wxFONTFAMILY_TELETYPE
;
366 else // not monospaceed
368 // don't even try guessing it, it doesn't work for too many fonts
370 m_family
= wxFONTFAMILY_UNKNOWN
;
373 // X fonts are never underlined...
374 m_underlined
= FALSE
;
376 // deal with font encoding
378 registry
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_REGISTRY
).Upper(),
379 encoding
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_ENCODING
).Upper();
381 if ( registry
== _T("ISO8859") )
384 if ( wxSscanf(encoding
, wxT("%d"), &cp
) == 1 )
386 m_encoding
= (wxFontEncoding
)(wxFONTENCODING_ISO8859_1
+ cp
- 1);
389 else if ( registry
== _T("MICROSOFT") )
392 if ( wxSscanf(encoding
, wxT("cp125%d"), &cp
) == 1 )
394 m_encoding
= (wxFontEncoding
)(wxFONTENCODING_CP1250
+ cp
);
397 else if ( registry
== _T("KOI8") )
399 m_encoding
= wxFONTENCODING_KOI8
;
401 else // unknown encoding
403 // may be give a warning here? or use wxFontMapper?
404 m_encoding
= wxFONTENCODING_SYSTEM
;
406 #endif // GTK 2.0/1.x
409 wxFontRefData::wxFontRefData( const wxFontRefData
& data
)
412 m_pointSize
= data
.m_pointSize
;
413 m_family
= data
.m_family
;
414 m_style
= data
.m_style
;
415 m_weight
= data
.m_weight
;
417 m_underlined
= data
.m_underlined
;
419 m_faceName
= data
.m_faceName
;
420 m_encoding
= data
.m_encoding
;
422 m_noAA
= data
.m_noAA
;
424 // Forces a copy of the internal data. wxNativeFontInfo should probably
425 // have a copy ctor and assignment operator to fix this properly but that
426 // would break binary compatibility...
427 m_nativeFontInfo
.FromString(data
.m_nativeFontInfo
.ToString());
430 wxFontRefData::wxFontRefData(int size
, int family
, int style
,
431 int weight
, bool underlined
,
432 const wxString
& faceName
,
433 wxFontEncoding encoding
)
435 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
438 wxFontRefData::wxFontRefData(const wxString
& fontname
)
440 // VZ: FromString() should really work in both cases, doesn't it?
442 m_nativeFontInfo
.FromString( fontname
);
444 m_nativeFontInfo
.SetXFontName(fontname
);
445 #endif // GTK 2.0/1.x
451 void wxFontRefData::ReInit(const wxString
& fontname
)
453 m_nativeFontInfo
.SetXFontName(fontname
);
459 void wxFontRefData::ClearGdkFonts()
462 for ( wxScaledFontList::iterator i
= m_scaled_xfonts
.begin();
463 i
!= m_scaled_xfonts
.end();
466 GdkFont
*font
= i
->second
;
467 gdk_font_unref( font
);
470 m_scaled_xfonts
.clear();
474 wxFontRefData::~wxFontRefData()
479 // ----------------------------------------------------------------------------
480 // wxFontRefData SetXXX()
481 // ----------------------------------------------------------------------------
483 void wxFontRefData::SetPointSize(int pointSize
)
485 m_pointSize
= pointSize
;
489 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
491 pango_font_description_set_size( desc
, m_pointSize
* PANGO_SCALE
);
493 if ( HasNativeFont() )
496 if ( pointSize
== -1 )
499 size
.Printf(_T("%d"), 10*pointSize
);
501 m_nativeFontInfo
.SetXFontComponent(wxXLFD_POINTSIZE
, size
);
506 void wxFontRefData::SetFamily(int family
)
510 // TODO: what are we supposed to do with m_nativeFontInfo here?
513 void wxFontRefData::SetStyle(int style
)
519 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
523 case wxFONTSTYLE_ITALIC
:
524 pango_font_description_set_style( desc
, PANGO_STYLE_ITALIC
);
526 case wxFONTSTYLE_SLANT
:
527 pango_font_description_set_style( desc
, PANGO_STYLE_OBLIQUE
);
530 wxFAIL_MSG( _T("unknown font style") );
532 case wxFONTSTYLE_NORMAL
:
533 pango_font_description_set_style( desc
, PANGO_STYLE_NORMAL
);
537 if ( HasNativeFont() )
542 case wxFONTSTYLE_ITALIC
:
546 case wxFONTSTYLE_SLANT
:
551 wxFAIL_MSG( _T("unknown font style") );
554 case wxFONTSTYLE_NORMAL
:
558 m_nativeFontInfo
.SetXFontComponent(wxXLFD_SLANT
, slant
);
563 void wxFontRefData::SetWeight(int weight
)
568 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
571 case wxFONTWEIGHT_BOLD
:
572 pango_font_description_set_weight(desc
, PANGO_WEIGHT_BOLD
);
575 case wxFONTWEIGHT_LIGHT
:
576 pango_font_description_set_weight(desc
, PANGO_WEIGHT_LIGHT
);
580 wxFAIL_MSG( _T("unknown font weight") );
583 case wxFONTWEIGHT_NORMAL
:
585 pango_font_description_set_weight(desc
, PANGO_WEIGHT_NORMAL
);
588 if ( HasNativeFont() )
593 case wxFONTWEIGHT_BOLD
:
594 boldness
= _T("bold");
597 case wxFONTWEIGHT_LIGHT
:
598 boldness
= _T("light");
602 wxFAIL_MSG( _T("unknown font weight") );
605 case wxFONTWEIGHT_NORMAL
:
607 boldness
= _T("medium");
610 m_nativeFontInfo
.SetXFontComponent(wxXLFD_WEIGHT
, boldness
);
615 void wxFontRefData::SetUnderlined(bool underlined
)
617 m_underlined
= underlined
;
619 // the XLFD doesn't have "underlined" field anyhow
622 void wxFontRefData::SetFaceName(const wxString
& facename
)
624 m_faceName
= facename
;
627 if ( HasNativeFont() )
629 m_nativeFontInfo
.SetXFontComponent(wxXLFD_FAMILY
, facename
);
634 void wxFontRefData::SetEncoding(wxFontEncoding encoding
)
636 m_encoding
= encoding
;
639 if ( HasNativeFont() )
641 wxNativeEncodingInfo info
;
642 if ( wxGetNativeFontEncoding(encoding
, &info
) )
644 m_nativeFontInfo
.SetXFontComponent(wxXLFD_REGISTRY
, info
.xregistry
);
645 m_nativeFontInfo
.SetXFontComponent(wxXLFD_ENCODING
, info
.xencoding
);
651 void wxFontRefData::SetNativeFontInfo(const wxNativeFontInfo
& info
)
653 // previously cached fonts shouldn't be used
656 m_nativeFontInfo
= info
;
658 // set all the other font parameters from the native font info
662 // ----------------------------------------------------------------------------
664 // ----------------------------------------------------------------------------
666 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
672 wxFont::wxFont(const wxNativeFontInfo
& info
)
677 Create( info
.GetPointSize(),
681 info
.GetUnderlined(),
683 info
.GetEncoding() );
685 (void) Create(info
.GetXFontName());
689 bool wxFont::Create( int pointSize
,
694 const wxString
& face
,
695 wxFontEncoding encoding
)
699 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
700 underlined
, face
, encoding
);
705 bool wxFont::Create(const wxString
& fontname
)
707 // VZ: does this really happen?
708 if ( fontname
.empty() )
710 *this = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
715 m_refData
= new wxFontRefData(fontname
);
720 void wxFont::Unshare()
724 m_refData
= new wxFontRefData();
728 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
738 // ----------------------------------------------------------------------------
740 // ----------------------------------------------------------------------------
742 int wxFont::GetPointSize() const
744 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
746 return M_FONTDATA
->m_pointSize
;
749 wxString
wxFont::GetFaceName() const
751 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
753 return M_FONTDATA
->m_faceName
;
756 int wxFont::GetFamily() const
758 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
760 return M_FONTDATA
->m_family
;
763 int wxFont::GetStyle() const
765 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
767 return M_FONTDATA
->m_style
;
770 int wxFont::GetWeight() const
772 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
774 return M_FONTDATA
->m_weight
;
777 bool wxFont::GetUnderlined() const
779 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid font") );
781 return M_FONTDATA
->m_underlined
;
784 wxFontEncoding
wxFont::GetEncoding() const
786 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
788 return M_FONTDATA
->m_encoding
;
791 bool wxFont::GetNoAntiAliasing()
793 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
795 return M_FONTDATA
->m_noAA
;
798 const wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
800 wxCHECK_MSG( Ok(), (wxNativeFontInfo
*)NULL
, wxT("invalid font") );
803 if ( M_FONTDATA
->m_nativeFontInfo
.GetXFontName().empty() )
807 return &(M_FONTDATA
->m_nativeFontInfo
);
810 bool wxFont::IsFixedWidth() const
812 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid font") );
815 if ( M_FONTDATA
->HasNativeFont() )
817 // the monospace fonts are supposed to have "M" in the spacing field
818 wxString spacing
= M_FONTDATA
->
819 m_nativeFontInfo
.GetXFontComponent(wxXLFD_SPACING
);
821 return spacing
.Upper() == _T('M');
825 return wxFontBase::IsFixedWidth();
828 // ----------------------------------------------------------------------------
829 // change font attributes
830 // ----------------------------------------------------------------------------
832 void wxFont::SetPointSize(int pointSize
)
836 M_FONTDATA
->SetPointSize(pointSize
);
839 void wxFont::SetFamily(int family
)
843 M_FONTDATA
->SetFamily(family
);
846 void wxFont::SetStyle(int style
)
850 M_FONTDATA
->SetStyle(style
);
853 void wxFont::SetWeight(int weight
)
857 M_FONTDATA
->SetWeight(weight
);
860 void wxFont::SetFaceName(const wxString
& faceName
)
864 M_FONTDATA
->SetFaceName(faceName
);
867 void wxFont::SetUnderlined(bool underlined
)
871 M_FONTDATA
->SetUnderlined(underlined
);
874 void wxFont::SetEncoding(wxFontEncoding encoding
)
878 M_FONTDATA
->SetEncoding(encoding
);
881 void wxFont::DoSetNativeFontInfo( const wxNativeFontInfo
& info
)
885 M_FONTDATA
->SetNativeFontInfo( info
);
888 void wxFont::SetNoAntiAliasing( bool no
)
892 M_FONTDATA
->SetNoAntiAliasing( no
);
895 // ----------------------------------------------------------------------------
896 // get internal representation of font
897 // ----------------------------------------------------------------------------
900 static GdkFont
*g_systemDefaultGuiFont
= (GdkFont
*) NULL
;
902 // this is also used from tbargtk.cpp and tooltip.cpp, hence extern
903 extern GdkFont
*GtkGetDefaultGuiFont()
905 if (!g_systemDefaultGuiFont
)
907 GtkWidget
*widget
= gtk_button_new();
908 GtkStyle
*def
= gtk_rc_get_style( widget
);
911 g_systemDefaultGuiFont
= gdk_font_ref( def
->font
);
915 def
= gtk_widget_get_default_style();
917 g_systemDefaultGuiFont
= gdk_font_ref( def
->font
);
919 gtk_widget_destroy( widget
);
923 // already have it, but ref it once more before returning
924 gdk_font_ref(g_systemDefaultGuiFont
);
927 return g_systemDefaultGuiFont
;
930 GdkFont
*wxFont::GetInternalFont( float scale
) const
932 GdkFont
*font
= (GdkFont
*) NULL
;
934 wxCHECK_MSG( Ok(), font
, wxT("invalid font") )
936 long int_scale
= long(scale
* 100.0 + 0.5); // key for fontlist
937 int point_scale
= (int)((M_FONTDATA
->m_pointSize
* 10 * int_scale
) / 100);
939 wxScaledFontList
& list
= M_FONTDATA
->m_scaled_xfonts
;
940 wxScaledFontList::iterator i
= list
.find(int_scale
);
941 if ( i
!= list
.end() )
945 else // we don't have this font in this size yet
947 if (*this == wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
))
949 font
= GtkGetDefaultGuiFont();
954 // do we have the XLFD?
955 if ( M_FONTDATA
->HasNativeFont() )
957 font
= wxLoadFont(M_FONTDATA
->m_nativeFontInfo
.GetXFontName());
960 // no XLFD of no exact match - try the approximate one now
964 font
= wxLoadQueryNearestFont( point_scale
,
965 M_FONTDATA
->m_family
,
967 M_FONTDATA
->m_weight
,
968 M_FONTDATA
->m_underlined
,
969 M_FONTDATA
->m_faceName
,
970 M_FONTDATA
->m_encoding
,
974 M_FONTDATA
->ReInit(xfontname
);
981 list
[int_scale
] = font
;
985 // it's quite useless to make it a wxCHECK because we're going to crash
987 wxASSERT_MSG( font
, wxT("could not load any font?") );
991 #endif // not GTK 2.0