]> git.saurik.com Git - wxWidgets.git/blob - src/unix/fontenum.cpp
merged MapBitmap() from 2.2 branch
[wxWidgets.git] / src / unix / fontenum.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/unix/fontenum.cpp
3 // Purpose: wxFontEnumerator class for X11/GDK
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 01.10.99
7 // RCS-ID: $Id$
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "fontenum.h"
22 #endif
23
24 #include "wx/defs.h"
25 #include "wx/dynarray.h"
26 #include "wx/string.h"
27 #include "wx/utils.h"
28
29 #include "wx/fontmap.h"
30 #include "wx/fontenum.h"
31 #include "wx/fontutil.h"
32
33 #ifdef __VMS__ // Xlib.h for VMS is not (yet) compatible with C++
34 // The resulting warnings are switched off here
35 #pragma message disable nosimpint
36 #endif
37 #include <X11/Xlib.h>
38 #ifdef __VMS__
39 #pragma message enable nosimpint
40 #endif
41
42 // ----------------------------------------------------------------------------
43 // private functions
44 // ----------------------------------------------------------------------------
45
46 // create the list of all fonts with the given spacing and encoding
47 static char **CreateFontList(wxChar spacing, wxFontEncoding encoding,
48 int *nFonts);
49
50 // extract all font families from the given font list and call our
51 // OnFacename() for each of them
52 static bool ProcessFamiliesFromFontList(wxFontEnumerator *This,
53 char **fonts,
54 int nFonts);
55
56
57 // ----------------------------------------------------------------------------
58 // private types
59 // ----------------------------------------------------------------------------
60
61 // ============================================================================
62 // implementation
63 // ============================================================================
64
65 // ----------------------------------------------------------------------------
66 // helpers
67 // ----------------------------------------------------------------------------
68
69 static char **CreateFontList(wxChar spacing,
70 wxFontEncoding encoding,
71 int *nFonts)
72 {
73 wxNativeEncodingInfo info;
74 wxGetNativeFontEncoding(encoding, &info);
75
76 #if wxUSE_FONTMAP
77 if ( !wxTestFontEncoding(info) )
78 {
79 // ask font mapper for a replacement
80 (void)wxTheFontMapper->GetAltForEncoding(encoding, &info);
81 }
82 #endif // wxUSE_FONTMAP
83
84 wxString pattern;
85 pattern.Printf(wxT("-*-*-*-*-*-*-*-*-*-*-%c-*-%s-%s"),
86 spacing,
87 info.xregistry.c_str(),
88 info.xencoding.c_str());
89
90 // get the list of all fonts
91 return XListFonts((Display *)wxGetDisplay(), pattern.mb_str(), 32767, nFonts);
92 }
93
94 static bool ProcessFamiliesFromFontList(wxFontEnumerator *This,
95 char **fonts,
96 int nFonts)
97 {
98 // extract the list of (unique) font families
99 wxSortedArrayString families;
100 for ( int n = 0; n < nFonts; n++ )
101 {
102 char *font = fonts[n];
103 if ( !wxString(font).Matches(wxT("-*-*-*-*-*-*-*-*-*-*-*-*-*-*")) )
104 {
105 // it's not a full font name (probably an alias)
106 continue;
107 }
108
109 char *dash = strchr(font + 1, '-');
110 char *family = dash + 1;
111 dash = strchr(family, '-');
112 *dash = '\0'; // !NULL because Matches() above succeeded
113 wxString fam(family);
114
115 if ( families.Index(fam) == wxNOT_FOUND )
116 {
117 if ( !This->OnFacename(fam) )
118 {
119 // stop enumerating
120 return FALSE;
121 }
122
123 families.Add(fam);
124 }
125 //else: already seen
126 }
127
128 return TRUE;
129 }
130
131 // ----------------------------------------------------------------------------
132 // wxFontEnumerator
133 // ----------------------------------------------------------------------------
134
135 bool wxFontEnumerator::EnumerateFacenames(wxFontEncoding encoding,
136 bool fixedWidthOnly)
137 {
138 int nFonts;
139 char **fonts;
140
141 if ( fixedWidthOnly )
142 {
143 bool cont = TRUE;
144 fonts = CreateFontList(wxT('m'), encoding, &nFonts);
145 if ( fonts )
146 {
147 cont = ProcessFamiliesFromFontList(this, fonts, nFonts);
148
149 XFreeFontNames(fonts);
150 }
151
152 if ( !cont )
153 {
154 return TRUE;
155 }
156
157 fonts = CreateFontList(wxT('c'), encoding, &nFonts);
158 if ( !fonts )
159 {
160 return TRUE;
161 }
162 }
163 else
164 {
165 fonts = CreateFontList(wxT('*'), encoding, &nFonts);
166
167 if ( !fonts )
168 {
169 // it's ok if there are no fonts in given encoding - but it's not
170 // ok if there are no fonts at all
171 wxASSERT_MSG(encoding != wxFONTENCODING_SYSTEM,
172 wxT("No fonts at all on this system?"));
173
174 return FALSE;
175 }
176 }
177
178 (void)ProcessFamiliesFromFontList(this, fonts, nFonts);
179
180 XFreeFontNames(fonts);
181
182 return TRUE;
183 }
184
185 bool wxFontEnumerator::EnumerateEncodings(const wxString& family)
186 {
187 wxString pattern;
188 pattern.Printf(wxT("-*-%s-*-*-*-*-*-*-*-*-*-*-*-*"),
189 family.IsEmpty() ? wxT("*") : family.c_str());
190
191 // get the list of all fonts
192 int nFonts;
193 char **fonts = XListFonts((Display *)wxGetDisplay(), pattern.mb_str(),
194 32767, &nFonts);
195
196 if ( !fonts )
197 {
198 // unknown family?
199 return FALSE;
200 }
201
202 // extract the list of (unique) encodings
203 wxSortedArrayString encodings;
204 for ( int n = 0; n < nFonts; n++ )
205 {
206 char *font = fonts[n];
207 if ( !wxString(font).Matches(wxT("-*-*-*-*-*-*-*-*-*-*-*-*-*-*")) )
208 {
209 // it's not a full font name (probably an alias)
210 continue;
211 }
212
213 // extract the family
214 char *dash = strchr(font + 1, '-');
215 char *familyFont = dash + 1;
216 dash = strchr(familyFont, '-');
217 *dash = '\0'; // !NULL because Matches() above succeeded
218
219 if ( !family.IsEmpty() && (family != familyFont) )
220 {
221 // family doesn't match
222 continue;
223 }
224
225 // now extract the registry/encoding
226 char *p = dash + 1; // just after the dash after family
227 dash = strrchr(p, '-');
228
229 wxString registry(dash + 1);
230 *dash = '\0';
231
232 dash = strrchr(p, '-');
233 wxString encoding(dash + 1);
234
235 encoding << wxT('-') << registry;
236 if ( encodings.Index(encoding) == wxNOT_FOUND )
237 {
238 if ( !OnFontEncoding(familyFont, encoding) )
239 {
240 break;
241 }
242
243 encodings.Add(encoding);
244 }
245 //else: already had this one
246 }
247
248 XFreeFontNames(fonts);
249
250 return TRUE;
251 }