]> git.saurik.com Git - wxWidgets.git/blob - src/common/fontcmn.cpp
minor cleanup of Unix GetInfo(): assert if invalid parameters are passed; don't use...
[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 default: return "wxFONTFAMILY_DEFAULT";
379 }
380 }
381
382 wxString wxFontBase::GetStyleString() const
383 {
384 wxCHECK_MSG( IsOk(), "wxFONTSTYLE_DEFAULT", "invalid font" );
385
386 switch ( GetStyle() )
387 {
388 case wxFONTSTYLE_NORMAL: return "wxFONTSTYLE_NORMAL";
389 case wxFONTSTYLE_SLANT: return "wxFONTSTYLE_SLANT";
390 case wxFONTSTYLE_ITALIC: return "wxFONTSTYLE_ITALIC";
391 default: return "wxFONTSTYLE_DEFAULT";
392 }
393 }
394
395 wxString wxFontBase::GetWeightString() const
396 {
397 wxCHECK_MSG( IsOk(), "wxFONTWEIGHT_DEFAULT", "invalid font" );
398
399 switch ( GetWeight() )
400 {
401 case wxFONTWEIGHT_NORMAL: return "wxFONTWEIGHT_NORMAL";
402 case wxFONTWEIGHT_BOLD: return "wxFONTWEIGHT_BOLD";
403 case wxFONTWEIGHT_LIGHT: return "wxFONTWEIGHT_LIGHT";
404 default: return "wxFONTWEIGHT_DEFAULT";
405 }
406 }
407
408 bool wxFontBase::SetFaceName(const wxString& facename)
409 {
410 #if wxUSE_FONTENUM
411 if (!wxFontEnumerator::IsValidFacename(facename))
412 {
413 UnRef(); // make IsOk() return false
414 return false;
415 }
416 #else // !wxUSE_FONTENUM
417 wxUnusedVar(facename);
418 #endif // wxUSE_FONTENUM/!wxUSE_FONTENUM
419
420 return true;
421 }
422
423
424 // ----------------------------------------------------------------------------
425 // wxNativeFontInfo
426 // ----------------------------------------------------------------------------
427
428 // Up to now, there are no native implementations of this function:
429 void wxNativeFontInfo::SetFaceName(const wxArrayString& facenames)
430 {
431 #if wxUSE_FONTENUM
432 for (size_t i=0; i < facenames.GetCount(); i++)
433 {
434 if (wxFontEnumerator::IsValidFacename(facenames[i]))
435 {
436 SetFaceName(facenames[i]);
437 return;
438 }
439 }
440
441 // set the first valid facename we can find on this system
442 wxString validfacename = wxFontEnumerator::GetFacenames().Item(0);
443 wxLogTrace(wxT("font"), wxT("Falling back to '%s'"), validfacename.c_str());
444 SetFaceName(validfacename);
445 #else // !wxUSE_FONTENUM
446 SetFaceName(facenames[0]);
447 #endif // wxUSE_FONTENUM/!wxUSE_FONTENUM
448 }
449
450
451 #ifdef wxNO_NATIVE_FONTINFO
452
453 // These are the generic forms of FromString()/ToString.
454 //
455 // convert to/from the string representation: format is
456 // version;pointsize;family;style;weight;underlined;facename;encoding
457
458 bool wxNativeFontInfo::FromString(const wxString& s)
459 {
460 long l;
461
462 wxStringTokenizer tokenizer(s, _T(";"));
463
464 wxString token = tokenizer.GetNextToken();
465 //
466 // Ignore the version for now
467 //
468
469 token = tokenizer.GetNextToken();
470 if ( !token.ToLong(&l) )
471 return false;
472 pointSize = (int)l;
473
474 token = tokenizer.GetNextToken();
475 if ( !token.ToLong(&l) )
476 return false;
477 family = (wxFontFamily)l;
478
479 token = tokenizer.GetNextToken();
480 if ( !token.ToLong(&l) )
481 return false;
482 style = (wxFontStyle)l;
483
484 token = tokenizer.GetNextToken();
485 if ( !token.ToLong(&l) )
486 return false;
487 weight = (wxFontWeight)l;
488
489 token = tokenizer.GetNextToken();
490 if ( !token.ToLong(&l) )
491 return false;
492 underlined = l != 0;
493
494 faceName = tokenizer.GetNextToken();
495
496 #ifndef __WXMAC__
497 if( !faceName )
498 return false;
499 #endif
500
501 token = tokenizer.GetNextToken();
502 if ( !token.ToLong(&l) )
503 return false;
504 encoding = (wxFontEncoding)l;
505
506 return true;
507 }
508
509 wxString wxNativeFontInfo::ToString() const
510 {
511 wxString s;
512
513 s.Printf(_T("%d;%d;%d;%d;%d;%d;%s;%d"),
514 0, // version
515 pointSize,
516 family,
517 (int)style,
518 (int)weight,
519 underlined,
520 faceName.GetData(),
521 (int)encoding);
522
523 return s;
524 }
525
526 void wxNativeFontInfo::Init()
527 {
528 pointSize = 0;
529 family = wxFONTFAMILY_DEFAULT;
530 style = wxFONTSTYLE_NORMAL;
531 weight = wxFONTWEIGHT_NORMAL;
532 underlined = false;
533 faceName.clear();
534 encoding = wxFONTENCODING_DEFAULT;
535 }
536
537 int wxNativeFontInfo::GetPointSize() const
538 {
539 return pointSize;
540 }
541
542 wxFontStyle wxNativeFontInfo::GetStyle() const
543 {
544 return style;
545 }
546
547 wxFontWeight wxNativeFontInfo::GetWeight() const
548 {
549 return weight;
550 }
551
552 bool wxNativeFontInfo::GetUnderlined() const
553 {
554 return underlined;
555 }
556
557 wxString wxNativeFontInfo::GetFaceName() const
558 {
559 return faceName;
560 }
561
562 wxFontFamily wxNativeFontInfo::GetFamily() const
563 {
564 return family;
565 }
566
567 wxFontEncoding wxNativeFontInfo::GetEncoding() const
568 {
569 return encoding;
570 }
571
572 void wxNativeFontInfo::SetPointSize(int pointsize)
573 {
574 pointSize = pointsize;
575 }
576
577 void wxNativeFontInfo::SetStyle(wxFontStyle style_)
578 {
579 style = style_;
580 }
581
582 void wxNativeFontInfo::SetWeight(wxFontWeight weight_)
583 {
584 weight = weight_;
585 }
586
587 void wxNativeFontInfo::SetUnderlined(bool underlined_)
588 {
589 underlined = underlined_;
590 }
591
592 bool wxNativeFontInfo::SetFaceName(const wxString& facename_)
593 {
594 faceName = facename_;
595 return true;
596 }
597
598 void wxNativeFontInfo::SetFamily(wxFontFamily family_)
599 {
600 family = family_;
601 }
602
603 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding_)
604 {
605 encoding = encoding_;
606 }
607
608 #endif // generic wxNativeFontInfo implementation
609
610 // conversion to/from user-readable string: this is used in the generic
611 // versions and under MSW as well because there is no standard font description
612 // format there anyhow (but there is a well-defined standard for X11 fonts used
613 // by wxGTK and wxMotif)
614
615 #if defined(wxNO_NATIVE_FONTINFO) || defined(__WXMSW__) || defined (__WXPM__) || defined(__WXOSX__)
616
617 wxString wxNativeFontInfo::ToUserString() const
618 {
619 wxString desc;
620
621 // first put the adjectives, if any - this is English-centric, of course,
622 // but what else can we do?
623 if ( GetUnderlined() )
624 {
625 desc << _("underlined");
626 }
627
628 switch ( GetWeight() )
629 {
630 default:
631 wxFAIL_MSG( _T("unknown font weight") );
632 // fall through
633
634 case wxFONTWEIGHT_NORMAL:
635 break;
636
637 case wxFONTWEIGHT_LIGHT:
638 desc << _(" light");
639 break;
640
641 case wxFONTWEIGHT_BOLD:
642 desc << _(" bold");
643 break;
644 }
645
646 switch ( GetStyle() )
647 {
648 default:
649 wxFAIL_MSG( _T("unknown font style") );
650 // fall through
651
652 case wxFONTSTYLE_NORMAL:
653 break;
654
655 // we don't distinguish between the two for now anyhow...
656 case wxFONTSTYLE_ITALIC:
657 case wxFONTSTYLE_SLANT:
658 desc << _(" italic");
659 break;
660 }
661
662 wxString face = GetFaceName();
663 if ( !face.empty() )
664 {
665 if (face.Contains(' ') || face.Contains(';') || face.Contains(','))
666 {
667 face.Replace("'", "");
668 // eventually remove quote characters: most systems do not
669 // allow them in a facename anyway so this usually does nothing
670
671 // make it possible for FromUserString() function to understand
672 // that the different words which compose this facename are
673 // not different adjectives or other data but rather all parts
674 // of the facename
675 desc << _T(" '") << face << _("'");
676 }
677 else
678 desc << _T(' ') << face;
679 }
680
681 int size = GetPointSize();
682 if ( size != wxNORMAL_FONT->GetPointSize() )
683 {
684 desc << _T(' ') << size;
685 }
686
687 #if wxUSE_FONTMAP
688 wxFontEncoding enc = GetEncoding();
689 if ( enc != wxFONTENCODING_DEFAULT && enc != wxFONTENCODING_SYSTEM )
690 {
691 desc << _T(' ') << wxFontMapper::GetEncodingName(enc);
692 }
693 #endif // wxUSE_FONTMAP
694
695 return desc.Strip(wxString::both).MakeLower();
696 }
697
698 bool wxNativeFontInfo::FromUserString(const wxString& s)
699 {
700 // reset to the default state
701 Init();
702
703 // ToUserString() will quote the facename if it contains spaces, commas
704 // or semicolons: we must be able to understand that quoted text is
705 // a single token:
706 wxString toparse(s);
707 /*
708 wxString::iterator i = toparse.find("'");
709 if (i != wxString::npos)
710 {
711 for (; *i != '\'' && *i != toparse.end(); i++)
712 ;
713 }*/
714
715 // parse a more or less free form string
716 wxStringTokenizer tokenizer(toparse, _T(";, "), wxTOKEN_STRTOK);
717
718 wxString face;
719 unsigned long size;
720 bool weightfound = false, pointsizefound = false;
721 #if wxUSE_FONTMAP
722 bool encodingfound = false;
723 #endif
724 bool insideQuotes = false;
725
726 while ( tokenizer.HasMoreTokens() )
727 {
728 wxString token = tokenizer.GetNextToken();
729
730 // normalize it
731 token.Trim(true).Trim(false).MakeLower();
732 if (insideQuotes)
733 {
734 if (token.StartsWith("'") ||
735 token.EndsWith("'"))
736 {
737 insideQuotes = false;
738
739 // add this last token to the facename:
740 face += " " + token;
741
742 // normalize facename:
743 face = face.Trim(true).Trim(false);
744 face.Replace("'", "");
745
746 continue;
747 }
748 }
749 else
750 {
751 if (token.StartsWith("'"))
752 insideQuotes = true;
753 }
754
755 // look for the known tokens
756 if ( insideQuotes )
757 {
758 // only the facename may be quoted:
759 face += " " + token;
760 continue;
761 }
762 if ( token == _T("underlined") || token == _("underlined") )
763 {
764 SetUnderlined(true);
765 }
766 else if ( token == _T("light") || token == _("light") )
767 {
768 SetWeight(wxFONTWEIGHT_LIGHT);
769 weightfound = true;
770 }
771 else if ( token == _T("bold") || token == _("bold") )
772 {
773 SetWeight(wxFONTWEIGHT_BOLD);
774 weightfound = true;
775 }
776 else if ( token == _T("italic") || token == _("italic") )
777 {
778 SetStyle(wxFONTSTYLE_ITALIC);
779 }
780 else if ( token.ToULong(&size) )
781 {
782 SetPointSize(size);
783 pointsizefound = true;
784 }
785 else
786 {
787 #if wxUSE_FONTMAP
788 // try to interpret this as an encoding
789 wxFontEncoding encoding = wxFontMapper::Get()->CharsetToEncoding(token, false);
790 if ( encoding != wxFONTENCODING_DEFAULT &&
791 encoding != wxFONTENCODING_SYSTEM ) // returned when the recognition failed
792 {
793 SetEncoding(encoding);
794 encodingfound = true;
795 }
796 else
797 {
798 #endif // wxUSE_FONTMAP
799
800 // assume it is the face name
801 if ( !face.empty() )
802 {
803 face += _T(' ');
804 }
805
806 face += token;
807
808 // skip the code which resets face below
809 continue;
810
811 #if wxUSE_FONTMAP
812 }
813 #endif // wxUSE_FONTMAP
814 }
815
816 // if we had had the facename, we shouldn't continue appending tokens
817 // to it (i.e. "foo bold bar" shouldn't result in the facename "foo
818 // bar")
819 if ( !face.empty() )
820 {
821 // NB: the check on the facename is implemented in wxFontBase::SetFaceName
822 // and not in wxNativeFontInfo::SetFaceName thus we need to explicitely
823 // call here wxFontEnumerator::IsValidFacename
824 if (
825 #if wxUSE_FONTENUM
826 !wxFontEnumerator::IsValidFacename(face) ||
827 #endif // wxUSE_FONTENUM
828 !SetFaceName(face) )
829 {
830 SetFaceName(wxNORMAL_FONT->GetFaceName());
831 }
832
833 face.clear();
834 }
835 }
836
837 // we might not have flushed it inside the loop
838 if ( !face.empty() )
839 {
840 // NB: the check on the facename is implemented in wxFontBase::SetFaceName
841 // and not in wxNativeFontInfo::SetFaceName thus we need to explicitely
842 // call here wxFontEnumerator::IsValidFacename
843 if (
844 #if wxUSE_FONTENUM
845 !wxFontEnumerator::IsValidFacename(face) ||
846 #endif // wxUSE_FONTENUM
847 !SetFaceName(face) )
848 {
849 SetFaceName(wxNORMAL_FONT->GetFaceName());
850 }
851 }
852
853 // set point size to default value if size was not given
854 if ( !pointsizefound )
855 SetPointSize(wxNORMAL_FONT->GetPointSize());
856
857 // set font weight to default value if weight was not given
858 if ( !weightfound )
859 SetWeight(wxFONTWEIGHT_NORMAL);
860
861 #if wxUSE_FONTMAP
862 // set font encoding to default value if encoding was not given
863 if ( !encodingfound )
864 SetEncoding(wxFONTENCODING_SYSTEM);
865 #endif // wxUSE_FONTMAP
866
867 return true;
868 }
869
870 #endif // generic or wxMSW or wxOS2
871
872
873 // wxFont <-> wxString utilities, used by wxConfig
874 wxString wxToString(const wxFontBase& font)
875 {
876 return font.IsOk() ? font.GetNativeFontInfoDesc()
877 : wxString();
878 }
879
880 bool wxFromString(const wxString& str, wxFontBase *font)
881 {
882 wxCHECK_MSG( font, false, _T("NULL output parameter") );
883
884 if ( str.empty() )
885 {
886 *font = wxNullFont;
887 return true;
888 }
889
890 return font->SetNativeFontInfo(str);
891 }
892
893