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