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 void wxFontBase::SetDefaultEncoding(wxFontEncoding encoding
)
55 // GetDefaultEncoding() should return something != wxFONTENCODING_DEFAULT
56 // and, besides, using this value here doesn't make any sense
57 wxCHECK_RET( encoding
!= wxFONTENCODING_DEFAULT
,
58 _T("can't set default encoding to wxFONTENCODING_DEFAULT") );
60 ms_encodingDefault
= encoding
;
63 wxFontBase::~wxFontBase()
65 // this destructor is required for Darwin
69 wxFont
*wxFontBase::New(int size
,
75 wxFontEncoding encoding
)
77 return new wxFont(size
, family
, style
, weight
, underlined
, face
, encoding
);
81 wxFont
*wxFontBase::New(const wxNativeFontInfo
& info
)
83 return new wxFont(info
);
87 wxFont
*wxFontBase::New(const wxString
& strNativeFontDesc
)
89 wxNativeFontInfo fontInfo
;
90 if ( !fontInfo
.FromString(strNativeFontDesc
) )
91 return new wxFont(*wxNORMAL_FONT
);
96 bool wxFontBase::IsFixedWidth() const
98 return GetFamily() == wxFONTFAMILY_TELETYPE
;
101 wxNativeFontInfo
*wxFontBase::GetNativeFontInfo() const
103 #ifdef wxNO_NATIVE_FONTINFO
104 wxNativeFontInfo
*fontInfo
= new wxNativeFontInfo();
106 fontInfo
->SetPointSize(GetPointSize());
107 fontInfo
->SetFamily((wxFontFamily
)GetFamily());
108 fontInfo
->SetStyle((wxFontStyle
)GetStyle());
109 fontInfo
->SetWeight((wxFontWeight
)GetWeight());
110 fontInfo
->SetUnderlined(GetUnderlined());
111 fontInfo
->SetFaceName(GetFaceName());
112 fontInfo
->SetEncoding(GetEncoding());
116 return (wxNativeFontInfo
*)NULL
;
120 void wxFontBase::SetNativeFontInfo(const wxNativeFontInfo
& info
)
122 #ifdef wxNO_NATIVE_FONTINFO
123 SetPointSize(info
.pointSize
);
124 SetFamily(info
.family
);
125 SetStyle(info
.style
);
126 SetWeight(info
.weight
);
127 SetUnderlined(info
.underlined
);
128 SetFaceName(info
.faceName
);
129 SetEncoding(info
.encoding
);
135 wxString
wxFontBase::GetNativeFontInfoDesc() const
138 wxNativeFontInfo
*fontInfo
= GetNativeFontInfo();
141 fontDesc
= fontInfo
->ToString();
148 wxString
wxFontBase::GetNativeFontInfoUserDesc() const
151 wxNativeFontInfo
*fontInfo
= GetNativeFontInfo();
154 fontDesc
= fontInfo
->ToUserString();
161 void wxFontBase::SetNativeFontInfo(const wxString
& info
)
163 wxNativeFontInfo fontInfo
;
164 if ( !info
.empty() && fontInfo
.FromString(info
) )
166 SetNativeFontInfo(fontInfo
);
170 void wxFontBase::SetNativeFontInfoUserDesc(const wxString
& info
)
172 wxNativeFontInfo fontInfo
;
173 if ( !info
.empty() && fontInfo
.FromUserString(info
) )
175 SetNativeFontInfo(fontInfo
);
179 wxFont
& wxFont::operator=(const wxFont
& font
)
184 return (wxFont
&)*this;
187 bool wxFontBase::operator==(const wxFont
& font
) const
189 // either it is the same font, i.e. they share the same common data or they
190 // have different ref datas but still describe the same font
191 return GetFontData() == font
.GetFontData() ||
194 GetPointSize() == font
.GetPointSize() &&
195 GetFamily() == font
.GetFamily() &&
196 GetStyle() == font
.GetStyle() &&
197 GetWeight() == font
.GetWeight() &&
198 GetUnderlined() == font
.GetUnderlined() &&
199 GetFaceName() == font
.GetFaceName() &&
200 GetEncoding() == font
.GetEncoding()
204 bool wxFontBase::operator!=(const wxFont
& font
) const
206 return !(*this == font
);
209 wxString
wxFontBase::GetFamilyString() const
211 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
213 switch ( GetFamily() )
215 case wxDECORATIVE
: return wxT("wxDECORATIVE");
216 case wxROMAN
: return wxT("wxROMAN");
217 case wxSCRIPT
: return wxT("wxSCRIPT");
218 case wxSWISS
: return wxT("wxSWISS");
219 case wxMODERN
: return wxT("wxMODERN");
220 case wxTELETYPE
: return wxT("wxTELETYPE");
221 default: return wxT("wxDEFAULT");
225 wxString
wxFontBase::GetStyleString() const
227 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
229 switch ( GetStyle() )
231 case wxNORMAL
: return wxT("wxNORMAL");
232 case wxSLANT
: return wxT("wxSLANT");
233 case wxITALIC
: return wxT("wxITALIC");
234 default: return wxT("wxDEFAULT");
238 wxString
wxFontBase::GetWeightString() const
240 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
242 switch ( GetWeight() )
244 case wxNORMAL
: return wxT("wxNORMAL");
245 case wxBOLD
: return wxT("wxBOLD");
246 case wxLIGHT
: return wxT("wxLIGHT");
247 default: return wxT("wxDEFAULT");
251 // ----------------------------------------------------------------------------
253 // ----------------------------------------------------------------------------
255 #ifdef wxNO_NATIVE_FONTINFO
257 // These are the generic forms of FromString()/ToString.
259 // convert to/from the string representation: format is
260 // version;pointsize;family;style;weight;underlined;facename;encoding
262 bool wxNativeFontInfo::FromString(const wxString
& s
)
266 wxStringTokenizer
tokenizer(s
, _T(";"));
268 wxString token
= tokenizer
.GetNextToken();
270 // Ignore the version for now
273 token
= tokenizer
.GetNextToken();
274 if ( !token
.ToLong(&l
) )
278 token
= tokenizer
.GetNextToken();
279 if ( !token
.ToLong(&l
) )
281 family
= (wxFontFamily
)l
;
283 token
= tokenizer
.GetNextToken();
284 if ( !token
.ToLong(&l
) )
286 style
= (wxFontStyle
)l
;
288 token
= tokenizer
.GetNextToken();
289 if ( !token
.ToLong(&l
) )
291 weight
= (wxFontWeight
)l
;
293 token
= tokenizer
.GetNextToken();
294 if ( !token
.ToLong(&l
) )
298 faceName
= tokenizer
.GetNextToken();
302 token
= tokenizer
.GetNextToken();
303 if ( !token
.ToLong(&l
) )
305 encoding
= (wxFontEncoding
)l
;
310 wxString
wxNativeFontInfo::ToString() const
314 s
.Printf(_T("%d;%d;%d;%d;%d;%d;%s;%d"),
327 void wxNativeFontInfo::Init()
329 pointSize
= wxNORMAL_FONT
->GetPointSize();
330 family
= wxFONTFAMILY_DEFAULT
;
331 style
= wxFONTSTYLE_NORMAL
;
332 weight
= wxFONTWEIGHT_NORMAL
;
335 encoding
= wxFONTENCODING_DEFAULT
;
338 int wxNativeFontInfo::GetPointSize() const
343 wxFontStyle
wxNativeFontInfo::GetStyle() const
348 wxFontWeight
wxNativeFontInfo::GetWeight() const
353 bool wxNativeFontInfo::GetUnderlined() const
358 wxString
wxNativeFontInfo::GetFaceName() const
363 wxFontFamily
wxNativeFontInfo::GetFamily() const
368 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
373 void wxNativeFontInfo::SetPointSize(int pointsize
)
375 pointSize
= pointsize
;
378 void wxNativeFontInfo::SetStyle(wxFontStyle style_
)
383 void wxNativeFontInfo::SetWeight(wxFontWeight weight_
)
388 void wxNativeFontInfo::SetUnderlined(bool underlined_
)
390 underlined
= underlined_
;
393 void wxNativeFontInfo::SetFaceName(wxString facename_
)
395 faceName
= facename_
;
398 void wxNativeFontInfo::SetFamily(wxFontFamily family_
)
403 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding_
)
405 encoding
= encoding_
;
408 #endif // generic wxNativeFontInfo implementation
410 // conversion to/from user-readable string: this is used in the generic
411 // versions and under MSW as well because there is no standard font description
412 // format there anyhow (but there is a well-defined standard for X11 fonts used
413 // by wxGTK and wxMotif)
415 #if defined(wxNO_NATIVE_FONTINFO) || defined(__WXMSW__) || defined (__WXPM__)
417 wxString
wxNativeFontInfo::ToUserString() const
421 // first put the adjectives, if any - this is English-centric, of course,
422 // but what else can we do?
423 if ( GetUnderlined() )
425 desc
<< _("underlined ");
428 switch ( GetWeight() )
431 wxFAIL_MSG( _T("unknown font weight") );
434 case wxFONTWEIGHT_NORMAL
:
437 case wxFONTWEIGHT_LIGHT
:
441 case wxFONTWEIGHT_BOLD
:
446 switch ( GetStyle() )
449 wxFAIL_MSG( _T("unknown font style") );
452 case wxFONTSTYLE_NORMAL
:
455 // we don't distinguish between the two for now anyhow...
456 case wxFONTSTYLE_ITALIC
:
457 case wxFONTSTYLE_SLANT
:
462 wxString face
= GetFaceName();
465 desc
<< _T(' ') << face
;
468 int size
= GetPointSize();
469 if ( size
!= wxNORMAL_FONT
->GetPointSize() )
471 desc
<< _T(' ') << size
;
475 wxFontEncoding enc
= GetEncoding();
476 if ( enc
!= wxFONTENCODING_DEFAULT
&& enc
!= wxFONTENCODING_SYSTEM
)
478 desc
<< _T(' ') << wxFontMapper::Get()->GetEncodingName(enc
);
480 #endif // wxUSE_FONTMAP
485 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
487 // reset to the default state
490 // parse a more or less free form string
492 // TODO: we should handle at least the quoted facenames
493 wxStringTokenizer
tokenizer(s
, _T(";, "), wxTOKEN_STRTOK
);
499 wxFontEncoding encoding
;
500 #endif // wxUSE_FONTMAP
502 while ( tokenizer
.HasMoreTokens() )
504 wxString token
= tokenizer
.GetNextToken();
507 token
.Trim(TRUE
).Trim(FALSE
).MakeLower();
509 // look for the known tokens
510 if ( token
== _T("underlined") || token
== _("underlined") )
514 else if ( token
== _T("light") || token
== _("light") )
516 SetWeight(wxFONTWEIGHT_LIGHT
);
518 else if ( token
== _T("bold") || token
== _("bold") )
520 SetWeight(wxFONTWEIGHT_BOLD
);
522 else if ( token
== _T("italic") || token
== _("italic") )
524 SetStyle(wxFONTSTYLE_ITALIC
);
526 else if ( token
.ToULong(&size
) )
531 else if ( (encoding
= wxFontMapper::Get()->CharsetToEncoding(token
, FALSE
))
532 != wxFONTENCODING_DEFAULT
)
534 SetEncoding(encoding
);
536 #endif // wxUSE_FONTMAP
537 else // assume it is the face name
546 // skip the code which resets face below
550 // if we had had the facename, we shouldn't continue appending tokens
551 // to it (i.e. "foo bold bar" shouldn't result in the facename "foo
560 // we might not have flushed it inside the loop
569 #endif // generic or wxMSW or wxOS2