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
)
61 bool initialGoodFound
= false;
62 bool initialBadFound
= false;
64 // NB: this assignment was separated from the variable definition
65 // in order to fix a gcc v3.3.3 compiler crash
66 int currentSize
= font
.GetPointSize();
67 while (currentSize
> 0)
71 // if currentSize (in points) results in a font that is smaller
72 // than required by pixelSize it is considered a good size
73 if (dc
.GetCharHeight() <= pixelSize
.GetHeight() &&
74 (!pixelSize
.GetWidth() ||
75 dc
.GetCharWidth() <= pixelSize
.GetWidth()))
77 largestGood
= currentSize
;
78 initialGoodFound
= true;
82 smallestBad
= currentSize
;
83 initialBadFound
= true;
85 if (!initialGoodFound
)
89 else if (!initialBadFound
)
95 int distance
= smallestBad
- largestGood
;
99 currentSize
= largestGood
+ distance
/ 2;
102 font
.SetPointSize(currentSize
);
105 if (currentSize
!= largestGood
)
106 font
.SetPointSize(largestGood
);
109 // ----------------------------------------------------------------------------
111 // ----------------------------------------------------------------------------
113 wxFontEncoding
wxFontBase::ms_encodingDefault
= wxFONTENCODING_SYSTEM
;
116 void wxFontBase::SetDefaultEncoding(wxFontEncoding encoding
)
118 // GetDefaultEncoding() should return something != wxFONTENCODING_DEFAULT
119 // and, besides, using this value here doesn't make any sense
120 wxCHECK_RET( encoding
!= wxFONTENCODING_DEFAULT
,
121 _T("can't set default encoding to wxFONTENCODING_DEFAULT") );
123 ms_encodingDefault
= encoding
;
126 wxFontBase::~wxFontBase()
128 // this destructor is required for Darwin
132 wxFont
*wxFontBase::New(int size
,
137 const wxString
& face
,
138 wxFontEncoding encoding
)
140 return new wxFont(size
, family
, style
, weight
, underlined
, face
, encoding
);
143 static inline int flags2Style(int flags
)
145 return flags
& wxFONTFLAG_ITALIC
147 : flags
& wxFONTFLAG_SLANT
149 : wxFONTSTYLE_NORMAL
;
152 static inline int flags2Weight(int flags
)
154 return flags
& wxFONTFLAG_LIGHT
156 : flags
& wxFONTFLAG_BOLD
158 : wxFONTWEIGHT_NORMAL
;
161 static inline bool flags2Underlined(int flags
)
163 return (flags
& wxFONTFLAG_UNDERLINED
) != 0;
167 wxFont
*wxFontBase::New(int pointSize
,
170 const wxString
& face
,
171 wxFontEncoding encoding
)
173 return New(pointSize
, family
, flags2Style(flags
), flags2Weight(flags
),
174 flags2Underlined(flags
), face
, encoding
);
178 wxFont
*wxFontBase::New(const wxSize
& pixelSize
,
183 const wxString
& face
,
184 wxFontEncoding encoding
)
186 #if defined(__WXMSW__)
187 return new wxFont(pixelSize
, family
, style
, weight
, underlined
,
190 wxFont
*self
= New(10, family
, style
, weight
, underlined
, face
, encoding
);
192 AdjustFontSize(*(wxFont
*)self
, dc
, pixelSize
);
198 wxFont
*wxFontBase::New(const wxSize
& pixelSize
,
201 const wxString
& face
,
202 wxFontEncoding encoding
)
204 return New(pixelSize
, family
, flags2Style(flags
), flags2Weight(flags
),
205 flags2Underlined(flags
), face
, encoding
);
208 wxSize
wxFontBase::GetPixelSize() const
211 dc
.SetFont(*(wxFont
*)this);
212 return wxSize(dc
.GetCharWidth(), dc
.GetCharHeight());
215 bool wxFontBase::IsUsingSizeInPixels() const
220 void wxFontBase::SetPixelSize( const wxSize
& pixelSize
)
223 AdjustFontSize(*(wxFont
*)this, dc
, pixelSize
);
227 wxFont
*wxFontBase::New(const wxNativeFontInfo
& info
)
229 return new wxFont(info
);
233 wxFont
*wxFontBase::New(const wxString
& strNativeFontDesc
)
235 wxNativeFontInfo fontInfo
;
236 if ( !fontInfo
.FromString(strNativeFontDesc
) )
237 return new wxFont(*wxNORMAL_FONT
);
239 return New(fontInfo
);
242 bool wxFontBase::IsFixedWidth() const
244 return GetFamily() == wxFONTFAMILY_TELETYPE
;
247 void wxFontBase::DoSetNativeFontInfo(const wxNativeFontInfo
& info
)
249 #ifdef wxNO_NATIVE_FONTINFO
250 SetPointSize(info
.pointSize
);
251 SetFamily(info
.family
);
252 SetStyle(info
.style
);
253 SetWeight(info
.weight
);
254 SetUnderlined(info
.underlined
);
255 SetFaceName(info
.faceName
);
256 SetEncoding(info
.encoding
);
262 wxString
wxFontBase::GetNativeFontInfoDesc() const
265 const wxNativeFontInfo
*fontInfo
= GetNativeFontInfo();
268 fontDesc
= fontInfo
->ToString();
269 wxASSERT_MSG(!fontDesc
.empty(), wxT("This should be a non-empty string!"));
273 wxFAIL_MSG(wxT("Derived class should have created the wxNativeFontInfo!"));
279 wxString
wxFontBase::GetNativeFontInfoUserDesc() const
282 const wxNativeFontInfo
*fontInfo
= GetNativeFontInfo();
285 fontDesc
= fontInfo
->ToUserString();
286 wxASSERT_MSG(!fontDesc
.empty(), wxT("This should be a non-empty string!"));
290 wxFAIL_MSG(wxT("Derived class should have created the wxNativeFontInfo!"));
296 bool wxFontBase::SetNativeFontInfo(const wxString
& info
)
298 wxNativeFontInfo fontInfo
;
299 if ( !info
.empty() && fontInfo
.FromString(info
) )
301 SetNativeFontInfo(fontInfo
);
308 bool wxFontBase::SetNativeFontInfoUserDesc(const wxString
& info
)
310 wxNativeFontInfo fontInfo
;
311 if ( !info
.empty() && fontInfo
.FromUserString(info
) )
313 SetNativeFontInfo(fontInfo
);
320 bool wxFontBase::operator==(const wxFont
& font
) const
322 // either it is the same font, i.e. they share the same common data or they
323 // have different ref datas but still describe the same font
324 return IsSameAs(font
) ||
327 GetPointSize() == font
.GetPointSize() &&
328 // in wxGTK1 GetPixelSize() calls GetInternalFont() which uses
329 // operator==() resulting in infinite recursion so we can't use it
331 #if !defined(__WXGTK__) || defined(__WXGTK20__)
332 GetPixelSize() == font
.GetPixelSize() &&
334 GetFamily() == font
.GetFamily() &&
335 GetStyle() == font
.GetStyle() &&
336 GetWeight() == font
.GetWeight() &&
337 GetUnderlined() == font
.GetUnderlined() &&
338 GetFaceName().IsSameAs(font
.GetFaceName(), false) &&
339 GetEncoding() == font
.GetEncoding()
343 wxString
wxFontBase::GetFamilyString() const
345 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
347 switch ( GetFamily() )
349 case wxDECORATIVE
: return wxT("wxDECORATIVE");
350 case wxROMAN
: return wxT("wxROMAN");
351 case wxSCRIPT
: return wxT("wxSCRIPT");
352 case wxSWISS
: return wxT("wxSWISS");
353 case wxMODERN
: return wxT("wxMODERN");
354 case wxTELETYPE
: return wxT("wxTELETYPE");
355 default: return wxT("wxDEFAULT");
359 wxString
wxFontBase::GetStyleString() const
361 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
363 switch ( GetStyle() )
365 case wxNORMAL
: return wxT("wxNORMAL");
366 case wxSLANT
: return wxT("wxSLANT");
367 case wxITALIC
: return wxT("wxITALIC");
368 default: return wxT("wxDEFAULT");
372 wxString
wxFontBase::GetWeightString() const
374 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
376 switch ( GetWeight() )
378 case wxNORMAL
: return wxT("wxNORMAL");
379 case wxBOLD
: return wxT("wxBOLD");
380 case wxLIGHT
: return wxT("wxLIGHT");
381 default: return wxT("wxDEFAULT");
385 bool wxFontBase::SetFaceName(const wxString
& facename
)
388 if (!wxFontEnumerator::IsValidFacename(facename
))
390 UnRef(); // make Ok() return false
393 #else // !wxUSE_FONTENUM
394 wxUnusedVar(facename
);
395 #endif // wxUSE_FONTENUM/!wxUSE_FONTENUM
401 // ----------------------------------------------------------------------------
403 // ----------------------------------------------------------------------------
405 // Up to now, there are no native implementations of this function:
406 void wxNativeFontInfo::SetFaceName(const wxArrayString
& facenames
)
409 for (size_t i
=0; i
< facenames
.GetCount(); i
++)
411 if (wxFontEnumerator::IsValidFacename(facenames
[i
]))
413 SetFaceName(facenames
[i
]);
418 // set the first valid facename we can find on this system
419 wxString validfacename
= wxFontEnumerator::GetFacenames().Item(0);
420 wxLogTrace(wxT("font"), wxT("Falling back to '%s'"), validfacename
.c_str());
421 SetFaceName(validfacename
);
422 #else // !wxUSE_FONTENUM
423 SetFaceName(facenames
[0]);
424 #endif // wxUSE_FONTENUM/!wxUSE_FONTENUM
428 #ifdef wxNO_NATIVE_FONTINFO
430 // These are the generic forms of FromString()/ToString.
432 // convert to/from the string representation: format is
433 // version;pointsize;family;style;weight;underlined;facename;encoding
435 bool wxNativeFontInfo::FromString(const wxString
& s
)
439 wxStringTokenizer
tokenizer(s
, _T(";"));
441 wxString token
= tokenizer
.GetNextToken();
443 // Ignore the version for now
446 token
= tokenizer
.GetNextToken();
447 if ( !token
.ToLong(&l
) )
451 token
= tokenizer
.GetNextToken();
452 if ( !token
.ToLong(&l
) )
454 family
= (wxFontFamily
)l
;
456 token
= tokenizer
.GetNextToken();
457 if ( !token
.ToLong(&l
) )
459 style
= (wxFontStyle
)l
;
461 token
= tokenizer
.GetNextToken();
462 if ( !token
.ToLong(&l
) )
464 weight
= (wxFontWeight
)l
;
466 token
= tokenizer
.GetNextToken();
467 if ( !token
.ToLong(&l
) )
471 faceName
= tokenizer
.GetNextToken();
478 token
= tokenizer
.GetNextToken();
479 if ( !token
.ToLong(&l
) )
481 encoding
= (wxFontEncoding
)l
;
486 wxString
wxNativeFontInfo::ToString() const
490 s
.Printf(_T("%d;%d;%d;%d;%d;%d;%s;%d"),
503 void wxNativeFontInfo::Init()
506 family
= wxFONTFAMILY_DEFAULT
;
507 style
= wxFONTSTYLE_NORMAL
;
508 weight
= wxFONTWEIGHT_NORMAL
;
511 encoding
= wxFONTENCODING_DEFAULT
;
514 int wxNativeFontInfo::GetPointSize() const
519 wxFontStyle
wxNativeFontInfo::GetStyle() const
524 wxFontWeight
wxNativeFontInfo::GetWeight() const
529 bool wxNativeFontInfo::GetUnderlined() const
534 wxString
wxNativeFontInfo::GetFaceName() const
539 wxFontFamily
wxNativeFontInfo::GetFamily() const
544 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
549 void wxNativeFontInfo::SetPointSize(int pointsize
)
551 pointSize
= pointsize
;
554 void wxNativeFontInfo::SetStyle(wxFontStyle style_
)
559 void wxNativeFontInfo::SetWeight(wxFontWeight weight_
)
564 void wxNativeFontInfo::SetUnderlined(bool underlined_
)
566 underlined
= underlined_
;
569 bool wxNativeFontInfo::SetFaceName(const wxString
& facename_
)
571 faceName
= facename_
;
575 void wxNativeFontInfo::SetFamily(wxFontFamily family_
)
580 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding_
)
582 encoding
= encoding_
;
585 #endif // generic wxNativeFontInfo implementation
587 // conversion to/from user-readable string: this is used in the generic
588 // versions and under MSW as well because there is no standard font description
589 // format there anyhow (but there is a well-defined standard for X11 fonts used
590 // by wxGTK and wxMotif)
592 #if defined(wxNO_NATIVE_FONTINFO) || defined(__WXMSW__) || defined (__WXPM__)
594 wxString
wxNativeFontInfo::ToUserString() const
598 // first put the adjectives, if any - this is English-centric, of course,
599 // but what else can we do?
600 if ( GetUnderlined() )
602 desc
<< _("underlined");
605 switch ( GetWeight() )
608 wxFAIL_MSG( _T("unknown font weight") );
611 case wxFONTWEIGHT_NORMAL
:
614 case wxFONTWEIGHT_LIGHT
:
618 case wxFONTWEIGHT_BOLD
:
623 switch ( GetStyle() )
626 wxFAIL_MSG( _T("unknown font style") );
629 case wxFONTSTYLE_NORMAL
:
632 // we don't distinguish between the two for now anyhow...
633 case wxFONTSTYLE_ITALIC
:
634 case wxFONTSTYLE_SLANT
:
635 desc
<< _(" italic");
639 wxString face
= GetFaceName();
642 desc
<< _T(' ') << face
;
645 int size
= GetPointSize();
646 if ( size
!= wxNORMAL_FONT
->GetPointSize() )
648 desc
<< _T(' ') << size
;
652 wxFontEncoding enc
= GetEncoding();
653 if ( enc
!= wxFONTENCODING_DEFAULT
&& enc
!= wxFONTENCODING_SYSTEM
)
655 desc
<< _T(' ') << wxFontMapper::GetEncodingName(enc
);
657 #endif // wxUSE_FONTMAP
659 return desc
.Strip(wxString::both
).MakeLower();
662 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
664 // reset to the default state
667 // parse a more or less free form string
669 // TODO: we should handle at least the quoted facenames
670 wxStringTokenizer
tokenizer(s
, _T(";, "), wxTOKEN_STRTOK
);
674 bool weightfound
= false, pointsizefound
= false;
676 bool encodingfound
= false;
679 while ( tokenizer
.HasMoreTokens() )
681 wxString token
= tokenizer
.GetNextToken();
684 token
.Trim(true).Trim(false).MakeLower();
686 // look for the known tokens
687 if ( token
== _T("underlined") || token
== _("underlined") )
691 else if ( token
== _T("light") || token
== _("light") )
693 SetWeight(wxFONTWEIGHT_LIGHT
);
696 else if ( token
== _T("bold") || token
== _("bold") )
698 SetWeight(wxFONTWEIGHT_BOLD
);
701 else if ( token
== _T("italic") || token
== _("italic") )
703 SetStyle(wxFONTSTYLE_ITALIC
);
705 else if ( token
.ToULong(&size
) )
708 pointsizefound
= true;
713 // try to interpret this as an encoding
714 wxFontEncoding encoding
= wxFontMapper::Get()->CharsetToEncoding(token
, false);
715 if ( encoding
!= wxFONTENCODING_DEFAULT
&&
716 encoding
!= wxFONTENCODING_SYSTEM
) // returned when the recognition failed
718 SetEncoding(encoding
);
719 encodingfound
= true;
723 #endif // wxUSE_FONTMAP
725 // assume it is the face name
733 // skip the code which resets face below
738 #endif // wxUSE_FONTMAP
741 // if we had had the facename, we shouldn't continue appending tokens
742 // to it (i.e. "foo bold bar" shouldn't result in the facename "foo
746 // NB: the check on the facename is implemented in wxFontBase::SetFaceName
747 // and not in wxNativeFontInfo::SetFaceName thus we need to explicitely
748 // call here wxFontEnumerator::IsValidFacename
751 !wxFontEnumerator::IsValidFacename(face
) ||
752 #endif // wxUSE_FONTENUM
755 SetFaceName(wxNORMAL_FONT
->GetFaceName());
762 // we might not have flushed it inside the loop
765 // NB: the check on the facename is implemented in wxFontBase::SetFaceName
766 // and not in wxNativeFontInfo::SetFaceName thus we need to explicitely
767 // call here wxFontEnumerator::IsValidFacename
770 !wxFontEnumerator::IsValidFacename(face
) ||
771 #endif // wxUSE_FONTENUM
774 SetFaceName(wxNORMAL_FONT
->GetFaceName());
778 // set point size to default value if size was not given
779 if ( !pointsizefound
)
780 SetPointSize(wxNORMAL_FONT
->GetPointSize());
782 // set font weight to default value if weight was not given
784 SetWeight(wxFONTWEIGHT_NORMAL
);
787 // set font encoding to default value if encoding was not given
788 if ( !encodingfound
)
789 SetEncoding(wxFONTENCODING_SYSTEM
);
790 #endif // wxUSE_FONTMAP
795 #endif // generic or wxMSW or wxOS2
798 // wxFont <-> wxString utilities, used by wxConfig
799 wxString
wxToString(const wxFontBase
& font
)
801 return font
.IsOk() ? font
.GetNativeFontInfoDesc()
805 bool wxFromString(const wxString
& str
, wxFontBase
*font
)
807 wxCHECK_MSG( font
, false, _T("NULL output parameter") );
815 return font
->SetNativeFontInfo(str
);