1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/fontcmn.cpp
3 // Purpose: implementation of wxFontBase methods
4 // Author: Vadim Zeitlin
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
33 #include "wx/dcscreen.h"
35 #include "wx/gdicmn.h"
38 #if defined(__WXMSW__)
39 #include "wx/msw/private.h" // includes windows.h for LOGFONT
40 #include "wx/msw/winundef.h"
43 #include "wx/fontutil.h" // for wxNativeFontInfo
44 #include "wx/fontmap.h"
45 #include "wx/fontenum.h"
47 #include "wx/tokenzr.h"
49 // debugger helper: this function can be called from a debugger to show what
51 extern const char *wxDumpFont(const wxFont
*font
)
55 const wxFontWeight weight
= font
->GetWeight();
58 s
.Printf(wxS("%s-%s-%s-%d-%d"),
60 weight
== wxFONTWEIGHT_NORMAL
62 : weight
== wxFONTWEIGHT_BOLD
65 font
->GetStyle() == wxFONTSTYLE_NORMAL
71 wxStrlcpy(buf
, s
.mb_str(), WXSIZEOF(buf
));
75 // ----------------------------------------------------------------------------
77 // ----------------------------------------------------------------------------
79 wxBEGIN_ENUM( wxFontFamily
)
80 wxENUM_MEMBER( wxFONTFAMILY_DEFAULT
)
81 wxENUM_MEMBER( wxFONTFAMILY_DECORATIVE
)
82 wxENUM_MEMBER( wxFONTFAMILY_ROMAN
)
83 wxENUM_MEMBER( wxFONTFAMILY_SCRIPT
)
84 wxENUM_MEMBER( wxFONTFAMILY_SWISS
)
85 wxENUM_MEMBER( wxFONTFAMILY_MODERN
)
86 wxENUM_MEMBER( wxFONTFAMILY_TELETYPE
)
87 wxEND_ENUM( wxFontFamily
)
89 wxBEGIN_ENUM( wxFontStyle
)
90 wxENUM_MEMBER( wxFONTSTYLE_NORMAL
)
91 wxENUM_MEMBER( wxFONTSTYLE_ITALIC
)
92 wxENUM_MEMBER( wxFONTSTYLE_SLANT
)
93 wxEND_ENUM( wxFontStyle
)
95 wxBEGIN_ENUM( wxFontWeight
)
96 wxENUM_MEMBER( wxFONTWEIGHT_NORMAL
)
97 wxENUM_MEMBER( wxFONTWEIGHT_LIGHT
)
98 wxENUM_MEMBER( wxFONTWEIGHT_BOLD
)
99 wxEND_ENUM( wxFontWeight
)
101 wxIMPLEMENT_DYNAMIC_CLASS_WITH_COPY_XTI(wxFont
, wxGDIObject
, "wx/font.h")
103 //WX_IMPLEMENT_ANY_VALUE_TYPE(wxAnyValueTypeImpl<wxFont>)
105 wxBEGIN_PROPERTIES_TABLE(wxFont
)
106 wxPROPERTY( Size
,int, SetPointSize
, GetPointSize
, 12, 0 /*flags*/, \
107 wxT("Helpstring"), wxT("group"))
108 wxPROPERTY( Family
, wxFontFamily
, SetFamily
, GetFamily
, (wxFontFamily
)wxDEFAULT
, \
109 0 /*flags*/, wxT("Helpstring"), wxT("group")) // wxFontFamily
110 wxPROPERTY( Style
, wxFontStyle
, SetStyle
, GetStyle
, (wxFontStyle
)wxNORMAL
, 0 /*flags*/, \
111 wxT("Helpstring"), wxT("group")) // wxFontStyle
112 wxPROPERTY( Weight
, wxFontWeight
, SetWeight
, GetWeight
, (wxFontWeight
)wxNORMAL
, 0 /*flags*/, \
113 wxT("Helpstring"), wxT("group")) // wxFontWeight
114 wxPROPERTY( Underlined
, bool, SetUnderlined
, GetUnderlined
, false, 0 /*flags*/, \
115 wxT("Helpstring"), wxT("group"))
116 wxPROPERTY( Face
, wxString
, SetFaceName
, GetFaceName
, wxEMPTY_PARAMETER_VALUE
, \
117 0 /*flags*/, wxT("Helpstring"), wxT("group"))
118 wxPROPERTY( Encoding
, wxFontEncoding
, SetEncoding
, GetEncoding
, \
119 wxFONTENCODING_DEFAULT
, 0 /*flags*/, wxT("Helpstring"), wxT("group"))
120 wxEND_PROPERTIES_TABLE()
122 wxCONSTRUCTOR_6( wxFont
, int, Size
, wxFontFamily
, Family
, wxFontStyle
, Style
, wxFontWeight
, Weight
, \
123 bool, Underlined
, wxString
, Face
)
125 wxEMPTY_HANDLERS_TABLE(wxFont
)
127 // ============================================================================
129 // ============================================================================
131 // ----------------------------------------------------------------------------
133 // ----------------------------------------------------------------------------
135 static inline int flags2Style(int flags
)
137 return flags
& wxFONTFLAG_ITALIC
139 : flags
& wxFONTFLAG_SLANT
141 : wxFONTSTYLE_NORMAL
;
144 static inline int flags2Weight(int flags
)
146 return flags
& wxFONTFLAG_LIGHT
148 : flags
& wxFONTFLAG_BOLD
150 : wxFONTWEIGHT_NORMAL
;
153 static inline bool flags2Underlined(int flags
)
155 return (flags
& wxFONTFLAG_UNDERLINED
) != 0;
158 // ----------------------------------------------------------------------------
160 // ----------------------------------------------------------------------------
162 wxFontEncoding
wxFontBase::ms_encodingDefault
= wxFONTENCODING_SYSTEM
;
165 void wxFontBase::SetDefaultEncoding(wxFontEncoding encoding
)
167 // GetDefaultEncoding() should return something != wxFONTENCODING_DEFAULT
168 // and, besides, using this value here doesn't make any sense
169 wxCHECK_RET( encoding
!= wxFONTENCODING_DEFAULT
,
170 wxT("can't set default encoding to wxFONTENCODING_DEFAULT") );
172 ms_encodingDefault
= encoding
;
175 wxFontBase::~wxFontBase()
177 // this destructor is required for Darwin
181 wxFont
*wxFontBase::New(int size
,
186 const wxString
& face
,
187 wxFontEncoding encoding
)
189 return new wxFont(size
, family
, style
, weight
, underlined
, face
, encoding
);
193 wxFont
*wxFontBase::New(const wxSize
& pixelSize
,
198 const wxString
& face
,
199 wxFontEncoding encoding
)
201 return new wxFont(pixelSize
, family
, style
, weight
, underlined
,
206 wxFont
*wxFontBase::New(int pointSize
,
209 const wxString
& face
,
210 wxFontEncoding encoding
)
212 return New(pointSize
, family
, flags2Style(flags
), flags2Weight(flags
),
213 flags2Underlined(flags
), face
, encoding
);
217 wxFont
*wxFontBase::New(const wxSize
& pixelSize
,
220 const wxString
& face
,
221 wxFontEncoding encoding
)
223 return New(pixelSize
, family
, flags2Style(flags
), flags2Weight(flags
),
224 flags2Underlined(flags
), face
, encoding
);
228 wxFont
*wxFontBase::New(const wxNativeFontInfo
& info
)
230 return new wxFont(info
);
234 wxFont
*wxFontBase::New(const wxString
& strNativeFontDesc
)
236 wxNativeFontInfo fontInfo
;
237 if ( !fontInfo
.FromString(strNativeFontDesc
) )
238 return new wxFont(*wxNORMAL_FONT
);
240 return New(fontInfo
);
243 bool wxFontBase::IsFixedWidth() const
245 return GetFamily() == wxFONTFAMILY_TELETYPE
;
248 wxSize
wxFontBase::GetPixelSize() const
251 dc
.SetFont(*(wxFont
*)this);
252 return wxSize(dc
.GetCharWidth(), dc
.GetCharHeight());
255 bool wxFontBase::IsUsingSizeInPixels() const
260 void wxFontBase::SetPixelSize( const wxSize
& pixelSize
)
262 wxCHECK_RET( pixelSize
.GetWidth() >= 0 && pixelSize
.GetHeight() > 0,
263 "Negative values for the pixel size or zero pixel height are not allowed" );
267 // NOTE: this algorithm for adjusting the font size is used by all
268 // implementations of wxFont except under wxMSW and wxGTK where
269 // native support to font creation using pixel-size is provided.
274 bool initialGoodFound
= false;
275 bool initialBadFound
= false;
277 // NB: this assignment was separated from the variable definition
278 // in order to fix a gcc v3.3.3 compiler crash
279 int currentSize
= GetPointSize();
280 while (currentSize
> 0)
282 dc
.SetFont(*static_cast<wxFont
*>(this));
284 // if currentSize (in points) results in a font that is smaller
285 // than required by pixelSize it is considered a good size
286 // NOTE: the pixel size width may be zero
287 if (dc
.GetCharHeight() <= pixelSize
.GetHeight() &&
288 (pixelSize
.GetWidth() == 0 ||
289 dc
.GetCharWidth() <= pixelSize
.GetWidth()))
291 largestGood
= currentSize
;
292 initialGoodFound
= true;
296 smallestBad
= currentSize
;
297 initialBadFound
= true;
299 if (!initialGoodFound
)
303 else if (!initialBadFound
)
309 int distance
= smallestBad
- largestGood
;
313 currentSize
= largestGood
+ distance
/ 2;
316 SetPointSize(currentSize
);
319 if (currentSize
!= largestGood
)
320 SetPointSize(largestGood
);
323 void wxFontBase::DoSetNativeFontInfo(const wxNativeFontInfo
& info
)
325 #ifdef wxNO_NATIVE_FONTINFO
326 SetPointSize(info
.pointSize
);
327 SetFamily(info
.family
);
328 SetStyle(info
.style
);
329 SetWeight(info
.weight
);
330 SetUnderlined(info
.underlined
);
331 SetFaceName(info
.faceName
);
332 SetEncoding(info
.encoding
);
338 wxString
wxFontBase::GetNativeFontInfoDesc() const
340 wxCHECK_MSG( IsOk(), wxEmptyString
, wxT("invalid font") );
343 const wxNativeFontInfo
*fontInfo
= GetNativeFontInfo();
346 fontDesc
= fontInfo
->ToString();
347 wxASSERT_MSG(!fontDesc
.empty(), wxT("This should be a non-empty string!"));
351 wxFAIL_MSG(wxT("Derived class should have created the wxNativeFontInfo!"));
357 wxString
wxFontBase::GetNativeFontInfoUserDesc() const
359 wxCHECK_MSG( IsOk(), wxEmptyString
, wxT("invalid font") );
362 const wxNativeFontInfo
*fontInfo
= GetNativeFontInfo();
365 fontDesc
= fontInfo
->ToUserString();
366 wxASSERT_MSG(!fontDesc
.empty(), wxT("This should be a non-empty string!"));
370 wxFAIL_MSG(wxT("Derived class should have created the wxNativeFontInfo!"));
376 bool wxFontBase::SetNativeFontInfo(const wxString
& info
)
378 wxNativeFontInfo fontInfo
;
379 if ( !info
.empty() && fontInfo
.FromString(info
) )
381 SetNativeFontInfo(fontInfo
);
388 bool wxFontBase::SetNativeFontInfoUserDesc(const wxString
& info
)
390 wxNativeFontInfo fontInfo
;
391 if ( !info
.empty() && fontInfo
.FromUserString(info
) )
393 SetNativeFontInfo(fontInfo
);
400 bool wxFontBase::operator==(const wxFont
& font
) const
402 // either it is the same font, i.e. they share the same common data or they
403 // have different ref datas but still describe the same font
404 return IsSameAs(font
) ||
406 IsOk() == font
.IsOk() &&
407 GetPointSize() == font
.GetPointSize() &&
408 // in wxGTK1 GetPixelSize() calls GetInternalFont() which uses
409 // operator==() resulting in infinite recursion so we can't use it
411 #if !defined(__WXGTK__) || defined(__WXGTK20__)
412 GetPixelSize() == font
.GetPixelSize() &&
414 GetFamily() == font
.GetFamily() &&
415 GetStyle() == font
.GetStyle() &&
416 GetWeight() == font
.GetWeight() &&
417 GetUnderlined() == font
.GetUnderlined() &&
418 GetFaceName().IsSameAs(font
.GetFaceName(), false) &&
419 GetEncoding() == font
.GetEncoding()
423 wxFontFamily
wxFontBase::GetFamily() const
425 wxCHECK_MSG( IsOk(), wxFONTFAMILY_UNKNOWN
, wxS("invalid font") );
427 // Don't return wxFONTFAMILY_UNKNOWN from here because it prevents the code
428 // like wxFont(size, wxNORMAL_FONT->GetFamily(), ...) from working (see
429 // #12330). This is really just a hack but it allows to keep compatibility
430 // and doesn't really have any bad drawbacks so do this until someone comes
431 // up with a better idea.
432 const wxFontFamily family
= DoGetFamily();
434 return family
== wxFONTFAMILY_UNKNOWN
? wxFONTFAMILY_DEFAULT
: family
;
437 wxString
wxFontBase::GetFamilyString() const
439 wxCHECK_MSG( IsOk(), "wxFONTFAMILY_DEFAULT", "invalid font" );
441 switch ( GetFamily() )
443 case wxFONTFAMILY_DECORATIVE
: return "wxFONTFAMILY_DECORATIVE";
444 case wxFONTFAMILY_ROMAN
: return "wxFONTFAMILY_ROMAN";
445 case wxFONTFAMILY_SCRIPT
: return "wxFONTFAMILY_SCRIPT";
446 case wxFONTFAMILY_SWISS
: return "wxFONTFAMILY_SWISS";
447 case wxFONTFAMILY_MODERN
: return "wxFONTFAMILY_MODERN";
448 case wxFONTFAMILY_TELETYPE
: return "wxFONTFAMILY_TELETYPE";
449 case wxFONTFAMILY_UNKNOWN
: return "wxFONTFAMILY_UNKNOWN";
450 default: return "wxFONTFAMILY_DEFAULT";
454 wxString
wxFontBase::GetStyleString() const
456 wxCHECK_MSG( IsOk(), "wxFONTSTYLE_DEFAULT", "invalid font" );
458 switch ( GetStyle() )
460 case wxFONTSTYLE_NORMAL
: return "wxFONTSTYLE_NORMAL";
461 case wxFONTSTYLE_SLANT
: return "wxFONTSTYLE_SLANT";
462 case wxFONTSTYLE_ITALIC
: return "wxFONTSTYLE_ITALIC";
463 default: return "wxFONTSTYLE_DEFAULT";
467 wxString
wxFontBase::GetWeightString() const
469 wxCHECK_MSG( IsOk(), "wxFONTWEIGHT_DEFAULT", "invalid font" );
471 switch ( GetWeight() )
473 case wxFONTWEIGHT_NORMAL
: return "wxFONTWEIGHT_NORMAL";
474 case wxFONTWEIGHT_BOLD
: return "wxFONTWEIGHT_BOLD";
475 case wxFONTWEIGHT_LIGHT
: return "wxFONTWEIGHT_LIGHT";
476 default: return "wxFONTWEIGHT_DEFAULT";
480 bool wxFontBase::SetFaceName(const wxString
& facename
)
483 if (!wxFontEnumerator::IsValidFacename(facename
))
485 UnRef(); // make IsOk() return false
488 #else // !wxUSE_FONTENUM
489 wxUnusedVar(facename
);
490 #endif // wxUSE_FONTENUM/!wxUSE_FONTENUM
495 void wxFontBase::SetSymbolicSize(wxFontSymbolicSize size
)
497 SetSymbolicSizeRelativeTo(size
, wxNORMAL_FONT
->GetPointSize());
501 int wxFontBase::AdjustToSymbolicSize(wxFontSymbolicSize size
, int base
)
503 // Using a fixed factor (1.2, from CSS2) is a bad idea as explained at
504 // http://www.w3.org/TR/CSS21/fonts.html#font-size-props so use the values
505 // from http://style.cleverchimp.com/font_size_intervals/altintervals.html
507 static const float factors
[] = { 0.60f
, 0.75f
, 0.89f
, 1.f
, 1.2f
, 1.5f
, 2.f
};
509 wxCOMPILE_TIME_ASSERT
511 WXSIZEOF(factors
) == wxFONTSIZE_XX_LARGE
- wxFONTSIZE_XX_SMALL
+ 1,
512 WrongFontSizeFactorsSize
515 return wxRound(factors
[size
- wxFONTSIZE_XX_SMALL
]*base
);
518 wxFont
& wxFont::MakeBold()
520 SetWeight(wxFONTWEIGHT_BOLD
);
524 wxFont
wxFont::Bold() const
531 wxFont
& wxFont::MakeItalic()
533 SetStyle(wxFONTSTYLE_ITALIC
);
537 wxFont
wxFont::Italic() const
544 wxFont
& wxFont::MakeUnderlined()
550 wxFont
wxFont::Underlined() const
553 font
.MakeUnderlined();
557 wxFont
& wxFont::Scale(float x
)
559 SetPointSize(int(x
*GetPointSize() + 0.5));
563 wxFont
wxFont::Scaled(float x
) const
570 // ----------------------------------------------------------------------------
572 // ----------------------------------------------------------------------------
574 // Up to now, there are no native implementations of this function:
575 void wxNativeFontInfo::SetFaceName(const wxArrayString
& facenames
)
578 for (size_t i
=0; i
< facenames
.GetCount(); i
++)
580 if (wxFontEnumerator::IsValidFacename(facenames
[i
]))
582 SetFaceName(facenames
[i
]);
587 // set the first valid facename we can find on this system
588 wxString validfacename
= wxFontEnumerator::GetFacenames().Item(0);
589 wxLogTrace(wxT("font"), wxT("Falling back to '%s'"), validfacename
.c_str());
590 SetFaceName(validfacename
);
591 #else // !wxUSE_FONTENUM
592 SetFaceName(facenames
[0]);
593 #endif // wxUSE_FONTENUM/!wxUSE_FONTENUM
597 #ifdef wxNO_NATIVE_FONTINFO
599 // These are the generic forms of FromString()/ToString.
601 // convert to/from the string representation: format is
602 // version;pointsize;family;style;weight;underlined;facename;encoding
604 bool wxNativeFontInfo::FromString(const wxString
& s
)
608 wxStringTokenizer
tokenizer(s
, wxT(";"));
610 wxString token
= tokenizer
.GetNextToken();
612 // Ignore the version for now
615 token
= tokenizer
.GetNextToken();
616 if ( !token
.ToLong(&l
) )
620 token
= tokenizer
.GetNextToken();
621 if ( !token
.ToLong(&l
) )
623 family
= (wxFontFamily
)l
;
625 token
= tokenizer
.GetNextToken();
626 if ( !token
.ToLong(&l
) )
628 style
= (wxFontStyle
)l
;
630 token
= tokenizer
.GetNextToken();
631 if ( !token
.ToLong(&l
) )
633 weight
= (wxFontWeight
)l
;
635 token
= tokenizer
.GetNextToken();
636 if ( !token
.ToLong(&l
) )
640 faceName
= tokenizer
.GetNextToken();
647 token
= tokenizer
.GetNextToken();
648 if ( !token
.ToLong(&l
) )
650 encoding
= (wxFontEncoding
)l
;
655 wxString
wxNativeFontInfo::ToString() const
659 s
.Printf(wxT("%d;%d;%d;%d;%d;%d;%s;%d"),
672 void wxNativeFontInfo::Init()
675 family
= wxFONTFAMILY_DEFAULT
;
676 style
= wxFONTSTYLE_NORMAL
;
677 weight
= wxFONTWEIGHT_NORMAL
;
680 encoding
= wxFONTENCODING_DEFAULT
;
683 int wxNativeFontInfo::GetPointSize() const
688 wxFontStyle
wxNativeFontInfo::GetStyle() const
693 wxFontWeight
wxNativeFontInfo::GetWeight() const
698 bool wxNativeFontInfo::GetUnderlined() const
703 wxString
wxNativeFontInfo::GetFaceName() const
708 wxFontFamily
wxNativeFontInfo::GetFamily() const
713 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
718 void wxNativeFontInfo::SetPointSize(int pointsize
)
720 pointSize
= pointsize
;
723 void wxNativeFontInfo::SetStyle(wxFontStyle style_
)
728 void wxNativeFontInfo::SetWeight(wxFontWeight weight_
)
733 void wxNativeFontInfo::SetUnderlined(bool underlined_
)
735 underlined
= underlined_
;
738 bool wxNativeFontInfo::SetFaceName(const wxString
& facename_
)
740 faceName
= facename_
;
744 void wxNativeFontInfo::SetFamily(wxFontFamily family_
)
749 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding_
)
751 encoding
= encoding_
;
754 #endif // generic wxNativeFontInfo implementation
756 // conversion to/from user-readable string: this is used in the generic
757 // versions and under MSW as well because there is no standard font description
758 // format there anyhow (but there is a well-defined standard for X11 fonts used
759 // by wxGTK and wxMotif)
761 #if defined(wxNO_NATIVE_FONTINFO) || defined(__WXMSW__) || defined (__WXPM__) || defined(__WXOSX__)
763 wxString
wxNativeFontInfo::ToUserString() const
767 // first put the adjectives, if any - this is English-centric, of course,
768 // but what else can we do?
769 if ( GetUnderlined() )
771 desc
<< _("underlined");
774 switch ( GetWeight() )
777 wxFAIL_MSG( wxT("unknown font weight") );
780 case wxFONTWEIGHT_NORMAL
:
783 case wxFONTWEIGHT_LIGHT
:
787 case wxFONTWEIGHT_BOLD
:
792 switch ( GetStyle() )
795 wxFAIL_MSG( wxT("unknown font style") );
798 case wxFONTSTYLE_NORMAL
:
801 // we don't distinguish between the two for now anyhow...
802 case wxFONTSTYLE_ITALIC
:
803 case wxFONTSTYLE_SLANT
:
804 desc
<< _(" italic");
808 wxString face
= GetFaceName();
811 if (face
.Contains(' ') || face
.Contains(';') || face
.Contains(','))
813 face
.Replace("'", "");
814 // eventually remove quote characters: most systems do not
815 // allow them in a facename anyway so this usually does nothing
817 // make it possible for FromUserString() function to understand
818 // that the different words which compose this facename are
819 // not different adjectives or other data but rather all parts
821 desc
<< wxT(" '") << face
<< _("'");
824 desc
<< wxT(' ') << face
;
826 else // no face name specified
830 switch ( GetFamily() )
832 case wxFONTFAMILY_DECORATIVE
:
833 familyStr
= "decorative";
836 case wxFONTFAMILY_ROMAN
:
840 case wxFONTFAMILY_SCRIPT
:
841 familyStr
= "script";
844 case wxFONTFAMILY_SWISS
:
848 case wxFONTFAMILY_MODERN
:
849 familyStr
= "modern";
852 case wxFONTFAMILY_TELETYPE
:
853 familyStr
= "teletype";
856 case wxFONTFAMILY_DEFAULT
:
857 case wxFONTFAMILY_UNKNOWN
:
861 wxFAIL_MSG( "unknown font family" );
864 if ( !familyStr
.empty() )
865 desc
<< " '" << familyStr
<< " family'";
868 int size
= GetPointSize();
869 if ( size
!= wxNORMAL_FONT
->GetPointSize() )
871 desc
<< wxT(' ') << size
;
875 wxFontEncoding enc
= GetEncoding();
876 if ( enc
!= wxFONTENCODING_DEFAULT
&& enc
!= wxFONTENCODING_SYSTEM
)
878 desc
<< wxT(' ') << wxFontMapper::GetEncodingName(enc
);
880 #endif // wxUSE_FONTMAP
882 return desc
.Strip(wxString::both
).MakeLower();
885 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
887 // reset to the default state
890 // ToUserString() will quote the facename if it contains spaces, commas
891 // or semicolons: we must be able to understand that quoted text is
895 // parse a more or less free form string
896 wxStringTokenizer
tokenizer(toparse
, wxT(";, "), wxTOKEN_STRTOK
);
900 bool weightfound
= false, pointsizefound
= false;
902 bool encodingfound
= false;
904 bool insideQuotes
= false;
906 while ( tokenizer
.HasMoreTokens() )
908 wxString token
= tokenizer
.GetNextToken();
911 token
.Trim(true).Trim(false).MakeLower();
914 if (token
.StartsWith("'") ||
917 insideQuotes
= false;
919 // add this last token to the facename:
922 // normalize facename:
923 face
= face
.Trim(true).Trim(false);
924 face
.Replace("'", "");
931 if (token
.StartsWith("'"))
935 // look for the known tokens
938 // only the facename may be quoted:
942 if ( token
== wxT("underlined") || token
== _("underlined") )
946 else if ( token
== wxT("light") || token
== _("light") )
948 SetWeight(wxFONTWEIGHT_LIGHT
);
951 else if ( token
== wxT("bold") || token
== _("bold") )
953 SetWeight(wxFONTWEIGHT_BOLD
);
956 else if ( token
== wxT("italic") || token
== _("italic") )
958 SetStyle(wxFONTSTYLE_ITALIC
);
960 else if ( token
.ToULong(&size
) )
963 pointsizefound
= true;
968 // try to interpret this as an encoding
969 wxFontEncoding encoding
= wxFontMapper::Get()->CharsetToEncoding(token
, false);
970 if ( encoding
!= wxFONTENCODING_DEFAULT
&&
971 encoding
!= wxFONTENCODING_SYSTEM
) // returned when the recognition failed
973 SetEncoding(encoding
);
974 encodingfound
= true;
978 #endif // wxUSE_FONTMAP
980 // assume it is the face name
988 // skip the code which resets face below
993 #endif // wxUSE_FONTMAP
996 // if we had had the facename, we shouldn't continue appending tokens
997 // to it (i.e. "foo bold bar" shouldn't result in the facename "foo
1002 if ( face
.EndsWith(" family", &familyStr
) )
1004 // it's not a facename but rather a font family
1005 wxFontFamily family
;
1006 if ( familyStr
== "decorative" )
1007 family
= wxFONTFAMILY_DECORATIVE
;
1008 else if ( familyStr
== "roman" )
1009 family
= wxFONTFAMILY_ROMAN
;
1010 else if ( familyStr
== "script" )
1011 family
= wxFONTFAMILY_SCRIPT
;
1012 else if ( familyStr
== "swiss" )
1013 family
= wxFONTFAMILY_SWISS
;
1014 else if ( familyStr
== "modern" )
1015 family
= wxFONTFAMILY_MODERN
;
1016 else if ( familyStr
== "teletype" )
1017 family
= wxFONTFAMILY_TELETYPE
;
1023 // NB: the check on the facename is implemented in wxFontBase::SetFaceName
1024 // and not in wxNativeFontInfo::SetFaceName thus we need to explicitly
1025 // call here wxFontEnumerator::IsValidFacename
1028 !wxFontEnumerator::IsValidFacename(face
) ||
1029 #endif // wxUSE_FONTENUM
1030 !SetFaceName(face
) )
1032 SetFaceName(wxNORMAL_FONT
->GetFaceName());
1039 // we might not have flushed it inside the loop
1040 if ( !face
.empty() )
1042 // NB: the check on the facename is implemented in wxFontBase::SetFaceName
1043 // and not in wxNativeFontInfo::SetFaceName thus we need to explicitly
1044 // call here wxFontEnumerator::IsValidFacename
1047 !wxFontEnumerator::IsValidFacename(face
) ||
1048 #endif // wxUSE_FONTENUM
1049 !SetFaceName(face
) )
1051 SetFaceName(wxNORMAL_FONT
->GetFaceName());
1055 // set point size to default value if size was not given
1056 if ( !pointsizefound
)
1057 SetPointSize(wxNORMAL_FONT
->GetPointSize());
1059 // set font weight to default value if weight was not given
1061 SetWeight(wxFONTWEIGHT_NORMAL
);
1064 // set font encoding to default value if encoding was not given
1065 if ( !encodingfound
)
1066 SetEncoding(wxFONTENCODING_SYSTEM
);
1067 #endif // wxUSE_FONTMAP
1072 #endif // generic or wxMSW or wxOS2
1075 // wxFont <-> wxString utilities, used by wxConfig
1076 wxString
wxToString(const wxFontBase
& font
)
1078 return font
.IsOk() ? font
.GetNativeFontInfoDesc()
1082 bool wxFromString(const wxString
& str
, wxFontBase
*font
)
1084 wxCHECK_MSG( font
, false, wxT("NULL output parameter") );
1092 return font
->SetNativeFontInfo(str
);