]> git.saurik.com Git - wxWidgets.git/blob - src/x11/font.cpp
Prevent crash in wxString::Mid under STL builds (fixes #1231725).
[wxWidgets.git] / src / x11 / font.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/x11/font.cpp
3 // Purpose: wxFont class
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 17/09/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "font.h"
22 #endif
23
24 #ifdef __VMS
25 #pragma message disable nosimpint
26 #include "wx/vms_x_fix.h"
27 #endif
28
29 #ifdef __VMS
30 #pragma message enable nosimpint
31 #endif
32
33 #include "wx/defs.h"
34 #include "wx/string.h"
35 #include "wx/font.h"
36 #include "wx/gdicmn.h"
37 #include "wx/utils.h" // for wxGetDisplay()
38 #include "wx/fontutil.h" // for wxNativeFontInfo
39 #include "wx/tokenzr.h"
40 #include "wx/settings.h"
41
42 #include "wx/x11/private.h"
43
44 IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
45
46 // ----------------------------------------------------------------------------
47 // constants
48 // ----------------------------------------------------------------------------
49
50 // the default size (in points) for the fonts
51 static const int wxDEFAULT_FONT_SIZE = 12;
52
53
54 #if wxUSE_UNICODE
55 #else
56 // ----------------------------------------------------------------------------
57 // wxXFont
58 // ----------------------------------------------------------------------------
59
60 // For every wxFont, there must be a font for each display and scale requested.
61 // So these objects are stored in wxFontRefData::m_fonts
62 class wxXFont : public wxObject
63 {
64 public:
65 wxXFont();
66 ~wxXFont();
67
68 WXFontStructPtr m_fontStruct; // XFontStruct
69 WXDisplay* m_display; // XDisplay
70 int m_scale; // Scale * 100
71 };
72
73 wxXFont::wxXFont()
74 {
75 m_fontStruct = (WXFontStructPtr) 0;
76 m_display = (WXDisplay*) 0;
77 m_scale = 100;
78 }
79
80 wxXFont::~wxXFont()
81 {
82 // Freeing the font used to produce a segv, but
83 // appears to be OK now (bug fix in X11?)
84 XFontStruct* fontStruct = (XFontStruct*) m_fontStruct;
85 XFreeFont((Display*) m_display, fontStruct);
86 }
87 #endif
88
89 // ----------------------------------------------------------------------------
90 // wxFontRefData
91 // ----------------------------------------------------------------------------
92
93 class wxFontRefData: public wxObjectRefData
94 {
95 friend class wxFont;
96
97 public:
98 wxFontRefData(int size = wxDEFAULT,
99 int family = wxDEFAULT,
100 int style = wxDEFAULT,
101 int weight = wxDEFAULT,
102 bool underlined = FALSE,
103 const wxString& faceName = wxEmptyString,
104 wxFontEncoding encoding = wxFONTENCODING_DEFAULT);
105
106 // copy cstr
107 wxFontRefData(const wxFontRefData& data);
108
109 // from XFLD
110 wxFontRefData(const wxString& fontname);
111
112 // dstr
113 virtual ~wxFontRefData();
114
115 // setters: all of them also take care to modify m_nativeFontInfo if we
116 // have it so as to not lose the information not carried by our fields
117 void SetPointSize(int pointSize);
118 void SetFamily(int family);
119 void SetStyle(int style);
120 void SetWeight(int weight);
121 void SetUnderlined(bool underlined);
122 void SetFaceName(const wxString& facename);
123 void SetEncoding(wxFontEncoding encoding);
124
125 void SetNoAntiAliasing( bool no = TRUE ) { m_noAA = no; }
126 bool GetNoAntiAliasing() const { return m_noAA; }
127
128 // and this one also modifies all the other font data fields
129 void SetNativeFontInfo(const wxNativeFontInfo& info);
130
131 protected:
132 // common part of all ctors
133 void Init(int size,
134 int family,
135 int style,
136 int weight,
137 bool underlined,
138 const wxString& faceName,
139 wxFontEncoding encoding);
140
141 // set all fields from (already initialized and valid) m_nativeFontInfo
142 void InitFromNative();
143
144 // font attributes
145 int m_pointSize;
146 int m_family;
147 int m_style;
148 int m_weight;
149 bool m_underlined;
150 wxString m_faceName;
151 wxFontEncoding m_encoding; // Unused in Unicode mode
152 bool m_noAA; // No anti-aliasing
153
154 wxNativeFontInfo m_nativeFontInfo;
155
156 void ClearX11Fonts();
157
158 #if wxUSE_UNICODE
159 #else
160 // A list of wxXFonts
161 wxList m_fonts;
162 #endif
163 };
164
165 // ----------------------------------------------------------------------------
166 // wxFontRefData
167 // ----------------------------------------------------------------------------
168
169 void wxFontRefData::Init(int pointSize,
170 int family,
171 int style,
172 int weight,
173 bool underlined,
174 const wxString& faceName,
175 wxFontEncoding encoding)
176 {
177 m_family = family == wxFONTFAMILY_DEFAULT ? wxFONTFAMILY_SWISS : family;
178
179 m_faceName = faceName;
180
181 // we accept both wxDEFAULT and wxNORMAL here - should we?
182 m_style = style == wxDEFAULT ? wxFONTSTYLE_NORMAL : style;
183 m_weight = weight == wxDEFAULT ? wxFONTWEIGHT_NORMAL : weight;
184
185 // and here, do we really want to forbid creation of the font of the size
186 // 90 (the value of wxDEFAULT)??
187 m_pointSize = pointSize == wxDEFAULT || pointSize == -1
188 ? wxDEFAULT_FONT_SIZE
189 : pointSize;
190
191 m_underlined = underlined;
192 m_encoding = encoding;
193
194 #if wxUSE_UNICODE
195 // Create native font info
196 m_nativeFontInfo.description = pango_font_description_new();
197
198 // And set its values
199 switch (m_family)
200 {
201 case wxFONTFAMILY_MODERN:
202 case wxFONTFAMILY_TELETYPE:
203 pango_font_description_set_family( m_nativeFontInfo.description, "monospace" );
204 break;
205 case wxFONTFAMILY_SWISS:
206 pango_font_description_set_family( m_nativeFontInfo.description, "serif" );
207 break;
208 default:
209 pango_font_description_set_family( m_nativeFontInfo.description, "sans" );
210 break;
211 }
212 SetStyle( m_style );
213 SetPointSize( m_pointSize );
214 SetWeight( m_weight );
215 #endif
216 }
217
218 void wxFontRefData::InitFromNative()
219 {
220 m_noAA = FALSE;
221
222 #if wxUSE_UNICODE
223 // Get native info
224 PangoFontDescription *desc = m_nativeFontInfo.description;
225
226 // init fields
227 m_faceName = wxGTK_CONV_BACK( pango_font_description_get_family( desc ) );
228
229 m_pointSize = pango_font_description_get_size( desc ) / PANGO_SCALE;
230
231 switch (pango_font_description_get_style( desc ))
232 {
233 case PANGO_STYLE_NORMAL:
234 m_style = wxFONTSTYLE_NORMAL;
235 break;
236 case PANGO_STYLE_ITALIC:
237 m_style = wxFONTSTYLE_ITALIC;
238 break;
239 case PANGO_STYLE_OBLIQUE:
240 m_style = wxFONTSTYLE_SLANT;
241 break;
242 }
243
244 switch (pango_font_description_get_weight( desc ))
245 {
246 case PANGO_WEIGHT_ULTRALIGHT:
247 m_weight = wxFONTWEIGHT_LIGHT;
248 break;
249 case PANGO_WEIGHT_LIGHT:
250 m_weight = wxFONTWEIGHT_LIGHT;
251 break;
252 case PANGO_WEIGHT_NORMAL:
253 m_weight = wxFONTWEIGHT_NORMAL;
254 break;
255 case PANGO_WEIGHT_BOLD:
256 m_weight = wxFONTWEIGHT_BOLD;
257 break;
258 case PANGO_WEIGHT_ULTRABOLD:
259 m_weight = wxFONTWEIGHT_BOLD;
260 break;
261 case PANGO_WEIGHT_HEAVY:
262 m_weight = wxFONTWEIGHT_BOLD;
263 break;
264 }
265
266 if (m_faceName == wxT("monospace"))
267 {
268 m_family = wxFONTFAMILY_TELETYPE;
269 }
270 else if (m_faceName == wxT("sans"))
271 {
272 m_family = wxFONTFAMILY_SWISS;
273 }
274 else
275 {
276 m_family = wxFONTFAMILY_UNKNOWN;
277 }
278
279 // Pango description are never underlined (?)
280 m_underlined = FALSE;
281
282 // Cannot we choose that
283 m_encoding = wxFONTENCODING_SYSTEM;
284 #else // X11
285 // get the font parameters from the XLFD
286 // -------------------------------------
287
288 m_faceName = m_nativeFontInfo.GetXFontComponent(wxXLFD_FAMILY);
289
290 m_weight = wxFONTWEIGHT_NORMAL;
291
292 wxString w = m_nativeFontInfo.GetXFontComponent(wxXLFD_WEIGHT).Upper();
293 if ( !w.empty() && w != _T('*') )
294 {
295 // the test below catches all of BOLD, EXTRABOLD, DEMIBOLD, ULTRABOLD
296 // and BLACK
297 if ( ((w[0u] == _T('B') && (!wxStrcmp(w.c_str() + 1, wxT("OLD")) ||
298 !wxStrcmp(w.c_str() + 1, wxT("LACK"))))) ||
299 wxStrstr(w.c_str() + 1, _T("BOLD")) )
300 {
301 m_weight = wxFONTWEIGHT_BOLD;
302 }
303 else if ( w == _T("LIGHT") || w == _T("THIN") )
304 {
305 m_weight = wxFONTWEIGHT_LIGHT;
306 }
307 }
308
309 switch ( wxToupper(*m_nativeFontInfo.
310 GetXFontComponent(wxXLFD_SLANT).c_str()) )
311 {
312 case _T('I'): // italique
313 m_style = wxFONTSTYLE_ITALIC;
314 break;
315
316 case _T('O'): // oblique
317 m_style = wxFONTSTYLE_SLANT;
318 break;
319
320 default:
321 m_style = wxFONTSTYLE_NORMAL;
322 }
323
324 long ptSize;
325 if ( m_nativeFontInfo.GetXFontComponent(wxXLFD_POINTSIZE).ToLong(&ptSize) )
326 {
327 // size in XLFD is in 10 point units
328 m_pointSize = (int)(ptSize / 10);
329 }
330 else
331 {
332 m_pointSize = wxDEFAULT_FONT_SIZE;
333 }
334
335 // examine the spacing: if the font is monospaced, assume wxTELETYPE
336 // family for compatibility with the old code which used it instead of
337 // IsFixedWidth()
338 if ( m_nativeFontInfo.GetXFontComponent(wxXLFD_SPACING).Upper() == _T('M') )
339 {
340 m_family = wxFONTFAMILY_TELETYPE;
341 }
342 else // not monospaceed
343 {
344 // don't even try guessing it, it doesn't work for too many fonts
345 // anyhow
346 m_family = wxFONTFAMILY_UNKNOWN;
347 }
348
349 // X fonts are never underlined...
350 m_underlined = FALSE;
351
352 // deal with font encoding
353 wxString
354 registry = m_nativeFontInfo.GetXFontComponent(wxXLFD_REGISTRY).Upper(),
355 encoding = m_nativeFontInfo.GetXFontComponent(wxXLFD_ENCODING).Upper();
356
357 if ( registry == _T("ISO8859") )
358 {
359 int cp;
360 if ( wxSscanf(encoding, wxT("%d"), &cp) == 1 )
361 {
362 m_encoding = (wxFontEncoding)(wxFONTENCODING_ISO8859_1 + cp - 1);
363 }
364 }
365 else if ( registry == _T("MICROSOFT") )
366 {
367 int cp;
368 if ( wxSscanf(encoding, wxT("cp125%d"), &cp) == 1 )
369 {
370 m_encoding = (wxFontEncoding)(wxFONTENCODING_CP1250 + cp);
371 }
372 }
373 else if ( registry == _T("KOI8") )
374 {
375 m_encoding = wxFONTENCODING_KOI8;
376 }
377 else // unknown encoding
378 {
379 // may be give a warning here? or use wxFontMapper?
380 m_encoding = wxFONTENCODING_SYSTEM;
381 }
382 #endif // Pango/X11
383 }
384
385 wxFontRefData::wxFontRefData( const wxFontRefData& data )
386 : wxObjectRefData()
387 {
388 m_pointSize = data.m_pointSize;
389 m_family = data.m_family;
390 m_style = data.m_style;
391 m_weight = data.m_weight;
392
393 m_underlined = data.m_underlined;
394
395 m_faceName = data.m_faceName;
396 m_encoding = data.m_encoding;
397
398 m_noAA = data.m_noAA;
399
400 m_nativeFontInfo = data.m_nativeFontInfo;
401 }
402
403 wxFontRefData::wxFontRefData(int size, int family, int style,
404 int weight, bool underlined,
405 const wxString& faceName,
406 wxFontEncoding encoding)
407 {
408 Init(size, family, style, weight, underlined, faceName, encoding);
409 }
410
411 wxFontRefData::wxFontRefData(const wxString& fontname)
412 {
413 // VZ: FromString() should really work in both cases, doesn't it?
414 #if wxUSE_UNICODE
415 m_nativeFontInfo.FromString( fontname );
416 #else
417 m_nativeFontInfo.SetXFontName(fontname);
418 #endif
419
420 InitFromNative();
421 }
422
423 void wxFontRefData::ClearX11Fonts()
424 {
425 #if wxUSE_UNICODE
426 #else
427 wxList::compatibility_iterator node = m_fonts.GetFirst();
428 while (node)
429 {
430 wxXFont* f = (wxXFont*) node->GetData();
431 delete f;
432 node = node->GetNext();
433 }
434 m_fonts.Clear();
435 #endif
436 }
437
438 wxFontRefData::~wxFontRefData()
439 {
440 ClearX11Fonts();
441 }
442
443 // ----------------------------------------------------------------------------
444 // wxFontRefData SetXXX()
445 // ----------------------------------------------------------------------------
446
447 void wxFontRefData::SetPointSize(int pointSize)
448 {
449 m_pointSize = pointSize;
450
451 #if wxUSE_UNICODE
452 // Get native info
453 PangoFontDescription *desc = m_nativeFontInfo.description;
454
455 pango_font_description_set_size( desc, m_pointSize * PANGO_SCALE );
456 #endif
457 }
458
459 void wxFontRefData::SetFamily(int family)
460 {
461 m_family = family;
462
463 // TODO: what are we supposed to do with m_nativeFontInfo here?
464 }
465
466 void wxFontRefData::SetStyle(int style)
467 {
468 m_style = style;
469
470 #if wxUSE_UNICODE
471 // Get native info
472 PangoFontDescription *desc = m_nativeFontInfo.description;
473
474 switch ( style )
475 {
476 case wxFONTSTYLE_ITALIC:
477 pango_font_description_set_style( desc, PANGO_STYLE_ITALIC );
478 break;
479 case wxFONTSTYLE_SLANT:
480 pango_font_description_set_style( desc, PANGO_STYLE_OBLIQUE );
481 break;
482 default:
483 wxFAIL_MSG( _T("unknown font style") );
484 // fall through
485 case wxFONTSTYLE_NORMAL:
486 pango_font_description_set_style( desc, PANGO_STYLE_NORMAL );
487 break;
488 }
489 #endif
490 }
491
492 void wxFontRefData::SetWeight(int weight)
493 {
494 m_weight = weight;
495 }
496
497 void wxFontRefData::SetUnderlined(bool underlined)
498 {
499 m_underlined = underlined;
500
501 // the XLFD doesn't have "underlined" field anyhow
502 }
503
504 void wxFontRefData::SetFaceName(const wxString& facename)
505 {
506 m_faceName = facename;
507 }
508
509 void wxFontRefData::SetEncoding(wxFontEncoding encoding)
510 {
511 m_encoding = encoding;
512 }
513
514 void wxFontRefData::SetNativeFontInfo(const wxNativeFontInfo& info)
515 {
516 // previously cached fonts shouldn't be used
517 ClearX11Fonts();
518
519 m_nativeFontInfo = info;
520
521 // set all the other font parameters from the native font info
522 InitFromNative();
523 }
524
525 // ----------------------------------------------------------------------------
526 // wxFont
527 // ----------------------------------------------------------------------------
528
529 void wxFont::Init()
530 {
531 }
532
533 wxFont::wxFont(const wxNativeFontInfo& info)
534 {
535 Init();
536
537 #if wxUSE_UNICODE
538 Create( info.GetPointSize(),
539 info.GetFamily(),
540 info.GetStyle(),
541 info.GetWeight(),
542 info.GetUnderlined(),
543 info.GetFaceName(),
544 info.GetEncoding() );
545 #else
546 (void) Create(info.GetXFontName());
547 #endif
548 }
549
550 bool wxFont::Create(int pointSize,
551 int family,
552 int style,
553 int weight,
554 bool underlined,
555 const wxString& faceName,
556 wxFontEncoding encoding)
557 {
558 UnRef();
559
560 m_refData = new wxFontRefData(pointSize, family, style, weight,
561 underlined, faceName, encoding);
562
563 return TRUE;
564 }
565
566 #if !wxUSE_UNICODE
567
568 bool wxFont::Create(const wxString& fontname, wxFontEncoding enc)
569 {
570 if( !fontname )
571 {
572 *this = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT);
573 return TRUE;
574 }
575
576 m_refData = new wxFontRefData();
577
578 M_FONTDATA->m_nativeFontInfo.SetXFontName(fontname); // X font name
579
580 wxString tmp;
581
582 wxStringTokenizer tn( fontname, wxT("-") );
583
584 tn.GetNextToken(); // skip initial empty token
585 tn.GetNextToken(); // foundry
586
587
588 M_FONTDATA->m_faceName = tn.GetNextToken(); // family
589
590 tmp = tn.GetNextToken().MakeUpper(); // weight
591 if (tmp == wxT("BOLD")) M_FONTDATA->m_weight = wxBOLD;
592 if (tmp == wxT("BLACK")) M_FONTDATA->m_weight = wxBOLD;
593 if (tmp == wxT("EXTRABOLD")) M_FONTDATA->m_weight = wxBOLD;
594 if (tmp == wxT("DEMIBOLD")) M_FONTDATA->m_weight = wxBOLD;
595 if (tmp == wxT("ULTRABOLD")) M_FONTDATA->m_weight = wxBOLD;
596
597 if (tmp == wxT("LIGHT")) M_FONTDATA->m_weight = wxLIGHT;
598 if (tmp == wxT("THIN")) M_FONTDATA->m_weight = wxLIGHT;
599
600 tmp = tn.GetNextToken().MakeUpper(); // slant
601 if (tmp == wxT("I")) M_FONTDATA->m_style = wxITALIC;
602 if (tmp == wxT("O")) M_FONTDATA->m_style = wxITALIC;
603
604 tn.GetNextToken(); // set width
605 tn.GetNextToken(); // add. style
606 tn.GetNextToken(); // pixel size
607
608 tmp = tn.GetNextToken(); // pointsize
609 if (tmp != wxT("*"))
610 {
611 long num = wxStrtol (tmp.c_str(), (wxChar **) NULL, 10);
612 M_FONTDATA->m_pointSize = (int)(num / 10);
613 }
614
615 tn.GetNextToken(); // x-res
616 tn.GetNextToken(); // y-res
617
618 tmp = tn.GetNextToken().MakeUpper(); // spacing
619
620 if (tmp == wxT("M"))
621 M_FONTDATA->m_family = wxMODERN;
622 else if (M_FONTDATA->m_faceName == wxT("TIMES"))
623 M_FONTDATA->m_family = wxROMAN;
624 else if (M_FONTDATA->m_faceName == wxT("HELVETICA"))
625 M_FONTDATA->m_family = wxSWISS;
626 else if (M_FONTDATA->m_faceName == wxT("LUCIDATYPEWRITER"))
627 M_FONTDATA->m_family = wxTELETYPE;
628 else if (M_FONTDATA->m_faceName == wxT("LUCIDA"))
629 M_FONTDATA->m_family = wxDECORATIVE;
630 else if (M_FONTDATA->m_faceName == wxT("UTOPIA"))
631 M_FONTDATA->m_family = wxSCRIPT;
632
633 tn.GetNextToken(); // avg width
634
635 // deal with font encoding
636 M_FONTDATA->m_encoding = enc;
637 if ( M_FONTDATA->m_encoding == wxFONTENCODING_SYSTEM )
638 {
639 wxString registry = tn.GetNextToken().MakeUpper(),
640 encoding = tn.GetNextToken().MakeUpper();
641
642 if ( registry == _T("ISO8859") )
643 {
644 int cp;
645 if ( wxSscanf(encoding, wxT("%d"), &cp) == 1 )
646 {
647 M_FONTDATA->m_encoding =
648 (wxFontEncoding)(wxFONTENCODING_ISO8859_1 + cp - 1);
649 }
650 }
651 else if ( registry == _T("MICROSOFT") )
652 {
653 int cp;
654 if ( wxSscanf(encoding, wxT("cp125%d"), &cp) == 1 )
655 {
656 M_FONTDATA->m_encoding =
657 (wxFontEncoding)(wxFONTENCODING_CP1250 + cp);
658 }
659 }
660 else if ( registry == _T("KOI8") )
661 {
662 M_FONTDATA->m_encoding = wxFONTENCODING_KOI8;
663 }
664 //else: unknown encoding - may be give a warning here?
665 else
666 return FALSE;
667 }
668 return TRUE;
669 }
670 #endif // !wxUSE_UNICODE
671
672 wxFont::~wxFont()
673 {
674 }
675
676 // ----------------------------------------------------------------------------
677 // change the font attributes
678 // ----------------------------------------------------------------------------
679
680 void wxFont::Unshare()
681 {
682 // Don't change shared data
683 if (!m_refData)
684 {
685 m_refData = new wxFontRefData();
686 }
687 else
688 {
689 wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData);
690 UnRef();
691 m_refData = ref;
692 }
693 }
694
695 // ----------------------------------------------------------------------------
696 // accessors
697 // ----------------------------------------------------------------------------
698
699 int wxFont::GetPointSize() const
700 {
701 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
702
703 return M_FONTDATA->m_pointSize;
704 }
705
706 wxString wxFont::GetFaceName() const
707 {
708 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
709
710 return M_FONTDATA->m_faceName;
711 }
712
713 int wxFont::GetFamily() const
714 {
715 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
716
717 return M_FONTDATA->m_family;
718 }
719
720 int wxFont::GetStyle() const
721 {
722 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
723
724 return M_FONTDATA->m_style;
725 }
726
727 int wxFont::GetWeight() const
728 {
729 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
730
731 return M_FONTDATA->m_weight;
732 }
733
734 bool wxFont::GetUnderlined() const
735 {
736 wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") );
737
738 return M_FONTDATA->m_underlined;
739 }
740
741 wxFontEncoding wxFont::GetEncoding() const
742 {
743 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") );
744
745 return M_FONTDATA->m_encoding;
746 }
747
748 bool wxFont::GetNoAntiAliasing() const
749 {
750 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") );
751
752 return M_FONTDATA->m_noAA;
753 }
754
755 const wxNativeFontInfo *wxFont::GetNativeFontInfo() const
756 {
757 wxCHECK_MSG( Ok(), (wxNativeFontInfo *)NULL, wxT("invalid font") );
758
759 #if wxUSE_UNICODE
760 #else
761 if ( M_FONTDATA->m_nativeFontInfo.GetXFontName().empty() )
762 GetInternalFont();
763 #endif
764
765 return &(M_FONTDATA->m_nativeFontInfo);
766 }
767
768 bool wxFont::IsFixedWidth() const
769 {
770 wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") );
771
772 #if wxUSE_UNICODE
773 return wxFontBase::IsFixedWidth();
774 #else
775 // Robert, is this right? HasNativeFont doesn't exist.
776 if ( TRUE )
777 // if ( M_FONTDATA->HasNativeFont() )
778 {
779 // the monospace fonts are supposed to have "M" in the spacing field
780 wxString spacing = M_FONTDATA->
781 m_nativeFontInfo.GetXFontComponent(wxXLFD_SPACING);
782
783 return spacing.Upper() == _T('M');
784 }
785 // Unreaceable code for now
786 // return wxFontBase::IsFixedWidth();
787 #endif
788
789 }
790
791 // ----------------------------------------------------------------------------
792 // change font attributes
793 // ----------------------------------------------------------------------------
794
795 void wxFont::SetPointSize(int pointSize)
796 {
797 Unshare();
798
799 M_FONTDATA->SetPointSize(pointSize);
800 }
801
802 void wxFont::SetFamily(int family)
803 {
804 Unshare();
805
806 M_FONTDATA->SetFamily(family);
807 }
808
809 void wxFont::SetStyle(int style)
810 {
811 Unshare();
812
813 M_FONTDATA->SetStyle(style);
814 }
815
816 void wxFont::SetWeight(int weight)
817 {
818 Unshare();
819
820 M_FONTDATA->SetWeight(weight);
821 }
822
823 void wxFont::SetFaceName(const wxString& faceName)
824 {
825 Unshare();
826
827 M_FONTDATA->SetFaceName(faceName);
828 }
829
830 void wxFont::SetUnderlined(bool underlined)
831 {
832 Unshare();
833
834 M_FONTDATA->SetUnderlined(underlined);
835 }
836
837 void wxFont::SetEncoding(wxFontEncoding encoding)
838 {
839 Unshare();
840
841 M_FONTDATA->SetEncoding(encoding);
842 }
843
844 void wxFont::DoSetNativeFontInfo( const wxNativeFontInfo& info )
845 {
846 Unshare();
847
848 M_FONTDATA->SetNativeFontInfo( info );
849 }
850
851 void wxFont::SetNoAntiAliasing( bool no )
852 {
853 Unshare();
854
855 M_FONTDATA->SetNoAntiAliasing( no );
856 }
857
858 #if wxUSE_UNICODE
859 #else
860
861 // ----------------------------------------------------------------------------
862 // X11 implementation
863 // ----------------------------------------------------------------------------
864
865 // Find an existing, or create a new, XFontStruct
866 // based on this wxFont and the given scale. Append the
867 // font to list in the private data for future reference.
868 wxXFont* wxFont::GetInternalFont(double scale, WXDisplay* display) const
869 {
870 if ( !Ok() )
871 return (wxXFont *)NULL;
872
873 long intScale = long(scale * 100.0 + 0.5); // key for wxXFont
874 int pointSize = (M_FONTDATA->m_pointSize * 10 * intScale) / 100;
875
876 // search existing fonts first
877 wxList::compatibility_iterator node = M_FONTDATA->m_fonts.GetFirst();
878 while (node)
879 {
880 wxXFont* f = (wxXFont*) node->GetData();
881 if ((!display || (f->m_display == display)) && (f->m_scale == intScale))
882 return f;
883 node = node->GetNext();
884 }
885
886 wxString xFontName = M_FONTDATA->m_nativeFontInfo.GetXFontName();
887 if (xFontName == "-*-*-*-*-*--*-*-*-*-*-*-*-*")
888 // wxFont constructor not called with native font info parameter => take M_FONTDATA values
889 xFontName.Clear();
890
891 // not found, create a new one
892 XFontStruct *font = (XFontStruct *)
893 wxLoadQueryNearestFont(pointSize,
894 M_FONTDATA->m_family,
895 M_FONTDATA->m_style,
896 M_FONTDATA->m_weight,
897 M_FONTDATA->m_underlined,
898 wxT(""),
899 M_FONTDATA->m_encoding,
900 & xFontName);
901
902 if ( !font )
903 {
904 wxFAIL_MSG( wxT("Could not allocate even a default font -- something is wrong.") );
905
906 return (wxXFont*) NULL;
907 }
908
909 wxXFont* f = new wxXFont;
910 f->m_fontStruct = (WXFontStructPtr)font;
911 f->m_display = ( display ? display : wxGetDisplay() );
912 f->m_scale = intScale;
913 M_FONTDATA->m_fonts.Append(f);
914
915 return f;
916 }
917
918 WXFontStructPtr wxFont::GetFontStruct(double scale, WXDisplay* display) const
919 {
920 wxXFont* f = GetInternalFont(scale, display);
921
922 return (f ? f->m_fontStruct : (WXFontStructPtr) 0);
923 }
924
925 #endif