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
;
160 if ( m_encoding
== wxFONTENCODING_DEFAULT
)
161 m_encoding
= wxFont::GetDefaultEncoding();
165 // Create native font info
166 m_nativeFontInfo
.description
= pango_font_description_new();
168 // And set its values
169 if (!m_faceName
.empty())
171 pango_font_description_set_family( m_nativeFontInfo
.description
,
172 wxGTK_CONV_SYS(m_faceName
) );
178 case wxFONTFAMILY_MODERN
:
179 case wxFONTFAMILY_TELETYPE
:
180 pango_font_description_set_family( m_nativeFontInfo
.description
, "monospace" );
182 case wxFONTFAMILY_ROMAN
:
183 pango_font_description_set_family( m_nativeFontInfo
.description
, "serif" );
185 case wxFONTFAMILY_SWISS
:
186 // SWISS = sans serif
188 pango_font_description_set_family( m_nativeFontInfo
.description
, "sans" );
194 SetPointSize( m_pointSize
);
195 SetWeight( m_weight
);
198 void wxFontRefData::InitFromNative()
203 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
206 m_faceName
= wxGTK_CONV_BACK_SYS(pango_font_description_get_family(desc
));
208 // Pango sometimes needs to have a size
209 int pango_size
= pango_font_description_get_size( desc
);
211 m_nativeFontInfo
.SetPointSize(12);
213 m_pointSize
= m_nativeFontInfo
.GetPointSize();
214 m_style
= m_nativeFontInfo
.GetStyle();
215 m_weight
= m_nativeFontInfo
.GetWeight();
217 if (m_faceName
== wxT("monospace"))
219 m_family
= wxFONTFAMILY_TELETYPE
;
221 else if (m_faceName
== wxT("sans"))
223 m_family
= wxFONTFAMILY_SWISS
;
225 else if (m_faceName
== wxT("serif"))
227 m_family
= wxFONTFAMILY_ROMAN
;
231 m_family
= wxFONTFAMILY_UNKNOWN
;
234 // Pango description are never underlined (?)
235 m_underlined
= false;
237 // always with GTK+ 2
238 m_encoding
= wxFONTENCODING_UTF8
;
241 wxFontRefData::wxFontRefData( const wxFontRefData
& data
)
244 m_pointSize
= data
.m_pointSize
;
245 m_family
= data
.m_family
;
246 m_style
= data
.m_style
;
247 m_weight
= data
.m_weight
;
249 m_underlined
= data
.m_underlined
;
251 m_faceName
= data
.m_faceName
;
252 m_encoding
= data
.m_encoding
;
254 m_noAA
= data
.m_noAA
;
256 // Forces a copy of the internal data. wxNativeFontInfo should probably
257 // have a copy ctor and assignment operator to fix this properly but that
258 // would break binary compatibility...
259 m_nativeFontInfo
.FromString(data
.m_nativeFontInfo
.ToString());
262 wxFontRefData::wxFontRefData(int size
, int family
, int style
,
263 int weight
, bool underlined
,
264 const wxString
& faceName
,
265 wxFontEncoding encoding
)
267 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
270 wxFontRefData::wxFontRefData(const wxString
& fontname
)
272 m_nativeFontInfo
.FromString( fontname
);
277 void wxFontRefData::ClearGdkFonts()
281 wxFontRefData::~wxFontRefData()
286 // ----------------------------------------------------------------------------
287 // wxFontRefData SetXXX()
288 // ----------------------------------------------------------------------------
290 void wxFontRefData::SetPointSize(int pointSize
)
292 m_pointSize
= pointSize
;
294 m_nativeFontInfo
.SetPointSize(pointSize
);
297 void wxFontRefData::SetFamily(int family
)
301 // TODO: what are we supposed to do with m_nativeFontInfo here?
304 void wxFontRefData::SetStyle(int style
)
308 m_nativeFontInfo
.SetStyle((wxFontStyle
)style
);
311 void wxFontRefData::SetWeight(int weight
)
315 m_nativeFontInfo
.SetWeight((wxFontWeight
)weight
);
318 void wxFontRefData::SetUnderlined(bool underlined
)
320 m_underlined
= underlined
;
322 // the XLFD doesn't have "underlined" field anyhow
325 bool wxFontRefData::SetFaceName(const wxString
& facename
)
327 m_faceName
= facename
;
329 return m_nativeFontInfo
.SetFaceName(facename
);
332 void wxFontRefData::SetEncoding(wxFontEncoding encoding
)
334 m_encoding
= encoding
;
337 void wxFontRefData::SetNativeFontInfo(const wxNativeFontInfo
& info
)
339 // previously cached fonts shouldn't be used
342 m_nativeFontInfo
= info
;
344 // set all the other font parameters from the native font info
348 // ----------------------------------------------------------------------------
350 // ----------------------------------------------------------------------------
352 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
354 wxFont::wxFont(const wxNativeFontInfo
& info
)
356 Create( info
.GetPointSize(),
360 info
.GetUnderlined(),
362 info
.GetEncoding() );
365 bool wxFont::Create( int pointSize
,
370 const wxString
& face
,
371 wxFontEncoding encoding
)
375 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
376 underlined
, face
, encoding
);
381 bool wxFont::Create(const wxString
& fontname
)
383 // VZ: does this really happen?
384 if ( fontname
.empty() )
386 *this = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
391 m_refData
= new wxFontRefData(fontname
);
400 // ----------------------------------------------------------------------------
402 // ----------------------------------------------------------------------------
404 int wxFont::GetPointSize() const
406 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
408 return M_FONTDATA
->HasNativeFont() ? M_FONTDATA
->m_nativeFontInfo
.GetPointSize()
409 : M_FONTDATA
->m_pointSize
;
412 wxString
wxFont::GetFaceName() const
414 wxCHECK_MSG( Ok(), wxEmptyString
, wxT("invalid font") );
416 return M_FONTDATA
->HasNativeFont() ? M_FONTDATA
->m_nativeFontInfo
.GetFaceName()
417 : M_FONTDATA
->m_faceName
;
420 int wxFont::GetFamily() const
422 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
424 int ret
= M_FONTDATA
->m_family
;
425 if (M_FONTDATA
->HasNativeFont())
426 // wxNativeFontInfo::GetFamily is expensive, must not call more than once
427 ret
= M_FONTDATA
->m_nativeFontInfo
.GetFamily();
429 if (ret
== wxFONTFAMILY_DEFAULT
)
430 ret
= M_FONTDATA
->m_family
;
435 int wxFont::GetStyle() const
437 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
439 return M_FONTDATA
->HasNativeFont() ? M_FONTDATA
->m_nativeFontInfo
.GetStyle()
440 : M_FONTDATA
->m_style
;
443 int wxFont::GetWeight() const
445 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
447 return M_FONTDATA
->HasNativeFont() ? M_FONTDATA
->m_nativeFontInfo
.GetWeight()
448 : M_FONTDATA
->m_weight
;
451 bool wxFont::GetUnderlined() const
453 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
455 return M_FONTDATA
->m_underlined
;
458 wxFontEncoding
wxFont::GetEncoding() const
460 wxCHECK_MSG( Ok(), wxFONTENCODING_SYSTEM
, wxT("invalid font") );
462 return M_FONTDATA
->m_encoding
;
465 bool wxFont::GetNoAntiAliasing() const
467 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
469 return M_FONTDATA
->m_noAA
;
472 const wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
474 wxCHECK_MSG( Ok(), (wxNativeFontInfo
*)NULL
, wxT("invalid font") );
476 return &(M_FONTDATA
->m_nativeFontInfo
);
479 bool wxFont::IsFixedWidth() const
481 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
483 return wxFontBase::IsFixedWidth();
486 // ----------------------------------------------------------------------------
487 // change font attributes
488 // ----------------------------------------------------------------------------
490 void wxFont::SetPointSize(int pointSize
)
494 M_FONTDATA
->SetPointSize(pointSize
);
497 void wxFont::SetFamily(int family
)
501 M_FONTDATA
->SetFamily(family
);
504 void wxFont::SetStyle(int style
)
508 M_FONTDATA
->SetStyle(style
);
511 void wxFont::SetWeight(int weight
)
515 M_FONTDATA
->SetWeight(weight
);
518 bool wxFont::SetFaceName(const wxString
& faceName
)
522 return M_FONTDATA
->SetFaceName(faceName
) &&
523 wxFontBase::SetFaceName(faceName
);
526 void wxFont::SetUnderlined(bool underlined
)
530 M_FONTDATA
->SetUnderlined(underlined
);
533 void wxFont::SetEncoding(wxFontEncoding encoding
)
537 M_FONTDATA
->SetEncoding(encoding
);
540 void wxFont::DoSetNativeFontInfo( const wxNativeFontInfo
& info
)
544 M_FONTDATA
->SetNativeFontInfo( info
);
547 void wxFont::SetNoAntiAliasing( bool no
)
551 M_FONTDATA
->SetNoAntiAliasing( no
);
554 wxObjectRefData
* wxFont::CreateRefData() const
556 return new wxFontRefData
;
559 wxObjectRefData
* wxFont::CloneRefData(const wxObjectRefData
* data
) const
561 return new wxFontRefData(*wx_static_cast(const wxFontRefData
*, data
));