1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/font.cpp
3 // Purpose: wxFont class
4 // Author: Julian Smart
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 "font.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
38 #include "wx/encinfo.h"
41 #include "wx/msw/private.h"
43 #include "wx/fontutil.h"
44 #include "wx/fontmap.h"
46 #include "wx/tokenzr.h"
48 #if wxUSE_EXTENDED_RTTI
50 wxBEGIN_ENUM( wxFontFamily
)
51 wxENUM_MEMBER( wxDEFAULT
)
52 wxENUM_MEMBER( wxDECORATIVE
)
53 wxENUM_MEMBER( wxROMAN
)
54 wxENUM_MEMBER( wxSCRIPT
)
55 wxENUM_MEMBER( wxSWISS
)
56 wxENUM_MEMBER( wxMODERN
)
57 wxENUM_MEMBER( wxTELETYPE
)
58 wxEND_ENUM( wxFontFamily
)
60 wxBEGIN_ENUM( wxFontStyle
)
61 wxENUM_MEMBER( wxNORMAL
)
62 wxENUM_MEMBER( wxITALIC
)
63 wxENUM_MEMBER( wxSLANT
)
64 wxEND_ENUM( wxFontStyle
)
66 wxBEGIN_ENUM( wxFontWeight
)
67 wxENUM_MEMBER( wxNORMAL
)
68 wxENUM_MEMBER( wxLIGHT
)
69 wxENUM_MEMBER( wxBOLD
)
70 wxEND_ENUM( wxFontWeight
)
72 IMPLEMENT_DYNAMIC_CLASS_WITH_COPY_XTI(wxFont
, wxGDIObject
,"wx/font.h")
74 wxBEGIN_PROPERTIES_TABLE(wxFont
)
75 wxPROPERTY( Size
,int, SetPointSize
, GetPointSize
, 12 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
76 wxPROPERTY( Family
, int , SetFamily
, GetFamily
, (int)wxDEFAULT
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // wxFontFamily
77 wxPROPERTY( Style
, int , SetStyle
, GetStyle
, (int)wxNORMAL
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // wxFontStyle
78 wxPROPERTY( Weight
, int , SetWeight
, GetWeight
, (int)wxNORMAL
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // wxFontWeight
79 wxPROPERTY( Underlined
, bool , SetUnderlined
, GetUnderlined
, false , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
80 wxPROPERTY( Face
, wxString
, SetFaceName
, GetFaceName
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
81 wxPROPERTY( Encoding
, wxFontEncoding
, SetEncoding
, GetEncoding
, wxFONTENCODING_DEFAULT
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
82 wxEND_PROPERTIES_TABLE()
84 wxCONSTRUCTOR_6( wxFont
, int , Size
, int , Family
, int , Style
, int , Weight
, bool , Underlined
, wxString
, Face
)
86 wxBEGIN_HANDLERS_TABLE(wxFont
)
87 wxEND_HANDLERS_TABLE()
90 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
94 // ----------------------------------------------------------------------------
96 // ----------------------------------------------------------------------------
98 // the mask used to extract the pitch from LOGFONT::lfPitchAndFamily field
99 static const int PITCH_MASK
= FIXED_PITCH
| VARIABLE_PITCH
;
101 // ----------------------------------------------------------------------------
102 // wxFontRefData - the internal description of the font
103 // ----------------------------------------------------------------------------
105 class WXDLLEXPORT wxFontRefData
: public wxGDIRefData
111 Init(-1, wxSize(0,0), false, wxFONTFAMILY_DEFAULT
, wxFONTSTYLE_NORMAL
,
112 wxFONTWEIGHT_NORMAL
, false, wxEmptyString
,
113 wxFONTENCODING_DEFAULT
);
116 wxFontRefData(int size
,
117 const wxSize
& pixelSize
,
118 bool sizeUsingPixels
,
123 const wxString
& faceName
,
124 wxFontEncoding encoding
)
126 Init(size
, pixelSize
, sizeUsingPixels
, family
, style
, weight
,
127 underlined
, faceName
, encoding
);
130 wxFontRefData(const wxNativeFontInfo
& info
, WXHFONT hFont
= 0)
135 wxFontRefData(const wxFontRefData
& data
) : wxGDIRefData()
137 if ( data
.m_nativeFontInfoOk
)
139 Init(data
.m_nativeFontInfo
);
143 Init(data
.m_pointSize
, data
.m_pixelSize
, data
.m_sizeUsingPixels
,
144 data
.m_family
, data
.m_style
, data
.m_weight
,
145 data
.m_underlined
, data
.m_faceName
, data
.m_encoding
);
149 virtual ~wxFontRefData();
152 bool Alloc(wxFont
*font
);
156 // all wxFont accessors
157 int GetPointSize() const
159 return m_nativeFontInfoOk
? m_nativeFontInfo
.GetPointSize()
163 wxSize
GetPixelSize() const
165 return m_nativeFontInfoOk
? m_nativeFontInfo
.GetPixelSize()
169 bool IsUsingSizeInPixels() const
171 return m_nativeFontInfoOk
? true : m_sizeUsingPixels
;
174 int GetFamily() const
181 return m_nativeFontInfoOk
? m_nativeFontInfo
.GetStyle()
185 int GetWeight() const
187 return m_nativeFontInfoOk
? m_nativeFontInfo
.GetWeight()
191 bool GetUnderlined() const
193 return m_nativeFontInfoOk
? m_nativeFontInfo
.GetUnderlined()
197 wxString
GetFaceName() const
200 if ( m_nativeFontInfoOk
)
201 s
= m_nativeFontInfo
.GetFaceName();
208 wxFontEncoding
GetEncoding() const
210 return m_nativeFontInfoOk
? m_nativeFontInfo
.GetEncoding()
214 WXHFONT
GetHFONT() const { return m_hFont
; }
217 void SetPointSize(int pointSize
)
219 if ( m_nativeFontInfoOk
)
221 m_nativeFontInfo
.SetPointSize(pointSize
);
225 m_pointSize
= pointSize
;
226 m_sizeUsingPixels
= false;
230 void SetPixelSize(const wxSize
& pixelSize
)
232 if ( m_nativeFontInfoOk
)
234 m_nativeFontInfo
.SetPixelSize(pixelSize
);
238 m_pixelSize
= pixelSize
;
239 m_sizeUsingPixels
= true;
243 void SetFamily(int family
)
248 void SetStyle(int style
)
250 if ( m_nativeFontInfoOk
)
251 m_nativeFontInfo
.SetStyle((wxFontStyle
)style
);
256 void SetWeight(int weight
)
258 if ( m_nativeFontInfoOk
)
259 m_nativeFontInfo
.SetWeight((wxFontWeight
)weight
);
264 void SetFaceName(const wxString
& faceName
)
266 if ( m_nativeFontInfoOk
)
267 m_nativeFontInfo
.SetFaceName(faceName
);
269 m_faceName
= faceName
;
272 void SetUnderlined(bool underlined
)
274 if ( m_nativeFontInfoOk
)
275 m_nativeFontInfo
.SetUnderlined(underlined
);
277 m_underlined
= underlined
;
280 void SetEncoding(wxFontEncoding encoding
)
282 if ( m_nativeFontInfoOk
)
283 m_nativeFontInfo
.SetEncoding(encoding
);
285 m_encoding
= encoding
;
288 // native font info tests
289 bool HasNativeFontInfo() const { return m_nativeFontInfoOk
; }
291 const wxNativeFontInfo
& GetNativeFontInfo() const
292 { return m_nativeFontInfo
; }
295 // common part of all ctors
297 const wxSize
& pixelSize
,
298 bool sizeUsingPixels
,
303 const wxString
& faceName
,
304 wxFontEncoding encoding
);
306 void Init(const wxNativeFontInfo
& info
, WXHFONT hFont
= 0);
308 // font characterstics
311 bool m_sizeUsingPixels
;
317 wxFontEncoding m_encoding
;
319 // Windows font handle
323 wxNativeFontInfo m_nativeFontInfo
;
324 bool m_nativeFontInfoOk
;
327 // ============================================================================
329 // ============================================================================
331 // ----------------------------------------------------------------------------
333 // ----------------------------------------------------------------------------
335 void wxFontRefData::Init(int pointSize
,
336 const wxSize
& pixelSize
,
337 bool sizeUsingPixels
,
342 const wxString
& faceName
,
343 wxFontEncoding encoding
)
346 m_pointSize
= pointSize
== -1 ? wxNORMAL_FONT
->GetPointSize() : pointSize
;
347 m_pixelSize
= pixelSize
;
348 m_sizeUsingPixels
= sizeUsingPixels
;
352 m_underlined
= underlined
;
353 m_faceName
= faceName
;
354 m_encoding
= encoding
;
358 m_nativeFontInfoOk
= false;
361 void wxFontRefData::Init(const wxNativeFontInfo
& info
, WXHFONT hFont
)
363 // hFont may be zero, or it be passed in case we really want to
364 // use the exact font created in the underlying system
365 // (for example where we can't guarantee conversion from HFONT
366 // to LOGFONT back to HFONT)
369 m_nativeFontInfoOk
= true;
370 m_nativeFontInfo
= info
;
371 // This is the best we can do since we don't have the
372 // correct information at this point.
376 wxFontRefData::~wxFontRefData()
381 bool wxFontRefData::Alloc(wxFont
*font
)
383 if ( !m_nativeFontInfoOk
)
385 wxFillLogFont(&m_nativeFontInfo
.lf
, font
);
386 m_nativeFontInfoOk
= true;
389 HFONT hfont
= ::CreateFontIndirect(&m_nativeFontInfo
.lf
);
392 wxLogLastError(wxT("CreateFont"));
397 m_hFont
= (WXHFONT
)hfont
;
402 void wxFontRefData::Free()
406 if ( !::DeleteObject((HFONT
) m_hFont
) )
408 wxLogLastError(wxT("DeleteObject(font)"));
415 // ----------------------------------------------------------------------------
417 // ----------------------------------------------------------------------------
419 void wxNativeFontInfo::Init()
424 int wxNativeFontInfo::GetPointSize() const
426 // FIXME: using the screen here results in incorrect font size calculation
428 const int ppInch
= ::GetDeviceCaps(ScreenHDC(), LOGPIXELSY
);
430 return (int) (((72.0*(double)abs(lf
.lfHeight
)) / (double) ppInch
) + 0.5);
433 wxSize
wxNativeFontInfo::GetPixelSize() const
436 ret
.SetHeight(lf
.lfHeight
);
437 ret
.SetWidth(lf
.lfWidth
);
441 wxFontStyle
wxNativeFontInfo::GetStyle() const
443 return lf
.lfItalic
? wxFONTSTYLE_ITALIC
: wxFONTSTYLE_NORMAL
;
446 wxFontWeight
wxNativeFontInfo::GetWeight() const
448 if ( lf
.lfWeight
<= 300 )
449 return wxFONTWEIGHT_LIGHT
;
451 if ( lf
.lfWeight
>= 600 )
452 return wxFONTWEIGHT_BOLD
;
454 return wxFONTWEIGHT_NORMAL
;
457 bool wxNativeFontInfo::GetUnderlined() const
459 return lf
.lfUnderline
!= 0;
462 wxString
wxNativeFontInfo::GetFaceName() const
464 return lf
.lfFaceName
;
467 wxFontFamily
wxNativeFontInfo::GetFamily() const
471 // extract family from pitch-and-family
472 switch ( lf
.lfPitchAndFamily
& ~PITCH_MASK
)
475 family
= wxFONTFAMILY_ROMAN
;
479 wxFAIL_MSG( _T("unknown LOGFONT::lfFamily value") );
483 family
= wxFONTFAMILY_SWISS
;
487 family
= wxFONTFAMILY_SCRIPT
;
491 family
= wxFONTFAMILY_MODERN
;
495 family
= wxFONTFAMILY_DECORATIVE
;
502 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
504 return wxGetFontEncFromCharSet(lf
.lfCharSet
);
507 void wxNativeFontInfo::SetPointSize(int pointsize
)
509 // FIXME: using the screen here results in incorrect font size calculation
511 const int ppInch
= ::GetDeviceCaps(ScreenHDC(), LOGPIXELSY
);
513 lf
.lfHeight
= -(int)((pointsize
*((double)ppInch
)/72.0) + 0.5);
516 void wxNativeFontInfo::SetPixelSize(const wxSize
& pixelSize
)
518 lf
.lfHeight
= pixelSize
.GetHeight();
519 lf
.lfWidth
= pixelSize
.GetWidth();
523 void wxNativeFontInfo::SetStyle(wxFontStyle style
)
528 wxFAIL_MSG( _T("unknown font style") );
531 case wxFONTSTYLE_NORMAL
:
535 case wxFONTSTYLE_ITALIC
:
536 case wxFONTSTYLE_SLANT
:
542 void wxNativeFontInfo::SetWeight(wxFontWeight weight
)
547 wxFAIL_MSG( _T("unknown font weight") );
550 case wxFONTWEIGHT_NORMAL
:
551 lf
.lfWeight
= FW_NORMAL
;
554 case wxFONTWEIGHT_LIGHT
:
555 lf
.lfWeight
= FW_LIGHT
;
558 case wxFONTWEIGHT_BOLD
:
559 lf
.lfWeight
= FW_BOLD
;
564 void wxNativeFontInfo::SetUnderlined(bool underlined
)
566 lf
.lfUnderline
= underlined
;
569 void wxNativeFontInfo::SetFaceName(wxString facename
)
571 wxStrncpy(lf
.lfFaceName
, facename
, WXSIZEOF(lf
.lfFaceName
));
574 void wxNativeFontInfo::SetFamily(wxFontFamily family
)
582 ff_family
= FF_SCRIPT
;
583 facename
= _T("Script");
587 ff_family
= FF_DECORATIVE
;
588 facename
= _T("Old English Text MT");
592 ff_family
= FF_ROMAN
;
593 facename
= _T("Times New Roman");
598 ff_family
= FF_MODERN
;
599 facename
= _T("Courier New");
603 ff_family
= FF_SWISS
;
604 facename
= _T("Arial");
610 // We want Windows 2000 or later to have new fonts even MS Shell Dlg
611 // is returned as default GUI font for compatibility
613 ff_family
= FF_SWISS
;
614 if(wxGetOsVersion(&verMaj
) == wxWINDOWS_NT
&& verMaj
>= 5)
615 facename
= _T("MS Shell Dlg 2");
617 facename
= _T("MS Shell Dlg");
621 lf
.lfPitchAndFamily
= (BYTE
)(DEFAULT_PITCH
) | ff_family
;
623 if ( !wxStrlen(lf
.lfFaceName
) )
625 SetFaceName(facename
);
629 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding
)
631 wxNativeEncodingInfo info
;
632 if ( !wxGetNativeFontEncoding(encoding
, &info
) )
635 if ( wxFontMapper::Get()->GetAltForEncoding(encoding
, &info
) )
637 if ( !info
.facename
.empty() )
639 // if we have this encoding only in some particular facename, use
640 // the facename - it is better to show the correct characters in a
641 // wrong facename than unreadable text in a correct one
642 SetFaceName(info
.facename
);
646 #endif // wxUSE_FONTMAP
648 // unsupported encoding, replace with the default
649 info
.charset
= DEFAULT_CHARSET
;
653 lf
.lfCharSet
= (BYTE
)info
.charset
;
656 bool wxNativeFontInfo::FromString(const wxString
& s
)
660 wxStringTokenizer
tokenizer(s
, _T(";"));
663 wxString token
= tokenizer
.GetNextToken();
664 if ( token
!= _T('0') )
667 token
= tokenizer
.GetNextToken();
668 if ( !token
.ToLong(&l
) )
672 token
= tokenizer
.GetNextToken();
673 if ( !token
.ToLong(&l
) )
677 token
= tokenizer
.GetNextToken();
678 if ( !token
.ToLong(&l
) )
682 token
= tokenizer
.GetNextToken();
683 if ( !token
.ToLong(&l
) )
685 lf
.lfOrientation
= l
;
687 token
= tokenizer
.GetNextToken();
688 if ( !token
.ToLong(&l
) )
692 token
= tokenizer
.GetNextToken();
693 if ( !token
.ToLong(&l
) )
695 lf
.lfItalic
= (BYTE
)l
;
697 token
= tokenizer
.GetNextToken();
698 if ( !token
.ToLong(&l
) )
700 lf
.lfUnderline
= (BYTE
)l
;
702 token
= tokenizer
.GetNextToken();
703 if ( !token
.ToLong(&l
) )
705 lf
.lfStrikeOut
= (BYTE
)l
;
707 token
= tokenizer
.GetNextToken();
708 if ( !token
.ToLong(&l
) )
710 lf
.lfCharSet
= (BYTE
)l
;
712 token
= tokenizer
.GetNextToken();
713 if ( !token
.ToLong(&l
) )
715 lf
.lfOutPrecision
= (BYTE
)l
;
717 token
= tokenizer
.GetNextToken();
718 if ( !token
.ToLong(&l
) )
720 lf
.lfClipPrecision
= (BYTE
)l
;
722 token
= tokenizer
.GetNextToken();
723 if ( !token
.ToLong(&l
) )
725 lf
.lfQuality
= (BYTE
)l
;
727 token
= tokenizer
.GetNextToken();
728 if ( !token
.ToLong(&l
) )
730 lf
.lfPitchAndFamily
= (BYTE
)l
;
732 token
= tokenizer
.GetNextToken();
735 wxStrcpy(lf
.lfFaceName
, token
.c_str());
740 wxString
wxNativeFontInfo::ToString() const
744 s
.Printf(_T("%d;%ld;%ld;%ld;%ld;%ld;%d;%d;%d;%d;%d;%d;%d;%d;%s"),
745 0, // version, in case we want to change the format later
764 // ----------------------------------------------------------------------------
766 // ----------------------------------------------------------------------------
772 bool wxFont::Create(const wxNativeFontInfo
& info
, WXHFONT hFont
)
776 m_refData
= new wxFontRefData(info
, hFont
);
783 wxFont::wxFont(const wxString
& fontdesc
)
785 wxNativeFontInfo info
;
786 if ( info
.FromString(fontdesc
) )
790 /* Constructor for a font. Note that the real construction is done
791 * in wxDC::SetFont, when information is available about scaling etc.
793 bool wxFont::DoCreate(int pointSize
,
794 const wxSize
& pixelSize
,
795 bool sizeUsingPixels
,
800 const wxString
& faceName
,
801 wxFontEncoding encoding
)
805 // wxDEFAULT is a valid value for the font size too so we must treat it
806 // specially here (otherwise the size would be 70 == wxDEFAULT value)
807 if ( pointSize
== wxDEFAULT
)
809 pointSize
= wxNORMAL_FONT
->GetPointSize();
812 m_refData
= new wxFontRefData(pointSize
, pixelSize
, sizeUsingPixels
,
813 family
, style
, weight
,
814 underlined
, faceName
, encoding
);
825 // ----------------------------------------------------------------------------
826 // real implementation
827 // ----------------------------------------------------------------------------
829 bool wxFont::RealizeResource()
831 if ( GetResourceHandle() )
833 // VZ: the old code returned false in this case, but it doesn't seem
834 // to make sense because the font _was_ created
838 return M_FONTDATA
->Alloc(this);
841 bool wxFont::FreeResource(bool WXUNUSED(force
))
843 if ( GetResourceHandle() )
853 WXHANDLE
wxFont::GetResourceHandle() const
855 return (WXHANDLE
)GetHFONT();
858 WXHFONT
wxFont::GetHFONT() const
860 return M_FONTDATA
? M_FONTDATA
->GetHFONT() : 0;
863 bool wxFont::IsFree() const
865 return M_FONTDATA
&& (M_FONTDATA
->GetHFONT() == 0);
868 void wxFont::Unshare()
870 // Don't change shared data
873 m_refData
= new wxFontRefData();
877 wxFontRefData
* ref
= new wxFontRefData(*M_FONTDATA
);
883 // ----------------------------------------------------------------------------
884 // change font attribute: we recreate font when doing it
885 // ----------------------------------------------------------------------------
887 void wxFont::SetPointSize(int pointSize
)
891 M_FONTDATA
->SetPointSize(pointSize
);
896 void wxFont::SetPixelSize(const wxSize
& pixelSize
)
900 M_FONTDATA
->SetPixelSize(pixelSize
);
905 void wxFont::SetFamily(int family
)
909 M_FONTDATA
->SetFamily(family
);
914 void wxFont::SetStyle(int style
)
918 M_FONTDATA
->SetStyle(style
);
923 void wxFont::SetWeight(int weight
)
927 M_FONTDATA
->SetWeight(weight
);
932 void wxFont::SetFaceName(const wxString
& faceName
)
936 M_FONTDATA
->SetFaceName(faceName
);
941 void wxFont::SetUnderlined(bool underlined
)
945 M_FONTDATA
->SetUnderlined(underlined
);
950 void wxFont::SetEncoding(wxFontEncoding encoding
)
954 M_FONTDATA
->SetEncoding(encoding
);
959 void wxFont::DoSetNativeFontInfo(const wxNativeFontInfo
& info
)
965 *M_FONTDATA
= wxFontRefData(info
);
970 // ----------------------------------------------------------------------------
972 // ----------------------------------------------------------------------------
974 int wxFont::GetPointSize() const
976 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
978 return M_FONTDATA
->GetPointSize();
981 wxSize
wxFont::GetPixelSize() const
983 return M_FONTDATA
->GetPixelSize();
986 bool wxFont::IsUsingSizeInPixels() const
988 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
990 return M_FONTDATA
->IsUsingSizeInPixels();
993 int wxFont::GetFamily() const
995 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
997 return M_FONTDATA
->GetFamily();
1000 int wxFont::GetStyle() const
1002 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
1004 return M_FONTDATA
->GetStyle();
1007 int wxFont::GetWeight() const
1009 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
1011 return M_FONTDATA
->GetWeight();
1014 bool wxFont::GetUnderlined() const
1016 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
1018 return M_FONTDATA
->GetUnderlined();
1021 wxString
wxFont::GetFaceName() const
1023 wxCHECK_MSG( Ok(), wxEmptyString
, wxT("invalid font") );
1025 return M_FONTDATA
->GetFaceName();
1028 wxFontEncoding
wxFont::GetEncoding() const
1030 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
1032 return M_FONTDATA
->GetEncoding();
1035 const wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
1037 return M_FONTDATA
->HasNativeFontInfo() ? &(M_FONTDATA
->GetNativeFontInfo())
1041 bool wxFont::IsFixedWidth() const
1043 if ( M_FONTDATA
->HasNativeFontInfo() )
1045 // the two low-order bits specify the pitch of the font, the rest is
1048 (BYTE
)(M_FONTDATA
->GetNativeFontInfo().lf
.lfPitchAndFamily
& PITCH_MASK
);
1050 return pitch
== FIXED_PITCH
;
1053 return wxFontBase::IsFixedWidth();