1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxFont class
4 // Author: David Webster
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 // For compilers that support precompilation, includes "wx.h".
22 #include "wx/wxprec.h"
33 #include "wx/os2/private.h"
35 #include "wx/fontutil.h"
36 #include "wx/fontmap.h"
38 #include "wx/tokenzr.h"
40 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
42 // ----------------------------------------------------------------------------
43 // wxFontRefData - the internal description of the font
44 // ----------------------------------------------------------------------------
46 class WXDLLEXPORT wxFontRefData
: public wxGDIRefData
51 Init(-1, wxFONTFAMILY_DEFAULT
, wxFONTSTYLE_NORMAL
, wxFONTWEIGHT_NORMAL
, FALSE
,
52 "", wxFONTENCODING_DEFAULT
);
55 wxFontRefData( int nSize
60 ,const wxString
& sFaceName
61 ,wxFontEncoding vEncoding
74 wxFontRefData( const wxNativeFontInfo
& rInfo
85 wxFontRefData(const wxFontRefData
& rData
)
87 Init( rData
.m_nPointSize
95 m_nFontId
= rData
.m_nFontId
;
98 virtual ~wxFontRefData();
103 bool Alloc(wxFont
* pFont
);
107 // All wxFont accessors
109 inline int GetPointSize(void) const
111 return m_bNativeFontInfoOk
? m_vNativeFontInfo
.GetPointSize()
115 inline int GetFamily(void) const
120 inline int GetStyle(void) const
122 return m_bNativeFontInfoOk
? m_vNativeFontInfo
.GetStyle()
126 inline int GetWeight(void) const
128 return m_bNativeFontInfoOk
? m_vNativeFontInfo
.GetWeight()
132 inline bool GetUnderlined(void) const
134 return m_bNativeFontInfoOk
? m_vNativeFontInfo
.GetUnderlined()
138 inline wxString
GetFaceName(void) const
142 if (m_bNativeFontInfoOk
)
143 sFaceName
= m_vNativeFontInfo
.GetFaceName();
145 sFaceName
= m_sFaceName
;
150 inline wxFontEncoding
GetEncoding(void) const
152 return m_bNativeFontInfoOk
? m_vNativeFontInfo
.GetEncoding()
156 inline WXHFONT
GetHFONT(void) const { return m_hFont
; }
157 inline HPS
GetPS(void) const { return m_hPS
; }
158 inline PFONTMETRICS
GetFM(void) const { return m_pFM
; }
159 inline int GetNumFonts(void) const { return m_nNumFonts
; }
162 inline void SetPointSize(int nPointSize
)
164 if (m_bNativeFontInfoOk
)
165 m_vNativeFontInfo
.SetPointSize(nPointSize
);
167 m_nPointSize
= nPointSize
;
170 inline void SetFamily(int nFamily
)
175 inline void SetStyle(int nStyle
)
177 if (m_bNativeFontInfoOk
)
178 m_vNativeFontInfo
.SetStyle((wxFontStyle
)nStyle
);
183 inline void SetWeight(int nWeight
)
185 if (m_bNativeFontInfoOk
)
186 m_vNativeFontInfo
.SetWeight((wxFontWeight
)nWeight
);
191 inline void SetFaceName(const wxString
& sFaceName
)
193 if (m_bNativeFontInfoOk
)
194 m_vNativeFontInfo
.SetFaceName(sFaceName
);
196 m_sFaceName
= sFaceName
;
199 inline void SetUnderlined(bool bUnderlined
)
201 if (m_bNativeFontInfoOk
)
202 m_vNativeFontInfo
.SetUnderlined(bUnderlined
);
204 m_bUnderlined
= bUnderlined
;
207 inline void SetEncoding(wxFontEncoding vEncoding
)
209 if (m_bNativeFontInfoOk
)
210 m_vNativeFontInfo
.SetEncoding(vEncoding
);
212 m_vEncoding
= vEncoding
;
215 inline void SetPS(HPS hPS
)
220 inline void SetFM(PFONTMETRICS pFM
)
225 inline void SetNumFonts(int nNumFonts
)
227 m_nNumFonts
= nNumFonts
;
231 // Native font info tests
233 bool HasNativeFontInfo() const { return m_bNativeFontInfoOk
; }
235 const wxNativeFontInfo
& GetNativeFontInfo() const
236 { return m_vNativeFontInfo
; }
240 // Common part of all ctors
247 ,const wxString
& rsFaceName
248 ,wxFontEncoding vEncoding
251 void Init( const wxNativeFontInfo
& rInfo
256 // If TRUE, the pointer to the actual font is temporary and SHOULD NOT BE
257 // DELETED by destructor
263 // Font characterstics
270 wxString m_sFaceName
;
271 wxFontEncoding m_vEncoding
;
277 wxNativeFontInfo m_vNativeFontInfo
;
278 bool m_bNativeFontInfoOk
;
281 // Some PM specific stuff
283 PFONTMETRICS m_pFM
; // array of FONTMETRICS structs
284 int m_nNumFonts
; // number of fonts in array
285 HPS m_hPS
; // PS handle this font belongs to
286 FATTRS m_vFattrs
; // Current fattrs struct
287 FACENAMEDESC m_vFname
; // Current facename struct
288 bool m_bInternalPS
; // Internally generated PS?
289 }; // end of CLASS wxFontRefData
291 // ============================================================================
293 // ============================================================================
295 // ----------------------------------------------------------------------------
297 // ----------------------------------------------------------------------------
299 void wxFontRefData::Init(
305 , const wxString
& rsFaceName
306 , wxFontEncoding vEncoding
310 m_nPointSize
= nPointSize
;
314 m_bUnderlined
= bUnderlined
;
315 m_sFaceName
= rsFaceName
;
316 m_vEncoding
= vEncoding
;
319 m_bNativeFontInfoOk
= FALSE
;
322 m_bTemporary
= FALSE
;
323 m_pFM
= (PFONTMETRICS
)NULL
;
326 } // end of wxFontRefData::Init
328 void wxFontRefData::Init(
329 const wxNativeFontInfo
& rInfo
330 , WXHFONT hFont
//this is the FontId -- functions as the hFont for OS/2
331 , WXHANDLE hPS
// Presentation Space we are using
335 // hFont may be zero, or it be passed in case we really want to
336 // use the exact font created in the underlying system
337 // (for example where we can't guarantee conversion from HFONT
338 // to LOGFONT back to HFONT)
341 m_nFontId
= (int)hFont
;
343 m_bNativeFontInfoOk
= TRUE
;
344 m_vNativeFontInfo
= rInfo
;
346 if (m_hPS
== NULLHANDLE
)
348 m_hPS
= ::WinGetPS(HWND_DESKTOP
);
355 wxFontRefData::~wxFontRefData()
360 bool wxFontRefData::Alloc(
367 if (!m_bNativeFontInfoOk
)
369 wxFillLogFont( &m_vNativeFontInfo
.fa
370 ,&m_vNativeFontInfo
.fn
376 m_bNativeFontInfoOk
= TRUE
;
379 if(::GpiCreateLogFont( m_hPS
382 ,&m_vNativeFontInfo
.fa
384 m_hFont
= (WXHFONT
)1;
387 // We don't actuall keep the font around if using a temporary PS
392 ::GpiDeleteSetId( m_hPS
396 ::WinReleasePS(m_hPS
);
400 // Select the font into the Presentation space
402 ::GpiSetCharSet(m_hPS
, flId
); // sets font for presentation space
405 wxLogLastError("CreateFont");
409 // Query for the actual metrics of the current font being used
411 ::GpiQueryFontMetrics(m_hPS
, sizeof(FONTMETRICS
), &m_vNativeFontInfo
.fm
);
414 // Set refData members with the results
416 m_hFont
= (WXHFONT
)m_nFontId
;
417 memcpy(&m_vFattrs
, &m_vNativeFontInfo
.fa
, sizeof(m_vFattrs
));
418 memcpy(&m_vFname
, &m_vNativeFontInfo
.fn
, sizeof(m_vFname
));
419 m_nPointSize
= m_vNativeFontInfo
.fm
.lEmHeight
;
420 if (strcmp(m_vNativeFontInfo
.fa
.szFacename
, "Times New Roman") == 0)
422 else if (strcmp(m_vNativeFontInfo
.fa
.szFacename
, "WarpSans") == 0)
424 else if (strcmp(m_vNativeFontInfo
.fa
.szFacename
, "Script") == 0)
425 m_nFamily
= wxSCRIPT
;
426 else if (strcmp(m_vNativeFontInfo
.fa
.szFacename
, "Courier New") == 0)
427 m_nFamily
= wxMODERN
;
428 else if (strcmp(m_vNativeFontInfo
.fa
.szFacename
, "Courier") == 0)
429 m_nFamily
= wxMODERN
;
433 if (m_vNativeFontInfo
.fa
.fsSelection
& FATTR_SEL_ITALIC
)
434 m_nStyle
= wxFONTSTYLE_ITALIC
;
436 m_nStyle
= wxFONTSTYLE_NORMAL
;
437 switch(m_vNativeFontInfo
.fn
.usWeightClass
)
439 case FWEIGHT_DONT_CARE
:
440 m_nWeight
= wxFONTWEIGHT_NORMAL
;
444 m_nWeight
= wxFONTWEIGHT_NORMAL
;
448 m_nWeight
= wxFONTWEIGHT_LIGHT
;
452 m_nWeight
= wxFONTWEIGHT_BOLD
;
455 case FWEIGHT_ULTRA_BOLD
:
456 m_nWeight
= wxFONTWEIGHT_MAX
;
460 m_nWeight
= wxFONTWEIGHT_NORMAL
;
462 m_bUnderlined
= ((m_vNativeFontInfo
.fa
.fsSelection
& FATTR_SEL_UNDERSCORE
) != 0);
463 m_sFaceName
= m_vNativeFontInfo
.fa
.szFacename
;
464 m_vEncoding
= wxGetFontEncFromCharSet(m_vNativeFontInfo
.fa
.usCodePage
);
466 } // end of wxFontRefData::Alloc
468 void wxFontRefData::Free()
472 m_pFM
= (PFONTMETRICS
)NULL
;
476 if (!::GpiSetCharSet(m_hPS
, LCID_DEFAULT
))
478 wxLogLastError(wxT("DeleteObject(font)"));
480 ::GpiDeleteSetId(m_hPS
, 1L); /* delete the logical font */
485 ::WinReleasePS(m_hPS
);
487 } // end of wxFontRefData::Free
489 // ----------------------------------------------------------------------------
491 // ----------------------------------------------------------------------------
493 void wxNativeFontInfo::Init()
495 memset(&fa
, '\0', sizeof(FATTRS
));
496 } // end of wxNativeFontInfo::Init
498 int wxNativeFontInfo::GetPointSize() const
501 } // end of wxNativeFontInfo::GetPointSize
503 wxFontStyle
wxNativeFontInfo::GetStyle() const
505 return fa
.fsSelection
& FATTR_SEL_ITALIC
? wxFONTSTYLE_ITALIC
: wxFONTSTYLE_NORMAL
;
506 } // end of wxNativeFontInfo::GetStyle
508 wxFontWeight
wxNativeFontInfo::GetWeight() const
510 switch(fn
.usWeightClass
)
512 case FWEIGHT_DONT_CARE
:
513 return wxFONTWEIGHT_NORMAL
;
516 return wxFONTWEIGHT_NORMAL
;
519 return wxFONTWEIGHT_LIGHT
;
522 return wxFONTWEIGHT_BOLD
;
524 case FWEIGHT_ULTRA_BOLD
:
525 return wxFONTWEIGHT_MAX
;
527 return wxFONTWEIGHT_NORMAL
;
528 } // end of wxNativeFontInfo::GetWeight
530 bool wxNativeFontInfo::GetUnderlined() const
532 return ((fa
.fsSelection
& FATTR_SEL_UNDERSCORE
) != 0);
533 } // end of wxNativeFontInfo::GetUnderlined
535 wxString
wxNativeFontInfo::GetFaceName() const
537 return fm
.szFacename
;
538 } // end of wxNativeFontInfo::GetFaceName
540 wxFontFamily
wxNativeFontInfo::GetFamily() const
545 // Extract family from facename
547 if (strcmp(fa
.szFacename
, "Times New Roman") == 0)
549 else if (strcmp(fa
.szFacename
, "WarpSans") == 0)
551 else if (strcmp(fa
.szFacename
, "Script") == 0)
553 else if (strcmp(fa
.szFacename
, "Courier New") == 0)
557 return (wxFontFamily
)nFamily
;
558 } // end of wxNativeFontInfo::GetFamily
560 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
562 return wxGetFontEncFromCharSet(fa
.usCodePage
);
563 } // end of wxNativeFontInfo::GetEncoding
565 void wxNativeFontInfo::SetPointSize(
569 fm
.lEmHeight
= (LONG
)nPointsize
;
570 } // end of wxNativeFontInfo::SetPointSize
572 void wxNativeFontInfo::SetStyle(
579 wxFAIL_MSG( _T("unknown font style") );
582 case wxFONTSTYLE_NORMAL
:
585 case wxFONTSTYLE_ITALIC
:
586 case wxFONTSTYLE_SLANT
:
587 fa
.fsSelection
|= FATTR_SEL_ITALIC
;
590 } // end of wxNativeFontInfo::SetStyle
592 void wxNativeFontInfo::SetWeight(
599 wxFAIL_MSG( _T("unknown font weight") );
602 case wxFONTWEIGHT_NORMAL
:
603 fn
.usWeightClass
= FWEIGHT_NORMAL
;
606 case wxFONTWEIGHT_LIGHT
:
607 fn
.usWeightClass
= FWEIGHT_LIGHT
;
610 case wxFONTWEIGHT_BOLD
:
611 fn
.usWeightClass
= FWEIGHT_BOLD
;
614 } // end of wxNativeFontInfo::SetWeight
616 void wxNativeFontInfo::SetUnderlined(
621 fa
.fsSelection
|= FATTR_SEL_UNDERSCORE
;
622 } // end of wxNativeFontInfo::SetUnderlined
624 void wxNativeFontInfo::SetFaceName(
628 wxStrncpy(fa
.szFacename
, sFacename
, WXSIZEOF(fa
.szFacename
));
629 } // end of wxNativeFontInfo::SetFaceName
631 void wxNativeFontInfo::SetFamily(
640 sFacename
= _T("Script");
644 sFacename
= _T("Times New Roman");
648 sFacename
= _T("Times New Roman");
653 sFacename
= _T("Courier New");
657 sFacename
= _T("WarpSans");
662 sFacename
= _T("Helv");
665 if (!wxStrlen(fa
.szFacename
) )
667 SetFaceName(sFacename
);
669 } // end of wxNativeFontInfo::SetFamily
671 void wxNativeFontInfo::SetEncoding(
672 wxFontEncoding eEncoding
675 wxNativeEncodingInfo vInfo
;
677 if ( !wxGetNativeFontEncoding( eEncoding
682 if (wxTheFontMapper
->GetAltForEncoding( eEncoding
686 if (!vInfo
.facename
.empty())
689 // If we have this encoding only in some particular facename, use
690 // the facename - it is better to show the correct characters in a
691 // wrong facename than unreadable text in a correct one
693 SetFaceName(vInfo
.facename
);
697 #endif // wxUSE_FONTMAP
699 // unsupported encoding, replace with the default
703 fa
.usCodePage
= vInfo
.charset
;
704 } // end of wxNativeFontInfo::SetFaceName
706 bool wxNativeFontInfo::FromString(
707 const wxString
& rsStr
712 wxStringTokenizer
vTokenizer(rsStr
, _T(";"));
717 wxString sToken
= vTokenizer
.GetNextToken();
719 if (sToken
!= _T('0'))
722 sToken
= vTokenizer
.GetNextToken();
723 if (!sToken
.ToLong(&lVal
))
727 sToken
= vTokenizer
.GetNextToken();
728 if (!sToken
.ToLong(&lVal
))
730 fa
.lAveCharWidth
= lVal
;
732 sToken
= vTokenizer
.GetNextToken();
733 if (!sToken
.ToLong(&lVal
))
735 fa
.fsSelection
= (USHORT
)lVal
;
737 sToken
= vTokenizer
.GetNextToken();
738 if (!sToken
.ToLong(&lVal
))
740 fa
.fsType
= (USHORT
)lVal
;
742 sToken
= vTokenizer
.GetNextToken();
743 if (!sToken
.ToLong(&lVal
))
745 fa
.fsFontUse
= (USHORT
)lVal
;
747 sToken
= vTokenizer
.GetNextToken();
748 if (!sToken
.ToLong(&lVal
))
750 fa
.idRegistry
= (USHORT
)lVal
;
752 sToken
= vTokenizer
.GetNextToken();
753 if (!sToken
.ToLong(&lVal
))
755 fa
.usCodePage
= (USHORT
)lVal
;
757 sToken
= vTokenizer
.GetNextToken();
758 if (!sToken
.ToLong(&lVal
))
762 sToken
= vTokenizer
.GetNextToken();
763 if (!sToken
.ToLong(&lVal
))
765 fn
.usWeightClass
= (USHORT
)lVal
;
767 sToken
= vTokenizer
.GetNextToken();
770 wxStrcpy(fa
.szFacename
, sToken
.c_str());
772 } // end of wxNativeFontInfo::FromString
774 wxString
wxNativeFontInfo::ToString() const
778 sStr
.Printf(_T("%d;%ld;%ld;%ld;%d;%d;%d;%d;%d;%ld;%d;%s"),
779 0, // version, in case we want to change the format later
792 } // end of wxNativeFontInfo::ToString
794 // ----------------------------------------------------------------------------
796 // ----------------------------------------------------------------------------
800 } // end of wxFont::Init
803 const wxNativeFontInfo
& rInfo
808 m_refData
= new wxFontRefData( rInfo
813 } // end of wxFont::Create
816 const wxString
& rsFontdesc
819 wxNativeFontInfo vInfo
;
821 if (vInfo
.FromString(rsFontdesc
))
823 } // end of wxFont::wxFont
825 // ----------------------------------------------------------------------------
826 // Constructor for a font. Note that the real construction is done
827 // in wxDC::SetFont, when information is available about scaling etc.
828 // ----------------------------------------------------------------------------
835 , const wxString
& rsFaceName
836 , wxFontEncoding vEncoding
842 // wxDEFAULT is a valid value for the font size too so we must treat it
843 // specially here (otherwise the size would be 70 == wxDEFAULT value)
845 if (nPointSize
== wxDEFAULT
)
847 nPointSize
= wxNORMAL_FONT
->GetPointSize();
849 m_refData
= new wxFontRefData( nPointSize
859 } // end of wxFont::Create
863 } // end of wxFont::~wxFont
865 // ----------------------------------------------------------------------------
866 // real implementation
867 // Boris' Kovalenko comments:
868 // Because OS/2 fonts are associated with PS we can not create the font
869 // here, but we may check that font definition is true
870 // ----------------------------------------------------------------------------
872 bool wxFont::RealizeResource()
874 if ( GetResourceHandle() )
878 return M_FONTDATA
->Alloc(this);
879 } // end of wxFont::RealizeResource
881 bool wxFont::FreeResource(
885 if (GetResourceHandle())
891 } // end of wxFont::FreeResource
893 WXHANDLE
wxFont::GetResourceHandle()
896 } // end of wxFont::GetResourceHandle
898 WXHFONT
wxFont::GetHFONT() const
900 return M_FONTDATA
? M_FONTDATA
->GetHFONT() : 0;
901 } // end of wxFont::GetHFONT
903 bool wxFont::IsFree() const
905 return M_FONTDATA
&& (M_FONTDATA
->GetHFONT() == 0);
906 } // end of wxFont::IsFree
908 void wxFont::Unshare()
910 // Don't change shared data
913 m_refData
= new wxFontRefData();
917 wxFontRefData
* ref
= new wxFontRefData(*M_FONTDATA
);
921 } // end of wxFont::Unshare
923 // ----------------------------------------------------------------------------
924 // change font attribute: we recreate font when doing it
925 // ----------------------------------------------------------------------------
927 void wxFont::SetPointSize(
933 M_FONTDATA
->SetPointSize(nPointSize
);
936 } // end of wxFont::SetPointSize
938 void wxFont::SetFamily(
944 M_FONTDATA
->SetFamily(nFamily
);
947 } // end of wxFont::SetFamily
949 void wxFont::SetStyle(
955 M_FONTDATA
->SetStyle(nStyle
);
958 } // end of wxFont::SetStyle
960 void wxFont::SetWeight(
966 M_FONTDATA
->SetWeight(nWeight
);
969 } // end of wxFont::SetWeight
971 void wxFont::SetFaceName(
972 const wxString
& rsFaceName
977 M_FONTDATA
->SetFaceName(rsFaceName
);
980 } // end of wxFont::SetFaceName
982 void wxFont::SetUnderlined(
988 M_FONTDATA
->SetUnderlined(bUnderlined
);
991 } // end of wxFont::SetUnderlined
993 void wxFont::SetEncoding(
994 wxFontEncoding vEncoding
999 M_FONTDATA
->SetEncoding(vEncoding
);
1002 } // end of wxFont::SetEncoding
1004 void wxFont::SetNativeFontInfo(
1005 const wxNativeFontInfo
& rInfo
1012 *M_FONTDATA
= wxFontRefData(rInfo
);
1017 // ----------------------------------------------------------------------------
1019 // ----------------------------------------------------------------------------
1021 int wxFont::GetPointSize() const
1023 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
1025 return M_FONTDATA
->GetPointSize();
1026 } // end of wxFont::GetPointSize
1028 int wxFont::GetFamily() const
1030 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
1032 return M_FONTDATA
->GetFamily();
1033 } // end of wxFont::GetFamily
1035 int wxFont::GetStyle() const
1037 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
1039 return M_FONTDATA
->GetStyle();
1040 } // end of wxFont::GetStyle
1042 int wxFont::GetWeight() const
1044 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
1046 return M_FONTDATA
->GetWeight();
1049 bool wxFont::GetUnderlined() const
1051 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid font") );
1053 return M_FONTDATA
->GetUnderlined();
1054 } // end of wxFont::GetUnderlined
1056 wxString
wxFont::GetFaceName() const
1058 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
1060 return M_FONTDATA
->GetFaceName();
1061 } // end of wxFont::GetFaceName
1063 wxFontEncoding
wxFont::GetEncoding() const
1065 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
1067 return M_FONTDATA
->GetEncoding();
1068 } // end of wxFont::GetEncoding
1070 wxNativeFontInfo
* wxFont::GetNativeFontInfo() const
1072 if (M_FONTDATA
->HasNativeFontInfo())
1073 return new wxNativeFontInfo(M_FONTDATA
->GetNativeFontInfo());
1075 } // end of wxFont::GetNativeFontInfo
1078 // Internal use only method to set the FONTMETRICS array
1085 M_FONTDATA
->SetFM(pFM
);
1086 M_FONTDATA
->SetNumFonts(nNumFonts
);
1087 } // end of wxFont::SetFM
1096 M_FONTDATA
->SetPS(hPS
);
1099 } // end of wxFont::SetUnderlined