1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/font.cpp
3 // Purpose: wxFont for wxGTK
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"
26 #include "wx/settings.h"
27 #include "wx/gdicmn.h"
30 #include "wx/fontutil.h"
31 #include "wx/tokenzr.h"
33 #include "wx/gtk/private.h"
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
39 // the default size (in points) for the fonts
40 static const int wxDEFAULT_FONT_SIZE
= 12;
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 class wxFontRefData
: public wxGDIRefData
49 // from broken down font parameters, also default ctor
50 wxFontRefData(int size
= -1,
51 wxFontFamily family
= wxFONTFAMILY_DEFAULT
,
52 wxFontStyle style
= wxFONTSTYLE_NORMAL
,
53 wxFontWeight weight
= wxFONTWEIGHT_NORMAL
,
54 bool underlined
= false,
55 const wxString
& faceName
= wxEmptyString
,
56 wxFontEncoding encoding
= wxFONTENCODING_DEFAULT
);
58 wxFontRefData(const wxString
& nativeFontInfoString
);
61 wxFontRefData( const wxFontRefData
& data
);
63 virtual ~wxFontRefData();
65 // setters: all of them also take care to modify m_nativeFontInfo if we
66 // have it so as to not lose the information not carried by our fields
67 void SetPointSize(int pointSize
);
68 void SetFamily(wxFontFamily family
);
69 void SetStyle(wxFontStyle style
);
70 void SetWeight(wxFontWeight weight
);
71 void SetUnderlined(bool underlined
);
72 bool SetFaceName(const wxString
& facename
);
73 void SetEncoding(wxFontEncoding encoding
);
75 // and this one also modifies all the other font data fields
76 void SetNativeFontInfo(const wxNativeFontInfo
& info
);
79 // common part of all ctors
80 void Init(int pointSize
,
85 const wxString
& faceName
,
86 wxFontEncoding encoding
);
88 // set all fields from (already initialized and valid) m_nativeFontInfo
89 void InitFromNative();
94 // The native font info: basically a PangoFontDescription
95 wxNativeFontInfo m_nativeFontInfo
;
100 #define M_FONTDATA ((wxFontRefData*)m_refData)
102 // ----------------------------------------------------------------------------
104 // ----------------------------------------------------------------------------
106 void wxFontRefData::Init(int pointSize
,
111 const wxString
& faceName
,
112 wxFontEncoding
WXUNUSED(encoding
))
114 if (family
== wxFONTFAMILY_DEFAULT
)
115 family
= wxFONTFAMILY_SWISS
;
117 m_underlined
= underlined
;
119 // Create native font info
120 m_nativeFontInfo
.description
= pango_font_description_new();
122 // And set its values
123 if (!faceName
.empty())
125 pango_font_description_set_family( m_nativeFontInfo
.description
,
126 wxGTK_CONV_SYS(faceName
) );
133 SetStyle( style
== wxDEFAULT
? wxFONTSTYLE_NORMAL
: style
);
134 SetPointSize( (pointSize
== wxDEFAULT
|| pointSize
== -1)
135 ? wxDEFAULT_FONT_SIZE
137 SetWeight( weight
== wxDEFAULT
? wxFONTWEIGHT_NORMAL
: weight
);
140 void wxFontRefData::InitFromNative()
143 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
145 // Pango sometimes needs to have a size
146 int pango_size
= pango_font_description_get_size( desc
);
148 m_nativeFontInfo
.SetPointSize(wxDEFAULT_FONT_SIZE
);
150 // Pango description are never underlined
151 m_underlined
= false;
154 wxFontRefData::wxFontRefData( const wxFontRefData
& data
)
157 m_underlined
= data
.m_underlined
;
159 // Forces a copy of the internal data. wxNativeFontInfo should probably
160 // have a copy ctor and assignment operator to fix this properly but that
161 // would break binary compatibility...
162 m_nativeFontInfo
.FromString(data
.m_nativeFontInfo
.ToString());
165 wxFontRefData::wxFontRefData(int size
, wxFontFamily family
, wxFontStyle style
,
166 wxFontWeight weight
, bool underlined
,
167 const wxString
& faceName
,
168 wxFontEncoding encoding
)
170 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
173 wxFontRefData::wxFontRefData(const wxString
& nativeFontInfoString
)
175 m_nativeFontInfo
.FromString( nativeFontInfoString
);
180 wxFontRefData::~wxFontRefData()
184 // ----------------------------------------------------------------------------
185 // wxFontRefData SetXXX()
186 // ----------------------------------------------------------------------------
188 void wxFontRefData::SetPointSize(int pointSize
)
190 m_nativeFontInfo
.SetPointSize(pointSize
);
194 NOTE: disabled because pango_font_description_set_absolute_size() and
195 wxDC::GetCharHeight() do not mix well: setting with the former a pixel
196 size of "30" makes the latter return 36...
197 Besides, we need to return GetPointSize() a point size value even if
198 SetPixelSize() was used and this would require further changes
199 (and use of pango_font_description_get_size_is_absolute in some places).
201 bool wxFontRefData::SetPixelSize(const wxSize& pixelSize)
203 wxCHECK_MSG( pixelSize.GetWidth() >= 0 && pixelSize.GetHeight() > 0, false,
204 "Negative values for the pixel size or zero pixel height are not allowed" );
206 if (wx_pango_version_check(1,8,0) != NULL ||
207 pixelSize.GetWidth() != 0)
209 // NOTE: pango_font_description_set_absolute_size() only sets the font height;
210 // if the user set the pixel width of the font explicitly or the pango
211 // library is too old, we cannot proceed
215 pango_font_description_set_absolute_size( m_nativeFontInfo.description,
216 pixelSize.GetHeight() * PANGO_SCALE );
222 void wxFontRefData::SetFamily(wxFontFamily family
)
224 m_nativeFontInfo
.SetFamily(family
);
227 void wxFontRefData::SetStyle(wxFontStyle style
)
229 m_nativeFontInfo
.SetStyle(style
);
232 void wxFontRefData::SetWeight(wxFontWeight weight
)
234 m_nativeFontInfo
.SetWeight(weight
);
237 void wxFontRefData::SetUnderlined(bool underlined
)
239 m_underlined
= underlined
;
241 // the Pango font descriptor does not have an underlined attribute
242 // (and wxNativeFontInfo::SetUnderlined asserts); rather it's
243 // wxWindowDCImpl::DoDrawText that handles underlined fonts, so we
244 // here we just need to save the underlined attribute
247 bool wxFontRefData::SetFaceName(const wxString
& facename
)
249 return m_nativeFontInfo
.SetFaceName(facename
);
252 void wxFontRefData::SetEncoding(wxFontEncoding
WXUNUSED(encoding
))
254 // with GTK+ 2 Pango always uses UTF8 internally, we cannot change it
257 void wxFontRefData::SetNativeFontInfo(const wxNativeFontInfo
& info
)
259 m_nativeFontInfo
= info
;
261 // set all the other font parameters from the native font info
265 // ----------------------------------------------------------------------------
267 // ----------------------------------------------------------------------------
269 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
271 wxFont::wxFont(const wxNativeFontInfo
& info
)
273 Create( info
.GetPointSize(),
277 info
.GetUnderlined(),
279 info
.GetEncoding() );
282 bool wxFont::Create( int pointSize
,
287 const wxString
& face
,
288 wxFontEncoding encoding
)
292 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
293 underlined
, face
, encoding
);
298 bool wxFont::Create(const wxString
& fontname
)
300 // VZ: does this really happen?
301 if ( fontname
.empty() )
303 *this = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
308 m_refData
= new wxFontRefData(fontname
);
317 // ----------------------------------------------------------------------------
319 // ----------------------------------------------------------------------------
321 int wxFont::GetPointSize() const
323 wxCHECK_MSG( IsOk(), 0, wxT("invalid font") );
325 return M_FONTDATA
->m_nativeFontInfo
.GetPointSize();
328 wxString
wxFont::GetFaceName() const
330 wxCHECK_MSG( IsOk(), wxEmptyString
, wxT("invalid font") );
332 return M_FONTDATA
->m_nativeFontInfo
.GetFaceName();
335 wxFontFamily
wxFont::DoGetFamily() const
337 return M_FONTDATA
->m_nativeFontInfo
.GetFamily();
340 wxFontStyle
wxFont::GetStyle() const
342 wxCHECK_MSG( IsOk(), wxFONTSTYLE_MAX
, wxT("invalid font") );
344 return M_FONTDATA
->m_nativeFontInfo
.GetStyle();
347 wxFontWeight
wxFont::GetWeight() const
349 wxCHECK_MSG( IsOk(), wxFONTWEIGHT_MAX
, wxT("invalid font") );
351 return M_FONTDATA
->m_nativeFontInfo
.GetWeight();
354 bool wxFont::GetUnderlined() const
356 wxCHECK_MSG( IsOk(), false, wxT("invalid font") );
358 return M_FONTDATA
->m_underlined
;
361 wxFontEncoding
wxFont::GetEncoding() const
363 wxCHECK_MSG( IsOk(), wxFONTENCODING_SYSTEM
, wxT("invalid font") );
365 return wxFONTENCODING_UTF8
;
366 // Pango always uses UTF8... see also SetEncoding()
369 const wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
371 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid font") );
373 return &(M_FONTDATA
->m_nativeFontInfo
);
376 bool wxFont::IsFixedWidth() const
378 wxCHECK_MSG( IsOk(), false, wxT("invalid font") );
380 return wxFontBase::IsFixedWidth();
383 // ----------------------------------------------------------------------------
384 // change font attributes
385 // ----------------------------------------------------------------------------
387 void wxFont::SetPointSize(int pointSize
)
391 M_FONTDATA
->SetPointSize(pointSize
);
394 void wxFont::SetFamily(wxFontFamily family
)
398 M_FONTDATA
->SetFamily(family
);
401 void wxFont::SetStyle(wxFontStyle style
)
405 M_FONTDATA
->SetStyle(style
);
408 void wxFont::SetWeight(wxFontWeight weight
)
412 M_FONTDATA
->SetWeight(weight
);
415 bool wxFont::SetFaceName(const wxString
& faceName
)
419 return M_FONTDATA
->SetFaceName(faceName
) &&
420 wxFontBase::SetFaceName(faceName
);
423 void wxFont::SetUnderlined(bool underlined
)
427 M_FONTDATA
->SetUnderlined(underlined
);
430 void wxFont::SetEncoding(wxFontEncoding encoding
)
434 M_FONTDATA
->SetEncoding(encoding
);
437 void wxFont::DoSetNativeFontInfo( const wxNativeFontInfo
& info
)
441 M_FONTDATA
->SetNativeFontInfo( info
);
444 wxGDIRefData
* wxFont::CreateGDIRefData() const
446 return new wxFontRefData
;
449 wxGDIRefData
* wxFont::CloneGDIRefData(const wxGDIRefData
* data
) const
451 return new wxFontRefData(*static_cast<const wxFontRefData
*>(data
));