1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/unix/fontenum.cpp
3 // Purpose: wxFontEnumerator class for X11/GDK
4 // Author: Vadim Zeitlin
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // for compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
23 #include "wx/fontenum.h"
26 #include "wx/dynarray.h"
27 #include "wx/string.h"
33 #include "wx/fontmap.h"
34 #include "wx/fontutil.h"
35 #include "wx/encinfo.h"
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
43 #include "pango/pango.h"
47 extern GtkWidget
*wxGetRootWindow();
50 extern "C" int wxCMPFUNC_CONV
51 wxCompareFamilies (const void *a
, const void *b
)
53 const char *a_name
= pango_font_family_get_name (*(PangoFontFamily
**)a
);
54 const char *b_name
= pango_font_family_get_name (*(PangoFontFamily
**)b
);
56 return g_utf8_collate (a_name
, b_name
);
59 // I admit I don't yet understand encodings with Pango
60 bool wxFontEnumerator::EnumerateFacenames(wxFontEncoding encoding
,
63 #if defined(__WXGTK20__) || !defined(HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE)
65 #if defined(__WXGTK24__)
66 && (gtk_check_version(2,4,0) != NULL
)
70 OnFacename( wxT("monospace") );
73 #endif // __WXGTK20__ || !HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE
75 PangoFontFamily
**families
= NULL
;
77 pango_context_list_families (
79 gtk_widget_get_pango_context( wxGetRootWindow() ),
81 wxTheApp
->GetPangoContext(),
83 &families
, &n_families
);
84 qsort (families
, n_families
, sizeof (PangoFontFamily
*), wxCompareFamilies
);
86 for (int i
=0; i
<n_families
; i
++)
88 #if defined(__WXGTK24__) || defined(HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE)
89 if (!fixedWidthOnly
|| (
91 !gtk_check_version(2,4,0) &&
93 pango_font_family_is_monospace(families
[i
])
97 const gchar
*name
= pango_font_family_get_name(families
[i
]);
98 OnFacename(wxString(name
, wxConvUTF8
));
107 bool wxFontEnumerator::EnumerateEncodings(const wxString
& family
)
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
120 #include <X11/Xlib.h>
122 #pragma message enable nosimpint
125 // ----------------------------------------------------------------------------
127 // ----------------------------------------------------------------------------
129 // create the list of all fonts with the given spacing and encoding
130 static char **CreateFontList(wxChar spacing
, wxFontEncoding encoding
,
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
,
140 // ----------------------------------------------------------------------------
142 // ----------------------------------------------------------------------------
144 // ============================================================================
146 // ============================================================================
148 // ----------------------------------------------------------------------------
150 // ----------------------------------------------------------------------------
153 static char **CreateFontList(wxChar spacing
,
154 wxFontEncoding encoding
,
157 wxNativeEncodingInfo info
;
158 wxGetNativeFontEncoding(encoding
, &info
);
161 if ( !wxTestFontEncoding(info
) )
163 // ask font mapper for a replacement
164 (void)wxFontMapper::Get()->GetAltForEncoding(encoding
, &info
);
166 #endif // wxUSE_FONTMAP
169 pattern
.Printf(wxT("-*-*-*-*-*-*-*-*-*-*-%c-*-%s-%s"),
171 info
.xregistry
.c_str(),
172 info
.xencoding
.c_str());
174 // get the list of all fonts
175 return XListFonts((Display
*)wxGetDisplay(), pattern
.mb_str(), 32767, nFonts
);
178 static bool ProcessFamiliesFromFontList(wxFontEnumerator
*This
,
183 wxRegEx
re(wxT("^(-[^-]*){14}$"), wxRE_NOSUB
);
184 #endif // wxUSE_REGEX
186 // extract the list of (unique) font families
187 wxSortedArrayString families
;
188 for ( int n
= 0; n
< nFonts
; n
++ )
190 char *font
= fonts
[n
];
192 if ( !re
.Matches(font
) )
193 #else // !wxUSE_REGEX
194 if ( !wxString(font
).Matches(wxT("-*-*-*-*-*-*-*-*-*-*-*-*-*-*")) )
195 #endif // wxUSE_REGEX/!wxUSE_REGEX
197 // it's not a full font name (probably an alias)
201 // coverity[returned_null]
202 char *dash
= strchr(font
+ 1, '-');
203 char *family
= dash
+ 1;
204 dash
= strchr(family
, '-');
205 *dash
= '\0'; // !NULL because Matches() above succeeded
206 wxString
fam(family
);
208 if ( families
.Index(fam
) == wxNOT_FOUND
)
210 if ( !This
->OnFacename(fam
) )
226 // ----------------------------------------------------------------------------
228 // ----------------------------------------------------------------------------
230 bool wxFontEnumerator::EnumerateFacenames(wxFontEncoding encoding
,
239 if ( fixedWidthOnly
)
242 fonts
= CreateFontList(wxT('m'), encoding
, &nFonts
);
245 cont
= ProcessFamiliesFromFontList(this, fonts
, nFonts
);
247 XFreeFontNames(fonts
);
255 fonts
= CreateFontList(wxT('c'), encoding
, &nFonts
);
263 fonts
= CreateFontList(wxT('*'), encoding
, &nFonts
);
267 // it's ok if there are no fonts in given encoding - but it's not
268 // ok if there are no fonts at all
269 wxASSERT_MSG(encoding
!= wxFONTENCODING_SYSTEM
,
270 wxT("No fonts at all on this system?"));
276 (void)ProcessFamiliesFromFontList(this, fonts
, nFonts
);
278 XFreeFontNames(fonts
);
284 bool wxFontEnumerator::EnumerateEncodings(const wxString
& family
)
290 pattern
.Printf(wxT("-*-%s-*-*-*-*-*-*-*-*-*-*-*-*"),
291 family
.empty() ? wxT("*") : family
.c_str());
293 // get the list of all fonts
295 char **fonts
= XListFonts((Display
*)wxGetDisplay(), pattern
.mb_str(),
304 // extract the list of (unique) encodings
305 wxSortedArrayString encodings
;
306 for ( int n
= 0; n
< nFonts
; n
++ )
308 char *font
= fonts
[n
];
309 if ( !wxString(font
).Matches(wxT("-*-*-*-*-*-*-*-*-*-*-*-*-*-*")) )
311 // it's not a full font name (probably an alias)
315 // extract the family
316 char *dash
= strchr(font
+ 1, '-');
317 char *familyFont
= dash
+ 1;
318 dash
= strchr(familyFont
, '-');
319 *dash
= '\0'; // !NULL because Matches() above succeeded
321 if ( !family
.empty() && (family
!= familyFont
) )
323 // family doesn't match
327 // now extract the registry/encoding
328 char *p
= dash
+ 1; // just after the dash after family
329 dash
= strrchr(p
, '-');
331 wxString
registry(dash
+ 1);
334 dash
= strrchr(p
, '-');
335 wxString
encoding(dash
+ 1);
337 encoding
<< wxT('-') << registry
;
338 if ( encodings
.Index(encoding
) == wxNOT_FOUND
)
340 if ( !OnFontEncoding(familyFont
, encoding
) )
345 encodings
.Add(encoding
);
347 //else: already had this one
350 XFreeFontNames(fonts
);
357 #endif // !wxUSE_PANGO