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();
96 wxFontEncoding m_encoding
;
98 bool m_noAA
; // No anti-aliasing
100 // The native font info: basically a PangoFontDescription
101 wxNativeFontInfo m_nativeFontInfo
;
106 #define M_FONTDATA ((wxFontRefData*)m_refData)
108 // ----------------------------------------------------------------------------
110 // ----------------------------------------------------------------------------
112 void wxFontRefData::Init(int pointSize
,
117 const wxString
& faceName
,
118 wxFontEncoding encoding
)
120 if (family
== wxFONTFAMILY_DEFAULT
)
121 family
= wxFONTFAMILY_SWISS
;
123 m_underlined
= underlined
;
124 m_encoding
= encoding
;
125 if ( m_encoding
== wxFONTENCODING_DEFAULT
)
126 m_encoding
= wxFont::GetDefaultEncoding();
130 // Create native font info
131 m_nativeFontInfo
.description
= pango_font_description_new();
133 // And set its values
134 if (!faceName
.empty())
136 pango_font_description_set_family( m_nativeFontInfo
.description
,
137 wxGTK_CONV_SYS(faceName
) );
144 SetStyle( style
== wxDEFAULT
? wxFONTSTYLE_NORMAL
: style
);
145 SetPointSize( (pointSize
== wxDEFAULT
|| pointSize
== -1)
146 ? wxDEFAULT_FONT_SIZE
148 SetWeight( weight
== wxDEFAULT
? wxFONTWEIGHT_NORMAL
: weight
);
151 void wxFontRefData::InitFromNative()
156 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
158 // Pango sometimes needs to have a size
159 int pango_size
= pango_font_description_get_size( desc
);
161 m_nativeFontInfo
.SetPointSize(wxDEFAULT_FONT_SIZE
);
163 // Pango description are never underlined
164 m_underlined
= false;
166 // always with GTK+ 2
167 m_encoding
= wxFONTENCODING_UTF8
;
170 wxFontRefData::wxFontRefData( const wxFontRefData
& data
)
173 m_underlined
= data
.m_underlined
;
174 m_encoding
= data
.m_encoding
;
175 m_noAA
= data
.m_noAA
;
177 // Forces a copy of the internal data. wxNativeFontInfo should probably
178 // have a copy ctor and assignment operator to fix this properly but that
179 // would break binary compatibility...
180 m_nativeFontInfo
.FromString(data
.m_nativeFontInfo
.ToString());
183 wxFontRefData::wxFontRefData(int size
, wxFontFamily family
, wxFontStyle style
,
184 wxFontWeight weight
, bool underlined
,
185 const wxString
& faceName
,
186 wxFontEncoding encoding
)
188 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
191 wxFontRefData::wxFontRefData(const wxString
& nativeFontInfoString
)
193 m_nativeFontInfo
.FromString( nativeFontInfoString
);
198 wxFontRefData::~wxFontRefData()
202 // ----------------------------------------------------------------------------
203 // wxFontRefData SetXXX()
204 // ----------------------------------------------------------------------------
206 void wxFontRefData::SetPointSize(int pointSize
)
208 m_nativeFontInfo
.SetPointSize(pointSize
);
212 NOTE: disabled because pango_font_description_set_absolute_size() and
213 wxDC::GetCharHeight() do not mix well: setting with the former a pixel
214 size of "30" makes the latter return 36...
215 Besides, we need to return GetPointSize() a point size value even if
216 SetPixelSize() was used and this would require further changes
217 (and use of pango_font_description_get_size_is_absolute in some places).
219 bool wxFontRefData::SetPixelSize(const wxSize& pixelSize)
221 wxCHECK_MSG( pixelSize.GetWidth() >= 0 && pixelSize.GetHeight() > 0, false,
222 "Negative values for the pixel size or zero pixel height are not allowed" );
224 if (wx_pango_version_check(1,8,0) != NULL ||
225 pixelSize.GetWidth() != 0)
227 // NOTE: pango_font_description_set_absolute_size() only sets the font height;
228 // if the user set the pixel width of the font explicitly or the pango
229 // library is too old, we cannot proceed
233 pango_font_description_set_absolute_size( m_nativeFontInfo.description,
234 pixelSize.GetHeight() * PANGO_SCALE );
240 void wxFontRefData::SetFamily(wxFontFamily family
)
242 m_nativeFontInfo
.SetFamily(family
);
245 void wxFontRefData::SetStyle(wxFontStyle style
)
247 m_nativeFontInfo
.SetStyle(style
);
250 void wxFontRefData::SetWeight(wxFontWeight weight
)
252 m_nativeFontInfo
.SetWeight(weight
);
255 void wxFontRefData::SetUnderlined(bool underlined
)
257 m_underlined
= underlined
;
259 // the Pango font descriptor does not have an underlined attribute
260 // (and wxNativeFontInfo::SetUnderlined asserts); rather it's
261 // wxWindowDCImpl::DoDrawText that handles underlined fonts, so we
262 // here we just need to save the underlined attribute
265 bool wxFontRefData::SetFaceName(const wxString
& facename
)
267 return m_nativeFontInfo
.SetFaceName(facename
);
270 void wxFontRefData::SetEncoding(wxFontEncoding encoding
)
272 m_encoding
= encoding
;
274 // the internal Pango encoding is always UTF8; here we save the
275 // encoding just to make it possible to return it from GetEncoding()
276 // FIXME: this seems wrong; shouldn't GetEncoding() always return wxFONTENCODING_UTF8?
279 void wxFontRefData::SetNativeFontInfo(const wxNativeFontInfo
& info
)
281 m_nativeFontInfo
= info
;
283 // set all the other font parameters from the native font info
287 // ----------------------------------------------------------------------------
289 // ----------------------------------------------------------------------------
291 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
293 wxFont::wxFont(const wxNativeFontInfo
& info
)
295 Create( info
.GetPointSize(),
299 info
.GetUnderlined(),
301 info
.GetEncoding() );
304 bool wxFont::Create( int pointSize
,
309 const wxString
& face
,
310 wxFontEncoding encoding
)
314 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
315 underlined
, face
, encoding
);
320 bool wxFont::Create(const wxString
& fontname
)
322 // VZ: does this really happen?
323 if ( fontname
.empty() )
325 *this = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
330 m_refData
= new wxFontRefData(fontname
);
339 // ----------------------------------------------------------------------------
341 // ----------------------------------------------------------------------------
343 int wxFont::GetPointSize() const
345 wxCHECK_MSG( IsOk(), 0, wxT("invalid font") );
347 return M_FONTDATA
->m_nativeFontInfo
.GetPointSize();
350 wxString
wxFont::GetFaceName() const
352 wxCHECK_MSG( IsOk(), wxEmptyString
, wxT("invalid font") );
354 return M_FONTDATA
->m_nativeFontInfo
.GetFaceName();
357 wxFontFamily
wxFont::GetFamily() const
359 wxCHECK_MSG( IsOk(), wxFONTFAMILY_MAX
, wxT("invalid font") );
361 return M_FONTDATA
->m_nativeFontInfo
.GetFamily();
364 wxFontStyle
wxFont::GetStyle() const
366 wxCHECK_MSG( IsOk(), wxFONTSTYLE_MAX
, wxT("invalid font") );
368 return M_FONTDATA
->m_nativeFontInfo
.GetStyle();
371 wxFontWeight
wxFont::GetWeight() const
373 wxCHECK_MSG( IsOk(), wxFONTWEIGHT_MAX
, wxT("invalid font") );
375 return M_FONTDATA
->m_nativeFontInfo
.GetWeight();
378 bool wxFont::GetUnderlined() const
380 wxCHECK_MSG( IsOk(), false, wxT("invalid font") );
382 return M_FONTDATA
->m_underlined
;
385 wxFontEncoding
wxFont::GetEncoding() const
387 wxCHECK_MSG( IsOk(), wxFONTENCODING_SYSTEM
, wxT("invalid font") );
389 return M_FONTDATA
->m_encoding
;
392 bool wxFont::GetNoAntiAliasing() const
394 wxCHECK_MSG( IsOk(), false, wxT("invalid font") );
396 return M_FONTDATA
->m_noAA
;
399 const wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
401 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid font") );
403 return &(M_FONTDATA
->m_nativeFontInfo
);
406 bool wxFont::IsFixedWidth() const
408 wxCHECK_MSG( IsOk(), false, wxT("invalid font") );
410 return wxFontBase::IsFixedWidth();
413 // ----------------------------------------------------------------------------
414 // change font attributes
415 // ----------------------------------------------------------------------------
417 void wxFont::SetPointSize(int pointSize
)
421 M_FONTDATA
->SetPointSize(pointSize
);
424 void wxFont::SetFamily(wxFontFamily family
)
428 M_FONTDATA
->SetFamily(family
);
431 void wxFont::SetStyle(wxFontStyle style
)
435 M_FONTDATA
->SetStyle(style
);
438 void wxFont::SetWeight(wxFontWeight weight
)
442 M_FONTDATA
->SetWeight(weight
);
445 bool wxFont::SetFaceName(const wxString
& faceName
)
449 return M_FONTDATA
->SetFaceName(faceName
) &&
450 wxFontBase::SetFaceName(faceName
);
453 void wxFont::SetUnderlined(bool underlined
)
457 M_FONTDATA
->SetUnderlined(underlined
);
460 void wxFont::SetEncoding(wxFontEncoding encoding
)
464 M_FONTDATA
->SetEncoding(encoding
);
467 void wxFont::DoSetNativeFontInfo( const wxNativeFontInfo
& info
)
471 M_FONTDATA
->SetNativeFontInfo( info
);
474 void wxFont::SetNoAntiAliasing( bool no
)
478 M_FONTDATA
->SetNoAntiAliasing( no
);
481 wxGDIRefData
* wxFont::CreateGDIRefData() const
483 return new wxFontRefData
;
486 wxGDIRefData
* wxFont::CloneGDIRefData(const wxGDIRefData
* data
) const
488 return new wxFontRefData(*static_cast<const wxFontRefData
*>(data
));