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/cmndata.h"
28 #include "wx/gdicmn.h"
31 #include "wx/fontutil.h"
32 #include "wx/tokenzr.h"
34 #include "wx/gtk/private.h"
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 // the default size (in points) for the fonts
41 static const int wxDEFAULT_FONT_SIZE
= 12;
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 class wxFontRefData
: public wxGDIRefData
50 // from broken down font parameters, also default ctor
51 wxFontRefData(int size
= -1,
52 wxFontFamily family
= wxFONTFAMILY_DEFAULT
,
53 wxFontStyle style
= wxFONTSTYLE_NORMAL
,
54 wxFontWeight weight
= wxFONTWEIGHT_NORMAL
,
55 bool underlined
= false,
56 const wxString
& faceName
= wxEmptyString
,
57 wxFontEncoding encoding
= wxFONTENCODING_DEFAULT
);
59 wxFontRefData(const wxString
& nativeFontInfoString
);
62 wxFontRefData( const wxFontRefData
& data
);
64 virtual ~wxFontRefData();
66 // setters: all of them also take care to modify m_nativeFontInfo if we
67 // have it so as to not lose the information not carried by our fields
68 void SetPointSize(int pointSize
);
69 void SetFamily(wxFontFamily family
);
70 void SetStyle(wxFontStyle style
);
71 void SetWeight(wxFontWeight weight
);
72 void SetUnderlined(bool underlined
);
73 bool SetFaceName(const wxString
& facename
);
74 void SetEncoding(wxFontEncoding encoding
);
76 void SetNoAntiAliasing( bool no
= true ) { m_noAA
= no
; }
77 bool GetNoAntiAliasing() const { return m_noAA
; }
79 // and this one also modifies all the other font data fields
80 void SetNativeFontInfo(const wxNativeFontInfo
& info
);
83 // common part of all ctors
84 void Init(int pointSize
,
89 const wxString
& faceName
,
90 wxFontEncoding encoding
);
92 // set all fields from (already initialized and valid) m_nativeFontInfo
93 void InitFromNative();
97 bool m_noAA
; // No anti-aliasing
99 // The native font info: basically a PangoFontDescription
100 wxNativeFontInfo m_nativeFontInfo
;
105 #define M_FONTDATA ((wxFontRefData*)m_refData)
107 // ----------------------------------------------------------------------------
109 // ----------------------------------------------------------------------------
111 void wxFontRefData::Init(int pointSize
,
116 const wxString
& faceName
,
117 wxFontEncoding
WXUNUSED(encoding
))
119 if (family
== wxFONTFAMILY_DEFAULT
)
120 family
= wxFONTFAMILY_SWISS
;
122 m_underlined
= underlined
;
125 // Create native font info
126 m_nativeFontInfo
.description
= pango_font_description_new();
128 // And set its values
129 if (!faceName
.empty())
131 pango_font_description_set_family( m_nativeFontInfo
.description
,
132 wxGTK_CONV_SYS(faceName
) );
139 SetStyle( style
== wxDEFAULT
? wxFONTSTYLE_NORMAL
: style
);
140 SetPointSize( (pointSize
== wxDEFAULT
|| pointSize
== -1)
141 ? wxDEFAULT_FONT_SIZE
143 SetWeight( weight
== wxDEFAULT
? wxFONTWEIGHT_NORMAL
: weight
);
146 void wxFontRefData::InitFromNative()
151 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
153 // Pango sometimes needs to have a size
154 int pango_size
= pango_font_description_get_size( desc
);
156 m_nativeFontInfo
.SetPointSize(wxDEFAULT_FONT_SIZE
);
158 // Pango description are never underlined
159 m_underlined
= false;
162 wxFontRefData::wxFontRefData( const wxFontRefData
& data
)
165 m_underlined
= data
.m_underlined
;
166 m_noAA
= data
.m_noAA
;
168 // Forces a copy of the internal data. wxNativeFontInfo should probably
169 // have a copy ctor and assignment operator to fix this properly but that
170 // would break binary compatibility...
171 m_nativeFontInfo
.FromString(data
.m_nativeFontInfo
.ToString());
174 wxFontRefData::wxFontRefData(int size
, wxFontFamily family
, wxFontStyle style
,
175 wxFontWeight weight
, bool underlined
,
176 const wxString
& faceName
,
177 wxFontEncoding encoding
)
179 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
182 wxFontRefData::wxFontRefData(const wxString
& nativeFontInfoString
)
184 m_nativeFontInfo
.FromString( nativeFontInfoString
);
189 wxFontRefData::~wxFontRefData()
193 // ----------------------------------------------------------------------------
194 // wxFontRefData SetXXX()
195 // ----------------------------------------------------------------------------
197 void wxFontRefData::SetPointSize(int pointSize
)
199 m_nativeFontInfo
.SetPointSize(pointSize
);
203 NOTE: disabled because pango_font_description_set_absolute_size() and
204 wxDC::GetCharHeight() do not mix well: setting with the former a pixel
205 size of "30" makes the latter return 36...
206 Besides, we need to return GetPointSize() a point size value even if
207 SetPixelSize() was used and this would require further changes
208 (and use of pango_font_description_get_size_is_absolute in some places).
210 bool wxFontRefData::SetPixelSize(const wxSize& pixelSize)
212 wxCHECK_MSG( pixelSize.GetWidth() >= 0 && pixelSize.GetHeight() > 0, false,
213 "Negative values for the pixel size or zero pixel height are not allowed" );
215 if (wx_pango_version_check(1,8,0) != NULL ||
216 pixelSize.GetWidth() != 0)
218 // NOTE: pango_font_description_set_absolute_size() only sets the font height;
219 // if the user set the pixel width of the font explicitly or the pango
220 // library is too old, we cannot proceed
224 pango_font_description_set_absolute_size( m_nativeFontInfo.description,
225 pixelSize.GetHeight() * PANGO_SCALE );
231 void wxFontRefData::SetFamily(wxFontFamily family
)
233 m_nativeFontInfo
.SetFamily(family
);
236 void wxFontRefData::SetStyle(wxFontStyle style
)
238 m_nativeFontInfo
.SetStyle(style
);
241 void wxFontRefData::SetWeight(wxFontWeight weight
)
243 m_nativeFontInfo
.SetWeight(weight
);
246 void wxFontRefData::SetUnderlined(bool underlined
)
248 m_underlined
= underlined
;
250 // the Pango font descriptor does not have an underlined attribute
251 // (and wxNativeFontInfo::SetUnderlined asserts); rather it's
252 // wxWindowDCImpl::DoDrawText that handles underlined fonts, so we
253 // here we just need to save the underlined attribute
256 bool wxFontRefData::SetFaceName(const wxString
& facename
)
258 return m_nativeFontInfo
.SetFaceName(facename
);
261 void wxFontRefData::SetEncoding(wxFontEncoding
WXUNUSED(encoding
))
263 // with GTK+ 2 Pango always uses UTF8 internally, we cannot change it
266 void wxFontRefData::SetNativeFontInfo(const wxNativeFontInfo
& info
)
268 m_nativeFontInfo
= info
;
270 // set all the other font parameters from the native font info
274 // ----------------------------------------------------------------------------
276 // ----------------------------------------------------------------------------
278 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
280 wxFont::wxFont(const wxNativeFontInfo
& info
)
282 Create( info
.GetPointSize(),
286 info
.GetUnderlined(),
288 info
.GetEncoding() );
291 bool wxFont::Create( int pointSize
,
296 const wxString
& face
,
297 wxFontEncoding encoding
)
301 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
302 underlined
, face
, encoding
);
307 bool wxFont::Create(const wxString
& fontname
)
309 // VZ: does this really happen?
310 if ( fontname
.empty() )
312 *this = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
317 m_refData
= new wxFontRefData(fontname
);
326 // ----------------------------------------------------------------------------
328 // ----------------------------------------------------------------------------
330 int wxFont::GetPointSize() const
332 wxCHECK_MSG( IsOk(), 0, wxT("invalid font") );
334 return M_FONTDATA
->m_nativeFontInfo
.GetPointSize();
337 wxString
wxFont::GetFaceName() const
339 wxCHECK_MSG( IsOk(), wxEmptyString
, wxT("invalid font") );
341 return M_FONTDATA
->m_nativeFontInfo
.GetFaceName();
344 wxFontFamily
wxFont::GetFamily() const
346 wxCHECK_MSG( IsOk(), wxFONTFAMILY_MAX
, wxT("invalid font") );
348 return M_FONTDATA
->m_nativeFontInfo
.GetFamily();
351 wxFontStyle
wxFont::GetStyle() const
353 wxCHECK_MSG( IsOk(), wxFONTSTYLE_MAX
, wxT("invalid font") );
355 return M_FONTDATA
->m_nativeFontInfo
.GetStyle();
358 wxFontWeight
wxFont::GetWeight() const
360 wxCHECK_MSG( IsOk(), wxFONTWEIGHT_MAX
, wxT("invalid font") );
362 return M_FONTDATA
->m_nativeFontInfo
.GetWeight();
365 bool wxFont::GetUnderlined() const
367 wxCHECK_MSG( IsOk(), false, wxT("invalid font") );
369 return M_FONTDATA
->m_underlined
;
372 wxFontEncoding
wxFont::GetEncoding() const
374 wxCHECK_MSG( IsOk(), wxFONTENCODING_SYSTEM
, wxT("invalid font") );
376 return wxFONTENCODING_UTF8
;
377 // Pango always uses UTF8... see also SetEncoding()
380 bool wxFont::GetNoAntiAliasing() const
382 wxCHECK_MSG( IsOk(), false, wxT("invalid font") );
384 return M_FONTDATA
->m_noAA
;
387 const wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
389 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid font") );
391 return &(M_FONTDATA
->m_nativeFontInfo
);
394 bool wxFont::IsFixedWidth() const
396 wxCHECK_MSG( IsOk(), false, wxT("invalid font") );
398 return wxFontBase::IsFixedWidth();
401 // ----------------------------------------------------------------------------
402 // change font attributes
403 // ----------------------------------------------------------------------------
405 void wxFont::SetPointSize(int pointSize
)
409 M_FONTDATA
->SetPointSize(pointSize
);
412 void wxFont::SetFamily(wxFontFamily family
)
416 M_FONTDATA
->SetFamily(family
);
419 void wxFont::SetStyle(wxFontStyle style
)
423 M_FONTDATA
->SetStyle(style
);
426 void wxFont::SetWeight(wxFontWeight weight
)
430 M_FONTDATA
->SetWeight(weight
);
433 bool wxFont::SetFaceName(const wxString
& faceName
)
437 return M_FONTDATA
->SetFaceName(faceName
) &&
438 wxFontBase::SetFaceName(faceName
);
441 void wxFont::SetUnderlined(bool underlined
)
445 M_FONTDATA
->SetUnderlined(underlined
);
448 void wxFont::SetEncoding(wxFontEncoding encoding
)
452 M_FONTDATA
->SetEncoding(encoding
);
455 void wxFont::DoSetNativeFontInfo( const wxNativeFontInfo
& info
)
459 M_FONTDATA
->SetNativeFontInfo( info
);
462 void wxFont::SetNoAntiAliasing( bool no
)
466 M_FONTDATA
->SetNoAntiAliasing( no
);
469 wxGDIRefData
* wxFont::CreateGDIRefData() const
471 return new wxFontRefData
;
474 wxGDIRefData
* wxFont::CloneGDIRefData(const wxGDIRefData
* data
) const
476 return new wxFontRefData(*static_cast<const wxFontRefData
*>(data
));