1 /////////////////////////////////////////////////////////////////////////////
2 // Name: common/fontcmn.cpp
3 // Purpose: implementation of wxFontBase methods
4 // Author: Vadim Zeitlin
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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 void wxFontBase::DoSetNativeFontInfo(const wxNativeFontInfo
& info
)
136 #ifdef wxNO_NATIVE_FONTINFO
137 SetPointSize(info
.pointSize
);
138 SetFamily(info
.family
);
139 SetStyle(info
.style
);
140 SetWeight(info
.weight
);
141 SetUnderlined(info
.underlined
);
142 SetFaceName(info
.faceName
);
143 SetEncoding(info
.encoding
);
149 wxString
wxFontBase::GetNativeFontInfoDesc() const
152 const wxNativeFontInfo
*fontInfo
= GetNativeFontInfo();
155 fontDesc
= fontInfo
->ToString();
161 wxString
wxFontBase::GetNativeFontInfoUserDesc() const
164 const wxNativeFontInfo
*fontInfo
= GetNativeFontInfo();
167 fontDesc
= fontInfo
->ToUserString();
173 void wxFontBase::SetNativeFontInfo(const wxString
& info
)
175 wxNativeFontInfo fontInfo
;
176 if ( !info
.empty() && fontInfo
.FromString(info
) )
178 SetNativeFontInfo(fontInfo
);
182 void wxFontBase::SetNativeFontInfoUserDesc(const wxString
& info
)
184 wxNativeFontInfo fontInfo
;
185 if ( !info
.empty() && fontInfo
.FromUserString(info
) )
187 SetNativeFontInfo(fontInfo
);
191 wxFont
& wxFont::operator=(const wxFont
& font
)
196 return (wxFont
&)*this;
199 bool wxFontBase::operator==(const wxFont
& font
) const
201 // either it is the same font, i.e. they share the same common data or they
202 // have different ref datas but still describe the same font
203 return GetFontData() == font
.GetFontData() ||
206 GetPointSize() == font
.GetPointSize() &&
207 GetFamily() == font
.GetFamily() &&
208 GetStyle() == font
.GetStyle() &&
209 GetWeight() == font
.GetWeight() &&
210 GetUnderlined() == font
.GetUnderlined() &&
211 GetFaceName() == font
.GetFaceName() &&
212 GetEncoding() == font
.GetEncoding()
216 bool wxFontBase::operator!=(const wxFont
& font
) const
218 return !(*this == font
);
221 wxString
wxFontBase::GetFamilyString() const
223 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
225 switch ( GetFamily() )
227 case wxDECORATIVE
: return wxT("wxDECORATIVE");
228 case wxROMAN
: return wxT("wxROMAN");
229 case wxSCRIPT
: return wxT("wxSCRIPT");
230 case wxSWISS
: return wxT("wxSWISS");
231 case wxMODERN
: return wxT("wxMODERN");
232 case wxTELETYPE
: return wxT("wxTELETYPE");
233 default: return wxT("wxDEFAULT");
237 wxString
wxFontBase::GetStyleString() const
239 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
241 switch ( GetStyle() )
243 case wxNORMAL
: return wxT("wxNORMAL");
244 case wxSLANT
: return wxT("wxSLANT");
245 case wxITALIC
: return wxT("wxITALIC");
246 default: return wxT("wxDEFAULT");
250 wxString
wxFontBase::GetWeightString() const
252 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
254 switch ( GetWeight() )
256 case wxNORMAL
: return wxT("wxNORMAL");
257 case wxBOLD
: return wxT("wxBOLD");
258 case wxLIGHT
: return wxT("wxLIGHT");
259 default: return wxT("wxDEFAULT");
263 // ----------------------------------------------------------------------------
265 // ----------------------------------------------------------------------------
267 #ifdef wxNO_NATIVE_FONTINFO
269 // These are the generic forms of FromString()/ToString.
271 // convert to/from the string representation: format is
272 // version;pointsize;family;style;weight;underlined;facename;encoding
274 bool wxNativeFontInfo::FromString(const wxString
& s
)
278 wxStringTokenizer
tokenizer(s
, _T(";"));
280 wxString token
= tokenizer
.GetNextToken();
282 // Ignore the version for now
285 token
= tokenizer
.GetNextToken();
286 if ( !token
.ToLong(&l
) )
290 token
= tokenizer
.GetNextToken();
291 if ( !token
.ToLong(&l
) )
293 family
= (wxFontFamily
)l
;
295 token
= tokenizer
.GetNextToken();
296 if ( !token
.ToLong(&l
) )
298 style
= (wxFontStyle
)l
;
300 token
= tokenizer
.GetNextToken();
301 if ( !token
.ToLong(&l
) )
303 weight
= (wxFontWeight
)l
;
305 token
= tokenizer
.GetNextToken();
306 if ( !token
.ToLong(&l
) )
310 faceName
= tokenizer
.GetNextToken();
317 token
= tokenizer
.GetNextToken();
318 if ( !token
.ToLong(&l
) )
320 encoding
= (wxFontEncoding
)l
;
325 wxString
wxNativeFontInfo::ToString() const
329 s
.Printf(_T("%d;%d;%d;%d;%d;%d;%s;%d"),
342 void wxNativeFontInfo::Init()
345 family
= wxFONTFAMILY_DEFAULT
;
346 style
= wxFONTSTYLE_NORMAL
;
347 weight
= wxFONTWEIGHT_NORMAL
;
350 encoding
= wxFONTENCODING_DEFAULT
;
353 int wxNativeFontInfo::GetPointSize() const
358 wxFontStyle
wxNativeFontInfo::GetStyle() const
363 wxFontWeight
wxNativeFontInfo::GetWeight() const
368 bool wxNativeFontInfo::GetUnderlined() const
373 wxString
wxNativeFontInfo::GetFaceName() const
378 wxFontFamily
wxNativeFontInfo::GetFamily() const
383 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
388 void wxNativeFontInfo::SetPointSize(int pointsize
)
390 pointSize
= pointsize
;
393 void wxNativeFontInfo::SetStyle(wxFontStyle style_
)
398 void wxNativeFontInfo::SetWeight(wxFontWeight weight_
)
403 void wxNativeFontInfo::SetUnderlined(bool underlined_
)
405 underlined
= underlined_
;
408 void wxNativeFontInfo::SetFaceName(wxString facename_
)
410 faceName
= facename_
;
413 void wxNativeFontInfo::SetFamily(wxFontFamily family_
)
418 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding_
)
420 encoding
= encoding_
;
423 #endif // generic wxNativeFontInfo implementation
425 // conversion to/from user-readable string: this is used in the generic
426 // versions and under MSW as well because there is no standard font description
427 // format there anyhow (but there is a well-defined standard for X11 fonts used
428 // by wxGTK and wxMotif)
430 #if defined(wxNO_NATIVE_FONTINFO) || defined(__WXMSW__) || defined (__WXPM__)
432 wxString
wxNativeFontInfo::ToUserString() const
436 // first put the adjectives, if any - this is English-centric, of course,
437 // but what else can we do?
438 if ( GetUnderlined() )
440 desc
<< _("underlined ");
443 switch ( GetWeight() )
446 wxFAIL_MSG( _T("unknown font weight") );
449 case wxFONTWEIGHT_NORMAL
:
452 case wxFONTWEIGHT_LIGHT
:
456 case wxFONTWEIGHT_BOLD
:
461 switch ( GetStyle() )
464 wxFAIL_MSG( _T("unknown font style") );
467 case wxFONTSTYLE_NORMAL
:
470 // we don't distinguish between the two for now anyhow...
471 case wxFONTSTYLE_ITALIC
:
472 case wxFONTSTYLE_SLANT
:
477 wxString face
= GetFaceName();
480 desc
<< _T(' ') << face
;
483 int size
= GetPointSize();
484 if ( size
!= wxNORMAL_FONT
->GetPointSize() )
486 desc
<< _T(' ') << size
;
490 wxFontEncoding enc
= GetEncoding();
491 if ( enc
!= wxFONTENCODING_DEFAULT
&& enc
!= wxFONTENCODING_SYSTEM
)
493 desc
<< _T(' ') << wxFontMapper::Get()->GetEncodingName(enc
);
495 #endif // wxUSE_FONTMAP
500 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
502 // reset to the default state
505 // parse a more or less free form string
507 // TODO: we should handle at least the quoted facenames
508 wxStringTokenizer
tokenizer(s
, _T(";, "), wxTOKEN_STRTOK
);
514 wxFontEncoding encoding
;
515 #endif // wxUSE_FONTMAP
517 while ( tokenizer
.HasMoreTokens() )
519 wxString token
= tokenizer
.GetNextToken();
522 token
.Trim(TRUE
).Trim(FALSE
).MakeLower();
524 // look for the known tokens
525 if ( token
== _T("underlined") || token
== _("underlined") )
529 else if ( token
== _T("light") || token
== _("light") )
531 SetWeight(wxFONTWEIGHT_LIGHT
);
533 else if ( token
== _T("bold") || token
== _("bold") )
535 SetWeight(wxFONTWEIGHT_BOLD
);
537 else if ( token
== _T("italic") || token
== _("italic") )
539 SetStyle(wxFONTSTYLE_ITALIC
);
541 else if ( token
.ToULong(&size
) )
546 else if ( (encoding
= wxFontMapper::Get()->CharsetToEncoding(token
, FALSE
))
547 != wxFONTENCODING_DEFAULT
)
549 SetEncoding(encoding
);
551 #endif // wxUSE_FONTMAP
552 else // assume it is the face name
561 // skip the code which resets face below
565 // if we had had the facename, we shouldn't continue appending tokens
566 // to it (i.e. "foo bold bar" shouldn't result in the facename "foo
575 // we might not have flushed it inside the loop
584 #endif // generic or wxMSW or wxOS2