1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/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 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/fontenum.h"
32 #include "wx/gdicmn.h"
34 #include "wx/dynarray.h"
35 #include "wx/msw/private.h"
38 #include "wx/encinfo.h"
39 #include "wx/fontutil.h"
40 #include "wx/fontmap.h"
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 // the helper class which calls ::EnumFontFamilies() and whose OnFont() is
47 // called from the callback passed to this function and, in its turn, calls the
48 // appropariate wxFontEnumerator method
49 class wxFontEnumeratorHelper
52 wxFontEnumeratorHelper(wxFontEnumerator
*fontEnum
);
54 // control what exactly are we enumerating
55 // we enumerate fonts with given enocding
56 bool SetEncoding(wxFontEncoding encoding
);
57 // we enumerate fixed-width fonts
58 void SetFixedOnly(bool fixedOnly
) { m_fixedOnly
= fixedOnly
; }
59 // we enumerate the encodings available in this family
60 void SetFamily(const wxString
& family
);
62 // call to start enumeration
65 // called by our font enumeration proc
66 bool OnFont(const LPLOGFONT lf
, const LPTEXTMETRIC tm
) const;
69 // the object we forward calls to OnFont() to
70 wxFontEnumerator
*m_fontEnum
;
72 // if != -1, enum only fonts which have this encoding
75 // if not empty, enum only the fonts with this facename
78 // if not empty, enum only the fonts in this family
81 // if true, enum only fixed fonts
84 // if true, we enumerate the encodings, not fonts
87 // the list of charsets we already found while enumerating charsets
88 wxArrayInt m_charsets
;
90 // the list of facenames we already found while enumerating facenames
91 wxArrayString m_facenames
;
93 wxDECLARE_NO_COPY_CLASS(wxFontEnumeratorHelper
);
96 // ----------------------------------------------------------------------------
98 // ----------------------------------------------------------------------------
100 #ifndef __WXMICROWIN__
101 int CALLBACK
wxFontEnumeratorProc(LPLOGFONT lplf
, LPTEXTMETRIC lptm
,
102 DWORD dwStyle
, LPARAM lParam
);
105 // ============================================================================
107 // ============================================================================
109 // ----------------------------------------------------------------------------
110 // wxFontEnumeratorHelper
111 // ----------------------------------------------------------------------------
113 wxFontEnumeratorHelper::wxFontEnumeratorHelper(wxFontEnumerator
*fontEnum
)
115 m_fontEnum
= fontEnum
;
116 m_charset
= DEFAULT_CHARSET
;
118 m_enumEncodings
= false;
121 void wxFontEnumeratorHelper::SetFamily(const wxString
& family
)
123 m_enumEncodings
= true;
127 bool wxFontEnumeratorHelper::SetEncoding(wxFontEncoding encoding
)
129 if ( encoding
!= wxFONTENCODING_SYSTEM
)
131 wxNativeEncodingInfo info
;
132 if ( !wxGetNativeFontEncoding(encoding
, &info
) )
135 if ( !wxFontMapper::Get()->GetAltForEncoding(encoding
, &info
) )
136 #endif // wxUSE_FONTMAP
138 // no such encodings at all
143 m_charset
= info
.charset
;
144 m_facename
= info
.facename
;
150 #if defined(__GNUWIN32__) && !defined(__CYGWIN10__) && !wxCHECK_W32API_VERSION( 1, 1 ) && !wxUSE_NORLANDER_HEADERS
151 #define wxFONTENUMPROC int(*)(ENUMLOGFONTEX *, NEWTEXTMETRICEX*, int, LPARAM)
153 #define wxFONTENUMPROC FONTENUMPROC
156 void wxFontEnumeratorHelper::DoEnumerate()
158 #ifndef __WXMICROWIN__
159 HDC hDC
= ::GetDC(NULL
);
162 ::EnumFontFamilies(hDC
,
163 m_facename
.empty() ? NULL
: m_facename
.wx_str(),
164 (wxFONTENUMPROC
)wxFontEnumeratorProc
,
168 lf
.lfCharSet
= (BYTE
)m_charset
;
169 wxStrlcpy(lf
.lfFaceName
, m_facename
.c_str(), WXSIZEOF(lf
.lfFaceName
));
170 lf
.lfPitchAndFamily
= 0;
171 ::EnumFontFamiliesEx(hDC
, &lf
, (wxFONTENUMPROC
)wxFontEnumeratorProc
,
172 (LPARAM
)this, 0 /* reserved */) ;
175 ::ReleaseDC(NULL
, hDC
);
179 bool wxFontEnumeratorHelper::OnFont(const LPLOGFONT lf
,
180 const LPTEXTMETRIC tm
) const
182 if ( m_enumEncodings
)
184 // is this a new charset?
185 int cs
= lf
->lfCharSet
;
186 if ( m_charsets
.Index(cs
) == wxNOT_FOUND
)
188 wxConstCast(this, wxFontEnumeratorHelper
)->m_charsets
.Add(cs
);
191 wxFontEncoding enc
= wxGetFontEncFromCharSet(cs
);
192 return m_fontEnum
->OnFontEncoding(lf
->lfFaceName
,
193 wxFontMapper::GetEncodingName(enc
));
194 #else // !wxUSE_FONTMAP
195 // Just use some unique and, hopefully, understandable, name.
196 return m_fontEnum
->OnFontEncoding
199 wxString::Format(wxS("Code page %d"), cs
)
201 #endif // wxUSE_FONTMAP/!wxUSE_FONTMAP
205 // continue enumeration
212 // check that it's a fixed pitch font (there is *no* error here, the
213 // flag name is misleading!)
214 if ( tm
->tmPitchAndFamily
& TMPF_FIXED_PITCH
)
216 // not a fixed pitch font
221 if ( m_charset
!= DEFAULT_CHARSET
)
223 // check that we have the right encoding
224 if ( lf
->lfCharSet
!= m_charset
)
229 else // enumerating fonts in all charsets
231 // we can get the same facename twice or more in this case because it
232 // may exist in several charsets but we only want to return one copy of
233 // it (note that this can't happen for m_charset != DEFAULT_CHARSET)
234 if ( m_facenames
.Index(lf
->lfFaceName
) != wxNOT_FOUND
)
236 // continue enumeration
240 wxConstCast(this, wxFontEnumeratorHelper
)->
241 m_facenames
.Add(lf
->lfFaceName
);
244 return m_fontEnum
->OnFacename(lf
->lfFaceName
);
247 // ----------------------------------------------------------------------------
249 // ----------------------------------------------------------------------------
251 bool wxFontEnumerator::EnumerateFacenames(wxFontEncoding encoding
,
254 wxFontEnumeratorHelper
fe(this);
255 if ( fe
.SetEncoding(encoding
) )
257 fe
.SetFixedOnly(fixedWidthOnly
);
261 // else: no such fonts, unknown encoding
266 bool wxFontEnumerator::EnumerateEncodings(const wxString
& family
)
268 wxFontEnumeratorHelper
fe(this);
269 fe
.SetFamily(family
);
275 // ----------------------------------------------------------------------------
277 // ----------------------------------------------------------------------------
279 #ifndef __WXMICROWIN__
280 int CALLBACK
wxFontEnumeratorProc(LPLOGFONT lplf
, LPTEXTMETRIC lptm
,
281 DWORD
WXUNUSED(dwStyle
), LPARAM lParam
)
284 // we used to process TrueType fonts only, but there doesn't seem to be any
285 // reasons to restrict ourselves to them here
287 // Get rid of any fonts that we don't want...
288 if ( dwStyle
!= TRUETYPE_FONTTYPE
)
290 // continue enumeration
295 wxFontEnumeratorHelper
*fontEnum
= (wxFontEnumeratorHelper
*)lParam
;
297 return fontEnum
->OnFont(lplf
, lptm
);
301 #endif // wxUSE_FONTENUM