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