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 wxT("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
287 wxCHECK_MSG( IsOk(), wxEmptyString
, wxT("invalid font") );
290 const wxNativeFontInfo
*fontInfo
= GetNativeFontInfo();
293 fontDesc
= fontInfo
->ToString();
294 wxASSERT_MSG(!fontDesc
.empty(), wxT("This should be a non-empty string!"));
298 wxFAIL_MSG(wxT("Derived class should have created the wxNativeFontInfo!"));
304 wxString
wxFontBase::GetNativeFontInfoUserDesc() const
306 wxCHECK_MSG( IsOk(), wxEmptyString
, wxT("invalid font") );
309 const wxNativeFontInfo
*fontInfo
= GetNativeFontInfo();
312 fontDesc
= fontInfo
->ToUserString();
313 wxASSERT_MSG(!fontDesc
.empty(), wxT("This should be a non-empty string!"));
317 wxFAIL_MSG(wxT("Derived class should have created the wxNativeFontInfo!"));
323 bool wxFontBase::SetNativeFontInfo(const wxString
& info
)
325 wxNativeFontInfo fontInfo
;
326 if ( !info
.empty() && fontInfo
.FromString(info
) )
328 SetNativeFontInfo(fontInfo
);
335 bool wxFontBase::SetNativeFontInfoUserDesc(const wxString
& info
)
337 wxNativeFontInfo fontInfo
;
338 if ( !info
.empty() && fontInfo
.FromUserString(info
) )
340 SetNativeFontInfo(fontInfo
);
347 bool wxFontBase::operator==(const wxFont
& font
) const
349 // either it is the same font, i.e. they share the same common data or they
350 // have different ref datas but still describe the same font
351 return IsSameAs(font
) ||
353 IsOk() == font
.IsOk() &&
354 GetPointSize() == font
.GetPointSize() &&
355 // in wxGTK1 GetPixelSize() calls GetInternalFont() which uses
356 // operator==() resulting in infinite recursion so we can't use it
358 #if !defined(__WXGTK__) || defined(__WXGTK20__)
359 GetPixelSize() == font
.GetPixelSize() &&
361 GetFamily() == font
.GetFamily() &&
362 GetStyle() == font
.GetStyle() &&
363 GetWeight() == font
.GetWeight() &&
364 GetUnderlined() == font
.GetUnderlined() &&
365 GetFaceName().IsSameAs(font
.GetFaceName(), false) &&
366 GetEncoding() == font
.GetEncoding()
370 wxFontFamily
wxFontBase::GetFamily() const
372 wxCHECK_MSG( IsOk(), wxFONTFAMILY_UNKNOWN
, wxS("invalid font") );
374 // Don't return wxFONTFAMILY_UNKNOWN from here because it prevents the code
375 // like wxFont(size, wxNORMAL_FONT->GetFamily(), ...) from working (see
376 // #12330). This is really just a hack but it allows to keep compatibility
377 // and doesn't really have any bad drawbacks so do this until someone comes
378 // up with a better idea.
379 const wxFontFamily family
= DoGetFamily();
381 return family
== wxFONTFAMILY_UNKNOWN
? wxFONTFAMILY_DEFAULT
: family
;
384 wxString
wxFontBase::GetFamilyString() const
386 wxCHECK_MSG( IsOk(), "wxFONTFAMILY_DEFAULT", "invalid font" );
388 switch ( GetFamily() )
390 case wxFONTFAMILY_DECORATIVE
: return "wxFONTFAMILY_DECORATIVE";
391 case wxFONTFAMILY_ROMAN
: return "wxFONTFAMILY_ROMAN";
392 case wxFONTFAMILY_SCRIPT
: return "wxFONTFAMILY_SCRIPT";
393 case wxFONTFAMILY_SWISS
: return "wxFONTFAMILY_SWISS";
394 case wxFONTFAMILY_MODERN
: return "wxFONTFAMILY_MODERN";
395 case wxFONTFAMILY_TELETYPE
: return "wxFONTFAMILY_TELETYPE";
396 case wxFONTFAMILY_UNKNOWN
: return "wxFONTFAMILY_UNKNOWN";
397 default: return "wxFONTFAMILY_DEFAULT";
401 wxString
wxFontBase::GetStyleString() const
403 wxCHECK_MSG( IsOk(), "wxFONTSTYLE_DEFAULT", "invalid font" );
405 switch ( GetStyle() )
407 case wxFONTSTYLE_NORMAL
: return "wxFONTSTYLE_NORMAL";
408 case wxFONTSTYLE_SLANT
: return "wxFONTSTYLE_SLANT";
409 case wxFONTSTYLE_ITALIC
: return "wxFONTSTYLE_ITALIC";
410 default: return "wxFONTSTYLE_DEFAULT";
414 wxString
wxFontBase::GetWeightString() const
416 wxCHECK_MSG( IsOk(), "wxFONTWEIGHT_DEFAULT", "invalid font" );
418 switch ( GetWeight() )
420 case wxFONTWEIGHT_NORMAL
: return "wxFONTWEIGHT_NORMAL";
421 case wxFONTWEIGHT_BOLD
: return "wxFONTWEIGHT_BOLD";
422 case wxFONTWEIGHT_LIGHT
: return "wxFONTWEIGHT_LIGHT";
423 default: return "wxFONTWEIGHT_DEFAULT";
427 bool wxFontBase::SetFaceName(const wxString
& facename
)
430 if (!wxFontEnumerator::IsValidFacename(facename
))
432 UnRef(); // make IsOk() return false
435 #else // !wxUSE_FONTENUM
436 wxUnusedVar(facename
);
437 #endif // wxUSE_FONTENUM/!wxUSE_FONTENUM
442 wxFont
& wxFont::MakeBold()
444 SetWeight(wxFONTWEIGHT_BOLD
);
448 wxFont
wxFont::Bold() const
455 wxFont
& wxFont::MakeItalic()
457 SetStyle(wxFONTSTYLE_ITALIC
);
461 wxFont
wxFont::Italic() const
464 font
.SetStyle(wxFONTSTYLE_ITALIC
);
468 wxFont
& wxFont::Scale(float x
)
470 SetPointSize(int(x
*GetPointSize() + 0.5));
474 wxFont
wxFont::Scaled(float x
) const
481 // ----------------------------------------------------------------------------
483 // ----------------------------------------------------------------------------
485 // Up to now, there are no native implementations of this function:
486 void wxNativeFontInfo::SetFaceName(const wxArrayString
& facenames
)
489 for (size_t i
=0; i
< facenames
.GetCount(); i
++)
491 if (wxFontEnumerator::IsValidFacename(facenames
[i
]))
493 SetFaceName(facenames
[i
]);
498 // set the first valid facename we can find on this system
499 wxString validfacename
= wxFontEnumerator::GetFacenames().Item(0);
500 wxLogTrace(wxT("font"), wxT("Falling back to '%s'"), validfacename
.c_str());
501 SetFaceName(validfacename
);
502 #else // !wxUSE_FONTENUM
503 SetFaceName(facenames
[0]);
504 #endif // wxUSE_FONTENUM/!wxUSE_FONTENUM
508 #ifdef wxNO_NATIVE_FONTINFO
510 // These are the generic forms of FromString()/ToString.
512 // convert to/from the string representation: format is
513 // version;pointsize;family;style;weight;underlined;facename;encoding
515 bool wxNativeFontInfo::FromString(const wxString
& s
)
519 wxStringTokenizer
tokenizer(s
, wxT(";"));
521 wxString token
= tokenizer
.GetNextToken();
523 // Ignore the version for now
526 token
= tokenizer
.GetNextToken();
527 if ( !token
.ToLong(&l
) )
531 token
= tokenizer
.GetNextToken();
532 if ( !token
.ToLong(&l
) )
534 family
= (wxFontFamily
)l
;
536 token
= tokenizer
.GetNextToken();
537 if ( !token
.ToLong(&l
) )
539 style
= (wxFontStyle
)l
;
541 token
= tokenizer
.GetNextToken();
542 if ( !token
.ToLong(&l
) )
544 weight
= (wxFontWeight
)l
;
546 token
= tokenizer
.GetNextToken();
547 if ( !token
.ToLong(&l
) )
551 faceName
= tokenizer
.GetNextToken();
558 token
= tokenizer
.GetNextToken();
559 if ( !token
.ToLong(&l
) )
561 encoding
= (wxFontEncoding
)l
;
566 wxString
wxNativeFontInfo::ToString() const
570 s
.Printf(wxT("%d;%d;%d;%d;%d;%d;%s;%d"),
583 void wxNativeFontInfo::Init()
586 family
= wxFONTFAMILY_DEFAULT
;
587 style
= wxFONTSTYLE_NORMAL
;
588 weight
= wxFONTWEIGHT_NORMAL
;
591 encoding
= wxFONTENCODING_DEFAULT
;
594 int wxNativeFontInfo::GetPointSize() const
599 wxFontStyle
wxNativeFontInfo::GetStyle() const
604 wxFontWeight
wxNativeFontInfo::GetWeight() const
609 bool wxNativeFontInfo::GetUnderlined() const
614 wxString
wxNativeFontInfo::GetFaceName() const
619 wxFontFamily
wxNativeFontInfo::GetFamily() const
624 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
629 void wxNativeFontInfo::SetPointSize(int pointsize
)
631 pointSize
= pointsize
;
634 void wxNativeFontInfo::SetStyle(wxFontStyle style_
)
639 void wxNativeFontInfo::SetWeight(wxFontWeight weight_
)
644 void wxNativeFontInfo::SetUnderlined(bool underlined_
)
646 underlined
= underlined_
;
649 bool wxNativeFontInfo::SetFaceName(const wxString
& facename_
)
651 faceName
= facename_
;
655 void wxNativeFontInfo::SetFamily(wxFontFamily family_
)
660 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding_
)
662 encoding
= encoding_
;
665 #endif // generic wxNativeFontInfo implementation
667 // conversion to/from user-readable string: this is used in the generic
668 // versions and under MSW as well because there is no standard font description
669 // format there anyhow (but there is a well-defined standard for X11 fonts used
670 // by wxGTK and wxMotif)
672 #if defined(wxNO_NATIVE_FONTINFO) || defined(__WXMSW__) || defined (__WXPM__) || defined(__WXOSX__)
674 wxString
wxNativeFontInfo::ToUserString() const
678 // first put the adjectives, if any - this is English-centric, of course,
679 // but what else can we do?
680 if ( GetUnderlined() )
682 desc
<< _("underlined");
685 switch ( GetWeight() )
688 wxFAIL_MSG( wxT("unknown font weight") );
691 case wxFONTWEIGHT_NORMAL
:
694 case wxFONTWEIGHT_LIGHT
:
698 case wxFONTWEIGHT_BOLD
:
703 switch ( GetStyle() )
706 wxFAIL_MSG( wxT("unknown font style") );
709 case wxFONTSTYLE_NORMAL
:
712 // we don't distinguish between the two for now anyhow...
713 case wxFONTSTYLE_ITALIC
:
714 case wxFONTSTYLE_SLANT
:
715 desc
<< _(" italic");
719 wxString face
= GetFaceName();
722 if (face
.Contains(' ') || face
.Contains(';') || face
.Contains(','))
724 face
.Replace("'", "");
725 // eventually remove quote characters: most systems do not
726 // allow them in a facename anyway so this usually does nothing
728 // make it possible for FromUserString() function to understand
729 // that the different words which compose this facename are
730 // not different adjectives or other data but rather all parts
732 desc
<< wxT(" '") << face
<< _("'");
735 desc
<< wxT(' ') << face
;
737 else // no face name specified
741 switch ( GetFamily() )
743 case wxFONTFAMILY_DECORATIVE
:
744 familyStr
= "decorative";
747 case wxFONTFAMILY_ROMAN
:
751 case wxFONTFAMILY_SCRIPT
:
752 familyStr
= "script";
755 case wxFONTFAMILY_SWISS
:
759 case wxFONTFAMILY_MODERN
:
760 familyStr
= "modern";
763 case wxFONTFAMILY_TELETYPE
:
764 familyStr
= "teletype";
767 case wxFONTFAMILY_DEFAULT
:
768 case wxFONTFAMILY_UNKNOWN
:
772 wxFAIL_MSG( "unknown font family" );
775 if ( !familyStr
.empty() )
776 desc
<< " '" << familyStr
<< " family'";
779 int size
= GetPointSize();
780 if ( size
!= wxNORMAL_FONT
->GetPointSize() )
782 desc
<< wxT(' ') << size
;
786 wxFontEncoding enc
= GetEncoding();
787 if ( enc
!= wxFONTENCODING_DEFAULT
&& enc
!= wxFONTENCODING_SYSTEM
)
789 desc
<< wxT(' ') << wxFontMapper::GetEncodingName(enc
);
791 #endif // wxUSE_FONTMAP
793 return desc
.Strip(wxString::both
).MakeLower();
796 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
798 // reset to the default state
801 // ToUserString() will quote the facename if it contains spaces, commas
802 // or semicolons: we must be able to understand that quoted text is
806 // parse a more or less free form string
807 wxStringTokenizer
tokenizer(toparse
, wxT(";, "), wxTOKEN_STRTOK
);
811 bool weightfound
= false, pointsizefound
= false;
813 bool encodingfound
= false;
815 bool insideQuotes
= false;
817 while ( tokenizer
.HasMoreTokens() )
819 wxString token
= tokenizer
.GetNextToken();
822 token
.Trim(true).Trim(false).MakeLower();
825 if (token
.StartsWith("'") ||
828 insideQuotes
= false;
830 // add this last token to the facename:
833 // normalize facename:
834 face
= face
.Trim(true).Trim(false);
835 face
.Replace("'", "");
842 if (token
.StartsWith("'"))
846 // look for the known tokens
849 // only the facename may be quoted:
853 if ( token
== wxT("underlined") || token
== _("underlined") )
857 else if ( token
== wxT("light") || token
== _("light") )
859 SetWeight(wxFONTWEIGHT_LIGHT
);
862 else if ( token
== wxT("bold") || token
== _("bold") )
864 SetWeight(wxFONTWEIGHT_BOLD
);
867 else if ( token
== wxT("italic") || token
== _("italic") )
869 SetStyle(wxFONTSTYLE_ITALIC
);
871 else if ( token
.ToULong(&size
) )
874 pointsizefound
= true;
879 // try to interpret this as an encoding
880 wxFontEncoding encoding
= wxFontMapper::Get()->CharsetToEncoding(token
, false);
881 if ( encoding
!= wxFONTENCODING_DEFAULT
&&
882 encoding
!= wxFONTENCODING_SYSTEM
) // returned when the recognition failed
884 SetEncoding(encoding
);
885 encodingfound
= true;
889 #endif // wxUSE_FONTMAP
891 // assume it is the face name
899 // skip the code which resets face below
904 #endif // wxUSE_FONTMAP
907 // if we had had the facename, we shouldn't continue appending tokens
908 // to it (i.e. "foo bold bar" shouldn't result in the facename "foo
913 if ( face
.EndsWith(" family", &familyStr
) )
915 // it's not a facename but rather a font family
917 if ( familyStr
== "decorative" )
918 family
= wxFONTFAMILY_DECORATIVE
;
919 else if ( familyStr
== "roman" )
920 family
= wxFONTFAMILY_ROMAN
;
921 else if ( familyStr
== "script" )
922 family
= wxFONTFAMILY_SCRIPT
;
923 else if ( familyStr
== "swiss" )
924 family
= wxFONTFAMILY_SWISS
;
925 else if ( familyStr
== "modern" )
926 family
= wxFONTFAMILY_MODERN
;
927 else if ( familyStr
== "teletype" )
928 family
= wxFONTFAMILY_TELETYPE
;
934 // NB: the check on the facename is implemented in wxFontBase::SetFaceName
935 // and not in wxNativeFontInfo::SetFaceName thus we need to explicitely
936 // call here wxFontEnumerator::IsValidFacename
939 !wxFontEnumerator::IsValidFacename(face
) ||
940 #endif // wxUSE_FONTENUM
943 SetFaceName(wxNORMAL_FONT
->GetFaceName());
950 // we might not have flushed it inside the loop
953 // NB: the check on the facename is implemented in wxFontBase::SetFaceName
954 // and not in wxNativeFontInfo::SetFaceName thus we need to explicitely
955 // call here wxFontEnumerator::IsValidFacename
958 !wxFontEnumerator::IsValidFacename(face
) ||
959 #endif // wxUSE_FONTENUM
962 SetFaceName(wxNORMAL_FONT
->GetFaceName());
966 // set point size to default value if size was not given
967 if ( !pointsizefound
)
968 SetPointSize(wxNORMAL_FONT
->GetPointSize());
970 // set font weight to default value if weight was not given
972 SetWeight(wxFONTWEIGHT_NORMAL
);
975 // set font encoding to default value if encoding was not given
976 if ( !encodingfound
)
977 SetEncoding(wxFONTENCODING_SYSTEM
);
978 #endif // wxUSE_FONTMAP
983 #endif // generic or wxMSW or wxOS2
986 // wxFont <-> wxString utilities, used by wxConfig
987 wxString
wxToString(const wxFontBase
& font
)
989 return font
.IsOk() ? font
.GetNativeFontInfoDesc()
993 bool wxFromString(const wxString
& str
, wxFontBase
*font
)
995 wxCHECK_MSG( font
, false, wxT("NULL output parameter") );
1003 return font
->SetNativeFontInfo(str
);