1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling and Julian Smart
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // ============================================================================
12 // ============================================================================
14 // ----------------------------------------------------------------------------
16 // ----------------------------------------------------------------------------
18 // For compilers that support precompilation, includes "wx.h".
19 #include "wx/wxprec.h"
22 #include "wx/fontutil.h"
23 #include "wx/cmndata.h"
26 #include "wx/gdicmn.h"
27 #include "wx/tokenzr.h"
28 #include "wx/settings.h"
32 #include "wx/gtk/private.h"
33 #include <gdk/gdkprivate.h>
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
39 // the default size (in points) for the fonts
40 static const int wxDEFAULT_FONT_SIZE
= 12;
42 // ----------------------------------------------------------------------------
43 // wxScaledFontList: maps the font sizes to the GDK fonts for the given font
44 // ----------------------------------------------------------------------------
46 WX_DECLARE_HASH_MAP(int, GdkFont
*, wxIntegerHash
, wxIntegerEqual
,
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 class wxFontRefData
: public wxObjectRefData
56 // from broken down font parameters, also default ctor
57 wxFontRefData(int size
= -1,
58 int family
= wxFONTFAMILY_DEFAULT
,
59 int style
= wxFONTSTYLE_NORMAL
,
60 int weight
= wxFONTWEIGHT_NORMAL
,
61 bool underlined
= FALSE
,
62 const wxString
& faceName
= wxEmptyString
,
63 wxFontEncoding encoding
= wxFONTENCODING_DEFAULT
);
66 wxFontRefData(const wxString
& fontname
);
69 wxFontRefData( const wxFontRefData
& data
);
71 virtual ~wxFontRefData();
73 // do we have the native font info?
74 bool HasNativeFont() const
77 // we always have a Pango font description
80 // only use m_nativeFontInfo if it had been initialized
81 return !m_nativeFontInfo
.IsDefault();
85 // setters: all of them also take care to modify m_nativeFontInfo if we
86 // have it so as to not lose the information not carried by our fields
87 void SetPointSize(int pointSize
);
88 void SetFamily(int family
);
89 void SetStyle(int style
);
90 void SetWeight(int weight
);
91 void SetUnderlined(bool underlined
);
92 void SetFaceName(const wxString
& facename
);
93 void SetEncoding(wxFontEncoding encoding
);
95 void SetNoAntiAliasing( bool no
= TRUE
) { m_noAA
= no
; }
96 bool GetNoAntiAliasing() const { return m_noAA
; }
98 // and this one also modifies all the other font data fields
99 void SetNativeFontInfo(const wxNativeFontInfo
& info
);
101 // debugger helper: shows what the font really is
103 // VZ: I need this as my gdb either shows wildly wrong values or crashes
104 // when I ask it to "p fontRefData" :-(
105 #if defined(__WXDEBUG__) && !defined(__WXGTK20__)
108 wxPrintf(_T("%s-%s-%s-%d-%d\n"),
110 m_weight
== wxFONTWEIGHT_NORMAL
112 : m_weight
== wxFONTWEIGHT_BOLD
115 m_style
== wxFONTSTYLE_NORMAL
? _T("regular") : _T("italic"),
122 // common part of all ctors
123 void Init(int pointSize
,
128 const wxString
& faceName
,
129 wxFontEncoding encoding
);
131 // set all fields from (already initialized and valid) m_nativeFontInfo
132 void InitFromNative();
135 // clear m_scaled_xfonts if any
136 void ClearGdkFonts();
139 // the map of font sizes to "GdkFont *"
140 wxScaledFontList m_scaled_xfonts
;
141 #endif // GTK 2.0/1.x
149 wxFontEncoding m_encoding
; // Unused under GTK 2.0
150 bool m_noAA
; // No anti-aliasing
152 // The native font info, basicly an XFLD under GTK 1.2 and
153 // the pango font description under GTK 2.0.
154 wxNativeFontInfo m_nativeFontInfo
;
159 // ----------------------------------------------------------------------------
161 // ----------------------------------------------------------------------------
163 void wxFontRefData::Init(int pointSize
,
168 const wxString
& faceName
,
169 wxFontEncoding encoding
)
171 m_family
= family
== wxFONTFAMILY_DEFAULT
? wxFONTFAMILY_SWISS
: family
;
173 m_faceName
= faceName
;
175 // we accept both wxDEFAULT and wxNORMAL here - should we?
176 m_style
= style
== wxDEFAULT
? wxFONTSTYLE_NORMAL
: style
;
177 m_weight
= weight
== wxDEFAULT
? wxFONTWEIGHT_NORMAL
: weight
;
179 // and here, do we really want to forbid creation of the font of the size
180 // 90 (the value of wxDEFAULT)??
181 m_pointSize
= pointSize
== wxDEFAULT
|| pointSize
== -1
182 ? wxDEFAULT_FONT_SIZE
185 m_underlined
= underlined
;
186 m_encoding
= encoding
;
191 // Create native font info
192 m_nativeFontInfo
.description
= pango_font_description_new();
194 // And set its values
195 if (!m_faceName
.empty())
197 pango_font_description_set_family( m_nativeFontInfo
.description
, wxGTK_CONV(m_faceName
) );
203 case wxFONTFAMILY_MODERN
:
204 case wxFONTFAMILY_TELETYPE
:
205 pango_font_description_set_family( m_nativeFontInfo
.description
, "monospace" );
207 case wxFONTFAMILY_ROMAN
:
208 pango_font_description_set_family( m_nativeFontInfo
.description
, "serif" );
210 case wxFONTFAMILY_SWISS
:
211 // SWISS = sans serif
213 pango_font_description_set_family( m_nativeFontInfo
.description
, "sans" );
219 SetPointSize( m_pointSize
);
220 SetWeight( m_weight
);
224 void wxFontRefData::InitFromNative()
230 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
233 m_faceName
= wxGTK_CONV_BACK( pango_font_description_get_family( desc
) );
235 // Pango sometimes needs to have a size
236 int pango_size
= pango_font_description_get_size( desc
);
238 m_nativeFontInfo
.SetPointSize(12);
240 m_pointSize
= m_nativeFontInfo
.GetPointSize();
241 m_style
= m_nativeFontInfo
.GetStyle();
242 m_weight
= m_nativeFontInfo
.GetWeight();
244 if (m_faceName
== wxT("monospace"))
246 m_family
= wxFONTFAMILY_TELETYPE
;
248 else if (m_faceName
== wxT("sans"))
250 m_family
= wxFONTFAMILY_SWISS
;
252 else if (m_faceName
== wxT("serif"))
254 m_family
= wxFONTFAMILY_ROMAN
;
258 m_family
= wxFONTFAMILY_UNKNOWN
;
261 // Pango description are never underlined (?)
262 m_underlined
= FALSE
;
264 // Cannot we choose that
265 m_encoding
= wxFONTENCODING_SYSTEM
;
267 // get the font parameters from the XLFD
268 // -------------------------------------
270 m_faceName
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_FAMILY
);
272 m_weight
= wxFONTWEIGHT_NORMAL
;
274 wxString w
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_WEIGHT
).Upper();
275 if ( !w
.empty() && w
!= _T('*') )
277 // the test below catches all of BOLD, EXTRABOLD, DEMIBOLD, ULTRABOLD
279 if ( ((w
[0u] == _T('B') && (!wxStrcmp(w
.c_str() + 1, wxT("OLD")) ||
280 !wxStrcmp(w
.c_str() + 1, wxT("LACK"))))) ||
281 wxStrstr(w
.c_str() + 1, _T("BOLD")) )
283 m_weight
= wxFONTWEIGHT_BOLD
;
285 else if ( w
== _T("LIGHT") || w
== _T("THIN") )
287 m_weight
= wxFONTWEIGHT_LIGHT
;
291 switch ( wxToupper(*m_nativeFontInfo
.
292 GetXFontComponent(wxXLFD_SLANT
).c_str()) )
294 case _T('I'): // italique
295 m_style
= wxFONTSTYLE_ITALIC
;
298 case _T('O'): // oblique
299 m_style
= wxFONTSTYLE_SLANT
;
303 m_style
= wxFONTSTYLE_NORMAL
;
307 if ( m_nativeFontInfo
.GetXFontComponent(wxXLFD_POINTSIZE
).ToLong(&ptSize
) )
309 // size in XLFD is in 10 point units
310 m_pointSize
= (int)(ptSize
/ 10);
314 m_pointSize
= wxDEFAULT_FONT_SIZE
;
317 // examine the spacing: if the font is monospaced, assume wxTELETYPE
318 // family for compatibility with the old code which used it instead of
320 if ( m_nativeFontInfo
.GetXFontComponent(wxXLFD_SPACING
).Upper() == _T('M') )
322 m_family
= wxFONTFAMILY_TELETYPE
;
324 else // not monospaceed
326 // don't even try guessing it, it doesn't work for too many fonts
328 m_family
= wxFONTFAMILY_UNKNOWN
;
331 // X fonts are never underlined...
332 m_underlined
= FALSE
;
334 // deal with font encoding
336 registry
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_REGISTRY
).Upper(),
337 encoding
= m_nativeFontInfo
.GetXFontComponent(wxXLFD_ENCODING
).Upper();
339 if ( registry
== _T("ISO8859") )
342 if ( wxSscanf(encoding
, wxT("%d"), &cp
) == 1 )
344 m_encoding
= (wxFontEncoding
)(wxFONTENCODING_ISO8859_1
+ cp
- 1);
347 else if ( registry
== _T("MICROSOFT") )
350 if ( wxSscanf(encoding
, wxT("cp125%d"), &cp
) == 1 )
352 m_encoding
= (wxFontEncoding
)(wxFONTENCODING_CP1250
+ cp
);
355 else if ( registry
== _T("KOI8") )
357 m_encoding
= wxFONTENCODING_KOI8
;
359 else // unknown encoding
361 // may be give a warning here? or use wxFontMapper?
362 m_encoding
= wxFONTENCODING_SYSTEM
;
364 #endif // GTK 2.0/1.x
367 wxFontRefData::wxFontRefData( const wxFontRefData
& data
)
370 m_pointSize
= data
.m_pointSize
;
371 m_family
= data
.m_family
;
372 m_style
= data
.m_style
;
373 m_weight
= data
.m_weight
;
375 m_underlined
= data
.m_underlined
;
377 m_faceName
= data
.m_faceName
;
378 m_encoding
= data
.m_encoding
;
380 m_noAA
= data
.m_noAA
;
382 // Forces a copy of the internal data. wxNativeFontInfo should probably
383 // have a copy ctor and assignment operator to fix this properly but that
384 // would break binary compatibility...
385 m_nativeFontInfo
.FromString(data
.m_nativeFontInfo
.ToString());
388 wxFontRefData::wxFontRefData(int size
, int family
, int style
,
389 int weight
, bool underlined
,
390 const wxString
& faceName
,
391 wxFontEncoding encoding
)
393 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
396 wxFontRefData::wxFontRefData(const wxString
& fontname
)
398 // VZ: FromString() should really work in both cases, doesn't it?
400 m_nativeFontInfo
.FromString( fontname
);
402 m_nativeFontInfo
.SetXFontName(fontname
);
403 #endif // GTK 2.0/1.x
408 void wxFontRefData::ClearGdkFonts()
411 for ( wxScaledFontList::iterator i
= m_scaled_xfonts
.begin();
412 i
!= m_scaled_xfonts
.end();
415 GdkFont
*font
= i
->second
;
416 gdk_font_unref( font
);
419 m_scaled_xfonts
.clear();
423 wxFontRefData::~wxFontRefData()
428 // ----------------------------------------------------------------------------
429 // wxFontRefData SetXXX()
430 // ----------------------------------------------------------------------------
432 void wxFontRefData::SetPointSize(int pointSize
)
434 m_pointSize
= pointSize
;
437 m_nativeFontInfo
.SetPointSize(pointSize
);
439 if ( HasNativeFont() )
442 if ( pointSize
== -1 )
445 size
.Printf(_T("%d"), 10*pointSize
);
447 m_nativeFontInfo
.SetXFontComponent(wxXLFD_POINTSIZE
, size
);
452 void wxFontRefData::SetFamily(int family
)
456 // TODO: what are we supposed to do with m_nativeFontInfo here?
459 void wxFontRefData::SetStyle(int style
)
464 m_nativeFontInfo
.SetStyle((wxFontStyle
)style
);
466 if ( HasNativeFont() )
471 case wxFONTSTYLE_ITALIC
:
475 case wxFONTSTYLE_SLANT
:
480 wxFAIL_MSG( _T("unknown font style") );
483 case wxFONTSTYLE_NORMAL
:
487 m_nativeFontInfo
.SetXFontComponent(wxXLFD_SLANT
, slant
);
492 void wxFontRefData::SetWeight(int weight
)
497 m_nativeFontInfo
.SetWeight((wxFontWeight
)weight
);
499 if ( HasNativeFont() )
504 case wxFONTWEIGHT_BOLD
:
505 boldness
= _T("bold");
508 case wxFONTWEIGHT_LIGHT
:
509 boldness
= _T("light");
513 wxFAIL_MSG( _T("unknown font weight") );
516 case wxFONTWEIGHT_NORMAL
:
518 boldness
= _T("medium");
521 m_nativeFontInfo
.SetXFontComponent(wxXLFD_WEIGHT
, boldness
);
526 void wxFontRefData::SetUnderlined(bool underlined
)
528 m_underlined
= underlined
;
530 // the XLFD doesn't have "underlined" field anyhow
533 void wxFontRefData::SetFaceName(const wxString
& facename
)
535 m_faceName
= facename
;
538 m_nativeFontInfo
.SetFaceName(facename
);
540 if ( HasNativeFont() )
542 m_nativeFontInfo
.SetXFontComponent(wxXLFD_FAMILY
, facename
);
547 void wxFontRefData::SetEncoding(wxFontEncoding encoding
)
549 m_encoding
= encoding
;
552 if ( HasNativeFont() )
554 wxNativeEncodingInfo info
;
555 if ( wxGetNativeFontEncoding(encoding
, &info
) )
557 m_nativeFontInfo
.SetXFontComponent(wxXLFD_REGISTRY
, info
.xregistry
);
558 m_nativeFontInfo
.SetXFontComponent(wxXLFD_ENCODING
, info
.xencoding
);
564 void wxFontRefData::SetNativeFontInfo(const wxNativeFontInfo
& info
)
566 // previously cached fonts shouldn't be used
569 m_nativeFontInfo
= info
;
571 // set all the other font parameters from the native font info
575 // ----------------------------------------------------------------------------
577 // ----------------------------------------------------------------------------
579 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
585 wxFont::wxFont(const wxNativeFontInfo
& info
)
590 Create( info
.GetPointSize(),
594 info
.GetUnderlined(),
596 info
.GetEncoding() );
598 (void) Create(info
.GetXFontName());
602 bool wxFont::Create( int pointSize
,
607 const wxString
& face
,
608 wxFontEncoding encoding
)
612 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
613 underlined
, face
, encoding
);
618 bool wxFont::Create(const wxString
& fontname
)
620 // VZ: does this really happen?
621 if ( fontname
.empty() )
623 *this = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
628 m_refData
= new wxFontRefData(fontname
);
633 void wxFont::Unshare()
637 m_refData
= new wxFontRefData();
641 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
651 // ----------------------------------------------------------------------------
653 // ----------------------------------------------------------------------------
655 int wxFont::GetPointSize() const
657 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
660 return M_FONTDATA
->HasNativeFont() ? M_FONTDATA
->m_nativeFontInfo
.GetPointSize()
661 : M_FONTDATA
->m_pointSize
;
663 return M_FONTDATA
->m_pointSize
;
667 wxString
wxFont::GetFaceName() const
669 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
672 return M_FONTDATA
->HasNativeFont() ? M_FONTDATA
->m_nativeFontInfo
.GetFaceName()
673 : M_FONTDATA
->m_faceName
;
675 return M_FONTDATA
->m_faceName
;
679 int wxFont::GetFamily() const
681 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
684 int ret
= M_FONTDATA
->m_family
;
685 if (M_FONTDATA
->HasNativeFont())
686 // wxNativeFontInfo::GetFamily is expensive, must not call more than once
687 ret
= M_FONTDATA
->m_nativeFontInfo
.GetFamily();
689 if (ret
== wxFONTFAMILY_DEFAULT
)
690 ret
= M_FONTDATA
->m_family
;
694 return M_FONTDATA
->m_family
;
698 int wxFont::GetStyle() const
700 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
703 return M_FONTDATA
->HasNativeFont() ? M_FONTDATA
->m_nativeFontInfo
.GetStyle()
704 : M_FONTDATA
->m_style
;
706 return M_FONTDATA
->m_style
;
710 int wxFont::GetWeight() const
712 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
715 return M_FONTDATA
->HasNativeFont() ? M_FONTDATA
->m_nativeFontInfo
.GetWeight()
716 : M_FONTDATA
->m_weight
;
718 return M_FONTDATA
->m_weight
;
722 bool wxFont::GetUnderlined() const
724 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid font") );
726 return M_FONTDATA
->m_underlined
;
729 wxFontEncoding
wxFont::GetEncoding() const
731 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
733 // m_encoding is unused in wxGTK2, return encoding that the user set.
734 return M_FONTDATA
->m_encoding
;
737 bool wxFont::GetNoAntiAliasing() const
739 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
741 return M_FONTDATA
->m_noAA
;
744 const wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
746 wxCHECK_MSG( Ok(), (wxNativeFontInfo
*)NULL
, wxT("invalid font") );
749 if ( !M_FONTDATA
->HasNativeFont() )
751 // NB: this call has important side-effect: it not only finds
752 // GdkFont representation, it also initializes m_nativeFontInfo
753 // by calling its SetXFontName method
758 return &(M_FONTDATA
->m_nativeFontInfo
);
761 bool wxFont::IsFixedWidth() const
763 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid font") );
766 if ( M_FONTDATA
->HasNativeFont() )
768 // the monospace fonts are supposed to have "M" in the spacing field
769 wxString spacing
= M_FONTDATA
->
770 m_nativeFontInfo
.GetXFontComponent(wxXLFD_SPACING
);
772 return spacing
.Upper() == _T('M');
776 return wxFontBase::IsFixedWidth();
779 // ----------------------------------------------------------------------------
780 // change font attributes
781 // ----------------------------------------------------------------------------
783 void wxFont::SetPointSize(int pointSize
)
787 M_FONTDATA
->SetPointSize(pointSize
);
790 void wxFont::SetFamily(int family
)
794 M_FONTDATA
->SetFamily(family
);
797 void wxFont::SetStyle(int style
)
801 M_FONTDATA
->SetStyle(style
);
804 void wxFont::SetWeight(int weight
)
808 M_FONTDATA
->SetWeight(weight
);
811 void wxFont::SetFaceName(const wxString
& faceName
)
815 M_FONTDATA
->SetFaceName(faceName
);
818 void wxFont::SetUnderlined(bool underlined
)
822 M_FONTDATA
->SetUnderlined(underlined
);
825 void wxFont::SetEncoding(wxFontEncoding encoding
)
829 M_FONTDATA
->SetEncoding(encoding
);
832 void wxFont::DoSetNativeFontInfo( const wxNativeFontInfo
& info
)
836 M_FONTDATA
->SetNativeFontInfo( info
);
839 void wxFont::SetNoAntiAliasing( bool no
)
843 M_FONTDATA
->SetNoAntiAliasing( no
);
846 // ----------------------------------------------------------------------------
847 // get internal representation of font
848 // ----------------------------------------------------------------------------
851 static GdkFont
*g_systemDefaultGuiFont
= (GdkFont
*) NULL
;
853 // this is also used from tbargtk.cpp and tooltip.cpp, hence extern
854 extern GdkFont
*GtkGetDefaultGuiFont()
856 if (!g_systemDefaultGuiFont
)
858 GtkWidget
*widget
= gtk_button_new();
859 GtkStyle
*def
= gtk_rc_get_style( widget
);
862 g_systemDefaultGuiFont
= gdk_font_ref( def
->font
);
866 def
= gtk_widget_get_default_style();
868 g_systemDefaultGuiFont
= gdk_font_ref( def
->font
);
870 gtk_widget_destroy( widget
);
874 // already have it, but ref it once more before returning
875 gdk_font_ref(g_systemDefaultGuiFont
);
878 return g_systemDefaultGuiFont
;
881 GdkFont
*wxFont::GetInternalFont( float scale
) const
883 GdkFont
*font
= (GdkFont
*) NULL
;
885 wxCHECK_MSG( Ok(), font
, wxT("invalid font") )
887 long int_scale
= long(scale
* 100.0 + 0.5); // key for fontlist
888 int point_scale
= (int)((M_FONTDATA
->m_pointSize
* 10 * int_scale
) / 100);
890 wxScaledFontList
& list
= M_FONTDATA
->m_scaled_xfonts
;
891 wxScaledFontList::iterator i
= list
.find(int_scale
);
892 if ( i
!= list
.end() )
896 else // we don't have this font in this size yet
898 if (*this == wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
))
900 font
= GtkGetDefaultGuiFont();
905 // do we have the XLFD?
906 if ( int_scale
== 100 && M_FONTDATA
->HasNativeFont() )
908 font
= wxLoadFont(M_FONTDATA
->m_nativeFontInfo
.GetXFontName());
911 // no XLFD of no exact match - try the approximate one now
915 font
= wxLoadQueryNearestFont( point_scale
,
916 M_FONTDATA
->m_family
,
918 M_FONTDATA
->m_weight
,
919 M_FONTDATA
->m_underlined
,
920 M_FONTDATA
->m_faceName
,
921 M_FONTDATA
->m_encoding
,
923 // NB: wxFont::GetNativeFontInfo relies on this
924 // side-effect of GetInternalFont
925 if ( int_scale
== 100 )
926 M_FONTDATA
->m_nativeFontInfo
.SetXFontName(xfontname
);
932 list
[int_scale
] = font
;
936 // it's quite useless to make it a wxCHECK because we're going to crash
938 wxASSERT_MSG( font
, wxT("could not load any font?") );
942 #endif // not GTK 2.0