]> git.saurik.com Git - wxWidgets.git/blob - src/common/fontenumcmn.cpp
fixing overrelease and out-of-bounds write, fixes #13725
[wxWidgets.git] / src / common / fontenumcmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/fontenumcmn.cpp
3 // Purpose: wxFontEnumerator class
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 7/5/2006
7 // RCS-ID: $Id$
8 // Copyright: (c) 1999-2003 Vadim Zeitlin <vadim@wxwindows.org>
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_FONTENUM
28
29 #include "wx/fontenum.h"
30
31 // ============================================================================
32 // implementation
33 // ============================================================================
34
35 // A simple wxFontEnumerator which doesn't perform any filtering and
36 // just returns all facenames and encodings found in the system
37 class WXDLLEXPORT wxSimpleFontEnumerator : public wxFontEnumerator
38 {
39 public:
40 wxSimpleFontEnumerator() { }
41
42 // called by EnumerateFacenames
43 virtual bool OnFacename(const wxString& facename)
44 {
45 m_arrFacenames.Add(facename);
46 return true;
47 }
48
49 // called by EnumerateEncodings
50 virtual bool OnFontEncoding(const wxString& WXUNUSED(facename),
51 const wxString& encoding)
52 {
53 m_arrEncodings.Add(encoding);
54 return true;
55 }
56
57 public:
58 wxArrayString m_arrFacenames, m_arrEncodings;
59 };
60
61
62 /* static */
63 wxArrayString wxFontEnumerator::GetFacenames(wxFontEncoding encoding, bool fixedWidthOnly)
64 {
65 wxSimpleFontEnumerator temp;
66 temp.EnumerateFacenames(encoding, fixedWidthOnly);
67 return temp.m_arrFacenames;
68 }
69
70 /* static */
71 wxArrayString wxFontEnumerator::GetEncodings(const wxString& facename)
72 {
73 wxSimpleFontEnumerator temp;
74 temp.EnumerateEncodings(facename);
75 return temp.m_arrEncodings;
76 }
77
78 /* static */
79 bool wxFontEnumerator::IsValidFacename(const wxString &facename)
80 {
81 // we cache the result of wxFontEnumerator::GetFacenames supposing that
82 // the array of face names won't change in the session of this program
83 static wxArrayString s_arr = wxFontEnumerator::GetFacenames();
84
85 #ifdef __WXMSW__
86 // Quoting the MSDN:
87 // "MS Shell Dlg is a mapping mechanism that enables
88 // U.S. English Microsoft Windows NT, and Microsoft Windows 2000 to
89 // support locales that have characters that are not contained in code
90 // page 1252. It is not a font but a face name for a nonexistent font."
91 // Thus we need to consider "Ms Shell Dlg" and "Ms Shell Dlg 2" as valid
92 // font face names even if they are not enumerated by wxFontEnumerator
93 if (facename.IsSameAs(wxT("Ms Shell Dlg"), false) ||
94 facename.IsSameAs(wxT("Ms Shell Dlg 2"), false))
95 return true;
96 #endif
97
98 // is given font face name a valid one ?
99 if (s_arr.Index(facename, false) == wxNOT_FOUND)
100 return false;
101
102 return true;
103 }
104
105 #ifdef wxHAS_UTF8_FONTS
106 bool wxFontEnumerator::EnumerateEncodingsUTF8(const wxString& facename)
107 {
108 // name of UTF-8 encoding: no need to use wxFontMapper for it as it's
109 // unlikely to change
110 const wxString utf8(wxT("UTF-8"));
111
112 // all fonts are in UTF-8 only if this code is used
113 if ( !facename.empty() )
114 {
115 OnFontEncoding(facename, utf8);
116 return true;
117 }
118
119 // so enumerating all facenames supporting this encoding is the same as
120 // enumerating all facenames
121 const wxArrayString facenames(GetFacenames(wxFONTENCODING_UTF8));
122 const size_t count = facenames.size();
123 if ( !count )
124 return false;
125
126 for ( size_t n = 0; n < count; n++ )
127 {
128 OnFontEncoding(facenames[n], utf8);
129 }
130
131 return true;
132 }
133 #endif // wxHAS_UTF8_FONTS
134
135 #endif // wxUSE_FONTENUM