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"
25 #include "wx/fontenum.h"
28 #include "wx/dynarray.h"
29 #include "wx/string.h"
35 #include "wx/fontmap.h"
36 #include "wx/fontutil.h"
37 #include "wx/encinfo.h"
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
45 #include "pango/pango.h"
49 extern GtkWidget
*wxGetRootWindow();
52 extern "C" int wxCMPFUNC_CONV
53 wxCompareFamilies (const void *a
, const void *b
)
55 const char *a_name
= pango_font_family_get_name (*(PangoFontFamily
**)a
);
56 const char *b_name
= pango_font_family_get_name (*(PangoFontFamily
**)b
);
58 return g_utf8_collate (a_name
, b_name
);
61 bool wxFontEnumerator::EnumerateFacenames(wxFontEncoding encoding
,
64 if ( encoding
!= wxFONTENCODING_SYSTEM
&& encoding
!= wxFONTENCODING_UTF8
)
66 // Pango supports only UTF-8 encoding (and system means any, so we
71 PangoFontFamily
**families
= NULL
;
73 pango_context_list_families (
75 gtk_widget_get_pango_context( wxGetRootWindow() ),
77 wxTheApp
->GetPangoContext(),
79 &families
, &n_families
);
80 qsort (families
, n_families
, sizeof (PangoFontFamily
*), wxCompareFamilies
);
82 for ( int i
= 0; i
< n_families
; i
++ )
84 #if defined(__WXGTK20__) || defined(HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE)
85 if ( !fixedWidthOnly
||
86 pango_font_family_is_monospace(families
[i
]) )
89 const gchar
*name
= pango_font_family_get_name(families
[i
]);
90 OnFacename(wxString(name
, wxConvUTF8
));
98 bool wxFontEnumerator::EnumerateEncodings(const wxString
& facename
)
100 return EnumerateEncodingsUTF8(facename
);
104 #else // !wxUSE_PANGO
106 #ifdef __VMS__ // Xlib.h for VMS is not (yet) compatible with C++
107 // The resulting warnings are switched off here
108 #pragma message disable nosimpint
110 #include <X11/Xlib.h>
112 #pragma message enable nosimpint
115 // ----------------------------------------------------------------------------
117 // ----------------------------------------------------------------------------
119 // create the list of all fonts with the given spacing and encoding
120 static char **CreateFontList(wxChar spacing
, wxFontEncoding encoding
,
123 // extract all font families from the given font list and call our
124 // OnFacename() for each of them
125 static bool ProcessFamiliesFromFontList(wxFontEnumerator
*This
,
130 // ----------------------------------------------------------------------------
132 // ----------------------------------------------------------------------------
134 // ============================================================================
136 // ============================================================================
138 // ----------------------------------------------------------------------------
140 // ----------------------------------------------------------------------------
143 static char **CreateFontList(wxChar spacing
,
144 wxFontEncoding encoding
,
147 wxNativeEncodingInfo info
;
148 wxGetNativeFontEncoding(encoding
, &info
);
151 if ( !wxTestFontEncoding(info
) )
153 // ask font mapper for a replacement
154 (void)wxFontMapper::Get()->GetAltForEncoding(encoding
, &info
);
156 #endif // wxUSE_FONTMAP
159 pattern
.Printf(wxT("-*-*-*-*-*-*-*-*-*-*-%c-*-%s-%s"),
161 info
.xregistry
.c_str(),
162 info
.xencoding
.c_str());
164 // get the list of all fonts
165 return XListFonts((Display
*)wxGetDisplay(), pattern
.mb_str(), 32767, nFonts
);
168 static bool ProcessFamiliesFromFontList(wxFontEnumerator
*This
,
173 wxRegEx
re(wxT("^(-[^-]*){14}$"), wxRE_NOSUB
);
174 #endif // wxUSE_REGEX
176 // extract the list of (unique) font families
177 wxSortedArrayString families
;
178 for ( int n
= 0; n
< nFonts
; n
++ )
180 char *font
= fonts
[n
];
182 if ( !re
.Matches(font
) )
183 #else // !wxUSE_REGEX
184 if ( !wxString(font
).Matches(wxT("-*-*-*-*-*-*-*-*-*-*-*-*-*-*")) )
185 #endif // wxUSE_REGEX/!wxUSE_REGEX
187 // it's not a full font name (probably an alias)
191 // coverity[returned_null]
192 char *dash
= strchr(font
+ 1, '-');
193 char *family
= dash
+ 1;
194 dash
= strchr(family
, '-');
195 *dash
= '\0'; // !NULL because Matches() above succeeded
196 wxString
fam(family
);
198 if ( families
.Index(fam
) == wxNOT_FOUND
)
200 if ( !This
->OnFacename(fam
) )
216 // ----------------------------------------------------------------------------
218 // ----------------------------------------------------------------------------
220 bool wxFontEnumerator::EnumerateFacenames(wxFontEncoding encoding
,
229 if ( fixedWidthOnly
)
232 fonts
= CreateFontList(wxT('m'), encoding
, &nFonts
);
235 cont
= ProcessFamiliesFromFontList(this, fonts
, nFonts
);
237 XFreeFontNames(fonts
);
245 fonts
= CreateFontList(wxT('c'), encoding
, &nFonts
);
253 fonts
= CreateFontList(wxT('*'), encoding
, &nFonts
);
257 // it's ok if there are no fonts in given encoding - but it's not
258 // ok if there are no fonts at all
259 wxASSERT_MSG(encoding
!= wxFONTENCODING_SYSTEM
,
260 wxT("No fonts at all on this system?"));
266 (void)ProcessFamiliesFromFontList(this, fonts
, nFonts
);
268 XFreeFontNames(fonts
);
274 bool wxFontEnumerator::EnumerateEncodings(const wxString
& family
)
280 pattern
.Printf(wxT("-*-%s-*-*-*-*-*-*-*-*-*-*-*-*"),
281 family
.empty() ? wxT("*") : family
.c_str());
283 // get the list of all fonts
285 char **fonts
= XListFonts((Display
*)wxGetDisplay(), pattern
.mb_str(),
294 // extract the list of (unique) encodings
295 wxSortedArrayString encodings
;
296 for ( int n
= 0; n
< nFonts
; n
++ )
298 char *font
= fonts
[n
];
299 if ( !wxString(font
).Matches(wxT("-*-*-*-*-*-*-*-*-*-*-*-*-*-*")) )
301 // it's not a full font name (probably an alias)
305 // extract the family
306 char *dash
= strchr(font
+ 1, '-');
307 char *familyFont
= dash
+ 1;
308 dash
= strchr(familyFont
, '-');
309 *dash
= '\0'; // !NULL because Matches() above succeeded
311 if ( !family
.empty() && (family
!= familyFont
) )
313 // family doesn't match
317 // now extract the registry/encoding
318 char *p
= dash
+ 1; // just after the dash after family
319 dash
= strrchr(p
, '-');
321 wxString
registry(dash
+ 1);
324 dash
= strrchr(p
, '-');
325 wxString
encoding(dash
+ 1);
327 encoding
<< wxT('-') << registry
;
328 if ( encodings
.Index(encoding
) == wxNOT_FOUND
)
330 if ( !OnFontEncoding(familyFont
, encoding
) )
335 encodings
.Add(encoding
);
337 //else: already had this one
340 XFreeFontNames(fonts
);
347 #endif // !wxUSE_PANGO
349 #endif // wxUSE_FONTENUM