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 bool wxFontBase::IsFixedWidth() const
87 return GetFamily() == wxFONTFAMILY_TELETYPE
;
90 wxNativeFontInfo
*wxFontBase::GetNativeFontInfo() const
92 #ifdef wxNO_NATIVE_FONTINFO
93 wxNativeFontInfo
*fontInfo
= new wxNativeFontInfo();
95 fontInfo
->SetPointSize(GetPointSize());
96 fontInfo
->SetFamily((wxFontFamily
)GetFamily());
97 fontInfo
->SetStyle((wxFontStyle
)GetStyle());
98 fontInfo
->SetWeight((wxFontWeight
)GetWeight());
99 fontInfo
->SetUnderlined(GetUnderlined());
100 fontInfo
->SetFaceName(GetFaceName());
101 fontInfo
->SetEncoding(GetEncoding());
105 return (wxNativeFontInfo
*)NULL
;
109 void wxFontBase::SetNativeFontInfo(const wxNativeFontInfo
& info
)
111 #ifdef wxNO_NATIVE_FONTINFO
112 SetPointSize(info
.pointSize
);
113 SetFamily(info
.family
);
114 SetStyle(info
.style
);
115 SetWeight(info
.weight
);
116 SetUnderlined(info
.underlined
);
117 SetFaceName(info
.faceName
);
118 SetEncoding(info
.encoding
);
124 wxString
wxFontBase::GetNativeFontInfoDesc() const
127 wxNativeFontInfo
*fontInfo
= GetNativeFontInfo();
130 fontDesc
= fontInfo
->ToString();
137 wxString
wxFontBase::GetNativeFontInfoUserDesc() const
140 wxNativeFontInfo
*fontInfo
= GetNativeFontInfo();
143 fontDesc
= fontInfo
->ToUserString();
150 void wxFontBase::SetNativeFontInfo(const wxString
& info
)
152 wxNativeFontInfo fontInfo
;
153 if ( !info
.empty() && fontInfo
.FromString(info
) )
155 SetNativeFontInfo(fontInfo
);
159 void wxFontBase::SetNativeFontInfoUserDesc(const wxString
& info
)
161 wxNativeFontInfo fontInfo
;
162 if ( !info
.empty() && fontInfo
.FromUserString(info
) )
164 SetNativeFontInfo(fontInfo
);
168 wxFont
& wxFont::operator=(const wxFont
& font
)
173 return (wxFont
&)*this;
176 bool wxFontBase::operator==(const wxFont
& font
) const
178 // either it is the same font, i.e. they share the same common data or they
179 // have different ref datas but still describe the same font
180 return GetFontData() == font
.GetFontData() ||
183 GetPointSize() == font
.GetPointSize() &&
184 GetFamily() == font
.GetFamily() &&
185 GetStyle() == font
.GetStyle() &&
186 GetWeight() == font
.GetWeight() &&
187 GetUnderlined() == font
.GetUnderlined() &&
188 GetFaceName() == font
.GetFaceName() &&
189 GetEncoding() == font
.GetEncoding()
193 bool wxFontBase::operator!=(const wxFont
& font
) const
195 return !(*this == font
);
198 wxString
wxFontBase::GetFamilyString() const
200 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
202 switch ( GetFamily() )
204 case wxDECORATIVE
: return wxT("wxDECORATIVE");
205 case wxROMAN
: return wxT("wxROMAN");
206 case wxSCRIPT
: return wxT("wxSCRIPT");
207 case wxSWISS
: return wxT("wxSWISS");
208 case wxMODERN
: return wxT("wxMODERN");
209 case wxTELETYPE
: return wxT("wxTELETYPE");
210 default: return wxT("wxDEFAULT");
214 wxString
wxFontBase::GetStyleString() const
216 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
218 switch ( GetStyle() )
220 case wxNORMAL
: return wxT("wxNORMAL");
221 case wxSLANT
: return wxT("wxSLANT");
222 case wxITALIC
: return wxT("wxITALIC");
223 default: return wxT("wxDEFAULT");
227 wxString
wxFontBase::GetWeightString() const
229 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
231 switch ( GetWeight() )
233 case wxNORMAL
: return wxT("wxNORMAL");
234 case wxBOLD
: return wxT("wxBOLD");
235 case wxLIGHT
: return wxT("wxLIGHT");
236 default: return wxT("wxDEFAULT");
240 // ----------------------------------------------------------------------------
242 // ----------------------------------------------------------------------------
244 #ifdef wxNO_NATIVE_FONTINFO
246 // These are the generic forms of FromString()/ToString.
248 // convert to/from the string representation: format is
249 // version;pointsize;family;style;weight;underlined;facename;encoding
251 bool wxNativeFontInfo::FromString(const wxString
& s
)
255 wxStringTokenizer
tokenizer(s
, _T(";"));
257 wxString token
= tokenizer
.GetNextToken();
259 // Ignore the version for now
262 token
= tokenizer
.GetNextToken();
263 if ( !token
.ToLong(&l
) )
267 token
= tokenizer
.GetNextToken();
268 if ( !token
.ToLong(&l
) )
270 family
= (wxFontFamily
)l
;
272 token
= tokenizer
.GetNextToken();
273 if ( !token
.ToLong(&l
) )
275 style
= (wxFontStyle
)l
;
277 token
= tokenizer
.GetNextToken();
278 if ( !token
.ToLong(&l
) )
280 weight
= (wxFontWeight
)l
;
282 token
= tokenizer
.GetNextToken();
283 if ( !token
.ToLong(&l
) )
287 faceName
= tokenizer
.GetNextToken();
291 token
= tokenizer
.GetNextToken();
292 if ( !token
.ToLong(&l
) )
294 encoding
= (wxFontEncoding
)l
;
299 wxString
wxNativeFontInfo::ToString() const
303 s
.Printf(_T("%d;%d;%d;%d;%d;%d;%s;%d"),
316 void wxNativeFontInfo::Init()
318 pointSize
= wxNORMAL_FONT
->GetPointSize();
319 family
= wxFONTFAMILY_DEFAULT
;
320 style
= wxFONTSTYLE_NORMAL
;
321 weight
= wxFONTWEIGHT_NORMAL
;
324 encoding
= wxFONTENCODING_DEFAULT
;
327 int wxNativeFontInfo::GetPointSize() const
332 wxFontStyle
wxNativeFontInfo::GetStyle() const
337 wxFontWeight
wxNativeFontInfo::GetWeight() const
342 bool wxNativeFontInfo::GetUnderlined() const
347 wxString
wxNativeFontInfo::GetFaceName() const
352 wxFontFamily
wxNativeFontInfo::GetFamily() const
357 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
362 void wxNativeFontInfo::SetPointSize(int pointsize
)
364 pointSize
= pointsize
;
367 void wxNativeFontInfo::SetStyle(wxFontStyle style_
)
372 void wxNativeFontInfo::SetWeight(wxFontWeight weight_
)
377 void wxNativeFontInfo::SetUnderlined(bool underlined_
)
379 underlined
= underlined_
;
382 void wxNativeFontInfo::SetFaceName(wxString facename_
)
384 faceName
= facename_
;
387 void wxNativeFontInfo::SetFamily(wxFontFamily family_
)
392 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding_
)
394 encoding
= encoding_
;
397 #endif // generic wxNativeFontInfo implementation
399 // conversion to/from user-readable string: this is used in the generic
400 // versions and under MSW as well because there is no standard font description
401 // format there anyhow (but there is a well-defined standard for X11 fonts used
402 // by wxGTK and wxMotif)
404 #if defined(wxNO_NATIVE_FONTINFO) || defined(__WXMSW__) || defined (__WXPM__)
406 wxString
wxNativeFontInfo::ToUserString() const
410 // first put the adjectives, if any - this is English-centric, of course,
411 // but what else can we do?
412 if ( GetUnderlined() )
414 desc
<< _("underlined ");
417 switch ( GetWeight() )
420 wxFAIL_MSG( _T("unknown font weight") );
423 case wxFONTWEIGHT_NORMAL
:
426 case wxFONTWEIGHT_LIGHT
:
430 case wxFONTWEIGHT_BOLD
:
435 switch ( GetStyle() )
438 wxFAIL_MSG( _T("unknown font style") );
441 case wxFONTSTYLE_NORMAL
:
444 // we don't distinguish between the two for now anyhow...
445 case wxFONTSTYLE_ITALIC
:
446 case wxFONTSTYLE_SLANT
:
451 wxString face
= GetFaceName();
454 desc
<< _T(' ') << face
;
457 int size
= GetPointSize();
458 if ( size
!= wxNORMAL_FONT
->GetPointSize() )
460 desc
<< _T(' ') << size
;
464 wxFontEncoding enc
= GetEncoding();
465 if ( enc
!= wxFONTENCODING_DEFAULT
&& enc
!= wxFONTENCODING_SYSTEM
)
467 desc
<< _T(' ') << wxTheFontMapper
->GetEncodingName(enc
);
469 #endif // wxUSE_FONTMAP
474 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
476 // reset to the default state
479 // parse a more or less free form string
481 // TODO: we should handle at least the quoted facenames
482 wxStringTokenizer
tokenizer(s
, _T(";, "), wxTOKEN_STRTOK
);
488 wxFontEncoding encoding
;
489 #endif // wxUSE_FONTMAP
491 while ( tokenizer
.HasMoreTokens() )
493 wxString token
= tokenizer
.GetNextToken();
496 token
.Trim(TRUE
).Trim(FALSE
).MakeLower();
498 // look for the known tokens
499 if ( token
== _T("underlined") || token
== _("underlined") )
503 else if ( token
== _T("light") || token
== _("light") )
505 SetWeight(wxFONTWEIGHT_LIGHT
);
507 else if ( token
== _T("bold") || token
== _("bold") )
509 SetWeight(wxFONTWEIGHT_BOLD
);
511 else if ( token
== _T("italic") || token
== _("italic") )
513 SetStyle(wxFONTSTYLE_ITALIC
);
515 else if ( token
.ToULong(&size
) )
520 else if ( (encoding
= wxTheFontMapper
->CharsetToEncoding(token
, FALSE
))
521 != wxFONTENCODING_DEFAULT
)
523 SetEncoding(encoding
);
525 #endif // wxUSE_FONTMAP
526 else // assume it is the face name
535 // skip the code which resets face below
539 // if we had had the facename, we shouldn't continue appending tokens
540 // to it (i.e. "foo bold bar" shouldn't result in the facename "foo
549 // we might not have flushed it inside the loop
558 #endif // generic or wxMSW or wxOS2