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
377 m_bNativeFontInfoOk
= TRUE
;
380 if(::GpiCreateLogFont( m_hPS
383 ,&m_vNativeFontInfo
.fa
385 m_hFont
= (WXHFONT
)flId
;
390 wxLogLastError("CreateFont");
394 // Query for the actual metrics of the current font being used
396 ::GpiQueryFontMetrics(m_hPS
, sizeof(FONTMETRICS
), &m_vNativeFontInfo
.fm
);
399 // Set refData members with the results
401 memcpy(&m_vFattrs
, &m_vNativeFontInfo
.fa
, sizeof(m_vFattrs
));
402 memcpy(&m_vFname
, &m_vNativeFontInfo
.fn
, sizeof(m_vFname
));
403 m_nPointSize
= m_vNativeFontInfo
.fm
.lEmHeight
;
404 if (strcmp(m_vNativeFontInfo
.fa
.szFacename
, "Times New Roman") == 0)
406 else if (strcmp(m_vNativeFontInfo
.fa
.szFacename
, "WarpSans") == 0)
408 else if (strcmp(m_vNativeFontInfo
.fa
.szFacename
, "Script") == 0)
409 m_nFamily
= wxSCRIPT
;
410 else if (strcmp(m_vNativeFontInfo
.fa
.szFacename
, "Courier New") == 0)
411 m_nFamily
= wxMODERN
;
412 else if (strcmp(m_vNativeFontInfo
.fa
.szFacename
, "Courier") == 0)
413 m_nFamily
= wxMODERN
;
417 if (m_vNativeFontInfo
.fa
.fsSelection
& FATTR_SEL_ITALIC
)
418 m_nStyle
= wxFONTSTYLE_ITALIC
;
420 m_nStyle
= wxFONTSTYLE_NORMAL
;
421 switch(m_vNativeFontInfo
.fn
.usWeightClass
)
423 case FWEIGHT_DONT_CARE
:
424 m_nWeight
= wxFONTWEIGHT_NORMAL
;
428 m_nWeight
= wxFONTWEIGHT_NORMAL
;
432 m_nWeight
= wxFONTWEIGHT_LIGHT
;
436 m_nWeight
= wxFONTWEIGHT_BOLD
;
439 case FWEIGHT_ULTRA_BOLD
:
440 m_nWeight
= wxFONTWEIGHT_MAX
;
444 m_nWeight
= wxFONTWEIGHT_NORMAL
;
446 m_bUnderlined
= ((m_vNativeFontInfo
.fa
.fsSelection
& FATTR_SEL_UNDERSCORE
) != 0);
447 m_sFaceName
= m_vNativeFontInfo
.fa
.szFacename
;
448 m_vEncoding
= wxGetFontEncFromCharSet(m_vNativeFontInfo
.fa
.usCodePage
);
451 // We don't actuall keep the font around if using a temporary PS
456 ::GpiDeleteSetId( m_hPS
460 ::WinReleasePS(m_hPS
);
464 // Select the font into the Presentation space
466 ::GpiSetCharSet(m_hPS
, flId
); // sets font for presentation space
468 } // end of wxFontRefData::Alloc
470 void wxFontRefData::Free()
474 m_pFM
= (PFONTMETRICS
)NULL
;
478 if (!::GpiSetCharSet(m_hPS
, LCID_DEFAULT
))
480 wxLogLastError(wxT("DeleteObject(font)"));
482 ::GpiDeleteSetId(m_hPS
, 1L); /* delete the logical font */
487 ::WinReleasePS(m_hPS
);
489 } // end of wxFontRefData::Free
491 // ----------------------------------------------------------------------------
493 // ----------------------------------------------------------------------------
495 void wxNativeFontInfo::Init()
497 memset(&fa
, '\0', sizeof(FATTRS
));
498 } // end of wxNativeFontInfo::Init
500 int wxNativeFontInfo::GetPointSize() const
503 } // end of wxNativeFontInfo::GetPointSize
505 wxFontStyle
wxNativeFontInfo::GetStyle() const
507 return fa
.fsSelection
& FATTR_SEL_ITALIC
? wxFONTSTYLE_ITALIC
: wxFONTSTYLE_NORMAL
;
508 } // end of wxNativeFontInfo::GetStyle
510 wxFontWeight
wxNativeFontInfo::GetWeight() const
512 switch(fn
.usWeightClass
)
514 case FWEIGHT_DONT_CARE
:
515 return wxFONTWEIGHT_NORMAL
;
518 return wxFONTWEIGHT_NORMAL
;
521 return wxFONTWEIGHT_LIGHT
;
524 return wxFONTWEIGHT_BOLD
;
526 case FWEIGHT_ULTRA_BOLD
:
527 return wxFONTWEIGHT_MAX
;
529 return wxFONTWEIGHT_NORMAL
;
530 } // end of wxNativeFontInfo::GetWeight
532 bool wxNativeFontInfo::GetUnderlined() const
534 return ((fa
.fsSelection
& FATTR_SEL_UNDERSCORE
) != 0);
535 } // end of wxNativeFontInfo::GetUnderlined
537 wxString
wxNativeFontInfo::GetFaceName() const
539 return fm
.szFacename
;
540 } // end of wxNativeFontInfo::GetFaceName
542 wxFontFamily
wxNativeFontInfo::GetFamily() const
547 // Extract family from facename
549 if (strcmp(fa
.szFacename
, "Times New Roman") == 0)
551 else if (strcmp(fa
.szFacename
, "WarpSans") == 0)
553 else if (strcmp(fa
.szFacename
, "Script") == 0)
555 else if (strcmp(fa
.szFacename
, "Courier New") == 0)
559 return (wxFontFamily
)nFamily
;
560 } // end of wxNativeFontInfo::GetFamily
562 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
564 return wxGetFontEncFromCharSet(fa
.usCodePage
);
565 } // end of wxNativeFontInfo::GetEncoding
567 void wxNativeFontInfo::SetPointSize(
571 fm
.lEmHeight
= (LONG
)nPointsize
;
572 } // end of wxNativeFontInfo::SetPointSize
574 void wxNativeFontInfo::SetStyle(
581 wxFAIL_MSG( _T("unknown font style") );
584 case wxFONTSTYLE_NORMAL
:
587 case wxFONTSTYLE_ITALIC
:
588 case wxFONTSTYLE_SLANT
:
589 fa
.fsSelection
|= FATTR_SEL_ITALIC
;
592 } // end of wxNativeFontInfo::SetStyle
594 void wxNativeFontInfo::SetWeight(
601 wxFAIL_MSG( _T("unknown font weight") );
604 case wxFONTWEIGHT_NORMAL
:
605 fn
.usWeightClass
= FWEIGHT_NORMAL
;
608 case wxFONTWEIGHT_LIGHT
:
609 fn
.usWeightClass
= FWEIGHT_LIGHT
;
612 case wxFONTWEIGHT_BOLD
:
613 fn
.usWeightClass
= FWEIGHT_BOLD
;
616 } // end of wxNativeFontInfo::SetWeight
618 void wxNativeFontInfo::SetUnderlined(
623 fa
.fsSelection
|= FATTR_SEL_UNDERSCORE
;
624 } // end of wxNativeFontInfo::SetUnderlined
626 void wxNativeFontInfo::SetFaceName(
630 wxStrncpy(fa
.szFacename
, sFacename
, WXSIZEOF(fa
.szFacename
));
631 } // end of wxNativeFontInfo::SetFaceName
633 void wxNativeFontInfo::SetFamily(
642 sFacename
= _T("Script");
646 sFacename
= _T("Times New Roman");
650 sFacename
= _T("Times New Roman");
655 sFacename
= _T("Courier New");
659 sFacename
= _T("WarpSans");
664 sFacename
= _T("Helv");
667 if (!wxStrlen(fa
.szFacename
) )
669 SetFaceName(sFacename
);
671 } // end of wxNativeFontInfo::SetFamily
673 void wxNativeFontInfo::SetEncoding(
674 wxFontEncoding eEncoding
677 wxNativeEncodingInfo vInfo
;
679 if ( !wxGetNativeFontEncoding( eEncoding
684 if (wxTheFontMapper
->GetAltForEncoding( eEncoding
688 if (!vInfo
.facename
.empty())
691 // If we have this encoding only in some particular facename, use
692 // the facename - it is better to show the correct characters in a
693 // wrong facename than unreadable text in a correct one
695 SetFaceName(vInfo
.facename
);
699 #endif // wxUSE_FONTMAP
701 // unsupported encoding, replace with the default
705 fa
.usCodePage
= vInfo
.charset
;
706 } // end of wxNativeFontInfo::SetFaceName
708 bool wxNativeFontInfo::FromString(
709 const wxString
& rsStr
714 wxStringTokenizer
vTokenizer(rsStr
, _T(";"));
719 wxString sToken
= vTokenizer
.GetNextToken();
721 if (sToken
!= _T('0'))
724 sToken
= vTokenizer
.GetNextToken();
725 if (!sToken
.ToLong(&lVal
))
729 sToken
= vTokenizer
.GetNextToken();
730 if (!sToken
.ToLong(&lVal
))
732 fa
.lAveCharWidth
= lVal
;
734 sToken
= vTokenizer
.GetNextToken();
735 if (!sToken
.ToLong(&lVal
))
737 fa
.fsSelection
= (USHORT
)lVal
;
739 sToken
= vTokenizer
.GetNextToken();
740 if (!sToken
.ToLong(&lVal
))
742 fa
.fsType
= (USHORT
)lVal
;
744 sToken
= vTokenizer
.GetNextToken();
745 if (!sToken
.ToLong(&lVal
))
747 fa
.fsFontUse
= (USHORT
)lVal
;
749 sToken
= vTokenizer
.GetNextToken();
750 if (!sToken
.ToLong(&lVal
))
752 fa
.idRegistry
= (USHORT
)lVal
;
754 sToken
= vTokenizer
.GetNextToken();
755 if (!sToken
.ToLong(&lVal
))
757 fa
.usCodePage
= (USHORT
)lVal
;
759 sToken
= vTokenizer
.GetNextToken();
760 if (!sToken
.ToLong(&lVal
))
764 sToken
= vTokenizer
.GetNextToken();
765 if (!sToken
.ToLong(&lVal
))
767 fn
.usWeightClass
= (USHORT
)lVal
;
769 sToken
= vTokenizer
.GetNextToken();
772 wxStrcpy(fa
.szFacename
, sToken
.c_str());
774 } // end of wxNativeFontInfo::FromString
776 wxString
wxNativeFontInfo::ToString() const
780 sStr
.Printf(_T("%d;%ld;%ld;%ld;%d;%d;%d;%d;%d;%ld;%d;%s"),
781 0, // version, in case we want to change the format later
794 } // end of wxNativeFontInfo::ToString
796 // ----------------------------------------------------------------------------
798 // ----------------------------------------------------------------------------
802 } // end of wxFont::Init
805 const wxNativeFontInfo
& rInfo
810 m_refData
= new wxFontRefData( rInfo
815 } // end of wxFont::Create
818 const wxString
& rsFontdesc
821 wxNativeFontInfo vInfo
;
823 if (vInfo
.FromString(rsFontdesc
))
825 } // end of wxFont::wxFont
827 // ----------------------------------------------------------------------------
828 // Constructor for a font. Note that the real construction is done
829 // in wxDC::SetFont, when information is available about scaling etc.
830 // ----------------------------------------------------------------------------
837 , const wxString
& rsFaceName
838 , wxFontEncoding vEncoding
844 // wxDEFAULT is a valid value for the font size too so we must treat it
845 // specially here (otherwise the size would be 70 == wxDEFAULT value)
847 if (nPointSize
== wxDEFAULT
)
849 nPointSize
= wxNORMAL_FONT
->GetPointSize();
851 m_refData
= new wxFontRefData( nPointSize
861 } // end of wxFont::Create
865 } // end of wxFont::~wxFont
867 // ----------------------------------------------------------------------------
868 // real implementation
869 // Boris' Kovalenko comments:
870 // Because OS/2 fonts are associated with PS we can not create the font
871 // here, but we may check that font definition is true
872 // ----------------------------------------------------------------------------
874 bool wxFont::RealizeResource()
876 if ( GetResourceHandle() )
880 return M_FONTDATA
->Alloc(this);
881 } // end of wxFont::RealizeResource
883 bool wxFont::FreeResource(
887 if (GetResourceHandle())
893 } // end of wxFont::FreeResource
895 WXHANDLE
wxFont::GetResourceHandle()
898 } // end of wxFont::GetResourceHandle
900 WXHFONT
wxFont::GetHFONT() const
902 return M_FONTDATA
? M_FONTDATA
->GetHFONT() : 0;
903 } // end of wxFont::GetHFONT
905 bool wxFont::IsFree() const
907 return M_FONTDATA
&& (M_FONTDATA
->GetHFONT() == 0);
908 } // end of wxFont::IsFree
910 void wxFont::Unshare()
912 // Don't change shared data
915 m_refData
= new wxFontRefData();
919 wxFontRefData
* ref
= new wxFontRefData(*M_FONTDATA
);
923 } // end of wxFont::Unshare
925 // ----------------------------------------------------------------------------
926 // change font attribute: we recreate font when doing it
927 // ----------------------------------------------------------------------------
929 void wxFont::SetPointSize(
935 M_FONTDATA
->SetPointSize(nPointSize
);
938 } // end of wxFont::SetPointSize
940 void wxFont::SetFamily(
946 M_FONTDATA
->SetFamily(nFamily
);
949 } // end of wxFont::SetFamily
951 void wxFont::SetStyle(
957 M_FONTDATA
->SetStyle(nStyle
);
960 } // end of wxFont::SetStyle
962 void wxFont::SetWeight(
968 M_FONTDATA
->SetWeight(nWeight
);
971 } // end of wxFont::SetWeight
973 void wxFont::SetFaceName(
974 const wxString
& rsFaceName
979 M_FONTDATA
->SetFaceName(rsFaceName
);
982 } // end of wxFont::SetFaceName
984 void wxFont::SetUnderlined(
990 M_FONTDATA
->SetUnderlined(bUnderlined
);
993 } // end of wxFont::SetUnderlined
995 void wxFont::SetEncoding(
996 wxFontEncoding vEncoding
1001 M_FONTDATA
->SetEncoding(vEncoding
);
1004 } // end of wxFont::SetEncoding
1006 void wxFont::SetNativeFontInfo(
1007 const wxNativeFontInfo
& rInfo
1014 *M_FONTDATA
= wxFontRefData(rInfo
);
1019 // ----------------------------------------------------------------------------
1021 // ----------------------------------------------------------------------------
1023 int wxFont::GetPointSize() const
1025 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
1027 return M_FONTDATA
->GetPointSize();
1028 } // end of wxFont::GetPointSize
1030 int wxFont::GetFamily() const
1032 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
1034 return M_FONTDATA
->GetFamily();
1035 } // end of wxFont::GetFamily
1037 int wxFont::GetStyle() const
1039 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
1041 return M_FONTDATA
->GetStyle();
1042 } // end of wxFont::GetStyle
1044 int wxFont::GetWeight() const
1046 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
1048 return M_FONTDATA
->GetWeight();
1051 bool wxFont::GetUnderlined() const
1053 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid font") );
1055 return M_FONTDATA
->GetUnderlined();
1056 } // end of wxFont::GetUnderlined
1058 wxString
wxFont::GetFaceName() const
1060 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
1062 return M_FONTDATA
->GetFaceName();
1063 } // end of wxFont::GetFaceName
1065 wxFontEncoding
wxFont::GetEncoding() const
1067 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
1069 return M_FONTDATA
->GetEncoding();
1070 } // end of wxFont::GetEncoding
1072 wxNativeFontInfo
* wxFont::GetNativeFontInfo() const
1074 if (M_FONTDATA
->HasNativeFontInfo())
1075 return new wxNativeFontInfo(M_FONTDATA
->GetNativeFontInfo());
1077 } // end of wxFont::GetNativeFontInfo
1080 // Internal use only method to set the FONTMETRICS array
1087 M_FONTDATA
->SetFM(pFM
);
1088 M_FONTDATA
->SetNumFonts(nNumFonts
);
1089 } // end of wxFont::SetFM
1098 M_FONTDATA
->SetPS(hPS
);
1101 } // end of wxFont::SetUnderlined