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 // ----------------------------------------------------------------------------
21 #pragma implementation "fontenum.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
37 #include "wx/fontutil.h"
38 #include "wx/fontenum.h"
39 #include "wx/fontmap.h"
41 #include "wx/msw/private.h"
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 // the helper class which calls ::EnumFontFamilies() and whose OnFont() is
48 // called from the callback passed to this function and, in its turn, calls the
49 // appropariate wxFontEnumerator method
50 class wxFontEnumeratorHelper
53 wxFontEnumeratorHelper(wxFontEnumerator
*fontEnum
);
55 // control what exactly are we enumerating
56 // we enumerate fonts with given enocding
57 bool SetEncoding(wxFontEncoding encoding
);
58 // we enumerate fixed-width fonts
59 void SetFixedOnly(bool fixedOnly
) { m_fixedOnly
= fixedOnly
; }
60 // we enumerate the encodings available in this family
61 void SetFamily(const wxString
& family
);
63 // call to start enumeration
66 // called by our font enumeration proc
67 bool OnFont(const LPLOGFONT lf
, const LPTEXTMETRIC tm
) const;
70 // the object we forward calls to OnFont() to
71 wxFontEnumerator
*m_fontEnum
;
73 // if != -1, enum only fonts which have this encoding
76 // if not empty, enum only the fonts with this facename
79 // if not empty, enum only the fonts in this family
82 // if TRUE, enum only fixed fonts
85 // if TRUE, we enumerate the encodings, not fonts
88 // the list of charsets we already found while enumerating charsets
89 wxArrayInt m_charsets
;
91 // the list of facenames we already found while enumerating facenames
92 wxArrayString m_facenames
;
94 DECLARE_NO_COPY_CLASS(wxFontEnumeratorHelper
)
97 // ----------------------------------------------------------------------------
99 // ----------------------------------------------------------------------------
101 #ifndef __WXMICROWIN__
102 int CALLBACK
wxFontEnumeratorProc(LPLOGFONT lplf
, LPTEXTMETRIC lptm
,
103 DWORD dwStyle
, LONG lParam
);
106 // ============================================================================
108 // ============================================================================
110 // ----------------------------------------------------------------------------
111 // wxFontEnumeratorHelper
112 // ----------------------------------------------------------------------------
114 wxFontEnumeratorHelper::wxFontEnumeratorHelper(wxFontEnumerator
*fontEnum
)
116 m_fontEnum
= fontEnum
;
117 m_charset
= DEFAULT_CHARSET
;
119 m_enumEncodings
= FALSE
;
122 void wxFontEnumeratorHelper::SetFamily(const wxString
& family
)
124 m_enumEncodings
= TRUE
;
128 bool wxFontEnumeratorHelper::SetEncoding(wxFontEncoding encoding
)
130 if ( encoding
!= wxFONTENCODING_SYSTEM
)
132 wxNativeEncodingInfo info
;
133 if ( !wxGetNativeFontEncoding(encoding
, &info
) )
136 if ( !wxFontMapper::Get()->GetAltForEncoding(encoding
, &info
) )
137 #endif // wxUSE_FONTMAP
139 // no such encodings at all
144 m_charset
= info
.charset
;
145 m_facename
= info
.facename
;
151 #if defined(__WXWINE__)
152 #define wxFONTENUMPROC FONTENUMPROCEX
153 #elif (defined(__GNUWIN32__) && !defined(__CYGWIN10__) && !wxCHECK_W32API_VERSION( 1, 1 ))
154 #if wxUSE_NORLANDER_HEADERS
155 #define wxFONTENUMPROC FONTENUMPROC
157 #define wxFONTENUMPROC int(*)(ENUMLOGFONTEX *, NEWTEXTMETRICEX*, int, LPARAM)
160 #define wxFONTENUMPROC FONTENUMPROC
163 void wxFontEnumeratorHelper::DoEnumerate()
165 #ifndef __WXMICROWIN__
166 HDC hDC
= ::GetDC(NULL
);
170 lf
.lfCharSet
= m_charset
;
171 wxStrncpy(lf
.lfFaceName
, m_facename
, WXSIZEOF(lf
.lfFaceName
));
172 lf
.lfPitchAndFamily
= 0;
173 ::EnumFontFamiliesEx(hDC
, &lf
, (wxFONTENUMPROC
)wxFontEnumeratorProc
,
174 (LPARAM
)this, 0 /* reserved */) ;
176 ::EnumFonts(hDC
, (LPTSTR
)NULL
, (FONTENUMPROC
)wxFontEnumeratorProc
,
185 ::ReleaseDC(NULL
, hDC
);
189 bool wxFontEnumeratorHelper::OnFont(const LPLOGFONT lf
,
190 const LPTEXTMETRIC tm
) const
192 if ( m_enumEncodings
)
194 // is this a new charset?
195 int cs
= lf
->lfCharSet
;
196 if ( m_charsets
.Index(cs
) == wxNOT_FOUND
)
198 wxConstCast(this, wxFontEnumeratorHelper
)->m_charsets
.Add(cs
);
200 wxFontEncoding enc
= wxGetFontEncFromCharSet(cs
);
201 return m_fontEnum
->OnFontEncoding(lf
->lfFaceName
,
202 wxFontMapper::GetEncodingName(enc
));
206 // continue enumeration
213 // check that it's a fixed pitch font (there is *no* error here, the
214 // flag name is misleading!)
215 if ( tm
->tmPitchAndFamily
& TMPF_FIXED_PITCH
)
217 // not a fixed pitch font
222 if ( m_charset
!= DEFAULT_CHARSET
)
224 // check that we have the right encoding
225 if ( lf
->lfCharSet
!= m_charset
)
230 else // enumerating fonts in all charsets
232 // we can get the same facename twice or more in this case because it
233 // may exist in several charsets but we only want to return one copy of
234 // it (note that this can't happen for m_charset != DEFAULT_CHARSET)
235 if ( m_facenames
.Index(lf
->lfFaceName
) != wxNOT_FOUND
)
237 // continue enumeration
241 wxConstCast(this, wxFontEnumeratorHelper
)->
242 m_facenames
.Add(lf
->lfFaceName
);
245 return m_fontEnum
->OnFacename(lf
->lfFaceName
);
248 // ----------------------------------------------------------------------------
250 // ----------------------------------------------------------------------------
252 bool wxFontEnumerator::EnumerateFacenames(wxFontEncoding encoding
,
255 wxFontEnumeratorHelper
fe(this);
256 if ( fe
.SetEncoding(encoding
) )
258 fe
.SetFixedOnly(fixedWidthOnly
);
262 // else: no such fonts, unknown encoding
267 bool wxFontEnumerator::EnumerateEncodings(const wxString
& family
)
269 wxFontEnumeratorHelper
fe(this);
270 fe
.SetFamily(family
);
276 // ----------------------------------------------------------------------------
278 // ----------------------------------------------------------------------------
280 #ifndef __WXMICROWIN__
281 int CALLBACK
wxFontEnumeratorProc(LPLOGFONT lplf
, LPTEXTMETRIC lptm
,
282 DWORD
WXUNUSED(dwStyle
), LONG lParam
)
285 // we used to process TrueType fonts only, but there doesn't seem to be any
286 // reasons to restrict ourselves to them here
288 // Get rid of any fonts that we don't want...
289 if ( dwStyle
!= TRUETYPE_FONTTYPE
)
291 // continue enumeration
296 wxFontEnumeratorHelper
*fontEnum
= (wxFontEnumeratorHelper
*)lParam
;
298 return fontEnum
->OnFont(lplf
, lptm
);
302 #endif // wxUSE_FONTMAP