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