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 // ----------------------------------------------------------------------------
44 // wxScaledFontList: maps the font sizes to the GDK fonts for the given font
45 // ----------------------------------------------------------------------------
47 WX_DECLARE_HASH_MAP(int, GdkFont
*, wxIntegerHash
, wxIntegerEqual
,
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 class wxFontRefData
: public wxObjectRefData
57 // from broken down font parameters, also default ctor
58 wxFontRefData(int size
= -1,
59 int family
= wxFONTFAMILY_DEFAULT
,
60 int style
= wxFONTSTYLE_NORMAL
,
61 int weight
= wxFONTWEIGHT_NORMAL
,
62 bool underlined
= false,
63 const wxString
& faceName
= wxEmptyString
,
64 wxFontEncoding encoding
= wxFONTENCODING_DEFAULT
);
67 wxFontRefData(const wxString
& fontname
);
70 wxFontRefData( const wxFontRefData
& data
);
72 virtual ~wxFontRefData();
74 // do we have the native font info?
75 bool HasNativeFont() const
77 // we always have a Pango font description
81 // setters: all of them also take care to modify m_nativeFontInfo if we
82 // have it so as to not lose the information not carried by our fields
83 void SetPointSize(int pointSize
);
84 void SetFamily(int family
);
85 void SetStyle(int style
);
86 void SetWeight(int weight
);
87 void SetUnderlined(bool underlined
);
88 bool SetFaceName(const wxString
& facename
);
89 void SetEncoding(wxFontEncoding encoding
);
91 void SetNoAntiAliasing( bool no
= true ) { m_noAA
= no
; }
92 bool GetNoAntiAliasing() const { return m_noAA
; }
94 // and this one also modifies all the other font data fields
95 void SetNativeFontInfo(const wxNativeFontInfo
& info
);
98 // common part of all ctors
99 void Init(int pointSize
,
104 const wxString
& faceName
,
105 wxFontEncoding encoding
);
107 // set all fields from (already initialized and valid) m_nativeFontInfo
108 void InitFromNative();
111 // clear m_scaled_xfonts if any
112 void ClearGdkFonts();
120 wxFontEncoding m_encoding
;
121 bool m_noAA
; // No anti-aliasing
123 // The native font info, basicly an XFLD under GTK 1.2 and
124 // the pango font description under GTK 2.0.
125 wxNativeFontInfo m_nativeFontInfo
;
130 #define M_FONTDATA ((wxFontRefData*)m_refData)
132 // ----------------------------------------------------------------------------
134 // ----------------------------------------------------------------------------
136 void wxFontRefData::Init(int pointSize
,
141 const wxString
& faceName
,
142 wxFontEncoding encoding
)
144 m_family
= family
== wxFONTFAMILY_DEFAULT
? wxFONTFAMILY_SWISS
: family
;
146 m_faceName
= faceName
;
148 // we accept both wxDEFAULT and wxNORMAL here - should we?
149 m_style
= style
== wxDEFAULT
? wxFONTSTYLE_NORMAL
: style
;
150 m_weight
= weight
== wxDEFAULT
? wxFONTWEIGHT_NORMAL
: weight
;
152 // and here, do we really want to forbid creation of the font of the size
153 // 90 (the value of wxDEFAULT)??
154 m_pointSize
= pointSize
== wxDEFAULT
|| pointSize
== -1
155 ? wxDEFAULT_FONT_SIZE
158 m_underlined
= underlined
;
159 m_encoding
= encoding
;
163 // Create native font info
164 m_nativeFontInfo
.description
= pango_font_description_new();
166 // And set its values
167 if (!m_faceName
.empty())
169 pango_font_description_set_family( m_nativeFontInfo
.description
,
170 wxGTK_CONV_SYS(m_faceName
) );
176 case wxFONTFAMILY_MODERN
:
177 case wxFONTFAMILY_TELETYPE
:
178 pango_font_description_set_family( m_nativeFontInfo
.description
, "monospace" );
180 case wxFONTFAMILY_ROMAN
:
181 pango_font_description_set_family( m_nativeFontInfo
.description
, "serif" );
183 case wxFONTFAMILY_SWISS
:
184 // SWISS = sans serif
186 pango_font_description_set_family( m_nativeFontInfo
.description
, "sans" );
192 SetPointSize( m_pointSize
);
193 SetWeight( m_weight
);
196 void wxFontRefData::InitFromNative()
201 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
204 m_faceName
= wxGTK_CONV_BACK_SYS(pango_font_description_get_family(desc
));
206 // Pango sometimes needs to have a size
207 int pango_size
= pango_font_description_get_size( desc
);
209 m_nativeFontInfo
.SetPointSize(12);
211 m_pointSize
= m_nativeFontInfo
.GetPointSize();
212 m_style
= m_nativeFontInfo
.GetStyle();
213 m_weight
= m_nativeFontInfo
.GetWeight();
215 if (m_faceName
== wxT("monospace"))
217 m_family
= wxFONTFAMILY_TELETYPE
;
219 else if (m_faceName
== wxT("sans"))
221 m_family
= wxFONTFAMILY_SWISS
;
223 else if (m_faceName
== wxT("serif"))
225 m_family
= wxFONTFAMILY_ROMAN
;
229 m_family
= wxFONTFAMILY_UNKNOWN
;
232 // Pango description are never underlined (?)
233 m_underlined
= false;
235 // always with GTK+ 2
236 m_encoding
= wxFONTENCODING_UTF8
;
239 wxFontRefData::wxFontRefData( const wxFontRefData
& data
)
242 m_pointSize
= data
.m_pointSize
;
243 m_family
= data
.m_family
;
244 m_style
= data
.m_style
;
245 m_weight
= data
.m_weight
;
247 m_underlined
= data
.m_underlined
;
249 m_faceName
= data
.m_faceName
;
250 m_encoding
= data
.m_encoding
;
252 m_noAA
= data
.m_noAA
;
254 // Forces a copy of the internal data. wxNativeFontInfo should probably
255 // have a copy ctor and assignment operator to fix this properly but that
256 // would break binary compatibility...
257 m_nativeFontInfo
.FromString(data
.m_nativeFontInfo
.ToString());
260 wxFontRefData::wxFontRefData(int size
, int family
, int style
,
261 int weight
, bool underlined
,
262 const wxString
& faceName
,
263 wxFontEncoding encoding
)
265 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
268 wxFontRefData::wxFontRefData(const wxString
& fontname
)
270 m_nativeFontInfo
.FromString( fontname
);
275 void wxFontRefData::ClearGdkFonts()
279 wxFontRefData::~wxFontRefData()
284 // ----------------------------------------------------------------------------
285 // wxFontRefData SetXXX()
286 // ----------------------------------------------------------------------------
288 void wxFontRefData::SetPointSize(int pointSize
)
290 m_pointSize
= pointSize
;
292 m_nativeFontInfo
.SetPointSize(pointSize
);
295 void wxFontRefData::SetFamily(int family
)
299 // TODO: what are we supposed to do with m_nativeFontInfo here?
302 void wxFontRefData::SetStyle(int style
)
306 m_nativeFontInfo
.SetStyle((wxFontStyle
)style
);
309 void wxFontRefData::SetWeight(int weight
)
313 m_nativeFontInfo
.SetWeight((wxFontWeight
)weight
);
316 void wxFontRefData::SetUnderlined(bool underlined
)
318 m_underlined
= underlined
;
320 // the XLFD doesn't have "underlined" field anyhow
323 bool wxFontRefData::SetFaceName(const wxString
& facename
)
325 m_faceName
= facename
;
327 return m_nativeFontInfo
.SetFaceName(facename
);
330 void wxFontRefData::SetEncoding(wxFontEncoding encoding
)
332 m_encoding
= encoding
;
335 void wxFontRefData::SetNativeFontInfo(const wxNativeFontInfo
& info
)
337 // previously cached fonts shouldn't be used
340 m_nativeFontInfo
= info
;
342 // set all the other font parameters from the native font info
346 // ----------------------------------------------------------------------------
348 // ----------------------------------------------------------------------------
350 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
352 wxFont::wxFont(const wxNativeFontInfo
& info
)
354 Create( info
.GetPointSize(),
358 info
.GetUnderlined(),
360 info
.GetEncoding() );
363 bool wxFont::Create( int pointSize
,
368 const wxString
& face
,
369 wxFontEncoding encoding
)
373 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
374 underlined
, face
, encoding
);
379 bool wxFont::Create(const wxString
& fontname
)
381 // VZ: does this really happen?
382 if ( fontname
.empty() )
384 *this = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
389 m_refData
= new wxFontRefData(fontname
);
398 // ----------------------------------------------------------------------------
400 // ----------------------------------------------------------------------------
402 int wxFont::GetPointSize() const
404 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
406 return M_FONTDATA
->HasNativeFont() ? M_FONTDATA
->m_nativeFontInfo
.GetPointSize()
407 : M_FONTDATA
->m_pointSize
;
410 wxString
wxFont::GetFaceName() const
412 wxCHECK_MSG( Ok(), wxEmptyString
, wxT("invalid font") );
414 return M_FONTDATA
->HasNativeFont() ? M_FONTDATA
->m_nativeFontInfo
.GetFaceName()
415 : M_FONTDATA
->m_faceName
;
418 int wxFont::GetFamily() const
420 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
422 int ret
= M_FONTDATA
->m_family
;
423 if (M_FONTDATA
->HasNativeFont())
424 // wxNativeFontInfo::GetFamily is expensive, must not call more than once
425 ret
= M_FONTDATA
->m_nativeFontInfo
.GetFamily();
427 if (ret
== wxFONTFAMILY_DEFAULT
)
428 ret
= M_FONTDATA
->m_family
;
433 int wxFont::GetStyle() const
435 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
437 return M_FONTDATA
->HasNativeFont() ? M_FONTDATA
->m_nativeFontInfo
.GetStyle()
438 : M_FONTDATA
->m_style
;
441 int wxFont::GetWeight() const
443 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
445 return M_FONTDATA
->HasNativeFont() ? M_FONTDATA
->m_nativeFontInfo
.GetWeight()
446 : M_FONTDATA
->m_weight
;
449 bool wxFont::GetUnderlined() const
451 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
453 return M_FONTDATA
->m_underlined
;
456 wxFontEncoding
wxFont::GetEncoding() const
458 wxCHECK_MSG( Ok(), wxFONTENCODING_SYSTEM
, wxT("invalid font") );
460 return M_FONTDATA
->m_encoding
;
463 bool wxFont::GetNoAntiAliasing() const
465 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
467 return M_FONTDATA
->m_noAA
;
470 const wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
472 wxCHECK_MSG( Ok(), (wxNativeFontInfo
*)NULL
, wxT("invalid font") );
474 return &(M_FONTDATA
->m_nativeFontInfo
);
477 bool wxFont::IsFixedWidth() const
479 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
481 return wxFontBase::IsFixedWidth();
484 // ----------------------------------------------------------------------------
485 // change font attributes
486 // ----------------------------------------------------------------------------
488 void wxFont::SetPointSize(int pointSize
)
492 M_FONTDATA
->SetPointSize(pointSize
);
495 void wxFont::SetFamily(int family
)
499 M_FONTDATA
->SetFamily(family
);
502 void wxFont::SetStyle(int style
)
506 M_FONTDATA
->SetStyle(style
);
509 void wxFont::SetWeight(int weight
)
513 M_FONTDATA
->SetWeight(weight
);
516 bool wxFont::SetFaceName(const wxString
& faceName
)
520 return M_FONTDATA
->SetFaceName(faceName
) &&
521 wxFontBase::SetFaceName(faceName
);
524 void wxFont::SetUnderlined(bool underlined
)
528 M_FONTDATA
->SetUnderlined(underlined
);
531 void wxFont::SetEncoding(wxFontEncoding encoding
)
535 M_FONTDATA
->SetEncoding(encoding
);
538 void wxFont::DoSetNativeFontInfo( const wxNativeFontInfo
& info
)
542 M_FONTDATA
->SetNativeFontInfo( info
);
545 void wxFont::SetNoAntiAliasing( bool no
)
549 M_FONTDATA
->SetNoAntiAliasing( no
);
552 wxObjectRefData
* wxFont::CreateRefData() const
554 return new wxFontRefData
;
557 wxObjectRefData
* wxFont::CloneRefData(const wxObjectRefData
* data
) const
559 return new wxFontRefData(*wx_static_cast(const wxFontRefData
*, data
));