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"
30 #include "wx/fontmap.h"
31 #include "wx/fontenum.h"
32 #include "wx/fontutil.h"
34 #ifdef __VMS__ // Xlib.h for VMS is not (yet) compatible with C++
35 // The resulting warnings are switched off here
36 #pragma message disable nosimpint
40 #pragma message enable nosimpint
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 // create the list of all fonts with the given spacing and encoding
48 static char **CreateFontList(wxChar spacing
, wxFontEncoding encoding
,
51 // extract all font families from the given font list and call our
52 // OnFacename() for each of them
53 static bool ProcessFamiliesFromFontList(wxFontEnumerator
*This
,
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 // ============================================================================
64 // ============================================================================
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
70 static char **CreateFontList(wxChar spacing
,
71 wxFontEncoding encoding
,
74 wxNativeEncodingInfo info
;
75 wxGetNativeFontEncoding(encoding
, &info
);
78 if ( !wxTestFontEncoding(info
) )
80 // ask font mapper for a replacement
81 (void)wxTheFontMapper
->GetAltForEncoding(encoding
, &info
);
83 #endif // wxUSE_FONTMAP
86 pattern
.Printf(wxT("-*-*-*-*-*-*-*-*-*-*-%c-*-%s-%s"),
88 info
.xregistry
.c_str(),
89 info
.xencoding
.c_str());
91 // get the list of all fonts
92 return XListFonts((Display
*)wxGetDisplay(), pattern
.mb_str(), 32767, nFonts
);
95 static bool ProcessFamiliesFromFontList(wxFontEnumerator
*This
,
100 wxRegEx
re(wxT("^(-[^-]*){14}$"), wxRE_NOSUB
);
101 #endif // wxUSE_REGEX
103 // extract the list of (unique) font families
104 wxSortedArrayString families
;
105 for ( int n
= 0; n
< nFonts
; n
++ )
107 char *font
= fonts
[n
];
109 if ( re
.Matches(font
) )
110 #else // !wxUSE_REGEX
111 if ( !wxString(font
).Matches(wxT("-*-*-*-*-*-*-*-*-*-*-*-*-*-*")) )
112 #endif // wxUSE_REGEX/!wxUSE_REGEX
114 // it's not a full font name (probably an alias)
118 char *dash
= strchr(font
+ 1, '-');
119 char *family
= dash
+ 1;
120 dash
= strchr(family
, '-');
121 *dash
= '\0'; // !NULL because Matches() above succeeded
122 wxString
fam(family
);
124 if ( families
.Index(fam
) == wxNOT_FOUND
)
126 if ( !This
->OnFacename(fam
) )
140 // ----------------------------------------------------------------------------
142 // ----------------------------------------------------------------------------
144 bool wxFontEnumerator::EnumerateFacenames(wxFontEncoding encoding
,
150 if ( fixedWidthOnly
)
153 fonts
= CreateFontList(wxT('m'), encoding
, &nFonts
);
156 cont
= ProcessFamiliesFromFontList(this, fonts
, nFonts
);
158 XFreeFontNames(fonts
);
166 fonts
= CreateFontList(wxT('c'), encoding
, &nFonts
);
174 fonts
= CreateFontList(wxT('*'), encoding
, &nFonts
);
178 // it's ok if there are no fonts in given encoding - but it's not
179 // ok if there are no fonts at all
180 wxASSERT_MSG(encoding
!= wxFONTENCODING_SYSTEM
,
181 wxT("No fonts at all on this system?"));
187 (void)ProcessFamiliesFromFontList(this, fonts
, nFonts
);
189 XFreeFontNames(fonts
);
194 bool wxFontEnumerator::EnumerateEncodings(const wxString
& family
)
197 pattern
.Printf(wxT("-*-%s-*-*-*-*-*-*-*-*-*-*-*-*"),
198 family
.IsEmpty() ? wxT("*") : family
.c_str());
200 // get the list of all fonts
202 char **fonts
= XListFonts((Display
*)wxGetDisplay(), pattern
.mb_str(),
211 // extract the list of (unique) encodings
212 wxSortedArrayString encodings
;
213 for ( int n
= 0; n
< nFonts
; n
++ )
215 char *font
= fonts
[n
];
216 if ( !wxString(font
).Matches(wxT("-*-*-*-*-*-*-*-*-*-*-*-*-*-*")) )
218 // it's not a full font name (probably an alias)
222 // extract the family
223 char *dash
= strchr(font
+ 1, '-');
224 char *familyFont
= dash
+ 1;
225 dash
= strchr(familyFont
, '-');
226 *dash
= '\0'; // !NULL because Matches() above succeeded
228 if ( !family
.IsEmpty() && (family
!= familyFont
) )
230 // family doesn't match
234 // now extract the registry/encoding
235 char *p
= dash
+ 1; // just after the dash after family
236 dash
= strrchr(p
, '-');
238 wxString
registry(dash
+ 1);
241 dash
= strrchr(p
, '-');
242 wxString
encoding(dash
+ 1);
244 encoding
<< wxT('-') << registry
;
245 if ( encodings
.Index(encoding
) == wxNOT_FOUND
)
247 if ( !OnFontEncoding(familyFont
, encoding
) )
252 encodings
.Add(encoding
);
254 //else: already had this one
257 XFreeFontNames(fonts
);