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 // and this one also modifies all the other font data fields
77 void SetNativeFontInfo(const wxNativeFontInfo
& info
);
80 // common part of all ctors
81 void Init(int pointSize
,
86 const wxString
& faceName
,
87 wxFontEncoding encoding
);
89 // set all fields from (already initialized and valid) m_nativeFontInfo
90 void InitFromNative();
95 // The native font info: basically a PangoFontDescription
96 wxNativeFontInfo m_nativeFontInfo
;
101 #define M_FONTDATA ((wxFontRefData*)m_refData)
103 // ----------------------------------------------------------------------------
105 // ----------------------------------------------------------------------------
107 void wxFontRefData::Init(int pointSize
,
112 const wxString
& faceName
,
113 wxFontEncoding
WXUNUSED(encoding
))
115 if (family
== wxFONTFAMILY_DEFAULT
)
116 family
= wxFONTFAMILY_SWISS
;
118 m_underlined
= underlined
;
120 // Create native font info
121 m_nativeFontInfo
.description
= pango_font_description_new();
123 // And set its values
124 if (!faceName
.empty())
126 pango_font_description_set_family( m_nativeFontInfo
.description
,
127 wxGTK_CONV_SYS(faceName
) );
134 SetStyle( style
== wxDEFAULT
? wxFONTSTYLE_NORMAL
: style
);
135 SetPointSize( (pointSize
== wxDEFAULT
|| pointSize
== -1)
136 ? wxDEFAULT_FONT_SIZE
138 SetWeight( weight
== wxDEFAULT
? wxFONTWEIGHT_NORMAL
: weight
);
141 void wxFontRefData::InitFromNative()
144 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
146 // Pango sometimes needs to have a size
147 int pango_size
= pango_font_description_get_size( desc
);
149 m_nativeFontInfo
.SetPointSize(wxDEFAULT_FONT_SIZE
);
151 // Pango description are never underlined
152 m_underlined
= false;
155 wxFontRefData::wxFontRefData( const wxFontRefData
& data
)
158 m_underlined
= data
.m_underlined
;
160 // Forces a copy of the internal data. wxNativeFontInfo should probably
161 // have a copy ctor and assignment operator to fix this properly but that
162 // would break binary compatibility...
163 m_nativeFontInfo
.FromString(data
.m_nativeFontInfo
.ToString());
166 wxFontRefData::wxFontRefData(int size
, wxFontFamily family
, wxFontStyle style
,
167 wxFontWeight weight
, bool underlined
,
168 const wxString
& faceName
,
169 wxFontEncoding encoding
)
171 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
174 wxFontRefData::wxFontRefData(const wxString
& nativeFontInfoString
)
176 m_nativeFontInfo
.FromString( nativeFontInfoString
);
181 wxFontRefData::~wxFontRefData()
185 // ----------------------------------------------------------------------------
186 // wxFontRefData SetXXX()
187 // ----------------------------------------------------------------------------
189 void wxFontRefData::SetPointSize(int pointSize
)
191 m_nativeFontInfo
.SetPointSize(pointSize
);
195 NOTE: disabled because pango_font_description_set_absolute_size() and
196 wxDC::GetCharHeight() do not mix well: setting with the former a pixel
197 size of "30" makes the latter return 36...
198 Besides, we need to return GetPointSize() a point size value even if
199 SetPixelSize() was used and this would require further changes
200 (and use of pango_font_description_get_size_is_absolute in some places).
202 bool wxFontRefData::SetPixelSize(const wxSize& pixelSize)
204 wxCHECK_MSG( pixelSize.GetWidth() >= 0 && pixelSize.GetHeight() > 0, false,
205 "Negative values for the pixel size or zero pixel height are not allowed" );
207 if (wx_pango_version_check(1,8,0) != NULL ||
208 pixelSize.GetWidth() != 0)
210 // NOTE: pango_font_description_set_absolute_size() only sets the font height;
211 // if the user set the pixel width of the font explicitly or the pango
212 // library is too old, we cannot proceed
216 pango_font_description_set_absolute_size( m_nativeFontInfo.description,
217 pixelSize.GetHeight() * PANGO_SCALE );
223 void wxFontRefData::SetFamily(wxFontFamily family
)
225 m_nativeFontInfo
.SetFamily(family
);
228 void wxFontRefData::SetStyle(wxFontStyle style
)
230 m_nativeFontInfo
.SetStyle(style
);
233 void wxFontRefData::SetWeight(wxFontWeight weight
)
235 m_nativeFontInfo
.SetWeight(weight
);
238 void wxFontRefData::SetUnderlined(bool underlined
)
240 m_underlined
= underlined
;
242 // the Pango font descriptor does not have an underlined attribute
243 // (and wxNativeFontInfo::SetUnderlined asserts); rather it's
244 // wxWindowDCImpl::DoDrawText that handles underlined fonts, so we
245 // here we just need to save the underlined attribute
248 bool wxFontRefData::SetFaceName(const wxString
& facename
)
250 return m_nativeFontInfo
.SetFaceName(facename
);
253 void wxFontRefData::SetEncoding(wxFontEncoding
WXUNUSED(encoding
))
255 // with GTK+ 2 Pango always uses UTF8 internally, we cannot change it
258 void wxFontRefData::SetNativeFontInfo(const wxNativeFontInfo
& info
)
260 m_nativeFontInfo
= info
;
262 // set all the other font parameters from the native font info
266 // ----------------------------------------------------------------------------
268 // ----------------------------------------------------------------------------
270 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
272 wxFont::wxFont(const wxNativeFontInfo
& info
)
274 Create( info
.GetPointSize(),
278 info
.GetUnderlined(),
280 info
.GetEncoding() );
283 bool wxFont::Create( int pointSize
,
288 const wxString
& face
,
289 wxFontEncoding encoding
)
293 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
294 underlined
, face
, encoding
);
299 bool wxFont::Create(const wxString
& fontname
)
301 // VZ: does this really happen?
302 if ( fontname
.empty() )
304 *this = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
309 m_refData
= new wxFontRefData(fontname
);
318 // ----------------------------------------------------------------------------
320 // ----------------------------------------------------------------------------
322 int wxFont::GetPointSize() const
324 wxCHECK_MSG( IsOk(), 0, wxT("invalid font") );
326 return M_FONTDATA
->m_nativeFontInfo
.GetPointSize();
329 wxString
wxFont::GetFaceName() const
331 wxCHECK_MSG( IsOk(), wxEmptyString
, wxT("invalid font") );
333 return M_FONTDATA
->m_nativeFontInfo
.GetFaceName();
336 wxFontFamily
wxFont::GetFamily() const
338 wxCHECK_MSG( IsOk(), wxFONTFAMILY_MAX
, wxT("invalid font") );
340 return M_FONTDATA
->m_nativeFontInfo
.GetFamily();
343 wxFontStyle
wxFont::GetStyle() const
345 wxCHECK_MSG( IsOk(), wxFONTSTYLE_MAX
, wxT("invalid font") );
347 return M_FONTDATA
->m_nativeFontInfo
.GetStyle();
350 wxFontWeight
wxFont::GetWeight() const
352 wxCHECK_MSG( IsOk(), wxFONTWEIGHT_MAX
, wxT("invalid font") );
354 return M_FONTDATA
->m_nativeFontInfo
.GetWeight();
357 bool wxFont::GetUnderlined() const
359 wxCHECK_MSG( IsOk(), false, wxT("invalid font") );
361 return M_FONTDATA
->m_underlined
;
364 wxFontEncoding
wxFont::GetEncoding() const
366 wxCHECK_MSG( IsOk(), wxFONTENCODING_SYSTEM
, wxT("invalid font") );
368 return wxFONTENCODING_UTF8
;
369 // Pango always uses UTF8... see also SetEncoding()
372 const wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
374 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid font") );
376 return &(M_FONTDATA
->m_nativeFontInfo
);
379 bool wxFont::IsFixedWidth() const
381 wxCHECK_MSG( IsOk(), false, wxT("invalid font") );
383 return wxFontBase::IsFixedWidth();
386 // ----------------------------------------------------------------------------
387 // change font attributes
388 // ----------------------------------------------------------------------------
390 void wxFont::SetPointSize(int pointSize
)
394 M_FONTDATA
->SetPointSize(pointSize
);
397 void wxFont::SetFamily(wxFontFamily family
)
401 M_FONTDATA
->SetFamily(family
);
404 void wxFont::SetStyle(wxFontStyle style
)
408 M_FONTDATA
->SetStyle(style
);
411 void wxFont::SetWeight(wxFontWeight weight
)
415 M_FONTDATA
->SetWeight(weight
);
418 bool wxFont::SetFaceName(const wxString
& faceName
)
422 return M_FONTDATA
->SetFaceName(faceName
) &&
423 wxFontBase::SetFaceName(faceName
);
426 void wxFont::SetUnderlined(bool underlined
)
430 M_FONTDATA
->SetUnderlined(underlined
);
433 void wxFont::SetEncoding(wxFontEncoding encoding
)
437 M_FONTDATA
->SetEncoding(encoding
);
440 void wxFont::DoSetNativeFontInfo( const wxNativeFontInfo
& info
)
444 M_FONTDATA
->SetNativeFontInfo( info
);
447 wxGDIRefData
* wxFont::CreateGDIRefData() const
449 return new wxFontRefData
;
452 wxGDIRefData
* wxFont::CloneGDIRefData(const wxGDIRefData
* data
) const
454 return new wxFontRefData(*static_cast<const wxFontRefData
*>(data
));