1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/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 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
32 #include "wx/dcscreen.h"
34 #include "wx/gdicmn.h"
37 #if defined(__WXMSW__)
38 #include "wx/msw/private.h" // includes windows.h for LOGFONT
39 #include "wx/msw/winundef.h"
42 #include "wx/fontutil.h" // for wxNativeFontInfo
43 #include "wx/fontmap.h"
44 #include "wx/fontenum.h"
46 #include "wx/tokenzr.h"
48 // ============================================================================
50 // ============================================================================
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 static void AdjustFontSize(wxFont
& font
, wxDC
& dc
, const wxSize
& pixelSize
)
62 bool initialGoodFound
= false;
63 bool initialBadFound
= false;
65 // NB: this assignment was separated from the variable definition
66 // in order to fix a gcc v3.3.3 compiler crash
67 currentSize
= font
.GetPointSize();
68 while (currentSize
> 0)
72 // if currentSize (in points) results in a font that is smaller
73 // than required by pixelSize it is considered a good size
74 if (dc
.GetCharHeight() <= pixelSize
.GetHeight() &&
75 (!pixelSize
.GetWidth() ||
76 dc
.GetCharWidth() <= pixelSize
.GetWidth()))
78 largestGood
= currentSize
;
79 initialGoodFound
= true;
83 smallestBad
= currentSize
;
84 initialBadFound
= true;
86 if (!initialGoodFound
)
90 else if (!initialBadFound
)
96 int distance
= smallestBad
- largestGood
;
100 currentSize
= largestGood
+ distance
/ 2;
103 font
.SetPointSize(currentSize
);
106 if (currentSize
!= largestGood
)
107 font
.SetPointSize(largestGood
);
110 // ----------------------------------------------------------------------------
112 // ----------------------------------------------------------------------------
114 wxFontEncoding
wxFontBase::ms_encodingDefault
= wxFONTENCODING_SYSTEM
;
117 void wxFontBase::SetDefaultEncoding(wxFontEncoding encoding
)
119 // GetDefaultEncoding() should return something != wxFONTENCODING_DEFAULT
120 // and, besides, using this value here doesn't make any sense
121 wxCHECK_RET( encoding
!= wxFONTENCODING_DEFAULT
,
122 _T("can't set default encoding to wxFONTENCODING_DEFAULT") );
124 ms_encodingDefault
= encoding
;
127 wxFontBase::~wxFontBase()
129 // this destructor is required for Darwin
133 wxFont
*wxFontBase::New(int size
,
138 const wxString
& face
,
139 wxFontEncoding encoding
)
141 return new wxFont(size
, family
, style
, weight
, underlined
, face
, encoding
);
144 static inline int flags2Style(int flags
)
146 return flags
& wxFONTFLAG_ITALIC
148 : flags
& wxFONTFLAG_SLANT
150 : wxFONTSTYLE_NORMAL
;
153 static inline int flags2Weight(int flags
)
155 return flags
& wxFONTFLAG_LIGHT
157 : flags
& wxFONTFLAG_BOLD
159 : wxFONTWEIGHT_NORMAL
;
162 static inline bool flags2Underlined(int flags
)
164 return (flags
& wxFONTFLAG_UNDERLINED
) != 0;
168 wxFont
*wxFontBase::New(int pointSize
,
171 const wxString
& face
,
172 wxFontEncoding encoding
)
174 return New(pointSize
, family
, flags2Style(flags
), flags2Weight(flags
),
175 flags2Underlined(flags
), face
, encoding
);
179 wxFont
*wxFontBase::New(const wxSize
& pixelSize
,
184 const wxString
& face
,
185 wxFontEncoding encoding
)
187 #if defined(__WXMSW__)
188 return new wxFont(pixelSize
, family
, style
, weight
, underlined
,
191 wxFont
*self
= New(10, family
, style
, weight
, underlined
, face
, encoding
);
193 AdjustFontSize(*(wxFont
*)self
, dc
, pixelSize
);
199 wxFont
*wxFontBase::New(const wxSize
& pixelSize
,
202 const wxString
& face
,
203 wxFontEncoding encoding
)
205 return New(pixelSize
, family
, flags2Style(flags
), flags2Weight(flags
),
206 flags2Underlined(flags
), face
, encoding
);
209 wxSize
wxFontBase::GetPixelSize() const
212 dc
.SetFont(*(wxFont
*)this);
213 return wxSize(dc
.GetCharWidth(), dc
.GetCharHeight());
216 bool wxFontBase::IsUsingSizeInPixels() const
221 void wxFontBase::SetPixelSize( const wxSize
& pixelSize
)
224 AdjustFontSize(*(wxFont
*)this, dc
, pixelSize
);
228 wxFont
*wxFontBase::New(const wxNativeFontInfo
& info
)
230 return new wxFont(info
);
234 wxFont
*wxFontBase::New(const wxString
& strNativeFontDesc
)
236 wxNativeFontInfo fontInfo
;
237 if ( !fontInfo
.FromString(strNativeFontDesc
) )
238 return new wxFont(*wxNORMAL_FONT
);
240 return New(fontInfo
);
243 bool wxFontBase::IsFixedWidth() const
245 return GetFamily() == wxFONTFAMILY_TELETYPE
;
248 void wxFontBase::DoSetNativeFontInfo(const wxNativeFontInfo
& info
)
250 #ifdef wxNO_NATIVE_FONTINFO
251 SetPointSize(info
.pointSize
);
252 SetFamily(info
.family
);
253 SetStyle(info
.style
);
254 SetWeight(info
.weight
);
255 SetUnderlined(info
.underlined
);
256 SetFaceName(info
.faceName
);
257 SetEncoding(info
.encoding
);
263 wxString
wxFontBase::GetNativeFontInfoDesc() const
266 const wxNativeFontInfo
*fontInfo
= GetNativeFontInfo();
269 fontDesc
= fontInfo
->ToString();
270 wxASSERT_MSG(!fontDesc
.empty(), wxT("This should be a non-empty string!"));
274 wxFAIL_MSG(wxT("Derived class should have created the wxNativeFontInfo!"));
280 wxString
wxFontBase::GetNativeFontInfoUserDesc() const
283 const wxNativeFontInfo
*fontInfo
= GetNativeFontInfo();
286 fontDesc
= fontInfo
->ToUserString();
287 wxASSERT_MSG(!fontDesc
.empty(), wxT("This should be a non-empty string!"));
291 wxFAIL_MSG(wxT("Derived class should have created the wxNativeFontInfo!"));
297 bool wxFontBase::SetNativeFontInfo(const wxString
& info
)
299 wxNativeFontInfo fontInfo
;
300 if ( !info
.empty() && fontInfo
.FromString(info
) )
302 SetNativeFontInfo(fontInfo
);
309 bool wxFontBase::SetNativeFontInfoUserDesc(const wxString
& info
)
311 wxNativeFontInfo fontInfo
;
312 if ( !info
.empty() && fontInfo
.FromUserString(info
) )
314 SetNativeFontInfo(fontInfo
);
321 bool wxFontBase::operator==(const wxFont
& font
) const
323 // either it is the same font, i.e. they share the same common data or they
324 // have different ref datas but still describe the same font
325 return IsSameAs(font
) ||
328 GetPointSize() == font
.GetPointSize() &&
329 // in wxGTK1 GetPixelSize() calls GetInternalFont() which uses
330 // operator==() resulting in infinite recursion so we can't use it
332 #if !defined(__WXGTK__) || defined(__WXGTK20__)
333 GetPixelSize() == font
.GetPixelSize() &&
335 GetFamily() == font
.GetFamily() &&
336 GetStyle() == font
.GetStyle() &&
337 GetWeight() == font
.GetWeight() &&
338 GetUnderlined() == font
.GetUnderlined() &&
339 GetFaceName().IsSameAs(font
.GetFaceName(), false) &&
340 GetEncoding() == font
.GetEncoding()
344 bool wxFontBase::operator!=(const wxFont
& font
) const
346 return !(*this == font
);
349 wxString
wxFontBase::GetFamilyString() const
351 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
353 switch ( GetFamily() )
355 case wxDECORATIVE
: return wxT("wxDECORATIVE");
356 case wxROMAN
: return wxT("wxROMAN");
357 case wxSCRIPT
: return wxT("wxSCRIPT");
358 case wxSWISS
: return wxT("wxSWISS");
359 case wxMODERN
: return wxT("wxMODERN");
360 case wxTELETYPE
: return wxT("wxTELETYPE");
361 default: return wxT("wxDEFAULT");
365 wxString
wxFontBase::GetStyleString() const
367 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
369 switch ( GetStyle() )
371 case wxNORMAL
: return wxT("wxNORMAL");
372 case wxSLANT
: return wxT("wxSLANT");
373 case wxITALIC
: return wxT("wxITALIC");
374 default: return wxT("wxDEFAULT");
378 wxString
wxFontBase::GetWeightString() const
380 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
382 switch ( GetWeight() )
384 case wxNORMAL
: return wxT("wxNORMAL");
385 case wxBOLD
: return wxT("wxBOLD");
386 case wxLIGHT
: return wxT("wxLIGHT");
387 default: return wxT("wxDEFAULT");
391 bool wxFontBase::SetFaceName(const wxString
& facename
)
394 if (!wxFontEnumerator::IsValidFacename(facename
))
396 UnRef(); // make Ok() return false
399 #else // !wxUSE_FONTENUM
400 wxUnusedVar(facename
);
401 #endif // wxUSE_FONTENUM/!wxUSE_FONTENUM
407 // ----------------------------------------------------------------------------
409 // ----------------------------------------------------------------------------
411 // Up to now, there are no native implementations of this function:
412 void wxNativeFontInfo::SetFaceName(const wxArrayString
& facenames
)
415 for (size_t i
=0; i
< facenames
.GetCount(); i
++)
417 if (wxFontEnumerator::IsValidFacename(facenames
[i
]))
419 SetFaceName(facenames
[i
]);
424 // set the first valid facename we can find on this system
425 wxString validfacename
= wxFontEnumerator::GetFacenames().Item(0);
426 wxLogTrace(wxT("font"), wxT("Falling back to '%s'"), validfacename
.c_str());
427 SetFaceName(validfacename
);
428 #else // !wxUSE_FONTENUM
429 SetFaceName(facenames
[0]);
430 #endif // wxUSE_FONTENUM/!wxUSE_FONTENUM
434 #ifdef wxNO_NATIVE_FONTINFO
436 // These are the generic forms of FromString()/ToString.
438 // convert to/from the string representation: format is
439 // version;pointsize;family;style;weight;underlined;facename;encoding
441 bool wxNativeFontInfo::FromString(const wxString
& s
)
445 wxStringTokenizer
tokenizer(s
, _T(";"));
447 wxString token
= tokenizer
.GetNextToken();
449 // Ignore the version for now
452 token
= tokenizer
.GetNextToken();
453 if ( !token
.ToLong(&l
) )
457 token
= tokenizer
.GetNextToken();
458 if ( !token
.ToLong(&l
) )
460 family
= (wxFontFamily
)l
;
462 token
= tokenizer
.GetNextToken();
463 if ( !token
.ToLong(&l
) )
465 style
= (wxFontStyle
)l
;
467 token
= tokenizer
.GetNextToken();
468 if ( !token
.ToLong(&l
) )
470 weight
= (wxFontWeight
)l
;
472 token
= tokenizer
.GetNextToken();
473 if ( !token
.ToLong(&l
) )
477 faceName
= tokenizer
.GetNextToken();
484 token
= tokenizer
.GetNextToken();
485 if ( !token
.ToLong(&l
) )
487 encoding
= (wxFontEncoding
)l
;
492 wxString
wxNativeFontInfo::ToString() const
496 s
.Printf(_T("%d;%d;%d;%d;%d;%d;%s;%d"),
509 void wxNativeFontInfo::Init()
512 family
= wxFONTFAMILY_DEFAULT
;
513 style
= wxFONTSTYLE_NORMAL
;
514 weight
= wxFONTWEIGHT_NORMAL
;
517 encoding
= wxFONTENCODING_DEFAULT
;
520 int wxNativeFontInfo::GetPointSize() const
525 wxFontStyle
wxNativeFontInfo::GetStyle() const
530 wxFontWeight
wxNativeFontInfo::GetWeight() const
535 bool wxNativeFontInfo::GetUnderlined() const
540 wxString
wxNativeFontInfo::GetFaceName() const
545 wxFontFamily
wxNativeFontInfo::GetFamily() const
550 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
555 void wxNativeFontInfo::SetPointSize(int pointsize
)
557 pointSize
= pointsize
;
560 void wxNativeFontInfo::SetStyle(wxFontStyle style_
)
565 void wxNativeFontInfo::SetWeight(wxFontWeight weight_
)
570 void wxNativeFontInfo::SetUnderlined(bool underlined_
)
572 underlined
= underlined_
;
575 bool wxNativeFontInfo::SetFaceName(const wxString
& facename_
)
577 faceName
= facename_
;
581 void wxNativeFontInfo::SetFamily(wxFontFamily family_
)
586 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding_
)
588 encoding
= encoding_
;
591 #endif // generic wxNativeFontInfo implementation
593 // conversion to/from user-readable string: this is used in the generic
594 // versions and under MSW as well because there is no standard font description
595 // format there anyhow (but there is a well-defined standard for X11 fonts used
596 // by wxGTK and wxMotif)
598 #if defined(wxNO_NATIVE_FONTINFO) || defined(__WXMSW__) || defined (__WXPM__)
600 wxString
wxNativeFontInfo::ToUserString() const
604 // first put the adjectives, if any - this is English-centric, of course,
605 // but what else can we do?
606 if ( GetUnderlined() )
608 desc
<< _("underlined");
611 switch ( GetWeight() )
614 wxFAIL_MSG( _T("unknown font weight") );
617 case wxFONTWEIGHT_NORMAL
:
620 case wxFONTWEIGHT_LIGHT
:
624 case wxFONTWEIGHT_BOLD
:
629 switch ( GetStyle() )
632 wxFAIL_MSG( _T("unknown font style") );
635 case wxFONTSTYLE_NORMAL
:
638 // we don't distinguish between the two for now anyhow...
639 case wxFONTSTYLE_ITALIC
:
640 case wxFONTSTYLE_SLANT
:
641 desc
<< _(" italic");
645 wxString face
= GetFaceName();
648 desc
<< _T(' ') << face
;
651 int size
= GetPointSize();
652 if ( size
!= wxNORMAL_FONT
->GetPointSize() )
654 desc
<< _T(' ') << size
;
658 wxFontEncoding enc
= GetEncoding();
659 if ( enc
!= wxFONTENCODING_DEFAULT
&& enc
!= wxFONTENCODING_SYSTEM
)
661 desc
<< _T(' ') << wxFontMapper::GetEncodingName(enc
);
663 #endif // wxUSE_FONTMAP
665 return desc
.Strip(wxString::both
).MakeLower();
668 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
670 // reset to the default state
673 // parse a more or less free form string
675 // TODO: we should handle at least the quoted facenames
676 wxStringTokenizer
tokenizer(s
, _T(";, "), wxTOKEN_STRTOK
);
680 bool weightfound
= false, pointsizefound
= false;
682 bool encodingfound
= false;
685 while ( tokenizer
.HasMoreTokens() )
687 wxString token
= tokenizer
.GetNextToken();
690 token
.Trim(true).Trim(false).MakeLower();
692 // look for the known tokens
693 if ( token
== _T("underlined") || token
== _("underlined") )
697 else if ( token
== _T("light") || token
== _("light") )
699 SetWeight(wxFONTWEIGHT_LIGHT
);
702 else if ( token
== _T("bold") || token
== _("bold") )
704 SetWeight(wxFONTWEIGHT_BOLD
);
707 else if ( token
== _T("italic") || token
== _("italic") )
709 SetStyle(wxFONTSTYLE_ITALIC
);
711 else if ( token
.ToULong(&size
) )
714 pointsizefound
= true;
719 // try to interpret this as an encoding
720 wxFontEncoding encoding
= wxFontMapper::Get()->CharsetToEncoding(token
, false);
721 if ( encoding
!= wxFONTENCODING_DEFAULT
&&
722 encoding
!= wxFONTENCODING_SYSTEM
) // returned when the recognition failed
724 SetEncoding(encoding
);
725 encodingfound
= true;
729 #endif // wxUSE_FONTMAP
731 // assume it is the face name
739 // skip the code which resets face below
744 #endif // wxUSE_FONTMAP
747 // if we had had the facename, we shouldn't continue appending tokens
748 // to it (i.e. "foo bold bar" shouldn't result in the facename "foo
752 // NB: the check on the facename is implemented in wxFontBase::SetFaceName
753 // and not in wxNativeFontInfo::SetFaceName thus we need to explicitely
754 // call here wxFontEnumerator::IsValidFacename
757 !wxFontEnumerator::IsValidFacename(face
) ||
758 #endif // wxUSE_FONTENUM
761 SetFaceName(wxNORMAL_FONT
->GetFaceName());
768 // we might not have flushed it inside the loop
771 // NB: the check on the facename is implemented in wxFontBase::SetFaceName
772 // and not in wxNativeFontInfo::SetFaceName thus we need to explicitely
773 // call here wxFontEnumerator::IsValidFacename
776 !wxFontEnumerator::IsValidFacename(face
) ||
777 #endif // wxUSE_FONTENUM
780 SetFaceName(wxNORMAL_FONT
->GetFaceName());
784 // set point size to default value if size was not given
785 if ( !pointsizefound
)
786 SetPointSize(wxNORMAL_FONT
->GetPointSize());
788 // set font weight to default value if weight was not given
790 SetWeight(wxFONTWEIGHT_NORMAL
);
793 // set font encoding to default value if encoding was not given
794 if ( !encodingfound
)
795 SetEncoding(wxFONTENCODING_SYSTEM
);
796 #endif // wxUSE_FONTMAP
801 #endif // generic or wxMSW or wxOS2
804 // wxFont <-> wxString utilities, used by wxConfig
805 wxString
wxToString(const wxFontBase
& font
)
807 return font
.IsOk() ? font
.GetNativeFontInfoDesc()
811 bool wxFromString(const wxString
& str
, wxFontBase
*font
)
813 wxCHECK_MSG( font
, false, _T("NULL output parameter") );
821 return font
->SetNativeFontInfo(str
);