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
);
60 wxFontRefData(const wxString
& fontname
);
63 wxFontRefData( const wxFontRefData
& data
);
65 virtual ~wxFontRefData();
67 // setters: all of them also take care to modify m_nativeFontInfo if we
68 // have it so as to not lose the information not carried by our fields
69 void SetPointSize(int pointSize
);
70 void SetFamily(wxFontFamily family
);
71 void SetStyle(wxFontStyle style
);
72 void SetWeight(wxFontWeight weight
);
73 void SetUnderlined(bool underlined
);
74 bool SetFaceName(const wxString
& facename
);
75 void SetEncoding(wxFontEncoding encoding
);
77 void SetNoAntiAliasing( bool no
= true ) { m_noAA
= no
; }
78 bool GetNoAntiAliasing() const { return m_noAA
; }
80 // and this one also modifies all the other font data fields
81 void SetNativeFontInfo(const wxNativeFontInfo
& info
);
84 // common part of all ctors
85 void Init(int pointSize
,
90 const wxString
& faceName
,
91 wxFontEncoding encoding
);
93 // set all fields from (already initialized and valid) m_nativeFontInfo
94 void InitFromNative();
97 wxFontFamily m_family
;
98 wxFontEncoding m_encoding
;
100 bool m_noAA
; // No anti-aliasing
102 // The native font info: basically a PangoFontDescription
103 wxNativeFontInfo m_nativeFontInfo
;
108 #define M_FONTDATA ((wxFontRefData*)m_refData)
110 // ----------------------------------------------------------------------------
112 // ----------------------------------------------------------------------------
114 void wxFontRefData::Init(int pointSize
,
119 const wxString
& faceName
,
120 wxFontEncoding encoding
)
122 m_family
= family
== wxFONTFAMILY_DEFAULT
? wxFONTFAMILY_SWISS
: family
;
124 m_underlined
= underlined
;
125 m_encoding
= encoding
;
126 if ( m_encoding
== wxFONTENCODING_DEFAULT
)
127 m_encoding
= wxFont::GetDefaultEncoding();
131 // Create native font info
132 m_nativeFontInfo
.description
= pango_font_description_new();
134 // And set its values
135 if (!faceName
.empty())
137 pango_font_description_set_family( m_nativeFontInfo
.description
,
138 wxGTK_CONV_SYS(faceName
) );
144 case wxFONTFAMILY_MODERN
:
145 case wxFONTFAMILY_TELETYPE
:
146 pango_font_description_set_family( m_nativeFontInfo
.description
, "monospace" );
148 case wxFONTFAMILY_ROMAN
:
149 pango_font_description_set_family( m_nativeFontInfo
.description
, "serif" );
151 case wxFONTFAMILY_SWISS
:
152 // SWISS = sans serif
154 pango_font_description_set_family( m_nativeFontInfo
.description
, "sans" );
159 SetStyle( style
== wxDEFAULT
? wxFONTSTYLE_NORMAL
: style
);
160 SetPointSize( (pointSize
== wxDEFAULT
|| pointSize
== -1)
161 ? wxDEFAULT_FONT_SIZE
163 SetWeight( weight
== wxDEFAULT
? wxFONTWEIGHT_NORMAL
: weight
);
166 void wxFontRefData::InitFromNative()
171 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
173 // Pango sometimes needs to have a size
174 int pango_size
= pango_font_description_get_size( desc
);
176 m_nativeFontInfo
.SetPointSize(wxDEFAULT_FONT_SIZE
);
178 wxString faceName
= wxGTK_CONV_BACK_SYS(pango_font_description_get_family(desc
));
179 if (faceName
== wxT("monospace"))
181 m_family
= wxFONTFAMILY_TELETYPE
;
183 else if (faceName
== wxT("sans"))
185 m_family
= wxFONTFAMILY_SWISS
;
187 else if (faceName
== wxT("serif"))
189 m_family
= wxFONTFAMILY_ROMAN
;
193 m_family
= wxFONTFAMILY_UNKNOWN
;
196 // Pango description are never underlined
197 m_underlined
= false;
199 // always with GTK+ 2
200 m_encoding
= wxFONTENCODING_UTF8
;
203 wxFontRefData::wxFontRefData( const wxFontRefData
& data
)
206 m_family
= data
.m_family
;
207 m_underlined
= data
.m_underlined
;
208 m_encoding
= data
.m_encoding
;
209 m_noAA
= data
.m_noAA
;
211 // Forces a copy of the internal data. wxNativeFontInfo should probably
212 // have a copy ctor and assignment operator to fix this properly but that
213 // would break binary compatibility...
214 m_nativeFontInfo
.FromString(data
.m_nativeFontInfo
.ToString());
217 wxFontRefData::wxFontRefData(int size
, wxFontFamily family
, wxFontStyle style
,
218 wxFontWeight weight
, bool underlined
,
219 const wxString
& faceName
,
220 wxFontEncoding encoding
)
222 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
225 wxFontRefData::wxFontRefData(const wxString
& fontname
)
227 m_nativeFontInfo
.FromString( fontname
);
232 wxFontRefData::~wxFontRefData()
236 // ----------------------------------------------------------------------------
237 // wxFontRefData SetXXX()
238 // ----------------------------------------------------------------------------
240 void wxFontRefData::SetPointSize(int pointSize
)
242 m_nativeFontInfo
.SetPointSize(pointSize
);
246 NOTE: disabled because pango_font_description_set_absolute_size() and
247 wxDC::GetCharHeight() do not mix well: setting with the former a pixel
248 size of "30" makes the latter return 36...
249 Besides, we need to return GetPointSize() a point size value even if
250 SetPixelSize() was used and this would require further changes
251 (and use of pango_font_description_get_size_is_absolute in some places).
253 bool wxFontRefData::SetPixelSize(const wxSize& pixelSize)
255 wxCHECK_MSG( pixelSize.GetWidth() >= 0 && pixelSize.GetHeight() > 0, false,
256 "Negative values for the pixel size or zero pixel height are not allowed" );
258 if (wx_pango_version_check(1,8,0) != NULL ||
259 pixelSize.GetWidth() != 0)
261 // NOTE: pango_font_description_set_absolute_size() only sets the font height;
262 // if the user set the pixel width of the font explicitly or the pango
263 // library is too old, we cannot proceed
267 pango_font_description_set_absolute_size( m_nativeFontInfo.description,
268 pixelSize.GetHeight() * PANGO_SCALE );
275 void wxFontRefData::SetFamily(wxFontFamily family
)
279 // wxNativeInfo::SetFamily asserts because is currently not implemented---
280 // we just save the family here FIXME
283 void wxFontRefData::SetStyle(wxFontStyle style
)
285 m_nativeFontInfo
.SetStyle((wxFontStyle
)style
);
288 void wxFontRefData::SetWeight(wxFontWeight weight
)
290 m_nativeFontInfo
.SetWeight((wxFontWeight
)weight
);
293 void wxFontRefData::SetUnderlined(bool underlined
)
295 m_underlined
= underlined
;
297 // the Pango font descriptor does not have an underlined attribute
298 // (and wxNativeFontInfo::SetUnderlined asserts); rather it's
299 // wxWindowDCImpl::DoDrawText that handles underlined fonts, so we
300 // here we just need to save the underlined attribute
303 bool wxFontRefData::SetFaceName(const wxString
& facename
)
305 return m_nativeFontInfo
.SetFaceName(facename
);
308 void wxFontRefData::SetEncoding(wxFontEncoding encoding
)
310 m_encoding
= encoding
;
312 // the internal Pango encoding is always UTF8; here we save the
313 // encoding just to make it possible to return it from GetEncoding()
314 // FIXME: this seems wrong; shouldn't GetEncoding() always return wxFONTENCODING_UTF8?
317 void wxFontRefData::SetNativeFontInfo(const wxNativeFontInfo
& info
)
319 m_nativeFontInfo
= info
;
321 // set all the other font parameters from the native font info
325 // ----------------------------------------------------------------------------
327 // ----------------------------------------------------------------------------
329 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
331 wxFont::wxFont(const wxNativeFontInfo
& info
)
333 Create( info
.GetPointSize(),
337 info
.GetUnderlined(),
339 info
.GetEncoding() );
342 bool wxFont::Create( int pointSize
,
347 const wxString
& face
,
348 wxFontEncoding encoding
)
352 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
353 underlined
, face
, encoding
);
358 bool wxFont::Create(const wxString
& fontname
)
360 // VZ: does this really happen?
361 if ( fontname
.empty() )
363 *this = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
368 m_refData
= new wxFontRefData(fontname
);
377 // ----------------------------------------------------------------------------
379 // ----------------------------------------------------------------------------
381 int wxFont::GetPointSize() const
383 wxCHECK_MSG( IsOk(), 0, wxT("invalid font") );
385 return M_FONTDATA
->m_nativeFontInfo
.GetPointSize();
388 wxString
wxFont::GetFaceName() const
390 wxCHECK_MSG( IsOk(), wxEmptyString
, wxT("invalid font") );
392 return M_FONTDATA
->m_nativeFontInfo
.GetFaceName();
395 wxFontFamily
wxFont::GetFamily() const
397 wxCHECK_MSG( IsOk(), wxFONTFAMILY_MAX
, wxT("invalid font") );
399 wxFontFamily ret
= M_FONTDATA
->m_nativeFontInfo
.GetFamily();
401 if (ret
== wxFONTFAMILY_DEFAULT
)
402 ret
= M_FONTDATA
->m_family
;
407 wxFontStyle
wxFont::GetStyle() const
409 wxCHECK_MSG( IsOk(), wxFONTSTYLE_MAX
, wxT("invalid font") );
411 return M_FONTDATA
->m_nativeFontInfo
.GetStyle();
414 wxFontWeight
wxFont::GetWeight() const
416 wxCHECK_MSG( IsOk(), wxFONTWEIGHT_MAX
, wxT("invalid font") );
418 return M_FONTDATA
->m_nativeFontInfo
.GetWeight();
421 bool wxFont::GetUnderlined() const
423 wxCHECK_MSG( IsOk(), false, wxT("invalid font") );
425 return M_FONTDATA
->m_underlined
;
428 wxFontEncoding
wxFont::GetEncoding() const
430 wxCHECK_MSG( IsOk(), wxFONTENCODING_SYSTEM
, wxT("invalid font") );
432 return M_FONTDATA
->m_encoding
;
435 bool wxFont::GetNoAntiAliasing() const
437 wxCHECK_MSG( IsOk(), false, wxT("invalid font") );
439 return M_FONTDATA
->m_noAA
;
442 const wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
444 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid font") );
446 return &(M_FONTDATA
->m_nativeFontInfo
);
449 bool wxFont::IsFixedWidth() const
451 wxCHECK_MSG( IsOk(), false, wxT("invalid font") );
453 return wxFontBase::IsFixedWidth();
456 // ----------------------------------------------------------------------------
457 // change font attributes
458 // ----------------------------------------------------------------------------
460 void wxFont::SetPointSize(int pointSize
)
464 M_FONTDATA
->SetPointSize(pointSize
);
467 void wxFont::SetFamily(wxFontFamily family
)
471 M_FONTDATA
->SetFamily(family
);
474 void wxFont::SetStyle(wxFontStyle style
)
478 M_FONTDATA
->SetStyle(style
);
481 void wxFont::SetWeight(wxFontWeight weight
)
485 M_FONTDATA
->SetWeight(weight
);
488 bool wxFont::SetFaceName(const wxString
& faceName
)
492 return M_FONTDATA
->SetFaceName(faceName
) &&
493 wxFontBase::SetFaceName(faceName
);
496 void wxFont::SetUnderlined(bool underlined
)
500 M_FONTDATA
->SetUnderlined(underlined
);
503 void wxFont::SetEncoding(wxFontEncoding encoding
)
507 M_FONTDATA
->SetEncoding(encoding
);
510 void wxFont::DoSetNativeFontInfo( const wxNativeFontInfo
& info
)
514 M_FONTDATA
->SetNativeFontInfo( info
);
517 void wxFont::SetNoAntiAliasing( bool no
)
521 M_FONTDATA
->SetNoAntiAliasing( no
);
524 wxGDIRefData
* wxFont::CreateGDIRefData() const
526 return new wxFontRefData
;
529 wxGDIRefData
* wxFont::CloneGDIRefData(const wxGDIRefData
* data
) const
531 return new wxFontRefData(*static_cast<const wxFontRefData
*>(data
));