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
;
431 if (m_vNativeFontInfo
.fa
.fsSelection
& FATTR_SEL_ITALIC
)
432 m_nStyle
= wxFONTSTYLE_ITALIC
;
434 m_nStyle
= wxFONTSTYLE_NORMAL
;
435 switch(m_vNativeFontInfo
.fn
.usWeightClass
)
437 case FWEIGHT_DONT_CARE
:
438 m_nWeight
= wxFONTWEIGHT_NORMAL
;
442 m_nWeight
= wxFONTWEIGHT_NORMAL
;
446 m_nWeight
= wxFONTWEIGHT_LIGHT
;
450 m_nWeight
= wxFONTWEIGHT_BOLD
;
453 case FWEIGHT_ULTRA_BOLD
:
454 m_nWeight
= wxFONTWEIGHT_MAX
;
458 m_nWeight
= wxFONTWEIGHT_NORMAL
;
460 m_bUnderlined
= ((m_vNativeFontInfo
.fa
.fsSelection
& FATTR_SEL_UNDERSCORE
) != 0);
461 m_sFaceName
= m_vNativeFontInfo
.fa
.szFacename
;
462 m_vEncoding
= wxGetFontEncFromCharSet(m_vNativeFontInfo
.fa
.usCodePage
);
464 } // end of wxFontRefData::Alloc
466 void wxFontRefData
::Free()
470 m_pFM
= (PFONTMETRICS
)NULL
;
474 if (!::GpiSetCharSet(m_hPS
, LCID_DEFAULT
))
476 wxLogLastError(wxT("DeleteObject(font)"));
478 ::GpiDeleteSetId(m_hPS
, 1L); /* delete the logical font */
483 ::WinReleasePS(m_hPS
);
485 } // end of wxFontRefData::Free
487 // ----------------------------------------------------------------------------
489 // ----------------------------------------------------------------------------
491 void wxNativeFontInfo
::Init()
493 memset(&fa
, '\0', sizeof(FATTRS
));
494 } // end of wxNativeFontInfo::Init
496 int wxNativeFontInfo
::GetPointSize() const
499 } // end of wxNativeFontInfo::GetPointSize
501 wxFontStyle wxNativeFontInfo
::GetStyle() const
503 return fa
.fsSelection
& FATTR_SEL_ITALIC ? wxFONTSTYLE_ITALIC
: wxFONTSTYLE_NORMAL
;
504 } // end of wxNativeFontInfo::GetStyle
506 wxFontWeight wxNativeFontInfo
::GetWeight() const
508 switch(fn
.usWeightClass
)
510 case FWEIGHT_DONT_CARE
:
511 return wxFONTWEIGHT_NORMAL
;
514 return wxFONTWEIGHT_NORMAL
;
517 return wxFONTWEIGHT_LIGHT
;
520 return wxFONTWEIGHT_BOLD
;
522 case FWEIGHT_ULTRA_BOLD
:
523 return wxFONTWEIGHT_MAX
;
525 return wxFONTWEIGHT_NORMAL
;
526 } // end of wxNativeFontInfo::GetWeight
528 bool wxNativeFontInfo
::GetUnderlined() const
530 return ((fa
.fsSelection
& FATTR_SEL_UNDERSCORE
) != 0);
531 } // end of wxNativeFontInfo::GetUnderlined
533 wxString wxNativeFontInfo
::GetFaceName() const
535 return fm
.szFacename
;
536 } // end of wxNativeFontInfo::GetFaceName
538 wxFontFamily wxNativeFontInfo
::GetFamily() const
543 // Extract family from facename
545 if (strcmp(fa
.szFacename
, "Times New Roman") == 0)
547 else if (strcmp(fa
.szFacename
, "WarpSans") == 0)
549 else if (strcmp(fa
.szFacename
, "Script") == 0)
551 else if (strcmp(fa
.szFacename
, "Courier New") == 0)
555 return (wxFontFamily
)nFamily
;
556 } // end of wxNativeFontInfo::GetFamily
558 wxFontEncoding wxNativeFontInfo
::GetEncoding() const
560 return wxGetFontEncFromCharSet(fa
.usCodePage
);
561 } // end of wxNativeFontInfo::GetEncoding
563 void wxNativeFontInfo
::SetPointSize(
567 fm
.lEmHeight
= (LONG
)nPointsize
;
568 } // end of wxNativeFontInfo::SetPointSize
570 void wxNativeFontInfo
::SetStyle(
577 wxFAIL_MSG( _T("unknown font style") );
580 case wxFONTSTYLE_NORMAL
:
583 case wxFONTSTYLE_ITALIC
:
584 case wxFONTSTYLE_SLANT
:
585 fa
.fsSelection
|= FATTR_SEL_ITALIC
;
588 } // end of wxNativeFontInfo::SetStyle
590 void wxNativeFontInfo
::SetWeight(
597 wxFAIL_MSG( _T("unknown font weight") );
600 case wxFONTWEIGHT_NORMAL
:
601 fn
.usWeightClass
= FWEIGHT_NORMAL
;
604 case wxFONTWEIGHT_LIGHT
:
605 fn
.usWeightClass
= FWEIGHT_LIGHT
;
608 case wxFONTWEIGHT_BOLD
:
609 fn
.usWeightClass
= FWEIGHT_BOLD
;
612 } // end of wxNativeFontInfo::SetWeight
614 void wxNativeFontInfo
::SetUnderlined(
619 fa
.fsSelection
|= FATTR_SEL_UNDERSCORE
;
620 } // end of wxNativeFontInfo::SetUnderlined
622 void wxNativeFontInfo
::SetFaceName(
626 wxStrncpy(fa
.szFacename
, sFacename
, WXSIZEOF(fa
.szFacename
));
627 } // end of wxNativeFontInfo::SetFaceName
629 void wxNativeFontInfo
::SetFamily(
638 sFacename
= _T("Script");
642 sFacename
= _T("Times New Roman");
646 sFacename
= _T("Times New Roman");
651 sFacename
= _T("Courier New");
655 sFacename
= _T("WarpSans");
660 sFacename
= _T("Helv");
663 if (!wxStrlen(fa
.szFacename
) )
665 SetFaceName(sFacename
);
667 } // end of wxNativeFontInfo::SetFamily
669 void wxNativeFontInfo
::SetEncoding(
670 wxFontEncoding eEncoding
673 wxNativeEncodingInfo vInfo
;
675 if ( !wxGetNativeFontEncoding( eEncoding
680 if (wxTheFontMapper
->GetAltForEncoding( eEncoding
684 if (!vInfo
.facename
.empty())
687 // If we have this encoding only in some particular facename, use
688 // the facename - it is better to show the correct characters in a
689 // wrong facename than unreadable text in a correct one
691 SetFaceName(vInfo
.facename
);
695 #endif // wxUSE_FONTMAP
697 // unsupported encoding, replace with the default
701 fa
.usCodePage
= vInfo
.charset
;
702 } // end of wxNativeFontInfo::SetFaceName
704 bool wxNativeFontInfo
::FromString(
705 const wxString
& rsStr
710 wxStringTokenizer
vTokenizer(rsStr
, _T(";"));
715 wxString sToken
= vTokenizer
.GetNextToken();
717 if (sToken
!= _T('0'))
720 sToken
= vTokenizer
.GetNextToken();
721 if (!sToken
.ToLong(&lVal
))
725 sToken
= vTokenizer
.GetNextToken();
726 if (!sToken
.ToLong(&lVal
))
728 fa
.lAveCharWidth
= lVal
;
730 sToken
= vTokenizer
.GetNextToken();
731 if (!sToken
.ToLong(&lVal
))
733 fa
.fsSelection
= (USHORT
)lVal
;
735 sToken
= vTokenizer
.GetNextToken();
736 if (!sToken
.ToLong(&lVal
))
738 fa
.fsType
= (USHORT
)lVal
;
740 sToken
= vTokenizer
.GetNextToken();
741 if (!sToken
.ToLong(&lVal
))
743 fa
.fsFontUse
= (USHORT
)lVal
;
745 sToken
= vTokenizer
.GetNextToken();
746 if (!sToken
.ToLong(&lVal
))
748 fa
.idRegistry
= (USHORT
)lVal
;
750 sToken
= vTokenizer
.GetNextToken();
751 if (!sToken
.ToLong(&lVal
))
753 fa
.usCodePage
= (USHORT
)lVal
;
755 sToken
= vTokenizer
.GetNextToken();
756 if (!sToken
.ToLong(&lVal
))
760 sToken
= vTokenizer
.GetNextToken();
761 if (!sToken
.ToLong(&lVal
))
763 fn
.usWeightClass
= (USHORT
)lVal
;
765 sToken
= vTokenizer
.GetNextToken();
768 wxStrcpy(fa
.szFacename
, sToken
.c_str());
770 } // end of wxNativeFontInfo::FromString
772 wxString wxNativeFontInfo
::ToString() const
776 sStr
.Printf(_T("%d;%ld;%ld;%ld;%d;%d;%d;%d;%d;%ld;%d;%s"),
777 0, // version, in case we want to change the format later
790 } // end of wxNativeFontInfo::ToString
792 // ----------------------------------------------------------------------------
794 // ----------------------------------------------------------------------------
798 } // end of wxFont::Init
801 const wxNativeFontInfo
& rInfo
806 m_refData
= new wxFontRefData( rInfo
811 } // end of wxFont::Create
814 const wxString
& rsFontdesc
817 wxNativeFontInfo vInfo
;
819 if (vInfo
.FromString(rsFontdesc
))
821 } // end of wxFont::wxFont
823 // ----------------------------------------------------------------------------
824 // Constructor for a font. Note that the real construction is done
825 // in wxDC::SetFont, when information is available about scaling etc.
826 // ----------------------------------------------------------------------------
833 , const wxString
& rsFaceName
834 , wxFontEncoding vEncoding
840 // wxDEFAULT is a valid value for the font size too so we must treat it
841 // specially here (otherwise the size would be 70 == wxDEFAULT value)
843 if (nPointSize
== wxDEFAULT
)
845 nPointSize
= wxNORMAL_FONT
->GetPointSize();
847 m_refData
= new wxFontRefData( nPointSize
857 } // end of wxFont::Create
861 } // end of wxFont::~wxFont
863 // ----------------------------------------------------------------------------
864 // real implementation
865 // Boris' Kovalenko comments:
866 // Because OS/2 fonts are associated with PS we can not create the font
867 // here, but we may check that font definition is true
868 // ----------------------------------------------------------------------------
870 bool wxFont
::RealizeResource()
872 if ( GetResourceHandle() )
876 return M_FONTDATA
->Alloc(this);
877 } // end of wxFont::RealizeResource
879 bool wxFont
::FreeResource(
883 if (GetResourceHandle())
889 } // end of wxFont::FreeResource
891 WXHANDLE wxFont
::GetResourceHandle()
894 } // end of wxFont::GetResourceHandle
896 WXHFONT wxFont
::GetHFONT() const
898 return M_FONTDATA ? M_FONTDATA
->GetHFONT() : 0;
899 } // end of wxFont::GetHFONT
901 bool wxFont
::IsFree() const
903 return M_FONTDATA
&& (M_FONTDATA
->GetHFONT() == 0);
904 } // end of wxFont::IsFree
906 void wxFont
::Unshare()
908 // Don't change shared data
911 m_refData
= new wxFontRefData();
915 wxFontRefData
* ref
= new wxFontRefData(*M_FONTDATA
);
919 } // end of wxFont::Unshare
921 // ----------------------------------------------------------------------------
922 // change font attribute: we recreate font when doing it
923 // ----------------------------------------------------------------------------
925 void wxFont
::SetPointSize(
931 M_FONTDATA
->SetPointSize(nPointSize
);
934 } // end of wxFont::SetPointSize
936 void wxFont
::SetFamily(
942 M_FONTDATA
->SetFamily(nFamily
);
945 } // end of wxFont::SetFamily
947 void wxFont
::SetStyle(
953 M_FONTDATA
->SetStyle(nStyle
);
956 } // end of wxFont::SetStyle
958 void wxFont
::SetWeight(
964 M_FONTDATA
->SetWeight(nWeight
);
967 } // end of wxFont::SetWeight
969 void wxFont
::SetFaceName(
970 const wxString
& rsFaceName
975 M_FONTDATA
->SetFaceName(rsFaceName
);
978 } // end of wxFont::SetFaceName
980 void wxFont
::SetUnderlined(
986 M_FONTDATA
->SetUnderlined(bUnderlined
);
989 } // end of wxFont::SetUnderlined
991 void wxFont
::SetEncoding(
992 wxFontEncoding vEncoding
997 M_FONTDATA
->SetEncoding(vEncoding
);
1000 } // end of wxFont::SetEncoding
1002 void wxFont
::SetNativeFontInfo(
1003 const wxNativeFontInfo
& rInfo
1010 *M_FONTDATA
= wxFontRefData(rInfo
);
1015 // ----------------------------------------------------------------------------
1017 // ----------------------------------------------------------------------------
1019 int wxFont
::GetPointSize() const
1021 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
1023 return M_FONTDATA
->GetPointSize();
1024 } // end of wxFont::GetPointSize
1026 int wxFont
::GetFamily() const
1028 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
1030 return M_FONTDATA
->GetFamily();
1031 } // end of wxFont::GetFamily
1033 int wxFont
::GetStyle() const
1035 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
1037 return M_FONTDATA
->GetStyle();
1038 } // end of wxFont::GetStyle
1040 int wxFont
::GetWeight() const
1042 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
1044 return M_FONTDATA
->GetWeight();
1047 bool wxFont
::GetUnderlined() const
1049 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid font") );
1051 return M_FONTDATA
->GetUnderlined();
1052 } // end of wxFont::GetUnderlined
1054 wxString wxFont
::GetFaceName() const
1056 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
1058 return M_FONTDATA
->GetFaceName();
1059 } // end of wxFont::GetFaceName
1061 wxFontEncoding wxFont
::GetEncoding() const
1063 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT
, wxT("invalid font") );
1065 return M_FONTDATA
->GetEncoding();
1066 } // end of wxFont::GetEncoding
1068 wxNativeFontInfo
* wxFont
::GetNativeFontInfo() const
1070 if (M_FONTDATA
->HasNativeFontInfo())
1071 return new wxNativeFontInfo(M_FONTDATA
->GetNativeFontInfo());
1073 } // end of wxFont::GetNativeFontInfo
1076 // Internal use only method to set the FONTMETRICS array
1083 M_FONTDATA
->SetFM(pFM
);
1084 M_FONTDATA
->SetNumFonts(nNumFonts
);
1085 } // end of wxFont::SetFM