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 // ----------------------------------------------------------------------------
21 #pragma implementation "fontenum.h"
25 #include "wx/dynarray.h"
26 #include "wx/string.h"
30 #include "wx/fontmap.h"
31 #include "wx/fontenum.h"
32 #include "wx/fontutil.h"
34 // ----------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
40 #include "pango/pango.h"
44 extern GtkWidget
*wxGetRootWindow();
48 cmp_families (const void *a
, const void *b
)
50 const char *a_name
= pango_font_family_get_name (*(PangoFontFamily
**)a
);
51 const char *b_name
= pango_font_family_get_name (*(PangoFontFamily
**)b
);
53 return g_utf8_collate (a_name
, b_name
);
56 // I admit I don't yet understand encodings with Pango
57 bool wxFontEnumerator::EnumerateFacenames(wxFontEncoding encoding
,
62 OnFacename( wxT("monospace") );
66 PangoFontFamily
**families
= NULL
;
68 pango_context_list_families (
70 gtk_widget_get_pango_context( wxGetRootWindow() ),
72 wxTheApp
->GetPangoContext(),
74 &families
, &n_families
);
75 qsort (families
, n_families
, sizeof (PangoFontFamily
*), cmp_families
);
77 for (int i
=0; i
<n_families
; i
++)
79 const gchar
*name
= pango_font_family_get_name( families
[i
] );
81 wxString
tmp( name
, wxConvUTF8
);
89 bool wxFontEnumerator::EnumerateEncodings(const wxString
& family
)
98 #ifdef __VMS__ // Xlib.h for VMS is not (yet) compatible with C++
99 // The resulting warnings are switched off here
100 #pragma message disable nosimpint
102 #include <X11/Xlib.h>
104 #pragma message enable nosimpint
107 // ----------------------------------------------------------------------------
109 // ----------------------------------------------------------------------------
111 // create the list of all fonts with the given spacing and encoding
112 static char **CreateFontList(wxChar spacing
, wxFontEncoding encoding
,
115 // extract all font families from the given font list and call our
116 // OnFacename() for each of them
117 static bool ProcessFamiliesFromFontList(wxFontEnumerator
*This
,
122 // ----------------------------------------------------------------------------
124 // ----------------------------------------------------------------------------
126 // ============================================================================
128 // ============================================================================
130 // ----------------------------------------------------------------------------
132 // ----------------------------------------------------------------------------
135 static char **CreateFontList(wxChar spacing
,
136 wxFontEncoding encoding
,
139 wxNativeEncodingInfo info
;
140 wxGetNativeFontEncoding(encoding
, &info
);
143 if ( !wxTestFontEncoding(info
) )
145 // ask font mapper for a replacement
146 (void)wxFontMapper::Get()->GetAltForEncoding(encoding
, &info
);
148 #endif // wxUSE_FONTMAP
151 pattern
.Printf(wxT("-*-*-*-*-*-*-*-*-*-*-%c-*-%s-%s"),
153 info
.xregistry
.c_str(),
154 info
.xencoding
.c_str());
156 // get the list of all fonts
157 return XListFonts((Display
*)wxGetDisplay(), pattern
.mb_str(), 32767, nFonts
);
160 static bool ProcessFamiliesFromFontList(wxFontEnumerator
*This
,
165 wxRegEx
re(wxT("^(-[^-]*){14}$"), wxRE_NOSUB
);
166 #endif // wxUSE_REGEX
168 // extract the list of (unique) font families
169 wxSortedArrayString families
;
170 for ( int n
= 0; n
< nFonts
; n
++ )
172 char *font
= fonts
[n
];
174 if ( !re
.Matches(font
) )
175 #else // !wxUSE_REGEX
176 if ( !wxString(font
).Matches(wxT("-*-*-*-*-*-*-*-*-*-*-*-*-*-*")) )
177 #endif // wxUSE_REGEX/!wxUSE_REGEX
179 // it's not a full font name (probably an alias)
183 char *dash
= strchr(font
+ 1, '-');
184 char *family
= dash
+ 1;
185 dash
= strchr(family
, '-');
186 *dash
= '\0'; // !NULL because Matches() above succeeded
187 wxString
fam(family
);
189 if ( families
.Index(fam
) == wxNOT_FOUND
)
191 if ( !This
->OnFacename(fam
) )
207 // ----------------------------------------------------------------------------
209 // ----------------------------------------------------------------------------
211 bool wxFontEnumerator::EnumerateFacenames(wxFontEncoding encoding
,
220 if ( fixedWidthOnly
)
223 fonts
= CreateFontList(wxT('m'), encoding
, &nFonts
);
226 cont
= ProcessFamiliesFromFontList(this, fonts
, nFonts
);
228 XFreeFontNames(fonts
);
236 fonts
= CreateFontList(wxT('c'), encoding
, &nFonts
);
244 fonts
= CreateFontList(wxT('*'), encoding
, &nFonts
);
248 // it's ok if there are no fonts in given encoding - but it's not
249 // ok if there are no fonts at all
250 wxASSERT_MSG(encoding
!= wxFONTENCODING_SYSTEM
,
251 wxT("No fonts at all on this system?"));
257 (void)ProcessFamiliesFromFontList(this, fonts
, nFonts
);
259 XFreeFontNames(fonts
);
265 bool wxFontEnumerator::EnumerateEncodings(const wxString
& family
)
271 pattern
.Printf(wxT("-*-%s-*-*-*-*-*-*-*-*-*-*-*-*"),
272 family
.IsEmpty() ? wxT("*") : family
.c_str());
274 // get the list of all fonts
276 char **fonts
= XListFonts((Display
*)wxGetDisplay(), pattern
.mb_str(),
285 // extract the list of (unique) encodings
286 wxSortedArrayString encodings
;
287 for ( int n
= 0; n
< nFonts
; n
++ )
289 char *font
= fonts
[n
];
290 if ( !wxString(font
).Matches(wxT("-*-*-*-*-*-*-*-*-*-*-*-*-*-*")) )
292 // it's not a full font name (probably an alias)
296 // extract the family
297 char *dash
= strchr(font
+ 1, '-');
298 char *familyFont
= dash
+ 1;
299 dash
= strchr(familyFont
, '-');
300 *dash
= '\0'; // !NULL because Matches() above succeeded
302 if ( !family
.IsEmpty() && (family
!= familyFont
) )
304 // family doesn't match
308 // now extract the registry/encoding
309 char *p
= dash
+ 1; // just after the dash after family
310 dash
= strrchr(p
, '-');
312 wxString
registry(dash
+ 1);
315 dash
= strrchr(p
, '-');
316 wxString
encoding(dash
+ 1);
318 encoding
<< wxT('-') << registry
;
319 if ( encodings
.Index(encoding
) == wxNOT_FOUND
)
321 if ( !OnFontEncoding(familyFont
, encoding
) )
326 encodings
.Add(encoding
);
328 //else: already had this one
331 XFreeFontNames(fonts
);