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
;
52 wxFontBase::~wxFontBase()
54 // this destructor is required for Darwin
58 wxFont
*wxFontBase::New(int size
,
64 wxFontEncoding encoding
)
66 return new wxFont(size
, family
, style
, weight
, underlined
, face
, encoding
);
70 wxFont
*wxFontBase::New(const wxNativeFontInfo
& info
)
72 return new wxFont(info
);
76 wxFont
*wxFontBase::New(const wxString
& strNativeFontDesc
)
78 wxNativeFontInfo fontInfo
;
79 if ( !fontInfo
.FromString(strNativeFontDesc
) )
80 return new wxFont(*wxNORMAL_FONT
);
85 wxNativeFontInfo
*wxFontBase::GetNativeFontInfo() const
87 #ifdef wxNO_NATIVE_FONTINFO
88 wxNativeFontInfo
*fontInfo
= new wxNativeFontInfo();
90 fontInfo
->SetPointSize(GetPointSize());
91 fontInfo
->SetFamily((wxFontFamily
)GetFamily());
92 fontInfo
->SetStyle((wxFontStyle
)GetStyle());
93 fontInfo
->SetWeight((wxFontWeight
)GetWeight());
94 fontInfo
->SetUnderlined(GetUnderlined());
95 fontInfo
->SetFaceName(GetFaceName());
96 fontInfo
->SetEncoding(GetEncoding());
100 return (wxNativeFontInfo
*)NULL
;
104 void wxFontBase::SetNativeFontInfo(const wxNativeFontInfo
& info
)
106 #ifdef wxNO_NATIVE_FONTINFO
107 SetPointSize(info
.pointSize
);
108 SetFamily(info
.family
);
109 SetStyle(info
.style
);
110 SetWeight(info
.weight
);
111 SetUnderlined(info
.underlined
);
112 SetFaceName(info
.faceName
);
113 SetEncoding(info
.encoding
);
119 wxString
wxFontBase::GetNativeFontInfoDesc() const
122 wxNativeFontInfo
*fontInfo
= GetNativeFontInfo();
125 fontDesc
= fontInfo
->ToString();
132 wxString
wxFontBase::GetNativeFontInfoUserDesc() const
135 wxNativeFontInfo
*fontInfo
= GetNativeFontInfo();
138 fontDesc
= fontInfo
->ToUserString();
145 void wxFontBase::SetNativeFontInfo(const wxString
& info
)
147 wxNativeFontInfo fontInfo
;
148 if ( !info
.empty() && fontInfo
.FromString(info
) )
150 SetNativeFontInfo(fontInfo
);
154 void wxFontBase::SetNativeFontInfoUserDesc(const wxString
& info
)
156 wxNativeFontInfo fontInfo
;
157 if ( !info
.empty() && fontInfo
.FromUserString(info
) )
159 SetNativeFontInfo(fontInfo
);
163 wxFont
& wxFont::operator=(const wxFont
& font
)
168 return (wxFont
&)*this;
171 bool wxFontBase::operator==(const wxFont
& font
) const
173 // either it is the same font, i.e. they share the same common data or they
174 // have different ref datas but still describe the same font
175 return GetFontData() == font
.GetFontData() ||
178 GetPointSize() == font
.GetPointSize() &&
179 GetFamily() == font
.GetFamily() &&
180 GetStyle() == font
.GetStyle() &&
181 GetWeight() == font
.GetWeight() &&
182 GetUnderlined() == font
.GetUnderlined() &&
183 GetFaceName() == font
.GetFaceName() &&
184 GetEncoding() == font
.GetEncoding()
188 bool wxFontBase::operator!=(const wxFont
& font
) const
190 return !(*this == font
);
193 wxString
wxFontBase::GetFamilyString() const
195 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
197 switch ( GetFamily() )
199 case wxDECORATIVE
: return wxT("wxDECORATIVE");
200 case wxROMAN
: return wxT("wxROMAN");
201 case wxSCRIPT
: return wxT("wxSCRIPT");
202 case wxSWISS
: return wxT("wxSWISS");
203 case wxMODERN
: return wxT("wxMODERN");
204 case wxTELETYPE
: return wxT("wxTELETYPE");
205 default: return wxT("wxDEFAULT");
209 wxString
wxFontBase::GetStyleString() const
211 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
213 switch ( GetStyle() )
215 case wxNORMAL
: return wxT("wxNORMAL");
216 case wxSLANT
: return wxT("wxSLANT");
217 case wxITALIC
: return wxT("wxITALIC");
218 default: return wxT("wxDEFAULT");
222 wxString
wxFontBase::GetWeightString() const
224 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
226 switch ( GetWeight() )
228 case wxNORMAL
: return wxT("wxNORMAL");
229 case wxBOLD
: return wxT("wxBOLD");
230 case wxLIGHT
: return wxT("wxLIGHT");
231 default: return wxT("wxDEFAULT");
235 // ----------------------------------------------------------------------------
237 // ----------------------------------------------------------------------------
239 #ifdef wxNO_NATIVE_FONTINFO
241 // These are the generic forms of FromString()/ToString.
243 // convert to/from the string representation: format is
244 // version;pointsize;family;style;weight;underlined;facename;encoding
246 bool wxNativeFontInfo::FromString(const wxString
& s
)
250 wxStringTokenizer
tokenizer(s
, _T(";"));
252 wxString token
= tokenizer
.GetNextToken();
254 // Ignore the version for now
257 token
= tokenizer
.GetNextToken();
258 if ( !token
.ToLong(&l
) )
262 token
= tokenizer
.GetNextToken();
263 if ( !token
.ToLong(&l
) )
265 family
= (wxFontFamily
)l
;
267 token
= tokenizer
.GetNextToken();
268 if ( !token
.ToLong(&l
) )
270 style
= (wxFontStyle
)l
;
272 token
= tokenizer
.GetNextToken();
273 if ( !token
.ToLong(&l
) )
275 weight
= (wxFontWeight
)l
;
277 token
= tokenizer
.GetNextToken();
278 if ( !token
.ToLong(&l
) )
282 faceName
= tokenizer
.GetNextToken();
286 token
= tokenizer
.GetNextToken();
287 if ( !token
.ToLong(&l
) )
289 encoding
= (wxFontEncoding
)l
;
294 wxString
wxNativeFontInfo::ToString() const
298 s
.Printf(_T("%d;%d;%d;%d;%d;%d;%s;%d"),
311 void wxNativeFontInfo::Init()
313 pointSize
= wxNORMAL_FONT
->GetPointSize();
314 family
= wxFONTFAMILY_DEFAULT
;
315 style
= wxFONTSTYLE_NORMAL
;
316 weight
= wxFONTWEIGHT_NORMAL
;
319 encoding
= wxFONTENCODING_DEFAULT
;
322 int wxNativeFontInfo::GetPointSize() const
327 wxFontStyle
wxNativeFontInfo::GetStyle() const
332 wxFontWeight
wxNativeFontInfo::GetWeight() const
337 bool wxNativeFontInfo::GetUnderlined() const
342 wxString
wxNativeFontInfo::GetFaceName() const
347 wxFontFamily
wxNativeFontInfo::GetFamily() const
352 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
357 void wxNativeFontInfo::SetPointSize(int pointsize
)
359 pointSize
= pointsize
;
362 void wxNativeFontInfo::SetStyle(wxFontStyle style_
)
367 void wxNativeFontInfo::SetWeight(wxFontWeight weight_
)
372 void wxNativeFontInfo::SetUnderlined(bool underlined_
)
374 underlined
= underlined_
;
377 void wxNativeFontInfo::SetFaceName(wxString facename_
)
379 faceName
= facename_
;
382 void wxNativeFontInfo::SetFamily(wxFontFamily family_
)
387 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding_
)
389 encoding
= encoding_
;
392 #endif // generic wxNativeFontInfo implementation
394 // conversion to/from user-readable string: this is used in the generic
395 // versions and under MSW as well because there is no standard font description
396 // format there anyhow (but there is a well-defined standard for X11 fonts used
397 // by wxGTK and wxMotif)
399 #if defined(wxNO_NATIVE_FONTINFO) || defined(__WXMSW__) || defined (__WXPM__)
401 wxString
wxNativeFontInfo::ToUserString() const
405 // first put the adjectives, if any - this is English-centric, of course,
406 // but what else can we do?
407 if ( GetUnderlined() )
409 desc
<< _("underlined ");
412 switch ( GetWeight() )
415 wxFAIL_MSG( _T("unknown font weight") );
418 case wxFONTWEIGHT_NORMAL
:
421 case wxFONTWEIGHT_LIGHT
:
425 case wxFONTWEIGHT_BOLD
:
430 switch ( GetStyle() )
433 wxFAIL_MSG( _T("unknown font style") );
436 case wxFONTSTYLE_NORMAL
:
439 // we don't distinguish between the two for now anyhow...
440 case wxFONTSTYLE_ITALIC
:
441 case wxFONTSTYLE_SLANT
:
446 wxString face
= GetFaceName();
449 desc
<< _T(' ') << face
;
452 int size
= GetPointSize();
453 if ( size
!= wxNORMAL_FONT
->GetPointSize() )
455 desc
<< _T(' ') << size
;
459 wxFontEncoding enc
= GetEncoding();
460 if ( enc
!= wxFONTENCODING_DEFAULT
&& enc
!= wxFONTENCODING_SYSTEM
)
462 desc
<< _T(' ') << wxTheFontMapper
->GetEncodingName(enc
);
464 #endif // wxUSE_FONTMAP
469 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
471 // reset to the default state
474 // parse a more or less free form string
476 // TODO: we should handle at least the quoted facenames
477 wxStringTokenizer
tokenizer(s
, _T(";, "), wxTOKEN_STRTOK
);
483 wxFontEncoding encoding
;
484 #endif // wxUSE_FONTMAP
486 while ( tokenizer
.HasMoreTokens() )
488 wxString token
= tokenizer
.GetNextToken();
491 token
.Trim(TRUE
).Trim(FALSE
).MakeLower();
493 // look for the known tokens
494 if ( token
== _T("underlined") || token
== _("underlined") )
498 else if ( token
== _T("light") || token
== _("light") )
500 SetWeight(wxFONTWEIGHT_LIGHT
);
502 else if ( token
== _T("bold") || token
== _("bold") )
504 SetWeight(wxFONTWEIGHT_BOLD
);
506 else if ( token
== _T("italic") || token
== _("italic") )
508 SetStyle(wxFONTSTYLE_ITALIC
);
510 else if ( token
.ToULong(&size
) )
515 else if ( (encoding
= wxTheFontMapper
->CharsetToEncoding(token
, FALSE
))
516 != wxFONTENCODING_DEFAULT
)
518 SetEncoding(encoding
);
520 #endif // wxUSE_FONTMAP
521 else // assume it is the face name
530 // skip the code which resets face below
534 // if we had had the facename, we shouldn't continue appending tokens
535 // to it (i.e. "foo bold bar" shouldn't result in the facename "foo
544 // we might not have flushed it inside the loop
553 #endif // generic or wxMSW or wxOS2