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 // ----------------------------------------------------------------------------
71 static char **CreateFontList(wxChar spacing
,
72 wxFontEncoding encoding
,
75 wxNativeEncodingInfo info
;
76 wxGetNativeFontEncoding(encoding
, &info
);
79 if ( !wxTestFontEncoding(info
) )
81 // ask font mapper for a replacement
82 (void)wxTheFontMapper
->GetAltForEncoding(encoding
, &info
);
84 #endif // wxUSE_FONTMAP
87 pattern
.Printf(wxT("-*-*-*-*-*-*-*-*-*-*-%c-*-%s-%s"),
89 info
.xregistry
.c_str(),
90 info
.xencoding
.c_str());
92 // get the list of all fonts
93 return XListFonts((Display
*)wxGetDisplay(), pattern
.mb_str(), 32767, nFonts
);
96 static bool ProcessFamiliesFromFontList(wxFontEnumerator
*This
,
101 wxRegEx
re(wxT("^(-[^-]*){14}$"), wxRE_NOSUB
);
102 #endif // wxUSE_REGEX
104 // extract the list of (unique) font families
105 wxSortedArrayString families
;
106 for ( int n
= 0; n
< nFonts
; n
++ )
108 char *font
= fonts
[n
];
110 if ( !re
.Matches(font
) )
111 #else // !wxUSE_REGEX
112 if ( !wxString(font
).Matches(wxT("-*-*-*-*-*-*-*-*-*-*-*-*-*-*")) )
113 #endif // wxUSE_REGEX/!wxUSE_REGEX
115 // it's not a full font name (probably an alias)
119 char *dash
= strchr(font
+ 1, '-');
120 char *family
= dash
+ 1;
121 dash
= strchr(family
, '-');
122 *dash
= '\0'; // !NULL because Matches() above succeeded
123 wxString
fam(family
);
125 if ( families
.Index(fam
) == wxNOT_FOUND
)
127 if ( !This
->OnFacename(fam
) )
143 // ----------------------------------------------------------------------------
145 // ----------------------------------------------------------------------------
147 bool wxFontEnumerator::EnumerateFacenames(wxFontEncoding encoding
,
156 if ( fixedWidthOnly
)
159 fonts
= CreateFontList(wxT('m'), encoding
, &nFonts
);
162 cont
= ProcessFamiliesFromFontList(this, fonts
, nFonts
);
164 XFreeFontNames(fonts
);
172 fonts
= CreateFontList(wxT('c'), encoding
, &nFonts
);
180 fonts
= CreateFontList(wxT('*'), encoding
, &nFonts
);
184 // it's ok if there are no fonts in given encoding - but it's not
185 // ok if there are no fonts at all
186 wxASSERT_MSG(encoding
!= wxFONTENCODING_SYSTEM
,
187 wxT("No fonts at all on this system?"));
193 (void)ProcessFamiliesFromFontList(this, fonts
, nFonts
);
195 XFreeFontNames(fonts
);
201 bool wxFontEnumerator::EnumerateEncodings(const wxString
& family
)
207 pattern
.Printf(wxT("-*-%s-*-*-*-*-*-*-*-*-*-*-*-*"),
208 family
.IsEmpty() ? wxT("*") : family
.c_str());
210 // get the list of all fonts
212 char **fonts
= XListFonts((Display
*)wxGetDisplay(), pattern
.mb_str(),
221 // extract the list of (unique) encodings
222 wxSortedArrayString encodings
;
223 for ( int n
= 0; n
< nFonts
; n
++ )
225 char *font
= fonts
[n
];
226 if ( !wxString(font
).Matches(wxT("-*-*-*-*-*-*-*-*-*-*-*-*-*-*")) )
228 // it's not a full font name (probably an alias)
232 // extract the family
233 char *dash
= strchr(font
+ 1, '-');
234 char *familyFont
= dash
+ 1;
235 dash
= strchr(familyFont
, '-');
236 *dash
= '\0'; // !NULL because Matches() above succeeded
238 if ( !family
.IsEmpty() && (family
!= familyFont
) )
240 // family doesn't match
244 // now extract the registry/encoding
245 char *p
= dash
+ 1; // just after the dash after family
246 dash
= strrchr(p
, '-');
248 wxString
registry(dash
+ 1);
251 dash
= strrchr(p
, '-');
252 wxString
encoding(dash
+ 1);
254 encoding
<< wxT('-') << registry
;
255 if ( encodings
.Index(encoding
) == wxNOT_FOUND
)
257 if ( !OnFontEncoding(familyFont
, encoding
) )
262 encodings
.Add(encoding
);
264 //else: already had this one
267 XFreeFontNames(fonts
);