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 wxFontFamily m_family
;
97 wxFontEncoding m_encoding
;
99 bool m_noAA
; // No anti-aliasing
101 // The native font info: basically a PangoFontDescription
102 wxNativeFontInfo m_nativeFontInfo
;
107 #define M_FONTDATA ((wxFontRefData*)m_refData)
109 // ----------------------------------------------------------------------------
111 // ----------------------------------------------------------------------------
113 void wxFontRefData::Init(int pointSize
,
118 const wxString
& faceName
,
119 wxFontEncoding encoding
)
121 m_family
= family
== wxFONTFAMILY_DEFAULT
? wxFONTFAMILY_SWISS
: family
;
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
) );
143 case wxFONTFAMILY_MODERN
:
144 case wxFONTFAMILY_TELETYPE
:
145 pango_font_description_set_family( m_nativeFontInfo
.description
, "monospace" );
147 case wxFONTFAMILY_ROMAN
:
148 pango_font_description_set_family( m_nativeFontInfo
.description
, "serif" );
150 case wxFONTFAMILY_SWISS
:
151 // SWISS = sans serif
153 pango_font_description_set_family( m_nativeFontInfo
.description
, "sans" );
158 SetStyle( style
== wxDEFAULT
? wxFONTSTYLE_NORMAL
: style
);
159 SetPointSize( (pointSize
== wxDEFAULT
|| pointSize
== -1)
160 ? wxDEFAULT_FONT_SIZE
162 SetWeight( weight
== wxDEFAULT
? wxFONTWEIGHT_NORMAL
: weight
);
165 void wxFontRefData::InitFromNative()
170 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
172 // Pango sometimes needs to have a size
173 int pango_size
= pango_font_description_get_size( desc
);
175 m_nativeFontInfo
.SetPointSize(wxDEFAULT_FONT_SIZE
);
177 wxString faceName
= wxGTK_CONV_BACK_SYS(pango_font_description_get_family(desc
));
178 if (faceName
== wxT("monospace"))
180 m_family
= wxFONTFAMILY_TELETYPE
;
182 else if (faceName
== wxT("sans"))
184 m_family
= wxFONTFAMILY_SWISS
;
186 else if (faceName
== wxT("serif"))
188 m_family
= wxFONTFAMILY_ROMAN
;
192 m_family
= wxFONTFAMILY_UNKNOWN
;
195 // Pango description are never underlined
196 m_underlined
= false;
198 // always with GTK+ 2
199 m_encoding
= wxFONTENCODING_UTF8
;
202 wxFontRefData::wxFontRefData( const wxFontRefData
& data
)
205 m_family
= data
.m_family
;
206 m_underlined
= data
.m_underlined
;
207 m_encoding
= data
.m_encoding
;
208 m_noAA
= data
.m_noAA
;
210 // Forces a copy of the internal data. wxNativeFontInfo should probably
211 // have a copy ctor and assignment operator to fix this properly but that
212 // would break binary compatibility...
213 m_nativeFontInfo
.FromString(data
.m_nativeFontInfo
.ToString());
216 wxFontRefData::wxFontRefData(int size
, wxFontFamily family
, wxFontStyle style
,
217 wxFontWeight weight
, bool underlined
,
218 const wxString
& faceName
,
219 wxFontEncoding encoding
)
221 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
224 wxFontRefData::wxFontRefData(const wxString
& nativeFontInfoString
)
226 m_nativeFontInfo
.FromString( nativeFontInfoString
);
231 wxFontRefData::~wxFontRefData()
235 // ----------------------------------------------------------------------------
236 // wxFontRefData SetXXX()
237 // ----------------------------------------------------------------------------
239 void wxFontRefData::SetPointSize(int pointSize
)
241 m_nativeFontInfo
.SetPointSize(pointSize
);
245 NOTE: disabled because pango_font_description_set_absolute_size() and
246 wxDC::GetCharHeight() do not mix well: setting with the former a pixel
247 size of "30" makes the latter return 36...
248 Besides, we need to return GetPointSize() a point size value even if
249 SetPixelSize() was used and this would require further changes
250 (and use of pango_font_description_get_size_is_absolute in some places).
252 bool wxFontRefData::SetPixelSize(const wxSize& pixelSize)
254 wxCHECK_MSG( pixelSize.GetWidth() >= 0 && pixelSize.GetHeight() > 0, false,
255 "Negative values for the pixel size or zero pixel height are not allowed" );
257 if (wx_pango_version_check(1,8,0) != NULL ||
258 pixelSize.GetWidth() != 0)
260 // NOTE: pango_font_description_set_absolute_size() only sets the font height;
261 // if the user set the pixel width of the font explicitly or the pango
262 // library is too old, we cannot proceed
266 pango_font_description_set_absolute_size( m_nativeFontInfo.description,
267 pixelSize.GetHeight() * PANGO_SCALE );
274 void wxFontRefData::SetFamily(wxFontFamily family
)
278 // wxNativeInfo::SetFamily asserts because is currently not implemented---
279 // we just save the family here FIXME
282 void wxFontRefData::SetStyle(wxFontStyle style
)
284 m_nativeFontInfo
.SetStyle((wxFontStyle
)style
);
287 void wxFontRefData::SetWeight(wxFontWeight weight
)
289 m_nativeFontInfo
.SetWeight((wxFontWeight
)weight
);
292 void wxFontRefData::SetUnderlined(bool underlined
)
294 m_underlined
= underlined
;
296 // the Pango font descriptor does not have an underlined attribute
297 // (and wxNativeFontInfo::SetUnderlined asserts); rather it's
298 // wxWindowDCImpl::DoDrawText that handles underlined fonts, so we
299 // here we just need to save the underlined attribute
302 bool wxFontRefData::SetFaceName(const wxString
& facename
)
304 return m_nativeFontInfo
.SetFaceName(facename
);
307 void wxFontRefData::SetEncoding(wxFontEncoding encoding
)
309 m_encoding
= encoding
;
311 // the internal Pango encoding is always UTF8; here we save the
312 // encoding just to make it possible to return it from GetEncoding()
313 // FIXME: this seems wrong; shouldn't GetEncoding() always return wxFONTENCODING_UTF8?
316 void wxFontRefData::SetNativeFontInfo(const wxNativeFontInfo
& info
)
318 m_nativeFontInfo
= info
;
320 // set all the other font parameters from the native font info
324 // ----------------------------------------------------------------------------
326 // ----------------------------------------------------------------------------
328 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
330 wxFont::wxFont(const wxNativeFontInfo
& info
)
332 Create( info
.GetPointSize(),
336 info
.GetUnderlined(),
338 info
.GetEncoding() );
341 bool wxFont::Create( int pointSize
,
346 const wxString
& face
,
347 wxFontEncoding encoding
)
351 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
352 underlined
, face
, encoding
);
357 bool wxFont::Create(const wxString
& fontname
)
359 // VZ: does this really happen?
360 if ( fontname
.empty() )
362 *this = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
367 m_refData
= new wxFontRefData(fontname
);
376 // ----------------------------------------------------------------------------
378 // ----------------------------------------------------------------------------
380 int wxFont::GetPointSize() const
382 wxCHECK_MSG( IsOk(), 0, wxT("invalid font") );
384 return M_FONTDATA
->m_nativeFontInfo
.GetPointSize();
387 wxString
wxFont::GetFaceName() const
389 wxCHECK_MSG( IsOk(), wxEmptyString
, wxT("invalid font") );
391 return M_FONTDATA
->m_nativeFontInfo
.GetFaceName();
394 wxFontFamily
wxFont::GetFamily() const
396 wxCHECK_MSG( IsOk(), wxFONTFAMILY_MAX
, wxT("invalid font") );
398 wxFontFamily ret
= M_FONTDATA
->m_nativeFontInfo
.GetFamily();
400 if (ret
== wxFONTFAMILY_DEFAULT
)
401 ret
= M_FONTDATA
->m_family
;
406 wxFontStyle
wxFont::GetStyle() const
408 wxCHECK_MSG( IsOk(), wxFONTSTYLE_MAX
, wxT("invalid font") );
410 return M_FONTDATA
->m_nativeFontInfo
.GetStyle();
413 wxFontWeight
wxFont::GetWeight() const
415 wxCHECK_MSG( IsOk(), wxFONTWEIGHT_MAX
, wxT("invalid font") );
417 return M_FONTDATA
->m_nativeFontInfo
.GetWeight();
420 bool wxFont::GetUnderlined() const
422 wxCHECK_MSG( IsOk(), false, wxT("invalid font") );
424 return M_FONTDATA
->m_underlined
;
427 wxFontEncoding
wxFont::GetEncoding() const
429 wxCHECK_MSG( IsOk(), wxFONTENCODING_SYSTEM
, wxT("invalid font") );
431 return M_FONTDATA
->m_encoding
;
434 bool wxFont::GetNoAntiAliasing() const
436 wxCHECK_MSG( IsOk(), false, wxT("invalid font") );
438 return M_FONTDATA
->m_noAA
;
441 const wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
443 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid font") );
445 return &(M_FONTDATA
->m_nativeFontInfo
);
448 bool wxFont::IsFixedWidth() const
450 wxCHECK_MSG( IsOk(), false, wxT("invalid font") );
452 return wxFontBase::IsFixedWidth();
455 // ----------------------------------------------------------------------------
456 // change font attributes
457 // ----------------------------------------------------------------------------
459 void wxFont::SetPointSize(int pointSize
)
463 M_FONTDATA
->SetPointSize(pointSize
);
466 void wxFont::SetFamily(wxFontFamily family
)
470 M_FONTDATA
->SetFamily(family
);
473 void wxFont::SetStyle(wxFontStyle style
)
477 M_FONTDATA
->SetStyle(style
);
480 void wxFont::SetWeight(wxFontWeight weight
)
484 M_FONTDATA
->SetWeight(weight
);
487 bool wxFont::SetFaceName(const wxString
& faceName
)
491 return M_FONTDATA
->SetFaceName(faceName
) &&
492 wxFontBase::SetFaceName(faceName
);
495 void wxFont::SetUnderlined(bool underlined
)
499 M_FONTDATA
->SetUnderlined(underlined
);
502 void wxFont::SetEncoding(wxFontEncoding encoding
)
506 M_FONTDATA
->SetEncoding(encoding
);
509 void wxFont::DoSetNativeFontInfo( const wxNativeFontInfo
& info
)
513 M_FONTDATA
->SetNativeFontInfo( info
);
516 void wxFont::SetNoAntiAliasing( bool no
)
520 M_FONTDATA
->SetNoAntiAliasing( no
);
523 wxGDIRefData
* wxFont::CreateGDIRefData() const
525 return new wxFontRefData
;
528 wxGDIRefData
* wxFont::CloneGDIRefData(const wxGDIRefData
* data
) const
530 return new wxFontRefData(*static_cast<const wxFontRefData
*>(data
));