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"
36 #include "wx/gdicmn.h"
37 #include "wx/fontutil.h" // for wxNativeFontInfo
38 #include "wx/fontmap.h"
40 #include "wx/tokenzr.h"
42 // ============================================================================
44 // ============================================================================
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 wxFontEncoding
wxFontBase::ms_encodingDefault
= wxFONTENCODING_SYSTEM
;
53 wxFont
*wxFontBase::New(int size
,
59 wxFontEncoding encoding
)
61 return new wxFont(size
, family
, style
, weight
, underlined
, face
, encoding
);
65 wxFont
*wxFontBase::New(const wxNativeFontInfo
& info
)
67 return new wxFont(info
);
71 wxFont
*wxFontBase::New(const wxString
& strNativeFontDesc
)
73 wxNativeFontInfo fontInfo
;
74 if ( !fontInfo
.FromString(strNativeFontDesc
) )
75 return new wxFont(*wxNORMAL_FONT
);
80 wxNativeFontInfo
*wxFontBase::GetNativeFontInfo() const
82 #ifdef wxNO_NATIVE_FONTINFO
83 wxNativeFontInfo
*fontInfo
= new wxNativeFontInfo();
85 fontInfo
->SetPointSize(GetPointSize());
86 fontInfo
->SetFamily((wxFontFamily
)GetFamily());
87 fontInfo
->SetStyle((wxFontStyle
)GetStyle());
88 fontInfo
->SetWeight((wxFontWeight
)GetWeight());
89 fontInfo
->SetUnderlined(GetUnderlined());
90 fontInfo
->SetFaceName(GetFaceName());
91 fontInfo
->SetEncoding(GetEncoding());
95 return (wxNativeFontInfo
*)NULL
;
99 void wxFontBase::SetNativeFontInfo(const wxNativeFontInfo
& info
)
101 #ifdef wxNO_NATIVE_FONTINFO
102 SetPointSize(info
.pointSize
);
103 SetFamily(info
.family
);
104 SetStyle(info
.style
);
105 SetWeight(info
.weight
);
106 SetUnderlined(info
.underlined
);
107 SetFaceName(info
.faceName
);
108 SetEncoding(info
.encoding
);
114 wxString
wxFontBase::GetNativeFontInfoDesc() const
117 wxNativeFontInfo
*fontInfo
= GetNativeFontInfo();
120 fontDesc
= fontInfo
->ToString();
127 wxString
wxFontBase::GetNativeFontInfoUserDesc() const
130 wxNativeFontInfo
*fontInfo
= GetNativeFontInfo();
133 fontDesc
= fontInfo
->ToUserString();
140 void wxFontBase::SetNativeFontInfo(const wxString
& info
)
142 wxNativeFontInfo fontInfo
;
143 if ( !info
.empty() && fontInfo
.FromString(info
) )
145 SetNativeFontInfo(fontInfo
);
149 void wxFontBase::SetNativeFontInfoUserDesc(const wxString
& info
)
151 wxNativeFontInfo fontInfo
;
152 if ( !info
.empty() && fontInfo
.FromUserString(info
) )
154 SetNativeFontInfo(fontInfo
);
158 wxFont
& wxFont::operator=(const wxFont
& font
)
163 return (wxFont
&)*this;
166 bool wxFontBase::operator==(const wxFont
& font
) const
168 // either it is the same font, i.e. they share the same common data or they
169 // have different ref datas but still describe the same font
170 return GetFontData() == font
.GetFontData() ||
173 GetPointSize() == font
.GetPointSize() &&
174 GetFamily() == font
.GetFamily() &&
175 GetStyle() == font
.GetStyle() &&
176 GetWeight() == font
.GetWeight() &&
177 GetUnderlined() == font
.GetUnderlined() &&
178 GetFaceName() == font
.GetFaceName() &&
179 GetEncoding() == font
.GetEncoding()
183 bool wxFontBase::operator!=(const wxFont
& font
) const
185 return !(*this == font
);
188 wxString
wxFontBase::GetFamilyString() const
190 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
192 switch ( GetFamily() )
194 case wxDECORATIVE
: return wxT("wxDECORATIVE");
195 case wxROMAN
: return wxT("wxROMAN");
196 case wxSCRIPT
: return wxT("wxSCRIPT");
197 case wxSWISS
: return wxT("wxSWISS");
198 case wxMODERN
: return wxT("wxMODERN");
199 case wxTELETYPE
: return wxT("wxTELETYPE");
200 default: return wxT("wxDEFAULT");
204 wxString
wxFontBase::GetStyleString() const
206 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
208 switch ( GetStyle() )
210 case wxNORMAL
: return wxT("wxNORMAL");
211 case wxSLANT
: return wxT("wxSLANT");
212 case wxITALIC
: return wxT("wxITALIC");
213 default: return wxT("wxDEFAULT");
217 wxString
wxFontBase::GetWeightString() const
219 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
221 switch ( GetWeight() )
223 case wxNORMAL
: return wxT("wxNORMAL");
224 case wxBOLD
: return wxT("wxBOLD");
225 case wxLIGHT
: return wxT("wxLIGHT");
226 default: return wxT("wxDEFAULT");
230 // ----------------------------------------------------------------------------
232 // ----------------------------------------------------------------------------
234 #ifdef wxNO_NATIVE_FONTINFO
236 // These are the generic forms of FromString()/ToString.
238 // convert to/from the string representation: format is
239 // version;pointsize;family;style;weight;underlined;facename;encoding
241 bool wxNativeFontInfo::FromString(const wxString
& s
)
245 wxStringTokenizer
tokenizer(s
, _T(";"));
247 wxString token
= tokenizer
.GetNextToken();
249 // Ignore the version for now
252 token
= tokenizer
.GetNextToken();
253 if ( !token
.ToLong(&l
) )
257 token
= tokenizer
.GetNextToken();
258 if ( !token
.ToLong(&l
) )
260 family
= (wxFontFamily
)l
;
262 token
= tokenizer
.GetNextToken();
263 if ( !token
.ToLong(&l
) )
265 style
= (wxFontStyle
)l
;
267 token
= tokenizer
.GetNextToken();
268 if ( !token
.ToLong(&l
) )
270 weight
= (wxFontWeight
)l
;
272 token
= tokenizer
.GetNextToken();
273 if ( !token
.ToLong(&l
) )
277 faceName
= tokenizer
.GetNextToken();
281 token
= tokenizer
.GetNextToken();
282 if ( !token
.ToLong(&l
) )
284 encoding
= (wxFontEncoding
)l
;
289 wxString
wxNativeFontInfo::ToString() const
293 s
.Printf(_T("%d;%d;%d;%d;%d;%d;%s;%d"),
306 void wxNativeFontInfo::Init()
308 pointSize
= wxNORMAL_FONT
->GetPointSize();
309 family
= wxFONTFAMILY_DEFAULT
;
310 style
= wxFONTSTYLE_NORMAL
;
311 weight
= wxFONTWEIGHT_NORMAL
;
314 encoding
= wxFONTENCODING_DEFAULT
;
317 int wxNativeFontInfo::GetPointSize() const
322 wxFontStyle
wxNativeFontInfo::GetStyle() const
327 wxFontWeight
wxNativeFontInfo::GetWeight() const
332 bool wxNativeFontInfo::GetUnderlined() const
337 wxString
wxNativeFontInfo::GetFaceName() const
342 wxFontFamily
wxNativeFontInfo::GetFamily() const
347 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
352 void wxNativeFontInfo::SetPointSize(int pointsize
)
354 pointSize
= pointsize
;
357 void wxNativeFontInfo::SetStyle(wxFontStyle style_
)
362 void wxNativeFontInfo::SetWeight(wxFontWeight weight_
)
367 void wxNativeFontInfo::SetUnderlined(bool underlined_
)
369 underlined
= underlined_
;
372 void wxNativeFontInfo::SetFaceName(wxString facename_
)
374 faceName
= facename_
;
377 void wxNativeFontInfo::SetFamily(wxFontFamily family_
)
382 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding_
)
384 encoding
= encoding_
;
387 #endif // generic wxNativeFontInfo implementation
389 // conversion to/from user-readable string: this is used in the generic
390 // versions and under MSW as well because there is no standard font description
391 // format there anyhow (but there is a well-defined standard for X11 fonts used
392 // by wxGTK and wxMotif)
394 #if defined(wxNO_NATIVE_FONTINFO) || defined(__WXMSW__)
396 wxString
wxNativeFontInfo::ToUserString() const
400 // first put the adjectives, if any - this is English-centric, of course,
401 // but what else can we do?
402 if ( GetUnderlined() )
404 desc
<< _("underlined ");
407 switch ( GetWeight() )
410 wxFAIL_MSG( _T("unknown font weight") );
413 case wxFONTWEIGHT_NORMAL
:
416 case wxFONTWEIGHT_LIGHT
:
420 case wxFONTWEIGHT_BOLD
:
425 switch ( GetStyle() )
428 wxFAIL_MSG( _T("unknown font style") );
431 case wxFONTSTYLE_NORMAL
:
434 // we don't distinguish between the two for now anyhow...
435 case wxFONTSTYLE_ITALIC
:
436 case wxFONTSTYLE_SLANT
:
441 wxString face
= GetFaceName();
444 desc
<< _T(' ') << face
;
447 int size
= GetPointSize();
448 if ( size
!= wxNORMAL_FONT
->GetPointSize() )
450 desc
<< _T(' ') << size
;
454 wxFontEncoding enc
= GetEncoding();
455 if ( enc
!= wxFONTENCODING_DEFAULT
&& enc
!= wxFONTENCODING_SYSTEM
)
457 desc
<< _T(' ') << wxTheFontMapper
->GetEncodingName(enc
);
459 #endif // wxUSE_FONTMAP
464 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
466 // reset to the default state
469 // parse a more or less free form string
471 // TODO: we should handle at least the quoted facenames
472 wxStringTokenizer
tokenizer(s
, _T(";, "), wxTOKEN_STRTOK
);
478 wxFontEncoding encoding
;
479 #endif // wxUSE_FONTMAP
481 while ( tokenizer
.HasMoreTokens() )
483 wxString token
= tokenizer
.GetNextToken();
486 token
.Trim(TRUE
).Trim(FALSE
).MakeLower();
488 // look for the known tokens
489 if ( token
== _T("underlined") || token
== _("underlined") )
493 else if ( token
== _T("light") || token
== _("light") )
495 SetWeight(wxFONTWEIGHT_LIGHT
);
497 else if ( token
== _T("bold") || token
== _("bold") )
499 SetWeight(wxFONTWEIGHT_BOLD
);
501 else if ( token
== _T("italic") || token
== _("italic") )
503 SetStyle(wxFONTSTYLE_ITALIC
);
505 else if ( token
.ToULong(&size
) )
510 else if ( (encoding
= wxTheFontMapper
->CharsetToEncoding(token
, FALSE
))
511 != wxFONTENCODING_DEFAULT
)
513 SetEncoding(encoding
);
515 #endif // wxUSE_FONTMAP
516 else // assume it is the face name
525 // skip the code which resets face below
529 // if we had had the facename, we shouldn't continue appending tokens
530 // to it (i.e. "foo bold bar" shouldn't result in the facename "foo
539 // we might not have flushed it inside the loop
548 #endif // generic or wxMSW