1 /////////////////////////////////////////////////////////////////////////////
2 // Name: unix/fontutil.cpp
3 // Purpose: Font helper functions for X11 (GDK/X)
4 // Author: Vadim Zeitlin
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "fontutil.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
34 #include "wx/fontutil.h"
35 #include "wx/fontmap.h"
36 #include "wx/tokenzr.h"
38 // ----------------------------------------------------------------------------
40 // ----------------------------------------------------------------------------
42 // define the functions to create and destroy native fonts for this toolkit
44 static inline wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
46 return XLoadQueryFont((Display
*)wxGetDisplay(), fontSpec
);
49 static inline void wxFreeFont(wxNativeFont font
)
51 XFreeFont((Display
*)wxGetDisplay(), font
);
53 #elif defined(__WXGTK__)
56 static inline wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
58 return gdk_font_load( wxConvertWX2MB(fontSpec
) );
61 static inline void wxFreeFont(wxNativeFont font
)
66 #error "Unknown GUI toolkit"
69 static bool wxTestFontSpec(const wxString
& fontspec
);
71 static wxNativeFont
wxLoadQueryFont(int pointSize
,
76 const wxString
& facename
,
77 const wxString
& xregistry
,
78 const wxString
& xencoding
);
80 // ============================================================================
82 // ============================================================================
84 // ----------------------------------------------------------------------------
85 // wxNativeEncodingInfo
86 // ----------------------------------------------------------------------------
88 // convert to/from the string representation: format is
89 // registry-encoding[-facename]
90 bool wxNativeEncodingInfo::FromString(const wxString
& s
)
92 wxStringTokenizer
tokenizer(s
, _T("-"));
94 xregistry
= tokenizer
.GetNextToken();
98 xencoding
= tokenizer
.GetNextToken();
103 facename
= tokenizer
.GetNextToken();
108 wxString
wxNativeEncodingInfo::ToString() const
111 s
<< xregistry
<< _T('-') << xencoding
;
114 s
<< _T('-') << facename
;
120 // ----------------------------------------------------------------------------
122 // ----------------------------------------------------------------------------
124 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
125 wxNativeEncodingInfo
*info
)
127 wxCHECK_MSG( info
, FALSE
, _T("bad pointer in wxGetNativeFontEncoding") );
129 if ( encoding
== wxFONTENCODING_DEFAULT
)
131 encoding
= wxFont::GetDefaultEncoding();
136 case wxFONTENCODING_ISO8859_1
:
137 case wxFONTENCODING_ISO8859_2
:
138 case wxFONTENCODING_ISO8859_3
:
139 case wxFONTENCODING_ISO8859_4
:
140 case wxFONTENCODING_ISO8859_5
:
141 case wxFONTENCODING_ISO8859_6
:
142 case wxFONTENCODING_ISO8859_7
:
143 case wxFONTENCODING_ISO8859_8
:
144 case wxFONTENCODING_ISO8859_9
:
145 case wxFONTENCODING_ISO8859_10
:
146 case wxFONTENCODING_ISO8859_11
:
147 case wxFONTENCODING_ISO8859_13
:
148 case wxFONTENCODING_ISO8859_14
:
149 case wxFONTENCODING_ISO8859_15
:
151 int cp
= encoding
- wxFONTENCODING_ISO8859_1
+ 1;
152 info
->xregistry
= wxT("iso8859");
153 info
->xencoding
.Printf(wxT("%d"), cp
);
157 case wxFONTENCODING_KOI8
:
158 info
->xregistry
= wxT("koi8");
160 // we don't make distinction between koi8-r and koi8-u (so far)
161 info
->xencoding
= wxT("*");
164 case wxFONTENCODING_CP1250
:
165 case wxFONTENCODING_CP1251
:
166 case wxFONTENCODING_CP1252
:
167 case wxFONTENCODING_CP1253
:
168 case wxFONTENCODING_CP1254
:
169 case wxFONTENCODING_CP1255
:
170 case wxFONTENCODING_CP1256
:
171 case wxFONTENCODING_CP1257
:
173 int cp
= encoding
- wxFONTENCODING_CP1250
+ 1250;
174 info
->xregistry
= wxT("microsoft");
175 info
->xencoding
.Printf(wxT("cp%d"), cp
);
179 case wxFONTENCODING_SYSTEM
:
181 info
->xencoding
= wxT('*');
185 // don't know how to translate this encoding into X fontspec
192 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
195 fontspec
.Printf(_T("-*-%s-*-*-*-*-*-*-*-*-*-*-%s-%s"),
196 !info
.facename
? _T("*") : info
.facename
.c_str(),
197 info
.xregistry
.c_str(),
198 info
.xencoding
.c_str());
200 return wxTestFontSpec(fontspec
);
203 // ----------------------------------------------------------------------------
204 // X-specific functions
205 // ----------------------------------------------------------------------------
207 wxNativeFont
wxLoadQueryNearestFont(int pointSize
,
212 const wxString
&facename
,
213 wxFontEncoding encoding
)
215 // first determine the encoding - if the font doesn't exist at all in this
216 // encoding, it's useless to do all other approximations (i.e. size,
217 // family &c don't matter much)
218 wxNativeEncodingInfo info
;
219 if ( !wxGetNativeFontEncoding(encoding
, &info
) ||
220 !wxTestFontEncoding(info
) )
222 if ( !wxTheFontMapper
->GetAltForEncoding(encoding
, &info
) )
224 // unspported encoding - replace it with the default
226 // NB: we can't just return 0 from here because wxGTK code doesn't
227 // check for it (i.e. it supposes that we'll always succeed),
228 // so it would provoke a crash
229 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
232 //else: we have the correct xregistry/xencoding in info structure
234 wxNativeFont font
= wxLoadQueryFont( pointSize
, family
, style
, weight
,
235 underlined
, facename
,
236 info
.xregistry
, info
.xencoding
);
240 // search up and down by stepsize 10
241 int max_size
= pointSize
+ 20 * (1 + (pointSize
/180));
242 int min_size
= pointSize
- 20 * (1 + (pointSize
/180));
246 // Search for smaller size (approx.)
247 for ( i
= pointSize
- 10; !font
&& i
>= 10 && i
>= min_size
; i
-= 10 )
249 font
= wxLoadQueryFont(i
, family
, style
, weight
, underlined
,
250 facename
, info
.xregistry
, info
.xencoding
);
253 // Search for larger size (approx.)
254 for ( i
= pointSize
+ 10; !font
&& i
<= max_size
; i
+= 10 )
256 font
= wxLoadQueryFont(i
, family
, style
, weight
, underlined
,
257 facename
, info
.xregistry
, info
.xencoding
);
260 // Try default family
261 if ( !font
&& family
!= wxDEFAULT
)
263 font
= wxLoadQueryFont(pointSize
, wxDEFAULT
, style
, weight
,
264 underlined
, facename
,
265 info
.xregistry
, info
.xencoding
);
271 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
272 underlined
, facename
,
273 info
.xregistry
, info
.xencoding
);
279 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
280 underlined
, wxEmptyString
,
281 info
.xregistry
, info
.xencoding
);
288 // ----------------------------------------------------------------------------
290 // ----------------------------------------------------------------------------
292 // returns TRUE if there are any fonts matching this font spec
293 static bool wxTestFontSpec(const wxString
& fontspec
)
295 wxNativeFont test
= wxLoadFont(fontspec
);
308 static wxNativeFont
wxLoadQueryFont(int pointSize
,
312 bool WXUNUSED(underlined
),
313 const wxString
& facename
,
314 const wxString
& xregistry
,
315 const wxString
& xencoding
)
320 case wxDECORATIVE
: xfamily
= wxT("lucida"); break;
321 case wxROMAN
: xfamily
= wxT("times"); break;
322 case wxMODERN
: xfamily
= wxT("courier"); break;
323 case wxSWISS
: xfamily
= wxT("helvetica"); break;
324 case wxTELETYPE
: xfamily
= wxT("lucidatypewriter"); break;
325 case wxSCRIPT
: xfamily
= wxT("utopia"); break;
326 default: xfamily
= wxT("*");
330 if (!facename
.IsEmpty())
332 fontSpec
.Printf(wxT("-*-%s-*-*-normal-*-*-*-*-*-*-*-*-*"),
335 if ( wxTestFontSpec(fontSpec
) )
339 //else: no such family, use default one instead
345 case wxITALIC
: xstyle
= wxT("i"); break;
346 case wxSLANT
: xstyle
= wxT("o"); break;
347 case wxNORMAL
: xstyle
= wxT("r"); break;
348 default: xstyle
= wxT("*"); break;
356 fontSpec
.Printf(wxT("-*-%s-bold-*-*-*-*-*-*-*-*-*-*-*"),
358 if ( wxTestFontSpec(fontSpec
) )
360 xweight
= wxT("bold");
363 fontSpec
.Printf(wxT("-*-%s-heavy-*-*-*-*-*-*-*-*-*-*-*"),
365 if ( wxTestFontSpec(fontSpec
) )
367 xweight
= wxT("heavy");
370 fontSpec
.Printf(wxT("-*-%s-extrabold-*-*-*-*-*-*-*-*-*-*-*"),
372 if ( wxTestFontSpec(fontSpec
) )
374 xweight
= wxT("extrabold");
377 fontSpec
.Printf(wxT("-*-%s-demibold-*-*-*-*-*-*-*-*-*-*-*"),
379 if ( wxTestFontSpec(fontSpec
) )
381 xweight
= wxT("demibold");
384 fontSpec
.Printf(wxT("-*-%s-black-*-*-*-*-*-*-*-*-*-*-*"),
386 if ( wxTestFontSpec(fontSpec
) )
388 xweight
= wxT("black");
391 fontSpec
.Printf(wxT("-*-%s-ultrablack-*-*-*-*-*-*-*-*-*-*-*"),
393 if ( wxTestFontSpec(fontSpec
) )
395 xweight
= wxT("ultrablack");
402 fontSpec
.Printf(wxT("-*-%s-light-*-*-*-*-*-*-*-*-*-*-*"),
404 if ( wxTestFontSpec(fontSpec
) )
406 xweight
= wxT("light");
409 fontSpec
.Printf(wxT("-*-%s-thin-*-*-*-*-*-*-*-*-*-*-*"),
411 if ( wxTestFontSpec(fontSpec
) )
413 xweight
= wxT("thin");
420 fontSpec
.Printf(wxT("-*-%s-medium-*-*-*-*-*-*-*-*-*-*-*"),
422 if ( wxTestFontSpec(fontSpec
) )
424 xweight
= wxT("medium");
427 fontSpec
.Printf(wxT("-*-%s-normal-*-*-*-*-*-*-*-*-*-*-*"),
429 if ( wxTestFontSpec(fontSpec
) )
431 xweight
= wxT("normal");
434 fontSpec
.Printf(wxT("-*-%s-regular-*-*-*-*-*-*-*-*-*-*-*"),
436 if ( wxTestFontSpec(fontSpec
) )
438 xweight
= wxT("regular");
444 default: xweight
= wxT("*"); break;
447 // construct the X font spec from our data
448 fontSpec
.Printf(wxT("-*-%s-%s-%s-normal-*-*-%d-*-*-*-*-%s-%s"),
449 xfamily
.c_str(), xweight
.c_str(), xstyle
.c_str(),
450 pointSize
, xregistry
.c_str(), xencoding
.c_str());
452 return wxLoadFont(fontSpec
);