deal with the fonts without any specified face name in wxNativeFontInfo::To/FromUserS...
[wxWidgets.git] / src / common / fontcmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/fontcmn.cpp
3 // Purpose: implementation of wxFontBase methods
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 20.09.99
7 // RCS-ID: $Id$
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
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__
24 #pragma hdrstop
25 #endif
26
27 #include "wx/font.h"
28
29 #ifndef WX_PRECOMP
30 #include "wx/dc.h"
31 #include "wx/intl.h"
32 #include "wx/dcscreen.h"
33 #include "wx/log.h"
34 #include "wx/gdicmn.h"
35 #endif // WX_PRECOMP
36
37 #if defined(__WXMSW__)
38 #include "wx/msw/private.h" // includes windows.h for LOGFONT
39 #include "wx/msw/winundef.h"
40 #endif
41
42 #include "wx/fontutil.h" // for wxNativeFontInfo
43 #include "wx/fontmap.h"
44 #include "wx/fontenum.h"
45
46 #include "wx/tokenzr.h"
47
48 // debugger helper: this function can be called from a debugger to show what
49 // the date really is
50 extern const char *wxDumpFont(const wxFont *font)
51 {
52 static char buf[256];
53
54 const wxFontWeight weight = font->GetWeight();
55
56 wxString s;
57 s.Printf(wxS("%s-%s-%s-%d-%d"),
58 font->GetFaceName(),
59 weight == wxFONTWEIGHT_NORMAL
60 ? _T("normal")
61 : weight == wxFONTWEIGHT_BOLD
62 ? _T("bold")
63 : _T("light"),
64 font->GetStyle() == wxFONTSTYLE_NORMAL
65 ? _T("regular")
66 : _T("italic"),
67 font->GetPointSize(),
68 font->GetEncoding());
69
70 wxStrlcpy(buf, s.mb_str(), WXSIZEOF(buf));
71 return buf;
72 }
73
74 // ============================================================================
75 // implementation
76 // ============================================================================
77
78 // ----------------------------------------------------------------------------
79 // helper functions
80 // ----------------------------------------------------------------------------
81
82 static inline int flags2Style(int flags)
83 {
84 return flags & wxFONTFLAG_ITALIC
85 ? wxFONTSTYLE_ITALIC
86 : flags & wxFONTFLAG_SLANT
87 ? wxFONTSTYLE_SLANT
88 : wxFONTSTYLE_NORMAL;
89 }
90
91 static inline int flags2Weight(int flags)
92 {
93 return flags & wxFONTFLAG_LIGHT
94 ? wxFONTWEIGHT_LIGHT
95 : flags & wxFONTFLAG_BOLD
96 ? wxFONTWEIGHT_BOLD
97 : wxFONTWEIGHT_NORMAL;
98 }
99
100 static inline bool flags2Underlined(int flags)
101 {
102 return (flags & wxFONTFLAG_UNDERLINED) != 0;
103 }
104
105 // ----------------------------------------------------------------------------
106 // wxFontBase
107 // ----------------------------------------------------------------------------
108
109 wxFontEncoding wxFontBase::ms_encodingDefault = wxFONTENCODING_SYSTEM;
110
111 /* static */
112 void wxFontBase::SetDefaultEncoding(wxFontEncoding encoding)
113 {
114 // GetDefaultEncoding() should return something != wxFONTENCODING_DEFAULT
115 // and, besides, using this value here doesn't make any sense
116 wxCHECK_RET( encoding != wxFONTENCODING_DEFAULT,
117 _T("can't set default encoding to wxFONTENCODING_DEFAULT") );
118
119 ms_encodingDefault = encoding;
120 }
121
122 wxFontBase::~wxFontBase()
123 {
124 // this destructor is required for Darwin
125 }
126
127 /* static */
128 wxFont *wxFontBase::New(int size,
129 wxFontFamily family,
130 wxFontStyle style,
131 wxFontWeight weight,
132 bool underlined,
133 const wxString& face,
134 wxFontEncoding encoding)
135 {
136 return new wxFont(size, family, style, weight, underlined, face, encoding);
137 }
138
139 /* static */
140 wxFont *wxFontBase::New(const wxSize& pixelSize,
141 wxFontFamily family,
142 wxFontStyle style,
143 wxFontWeight weight,
144 bool underlined,
145 const wxString& face,
146 wxFontEncoding encoding)
147 {
148 return new wxFont(pixelSize, family, style, weight, underlined,
149 face, encoding);
150 }
151
152 /* static */
153 wxFont *wxFontBase::New(int pointSize,
154 wxFontFamily family,
155 int flags,
156 const wxString& face,
157 wxFontEncoding encoding)
158 {
159 return New(pointSize, family, flags2Style(flags), flags2Weight(flags),
160 flags2Underlined(flags), face, encoding);
161 }
162
163 /* static */
164 wxFont *wxFontBase::New(const wxSize& pixelSize,
165 wxFontFamily family,
166 int flags,
167 const wxString& face,
168 wxFontEncoding encoding)
169 {
170 return New(pixelSize, family, flags2Style(flags), flags2Weight(flags),
171 flags2Underlined(flags), face, encoding);
172 }
173
174 /* static */
175 wxFont *wxFontBase::New(const wxNativeFontInfo& info)
176 {
177 return new wxFont(info);
178 }
179
180 /* static */
181 wxFont *wxFontBase::New(const wxString& strNativeFontDesc)
182 {
183 wxNativeFontInfo fontInfo;
184 if ( !fontInfo.FromString(strNativeFontDesc) )
185 return new wxFont(*wxNORMAL_FONT);
186
187 return New(fontInfo);
188 }
189
190 bool wxFontBase::IsFixedWidth() const
191 {
192 return GetFamily() == wxFONTFAMILY_TELETYPE;
193 }
194
195 wxSize wxFontBase::GetPixelSize() const
196 {
197 wxScreenDC dc;
198 dc.SetFont(*(wxFont *)this);
199 return wxSize(dc.GetCharWidth(), dc.GetCharHeight());
200 }
201
202 bool wxFontBase::IsUsingSizeInPixels() const
203 {
204 return false;
205 }
206
207 void wxFontBase::SetPixelSize( const wxSize& pixelSize )
208 {
209 wxCHECK_RET( pixelSize.GetWidth() >= 0 && pixelSize.GetHeight() > 0,
210 "Negative values for the pixel size or zero pixel height are not allowed" );
211
212 wxScreenDC dc;
213
214 // NOTE: this algorithm for adjusting the font size is used by all
215 // implementations of wxFont except under wxMSW and wxGTK where
216 // native support to font creation using pixel-size is provided.
217
218 int largestGood = 0;
219 int smallestBad = 0;
220
221 bool initialGoodFound = false;
222 bool initialBadFound = false;
223
224 // NB: this assignment was separated from the variable definition
225 // in order to fix a gcc v3.3.3 compiler crash
226 int currentSize = GetPointSize();
227 while (currentSize > 0)
228 {
229 dc.SetFont(*static_cast<wxFont*>(this));
230
231 // if currentSize (in points) results in a font that is smaller
232 // than required by pixelSize it is considered a good size
233 // NOTE: the pixel size width may be zero
234 if (dc.GetCharHeight() <= pixelSize.GetHeight() &&
235 (pixelSize.GetWidth() == 0 ||
236 dc.GetCharWidth() <= pixelSize.GetWidth()))
237 {
238 largestGood = currentSize;
239 initialGoodFound = true;
240 }
241 else
242 {
243 smallestBad = currentSize;
244 initialBadFound = true;
245 }
246 if (!initialGoodFound)
247 {
248 currentSize /= 2;
249 }
250 else if (!initialBadFound)
251 {
252 currentSize *= 2;
253 }
254 else
255 {
256 int distance = smallestBad - largestGood;
257 if (distance == 1)
258 break;
259
260 currentSize = largestGood + distance / 2;
261 }
262
263 SetPointSize(currentSize);
264 }
265
266 if (currentSize != largestGood)
267 SetPointSize(largestGood);
268 }
269
270 void wxFontBase::DoSetNativeFontInfo(const wxNativeFontInfo& info)
271 {
272 #ifdef wxNO_NATIVE_FONTINFO
273 SetPointSize(info.pointSize);
274 SetFamily(info.family);
275 SetStyle(info.style);
276 SetWeight(info.weight);
277 SetUnderlined(info.underlined);
278 SetFaceName(info.faceName);
279 SetEncoding(info.encoding);
280 #else
281 (void)info;
282 #endif
283 }
284
285 wxString wxFontBase::GetNativeFontInfoDesc() const
286 {
287 wxString fontDesc;
288 const wxNativeFontInfo *fontInfo = GetNativeFontInfo();
289 if ( fontInfo )
290 {
291 fontDesc = fontInfo->ToString();
292 wxASSERT_MSG(!fontDesc.empty(), wxT("This should be a non-empty string!"));
293 }
294 else
295 {
296 wxFAIL_MSG(wxT("Derived class should have created the wxNativeFontInfo!"));
297 }
298
299 return fontDesc;
300 }
301
302 wxString wxFontBase::GetNativeFontInfoUserDesc() const
303 {
304 wxString fontDesc;
305 const wxNativeFontInfo *fontInfo = GetNativeFontInfo();
306 if ( fontInfo )
307 {
308 fontDesc = fontInfo->ToUserString();
309 wxASSERT_MSG(!fontDesc.empty(), wxT("This should be a non-empty string!"));
310 }
311 else
312 {
313 wxFAIL_MSG(wxT("Derived class should have created the wxNativeFontInfo!"));
314 }
315
316 return fontDesc;
317 }
318
319 bool wxFontBase::SetNativeFontInfo(const wxString& info)
320 {
321 wxNativeFontInfo fontInfo;
322 if ( !info.empty() && fontInfo.FromString(info) )
323 {
324 SetNativeFontInfo(fontInfo);
325 return true;
326 }
327
328 return false;
329 }
330
331 bool wxFontBase::SetNativeFontInfoUserDesc(const wxString& info)
332 {
333 wxNativeFontInfo fontInfo;
334 if ( !info.empty() && fontInfo.FromUserString(info) )
335 {
336 SetNativeFontInfo(fontInfo);
337 return true;
338 }
339
340 return false;
341 }
342
343 bool wxFontBase::operator==(const wxFont& font) const
344 {
345 // either it is the same font, i.e. they share the same common data or they
346 // have different ref datas but still describe the same font
347 return IsSameAs(font) ||
348 (
349 IsOk() == font.IsOk() &&
350 GetPointSize() == font.GetPointSize() &&
351 // in wxGTK1 GetPixelSize() calls GetInternalFont() which uses
352 // operator==() resulting in infinite recursion so we can't use it
353 // in that port
354 #if !defined(__WXGTK__) || defined(__WXGTK20__)
355 GetPixelSize() == font.GetPixelSize() &&
356 #endif
357 GetFamily() == font.GetFamily() &&
358 GetStyle() == font.GetStyle() &&
359 GetWeight() == font.GetWeight() &&
360 GetUnderlined() == font.GetUnderlined() &&
361 GetFaceName().IsSameAs(font.GetFaceName(), false) &&
362 GetEncoding() == font.GetEncoding()
363 );
364 }
365
366 wxString wxFontBase::GetFamilyString() const
367 {
368 wxCHECK_MSG( IsOk(), "wxFONTFAMILY_DEFAULT", "invalid font" );
369
370 switch ( GetFamily() )
371 {
372 case wxFONTFAMILY_DECORATIVE: return "wxFONTFAMILY_DECORATIVE";
373 case wxFONTFAMILY_ROMAN: return "wxFONTFAMILY_ROMAN";
374 case wxFONTFAMILY_SCRIPT: return "wxFONTFAMILY_SCRIPT";
375 case wxFONTFAMILY_SWISS: return "wxFONTFAMILY_SWISS";
376 case wxFONTFAMILY_MODERN: return "wxFONTFAMILY_MODERN";
377 case wxFONTFAMILY_TELETYPE: return "wxFONTFAMILY_TELETYPE";
378 case wxFONTFAMILY_UNKNOWN: return "wxFONTFAMILY_UNKNOWN";
379 default: return "wxFONTFAMILY_DEFAULT";
380 }
381 }
382
383 wxString wxFontBase::GetStyleString() const
384 {
385 wxCHECK_MSG( IsOk(), "wxFONTSTYLE_DEFAULT", "invalid font" );
386
387 switch ( GetStyle() )
388 {
389 case wxFONTSTYLE_NORMAL: return "wxFONTSTYLE_NORMAL";
390 case wxFONTSTYLE_SLANT: return "wxFONTSTYLE_SLANT";
391 case wxFONTSTYLE_ITALIC: return "wxFONTSTYLE_ITALIC";
392 default: return "wxFONTSTYLE_DEFAULT";
393 }
394 }
395
396 wxString wxFontBase::GetWeightString() const
397 {
398 wxCHECK_MSG( IsOk(), "wxFONTWEIGHT_DEFAULT", "invalid font" );
399
400 switch ( GetWeight() )
401 {
402 case wxFONTWEIGHT_NORMAL: return "wxFONTWEIGHT_NORMAL";
403 case wxFONTWEIGHT_BOLD: return "wxFONTWEIGHT_BOLD";
404 case wxFONTWEIGHT_LIGHT: return "wxFONTWEIGHT_LIGHT";
405 default: return "wxFONTWEIGHT_DEFAULT";
406 }
407 }
408
409 bool wxFontBase::SetFaceName(const wxString& facename)
410 {
411 #if wxUSE_FONTENUM
412 if (!wxFontEnumerator::IsValidFacename(facename))
413 {
414 UnRef(); // make IsOk() return false
415 return false;
416 }
417 #else // !wxUSE_FONTENUM
418 wxUnusedVar(facename);
419 #endif // wxUSE_FONTENUM/!wxUSE_FONTENUM
420
421 return true;
422 }
423
424
425 // ----------------------------------------------------------------------------
426 // wxNativeFontInfo
427 // ----------------------------------------------------------------------------
428
429 // Up to now, there are no native implementations of this function:
430 void wxNativeFontInfo::SetFaceName(const wxArrayString& facenames)
431 {
432 #if wxUSE_FONTENUM
433 for (size_t i=0; i < facenames.GetCount(); i++)
434 {
435 if (wxFontEnumerator::IsValidFacename(facenames[i]))
436 {
437 SetFaceName(facenames[i]);
438 return;
439 }
440 }
441
442 // set the first valid facename we can find on this system
443 wxString validfacename = wxFontEnumerator::GetFacenames().Item(0);
444 wxLogTrace(wxT("font"), wxT("Falling back to '%s'"), validfacename.c_str());
445 SetFaceName(validfacename);
446 #else // !wxUSE_FONTENUM
447 SetFaceName(facenames[0]);
448 #endif // wxUSE_FONTENUM/!wxUSE_FONTENUM
449 }
450
451
452 #ifdef wxNO_NATIVE_FONTINFO
453
454 // These are the generic forms of FromString()/ToString.
455 //
456 // convert to/from the string representation: format is
457 // version;pointsize;family;style;weight;underlined;facename;encoding
458
459 bool wxNativeFontInfo::FromString(const wxString& s)
460 {
461 long l;
462
463 wxStringTokenizer tokenizer(s, _T(";"));
464
465 wxString token = tokenizer.GetNextToken();
466 //
467 // Ignore the version for now
468 //
469
470 token = tokenizer.GetNextToken();
471 if ( !token.ToLong(&l) )
472 return false;
473 pointSize = (int)l;
474
475 token = tokenizer.GetNextToken();
476 if ( !token.ToLong(&l) )
477 return false;
478 family = (wxFontFamily)l;
479
480 token = tokenizer.GetNextToken();
481 if ( !token.ToLong(&l) )
482 return false;
483 style = (wxFontStyle)l;
484
485 token = tokenizer.GetNextToken();
486 if ( !token.ToLong(&l) )
487 return false;
488 weight = (wxFontWeight)l;
489
490 token = tokenizer.GetNextToken();
491 if ( !token.ToLong(&l) )
492 return false;
493 underlined = l != 0;
494
495 faceName = tokenizer.GetNextToken();
496
497 #ifndef __WXMAC__
498 if( !faceName )
499 return false;
500 #endif
501
502 token = tokenizer.GetNextToken();
503 if ( !token.ToLong(&l) )
504 return false;
505 encoding = (wxFontEncoding)l;
506
507 return true;
508 }
509
510 wxString wxNativeFontInfo::ToString() const
511 {
512 wxString s;
513
514 s.Printf(_T("%d;%d;%d;%d;%d;%d;%s;%d"),
515 0, // version
516 pointSize,
517 family,
518 (int)style,
519 (int)weight,
520 underlined,
521 faceName.GetData(),
522 (int)encoding);
523
524 return s;
525 }
526
527 void wxNativeFontInfo::Init()
528 {
529 pointSize = 0;
530 family = wxFONTFAMILY_DEFAULT;
531 style = wxFONTSTYLE_NORMAL;
532 weight = wxFONTWEIGHT_NORMAL;
533 underlined = false;
534 faceName.clear();
535 encoding = wxFONTENCODING_DEFAULT;
536 }
537
538 int wxNativeFontInfo::GetPointSize() const
539 {
540 return pointSize;
541 }
542
543 wxFontStyle wxNativeFontInfo::GetStyle() const
544 {
545 return style;
546 }
547
548 wxFontWeight wxNativeFontInfo::GetWeight() const
549 {
550 return weight;
551 }
552
553 bool wxNativeFontInfo::GetUnderlined() const
554 {
555 return underlined;
556 }
557
558 wxString wxNativeFontInfo::GetFaceName() const
559 {
560 return faceName;
561 }
562
563 wxFontFamily wxNativeFontInfo::GetFamily() const
564 {
565 return family;
566 }
567
568 wxFontEncoding wxNativeFontInfo::GetEncoding() const
569 {
570 return encoding;
571 }
572
573 void wxNativeFontInfo::SetPointSize(int pointsize)
574 {
575 pointSize = pointsize;
576 }
577
578 void wxNativeFontInfo::SetStyle(wxFontStyle style_)
579 {
580 style = style_;
581 }
582
583 void wxNativeFontInfo::SetWeight(wxFontWeight weight_)
584 {
585 weight = weight_;
586 }
587
588 void wxNativeFontInfo::SetUnderlined(bool underlined_)
589 {
590 underlined = underlined_;
591 }
592
593 bool wxNativeFontInfo::SetFaceName(const wxString& facename_)
594 {
595 faceName = facename_;
596 return true;
597 }
598
599 void wxNativeFontInfo::SetFamily(wxFontFamily family_)
600 {
601 family = family_;
602 }
603
604 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding_)
605 {
606 encoding = encoding_;
607 }
608
609 #endif // generic wxNativeFontInfo implementation
610
611 // conversion to/from user-readable string: this is used in the generic
612 // versions and under MSW as well because there is no standard font description
613 // format there anyhow (but there is a well-defined standard for X11 fonts used
614 // by wxGTK and wxMotif)
615
616 #if defined(wxNO_NATIVE_FONTINFO) || defined(__WXMSW__) || defined (__WXPM__) || defined(__WXOSX__)
617
618 wxString wxNativeFontInfo::ToUserString() const
619 {
620 wxString desc;
621
622 // first put the adjectives, if any - this is English-centric, of course,
623 // but what else can we do?
624 if ( GetUnderlined() )
625 {
626 desc << _("underlined");
627 }
628
629 switch ( GetWeight() )
630 {
631 default:
632 wxFAIL_MSG( _T("unknown font weight") );
633 // fall through
634
635 case wxFONTWEIGHT_NORMAL:
636 break;
637
638 case wxFONTWEIGHT_LIGHT:
639 desc << _(" light");
640 break;
641
642 case wxFONTWEIGHT_BOLD:
643 desc << _(" bold");
644 break;
645 }
646
647 switch ( GetStyle() )
648 {
649 default:
650 wxFAIL_MSG( _T("unknown font style") );
651 // fall through
652
653 case wxFONTSTYLE_NORMAL:
654 break;
655
656 // we don't distinguish between the two for now anyhow...
657 case wxFONTSTYLE_ITALIC:
658 case wxFONTSTYLE_SLANT:
659 desc << _(" italic");
660 break;
661 }
662
663 wxString face = GetFaceName();
664 if ( !face.empty() )
665 {
666 if (face.Contains(' ') || face.Contains(';') || face.Contains(','))
667 {
668 face.Replace("'", "");
669 // eventually remove quote characters: most systems do not
670 // allow them in a facename anyway so this usually does nothing
671
672 // make it possible for FromUserString() function to understand
673 // that the different words which compose this facename are
674 // not different adjectives or other data but rather all parts
675 // of the facename
676 desc << _T(" '") << face << _("'");
677 }
678 else
679 desc << _T(' ') << face;
680 }
681 else // no face name specified
682 {
683 // use the family
684 wxString familyStr;
685 switch ( GetFamily() )
686 {
687 case wxFONTFAMILY_DECORATIVE:
688 familyStr = "decorative";
689 break;
690
691 case wxFONTFAMILY_ROMAN:
692 familyStr = "roman";
693 break;
694
695 case wxFONTFAMILY_SCRIPT:
696 familyStr = "script";
697 break;
698
699 case wxFONTFAMILY_SWISS:
700 familyStr = "swiss";
701 break;
702
703 case wxFONTFAMILY_MODERN:
704 familyStr = "modern";
705 break;
706
707 case wxFONTFAMILY_TELETYPE:
708 familyStr = "teletype";
709 break;
710
711 case wxFONTFAMILY_DEFAULT:
712 case wxFONTFAMILY_UNKNOWN:
713 break;
714
715 default:
716 wxFAIL_MSG( "unknown font family" );
717 }
718
719 if ( !familyStr.empty() )
720 desc << " '" << familyStr << " family'";
721 }
722
723 int size = GetPointSize();
724 if ( size != wxNORMAL_FONT->GetPointSize() )
725 {
726 desc << _T(' ') << size;
727 }
728
729 #if wxUSE_FONTMAP
730 wxFontEncoding enc = GetEncoding();
731 if ( enc != wxFONTENCODING_DEFAULT && enc != wxFONTENCODING_SYSTEM )
732 {
733 desc << _T(' ') << wxFontMapper::GetEncodingName(enc);
734 }
735 #endif // wxUSE_FONTMAP
736
737 return desc.Strip(wxString::both).MakeLower();
738 }
739
740 bool wxNativeFontInfo::FromUserString(const wxString& s)
741 {
742 // reset to the default state
743 Init();
744
745 // ToUserString() will quote the facename if it contains spaces, commas
746 // or semicolons: we must be able to understand that quoted text is
747 // a single token:
748 wxString toparse(s);
749
750 // parse a more or less free form string
751 wxStringTokenizer tokenizer(toparse, _T(";, "), wxTOKEN_STRTOK);
752
753 wxString face;
754 unsigned long size;
755 bool weightfound = false, pointsizefound = false;
756 #if wxUSE_FONTMAP
757 bool encodingfound = false;
758 #endif
759 bool insideQuotes = false;
760
761 while ( tokenizer.HasMoreTokens() )
762 {
763 wxString token = tokenizer.GetNextToken();
764
765 // normalize it
766 token.Trim(true).Trim(false).MakeLower();
767 if (insideQuotes)
768 {
769 if (token.StartsWith("'") ||
770 token.EndsWith("'"))
771 {
772 insideQuotes = false;
773
774 // add this last token to the facename:
775 face += " " + token;
776
777 // normalize facename:
778 face = face.Trim(true).Trim(false);
779 face.Replace("'", "");
780
781 continue;
782 }
783 }
784 else
785 {
786 if (token.StartsWith("'"))
787 insideQuotes = true;
788 }
789
790 // look for the known tokens
791 if ( insideQuotes )
792 {
793 // only the facename may be quoted:
794 face += " " + token;
795 continue;
796 }
797 if ( token == _T("underlined") || token == _("underlined") )
798 {
799 SetUnderlined(true);
800 }
801 else if ( token == _T("light") || token == _("light") )
802 {
803 SetWeight(wxFONTWEIGHT_LIGHT);
804 weightfound = true;
805 }
806 else if ( token == _T("bold") || token == _("bold") )
807 {
808 SetWeight(wxFONTWEIGHT_BOLD);
809 weightfound = true;
810 }
811 else if ( token == _T("italic") || token == _("italic") )
812 {
813 SetStyle(wxFONTSTYLE_ITALIC);
814 }
815 else if ( token.ToULong(&size) )
816 {
817 SetPointSize(size);
818 pointsizefound = true;
819 }
820 else
821 {
822 #if wxUSE_FONTMAP
823 // try to interpret this as an encoding
824 wxFontEncoding encoding = wxFontMapper::Get()->CharsetToEncoding(token, false);
825 if ( encoding != wxFONTENCODING_DEFAULT &&
826 encoding != wxFONTENCODING_SYSTEM ) // returned when the recognition failed
827 {
828 SetEncoding(encoding);
829 encodingfound = true;
830 }
831 else
832 {
833 #endif // wxUSE_FONTMAP
834
835 // assume it is the face name
836 if ( !face.empty() )
837 {
838 face += _T(' ');
839 }
840
841 face += token;
842
843 // skip the code which resets face below
844 continue;
845
846 #if wxUSE_FONTMAP
847 }
848 #endif // wxUSE_FONTMAP
849 }
850
851 // if we had had the facename, we shouldn't continue appending tokens
852 // to it (i.e. "foo bold bar" shouldn't result in the facename "foo
853 // bar")
854 if ( !face.empty() )
855 {
856 wxString familyStr;
857 if ( face.EndsWith(" family", &familyStr) )
858 {
859 // it's not a facename but rather a font family
860 wxFontFamily family;
861 if ( familyStr == "decorative" )
862 family = wxFONTFAMILY_DECORATIVE;
863 else if ( familyStr == "roman" )
864 family = wxFONTFAMILY_ROMAN;
865 else if ( familyStr == "script" )
866 family = wxFONTFAMILY_SCRIPT;
867 else if ( familyStr == "swiss" )
868 family = wxFONTFAMILY_SWISS;
869 else if ( familyStr == "modern" )
870 family = wxFONTFAMILY_MODERN;
871 else if ( familyStr == "teletype" )
872 family = wxFONTFAMILY_TELETYPE;
873 else
874 return false;
875
876 SetFamily(family);
877 }
878 // NB: the check on the facename is implemented in wxFontBase::SetFaceName
879 // and not in wxNativeFontInfo::SetFaceName thus we need to explicitely
880 // call here wxFontEnumerator::IsValidFacename
881 else if (
882 #if wxUSE_FONTENUM
883 !wxFontEnumerator::IsValidFacename(face) ||
884 #endif // wxUSE_FONTENUM
885 !SetFaceName(face) )
886 {
887 SetFaceName(wxNORMAL_FONT->GetFaceName());
888 }
889
890 face.clear();
891 }
892 }
893
894 // we might not have flushed it inside the loop
895 if ( !face.empty() )
896 {
897 // NB: the check on the facename is implemented in wxFontBase::SetFaceName
898 // and not in wxNativeFontInfo::SetFaceName thus we need to explicitely
899 // call here wxFontEnumerator::IsValidFacename
900 if (
901 #if wxUSE_FONTENUM
902 !wxFontEnumerator::IsValidFacename(face) ||
903 #endif // wxUSE_FONTENUM
904 !SetFaceName(face) )
905 {
906 SetFaceName(wxNORMAL_FONT->GetFaceName());
907 }
908 }
909
910 // set point size to default value if size was not given
911 if ( !pointsizefound )
912 SetPointSize(wxNORMAL_FONT->GetPointSize());
913
914 // set font weight to default value if weight was not given
915 if ( !weightfound )
916 SetWeight(wxFONTWEIGHT_NORMAL);
917
918 #if wxUSE_FONTMAP
919 // set font encoding to default value if encoding was not given
920 if ( !encodingfound )
921 SetEncoding(wxFONTENCODING_SYSTEM);
922 #endif // wxUSE_FONTMAP
923
924 return true;
925 }
926
927 #endif // generic or wxMSW or wxOS2
928
929
930 // wxFont <-> wxString utilities, used by wxConfig
931 wxString wxToString(const wxFontBase& font)
932 {
933 return font.IsOk() ? font.GetNativeFontInfoDesc()
934 : wxString();
935 }
936
937 bool wxFromString(const wxString& str, wxFontBase *font)
938 {
939 wxCHECK_MSG( font, false, _T("NULL output parameter") );
940
941 if ( str.empty() )
942 {
943 *font = wxNullFont;
944 return true;
945 }
946
947 return font->SetNativeFontInfo(str);
948 }
949
950