]>
Commit | Line | Data |
---|---|---|
c801d85f | 1 | ///////////////////////////////////////////////////////////////////////////// |
e4db172a | 2 | // Name: src/gtk/font.cpp |
b5791cc7 | 3 | // Purpose: wxFont for wxGTK |
c801d85f | 4 | // Author: Robert Roebling |
a81258be | 5 | // Id: $Id$ |
6c9a19aa | 6 | // Copyright: (c) 1998 Robert Roebling and Julian Smart |
65571936 | 7 | // Licence: wxWindows licence |
c801d85f KB |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
0c5d3e1c VZ |
10 | // ============================================================================ |
11 | // declarations | |
12 | // ============================================================================ | |
13 | ||
14 | // ---------------------------------------------------------------------------- | |
15 | // headers | |
16 | // ---------------------------------------------------------------------------- | |
17 | ||
14f355c2 VS |
18 | // For compilers that support precompilation, includes "wx.h". |
19 | #include "wx/wxprec.h" | |
20 | ||
c801d85f | 21 | #include "wx/font.h" |
e4db172a WS |
22 | |
23 | #ifndef WX_PRECOMP | |
24 | #include "wx/log.h" | |
de6185e2 | 25 | #include "wx/utils.h" |
9eddec69 | 26 | #include "wx/settings.h" |
ce5d92e1 | 27 | #include "wx/cmndata.h" |
dd05139a | 28 | #include "wx/gdicmn.h" |
e4db172a WS |
29 | #endif |
30 | ||
7beba2fc | 31 | #include "wx/fontutil.h" |
8636aed8 | 32 | #include "wx/tokenzr.h" |
0c5d3e1c | 33 | |
9e691f46 | 34 | #include "wx/gtk/private.h" |
83624f79 | 35 | |
409d5a58 VZ |
36 | // ---------------------------------------------------------------------------- |
37 | // constants | |
38 | // ---------------------------------------------------------------------------- | |
39 | ||
40 | // the default size (in points) for the fonts | |
41 | static const int wxDEFAULT_FONT_SIZE = 12; | |
42 | ||
0c5d3e1c VZ |
43 | // ---------------------------------------------------------------------------- |
44 | // wxFontRefData | |
45 | // ---------------------------------------------------------------------------- | |
46 | ||
8f884a0d | 47 | class wxFontRefData : public wxGDIRefData |
c801d85f | 48 | { |
8bbe427f | 49 | public: |
409d5a58 VZ |
50 | // from broken down font parameters, also default ctor |
51 | wxFontRefData(int size = -1, | |
0c14b6c3 FM |
52 | wxFontFamily family = wxFONTFAMILY_DEFAULT, |
53 | wxFontStyle style = wxFONTSTYLE_NORMAL, | |
54 | wxFontWeight weight = wxFONTWEIGHT_NORMAL, | |
de6185e2 | 55 | bool underlined = false, |
0c5d3e1c | 56 | const wxString& faceName = wxEmptyString, |
7826e2dd | 57 | wxFontEncoding encoding = wxFONTENCODING_DEFAULT); |
409d5a58 | 58 | |
34be948f | 59 | wxFontRefData(const wxString& nativeFontInfoString); |
409d5a58 VZ |
60 | |
61 | // copy ctor | |
358fc25c | 62 | wxFontRefData( const wxFontRefData& data ); |
409d5a58 | 63 | |
0c5d3e1c VZ |
64 | virtual ~wxFontRefData(); |
65 | ||
409d5a58 VZ |
66 | // setters: all of them also take care to modify m_nativeFontInfo if we |
67 | // have it so as to not lose the information not carried by our fields | |
68 | void SetPointSize(int pointSize); | |
0c14b6c3 FM |
69 | void SetFamily(wxFontFamily family); |
70 | void SetStyle(wxFontStyle style); | |
71 | void SetWeight(wxFontWeight weight); | |
409d5a58 | 72 | void SetUnderlined(bool underlined); |
85ab460e | 73 | bool SetFaceName(const wxString& facename); |
409d5a58 VZ |
74 | void SetEncoding(wxFontEncoding encoding); |
75 | ||
de6185e2 | 76 | void SetNoAntiAliasing( bool no = true ) { m_noAA = no; } |
5ac2e80c | 77 | bool GetNoAntiAliasing() const { return m_noAA; } |
cd9a673c | 78 | |
011ba5ed VZ |
79 | // and this one also modifies all the other font data fields |
80 | void SetNativeFontInfo(const wxNativeFontInfo& info); | |
81 | ||
0c5d3e1c VZ |
82 | protected: |
83 | // common part of all ctors | |
84 | void Init(int pointSize, | |
0c14b6c3 FM |
85 | wxFontFamily family, |
86 | wxFontStyle style, | |
87 | wxFontWeight weight, | |
0c5d3e1c VZ |
88 | bool underlined, |
89 | const wxString& faceName, | |
7826e2dd | 90 | wxFontEncoding encoding); |
0c5d3e1c | 91 | |
011ba5ed VZ |
92 | // set all fields from (already initialized and valid) m_nativeFontInfo |
93 | void InitFromNative(); | |
94 | ||
0c5d3e1c | 95 | private: |
ecde8361 | 96 | bool m_underlined; |
2b5f62a0 | 97 | bool m_noAA; // No anti-aliasing |
7826e2dd | 98 | |
b5791cc7 | 99 | // The native font info: basically a PangoFontDescription |
30764ab5 | 100 | wxNativeFontInfo m_nativeFontInfo; |
8bbe427f | 101 | |
f6bcfd97 | 102 | friend class wxFont; |
c801d85f KB |
103 | }; |
104 | ||
68c95704 | 105 | #define M_FONTDATA ((wxFontRefData*)m_refData) |
873fd4af | 106 | |
0c5d3e1c | 107 | // ---------------------------------------------------------------------------- |
cd9a673c | 108 | // wxFontRefData |
0c5d3e1c VZ |
109 | // ---------------------------------------------------------------------------- |
110 | ||
111 | void wxFontRefData::Init(int pointSize, | |
0c14b6c3 FM |
112 | wxFontFamily family, |
113 | wxFontStyle style, | |
114 | wxFontWeight weight, | |
0c5d3e1c VZ |
115 | bool underlined, |
116 | const wxString& faceName, | |
7ce58684 | 117 | wxFontEncoding WXUNUSED(encoding)) |
8bbe427f | 118 | { |
6aea1e4a FM |
119 | if (family == wxFONTFAMILY_DEFAULT) |
120 | family = wxFONTFAMILY_SWISS; | |
0c5d3e1c | 121 | |
0c5d3e1c | 122 | m_underlined = underlined; |
de6185e2 | 123 | m_noAA = false; |
011ba5ed | 124 | |
46eed000 RR |
125 | // Create native font info |
126 | m_nativeFontInfo.description = pango_font_description_new(); | |
127 | ||
011ba5ed | 128 | // And set its values |
ecde8361 | 129 | if (!faceName.empty()) |
2b5f62a0 | 130 | { |
7ce58684 | 131 | pango_font_description_set_family( m_nativeFontInfo.description, |
ecde8361 | 132 | wxGTK_CONV_SYS(faceName) ); |
2b5f62a0 VZ |
133 | } |
134 | else | |
135 | { | |
6aea1e4a | 136 | SetFamily(family); |
46eed000 | 137 | } |
cd9a673c | 138 | |
ecde8361 FM |
139 | SetStyle( style == wxDEFAULT ? wxFONTSTYLE_NORMAL : style ); |
140 | SetPointSize( (pointSize == wxDEFAULT || pointSize == -1) | |
141 | ? wxDEFAULT_FONT_SIZE | |
142 | : pointSize ); | |
143 | SetWeight( weight == wxDEFAULT ? wxFONTWEIGHT_NORMAL : weight ); | |
358fc25c RR |
144 | } |
145 | ||
011ba5ed | 146 | void wxFontRefData::InitFromNative() |
409d5a58 | 147 | { |
de6185e2 | 148 | m_noAA = false; |
2b5f62a0 | 149 | |
db16cab4 RR |
150 | // Get native info |
151 | PangoFontDescription *desc = m_nativeFontInfo.description; | |
011ba5ed | 152 | |
b6b579bd RR |
153 | // Pango sometimes needs to have a size |
154 | int pango_size = pango_font_description_get_size( desc ); | |
155 | if (pango_size == 0) | |
ecde8361 | 156 | m_nativeFontInfo.SetPointSize(wxDEFAULT_FONT_SIZE); |
0f6858b6 | 157 | |
ecde8361 | 158 | // Pango description are never underlined |
de6185e2 | 159 | m_underlined = false; |
409d5a58 VZ |
160 | } |
161 | ||
011ba5ed | 162 | wxFontRefData::wxFontRefData( const wxFontRefData& data ) |
8f884a0d | 163 | : wxGDIRefData() |
011ba5ed | 164 | { |
011ba5ed | 165 | m_underlined = data.m_underlined; |
2b5f62a0 | 166 | m_noAA = data.m_noAA; |
cd9a673c RD |
167 | |
168 | // Forces a copy of the internal data. wxNativeFontInfo should probably | |
169 | // have a copy ctor and assignment operator to fix this properly but that | |
170 | // would break binary compatibility... | |
171 | m_nativeFontInfo.FromString(data.m_nativeFontInfo.ToString()); | |
011ba5ed VZ |
172 | } |
173 | ||
0c14b6c3 FM |
174 | wxFontRefData::wxFontRefData(int size, wxFontFamily family, wxFontStyle style, |
175 | wxFontWeight weight, bool underlined, | |
011ba5ed VZ |
176 | const wxString& faceName, |
177 | wxFontEncoding encoding) | |
178 | { | |
179 | Init(size, family, style, weight, underlined, faceName, encoding); | |
180 | } | |
181 | ||
34be948f | 182 | wxFontRefData::wxFontRefData(const wxString& nativeFontInfoString) |
8bbe427f | 183 | { |
34be948f | 184 | m_nativeFontInfo.FromString( nativeFontInfoString ); |
011ba5ed VZ |
185 | |
186 | InitFromNative(); | |
187 | } | |
188 | ||
011ba5ed VZ |
189 | wxFontRefData::~wxFontRefData() |
190 | { | |
0c5d3e1c | 191 | } |
c801d85f | 192 | |
0c5d3e1c | 193 | // ---------------------------------------------------------------------------- |
409d5a58 | 194 | // wxFontRefData SetXXX() |
0c5d3e1c | 195 | // ---------------------------------------------------------------------------- |
c801d85f | 196 | |
409d5a58 | 197 | void wxFontRefData::SetPointSize(int pointSize) |
c801d85f | 198 | { |
8a15e8ba | 199 | m_nativeFontInfo.SetPointSize(pointSize); |
7826e2dd VZ |
200 | } |
201 | ||
b5791cc7 FM |
202 | /* |
203 | NOTE: disabled because pango_font_description_set_absolute_size() and | |
204 | wxDC::GetCharHeight() do not mix well: setting with the former a pixel | |
205 | size of "30" makes the latter return 36... | |
206 | Besides, we need to return GetPointSize() a point size value even if | |
207 | SetPixelSize() was used and this would require further changes | |
208 | (and use of pango_font_description_get_size_is_absolute in some places). | |
209 | ||
210 | bool wxFontRefData::SetPixelSize(const wxSize& pixelSize) | |
211 | { | |
212 | wxCHECK_MSG( pixelSize.GetWidth() >= 0 && pixelSize.GetHeight() > 0, false, | |
213 | "Negative values for the pixel size or zero pixel height are not allowed" ); | |
214 | ||
215 | if (wx_pango_version_check(1,8,0) != NULL || | |
216 | pixelSize.GetWidth() != 0) | |
217 | { | |
218 | // NOTE: pango_font_description_set_absolute_size() only sets the font height; | |
219 | // if the user set the pixel width of the font explicitly or the pango | |
220 | // library is too old, we cannot proceed | |
221 | return false; | |
222 | } | |
223 | ||
224 | pango_font_description_set_absolute_size( m_nativeFontInfo.description, | |
225 | pixelSize.GetHeight() * PANGO_SCALE ); | |
226 | ||
227 | return true; | |
228 | } | |
b5791cc7 FM |
229 | */ |
230 | ||
0c14b6c3 | 231 | void wxFontRefData::SetFamily(wxFontFamily family) |
7826e2dd | 232 | { |
6aea1e4a | 233 | m_nativeFontInfo.SetFamily(family); |
30764ab5 VZ |
234 | } |
235 | ||
0c14b6c3 | 236 | void wxFontRefData::SetStyle(wxFontStyle style) |
c801d85f | 237 | { |
6aea1e4a | 238 | m_nativeFontInfo.SetStyle(style); |
409d5a58 | 239 | } |
7beba2fc | 240 | |
0c14b6c3 | 241 | void wxFontRefData::SetWeight(wxFontWeight weight) |
409d5a58 | 242 | { |
6aea1e4a | 243 | m_nativeFontInfo.SetWeight(weight); |
409d5a58 | 244 | } |
30764ab5 | 245 | |
409d5a58 VZ |
246 | void wxFontRefData::SetUnderlined(bool underlined) |
247 | { | |
248 | m_underlined = underlined; | |
8636aed8 | 249 | |
ecde8361 FM |
250 | // the Pango font descriptor does not have an underlined attribute |
251 | // (and wxNativeFontInfo::SetUnderlined asserts); rather it's | |
252 | // wxWindowDCImpl::DoDrawText that handles underlined fonts, so we | |
253 | // here we just need to save the underlined attribute | |
409d5a58 | 254 | } |
30760ce7 | 255 | |
85ab460e | 256 | bool wxFontRefData::SetFaceName(const wxString& facename) |
409d5a58 | 257 | { |
85ab460e | 258 | return m_nativeFontInfo.SetFaceName(facename); |
409d5a58 | 259 | } |
284b4c88 | 260 | |
7ce58684 | 261 | void wxFontRefData::SetEncoding(wxFontEncoding WXUNUSED(encoding)) |
409d5a58 | 262 | { |
7ce58684 | 263 | // with GTK+ 2 Pango always uses UTF8 internally, we cannot change it |
409d5a58 | 264 | } |
284b4c88 | 265 | |
011ba5ed VZ |
266 | void wxFontRefData::SetNativeFontInfo(const wxNativeFontInfo& info) |
267 | { | |
011ba5ed VZ |
268 | m_nativeFontInfo = info; |
269 | ||
270 | // set all the other font parameters from the native font info | |
271 | InitFromNative(); | |
272 | } | |
273 | ||
409d5a58 VZ |
274 | // ---------------------------------------------------------------------------- |
275 | // wxFont creation | |
276 | // ---------------------------------------------------------------------------- | |
36f210c8 | 277 | |
409d5a58 | 278 | IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject) |
36f210c8 | 279 | |
409d5a58 VZ |
280 | wxFont::wxFont(const wxNativeFontInfo& info) |
281 | { | |
011ba5ed | 282 | Create( info.GetPointSize(), |
db16cab4 RR |
283 | info.GetFamily(), |
284 | info.GetStyle(), | |
285 | info.GetWeight(), | |
286 | info.GetUnderlined(), | |
287 | info.GetFaceName(), | |
288 | info.GetEncoding() ); | |
409d5a58 VZ |
289 | } |
290 | ||
291 | bool wxFont::Create( int pointSize, | |
b5791cc7 FM |
292 | wxFontFamily family, |
293 | wxFontStyle style, | |
294 | wxFontWeight weight, | |
409d5a58 VZ |
295 | bool underlined, |
296 | const wxString& face, | |
b5791cc7 | 297 | wxFontEncoding encoding ) |
409d5a58 | 298 | { |
2b5f62a0 VZ |
299 | UnRef(); |
300 | ||
409d5a58 VZ |
301 | m_refData = new wxFontRefData(pointSize, family, style, weight, |
302 | underlined, face, encoding); | |
303 | ||
de6185e2 | 304 | return true; |
409d5a58 VZ |
305 | } |
306 | ||
307 | bool wxFont::Create(const wxString& fontname) | |
308 | { | |
309 | // VZ: does this really happen? | |
310 | if ( fontname.empty() ) | |
36f210c8 | 311 | { |
409d5a58 | 312 | *this = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT); |
7beba2fc | 313 | |
de6185e2 | 314 | return true; |
36f210c8 | 315 | } |
409d5a58 VZ |
316 | |
317 | m_refData = new wxFontRefData(fontname); | |
318 | ||
de6185e2 | 319 | return true; |
ff7b1510 | 320 | } |
c801d85f | 321 | |
8bbe427f | 322 | wxFont::~wxFont() |
c801d85f | 323 | { |
ff7b1510 | 324 | } |
c801d85f | 325 | |
0c5d3e1c VZ |
326 | // ---------------------------------------------------------------------------- |
327 | // accessors | |
328 | // ---------------------------------------------------------------------------- | |
c801d85f | 329 | |
8bbe427f | 330 | int wxFont::GetPointSize() const |
c801d85f | 331 | { |
ecde8361 | 332 | wxCHECK_MSG( IsOk(), 0, wxT("invalid font") ); |
8bbe427f | 333 | |
ecde8361 | 334 | return M_FONTDATA->m_nativeFontInfo.GetPointSize(); |
ff7b1510 | 335 | } |
c801d85f | 336 | |
8bbe427f | 337 | wxString wxFont::GetFaceName() const |
c801d85f | 338 | { |
ecde8361 | 339 | wxCHECK_MSG( IsOk(), wxEmptyString, wxT("invalid font") ); |
8bbe427f | 340 | |
ecde8361 | 341 | return M_FONTDATA->m_nativeFontInfo.GetFaceName(); |
ff7b1510 | 342 | } |
c801d85f | 343 | |
0c14b6c3 | 344 | wxFontFamily wxFont::GetFamily() const |
c801d85f | 345 | { |
ecde8361 | 346 | wxCHECK_MSG( IsOk(), wxFONTFAMILY_MAX, wxT("invalid font") ); |
8bbe427f | 347 | |
6aea1e4a | 348 | return M_FONTDATA->m_nativeFontInfo.GetFamily(); |
ff7b1510 | 349 | } |
c801d85f | 350 | |
0c14b6c3 | 351 | wxFontStyle wxFont::GetStyle() const |
c801d85f | 352 | { |
ecde8361 | 353 | wxCHECK_MSG( IsOk(), wxFONTSTYLE_MAX, wxT("invalid font") ); |
d84eb083 | 354 | |
ecde8361 | 355 | return M_FONTDATA->m_nativeFontInfo.GetStyle(); |
ff7b1510 | 356 | } |
c801d85f | 357 | |
0c14b6c3 | 358 | wxFontWeight wxFont::GetWeight() const |
c801d85f | 359 | { |
ecde8361 | 360 | wxCHECK_MSG( IsOk(), wxFONTWEIGHT_MAX, wxT("invalid font") ); |
8bbe427f | 361 | |
ecde8361 | 362 | return M_FONTDATA->m_nativeFontInfo.GetWeight(); |
8bbe427f VZ |
363 | } |
364 | ||
8bbe427f VZ |
365 | bool wxFont::GetUnderlined() const |
366 | { | |
ecde8361 | 367 | wxCHECK_MSG( IsOk(), false, wxT("invalid font") ); |
8bbe427f VZ |
368 | |
369 | return M_FONTDATA->m_underlined; | |
ff7b1510 | 370 | } |
c801d85f | 371 | |
0c5d3e1c | 372 | wxFontEncoding wxFont::GetEncoding() const |
358fc25c | 373 | { |
ecde8361 | 374 | wxCHECK_MSG( IsOk(), wxFONTENCODING_SYSTEM, wxT("invalid font") ); |
0c5d3e1c | 375 | |
7ce58684 FM |
376 | return wxFONTENCODING_UTF8; |
377 | // Pango always uses UTF8... see also SetEncoding() | |
358fc25c RR |
378 | } |
379 | ||
5ac2e80c | 380 | bool wxFont::GetNoAntiAliasing() const |
2b5f62a0 | 381 | { |
ecde8361 | 382 | wxCHECK_MSG( IsOk(), false, wxT("invalid font") ); |
2b5f62a0 VZ |
383 | |
384 | return M_FONTDATA->m_noAA; | |
385 | } | |
386 | ||
3bf5a59b | 387 | const wxNativeFontInfo *wxFont::GetNativeFontInfo() const |
30764ab5 | 388 | { |
ecde8361 | 389 | wxCHECK_MSG( IsOk(), NULL, wxT("invalid font") ); |
30764ab5 | 390 | |
3bf5a59b | 391 | return &(M_FONTDATA->m_nativeFontInfo); |
30764ab5 VZ |
392 | } |
393 | ||
53f6aab7 VZ |
394 | bool wxFont::IsFixedWidth() const |
395 | { | |
ecde8361 | 396 | wxCHECK_MSG( IsOk(), false, wxT("invalid font") ); |
53f6aab7 | 397 | |
53f6aab7 VZ |
398 | return wxFontBase::IsFixedWidth(); |
399 | } | |
30764ab5 | 400 | |
0c5d3e1c VZ |
401 | // ---------------------------------------------------------------------------- |
402 | // change font attributes | |
403 | // ---------------------------------------------------------------------------- | |
404 | ||
358fc25c RR |
405 | void wxFont::SetPointSize(int pointSize) |
406 | { | |
fd7a7443 | 407 | AllocExclusive(); |
011ba5ed | 408 | |
409d5a58 | 409 | M_FONTDATA->SetPointSize(pointSize); |
358fc25c RR |
410 | } |
411 | ||
0c14b6c3 | 412 | void wxFont::SetFamily(wxFontFamily family) |
358fc25c | 413 | { |
fd7a7443 | 414 | AllocExclusive(); |
358fc25c | 415 | |
409d5a58 | 416 | M_FONTDATA->SetFamily(family); |
358fc25c RR |
417 | } |
418 | ||
0c14b6c3 | 419 | void wxFont::SetStyle(wxFontStyle style) |
358fc25c | 420 | { |
fd7a7443 | 421 | AllocExclusive(); |
358fc25c | 422 | |
409d5a58 | 423 | M_FONTDATA->SetStyle(style); |
358fc25c RR |
424 | } |
425 | ||
0c14b6c3 | 426 | void wxFont::SetWeight(wxFontWeight weight) |
358fc25c | 427 | { |
fd7a7443 | 428 | AllocExclusive(); |
358fc25c | 429 | |
409d5a58 | 430 | M_FONTDATA->SetWeight(weight); |
358fc25c RR |
431 | } |
432 | ||
85ab460e | 433 | bool wxFont::SetFaceName(const wxString& faceName) |
358fc25c | 434 | { |
fd7a7443 | 435 | AllocExclusive(); |
358fc25c | 436 | |
85ab460e VZ |
437 | return M_FONTDATA->SetFaceName(faceName) && |
438 | wxFontBase::SetFaceName(faceName); | |
358fc25c RR |
439 | } |
440 | ||
441 | void wxFont::SetUnderlined(bool underlined) | |
442 | { | |
fd7a7443 | 443 | AllocExclusive(); |
358fc25c | 444 | |
409d5a58 | 445 | M_FONTDATA->SetUnderlined(underlined); |
358fc25c RR |
446 | } |
447 | ||
0c5d3e1c VZ |
448 | void wxFont::SetEncoding(wxFontEncoding encoding) |
449 | { | |
fd7a7443 | 450 | AllocExclusive(); |
c801d85f | 451 | |
409d5a58 | 452 | M_FONTDATA->SetEncoding(encoding); |
30764ab5 VZ |
453 | } |
454 | ||
9045ad9d | 455 | void wxFont::DoSetNativeFontInfo( const wxNativeFontInfo& info ) |
2b5f62a0 | 456 | { |
fd7a7443 | 457 | AllocExclusive(); |
2b5f62a0 VZ |
458 | |
459 | M_FONTDATA->SetNativeFontInfo( info ); | |
460 | } | |
461 | ||
462 | void wxFont::SetNoAntiAliasing( bool no ) | |
30764ab5 | 463 | { |
fd7a7443 | 464 | AllocExclusive(); |
30764ab5 | 465 | |
2b5f62a0 | 466 | M_FONTDATA->SetNoAntiAliasing( no ); |
0c5d3e1c | 467 | } |
fd7a7443 | 468 | |
8f884a0d | 469 | wxGDIRefData* wxFont::CreateGDIRefData() const |
fd7a7443 PC |
470 | { |
471 | return new wxFontRefData; | |
472 | } | |
473 | ||
8f884a0d | 474 | wxGDIRefData* wxFont::CloneGDIRefData(const wxGDIRefData* data) const |
fd7a7443 | 475 | { |
5c33522f | 476 | return new wxFontRefData(*static_cast<const wxFontRefData*>(data)); |
fd7a7443 | 477 | } |