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();
332 token
= tokenizer
.GetNextToken();
333 if ( !token
.ToLong(&l
) )
335 encoding
= (wxFontEncoding
)l
;
340 wxString
wxNativeFontInfo::ToString() const
344 s
.Printf(_T("%d;%d;%d;%d;%d;%d;%s;%d"),
357 void wxNativeFontInfo::Init()
359 pointSize
= wxNORMAL_FONT
->GetPointSize();
360 family
= wxFONTFAMILY_DEFAULT
;
361 style
= wxFONTSTYLE_NORMAL
;
362 weight
= wxFONTWEIGHT_NORMAL
;
365 encoding
= wxFONTENCODING_DEFAULT
;
368 int wxNativeFontInfo::GetPointSize() const
373 wxFontStyle
wxNativeFontInfo::GetStyle() const
378 wxFontWeight
wxNativeFontInfo::GetWeight() const
383 bool wxNativeFontInfo::GetUnderlined() const
388 wxString
wxNativeFontInfo::GetFaceName() const
393 wxFontFamily
wxNativeFontInfo::GetFamily() const
398 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
403 void wxNativeFontInfo::SetPointSize(int pointsize
)
405 pointSize
= pointsize
;
408 void wxNativeFontInfo::SetStyle(wxFontStyle style_
)
413 void wxNativeFontInfo::SetWeight(wxFontWeight weight_
)
418 void wxNativeFontInfo::SetUnderlined(bool underlined_
)
420 underlined
= underlined_
;
423 void wxNativeFontInfo::SetFaceName(wxString facename_
)
425 faceName
= facename_
;
428 void wxNativeFontInfo::SetFamily(wxFontFamily family_
)
433 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding_
)
435 encoding
= encoding_
;
438 #endif // generic wxNativeFontInfo implementation
440 // conversion to/from user-readable string: this is used in the generic
441 // versions and under MSW as well because there is no standard font description
442 // format there anyhow (but there is a well-defined standard for X11 fonts used
443 // by wxGTK and wxMotif)
445 #if defined(wxNO_NATIVE_FONTINFO) || defined(__WXMSW__) || defined (__WXPM__)
447 wxString
wxNativeFontInfo::ToUserString() const
451 // first put the adjectives, if any - this is English-centric, of course,
452 // but what else can we do?
453 if ( GetUnderlined() )
455 desc
<< _("underlined ");
458 switch ( GetWeight() )
461 wxFAIL_MSG( _T("unknown font weight") );
464 case wxFONTWEIGHT_NORMAL
:
467 case wxFONTWEIGHT_LIGHT
:
471 case wxFONTWEIGHT_BOLD
:
476 switch ( GetStyle() )
479 wxFAIL_MSG( _T("unknown font style") );
482 case wxFONTSTYLE_NORMAL
:
485 // we don't distinguish between the two for now anyhow...
486 case wxFONTSTYLE_ITALIC
:
487 case wxFONTSTYLE_SLANT
:
492 wxString face
= GetFaceName();
495 desc
<< _T(' ') << face
;
498 int size
= GetPointSize();
499 if ( size
!= wxNORMAL_FONT
->GetPointSize() )
501 desc
<< _T(' ') << size
;
505 wxFontEncoding enc
= GetEncoding();
506 if ( enc
!= wxFONTENCODING_DEFAULT
&& enc
!= wxFONTENCODING_SYSTEM
)
508 desc
<< _T(' ') << wxFontMapper::Get()->GetEncodingName(enc
);
510 #endif // wxUSE_FONTMAP
515 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
517 // reset to the default state
520 // parse a more or less free form string
522 // TODO: we should handle at least the quoted facenames
523 wxStringTokenizer
tokenizer(s
, _T(";, "), wxTOKEN_STRTOK
);
529 wxFontEncoding encoding
;
530 #endif // wxUSE_FONTMAP
532 while ( tokenizer
.HasMoreTokens() )
534 wxString token
= tokenizer
.GetNextToken();
537 token
.Trim(TRUE
).Trim(FALSE
).MakeLower();
539 // look for the known tokens
540 if ( token
== _T("underlined") || token
== _("underlined") )
544 else if ( token
== _T("light") || token
== _("light") )
546 SetWeight(wxFONTWEIGHT_LIGHT
);
548 else if ( token
== _T("bold") || token
== _("bold") )
550 SetWeight(wxFONTWEIGHT_BOLD
);
552 else if ( token
== _T("italic") || token
== _("italic") )
554 SetStyle(wxFONTSTYLE_ITALIC
);
556 else if ( token
.ToULong(&size
) )
561 else if ( (encoding
= wxFontMapper::Get()->CharsetToEncoding(token
, FALSE
))
562 != wxFONTENCODING_DEFAULT
)
564 SetEncoding(encoding
);
566 #endif // wxUSE_FONTMAP
567 else // assume it is the face name
576 // skip the code which resets face below
580 // if we had had the facename, we shouldn't continue appending tokens
581 // to it (i.e. "foo bold bar" shouldn't result in the facename "foo
590 // we might not have flushed it inside the loop
599 #endif // generic or wxMSW or wxOS2