]> git.saurik.com Git - wxWidgets.git/blob - src/unix/fontenum.cpp
a577af0357d99ff37e08bcb26207a97ee49415df
[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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "fontenum.h"
22 #endif
23
24 // for compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #include "wx/dynarray.h"
28 #include "wx/string.h"
29 #include "wx/regex.h"
30 #include "wx/utils.h"
31 #include "wx/app.h"
32 #include "wx/fontmap.h"
33 #include "wx/fontenum.h"
34 #include "wx/fontutil.h"
35 #include "wx/encinfo.h"
36
37 // ----------------------------------------------------------------------------
38 // Pango
39 // ----------------------------------------------------------------------------
40
41 #if wxUSE_PANGO
42
43 #include "pango/pango.h"
44
45 #ifdef __WXGTK20__
46 #include "gtk/gtk.h"
47 extern GtkWidget *wxGetRootWindow();
48 #endif
49
50 static int
51 cmp_families (const void *a, const void *b)
52 {
53 const char *a_name = pango_font_family_get_name (*(PangoFontFamily **)a);
54 const char *b_name = pango_font_family_get_name (*(PangoFontFamily **)b);
55
56 return g_utf8_collate (a_name, b_name);
57 }
58
59 // I admit I don't yet understand encodings with Pango
60 bool wxFontEnumerator::EnumerateFacenames(wxFontEncoding encoding,
61 bool fixedWidthOnly)
62 {
63 #if defined(__WXGTK20__) || !defined(HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE)
64 if ( fixedWidthOnly
65 #if defined(__WXGTK24__)
66 && (gtk_check_version(2,4,0) != NULL)
67 #endif
68 )
69 {
70 OnFacename( wxT("monospace") );
71 }
72 else
73 #endif
74 {
75 PangoFontFamily **families = NULL;
76 gint n_families = 0;
77 pango_context_list_families (
78 #ifdef __WXGTK20__
79 gtk_widget_get_pango_context( wxGetRootWindow() ),
80 #else
81 wxTheApp->GetPangoContext(),
82 #endif
83 &families, &n_families );
84 qsort (families, n_families, sizeof (PangoFontFamily *), cmp_families);
85
86 for (int i=0; i<n_families; i++)
87 {
88 #if defined(__WXGTK24__) || defined(HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE)
89 if (!fixedWidthOnly || (
90 #ifdef __WXGTK24__
91 !gtk_check_version(2,4,0) &&
92 #endif
93 pango_font_family_is_monospace(families[i])
94 ) )
95 #endif
96 {
97 const gchar *name = pango_font_family_get_name(families[i]);
98 OnFacename(wxString(name, wxConvUTF8));
99 }
100 }
101 g_free(families);
102 }
103
104 return TRUE;
105 }
106
107 bool wxFontEnumerator::EnumerateEncodings(const wxString& family)
108 {
109 return FALSE;
110 }
111
112
113 #else
114 // Pango
115
116 #ifdef __VMS__ // Xlib.h for VMS is not (yet) compatible with C++
117 // The resulting warnings are switched off here
118 #pragma message disable nosimpint
119 #endif
120 #include <X11/Xlib.h>
121 #ifdef __VMS__
122 #pragma message enable nosimpint
123 #endif
124
125 // ----------------------------------------------------------------------------
126 // private functions
127 // ----------------------------------------------------------------------------
128
129 // create the list of all fonts with the given spacing and encoding
130 static char **CreateFontList(wxChar spacing, wxFontEncoding encoding,
131 int *nFonts);
132
133 // extract all font families from the given font list and call our
134 // OnFacename() for each of them
135 static bool ProcessFamiliesFromFontList(wxFontEnumerator *This,
136 char **fonts,
137 int nFonts);
138
139
140 // ----------------------------------------------------------------------------
141 // private types
142 // ----------------------------------------------------------------------------
143
144 // ============================================================================
145 // implementation
146 // ============================================================================
147
148 // ----------------------------------------------------------------------------
149 // helpers
150 // ----------------------------------------------------------------------------
151
152 #if !wxUSE_NANOX
153 static char **CreateFontList(wxChar spacing,
154 wxFontEncoding encoding,
155 int *nFonts)
156 {
157 wxNativeEncodingInfo info;
158 wxGetNativeFontEncoding(encoding, &info);
159
160 #if wxUSE_FONTMAP
161 if ( !wxTestFontEncoding(info) )
162 {
163 // ask font mapper for a replacement
164 (void)wxFontMapper::Get()->GetAltForEncoding(encoding, &info);
165 }
166 #endif // wxUSE_FONTMAP
167
168 wxString pattern;
169 pattern.Printf(wxT("-*-*-*-*-*-*-*-*-*-*-%c-*-%s-%s"),
170 spacing,
171 info.xregistry.c_str(),
172 info.xencoding.c_str());
173
174 // get the list of all fonts
175 return XListFonts((Display *)wxGetDisplay(), pattern.mb_str(), 32767, nFonts);
176 }
177
178 static bool ProcessFamiliesFromFontList(wxFontEnumerator *This,
179 char **fonts,
180 int nFonts)
181 {
182 #if wxUSE_REGEX
183 wxRegEx re(wxT("^(-[^-]*){14}$"), wxRE_NOSUB);
184 #endif // wxUSE_REGEX
185
186 // extract the list of (unique) font families
187 wxSortedArrayString families;
188 for ( int n = 0; n < nFonts; n++ )
189 {
190 char *font = fonts[n];
191 #if wxUSE_REGEX
192 if ( !re.Matches(font) )
193 #else // !wxUSE_REGEX
194 if ( !wxString(font).Matches(wxT("-*-*-*-*-*-*-*-*-*-*-*-*-*-*")) )
195 #endif // wxUSE_REGEX/!wxUSE_REGEX
196 {
197 // it's not a full font name (probably an alias)
198 continue;
199 }
200
201 char *dash = strchr(font + 1, '-');
202 char *family = dash + 1;
203 dash = strchr(family, '-');
204 *dash = '\0'; // !NULL because Matches() above succeeded
205 wxString fam(family);
206
207 if ( families.Index(fam) == wxNOT_FOUND )
208 {
209 if ( !This->OnFacename(fam) )
210 {
211 // stop enumerating
212 return FALSE;
213 }
214
215 families.Add(fam);
216 }
217 //else: already seen
218 }
219
220 return TRUE;
221 }
222 #endif
223 // wxUSE_NANOX
224
225 // ----------------------------------------------------------------------------
226 // wxFontEnumerator
227 // ----------------------------------------------------------------------------
228
229 bool wxFontEnumerator::EnumerateFacenames(wxFontEncoding encoding,
230 bool fixedWidthOnly)
231 {
232 #if wxUSE_NANOX
233 return FALSE;
234 #else
235 int nFonts;
236 char **fonts;
237
238 if ( fixedWidthOnly )
239 {
240 bool cont = TRUE;
241 fonts = CreateFontList(wxT('m'), encoding, &nFonts);
242 if ( fonts )
243 {
244 cont = ProcessFamiliesFromFontList(this, fonts, nFonts);
245
246 XFreeFontNames(fonts);
247 }
248
249 if ( !cont )
250 {
251 return TRUE;
252 }
253
254 fonts = CreateFontList(wxT('c'), encoding, &nFonts);
255 if ( !fonts )
256 {
257 return TRUE;
258 }
259 }
260 else
261 {
262 fonts = CreateFontList(wxT('*'), encoding, &nFonts);
263
264 if ( !fonts )
265 {
266 // it's ok if there are no fonts in given encoding - but it's not
267 // ok if there are no fonts at all
268 wxASSERT_MSG(encoding != wxFONTENCODING_SYSTEM,
269 wxT("No fonts at all on this system?"));
270
271 return FALSE;
272 }
273 }
274
275 (void)ProcessFamiliesFromFontList(this, fonts, nFonts);
276
277 XFreeFontNames(fonts);
278 return TRUE;
279 #endif
280 // wxUSE_NANOX
281 }
282
283 bool wxFontEnumerator::EnumerateEncodings(const wxString& family)
284 {
285 #if wxUSE_NANOX
286 return FALSE;
287 #else
288 wxString pattern;
289 pattern.Printf(wxT("-*-%s-*-*-*-*-*-*-*-*-*-*-*-*"),
290 family.IsEmpty() ? wxT("*") : family.c_str());
291
292 // get the list of all fonts
293 int nFonts;
294 char **fonts = XListFonts((Display *)wxGetDisplay(), pattern.mb_str(),
295 32767, &nFonts);
296
297 if ( !fonts )
298 {
299 // unknown family?
300 return FALSE;
301 }
302
303 // extract the list of (unique) encodings
304 wxSortedArrayString encodings;
305 for ( int n = 0; n < nFonts; n++ )
306 {
307 char *font = fonts[n];
308 if ( !wxString(font).Matches(wxT("-*-*-*-*-*-*-*-*-*-*-*-*-*-*")) )
309 {
310 // it's not a full font name (probably an alias)
311 continue;
312 }
313
314 // extract the family
315 char *dash = strchr(font + 1, '-');
316 char *familyFont = dash + 1;
317 dash = strchr(familyFont, '-');
318 *dash = '\0'; // !NULL because Matches() above succeeded
319
320 if ( !family.IsEmpty() && (family != familyFont) )
321 {
322 // family doesn't match
323 continue;
324 }
325
326 // now extract the registry/encoding
327 char *p = dash + 1; // just after the dash after family
328 dash = strrchr(p, '-');
329
330 wxString registry(dash + 1);
331 *dash = '\0';
332
333 dash = strrchr(p, '-');
334 wxString encoding(dash + 1);
335
336 encoding << wxT('-') << registry;
337 if ( encodings.Index(encoding) == wxNOT_FOUND )
338 {
339 if ( !OnFontEncoding(familyFont, encoding) )
340 {
341 break;
342 }
343
344 encodings.Add(encoding);
345 }
346 //else: already had this one
347 }
348
349 XFreeFontNames(fonts);
350
351 return TRUE;
352 #endif
353 // wxUSE_NANOX
354 }
355
356 #endif
357 // __WXGTK20__