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"
33 #ifdef __VMS__ // Xlib.h for VMS is not (yet) compatible with C++
34 // The resulting warnings are switched off here
35 #pragma message disable nosimpint
39 #pragma message enable nosimpint
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 // create the list of all fonts with the given spacing and encoding
47 static char **CreateFontList(wxChar spacing
, wxFontEncoding encoding
,
50 // extract all font families from the given font list and call our
51 // OnFacename() for each of them
52 static bool ProcessFamiliesFromFontList(wxFontEnumerator
*This
,
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 // ============================================================================
63 // ============================================================================
65 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
69 static char **CreateFontList(wxChar spacing
,
70 wxFontEncoding encoding
,
73 wxNativeEncodingInfo info
;
74 wxGetNativeFontEncoding(encoding
, &info
);
77 if ( !wxTestFontEncoding(info
) )
79 // ask font mapper for a replacement
80 (void)wxTheFontMapper
->GetAltForEncoding(encoding
, &info
);
82 #endif // wxUSE_FONTMAP
85 pattern
.Printf(wxT("-*-*-*-*-*-*-*-*-*-*-%c-*-%s-%s"),
87 info
.xregistry
.c_str(),
88 info
.xencoding
.c_str());
90 // get the list of all fonts
91 return XListFonts((Display
*)wxGetDisplay(), pattern
.mb_str(), 32767, nFonts
);
94 static bool ProcessFamiliesFromFontList(wxFontEnumerator
*This
,
98 // extract the list of (unique) font families
99 wxSortedArrayString families
;
100 for ( int n
= 0; n
< nFonts
; n
++ )
102 char *font
= fonts
[n
];
103 if ( !wxString(font
).Matches(wxT("-*-*-*-*-*-*-*-*-*-*-*-*-*-*")) )
105 // it's not a full font name (probably an alias)
109 char *dash
= strchr(font
+ 1, '-');
110 char *family
= dash
+ 1;
111 dash
= strchr(family
, '-');
112 *dash
= '\0'; // !NULL because Matches() above succeeded
113 wxString
fam(family
);
115 if ( families
.Index(fam
) == wxNOT_FOUND
)
117 if ( !This
->OnFacename(fam
) )
131 // ----------------------------------------------------------------------------
133 // ----------------------------------------------------------------------------
135 bool wxFontEnumerator::EnumerateFacenames(wxFontEncoding encoding
,
141 if ( fixedWidthOnly
)
144 fonts
= CreateFontList(wxT('m'), encoding
, &nFonts
);
147 cont
= ProcessFamiliesFromFontList(this, fonts
, nFonts
);
149 XFreeFontNames(fonts
);
157 fonts
= CreateFontList(wxT('c'), encoding
, &nFonts
);
165 fonts
= CreateFontList(wxT('*'), encoding
, &nFonts
);
169 // it's ok if there are no fonts in given encoding - but it's not
170 // ok if there are no fonts at all
171 wxASSERT_MSG(encoding
!= wxFONTENCODING_SYSTEM
,
172 wxT("No fonts at all on this system?"));
178 (void)ProcessFamiliesFromFontList(this, fonts
, nFonts
);
180 XFreeFontNames(fonts
);
185 bool wxFontEnumerator::EnumerateEncodings(const wxString
& family
)
188 pattern
.Printf(wxT("-*-%s-*-*-*-*-*-*-*-*-*-*-*-*"),
189 family
.IsEmpty() ? wxT("*") : family
.c_str());
191 // get the list of all fonts
193 char **fonts
= XListFonts((Display
*)wxGetDisplay(), pattern
.mb_str(),
202 // extract the list of (unique) encodings
203 wxSortedArrayString encodings
;
204 for ( int n
= 0; n
< nFonts
; n
++ )
206 char *font
= fonts
[n
];
207 if ( !wxString(font
).Matches(wxT("-*-*-*-*-*-*-*-*-*-*-*-*-*-*")) )
209 // it's not a full font name (probably an alias)
213 // extract the family
214 char *dash
= strchr(font
+ 1, '-');
215 char *familyFont
= dash
+ 1;
216 dash
= strchr(familyFont
, '-');
217 *dash
= '\0'; // !NULL because Matches() above succeeded
219 if ( !family
.IsEmpty() && (family
!= familyFont
) )
221 // family doesn't match
225 // now extract the registry/encoding
226 char *p
= dash
+ 1; // just after the dash after family
227 dash
= strrchr(p
, '-');
229 wxString
registry(dash
+ 1);
232 dash
= strrchr(p
, '-');
233 wxString
encoding(dash
+ 1);
235 encoding
<< wxT('-') << registry
;
236 if ( encodings
.Index(encoding
) == wxNOT_FOUND
)
238 if ( !OnFontEncoding(familyFont
, encoding
) )
243 encodings
.Add(encoding
);
245 //else: already had this one
248 XFreeFontNames(fonts
);