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 bool wxFontEnumerator::EnumerateFacenames(wxFontEncoding encoding
,
62 if ( encoding
!= wxFONTENCODING_SYSTEM
&& encoding
!= wxFONTENCODING_UTF8
)
64 // Pango supports only UTF-8 encoding (and system means any, so we
69 #if defined(__WXGTK20__) || !defined(HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE)
71 #if defined(__WXGTK24__)
72 && (gtk_check_version(2,4,0) != NULL
)
76 OnFacename( wxT("monospace") );
78 else // !fixedWidthOnly
79 #endif // __WXGTK20__ || !HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE
81 PangoFontFamily
**families
= NULL
;
83 pango_context_list_families (
85 gtk_widget_get_pango_context( wxGetRootWindow() ),
87 wxTheApp
->GetPangoContext(),
89 &families
, &n_families
);
90 qsort (families
, n_families
, sizeof (PangoFontFamily
*), wxCompareFamilies
);
92 for (int i
=0; i
<n_families
; i
++)
94 #if defined(__WXGTK24__) || defined(HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE)
95 if (!fixedWidthOnly
|| (
97 !gtk_check_version(2,4,0) &&
99 pango_font_family_is_monospace(families
[i
])
103 const gchar
*name
= pango_font_family_get_name(families
[i
]);
104 OnFacename(wxString(name
, wxConvUTF8
));
113 bool wxFontEnumerator::EnumerateEncodings(const wxString
& facename
)
115 return EnumerateEncodingsUTF8(facename
);
119 #else // !wxUSE_PANGO
121 #ifdef __VMS__ // Xlib.h for VMS is not (yet) compatible with C++
122 // The resulting warnings are switched off here
123 #pragma message disable nosimpint
125 #include <X11/Xlib.h>
127 #pragma message enable nosimpint
130 // ----------------------------------------------------------------------------
132 // ----------------------------------------------------------------------------
134 // create the list of all fonts with the given spacing and encoding
135 static char **CreateFontList(wxChar spacing
, wxFontEncoding encoding
,
138 // extract all font families from the given font list and call our
139 // OnFacename() for each of them
140 static bool ProcessFamiliesFromFontList(wxFontEnumerator
*This
,
145 // ----------------------------------------------------------------------------
147 // ----------------------------------------------------------------------------
149 // ============================================================================
151 // ============================================================================
153 // ----------------------------------------------------------------------------
155 // ----------------------------------------------------------------------------
158 static char **CreateFontList(wxChar spacing
,
159 wxFontEncoding encoding
,
162 wxNativeEncodingInfo info
;
163 wxGetNativeFontEncoding(encoding
, &info
);
166 if ( !wxTestFontEncoding(info
) )
168 // ask font mapper for a replacement
169 (void)wxFontMapper::Get()->GetAltForEncoding(encoding
, &info
);
171 #endif // wxUSE_FONTMAP
174 pattern
.Printf(wxT("-*-*-*-*-*-*-*-*-*-*-%c-*-%s-%s"),
176 info
.xregistry
.c_str(),
177 info
.xencoding
.c_str());
179 // get the list of all fonts
180 return XListFonts((Display
*)wxGetDisplay(), pattern
.mb_str(), 32767, nFonts
);
183 static bool ProcessFamiliesFromFontList(wxFontEnumerator
*This
,
188 wxRegEx
re(wxT("^(-[^-]*){14}$"), wxRE_NOSUB
);
189 #endif // wxUSE_REGEX
191 // extract the list of (unique) font families
192 wxSortedArrayString families
;
193 for ( int n
= 0; n
< nFonts
; n
++ )
195 char *font
= fonts
[n
];
197 if ( !re
.Matches(font
) )
198 #else // !wxUSE_REGEX
199 if ( !wxString(font
).Matches(wxT("-*-*-*-*-*-*-*-*-*-*-*-*-*-*")) )
200 #endif // wxUSE_REGEX/!wxUSE_REGEX
202 // it's not a full font name (probably an alias)
206 // coverity[returned_null]
207 char *dash
= strchr(font
+ 1, '-');
208 char *family
= dash
+ 1;
209 dash
= strchr(family
, '-');
210 *dash
= '\0'; // !NULL because Matches() above succeeded
211 wxString
fam(family
);
213 if ( families
.Index(fam
) == wxNOT_FOUND
)
215 if ( !This
->OnFacename(fam
) )
231 // ----------------------------------------------------------------------------
233 // ----------------------------------------------------------------------------
235 bool wxFontEnumerator::EnumerateFacenames(wxFontEncoding encoding
,
244 if ( fixedWidthOnly
)
247 fonts
= CreateFontList(wxT('m'), encoding
, &nFonts
);
250 cont
= ProcessFamiliesFromFontList(this, fonts
, nFonts
);
252 XFreeFontNames(fonts
);
260 fonts
= CreateFontList(wxT('c'), encoding
, &nFonts
);
268 fonts
= CreateFontList(wxT('*'), encoding
, &nFonts
);
272 // it's ok if there are no fonts in given encoding - but it's not
273 // ok if there are no fonts at all
274 wxASSERT_MSG(encoding
!= wxFONTENCODING_SYSTEM
,
275 wxT("No fonts at all on this system?"));
281 (void)ProcessFamiliesFromFontList(this, fonts
, nFonts
);
283 XFreeFontNames(fonts
);
289 bool wxFontEnumerator::EnumerateEncodings(const wxString
& family
)
295 pattern
.Printf(wxT("-*-%s-*-*-*-*-*-*-*-*-*-*-*-*"),
296 family
.empty() ? wxT("*") : family
.c_str());
298 // get the list of all fonts
300 char **fonts
= XListFonts((Display
*)wxGetDisplay(), pattern
.mb_str(),
309 // extract the list of (unique) encodings
310 wxSortedArrayString encodings
;
311 for ( int n
= 0; n
< nFonts
; n
++ )
313 char *font
= fonts
[n
];
314 if ( !wxString(font
).Matches(wxT("-*-*-*-*-*-*-*-*-*-*-*-*-*-*")) )
316 // it's not a full font name (probably an alias)
320 // extract the family
321 char *dash
= strchr(font
+ 1, '-');
322 char *familyFont
= dash
+ 1;
323 dash
= strchr(familyFont
, '-');
324 *dash
= '\0'; // !NULL because Matches() above succeeded
326 if ( !family
.empty() && (family
!= familyFont
) )
328 // family doesn't match
332 // now extract the registry/encoding
333 char *p
= dash
+ 1; // just after the dash after family
334 dash
= strrchr(p
, '-');
336 wxString
registry(dash
+ 1);
339 dash
= strrchr(p
, '-');
340 wxString
encoding(dash
+ 1);
342 encoding
<< wxT('-') << registry
;
343 if ( encodings
.Index(encoding
) == wxNOT_FOUND
)
345 if ( !OnFontEncoding(familyFont
, encoding
) )
350 encodings
.Add(encoding
);
352 //else: already had this one
355 XFreeFontNames(fonts
);
362 #endif // !wxUSE_PANGO