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/dynarray.h"
24 #include "wx/string.h"
28 #include "wx/fontmap.h"
29 #include "wx/fontenum.h"
30 #include "wx/fontutil.h"
31 #include "wx/encinfo.h"
33 // ----------------------------------------------------------------------------
35 // ----------------------------------------------------------------------------
39 #include "pango/pango.h"
43 extern GtkWidget
*wxGetRootWindow();
46 extern "C" int wxCMPFUNC_CONV
47 wxCompareFamilies (const void *a
, const void *b
)
49 const char *a_name
= pango_font_family_get_name (*(PangoFontFamily
**)a
);
50 const char *b_name
= pango_font_family_get_name (*(PangoFontFamily
**)b
);
52 return g_utf8_collate (a_name
, b_name
);
55 // I admit I don't yet understand encodings with Pango
56 bool wxFontEnumerator::EnumerateFacenames(wxFontEncoding encoding
,
59 #if defined(__WXGTK20__) || !defined(HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE)
61 #if defined(__WXGTK24__)
62 && (gtk_check_version(2,4,0) != NULL
)
66 OnFacename( wxT("monospace") );
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(__WXGTK24__) || defined(HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE)
85 if (!fixedWidthOnly
|| (
87 !gtk_check_version(2,4,0) &&
89 pango_font_family_is_monospace(families
[i
])
93 const gchar
*name
= pango_font_family_get_name(families
[i
]);
94 OnFacename(wxString(name
, wxConvUTF8
));
103 bool wxFontEnumerator::EnumerateEncodings(const wxString
& family
)
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
116 #include <X11/Xlib.h>
118 #pragma message enable nosimpint
121 // ----------------------------------------------------------------------------
123 // ----------------------------------------------------------------------------
125 // create the list of all fonts with the given spacing and encoding
126 static char **CreateFontList(wxChar spacing
, wxFontEncoding encoding
,
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
,
136 // ----------------------------------------------------------------------------
138 // ----------------------------------------------------------------------------
140 // ============================================================================
142 // ============================================================================
144 // ----------------------------------------------------------------------------
146 // ----------------------------------------------------------------------------
149 static char **CreateFontList(wxChar spacing
,
150 wxFontEncoding encoding
,
153 wxNativeEncodingInfo info
;
154 wxGetNativeFontEncoding(encoding
, &info
);
157 if ( !wxTestFontEncoding(info
) )
159 // ask font mapper for a replacement
160 (void)wxFontMapper::Get()->GetAltForEncoding(encoding
, &info
);
162 #endif // wxUSE_FONTMAP
165 pattern
.Printf(wxT("-*-*-*-*-*-*-*-*-*-*-%c-*-%s-%s"),
167 info
.xregistry
.c_str(),
168 info
.xencoding
.c_str());
170 // get the list of all fonts
171 return XListFonts((Display
*)wxGetDisplay(), pattern
.mb_str(), 32767, nFonts
);
174 static bool ProcessFamiliesFromFontList(wxFontEnumerator
*This
,
179 wxRegEx
re(wxT("^(-[^-]*){14}$"), wxRE_NOSUB
);
180 #endif // wxUSE_REGEX
182 // extract the list of (unique) font families
183 wxSortedArrayString families
;
184 for ( int n
= 0; n
< nFonts
; n
++ )
186 char *font
= fonts
[n
];
188 if ( !re
.Matches(font
) )
189 #else // !wxUSE_REGEX
190 if ( !wxString(font
).Matches(wxT("-*-*-*-*-*-*-*-*-*-*-*-*-*-*")) )
191 #endif // wxUSE_REGEX/!wxUSE_REGEX
193 // it's not a full font name (probably an alias)
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
);
203 if ( families
.Index(fam
) == wxNOT_FOUND
)
205 if ( !This
->OnFacename(fam
) )
221 // ----------------------------------------------------------------------------
223 // ----------------------------------------------------------------------------
225 bool wxFontEnumerator::EnumerateFacenames(wxFontEncoding encoding
,
234 if ( fixedWidthOnly
)
237 fonts
= CreateFontList(wxT('m'), encoding
, &nFonts
);
240 cont
= ProcessFamiliesFromFontList(this, fonts
, nFonts
);
242 XFreeFontNames(fonts
);
250 fonts
= CreateFontList(wxT('c'), encoding
, &nFonts
);
258 fonts
= CreateFontList(wxT('*'), encoding
, &nFonts
);
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?"));
271 (void)ProcessFamiliesFromFontList(this, fonts
, nFonts
);
273 XFreeFontNames(fonts
);
279 bool wxFontEnumerator::EnumerateEncodings(const wxString
& family
)
285 pattern
.Printf(wxT("-*-%s-*-*-*-*-*-*-*-*-*-*-*-*"),
286 family
.IsEmpty() ? wxT("*") : family
.c_str());
288 // get the list of all fonts
290 char **fonts
= XListFonts((Display
*)wxGetDisplay(), pattern
.mb_str(),
299 // extract the list of (unique) encodings
300 wxSortedArrayString encodings
;
301 for ( int n
= 0; n
< nFonts
; n
++ )
303 char *font
= fonts
[n
];
304 if ( !wxString(font
).Matches(wxT("-*-*-*-*-*-*-*-*-*-*-*-*-*-*")) )
306 // it's not a full font name (probably an alias)
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
316 if ( !family
.IsEmpty() && (family
!= familyFont
) )
318 // family doesn't match
322 // now extract the registry/encoding
323 char *p
= dash
+ 1; // just after the dash after family
324 dash
= strrchr(p
, '-');
326 wxString
registry(dash
+ 1);
329 dash
= strrchr(p
, '-');
330 wxString
encoding(dash
+ 1);
332 encoding
<< wxT('-') << registry
;
333 if ( encodings
.Index(encoding
) == wxNOT_FOUND
)
335 if ( !OnFontEncoding(familyFont
, encoding
) )
340 encodings
.Add(encoding
);
342 //else: already had this one
345 XFreeFontNames(fonts
);