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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "fontenum.h"
24 // for compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
27 #include "wx/dynarray.h"
28 #include "wx/string.h"
32 #include "wx/fontmap.h"
33 #include "wx/fontenum.h"
34 #include "wx/fontutil.h"
35 #include "wx/encinfo.h"
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
43 #include "pango/pango.h"
47 extern GtkWidget
*wxGetRootWindow();
51 cmp_families (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 // I admit I don't yet understand encodings with Pango
60 bool wxFontEnumerator::EnumerateFacenames(wxFontEncoding encoding
,
63 #ifndef HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE
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
*), cmp_families
);
82 for (int i
=0; i
<n_families
; i
++)
84 #ifdef 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
& family
)
107 #ifdef __VMS__ // Xlib.h for VMS is not (yet) compatible with C++
108 // The resulting warnings are switched off here
109 #pragma message disable nosimpint
111 #include <X11/Xlib.h>
113 #pragma message enable nosimpint
116 // ----------------------------------------------------------------------------
118 // ----------------------------------------------------------------------------
120 // create the list of all fonts with the given spacing and encoding
121 static char **CreateFontList(wxChar spacing
, wxFontEncoding encoding
,
124 // extract all font families from the given font list and call our
125 // OnFacename() for each of them
126 static bool ProcessFamiliesFromFontList(wxFontEnumerator
*This
,
131 // ----------------------------------------------------------------------------
133 // ----------------------------------------------------------------------------
135 // ============================================================================
137 // ============================================================================
139 // ----------------------------------------------------------------------------
141 // ----------------------------------------------------------------------------
144 static char **CreateFontList(wxChar spacing
,
145 wxFontEncoding encoding
,
148 wxNativeEncodingInfo info
;
149 wxGetNativeFontEncoding(encoding
, &info
);
152 if ( !wxTestFontEncoding(info
) )
154 // ask font mapper for a replacement
155 (void)wxFontMapper::Get()->GetAltForEncoding(encoding
, &info
);
157 #endif // wxUSE_FONTMAP
160 pattern
.Printf(wxT("-*-*-*-*-*-*-*-*-*-*-%c-*-%s-%s"),
162 info
.xregistry
.c_str(),
163 info
.xencoding
.c_str());
165 // get the list of all fonts
166 return XListFonts((Display
*)wxGetDisplay(), pattern
.mb_str(), 32767, nFonts
);
169 static bool ProcessFamiliesFromFontList(wxFontEnumerator
*This
,
174 wxRegEx
re(wxT("^(-[^-]*){14}$"), wxRE_NOSUB
);
175 #endif // wxUSE_REGEX
177 // extract the list of (unique) font families
178 wxSortedArrayString families
;
179 for ( int n
= 0; n
< nFonts
; n
++ )
181 char *font
= fonts
[n
];
183 if ( !re
.Matches(font
) )
184 #else // !wxUSE_REGEX
185 if ( !wxString(font
).Matches(wxT("-*-*-*-*-*-*-*-*-*-*-*-*-*-*")) )
186 #endif // wxUSE_REGEX/!wxUSE_REGEX
188 // it's not a full font name (probably an alias)
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
.IsEmpty() ? 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
.IsEmpty() && (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
);