Coverity CID117: Ignore returned_null here, because we know it can't happen because...
[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 // for compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifndef WX_PRECOMP
24 #include "wx/dynarray.h"
25 #endif
26
27 #include "wx/string.h"
28 #include "wx/regex.h"
29 #include "wx/utils.h"
30 #include "wx/app.h"
31 #include "wx/fontmap.h"
32 #include "wx/fontenum.h"
33 #include "wx/fontutil.h"
34 #include "wx/encinfo.h"
35
36 // ----------------------------------------------------------------------------
37 // Pango
38 // ----------------------------------------------------------------------------
39
40 #if wxUSE_PANGO
41
42 #include "pango/pango.h"
43
44 #ifdef __WXGTK20__
45 #include "gtk/gtk.h"
46 extern GtkWidget *wxGetRootWindow();
47 #endif // __WXGTK20__
48
49 extern "C" int wxCMPFUNC_CONV
50 wxCompareFamilies (const void *a, const void *b)
51 {
52 const char *a_name = pango_font_family_get_name (*(PangoFontFamily **)a);
53 const char *b_name = pango_font_family_get_name (*(PangoFontFamily **)b);
54
55 return g_utf8_collate (a_name, b_name);
56 }
57
58 // I admit I don't yet understand encodings with Pango
59 bool wxFontEnumerator::EnumerateFacenames(wxFontEncoding encoding,
60 bool fixedWidthOnly)
61 {
62 #if defined(__WXGTK20__) || !defined(HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE)
63 if ( fixedWidthOnly
64 #if defined(__WXGTK24__)
65 && (gtk_check_version(2,4,0) != NULL)
66 #endif
67 )
68 {
69 OnFacename( wxT("monospace") );
70 }
71 else
72 #endif // __WXGTK20__ || !HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE
73 {
74 PangoFontFamily **families = NULL;
75 gint n_families = 0;
76 pango_context_list_families (
77 #ifdef __WXGTK20__
78 gtk_widget_get_pango_context( wxGetRootWindow() ),
79 #else
80 wxTheApp->GetPangoContext(),
81 #endif
82 &families, &n_families );
83 qsort (families, n_families, sizeof (PangoFontFamily *), wxCompareFamilies);
84
85 for (int i=0; i<n_families; i++)
86 {
87 #if defined(__WXGTK24__) || defined(HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE)
88 if (!fixedWidthOnly || (
89 #ifdef __WXGTK24__
90 !gtk_check_version(2,4,0) &&
91 #endif
92 pango_font_family_is_monospace(families[i])
93 ) )
94 #endif
95 {
96 const gchar *name = pango_font_family_get_name(families[i]);
97 OnFacename(wxString(name, wxConvUTF8));
98 }
99 }
100 g_free(families);
101 }
102
103 return true;
104 }
105
106 bool wxFontEnumerator::EnumerateEncodings(const wxString& family)
107 {
108 return false;
109 }
110
111
112 #else
113 // Pango
114
115 #ifdef __VMS__ // Xlib.h for VMS is not (yet) compatible with C++
116 // The resulting warnings are switched off here
117 #pragma message disable nosimpint
118 #endif
119 #include <X11/Xlib.h>
120 #ifdef __VMS__
121 #pragma message enable nosimpint
122 #endif
123
124 // ----------------------------------------------------------------------------
125 // private functions
126 // ----------------------------------------------------------------------------
127
128 // create the list of all fonts with the given spacing and encoding
129 static char **CreateFontList(wxChar spacing, wxFontEncoding encoding,
130 int *nFonts);
131
132 // extract all font families from the given font list and call our
133 // OnFacename() for each of them
134 static bool ProcessFamiliesFromFontList(wxFontEnumerator *This,
135 char **fonts,
136 int nFonts);
137
138
139 // ----------------------------------------------------------------------------
140 // private types
141 // ----------------------------------------------------------------------------
142
143 // ============================================================================
144 // implementation
145 // ============================================================================
146
147 // ----------------------------------------------------------------------------
148 // helpers
149 // ----------------------------------------------------------------------------
150
151 #if !wxUSE_NANOX
152 static char **CreateFontList(wxChar spacing,
153 wxFontEncoding encoding,
154 int *nFonts)
155 {
156 wxNativeEncodingInfo info;
157 wxGetNativeFontEncoding(encoding, &info);
158
159 #if wxUSE_FONTMAP
160 if ( !wxTestFontEncoding(info) )
161 {
162 // ask font mapper for a replacement
163 (void)wxFontMapper::Get()->GetAltForEncoding(encoding, &info);
164 }
165 #endif // wxUSE_FONTMAP
166
167 wxString pattern;
168 pattern.Printf(wxT("-*-*-*-*-*-*-*-*-*-*-%c-*-%s-%s"),
169 spacing,
170 info.xregistry.c_str(),
171 info.xencoding.c_str());
172
173 // get the list of all fonts
174 return XListFonts((Display *)wxGetDisplay(), pattern.mb_str(), 32767, nFonts);
175 }
176
177 static bool ProcessFamiliesFromFontList(wxFontEnumerator *This,
178 char **fonts,
179 int nFonts)
180 {
181 #if wxUSE_REGEX
182 wxRegEx re(wxT("^(-[^-]*){14}$"), wxRE_NOSUB);
183 #endif // wxUSE_REGEX
184
185 // extract the list of (unique) font families
186 wxSortedArrayString families;
187 for ( int n = 0; n < nFonts; n++ )
188 {
189 char *font = fonts[n];
190 #if wxUSE_REGEX
191 if ( !re.Matches(font) )
192 #else // !wxUSE_REGEX
193 if ( !wxString(font).Matches(wxT("-*-*-*-*-*-*-*-*-*-*-*-*-*-*")) )
194 #endif // wxUSE_REGEX/!wxUSE_REGEX
195 {
196 // it's not a full font name (probably an alias)
197 continue;
198 }
199
200 // coverity[returned_null]
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.empty() ? 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.empty() && (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 // !wxUSE_PANGO