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"
29 #include "wx/fontenum.h"
33 // ----------------------------------------------------------------------------
35 // ----------------------------------------------------------------------------
37 // create the list of all fonts with the given spacing and encoding
38 static char **CreateFontList(wxChar spacing
, wxFontEncoding encoding
,
41 // extract all font families from the given font list and call our
42 // OnFacename() for each of them
43 static bool ProcessFamiliesFromFontList(wxFontEnumerator
*This
,
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 // ============================================================================
54 // ============================================================================
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 static char **CreateFontList(wxChar spacing
,
61 wxFontEncoding encoding
,
64 wxString xencoding
, xregistry
;
65 wxGetXFontEncoding(encoding
, &xregistry
, &xencoding
);
68 pattern
.Printf(wxT("-*-*-*-*-*-*-*-*-*-*-%c-*-%s-%s"),
69 spacing
, xregistry
.c_str(), xencoding
.c_str());
71 // get the list of all fonts
72 return XListFonts((Display
*)wxGetDisplay(), pattern
.mb_str(), 32767, nFonts
);
75 static bool ProcessFamiliesFromFontList(wxFontEnumerator
*This
,
79 // extract the list of (unique) font families
80 wxSortedArrayString families
;
81 for ( int n
= 0; n
< nFonts
; n
++ )
83 char *font
= fonts
[n
];
84 if ( !wxString(font
).Matches(wxT("-*-*-*-*-*-*-*-*-*-*-*-*-*-*")) )
86 // it's not a full font name (probably an alias)
90 char *dash
= strchr(font
+ 1, '-');
91 char *family
= dash
+ 1;
92 dash
= strchr(family
, '-');
93 *dash
= '\0'; // !NULL because Matches() above succeeded
96 if ( families
.Index(fam
) == wxNOT_FOUND
)
98 if ( !This
->OnFacename(fam
) )
112 // ----------------------------------------------------------------------------
114 // ----------------------------------------------------------------------------
116 bool wxFontEnumerator::EnumerateFacenames(wxFontEncoding encoding
,
122 if ( fixedWidthOnly
)
125 fonts
= CreateFontList(wxT('m'), encoding
, &nFonts
);
128 cont
= ProcessFamiliesFromFontList(this, fonts
, nFonts
);
130 XFreeFontNames(fonts
);
138 fonts
= CreateFontList(wxT('c'), encoding
, &nFonts
);
146 fonts
= CreateFontList(wxT('*'), encoding
, &nFonts
);
150 // it's ok if there are no fonts in given encoding - but it's not
151 // ok if there are no fonts at all
152 wxASSERT_MSG(encoding
!= wxFONTENCODING_SYSTEM
,
153 wxT("No fonts at all on this system?"));
159 (void)ProcessFamiliesFromFontList(this, fonts
, nFonts
);
161 XFreeFontNames(fonts
);
166 bool wxFontEnumerator::EnumerateEncodings(const wxString
& family
)
169 pattern
.Printf(wxT("-*-%s-*-*-*-*-*-*-*-*-*-*-*-*"),
170 family
.IsEmpty() ? wxT("*") : family
.c_str());
172 // get the list of all fonts
174 char **fonts
= XListFonts((Display
*)wxGetDisplay(), pattern
.mb_str(),
183 // extract the list of (unique) encodings
184 wxSortedArrayString encodings
;
185 for ( int n
= 0; n
< nFonts
; n
++ )
187 char *font
= fonts
[n
];
188 if ( !wxString(font
).Matches(wxT("-*-*-*-*-*-*-*-*-*-*-*-*-*-*")) )
190 // it's not a full font name (probably an alias)
194 // extract the family
195 char *dash
= strchr(font
+ 1, '-');
196 char *familyFont
= dash
+ 1;
197 dash
= strchr(familyFont
, '-');
198 *dash
= '\0'; // !NULL because Matches() above succeeded
200 if ( !family
.IsEmpty() && (family
!= familyFont
) )
202 // family doesn't match
206 // now extract the registry/encoding
207 char *p
= dash
+ 1; // just after the dash after family
208 dash
= strrchr(p
, '-');
210 wxString
registry(dash
+ 1);
213 dash
= strrchr(p
, '-');
214 wxString
encoding(dash
+ 1);
216 encoding
<< wxT('-') << registry
;
217 if ( encodings
.Index(encoding
) == wxNOT_FOUND
)
219 if ( !OnFontEncoding(familyFont
, encoding
) )
224 encodings
.Add(encoding
);
226 //else: already had this one
229 XFreeFontNames(fonts
);