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 // debugger helper: this function can be called from a debugger to show what
50 extern const char *wxDumpFont(const wxFont
*font
)
54 const wxFontWeight weight
= font
->GetWeight();
57 s
.Printf(wxS("%s-%s-%s-%d-%d"),
59 weight
== wxFONTWEIGHT_NORMAL
61 : weight
== wxFONTWEIGHT_BOLD
64 font
->GetStyle() == wxFONTSTYLE_NORMAL
70 wxStrlcpy(buf
, s
.mb_str(), WXSIZEOF(buf
));
74 // ============================================================================
76 // ============================================================================
78 // ----------------------------------------------------------------------------
80 // ----------------------------------------------------------------------------
82 static inline int flags2Style(int flags
)
84 return flags
& wxFONTFLAG_ITALIC
86 : flags
& wxFONTFLAG_SLANT
91 static inline int flags2Weight(int flags
)
93 return flags
& wxFONTFLAG_LIGHT
95 : flags
& wxFONTFLAG_BOLD
97 : wxFONTWEIGHT_NORMAL
;
100 static inline bool flags2Underlined(int flags
)
102 return (flags
& wxFONTFLAG_UNDERLINED
) != 0;
105 // ----------------------------------------------------------------------------
107 // ----------------------------------------------------------------------------
109 wxFontEncoding
wxFontBase::ms_encodingDefault
= wxFONTENCODING_SYSTEM
;
112 void wxFontBase::SetDefaultEncoding(wxFontEncoding encoding
)
114 // GetDefaultEncoding() should return something != wxFONTENCODING_DEFAULT
115 // and, besides, using this value here doesn't make any sense
116 wxCHECK_RET( encoding
!= wxFONTENCODING_DEFAULT
,
117 _T("can't set default encoding to wxFONTENCODING_DEFAULT") );
119 ms_encodingDefault
= encoding
;
122 wxFontBase::~wxFontBase()
124 // this destructor is required for Darwin
128 wxFont
*wxFontBase::New(int size
,
133 const wxString
& face
,
134 wxFontEncoding encoding
)
136 return new wxFont(size
, family
, style
, weight
, underlined
, face
, encoding
);
140 wxFont
*wxFontBase::New(const wxSize
& pixelSize
,
145 const wxString
& face
,
146 wxFontEncoding encoding
)
148 return new wxFont(pixelSize
, family
, style
, weight
, underlined
,
153 wxFont
*wxFontBase::New(int pointSize
,
156 const wxString
& face
,
157 wxFontEncoding encoding
)
159 return New(pointSize
, family
, flags2Style(flags
), flags2Weight(flags
),
160 flags2Underlined(flags
), face
, encoding
);
164 wxFont
*wxFontBase::New(const wxSize
& pixelSize
,
167 const wxString
& face
,
168 wxFontEncoding encoding
)
170 return New(pixelSize
, family
, flags2Style(flags
), flags2Weight(flags
),
171 flags2Underlined(flags
), face
, encoding
);
175 wxFont
*wxFontBase::New(const wxNativeFontInfo
& info
)
177 return new wxFont(info
);
181 wxFont
*wxFontBase::New(const wxString
& strNativeFontDesc
)
183 wxNativeFontInfo fontInfo
;
184 if ( !fontInfo
.FromString(strNativeFontDesc
) )
185 return new wxFont(*wxNORMAL_FONT
);
187 return New(fontInfo
);
190 bool wxFontBase::IsFixedWidth() const
192 return GetFamily() == wxFONTFAMILY_TELETYPE
;
195 wxSize
wxFontBase::GetPixelSize() const
198 dc
.SetFont(*(wxFont
*)this);
199 return wxSize(dc
.GetCharWidth(), dc
.GetCharHeight());
202 bool wxFontBase::IsUsingSizeInPixels() const
207 void wxFontBase::SetPixelSize( const wxSize
& pixelSize
)
209 wxCHECK_RET( pixelSize
.GetWidth() >= 0 && pixelSize
.GetHeight() > 0,
210 "Negative values for the pixel size or zero pixel height are not allowed" );
214 // NOTE: this algorithm for adjusting the font size is used by all
215 // implementations of wxFont except under wxMSW and wxGTK where
216 // native support to font creation using pixel-size is provided.
221 bool initialGoodFound
= false;
222 bool initialBadFound
= false;
224 // NB: this assignment was separated from the variable definition
225 // in order to fix a gcc v3.3.3 compiler crash
226 int currentSize
= GetPointSize();
227 while (currentSize
> 0)
229 dc
.SetFont(*static_cast<wxFont
*>(this));
231 // if currentSize (in points) results in a font that is smaller
232 // than required by pixelSize it is considered a good size
233 // NOTE: the pixel size width may be zero
234 if (dc
.GetCharHeight() <= pixelSize
.GetHeight() &&
235 (pixelSize
.GetWidth() == 0 ||
236 dc
.GetCharWidth() <= pixelSize
.GetWidth()))
238 largestGood
= currentSize
;
239 initialGoodFound
= true;
243 smallestBad
= currentSize
;
244 initialBadFound
= true;
246 if (!initialGoodFound
)
250 else if (!initialBadFound
)
256 int distance
= smallestBad
- largestGood
;
260 currentSize
= largestGood
+ distance
/ 2;
263 SetPointSize(currentSize
);
266 if (currentSize
!= largestGood
)
267 SetPointSize(largestGood
);
270 void wxFontBase::DoSetNativeFontInfo(const wxNativeFontInfo
& info
)
272 #ifdef wxNO_NATIVE_FONTINFO
273 SetPointSize(info
.pointSize
);
274 SetFamily(info
.family
);
275 SetStyle(info
.style
);
276 SetWeight(info
.weight
);
277 SetUnderlined(info
.underlined
);
278 SetFaceName(info
.faceName
);
279 SetEncoding(info
.encoding
);
285 wxString
wxFontBase::GetNativeFontInfoDesc() const
288 const wxNativeFontInfo
*fontInfo
= GetNativeFontInfo();
291 fontDesc
= fontInfo
->ToString();
292 wxASSERT_MSG(!fontDesc
.empty(), wxT("This should be a non-empty string!"));
296 wxFAIL_MSG(wxT("Derived class should have created the wxNativeFontInfo!"));
302 wxString
wxFontBase::GetNativeFontInfoUserDesc() const
305 const wxNativeFontInfo
*fontInfo
= GetNativeFontInfo();
308 fontDesc
= fontInfo
->ToUserString();
309 wxASSERT_MSG(!fontDesc
.empty(), wxT("This should be a non-empty string!"));
313 wxFAIL_MSG(wxT("Derived class should have created the wxNativeFontInfo!"));
319 bool wxFontBase::SetNativeFontInfo(const wxString
& info
)
321 wxNativeFontInfo fontInfo
;
322 if ( !info
.empty() && fontInfo
.FromString(info
) )
324 SetNativeFontInfo(fontInfo
);
331 bool wxFontBase::SetNativeFontInfoUserDesc(const wxString
& info
)
333 wxNativeFontInfo fontInfo
;
334 if ( !info
.empty() && fontInfo
.FromUserString(info
) )
336 SetNativeFontInfo(fontInfo
);
343 bool wxFontBase::operator==(const wxFont
& font
) const
345 // either it is the same font, i.e. they share the same common data or they
346 // have different ref datas but still describe the same font
347 return IsSameAs(font
) ||
349 IsOk() == font
.IsOk() &&
350 GetPointSize() == font
.GetPointSize() &&
351 // in wxGTK1 GetPixelSize() calls GetInternalFont() which uses
352 // operator==() resulting in infinite recursion so we can't use it
354 #if !defined(__WXGTK__) || defined(__WXGTK20__)
355 GetPixelSize() == font
.GetPixelSize() &&
357 GetFamily() == font
.GetFamily() &&
358 GetStyle() == font
.GetStyle() &&
359 GetWeight() == font
.GetWeight() &&
360 GetUnderlined() == font
.GetUnderlined() &&
361 GetFaceName().IsSameAs(font
.GetFaceName(), false) &&
362 GetEncoding() == font
.GetEncoding()
366 wxString
wxFontBase::GetFamilyString() const
368 wxCHECK_MSG( IsOk(), "wxFONTFAMILY_DEFAULT", "invalid font" );
370 switch ( GetFamily() )
372 case wxFONTFAMILY_DECORATIVE
: return "wxFONTFAMILY_DECORATIVE";
373 case wxFONTFAMILY_ROMAN
: return "wxFONTFAMILY_ROMAN";
374 case wxFONTFAMILY_SCRIPT
: return "wxFONTFAMILY_SCRIPT";
375 case wxFONTFAMILY_SWISS
: return "wxFONTFAMILY_SWISS";
376 case wxFONTFAMILY_MODERN
: return "wxFONTFAMILY_MODERN";
377 case wxFONTFAMILY_TELETYPE
: return "wxFONTFAMILY_TELETYPE";
378 default: return "wxFONTFAMILY_DEFAULT";
382 wxString
wxFontBase::GetStyleString() const
384 wxCHECK_MSG( IsOk(), "wxFONTSTYLE_DEFAULT", "invalid font" );
386 switch ( GetStyle() )
388 case wxFONTSTYLE_NORMAL
: return "wxFONTSTYLE_NORMAL";
389 case wxFONTSTYLE_SLANT
: return "wxFONTSTYLE_SLANT";
390 case wxFONTSTYLE_ITALIC
: return "wxFONTSTYLE_ITALIC";
391 default: return "wxFONTSTYLE_DEFAULT";
395 wxString
wxFontBase::GetWeightString() const
397 wxCHECK_MSG( IsOk(), "wxFONTWEIGHT_DEFAULT", "invalid font" );
399 switch ( GetWeight() )
401 case wxFONTWEIGHT_NORMAL
: return "wxFONTWEIGHT_NORMAL";
402 case wxFONTWEIGHT_BOLD
: return "wxFONTWEIGHT_BOLD";
403 case wxFONTWEIGHT_LIGHT
: return "wxFONTWEIGHT_LIGHT";
404 default: return "wxFONTWEIGHT_DEFAULT";
408 bool wxFontBase::SetFaceName(const wxString
& facename
)
411 if (!wxFontEnumerator::IsValidFacename(facename
))
413 UnRef(); // make IsOk() return false
416 #else // !wxUSE_FONTENUM
417 wxUnusedVar(facename
);
418 #endif // wxUSE_FONTENUM/!wxUSE_FONTENUM
424 // ----------------------------------------------------------------------------
426 // ----------------------------------------------------------------------------
428 // Up to now, there are no native implementations of this function:
429 void wxNativeFontInfo::SetFaceName(const wxArrayString
& facenames
)
432 for (size_t i
=0; i
< facenames
.GetCount(); i
++)
434 if (wxFontEnumerator::IsValidFacename(facenames
[i
]))
436 SetFaceName(facenames
[i
]);
441 // set the first valid facename we can find on this system
442 wxString validfacename
= wxFontEnumerator::GetFacenames().Item(0);
443 wxLogTrace(wxT("font"), wxT("Falling back to '%s'"), validfacename
.c_str());
444 SetFaceName(validfacename
);
445 #else // !wxUSE_FONTENUM
446 SetFaceName(facenames
[0]);
447 #endif // wxUSE_FONTENUM/!wxUSE_FONTENUM
451 #ifdef wxNO_NATIVE_FONTINFO
453 // These are the generic forms of FromString()/ToString.
455 // convert to/from the string representation: format is
456 // version;pointsize;family;style;weight;underlined;facename;encoding
458 bool wxNativeFontInfo::FromString(const wxString
& s
)
462 wxStringTokenizer
tokenizer(s
, _T(";"));
464 wxString token
= tokenizer
.GetNextToken();
466 // Ignore the version for now
469 token
= tokenizer
.GetNextToken();
470 if ( !token
.ToLong(&l
) )
474 token
= tokenizer
.GetNextToken();
475 if ( !token
.ToLong(&l
) )
477 family
= (wxFontFamily
)l
;
479 token
= tokenizer
.GetNextToken();
480 if ( !token
.ToLong(&l
) )
482 style
= (wxFontStyle
)l
;
484 token
= tokenizer
.GetNextToken();
485 if ( !token
.ToLong(&l
) )
487 weight
= (wxFontWeight
)l
;
489 token
= tokenizer
.GetNextToken();
490 if ( !token
.ToLong(&l
) )
494 faceName
= tokenizer
.GetNextToken();
501 token
= tokenizer
.GetNextToken();
502 if ( !token
.ToLong(&l
) )
504 encoding
= (wxFontEncoding
)l
;
509 wxString
wxNativeFontInfo::ToString() const
513 s
.Printf(_T("%d;%d;%d;%d;%d;%d;%s;%d"),
526 void wxNativeFontInfo::Init()
529 family
= wxFONTFAMILY_DEFAULT
;
530 style
= wxFONTSTYLE_NORMAL
;
531 weight
= wxFONTWEIGHT_NORMAL
;
534 encoding
= wxFONTENCODING_DEFAULT
;
537 int wxNativeFontInfo::GetPointSize() const
542 wxFontStyle
wxNativeFontInfo::GetStyle() const
547 wxFontWeight
wxNativeFontInfo::GetWeight() const
552 bool wxNativeFontInfo::GetUnderlined() const
557 wxString
wxNativeFontInfo::GetFaceName() const
562 wxFontFamily
wxNativeFontInfo::GetFamily() const
567 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
572 void wxNativeFontInfo::SetPointSize(int pointsize
)
574 pointSize
= pointsize
;
577 void wxNativeFontInfo::SetStyle(wxFontStyle style_
)
582 void wxNativeFontInfo::SetWeight(wxFontWeight weight_
)
587 void wxNativeFontInfo::SetUnderlined(bool underlined_
)
589 underlined
= underlined_
;
592 bool wxNativeFontInfo::SetFaceName(const wxString
& facename_
)
594 faceName
= facename_
;
598 void wxNativeFontInfo::SetFamily(wxFontFamily family_
)
603 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding_
)
605 encoding
= encoding_
;
608 #endif // generic wxNativeFontInfo implementation
610 // conversion to/from user-readable string: this is used in the generic
611 // versions and under MSW as well because there is no standard font description
612 // format there anyhow (but there is a well-defined standard for X11 fonts used
613 // by wxGTK and wxMotif)
615 #if defined(wxNO_NATIVE_FONTINFO) || defined(__WXMSW__) || defined (__WXPM__) || defined(__WXOSX__)
617 wxString
wxNativeFontInfo::ToUserString() const
621 // first put the adjectives, if any - this is English-centric, of course,
622 // but what else can we do?
623 if ( GetUnderlined() )
625 desc
<< _("underlined");
628 switch ( GetWeight() )
631 wxFAIL_MSG( _T("unknown font weight") );
634 case wxFONTWEIGHT_NORMAL
:
637 case wxFONTWEIGHT_LIGHT
:
641 case wxFONTWEIGHT_BOLD
:
646 switch ( GetStyle() )
649 wxFAIL_MSG( _T("unknown font style") );
652 case wxFONTSTYLE_NORMAL
:
655 // we don't distinguish between the two for now anyhow...
656 case wxFONTSTYLE_ITALIC
:
657 case wxFONTSTYLE_SLANT
:
658 desc
<< _(" italic");
662 wxString face
= GetFaceName();
665 if (face
.Contains(' ') || face
.Contains(';') || face
.Contains(','))
667 face
.Replace("'", "");
668 // eventually remove quote characters: most systems do not
669 // allow them in a facename anyway so this usually does nothing
671 // make it possible for FromUserString() function to understand
672 // that the different words which compose this facename are
673 // not different adjectives or other data but rather all parts
675 desc
<< _T(" '") << face
<< _("'");
678 desc
<< _T(' ') << face
;
681 int size
= GetPointSize();
682 if ( size
!= wxNORMAL_FONT
->GetPointSize() )
684 desc
<< _T(' ') << size
;
688 wxFontEncoding enc
= GetEncoding();
689 if ( enc
!= wxFONTENCODING_DEFAULT
&& enc
!= wxFONTENCODING_SYSTEM
)
691 desc
<< _T(' ') << wxFontMapper::GetEncodingName(enc
);
693 #endif // wxUSE_FONTMAP
695 return desc
.Strip(wxString::both
).MakeLower();
698 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
700 // reset to the default state
703 // ToUserString() will quote the facename if it contains spaces, commas
704 // or semicolons: we must be able to understand that quoted text is
708 wxString::iterator i = toparse.find("'");
709 if (i != wxString::npos)
711 for (; *i != '\'' && *i != toparse.end(); i++)
715 // parse a more or less free form string
716 wxStringTokenizer
tokenizer(toparse
, _T(";, "), wxTOKEN_STRTOK
);
720 bool weightfound
= false, pointsizefound
= false;
722 bool encodingfound
= false;
724 bool insideQuotes
= false;
726 while ( tokenizer
.HasMoreTokens() )
728 wxString token
= tokenizer
.GetNextToken();
731 token
.Trim(true).Trim(false).MakeLower();
734 if (token
.StartsWith("'") ||
737 insideQuotes
= false;
739 // add this last token to the facename:
742 // normalize facename:
743 face
= face
.Trim(true).Trim(false);
744 face
.Replace("'", "");
751 if (token
.StartsWith("'"))
755 // look for the known tokens
758 // only the facename may be quoted:
762 if ( token
== _T("underlined") || token
== _("underlined") )
766 else if ( token
== _T("light") || token
== _("light") )
768 SetWeight(wxFONTWEIGHT_LIGHT
);
771 else if ( token
== _T("bold") || token
== _("bold") )
773 SetWeight(wxFONTWEIGHT_BOLD
);
776 else if ( token
== _T("italic") || token
== _("italic") )
778 SetStyle(wxFONTSTYLE_ITALIC
);
780 else if ( token
.ToULong(&size
) )
783 pointsizefound
= true;
788 // try to interpret this as an encoding
789 wxFontEncoding encoding
= wxFontMapper::Get()->CharsetToEncoding(token
, false);
790 if ( encoding
!= wxFONTENCODING_DEFAULT
&&
791 encoding
!= wxFONTENCODING_SYSTEM
) // returned when the recognition failed
793 SetEncoding(encoding
);
794 encodingfound
= true;
798 #endif // wxUSE_FONTMAP
800 // assume it is the face name
808 // skip the code which resets face below
813 #endif // wxUSE_FONTMAP
816 // if we had had the facename, we shouldn't continue appending tokens
817 // to it (i.e. "foo bold bar" shouldn't result in the facename "foo
821 // NB: the check on the facename is implemented in wxFontBase::SetFaceName
822 // and not in wxNativeFontInfo::SetFaceName thus we need to explicitely
823 // call here wxFontEnumerator::IsValidFacename
826 !wxFontEnumerator::IsValidFacename(face
) ||
827 #endif // wxUSE_FONTENUM
830 SetFaceName(wxNORMAL_FONT
->GetFaceName());
837 // we might not have flushed it inside the loop
840 // NB: the check on the facename is implemented in wxFontBase::SetFaceName
841 // and not in wxNativeFontInfo::SetFaceName thus we need to explicitely
842 // call here wxFontEnumerator::IsValidFacename
845 !wxFontEnumerator::IsValidFacename(face
) ||
846 #endif // wxUSE_FONTENUM
849 SetFaceName(wxNORMAL_FONT
->GetFaceName());
853 // set point size to default value if size was not given
854 if ( !pointsizefound
)
855 SetPointSize(wxNORMAL_FONT
->GetPointSize());
857 // set font weight to default value if weight was not given
859 SetWeight(wxFONTWEIGHT_NORMAL
);
862 // set font encoding to default value if encoding was not given
863 if ( !encodingfound
)
864 SetEncoding(wxFONTENCODING_SYSTEM
);
865 #endif // wxUSE_FONTMAP
870 #endif // generic or wxMSW or wxOS2
873 // wxFont <-> wxString utilities, used by wxConfig
874 wxString
wxToString(const wxFontBase
& font
)
876 return font
.IsOk() ? font
.GetNativeFontInfoDesc()
880 bool wxFromString(const wxString
& str
, wxFontBase
*font
)
882 wxCHECK_MSG( font
, false, _T("NULL output parameter") );
890 return font
->SetNativeFontInfo(str
);