]>
Commit | Line | Data |
---|---|---|
a1d58ddc VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: msw/fontenum.cpp | |
543f08a6 JS |
3 | // Purpose: wxFontEnumerator class for Windows |
4 | // Author: Julian Smart | |
a1d58ddc | 5 | // Modified by: Vadim Zeitlin to add support for font encodings |
543f08a6 JS |
6 | // Created: 04/01/98 |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
65571936 | 9 | // Licence: wxWindows licence |
a1d58ddc VZ |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
543f08a6 JS |
20 | // For compilers that support precompilation, includes "wx.h". |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
1e6feb95 VZ |
27 | #if wxUSE_FONTMAP |
28 | ||
543f08a6 | 29 | #ifndef WX_PRECOMP |
2e2b69ee | 30 | #include "wx/gdicmn.h" |
e4ffab29 MB |
31 | #include "wx/font.h" |
32 | #include "wx/encinfo.h" | |
543f08a6 JS |
33 | #endif |
34 | ||
4676948b JS |
35 | #include "wx/msw/private.h" |
36 | ||
76e23cdb | 37 | #include "wx/fontutil.h" |
a1d58ddc | 38 | #include "wx/fontenum.h" |
11c7d5b6 | 39 | #include "wx/fontmap.h" |
543f08a6 | 40 | |
a1d58ddc VZ |
41 | // ---------------------------------------------------------------------------- |
42 | // private classes | |
43 | // ---------------------------------------------------------------------------- | |
44 | ||
f6bcfd97 BP |
45 | // the helper class which calls ::EnumFontFamilies() and whose OnFont() is |
46 | // called from the callback passed to this function and, in its turn, calls the | |
47 | // appropariate wxFontEnumerator method | |
a1d58ddc | 48 | class wxFontEnumeratorHelper |
543f08a6 | 49 | { |
a1d58ddc VZ |
50 | public: |
51 | wxFontEnumeratorHelper(wxFontEnumerator *fontEnum); | |
52 | ||
53 | // control what exactly are we enumerating | |
f6bcfd97 | 54 | // we enumerate fonts with given enocding |
a1d58ddc | 55 | bool SetEncoding(wxFontEncoding encoding); |
f6bcfd97 BP |
56 | // we enumerate fixed-width fonts |
57 | void SetFixedOnly(bool fixedOnly) { m_fixedOnly = fixedOnly; } | |
58 | // we enumerate the encodings available in this family | |
59 | void SetFamily(const wxString& family); | |
a1d58ddc VZ |
60 | |
61 | // call to start enumeration | |
62 | void DoEnumerate(); | |
63 | ||
64 | // called by our font enumeration proc | |
65 | bool OnFont(const LPLOGFONT lf, const LPTEXTMETRIC tm) const; | |
66 | ||
67 | private: | |
68 | // the object we forward calls to OnFont() to | |
69 | wxFontEnumerator *m_fontEnum; | |
543f08a6 | 70 | |
a1d58ddc VZ |
71 | // if != -1, enum only fonts which have this encoding |
72 | int m_charset; | |
543f08a6 | 73 | |
11c7d5b6 VZ |
74 | // if not empty, enum only the fonts with this facename |
75 | wxString m_facename; | |
76 | ||
f6bcfd97 BP |
77 | // if not empty, enum only the fonts in this family |
78 | wxString m_family; | |
79 | ||
cbe874bd | 80 | // if true, enum only fixed fonts |
a1d58ddc | 81 | bool m_fixedOnly; |
f6bcfd97 | 82 | |
cbe874bd | 83 | // if true, we enumerate the encodings, not fonts |
f6bcfd97 BP |
84 | bool m_enumEncodings; |
85 | ||
86 | // the list of charsets we already found while enumerating charsets | |
87 | wxArrayInt m_charsets; | |
98e6fc81 VZ |
88 | |
89 | // the list of facenames we already found while enumerating facenames | |
90 | wxArrayString m_facenames; | |
22f3361e VZ |
91 | |
92 | DECLARE_NO_COPY_CLASS(wxFontEnumeratorHelper) | |
a1d58ddc | 93 | }; |
543f08a6 | 94 | |
a1d58ddc VZ |
95 | // ---------------------------------------------------------------------------- |
96 | // private functions | |
97 | // ---------------------------------------------------------------------------- | |
98 | ||
04ef50df | 99 | #ifndef __WXMICROWIN__ |
a1d58ddc VZ |
100 | int CALLBACK wxFontEnumeratorProc(LPLOGFONT lplf, LPTEXTMETRIC lptm, |
101 | DWORD dwStyle, LONG lParam); | |
04ef50df | 102 | #endif |
a1d58ddc VZ |
103 | |
104 | // ============================================================================ | |
105 | // implementation | |
106 | // ============================================================================ | |
107 | ||
108 | // ---------------------------------------------------------------------------- | |
109 | // wxFontEnumeratorHelper | |
110 | // ---------------------------------------------------------------------------- | |
111 | ||
112 | wxFontEnumeratorHelper::wxFontEnumeratorHelper(wxFontEnumerator *fontEnum) | |
113 | { | |
114 | m_fontEnum = fontEnum; | |
14647781 | 115 | m_charset = DEFAULT_CHARSET; |
cbe874bd WS |
116 | m_fixedOnly = false; |
117 | m_enumEncodings = false; | |
f6bcfd97 BP |
118 | } |
119 | ||
120 | void wxFontEnumeratorHelper::SetFamily(const wxString& family) | |
121 | { | |
cbe874bd | 122 | m_enumEncodings = true; |
f6bcfd97 | 123 | m_family = family; |
543f08a6 JS |
124 | } |
125 | ||
a1d58ddc VZ |
126 | bool wxFontEnumeratorHelper::SetEncoding(wxFontEncoding encoding) |
127 | { | |
98e6fc81 | 128 | if ( encoding != wxFONTENCODING_SYSTEM ) |
a1d58ddc | 129 | { |
98e6fc81 VZ |
130 | wxNativeEncodingInfo info; |
131 | if ( !wxGetNativeFontEncoding(encoding, &info) ) | |
11c7d5b6 | 132 | { |
98e6fc81 | 133 | #if wxUSE_FONTMAP |
142b3bc2 | 134 | if ( !wxFontMapper::Get()->GetAltForEncoding(encoding, &info) ) |
98e6fc81 VZ |
135 | #endif // wxUSE_FONTMAP |
136 | { | |
137 | // no such encodings at all | |
cbe874bd | 138 | return false; |
98e6fc81 | 139 | } |
11c7d5b6 | 140 | } |
98e6fc81 VZ |
141 | |
142 | m_charset = info.charset; | |
143 | m_facename = info.facename; | |
a1d58ddc | 144 | } |
543f08a6 | 145 | |
cbe874bd | 146 | return true; |
a1d58ddc VZ |
147 | } |
148 | ||
b4da152e JS |
149 | #if defined(__GNUWIN32__) && !defined(__CYGWIN10__) && !wxCHECK_W32API_VERSION( 1, 1 ) && !wxUSE_NORLANDER_HEADERS |
150 | #define wxFONTENUMPROC int(*)(ENUMLOGFONTEX *, NEWTEXTMETRICEX*, int, LPARAM) | |
01dba85a | 151 | #else |
9f83044f | 152 | #define wxFONTENUMPROC FONTENUMPROC |
01dba85a JS |
153 | #endif |
154 | ||
a1d58ddc | 155 | void wxFontEnumeratorHelper::DoEnumerate() |
543f08a6 | 156 | { |
04ef50df | 157 | #ifndef __WXMICROWIN__ |
a1d58ddc | 158 | HDC hDC = ::GetDC(NULL); |
543f08a6 | 159 | |
4676948b JS |
160 | #ifdef __WXWINCE__ |
161 | ::EnumFontFamilies(hDC, m_facename, (wxFONTENUMPROC)wxFontEnumeratorProc, | |
162 | (LPARAM)this) ; | |
3a5bcc4d | 163 | #else // __WIN32__ |
a1d58ddc | 164 | LOGFONT lf; |
373a5fb3 | 165 | lf.lfCharSet = (BYTE)m_charset; |
11c7d5b6 | 166 | wxStrncpy(lf.lfFaceName, m_facename, WXSIZEOF(lf.lfFaceName)); |
a1d58ddc | 167 | lf.lfPitchAndFamily = 0; |
01dba85a | 168 | ::EnumFontFamiliesEx(hDC, &lf, (wxFONTENUMPROC)wxFontEnumeratorProc, |
a1d58ddc | 169 | (LPARAM)this, 0 /* reserved */) ; |
3a5bcc4d | 170 | #endif // Win32/CE |
543f08a6 | 171 | |
a1d58ddc | 172 | ::ReleaseDC(NULL, hDC); |
04ef50df | 173 | #endif |
a1d58ddc VZ |
174 | } |
175 | ||
176 | bool wxFontEnumeratorHelper::OnFont(const LPLOGFONT lf, | |
177 | const LPTEXTMETRIC tm) const | |
178 | { | |
f6bcfd97 BP |
179 | if ( m_enumEncodings ) |
180 | { | |
181 | // is this a new charset? | |
182 | int cs = lf->lfCharSet; | |
183 | if ( m_charsets.Index(cs) == wxNOT_FOUND ) | |
184 | { | |
185 | wxConstCast(this, wxFontEnumeratorHelper)->m_charsets.Add(cs); | |
186 | ||
187 | wxFontEncoding enc = wxGetFontEncFromCharSet(cs); | |
14647781 | 188 | return m_fontEnum->OnFontEncoding(lf->lfFaceName, |
f6bcfd97 BP |
189 | wxFontMapper::GetEncodingName(enc)); |
190 | } | |
191 | else | |
192 | { | |
193 | // continue enumeration | |
cbe874bd | 194 | return true; |
f6bcfd97 BP |
195 | } |
196 | } | |
197 | ||
a1d58ddc VZ |
198 | if ( m_fixedOnly ) |
199 | { | |
200 | // check that it's a fixed pitch font (there is *no* error here, the | |
201 | // flag name is misleading!) | |
202 | if ( tm->tmPitchAndFamily & TMPF_FIXED_PITCH ) | |
203 | { | |
204 | // not a fixed pitch font | |
cbe874bd | 205 | return true; |
a1d58ddc VZ |
206 | } |
207 | } | |
208 | ||
98e6fc81 | 209 | if ( m_charset != DEFAULT_CHARSET ) |
a1d58ddc VZ |
210 | { |
211 | // check that we have the right encoding | |
212 | if ( lf->lfCharSet != m_charset ) | |
213 | { | |
cbe874bd | 214 | return true; |
a1d58ddc VZ |
215 | } |
216 | } | |
98e6fc81 VZ |
217 | else // enumerating fonts in all charsets |
218 | { | |
219 | // we can get the same facename twice or more in this case because it | |
220 | // may exist in several charsets but we only want to return one copy of | |
221 | // it (note that this can't happen for m_charset != DEFAULT_CHARSET) | |
222 | if ( m_facenames.Index(lf->lfFaceName) != wxNOT_FOUND ) | |
223 | { | |
224 | // continue enumeration | |
cbe874bd | 225 | return true; |
98e6fc81 VZ |
226 | } |
227 | ||
228 | wxConstCast(this, wxFontEnumeratorHelper)-> | |
229 | m_facenames.Add(lf->lfFaceName); | |
230 | } | |
a1d58ddc | 231 | |
3c1866e8 | 232 | return m_fontEnum->OnFacename(lf->lfFaceName); |
a1d58ddc VZ |
233 | } |
234 | ||
235 | // ---------------------------------------------------------------------------- | |
236 | // wxFontEnumerator | |
237 | // ---------------------------------------------------------------------------- | |
238 | ||
3c1866e8 VZ |
239 | bool wxFontEnumerator::EnumerateFacenames(wxFontEncoding encoding, |
240 | bool fixedWidthOnly) | |
543f08a6 | 241 | { |
a1d58ddc VZ |
242 | wxFontEnumeratorHelper fe(this); |
243 | if ( fe.SetEncoding(encoding) ) | |
244 | { | |
245 | fe.SetFixedOnly(fixedWidthOnly); | |
246 | ||
247 | fe.DoEnumerate(); | |
248 | } | |
249 | // else: no such fonts, unknown encoding | |
250 | ||
cbe874bd | 251 | return true; |
543f08a6 JS |
252 | } |
253 | ||
a1d58ddc VZ |
254 | bool wxFontEnumerator::EnumerateEncodings(const wxString& family) |
255 | { | |
f6bcfd97 BP |
256 | wxFontEnumeratorHelper fe(this); |
257 | fe.SetFamily(family); | |
258 | fe.DoEnumerate(); | |
a1d58ddc | 259 | |
cbe874bd | 260 | return true; |
a1d58ddc VZ |
261 | } |
262 | ||
263 | // ---------------------------------------------------------------------------- | |
264 | // Windows callbacks | |
265 | // ---------------------------------------------------------------------------- | |
266 | ||
04ef50df | 267 | #ifndef __WXMICROWIN__ |
a1d58ddc | 268 | int CALLBACK wxFontEnumeratorProc(LPLOGFONT lplf, LPTEXTMETRIC lptm, |
574c939e | 269 | DWORD WXUNUSED(dwStyle), LONG lParam) |
a1d58ddc | 270 | { |
574c939e | 271 | |
98e6fc81 VZ |
272 | // we used to process TrueType fonts only, but there doesn't seem to be any |
273 | // reasons to restrict ourselves to them here | |
274 | #if 0 | |
a1d58ddc VZ |
275 | // Get rid of any fonts that we don't want... |
276 | if ( dwStyle != TRUETYPE_FONTTYPE ) | |
277 | { | |
278 | // continue enumeration | |
279 | return TRUE; | |
280 | } | |
98e6fc81 | 281 | #endif // 0 |
a1d58ddc VZ |
282 | |
283 | wxFontEnumeratorHelper *fontEnum = (wxFontEnumeratorHelper *)lParam; | |
284 | ||
285 | return fontEnum->OnFont(lplf, lptm); | |
286 | } | |
04ef50df | 287 | #endif |
a1d58ddc | 288 | |
1e6feb95 | 289 | #endif // wxUSE_FONTMAP |