1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: msw/fontenum.cpp
3 // Purpose: wxFontEnumerator class for Windows
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin to add support for font encodings
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "fontenum.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
34 #include "wx/gdicmn.h"
36 #include "wx/encinfo.h"
39 #include "wx/msw/private.h"
41 #include "wx/fontutil.h"
42 #include "wx/fontenum.h"
43 #include "wx/fontmap.h"
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 // the helper class which calls ::EnumFontFamilies() and whose OnFont() is
50 // called from the callback passed to this function and, in its turn, calls the
51 // appropariate wxFontEnumerator method
52 class wxFontEnumeratorHelper
55 wxFontEnumeratorHelper(wxFontEnumerator
*fontEnum
);
57 // control what exactly are we enumerating
58 // we enumerate fonts with given enocding
59 bool SetEncoding(wxFontEncoding encoding
);
60 // we enumerate fixed-width fonts
61 void SetFixedOnly(bool fixedOnly
) { m_fixedOnly
= fixedOnly
; }
62 // we enumerate the encodings available in this family
63 void SetFamily(const wxString
& family
);
65 // call to start enumeration
68 // called by our font enumeration proc
69 bool OnFont(const LPLOGFONT lf
, const LPTEXTMETRIC tm
) const;
72 // the object we forward calls to OnFont() to
73 wxFontEnumerator
*m_fontEnum
;
75 // if != -1, enum only fonts which have this encoding
78 // if not empty, enum only the fonts with this facename
81 // if not empty, enum only the fonts in this family
84 // if true, enum only fixed fonts
87 // if true, we enumerate the encodings, not fonts
90 // the list of charsets we already found while enumerating charsets
91 wxArrayInt m_charsets
;
93 // the list of facenames we already found while enumerating facenames
94 wxArrayString m_facenames
;
96 DECLARE_NO_COPY_CLASS(wxFontEnumeratorHelper
)
99 // ----------------------------------------------------------------------------
101 // ----------------------------------------------------------------------------
103 #ifndef __WXMICROWIN__
104 int CALLBACK
wxFontEnumeratorProc(LPLOGFONT lplf
, LPTEXTMETRIC lptm
,
105 DWORD dwStyle
, LONG lParam
);
108 // ============================================================================
110 // ============================================================================
112 // ----------------------------------------------------------------------------
113 // wxFontEnumeratorHelper
114 // ----------------------------------------------------------------------------
116 wxFontEnumeratorHelper::wxFontEnumeratorHelper(wxFontEnumerator
*fontEnum
)
118 m_fontEnum
= fontEnum
;
119 m_charset
= DEFAULT_CHARSET
;
121 m_enumEncodings
= false;
124 void wxFontEnumeratorHelper::SetFamily(const wxString
& family
)
126 m_enumEncodings
= true;
130 bool wxFontEnumeratorHelper::SetEncoding(wxFontEncoding encoding
)
132 if ( encoding
!= wxFONTENCODING_SYSTEM
)
134 wxNativeEncodingInfo info
;
135 if ( !wxGetNativeFontEncoding(encoding
, &info
) )
138 if ( !wxFontMapper::Get()->GetAltForEncoding(encoding
, &info
) )
139 #endif // wxUSE_FONTMAP
141 // no such encodings at all
146 m_charset
= info
.charset
;
147 m_facename
= info
.facename
;
153 #if defined(__GNUWIN32__) && !defined(__CYGWIN10__) && !wxCHECK_W32API_VERSION( 1, 1 ) && !wxUSE_NORLANDER_HEADERS
154 #define wxFONTENUMPROC int(*)(ENUMLOGFONTEX *, NEWTEXTMETRICEX*, int, LPARAM)
156 #define wxFONTENUMPROC FONTENUMPROC
159 void wxFontEnumeratorHelper::DoEnumerate()
161 #ifndef __WXMICROWIN__
162 HDC hDC
= ::GetDC(NULL
);
165 ::EnumFontFamilies(hDC
, m_facename
, (wxFONTENUMPROC
)wxFontEnumeratorProc
,
169 lf
.lfCharSet
= m_charset
;
170 wxStrncpy(lf
.lfFaceName
, m_facename
, WXSIZEOF(lf
.lfFaceName
));
171 lf
.lfPitchAndFamily
= 0;
172 ::EnumFontFamiliesEx(hDC
, &lf
, (wxFONTENUMPROC
)wxFontEnumeratorProc
,
173 (LPARAM
)this, 0 /* reserved */) ;
176 ::ReleaseDC(NULL
, hDC
);
180 bool wxFontEnumeratorHelper::OnFont(const LPLOGFONT lf
,
181 const LPTEXTMETRIC tm
) const
183 if ( m_enumEncodings
)
185 // is this a new charset?
186 int cs
= lf
->lfCharSet
;
187 if ( m_charsets
.Index(cs
) == wxNOT_FOUND
)
189 wxConstCast(this, wxFontEnumeratorHelper
)->m_charsets
.Add(cs
);
191 wxFontEncoding enc
= wxGetFontEncFromCharSet(cs
);
192 return m_fontEnum
->OnFontEncoding(lf
->lfFaceName
,
193 wxFontMapper::GetEncodingName(enc
));
197 // continue enumeration
204 // check that it's a fixed pitch font (there is *no* error here, the
205 // flag name is misleading!)
206 if ( tm
->tmPitchAndFamily
& TMPF_FIXED_PITCH
)
208 // not a fixed pitch font
213 if ( m_charset
!= DEFAULT_CHARSET
)
215 // check that we have the right encoding
216 if ( lf
->lfCharSet
!= m_charset
)
221 else // enumerating fonts in all charsets
223 // we can get the same facename twice or more in this case because it
224 // may exist in several charsets but we only want to return one copy of
225 // it (note that this can't happen for m_charset != DEFAULT_CHARSET)
226 if ( m_facenames
.Index(lf
->lfFaceName
) != wxNOT_FOUND
)
228 // continue enumeration
232 wxConstCast(this, wxFontEnumeratorHelper
)->
233 m_facenames
.Add(lf
->lfFaceName
);
236 return m_fontEnum
->OnFacename(lf
->lfFaceName
);
239 // ----------------------------------------------------------------------------
241 // ----------------------------------------------------------------------------
243 bool wxFontEnumerator::EnumerateFacenames(wxFontEncoding encoding
,
246 wxFontEnumeratorHelper
fe(this);
247 if ( fe
.SetEncoding(encoding
) )
249 fe
.SetFixedOnly(fixedWidthOnly
);
253 // else: no such fonts, unknown encoding
258 bool wxFontEnumerator::EnumerateEncodings(const wxString
& family
)
260 wxFontEnumeratorHelper
fe(this);
261 fe
.SetFamily(family
);
267 // ----------------------------------------------------------------------------
269 // ----------------------------------------------------------------------------
271 #ifndef __WXMICROWIN__
272 int CALLBACK
wxFontEnumeratorProc(LPLOGFONT lplf
, LPTEXTMETRIC lptm
,
273 DWORD
WXUNUSED(dwStyle
), LONG lParam
)
276 // we used to process TrueType fonts only, but there doesn't seem to be any
277 // reasons to restrict ourselves to them here
279 // Get rid of any fonts that we don't want...
280 if ( dwStyle
!= TRUETYPE_FONTTYPE
)
282 // continue enumeration
287 wxFontEnumeratorHelper
*fontEnum
= (wxFontEnumeratorHelper
*)lParam
;
289 return fontEnum
->OnFont(lplf
, lptm
);
293 #endif // wxUSE_FONTMAP