1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "fontbase.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
34 #include "wx/dcscreen.h"
37 #include "wx/gdicmn.h"
39 #if defined(__WXMSW__)
40 #include "wx/msw/private.h" // includes windows.h for LOGFONT
41 #include "wx/msw/winundef.h"
44 #include "wx/fontutil.h" // for wxNativeFontInfo
45 #include "wx/fontmap.h"
47 #include "wx/tokenzr.h"
49 // ============================================================================
51 // ============================================================================
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 static void AdjustFontSize(wxFont font
, wxDC
& dc
, const wxSize
& pixelSize
)
59 int currentSize
= font
.GetPointSize();
63 bool initialGoodFound
= false;
64 bool initialBadFound
= false;
66 while (currentSize
> 0)
70 // if currentSize (in points) results in a font that is smaller
71 // than required by pixelSize it is considered a good size
72 if (dc
.GetCharHeight() <= pixelSize
.GetHeight() &&
73 (!pixelSize
.GetWidth() ||
74 dc
.GetCharWidth() <= pixelSize
.GetWidth()))
76 largestGood
= currentSize
;
77 initialGoodFound
= true;
81 smallestBad
= currentSize
;
82 initialBadFound
= true;
84 if (!initialGoodFound
)
88 else if (!initialBadFound
)
94 int distance
= smallestBad
- largestGood
;
98 currentSize
= largestGood
+ distance
/ 2;
101 font
.SetPointSize(currentSize
);
104 if (currentSize
!= largestGood
)
105 font
.SetPointSize(largestGood
);
108 // ----------------------------------------------------------------------------
110 // ----------------------------------------------------------------------------
112 wxFontEncoding
wxFontBase::ms_encodingDefault
= wxFONTENCODING_SYSTEM
;
115 void wxFontBase::SetDefaultEncoding(wxFontEncoding encoding
)
117 // GetDefaultEncoding() should return something != wxFONTENCODING_DEFAULT
118 // and, besides, using this value here doesn't make any sense
119 wxCHECK_RET( encoding
!= wxFONTENCODING_DEFAULT
,
120 _T("can't set default encoding to wxFONTENCODING_DEFAULT") );
122 ms_encodingDefault
= encoding
;
125 wxFontBase::~wxFontBase()
127 // this destructor is required for Darwin
131 wxFont
*wxFontBase::New(int size
,
136 const wxString
& face
,
137 wxFontEncoding encoding
)
139 return new wxFont(size
, family
, style
, weight
, underlined
, face
, encoding
);
142 static inline int flags2Style(int flags
)
144 return flags
& wxFONTFLAG_ITALIC
146 : flags
& wxFONTFLAG_SLANT
148 : wxFONTSTYLE_NORMAL
;
151 static inline int flags2Weight(int flags
)
153 return flags
& wxFONTFLAG_LIGHT
155 : flags
& wxFONTFLAG_BOLD
157 : wxFONTWEIGHT_NORMAL
;
160 static inline bool flags2Underlined(int flags
)
162 return (flags
& wxFONTFLAG_UNDERLINED
) != 0;
166 wxFont
*wxFontBase::New(int pointSize
,
169 const wxString
& face
,
170 wxFontEncoding encoding
)
172 return New(pointSize
, family
, flags2Style(flags
), flags2Weight(flags
),
173 flags2Underlined(flags
), face
, encoding
);
177 wxFont
*wxFontBase::New(const wxSize
& pixelSize
,
182 const wxString
& face
,
183 wxFontEncoding encoding
)
185 #if defined(__WXMSW__)
186 return new wxFont(pixelSize
, family
, style
, weight
, underlined
,
189 wxFont
*self
= New(10, family
, style
, weight
, underlined
, face
, encoding
);
191 AdjustFontSize(*(wxFont
*)self
, dc
, pixelSize
);
197 wxFont
*wxFontBase::New(const wxSize
& pixelSize
,
200 const wxString
& face
,
201 wxFontEncoding encoding
)
203 return New(pixelSize
, family
, flags2Style(flags
), flags2Weight(flags
),
204 flags2Underlined(flags
), face
, encoding
);
207 wxSize
wxFontBase::GetPixelSize() const
210 dc
.SetFont(*(wxFont
*)this);
211 return wxSize(dc
.GetCharWidth(), dc
.GetCharHeight());
214 bool wxFontBase::IsUsingSizeInPixels() const
219 void wxFontBase::SetPixelSize( const wxSize
& pixelSize
)
222 AdjustFontSize(*(wxFont
*)this, dc
, pixelSize
);
226 wxFont
*wxFontBase::New(const wxNativeFontInfo
& info
)
228 return new wxFont(info
);
232 wxFont
*wxFontBase::New(const wxString
& strNativeFontDesc
)
234 wxNativeFontInfo fontInfo
;
235 if ( !fontInfo
.FromString(strNativeFontDesc
) )
236 return new wxFont(*wxNORMAL_FONT
);
238 return New(fontInfo
);
241 bool wxFontBase::IsFixedWidth() const
243 return GetFamily() == wxFONTFAMILY_TELETYPE
;
246 void wxFontBase::DoSetNativeFontInfo(const wxNativeFontInfo
& info
)
248 #ifdef wxNO_NATIVE_FONTINFO
249 SetPointSize(info
.pointSize
);
250 SetFamily(info
.family
);
251 SetStyle(info
.style
);
252 SetWeight(info
.weight
);
253 SetUnderlined(info
.underlined
);
254 SetFaceName(info
.faceName
);
255 SetEncoding(info
.encoding
);
261 wxString
wxFontBase::GetNativeFontInfoDesc() const
264 const wxNativeFontInfo
*fontInfo
= GetNativeFontInfo();
267 fontDesc
= fontInfo
->ToString();
273 wxString
wxFontBase::GetNativeFontInfoUserDesc() const
276 const wxNativeFontInfo
*fontInfo
= GetNativeFontInfo();
279 fontDesc
= fontInfo
->ToUserString();
285 void wxFontBase::SetNativeFontInfo(const wxString
& info
)
287 wxNativeFontInfo fontInfo
;
288 if ( !info
.empty() && fontInfo
.FromString(info
) )
290 SetNativeFontInfo(fontInfo
);
294 void wxFontBase::SetNativeFontInfoUserDesc(const wxString
& info
)
296 wxNativeFontInfo fontInfo
;
297 if ( !info
.empty() && fontInfo
.FromUserString(info
) )
299 SetNativeFontInfo(fontInfo
);
303 wxFont
& wxFont::operator=(const wxFont
& font
)
308 return (wxFont
&)*this;
311 bool wxFontBase::operator==(const wxFont
& font
) const
313 // either it is the same font, i.e. they share the same common data or they
314 // have different ref datas but still describe the same font
315 return GetFontData() == font
.GetFontData() ||
318 GetPointSize() == font
.GetPointSize() &&
319 GetFamily() == font
.GetFamily() &&
320 GetStyle() == font
.GetStyle() &&
321 GetWeight() == font
.GetWeight() &&
322 GetUnderlined() == font
.GetUnderlined() &&
323 GetFaceName() == font
.GetFaceName() &&
324 GetEncoding() == font
.GetEncoding()
328 bool wxFontBase::operator!=(const wxFont
& font
) const
330 return !(*this == font
);
333 wxString
wxFontBase::GetFamilyString() const
335 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
337 switch ( GetFamily() )
339 case wxDECORATIVE
: return wxT("wxDECORATIVE");
340 case wxROMAN
: return wxT("wxROMAN");
341 case wxSCRIPT
: return wxT("wxSCRIPT");
342 case wxSWISS
: return wxT("wxSWISS");
343 case wxMODERN
: return wxT("wxMODERN");
344 case wxTELETYPE
: return wxT("wxTELETYPE");
345 default: return wxT("wxDEFAULT");
349 wxString
wxFontBase::GetStyleString() const
351 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
353 switch ( GetStyle() )
355 case wxNORMAL
: return wxT("wxNORMAL");
356 case wxSLANT
: return wxT("wxSLANT");
357 case wxITALIC
: return wxT("wxITALIC");
358 default: return wxT("wxDEFAULT");
362 wxString
wxFontBase::GetWeightString() const
364 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
366 switch ( GetWeight() )
368 case wxNORMAL
: return wxT("wxNORMAL");
369 case wxBOLD
: return wxT("wxBOLD");
370 case wxLIGHT
: return wxT("wxLIGHT");
371 default: return wxT("wxDEFAULT");
375 // ----------------------------------------------------------------------------
377 // ----------------------------------------------------------------------------
379 #ifdef wxNO_NATIVE_FONTINFO
381 // These are the generic forms of FromString()/ToString.
383 // convert to/from the string representation: format is
384 // version;pointsize;family;style;weight;underlined;facename;encoding
386 bool wxNativeFontInfo::FromString(const wxString
& s
)
390 wxStringTokenizer
tokenizer(s
, _T(";"));
392 wxString token
= tokenizer
.GetNextToken();
394 // Ignore the version for now
397 token
= tokenizer
.GetNextToken();
398 if ( !token
.ToLong(&l
) )
402 token
= tokenizer
.GetNextToken();
403 if ( !token
.ToLong(&l
) )
405 family
= (wxFontFamily
)l
;
407 token
= tokenizer
.GetNextToken();
408 if ( !token
.ToLong(&l
) )
410 style
= (wxFontStyle
)l
;
412 token
= tokenizer
.GetNextToken();
413 if ( !token
.ToLong(&l
) )
415 weight
= (wxFontWeight
)l
;
417 token
= tokenizer
.GetNextToken();
418 if ( !token
.ToLong(&l
) )
422 faceName
= tokenizer
.GetNextToken();
429 token
= tokenizer
.GetNextToken();
430 if ( !token
.ToLong(&l
) )
432 encoding
= (wxFontEncoding
)l
;
437 wxString
wxNativeFontInfo::ToString() const
441 s
.Printf(_T("%d;%d;%d;%d;%d;%d;%s;%d"),
454 void wxNativeFontInfo::Init()
457 family
= wxFONTFAMILY_DEFAULT
;
458 style
= wxFONTSTYLE_NORMAL
;
459 weight
= wxFONTWEIGHT_NORMAL
;
462 encoding
= wxFONTENCODING_DEFAULT
;
465 int wxNativeFontInfo::GetPointSize() const
470 wxFontStyle
wxNativeFontInfo::GetStyle() const
475 wxFontWeight
wxNativeFontInfo::GetWeight() const
480 bool wxNativeFontInfo::GetUnderlined() const
485 wxString
wxNativeFontInfo::GetFaceName() const
490 wxFontFamily
wxNativeFontInfo::GetFamily() const
495 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
500 void wxNativeFontInfo::SetPointSize(int pointsize
)
502 pointSize
= pointsize
;
505 void wxNativeFontInfo::SetStyle(wxFontStyle style_
)
510 void wxNativeFontInfo::SetWeight(wxFontWeight weight_
)
515 void wxNativeFontInfo::SetUnderlined(bool underlined_
)
517 underlined
= underlined_
;
520 void wxNativeFontInfo::SetFaceName(wxString facename_
)
522 faceName
= facename_
;
525 void wxNativeFontInfo::SetFamily(wxFontFamily family_
)
530 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding_
)
532 encoding
= encoding_
;
535 #endif // generic wxNativeFontInfo implementation
537 // conversion to/from user-readable string: this is used in the generic
538 // versions and under MSW as well because there is no standard font description
539 // format there anyhow (but there is a well-defined standard for X11 fonts used
540 // by wxGTK and wxMotif)
542 #if defined(wxNO_NATIVE_FONTINFO) || defined(__WXMSW__) || defined (__WXPM__)
544 wxString
wxNativeFontInfo::ToUserString() const
548 // first put the adjectives, if any - this is English-centric, of course,
549 // but what else can we do?
550 if ( GetUnderlined() )
552 desc
<< _("underlined ");
555 switch ( GetWeight() )
558 wxFAIL_MSG( _T("unknown font weight") );
561 case wxFONTWEIGHT_NORMAL
:
564 case wxFONTWEIGHT_LIGHT
:
568 case wxFONTWEIGHT_BOLD
:
573 switch ( GetStyle() )
576 wxFAIL_MSG( _T("unknown font style") );
579 case wxFONTSTYLE_NORMAL
:
582 // we don't distinguish between the two for now anyhow...
583 case wxFONTSTYLE_ITALIC
:
584 case wxFONTSTYLE_SLANT
:
589 wxString face
= GetFaceName();
592 desc
<< _T(' ') << face
;
595 int size
= GetPointSize();
596 if ( size
!= wxNORMAL_FONT
->GetPointSize() )
598 desc
<< _T(' ') << size
;
602 wxFontEncoding enc
= GetEncoding();
603 if ( enc
!= wxFONTENCODING_DEFAULT
&& enc
!= wxFONTENCODING_SYSTEM
)
605 desc
<< _T(' ') << wxFontMapper::Get()->GetEncodingName(enc
);
607 #endif // wxUSE_FONTMAP
612 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
614 // reset to the default state
617 // parse a more or less free form string
619 // TODO: we should handle at least the quoted facenames
620 wxStringTokenizer
tokenizer(s
, _T(";, "), wxTOKEN_STRTOK
);
626 wxFontEncoding encoding
;
627 #endif // wxUSE_FONTMAP
629 while ( tokenizer
.HasMoreTokens() )
631 wxString token
= tokenizer
.GetNextToken();
634 token
.Trim(true).Trim(false).MakeLower();
636 // look for the known tokens
637 if ( token
== _T("underlined") || token
== _("underlined") )
641 else if ( token
== _T("light") || token
== _("light") )
643 SetWeight(wxFONTWEIGHT_LIGHT
);
645 else if ( token
== _T("bold") || token
== _("bold") )
647 SetWeight(wxFONTWEIGHT_BOLD
);
649 else if ( token
== _T("italic") || token
== _("italic") )
651 SetStyle(wxFONTSTYLE_ITALIC
);
653 else if ( token
.ToULong(&size
) )
658 else if ( (encoding
= wxFontMapper::Get()->CharsetToEncoding(token
, false))
659 != wxFONTENCODING_DEFAULT
)
661 SetEncoding(encoding
);
663 #endif // wxUSE_FONTMAP
664 else // assume it is the face name
673 // skip the code which resets face below
677 // if we had had the facename, we shouldn't continue appending tokens
678 // to it (i.e. "foo bold bar" shouldn't result in the facename "foo
687 // we might not have flushed it inside the loop
696 #endif // generic or wxMSW or wxOS2