1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/font.cpp
3 // Purpose: wxFont class
4 // Author: Julian Smart
8 // Copyright: (c) wxWindows team
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "font.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
40 #include "wx/msw/private.h"
42 #include "wx/fontutil.h"
43 #include "wx/fontmap.h"
45 #include "wx/tokenzr.h"
47 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 // the mask used to extract the pitch from LOGFONT::lfPitchAndFamily field
54 static const int PITCH_MASK
= FIXED_PITCH
| VARIABLE_PITCH
;
56 // ----------------------------------------------------------------------------
57 // wxFontRefData - the internal description of the font
58 // ----------------------------------------------------------------------------
60 class WXDLLEXPORT wxFontRefData
: public wxGDIRefData
66 Init(-1, wxFONTFAMILY_DEFAULT
, wxFONTSTYLE_NORMAL
, wxFONTWEIGHT_NORMAL
,
67 FALSE
, _T(""), wxFONTENCODING_DEFAULT
);
70 wxFontRefData(int size
,
75 const wxString
& faceName
,
76 wxFontEncoding encoding
)
78 Init(size
, family
, style
, weight
, underlined
, faceName
, encoding
);
81 wxFontRefData(const wxNativeFontInfo
& info
, WXHFONT hFont
= 0)
86 wxFontRefData(const wxFontRefData
& data
)
88 if ( data
.m_nativeFontInfoOk
)
90 Init(data
.m_nativeFontInfo
);
94 Init(data
.m_pointSize
, data
.m_family
, data
.m_style
, data
.m_weight
,
95 data
.m_underlined
, data
.m_faceName
, data
.m_encoding
);
99 virtual ~wxFontRefData();
102 bool Alloc(wxFont
*font
);
106 // all wxFont accessors
107 int GetPointSize() const
109 return m_nativeFontInfoOk
? m_nativeFontInfo
.GetPointSize()
113 int GetFamily() const
120 return m_nativeFontInfoOk
? m_nativeFontInfo
.GetStyle()
124 int GetWeight() const
126 return m_nativeFontInfoOk
? m_nativeFontInfo
.GetWeight()
130 bool GetUnderlined() const
132 return m_nativeFontInfoOk
? m_nativeFontInfo
.GetUnderlined()
136 wxString
GetFaceName() const
139 if ( m_nativeFontInfoOk
)
140 s
= m_nativeFontInfo
.GetFaceName();
147 wxFontEncoding
GetEncoding() const
149 return m_nativeFontInfoOk
? m_nativeFontInfo
.GetEncoding()
153 WXHFONT
GetHFONT() const { return m_hFont
; }
156 void SetPointSize(int pointSize
)
158 if ( m_nativeFontInfoOk
)
159 m_nativeFontInfo
.SetPointSize(pointSize
);
161 m_pointSize
= pointSize
;
164 void SetFamily(int family
)
169 void SetStyle(int style
)
171 if ( m_nativeFontInfoOk
)
172 m_nativeFontInfo
.SetStyle((wxFontStyle
)style
);
177 void SetWeight(int weight
)
179 if ( m_nativeFontInfoOk
)
180 m_nativeFontInfo
.SetWeight((wxFontWeight
)weight
);
185 void SetFaceName(const wxString
& faceName
)
187 if ( m_nativeFontInfoOk
)
188 m_nativeFontInfo
.SetFaceName(faceName
);
190 m_faceName
= faceName
;
193 void SetUnderlined(bool underlined
)
195 if ( m_nativeFontInfoOk
)
196 m_nativeFontInfo
.SetUnderlined(underlined
);
198 m_underlined
= underlined
;
201 void SetEncoding(wxFontEncoding encoding
)
203 if ( m_nativeFontInfoOk
)
204 m_nativeFontInfo
.SetEncoding(encoding
);
206 m_encoding
= encoding
;
209 // native font info tests
210 bool HasNativeFontInfo() const { return m_nativeFontInfoOk
; }
212 const wxNativeFontInfo
& GetNativeFontInfo() const
213 { return m_nativeFontInfo
; }
216 // common part of all ctors
222 const wxString
& faceName
,
223 wxFontEncoding encoding
);
225 void Init(const wxNativeFontInfo
& info
, WXHFONT hFont
= 0);
227 // font characterstics
234 wxFontEncoding m_encoding
;
236 // Windows font handle
240 wxNativeFontInfo m_nativeFontInfo
;
241 bool m_nativeFontInfoOk
;
244 // ============================================================================
246 // ============================================================================
248 // ----------------------------------------------------------------------------
250 // ----------------------------------------------------------------------------
252 void wxFontRefData::Init(int pointSize
,
257 const wxString
& faceName
,
258 wxFontEncoding encoding
)
261 m_pointSize
= pointSize
== -1 ? wxNORMAL_FONT
->GetPointSize() : pointSize
;
265 m_underlined
= underlined
;
266 m_faceName
= faceName
;
267 m_encoding
= encoding
;
271 m_nativeFontInfoOk
= FALSE
;
274 void wxFontRefData::Init(const wxNativeFontInfo
& info
, WXHFONT hFont
)
276 // hFont may be zero, or it be passed in case we really want to
277 // use the exact font created in the underlying system
278 // (for example where we can't guarantee conversion from HFONT
279 // to LOGFONT back to HFONT)
282 m_nativeFontInfoOk
= TRUE
;
283 m_nativeFontInfo
= info
;
286 wxFontRefData::~wxFontRefData()
291 bool wxFontRefData::Alloc(wxFont
*font
)
293 if ( !m_nativeFontInfoOk
)
295 wxFillLogFont(&m_nativeFontInfo
.lf
, font
);
296 m_nativeFontInfoOk
= TRUE
;
299 HFONT hfont
= ::CreateFontIndirect(&m_nativeFontInfo
.lf
);
302 wxLogLastError(wxT("CreateFont"));
307 m_hFont
= (WXHFONT
)hfont
;
312 void wxFontRefData::Free()
316 if ( !::DeleteObject((HFONT
) m_hFont
) )
318 wxLogLastError(wxT("DeleteObject(font)"));
325 // ----------------------------------------------------------------------------
327 // ----------------------------------------------------------------------------
329 void wxNativeFontInfo::Init()
334 int wxNativeFontInfo::GetPointSize() const
336 // FIXME: using the screen here results in incorrect font size calculation
338 const int ppInch
= ::GetDeviceCaps(ScreenHDC(), LOGPIXELSY
);
340 return (int) (((72.0*(double)abs(lf
.lfHeight
)) / (double) ppInch
) + 0.5);
343 wxFontStyle
wxNativeFontInfo::GetStyle() const
345 return lf
.lfItalic
? wxFONTSTYLE_ITALIC
: wxFONTSTYLE_NORMAL
;
348 wxFontWeight
wxNativeFontInfo::GetWeight() const
350 if ( lf
.lfWeight
<= 300 )
351 return wxFONTWEIGHT_LIGHT
;
353 if ( lf
.lfWeight
>= 600 )
354 return wxFONTWEIGHT_BOLD
;
356 return wxFONTWEIGHT_NORMAL
;
359 bool wxNativeFontInfo::GetUnderlined() const
361 return lf
.lfUnderline
!= 0;
364 wxString
wxNativeFontInfo::GetFaceName() const
366 return lf
.lfFaceName
;
369 wxFontFamily
wxNativeFontInfo::GetFamily() const
373 // extract family from pitch-and-family
374 switch ( lf
.lfPitchAndFamily
& ~PITCH_MASK
)
377 family
= wxFONTFAMILY_ROMAN
;
381 wxFAIL_MSG( _T("unknown LOGFONT::lfFamily value") );
385 family
= wxFONTFAMILY_SWISS
;
389 family
= wxFONTFAMILY_SCRIPT
;
393 family
= wxFONTFAMILY_MODERN
;
397 family
= wxFONTFAMILY_DECORATIVE
;
404 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
406 return wxGetFontEncFromCharSet(lf
.lfCharSet
);
409 void wxNativeFontInfo::SetPointSize(int pointsize
)
411 #if wxFONT_SIZE_COMPATIBILITY
412 // Incorrect, but compatible with old wxWindows behaviour
413 lf
.lfHeight
= (pointSize
*ppInch
)/72;
414 #else // wxFONT_SIZE_COMPATIBILITY
415 // FIXME: using the screen here results in incorrect font size calculation
417 const int ppInch
= ::GetDeviceCaps(ScreenHDC(), LOGPIXELSY
);
419 lf
.lfHeight
= -(int)((pointsize
*((double)ppInch
)/72.0) + 0.5);
420 #endif // wxFONT_SIZE_COMPATIBILITY/!wxFONT_SIZE_COMPATIBILITY
423 void wxNativeFontInfo::SetStyle(wxFontStyle style
)
428 wxFAIL_MSG( _T("unknown font style") );
431 case wxFONTSTYLE_NORMAL
:
434 case wxFONTSTYLE_ITALIC
:
435 case wxFONTSTYLE_SLANT
:
441 void wxNativeFontInfo::SetWeight(wxFontWeight weight
)
446 wxFAIL_MSG( _T("unknown font weight") );
449 case wxFONTWEIGHT_NORMAL
:
450 lf
.lfWeight
= FW_NORMAL
;
453 case wxFONTWEIGHT_LIGHT
:
454 lf
.lfWeight
= FW_LIGHT
;
457 case wxFONTWEIGHT_BOLD
:
458 lf
.lfWeight
= FW_BOLD
;
463 void wxNativeFontInfo::SetUnderlined(bool underlined
)
465 lf
.lfUnderline
= underlined
;
468 void wxNativeFontInfo::SetFaceName(wxString facename
)
470 wxStrncpy(lf
.lfFaceName
, facename
, WXSIZEOF(lf
.lfFaceName
));
473 void wxNativeFontInfo::SetFamily(wxFontFamily family
)
481 ff_family
= FF_SCRIPT
;
482 facename
= _T("Script");
486 ff_family
= FF_DECORATIVE
;
487 facename
= _T("Old English Text MT");
491 ff_family
= FF_ROMAN
;
492 facename
= _T("Times New Roman");
497 ff_family
= FF_MODERN
;
498 facename
= _T("Courier New");
502 ff_family
= FF_SWISS
;
503 facename
= _T("Arial");
508 ff_family
= FF_SWISS
;
509 facename
= _T("MS Sans Serif");
512 lf
.lfPitchAndFamily
= DEFAULT_PITCH
| ff_family
;
514 if ( !wxStrlen(lf
.lfFaceName
) )
516 SetFaceName(facename
);
520 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding
)
522 wxNativeEncodingInfo info
;
523 if ( !wxGetNativeFontEncoding(encoding
, &info
) )
526 if ( wxFontMapper::Get()->GetAltForEncoding(encoding
, &info
) )
528 if ( !info
.facename
.empty() )
530 // if we have this encoding only in some particular facename, use
531 // the facename - it is better to show the correct characters in a
532 // wrong facename than unreadable text in a correct one
533 SetFaceName(info
.facename
);
537 #endif // wxUSE_FONTMAP
539 // unsupported encoding, replace with the default
540 info
.charset
= DEFAULT_CHARSET
;
544 lf
.lfCharSet
= info
.charset
;
547 bool wxNativeFontInfo::FromString(const wxString
& s
)
551 wxStringTokenizer
tokenizer(s
, _T(";"));
554 wxString token
= tokenizer
.GetNextToken();
555 if ( token
!= _T('0') )
558 token
= tokenizer
.GetNextToken();
559 if ( !token
.ToLong(&l
) )
563 token
= tokenizer
.GetNextToken();
564 if ( !token
.ToLong(&l
) )
568 token
= tokenizer
.GetNextToken();
569 if ( !token
.ToLong(&l
) )
573 token
= tokenizer
.GetNextToken();
574 if ( !token
.ToLong(&l
) )
576 lf
.lfOrientation
= l
;
578 token
= tokenizer
.GetNextToken();
579 if ( !token
.ToLong(&l
) )
583 token
= tokenizer
.GetNextToken();
584 if ( !token
.ToLong(&l
) )
586 lf
.lfItalic
= (BYTE
)l
;
588 token
= tokenizer
.GetNextToken();
589 if ( !token
.ToLong(&l
) )
591 lf
.lfUnderline
= (BYTE
)l
;
593 token
= tokenizer
.GetNextToken();
594 if ( !token
.ToLong(&l
) )
596 lf
.lfStrikeOut
= (BYTE
)l
;
598 token
= tokenizer
.GetNextToken();
599 if ( !token
.ToLong(&l
) )
601 lf
.lfCharSet
= (BYTE
)l
;
603 token
= tokenizer
.GetNextToken();
604 if ( !token
.ToLong(&l
) )
606 lf
.lfOutPrecision
= (BYTE
)l
;
608 token
= tokenizer
.GetNextToken();
609 if ( !token
.ToLong(&l
) )
611 lf
.lfClipPrecision
= (BYTE
)l
;
613 token
= tokenizer
.GetNextToken();
614 if ( !token
.ToLong(&l
) )
616 lf
.lfQuality
= (BYTE
)l
;
618 token
= tokenizer
.GetNextToken();
619 if ( !token
.ToLong(&l
) )
621 lf
.lfPitchAndFamily
= (BYTE
)l
;
623 token
= tokenizer
.GetNextToken();
626 wxStrcpy(lf
.lfFaceName
, token
.c_str());
631 wxString
wxNativeFontInfo::ToString() const
635 s
.Printf(_T("%d;%d;%d;%d;%d;%d;%d;%d;%d;%d;%d;%d;%d;%d;%s"),
636 0, // version, in case we want to change the format later
655 // ----------------------------------------------------------------------------
657 // ----------------------------------------------------------------------------
663 bool wxFont::Create(const wxNativeFontInfo
& info
, WXHFONT hFont
)
667 m_refData
= new wxFontRefData(info
, hFont
);
674 wxFont::wxFont(const wxString
& fontdesc
)
676 wxNativeFontInfo info
;
677 if ( info
.FromString(fontdesc
) )
681 /* Constructor for a font. Note that the real construction is done
682 * in wxDC::SetFont, when information is available about scaling etc.
684 bool wxFont::Create(int pointSize
,
689 const wxString
& faceName
,
690 wxFontEncoding encoding
)
694 // wxDEFAULT is a valid value for the font size too so we must treat it
695 // specially here (otherwise the size would be 70 == wxDEFAULT value)
696 if ( pointSize
== wxDEFAULT
)
698 pointSize
= wxNORMAL_FONT
->GetPointSize();
701 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
702 underlined
, faceName
, encoding
);
713 // ----------------------------------------------------------------------------
714 // real implementation
715 // ----------------------------------------------------------------------------
717 bool wxFont::RealizeResource()
719 if ( GetResourceHandle() )
721 // VZ: the old code returned FALSE in this case, but it doesn't seem
722 // to make sense because the font _was_ created
726 return M_FONTDATA
->Alloc(this);
729 bool wxFont::FreeResource(bool WXUNUSED(force
))
731 if ( GetResourceHandle() )
741 WXHANDLE
wxFont::GetResourceHandle()
746 WXHFONT
wxFont::GetHFONT() const
748 return M_FONTDATA
? M_FONTDATA
->GetHFONT() : 0;
751 bool wxFont::IsFree() const
753 return M_FONTDATA
&& (M_FONTDATA
->GetHFONT() == 0);
756 void wxFont::Unshare()
758 // Don't change shared data
761 m_refData
= new wxFontRefData();
765 wxFontRefData
* ref
= new wxFontRefData(*M_FONTDATA
);
771 // ----------------------------------------------------------------------------
772 // change font attribute: we recreate font when doing it
773 // ----------------------------------------------------------------------------
775 void wxFont::SetPointSize(int pointSize
)
779 M_FONTDATA
->SetPointSize(pointSize
);
784 void wxFont::SetFamily(int family
)
788 M_FONTDATA
->SetFamily(family
);
793 void wxFont::SetStyle(int style
)
797 M_FONTDATA
->SetStyle(style
);
802 void wxFont::SetWeight(int weight
)
806 M_FONTDATA
->SetWeight(weight
);
811 void wxFont::SetFaceName(const wxString
& faceName
)
815 M_FONTDATA
->SetFaceName(faceName
);
820 void wxFont::SetUnderlined(bool underlined
)
824 M_FONTDATA
->SetUnderlined(underlined
);
829 void wxFont::SetEncoding(wxFontEncoding encoding
)
833 M_FONTDATA
->SetEncoding(encoding
);
838 void wxFont::SetNativeFontInfo(const wxNativeFontInfo
& info
)
844 *M_FONTDATA
= wxFontRefData(info
);
849 // ----------------------------------------------------------------------------
851 // ----------------------------------------------------------------------------
853 int wxFont::GetPointSize() const
855 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
857 return M_FONTDATA
->GetPointSize();
860 int wxFont::GetFamily() const
862 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
864 return M_FONTDATA
->GetFamily();
867 int wxFont::GetStyle() const
869 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
871 return M_FONTDATA
->GetStyle();
874 int wxFont::GetWeight() const
876 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
878 return M_FONTDATA
->GetWeight();
881 bool wxFont::GetUnderlined() const
883 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid font") );
885 return M_FONTDATA
->GetUnderlined();
888 wxString
wxFont::GetFaceName() const
890 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
892 return M_FONTDATA
->GetFaceName();
895 wxFontEncoding
wxFont::GetEncoding() const
897 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
899 return M_FONTDATA
->GetEncoding();
902 wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
904 if ( M_FONTDATA
->HasNativeFontInfo() )
905 return new wxNativeFontInfo(M_FONTDATA
->GetNativeFontInfo());
910 bool wxFont::IsFixedWidth() const
912 if ( M_FONTDATA
->HasNativeFontInfo() )
914 // the two low-order bits specify the pitch of the font, the rest is
916 BYTE pitch
= M_FONTDATA
->GetNativeFontInfo().
917 lf
.lfPitchAndFamily
& PITCH_MASK
;
919 return pitch
== FIXED_PITCH
;
922 return wxFontBase::IsFixedWidth();