1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/font.cpp
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 int family
= wxFONTFAMILY_DEFAULT
,
53 int style
= wxFONTSTYLE_NORMAL
,
54 int 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(int family
);
78 void SetStyle(int style
);
79 void SetWeight(int 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();
113 wxFontEncoding m_encoding
;
114 bool m_noAA
; // No anti-aliasing
116 // The native font info, basicly an XFLD under GTK 1.2 and
117 // the pango font description under GTK 2.0.
118 wxNativeFontInfo m_nativeFontInfo
;
123 #define M_FONTDATA ((wxFontRefData*)m_refData)
125 // ----------------------------------------------------------------------------
127 // ----------------------------------------------------------------------------
129 void wxFontRefData::Init(int pointSize
,
134 const wxString
& faceName
,
135 wxFontEncoding encoding
)
137 m_family
= family
== wxFONTFAMILY_DEFAULT
? wxFONTFAMILY_SWISS
: family
;
139 m_faceName
= faceName
;
141 // we accept both wxDEFAULT and wxNORMAL here - should we?
142 m_style
= style
== wxDEFAULT
? wxFONTSTYLE_NORMAL
: style
;
143 m_weight
= weight
== wxDEFAULT
? wxFONTWEIGHT_NORMAL
: weight
;
145 // and here, do we really want to forbid creation of the font of the size
146 // 90 (the value of wxDEFAULT)??
147 m_pointSize
= pointSize
== wxDEFAULT
|| pointSize
== -1
148 ? wxDEFAULT_FONT_SIZE
151 m_underlined
= underlined
;
152 m_encoding
= encoding
;
153 if ( m_encoding
== wxFONTENCODING_DEFAULT
)
154 m_encoding
= wxFont::GetDefaultEncoding();
158 // Create native font info
159 m_nativeFontInfo
.description
= pango_font_description_new();
161 // And set its values
162 if (!m_faceName
.empty())
164 pango_font_description_set_family( m_nativeFontInfo
.description
,
165 wxGTK_CONV_SYS(m_faceName
) );
171 case wxFONTFAMILY_MODERN
:
172 case wxFONTFAMILY_TELETYPE
:
173 pango_font_description_set_family( m_nativeFontInfo
.description
, "monospace" );
175 case wxFONTFAMILY_ROMAN
:
176 pango_font_description_set_family( m_nativeFontInfo
.description
, "serif" );
178 case wxFONTFAMILY_SWISS
:
179 // SWISS = sans serif
181 pango_font_description_set_family( m_nativeFontInfo
.description
, "sans" );
187 SetPointSize( m_pointSize
);
188 SetWeight( m_weight
);
191 void wxFontRefData::InitFromNative()
196 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
199 m_faceName
= wxGTK_CONV_BACK_SYS(pango_font_description_get_family(desc
));
201 // Pango sometimes needs to have a size
202 int pango_size
= pango_font_description_get_size( desc
);
204 m_nativeFontInfo
.SetPointSize(12);
206 m_pointSize
= m_nativeFontInfo
.GetPointSize();
207 m_style
= m_nativeFontInfo
.GetStyle();
208 m_weight
= m_nativeFontInfo
.GetWeight();
210 if (m_faceName
== wxT("monospace"))
212 m_family
= wxFONTFAMILY_TELETYPE
;
214 else if (m_faceName
== wxT("sans"))
216 m_family
= wxFONTFAMILY_SWISS
;
218 else if (m_faceName
== wxT("serif"))
220 m_family
= wxFONTFAMILY_ROMAN
;
224 m_family
= wxFONTFAMILY_UNKNOWN
;
227 // Pango description are never underlined (?)
228 m_underlined
= false;
230 // always with GTK+ 2
231 m_encoding
= wxFONTENCODING_UTF8
;
234 wxFontRefData::wxFontRefData( const wxFontRefData
& data
)
237 m_pointSize
= data
.m_pointSize
;
238 m_family
= data
.m_family
;
239 m_style
= data
.m_style
;
240 m_weight
= data
.m_weight
;
242 m_underlined
= data
.m_underlined
;
244 m_faceName
= data
.m_faceName
;
245 m_encoding
= data
.m_encoding
;
247 m_noAA
= data
.m_noAA
;
249 // Forces a copy of the internal data. wxNativeFontInfo should probably
250 // have a copy ctor and assignment operator to fix this properly but that
251 // would break binary compatibility...
252 m_nativeFontInfo
.FromString(data
.m_nativeFontInfo
.ToString());
255 wxFontRefData::wxFontRefData(int size
, int family
, int style
,
256 int weight
, bool underlined
,
257 const wxString
& faceName
,
258 wxFontEncoding encoding
)
260 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
263 wxFontRefData::wxFontRefData(const wxString
& fontname
)
265 m_nativeFontInfo
.FromString( fontname
);
270 void wxFontRefData::ClearGdkFonts()
274 wxFontRefData::~wxFontRefData()
279 // ----------------------------------------------------------------------------
280 // wxFontRefData SetXXX()
281 // ----------------------------------------------------------------------------
283 void wxFontRefData::SetPointSize(int pointSize
)
285 m_pointSize
= pointSize
;
287 m_nativeFontInfo
.SetPointSize(pointSize
);
290 void wxFontRefData::SetFamily(int family
)
294 // TODO: what are we supposed to do with m_nativeFontInfo here?
297 void wxFontRefData::SetStyle(int style
)
301 m_nativeFontInfo
.SetStyle((wxFontStyle
)style
);
304 void wxFontRefData::SetWeight(int weight
)
308 m_nativeFontInfo
.SetWeight((wxFontWeight
)weight
);
311 void wxFontRefData::SetUnderlined(bool underlined
)
313 m_underlined
= underlined
;
315 // the XLFD doesn't have "underlined" field anyhow
318 bool wxFontRefData::SetFaceName(const wxString
& facename
)
320 m_faceName
= facename
;
322 return m_nativeFontInfo
.SetFaceName(facename
);
325 void wxFontRefData::SetEncoding(wxFontEncoding encoding
)
327 m_encoding
= encoding
;
330 void wxFontRefData::SetNativeFontInfo(const wxNativeFontInfo
& info
)
332 // previously cached fonts shouldn't be used
335 m_nativeFontInfo
= info
;
337 // set all the other font parameters from the native font info
341 // ----------------------------------------------------------------------------
343 // ----------------------------------------------------------------------------
345 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
347 wxFont::wxFont(const wxNativeFontInfo
& info
)
349 Create( info
.GetPointSize(),
353 info
.GetUnderlined(),
355 info
.GetEncoding() );
358 bool wxFont::Create( int pointSize
,
363 const wxString
& face
,
364 wxFontEncoding encoding
)
368 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
369 underlined
, face
, encoding
);
374 bool wxFont::Create(const wxString
& fontname
)
376 // VZ: does this really happen?
377 if ( fontname
.empty() )
379 *this = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
384 m_refData
= new wxFontRefData(fontname
);
393 // ----------------------------------------------------------------------------
395 // ----------------------------------------------------------------------------
397 int wxFont::GetPointSize() const
399 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
401 return M_FONTDATA
->HasNativeFont() ? M_FONTDATA
->m_nativeFontInfo
.GetPointSize()
402 : M_FONTDATA
->m_pointSize
;
405 wxString
wxFont::GetFaceName() const
407 wxCHECK_MSG( Ok(), wxEmptyString
, wxT("invalid font") );
409 return M_FONTDATA
->HasNativeFont() ? M_FONTDATA
->m_nativeFontInfo
.GetFaceName()
410 : M_FONTDATA
->m_faceName
;
413 int wxFont::GetFamily() const
415 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
417 int ret
= M_FONTDATA
->m_family
;
418 if (M_FONTDATA
->HasNativeFont())
419 // wxNativeFontInfo::GetFamily is expensive, must not call more than once
420 ret
= M_FONTDATA
->m_nativeFontInfo
.GetFamily();
422 if (ret
== wxFONTFAMILY_DEFAULT
)
423 ret
= M_FONTDATA
->m_family
;
428 int wxFont::GetStyle() const
430 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
432 return M_FONTDATA
->HasNativeFont() ? M_FONTDATA
->m_nativeFontInfo
.GetStyle()
433 : M_FONTDATA
->m_style
;
436 int wxFont::GetWeight() const
438 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
440 return M_FONTDATA
->HasNativeFont() ? M_FONTDATA
->m_nativeFontInfo
.GetWeight()
441 : M_FONTDATA
->m_weight
;
444 bool wxFont::GetUnderlined() const
446 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
448 return M_FONTDATA
->m_underlined
;
451 wxFontEncoding
wxFont::GetEncoding() const
453 wxCHECK_MSG( Ok(), wxFONTENCODING_SYSTEM
, wxT("invalid font") );
455 return M_FONTDATA
->m_encoding
;
458 bool wxFont::GetNoAntiAliasing() const
460 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
462 return M_FONTDATA
->m_noAA
;
465 const wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
467 wxCHECK_MSG( Ok(), (wxNativeFontInfo
*)NULL
, wxT("invalid font") );
469 return &(M_FONTDATA
->m_nativeFontInfo
);
472 bool wxFont::IsFixedWidth() const
474 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
476 return wxFontBase::IsFixedWidth();
479 // ----------------------------------------------------------------------------
480 // change font attributes
481 // ----------------------------------------------------------------------------
483 void wxFont::SetPointSize(int pointSize
)
487 M_FONTDATA
->SetPointSize(pointSize
);
490 void wxFont::SetFamily(int family
)
494 M_FONTDATA
->SetFamily(family
);
497 void wxFont::SetStyle(int style
)
501 M_FONTDATA
->SetStyle(style
);
504 void wxFont::SetWeight(int weight
)
508 M_FONTDATA
->SetWeight(weight
);
511 bool wxFont::SetFaceName(const wxString
& faceName
)
515 return M_FONTDATA
->SetFaceName(faceName
) &&
516 wxFontBase::SetFaceName(faceName
);
519 void wxFont::SetUnderlined(bool underlined
)
523 M_FONTDATA
->SetUnderlined(underlined
);
526 void wxFont::SetEncoding(wxFontEncoding encoding
)
530 M_FONTDATA
->SetEncoding(encoding
);
533 void wxFont::DoSetNativeFontInfo( const wxNativeFontInfo
& info
)
537 M_FONTDATA
->SetNativeFontInfo( info
);
540 void wxFont::SetNoAntiAliasing( bool no
)
544 M_FONTDATA
->SetNoAntiAliasing( no
);
547 wxGDIRefData
* wxFont::CreateGDIRefData() const
549 return new wxFontRefData
;
552 wxGDIRefData
* wxFont::CloneGDIRefData(const wxGDIRefData
* data
) const
554 return new wxFontRefData(*wx_static_cast(const wxFontRefData
*, data
));