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 wxString
wxFontBase::GetFamilyString() const
372 wxCHECK_MSG( IsOk(), "wxFONTFAMILY_DEFAULT", "invalid font" );
374 switch ( GetFamily() )
376 case wxFONTFAMILY_DECORATIVE
: return "wxFONTFAMILY_DECORATIVE";
377 case wxFONTFAMILY_ROMAN
: return "wxFONTFAMILY_ROMAN";
378 case wxFONTFAMILY_SCRIPT
: return "wxFONTFAMILY_SCRIPT";
379 case wxFONTFAMILY_SWISS
: return "wxFONTFAMILY_SWISS";
380 case wxFONTFAMILY_MODERN
: return "wxFONTFAMILY_MODERN";
381 case wxFONTFAMILY_TELETYPE
: return "wxFONTFAMILY_TELETYPE";
382 case wxFONTFAMILY_UNKNOWN
: return "wxFONTFAMILY_UNKNOWN";
383 default: return "wxFONTFAMILY_DEFAULT";
387 wxString
wxFontBase::GetStyleString() const
389 wxCHECK_MSG( IsOk(), "wxFONTSTYLE_DEFAULT", "invalid font" );
391 switch ( GetStyle() )
393 case wxFONTSTYLE_NORMAL
: return "wxFONTSTYLE_NORMAL";
394 case wxFONTSTYLE_SLANT
: return "wxFONTSTYLE_SLANT";
395 case wxFONTSTYLE_ITALIC
: return "wxFONTSTYLE_ITALIC";
396 default: return "wxFONTSTYLE_DEFAULT";
400 wxString
wxFontBase::GetWeightString() const
402 wxCHECK_MSG( IsOk(), "wxFONTWEIGHT_DEFAULT", "invalid font" );
404 switch ( GetWeight() )
406 case wxFONTWEIGHT_NORMAL
: return "wxFONTWEIGHT_NORMAL";
407 case wxFONTWEIGHT_BOLD
: return "wxFONTWEIGHT_BOLD";
408 case wxFONTWEIGHT_LIGHT
: return "wxFONTWEIGHT_LIGHT";
409 default: return "wxFONTWEIGHT_DEFAULT";
413 bool wxFontBase::SetFaceName(const wxString
& facename
)
416 if (!wxFontEnumerator::IsValidFacename(facename
))
418 UnRef(); // make IsOk() return false
421 #else // !wxUSE_FONTENUM
422 wxUnusedVar(facename
);
423 #endif // wxUSE_FONTENUM/!wxUSE_FONTENUM
428 wxFont
& wxFont::MakeBold()
430 SetWeight(wxFONTWEIGHT_BOLD
);
434 wxFont
wxFont::Bold() const
441 wxFont
& wxFont::MakeItalic()
443 SetStyle(wxFONTSTYLE_ITALIC
);
447 wxFont
wxFont::Italic() const
450 font
.SetStyle(wxFONTSTYLE_ITALIC
);
454 wxFont
& wxFont::Scale(float x
)
456 SetPointSize(int(x
*GetPointSize() + 0.5));
460 wxFont
wxFont::Scaled(float x
) const
467 // ----------------------------------------------------------------------------
469 // ----------------------------------------------------------------------------
471 // Up to now, there are no native implementations of this function:
472 void wxNativeFontInfo::SetFaceName(const wxArrayString
& facenames
)
475 for (size_t i
=0; i
< facenames
.GetCount(); i
++)
477 if (wxFontEnumerator::IsValidFacename(facenames
[i
]))
479 SetFaceName(facenames
[i
]);
484 // set the first valid facename we can find on this system
485 wxString validfacename
= wxFontEnumerator::GetFacenames().Item(0);
486 wxLogTrace(wxT("font"), wxT("Falling back to '%s'"), validfacename
.c_str());
487 SetFaceName(validfacename
);
488 #else // !wxUSE_FONTENUM
489 SetFaceName(facenames
[0]);
490 #endif // wxUSE_FONTENUM/!wxUSE_FONTENUM
494 #ifdef wxNO_NATIVE_FONTINFO
496 // These are the generic forms of FromString()/ToString.
498 // convert to/from the string representation: format is
499 // version;pointsize;family;style;weight;underlined;facename;encoding
501 bool wxNativeFontInfo::FromString(const wxString
& s
)
505 wxStringTokenizer
tokenizer(s
, wxT(";"));
507 wxString token
= tokenizer
.GetNextToken();
509 // Ignore the version for now
512 token
= tokenizer
.GetNextToken();
513 if ( !token
.ToLong(&l
) )
517 token
= tokenizer
.GetNextToken();
518 if ( !token
.ToLong(&l
) )
520 family
= (wxFontFamily
)l
;
522 token
= tokenizer
.GetNextToken();
523 if ( !token
.ToLong(&l
) )
525 style
= (wxFontStyle
)l
;
527 token
= tokenizer
.GetNextToken();
528 if ( !token
.ToLong(&l
) )
530 weight
= (wxFontWeight
)l
;
532 token
= tokenizer
.GetNextToken();
533 if ( !token
.ToLong(&l
) )
537 faceName
= tokenizer
.GetNextToken();
544 token
= tokenizer
.GetNextToken();
545 if ( !token
.ToLong(&l
) )
547 encoding
= (wxFontEncoding
)l
;
552 wxString
wxNativeFontInfo::ToString() const
556 s
.Printf(wxT("%d;%d;%d;%d;%d;%d;%s;%d"),
569 void wxNativeFontInfo::Init()
572 family
= wxFONTFAMILY_DEFAULT
;
573 style
= wxFONTSTYLE_NORMAL
;
574 weight
= wxFONTWEIGHT_NORMAL
;
577 encoding
= wxFONTENCODING_DEFAULT
;
580 int wxNativeFontInfo::GetPointSize() const
585 wxFontStyle
wxNativeFontInfo::GetStyle() const
590 wxFontWeight
wxNativeFontInfo::GetWeight() const
595 bool wxNativeFontInfo::GetUnderlined() const
600 wxString
wxNativeFontInfo::GetFaceName() const
605 wxFontFamily
wxNativeFontInfo::GetFamily() const
610 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
615 void wxNativeFontInfo::SetPointSize(int pointsize
)
617 pointSize
= pointsize
;
620 void wxNativeFontInfo::SetStyle(wxFontStyle style_
)
625 void wxNativeFontInfo::SetWeight(wxFontWeight weight_
)
630 void wxNativeFontInfo::SetUnderlined(bool underlined_
)
632 underlined
= underlined_
;
635 bool wxNativeFontInfo::SetFaceName(const wxString
& facename_
)
637 faceName
= facename_
;
641 void wxNativeFontInfo::SetFamily(wxFontFamily family_
)
646 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding_
)
648 encoding
= encoding_
;
651 #endif // generic wxNativeFontInfo implementation
653 // conversion to/from user-readable string: this is used in the generic
654 // versions and under MSW as well because there is no standard font description
655 // format there anyhow (but there is a well-defined standard for X11 fonts used
656 // by wxGTK and wxMotif)
658 #if defined(wxNO_NATIVE_FONTINFO) || defined(__WXMSW__) || defined (__WXPM__) || defined(__WXOSX__)
660 wxString
wxNativeFontInfo::ToUserString() const
664 // first put the adjectives, if any - this is English-centric, of course,
665 // but what else can we do?
666 if ( GetUnderlined() )
668 desc
<< _("underlined");
671 switch ( GetWeight() )
674 wxFAIL_MSG( wxT("unknown font weight") );
677 case wxFONTWEIGHT_NORMAL
:
680 case wxFONTWEIGHT_LIGHT
:
684 case wxFONTWEIGHT_BOLD
:
689 switch ( GetStyle() )
692 wxFAIL_MSG( wxT("unknown font style") );
695 case wxFONTSTYLE_NORMAL
:
698 // we don't distinguish between the two for now anyhow...
699 case wxFONTSTYLE_ITALIC
:
700 case wxFONTSTYLE_SLANT
:
701 desc
<< _(" italic");
705 wxString face
= GetFaceName();
708 if (face
.Contains(' ') || face
.Contains(';') || face
.Contains(','))
710 face
.Replace("'", "");
711 // eventually remove quote characters: most systems do not
712 // allow them in a facename anyway so this usually does nothing
714 // make it possible for FromUserString() function to understand
715 // that the different words which compose this facename are
716 // not different adjectives or other data but rather all parts
718 desc
<< wxT(" '") << face
<< _("'");
721 desc
<< wxT(' ') << face
;
723 else // no face name specified
727 switch ( GetFamily() )
729 case wxFONTFAMILY_DECORATIVE
:
730 familyStr
= "decorative";
733 case wxFONTFAMILY_ROMAN
:
737 case wxFONTFAMILY_SCRIPT
:
738 familyStr
= "script";
741 case wxFONTFAMILY_SWISS
:
745 case wxFONTFAMILY_MODERN
:
746 familyStr
= "modern";
749 case wxFONTFAMILY_TELETYPE
:
750 familyStr
= "teletype";
753 case wxFONTFAMILY_DEFAULT
:
754 case wxFONTFAMILY_UNKNOWN
:
758 wxFAIL_MSG( "unknown font family" );
761 if ( !familyStr
.empty() )
762 desc
<< " '" << familyStr
<< " family'";
765 int size
= GetPointSize();
766 if ( size
!= wxNORMAL_FONT
->GetPointSize() )
768 desc
<< wxT(' ') << size
;
772 wxFontEncoding enc
= GetEncoding();
773 if ( enc
!= wxFONTENCODING_DEFAULT
&& enc
!= wxFONTENCODING_SYSTEM
)
775 desc
<< wxT(' ') << wxFontMapper::GetEncodingName(enc
);
777 #endif // wxUSE_FONTMAP
779 return desc
.Strip(wxString::both
).MakeLower();
782 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
784 // reset to the default state
787 // ToUserString() will quote the facename if it contains spaces, commas
788 // or semicolons: we must be able to understand that quoted text is
792 // parse a more or less free form string
793 wxStringTokenizer
tokenizer(toparse
, wxT(";, "), wxTOKEN_STRTOK
);
797 bool weightfound
= false, pointsizefound
= false;
799 bool encodingfound
= false;
801 bool insideQuotes
= false;
803 while ( tokenizer
.HasMoreTokens() )
805 wxString token
= tokenizer
.GetNextToken();
808 token
.Trim(true).Trim(false).MakeLower();
811 if (token
.StartsWith("'") ||
814 insideQuotes
= false;
816 // add this last token to the facename:
819 // normalize facename:
820 face
= face
.Trim(true).Trim(false);
821 face
.Replace("'", "");
828 if (token
.StartsWith("'"))
832 // look for the known tokens
835 // only the facename may be quoted:
839 if ( token
== wxT("underlined") || token
== _("underlined") )
843 else if ( token
== wxT("light") || token
== _("light") )
845 SetWeight(wxFONTWEIGHT_LIGHT
);
848 else if ( token
== wxT("bold") || token
== _("bold") )
850 SetWeight(wxFONTWEIGHT_BOLD
);
853 else if ( token
== wxT("italic") || token
== _("italic") )
855 SetStyle(wxFONTSTYLE_ITALIC
);
857 else if ( token
.ToULong(&size
) )
860 pointsizefound
= true;
865 // try to interpret this as an encoding
866 wxFontEncoding encoding
= wxFontMapper::Get()->CharsetToEncoding(token
, false);
867 if ( encoding
!= wxFONTENCODING_DEFAULT
&&
868 encoding
!= wxFONTENCODING_SYSTEM
) // returned when the recognition failed
870 SetEncoding(encoding
);
871 encodingfound
= true;
875 #endif // wxUSE_FONTMAP
877 // assume it is the face name
885 // skip the code which resets face below
890 #endif // wxUSE_FONTMAP
893 // if we had had the facename, we shouldn't continue appending tokens
894 // to it (i.e. "foo bold bar" shouldn't result in the facename "foo
899 if ( face
.EndsWith(" family", &familyStr
) )
901 // it's not a facename but rather a font family
903 if ( familyStr
== "decorative" )
904 family
= wxFONTFAMILY_DECORATIVE
;
905 else if ( familyStr
== "roman" )
906 family
= wxFONTFAMILY_ROMAN
;
907 else if ( familyStr
== "script" )
908 family
= wxFONTFAMILY_SCRIPT
;
909 else if ( familyStr
== "swiss" )
910 family
= wxFONTFAMILY_SWISS
;
911 else if ( familyStr
== "modern" )
912 family
= wxFONTFAMILY_MODERN
;
913 else if ( familyStr
== "teletype" )
914 family
= wxFONTFAMILY_TELETYPE
;
920 // NB: the check on the facename is implemented in wxFontBase::SetFaceName
921 // and not in wxNativeFontInfo::SetFaceName thus we need to explicitely
922 // call here wxFontEnumerator::IsValidFacename
925 !wxFontEnumerator::IsValidFacename(face
) ||
926 #endif // wxUSE_FONTENUM
929 SetFaceName(wxNORMAL_FONT
->GetFaceName());
936 // we might not have flushed it inside the loop
939 // NB: the check on the facename is implemented in wxFontBase::SetFaceName
940 // and not in wxNativeFontInfo::SetFaceName thus we need to explicitely
941 // call here wxFontEnumerator::IsValidFacename
944 !wxFontEnumerator::IsValidFacename(face
) ||
945 #endif // wxUSE_FONTENUM
948 SetFaceName(wxNORMAL_FONT
->GetFaceName());
952 // set point size to default value if size was not given
953 if ( !pointsizefound
)
954 SetPointSize(wxNORMAL_FONT
->GetPointSize());
956 // set font weight to default value if weight was not given
958 SetWeight(wxFONTWEIGHT_NORMAL
);
961 // set font encoding to default value if encoding was not given
962 if ( !encodingfound
)
963 SetEncoding(wxFONTENCODING_SYSTEM
);
964 #endif // wxUSE_FONTMAP
969 #endif // generic or wxMSW or wxOS2
972 // wxFont <-> wxString utilities, used by wxConfig
973 wxString
wxToString(const wxFontBase
& font
)
975 return font
.IsOk() ? font
.GetNativeFontInfoDesc()
979 bool wxFromString(const wxString
& str
, wxFontBase
*font
)
981 wxCHECK_MSG( font
, false, wxT("NULL output parameter") );
989 return font
->SetNativeFontInfo(str
);