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"
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
42 #include "pango/pango.h"
46 extern GtkWidget
*wxGetRootWindow();
50 cmp_families (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
,
64 OnFacename( wxT("monospace") );
68 PangoFontFamily
**families
= NULL
;
70 pango_context_list_families (
72 gtk_widget_get_pango_context( wxGetRootWindow() ),
74 wxTheApp
->GetPangoContext(),
76 &families
, &n_families
);
77 qsort (families
, n_families
, sizeof (PangoFontFamily
*), cmp_families
);
79 for (int i
=0; i
<n_families
; i
++)
81 const gchar
*name
= pango_font_family_get_name( families
[i
] );
83 wxString
tmp( name
, wxConvUTF8
);
91 bool wxFontEnumerator::EnumerateEncodings(const wxString
& family
)
100 #ifdef __VMS__ // Xlib.h for VMS is not (yet) compatible with C++
101 // The resulting warnings are switched off here
102 #pragma message disable nosimpint
104 #include <X11/Xlib.h>
106 #pragma message enable nosimpint
109 // ----------------------------------------------------------------------------
111 // ----------------------------------------------------------------------------
113 // create the list of all fonts with the given spacing and encoding
114 static char **CreateFontList(wxChar spacing
, wxFontEncoding encoding
,
117 // extract all font families from the given font list and call our
118 // OnFacename() for each of them
119 static bool ProcessFamiliesFromFontList(wxFontEnumerator
*This
,
124 // ----------------------------------------------------------------------------
126 // ----------------------------------------------------------------------------
128 // ============================================================================
130 // ============================================================================
132 // ----------------------------------------------------------------------------
134 // ----------------------------------------------------------------------------
137 static char **CreateFontList(wxChar spacing
,
138 wxFontEncoding encoding
,
141 wxNativeEncodingInfo info
;
142 wxGetNativeFontEncoding(encoding
, &info
);
145 if ( !wxTestFontEncoding(info
) )
147 // ask font mapper for a replacement
148 (void)wxFontMapper::Get()->GetAltForEncoding(encoding
, &info
);
150 #endif // wxUSE_FONTMAP
153 pattern
.Printf(wxT("-*-*-*-*-*-*-*-*-*-*-%c-*-%s-%s"),
155 info
.xregistry
.c_str(),
156 info
.xencoding
.c_str());
158 // get the list of all fonts
159 return XListFonts((Display
*)wxGetDisplay(), pattern
.mb_str(), 32767, nFonts
);
162 static bool ProcessFamiliesFromFontList(wxFontEnumerator
*This
,
167 wxRegEx
re(wxT("^(-[^-]*){14}$"), wxRE_NOSUB
);
168 #endif // wxUSE_REGEX
170 // extract the list of (unique) font families
171 wxSortedArrayString families
;
172 for ( int n
= 0; n
< nFonts
; n
++ )
174 char *font
= fonts
[n
];
176 if ( !re
.Matches(font
) )
177 #else // !wxUSE_REGEX
178 if ( !wxString(font
).Matches(wxT("-*-*-*-*-*-*-*-*-*-*-*-*-*-*")) )
179 #endif // wxUSE_REGEX/!wxUSE_REGEX
181 // it's not a full font name (probably an alias)
185 char *dash
= strchr(font
+ 1, '-');
186 char *family
= dash
+ 1;
187 dash
= strchr(family
, '-');
188 *dash
= '\0'; // !NULL because Matches() above succeeded
189 wxString
fam(family
);
191 if ( families
.Index(fam
) == wxNOT_FOUND
)
193 if ( !This
->OnFacename(fam
) )
209 // ----------------------------------------------------------------------------
211 // ----------------------------------------------------------------------------
213 bool wxFontEnumerator::EnumerateFacenames(wxFontEncoding encoding
,
222 if ( fixedWidthOnly
)
225 fonts
= CreateFontList(wxT('m'), encoding
, &nFonts
);
228 cont
= ProcessFamiliesFromFontList(this, fonts
, nFonts
);
230 XFreeFontNames(fonts
);
238 fonts
= CreateFontList(wxT('c'), encoding
, &nFonts
);
246 fonts
= CreateFontList(wxT('*'), encoding
, &nFonts
);
250 // it's ok if there are no fonts in given encoding - but it's not
251 // ok if there are no fonts at all
252 wxASSERT_MSG(encoding
!= wxFONTENCODING_SYSTEM
,
253 wxT("No fonts at all on this system?"));
259 (void)ProcessFamiliesFromFontList(this, fonts
, nFonts
);
261 XFreeFontNames(fonts
);
267 bool wxFontEnumerator::EnumerateEncodings(const wxString
& family
)
273 pattern
.Printf(wxT("-*-%s-*-*-*-*-*-*-*-*-*-*-*-*"),
274 family
.IsEmpty() ? wxT("*") : family
.c_str());
276 // get the list of all fonts
278 char **fonts
= XListFonts((Display
*)wxGetDisplay(), pattern
.mb_str(),
287 // extract the list of (unique) encodings
288 wxSortedArrayString encodings
;
289 for ( int n
= 0; n
< nFonts
; n
++ )
291 char *font
= fonts
[n
];
292 if ( !wxString(font
).Matches(wxT("-*-*-*-*-*-*-*-*-*-*-*-*-*-*")) )
294 // it's not a full font name (probably an alias)
298 // extract the family
299 char *dash
= strchr(font
+ 1, '-');
300 char *familyFont
= dash
+ 1;
301 dash
= strchr(familyFont
, '-');
302 *dash
= '\0'; // !NULL because Matches() above succeeded
304 if ( !family
.IsEmpty() && (family
!= familyFont
) )
306 // family doesn't match
310 // now extract the registry/encoding
311 char *p
= dash
+ 1; // just after the dash after family
312 dash
= strrchr(p
, '-');
314 wxString
registry(dash
+ 1);
317 dash
= strrchr(p
, '-');
318 wxString
encoding(dash
+ 1);
320 encoding
<< wxT('-') << registry
;
321 if ( encodings
.Index(encoding
) == wxNOT_FOUND
)
323 if ( !OnFontEncoding(familyFont
, encoding
) )
328 encodings
.Add(encoding
);
330 //else: already had this one
333 XFreeFontNames(fonts
);