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 // do we have the native font info?
68 bool HasNativeFont() const
70 // we always have a Pango font description
74 // setters: all of them also take care to modify m_nativeFontInfo if we
75 // have it so as to not lose the information not carried by our fields
76 void SetPointSize(int pointSize
);
77 void SetFamily(wxFontFamily family
);
78 void SetStyle(wxFontStyle style
);
79 void SetWeight(wxFontWeight weight
);
80 void SetUnderlined(bool underlined
);
81 bool SetFaceName(const wxString
& facename
);
82 void SetEncoding(wxFontEncoding encoding
);
84 void SetNoAntiAliasing( bool no
= true ) { m_noAA
= no
; }
85 bool GetNoAntiAliasing() const { return m_noAA
; }
87 // and this one also modifies all the other font data fields
88 void SetNativeFontInfo(const wxNativeFontInfo
& info
);
91 // common part of all ctors
92 void Init(int pointSize
,
97 const wxString
& faceName
,
98 wxFontEncoding encoding
);
100 // set all fields from (already initialized and valid) m_nativeFontInfo
101 void InitFromNative();
104 // clear m_scaled_xfonts if any
105 void ClearGdkFonts();
108 wxFontFamily m_family
;
110 wxFontWeight m_weight
;
113 wxFontEncoding m_encoding
;
114 bool m_noAA
; // No anti-aliasing
116 // The native font info: basically a PangoFontDescription
117 wxNativeFontInfo m_nativeFontInfo
;
122 #define M_FONTDATA ((wxFontRefData*)m_refData)
124 // ----------------------------------------------------------------------------
126 // ----------------------------------------------------------------------------
128 void wxFontRefData::Init(int pointSize
,
133 const wxString
& faceName
,
134 wxFontEncoding encoding
)
136 m_family
= family
== wxFONTFAMILY_DEFAULT
? wxFONTFAMILY_SWISS
: family
;
138 m_faceName
= faceName
;
140 // we accept both wxDEFAULT and wxNORMAL here - should we?
141 m_style
= style
== wxDEFAULT
? wxFONTSTYLE_NORMAL
: style
;
142 m_weight
= weight
== wxDEFAULT
? wxFONTWEIGHT_NORMAL
: weight
;
144 // and here, do we really want to forbid creation of the font of the size
145 // 90 (the value of wxDEFAULT)??
146 m_pointSize
= pointSize
== wxDEFAULT
|| pointSize
== -1
147 ? wxDEFAULT_FONT_SIZE
150 m_underlined
= underlined
;
151 m_encoding
= encoding
;
152 if ( m_encoding
== wxFONTENCODING_DEFAULT
)
153 m_encoding
= wxFont::GetDefaultEncoding();
157 // Create native font info
158 m_nativeFontInfo
.description
= pango_font_description_new();
160 // And set its values
161 if (!m_faceName
.empty())
163 pango_font_description_set_family( m_nativeFontInfo
.description
,
164 wxGTK_CONV_SYS(m_faceName
) );
170 case wxFONTFAMILY_MODERN
:
171 case wxFONTFAMILY_TELETYPE
:
172 pango_font_description_set_family( m_nativeFontInfo
.description
, "monospace" );
174 case wxFONTFAMILY_ROMAN
:
175 pango_font_description_set_family( m_nativeFontInfo
.description
, "serif" );
177 case wxFONTFAMILY_SWISS
:
178 // SWISS = sans serif
180 pango_font_description_set_family( m_nativeFontInfo
.description
, "sans" );
186 SetPointSize( m_pointSize
);
187 SetWeight( m_weight
);
190 void wxFontRefData::InitFromNative()
195 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
198 m_faceName
= wxGTK_CONV_BACK_SYS(pango_font_description_get_family(desc
));
200 // Pango sometimes needs to have a size
201 int pango_size
= pango_font_description_get_size( desc
);
203 m_nativeFontInfo
.SetPointSize(12);
205 m_pointSize
= m_nativeFontInfo
.GetPointSize();
206 m_style
= m_nativeFontInfo
.GetStyle();
207 m_weight
= m_nativeFontInfo
.GetWeight();
209 if (m_faceName
== wxT("monospace"))
211 m_family
= wxFONTFAMILY_TELETYPE
;
213 else if (m_faceName
== wxT("sans"))
215 m_family
= wxFONTFAMILY_SWISS
;
217 else if (m_faceName
== wxT("serif"))
219 m_family
= wxFONTFAMILY_ROMAN
;
223 m_family
= wxFONTFAMILY_UNKNOWN
;
226 // Pango description are never underlined (?)
227 m_underlined
= false;
229 // always with GTK+ 2
230 m_encoding
= wxFONTENCODING_UTF8
;
233 wxFontRefData::wxFontRefData( const wxFontRefData
& data
)
236 m_pointSize
= data
.m_pointSize
;
237 m_family
= data
.m_family
;
238 m_style
= data
.m_style
;
239 m_weight
= data
.m_weight
;
241 m_underlined
= data
.m_underlined
;
243 m_faceName
= data
.m_faceName
;
244 m_encoding
= data
.m_encoding
;
246 m_noAA
= data
.m_noAA
;
248 // Forces a copy of the internal data. wxNativeFontInfo should probably
249 // have a copy ctor and assignment operator to fix this properly but that
250 // would break binary compatibility...
251 m_nativeFontInfo
.FromString(data
.m_nativeFontInfo
.ToString());
254 wxFontRefData::wxFontRefData(int size
, wxFontFamily family
, wxFontStyle style
,
255 wxFontWeight weight
, bool underlined
,
256 const wxString
& faceName
,
257 wxFontEncoding encoding
)
259 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
262 wxFontRefData::wxFontRefData(const wxString
& fontname
)
264 m_nativeFontInfo
.FromString( fontname
);
269 void wxFontRefData::ClearGdkFonts()
273 wxFontRefData::~wxFontRefData()
278 // ----------------------------------------------------------------------------
279 // wxFontRefData SetXXX()
280 // ----------------------------------------------------------------------------
282 void wxFontRefData::SetPointSize(int pointSize
)
284 m_pointSize
= pointSize
;
286 m_nativeFontInfo
.SetPointSize(pointSize
);
290 NOTE: disabled because pango_font_description_set_absolute_size() and
291 wxDC::GetCharHeight() do not mix well: setting with the former a pixel
292 size of "30" makes the latter return 36...
293 Besides, we need to return GetPointSize() a point size value even if
294 SetPixelSize() was used and this would require further changes
295 (and use of pango_font_description_get_size_is_absolute in some places).
297 bool wxFontRefData::SetPixelSize(const wxSize& pixelSize)
299 wxCHECK_MSG( pixelSize.GetWidth() >= 0 && pixelSize.GetHeight() > 0, false,
300 "Negative values for the pixel size or zero pixel height are not allowed" );
302 if (wx_pango_version_check(1,8,0) != NULL ||
303 pixelSize.GetWidth() != 0)
305 // NOTE: pango_font_description_set_absolute_size() only sets the font height;
306 // if the user set the pixel width of the font explicitly or the pango
307 // library is too old, we cannot proceed
311 pango_font_description_set_absolute_size( m_nativeFontInfo.description,
312 pixelSize.GetHeight() * PANGO_SCALE );
319 void wxFontRefData::SetFamily(wxFontFamily family
)
323 // TODO: what are we supposed to do with m_nativeFontInfo here?
326 void wxFontRefData::SetStyle(wxFontStyle style
)
330 m_nativeFontInfo
.SetStyle((wxFontStyle
)style
);
333 void wxFontRefData::SetWeight(wxFontWeight weight
)
337 m_nativeFontInfo
.SetWeight((wxFontWeight
)weight
);
340 void wxFontRefData::SetUnderlined(bool underlined
)
342 m_underlined
= underlined
;
344 // the XLFD doesn't have "underlined" field anyhow
347 bool wxFontRefData::SetFaceName(const wxString
& facename
)
349 m_faceName
= facename
;
351 return m_nativeFontInfo
.SetFaceName(facename
);
354 void wxFontRefData::SetEncoding(wxFontEncoding encoding
)
356 m_encoding
= encoding
;
359 void wxFontRefData::SetNativeFontInfo(const wxNativeFontInfo
& info
)
361 // previously cached fonts shouldn't be used
364 m_nativeFontInfo
= info
;
366 // set all the other font parameters from the native font info
370 // ----------------------------------------------------------------------------
372 // ----------------------------------------------------------------------------
374 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
376 wxFont::wxFont(const wxNativeFontInfo
& info
)
378 Create( info
.GetPointSize(),
382 info
.GetUnderlined(),
384 info
.GetEncoding() );
387 bool wxFont::Create( int pointSize
,
392 const wxString
& face
,
393 wxFontEncoding encoding
)
397 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
398 underlined
, face
, encoding
);
403 bool wxFont::Create(const wxString
& fontname
)
405 // VZ: does this really happen?
406 if ( fontname
.empty() )
408 *this = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
413 m_refData
= new wxFontRefData(fontname
);
422 // ----------------------------------------------------------------------------
424 // ----------------------------------------------------------------------------
426 int wxFont::GetPointSize() const
428 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
430 return M_FONTDATA
->HasNativeFont() ? M_FONTDATA
->m_nativeFontInfo
.GetPointSize()
431 : M_FONTDATA
->m_pointSize
;
434 wxString
wxFont::GetFaceName() const
436 wxCHECK_MSG( Ok(), wxEmptyString
, wxT("invalid font") );
438 return M_FONTDATA
->HasNativeFont() ? M_FONTDATA
->m_nativeFontInfo
.GetFaceName()
439 : M_FONTDATA
->m_faceName
;
442 wxFontFamily
wxFont::GetFamily() const
444 wxCHECK_MSG( Ok(), wxFONTFAMILY_MAX
, wxT("invalid font") );
446 wxFontFamily ret
= M_FONTDATA
->m_family
;
447 if (M_FONTDATA
->HasNativeFont())
448 // wxNativeFontInfo::GetFamily is expensive, must not call more than once
449 ret
= M_FONTDATA
->m_nativeFontInfo
.GetFamily();
451 if (ret
== wxFONTFAMILY_DEFAULT
)
452 ret
= M_FONTDATA
->m_family
;
457 wxFontStyle
wxFont::GetStyle() const
459 wxCHECK_MSG( Ok(), wxFONTSTYLE_MAX
, wxT("invalid font") );
461 return M_FONTDATA
->HasNativeFont() ? M_FONTDATA
->m_nativeFontInfo
.GetStyle()
462 : M_FONTDATA
->m_style
;
465 wxFontWeight
wxFont::GetWeight() const
467 wxCHECK_MSG( Ok(), wxFONTWEIGHT_MAX
, wxT("invalid font") );
469 return M_FONTDATA
->HasNativeFont() ? M_FONTDATA
->m_nativeFontInfo
.GetWeight()
470 : M_FONTDATA
->m_weight
;
473 bool wxFont::GetUnderlined() const
475 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
477 return M_FONTDATA
->m_underlined
;
480 wxFontEncoding
wxFont::GetEncoding() const
482 wxCHECK_MSG( Ok(), wxFONTENCODING_SYSTEM
, wxT("invalid font") );
484 return M_FONTDATA
->m_encoding
;
487 bool wxFont::GetNoAntiAliasing() const
489 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
491 return M_FONTDATA
->m_noAA
;
494 const wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
496 wxCHECK_MSG( Ok(), NULL
, wxT("invalid font") );
498 return &(M_FONTDATA
->m_nativeFontInfo
);
501 bool wxFont::IsFixedWidth() const
503 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
505 return wxFontBase::IsFixedWidth();
508 // ----------------------------------------------------------------------------
509 // change font attributes
510 // ----------------------------------------------------------------------------
512 void wxFont::SetPointSize(int pointSize
)
516 M_FONTDATA
->SetPointSize(pointSize
);
519 void wxFont::SetFamily(wxFontFamily family
)
523 M_FONTDATA
->SetFamily(family
);
526 void wxFont::SetStyle(wxFontStyle style
)
530 M_FONTDATA
->SetStyle(style
);
533 void wxFont::SetWeight(wxFontWeight weight
)
537 M_FONTDATA
->SetWeight(weight
);
540 bool wxFont::SetFaceName(const wxString
& faceName
)
544 return M_FONTDATA
->SetFaceName(faceName
) &&
545 wxFontBase::SetFaceName(faceName
);
548 void wxFont::SetUnderlined(bool underlined
)
552 M_FONTDATA
->SetUnderlined(underlined
);
555 void wxFont::SetEncoding(wxFontEncoding encoding
)
559 M_FONTDATA
->SetEncoding(encoding
);
562 void wxFont::DoSetNativeFontInfo( const wxNativeFontInfo
& info
)
566 M_FONTDATA
->SetNativeFontInfo( info
);
569 void wxFont::SetNoAntiAliasing( bool no
)
573 M_FONTDATA
->SetNoAntiAliasing( no
);
576 wxGDIRefData
* wxFont::CreateGDIRefData() const
578 return new wxFontRefData
;
581 wxGDIRefData
* wxFont::CloneGDIRefData(const wxGDIRefData
* data
) const
583 return new wxFontRefData(*static_cast<const wxFontRefData
*>(data
));