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
112 // We don't use the actual native font point size since it is
113 // the chosen physical font, which is usually only and approximation
114 // of the desired outline font. The actual displayable point size
115 // is the one stored in the refData
120 inline int GetFamily(void) const
125 inline int GetStyle(void) const
127 return m_bNativeFontInfoOk
? m_vNativeFontInfo
.GetStyle()
131 inline int GetWeight(void) const
133 return m_bNativeFontInfoOk
? m_vNativeFontInfo
.GetWeight()
137 inline bool GetUnderlined(void) const
139 return m_bNativeFontInfoOk
? m_vNativeFontInfo
.GetUnderlined()
143 inline wxString
GetFaceName(void) const
147 if (m_bNativeFontInfoOk
)
148 sFaceName
= m_vNativeFontInfo
.GetFaceName();
150 sFaceName
= m_sFaceName
;
155 inline wxFontEncoding
GetEncoding(void) const
157 return m_bNativeFontInfoOk
? m_vNativeFontInfo
.GetEncoding()
161 inline WXHFONT
GetHFONT(void) const { return m_hFont
; }
162 inline HPS
GetPS(void) const { return m_hPS
; }
163 inline PFONTMETRICS
GetFM(void) const { return m_pFM
; }
164 inline int GetNumFonts(void) const { return m_nNumFonts
; }
167 inline void SetPointSize(int nPointSize
)
169 if (m_bNativeFontInfoOk
)
170 m_vNativeFontInfo
.SetPointSize(nPointSize
);
172 m_nPointSize
= nPointSize
;
175 inline void SetFamily(int nFamily
)
180 inline void SetStyle(int nStyle
)
182 if (m_bNativeFontInfoOk
)
183 m_vNativeFontInfo
.SetStyle((wxFontStyle
)nStyle
);
188 inline void SetWeight(int nWeight
)
190 if (m_bNativeFontInfoOk
)
191 m_vNativeFontInfo
.SetWeight((wxFontWeight
)nWeight
);
196 inline void SetFaceName(const wxString
& sFaceName
)
198 if (m_bNativeFontInfoOk
)
199 m_vNativeFontInfo
.SetFaceName(sFaceName
);
201 m_sFaceName
= sFaceName
;
204 inline void SetUnderlined(bool bUnderlined
)
206 if (m_bNativeFontInfoOk
)
207 m_vNativeFontInfo
.SetUnderlined(bUnderlined
);
209 m_bUnderlined
= bUnderlined
;
212 inline void SetEncoding(wxFontEncoding vEncoding
)
214 if (m_bNativeFontInfoOk
)
215 m_vNativeFontInfo
.SetEncoding(vEncoding
);
217 m_vEncoding
= vEncoding
;
220 inline void SetPS(HPS hPS
)
225 inline void SetFM(PFONTMETRICS pFM
)
230 inline void SetNumFonts(int nNumFonts
)
232 m_nNumFonts
= nNumFonts
;
236 // Native font info tests
238 bool HasNativeFontInfo() const { return m_bNativeFontInfoOk
; }
240 const wxNativeFontInfo
& GetNativeFontInfo() const
241 { return m_vNativeFontInfo
; }
245 // Common part of all ctors
252 ,const wxString
& rsFaceName
253 ,wxFontEncoding vEncoding
256 void Init( const wxNativeFontInfo
& rInfo
261 // If TRUE, the pointer to the actual font is temporary and SHOULD NOT BE
262 // DELETED by destructor
268 // Font characterstics
275 wxString m_sFaceName
;
276 wxFontEncoding m_vEncoding
;
282 wxNativeFontInfo m_vNativeFontInfo
;
283 bool m_bNativeFontInfoOk
;
286 // Some PM specific stuff
288 PFONTMETRICS m_pFM
; // array of FONTMETRICS structs
289 int m_nNumFonts
; // number of fonts in array
290 HPS m_hPS
; // PS handle this font belongs to
291 FATTRS m_vFattrs
; // Current fattrs struct
292 FACENAMEDESC m_vFname
; // Current facename struct
293 bool m_bInternalPS
; // Internally generated PS?
294 }; // end of CLASS wxFontRefData
296 // ============================================================================
298 // ============================================================================
300 // ----------------------------------------------------------------------------
302 // ----------------------------------------------------------------------------
304 void wxFontRefData::Init(
310 , const wxString
& rsFaceName
311 , wxFontEncoding vEncoding
315 m_nPointSize
= nPointSize
;
319 m_bUnderlined
= bUnderlined
;
320 m_sFaceName
= rsFaceName
;
321 m_vEncoding
= vEncoding
;
324 m_bNativeFontInfoOk
= FALSE
;
327 m_bTemporary
= FALSE
;
328 m_pFM
= (PFONTMETRICS
)NULL
;
331 } // end of wxFontRefData::Init
333 void wxFontRefData::Init(
334 const wxNativeFontInfo
& rInfo
335 , WXHFONT hFont
//this is the FontId -- functions as the hFont for OS/2
336 , WXHANDLE hPS
// Presentation Space we are using
340 // hFont may be zero, or it be passed in case we really want to
341 // use the exact font created in the underlying system
342 // (for example where we can't guarantee conversion from HFONT
343 // to LOGFONT back to HFONT)
346 m_nFontId
= (int)hFont
;
348 m_bNativeFontInfoOk
= TRUE
;
349 m_vNativeFontInfo
= rInfo
;
351 if (m_hPS
== NULLHANDLE
)
353 m_hPS
= ::WinGetPS(HWND_DESKTOP
);
360 wxFontRefData::~wxFontRefData()
365 bool wxFontRefData::Alloc(
373 PFONTMETRICS pFM
= NULL
;
375 if (!m_bNativeFontInfoOk
)
377 wxFillLogFont( &m_vNativeFontInfo
.fa
378 ,&m_vNativeFontInfo
.fn
385 m_bNativeFontInfoOk
= TRUE
;
388 if((lRc
= ::GpiCreateLogFont( m_hPS
391 ,&m_vNativeFontInfo
.fa
394 m_hFont
= (WXHFONT
)flId
;
399 wxLogLastError("CreateFont");
402 ::GpiSetCharSet(m_hPS
, flId
); // sets font for presentation space
403 ::GpiQueryFontMetrics(m_hPS
, sizeof(FONTMETRICS
), &m_vNativeFontInfo
.fm
);
406 // Set refData members with the results
408 memcpy(&m_vFattrs
, &m_vNativeFontInfo
.fa
, sizeof(m_vFattrs
));
409 memcpy(&m_vFname
, &m_vNativeFontInfo
.fn
, sizeof(m_vFname
));
411 // Going to leave the point size alone. Mostly we use outline fonts
412 // that can be set to any point size inside of Presentation Parameters,
413 // regardless of whether or not the actual font is registered in the system.
414 // The GpiCreateLogFont will do enough by selecting the right family,
417 if (strcmp(m_vNativeFontInfo
.fa
.szFacename
, "Times New Roman") == 0)
419 else if (strcmp(m_vNativeFontInfo
.fa
.szFacename
, "Tms Rmn") == 0)
421 else if (strcmp(m_vNativeFontInfo
.fa
.szFacename
, "WarpSans") == 0)
423 else if (strcmp(m_vNativeFontInfo
.fa
.szFacename
, "Helvitica") == 0)
425 else if (strcmp(m_vNativeFontInfo
.fa
.szFacename
, "Helv") == 0)
427 else if (strcmp(m_vNativeFontInfo
.fa
.szFacename
, "Script") == 0)
428 m_nFamily
= wxSCRIPT
;
429 else if (strcmp(m_vNativeFontInfo
.fa
.szFacename
, "Courier New") == 0)
430 m_nFamily
= wxTELETYPE
;
431 else if (strcmp(m_vNativeFontInfo
.fa
.szFacename
, "Courier") == 0)
432 m_nFamily
= wxTELETYPE
;
433 else if (strcmp(m_vNativeFontInfo
.fa
.szFacename
, "System VIO") == 0)
434 m_nFamily
= wxMODERN
;
438 if (m_vNativeFontInfo
.fa
.fsSelection
& FATTR_SEL_ITALIC
)
439 m_nStyle
= wxFONTSTYLE_ITALIC
;
441 m_nStyle
= wxFONTSTYLE_NORMAL
;
442 switch(m_vNativeFontInfo
.fn
.usWeightClass
)
444 case FWEIGHT_DONT_CARE
:
445 m_nWeight
= wxFONTWEIGHT_NORMAL
;
449 m_nWeight
= wxFONTWEIGHT_NORMAL
;
453 m_nWeight
= wxFONTWEIGHT_LIGHT
;
457 m_nWeight
= wxFONTWEIGHT_BOLD
;
460 case FWEIGHT_ULTRA_BOLD
:
461 m_nWeight
= wxFONTWEIGHT_MAX
;
465 m_nWeight
= wxFONTWEIGHT_NORMAL
;
467 m_bUnderlined
= ((m_vNativeFontInfo
.fa
.fsSelection
& FATTR_SEL_UNDERSCORE
) != 0);
468 m_sFaceName
= m_vNativeFontInfo
.fa
.szFacename
;
469 m_vEncoding
= wxGetFontEncFromCharSet(m_vNativeFontInfo
.fa
.usCodePage
);
472 // We don't actuall keep the font around if using a temporary PS
477 ::GpiDeleteSetId( m_hPS
481 ::WinReleasePS(m_hPS
);
485 // Select the font into the Presentation space
487 ::GpiSetCharSet(m_hPS
, flId
); // sets font for presentation space
489 } // end of wxFontRefData::Alloc
491 void wxFontRefData::Free()
495 m_pFM
= (PFONTMETRICS
)NULL
;
499 if (!::GpiSetCharSet(m_hPS
, LCID_DEFAULT
))
501 wxLogLastError(wxT("DeleteObject(font)"));
503 ::GpiDeleteSetId(m_hPS
, 1L); /* delete the logical font */
508 ::WinReleasePS(m_hPS
);
510 } // end of wxFontRefData::Free
512 // ----------------------------------------------------------------------------
514 // ----------------------------------------------------------------------------
516 void wxNativeFontInfo::Init()
518 memset(&fa
, '\0', sizeof(FATTRS
));
519 } // end of wxNativeFontInfo::Init
521 int wxNativeFontInfo::GetPointSize() const
524 } // end of wxNativeFontInfo::GetPointSize
526 wxFontStyle
wxNativeFontInfo::GetStyle() const
528 return fa
.fsSelection
& FATTR_SEL_ITALIC
? wxFONTSTYLE_ITALIC
: wxFONTSTYLE_NORMAL
;
529 } // end of wxNativeFontInfo::GetStyle
531 wxFontWeight
wxNativeFontInfo::GetWeight() const
533 switch(fn
.usWeightClass
)
535 case FWEIGHT_DONT_CARE
:
536 return wxFONTWEIGHT_NORMAL
;
539 return wxFONTWEIGHT_NORMAL
;
542 return wxFONTWEIGHT_LIGHT
;
545 return wxFONTWEIGHT_BOLD
;
547 case FWEIGHT_ULTRA_BOLD
:
548 return wxFONTWEIGHT_MAX
;
550 return wxFONTWEIGHT_NORMAL
;
551 } // end of wxNativeFontInfo::GetWeight
553 bool wxNativeFontInfo::GetUnderlined() const
555 return ((fa
.fsSelection
& FATTR_SEL_UNDERSCORE
) != 0);
556 } // end of wxNativeFontInfo::GetUnderlined
558 wxString
wxNativeFontInfo::GetFaceName() const
560 return fm
.szFacename
;
561 } // end of wxNativeFontInfo::GetFaceName
563 wxFontFamily
wxNativeFontInfo::GetFamily() const
568 // Extract family from facename
570 if (strcmp(fa
.szFacename
, "Times New Roman") == 0)
572 else if (strcmp(fa
.szFacename
, "WarpSans") == 0)
574 else if (strcmp(fa
.szFacename
, "Script") == 0)
576 else if (strcmp(fa
.szFacename
, "Courier New") == 0)
580 return (wxFontFamily
)nFamily
;
581 } // end of wxNativeFontInfo::GetFamily
583 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
585 return wxGetFontEncFromCharSet(fa
.usCodePage
);
586 } // end of wxNativeFontInfo::GetEncoding
588 void wxNativeFontInfo::SetPointSize(
592 fm
.lEmHeight
= (LONG
)nPointsize
;
593 } // end of wxNativeFontInfo::SetPointSize
595 void wxNativeFontInfo::SetStyle(
602 wxFAIL_MSG( _T("unknown font style") );
605 case wxFONTSTYLE_NORMAL
:
608 case wxFONTSTYLE_ITALIC
:
609 case wxFONTSTYLE_SLANT
:
610 fa
.fsSelection
|= FATTR_SEL_ITALIC
;
613 } // end of wxNativeFontInfo::SetStyle
615 void wxNativeFontInfo::SetWeight(
622 wxFAIL_MSG( _T("unknown font weight") );
625 case wxFONTWEIGHT_NORMAL
:
626 fn
.usWeightClass
= FWEIGHT_NORMAL
;
629 case wxFONTWEIGHT_LIGHT
:
630 fn
.usWeightClass
= FWEIGHT_LIGHT
;
633 case wxFONTWEIGHT_BOLD
:
634 fn
.usWeightClass
= FWEIGHT_BOLD
;
637 } // end of wxNativeFontInfo::SetWeight
639 void wxNativeFontInfo::SetUnderlined(
644 fa
.fsSelection
|= FATTR_SEL_UNDERSCORE
;
645 } // end of wxNativeFontInfo::SetUnderlined
647 void wxNativeFontInfo::SetFaceName(
651 wxStrncpy(fa
.szFacename
, sFacename
, WXSIZEOF(fa
.szFacename
));
652 } // end of wxNativeFontInfo::SetFaceName
654 void wxNativeFontInfo::SetFamily(
663 sFacename
= _T("Script");
667 sFacename
= _T("Times New Roman");
671 sFacename
= _T("Times New Roman");
676 sFacename
= _T("Courier New");
680 sFacename
= _T("WarpSans");
685 sFacename
= _T("Helv");
688 if (!wxStrlen(fa
.szFacename
) )
690 SetFaceName(sFacename
);
692 } // end of wxNativeFontInfo::SetFamily
694 void wxNativeFontInfo::SetEncoding(
695 wxFontEncoding eEncoding
698 wxNativeEncodingInfo vInfo
;
700 if ( !wxGetNativeFontEncoding( eEncoding
705 if (wxTheFontMapper
->GetAltForEncoding( eEncoding
709 if (!vInfo
.facename
.empty())
712 // If we have this encoding only in some particular facename, use
713 // the facename - it is better to show the correct characters in a
714 // wrong facename than unreadable text in a correct one
716 SetFaceName(vInfo
.facename
);
720 #endif // wxUSE_FONTMAP
722 // unsupported encoding, replace with the default
726 fa
.usCodePage
= vInfo
.charset
;
727 } // end of wxNativeFontInfo::SetFaceName
729 bool wxNativeFontInfo::FromString(
730 const wxString
& rsStr
735 wxStringTokenizer
vTokenizer(rsStr
, _T(";"));
740 wxString sToken
= vTokenizer
.GetNextToken();
742 if (sToken
!= _T('0'))
745 sToken
= vTokenizer
.GetNextToken();
746 if (!sToken
.ToLong(&lVal
))
750 sToken
= vTokenizer
.GetNextToken();
751 if (!sToken
.ToLong(&lVal
))
753 fa
.lAveCharWidth
= lVal
;
755 sToken
= vTokenizer
.GetNextToken();
756 if (!sToken
.ToLong(&lVal
))
758 fa
.fsSelection
= (USHORT
)lVal
;
760 sToken
= vTokenizer
.GetNextToken();
761 if (!sToken
.ToLong(&lVal
))
763 fa
.fsType
= (USHORT
)lVal
;
765 sToken
= vTokenizer
.GetNextToken();
766 if (!sToken
.ToLong(&lVal
))
768 fa
.fsFontUse
= (USHORT
)lVal
;
770 sToken
= vTokenizer
.GetNextToken();
771 if (!sToken
.ToLong(&lVal
))
773 fa
.idRegistry
= (USHORT
)lVal
;
775 sToken
= vTokenizer
.GetNextToken();
776 if (!sToken
.ToLong(&lVal
))
778 fa
.usCodePage
= (USHORT
)lVal
;
780 sToken
= vTokenizer
.GetNextToken();
781 if (!sToken
.ToLong(&lVal
))
785 sToken
= vTokenizer
.GetNextToken();
786 if (!sToken
.ToLong(&lVal
))
788 fn
.usWeightClass
= (USHORT
)lVal
;
790 sToken
= vTokenizer
.GetNextToken();
793 wxStrcpy(fa
.szFacename
, sToken
.c_str());
795 } // end of wxNativeFontInfo::FromString
797 wxString
wxNativeFontInfo::ToString() const
801 sStr
.Printf(_T("%d;%ld;%ld;%ld;%d;%d;%d;%d;%d;%ld;%d;%s"),
802 0, // version, in case we want to change the format later
815 } // end of wxNativeFontInfo::ToString
817 // ----------------------------------------------------------------------------
819 // ----------------------------------------------------------------------------
823 } // end of wxFont::Init
826 const wxNativeFontInfo
& rInfo
831 m_refData
= new wxFontRefData( rInfo
836 } // end of wxFont::Create
839 const wxString
& rsFontdesc
842 wxNativeFontInfo vInfo
;
844 if (vInfo
.FromString(rsFontdesc
))
846 } // end of wxFont::wxFont
848 // ----------------------------------------------------------------------------
849 // Constructor for a font. Note that the real construction is done
850 // in wxDC::SetFont, when information is available about scaling etc.
851 // ----------------------------------------------------------------------------
858 , const wxString
& rsFaceName
859 , wxFontEncoding vEncoding
865 // wxDEFAULT is a valid value for the font size too so we must treat it
866 // specially here (otherwise the size would be 70 == wxDEFAULT value)
868 if (nPointSize
== wxDEFAULT
)
870 nPointSize
= wxNORMAL_FONT
->GetPointSize();
872 m_refData
= new wxFontRefData( nPointSize
882 } // end of wxFont::Create
886 } // end of wxFont::~wxFont
888 // ----------------------------------------------------------------------------
889 // real implementation
890 // Boris' Kovalenko comments:
891 // Because OS/2 fonts are associated with PS we can not create the font
892 // here, but we may check that font definition is true
893 // ----------------------------------------------------------------------------
895 bool wxFont::RealizeResource()
897 if ( GetResourceHandle() )
901 return M_FONTDATA
->Alloc(this);
902 } // end of wxFont::RealizeResource
904 bool wxFont::FreeResource(
908 if (GetResourceHandle())
914 } // end of wxFont::FreeResource
916 WXHANDLE
wxFont::GetResourceHandle()
919 } // end of wxFont::GetResourceHandle
921 WXHFONT
wxFont::GetHFONT() const
923 return M_FONTDATA
? M_FONTDATA
->GetHFONT() : 0;
924 } // end of wxFont::GetHFONT
926 bool wxFont::IsFree() const
928 return M_FONTDATA
&& (M_FONTDATA
->GetHFONT() == 0);
929 } // end of wxFont::IsFree
931 void wxFont::Unshare()
933 // Don't change shared data
936 m_refData
= new wxFontRefData();
940 wxFontRefData
* ref
= new wxFontRefData(*M_FONTDATA
);
944 } // end of wxFont::Unshare
946 // ----------------------------------------------------------------------------
947 // change font attribute: we recreate font when doing it
948 // ----------------------------------------------------------------------------
950 void wxFont::SetPointSize(
956 M_FONTDATA
->SetPointSize(nPointSize
);
959 } // end of wxFont::SetPointSize
961 void wxFont::SetFamily(
967 M_FONTDATA
->SetFamily(nFamily
);
970 } // end of wxFont::SetFamily
972 void wxFont::SetStyle(
978 M_FONTDATA
->SetStyle(nStyle
);
981 } // end of wxFont::SetStyle
983 void wxFont::SetWeight(
989 M_FONTDATA
->SetWeight(nWeight
);
992 } // end of wxFont::SetWeight
994 void wxFont::SetFaceName(
995 const wxString
& rsFaceName
1000 M_FONTDATA
->SetFaceName(rsFaceName
);
1003 } // end of wxFont::SetFaceName
1005 void wxFont::SetUnderlined(
1011 M_FONTDATA
->SetUnderlined(bUnderlined
);
1014 } // end of wxFont::SetUnderlined
1016 void wxFont::SetEncoding(
1017 wxFontEncoding vEncoding
1022 M_FONTDATA
->SetEncoding(vEncoding
);
1025 } // end of wxFont::SetEncoding
1027 void wxFont::SetNativeFontInfo(
1028 const wxNativeFontInfo
& rInfo
1035 *M_FONTDATA
= wxFontRefData(rInfo
);
1040 // ----------------------------------------------------------------------------
1042 // ----------------------------------------------------------------------------
1044 int wxFont::GetPointSize() const
1046 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
1048 return M_FONTDATA
->GetPointSize();
1049 } // end of wxFont::GetPointSize
1051 int wxFont::GetFamily() const
1053 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
1055 return M_FONTDATA
->GetFamily();
1056 } // end of wxFont::GetFamily
1058 int wxFont::GetStyle() const
1060 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
1062 return M_FONTDATA
->GetStyle();
1063 } // end of wxFont::GetStyle
1065 int wxFont::GetWeight() const
1067 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
1069 return M_FONTDATA
->GetWeight();
1072 bool wxFont::GetUnderlined() const
1074 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid font") );
1076 return M_FONTDATA
->GetUnderlined();
1077 } // end of wxFont::GetUnderlined
1079 wxString
wxFont::GetFaceName() const
1081 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
1083 return M_FONTDATA
->GetFaceName();
1084 } // end of wxFont::GetFaceName
1086 wxFontEncoding
wxFont::GetEncoding() const
1088 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
1090 return M_FONTDATA
->GetEncoding();
1091 } // end of wxFont::GetEncoding
1093 wxNativeFontInfo
* wxFont::GetNativeFontInfo() const
1095 if (M_FONTDATA
->HasNativeFontInfo())
1096 return new wxNativeFontInfo(M_FONTDATA
->GetNativeFontInfo());
1098 } // end of wxFont::GetNativeFontInfo
1101 // Internal use only method to set the FONTMETRICS array
1108 M_FONTDATA
->SetFM(pFM
);
1109 M_FONTDATA
->SetNumFonts(nNumFonts
);
1110 } // end of wxFont::SetFM
1119 M_FONTDATA
->SetPS(hPS
);
1122 } // end of wxFont::SetUnderlined