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 ( !wxGetNativeFontEncoding(encoding
, &info
) ||
226 !wxTestFontEncoding(info
) )
228 if ( !wxTheFontMapper
->GetAltForEncoding(encoding
, &info
) )
230 // unspported encoding - replace it with the default
232 // NB: we can't just return 0 from here because wxGTK code doesn't
233 // check for it (i.e. it supposes that we'll always succeed),
234 // so it would provoke a crash
235 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
238 //else: we have the correct xregistry/xencoding in info structure
240 wxNativeFont font
= wxLoadQueryFont( pointSize
, family
, style
, weight
,
241 underlined
, facename
,
242 info
.xregistry
, info
.xencoding
);
246 // search up and down by stepsize 10
247 int max_size
= pointSize
+ 20 * (1 + (pointSize
/180));
248 int min_size
= pointSize
- 20 * (1 + (pointSize
/180));
252 // Search for smaller size (approx.)
253 for ( i
= pointSize
- 10; !font
&& i
>= 10 && i
>= min_size
; i
-= 10 )
255 font
= wxLoadQueryFont(i
, family
, style
, weight
, underlined
,
256 facename
, info
.xregistry
, info
.xencoding
);
259 // Search for larger size (approx.)
260 for ( i
= pointSize
+ 10; !font
&& i
<= max_size
; i
+= 10 )
262 font
= wxLoadQueryFont(i
, family
, style
, weight
, underlined
,
263 facename
, info
.xregistry
, info
.xencoding
);
266 // Try default family
267 if ( !font
&& family
!= wxDEFAULT
)
269 font
= wxLoadQueryFont(pointSize
, wxDEFAULT
, style
, weight
,
270 underlined
, facename
,
271 info
.xregistry
, info
.xencoding
);
277 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
278 underlined
, facename
,
279 info
.xregistry
, info
.xencoding
);
285 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
286 underlined
, wxEmptyString
,
287 info
.xregistry
, info
.xencoding
);
294 // ----------------------------------------------------------------------------
296 // ----------------------------------------------------------------------------
298 // returns TRUE if there are any fonts matching this font spec
299 static bool wxTestFontSpec(const wxString
& fontspec
)
301 wxNativeFont test
= wxLoadFont(fontspec
);
314 static wxNativeFont
wxLoadQueryFont(int pointSize
,
318 bool WXUNUSED(underlined
),
319 const wxString
& facename
,
320 const wxString
& xregistry
,
321 const wxString
& xencoding
)
326 case wxDECORATIVE
: xfamily
= wxT("lucida"); break;
327 case wxROMAN
: xfamily
= wxT("times"); break;
328 case wxMODERN
: xfamily
= wxT("courier"); break;
329 case wxSWISS
: xfamily
= wxT("helvetica"); break;
330 case wxTELETYPE
: xfamily
= wxT("lucidatypewriter"); break;
331 case wxSCRIPT
: xfamily
= wxT("utopia"); break;
332 default: xfamily
= wxT("*");
336 if (!facename
.IsEmpty())
338 fontSpec
.Printf(wxT("-*-%s-*-*-normal-*-*-*-*-*-*-*-*-*"),
341 if ( wxTestFontSpec(fontSpec
) )
345 //else: no such family, use default one instead
351 case wxITALIC
: xstyle
= wxT("i"); break;
352 case wxSLANT
: xstyle
= wxT("o"); break;
353 case wxNORMAL
: xstyle
= wxT("r"); break;
354 default: xstyle
= wxT("*"); break;
362 fontSpec
.Printf(wxT("-*-%s-bold-*-*-*-*-*-*-*-*-*-*-*"),
364 if ( wxTestFontSpec(fontSpec
) )
366 xweight
= wxT("bold");
369 fontSpec
.Printf(wxT("-*-%s-heavy-*-*-*-*-*-*-*-*-*-*-*"),
371 if ( wxTestFontSpec(fontSpec
) )
373 xweight
= wxT("heavy");
376 fontSpec
.Printf(wxT("-*-%s-extrabold-*-*-*-*-*-*-*-*-*-*-*"),
378 if ( wxTestFontSpec(fontSpec
) )
380 xweight
= wxT("extrabold");
383 fontSpec
.Printf(wxT("-*-%s-demibold-*-*-*-*-*-*-*-*-*-*-*"),
385 if ( wxTestFontSpec(fontSpec
) )
387 xweight
= wxT("demibold");
390 fontSpec
.Printf(wxT("-*-%s-black-*-*-*-*-*-*-*-*-*-*-*"),
392 if ( wxTestFontSpec(fontSpec
) )
394 xweight
= wxT("black");
397 fontSpec
.Printf(wxT("-*-%s-ultrablack-*-*-*-*-*-*-*-*-*-*-*"),
399 if ( wxTestFontSpec(fontSpec
) )
401 xweight
= wxT("ultrablack");
408 fontSpec
.Printf(wxT("-*-%s-light-*-*-*-*-*-*-*-*-*-*-*"),
410 if ( wxTestFontSpec(fontSpec
) )
412 xweight
= wxT("light");
415 fontSpec
.Printf(wxT("-*-%s-thin-*-*-*-*-*-*-*-*-*-*-*"),
417 if ( wxTestFontSpec(fontSpec
) )
419 xweight
= wxT("thin");
426 fontSpec
.Printf(wxT("-*-%s-medium-*-*-*-*-*-*-*-*-*-*-*"),
428 if ( wxTestFontSpec(fontSpec
) )
430 xweight
= wxT("medium");
433 fontSpec
.Printf(wxT("-*-%s-normal-*-*-*-*-*-*-*-*-*-*-*"),
435 if ( wxTestFontSpec(fontSpec
) )
437 xweight
= wxT("normal");
440 fontSpec
.Printf(wxT("-*-%s-regular-*-*-*-*-*-*-*-*-*-*-*"),
442 if ( wxTestFontSpec(fontSpec
) )
444 xweight
= wxT("regular");
450 default: xweight
= wxT("*"); break;
453 // construct the X font spec from our data
454 fontSpec
.Printf(wxT("-*-%s-%s-%s-normal-*-*-%d-*-*-*-*-%s-%s"),
455 xfamily
.c_str(), xweight
.c_str(), xstyle
.c_str(),
456 pointSize
, xregistry
.c_str(), xencoding
.c_str());
458 return wxLoadFont(fontSpec
);