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"
24 #include "wx/dynarray.h"
27 #include "wx/string.h"
31 #include "wx/fontmap.h"
32 #include "wx/fontenum.h"
33 #include "wx/fontutil.h"
34 #include "wx/encinfo.h"
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
42 #include "pango/pango.h"
46 extern GtkWidget
*wxGetRootWindow();
49 extern "C" int wxCMPFUNC_CONV
50 wxCompareFamilies (const void *a
, const void *b
)
52 const char *a_name
= pango_font_family_get_name (*(PangoFontFamily
**)a
);
53 const char *b_name
= pango_font_family_get_name (*(PangoFontFamily
**)b
);
55 return g_utf8_collate (a_name
, b_name
);
58 // I admit I don't yet understand encodings with Pango
59 bool wxFontEnumerator::EnumerateFacenames(wxFontEncoding encoding
,
62 #if defined(__WXGTK20__) || !defined(HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE)
64 #if defined(__WXGTK24__)
65 && (gtk_check_version(2,4,0) != NULL
)
69 OnFacename( wxT("monospace") );
72 #endif // __WXGTK20__ || !HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE
74 PangoFontFamily
**families
= NULL
;
76 pango_context_list_families (
78 gtk_widget_get_pango_context( wxGetRootWindow() ),
80 wxTheApp
->GetPangoContext(),
82 &families
, &n_families
);
83 qsort (families
, n_families
, sizeof (PangoFontFamily
*), wxCompareFamilies
);
85 for (int i
=0; i
<n_families
; i
++)
87 #if defined(__WXGTK24__) || defined(HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE)
88 if (!fixedWidthOnly
|| (
90 !gtk_check_version(2,4,0) &&
92 pango_font_family_is_monospace(families
[i
])
96 const gchar
*name
= pango_font_family_get_name(families
[i
]);
97 OnFacename(wxString(name
, wxConvUTF8
));
106 bool wxFontEnumerator::EnumerateEncodings(const wxString
& family
)
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
119 #include <X11/Xlib.h>
121 #pragma message enable nosimpint
124 // ----------------------------------------------------------------------------
126 // ----------------------------------------------------------------------------
128 // create the list of all fonts with the given spacing and encoding
129 static char **CreateFontList(wxChar spacing
, wxFontEncoding encoding
,
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
,
139 // ----------------------------------------------------------------------------
141 // ----------------------------------------------------------------------------
143 // ============================================================================
145 // ============================================================================
147 // ----------------------------------------------------------------------------
149 // ----------------------------------------------------------------------------
152 static char **CreateFontList(wxChar spacing
,
153 wxFontEncoding encoding
,
156 wxNativeEncodingInfo info
;
157 wxGetNativeFontEncoding(encoding
, &info
);
160 if ( !wxTestFontEncoding(info
) )
162 // ask font mapper for a replacement
163 (void)wxFontMapper::Get()->GetAltForEncoding(encoding
, &info
);
165 #endif // wxUSE_FONTMAP
168 pattern
.Printf(wxT("-*-*-*-*-*-*-*-*-*-*-%c-*-%s-%s"),
170 info
.xregistry
.c_str(),
171 info
.xencoding
.c_str());
173 // get the list of all fonts
174 return XListFonts((Display
*)wxGetDisplay(), pattern
.mb_str(), 32767, nFonts
);
177 static bool ProcessFamiliesFromFontList(wxFontEnumerator
*This
,
182 wxRegEx
re(wxT("^(-[^-]*){14}$"), wxRE_NOSUB
);
183 #endif // wxUSE_REGEX
185 // extract the list of (unique) font families
186 wxSortedArrayString families
;
187 for ( int n
= 0; n
< nFonts
; n
++ )
189 char *font
= fonts
[n
];
191 if ( !re
.Matches(font
) )
192 #else // !wxUSE_REGEX
193 if ( !wxString(font
).Matches(wxT("-*-*-*-*-*-*-*-*-*-*-*-*-*-*")) )
194 #endif // wxUSE_REGEX/!wxUSE_REGEX
196 // it's not a full font name (probably an alias)
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
);
207 if ( families
.Index(fam
) == wxNOT_FOUND
)
209 if ( !This
->OnFacename(fam
) )
225 // ----------------------------------------------------------------------------
227 // ----------------------------------------------------------------------------
229 bool wxFontEnumerator::EnumerateFacenames(wxFontEncoding encoding
,
238 if ( fixedWidthOnly
)
241 fonts
= CreateFontList(wxT('m'), encoding
, &nFonts
);
244 cont
= ProcessFamiliesFromFontList(this, fonts
, nFonts
);
246 XFreeFontNames(fonts
);
254 fonts
= CreateFontList(wxT('c'), encoding
, &nFonts
);
262 fonts
= CreateFontList(wxT('*'), encoding
, &nFonts
);
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?"));
275 (void)ProcessFamiliesFromFontList(this, fonts
, nFonts
);
277 XFreeFontNames(fonts
);
283 bool wxFontEnumerator::EnumerateEncodings(const wxString
& family
)
289 pattern
.Printf(wxT("-*-%s-*-*-*-*-*-*-*-*-*-*-*-*"),
290 family
.empty() ? wxT("*") : family
.c_str());
292 // get the list of all fonts
294 char **fonts
= XListFonts((Display
*)wxGetDisplay(), pattern
.mb_str(),
303 // extract the list of (unique) encodings
304 wxSortedArrayString encodings
;
305 for ( int n
= 0; n
< nFonts
; n
++ )
307 char *font
= fonts
[n
];
308 if ( !wxString(font
).Matches(wxT("-*-*-*-*-*-*-*-*-*-*-*-*-*-*")) )
310 // it's not a full font name (probably an alias)
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
320 if ( !family
.empty() && (family
!= familyFont
) )
322 // family doesn't match
326 // now extract the registry/encoding
327 char *p
= dash
+ 1; // just after the dash after family
328 dash
= strrchr(p
, '-');
330 wxString
registry(dash
+ 1);
333 dash
= strrchr(p
, '-');
334 wxString
encoding(dash
+ 1);
336 encoding
<< wxT('-') << registry
;
337 if ( encodings
.Index(encoding
) == wxNOT_FOUND
)
339 if ( !OnFontEncoding(familyFont
, encoding
) )
344 encodings
.Add(encoding
);
346 //else: already had this one
349 XFreeFontNames(fonts
);
356 #endif // !wxUSE_PANGO