]>
Commit | Line | Data |
---|---|---|
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(int pointSize, | |
282 | wxFontFamily family, | |
283 | int flags, | |
284 | const wxString& face, | |
285 | wxFontEncoding encoding) | |
286 | { | |
287 | m_refData = new wxFontRefData(pointSize, family, | |
288 | GetStyleFromFlags(flags), | |
289 | GetWeightFromFlags(flags), | |
290 | GetUnderlinedFromFlags(flags), | |
291 | GetStrikethroughFromFlags(flags), | |
292 | face, encoding); | |
293 | } | |
294 | ||
295 | bool wxFont::Create( int pointSize, | |
296 | wxFontFamily family, | |
297 | wxFontStyle style, | |
298 | wxFontWeight weight, | |
299 | bool underlined, | |
300 | const wxString& face, | |
301 | wxFontEncoding encoding ) | |
302 | { | |
303 | UnRef(); | |
304 | ||
305 | m_refData = new wxFontRefData(pointSize, family, style, weight, | |
306 | underlined, false, face, encoding); | |
307 | ||
308 | return true; | |
309 | } | |
310 | ||
311 | bool wxFont::Create(const wxString& fontname) | |
312 | { | |
313 | // VZ: does this really happen? | |
314 | if ( fontname.empty() ) | |
315 | { | |
316 | *this = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT); | |
317 | ||
318 | return true; | |
319 | } | |
320 | ||
321 | m_refData = new wxFontRefData(fontname); | |
322 | ||
323 | return true; | |
324 | } | |
325 | ||
326 | wxFont::~wxFont() | |
327 | { | |
328 | } | |
329 | ||
330 | // ---------------------------------------------------------------------------- | |
331 | // accessors | |
332 | // ---------------------------------------------------------------------------- | |
333 | ||
334 | int wxFont::GetPointSize() const | |
335 | { | |
336 | wxCHECK_MSG( IsOk(), 0, wxT("invalid font") ); | |
337 | ||
338 | return M_FONTDATA->m_nativeFontInfo.GetPointSize(); | |
339 | } | |
340 | ||
341 | wxString wxFont::GetFaceName() const | |
342 | { | |
343 | wxCHECK_MSG( IsOk(), wxEmptyString, wxT("invalid font") ); | |
344 | ||
345 | return M_FONTDATA->m_nativeFontInfo.GetFaceName(); | |
346 | } | |
347 | ||
348 | wxFontFamily wxFont::DoGetFamily() const | |
349 | { | |
350 | return M_FONTDATA->m_nativeFontInfo.GetFamily(); | |
351 | } | |
352 | ||
353 | wxFontStyle wxFont::GetStyle() const | |
354 | { | |
355 | wxCHECK_MSG( IsOk(), wxFONTSTYLE_MAX, wxT("invalid font") ); | |
356 | ||
357 | return M_FONTDATA->m_nativeFontInfo.GetStyle(); | |
358 | } | |
359 | ||
360 | wxFontWeight wxFont::GetWeight() const | |
361 | { | |
362 | wxCHECK_MSG( IsOk(), wxFONTWEIGHT_MAX, wxT("invalid font") ); | |
363 | ||
364 | return M_FONTDATA->m_nativeFontInfo.GetWeight(); | |
365 | } | |
366 | ||
367 | bool wxFont::GetUnderlined() const | |
368 | { | |
369 | wxCHECK_MSG( IsOk(), false, wxT("invalid font") ); | |
370 | ||
371 | return M_FONTDATA->m_nativeFontInfo.GetUnderlined(); | |
372 | } | |
373 | ||
374 | bool wxFont::GetStrikethrough() const | |
375 | { | |
376 | wxCHECK_MSG( IsOk(), false, wxT("invalid font") ); | |
377 | ||
378 | return M_FONTDATA->m_nativeFontInfo.GetStrikethrough(); | |
379 | } | |
380 | ||
381 | wxFontEncoding wxFont::GetEncoding() const | |
382 | { | |
383 | wxCHECK_MSG( IsOk(), wxFONTENCODING_SYSTEM, wxT("invalid font") ); | |
384 | ||
385 | return wxFONTENCODING_UTF8; | |
386 | // Pango always uses UTF8... see also SetEncoding() | |
387 | } | |
388 | ||
389 | const wxNativeFontInfo *wxFont::GetNativeFontInfo() const | |
390 | { | |
391 | wxCHECK_MSG( IsOk(), NULL, wxT("invalid font") ); | |
392 | ||
393 | return &(M_FONTDATA->m_nativeFontInfo); | |
394 | } | |
395 | ||
396 | bool wxFont::IsFixedWidth() const | |
397 | { | |
398 | wxCHECK_MSG( IsOk(), false, wxT("invalid font") ); | |
399 | ||
400 | return wxFontBase::IsFixedWidth(); | |
401 | } | |
402 | ||
403 | // ---------------------------------------------------------------------------- | |
404 | // change font attributes | |
405 | // ---------------------------------------------------------------------------- | |
406 | ||
407 | void wxFont::SetPointSize(int pointSize) | |
408 | { | |
409 | AllocExclusive(); | |
410 | ||
411 | M_FONTDATA->SetPointSize(pointSize); | |
412 | } | |
413 | ||
414 | void wxFont::SetFamily(wxFontFamily family) | |
415 | { | |
416 | AllocExclusive(); | |
417 | ||
418 | M_FONTDATA->SetFamily(family); | |
419 | } | |
420 | ||
421 | void wxFont::SetStyle(wxFontStyle style) | |
422 | { | |
423 | AllocExclusive(); | |
424 | ||
425 | M_FONTDATA->SetStyle(style); | |
426 | } | |
427 | ||
428 | void wxFont::SetWeight(wxFontWeight weight) | |
429 | { | |
430 | AllocExclusive(); | |
431 | ||
432 | M_FONTDATA->SetWeight(weight); | |
433 | } | |
434 | ||
435 | bool wxFont::SetFaceName(const wxString& faceName) | |
436 | { | |
437 | AllocExclusive(); | |
438 | ||
439 | return M_FONTDATA->SetFaceName(faceName) && | |
440 | wxFontBase::SetFaceName(faceName); | |
441 | } | |
442 | ||
443 | void wxFont::SetUnderlined(bool underlined) | |
444 | { | |
445 | AllocExclusive(); | |
446 | ||
447 | M_FONTDATA->SetUnderlined(underlined); | |
448 | } | |
449 | ||
450 | void wxFont::SetStrikethrough(bool strikethrough) | |
451 | { | |
452 | AllocExclusive(); | |
453 | ||
454 | M_FONTDATA->SetStrikethrough(strikethrough); | |
455 | } | |
456 | ||
457 | void wxFont::SetEncoding(wxFontEncoding encoding) | |
458 | { | |
459 | AllocExclusive(); | |
460 | ||
461 | M_FONTDATA->SetEncoding(encoding); | |
462 | } | |
463 | ||
464 | void wxFont::DoSetNativeFontInfo( const wxNativeFontInfo& info ) | |
465 | { | |
466 | AllocExclusive(); | |
467 | ||
468 | M_FONTDATA->SetNativeFontInfo( info ); | |
469 | } | |
470 | ||
471 | wxGDIRefData* wxFont::CreateGDIRefData() const | |
472 | { | |
473 | return new wxFontRefData; | |
474 | } | |
475 | ||
476 | wxGDIRefData* wxFont::CloneGDIRefData(const wxGDIRefData* data) const | |
477 | { | |
478 | return new wxFontRefData(*static_cast<const wxFontRefData*>(data)); | |
479 | } | |
480 | ||
481 | bool wxFont::GTKSetPangoAttrs(PangoLayout* layout) const | |
482 | { | |
483 | if (!IsOk() || !(GetUnderlined() || GetStrikethrough())) | |
484 | return false; | |
485 | ||
486 | PangoAttrList* attrs = pango_attr_list_new(); | |
487 | PangoAttribute* a; | |
488 | ||
489 | if (wx_pango_version_check(1,16,0)) | |
490 | { | |
491 | // a PangoLayout which has leading/trailing spaces with underlined font | |
492 | // is not correctly drawn by this pango version: Pango won't underline the spaces. | |
493 | // This can be a problem; e.g. wxHTML rendering of underlined text relies on | |
494 | // this behaviour. To workaround this problem, we use a special hack here | |
495 | // suggested by pango maintainer Behdad Esfahbod: we prepend and append two | |
496 | // empty space characters and give them a dummy colour attribute. | |
497 | // This will force Pango to underline the leading/trailing spaces, too. | |
498 | ||
499 | const char* text = pango_layout_get_text(layout); | |
500 | const size_t n = strlen(text); | |
501 | if ((n > 0 && text[0] == ' ') || (n > 1 && text[n - 1] == ' ')) | |
502 | { | |
503 | wxCharBuffer buf(n + 6); | |
504 | // copy the leading U+200C ZERO WIDTH NON-JOINER encoded in UTF8 format | |
505 | memcpy(buf.data(), "\342\200\214", 3); | |
506 | // copy the user string | |
507 | memcpy(buf.data() + 3, text, n); | |
508 | // copy the trailing U+200C ZERO WIDTH NON-JOINER encoded in UTF8 format | |
509 | memcpy(buf.data() + 3 + n, "\342\200\214", 3); | |
510 | ||
511 | pango_layout_set_text(layout, buf, n + 6); | |
512 | ||
513 | // Add dummy attributes (use colour as it's invisible anyhow for 0 | |
514 | // width spaces) to ensure that the spaces in the beginning/end of the | |
515 | // string are underlined too. | |
516 | a = pango_attr_foreground_new(0x0057, 0x52A9, 0xD614); | |
517 | a->start_index = 0; | |
518 | a->end_index = 3; | |
519 | pango_attr_list_insert(attrs, a); | |
520 | ||
521 | a = pango_attr_foreground_new(0x0057, 0x52A9, 0xD614); | |
522 | a->start_index = n + 3; | |
523 | a->end_index = n + 6; | |
524 | pango_attr_list_insert(attrs, a); | |
525 | } | |
526 | } | |
527 | if (GetUnderlined()) | |
528 | { | |
529 | a = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE); | |
530 | pango_attr_list_insert(attrs, a); | |
531 | } | |
532 | if (GetStrikethrough()) | |
533 | { | |
534 | a = pango_attr_strikethrough_new(true); | |
535 | pango_attr_list_insert(attrs, a); | |
536 | } | |
537 | ||
538 | pango_layout_set_attributes(layout, attrs); | |
539 | pango_attr_list_unref(attrs); | |
540 | ||
541 | return true; | |
542 | } |