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
;
284 // This is the best we can do since we don't have the
285 // correct information at this point.
289 wxFontRefData::~wxFontRefData()
294 bool wxFontRefData::Alloc(wxFont
*font
)
296 if ( !m_nativeFontInfoOk
)
298 wxFillLogFont(&m_nativeFontInfo
.lf
, font
);
299 m_nativeFontInfoOk
= TRUE
;
302 HFONT hfont
= ::CreateFontIndirect(&m_nativeFontInfo
.lf
);
305 wxLogLastError(wxT("CreateFont"));
310 m_hFont
= (WXHFONT
)hfont
;
315 void wxFontRefData::Free()
319 if ( !::DeleteObject((HFONT
) m_hFont
) )
321 wxLogLastError(wxT("DeleteObject(font)"));
328 // ----------------------------------------------------------------------------
330 // ----------------------------------------------------------------------------
332 void wxNativeFontInfo::Init()
337 int wxNativeFontInfo::GetPointSize() const
339 // FIXME: using the screen here results in incorrect font size calculation
341 const int ppInch
= ::GetDeviceCaps(ScreenHDC(), LOGPIXELSY
);
343 return (int) (((72.0*(double)abs(lf
.lfHeight
)) / (double) ppInch
) + 0.5);
346 wxFontStyle
wxNativeFontInfo::GetStyle() const
348 return lf
.lfItalic
? wxFONTSTYLE_ITALIC
: wxFONTSTYLE_NORMAL
;
351 wxFontWeight
wxNativeFontInfo::GetWeight() const
353 if ( lf
.lfWeight
<= 300 )
354 return wxFONTWEIGHT_LIGHT
;
356 if ( lf
.lfWeight
>= 600 )
357 return wxFONTWEIGHT_BOLD
;
359 return wxFONTWEIGHT_NORMAL
;
362 bool wxNativeFontInfo::GetUnderlined() const
364 return lf
.lfUnderline
!= 0;
367 wxString
wxNativeFontInfo::GetFaceName() const
369 return lf
.lfFaceName
;
372 wxFontFamily
wxNativeFontInfo::GetFamily() const
376 // extract family from pitch-and-family
377 switch ( lf
.lfPitchAndFamily
& ~PITCH_MASK
)
380 family
= wxFONTFAMILY_ROMAN
;
384 wxFAIL_MSG( _T("unknown LOGFONT::lfFamily value") );
388 family
= wxFONTFAMILY_SWISS
;
392 family
= wxFONTFAMILY_SCRIPT
;
396 family
= wxFONTFAMILY_MODERN
;
400 family
= wxFONTFAMILY_DECORATIVE
;
407 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
409 return wxGetFontEncFromCharSet(lf
.lfCharSet
);
412 void wxNativeFontInfo::SetPointSize(int pointsize
)
414 #if wxFONT_SIZE_COMPATIBILITY
415 // Incorrect, but compatible with old wxWindows behaviour
416 lf
.lfHeight
= (pointSize
*ppInch
)/72;
417 #else // wxFONT_SIZE_COMPATIBILITY
418 // FIXME: using the screen here results in incorrect font size calculation
420 const int ppInch
= ::GetDeviceCaps(ScreenHDC(), LOGPIXELSY
);
422 lf
.lfHeight
= -(int)((pointsize
*((double)ppInch
)/72.0) + 0.5);
423 #endif // wxFONT_SIZE_COMPATIBILITY/!wxFONT_SIZE_COMPATIBILITY
426 void wxNativeFontInfo::SetStyle(wxFontStyle style
)
431 wxFAIL_MSG( _T("unknown font style") );
434 case wxFONTSTYLE_NORMAL
:
437 case wxFONTSTYLE_ITALIC
:
438 case wxFONTSTYLE_SLANT
:
444 void wxNativeFontInfo::SetWeight(wxFontWeight weight
)
449 wxFAIL_MSG( _T("unknown font weight") );
452 case wxFONTWEIGHT_NORMAL
:
453 lf
.lfWeight
= FW_NORMAL
;
456 case wxFONTWEIGHT_LIGHT
:
457 lf
.lfWeight
= FW_LIGHT
;
460 case wxFONTWEIGHT_BOLD
:
461 lf
.lfWeight
= FW_BOLD
;
466 void wxNativeFontInfo::SetUnderlined(bool underlined
)
468 lf
.lfUnderline
= underlined
;
471 void wxNativeFontInfo::SetFaceName(wxString facename
)
473 wxStrncpy(lf
.lfFaceName
, facename
, WXSIZEOF(lf
.lfFaceName
));
476 void wxNativeFontInfo::SetFamily(wxFontFamily family
)
484 ff_family
= FF_SCRIPT
;
485 facename
= _T("Script");
489 ff_family
= FF_DECORATIVE
;
490 facename
= _T("Old English Text MT");
494 ff_family
= FF_ROMAN
;
495 facename
= _T("Times New Roman");
500 ff_family
= FF_MODERN
;
501 facename
= _T("Courier New");
505 ff_family
= FF_SWISS
;
506 facename
= _T("Arial");
511 ff_family
= FF_SWISS
;
512 facename
= _T("MS Sans Serif");
515 lf
.lfPitchAndFamily
= DEFAULT_PITCH
| ff_family
;
517 if ( !wxStrlen(lf
.lfFaceName
) )
519 SetFaceName(facename
);
523 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding
)
525 wxNativeEncodingInfo info
;
526 if ( !wxGetNativeFontEncoding(encoding
, &info
) )
529 if ( wxFontMapper::Get()->GetAltForEncoding(encoding
, &info
) )
531 if ( !info
.facename
.empty() )
533 // if we have this encoding only in some particular facename, use
534 // the facename - it is better to show the correct characters in a
535 // wrong facename than unreadable text in a correct one
536 SetFaceName(info
.facename
);
540 #endif // wxUSE_FONTMAP
542 // unsupported encoding, replace with the default
543 info
.charset
= DEFAULT_CHARSET
;
547 lf
.lfCharSet
= info
.charset
;
550 bool wxNativeFontInfo::FromString(const wxString
& s
)
554 wxStringTokenizer
tokenizer(s
, _T(";"));
557 wxString token
= tokenizer
.GetNextToken();
558 if ( token
!= _T('0') )
561 token
= tokenizer
.GetNextToken();
562 if ( !token
.ToLong(&l
) )
566 token
= tokenizer
.GetNextToken();
567 if ( !token
.ToLong(&l
) )
571 token
= tokenizer
.GetNextToken();
572 if ( !token
.ToLong(&l
) )
576 token
= tokenizer
.GetNextToken();
577 if ( !token
.ToLong(&l
) )
579 lf
.lfOrientation
= l
;
581 token
= tokenizer
.GetNextToken();
582 if ( !token
.ToLong(&l
) )
586 token
= tokenizer
.GetNextToken();
587 if ( !token
.ToLong(&l
) )
589 lf
.lfItalic
= (BYTE
)l
;
591 token
= tokenizer
.GetNextToken();
592 if ( !token
.ToLong(&l
) )
594 lf
.lfUnderline
= (BYTE
)l
;
596 token
= tokenizer
.GetNextToken();
597 if ( !token
.ToLong(&l
) )
599 lf
.lfStrikeOut
= (BYTE
)l
;
601 token
= tokenizer
.GetNextToken();
602 if ( !token
.ToLong(&l
) )
604 lf
.lfCharSet
= (BYTE
)l
;
606 token
= tokenizer
.GetNextToken();
607 if ( !token
.ToLong(&l
) )
609 lf
.lfOutPrecision
= (BYTE
)l
;
611 token
= tokenizer
.GetNextToken();
612 if ( !token
.ToLong(&l
) )
614 lf
.lfClipPrecision
= (BYTE
)l
;
616 token
= tokenizer
.GetNextToken();
617 if ( !token
.ToLong(&l
) )
619 lf
.lfQuality
= (BYTE
)l
;
621 token
= tokenizer
.GetNextToken();
622 if ( !token
.ToLong(&l
) )
624 lf
.lfPitchAndFamily
= (BYTE
)l
;
626 token
= tokenizer
.GetNextToken();
629 wxStrcpy(lf
.lfFaceName
, token
.c_str());
634 wxString
wxNativeFontInfo::ToString() const
638 s
.Printf(_T("%d;%d;%d;%d;%d;%d;%d;%d;%d;%d;%d;%d;%d;%d;%s"),
639 0, // version, in case we want to change the format later
658 // ----------------------------------------------------------------------------
660 // ----------------------------------------------------------------------------
666 bool wxFont::Create(const wxNativeFontInfo
& info
, WXHFONT hFont
)
670 m_refData
= new wxFontRefData(info
, hFont
);
677 wxFont::wxFont(const wxString
& fontdesc
)
679 wxNativeFontInfo info
;
680 if ( info
.FromString(fontdesc
) )
684 /* Constructor for a font. Note that the real construction is done
685 * in wxDC::SetFont, when information is available about scaling etc.
687 bool wxFont::Create(int pointSize
,
692 const wxString
& faceName
,
693 wxFontEncoding encoding
)
697 // wxDEFAULT is a valid value for the font size too so we must treat it
698 // specially here (otherwise the size would be 70 == wxDEFAULT value)
699 if ( pointSize
== wxDEFAULT
)
701 pointSize
= wxNORMAL_FONT
->GetPointSize();
704 m_refData
= new wxFontRefData(pointSize
, family
, style
, weight
,
705 underlined
, faceName
, encoding
);
716 // ----------------------------------------------------------------------------
717 // real implementation
718 // ----------------------------------------------------------------------------
720 bool wxFont::RealizeResource()
722 if ( GetResourceHandle() )
724 // VZ: the old code returned FALSE in this case, but it doesn't seem
725 // to make sense because the font _was_ created
729 return M_FONTDATA
->Alloc(this);
732 bool wxFont::FreeResource(bool WXUNUSED(force
))
734 if ( GetResourceHandle() )
744 WXHANDLE
wxFont::GetResourceHandle()
749 WXHFONT
wxFont::GetHFONT() const
751 return M_FONTDATA
? M_FONTDATA
->GetHFONT() : 0;
754 bool wxFont::IsFree() const
756 return M_FONTDATA
&& (M_FONTDATA
->GetHFONT() == 0);
759 void wxFont::Unshare()
761 // Don't change shared data
764 m_refData
= new wxFontRefData();
768 wxFontRefData
* ref
= new wxFontRefData(*M_FONTDATA
);
774 // ----------------------------------------------------------------------------
775 // change font attribute: we recreate font when doing it
776 // ----------------------------------------------------------------------------
778 void wxFont::SetPointSize(int pointSize
)
782 M_FONTDATA
->SetPointSize(pointSize
);
787 void wxFont::SetFamily(int family
)
791 M_FONTDATA
->SetFamily(family
);
796 void wxFont::SetStyle(int style
)
800 M_FONTDATA
->SetStyle(style
);
805 void wxFont::SetWeight(int weight
)
809 M_FONTDATA
->SetWeight(weight
);
814 void wxFont::SetFaceName(const wxString
& faceName
)
818 M_FONTDATA
->SetFaceName(faceName
);
823 void wxFont::SetUnderlined(bool underlined
)
827 M_FONTDATA
->SetUnderlined(underlined
);
832 void wxFont::SetEncoding(wxFontEncoding encoding
)
836 M_FONTDATA
->SetEncoding(encoding
);
841 void wxFont::SetNativeFontInfo(const wxNativeFontInfo
& info
)
847 *M_FONTDATA
= wxFontRefData(info
);
852 // ----------------------------------------------------------------------------
854 // ----------------------------------------------------------------------------
856 int wxFont::GetPointSize() const
858 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
860 return M_FONTDATA
->GetPointSize();
863 int wxFont::GetFamily() const
865 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
867 return M_FONTDATA
->GetFamily();
870 int wxFont::GetStyle() const
872 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
874 return M_FONTDATA
->GetStyle();
877 int wxFont::GetWeight() const
879 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
881 return M_FONTDATA
->GetWeight();
884 bool wxFont::GetUnderlined() const
886 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid font") );
888 return M_FONTDATA
->GetUnderlined();
891 wxString
wxFont::GetFaceName() const
893 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
895 return M_FONTDATA
->GetFaceName();
898 wxFontEncoding
wxFont::GetEncoding() const
900 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
902 return M_FONTDATA
->GetEncoding();
905 wxNativeFontInfo
*wxFont::GetNativeFontInfo() const
907 if ( M_FONTDATA
->HasNativeFontInfo() )
908 return new wxNativeFontInfo(M_FONTDATA
->GetNativeFontInfo());
913 bool wxFont::IsFixedWidth() const
915 if ( M_FONTDATA
->HasNativeFontInfo() )
917 // the two low-order bits specify the pitch of the font, the rest is
919 BYTE pitch
= M_FONTDATA
->GetNativeFontInfo().
920 lf
.lfPitchAndFamily
& PITCH_MASK
;
922 return pitch
== FIXED_PITCH
;
925 return wxFontBase::IsFixedWidth();