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
);
76 if ( !wxTestFontEncoding(info
) )
78 // ask font mapper for a replacement
79 (void)wxTheFontMapper
->GetAltForEncoding(encoding
, &info
);
83 pattern
.Printf(wxT("-*-*-*-*-*-*-*-*-*-*-%c-*-%s-%s"),
85 info
.xregistry
.c_str(),
86 info
.xencoding
.c_str());
88 // get the list of all fonts
89 return XListFonts((Display
*)wxGetDisplay(), pattern
.mb_str(), 32767, nFonts
);
92 static bool ProcessFamiliesFromFontList(wxFontEnumerator
*This
,
96 // extract the list of (unique) font families
97 wxSortedArrayString families
;
98 for ( int n
= 0; n
< nFonts
; n
++ )
100 char *font
= fonts
[n
];
101 if ( !wxString(font
).Matches(wxT("-*-*-*-*-*-*-*-*-*-*-*-*-*-*")) )
103 // it's not a full font name (probably an alias)
107 char *dash
= strchr(font
+ 1, '-');
108 char *family
= dash
+ 1;
109 dash
= strchr(family
, '-');
110 *dash
= '\0'; // !NULL because Matches() above succeeded
111 wxString
fam(family
);
113 if ( families
.Index(fam
) == wxNOT_FOUND
)
115 if ( !This
->OnFacename(fam
) )
129 // ----------------------------------------------------------------------------
131 // ----------------------------------------------------------------------------
133 bool wxFontEnumerator::EnumerateFacenames(wxFontEncoding encoding
,
139 if ( fixedWidthOnly
)
142 fonts
= CreateFontList(wxT('m'), encoding
, &nFonts
);
145 cont
= ProcessFamiliesFromFontList(this, fonts
, nFonts
);
147 XFreeFontNames(fonts
);
155 fonts
= CreateFontList(wxT('c'), encoding
, &nFonts
);
163 fonts
= CreateFontList(wxT('*'), encoding
, &nFonts
);
167 // it's ok if there are no fonts in given encoding - but it's not
168 // ok if there are no fonts at all
169 wxASSERT_MSG(encoding
!= wxFONTENCODING_SYSTEM
,
170 wxT("No fonts at all on this system?"));
176 (void)ProcessFamiliesFromFontList(this, fonts
, nFonts
);
178 XFreeFontNames(fonts
);
183 bool wxFontEnumerator::EnumerateEncodings(const wxString
& family
)
186 pattern
.Printf(wxT("-*-%s-*-*-*-*-*-*-*-*-*-*-*-*"),
187 family
.IsEmpty() ? wxT("*") : family
.c_str());
189 // get the list of all fonts
191 char **fonts
= XListFonts((Display
*)wxGetDisplay(), pattern
.mb_str(),
200 // extract the list of (unique) encodings
201 wxSortedArrayString encodings
;
202 for ( int n
= 0; n
< nFonts
; n
++ )
204 char *font
= fonts
[n
];
205 if ( !wxString(font
).Matches(wxT("-*-*-*-*-*-*-*-*-*-*-*-*-*-*")) )
207 // it's not a full font name (probably an alias)
211 // extract the family
212 char *dash
= strchr(font
+ 1, '-');
213 char *familyFont
= dash
+ 1;
214 dash
= strchr(familyFont
, '-');
215 *dash
= '\0'; // !NULL because Matches() above succeeded
217 if ( !family
.IsEmpty() && (family
!= familyFont
) )
219 // family doesn't match
223 // now extract the registry/encoding
224 char *p
= dash
+ 1; // just after the dash after family
225 dash
= strrchr(p
, '-');
227 wxString
registry(dash
+ 1);
230 dash
= strrchr(p
, '-');
231 wxString
encoding(dash
+ 1);
233 encoding
<< wxT('-') << registry
;
234 if ( encodings
.Index(encoding
) == wxNOT_FOUND
)
236 if ( !OnFontEncoding(familyFont
, encoding
) )
241 encodings
.Add(encoding
);
243 //else: already had this one
246 XFreeFontNames(fonts
);