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 char *dash
= strchr(font
+ 1, '-');
201 char *family
= dash
+ 1;
202 dash
= strchr(family
, '-');
203 *dash
= '\0'; // !NULL because Matches() above succeeded
204 wxString
fam(family
);
206 if ( families
.Index(fam
) == wxNOT_FOUND
)
208 if ( !This
->OnFacename(fam
) )
224 // ----------------------------------------------------------------------------
226 // ----------------------------------------------------------------------------
228 bool wxFontEnumerator::EnumerateFacenames(wxFontEncoding encoding
,
237 if ( fixedWidthOnly
)
240 fonts
= CreateFontList(wxT('m'), encoding
, &nFonts
);
243 cont
= ProcessFamiliesFromFontList(this, fonts
, nFonts
);
245 XFreeFontNames(fonts
);
253 fonts
= CreateFontList(wxT('c'), encoding
, &nFonts
);
261 fonts
= CreateFontList(wxT('*'), encoding
, &nFonts
);
265 // it's ok if there are no fonts in given encoding - but it's not
266 // ok if there are no fonts at all
267 wxASSERT_MSG(encoding
!= wxFONTENCODING_SYSTEM
,
268 wxT("No fonts at all on this system?"));
274 (void)ProcessFamiliesFromFontList(this, fonts
, nFonts
);
276 XFreeFontNames(fonts
);
282 bool wxFontEnumerator::EnumerateEncodings(const wxString
& family
)
288 pattern
.Printf(wxT("-*-%s-*-*-*-*-*-*-*-*-*-*-*-*"),
289 family
.empty() ? wxT("*") : family
.c_str());
291 // get the list of all fonts
293 char **fonts
= XListFonts((Display
*)wxGetDisplay(), pattern
.mb_str(),
302 // extract the list of (unique) encodings
303 wxSortedArrayString encodings
;
304 for ( int n
= 0; n
< nFonts
; n
++ )
306 char *font
= fonts
[n
];
307 if ( !wxString(font
).Matches(wxT("-*-*-*-*-*-*-*-*-*-*-*-*-*-*")) )
309 // it's not a full font name (probably an alias)
313 // extract the family
314 char *dash
= strchr(font
+ 1, '-');
315 char *familyFont
= dash
+ 1;
316 dash
= strchr(familyFont
, '-');
317 *dash
= '\0'; // !NULL because Matches() above succeeded
319 if ( !family
.empty() && (family
!= familyFont
) )
321 // family doesn't match
325 // now extract the registry/encoding
326 char *p
= dash
+ 1; // just after the dash after family
327 dash
= strrchr(p
, '-');
329 wxString
registry(dash
+ 1);
332 dash
= strrchr(p
, '-');
333 wxString
encoding(dash
+ 1);
335 encoding
<< wxT('-') << registry
;
336 if ( encodings
.Index(encoding
) == wxNOT_FOUND
)
338 if ( !OnFontEncoding(familyFont
, encoding
) )
343 encodings
.Add(encoding
);
345 //else: already had this one
348 XFreeFontNames(fonts
);
355 #endif // !wxUSE_PANGO