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 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
33 #include "wx/encinfo.h"
36 #include "wx/msw/private.h"
38 #include "wx/fontutil.h"
39 #include "wx/fontmap.h"
41 #include "wx/tokenzr.h"
43 #if wxUSE_EXTENDED_RTTI
45 wxBEGIN_ENUM( wxFontFamily
)
46 wxENUM_MEMBER( wxDEFAULT
)
47 wxENUM_MEMBER( wxDECORATIVE
)
48 wxENUM_MEMBER( wxROMAN
)
49 wxENUM_MEMBER( wxSCRIPT
)
50 wxENUM_MEMBER( wxSWISS
)
51 wxENUM_MEMBER( wxMODERN
)
52 wxENUM_MEMBER( wxTELETYPE
)
53 wxEND_ENUM( wxFontFamily
)
55 wxBEGIN_ENUM( wxFontStyle
)
56 wxENUM_MEMBER( wxNORMAL
)
57 wxENUM_MEMBER( wxITALIC
)
58 wxENUM_MEMBER( wxSLANT
)
59 wxEND_ENUM( wxFontStyle
)
61 wxBEGIN_ENUM( wxFontWeight
)
62 wxENUM_MEMBER( wxNORMAL
)
63 wxENUM_MEMBER( wxLIGHT
)
64 wxENUM_MEMBER( wxBOLD
)
65 wxEND_ENUM( wxFontWeight
)
67 IMPLEMENT_DYNAMIC_CLASS_WITH_COPY_XTI(wxFont
, wxGDIObject
,"wx/font.h")
69 wxBEGIN_PROPERTIES_TABLE(wxFont
)
70 wxPROPERTY( Size
,int, SetPointSize
, GetPointSize
, 12 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
71 wxPROPERTY( Family
, int , SetFamily
, GetFamily
, (int)wxDEFAULT
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // wxFontFamily
72 wxPROPERTY( Style
, int , SetStyle
, GetStyle
, (int)wxNORMAL
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // wxFontStyle
73 wxPROPERTY( Weight
, int , SetWeight
, GetWeight
, (int)wxNORMAL
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // wxFontWeight
74 wxPROPERTY( Underlined
, bool , SetUnderlined
, GetUnderlined
, false , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
75 wxPROPERTY( Face
, wxString
, SetFaceName
, GetFaceName
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
76 wxPROPERTY( Encoding
, wxFontEncoding
, SetEncoding
, GetEncoding
, wxFONTENCODING_DEFAULT
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
77 wxEND_PROPERTIES_TABLE()
79 wxCONSTRUCTOR_6( wxFont
, int , Size
, int , Family
, int , Style
, int , Weight
, bool , Underlined
, wxString
, Face
)
81 wxBEGIN_HANDLERS_TABLE(wxFont
)
82 wxEND_HANDLERS_TABLE()
85 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
89 // ----------------------------------------------------------------------------
91 // ----------------------------------------------------------------------------
93 // the mask used to extract the pitch from LOGFONT::lfPitchAndFamily field
94 static const int PITCH_MASK
= FIXED_PITCH
| VARIABLE_PITCH
;
96 // ----------------------------------------------------------------------------
97 // wxFontRefData - the internal description of the font
98 // ----------------------------------------------------------------------------
100 class WXDLLEXPORT wxFontRefData
: public wxGDIRefData
106 Init(-1, wxSize(0,0), false, wxFONTFAMILY_DEFAULT
, wxFONTSTYLE_NORMAL
,
107 wxFONTWEIGHT_NORMAL
, false, wxEmptyString
,
108 wxFONTENCODING_DEFAULT
);
111 wxFontRefData(int size
,
112 const wxSize
& pixelSize
,
113 bool sizeUsingPixels
,
118 const wxString
& faceName
,
119 wxFontEncoding encoding
)
121 Init(size
, pixelSize
, sizeUsingPixels
, family
, style
, weight
,
122 underlined
, faceName
, encoding
);
125 wxFontRefData(const wxNativeFontInfo
& info
, WXHFONT hFont
= 0)
130 wxFontRefData(const wxFontRefData
& data
) : wxGDIRefData()
132 if ( data
.m_nativeFontInfoOk
)
134 Init(data
.m_nativeFontInfo
);
138 Init(data
.m_pointSize
, data
.m_pixelSize
, data
.m_sizeUsingPixels
,
139 data
.m_family
, data
.m_style
, data
.m_weight
,
140 data
.m_underlined
, data
.m_faceName
, data
.m_encoding
);
144 virtual ~wxFontRefData();
147 bool Alloc(wxFont
*font
);
151 // all wxFont accessors
152 int GetPointSize() const
154 return m_nativeFontInfoOk
? m_nativeFontInfo
.GetPointSize()
158 wxSize
GetPixelSize() const
160 return m_nativeFontInfoOk
? m_nativeFontInfo
.GetPixelSize()
164 bool IsUsingSizeInPixels() const
166 return m_nativeFontInfoOk
? true : m_sizeUsingPixels
;
169 int GetFamily() const
176 return m_nativeFontInfoOk
? m_nativeFontInfo
.GetStyle()
180 int GetWeight() const
182 return m_nativeFontInfoOk
? m_nativeFontInfo
.GetWeight()
186 bool GetUnderlined() const
188 return m_nativeFontInfoOk
? m_nativeFontInfo
.GetUnderlined()
192 wxString
GetFaceName() const
195 if ( m_nativeFontInfoOk
)
196 s
= m_nativeFontInfo
.GetFaceName();
203 wxFontEncoding
GetEncoding() const
205 return m_nativeFontInfoOk
? m_nativeFontInfo
.GetEncoding()
209 WXHFONT
GetHFONT() const { return m_hFont
; }
212 void SetPointSize(int pointSize
)
214 if ( m_nativeFontInfoOk
)
216 m_nativeFontInfo
.SetPointSize(pointSize
);
220 m_pointSize
= pointSize
;
221 m_sizeUsingPixels
= false;
225 void SetPixelSize(const wxSize
& pixelSize
)
227 if ( m_nativeFontInfoOk
)
229 m_nativeFontInfo
.SetPixelSize(pixelSize
);
233 m_pixelSize
= pixelSize
;
234 m_sizeUsingPixels
= true;
238 void SetFamily(int family
)
243 void SetStyle(int style
)
245 if ( m_nativeFontInfoOk
)
246 m_nativeFontInfo
.SetStyle((wxFontStyle
)style
);
251 void SetWeight(int weight
)
253 if ( m_nativeFontInfoOk
)
254 m_nativeFontInfo
.SetWeight((wxFontWeight
)weight
);
259 void SetFaceName(const wxString
& faceName
)
261 if ( m_nativeFontInfoOk
)
262 m_nativeFontInfo
.SetFaceName(faceName
);
264 m_faceName
= faceName
;
267 void SetUnderlined(bool underlined
)
269 if ( m_nativeFontInfoOk
)
270 m_nativeFontInfo
.SetUnderlined(underlined
);
272 m_underlined
= underlined
;
275 void SetEncoding(wxFontEncoding encoding
)
277 if ( m_nativeFontInfoOk
)
278 m_nativeFontInfo
.SetEncoding(encoding
);
280 m_encoding
= encoding
;
283 // native font info tests
284 bool HasNativeFontInfo() const { return m_nativeFontInfoOk
; }
286 const wxNativeFontInfo
& GetNativeFontInfo() const
287 { return m_nativeFontInfo
; }
290 // common part of all ctors
292 const wxSize
& pixelSize
,
293 bool sizeUsingPixels
,
298 const wxString
& faceName
,
299 wxFontEncoding encoding
);
301 void Init(const wxNativeFontInfo
& info
, WXHFONT hFont
= 0);
303 // font characterstics
306 bool m_sizeUsingPixels
;
312 wxFontEncoding m_encoding
;
314 // Windows font handle
318 wxNativeFontInfo m_nativeFontInfo
;
319 bool m_nativeFontInfoOk
;
322 // ============================================================================
324 // ============================================================================
326 // ----------------------------------------------------------------------------
328 // ----------------------------------------------------------------------------
330 void wxFontRefData::Init(int pointSize
,
331 const wxSize
& pixelSize
,
332 bool sizeUsingPixels
,
337 const wxString
& faceName
,
338 wxFontEncoding encoding
)
341 m_pointSize
= pointSize
== -1 ? wxNORMAL_FONT
->GetPointSize() : pointSize
;
342 m_pixelSize
= pixelSize
;
343 m_sizeUsingPixels
= sizeUsingPixels
;
347 m_underlined
= underlined
;
348 m_faceName
= faceName
;
349 m_encoding
= encoding
;
353 m_nativeFontInfoOk
= false;
356 void wxFontRefData::Init(const wxNativeFontInfo
& info
, WXHFONT hFont
)
358 // hFont may be zero, or it be passed in case we really want to
359 // use the exact font created in the underlying system
360 // (for example where we can't guarantee conversion from HFONT
361 // to LOGFONT back to HFONT)
364 m_nativeFontInfoOk
= true;
365 m_nativeFontInfo
= info
;
366 // This is the best we can do since we don't have the
367 // correct information at this point.
371 wxFontRefData::~wxFontRefData()
376 bool wxFontRefData::Alloc(wxFont
*font
)
378 if ( !m_nativeFontInfoOk
)
380 wxFillLogFont(&m_nativeFontInfo
.lf
, font
);
381 m_nativeFontInfoOk
= true;
384 HFONT hfont
= ::CreateFontIndirect(&m_nativeFontInfo
.lf
);
387 wxLogLastError(wxT("CreateFont"));
392 m_hFont
= (WXHFONT
)hfont
;
397 void wxFontRefData::Free()
401 if ( !::DeleteObject((HFONT
) m_hFont
) )
403 wxLogLastError(wxT("DeleteObject(font)"));
410 // ----------------------------------------------------------------------------
412 // ----------------------------------------------------------------------------
414 void wxNativeFontInfo::Init()
419 int wxNativeFontInfo::GetPointSize() const
421 // FIXME: using the screen here results in incorrect font size calculation
423 const int ppInch
= ::GetDeviceCaps(ScreenHDC(), LOGPIXELSY
);
425 return (int) (((72.0*(double)abs(lf
.lfHeight
)) / (double) ppInch
) + 0.5);
428 wxSize
wxNativeFontInfo::GetPixelSize() const
431 ret
.SetHeight(lf
.lfHeight
);
432 ret
.SetWidth(lf
.lfWidth
);
436 wxFontStyle
wxNativeFontInfo::GetStyle() const
438 return lf
.lfItalic
? wxFONTSTYLE_ITALIC
: wxFONTSTYLE_NORMAL
;
441 wxFontWeight
wxNativeFontInfo::GetWeight() const
443 if ( lf
.lfWeight
<= 300 )
444 return wxFONTWEIGHT_LIGHT
;
446 if ( lf
.lfWeight
>= 600 )
447 return wxFONTWEIGHT_BOLD
;
449 return wxFONTWEIGHT_NORMAL
;
452 bool wxNativeFontInfo::GetUnderlined() const
454 return lf
.lfUnderline
!= 0;
457 wxString
wxNativeFontInfo::GetFaceName() const
459 return lf
.lfFaceName
;
462 wxFontFamily
wxNativeFontInfo::GetFamily() const
466 // extract family from pitch-and-family
467 switch ( lf
.lfPitchAndFamily
& ~PITCH_MASK
)
470 family
= wxFONTFAMILY_ROMAN
;
474 wxFAIL_MSG( _T("unknown LOGFONT::lfFamily value") );
478 family
= wxFONTFAMILY_SWISS
;
482 family
= wxFONTFAMILY_SCRIPT
;
486 family
= wxFONTFAMILY_MODERN
;
490 family
= wxFONTFAMILY_DECORATIVE
;
497 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
499 return wxGetFontEncFromCharSet(lf
.lfCharSet
);
502 void wxNativeFontInfo::SetPointSize(int pointsize
)
504 // FIXME: using the screen here results in incorrect font size calculation
506 const int ppInch
= ::GetDeviceCaps(ScreenHDC(), LOGPIXELSY
);
508 lf
.lfHeight
= -(int)((pointsize
*((double)ppInch
)/72.0) + 0.5);
511 void wxNativeFontInfo::SetPixelSize(const wxSize
& pixelSize
)
513 lf
.lfHeight
= pixelSize
.GetHeight();
514 lf
.lfWidth
= pixelSize
.GetWidth();
518 void wxNativeFontInfo::SetStyle(wxFontStyle style
)
523 wxFAIL_MSG( _T("unknown font style") );
526 case wxFONTSTYLE_NORMAL
:
530 case wxFONTSTYLE_ITALIC
:
531 case wxFONTSTYLE_SLANT
:
537 void wxNativeFontInfo::SetWeight(wxFontWeight weight
)
542 wxFAIL_MSG( _T("unknown font weight") );
545 case wxFONTWEIGHT_NORMAL
:
546 lf
.lfWeight
= FW_NORMAL
;
549 case wxFONTWEIGHT_LIGHT
:
550 lf
.lfWeight
= FW_LIGHT
;
553 case wxFONTWEIGHT_BOLD
:
554 lf
.lfWeight
= FW_BOLD
;
559 void wxNativeFontInfo::SetUnderlined(bool underlined
)
561 lf
.lfUnderline
= underlined
;
564 void wxNativeFontInfo::SetFaceName(const wxString
& facename
)
566 wxStrncpy(lf
.lfFaceName
, facename
, WXSIZEOF(lf
.lfFaceName
));
569 void wxNativeFontInfo::SetFamily(wxFontFamily family
)
577 ff_family
= FF_SCRIPT
;
578 facename
= _T("Script");
582 ff_family
= FF_DECORATIVE
;
583 facename
= _T("Old English Text MT");
587 ff_family
= FF_ROMAN
;
588 facename
= _T("Times New Roman");
593 ff_family
= FF_MODERN
;
594 facename
= _T("Courier New");
598 ff_family
= FF_SWISS
;
599 facename
= _T("Arial");
605 // We want Windows 2000 or later to have new fonts even MS Shell Dlg
606 // is returned as default GUI font for compatibility
608 ff_family
= FF_SWISS
;
609 if(wxGetOsVersion(&verMaj
) == wxWINDOWS_NT
&& verMaj
>= 5)
610 facename
= _T("MS Shell Dlg 2");
612 facename
= _T("MS Shell Dlg");
616 lf
.lfPitchAndFamily
= (BYTE
)(DEFAULT_PITCH
) | ff_family
;
618 if ( !wxStrlen(lf
.lfFaceName
) )
620 SetFaceName(facename
);
624 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding
)
626 wxNativeEncodingInfo info
;
627 if ( !wxGetNativeFontEncoding(encoding
, &info
) )
630 if ( wxFontMapper::Get()->GetAltForEncoding(encoding
, &info
) )
632 if ( !info
.facename
.empty() )
634 // if we have this encoding only in some particular facename, use
635 // the facename - it is better to show the correct characters in a
636 // wrong facename than unreadable text in a correct one
637 SetFaceName(info
.facename
);
641 #endif // wxUSE_FONTMAP
643 // unsupported encoding, replace with the default
644 info
.charset
= DEFAULT_CHARSET
;
648 lf
.lfCharSet
= (BYTE
)info
.charset
;
651 bool wxNativeFontInfo::FromString(const wxString
& s
)
655 wxStringTokenizer
tokenizer(s
, _T(";"));
658 wxString token
= tokenizer
.GetNextToken();
659 if ( token
!= _T('0') )
662 token
= tokenizer
.GetNextToken();
663 if ( !token
.ToLong(&l
) )
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
) )
680 lf
.lfOrientation
= l
;
682 token
= tokenizer
.GetNextToken();
683 if ( !token
.ToLong(&l
) )
687 token
= tokenizer
.GetNextToken();
688 if ( !token
.ToLong(&l
) )
690 lf
.lfItalic
= (BYTE
)l
;
692 token
= tokenizer
.GetNextToken();
693 if ( !token
.ToLong(&l
) )
695 lf
.lfUnderline
= (BYTE
)l
;
697 token
= tokenizer
.GetNextToken();
698 if ( !token
.ToLong(&l
) )
700 lf
.lfStrikeOut
= (BYTE
)l
;
702 token
= tokenizer
.GetNextToken();
703 if ( !token
.ToLong(&l
) )
705 lf
.lfCharSet
= (BYTE
)l
;
707 token
= tokenizer
.GetNextToken();
708 if ( !token
.ToLong(&l
) )
710 lf
.lfOutPrecision
= (BYTE
)l
;
712 token
= tokenizer
.GetNextToken();
713 if ( !token
.ToLong(&l
) )
715 lf
.lfClipPrecision
= (BYTE
)l
;
717 token
= tokenizer
.GetNextToken();
718 if ( !token
.ToLong(&l
) )
720 lf
.lfQuality
= (BYTE
)l
;
722 token
= tokenizer
.GetNextToken();
723 if ( !token
.ToLong(&l
) )
725 lf
.lfPitchAndFamily
= (BYTE
)l
;
727 token
= tokenizer
.GetNextToken();
730 wxStrcpy(lf
.lfFaceName
, token
.c_str());
735 wxString
wxNativeFontInfo::ToString() const
739 s
.Printf(_T("%d;%ld;%ld;%ld;%ld;%ld;%d;%d;%d;%d;%d;%d;%d;%d;%s"),
740 0, // version, in case we want to change the format later
759 // ----------------------------------------------------------------------------
761 // ----------------------------------------------------------------------------
767 bool wxFont::Create(const wxNativeFontInfo
& info
, WXHFONT hFont
)
771 m_refData
= new wxFontRefData(info
, hFont
);
778 wxFont::wxFont(const wxString
& fontdesc
)
780 wxNativeFontInfo info
;
781 if ( info
.FromString(fontdesc
) )
785 /* Constructor for a font. Note that the real construction is done
786 * in wxDC::SetFont, when information is available about scaling etc.
788 bool wxFont::DoCreate(int pointSize
,
789 const wxSize
& pixelSize
,
790 bool sizeUsingPixels
,
795 const wxString
& faceName
,
796 wxFontEncoding encoding
)
800 // wxDEFAULT is a valid value for the font size too so we must treat it
801 // specially here (otherwise the size would be 70 == wxDEFAULT value)
802 if ( pointSize
== wxDEFAULT
)
804 pointSize
= wxNORMAL_FONT
->GetPointSize();
807 m_refData
= new wxFontRefData(pointSize
, pixelSize
, sizeUsingPixels
,
808 family
, style
, weight
,
809 underlined
, faceName
, encoding
);
820 // ----------------------------------------------------------------------------
821 // real implementation
822 // ----------------------------------------------------------------------------
824 bool wxFont::RealizeResource()
826 if ( GetResourceHandle() )
828 // VZ: the old code returned false in this case, but it doesn't seem
829 // to make sense because the font _was_ created
833 return M_FONTDATA
->Alloc(this);
836 bool wxFont::FreeResource(bool WXUNUSED(force
))
838 if ( GetResourceHandle() )
848 WXHANDLE
wxFont::GetResourceHandle() const
850 return (WXHANDLE
)GetHFONT();
853 WXHFONT
wxFont::GetHFONT() const
855 return M_FONTDATA
? M_FONTDATA
->GetHFONT() : 0;
858 bool wxFont::IsFree() const
860 return M_FONTDATA
&& (M_FONTDATA
->GetHFONT() == 0);
863 void wxFont::Unshare()
865 // Don't change shared data
868 m_refData
= new wxFontRefData();
872 wxFontRefData
* ref
= new wxFontRefData(*M_FONTDATA
);
878 // ----------------------------------------------------------------------------
879 // change font attribute: we recreate font when doing it
880 // ----------------------------------------------------------------------------
882 void wxFont::SetPointSize(int pointSize
)
886 M_FONTDATA
->SetPointSize(pointSize
);
891 void wxFont::SetPixelSize(const wxSize
& pixelSize
)
895 M_FONTDATA
->SetPixelSize(pixelSize
);
900 void wxFont::SetFamily(int family
)
904 M_FONTDATA
->SetFamily(family
);
909 void wxFont::SetStyle(int style
)
913 M_FONTDATA
->SetStyle(style
);
918 void wxFont::SetWeight(int weight
)
922 M_FONTDATA
->SetWeight(weight
);
927 void wxFont::SetFaceName(const wxString
& faceName
)
931 M_FONTDATA
->SetFaceName(faceName
);
936 void wxFont::SetUnderlined(bool underlined
)
940 M_FONTDATA
->SetUnderlined(underlined
);
945 void wxFont::SetEncoding(wxFontEncoding encoding
)
949 M_FONTDATA
->SetEncoding(encoding
);
954 void wxFont::DoSetNativeFontInfo(const wxNativeFontInfo
& info
)
960 *M_FONTDATA
= wxFontRefData(info
);
965 // ----------------------------------------------------------------------------
967 // ----------------------------------------------------------------------------
969 int wxFont::GetPointSize() const
971 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
973 return M_FONTDATA
->GetPointSize();
976 wxSize
wxFont::GetPixelSize() const
978 return M_FONTDATA
->GetPixelSize();
981 bool wxFont::IsUsingSizeInPixels() const
983 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
985 return M_FONTDATA
->IsUsingSizeInPixels();
988 int wxFont::GetFamily() const
990 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
992 return M_FONTDATA
->GetFamily();
995 int wxFont::GetStyle() const
997 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
999 return M_FONTDATA
->GetStyle();
1002 int wxFont::GetWeight() const
1004 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
1006 return M_FONTDATA
->GetWeight();
1009 bool wxFont::GetUnderlined() const
1011 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
1013 return M_FONTDATA
->GetUnderlined();
1016 wxString
wxFont::GetFaceName() const
1018 wxCHECK_MSG( Ok(), wxEmptyString
, wxT("invalid font") );
1020 return M_FONTDATA
->GetFaceName();
1023 wxFontEncoding
wxFont::GetEncoding() const
1025 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
1027 return M_FONTDATA
->GetEncoding();
1030 const wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
1032 return M_FONTDATA
->HasNativeFontInfo() ? &(M_FONTDATA
->GetNativeFontInfo())
1036 bool wxFont::IsFixedWidth() const
1038 if ( M_FONTDATA
->HasNativeFontInfo() )
1040 // the two low-order bits specify the pitch of the font, the rest is
1043 (BYTE
)(M_FONTDATA
->GetNativeFontInfo().lf
.lfPitchAndFamily
& PITCH_MASK
);
1045 return pitch
== FIXED_PITCH
;
1048 return wxFontBase::IsFixedWidth();