1 /////////////////////////////////////////////////////////////////////////////
2 // Name: common/fontcmn.cpp
3 // Purpose: implementation of wxFontBase methods
4 // Author: Vadim Zeitlin
8 // Copyright: (c) wxWindows team
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "fontbase.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
35 #include "wx/gdicmn.h"
36 #include "wx/fontutil.h" // for wxNativeFontInfo
37 #include "wx/fontmap.h"
39 #include "wx/tokenzr.h"
41 // ============================================================================
43 // ============================================================================
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 wxFontEncoding
wxFontBase::ms_encodingDefault
= wxFONTENCODING_SYSTEM
;
52 wxFont
*wxFontBase::New(int size
,
58 wxFontEncoding encoding
)
60 return new wxFont(size
, family
, style
, weight
, underlined
, face
, encoding
);
64 wxFont
*wxFontBase::New(const wxNativeFontInfo
& info
)
66 return new wxFont(info
);
70 wxFont
*wxFontBase::New(const wxString
& strNativeFontDesc
)
72 wxNativeFontInfo fontInfo
;
73 if ( !fontInfo
.FromString(strNativeFontDesc
) )
74 return new wxFont(*wxNORMAL_FONT
);
79 wxNativeFontInfo
*wxFontBase::GetNativeFontInfo() const
81 #ifdef wxNO_NATIVE_FONTINFO
82 wxNativeFontInfo
*fontInfo
= new wxNativeFontInfo();
84 fontInfo
->SetPointSize(GetPointSize());
85 fontInfo
->SetFamily(GetFamily());
86 fontInfo
->SetStyle((wxFontStyle
)GetStyle());
87 fontInfo
->SetWeight((wxFontWeight
)GetWeight());
88 fontInfo
->SetUnderlined(GetUnderlined());
89 fontInfo
->SetFaceName(GetFaceName());
90 fontInfo
->SetEncoding(GetEncoding());
94 return (wxNativeFontInfo
*)NULL
;
98 void wxFontBase::SetNativeFontInfo(const wxNativeFontInfo
& info
)
100 #ifdef wxNO_NATIVE_FONTINFO
101 SetPointSize(info
.pointSize
);
102 SetFamily(info
.family
);
103 SetStyle(info
.style
);
104 SetWeight(info
.weight
);
105 SetUnderlined(info
.underlined
);
106 SetFaceName(info
.faceName
);
107 SetEncoding(info
.encoding
);
113 wxString
wxFontBase::GetNativeFontInfoDesc() const
116 wxNativeFontInfo
*fontInfo
= GetNativeFontInfo();
119 fontDesc
= fontInfo
->ToString();
126 wxString
wxFontBase::GetNativeFontInfoUserDesc() const
129 wxNativeFontInfo
*fontInfo
= GetNativeFontInfo();
132 fontDesc
= fontInfo
->ToUserString();
139 void wxFontBase::SetNativeFontInfo(const wxString
& info
)
141 wxNativeFontInfo fontInfo
;
142 if ( !info
.empty() && fontInfo
.FromString(info
) )
144 SetNativeFontInfo(fontInfo
);
148 void wxFontBase::SetNativeFontInfoUserDesc(const wxString
& info
)
150 wxNativeFontInfo fontInfo
;
151 if ( !info
.empty() && fontInfo
.FromUserString(info
) )
153 SetNativeFontInfo(fontInfo
);
157 wxFont
& wxFont::operator=(const wxFont
& font
)
162 return (wxFont
&)*this;
165 bool wxFontBase::operator==(const wxFont
& font
) const
167 // either it is the same font, i.e. they share the same common data or they
168 // have different ref datas but still describe the same font
169 return GetFontData() == font
.GetFontData() ||
172 GetPointSize() == font
.GetPointSize() &&
173 GetFamily() == font
.GetFamily() &&
174 GetStyle() == font
.GetStyle() &&
175 GetWeight() == font
.GetWeight() &&
176 GetUnderlined() == font
.GetUnderlined() &&
177 GetFaceName() == font
.GetFaceName() &&
178 GetEncoding() == font
.GetEncoding()
182 bool wxFontBase::operator!=(const wxFont
& font
) const
184 return !(*this == font
);
187 wxString
wxFontBase::GetFamilyString() const
189 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
191 switch ( GetFamily() )
193 case wxDECORATIVE
: return wxT("wxDECORATIVE");
194 case wxROMAN
: return wxT("wxROMAN");
195 case wxSCRIPT
: return wxT("wxSCRIPT");
196 case wxSWISS
: return wxT("wxSWISS");
197 case wxMODERN
: return wxT("wxMODERN");
198 case wxTELETYPE
: return wxT("wxTELETYPE");
199 default: return wxT("wxDEFAULT");
203 wxString
wxFontBase::GetStyleString() const
205 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
207 switch ( GetStyle() )
209 case wxNORMAL
: return wxT("wxNORMAL");
210 case wxSLANT
: return wxT("wxSLANT");
211 case wxITALIC
: return wxT("wxITALIC");
212 default: return wxT("wxDEFAULT");
216 wxString
wxFontBase::GetWeightString() const
218 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
220 switch ( GetWeight() )
222 case wxNORMAL
: return wxT("wxNORMAL");
223 case wxBOLD
: return wxT("wxBOLD");
224 case wxLIGHT
: return wxT("wxLIGHT");
225 default: return wxT("wxDEFAULT");
229 // ----------------------------------------------------------------------------
231 // ----------------------------------------------------------------------------
233 #ifdef wxNO_NATIVE_FONTINFO
235 // These are the generic forms of FromString()/ToString.
237 // convert to/from the string representation: format is
238 // version;pointsize;family;style;weight;underlined;facename;encoding
240 bool wxNativeFontInfo::FromString(const wxString
& s
)
244 wxStringTokenizer
tokenizer(s
, _T(";"));
246 wxString token
= tokenizer
.GetNextToken();
248 // Ignore the version for now
251 token
= tokenizer
.GetNextToken();
252 if ( !token
.ToLong(&l
) )
256 token
= tokenizer
.GetNextToken();
257 if ( !token
.ToLong(&l
) )
261 token
= tokenizer
.GetNextToken();
262 if ( !token
.ToLong(&l
) )
264 style
= (wxFontStyle
)l
;
266 token
= tokenizer
.GetNextToken();
267 if ( !token
.ToLong(&l
) )
269 weight
= (wxFontWeight
)l
;
271 token
= tokenizer
.GetNextToken();
272 if ( !token
.ToLong(&l
) )
276 faceName
= tokenizer
.GetNextToken();
280 token
= tokenizer
.GetNextToken();
281 if ( !token
.ToLong(&l
) )
283 encoding
= (wxFontEncoding
)l
;
288 wxString
wxNativeFontInfo::ToString() const
292 s
.Printf(_T("%d;%d;%d;%d;%d;%d;%s;%d"),
305 void wxNativeFontInfo::Init()
307 pointSize
= wxNORMAL_FONT
->GetPointSize();
308 family
= wxFONTFAMILY_DEFAULT
;
309 style
= wxFONTSTYLE_NORMAL
;
310 weight
= wxFONTWEIGHT_NORMAL
;
313 encoding
= wxFONTENCODING_DEFAULT
;
316 int wxNativeFontInfo::GetPointSize() const
321 wxFontStyle
wxNativeFontInfo::GetStyle() const
326 wxFontWeight
wxNativeFontInfo::GetWeight() const
331 bool wxNativeFontInfo::GetUnderlined() const
336 wxString
wxNativeFontInfo::GetFaceName() const
341 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
346 void wxNativeFontInfo::SetPointSize(int pointsize
)
348 pointSize
= pointsize
;
351 void wxNativeFontInfo::SetStyle(wxFontStyle style_
)
356 void wxNativeFontInfo::SetWeight(wxFontWeight weight_
)
361 void wxNativeFontInfo::SetUnderlined(bool underlined_
)
363 underlined
= underlined_
;
366 void wxNativeFontInfo::SetFaceName(wxString facename_
)
368 facename
= facename_
;
371 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding_
)
373 encoding
= encoding_
;
376 #endif // generic wxNativeFontInfo implementation
378 // conversion to/from user-readable string: this is used in the generic
379 // versions and under MSW as well because there is no standard font description
380 // format there anyhow (but there is a well-defined standard for X11 fonts used
381 // by wxGTK and wxMotif)
383 #if defined(wxNO_NATIVE_FONTINFO) || defined(__WXMSW__)
385 wxString
wxNativeFontInfo::ToUserString() const
389 // first put the adjectives, if any - this is English-centric, of course,
390 // but what else can we do?
391 if ( GetUnderlined() )
393 desc
<< _("underlined ");
396 switch ( GetWeight() )
399 wxFAIL_MSG( _T("unknown font weight") );
402 case wxFONTWEIGHT_NORMAL
:
405 case wxFONTWEIGHT_LIGHT
:
409 case wxFONTWEIGHT_BOLD
:
414 switch ( GetStyle() )
417 wxFAIL_MSG( _T("unknown font style") );
420 case wxFONTSTYLE_NORMAL
:
423 // we don't distinguish between the two for now anyhow...
424 case wxFONTSTYLE_ITALIC
:
425 case wxFONTSTYLE_SLANT
:
430 wxString face
= GetFaceName();
433 desc
<< _T(' ') << face
;
436 int size
= GetPointSize();
437 if ( size
!= wxNORMAL_FONT
->GetPointSize() )
439 desc
<< _T(' ') << size
;
442 wxFontEncoding enc
= GetEncoding();
443 if ( enc
!= wxFONTENCODING_DEFAULT
&& enc
!= wxFONTENCODING_SYSTEM
)
445 desc
<< _T(' ') << wxTheFontMapper
->GetEncodingName(enc
);
451 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
453 // reset to the default state
456 // parse a more or less free form string
458 // TODO: we should handle at least the quoted facenames
459 wxStringTokenizer
tokenizer(s
, _T(";, "), wxTOKEN_STRTOK
);
463 wxFontEncoding encoding
;
465 while ( tokenizer
.HasMoreTokens() )
467 wxString token
= tokenizer
.GetNextToken();
470 token
.Trim(TRUE
).Trim(FALSE
).MakeLower();
472 // look for the known tokens
473 if ( token
== _T("underlined") || token
== _("underlined") )
477 else if ( token
== _T("light") || token
== _("light") )
479 SetWeight(wxFONTWEIGHT_LIGHT
);
481 else if ( token
== _T("bold") || token
== _("bold") )
483 SetWeight(wxFONTWEIGHT_BOLD
);
485 else if ( token
== _T("italic") || token
== _("italic") )
487 SetStyle(wxFONTSTYLE_ITALIC
);
489 else if ( token
.ToULong(&size
) )
493 else if ( (encoding
= wxTheFontMapper
->CharsetToEncoding(token
, FALSE
))
494 != wxFONTENCODING_DEFAULT
)
496 SetEncoding(encoding
);
498 else // assume it is the face name
507 // skip the code which resets face below
511 // if we had had the facename, we shouldn't continue appending tokens
512 // to it (i.e. "foo bold bar" shouldn't result in the facename "foo
521 // we might not have flushed it inside the loop
530 #endif // generic or wxMSW