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"
30 #include "wx/fontutil.h"
34 // ----------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
38 // create the list of all fonts with the given spacing and encoding
39 static char **CreateFontList(wxChar spacing
, wxFontEncoding encoding
,
42 // extract all font families from the given font list and call our
43 // OnFacename() for each of them
44 static bool ProcessFamiliesFromFontList(wxFontEnumerator
*This
,
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 // ============================================================================
55 // ============================================================================
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 static char **CreateFontList(wxChar spacing
,
62 wxFontEncoding encoding
,
65 wxNativeEncodingInfo info
;
66 wxGetNativeFontEncoding(encoding
, &info
);
69 pattern
.Printf(wxT("-*-*-*-*-*-*-*-*-*-*-%c-*-%s-%s"),
71 info
.xregistry
.c_str(),
72 info
.xencoding
.c_str());
74 // get the list of all fonts
75 return XListFonts((Display
*)wxGetDisplay(), pattern
.mb_str(), 32767, nFonts
);
78 static bool ProcessFamiliesFromFontList(wxFontEnumerator
*This
,
82 // extract the list of (unique) font families
83 wxSortedArrayString families
;
84 for ( int n
= 0; n
< nFonts
; n
++ )
86 char *font
= fonts
[n
];
87 if ( !wxString(font
).Matches(wxT("-*-*-*-*-*-*-*-*-*-*-*-*-*-*")) )
89 // it's not a full font name (probably an alias)
93 char *dash
= strchr(font
+ 1, '-');
94 char *family
= dash
+ 1;
95 dash
= strchr(family
, '-');
96 *dash
= '\0'; // !NULL because Matches() above succeeded
99 if ( families
.Index(fam
) == wxNOT_FOUND
)
101 if ( !This
->OnFacename(fam
) )
115 // ----------------------------------------------------------------------------
117 // ----------------------------------------------------------------------------
119 bool wxFontEnumerator::EnumerateFacenames(wxFontEncoding encoding
,
125 if ( fixedWidthOnly
)
128 fonts
= CreateFontList(wxT('m'), encoding
, &nFonts
);
131 cont
= ProcessFamiliesFromFontList(this, fonts
, nFonts
);
133 XFreeFontNames(fonts
);
141 fonts
= CreateFontList(wxT('c'), encoding
, &nFonts
);
149 fonts
= CreateFontList(wxT('*'), encoding
, &nFonts
);
153 // it's ok if there are no fonts in given encoding - but it's not
154 // ok if there are no fonts at all
155 wxASSERT_MSG(encoding
!= wxFONTENCODING_SYSTEM
,
156 wxT("No fonts at all on this system?"));
162 (void)ProcessFamiliesFromFontList(this, fonts
, nFonts
);
164 XFreeFontNames(fonts
);
169 bool wxFontEnumerator::EnumerateEncodings(const wxString
& family
)
172 pattern
.Printf(wxT("-*-%s-*-*-*-*-*-*-*-*-*-*-*-*"),
173 family
.IsEmpty() ? wxT("*") : family
.c_str());
175 // get the list of all fonts
177 char **fonts
= XListFonts((Display
*)wxGetDisplay(), pattern
.mb_str(),
186 // extract the list of (unique) encodings
187 wxSortedArrayString encodings
;
188 for ( int n
= 0; n
< nFonts
; n
++ )
190 char *font
= fonts
[n
];
191 if ( !wxString(font
).Matches(wxT("-*-*-*-*-*-*-*-*-*-*-*-*-*-*")) )
193 // it's not a full font name (probably an alias)
197 // extract the family
198 char *dash
= strchr(font
+ 1, '-');
199 char *familyFont
= dash
+ 1;
200 dash
= strchr(familyFont
, '-');
201 *dash
= '\0'; // !NULL because Matches() above succeeded
203 if ( !family
.IsEmpty() && (family
!= familyFont
) )
205 // family doesn't match
209 // now extract the registry/encoding
210 char *p
= dash
+ 1; // just after the dash after family
211 dash
= strrchr(p
, '-');
213 wxString
registry(dash
+ 1);
216 dash
= strrchr(p
, '-');
217 wxString
encoding(dash
+ 1);
219 encoding
<< wxT('-') << registry
;
220 if ( encodings
.Index(encoding
) == wxNOT_FOUND
)
222 if ( !OnFontEncoding(familyFont
, encoding
) )
227 encodings
.Add(encoding
);
229 //else: already had this one
232 XFreeFontNames(fonts
);