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 wxNativeFont font
= gdk_font_load( wxConvertWX2MB(fontSpec
) );
65 if(fontSpec
== "-*-*-*-*-*-*-*-*-*-*-*-*-*-*")
68 font
= gdk_font_load (wxConvertWX2MB("-*-*-*-*-*-*-*-*-75-*-*-*-*-*"));
70 font
= gdk_font_load (wxConvertWX2MB("-*-*-*-*-*-*-*-*-100-*-*-*-*-*"));
72 font
= gdk_font_load (wxConvertWX2MB("-*-fixed-*-*-*-*-*-*-*-*-*-*-*-*"));
77 static inline void wxFreeFont(wxNativeFont font
)
82 #error "Unknown GUI toolkit"
85 static bool wxTestFontSpec(const wxString
& fontspec
);
87 static wxNativeFont
wxLoadQueryFont(int pointSize
,
92 const wxString
& facename
,
93 const wxString
& xregistry
,
94 const wxString
& xencoding
);
96 // ============================================================================
98 // ============================================================================
100 // ----------------------------------------------------------------------------
101 // wxNativeEncodingInfo
102 // ----------------------------------------------------------------------------
104 // convert to/from the string representation: format is
105 // registry-encoding[-facename]
106 bool wxNativeEncodingInfo::FromString(const wxString
& s
)
108 wxStringTokenizer
tokenizer(s
, _T("-"));
110 xregistry
= tokenizer
.GetNextToken();
114 xencoding
= tokenizer
.GetNextToken();
119 facename
= tokenizer
.GetNextToken();
124 wxString
wxNativeEncodingInfo::ToString() const
127 s
<< xregistry
<< _T('-') << xencoding
;
130 s
<< _T('-') << facename
;
136 // ----------------------------------------------------------------------------
138 // ----------------------------------------------------------------------------
140 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
141 wxNativeEncodingInfo
*info
)
143 wxCHECK_MSG( info
, FALSE
, _T("bad pointer in wxGetNativeFontEncoding") );
145 if ( encoding
== wxFONTENCODING_DEFAULT
)
147 encoding
= wxFont::GetDefaultEncoding();
152 case wxFONTENCODING_ISO8859_1
:
153 case wxFONTENCODING_ISO8859_2
:
154 case wxFONTENCODING_ISO8859_3
:
155 case wxFONTENCODING_ISO8859_4
:
156 case wxFONTENCODING_ISO8859_5
:
157 case wxFONTENCODING_ISO8859_6
:
158 case wxFONTENCODING_ISO8859_7
:
159 case wxFONTENCODING_ISO8859_8
:
160 case wxFONTENCODING_ISO8859_9
:
161 case wxFONTENCODING_ISO8859_10
:
162 case wxFONTENCODING_ISO8859_11
:
163 case wxFONTENCODING_ISO8859_13
:
164 case wxFONTENCODING_ISO8859_14
:
165 case wxFONTENCODING_ISO8859_15
:
167 int cp
= encoding
- wxFONTENCODING_ISO8859_1
+ 1;
168 info
->xregistry
= wxT("iso8859");
169 info
->xencoding
.Printf(wxT("%d"), cp
);
173 case wxFONTENCODING_KOI8
:
174 info
->xregistry
= wxT("koi8");
176 // we don't make distinction between koi8-r and koi8-u (so far)
177 info
->xencoding
= wxT("*");
180 case wxFONTENCODING_CP1250
:
181 case wxFONTENCODING_CP1251
:
182 case wxFONTENCODING_CP1252
:
183 case wxFONTENCODING_CP1253
:
184 case wxFONTENCODING_CP1254
:
185 case wxFONTENCODING_CP1255
:
186 case wxFONTENCODING_CP1256
:
187 case wxFONTENCODING_CP1257
:
189 int cp
= encoding
- wxFONTENCODING_CP1250
+ 1250;
190 info
->xregistry
= wxT("microsoft");
191 info
->xencoding
.Printf(wxT("cp%d"), cp
);
195 case wxFONTENCODING_SYSTEM
:
197 info
->xencoding
= wxT("*");
201 // don't know how to translate this encoding into X fontspec
208 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
211 fontspec
.Printf(_T("-*-%s-*-*-*-*-*-*-*-*-*-*-%s-%s"),
212 !info
.facename
? _T("*") : info
.facename
.c_str(),
213 info
.xregistry
.c_str(),
214 info
.xencoding
.c_str());
216 return wxTestFontSpec(fontspec
);
219 // ----------------------------------------------------------------------------
220 // X-specific functions
221 // ----------------------------------------------------------------------------
223 wxNativeFont
wxLoadQueryNearestFont(int pointSize
,
228 const wxString
&facename
,
229 wxFontEncoding encoding
)
231 // first determine the encoding - if the font doesn't exist at all in this
232 // encoding, it's useless to do all other approximations (i.e. size,
233 // family &c don't matter much)
234 wxNativeEncodingInfo info
;
235 if (encoding
== wxFONTENCODING_SYSTEM
)
237 // This will always work so we don't test to save time
238 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
242 if ( !wxGetNativeFontEncoding(encoding
, &info
) ||
243 !wxTestFontEncoding(info
) )
245 if ( !wxTheFontMapper
->GetAltForEncoding(encoding
, &info
) )
247 // unspported encoding - replace it with the default
249 // NB: we can't just return 0 from here because wxGTK code doesn't
250 // check for it (i.e. it supposes that we'll always succeed),
251 // so it would provoke a crash
252 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
257 // OK, we have the correct xregistry/xencoding in info structure
259 wxNativeFont font
= wxLoadQueryFont( pointSize
, family
, style
, weight
,
260 underlined
, facename
,
261 info
.xregistry
, info
.xencoding
);
265 // search up and down by stepsize 10
266 int max_size
= pointSize
+ 20 * (1 + (pointSize
/180));
267 int min_size
= pointSize
- 20 * (1 + (pointSize
/180));
271 // Search for smaller size (approx.)
272 for ( i
= pointSize
- 10; !font
&& i
>= 10 && i
>= min_size
; i
-= 10 )
274 font
= wxLoadQueryFont(i
, family
, style
, weight
, underlined
,
275 facename
, info
.xregistry
, info
.xencoding
);
278 // Search for larger size (approx.)
279 for ( i
= pointSize
+ 10; !font
&& i
<= max_size
; i
+= 10 )
281 font
= wxLoadQueryFont(i
, family
, style
, weight
, underlined
,
282 facename
, info
.xregistry
, info
.xencoding
);
285 // Try default family
286 if ( !font
&& family
!= wxDEFAULT
)
288 font
= wxLoadQueryFont(pointSize
, wxDEFAULT
, style
, weight
,
289 underlined
, facename
,
290 info
.xregistry
, info
.xencoding
);
296 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
297 underlined
, facename
,
298 info
.xregistry
, info
.xencoding
);
304 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
305 underlined
, wxEmptyString
,
306 info
.xregistry
, info
.xencoding
);
313 // ----------------------------------------------------------------------------
315 // ----------------------------------------------------------------------------
317 // returns TRUE if there are any fonts matching this font spec
318 static bool wxTestFontSpec(const wxString
& fontspec
)
320 wxNativeFont test
= wxLoadFont(fontspec
);
333 static wxNativeFont
wxLoadQueryFont(int pointSize
,
337 bool WXUNUSED(underlined
),
338 const wxString
& facename
,
339 const wxString
& xregistry
,
340 const wxString
& xencoding
)
345 case wxDECORATIVE
: xfamily
= wxT("lucida"); break;
346 case wxROMAN
: xfamily
= wxT("times"); break;
347 case wxMODERN
: xfamily
= wxT("courier"); break;
348 case wxSWISS
: xfamily
= wxT("helvetica"); break;
349 case wxTELETYPE
: xfamily
= wxT("lucidatypewriter"); break;
350 case wxSCRIPT
: xfamily
= wxT("utopia"); break;
351 default: xfamily
= wxT("*");
355 if (!facename
.IsEmpty())
357 fontSpec
.Printf(wxT("-*-%s-*-*-normal-*-*-*-*-*-*-*-*-*"),
360 if ( wxTestFontSpec(fontSpec
) )
364 //else: no such family, use default one instead
370 case wxITALIC
: xstyle
= wxT("i"); break;
371 case wxSLANT
: xstyle
= wxT("o"); break;
372 case wxNORMAL
: xstyle
= wxT("r"); break;
373 default: xstyle
= wxT("*"); break;
381 fontSpec
.Printf(wxT("-*-%s-bold-*-*-*-*-*-*-*-*-*-*-*"),
383 if ( wxTestFontSpec(fontSpec
) )
385 xweight
= wxT("bold");
388 fontSpec
.Printf(wxT("-*-%s-heavy-*-*-*-*-*-*-*-*-*-*-*"),
390 if ( wxTestFontSpec(fontSpec
) )
392 xweight
= wxT("heavy");
395 fontSpec
.Printf(wxT("-*-%s-extrabold-*-*-*-*-*-*-*-*-*-*-*"),
397 if ( wxTestFontSpec(fontSpec
) )
399 xweight
= wxT("extrabold");
402 fontSpec
.Printf(wxT("-*-%s-demibold-*-*-*-*-*-*-*-*-*-*-*"),
404 if ( wxTestFontSpec(fontSpec
) )
406 xweight
= wxT("demibold");
409 fontSpec
.Printf(wxT("-*-%s-black-*-*-*-*-*-*-*-*-*-*-*"),
411 if ( wxTestFontSpec(fontSpec
) )
413 xweight
= wxT("black");
416 fontSpec
.Printf(wxT("-*-%s-ultrablack-*-*-*-*-*-*-*-*-*-*-*"),
418 if ( wxTestFontSpec(fontSpec
) )
420 xweight
= wxT("ultrablack");
427 fontSpec
.Printf(wxT("-*-%s-light-*-*-*-*-*-*-*-*-*-*-*"),
429 if ( wxTestFontSpec(fontSpec
) )
431 xweight
= wxT("light");
434 fontSpec
.Printf(wxT("-*-%s-thin-*-*-*-*-*-*-*-*-*-*-*"),
436 if ( wxTestFontSpec(fontSpec
) )
438 xweight
= wxT("thin");
445 fontSpec
.Printf(wxT("-*-%s-medium-*-*-*-*-*-*-*-*-*-*-*"),
447 if ( wxTestFontSpec(fontSpec
) )
449 xweight
= wxT("medium");
452 fontSpec
.Printf(wxT("-*-%s-normal-*-*-*-*-*-*-*-*-*-*-*"),
454 if ( wxTestFontSpec(fontSpec
) )
456 xweight
= wxT("normal");
459 fontSpec
.Printf(wxT("-*-%s-regular-*-*-*-*-*-*-*-*-*-*-*"),
461 if ( wxTestFontSpec(fontSpec
) )
463 xweight
= wxT("regular");
469 default: xweight
= wxT("*"); break;
472 // construct the X font spec from our data
473 fontSpec
.Printf(wxT("-*-%s-%s-%s-normal-*-*-%d-*-*-*-*-%s-%s"),
474 xfamily
.c_str(), xweight
.c_str(), xstyle
.c_str(),
475 pointSize
, xregistry
.c_str(), xencoding
.c_str());
477 return wxLoadFont(fontSpec
);