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(), (XFontStruct
*)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 if ( encoding
== wxFONTENCODING_DEFAULT
)
223 encoding
= wxFont::GetDefaultEncoding();
226 // first determine the encoding - if the font doesn't exist at all in this
227 // encoding, it's useless to do all other approximations (i.e. size,
228 // family &c don't matter much)
229 wxNativeEncodingInfo info
;
230 if ( encoding
== wxFONTENCODING_SYSTEM
)
232 // This will always work so we don't test to save time
233 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
237 if ( !wxGetNativeFontEncoding(encoding
, &info
) ||
238 !wxTestFontEncoding(info
) )
240 if ( !wxTheFontMapper
->GetAltForEncoding(encoding
, &info
) )
242 // unspported encoding - replace it with the default
244 // NB: we can't just return 0 from here because wxGTK code doesn't
245 // check for it (i.e. it supposes that we'll always succeed),
246 // so it would provoke a crash
247 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
252 // OK, we have the correct xregistry/xencoding in info structure
253 wxNativeFont font
= wxLoadQueryFont( pointSize
, family
, style
, weight
,
254 underlined
, facename
,
255 info
.xregistry
, info
.xencoding
);
259 // search up and down by stepsize 10
260 int max_size
= pointSize
+ 20 * (1 + (pointSize
/180));
261 int min_size
= pointSize
- 20 * (1 + (pointSize
/180));
265 // Search for smaller size (approx.)
266 for ( i
= pointSize
- 10; !font
&& i
>= 10 && i
>= min_size
; i
-= 10 )
268 font
= wxLoadQueryFont(i
, family
, style
, weight
, underlined
,
269 facename
, info
.xregistry
, info
.xencoding
);
272 // Search for larger size (approx.)
273 for ( i
= pointSize
+ 10; !font
&& i
<= max_size
; i
+= 10 )
275 font
= wxLoadQueryFont(i
, family
, style
, weight
, underlined
,
276 facename
, info
.xregistry
, info
.xencoding
);
279 // Try default family
280 if ( !font
&& family
!= wxDEFAULT
)
282 font
= wxLoadQueryFont(pointSize
, wxDEFAULT
, style
, weight
,
283 underlined
, facename
,
284 info
.xregistry
, info
.xencoding
);
290 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
291 underlined
, facename
,
292 info
.xregistry
, info
.xencoding
);
298 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
299 underlined
, wxEmptyString
,
300 info
.xregistry
, info
.xencoding
);
307 // ----------------------------------------------------------------------------
309 // ----------------------------------------------------------------------------
311 // returns TRUE if there are any fonts matching this font spec
312 static bool wxTestFontSpec(const wxString
& fontspec
)
314 // some X servers will fail to load this font because there are too many
315 // matches so we must test explicitly for this
316 if ( fontspec
== _T("-*-*-*-*-*-*-*-*-*-*-*-*-*-*") )
321 wxNativeFont test
= wxLoadFont(fontspec
);
334 static wxNativeFont
wxLoadQueryFont(int pointSize
,
338 bool WXUNUSED(underlined
),
339 const wxString
& facename
,
340 const wxString
& xregistry
,
341 const wxString
& xencoding
)
346 case wxDECORATIVE
: xfamily
= wxT("lucida"); break;
347 case wxROMAN
: xfamily
= wxT("times"); break;
348 case wxMODERN
: xfamily
= wxT("courier"); break;
349 case wxSWISS
: xfamily
= wxT("helvetica"); break;
350 case wxTELETYPE
: xfamily
= wxT("lucidatypewriter"); break;
351 case wxSCRIPT
: xfamily
= wxT("utopia"); break;
352 default: xfamily
= wxT("*");
356 if (!facename
.IsEmpty())
358 fontSpec
.Printf(wxT("-*-%s-*-*-normal-*-*-*-*-*-*-*-*-*"),
361 if ( wxTestFontSpec(fontSpec
) )
365 //else: no such family, use default one instead
371 case wxITALIC
: xstyle
= wxT("i"); break;
372 case wxSLANT
: xstyle
= wxT("o"); break;
373 case wxNORMAL
: xstyle
= wxT("r"); break;
374 default: xstyle
= wxT("*"); break;
382 fontSpec
.Printf(wxT("-*-%s-bold-*-*-*-*-*-*-*-*-*-*-*"),
384 if ( wxTestFontSpec(fontSpec
) )
386 xweight
= wxT("bold");
389 fontSpec
.Printf(wxT("-*-%s-heavy-*-*-*-*-*-*-*-*-*-*-*"),
391 if ( wxTestFontSpec(fontSpec
) )
393 xweight
= wxT("heavy");
396 fontSpec
.Printf(wxT("-*-%s-extrabold-*-*-*-*-*-*-*-*-*-*-*"),
398 if ( wxTestFontSpec(fontSpec
) )
400 xweight
= wxT("extrabold");
403 fontSpec
.Printf(wxT("-*-%s-demibold-*-*-*-*-*-*-*-*-*-*-*"),
405 if ( wxTestFontSpec(fontSpec
) )
407 xweight
= wxT("demibold");
410 fontSpec
.Printf(wxT("-*-%s-black-*-*-*-*-*-*-*-*-*-*-*"),
412 if ( wxTestFontSpec(fontSpec
) )
414 xweight
= wxT("black");
417 fontSpec
.Printf(wxT("-*-%s-ultrablack-*-*-*-*-*-*-*-*-*-*-*"),
419 if ( wxTestFontSpec(fontSpec
) )
421 xweight
= wxT("ultrablack");
428 fontSpec
.Printf(wxT("-*-%s-light-*-*-*-*-*-*-*-*-*-*-*"),
430 if ( wxTestFontSpec(fontSpec
) )
432 xweight
= wxT("light");
435 fontSpec
.Printf(wxT("-*-%s-thin-*-*-*-*-*-*-*-*-*-*-*"),
437 if ( wxTestFontSpec(fontSpec
) )
439 xweight
= wxT("thin");
446 fontSpec
.Printf(wxT("-*-%s-medium-*-*-*-*-*-*-*-*-*-*-*"),
448 if ( wxTestFontSpec(fontSpec
) )
450 xweight
= wxT("medium");
453 fontSpec
.Printf(wxT("-*-%s-normal-*-*-*-*-*-*-*-*-*-*-*"),
455 if ( wxTestFontSpec(fontSpec
) )
457 xweight
= wxT("normal");
460 fontSpec
.Printf(wxT("-*-%s-regular-*-*-*-*-*-*-*-*-*-*-*"),
462 if ( wxTestFontSpec(fontSpec
) )
464 xweight
= wxT("regular");
470 default: xweight
= wxT("*"); break;
473 // construct the X font spec from our data
474 fontSpec
.Printf(wxT("-*-%s-%s-%s-normal-*-*-%d-*-*-*-*-%s-%s"),
475 xfamily
.c_str(), xweight
.c_str(), xstyle
.c_str(),
476 pointSize
, xregistry
.c_str(), xencoding
.c_str());
478 return wxLoadFont(fontSpec
);