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 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
30 #include "wx/gdicmn.h"
32 #include "wx/encinfo.h"
35 #include "wx/msw/private.h"
37 #include "wx/fontutil.h"
38 #include "wx/fontenum.h"
39 #include "wx/fontmap.h"
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
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
48 class wxFontEnumeratorHelper
51 wxFontEnumeratorHelper(wxFontEnumerator
*fontEnum
);
53 // control what exactly are we enumerating
54 // we enumerate fonts with given enocding
55 bool SetEncoding(wxFontEncoding encoding
);
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
);
61 // call to start enumeration
64 // called by our font enumeration proc
65 bool OnFont(const LPLOGFONT lf
, const LPTEXTMETRIC tm
) const;
68 // the object we forward calls to OnFont() to
69 wxFontEnumerator
*m_fontEnum
;
71 // if != -1, enum only fonts which have this encoding
74 // if not empty, enum only the fonts with this facename
77 // if not empty, enum only the fonts in this family
80 // if true, enum only fixed fonts
83 // if true, we enumerate the encodings, not fonts
86 // the list of charsets we already found while enumerating charsets
87 wxArrayInt m_charsets
;
89 // the list of facenames we already found while enumerating facenames
90 wxArrayString m_facenames
;
92 DECLARE_NO_COPY_CLASS(wxFontEnumeratorHelper
)
95 // ----------------------------------------------------------------------------
97 // ----------------------------------------------------------------------------
99 #ifndef __WXMICROWIN__
100 int CALLBACK
wxFontEnumeratorProc(LPLOGFONT lplf
, LPTEXTMETRIC lptm
,
101 DWORD dwStyle
, LONG lParam
);
104 // ============================================================================
106 // ============================================================================
108 // ----------------------------------------------------------------------------
109 // wxFontEnumeratorHelper
110 // ----------------------------------------------------------------------------
112 wxFontEnumeratorHelper::wxFontEnumeratorHelper(wxFontEnumerator
*fontEnum
)
114 m_fontEnum
= fontEnum
;
115 m_charset
= DEFAULT_CHARSET
;
117 m_enumEncodings
= false;
120 void wxFontEnumeratorHelper::SetFamily(const wxString
& family
)
122 m_enumEncodings
= true;
126 bool wxFontEnumeratorHelper::SetEncoding(wxFontEncoding encoding
)
128 if ( encoding
!= wxFONTENCODING_SYSTEM
)
130 wxNativeEncodingInfo info
;
131 if ( !wxGetNativeFontEncoding(encoding
, &info
) )
134 if ( !wxFontMapper::Get()->GetAltForEncoding(encoding
, &info
) )
135 #endif // wxUSE_FONTMAP
137 // no such encodings at all
142 m_charset
= info
.charset
;
143 m_facename
= info
.facename
;
149 #if defined(__GNUWIN32__) && !defined(__CYGWIN10__) && !wxCHECK_W32API_VERSION( 1, 1 ) && !wxUSE_NORLANDER_HEADERS
150 #define wxFONTENUMPROC int(*)(ENUMLOGFONTEX *, NEWTEXTMETRICEX*, int, LPARAM)
152 #define wxFONTENUMPROC FONTENUMPROC
155 void wxFontEnumeratorHelper::DoEnumerate()
157 #ifndef __WXMICROWIN__
158 HDC hDC
= ::GetDC(NULL
);
161 ::EnumFontFamilies(hDC
, m_facename
, (wxFONTENUMPROC
)wxFontEnumeratorProc
,
165 lf
.lfCharSet
= (BYTE
)m_charset
;
166 wxStrncpy(lf
.lfFaceName
, m_facename
, WXSIZEOF(lf
.lfFaceName
));
167 lf
.lfPitchAndFamily
= 0;
168 ::EnumFontFamiliesEx(hDC
, &lf
, (wxFONTENUMPROC
)wxFontEnumeratorProc
,
169 (LPARAM
)this, 0 /* reserved */) ;
172 ::ReleaseDC(NULL
, hDC
);
176 bool wxFontEnumeratorHelper::OnFont(const LPLOGFONT lf
,
177 const LPTEXTMETRIC tm
) const
179 if ( m_enumEncodings
)
181 // is this a new charset?
182 int cs
= lf
->lfCharSet
;
183 if ( m_charsets
.Index(cs
) == wxNOT_FOUND
)
185 wxConstCast(this, wxFontEnumeratorHelper
)->m_charsets
.Add(cs
);
187 wxFontEncoding enc
= wxGetFontEncFromCharSet(cs
);
188 return m_fontEnum
->OnFontEncoding(lf
->lfFaceName
,
189 wxFontMapper::GetEncodingName(enc
));
193 // continue enumeration
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
)
204 // not a fixed pitch font
209 if ( m_charset
!= DEFAULT_CHARSET
)
211 // check that we have the right encoding
212 if ( lf
->lfCharSet
!= m_charset
)
217 else // enumerating fonts in all charsets
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
)
224 // continue enumeration
228 wxConstCast(this, wxFontEnumeratorHelper
)->
229 m_facenames
.Add(lf
->lfFaceName
);
232 return m_fontEnum
->OnFacename(lf
->lfFaceName
);
235 // ----------------------------------------------------------------------------
237 // ----------------------------------------------------------------------------
239 bool wxFontEnumerator::EnumerateFacenames(wxFontEncoding encoding
,
242 wxFontEnumeratorHelper
fe(this);
243 if ( fe
.SetEncoding(encoding
) )
245 fe
.SetFixedOnly(fixedWidthOnly
);
249 // else: no such fonts, unknown encoding
254 bool wxFontEnumerator::EnumerateEncodings(const wxString
& family
)
256 wxFontEnumeratorHelper
fe(this);
257 fe
.SetFamily(family
);
263 // ----------------------------------------------------------------------------
265 // ----------------------------------------------------------------------------
267 #ifndef __WXMICROWIN__
268 int CALLBACK
wxFontEnumeratorProc(LPLOGFONT lplf
, LPTEXTMETRIC lptm
,
269 DWORD
WXUNUSED(dwStyle
), LONG lParam
)
272 // we used to process TrueType fonts only, but there doesn't seem to be any
273 // reasons to restrict ourselves to them here
275 // Get rid of any fonts that we don't want...
276 if ( dwStyle
!= TRUETYPE_FONTTYPE
)
278 // continue enumeration
283 wxFontEnumeratorHelper
*fontEnum
= (wxFontEnumeratorHelper
*)lParam
;
285 return fontEnum
->OnFont(lplf
, lptm
);
289 #endif // wxUSE_FONTMAP