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