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"
34 #include "wx/encinfo.h"
37 #include "wx/msw/private.h"
39 #include "wx/fontutil.h"
40 #include "wx/fontmap.h"
42 #include "wx/tokenzr.h"
44 #if wxUSE_EXTENDED_RTTI
46 wxBEGIN_ENUM( wxFontFamily
)
47 wxENUM_MEMBER( wxDEFAULT
)
48 wxENUM_MEMBER( wxDECORATIVE
)
49 wxENUM_MEMBER( wxROMAN
)
50 wxENUM_MEMBER( wxSCRIPT
)
51 wxENUM_MEMBER( wxSWISS
)
52 wxENUM_MEMBER( wxMODERN
)
53 wxENUM_MEMBER( wxTELETYPE
)
54 wxEND_ENUM( wxFontFamily
)
56 wxBEGIN_ENUM( wxFontStyle
)
57 wxENUM_MEMBER( wxNORMAL
)
58 wxENUM_MEMBER( wxITALIC
)
59 wxENUM_MEMBER( wxSLANT
)
60 wxEND_ENUM( wxFontStyle
)
62 wxBEGIN_ENUM( wxFontWeight
)
63 wxENUM_MEMBER( wxNORMAL
)
64 wxENUM_MEMBER( wxLIGHT
)
65 wxENUM_MEMBER( wxBOLD
)
66 wxEND_ENUM( wxFontWeight
)
68 IMPLEMENT_DYNAMIC_CLASS_WITH_COPY_XTI(wxFont
, wxGDIObject
,"wx/font.h")
70 wxBEGIN_PROPERTIES_TABLE(wxFont
)
71 wxPROPERTY( Size
,int, SetPointSize
, GetPointSize
, 12 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
72 wxPROPERTY( Family
, int , SetFamily
, GetFamily
, (int)wxDEFAULT
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // wxFontFamily
73 wxPROPERTY( Style
, int , SetStyle
, GetStyle
, (int)wxNORMAL
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // wxFontStyle
74 wxPROPERTY( Weight
, int , SetWeight
, GetWeight
, (int)wxNORMAL
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // wxFontWeight
75 wxPROPERTY( Underlined
, bool , SetUnderlined
, GetUnderlined
, false , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
76 wxPROPERTY( Face
, wxString
, SetFaceName
, GetFaceName
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
77 wxPROPERTY( Encoding
, wxFontEncoding
, SetEncoding
, GetEncoding
, wxFONTENCODING_DEFAULT
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
78 wxEND_PROPERTIES_TABLE()
80 wxCONSTRUCTOR_6( wxFont
, int , Size
, int , Family
, int , Style
, int , Weight
, bool , Underlined
, wxString
, Face
)
82 wxBEGIN_HANDLERS_TABLE(wxFont
)
83 wxEND_HANDLERS_TABLE()
86 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
90 // ----------------------------------------------------------------------------
92 // ----------------------------------------------------------------------------
94 // the mask used to extract the pitch from LOGFONT::lfPitchAndFamily field
95 static const int PITCH_MASK
= FIXED_PITCH
| VARIABLE_PITCH
;
97 // ----------------------------------------------------------------------------
98 // wxFontRefData - the internal description of the font
99 // ----------------------------------------------------------------------------
101 class WXDLLEXPORT wxFontRefData
: public wxGDIRefData
107 Init(-1, wxSize(0,0), false, wxFONTFAMILY_DEFAULT
, wxFONTSTYLE_NORMAL
,
108 wxFONTWEIGHT_NORMAL
, false, wxEmptyString
,
109 wxFONTENCODING_DEFAULT
);
112 wxFontRefData(int size
,
113 const wxSize
& pixelSize
,
114 bool sizeUsingPixels
,
119 const wxString
& faceName
,
120 wxFontEncoding encoding
)
122 Init(size
, pixelSize
, sizeUsingPixels
, family
, style
, weight
,
123 underlined
, faceName
, encoding
);
126 wxFontRefData(const wxNativeFontInfo
& info
, WXHFONT hFont
= 0)
131 wxFontRefData(const wxFontRefData
& data
) : wxGDIRefData()
133 if ( data
.m_nativeFontInfoOk
)
135 Init(data
.m_nativeFontInfo
);
139 Init(data
.m_pointSize
, data
.m_pixelSize
, data
.m_sizeUsingPixels
,
140 data
.m_family
, data
.m_style
, data
.m_weight
,
141 data
.m_underlined
, data
.m_faceName
, data
.m_encoding
);
145 virtual ~wxFontRefData();
148 bool Alloc(wxFont
*font
);
152 // all wxFont accessors
153 int GetPointSize() const
155 return m_nativeFontInfoOk
? m_nativeFontInfo
.GetPointSize()
159 wxSize
GetPixelSize() const
161 return m_nativeFontInfoOk
? m_nativeFontInfo
.GetPixelSize()
165 bool IsUsingSizeInPixels() const
167 return m_nativeFontInfoOk
? true : m_sizeUsingPixels
;
170 int GetFamily() const
177 return m_nativeFontInfoOk
? m_nativeFontInfo
.GetStyle()
181 int GetWeight() const
183 return m_nativeFontInfoOk
? m_nativeFontInfo
.GetWeight()
187 bool GetUnderlined() const
189 return m_nativeFontInfoOk
? m_nativeFontInfo
.GetUnderlined()
193 wxString
GetFaceName() const
196 if ( m_nativeFontInfoOk
)
197 s
= m_nativeFontInfo
.GetFaceName();
204 wxFontEncoding
GetEncoding() const
206 return m_nativeFontInfoOk
? m_nativeFontInfo
.GetEncoding()
210 WXHFONT
GetHFONT() const { return m_hFont
; }
213 void SetPointSize(int pointSize
)
215 if ( m_nativeFontInfoOk
)
217 m_nativeFontInfo
.SetPointSize(pointSize
);
221 m_pointSize
= pointSize
;
222 m_sizeUsingPixels
= false;
226 void SetPixelSize(const wxSize
& pixelSize
)
228 if ( m_nativeFontInfoOk
)
230 m_nativeFontInfo
.SetPixelSize(pixelSize
);
234 m_pixelSize
= pixelSize
;
235 m_sizeUsingPixels
= true;
239 void SetFamily(int family
)
244 void SetStyle(int style
)
246 if ( m_nativeFontInfoOk
)
247 m_nativeFontInfo
.SetStyle((wxFontStyle
)style
);
252 void SetWeight(int weight
)
254 if ( m_nativeFontInfoOk
)
255 m_nativeFontInfo
.SetWeight((wxFontWeight
)weight
);
260 void SetFaceName(const wxString
& faceName
)
262 if ( m_nativeFontInfoOk
)
263 m_nativeFontInfo
.SetFaceName(faceName
);
265 m_faceName
= faceName
;
268 void SetUnderlined(bool underlined
)
270 if ( m_nativeFontInfoOk
)
271 m_nativeFontInfo
.SetUnderlined(underlined
);
273 m_underlined
= underlined
;
276 void SetEncoding(wxFontEncoding encoding
)
278 if ( m_nativeFontInfoOk
)
279 m_nativeFontInfo
.SetEncoding(encoding
);
281 m_encoding
= encoding
;
284 // native font info tests
285 bool HasNativeFontInfo() const { return m_nativeFontInfoOk
; }
287 const wxNativeFontInfo
& GetNativeFontInfo() const
288 { return m_nativeFontInfo
; }
291 // common part of all ctors
293 const wxSize
& pixelSize
,
294 bool sizeUsingPixels
,
299 const wxString
& faceName
,
300 wxFontEncoding encoding
);
302 void Init(const wxNativeFontInfo
& info
, WXHFONT hFont
= 0);
304 // font characterstics
307 bool m_sizeUsingPixels
;
313 wxFontEncoding m_encoding
;
315 // Windows font handle
319 wxNativeFontInfo m_nativeFontInfo
;
320 bool m_nativeFontInfoOk
;
323 // ============================================================================
325 // ============================================================================
327 // ----------------------------------------------------------------------------
329 // ----------------------------------------------------------------------------
331 void wxFontRefData::Init(int pointSize
,
332 const wxSize
& pixelSize
,
333 bool sizeUsingPixels
,
338 const wxString
& faceName
,
339 wxFontEncoding encoding
)
342 m_pointSize
= pointSize
== -1 ? wxNORMAL_FONT
->GetPointSize() : pointSize
;
343 m_pixelSize
= pixelSize
;
344 m_sizeUsingPixels
= sizeUsingPixels
;
348 m_underlined
= underlined
;
349 m_faceName
= faceName
;
350 m_encoding
= encoding
;
354 m_nativeFontInfoOk
= false;
357 void wxFontRefData::Init(const wxNativeFontInfo
& info
, WXHFONT hFont
)
359 // hFont may be zero, or it be passed in case we really want to
360 // use the exact font created in the underlying system
361 // (for example where we can't guarantee conversion from HFONT
362 // to LOGFONT back to HFONT)
365 m_nativeFontInfoOk
= true;
366 m_nativeFontInfo
= info
;
367 // This is the best we can do since we don't have the
368 // correct information at this point.
372 wxFontRefData::~wxFontRefData()
377 bool wxFontRefData::Alloc(wxFont
*font
)
379 if ( !m_nativeFontInfoOk
)
381 wxFillLogFont(&m_nativeFontInfo
.lf
, font
);
382 m_nativeFontInfoOk
= true;
385 HFONT hfont
= ::CreateFontIndirect(&m_nativeFontInfo
.lf
);
388 wxLogLastError(wxT("CreateFont"));
393 m_hFont
= (WXHFONT
)hfont
;
398 void wxFontRefData::Free()
402 if ( !::DeleteObject((HFONT
) m_hFont
) )
404 wxLogLastError(wxT("DeleteObject(font)"));
411 // ----------------------------------------------------------------------------
413 // ----------------------------------------------------------------------------
415 void wxNativeFontInfo::Init()
420 int wxNativeFontInfo::GetPointSize() const
422 // FIXME: using the screen here results in incorrect font size calculation
424 const int ppInch
= ::GetDeviceCaps(ScreenHDC(), LOGPIXELSY
);
426 return (int) (((72.0*(double)abs(lf
.lfHeight
)) / (double) ppInch
) + 0.5);
429 wxSize
wxNativeFontInfo::GetPixelSize() const
432 ret
.SetHeight(lf
.lfHeight
);
433 ret
.SetWidth(lf
.lfWidth
);
437 wxFontStyle
wxNativeFontInfo::GetStyle() const
439 return lf
.lfItalic
? wxFONTSTYLE_ITALIC
: wxFONTSTYLE_NORMAL
;
442 wxFontWeight
wxNativeFontInfo::GetWeight() const
444 if ( lf
.lfWeight
<= 300 )
445 return wxFONTWEIGHT_LIGHT
;
447 if ( lf
.lfWeight
>= 600 )
448 return wxFONTWEIGHT_BOLD
;
450 return wxFONTWEIGHT_NORMAL
;
453 bool wxNativeFontInfo::GetUnderlined() const
455 return lf
.lfUnderline
!= 0;
458 wxString
wxNativeFontInfo::GetFaceName() const
460 return lf
.lfFaceName
;
463 wxFontFamily
wxNativeFontInfo::GetFamily() const
467 // extract family from pitch-and-family
468 switch ( lf
.lfPitchAndFamily
& ~PITCH_MASK
)
471 family
= wxFONTFAMILY_ROMAN
;
475 wxFAIL_MSG( _T("unknown LOGFONT::lfFamily value") );
479 family
= wxFONTFAMILY_SWISS
;
483 family
= wxFONTFAMILY_SCRIPT
;
487 family
= wxFONTFAMILY_MODERN
;
491 family
= wxFONTFAMILY_DECORATIVE
;
498 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
500 return wxGetFontEncFromCharSet(lf
.lfCharSet
);
503 void wxNativeFontInfo::SetPointSize(int pointsize
)
505 // FIXME: using the screen here results in incorrect font size calculation
507 const int ppInch
= ::GetDeviceCaps(ScreenHDC(), LOGPIXELSY
);
509 lf
.lfHeight
= -(int)((pointsize
*((double)ppInch
)/72.0) + 0.5);
512 void wxNativeFontInfo::SetPixelSize(const wxSize
& pixelSize
)
514 lf
.lfHeight
= pixelSize
.GetHeight();
515 lf
.lfWidth
= pixelSize
.GetWidth();
519 void wxNativeFontInfo::SetStyle(wxFontStyle style
)
524 wxFAIL_MSG( _T("unknown font style") );
527 case wxFONTSTYLE_NORMAL
:
531 case wxFONTSTYLE_ITALIC
:
532 case wxFONTSTYLE_SLANT
:
538 void wxNativeFontInfo::SetWeight(wxFontWeight weight
)
543 wxFAIL_MSG( _T("unknown font weight") );
546 case wxFONTWEIGHT_NORMAL
:
547 lf
.lfWeight
= FW_NORMAL
;
550 case wxFONTWEIGHT_LIGHT
:
551 lf
.lfWeight
= FW_LIGHT
;
554 case wxFONTWEIGHT_BOLD
:
555 lf
.lfWeight
= FW_BOLD
;
560 void wxNativeFontInfo::SetUnderlined(bool underlined
)
562 lf
.lfUnderline
= underlined
;
565 void wxNativeFontInfo::SetFaceName(const wxString
& facename
)
567 wxStrncpy(lf
.lfFaceName
, facename
, WXSIZEOF(lf
.lfFaceName
));
570 void wxNativeFontInfo::SetFamily(wxFontFamily family
)
578 ff_family
= FF_SCRIPT
;
579 facename
= _T("Script");
583 ff_family
= FF_DECORATIVE
;
584 facename
= _T("Old English Text MT");
588 ff_family
= FF_ROMAN
;
589 facename
= _T("Times New Roman");
594 ff_family
= FF_MODERN
;
595 facename
= _T("Courier New");
599 ff_family
= FF_SWISS
;
600 facename
= _T("Arial");
606 // We want Windows 2000 or later to have new fonts even MS Shell Dlg
607 // is returned as default GUI font for compatibility
609 ff_family
= FF_SWISS
;
610 if(wxGetOsVersion(&verMaj
) == wxWINDOWS_NT
&& verMaj
>= 5)
611 facename
= _T("MS Shell Dlg 2");
613 facename
= _T("MS Shell Dlg");
617 lf
.lfPitchAndFamily
= (BYTE
)(DEFAULT_PITCH
) | ff_family
;
619 if ( !wxStrlen(lf
.lfFaceName
) )
621 SetFaceName(facename
);
625 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding
)
627 wxNativeEncodingInfo info
;
628 if ( !wxGetNativeFontEncoding(encoding
, &info
) )
631 if ( wxFontMapper::Get()->GetAltForEncoding(encoding
, &info
) )
633 if ( !info
.facename
.empty() )
635 // if we have this encoding only in some particular facename, use
636 // the facename - it is better to show the correct characters in a
637 // wrong facename than unreadable text in a correct one
638 SetFaceName(info
.facename
);
642 #endif // wxUSE_FONTMAP
644 // unsupported encoding, replace with the default
645 info
.charset
= DEFAULT_CHARSET
;
649 lf
.lfCharSet
= (BYTE
)info
.charset
;
652 bool wxNativeFontInfo::FromString(const wxString
& s
)
656 wxStringTokenizer
tokenizer(s
, _T(";"));
659 wxString token
= tokenizer
.GetNextToken();
660 if ( token
!= _T('0') )
663 token
= tokenizer
.GetNextToken();
664 if ( !token
.ToLong(&l
) )
668 token
= tokenizer
.GetNextToken();
669 if ( !token
.ToLong(&l
) )
673 token
= tokenizer
.GetNextToken();
674 if ( !token
.ToLong(&l
) )
678 token
= tokenizer
.GetNextToken();
679 if ( !token
.ToLong(&l
) )
681 lf
.lfOrientation
= l
;
683 token
= tokenizer
.GetNextToken();
684 if ( !token
.ToLong(&l
) )
688 token
= tokenizer
.GetNextToken();
689 if ( !token
.ToLong(&l
) )
691 lf
.lfItalic
= (BYTE
)l
;
693 token
= tokenizer
.GetNextToken();
694 if ( !token
.ToLong(&l
) )
696 lf
.lfUnderline
= (BYTE
)l
;
698 token
= tokenizer
.GetNextToken();
699 if ( !token
.ToLong(&l
) )
701 lf
.lfStrikeOut
= (BYTE
)l
;
703 token
= tokenizer
.GetNextToken();
704 if ( !token
.ToLong(&l
) )
706 lf
.lfCharSet
= (BYTE
)l
;
708 token
= tokenizer
.GetNextToken();
709 if ( !token
.ToLong(&l
) )
711 lf
.lfOutPrecision
= (BYTE
)l
;
713 token
= tokenizer
.GetNextToken();
714 if ( !token
.ToLong(&l
) )
716 lf
.lfClipPrecision
= (BYTE
)l
;
718 token
= tokenizer
.GetNextToken();
719 if ( !token
.ToLong(&l
) )
721 lf
.lfQuality
= (BYTE
)l
;
723 token
= tokenizer
.GetNextToken();
724 if ( !token
.ToLong(&l
) )
726 lf
.lfPitchAndFamily
= (BYTE
)l
;
728 token
= tokenizer
.GetNextToken();
731 wxStrcpy(lf
.lfFaceName
, token
.c_str());
736 wxString
wxNativeFontInfo::ToString() const
740 s
.Printf(_T("%d;%ld;%ld;%ld;%ld;%ld;%d;%d;%d;%d;%d;%d;%d;%d;%s"),
741 0, // version, in case we want to change the format later
760 // ----------------------------------------------------------------------------
762 // ----------------------------------------------------------------------------
768 bool wxFont::Create(const wxNativeFontInfo
& info
, WXHFONT hFont
)
772 m_refData
= new wxFontRefData(info
, hFont
);
779 wxFont::wxFont(const wxString
& fontdesc
)
781 wxNativeFontInfo info
;
782 if ( info
.FromString(fontdesc
) )
786 /* Constructor for a font. Note that the real construction is done
787 * in wxDC::SetFont, when information is available about scaling etc.
789 bool wxFont::DoCreate(int pointSize
,
790 const wxSize
& pixelSize
,
791 bool sizeUsingPixels
,
796 const wxString
& faceName
,
797 wxFontEncoding encoding
)
801 // wxDEFAULT is a valid value for the font size too so we must treat it
802 // specially here (otherwise the size would be 70 == wxDEFAULT value)
803 if ( pointSize
== wxDEFAULT
)
805 pointSize
= wxNORMAL_FONT
->GetPointSize();
808 m_refData
= new wxFontRefData(pointSize
, pixelSize
, sizeUsingPixels
,
809 family
, style
, weight
,
810 underlined
, faceName
, encoding
);
821 // ----------------------------------------------------------------------------
822 // real implementation
823 // ----------------------------------------------------------------------------
825 bool wxFont::RealizeResource()
827 if ( GetResourceHandle() )
829 // VZ: the old code returned false in this case, but it doesn't seem
830 // to make sense because the font _was_ created
834 return M_FONTDATA
->Alloc(this);
837 bool wxFont::FreeResource(bool WXUNUSED(force
))
839 if ( GetResourceHandle() )
849 WXHANDLE
wxFont::GetResourceHandle() const
851 return (WXHANDLE
)GetHFONT();
854 WXHFONT
wxFont::GetHFONT() const
856 return M_FONTDATA
? M_FONTDATA
->GetHFONT() : 0;
859 bool wxFont::IsFree() const
861 return M_FONTDATA
&& (M_FONTDATA
->GetHFONT() == 0);
864 void wxFont::Unshare()
866 // Don't change shared data
869 m_refData
= new wxFontRefData();
873 wxFontRefData
* ref
= new wxFontRefData(*M_FONTDATA
);
879 // ----------------------------------------------------------------------------
880 // change font attribute: we recreate font when doing it
881 // ----------------------------------------------------------------------------
883 void wxFont::SetPointSize(int pointSize
)
887 M_FONTDATA
->SetPointSize(pointSize
);
892 void wxFont::SetPixelSize(const wxSize
& pixelSize
)
896 M_FONTDATA
->SetPixelSize(pixelSize
);
901 void wxFont::SetFamily(int family
)
905 M_FONTDATA
->SetFamily(family
);
910 void wxFont::SetStyle(int style
)
914 M_FONTDATA
->SetStyle(style
);
919 void wxFont::SetWeight(int weight
)
923 M_FONTDATA
->SetWeight(weight
);
928 void wxFont::SetFaceName(const wxString
& faceName
)
932 M_FONTDATA
->SetFaceName(faceName
);
937 void wxFont::SetUnderlined(bool underlined
)
941 M_FONTDATA
->SetUnderlined(underlined
);
946 void wxFont::SetEncoding(wxFontEncoding encoding
)
950 M_FONTDATA
->SetEncoding(encoding
);
955 void wxFont::DoSetNativeFontInfo(const wxNativeFontInfo
& info
)
961 *M_FONTDATA
= wxFontRefData(info
);
966 // ----------------------------------------------------------------------------
968 // ----------------------------------------------------------------------------
970 int wxFont::GetPointSize() const
972 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
974 return M_FONTDATA
->GetPointSize();
977 wxSize
wxFont::GetPixelSize() const
979 return M_FONTDATA
->GetPixelSize();
982 bool wxFont::IsUsingSizeInPixels() const
984 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
986 return M_FONTDATA
->IsUsingSizeInPixels();
989 int wxFont::GetFamily() const
991 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
993 return M_FONTDATA
->GetFamily();
996 int wxFont::GetStyle() const
998 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
1000 return M_FONTDATA
->GetStyle();
1003 int wxFont::GetWeight() const
1005 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
1007 return M_FONTDATA
->GetWeight();
1010 bool wxFont::GetUnderlined() const
1012 wxCHECK_MSG( Ok(), false, wxT("invalid font") );
1014 return M_FONTDATA
->GetUnderlined();
1017 wxString
wxFont::GetFaceName() const
1019 wxCHECK_MSG( Ok(), wxEmptyString
, wxT("invalid font") );
1021 return M_FONTDATA
->GetFaceName();
1024 wxFontEncoding
wxFont::GetEncoding() const
1026 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
1028 return M_FONTDATA
->GetEncoding();
1031 const wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
1033 return M_FONTDATA
->HasNativeFontInfo() ? &(M_FONTDATA
->GetNativeFontInfo())
1037 bool wxFont::IsFixedWidth() const
1039 if ( M_FONTDATA
->HasNativeFontInfo() )
1041 // the two low-order bits specify the pitch of the font, the rest is
1044 (BYTE
)(M_FONTDATA
->GetNativeFontInfo().lf
.lfPitchAndFamily
& PITCH_MASK
);
1046 return pitch
== FIXED_PITCH
;
1049 return wxFontBase::IsFixedWidth();