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
704 if (wxFontMapper::Get()->GetAltForEncoding( eEncoding
708 if (!vInfo
.facename
.empty())
711 // If we have this encoding only in some particular facename, use
712 // the facename - it is better to show the correct characters in a
713 // wrong facename than unreadable text in a correct one
715 SetFaceName(vInfo
.facename
);
720 // unsupported encoding, replace with the default
724 fa
.usCodePage
= vInfo
.charset
;
725 } // end of wxNativeFontInfo::SetFaceName
727 bool wxNativeFontInfo::FromString(
728 const wxString
& rsStr
733 wxStringTokenizer
vTokenizer(rsStr
, _T(";"));
738 wxString sToken
= vTokenizer
.GetNextToken();
740 if (sToken
!= _T('0'))
743 sToken
= vTokenizer
.GetNextToken();
744 if (!sToken
.ToLong(&lVal
))
748 sToken
= vTokenizer
.GetNextToken();
749 if (!sToken
.ToLong(&lVal
))
751 fa
.lAveCharWidth
= lVal
;
753 sToken
= vTokenizer
.GetNextToken();
754 if (!sToken
.ToLong(&lVal
))
756 fa
.fsSelection
= (USHORT
)lVal
;
758 sToken
= vTokenizer
.GetNextToken();
759 if (!sToken
.ToLong(&lVal
))
761 fa
.fsType
= (USHORT
)lVal
;
763 sToken
= vTokenizer
.GetNextToken();
764 if (!sToken
.ToLong(&lVal
))
766 fa
.fsFontUse
= (USHORT
)lVal
;
768 sToken
= vTokenizer
.GetNextToken();
769 if (!sToken
.ToLong(&lVal
))
771 fa
.idRegistry
= (USHORT
)lVal
;
773 sToken
= vTokenizer
.GetNextToken();
774 if (!sToken
.ToLong(&lVal
))
776 fa
.usCodePage
= (USHORT
)lVal
;
778 sToken
= vTokenizer
.GetNextToken();
779 if (!sToken
.ToLong(&lVal
))
783 sToken
= vTokenizer
.GetNextToken();
784 if (!sToken
.ToLong(&lVal
))
786 fn
.usWeightClass
= (USHORT
)lVal
;
788 sToken
= vTokenizer
.GetNextToken();
791 wxStrcpy(fa
.szFacename
, sToken
.c_str());
793 } // end of wxNativeFontInfo::FromString
795 wxString
wxNativeFontInfo::ToString() const
799 sStr
.Printf(_T("%d;%ld;%ld;%ld;%d;%d;%d;%d;%d;%ld;%d;%s"),
800 0, // version, in case we want to change the format later
813 } // end of wxNativeFontInfo::ToString
815 // ----------------------------------------------------------------------------
817 // ----------------------------------------------------------------------------
821 } // end of wxFont::Init
824 const wxNativeFontInfo
& rInfo
829 m_refData
= new wxFontRefData( rInfo
834 } // end of wxFont::Create
837 const wxString
& rsFontdesc
840 wxNativeFontInfo vInfo
;
842 if (vInfo
.FromString(rsFontdesc
))
844 } // end of wxFont::wxFont
846 // ----------------------------------------------------------------------------
847 // Constructor for a font. Note that the real construction is done
848 // in wxDC::SetFont, when information is available about scaling etc.
849 // ----------------------------------------------------------------------------
856 , const wxString
& rsFaceName
857 , wxFontEncoding vEncoding
863 // wxDEFAULT is a valid value for the font size too so we must treat it
864 // specially here (otherwise the size would be 70 == wxDEFAULT value)
866 if (nPointSize
== wxDEFAULT
)
868 nPointSize
= wxNORMAL_FONT
->GetPointSize();
870 m_refData
= new wxFontRefData( nPointSize
880 } // end of wxFont::Create
884 } // end of wxFont::~wxFont
886 // ----------------------------------------------------------------------------
887 // real implementation
888 // Boris' Kovalenko comments:
889 // Because OS/2 fonts are associated with PS we can not create the font
890 // here, but we may check that font definition is true
891 // ----------------------------------------------------------------------------
893 bool wxFont::RealizeResource()
895 if ( GetResourceHandle() )
899 return M_FONTDATA
->Alloc(this);
900 } // end of wxFont::RealizeResource
902 bool wxFont::FreeResource(
906 if (GetResourceHandle())
912 } // end of wxFont::FreeResource
914 WXHANDLE
wxFont::GetResourceHandle()
917 } // end of wxFont::GetResourceHandle
919 WXHFONT
wxFont::GetHFONT() const
921 return M_FONTDATA
? M_FONTDATA
->GetHFONT() : 0;
922 } // end of wxFont::GetHFONT
924 bool wxFont::IsFree() const
926 return M_FONTDATA
&& (M_FONTDATA
->GetHFONT() == 0);
927 } // end of wxFont::IsFree
929 void wxFont::Unshare()
931 // Don't change shared data
934 m_refData
= new wxFontRefData();
938 wxFontRefData
* ref
= new wxFontRefData(*M_FONTDATA
);
942 } // end of wxFont::Unshare
944 // ----------------------------------------------------------------------------
945 // change font attribute: we recreate font when doing it
946 // ----------------------------------------------------------------------------
948 void wxFont::SetPointSize(
954 M_FONTDATA
->SetPointSize(nPointSize
);
957 } // end of wxFont::SetPointSize
959 void wxFont::SetFamily(
965 M_FONTDATA
->SetFamily(nFamily
);
968 } // end of wxFont::SetFamily
970 void wxFont::SetStyle(
976 M_FONTDATA
->SetStyle(nStyle
);
979 } // end of wxFont::SetStyle
981 void wxFont::SetWeight(
987 M_FONTDATA
->SetWeight(nWeight
);
990 } // end of wxFont::SetWeight
992 void wxFont::SetFaceName(
993 const wxString
& rsFaceName
998 M_FONTDATA
->SetFaceName(rsFaceName
);
1001 } // end of wxFont::SetFaceName
1003 void wxFont::SetUnderlined(
1009 M_FONTDATA
->SetUnderlined(bUnderlined
);
1012 } // end of wxFont::SetUnderlined
1014 void wxFont::SetEncoding(
1015 wxFontEncoding vEncoding
1020 M_FONTDATA
->SetEncoding(vEncoding
);
1023 } // end of wxFont::SetEncoding
1025 void wxFont::SetNativeFontInfo(
1026 const wxNativeFontInfo
& rInfo
1033 *M_FONTDATA
= wxFontRefData(rInfo
);
1038 // ----------------------------------------------------------------------------
1040 // ----------------------------------------------------------------------------
1042 int wxFont::GetPointSize() const
1044 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
1046 return M_FONTDATA
->GetPointSize();
1047 } // end of wxFont::GetPointSize
1049 int wxFont::GetFamily() const
1051 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
1053 return M_FONTDATA
->GetFamily();
1054 } // end of wxFont::GetFamily
1056 int wxFont::GetStyle() const
1058 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
1060 return M_FONTDATA
->GetStyle();
1061 } // end of wxFont::GetStyle
1063 int wxFont::GetWeight() const
1065 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
1067 return M_FONTDATA
->GetWeight();
1070 bool wxFont::GetUnderlined() const
1072 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid font") );
1074 return M_FONTDATA
->GetUnderlined();
1075 } // end of wxFont::GetUnderlined
1077 wxString
wxFont::GetFaceName() const
1079 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
1081 return M_FONTDATA
->GetFaceName();
1082 } // end of wxFont::GetFaceName
1084 wxFontEncoding
wxFont::GetEncoding() const
1086 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
1088 return M_FONTDATA
->GetEncoding();
1089 } // end of wxFont::GetEncoding
1091 wxNativeFontInfo
* wxFont::GetNativeFontInfo() const
1093 if (M_FONTDATA
->HasNativeFontInfo())
1094 return new wxNativeFontInfo(M_FONTDATA
->GetNativeFontInfo());
1096 } // end of wxFont::GetNativeFontInfo
1099 // Internal use only method to set the FONTMETRICS array
1106 M_FONTDATA
->SetFM(pFM
);
1107 M_FONTDATA
->SetNumFonts(nNumFonts
);
1108 } // end of wxFont::SetFM
1117 M_FONTDATA
->SetPS(hPS
);
1120 } // end of wxFont::SetUnderlined