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(int pointSize
,
85 wxFontEncoding encoding
)
91 flags
& wxFONTFLAG_ITALIC
93 : flags
& wxFONTFLAG_SLANT
96 flags
& wxFONTFLAG_LIGHT
98 : flags
& wxFONTFLAG_BOLD
100 : wxFONTWEIGHT_NORMAL
,
101 (flags
& wxFONTFLAG_UNDERLINED
) != 0,
108 wxFont
*wxFontBase::New(const wxNativeFontInfo
& info
)
110 return new wxFont(info
);
114 wxFont
*wxFontBase::New(const wxString
& strNativeFontDesc
)
116 wxNativeFontInfo fontInfo
;
117 if ( !fontInfo
.FromString(strNativeFontDesc
) )
118 return new wxFont(*wxNORMAL_FONT
);
120 return New(fontInfo
);
123 bool wxFontBase::IsFixedWidth() const
125 return GetFamily() == wxFONTFAMILY_TELETYPE
;
128 wxNativeFontInfo
*wxFontBase::GetNativeFontInfo() const
130 #ifdef wxNO_NATIVE_FONTINFO
131 wxNativeFontInfo
*fontInfo
= new wxNativeFontInfo();
133 fontInfo
->SetPointSize(GetPointSize());
134 fontInfo
->SetFamily((wxFontFamily
)GetFamily());
135 fontInfo
->SetStyle((wxFontStyle
)GetStyle());
136 fontInfo
->SetWeight((wxFontWeight
)GetWeight());
137 fontInfo
->SetUnderlined(GetUnderlined());
138 fontInfo
->SetFaceName(GetFaceName());
139 fontInfo
->SetEncoding(GetEncoding());
143 return (wxNativeFontInfo
*)NULL
;
147 void wxFontBase::SetNativeFontInfo(const wxNativeFontInfo
& info
)
149 #ifdef wxNO_NATIVE_FONTINFO
150 SetPointSize(info
.pointSize
);
151 SetFamily(info
.family
);
152 SetStyle(info
.style
);
153 SetWeight(info
.weight
);
154 SetUnderlined(info
.underlined
);
155 SetFaceName(info
.faceName
);
156 SetEncoding(info
.encoding
);
162 wxString
wxFontBase::GetNativeFontInfoDesc() const
165 wxNativeFontInfo
*fontInfo
= GetNativeFontInfo();
168 fontDesc
= fontInfo
->ToString();
175 wxString
wxFontBase::GetNativeFontInfoUserDesc() const
178 wxNativeFontInfo
*fontInfo
= GetNativeFontInfo();
181 fontDesc
= fontInfo
->ToUserString();
188 void wxFontBase::SetNativeFontInfo(const wxString
& info
)
190 wxNativeFontInfo fontInfo
;
191 if ( !info
.empty() && fontInfo
.FromString(info
) )
193 SetNativeFontInfo(fontInfo
);
197 void wxFontBase::SetNativeFontInfoUserDesc(const wxString
& info
)
199 wxNativeFontInfo fontInfo
;
200 if ( !info
.empty() && fontInfo
.FromUserString(info
) )
202 SetNativeFontInfo(fontInfo
);
206 wxFont
& wxFont::operator=(const wxFont
& font
)
211 return (wxFont
&)*this;
214 bool wxFontBase::operator==(const wxFont
& font
) const
216 // either it is the same font, i.e. they share the same common data or they
217 // have different ref datas but still describe the same font
218 return GetFontData() == font
.GetFontData() ||
221 GetPointSize() == font
.GetPointSize() &&
222 GetFamily() == font
.GetFamily() &&
223 GetStyle() == font
.GetStyle() &&
224 GetWeight() == font
.GetWeight() &&
225 GetUnderlined() == font
.GetUnderlined() &&
226 GetFaceName() == font
.GetFaceName() &&
227 GetEncoding() == font
.GetEncoding()
231 bool wxFontBase::operator!=(const wxFont
& font
) const
233 return !(*this == font
);
236 wxString
wxFontBase::GetFamilyString() const
238 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
240 switch ( GetFamily() )
242 case wxDECORATIVE
: return wxT("wxDECORATIVE");
243 case wxROMAN
: return wxT("wxROMAN");
244 case wxSCRIPT
: return wxT("wxSCRIPT");
245 case wxSWISS
: return wxT("wxSWISS");
246 case wxMODERN
: return wxT("wxMODERN");
247 case wxTELETYPE
: return wxT("wxTELETYPE");
248 default: return wxT("wxDEFAULT");
252 wxString
wxFontBase::GetStyleString() const
254 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
256 switch ( GetStyle() )
258 case wxNORMAL
: return wxT("wxNORMAL");
259 case wxSLANT
: return wxT("wxSLANT");
260 case wxITALIC
: return wxT("wxITALIC");
261 default: return wxT("wxDEFAULT");
265 wxString
wxFontBase::GetWeightString() const
267 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
269 switch ( GetWeight() )
271 case wxNORMAL
: return wxT("wxNORMAL");
272 case wxBOLD
: return wxT("wxBOLD");
273 case wxLIGHT
: return wxT("wxLIGHT");
274 default: return wxT("wxDEFAULT");
278 // ----------------------------------------------------------------------------
280 // ----------------------------------------------------------------------------
282 #ifdef wxNO_NATIVE_FONTINFO
284 // These are the generic forms of FromString()/ToString.
286 // convert to/from the string representation: format is
287 // version;pointsize;family;style;weight;underlined;facename;encoding
289 bool wxNativeFontInfo::FromString(const wxString
& s
)
293 wxStringTokenizer
tokenizer(s
, _T(";"));
295 wxString token
= tokenizer
.GetNextToken();
297 // Ignore the version for now
300 token
= tokenizer
.GetNextToken();
301 if ( !token
.ToLong(&l
) )
305 token
= tokenizer
.GetNextToken();
306 if ( !token
.ToLong(&l
) )
308 family
= (wxFontFamily
)l
;
310 token
= tokenizer
.GetNextToken();
311 if ( !token
.ToLong(&l
) )
313 style
= (wxFontStyle
)l
;
315 token
= tokenizer
.GetNextToken();
316 if ( !token
.ToLong(&l
) )
318 weight
= (wxFontWeight
)l
;
320 token
= tokenizer
.GetNextToken();
321 if ( !token
.ToLong(&l
) )
325 faceName
= tokenizer
.GetNextToken();
329 token
= tokenizer
.GetNextToken();
330 if ( !token
.ToLong(&l
) )
332 encoding
= (wxFontEncoding
)l
;
337 wxString
wxNativeFontInfo::ToString() const
341 s
.Printf(_T("%d;%d;%d;%d;%d;%d;%s;%d"),
354 void wxNativeFontInfo::Init()
356 pointSize
= wxNORMAL_FONT
->GetPointSize();
357 family
= wxFONTFAMILY_DEFAULT
;
358 style
= wxFONTSTYLE_NORMAL
;
359 weight
= wxFONTWEIGHT_NORMAL
;
362 encoding
= wxFONTENCODING_DEFAULT
;
365 int wxNativeFontInfo::GetPointSize() const
370 wxFontStyle
wxNativeFontInfo::GetStyle() const
375 wxFontWeight
wxNativeFontInfo::GetWeight() const
380 bool wxNativeFontInfo::GetUnderlined() const
385 wxString
wxNativeFontInfo::GetFaceName() const
390 wxFontFamily
wxNativeFontInfo::GetFamily() const
395 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
400 void wxNativeFontInfo::SetPointSize(int pointsize
)
402 pointSize
= pointsize
;
405 void wxNativeFontInfo::SetStyle(wxFontStyle style_
)
410 void wxNativeFontInfo::SetWeight(wxFontWeight weight_
)
415 void wxNativeFontInfo::SetUnderlined(bool underlined_
)
417 underlined
= underlined_
;
420 void wxNativeFontInfo::SetFaceName(wxString facename_
)
422 faceName
= facename_
;
425 void wxNativeFontInfo::SetFamily(wxFontFamily family_
)
430 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding_
)
432 encoding
= encoding_
;
435 #endif // generic wxNativeFontInfo implementation
437 // conversion to/from user-readable string: this is used in the generic
438 // versions and under MSW as well because there is no standard font description
439 // format there anyhow (but there is a well-defined standard for X11 fonts used
440 // by wxGTK and wxMotif)
442 #if defined(wxNO_NATIVE_FONTINFO) || defined(__WXMSW__) || defined (__WXPM__)
444 wxString
wxNativeFontInfo::ToUserString() const
448 // first put the adjectives, if any - this is English-centric, of course,
449 // but what else can we do?
450 if ( GetUnderlined() )
452 desc
<< _("underlined ");
455 switch ( GetWeight() )
458 wxFAIL_MSG( _T("unknown font weight") );
461 case wxFONTWEIGHT_NORMAL
:
464 case wxFONTWEIGHT_LIGHT
:
468 case wxFONTWEIGHT_BOLD
:
473 switch ( GetStyle() )
476 wxFAIL_MSG( _T("unknown font style") );
479 case wxFONTSTYLE_NORMAL
:
482 // we don't distinguish between the two for now anyhow...
483 case wxFONTSTYLE_ITALIC
:
484 case wxFONTSTYLE_SLANT
:
489 wxString face
= GetFaceName();
492 desc
<< _T(' ') << face
;
495 int size
= GetPointSize();
496 if ( size
!= wxNORMAL_FONT
->GetPointSize() )
498 desc
<< _T(' ') << size
;
502 wxFontEncoding enc
= GetEncoding();
503 if ( enc
!= wxFONTENCODING_DEFAULT
&& enc
!= wxFONTENCODING_SYSTEM
)
505 desc
<< _T(' ') << wxFontMapper::Get()->GetEncodingName(enc
);
507 #endif // wxUSE_FONTMAP
512 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
514 // reset to the default state
517 // parse a more or less free form string
519 // TODO: we should handle at least the quoted facenames
520 wxStringTokenizer
tokenizer(s
, _T(";, "), wxTOKEN_STRTOK
);
526 wxFontEncoding encoding
;
527 #endif // wxUSE_FONTMAP
529 while ( tokenizer
.HasMoreTokens() )
531 wxString token
= tokenizer
.GetNextToken();
534 token
.Trim(TRUE
).Trim(FALSE
).MakeLower();
536 // look for the known tokens
537 if ( token
== _T("underlined") || token
== _("underlined") )
541 else if ( token
== _T("light") || token
== _("light") )
543 SetWeight(wxFONTWEIGHT_LIGHT
);
545 else if ( token
== _T("bold") || token
== _("bold") )
547 SetWeight(wxFONTWEIGHT_BOLD
);
549 else if ( token
== _T("italic") || token
== _("italic") )
551 SetStyle(wxFONTSTYLE_ITALIC
);
553 else if ( token
.ToULong(&size
) )
558 else if ( (encoding
= wxFontMapper::Get()->CharsetToEncoding(token
, FALSE
))
559 != wxFONTENCODING_DEFAULT
)
561 SetEncoding(encoding
);
563 #endif // wxUSE_FONTMAP
564 else // assume it is the face name
573 // skip the code which resets face below
577 // if we had had the facename, we shouldn't continue appending tokens
578 // to it (i.e. "foo bold bar" shouldn't result in the facename "foo
587 // we might not have flushed it inside the loop
596 #endif // generic or wxMSW or wxOS2