]> git.saurik.com Git - wxWidgets.git/blob - src/common/fontcmn.cpp
Compilation fix for wxUSE_FONTENUM==0 case.
[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 ? wxT("normal")
61 : weight == wxFONTWEIGHT_BOLD
62 ? wxT("bold")
63 : wxT("light"),
64 font->GetStyle() == wxFONTSTYLE_NORMAL
65 ? wxT("regular")
66 : wxT("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 wxT("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 wxCHECK_MSG( IsOk(), wxEmptyString, wxT("invalid font") );
288
289 wxString fontDesc;
290 const wxNativeFontInfo *fontInfo = GetNativeFontInfo();
291 if ( fontInfo )
292 {
293 fontDesc = fontInfo->ToString();
294 wxASSERT_MSG(!fontDesc.empty(), wxT("This should be a non-empty string!"));
295 }
296 else
297 {
298 wxFAIL_MSG(wxT("Derived class should have created the wxNativeFontInfo!"));
299 }
300
301 return fontDesc;
302 }
303
304 wxString wxFontBase::GetNativeFontInfoUserDesc() const
305 {
306 wxCHECK_MSG( IsOk(), wxEmptyString, wxT("invalid font") );
307
308 wxString fontDesc;
309 const wxNativeFontInfo *fontInfo = GetNativeFontInfo();
310 if ( fontInfo )
311 {
312 fontDesc = fontInfo->ToUserString();
313 wxASSERT_MSG(!fontDesc.empty(), wxT("This should be a non-empty string!"));
314 }
315 else
316 {
317 wxFAIL_MSG(wxT("Derived class should have created the wxNativeFontInfo!"));
318 }
319
320 return fontDesc;
321 }
322
323 bool wxFontBase::SetNativeFontInfo(const wxString& info)
324 {
325 wxNativeFontInfo fontInfo;
326 if ( !info.empty() && fontInfo.FromString(info) )
327 {
328 SetNativeFontInfo(fontInfo);
329 return true;
330 }
331
332 return false;
333 }
334
335 bool wxFontBase::SetNativeFontInfoUserDesc(const wxString& info)
336 {
337 wxNativeFontInfo fontInfo;
338 if ( !info.empty() && fontInfo.FromUserString(info) )
339 {
340 SetNativeFontInfo(fontInfo);
341 return true;
342 }
343
344 return false;
345 }
346
347 bool wxFontBase::operator==(const wxFont& font) const
348 {
349 // either it is the same font, i.e. they share the same common data or they
350 // have different ref datas but still describe the same font
351 return IsSameAs(font) ||
352 (
353 IsOk() == font.IsOk() &&
354 GetPointSize() == font.GetPointSize() &&
355 // in wxGTK1 GetPixelSize() calls GetInternalFont() which uses
356 // operator==() resulting in infinite recursion so we can't use it
357 // in that port
358 #if !defined(__WXGTK__) || defined(__WXGTK20__)
359 GetPixelSize() == font.GetPixelSize() &&
360 #endif
361 GetFamily() == font.GetFamily() &&
362 GetStyle() == font.GetStyle() &&
363 GetWeight() == font.GetWeight() &&
364 GetUnderlined() == font.GetUnderlined() &&
365 GetFaceName().IsSameAs(font.GetFaceName(), false) &&
366 GetEncoding() == font.GetEncoding()
367 );
368 }
369
370 wxString wxFontBase::GetFamilyString() const
371 {
372 wxCHECK_MSG( IsOk(), "wxFONTFAMILY_DEFAULT", "invalid font" );
373
374 switch ( GetFamily() )
375 {
376 case wxFONTFAMILY_DECORATIVE: return "wxFONTFAMILY_DECORATIVE";
377 case wxFONTFAMILY_ROMAN: return "wxFONTFAMILY_ROMAN";
378 case wxFONTFAMILY_SCRIPT: return "wxFONTFAMILY_SCRIPT";
379 case wxFONTFAMILY_SWISS: return "wxFONTFAMILY_SWISS";
380 case wxFONTFAMILY_MODERN: return "wxFONTFAMILY_MODERN";
381 case wxFONTFAMILY_TELETYPE: return "wxFONTFAMILY_TELETYPE";
382 case wxFONTFAMILY_UNKNOWN: return "wxFONTFAMILY_UNKNOWN";
383 default: return "wxFONTFAMILY_DEFAULT";
384 }
385 }
386
387 wxString wxFontBase::GetStyleString() const
388 {
389 wxCHECK_MSG( IsOk(), "wxFONTSTYLE_DEFAULT", "invalid font" );
390
391 switch ( GetStyle() )
392 {
393 case wxFONTSTYLE_NORMAL: return "wxFONTSTYLE_NORMAL";
394 case wxFONTSTYLE_SLANT: return "wxFONTSTYLE_SLANT";
395 case wxFONTSTYLE_ITALIC: return "wxFONTSTYLE_ITALIC";
396 default: return "wxFONTSTYLE_DEFAULT";
397 }
398 }
399
400 wxString wxFontBase::GetWeightString() const
401 {
402 wxCHECK_MSG( IsOk(), "wxFONTWEIGHT_DEFAULT", "invalid font" );
403
404 switch ( GetWeight() )
405 {
406 case wxFONTWEIGHT_NORMAL: return "wxFONTWEIGHT_NORMAL";
407 case wxFONTWEIGHT_BOLD: return "wxFONTWEIGHT_BOLD";
408 case wxFONTWEIGHT_LIGHT: return "wxFONTWEIGHT_LIGHT";
409 default: return "wxFONTWEIGHT_DEFAULT";
410 }
411 }
412
413 bool wxFontBase::SetFaceName(const wxString& facename)
414 {
415 #if wxUSE_FONTENUM
416 if (!wxFontEnumerator::IsValidFacename(facename))
417 {
418 UnRef(); // make IsOk() return false
419 return false;
420 }
421 #else // !wxUSE_FONTENUM
422 wxUnusedVar(facename);
423 #endif // wxUSE_FONTENUM/!wxUSE_FONTENUM
424
425 return true;
426 }
427
428 wxFont& wxFont::MakeBold()
429 {
430 SetWeight(wxFONTWEIGHT_BOLD);
431 return *this;
432 }
433
434 wxFont wxFont::Bold() const
435 {
436 wxFont font(*this);
437 font.MakeBold();
438 return font;
439 }
440
441 wxFont& wxFont::MakeItalic()
442 {
443 SetStyle(wxFONTSTYLE_ITALIC);
444 return *this;
445 }
446
447 wxFont wxFont::Italic() const
448 {
449 wxFont font(*this);
450 font.SetStyle(wxFONTSTYLE_ITALIC);
451 return font;
452 }
453
454 wxFont& wxFont::Scale(float x)
455 {
456 SetPointSize(int(x*GetPointSize() + 0.5));
457 return *this;
458 }
459
460 wxFont wxFont::Scaled(float x) const
461 {
462 wxFont font(*this);
463 font.Scale(x);
464 return font;
465 }
466
467 // ----------------------------------------------------------------------------
468 // wxNativeFontInfo
469 // ----------------------------------------------------------------------------
470
471 // Up to now, there are no native implementations of this function:
472 void wxNativeFontInfo::SetFaceName(const wxArrayString& facenames)
473 {
474 #if wxUSE_FONTENUM
475 for (size_t i=0; i < facenames.GetCount(); i++)
476 {
477 if (wxFontEnumerator::IsValidFacename(facenames[i]))
478 {
479 SetFaceName(facenames[i]);
480 return;
481 }
482 }
483
484 // set the first valid facename we can find on this system
485 wxString validfacename = wxFontEnumerator::GetFacenames().Item(0);
486 wxLogTrace(wxT("font"), wxT("Falling back to '%s'"), validfacename.c_str());
487 SetFaceName(validfacename);
488 #else // !wxUSE_FONTENUM
489 SetFaceName(facenames[0]);
490 #endif // wxUSE_FONTENUM/!wxUSE_FONTENUM
491 }
492
493
494 #ifdef wxNO_NATIVE_FONTINFO
495
496 // These are the generic forms of FromString()/ToString.
497 //
498 // convert to/from the string representation: format is
499 // version;pointsize;family;style;weight;underlined;facename;encoding
500
501 bool wxNativeFontInfo::FromString(const wxString& s)
502 {
503 long l;
504
505 wxStringTokenizer tokenizer(s, wxT(";"));
506
507 wxString token = tokenizer.GetNextToken();
508 //
509 // Ignore the version for now
510 //
511
512 token = tokenizer.GetNextToken();
513 if ( !token.ToLong(&l) )
514 return false;
515 pointSize = (int)l;
516
517 token = tokenizer.GetNextToken();
518 if ( !token.ToLong(&l) )
519 return false;
520 family = (wxFontFamily)l;
521
522 token = tokenizer.GetNextToken();
523 if ( !token.ToLong(&l) )
524 return false;
525 style = (wxFontStyle)l;
526
527 token = tokenizer.GetNextToken();
528 if ( !token.ToLong(&l) )
529 return false;
530 weight = (wxFontWeight)l;
531
532 token = tokenizer.GetNextToken();
533 if ( !token.ToLong(&l) )
534 return false;
535 underlined = l != 0;
536
537 faceName = tokenizer.GetNextToken();
538
539 #ifndef __WXMAC__
540 if( !faceName )
541 return false;
542 #endif
543
544 token = tokenizer.GetNextToken();
545 if ( !token.ToLong(&l) )
546 return false;
547 encoding = (wxFontEncoding)l;
548
549 return true;
550 }
551
552 wxString wxNativeFontInfo::ToString() const
553 {
554 wxString s;
555
556 s.Printf(wxT("%d;%d;%d;%d;%d;%d;%s;%d"),
557 0, // version
558 pointSize,
559 family,
560 (int)style,
561 (int)weight,
562 underlined,
563 faceName.GetData(),
564 (int)encoding);
565
566 return s;
567 }
568
569 void wxNativeFontInfo::Init()
570 {
571 pointSize = 0;
572 family = wxFONTFAMILY_DEFAULT;
573 style = wxFONTSTYLE_NORMAL;
574 weight = wxFONTWEIGHT_NORMAL;
575 underlined = false;
576 faceName.clear();
577 encoding = wxFONTENCODING_DEFAULT;
578 }
579
580 int wxNativeFontInfo::GetPointSize() const
581 {
582 return pointSize;
583 }
584
585 wxFontStyle wxNativeFontInfo::GetStyle() const
586 {
587 return style;
588 }
589
590 wxFontWeight wxNativeFontInfo::GetWeight() const
591 {
592 return weight;
593 }
594
595 bool wxNativeFontInfo::GetUnderlined() const
596 {
597 return underlined;
598 }
599
600 wxString wxNativeFontInfo::GetFaceName() const
601 {
602 return faceName;
603 }
604
605 wxFontFamily wxNativeFontInfo::GetFamily() const
606 {
607 return family;
608 }
609
610 wxFontEncoding wxNativeFontInfo::GetEncoding() const
611 {
612 return encoding;
613 }
614
615 void wxNativeFontInfo::SetPointSize(int pointsize)
616 {
617 pointSize = pointsize;
618 }
619
620 void wxNativeFontInfo::SetStyle(wxFontStyle style_)
621 {
622 style = style_;
623 }
624
625 void wxNativeFontInfo::SetWeight(wxFontWeight weight_)
626 {
627 weight = weight_;
628 }
629
630 void wxNativeFontInfo::SetUnderlined(bool underlined_)
631 {
632 underlined = underlined_;
633 }
634
635 bool wxNativeFontInfo::SetFaceName(const wxString& facename_)
636 {
637 faceName = facename_;
638 return true;
639 }
640
641 void wxNativeFontInfo::SetFamily(wxFontFamily family_)
642 {
643 family = family_;
644 }
645
646 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding_)
647 {
648 encoding = encoding_;
649 }
650
651 #endif // generic wxNativeFontInfo implementation
652
653 // conversion to/from user-readable string: this is used in the generic
654 // versions and under MSW as well because there is no standard font description
655 // format there anyhow (but there is a well-defined standard for X11 fonts used
656 // by wxGTK and wxMotif)
657
658 #if defined(wxNO_NATIVE_FONTINFO) || defined(__WXMSW__) || defined (__WXPM__) || defined(__WXOSX__)
659
660 wxString wxNativeFontInfo::ToUserString() const
661 {
662 wxString desc;
663
664 // first put the adjectives, if any - this is English-centric, of course,
665 // but what else can we do?
666 if ( GetUnderlined() )
667 {
668 desc << _("underlined");
669 }
670
671 switch ( GetWeight() )
672 {
673 default:
674 wxFAIL_MSG( wxT("unknown font weight") );
675 // fall through
676
677 case wxFONTWEIGHT_NORMAL:
678 break;
679
680 case wxFONTWEIGHT_LIGHT:
681 desc << _(" light");
682 break;
683
684 case wxFONTWEIGHT_BOLD:
685 desc << _(" bold");
686 break;
687 }
688
689 switch ( GetStyle() )
690 {
691 default:
692 wxFAIL_MSG( wxT("unknown font style") );
693 // fall through
694
695 case wxFONTSTYLE_NORMAL:
696 break;
697
698 // we don't distinguish between the two for now anyhow...
699 case wxFONTSTYLE_ITALIC:
700 case wxFONTSTYLE_SLANT:
701 desc << _(" italic");
702 break;
703 }
704
705 wxString face = GetFaceName();
706 if ( !face.empty() )
707 {
708 if (face.Contains(' ') || face.Contains(';') || face.Contains(','))
709 {
710 face.Replace("'", "");
711 // eventually remove quote characters: most systems do not
712 // allow them in a facename anyway so this usually does nothing
713
714 // make it possible for FromUserString() function to understand
715 // that the different words which compose this facename are
716 // not different adjectives or other data but rather all parts
717 // of the facename
718 desc << wxT(" '") << face << _("'");
719 }
720 else
721 desc << wxT(' ') << face;
722 }
723 else // no face name specified
724 {
725 // use the family
726 wxString familyStr;
727 switch ( GetFamily() )
728 {
729 case wxFONTFAMILY_DECORATIVE:
730 familyStr = "decorative";
731 break;
732
733 case wxFONTFAMILY_ROMAN:
734 familyStr = "roman";
735 break;
736
737 case wxFONTFAMILY_SCRIPT:
738 familyStr = "script";
739 break;
740
741 case wxFONTFAMILY_SWISS:
742 familyStr = "swiss";
743 break;
744
745 case wxFONTFAMILY_MODERN:
746 familyStr = "modern";
747 break;
748
749 case wxFONTFAMILY_TELETYPE:
750 familyStr = "teletype";
751 break;
752
753 case wxFONTFAMILY_DEFAULT:
754 case wxFONTFAMILY_UNKNOWN:
755 break;
756
757 default:
758 wxFAIL_MSG( "unknown font family" );
759 }
760
761 if ( !familyStr.empty() )
762 desc << " '" << familyStr << " family'";
763 }
764
765 int size = GetPointSize();
766 if ( size != wxNORMAL_FONT->GetPointSize() )
767 {
768 desc << wxT(' ') << size;
769 }
770
771 #if wxUSE_FONTMAP
772 wxFontEncoding enc = GetEncoding();
773 if ( enc != wxFONTENCODING_DEFAULT && enc != wxFONTENCODING_SYSTEM )
774 {
775 desc << wxT(' ') << wxFontMapper::GetEncodingName(enc);
776 }
777 #endif // wxUSE_FONTMAP
778
779 return desc.Strip(wxString::both).MakeLower();
780 }
781
782 bool wxNativeFontInfo::FromUserString(const wxString& s)
783 {
784 // reset to the default state
785 Init();
786
787 // ToUserString() will quote the facename if it contains spaces, commas
788 // or semicolons: we must be able to understand that quoted text is
789 // a single token:
790 wxString toparse(s);
791
792 // parse a more or less free form string
793 wxStringTokenizer tokenizer(toparse, wxT(";, "), wxTOKEN_STRTOK);
794
795 wxString face;
796 unsigned long size;
797 bool weightfound = false, pointsizefound = false;
798 #if wxUSE_FONTMAP
799 bool encodingfound = false;
800 #endif
801 bool insideQuotes = false;
802
803 while ( tokenizer.HasMoreTokens() )
804 {
805 wxString token = tokenizer.GetNextToken();
806
807 // normalize it
808 token.Trim(true).Trim(false).MakeLower();
809 if (insideQuotes)
810 {
811 if (token.StartsWith("'") ||
812 token.EndsWith("'"))
813 {
814 insideQuotes = false;
815
816 // add this last token to the facename:
817 face += " " + token;
818
819 // normalize facename:
820 face = face.Trim(true).Trim(false);
821 face.Replace("'", "");
822
823 continue;
824 }
825 }
826 else
827 {
828 if (token.StartsWith("'"))
829 insideQuotes = true;
830 }
831
832 // look for the known tokens
833 if ( insideQuotes )
834 {
835 // only the facename may be quoted:
836 face += " " + token;
837 continue;
838 }
839 if ( token == wxT("underlined") || token == _("underlined") )
840 {
841 SetUnderlined(true);
842 }
843 else if ( token == wxT("light") || token == _("light") )
844 {
845 SetWeight(wxFONTWEIGHT_LIGHT);
846 weightfound = true;
847 }
848 else if ( token == wxT("bold") || token == _("bold") )
849 {
850 SetWeight(wxFONTWEIGHT_BOLD);
851 weightfound = true;
852 }
853 else if ( token == wxT("italic") || token == _("italic") )
854 {
855 SetStyle(wxFONTSTYLE_ITALIC);
856 }
857 else if ( token.ToULong(&size) )
858 {
859 SetPointSize(size);
860 pointsizefound = true;
861 }
862 else
863 {
864 #if wxUSE_FONTMAP
865 // try to interpret this as an encoding
866 wxFontEncoding encoding = wxFontMapper::Get()->CharsetToEncoding(token, false);
867 if ( encoding != wxFONTENCODING_DEFAULT &&
868 encoding != wxFONTENCODING_SYSTEM ) // returned when the recognition failed
869 {
870 SetEncoding(encoding);
871 encodingfound = true;
872 }
873 else
874 {
875 #endif // wxUSE_FONTMAP
876
877 // assume it is the face name
878 if ( !face.empty() )
879 {
880 face += wxT(' ');
881 }
882
883 face += token;
884
885 // skip the code which resets face below
886 continue;
887
888 #if wxUSE_FONTMAP
889 }
890 #endif // wxUSE_FONTMAP
891 }
892
893 // if we had had the facename, we shouldn't continue appending tokens
894 // to it (i.e. "foo bold bar" shouldn't result in the facename "foo
895 // bar")
896 if ( !face.empty() )
897 {
898 wxString familyStr;
899 if ( face.EndsWith(" family", &familyStr) )
900 {
901 // it's not a facename but rather a font family
902 wxFontFamily family;
903 if ( familyStr == "decorative" )
904 family = wxFONTFAMILY_DECORATIVE;
905 else if ( familyStr == "roman" )
906 family = wxFONTFAMILY_ROMAN;
907 else if ( familyStr == "script" )
908 family = wxFONTFAMILY_SCRIPT;
909 else if ( familyStr == "swiss" )
910 family = wxFONTFAMILY_SWISS;
911 else if ( familyStr == "modern" )
912 family = wxFONTFAMILY_MODERN;
913 else if ( familyStr == "teletype" )
914 family = wxFONTFAMILY_TELETYPE;
915 else
916 return false;
917
918 SetFamily(family);
919 }
920 // NB: the check on the facename is implemented in wxFontBase::SetFaceName
921 // and not in wxNativeFontInfo::SetFaceName thus we need to explicitely
922 // call here wxFontEnumerator::IsValidFacename
923 else if (
924 #if wxUSE_FONTENUM
925 !wxFontEnumerator::IsValidFacename(face) ||
926 #endif // wxUSE_FONTENUM
927 !SetFaceName(face) )
928 {
929 SetFaceName(wxNORMAL_FONT->GetFaceName());
930 }
931
932 face.clear();
933 }
934 }
935
936 // we might not have flushed it inside the loop
937 if ( !face.empty() )
938 {
939 // NB: the check on the facename is implemented in wxFontBase::SetFaceName
940 // and not in wxNativeFontInfo::SetFaceName thus we need to explicitely
941 // call here wxFontEnumerator::IsValidFacename
942 if (
943 #if wxUSE_FONTENUM
944 !wxFontEnumerator::IsValidFacename(face) ||
945 #endif // wxUSE_FONTENUM
946 !SetFaceName(face) )
947 {
948 SetFaceName(wxNORMAL_FONT->GetFaceName());
949 }
950 }
951
952 // set point size to default value if size was not given
953 if ( !pointsizefound )
954 SetPointSize(wxNORMAL_FONT->GetPointSize());
955
956 // set font weight to default value if weight was not given
957 if ( !weightfound )
958 SetWeight(wxFONTWEIGHT_NORMAL);
959
960 #if wxUSE_FONTMAP
961 // set font encoding to default value if encoding was not given
962 if ( !encodingfound )
963 SetEncoding(wxFONTENCODING_SYSTEM);
964 #endif // wxUSE_FONTMAP
965
966 return true;
967 }
968
969 #endif // generic or wxMSW or wxOS2
970
971
972 // wxFont <-> wxString utilities, used by wxConfig
973 wxString wxToString(const wxFontBase& font)
974 {
975 return font.IsOk() ? font.GetNativeFontInfoDesc()
976 : wxString();
977 }
978
979 bool wxFromString(const wxString& str, wxFontBase *font)
980 {
981 wxCHECK_MSG( font, false, wxT("NULL output parameter") );
982
983 if ( str.empty() )
984 {
985 *font = wxNullFont;
986 return true;
987 }
988
989 return font->SetNativeFontInfo(str);
990 }
991
992