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