]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/font.cpp
Add doc for wxGraphicsContext.Create(), which is a lightweight context with no target...
[wxWidgets.git] / src / gtk / font.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/font.cpp
3 // Purpose: wxFont for wxGTK
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling and Julian Smart
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // ============================================================================
11 // declarations
12 // ============================================================================
13
14 // ----------------------------------------------------------------------------
15 // headers
16 // ----------------------------------------------------------------------------
17
18 // For compilers that support precompilation, includes "wx.h".
19 #include "wx/wxprec.h"
20
21 #include "wx/font.h"
22
23 #ifndef WX_PRECOMP
24 #include "wx/log.h"
25 #include "wx/utils.h"
26 #include "wx/settings.h"
27 #include "wx/gdicmn.h"
28 #endif
29
30 #include "wx/fontutil.h"
31 #include "wx/tokenzr.h"
32
33 #include "wx/gtk/private.h"
34
35 // ----------------------------------------------------------------------------
36 // constants
37 // ----------------------------------------------------------------------------
38
39 // the default size (in points) for the fonts
40 static const int wxDEFAULT_FONT_SIZE = 12;
41
42 // ----------------------------------------------------------------------------
43 // wxFontRefData
44 // ----------------------------------------------------------------------------
45
46 class wxFontRefData : public wxGDIRefData
47 {
48 public:
49 // from broken down font parameters, also default ctor
50 wxFontRefData(int size = -1,
51 wxFontFamily family = wxFONTFAMILY_DEFAULT,
52 wxFontStyle style = wxFONTSTYLE_NORMAL,
53 wxFontWeight weight = wxFONTWEIGHT_NORMAL,
54 bool underlined = false,
55 bool strikethrough = false,
56 const wxString& faceName = wxEmptyString,
57 wxFontEncoding encoding = wxFONTENCODING_DEFAULT);
58
59 wxFontRefData(const wxString& nativeFontInfoString);
60
61 // copy ctor
62 wxFontRefData( const wxFontRefData& data );
63
64 virtual ~wxFontRefData();
65
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);
69 void SetFamily(wxFontFamily family);
70 void SetStyle(wxFontStyle style);
71 void SetWeight(wxFontWeight weight);
72 void SetUnderlined(bool underlined);
73 void SetStrikethrough(bool strikethrough);
74 bool SetFaceName(const wxString& facename);
75 void SetEncoding(wxFontEncoding encoding);
76
77 // and this one also modifies all the other font data fields
78 void SetNativeFontInfo(const wxNativeFontInfo& info);
79
80 protected:
81 // common part of all ctors
82 void Init(int pointSize,
83 wxFontFamily family,
84 wxFontStyle style,
85 wxFontWeight weight,
86 bool underlined,
87 bool strikethrough,
88 const wxString& faceName,
89 wxFontEncoding encoding);
90
91 // set all fields from (already initialized and valid) m_nativeFontInfo
92 void InitFromNative();
93
94 private:
95 // The native font info: basically a PangoFontDescription, plus
96 // 'underlined' and 'strikethrough' attributes not supported by Pango.
97 wxNativeFontInfo m_nativeFontInfo;
98
99 friend class wxFont;
100 };
101
102 #define M_FONTDATA ((wxFontRefData*)m_refData)
103
104 // ----------------------------------------------------------------------------
105 // wxFontRefData
106 // ----------------------------------------------------------------------------
107
108 void wxFontRefData::Init(int pointSize,
109 wxFontFamily family,
110 wxFontStyle style,
111 wxFontWeight weight,
112 bool underlined,
113 bool strikethrough,
114 const wxString& faceName,
115 wxFontEncoding WXUNUSED(encoding))
116 {
117 if (family == wxFONTFAMILY_DEFAULT)
118 family = wxFONTFAMILY_SWISS;
119
120 // Create native font info
121 m_nativeFontInfo.description = pango_font_description_new();
122
123 // And set its values
124 if (!faceName.empty())
125 {
126 pango_font_description_set_family( m_nativeFontInfo.description,
127 wxGTK_CONV_SYS(faceName) );
128 }
129 else
130 {
131 SetFamily(family);
132 }
133
134 SetStyle( style == wxDEFAULT ? wxFONTSTYLE_NORMAL : style );
135 SetPointSize( (pointSize == wxDEFAULT || pointSize == -1)
136 ? wxDEFAULT_FONT_SIZE
137 : pointSize );
138 SetWeight( weight == wxDEFAULT ? wxFONTWEIGHT_NORMAL : weight );
139 SetUnderlined( underlined );
140 SetStrikethrough( strikethrough );
141 }
142
143 void wxFontRefData::InitFromNative()
144 {
145 // Get native info
146 PangoFontDescription *desc = m_nativeFontInfo.description;
147
148 // Pango sometimes needs to have a size
149 int pango_size = pango_font_description_get_size( desc );
150 if (pango_size == 0)
151 m_nativeFontInfo.SetPointSize(wxDEFAULT_FONT_SIZE);
152 }
153
154 wxFontRefData::wxFontRefData( const wxFontRefData& data )
155 : wxGDIRefData()
156 {
157 // Forces a copy of the internal data. wxNativeFontInfo should probably
158 // have a copy ctor and assignment operator to fix this properly but that
159 // would break binary compatibility...
160 m_nativeFontInfo.FromString(data.m_nativeFontInfo.ToString());
161 }
162
163 wxFontRefData::wxFontRefData(int size, wxFontFamily family, wxFontStyle style,
164 wxFontWeight weight, bool underlined, bool strikethrough,
165 const wxString& faceName,
166 wxFontEncoding encoding)
167 {
168 Init(size, family, style, weight, underlined, strikethrough, faceName, encoding);
169 }
170
171 wxFontRefData::wxFontRefData(const wxString& nativeFontInfoString)
172 {
173 m_nativeFontInfo.FromString( nativeFontInfoString );
174
175 InitFromNative();
176 }
177
178 wxFontRefData::~wxFontRefData()
179 {
180 }
181
182 // ----------------------------------------------------------------------------
183 // wxFontRefData SetXXX()
184 // ----------------------------------------------------------------------------
185
186 void wxFontRefData::SetPointSize(int pointSize)
187 {
188 m_nativeFontInfo.SetPointSize(pointSize);
189 }
190
191 /*
192 NOTE: disabled because pango_font_description_set_absolute_size() and
193 wxDC::GetCharHeight() do not mix well: setting with the former a pixel
194 size of "30" makes the latter return 36...
195 Besides, we need to return GetPointSize() a point size value even if
196 SetPixelSize() was used and this would require further changes
197 (and use of pango_font_description_get_size_is_absolute in some places).
198
199 bool wxFontRefData::SetPixelSize(const wxSize& pixelSize)
200 {
201 wxCHECK_MSG( pixelSize.GetWidth() >= 0 && pixelSize.GetHeight() > 0, false,
202 "Negative values for the pixel size or zero pixel height are not allowed" );
203
204 if (wx_pango_version_check(1,8,0) != NULL ||
205 pixelSize.GetWidth() != 0)
206 {
207 // NOTE: pango_font_description_set_absolute_size() only sets the font height;
208 // if the user set the pixel width of the font explicitly or the pango
209 // library is too old, we cannot proceed
210 return false;
211 }
212
213 pango_font_description_set_absolute_size( m_nativeFontInfo.description,
214 pixelSize.GetHeight() * PANGO_SCALE );
215
216 return true;
217 }
218 */
219
220 void wxFontRefData::SetFamily(wxFontFamily family)
221 {
222 m_nativeFontInfo.SetFamily(family);
223 }
224
225 void wxFontRefData::SetStyle(wxFontStyle style)
226 {
227 m_nativeFontInfo.SetStyle(style);
228 }
229
230 void wxFontRefData::SetWeight(wxFontWeight weight)
231 {
232 m_nativeFontInfo.SetWeight(weight);
233 }
234
235 void wxFontRefData::SetUnderlined(bool underlined)
236 {
237 m_nativeFontInfo.SetUnderlined(underlined);
238 }
239
240 void wxFontRefData::SetStrikethrough(bool strikethrough)
241 {
242 m_nativeFontInfo.SetStrikethrough(strikethrough);
243 }
244
245 bool wxFontRefData::SetFaceName(const wxString& facename)
246 {
247 return m_nativeFontInfo.SetFaceName(facename);
248 }
249
250 void wxFontRefData::SetEncoding(wxFontEncoding WXUNUSED(encoding))
251 {
252 // with GTK+ 2 Pango always uses UTF8 internally, we cannot change it
253 }
254
255 void wxFontRefData::SetNativeFontInfo(const wxNativeFontInfo& info)
256 {
257 m_nativeFontInfo = info;
258
259 // set all the other font parameters from the native font info
260 InitFromNative();
261 }
262
263 // ----------------------------------------------------------------------------
264 // wxFont creation
265 // ----------------------------------------------------------------------------
266
267 wxFont::wxFont(const wxNativeFontInfo& info)
268 {
269 Create( info.GetPointSize(),
270 info.GetFamily(),
271 info.GetStyle(),
272 info.GetWeight(),
273 info.GetUnderlined(),
274 info.GetFaceName(),
275 info.GetEncoding() );
276
277 if ( info.GetStrikethrough() )
278 SetStrikethrough(true);
279 }
280
281 wxFont::wxFont(const wxFontInfo& info)
282 {
283 m_refData = new wxFontRefData(info.GetPointSize(),
284 info.GetFamily(),
285 info.GetStyle(),
286 info.GetWeight(),
287 info.IsUnderlined(),
288 info.IsStrikethrough(),
289 info.GetFaceName(),
290 info.GetEncoding());
291
292 wxSize pixelSize = info.GetPixelSize();
293 if ( pixelSize != wxDefaultSize )
294 SetPixelSize(pixelSize);
295 }
296
297 bool wxFont::Create( int pointSize,
298 wxFontFamily family,
299 wxFontStyle style,
300 wxFontWeight weight,
301 bool underlined,
302 const wxString& face,
303 wxFontEncoding encoding )
304 {
305 UnRef();
306
307 m_refData = new wxFontRefData(pointSize, family, style, weight,
308 underlined, false, face, encoding);
309
310 return true;
311 }
312
313 bool wxFont::Create(const wxString& fontname)
314 {
315 // VZ: does this really happen?
316 if ( fontname.empty() )
317 {
318 *this = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
319
320 return true;
321 }
322
323 m_refData = new wxFontRefData(fontname);
324
325 return true;
326 }
327
328 wxFont::~wxFont()
329 {
330 }
331
332 // ----------------------------------------------------------------------------
333 // accessors
334 // ----------------------------------------------------------------------------
335
336 int wxFont::GetPointSize() const
337 {
338 wxCHECK_MSG( IsOk(), 0, wxT("invalid font") );
339
340 return M_FONTDATA->m_nativeFontInfo.GetPointSize();
341 }
342
343 wxString wxFont::GetFaceName() const
344 {
345 wxCHECK_MSG( IsOk(), wxEmptyString, wxT("invalid font") );
346
347 return M_FONTDATA->m_nativeFontInfo.GetFaceName();
348 }
349
350 wxFontFamily wxFont::DoGetFamily() const
351 {
352 return M_FONTDATA->m_nativeFontInfo.GetFamily();
353 }
354
355 wxFontStyle wxFont::GetStyle() const
356 {
357 wxCHECK_MSG( IsOk(), wxFONTSTYLE_MAX, wxT("invalid font") );
358
359 return M_FONTDATA->m_nativeFontInfo.GetStyle();
360 }
361
362 wxFontWeight wxFont::GetWeight() const
363 {
364 wxCHECK_MSG( IsOk(), wxFONTWEIGHT_MAX, wxT("invalid font") );
365
366 return M_FONTDATA->m_nativeFontInfo.GetWeight();
367 }
368
369 bool wxFont::GetUnderlined() const
370 {
371 wxCHECK_MSG( IsOk(), false, wxT("invalid font") );
372
373 return M_FONTDATA->m_nativeFontInfo.GetUnderlined();
374 }
375
376 bool wxFont::GetStrikethrough() const
377 {
378 wxCHECK_MSG( IsOk(), false, wxT("invalid font") );
379
380 return M_FONTDATA->m_nativeFontInfo.GetStrikethrough();
381 }
382
383 wxFontEncoding wxFont::GetEncoding() const
384 {
385 wxCHECK_MSG( IsOk(), wxFONTENCODING_SYSTEM, wxT("invalid font") );
386
387 return wxFONTENCODING_UTF8;
388 // Pango always uses UTF8... see also SetEncoding()
389 }
390
391 const wxNativeFontInfo *wxFont::GetNativeFontInfo() const
392 {
393 wxCHECK_MSG( IsOk(), NULL, wxT("invalid font") );
394
395 return &(M_FONTDATA->m_nativeFontInfo);
396 }
397
398 bool wxFont::IsFixedWidth() const
399 {
400 wxCHECK_MSG( IsOk(), false, wxT("invalid font") );
401
402 return wxFontBase::IsFixedWidth();
403 }
404
405 // ----------------------------------------------------------------------------
406 // change font attributes
407 // ----------------------------------------------------------------------------
408
409 void wxFont::SetPointSize(int pointSize)
410 {
411 AllocExclusive();
412
413 M_FONTDATA->SetPointSize(pointSize);
414 }
415
416 void wxFont::SetFamily(wxFontFamily family)
417 {
418 AllocExclusive();
419
420 M_FONTDATA->SetFamily(family);
421 }
422
423 void wxFont::SetStyle(wxFontStyle style)
424 {
425 AllocExclusive();
426
427 M_FONTDATA->SetStyle(style);
428 }
429
430 void wxFont::SetWeight(wxFontWeight weight)
431 {
432 AllocExclusive();
433
434 M_FONTDATA->SetWeight(weight);
435 }
436
437 bool wxFont::SetFaceName(const wxString& faceName)
438 {
439 AllocExclusive();
440
441 return M_FONTDATA->SetFaceName(faceName) &&
442 wxFontBase::SetFaceName(faceName);
443 }
444
445 void wxFont::SetUnderlined(bool underlined)
446 {
447 AllocExclusive();
448
449 M_FONTDATA->SetUnderlined(underlined);
450 }
451
452 void wxFont::SetStrikethrough(bool strikethrough)
453 {
454 AllocExclusive();
455
456 M_FONTDATA->SetStrikethrough(strikethrough);
457 }
458
459 void wxFont::SetEncoding(wxFontEncoding encoding)
460 {
461 AllocExclusive();
462
463 M_FONTDATA->SetEncoding(encoding);
464 }
465
466 void wxFont::DoSetNativeFontInfo( const wxNativeFontInfo& info )
467 {
468 AllocExclusive();
469
470 M_FONTDATA->SetNativeFontInfo( info );
471 }
472
473 wxGDIRefData* wxFont::CreateGDIRefData() const
474 {
475 return new wxFontRefData;
476 }
477
478 wxGDIRefData* wxFont::CloneGDIRefData(const wxGDIRefData* data) const
479 {
480 return new wxFontRefData(*static_cast<const wxFontRefData*>(data));
481 }
482
483 bool wxFont::GTKSetPangoAttrs(PangoLayout* layout) const
484 {
485 if (!IsOk() || !(GetUnderlined() || GetStrikethrough()))
486 return false;
487
488 PangoAttrList* attrs = pango_attr_list_new();
489 PangoAttribute* a;
490
491 if (wx_pango_version_check(1,16,0))
492 {
493 // a PangoLayout which has leading/trailing spaces with underlined font
494 // is not correctly drawn by this pango version: Pango won't underline the spaces.
495 // This can be a problem; e.g. wxHTML rendering of underlined text relies on
496 // this behaviour. To workaround this problem, we use a special hack here
497 // suggested by pango maintainer Behdad Esfahbod: we prepend and append two
498 // empty space characters and give them a dummy colour attribute.
499 // This will force Pango to underline the leading/trailing spaces, too.
500
501 const char* text = pango_layout_get_text(layout);
502 const size_t n = strlen(text);
503 if ((n > 0 && text[0] == ' ') || (n > 1 && text[n - 1] == ' '))
504 {
505 wxCharBuffer buf(n + 6);
506 // copy the leading U+200C ZERO WIDTH NON-JOINER encoded in UTF8 format
507 memcpy(buf.data(), "\342\200\214", 3);
508 // copy the user string
509 memcpy(buf.data() + 3, text, n);
510 // copy the trailing U+200C ZERO WIDTH NON-JOINER encoded in UTF8 format
511 memcpy(buf.data() + 3 + n, "\342\200\214", 3);
512
513 pango_layout_set_text(layout, buf, n + 6);
514
515 // Add dummy attributes (use colour as it's invisible anyhow for 0
516 // width spaces) to ensure that the spaces in the beginning/end of the
517 // string are underlined too.
518 a = pango_attr_foreground_new(0x0057, 0x52A9, 0xD614);
519 a->start_index = 0;
520 a->end_index = 3;
521 pango_attr_list_insert(attrs, a);
522
523 a = pango_attr_foreground_new(0x0057, 0x52A9, 0xD614);
524 a->start_index = n + 3;
525 a->end_index = n + 6;
526 pango_attr_list_insert(attrs, a);
527 }
528 }
529 if (GetUnderlined())
530 {
531 a = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE);
532 pango_attr_list_insert(attrs, a);
533 }
534 if (GetStrikethrough())
535 {
536 a = pango_attr_strikethrough_new(true);
537 pango_attr_list_insert(attrs, a);
538 }
539
540 pango_layout_set_attributes(layout, attrs);
541 pango_attr_list_unref(attrs);
542
543 return true;
544 }