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"
29 #include "wx/fontutil.h"
30 #include "wx/cmndata.h"
31 #include "wx/gdicmn.h"
32 #include "wx/tokenzr.h"
36 #include "wx/gtk/private.h"
37 #include <gdk/gdkprivate.h>
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 // the default size (in points) for the fonts
44 static const int wxDEFAULT_FONT_SIZE
= 12;
46 // ----------------------------------------------------------------------------
47 // wxScaledFontList: maps the font sizes to the GDK fonts for the given font
48 // ----------------------------------------------------------------------------
50 WX_DECLARE_HASH_MAP(int, GdkFont
*, wxIntegerHash
, wxIntegerEqual
,
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 class wxFontRefData
: public wxObjectRefData
60 // from broken down font parameters, also default ctor
61 wxFontRefData(int size
= -1,
62 int family
= wxFONTFAMILY_DEFAULT
,
63 int style
= wxFONTSTYLE_NORMAL
,
64 int weight
= wxFONTWEIGHT_NORMAL
,
65 bool underlined
= false,
66 const wxString
& faceName
= wxEmptyString
,
67 wxFontEncoding encoding
= wxFONTENCODING_DEFAULT
);
70 wxFontRefData(const wxString
& fontname
);
73 wxFontRefData( const wxFontRefData
& data
);
75 virtual ~wxFontRefData();
77 // do we have the native font info?
78 bool HasNativeFont() const
80 // we always have a Pango font description
84 // setters: all of them also take care to modify m_nativeFontInfo if we
85 // have it so as to not lose the information not carried by our fields
86 void SetPointSize(int pointSize
);
87 void SetFamily(int family
);
88 void SetStyle(int style
);
89 void SetWeight(int weight
);
90 void SetUnderlined(bool underlined
);
91 void SetFaceName(const wxString
& facename
);
92 void SetEncoding(wxFontEncoding encoding
);
94 void SetNoAntiAliasing( bool no
= true ) { m_noAA
= no
; }
95 bool GetNoAntiAliasing() const { return m_noAA
; }
97 // and this one also modifies all the other font data fields
98 void SetNativeFontInfo(const wxNativeFontInfo
& info
);
101 // common part of all ctors
102 void Init(int pointSize
,
107 const wxString
& faceName
,
108 wxFontEncoding encoding
);
110 // set all fields from (already initialized and valid) m_nativeFontInfo
111 void InitFromNative();
114 // clear m_scaled_xfonts if any
115 void ClearGdkFonts();
123 wxFontEncoding m_encoding
;
124 bool m_noAA
; // No anti-aliasing
126 // The native font info, basicly an XFLD under GTK 1.2 and
127 // the pango font description under GTK 2.0.
128 wxNativeFontInfo m_nativeFontInfo
;
133 // ----------------------------------------------------------------------------
135 // ----------------------------------------------------------------------------
137 void wxFontRefData::Init(int pointSize
,
142 const wxString
& faceName
,
143 wxFontEncoding encoding
)
145 m_family
= family
== wxFONTFAMILY_DEFAULT
? wxFONTFAMILY_SWISS
: family
;
147 m_faceName
= faceName
;
149 // we accept both wxDEFAULT and wxNORMAL here - should we?
150 m_style
= style
== wxDEFAULT
? wxFONTSTYLE_NORMAL
: style
;
151 m_weight
= weight
== wxDEFAULT
? wxFONTWEIGHT_NORMAL
: weight
;
153 // and here, do we really want to forbid creation of the font of the size
154 // 90 (the value of wxDEFAULT)??
155 m_pointSize
= pointSize
== wxDEFAULT
|| pointSize
== -1
156 ? wxDEFAULT_FONT_SIZE
159 m_underlined
= underlined
;
160 m_encoding
= encoding
;
164 // Create native font info
165 m_nativeFontInfo
.description
= pango_font_description_new();
167 // And set its values
168 if (!m_faceName
.empty())
170 pango_font_description_set_family( m_nativeFontInfo
.description
,
171 wxGTK_CONV_SYS(m_faceName
) );
177 case wxFONTFAMILY_MODERN
:
178 case wxFONTFAMILY_TELETYPE
:
179 pango_font_description_set_family( m_nativeFontInfo
.description
, "monospace" );
181 case wxFONTFAMILY_ROMAN
:
182 pango_font_description_set_family( m_nativeFontInfo
.description
, "serif" );
184 case wxFONTFAMILY_SWISS
:
185 // SWISS = sans serif
187 pango_font_description_set_family( m_nativeFontInfo
.description
, "sans" );
193 SetPointSize( m_pointSize
);
194 SetWeight( m_weight
);
197 void wxFontRefData::InitFromNative()
202 PangoFontDescription
*desc
= m_nativeFontInfo
.description
;
205 m_faceName
= wxGTK_CONV_BACK( pango_font_description_get_family( desc
) );
207 // Pango sometimes needs to have a size
208 int pango_size
= pango_font_description_get_size( desc
);
210 m_nativeFontInfo
.SetPointSize(12);
212 m_pointSize
= m_nativeFontInfo
.GetPointSize();
213 m_style
= m_nativeFontInfo
.GetStyle();
214 m_weight
= m_nativeFontInfo
.GetWeight();
216 if (m_faceName
== wxT("monospace"))
218 m_family
= wxFONTFAMILY_TELETYPE
;
220 else if (m_faceName
== wxT("sans"))
222 m_family
= wxFONTFAMILY_SWISS
;
224 else if (m_faceName
== wxT("serif"))
226 m_family
= wxFONTFAMILY_ROMAN
;
230 m_family
= wxFONTFAMILY_UNKNOWN
;
233 // Pango description are never underlined (?)
234 m_underlined
= false;
236 // always with GTK+ 2
237 m_encoding
= wxFONTENCODING_UTF8
;
240 wxFontRefData::wxFontRefData( const wxFontRefData
& data
)
243 m_pointSize
= data
.m_pointSize
;
244 m_family
= data
.m_family
;
245 m_style
= data
.m_style
;
246 m_weight
= data
.m_weight
;
248 m_underlined
= data
.m_underlined
;
250 m_faceName
= data
.m_faceName
;
251 m_encoding
= data
.m_encoding
;
253 m_noAA
= data
.m_noAA
;
255 // Forces a copy of the internal data. wxNativeFontInfo should probably
256 // have a copy ctor and assignment operator to fix this properly but that
257 // would break binary compatibility...
258 m_nativeFontInfo
.FromString(data
.m_nativeFontInfo
.ToString());
261 wxFontRefData::wxFontRefData(int size
, int family
, int style
,
262 int weight
, bool underlined
,
263 const wxString
& faceName
,
264 wxFontEncoding encoding
)
266 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
269 wxFontRefData::wxFontRefData(const wxString
& fontname
)
271 m_nativeFontInfo
.FromString( fontname
);
276 void wxFontRefData::ClearGdkFonts()
280 wxFontRefData::~wxFontRefData()
285 // ----------------------------------------------------------------------------
286 // wxFontRefData SetXXX()
287 // ----------------------------------------------------------------------------
289 void wxFontRefData::SetPointSize(int pointSize
)
291 m_pointSize
= pointSize
;
293 m_nativeFontInfo
.SetPointSize(pointSize
);
296 void wxFontRefData::SetFamily(int family
)
300 // TODO: what are we supposed to do with m_nativeFontInfo here?
303 void wxFontRefData::SetStyle(int style
)
307 m_nativeFontInfo
.SetStyle((wxFontStyle
)style
);
310 void wxFontRefData::SetWeight(int weight
)
314 m_nativeFontInfo
.SetWeight((wxFontWeight
)weight
);
317 void wxFontRefData::SetUnderlined(bool underlined
)
319 m_underlined
= underlined
;
321 // the XLFD doesn't have "underlined" field anyhow
324 void wxFontRefData::SetFaceName(const wxString
& facename
)
326 m_faceName
= facename
;
328 m_nativeFontInfo
.SetFaceName(facename
);
331 void wxFontRefData::SetEncoding(wxFontEncoding encoding
)
333 m_encoding
= encoding
;
336 void wxFontRefData::SetNativeFontInfo(const wxNativeFontInfo
& info
)
338 // previously cached fonts shouldn't be used
341 m_nativeFontInfo
= info
;
343 // set all the other font parameters from the native font info
347 // ----------------------------------------------------------------------------
349 // ----------------------------------------------------------------------------
351 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
353 wxFont::wxFont(const wxNativeFontInfo
& info
)
355 Create( info
.GetPointSize(),
359 info
.GetUnderlined(),
361 info
.GetEncoding() );
364 bool wxFont::Create( int pointSize
,
369 const wxString
& face
,
370 wxFontEncoding encoding
)
374 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
375 underlined
, face
, encoding
);
380 bool wxFont::Create(const wxString
& fontname
)
382 // VZ: does this really happen?
383 if ( fontname
.empty() )
385 *this = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
390 m_refData
= new wxFontRefData(fontname
);
395 void wxFont::Unshare()
399 m_refData
= new wxFontRefData();
403 wxFontRefData
* ref
= new wxFontRefData(*(wxFontRefData
*)m_refData
);
413 // ----------------------------------------------------------------------------
415 // ----------------------------------------------------------------------------
417 int wxFont::GetPointSize() const
419 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
422 return M_FONTDATA
->HasNativeFont() ? M_FONTDATA
->m_nativeFontInfo
.GetPointSize()
423 : M_FONTDATA
->m_pointSize
;
425 return M_FONTDATA
->m_pointSize
;
429 wxString
wxFont::GetFaceName() const
431 wxCHECK_MSG( Ok(), wxEmptyString
, wxT("invalid font") );
434 return M_FONTDATA
->HasNativeFont() ? M_FONTDATA
->m_nativeFontInfo
.GetFaceName()
435 : M_FONTDATA
->m_faceName
;
437 return M_FONTDATA
->m_faceName
;
441 int wxFont::GetFamily() const
443 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
446 int 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
;
456 return M_FONTDATA
->m_family
;
460 int wxFont::GetStyle() const
462 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
465 return M_FONTDATA
->HasNativeFont() ? M_FONTDATA
->m_nativeFontInfo
.GetStyle()
466 : M_FONTDATA
->m_style
;
468 return M_FONTDATA
->m_style
;
472 int wxFont::GetWeight() const
474 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
477 return M_FONTDATA
->HasNativeFont() ? M_FONTDATA
->m_nativeFontInfo
.GetWeight()
478 : M_FONTDATA
->m_weight
;
480 return M_FONTDATA
->m_weight
;
484 bool wxFont::GetUnderlined() const
486 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
488 return M_FONTDATA
->m_underlined
;
491 wxFontEncoding
wxFont::GetEncoding() const
493 wxCHECK_MSG( Ok(), wxFONTENCODING_SYSTEM
, wxT("invalid font") );
495 return M_FONTDATA
->m_encoding
;
498 bool wxFont::GetNoAntiAliasing() const
500 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
502 return M_FONTDATA
->m_noAA
;
505 const wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
507 wxCHECK_MSG( Ok(), (wxNativeFontInfo
*)NULL
, wxT("invalid font") );
509 return &(M_FONTDATA
->m_nativeFontInfo
);
512 bool wxFont::IsFixedWidth() const
514 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
516 return wxFontBase::IsFixedWidth();
519 // ----------------------------------------------------------------------------
520 // change font attributes
521 // ----------------------------------------------------------------------------
523 void wxFont::SetPointSize(int pointSize
)
527 M_FONTDATA
->SetPointSize(pointSize
);
530 void wxFont::SetFamily(int family
)
534 M_FONTDATA
->SetFamily(family
);
537 void wxFont::SetStyle(int style
)
541 M_FONTDATA
->SetStyle(style
);
544 void wxFont::SetWeight(int weight
)
548 M_FONTDATA
->SetWeight(weight
);
551 void wxFont::SetFaceName(const wxString
& faceName
)
555 M_FONTDATA
->SetFaceName(faceName
);
558 void wxFont::SetUnderlined(bool underlined
)
562 M_FONTDATA
->SetUnderlined(underlined
);
565 void wxFont::SetEncoding(wxFontEncoding encoding
)
569 M_FONTDATA
->SetEncoding(encoding
);
572 void wxFont::DoSetNativeFontInfo( const wxNativeFontInfo
& info
)
576 M_FONTDATA
->SetNativeFontInfo( info
);
579 void wxFont::SetNoAntiAliasing( bool no
)
583 M_FONTDATA
->SetNoAntiAliasing( no
);