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"
37 #include "wx/utils.h" // for wxGetDisplay()
38 #elif defined(__WXGTK__)
42 #include "wx/fontutil.h"
43 #include "wx/fontmap.h"
44 #include "wx/tokenzr.h"
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 // define the functions to create and destroy native fonts for this toolkit
52 static inline wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
54 return XLoadQueryFont((Display
*)wxGetDisplay(), fontSpec
);
57 static inline void wxFreeFont(wxNativeFont font
)
59 XFreeFont((Display
*)wxGetDisplay(), font
);
61 #elif defined(__WXGTK__)
62 static inline wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
64 return gdk_font_load( wxConvertWX2MB(fontSpec
) );
67 static inline void wxFreeFont(wxNativeFont font
)
72 #error "Unknown GUI toolkit"
75 static bool wxTestFontSpec(const wxString
& fontspec
);
77 static wxNativeFont
wxLoadQueryFont(int pointSize
,
82 const wxString
& facename
,
83 const wxString
& xregistry
,
84 const wxString
& xencoding
);
86 // ============================================================================
88 // ============================================================================
90 // ----------------------------------------------------------------------------
91 // wxNativeEncodingInfo
92 // ----------------------------------------------------------------------------
94 // convert to/from the string representation: format is
95 // registry-encoding[-facename]
96 bool wxNativeEncodingInfo::FromString(const wxString
& s
)
98 wxStringTokenizer
tokenizer(s
, _T("-"));
100 xregistry
= tokenizer
.GetNextToken();
104 xencoding
= tokenizer
.GetNextToken();
109 facename
= tokenizer
.GetNextToken();
114 wxString
wxNativeEncodingInfo::ToString() const
117 s
<< xregistry
<< _T('-') << xencoding
;
120 s
<< _T('-') << facename
;
126 // ----------------------------------------------------------------------------
128 // ----------------------------------------------------------------------------
130 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
131 wxNativeEncodingInfo
*info
)
133 wxCHECK_MSG( info
, FALSE
, _T("bad pointer in wxGetNativeFontEncoding") );
135 if ( encoding
== wxFONTENCODING_DEFAULT
)
137 encoding
= wxFont::GetDefaultEncoding();
142 case wxFONTENCODING_ISO8859_1
:
143 case wxFONTENCODING_ISO8859_2
:
144 case wxFONTENCODING_ISO8859_3
:
145 case wxFONTENCODING_ISO8859_4
:
146 case wxFONTENCODING_ISO8859_5
:
147 case wxFONTENCODING_ISO8859_6
:
148 case wxFONTENCODING_ISO8859_7
:
149 case wxFONTENCODING_ISO8859_8
:
150 case wxFONTENCODING_ISO8859_9
:
151 case wxFONTENCODING_ISO8859_10
:
152 case wxFONTENCODING_ISO8859_11
:
153 case wxFONTENCODING_ISO8859_13
:
154 case wxFONTENCODING_ISO8859_14
:
155 case wxFONTENCODING_ISO8859_15
:
157 int cp
= encoding
- wxFONTENCODING_ISO8859_1
+ 1;
158 info
->xregistry
= wxT("iso8859");
159 info
->xencoding
.Printf(wxT("%d"), cp
);
163 case wxFONTENCODING_KOI8
:
164 info
->xregistry
= wxT("koi8");
166 // we don't make distinction between koi8-r and koi8-u (so far)
167 info
->xencoding
= wxT("*");
170 case wxFONTENCODING_CP1250
:
171 case wxFONTENCODING_CP1251
:
172 case wxFONTENCODING_CP1252
:
173 case wxFONTENCODING_CP1253
:
174 case wxFONTENCODING_CP1254
:
175 case wxFONTENCODING_CP1255
:
176 case wxFONTENCODING_CP1256
:
177 case wxFONTENCODING_CP1257
:
179 int cp
= encoding
- wxFONTENCODING_CP1250
+ 1250;
180 info
->xregistry
= wxT("microsoft");
181 info
->xencoding
.Printf(wxT("cp%d"), cp
);
185 case wxFONTENCODING_SYSTEM
:
187 info
->xencoding
= wxT("*");
191 // don't know how to translate this encoding into X fontspec
198 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
201 fontspec
.Printf(_T("-*-%s-*-*-*-*-*-*-*-*-*-*-%s-%s"),
202 !info
.facename
? _T("*") : info
.facename
.c_str(),
203 info
.xregistry
.c_str(),
204 info
.xencoding
.c_str());
206 return wxTestFontSpec(fontspec
);
209 // ----------------------------------------------------------------------------
210 // X-specific functions
211 // ----------------------------------------------------------------------------
213 wxNativeFont
wxLoadQueryNearestFont(int pointSize
,
218 const wxString
&facename
,
219 wxFontEncoding encoding
)
221 // first determine the encoding - if the font doesn't exist at all in this
222 // encoding, it's useless to do all other approximations (i.e. size,
223 // family &c don't matter much)
224 wxNativeEncodingInfo info
;
225 if (encoding
== wxFONTENCODING_SYSTEM
)
227 // This will always work so we don't test to save time
228 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
232 if ( !wxGetNativeFontEncoding(encoding
, &info
) ||
233 !wxTestFontEncoding(info
) )
235 if ( !wxTheFontMapper
->GetAltForEncoding(encoding
, &info
) )
237 // unspported encoding - replace it with the default
239 // NB: we can't just return 0 from here because wxGTK code doesn't
240 // check for it (i.e. it supposes that we'll always succeed),
241 // so it would provoke a crash
242 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
247 // OK, we have the correct xregistry/xencoding in info structure
249 wxNativeFont font
= wxLoadQueryFont( pointSize
, family
, style
, weight
,
250 underlined
, facename
,
251 info
.xregistry
, info
.xencoding
);
255 // search up and down by stepsize 10
256 int max_size
= pointSize
+ 20 * (1 + (pointSize
/180));
257 int min_size
= pointSize
- 20 * (1 + (pointSize
/180));
261 // Search for smaller size (approx.)
262 for ( i
= pointSize
- 10; !font
&& i
>= 10 && i
>= min_size
; i
-= 10 )
264 font
= wxLoadQueryFont(i
, family
, style
, weight
, underlined
,
265 facename
, info
.xregistry
, info
.xencoding
);
268 // Search for larger size (approx.)
269 for ( i
= pointSize
+ 10; !font
&& i
<= max_size
; i
+= 10 )
271 font
= wxLoadQueryFont(i
, family
, style
, weight
, underlined
,
272 facename
, info
.xregistry
, info
.xencoding
);
275 // Try default family
276 if ( !font
&& family
!= wxDEFAULT
)
278 font
= wxLoadQueryFont(pointSize
, wxDEFAULT
, style
, weight
,
279 underlined
, facename
,
280 info
.xregistry
, info
.xencoding
);
286 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
287 underlined
, facename
,
288 info
.xregistry
, info
.xencoding
);
294 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
295 underlined
, wxEmptyString
,
296 info
.xregistry
, info
.xencoding
);
303 // ----------------------------------------------------------------------------
305 // ----------------------------------------------------------------------------
307 // returns TRUE if there are any fonts matching this font spec
308 static bool wxTestFontSpec(const wxString
& fontspec
)
310 wxNativeFont test
= wxLoadFont(fontspec
);
323 static wxNativeFont
wxLoadQueryFont(int pointSize
,
327 bool WXUNUSED(underlined
),
328 const wxString
& facename
,
329 const wxString
& xregistry
,
330 const wxString
& xencoding
)
335 case wxDECORATIVE
: xfamily
= wxT("lucida"); break;
336 case wxROMAN
: xfamily
= wxT("times"); break;
337 case wxMODERN
: xfamily
= wxT("courier"); break;
338 case wxSWISS
: xfamily
= wxT("helvetica"); break;
339 case wxTELETYPE
: xfamily
= wxT("lucidatypewriter"); break;
340 case wxSCRIPT
: xfamily
= wxT("utopia"); break;
341 default: xfamily
= wxT("*");
345 if (!facename
.IsEmpty())
347 fontSpec
.Printf(wxT("-*-%s-*-*-normal-*-*-*-*-*-*-*-*-*"),
350 if ( wxTestFontSpec(fontSpec
) )
354 //else: no such family, use default one instead
360 case wxITALIC
: xstyle
= wxT("i"); break;
361 case wxSLANT
: xstyle
= wxT("o"); break;
362 case wxNORMAL
: xstyle
= wxT("r"); break;
363 default: xstyle
= wxT("*"); break;
371 fontSpec
.Printf(wxT("-*-%s-bold-*-*-*-*-*-*-*-*-*-*-*"),
373 if ( wxTestFontSpec(fontSpec
) )
375 xweight
= wxT("bold");
378 fontSpec
.Printf(wxT("-*-%s-heavy-*-*-*-*-*-*-*-*-*-*-*"),
380 if ( wxTestFontSpec(fontSpec
) )
382 xweight
= wxT("heavy");
385 fontSpec
.Printf(wxT("-*-%s-extrabold-*-*-*-*-*-*-*-*-*-*-*"),
387 if ( wxTestFontSpec(fontSpec
) )
389 xweight
= wxT("extrabold");
392 fontSpec
.Printf(wxT("-*-%s-demibold-*-*-*-*-*-*-*-*-*-*-*"),
394 if ( wxTestFontSpec(fontSpec
) )
396 xweight
= wxT("demibold");
399 fontSpec
.Printf(wxT("-*-%s-black-*-*-*-*-*-*-*-*-*-*-*"),
401 if ( wxTestFontSpec(fontSpec
) )
403 xweight
= wxT("black");
406 fontSpec
.Printf(wxT("-*-%s-ultrablack-*-*-*-*-*-*-*-*-*-*-*"),
408 if ( wxTestFontSpec(fontSpec
) )
410 xweight
= wxT("ultrablack");
417 fontSpec
.Printf(wxT("-*-%s-light-*-*-*-*-*-*-*-*-*-*-*"),
419 if ( wxTestFontSpec(fontSpec
) )
421 xweight
= wxT("light");
424 fontSpec
.Printf(wxT("-*-%s-thin-*-*-*-*-*-*-*-*-*-*-*"),
426 if ( wxTestFontSpec(fontSpec
) )
428 xweight
= wxT("thin");
435 fontSpec
.Printf(wxT("-*-%s-medium-*-*-*-*-*-*-*-*-*-*-*"),
437 if ( wxTestFontSpec(fontSpec
) )
439 xweight
= wxT("medium");
442 fontSpec
.Printf(wxT("-*-%s-normal-*-*-*-*-*-*-*-*-*-*-*"),
444 if ( wxTestFontSpec(fontSpec
) )
446 xweight
= wxT("normal");
449 fontSpec
.Printf(wxT("-*-%s-regular-*-*-*-*-*-*-*-*-*-*-*"),
451 if ( wxTestFontSpec(fontSpec
) )
453 xweight
= wxT("regular");
459 default: xweight
= wxT("*"); break;
462 // construct the X font spec from our data
463 fontSpec
.Printf(wxT("-*-%s-%s-%s-normal-*-*-%d-*-*-*-*-%s-%s"),
464 xfamily
.c_str(), xweight
.c_str(), xstyle
.c_str(),
465 pointSize
, xregistry
.c_str(), xencoding
.c_str());
467 return wxLoadFont(fontSpec
);