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