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
);
310 bool wxFontBase::SetNativeFontInfoUserDesc(const wxString
& info
)
312 wxNativeFontInfo fontInfo
;
313 if ( !info
.empty() && fontInfo
.FromUserString(info
) )
315 SetNativeFontInfo(fontInfo
);
323 bool wxFontBase::operator==(const wxFont
& font
) const
325 // either it is the same font, i.e. they share the same common data or they
326 // have different ref datas but still describe the same font
327 return GetFontData() == font
.GetFontData() ||
330 GetPointSize() == font
.GetPointSize() &&
331 GetFamily() == font
.GetFamily() &&
332 GetStyle() == font
.GetStyle() &&
333 GetWeight() == font
.GetWeight() &&
334 GetUnderlined() == font
.GetUnderlined() &&
335 GetFaceName().IsSameAs(font
.GetFaceName(), false) &&
336 GetEncoding() == font
.GetEncoding()
340 bool wxFontBase::operator!=(const wxFont
& font
) const
342 return !(*this == font
);
345 wxString
wxFontBase::GetFamilyString() const
347 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
349 switch ( GetFamily() )
351 case wxDECORATIVE
: return wxT("wxDECORATIVE");
352 case wxROMAN
: return wxT("wxROMAN");
353 case wxSCRIPT
: return wxT("wxSCRIPT");
354 case wxSWISS
: return wxT("wxSWISS");
355 case wxMODERN
: return wxT("wxMODERN");
356 case wxTELETYPE
: return wxT("wxTELETYPE");
357 default: return wxT("wxDEFAULT");
361 wxString
wxFontBase::GetStyleString() const
363 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
365 switch ( GetStyle() )
367 case wxNORMAL
: return wxT("wxNORMAL");
368 case wxSLANT
: return wxT("wxSLANT");
369 case wxITALIC
: return wxT("wxITALIC");
370 default: return wxT("wxDEFAULT");
374 wxString
wxFontBase::GetWeightString() const
376 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
378 switch ( GetWeight() )
380 case wxNORMAL
: return wxT("wxNORMAL");
381 case wxBOLD
: return wxT("wxBOLD");
382 case wxLIGHT
: return wxT("wxLIGHT");
383 default: return wxT("wxDEFAULT");
387 bool wxFontBase::SetFaceName(const wxString
&facename
)
389 if (!wxFontEnumerator::IsValidFacename(facename
))
391 UnRef(); // make Ok() return false
399 // ----------------------------------------------------------------------------
401 // ----------------------------------------------------------------------------
403 // Up to now, there are no native implementations of this function:
404 void wxNativeFontInfo::SetFaceName(const wxArrayString
&facenames
)
406 for (size_t i
=0; i
< facenames
.GetCount(); i
++)
408 if (wxFontEnumerator::IsValidFacename(facenames
[i
]))
410 SetFaceName(facenames
[i
]);
415 // set the first valid facename we can find on this system
416 wxString validfacename
= wxFontEnumerator::GetFacenames().Item(0);
417 wxLogTrace(wxT("font"), wxT("Falling back to '%s'"), validfacename
.c_str());
418 SetFaceName(validfacename
);
422 #ifdef wxNO_NATIVE_FONTINFO
424 // These are the generic forms of FromString()/ToString.
426 // convert to/from the string representation: format is
427 // version;pointsize;family;style;weight;underlined;facename;encoding
429 bool wxNativeFontInfo::FromString(const wxString
& s
)
433 wxStringTokenizer
tokenizer(s
, _T(";"));
435 wxString token
= tokenizer
.GetNextToken();
437 // Ignore the version for now
440 token
= tokenizer
.GetNextToken();
441 if ( !token
.ToLong(&l
) )
445 token
= tokenizer
.GetNextToken();
446 if ( !token
.ToLong(&l
) )
448 family
= (wxFontFamily
)l
;
450 token
= tokenizer
.GetNextToken();
451 if ( !token
.ToLong(&l
) )
453 style
= (wxFontStyle
)l
;
455 token
= tokenizer
.GetNextToken();
456 if ( !token
.ToLong(&l
) )
458 weight
= (wxFontWeight
)l
;
460 token
= tokenizer
.GetNextToken();
461 if ( !token
.ToLong(&l
) )
465 faceName
= tokenizer
.GetNextToken();
472 token
= tokenizer
.GetNextToken();
473 if ( !token
.ToLong(&l
) )
475 encoding
= (wxFontEncoding
)l
;
480 wxString
wxNativeFontInfo::ToString() const
484 s
.Printf(_T("%d;%d;%d;%d;%d;%d;%s;%d"),
497 void wxNativeFontInfo::Init()
500 family
= wxFONTFAMILY_DEFAULT
;
501 style
= wxFONTSTYLE_NORMAL
;
502 weight
= wxFONTWEIGHT_NORMAL
;
505 encoding
= wxFONTENCODING_DEFAULT
;
508 int wxNativeFontInfo::GetPointSize() const
513 wxFontStyle
wxNativeFontInfo::GetStyle() const
518 wxFontWeight
wxNativeFontInfo::GetWeight() const
523 bool wxNativeFontInfo::GetUnderlined() const
528 wxString
wxNativeFontInfo::GetFaceName() const
533 wxFontFamily
wxNativeFontInfo::GetFamily() const
538 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
543 void wxNativeFontInfo::SetPointSize(int pointsize
)
545 pointSize
= pointsize
;
548 void wxNativeFontInfo::SetStyle(wxFontStyle style_
)
553 void wxNativeFontInfo::SetWeight(wxFontWeight weight_
)
558 void wxNativeFontInfo::SetUnderlined(bool underlined_
)
560 underlined
= underlined_
;
563 bool wxNativeFontInfo::SetFaceName(const wxString
& facename_
)
565 faceName
= facename_
;
569 void wxNativeFontInfo::SetFamily(wxFontFamily family_
)
574 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding_
)
576 encoding
= encoding_
;
579 #endif // generic wxNativeFontInfo implementation
581 // conversion to/from user-readable string: this is used in the generic
582 // versions and under MSW as well because there is no standard font description
583 // format there anyhow (but there is a well-defined standard for X11 fonts used
584 // by wxGTK and wxMotif)
586 #if defined(wxNO_NATIVE_FONTINFO) || defined(__WXMSW__) || defined (__WXPM__)
588 wxString
wxNativeFontInfo::ToUserString() const
592 // first put the adjectives, if any - this is English-centric, of course,
593 // but what else can we do?
594 if ( GetUnderlined() )
596 desc
<< _("underlined");
599 switch ( GetWeight() )
602 wxFAIL_MSG( _T("unknown font weight") );
605 case wxFONTWEIGHT_NORMAL
:
608 case wxFONTWEIGHT_LIGHT
:
612 case wxFONTWEIGHT_BOLD
:
617 switch ( GetStyle() )
620 wxFAIL_MSG( _T("unknown font style") );
623 case wxFONTSTYLE_NORMAL
:
626 // we don't distinguish between the two for now anyhow...
627 case wxFONTSTYLE_ITALIC
:
628 case wxFONTSTYLE_SLANT
:
629 desc
<< _(" italic");
633 wxString face
= GetFaceName();
636 desc
<< _T(' ') << face
;
639 int size
= GetPointSize();
640 if ( size
!= wxNORMAL_FONT
->GetPointSize() )
642 desc
<< _T(' ') << size
;
646 wxFontEncoding enc
= GetEncoding();
647 if ( enc
!= wxFONTENCODING_DEFAULT
&& enc
!= wxFONTENCODING_SYSTEM
)
649 desc
<< _T(' ') << wxFontMapper::GetEncodingName(enc
);
651 #endif // wxUSE_FONTMAP
653 return desc
.Strip(wxString::both
).MakeLower();
656 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
658 // reset to the default state
661 // parse a more or less free form string
663 // TODO: we should handle at least the quoted facenames
664 wxStringTokenizer
tokenizer(s
, _T(";, "), wxTOKEN_STRTOK
);
668 bool weightfound
= false, pointsizefound
= false, encodingfound
= false;
670 while ( tokenizer
.HasMoreTokens() )
672 wxString token
= tokenizer
.GetNextToken();
675 token
.Trim(true).Trim(false).MakeLower();
677 // look for the known tokens
678 if ( token
== _T("underlined") || token
== _("underlined") )
682 else if ( token
== _T("light") || token
== _("light") )
684 SetWeight(wxFONTWEIGHT_LIGHT
);
687 else if ( token
== _T("bold") || token
== _("bold") )
689 SetWeight(wxFONTWEIGHT_BOLD
);
692 else if ( token
== _T("italic") || token
== _("italic") )
694 SetStyle(wxFONTSTYLE_ITALIC
);
696 else if ( token
.ToULong(&size
) )
699 pointsizefound
= true;
704 // try to interpret this as an encoding
705 wxFontEncoding encoding
= wxFontMapper::Get()->CharsetToEncoding(token
, false);
706 if ( encoding
!= wxFONTENCODING_DEFAULT
&&
707 encoding
!= wxFONTENCODING_SYSTEM
) // returned when the recognition failed
709 SetEncoding(encoding
);
710 encodingfound
= true;
714 #endif // wxUSE_FONTMAP
716 // assume it is the face name
724 // skip the code which resets face below
729 #endif // wxUSE_FONTMAP
732 // if we had had the facename, we shouldn't continue appending tokens
733 // to it (i.e. "foo bold bar" shouldn't result in the facename "foo
737 // NB: the check on the facename is implemented in wxFontBase::SetFaceName
738 // and not in wxNativeFontInfo::SetFaceName thus we need to explicitely
739 // call here wxFontEnumerator::IsValidFacename
740 if (!wxFontEnumerator::IsValidFacename(face
) ||
742 SetFaceName(wxNORMAL_FONT
->GetFaceName());
747 // we might not have flushed it inside the loop
750 // NB: the check on the facename is implemented in wxFontBase::SetFaceName
751 // and not in wxNativeFontInfo::SetFaceName thus we need to explicitely
752 // call here wxFontEnumerator::IsValidFacename
753 if (!wxFontEnumerator::IsValidFacename(face
) ||
755 SetFaceName(wxNORMAL_FONT
->GetFaceName());
758 // set point size to default value if size was not given
759 if ( !pointsizefound
)
760 SetPointSize(wxNORMAL_FONT
->GetPointSize());
762 // set font weight to default value if weight was not given
764 SetWeight(wxFONTWEIGHT_NORMAL
);
767 // set font encoding to default value if encoding was not given
768 if ( !encodingfound
)
769 SetEncoding(wxFONTENCODING_SYSTEM
);
770 #endif // wxUSE_FONTMAP
775 #endif // generic or wxMSW or wxOS2