1 /////////////////////////////////////////////////////////////////////////////
2 // Name: common/fontcmn.cpp
3 // Purpose: implementation of wxFontBase methods
4 // Author: Vadim Zeitlin
8 // Copyright: (c) wxWindows team
9 // Licence: wxWindows licence
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"
38 #if defined(__WXMSW__)
39 #include "wx/msw/private.h" // includes windows.h for LOGFONT
40 #include "wx/msw/winundef.h"
43 #include "wx/fontutil.h" // for wxNativeFontInfo
44 #include "wx/fontmap.h"
46 #include "wx/tokenzr.h"
48 // ============================================================================
50 // ============================================================================
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 wxFontEncoding
wxFontBase::ms_encodingDefault
= wxFONTENCODING_SYSTEM
;
59 void wxFontBase::SetDefaultEncoding(wxFontEncoding encoding
)
61 // GetDefaultEncoding() should return something != wxFONTENCODING_DEFAULT
62 // and, besides, using this value here doesn't make any sense
63 wxCHECK_RET( encoding
!= wxFONTENCODING_DEFAULT
,
64 _T("can't set default encoding to wxFONTENCODING_DEFAULT") );
66 ms_encodingDefault
= encoding
;
69 wxFontBase::~wxFontBase()
71 // this destructor is required for Darwin
75 wxFont
*wxFontBase::New(int size
,
81 wxFontEncoding encoding
)
83 return new wxFont(size
, family
, style
, weight
, underlined
, face
, encoding
);
87 wxFont
*wxFontBase::New(int pointSize
,
91 wxFontEncoding encoding
)
97 flags
& wxFONTFLAG_ITALIC
99 : flags
& wxFONTFLAG_SLANT
101 : wxFONTSTYLE_NORMAL
,
102 flags
& wxFONTFLAG_LIGHT
104 : flags
& wxFONTFLAG_BOLD
106 : wxFONTWEIGHT_NORMAL
,
107 (flags
& wxFONTFLAG_UNDERLINED
) != 0,
114 wxFont
*wxFontBase::New(const wxNativeFontInfo
& info
)
116 return new wxFont(info
);
120 wxFont
*wxFontBase::New(const wxString
& strNativeFontDesc
)
122 wxNativeFontInfo fontInfo
;
123 if ( !fontInfo
.FromString(strNativeFontDesc
) )
124 return new wxFont(*wxNORMAL_FONT
);
126 return New(fontInfo
);
129 bool wxFontBase::IsFixedWidth() const
131 return GetFamily() == wxFONTFAMILY_TELETYPE
;
134 wxNativeFontInfo
*wxFontBase::GetNativeFontInfo() const
136 #ifdef wxNO_NATIVE_FONTINFO
137 wxNativeFontInfo
*fontInfo
= new wxNativeFontInfo();
139 fontInfo
->SetPointSize(GetPointSize());
140 fontInfo
->SetFamily((wxFontFamily
)GetFamily());
141 fontInfo
->SetStyle((wxFontStyle
)GetStyle());
142 fontInfo
->SetWeight((wxFontWeight
)GetWeight());
143 fontInfo
->SetUnderlined(GetUnderlined());
144 fontInfo
->SetFaceName(GetFaceName());
145 fontInfo
->SetEncoding(GetEncoding());
149 return (wxNativeFontInfo
*)NULL
;
153 void wxFontBase::DoSetNativeFontInfo(const wxNativeFontInfo
& info
)
155 #ifdef wxNO_NATIVE_FONTINFO
156 SetPointSize(info
.pointSize
);
157 SetFamily(info
.family
);
158 SetStyle(info
.style
);
159 SetWeight(info
.weight
);
160 SetUnderlined(info
.underlined
);
161 SetFaceName(info
.faceName
);
162 SetEncoding(info
.encoding
);
168 wxString
wxFontBase::GetNativeFontInfoDesc() const
171 wxNativeFontInfo
*fontInfo
= GetNativeFontInfo();
174 fontDesc
= fontInfo
->ToString();
181 wxString
wxFontBase::GetNativeFontInfoUserDesc() const
184 wxNativeFontInfo
*fontInfo
= GetNativeFontInfo();
187 fontDesc
= fontInfo
->ToUserString();
194 void wxFontBase::SetNativeFontInfo(const wxString
& info
)
196 wxNativeFontInfo fontInfo
;
197 if ( !info
.empty() && fontInfo
.FromString(info
) )
199 SetNativeFontInfo(fontInfo
);
203 void wxFontBase::SetNativeFontInfoUserDesc(const wxString
& info
)
205 wxNativeFontInfo fontInfo
;
206 if ( !info
.empty() && fontInfo
.FromUserString(info
) )
208 SetNativeFontInfo(fontInfo
);
212 wxFont
& wxFont::operator=(const wxFont
& font
)
217 return (wxFont
&)*this;
220 bool wxFontBase::operator==(const wxFont
& font
) const
222 // either it is the same font, i.e. they share the same common data or they
223 // have different ref datas but still describe the same font
224 return GetFontData() == font
.GetFontData() ||
227 GetPointSize() == font
.GetPointSize() &&
228 GetFamily() == font
.GetFamily() &&
229 GetStyle() == font
.GetStyle() &&
230 GetWeight() == font
.GetWeight() &&
231 GetUnderlined() == font
.GetUnderlined() &&
232 GetFaceName() == font
.GetFaceName() &&
233 GetEncoding() == font
.GetEncoding()
237 bool wxFontBase::operator!=(const wxFont
& font
) const
239 return !(*this == font
);
242 wxString
wxFontBase::GetFamilyString() const
244 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
246 switch ( GetFamily() )
248 case wxDECORATIVE
: return wxT("wxDECORATIVE");
249 case wxROMAN
: return wxT("wxROMAN");
250 case wxSCRIPT
: return wxT("wxSCRIPT");
251 case wxSWISS
: return wxT("wxSWISS");
252 case wxMODERN
: return wxT("wxMODERN");
253 case wxTELETYPE
: return wxT("wxTELETYPE");
254 default: return wxT("wxDEFAULT");
258 wxString
wxFontBase::GetStyleString() const
260 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
262 switch ( GetStyle() )
264 case wxNORMAL
: return wxT("wxNORMAL");
265 case wxSLANT
: return wxT("wxSLANT");
266 case wxITALIC
: return wxT("wxITALIC");
267 default: return wxT("wxDEFAULT");
271 wxString
wxFontBase::GetWeightString() const
273 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
275 switch ( GetWeight() )
277 case wxNORMAL
: return wxT("wxNORMAL");
278 case wxBOLD
: return wxT("wxBOLD");
279 case wxLIGHT
: return wxT("wxLIGHT");
280 default: return wxT("wxDEFAULT");
284 // ----------------------------------------------------------------------------
286 // ----------------------------------------------------------------------------
288 #ifdef wxNO_NATIVE_FONTINFO
290 // These are the generic forms of FromString()/ToString.
292 // convert to/from the string representation: format is
293 // version;pointsize;family;style;weight;underlined;facename;encoding
295 bool wxNativeFontInfo::FromString(const wxString
& s
)
299 wxStringTokenizer
tokenizer(s
, _T(";"));
301 wxString token
= tokenizer
.GetNextToken();
303 // Ignore the version for now
306 token
= tokenizer
.GetNextToken();
307 if ( !token
.ToLong(&l
) )
311 token
= tokenizer
.GetNextToken();
312 if ( !token
.ToLong(&l
) )
314 family
= (wxFontFamily
)l
;
316 token
= tokenizer
.GetNextToken();
317 if ( !token
.ToLong(&l
) )
319 style
= (wxFontStyle
)l
;
321 token
= tokenizer
.GetNextToken();
322 if ( !token
.ToLong(&l
) )
324 weight
= (wxFontWeight
)l
;
326 token
= tokenizer
.GetNextToken();
327 if ( !token
.ToLong(&l
) )
331 faceName
= tokenizer
.GetNextToken();
338 token
= tokenizer
.GetNextToken();
339 if ( !token
.ToLong(&l
) )
341 encoding
= (wxFontEncoding
)l
;
346 wxString
wxNativeFontInfo::ToString() const
350 s
.Printf(_T("%d;%d;%d;%d;%d;%d;%s;%d"),
363 void wxNativeFontInfo::Init()
365 pointSize
= wxNORMAL_FONT
->GetPointSize();
366 family
= wxFONTFAMILY_DEFAULT
;
367 style
= wxFONTSTYLE_NORMAL
;
368 weight
= wxFONTWEIGHT_NORMAL
;
371 encoding
= wxFONTENCODING_DEFAULT
;
374 int wxNativeFontInfo::GetPointSize() const
379 wxFontStyle
wxNativeFontInfo::GetStyle() const
384 wxFontWeight
wxNativeFontInfo::GetWeight() const
389 bool wxNativeFontInfo::GetUnderlined() const
394 wxString
wxNativeFontInfo::GetFaceName() const
399 wxFontFamily
wxNativeFontInfo::GetFamily() const
404 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
409 void wxNativeFontInfo::SetPointSize(int pointsize
)
411 pointSize
= pointsize
;
414 void wxNativeFontInfo::SetStyle(wxFontStyle style_
)
419 void wxNativeFontInfo::SetWeight(wxFontWeight weight_
)
424 void wxNativeFontInfo::SetUnderlined(bool underlined_
)
426 underlined
= underlined_
;
429 void wxNativeFontInfo::SetFaceName(wxString facename_
)
431 faceName
= facename_
;
434 void wxNativeFontInfo::SetFamily(wxFontFamily family_
)
439 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding_
)
441 encoding
= encoding_
;
444 #endif // generic wxNativeFontInfo implementation
446 // conversion to/from user-readable string: this is used in the generic
447 // versions and under MSW as well because there is no standard font description
448 // format there anyhow (but there is a well-defined standard for X11 fonts used
449 // by wxGTK and wxMotif)
451 #if defined(wxNO_NATIVE_FONTINFO) || defined(__WXMSW__) || defined (__WXPM__)
453 wxString
wxNativeFontInfo::ToUserString() const
457 // first put the adjectives, if any - this is English-centric, of course,
458 // but what else can we do?
459 if ( GetUnderlined() )
461 desc
<< _("underlined ");
464 switch ( GetWeight() )
467 wxFAIL_MSG( _T("unknown font weight") );
470 case wxFONTWEIGHT_NORMAL
:
473 case wxFONTWEIGHT_LIGHT
:
477 case wxFONTWEIGHT_BOLD
:
482 switch ( GetStyle() )
485 wxFAIL_MSG( _T("unknown font style") );
488 case wxFONTSTYLE_NORMAL
:
491 // we don't distinguish between the two for now anyhow...
492 case wxFONTSTYLE_ITALIC
:
493 case wxFONTSTYLE_SLANT
:
498 wxString face
= GetFaceName();
501 desc
<< _T(' ') << face
;
504 int size
= GetPointSize();
505 if ( size
!= wxNORMAL_FONT
->GetPointSize() )
507 desc
<< _T(' ') << size
;
511 wxFontEncoding enc
= GetEncoding();
512 if ( enc
!= wxFONTENCODING_DEFAULT
&& enc
!= wxFONTENCODING_SYSTEM
)
514 desc
<< _T(' ') << wxFontMapper::Get()->GetEncodingName(enc
);
516 #endif // wxUSE_FONTMAP
521 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
523 // reset to the default state
526 // parse a more or less free form string
528 // TODO: we should handle at least the quoted facenames
529 wxStringTokenizer
tokenizer(s
, _T(";, "), wxTOKEN_STRTOK
);
535 wxFontEncoding encoding
;
536 #endif // wxUSE_FONTMAP
538 while ( tokenizer
.HasMoreTokens() )
540 wxString token
= tokenizer
.GetNextToken();
543 token
.Trim(TRUE
).Trim(FALSE
).MakeLower();
545 // look for the known tokens
546 if ( token
== _T("underlined") || token
== _("underlined") )
550 else if ( token
== _T("light") || token
== _("light") )
552 SetWeight(wxFONTWEIGHT_LIGHT
);
554 else if ( token
== _T("bold") || token
== _("bold") )
556 SetWeight(wxFONTWEIGHT_BOLD
);
558 else if ( token
== _T("italic") || token
== _("italic") )
560 SetStyle(wxFONTSTYLE_ITALIC
);
562 else if ( token
.ToULong(&size
) )
567 else if ( (encoding
= wxFontMapper::Get()->CharsetToEncoding(token
, FALSE
))
568 != wxFONTENCODING_DEFAULT
)
570 SetEncoding(encoding
);
572 #endif // wxUSE_FONTMAP
573 else // assume it is the face name
582 // skip the code which resets face below
586 // if we had had the facename, we shouldn't continue appending tokens
587 // to it (i.e. "foo bold bar" shouldn't result in the facename "foo
596 // we might not have flushed it inside the loop
605 #endif // generic or wxMSW or wxOS2