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 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
31 #include "wx/dcscreen.h"
34 #include "wx/gdicmn.h"
36 #if defined(__WXMSW__)
37 #include "wx/msw/private.h" // includes windows.h for LOGFONT
38 #include "wx/msw/winundef.h"
41 #include "wx/fontutil.h" // for wxNativeFontInfo
42 #include "wx/fontmap.h"
44 #include "wx/tokenzr.h"
46 // ============================================================================
48 // ============================================================================
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 static void AdjustFontSize(wxFont
& font
, wxDC
& dc
, const wxSize
& pixelSize
)
60 bool initialGoodFound
= false;
61 bool initialBadFound
= false;
63 // NB: this assignment was separated from the variable definition
64 // in order to fix a gcc v3.3.3 compiler crash
65 currentSize
= font
.GetPointSize();
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 bool wxFontBase::operator==(const wxFont
& font
) const
305 // either it is the same font, i.e. they share the same common data or they
306 // have different ref datas but still describe the same font
307 return GetFontData() == font
.GetFontData() ||
310 GetPointSize() == font
.GetPointSize() &&
311 GetFamily() == font
.GetFamily() &&
312 GetStyle() == font
.GetStyle() &&
313 GetWeight() == font
.GetWeight() &&
314 GetUnderlined() == font
.GetUnderlined() &&
315 GetFaceName() == font
.GetFaceName() &&
316 GetEncoding() == font
.GetEncoding()
320 bool wxFontBase::operator!=(const wxFont
& font
) const
322 return !(*this == font
);
325 wxString
wxFontBase::GetFamilyString() const
327 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
329 switch ( GetFamily() )
331 case wxDECORATIVE
: return wxT("wxDECORATIVE");
332 case wxROMAN
: return wxT("wxROMAN");
333 case wxSCRIPT
: return wxT("wxSCRIPT");
334 case wxSWISS
: return wxT("wxSWISS");
335 case wxMODERN
: return wxT("wxMODERN");
336 case wxTELETYPE
: return wxT("wxTELETYPE");
337 default: return wxT("wxDEFAULT");
341 wxString
wxFontBase::GetStyleString() const
343 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
345 switch ( GetStyle() )
347 case wxNORMAL
: return wxT("wxNORMAL");
348 case wxSLANT
: return wxT("wxSLANT");
349 case wxITALIC
: return wxT("wxITALIC");
350 default: return wxT("wxDEFAULT");
354 wxString
wxFontBase::GetWeightString() const
356 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
358 switch ( GetWeight() )
360 case wxNORMAL
: return wxT("wxNORMAL");
361 case wxBOLD
: return wxT("wxBOLD");
362 case wxLIGHT
: return wxT("wxLIGHT");
363 default: return wxT("wxDEFAULT");
367 // ----------------------------------------------------------------------------
369 // ----------------------------------------------------------------------------
371 #ifdef wxNO_NATIVE_FONTINFO
373 // These are the generic forms of FromString()/ToString.
375 // convert to/from the string representation: format is
376 // version;pointsize;family;style;weight;underlined;facename;encoding
378 bool wxNativeFontInfo::FromString(const wxString
& s
)
382 wxStringTokenizer
tokenizer(s
, _T(";"));
384 wxString token
= tokenizer
.GetNextToken();
386 // Ignore the version for now
389 token
= tokenizer
.GetNextToken();
390 if ( !token
.ToLong(&l
) )
394 token
= tokenizer
.GetNextToken();
395 if ( !token
.ToLong(&l
) )
397 family
= (wxFontFamily
)l
;
399 token
= tokenizer
.GetNextToken();
400 if ( !token
.ToLong(&l
) )
402 style
= (wxFontStyle
)l
;
404 token
= tokenizer
.GetNextToken();
405 if ( !token
.ToLong(&l
) )
407 weight
= (wxFontWeight
)l
;
409 token
= tokenizer
.GetNextToken();
410 if ( !token
.ToLong(&l
) )
414 faceName
= tokenizer
.GetNextToken();
421 token
= tokenizer
.GetNextToken();
422 if ( !token
.ToLong(&l
) )
424 encoding
= (wxFontEncoding
)l
;
429 wxString
wxNativeFontInfo::ToString() const
433 s
.Printf(_T("%d;%d;%d;%d;%d;%d;%s;%d"),
446 void wxNativeFontInfo::Init()
449 family
= wxFONTFAMILY_DEFAULT
;
450 style
= wxFONTSTYLE_NORMAL
;
451 weight
= wxFONTWEIGHT_NORMAL
;
454 encoding
= wxFONTENCODING_DEFAULT
;
457 int wxNativeFontInfo::GetPointSize() const
462 wxFontStyle
wxNativeFontInfo::GetStyle() const
467 wxFontWeight
wxNativeFontInfo::GetWeight() const
472 bool wxNativeFontInfo::GetUnderlined() const
477 wxString
wxNativeFontInfo::GetFaceName() const
482 wxFontFamily
wxNativeFontInfo::GetFamily() const
487 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
492 void wxNativeFontInfo::SetPointSize(int pointsize
)
494 pointSize
= pointsize
;
497 void wxNativeFontInfo::SetStyle(wxFontStyle style_
)
502 void wxNativeFontInfo::SetWeight(wxFontWeight weight_
)
507 void wxNativeFontInfo::SetUnderlined(bool underlined_
)
509 underlined
= underlined_
;
512 void wxNativeFontInfo::SetFaceName(const wxString
& facename_
)
514 faceName
= facename_
;
517 void wxNativeFontInfo::SetFamily(wxFontFamily family_
)
522 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding_
)
524 encoding
= encoding_
;
527 #endif // generic wxNativeFontInfo implementation
529 // conversion to/from user-readable string: this is used in the generic
530 // versions and under MSW as well because there is no standard font description
531 // format there anyhow (but there is a well-defined standard for X11 fonts used
532 // by wxGTK and wxMotif)
534 #if defined(wxNO_NATIVE_FONTINFO) || defined(__WXMSW__) || defined (__WXPM__)
536 wxString
wxNativeFontInfo::ToUserString() const
540 // first put the adjectives, if any - this is English-centric, of course,
541 // but what else can we do?
542 if ( GetUnderlined() )
544 desc
<< _("underlined ");
547 switch ( GetWeight() )
550 wxFAIL_MSG( _T("unknown font weight") );
553 case wxFONTWEIGHT_NORMAL
:
556 case wxFONTWEIGHT_LIGHT
:
560 case wxFONTWEIGHT_BOLD
:
565 switch ( GetStyle() )
568 wxFAIL_MSG( _T("unknown font style") );
571 case wxFONTSTYLE_NORMAL
:
574 // we don't distinguish between the two for now anyhow...
575 case wxFONTSTYLE_ITALIC
:
576 case wxFONTSTYLE_SLANT
:
581 wxString face
= GetFaceName();
584 desc
<< _T(' ') << face
;
587 int size
= GetPointSize();
588 if ( size
!= wxNORMAL_FONT
->GetPointSize() )
590 desc
<< _T(' ') << size
;
594 wxFontEncoding enc
= GetEncoding();
595 if ( enc
!= wxFONTENCODING_DEFAULT
&& enc
!= wxFONTENCODING_SYSTEM
)
597 desc
<< _T(' ') << wxFontMapper::GetEncodingName(enc
);
599 #endif // wxUSE_FONTMAP
604 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
606 // reset to the default state
609 // parse a more or less free form string
611 // TODO: we should handle at least the quoted facenames
612 wxStringTokenizer
tokenizer(s
, _T(";, "), wxTOKEN_STRTOK
);
618 wxFontEncoding encoding
;
619 #endif // wxUSE_FONTMAP
621 while ( tokenizer
.HasMoreTokens() )
623 wxString token
= tokenizer
.GetNextToken();
626 token
.Trim(true).Trim(false).MakeLower();
628 // look for the known tokens
629 if ( token
== _T("underlined") || token
== _("underlined") )
633 else if ( token
== _T("light") || token
== _("light") )
635 SetWeight(wxFONTWEIGHT_LIGHT
);
637 else if ( token
== _T("bold") || token
== _("bold") )
639 SetWeight(wxFONTWEIGHT_BOLD
);
641 else if ( token
== _T("italic") || token
== _("italic") )
643 SetStyle(wxFONTSTYLE_ITALIC
);
645 else if ( token
.ToULong(&size
) )
650 else if ( (encoding
= wxFontMapper::Get()->CharsetToEncoding(token
, false))
651 != wxFONTENCODING_DEFAULT
)
653 SetEncoding(encoding
);
655 #endif // wxUSE_FONTMAP
656 else // assume it is the face name
665 // skip the code which resets face below
669 // if we had had the facename, we shouldn't continue appending tokens
670 // to it (i.e. "foo bold bar" shouldn't result in the facename "foo
679 // we might not have flushed it inside the loop
688 #endif // generic or wxMSW or wxOS2