]> git.saurik.com Git - wxWidgets.git/blame - src/common/fontcmn.cpp
fixed wxXmlResource::Load's detection of filenames to be done as early as possible
[wxWidgets.git] / src / common / fontcmn.cpp
CommitLineData
8f7fa6f8 1/////////////////////////////////////////////////////////////////////////////
da80ae71 2// Name: src/common/fontcmn.cpp
0c5d3e1c
VZ
3// Purpose: implementation of wxFontBase methods
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 20.09.99
7// RCS-ID: $Id$
77ffb593 8// Copyright: (c) wxWidgets team
65571936 9// Licence: wxWindows licence
0c5d3e1c
VZ
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
da80ae71 24 #pragma hdrstop
0c5d3e1c
VZ
25#endif
26
da80ae71
WS
27#include "wx/font.h"
28
0c5d3e1c 29#ifndef WX_PRECOMP
452aa069 30 #include "wx/dc.h"
9342fdbe
VZ
31 #include "wx/intl.h"
32 #include "wx/dcscreen.h"
8d3ba63b 33 #include "wx/log.h"
dd05139a 34 #include "wx/gdicmn.h"
0c5d3e1c
VZ
35#endif // WX_PRECOMP
36
82ef81ed 37#if defined(__WXMSW__)
7bd236e6
WS
38 #include "wx/msw/private.h" // includes windows.h for LOGFONT
39 #include "wx/msw/winundef.h"
1c193821
JS
40#endif
41
76e23cdb 42#include "wx/fontutil.h" // for wxNativeFontInfo
ab5fe833 43#include "wx/fontmap.h"
85ab460e 44#include "wx/fontenum.h"
76e23cdb 45
30764ab5
VZ
46#include "wx/tokenzr.h"
47
0c5d3e1c
VZ
48// ============================================================================
49// implementation
50// ============================================================================
51
544229d1
VZ
52// ----------------------------------------------------------------------------
53// helper functions
54// ----------------------------------------------------------------------------
55
8093538c 56static void AdjustFontSize(wxFont& font, wxDC& dc, const wxSize& pixelSize)
544229d1 57{
8093538c
VZ
58 int largestGood = 0;
59 int smallestBad = 0;
544229d1
VZ
60
61 bool initialGoodFound = false;
62 bool initialBadFound = false;
63
d141b218
DS
64 // NB: this assignment was separated from the variable definition
65 // in order to fix a gcc v3.3.3 compiler crash
e822d1bd 66 int currentSize = font.GetPointSize();
544229d1
VZ
67 while (currentSize > 0)
68 {
69 dc.SetFont(font);
70
71 // if currentSize (in points) results in a font that is smaller
72 // than required by pixelSize it is considered a good size
73 if (dc.GetCharHeight() <= pixelSize.GetHeight() &&
74 (!pixelSize.GetWidth() ||
75 dc.GetCharWidth() <= pixelSize.GetWidth()))
76 {
77 largestGood = currentSize;
78 initialGoodFound = true;
79 }
80 else
81 {
82 smallestBad = currentSize;
83 initialBadFound = true;
84 }
85 if (!initialGoodFound)
86 {
87 currentSize /= 2;
88 }
89 else if (!initialBadFound)
90 {
91 currentSize *= 2;
92 }
93 else
94 {
95 int distance = smallestBad - largestGood;
96 if (distance == 1)
97 break;
98
99 currentSize = largestGood + distance / 2;
100 }
101
102 font.SetPointSize(currentSize);
103 }
104
105 if (currentSize != largestGood)
106 font.SetPointSize(largestGood);
107}
108
0c5d3e1c
VZ
109// ----------------------------------------------------------------------------
110// wxFontBase
111// ----------------------------------------------------------------------------
112
113wxFontEncoding wxFontBase::ms_encodingDefault = wxFONTENCODING_SYSTEM;
114
cafbf6fb
VZ
115/* static */
116void wxFontBase::SetDefaultEncoding(wxFontEncoding encoding)
117{
118 // GetDefaultEncoding() should return something != wxFONTENCODING_DEFAULT
119 // and, besides, using this value here doesn't make any sense
120 wxCHECK_RET( encoding != wxFONTENCODING_DEFAULT,
121 _T("can't set default encoding to wxFONTENCODING_DEFAULT") );
122
123 ms_encodingDefault = encoding;
124}
125
799ea011
GD
126wxFontBase::~wxFontBase()
127{
128 // this destructor is required for Darwin
129}
130
7beba2fc 131/* static */
0c5d3e1c 132wxFont *wxFontBase::New(int size,
0c14b6c3
FM
133 wxFontFamily family,
134 wxFontStyle style,
135 wxFontWeight weight,
0c5d3e1c
VZ
136 bool underlined,
137 const wxString& face,
138 wxFontEncoding encoding)
139{
140 return new wxFont(size, family, style, weight, underlined, face, encoding);
141}
142
544229d1 143static inline int flags2Style(int flags)
01cb1c26 144{
544229d1 145 return flags & wxFONTFLAG_ITALIC
01cb1c26
VZ
146 ? wxFONTSTYLE_ITALIC
147 : flags & wxFONTFLAG_SLANT
148 ? wxFONTSTYLE_SLANT
544229d1
VZ
149 : wxFONTSTYLE_NORMAL;
150}
151
152static inline int flags2Weight(int flags)
153{
154 return flags & wxFONTFLAG_LIGHT
01cb1c26
VZ
155 ? wxFONTWEIGHT_LIGHT
156 : flags & wxFONTFLAG_BOLD
157 ? wxFONTWEIGHT_BOLD
544229d1
VZ
158 : wxFONTWEIGHT_NORMAL;
159}
160
161static inline bool flags2Underlined(int flags)
162{
163 return (flags & wxFONTFLAG_UNDERLINED) != 0;
164}
165
166/* static */
167wxFont *wxFontBase::New(int pointSize,
168 wxFontFamily family,
169 int flags,
170 const wxString& face,
171 wxFontEncoding encoding)
172{
173 return New(pointSize, family, flags2Style(flags), flags2Weight(flags),
174 flags2Underlined(flags), face, encoding);
175}
176
177/* static */
178wxFont *wxFontBase::New(const wxSize& pixelSize,
0c14b6c3
FM
179 wxFontFamily family,
180 wxFontStyle style,
181 wxFontWeight weight,
544229d1
VZ
182 bool underlined,
183 const wxString& face,
184 wxFontEncoding encoding)
185{
186#if defined(__WXMSW__)
187 return new wxFont(pixelSize, family, style, weight, underlined,
188 face, encoding);
189#else
9342fdbe 190 wxFont *self = New(10, family, style, weight, underlined, face, encoding);
544229d1 191 wxScreenDC dc;
9342fdbe
VZ
192 AdjustFontSize(*(wxFont *)self, dc, pixelSize);
193 return self;
544229d1
VZ
194#endif
195}
196
197/* static */
198wxFont *wxFontBase::New(const wxSize& pixelSize,
199 wxFontFamily family,
200 int flags,
201 const wxString& face,
202 wxFontEncoding encoding)
203{
204 return New(pixelSize, family, flags2Style(flags), flags2Weight(flags),
205 flags2Underlined(flags), face, encoding);
206}
207
208wxSize wxFontBase::GetPixelSize() const
209{
210 wxScreenDC dc;
211 dc.SetFont(*(wxFont *)this);
212 return wxSize(dc.GetCharWidth(), dc.GetCharHeight());
213}
214
215bool wxFontBase::IsUsingSizeInPixels() const
216{
217 return false;
218}
219
220void wxFontBase::SetPixelSize( const wxSize& pixelSize )
221{
222 wxScreenDC dc;
223 AdjustFontSize(*(wxFont *)this, dc, pixelSize);
01cb1c26
VZ
224}
225
30764ab5
VZ
226/* static */
227wxFont *wxFontBase::New(const wxNativeFontInfo& info)
228{
229 return new wxFont(info);
230}
231
7826e2dd
VZ
232/* static */
233wxFont *wxFontBase::New(const wxString& strNativeFontDesc)
30764ab5 234{
30764ab5 235 wxNativeFontInfo fontInfo;
7826e2dd 236 if ( !fontInfo.FromString(strNativeFontDesc) )
09fcd889 237 return new wxFont(*wxNORMAL_FONT);
7826e2dd
VZ
238
239 return New(fontInfo);
240}
241
53f6aab7
VZ
242bool wxFontBase::IsFixedWidth() const
243{
244 return GetFamily() == wxFONTFAMILY_TELETYPE;
245}
246
9045ad9d 247void wxFontBase::DoSetNativeFontInfo(const wxNativeFontInfo& info)
30764ab5 248{
ab5fe833 249#ifdef wxNO_NATIVE_FONTINFO
30764ab5
VZ
250 SetPointSize(info.pointSize);
251 SetFamily(info.family);
252 SetStyle(info.style);
253 SetWeight(info.weight);
254 SetUnderlined(info.underlined);
255 SetFaceName(info.faceName);
256 SetEncoding(info.encoding);
33ac7e6f 257#else
1e6feb95 258 (void)info;
30764ab5
VZ
259#endif
260}
261
7826e2dd
VZ
262wxString wxFontBase::GetNativeFontInfoDesc() const
263{
264 wxString fontDesc;
3bf5a59b 265 const wxNativeFontInfo *fontInfo = GetNativeFontInfo();
7826e2dd
VZ
266 if ( fontInfo )
267 {
268 fontDesc = fontInfo->ToString();
dd05139a 269 wxASSERT_MSG(!fontDesc.empty(), wxT("This should be a non-empty string!"));
85ab460e
VZ
270 }
271 else
272 {
7bd236e6 273 wxFAIL_MSG(wxT("Derived class should have created the wxNativeFontInfo!"));
7826e2dd
VZ
274 }
275
276 return fontDesc;
277}
278
ab5fe833
VZ
279wxString wxFontBase::GetNativeFontInfoUserDesc() const
280{
281 wxString fontDesc;
3bf5a59b 282 const wxNativeFontInfo *fontInfo = GetNativeFontInfo();
ab5fe833
VZ
283 if ( fontInfo )
284 {
285 fontDesc = fontInfo->ToUserString();
dd05139a 286 wxASSERT_MSG(!fontDesc.empty(), wxT("This should be a non-empty string!"));
85ab460e
VZ
287 }
288 else
289 {
7456f382 290 wxFAIL_MSG(wxT("Derived class should have created the wxNativeFontInfo!"));
ab5fe833
VZ
291 }
292
293 return fontDesc;
294}
295
85ab460e 296bool wxFontBase::SetNativeFontInfo(const wxString& info)
31d1b66e
VZ
297{
298 wxNativeFontInfo fontInfo;
299 if ( !info.empty() && fontInfo.FromString(info) )
300 {
301 SetNativeFontInfo(fontInfo);
85ab460e 302 return true;
31d1b66e 303 }
85ab460e 304
85ab460e 305 return false;
31d1b66e
VZ
306}
307
85ab460e 308bool wxFontBase::SetNativeFontInfoUserDesc(const wxString& info)
ab5fe833
VZ
309{
310 wxNativeFontInfo fontInfo;
311 if ( !info.empty() && fontInfo.FromUserString(info) )
312 {
313 SetNativeFontInfo(fontInfo);
85ab460e 314 return true;
ab5fe833 315 }
85ab460e 316
85ab460e 317 return false;
ab5fe833
VZ
318}
319
0c5d3e1c
VZ
320bool wxFontBase::operator==(const wxFont& font) const
321{
8bf30fe9
VZ
322 // either it is the same font, i.e. they share the same common data or they
323 // have different ref datas but still describe the same font
a38cd629 324 return IsSameAs(font) ||
8bf30fe9
VZ
325 (
326 Ok() == font.Ok() &&
327 GetPointSize() == font.GetPointSize() &&
82d0e7fe
VZ
328 // in wxGTK1 GetPixelSize() calls GetInternalFont() which uses
329 // operator==() resulting in infinite recursion so we can't use it
330 // in that port
331#if !defined(__WXGTK__) || defined(__WXGTK20__)
cc3de8a3 332 GetPixelSize() == font.GetPixelSize() &&
82d0e7fe 333#endif
8bf30fe9
VZ
334 GetFamily() == font.GetFamily() &&
335 GetStyle() == font.GetStyle() &&
e6adf058 336 GetWeight() == font.GetWeight() &&
8bf30fe9 337 GetUnderlined() == font.GetUnderlined() &&
85ab460e 338 GetFaceName().IsSameAs(font.GetFaceName(), false) &&
8bf30fe9
VZ
339 GetEncoding() == font.GetEncoding()
340 );
0c5d3e1c
VZ
341}
342
0c5d3e1c
VZ
343wxString wxFontBase::GetFamilyString() const
344{
223d09f6 345 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
0c5d3e1c
VZ
346
347 switch ( GetFamily() )
348 {
223d09f6
KB
349 case wxDECORATIVE: return wxT("wxDECORATIVE");
350 case wxROMAN: return wxT("wxROMAN");
351 case wxSCRIPT: return wxT("wxSCRIPT");
352 case wxSWISS: return wxT("wxSWISS");
353 case wxMODERN: return wxT("wxMODERN");
354 case wxTELETYPE: return wxT("wxTELETYPE");
355 default: return wxT("wxDEFAULT");
0c5d3e1c
VZ
356 }
357}
358
359wxString wxFontBase::GetStyleString() const
360{
223d09f6 361 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
0c5d3e1c
VZ
362
363 switch ( GetStyle() )
364 {
223d09f6
KB
365 case wxNORMAL: return wxT("wxNORMAL");
366 case wxSLANT: return wxT("wxSLANT");
367 case wxITALIC: return wxT("wxITALIC");
368 default: return wxT("wxDEFAULT");
0c5d3e1c
VZ
369 }
370}
371
372wxString wxFontBase::GetWeightString() const
373{
223d09f6 374 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
0c5d3e1c
VZ
375
376 switch ( GetWeight() )
377 {
223d09f6
KB
378 case wxNORMAL: return wxT("wxNORMAL");
379 case wxBOLD: return wxT("wxBOLD");
380 case wxLIGHT: return wxT("wxLIGHT");
381 default: return wxT("wxDEFAULT");
0c5d3e1c
VZ
382 }
383}
384
ff427585 385bool wxFontBase::SetFaceName(const wxString& facename)
85ab460e 386{
ff427585 387#if wxUSE_FONTENUM
85ab460e
VZ
388 if (!wxFontEnumerator::IsValidFacename(facename))
389 {
390 UnRef(); // make Ok() return false
391 return false;
392 }
ff427585
VZ
393#else // !wxUSE_FONTENUM
394 wxUnusedVar(facename);
395#endif // wxUSE_FONTENUM/!wxUSE_FONTENUM
85ab460e
VZ
396
397 return true;
398}
399
400
30764ab5
VZ
401// ----------------------------------------------------------------------------
402// wxNativeFontInfo
403// ----------------------------------------------------------------------------
404
85ab460e 405// Up to now, there are no native implementations of this function:
ff427585 406void wxNativeFontInfo::SetFaceName(const wxArrayString& facenames)
85ab460e 407{
ff427585 408#if wxUSE_FONTENUM
85ab460e
VZ
409 for (size_t i=0; i < facenames.GetCount(); i++)
410 {
411 if (wxFontEnumerator::IsValidFacename(facenames[i]))
412 {
413 SetFaceName(facenames[i]);
414 return;
415 }
416 }
417
418 // set the first valid facename we can find on this system
419 wxString validfacename = wxFontEnumerator::GetFacenames().Item(0);
420 wxLogTrace(wxT("font"), wxT("Falling back to '%s'"), validfacename.c_str());
421 SetFaceName(validfacename);
ff427585
VZ
422#else // !wxUSE_FONTENUM
423 SetFaceName(facenames[0]);
424#endif // wxUSE_FONTENUM/!wxUSE_FONTENUM
85ab460e
VZ
425}
426
427
ab5fe833
VZ
428#ifdef wxNO_NATIVE_FONTINFO
429
30764ab5
VZ
430// These are the generic forms of FromString()/ToString.
431//
432// convert to/from the string representation: format is
09fcd889 433// version;pointsize;family;style;weight;underlined;facename;encoding
30764ab5
VZ
434
435bool wxNativeFontInfo::FromString(const wxString& s)
436{
437 long l;
438
439 wxStringTokenizer tokenizer(s, _T(";"));
440
441 wxString token = tokenizer.GetNextToken();
09fcd889
VZ
442 //
443 // Ignore the version for now
444 //
33ac7e6f 445
09fcd889 446 token = tokenizer.GetNextToken();
30764ab5 447 if ( !token.ToLong(&l) )
a62848fd 448 return false;
30764ab5
VZ
449 pointSize = (int)l;
450
451 token = tokenizer.GetNextToken();
452 if ( !token.ToLong(&l) )
a62848fd 453 return false;
f7b301fa 454 family = (wxFontFamily)l;
30764ab5
VZ
455
456 token = tokenizer.GetNextToken();
457 if ( !token.ToLong(&l) )
a62848fd 458 return false;
ab5fe833 459 style = (wxFontStyle)l;
30764ab5
VZ
460
461 token = tokenizer.GetNextToken();
462 if ( !token.ToLong(&l) )
a62848fd 463 return false;
ab5fe833 464 weight = (wxFontWeight)l;
30764ab5
VZ
465
466 token = tokenizer.GetNextToken();
467 if ( !token.ToLong(&l) )
a62848fd 468 return false;
189e08b4 469 underlined = l != 0;
30764ab5
VZ
470
471 faceName = tokenizer.GetNextToken();
0a9f0ef7
JS
472
473#ifndef __WXMAC__
30764ab5 474 if( !faceName )
a62848fd 475 return false;
0a9f0ef7 476#endif
30764ab5
VZ
477
478 token = tokenizer.GetNextToken();
479 if ( !token.ToLong(&l) )
a62848fd 480 return false;
30764ab5
VZ
481 encoding = (wxFontEncoding)l;
482
a62848fd 483 return true;
30764ab5
VZ
484}
485
486wxString wxNativeFontInfo::ToString() const
487{
488 wxString s;
489
09fcd889
VZ
490 s.Printf(_T("%d;%d;%d;%d;%d;%d;%s;%d"),
491 0, // version
30764ab5
VZ
492 pointSize,
493 family,
ab5fe833
VZ
494 (int)style,
495 (int)weight,
30764ab5
VZ
496 underlined,
497 faceName.GetData(),
498 (int)encoding);
499
500 return s;
501}
502
ab5fe833
VZ
503void wxNativeFontInfo::Init()
504{
3bf5a59b 505 pointSize = 0;
ab5fe833
VZ
506 family = wxFONTFAMILY_DEFAULT;
507 style = wxFONTSTYLE_NORMAL;
508 weight = wxFONTWEIGHT_NORMAL;
a62848fd 509 underlined = false;
ab5fe833
VZ
510 faceName.clear();
511 encoding = wxFONTENCODING_DEFAULT;
512}
513
514int wxNativeFontInfo::GetPointSize() const
515{
516 return pointSize;
517}
518
519wxFontStyle wxNativeFontInfo::GetStyle() const
520{
521 return style;
522}
523
524wxFontWeight wxNativeFontInfo::GetWeight() const
525{
526 return weight;
527}
528
529bool wxNativeFontInfo::GetUnderlined() const
530{
531 return underlined;
532}
533
534wxString wxNativeFontInfo::GetFaceName() const
535{
536 return faceName;
537}
538
7936354d
VZ
539wxFontFamily wxNativeFontInfo::GetFamily() const
540{
541 return family;
542}
543
ab5fe833
VZ
544wxFontEncoding wxNativeFontInfo::GetEncoding() const
545{
546 return encoding;
547}
548
549void wxNativeFontInfo::SetPointSize(int pointsize)
550{
551 pointSize = pointsize;
552}
553
554void wxNativeFontInfo::SetStyle(wxFontStyle style_)
555{
556 style = style_;
557}
558
559void wxNativeFontInfo::SetWeight(wxFontWeight weight_)
560{
561 weight = weight_;
562}
563
564void wxNativeFontInfo::SetUnderlined(bool underlined_)
565{
566 underlined = underlined_;
567}
568
85ab460e 569bool wxNativeFontInfo::SetFaceName(const wxString& facename_)
ab5fe833 570{
f7b301fa 571 faceName = facename_;
85ab460e 572 return true;
ab5fe833
VZ
573}
574
3f1d1373 575void wxNativeFontInfo::SetFamily(wxFontFamily family_)
7936354d
VZ
576{
577 family = family_;
578}
579
ab5fe833
VZ
580void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding_)
581{
582 encoding = encoding_;
583}
584
7826e2dd 585#endif // generic wxNativeFontInfo implementation
30764ab5 586
ab5fe833
VZ
587// conversion to/from user-readable string: this is used in the generic
588// versions and under MSW as well because there is no standard font description
589// format there anyhow (but there is a well-defined standard for X11 fonts used
590// by wxGTK and wxMotif)
591
0eb529d9 592#if defined(wxNO_NATIVE_FONTINFO) || defined(__WXMSW__) || defined (__WXPM__)
ab5fe833
VZ
593
594wxString wxNativeFontInfo::ToUserString() const
595{
596 wxString desc;
597
598 // first put the adjectives, if any - this is English-centric, of course,
599 // but what else can we do?
600 if ( GetUnderlined() )
601 {
85ab460e 602 desc << _("underlined");
ab5fe833
VZ
603 }
604
605 switch ( GetWeight() )
606 {
607 default:
608 wxFAIL_MSG( _T("unknown font weight") );
609 // fall through
610
611 case wxFONTWEIGHT_NORMAL:
612 break;
613
614 case wxFONTWEIGHT_LIGHT:
85ab460e 615 desc << _(" light");
ab5fe833
VZ
616 break;
617
618 case wxFONTWEIGHT_BOLD:
85ab460e 619 desc << _(" bold");
ab5fe833
VZ
620 break;
621 }
622
623 switch ( GetStyle() )
624 {
625 default:
626 wxFAIL_MSG( _T("unknown font style") );
627 // fall through
628
629 case wxFONTSTYLE_NORMAL:
630 break;
631
632 // we don't distinguish between the two for now anyhow...
633 case wxFONTSTYLE_ITALIC:
634 case wxFONTSTYLE_SLANT:
85ab460e 635 desc << _(" italic");
ab5fe833
VZ
636 break;
637 }
638
a9249b2e
VZ
639 wxString face = GetFaceName();
640 if ( !face.empty() )
ab5fe833 641 {
a9249b2e 642 desc << _T(' ') << face;
ab5fe833
VZ
643 }
644
a9249b2e
VZ
645 int size = GetPointSize();
646 if ( size != wxNORMAL_FONT->GetPointSize() )
ab5fe833 647 {
a9249b2e 648 desc << _T(' ') << size;
ab5fe833 649 }
a9249b2e 650
e7e52b6d 651#if wxUSE_FONTMAP
a9249b2e
VZ
652 wxFontEncoding enc = GetEncoding();
653 if ( enc != wxFONTENCODING_DEFAULT && enc != wxFONTENCODING_SYSTEM )
654 {
2a1f999f 655 desc << _T(' ') << wxFontMapper::GetEncodingName(enc);
a9249b2e 656 }
e7e52b6d 657#endif // wxUSE_FONTMAP
a9249b2e 658
85ab460e 659 return desc.Strip(wxString::both).MakeLower();
ab5fe833
VZ
660}
661
662bool wxNativeFontInfo::FromUserString(const wxString& s)
663{
664 // reset to the default state
665 Init();
666
667 // parse a more or less free form string
668 //
669 // TODO: we should handle at least the quoted facenames
670 wxStringTokenizer tokenizer(s, _T(";, "), wxTOKEN_STRTOK);
671
672 wxString face;
673 unsigned long size;
77f15ffe
VS
674 bool weightfound = false, pointsizefound = false;
675#if wxUSE_FONTMAP
676 bool encodingfound = false;
677#endif
ab5fe833
VZ
678
679 while ( tokenizer.HasMoreTokens() )
680 {
681 wxString token = tokenizer.GetNextToken();
682
683 // normalize it
a62848fd 684 token.Trim(true).Trim(false).MakeLower();
ab5fe833
VZ
685
686 // look for the known tokens
687 if ( token == _T("underlined") || token == _("underlined") )
688 {
a62848fd 689 SetUnderlined(true);
ab5fe833
VZ
690 }
691 else if ( token == _T("light") || token == _("light") )
692 {
693 SetWeight(wxFONTWEIGHT_LIGHT);
85ab460e 694 weightfound = true;
ab5fe833
VZ
695 }
696 else if ( token == _T("bold") || token == _("bold") )
697 {
698 SetWeight(wxFONTWEIGHT_BOLD);
85ab460e 699 weightfound = true;
ab5fe833
VZ
700 }
701 else if ( token == _T("italic") || token == _("italic") )
702 {
703 SetStyle(wxFONTSTYLE_ITALIC);
704 }
705 else if ( token.ToULong(&size) )
706 {
a9249b2e 707 SetPointSize(size);
85ab460e 708 pointsizefound = true;
ab5fe833 709 }
85ab460e
VZ
710 else
711 {
e7e52b6d 712#if wxUSE_FONTMAP
85ab460e
VZ
713 // try to interpret this as an encoding
714 wxFontEncoding encoding = wxFontMapper::Get()->CharsetToEncoding(token, false);
715 if ( encoding != wxFONTENCODING_DEFAULT &&
716 encoding != wxFONTENCODING_SYSTEM ) // returned when the recognition failed
ab5fe833
VZ
717 {
718 SetEncoding(encoding);
85ab460e 719 encodingfound = true;
ab5fe833 720 }
85ab460e 721 else
ab5fe833 722 {
85ab460e
VZ
723#endif // wxUSE_FONTMAP
724
725 // assume it is the face name
ab5fe833
VZ
726 if ( !face.empty() )
727 {
728 face += _T(' ');
729 }
730
731 face += token;
732
733 // skip the code which resets face below
734 continue;
85ab460e
VZ
735
736#if wxUSE_FONTMAP
737 }
738#endif // wxUSE_FONTMAP
ab5fe833
VZ
739 }
740
741 // if we had had the facename, we shouldn't continue appending tokens
742 // to it (i.e. "foo bold bar" shouldn't result in the facename "foo
743 // bar")
744 if ( !face.empty() )
745 {
85ab460e
VZ
746 // NB: the check on the facename is implemented in wxFontBase::SetFaceName
747 // and not in wxNativeFontInfo::SetFaceName thus we need to explicitely
748 // call here wxFontEnumerator::IsValidFacename
ff427585
VZ
749 if (
750#if wxUSE_FONTENUM
751 !wxFontEnumerator::IsValidFacename(face) ||
752#endif // wxUSE_FONTENUM
753 !SetFaceName(face) )
754 {
85ab460e 755 SetFaceName(wxNORMAL_FONT->GetFaceName());
ff427585
VZ
756 }
757
ab5fe833
VZ
758 face.clear();
759 }
760 }
761
762 // we might not have flushed it inside the loop
763 if ( !face.empty() )
764 {
85ab460e
VZ
765 // NB: the check on the facename is implemented in wxFontBase::SetFaceName
766 // and not in wxNativeFontInfo::SetFaceName thus we need to explicitely
767 // call here wxFontEnumerator::IsValidFacename
ff427585
VZ
768 if (
769#if wxUSE_FONTENUM
770 !wxFontEnumerator::IsValidFacename(face) ||
771#endif // wxUSE_FONTENUM
772 !SetFaceName(face) )
773 {
774 SetFaceName(wxNORMAL_FONT->GetFaceName());
775 }
ab5fe833
VZ
776 }
777
85ab460e
VZ
778 // set point size to default value if size was not given
779 if ( !pointsizefound )
780 SetPointSize(wxNORMAL_FONT->GetPointSize());
781
782 // set font weight to default value if weight was not given
783 if ( !weightfound )
784 SetWeight(wxFONTWEIGHT_NORMAL);
785
786#if wxUSE_FONTMAP
787 // set font encoding to default value if encoding was not given
788 if ( !encodingfound )
789 SetEncoding(wxFONTENCODING_SYSTEM);
790#endif // wxUSE_FONTMAP
791
a62848fd 792 return true;
ab5fe833
VZ
793}
794
0eb529d9 795#endif // generic or wxMSW or wxOS2
fc9361e3
VZ
796
797
798// wxFont <-> wxString utilities, used by wxConfig
799wxString wxToString(const wxFontBase& font)
800{
801 return font.IsOk() ? font.GetNativeFontInfoDesc()
802 : wxString();
803}
804
805bool wxFromString(const wxString& str, wxFontBase *font)
806{
807 wxCHECK_MSG( font, false, _T("NULL output parameter") );
808
809 if ( str.empty() )
810 {
811 *font = wxNullFont;
812 return true;
813 }
814
815 return font->SetNativeFontInfo(str);
816}
817
818