1/////////////////////////////////////////////////////////////////////////////
2// Name:        src/common/fontenumcmn.cpp
3// Purpose:     wxFontEnumerator class
4// Author:      Vadim Zeitlin
5// Modified by:
6// Created:     7/5/2006
7// RCS-ID:      $Id: fontenumcmn.cpp 43727 2006-12-01 10:14:28Z VS $
8// Copyright:   (c) 1999-2003 Vadim Zeitlin <vadim@wxwindows.org>
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#include "wx/fontenum.h"
28
29// ============================================================================
30// implementation
31// ============================================================================
32
33// A simple wxFontEnumerator which doesn't perform any filtering and
34// just returns all facenames and encodings found in the system
35class WXDLLEXPORT wxSimpleFontEnumerator : public wxFontEnumerator
36{
37public:
38    wxSimpleFontEnumerator() { }
39
40    // called by EnumerateFacenames
41    virtual bool OnFacename(const wxString& facename)
42    {
43        m_arrFacenames.Add(facename);
44        return true;
45    }
46
47    // called by EnumerateEncodings
48    virtual bool OnFontEncoding(const wxString& WXUNUSED(facename),
49                                const wxString& encoding)
50    {
51        m_arrEncodings.Add(encoding);
52        return true;
53    }
54
55public:
56    wxArrayString m_arrFacenames, m_arrEncodings;
57};
58
59
60/* static */
61wxArrayString wxFontEnumerator::GetFacenames(wxFontEncoding encoding, bool fixedWidthOnly)
62{
63    wxSimpleFontEnumerator temp;
64    temp.EnumerateFacenames(encoding, fixedWidthOnly);
65    return temp.m_arrFacenames;
66}
67
68/* static */
69wxArrayString wxFontEnumerator::GetEncodings(const wxString& facename)
70{
71    wxSimpleFontEnumerator temp;
72    temp.EnumerateEncodings(facename);
73    return temp.m_arrEncodings;
74}
75
76/* static */
77bool wxFontEnumerator::IsValidFacename(const wxString &facename)
78{
79    // we cache the result of wxFontEnumerator::GetFacenames supposing that
80    // the array of face names won't change in the session of this program
81    static wxArrayString s_arr = wxFontEnumerator::GetFacenames();
82
83#ifdef __WXMSW__
84    // Quoting the MSDN:
85    //     "MS Shell Dlg is a mapping mechanism that enables
86    //     U.S. English Microsoft Windows NT, and Microsoft Windows 2000 to
87    //     support locales that have characters that are not contained in code
88    //     page 1252. It is not a font but a face name for a nonexistent font."
89    // Thus we need to consider "Ms Shell Dlg" and "Ms Shell Dlg 2" as valid
90    // font face names even if they are enumerated by wxFontEnumerator
91    if (facename.IsSameAs(wxT("Ms Shell Dlg"), false) ||
92        facename.IsSameAs(wxT("Ms Shell Dlg 2"), false))
93        return true;
94#endif
95
96    // is given font face name a valid one ?
97    if (s_arr.Index(facename, false) == wxNOT_FOUND)
98        return false;
99
100    return true;
101}
102
103#ifdef wxHAS_UTF8_FONTS
104bool wxFontEnumerator::EnumerateEncodingsUTF8(const wxString& facename)
105{
106    // name of UTF-8 encoding: no need to use wxFontMapper for it as it's
107    // unlikely to change
108    const wxString utf8(_T("UTF-8"));
109
110    // all fonts are in UTF-8 only if this code is used
111    if ( !facename.empty() )
112    {
113        OnFontEncoding(facename, utf8);
114        return true;
115    }
116
117    // so enumerating all facenames supporting this encoding is the same as
118    // enumerating all facenames
119    const wxArrayString facenames(GetFacenames(wxFONTENCODING_UTF8));
120    const size_t count = facenames.size();
121    if ( !count )
122        return false;
123
124    for ( size_t n = 0; n < count; n++ )
125    {
126        OnFontEncoding(facenames[n], utf8);
127    }
128
129    return true;
130}
131#endif // wxHAS_UTF8_FONTS
132