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/fontmap.h"
30 #include "wx/fontenum.h"
31 #include "wx/fontutil.h"
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
39 // create the list of all fonts with the given spacing and encoding
40 static char **CreateFontList(wxChar spacing
, wxFontEncoding encoding
,
43 // extract all font families from the given font list and call our
44 // OnFacename() for each of them
45 static bool ProcessFamiliesFromFontList(wxFontEnumerator
*This
,
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 // ============================================================================
56 // ============================================================================
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 static char **CreateFontList(wxChar spacing
,
63 wxFontEncoding encoding
,
66 wxNativeEncodingInfo info
;
67 wxGetNativeFontEncoding(encoding
, &info
);
69 if ( !wxTestFontEncoding(info
) )
71 // ask font mapper for a replacement
72 (void)wxTheFontMapper
->GetAltForEncoding(encoding
, &info
);
76 pattern
.Printf(wxT("-*-*-*-*-*-*-*-*-*-*-%c-*-%s-%s"),
78 info
.xregistry
.c_str(),
79 info
.xencoding
.c_str());
81 // get the list of all fonts
82 return XListFonts((Display
*)wxGetDisplay(), pattern
.mb_str(), 32767, nFonts
);
85 static bool ProcessFamiliesFromFontList(wxFontEnumerator
*This
,
89 // extract the list of (unique) font families
90 wxSortedArrayString families
;
91 for ( int n
= 0; n
< nFonts
; n
++ )
93 char *font
= fonts
[n
];
94 if ( !wxString(font
).Matches(wxT("-*-*-*-*-*-*-*-*-*-*-*-*-*-*")) )
96 // it's not a full font name (probably an alias)
100 char *dash
= strchr(font
+ 1, '-');
101 char *family
= dash
+ 1;
102 dash
= strchr(family
, '-');
103 *dash
= '\0'; // !NULL because Matches() above succeeded
104 wxString
fam(family
);
106 if ( families
.Index(fam
) == wxNOT_FOUND
)
108 if ( !This
->OnFacename(fam
) )
122 // ----------------------------------------------------------------------------
124 // ----------------------------------------------------------------------------
126 bool wxFontEnumerator::EnumerateFacenames(wxFontEncoding encoding
,
132 if ( fixedWidthOnly
)
135 fonts
= CreateFontList(wxT('m'), encoding
, &nFonts
);
138 cont
= ProcessFamiliesFromFontList(this, fonts
, nFonts
);
140 XFreeFontNames(fonts
);
148 fonts
= CreateFontList(wxT('c'), encoding
, &nFonts
);
156 fonts
= CreateFontList(wxT('*'), encoding
, &nFonts
);
160 // it's ok if there are no fonts in given encoding - but it's not
161 // ok if there are no fonts at all
162 wxASSERT_MSG(encoding
!= wxFONTENCODING_SYSTEM
,
163 wxT("No fonts at all on this system?"));
169 (void)ProcessFamiliesFromFontList(this, fonts
, nFonts
);
171 XFreeFontNames(fonts
);
176 bool wxFontEnumerator::EnumerateEncodings(const wxString
& family
)
179 pattern
.Printf(wxT("-*-%s-*-*-*-*-*-*-*-*-*-*-*-*"),
180 family
.IsEmpty() ? wxT("*") : family
.c_str());
182 // get the list of all fonts
184 char **fonts
= XListFonts((Display
*)wxGetDisplay(), pattern
.mb_str(),
193 // extract the list of (unique) encodings
194 wxSortedArrayString encodings
;
195 for ( int n
= 0; n
< nFonts
; n
++ )
197 char *font
= fonts
[n
];
198 if ( !wxString(font
).Matches(wxT("-*-*-*-*-*-*-*-*-*-*-*-*-*-*")) )
200 // it's not a full font name (probably an alias)
204 // extract the family
205 char *dash
= strchr(font
+ 1, '-');
206 char *familyFont
= dash
+ 1;
207 dash
= strchr(familyFont
, '-');
208 *dash
= '\0'; // !NULL because Matches() above succeeded
210 if ( !family
.IsEmpty() && (family
!= familyFont
) )
212 // family doesn't match
216 // now extract the registry/encoding
217 char *p
= dash
+ 1; // just after the dash after family
218 dash
= strrchr(p
, '-');
220 wxString
registry(dash
+ 1);
223 dash
= strrchr(p
, '-');
224 wxString
encoding(dash
+ 1);
226 encoding
<< wxT('-') << registry
;
227 if ( encodings
.Index(encoding
) == wxNOT_FOUND
)
229 if ( !OnFontEncoding(familyFont
, encoding
) )
234 encodings
.Add(encoding
);
236 //else: already had this one
239 XFreeFontNames(fonts
);