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"
36 #pragma message disable nosimpint
40 #pragma message enable nosimpint
43 #include "wx/utils.h" // for wxGetDisplay()
44 #elif defined(__WXGTK__)
48 #include "wx/fontutil.h"
49 #include "wx/fontmap.h"
50 #include "wx/tokenzr.h"
52 #include "wx/module.h"
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 static wxHashTable
*g_fontHash
= (wxHashTable
*) NULL
;
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
64 // define the functions to create and destroy native fonts for this toolkit
66 static inline wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
68 return XLoadQueryFont((Display
*)wxGetDisplay(), fontSpec
);
71 static inline void wxFreeFont(wxNativeFont font
)
73 XFreeFont((Display
*)wxGetDisplay(), (XFontStruct
*)font
);
75 #elif defined(__WXGTK__)
76 static inline wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
78 return gdk_font_load( wxConvertWX2MB(fontSpec
) );
81 static inline void wxFreeFont(wxNativeFont font
)
86 #error "Unknown GUI toolkit"
89 static bool wxTestFontSpec(const wxString
& fontspec
);
91 static wxNativeFont
wxLoadQueryFont(int pointSize
,
96 const wxString
& facename
,
97 const wxString
& xregistry
,
98 const wxString
& xencoding
);
100 // ============================================================================
102 // ============================================================================
104 // ----------------------------------------------------------------------------
105 // wxNativeEncodingInfo
106 // ----------------------------------------------------------------------------
108 // convert to/from the string representation: format is
109 // registry-encoding[-facename]
110 bool wxNativeEncodingInfo::FromString(const wxString
& s
)
112 wxStringTokenizer
tokenizer(s
, _T("-"));
114 xregistry
= tokenizer
.GetNextToken();
118 xencoding
= tokenizer
.GetNextToken();
123 facename
= tokenizer
.GetNextToken();
128 wxString
wxNativeEncodingInfo::ToString() const
131 s
<< xregistry
<< _T('-') << xencoding
;
134 s
<< _T('-') << facename
;
140 // ----------------------------------------------------------------------------
142 // ----------------------------------------------------------------------------
144 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
145 wxNativeEncodingInfo
*info
)
147 wxCHECK_MSG( info
, FALSE
, _T("bad pointer in wxGetNativeFontEncoding") );
149 if ( encoding
== wxFONTENCODING_DEFAULT
)
151 encoding
= wxFont::GetDefaultEncoding();
156 case wxFONTENCODING_ISO8859_1
:
157 case wxFONTENCODING_ISO8859_2
:
158 case wxFONTENCODING_ISO8859_3
:
159 case wxFONTENCODING_ISO8859_4
:
160 case wxFONTENCODING_ISO8859_5
:
161 case wxFONTENCODING_ISO8859_6
:
162 case wxFONTENCODING_ISO8859_7
:
163 case wxFONTENCODING_ISO8859_8
:
164 case wxFONTENCODING_ISO8859_9
:
165 case wxFONTENCODING_ISO8859_10
:
166 case wxFONTENCODING_ISO8859_11
:
167 case wxFONTENCODING_ISO8859_13
:
168 case wxFONTENCODING_ISO8859_14
:
169 case wxFONTENCODING_ISO8859_15
:
171 int cp
= encoding
- wxFONTENCODING_ISO8859_1
+ 1;
172 info
->xregistry
= wxT("iso8859");
173 info
->xencoding
.Printf(wxT("%d"), cp
);
177 case wxFONTENCODING_KOI8
:
178 info
->xregistry
= wxT("koi8");
180 // we don't make distinction between koi8-r and koi8-u (so far)
181 info
->xencoding
= wxT("*");
184 case wxFONTENCODING_CP1250
:
185 case wxFONTENCODING_CP1251
:
186 case wxFONTENCODING_CP1252
:
187 case wxFONTENCODING_CP1253
:
188 case wxFONTENCODING_CP1254
:
189 case wxFONTENCODING_CP1255
:
190 case wxFONTENCODING_CP1256
:
191 case wxFONTENCODING_CP1257
:
193 int cp
= encoding
- wxFONTENCODING_CP1250
+ 1250;
194 info
->xregistry
= wxT("microsoft");
195 info
->xencoding
.Printf(wxT("cp%d"), cp
);
199 case wxFONTENCODING_SYSTEM
:
201 info
->xencoding
= wxT("*");
205 // don't know how to translate this encoding into X fontspec
212 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
215 fontspec
.Printf(_T("-*-%s-*-*-*-*-*-*-*-*-*-*-%s-%s"),
216 !info
.facename
? _T("*") : info
.facename
.c_str(),
217 info
.xregistry
.c_str(),
218 info
.xencoding
.c_str());
220 return wxTestFontSpec(fontspec
);
223 // ----------------------------------------------------------------------------
224 // X-specific functions
225 // ----------------------------------------------------------------------------
227 wxNativeFont
wxLoadQueryNearestFont(int pointSize
,
232 const wxString
&facename
,
233 wxFontEncoding encoding
)
235 if ( encoding
== wxFONTENCODING_DEFAULT
)
237 encoding
= wxFont::GetDefaultEncoding();
240 // first determine the encoding - if the font doesn't exist at all in this
241 // encoding, it's useless to do all other approximations (i.e. size,
242 // family &c don't matter much)
243 wxNativeEncodingInfo info
;
244 if ( encoding
== wxFONTENCODING_SYSTEM
)
246 // This will always work so we don't test to save time
247 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
251 if ( !wxGetNativeFontEncoding(encoding
, &info
) ||
252 !wxTestFontEncoding(info
) )
254 if ( !wxTheFontMapper
->GetAltForEncoding(encoding
, &info
) )
256 // unspported encoding - replace it with the default
258 // NB: we can't just return 0 from here because wxGTK code doesn't
259 // check for it (i.e. it supposes that we'll always succeed),
260 // so it would provoke a crash
261 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
266 // OK, we have the correct xregistry/xencoding in info structure
267 wxNativeFont font
= wxLoadQueryFont( pointSize
, family
, style
, weight
,
268 underlined
, facename
,
269 info
.xregistry
, info
.xencoding
);
273 // search up and down by stepsize 10
274 int max_size
= pointSize
+ 20 * (1 + (pointSize
/180));
275 int min_size
= pointSize
- 20 * (1 + (pointSize
/180));
279 // Search for smaller size (approx.)
280 for ( i
= pointSize
- 10; !font
&& i
>= 10 && i
>= min_size
; i
-= 10 )
282 font
= wxLoadQueryFont(i
, family
, style
, weight
, underlined
,
283 facename
, info
.xregistry
, info
.xencoding
);
286 // Search for larger size (approx.)
287 for ( i
= pointSize
+ 10; !font
&& i
<= max_size
; i
+= 10 )
289 font
= wxLoadQueryFont(i
, family
, style
, weight
, underlined
,
290 facename
, info
.xregistry
, info
.xencoding
);
293 // Try default family
294 if ( !font
&& family
!= wxDEFAULT
)
296 font
= wxLoadQueryFont(pointSize
, wxDEFAULT
, style
, weight
,
297 underlined
, facename
,
298 info
.xregistry
, info
.xencoding
);
304 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
305 underlined
, facename
,
306 info
.xregistry
, info
.xencoding
);
312 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
313 underlined
, wxEmptyString
,
314 info
.xregistry
, info
.xencoding
);
321 // ----------------------------------------------------------------------------
323 // ----------------------------------------------------------------------------
325 // returns TRUE if there are any fonts matching this font spec
326 static bool wxTestFontSpec(const wxString
& fontspec
)
328 // some X servers will fail to load this font because there are too many
329 // matches so we must test explicitly for this
330 if ( fontspec
== _T("-*-*-*-*-*-*-*-*-*-*-*-*-*-*") )
335 wxNativeFont test
= (wxNativeFont
) g_fontHash
->Get( fontspec
);
338 // printf( "speed up\n" );
342 test
= wxLoadFont(fontspec
);
343 g_fontHash
->Put( fontspec
, (wxObject
*) test
);
357 static wxNativeFont
wxLoadQueryFont(int pointSize
,
361 bool WXUNUSED(underlined
),
362 const wxString
& facename
,
363 const wxString
& xregistry
,
364 const wxString
& xencoding
)
369 case wxDECORATIVE
: xfamily
= wxT("lucida"); break;
370 case wxROMAN
: xfamily
= wxT("times"); break;
371 case wxMODERN
: xfamily
= wxT("courier"); break;
372 case wxSWISS
: xfamily
= wxT("helvetica"); break;
373 case wxTELETYPE
: xfamily
= wxT("lucidatypewriter"); break;
374 case wxSCRIPT
: xfamily
= wxT("utopia"); break;
375 default: xfamily
= wxT("*");
379 if (!facename
.IsEmpty())
381 fontSpec
.Printf(wxT("-*-%s-*-*-normal-*-*-*-*-*-*-*-*-*"),
384 if ( wxTestFontSpec(fontSpec
) )
388 //else: no such family, use default one instead
394 case wxITALIC
: xstyle
= wxT("i"); break;
395 case wxSLANT
: xstyle
= wxT("o"); break;
396 case wxNORMAL
: xstyle
= wxT("r"); break;
397 default: xstyle
= wxT("*"); break;
405 fontSpec
.Printf(wxT("-*-%s-bold-*-*-*-*-*-*-*-*-*-*-*"),
407 if ( wxTestFontSpec(fontSpec
) )
409 xweight
= wxT("bold");
412 fontSpec
.Printf(wxT("-*-%s-heavy-*-*-*-*-*-*-*-*-*-*-*"),
414 if ( wxTestFontSpec(fontSpec
) )
416 xweight
= wxT("heavy");
419 fontSpec
.Printf(wxT("-*-%s-extrabold-*-*-*-*-*-*-*-*-*-*-*"),
421 if ( wxTestFontSpec(fontSpec
) )
423 xweight
= wxT("extrabold");
426 fontSpec
.Printf(wxT("-*-%s-demibold-*-*-*-*-*-*-*-*-*-*-*"),
428 if ( wxTestFontSpec(fontSpec
) )
430 xweight
= wxT("demibold");
433 fontSpec
.Printf(wxT("-*-%s-black-*-*-*-*-*-*-*-*-*-*-*"),
435 if ( wxTestFontSpec(fontSpec
) )
437 xweight
= wxT("black");
440 fontSpec
.Printf(wxT("-*-%s-ultrablack-*-*-*-*-*-*-*-*-*-*-*"),
442 if ( wxTestFontSpec(fontSpec
) )
444 xweight
= wxT("ultrablack");
451 fontSpec
.Printf(wxT("-*-%s-light-*-*-*-*-*-*-*-*-*-*-*"),
453 if ( wxTestFontSpec(fontSpec
) )
455 xweight
= wxT("light");
458 fontSpec
.Printf(wxT("-*-%s-thin-*-*-*-*-*-*-*-*-*-*-*"),
460 if ( wxTestFontSpec(fontSpec
) )
462 xweight
= wxT("thin");
469 fontSpec
.Printf(wxT("-*-%s-medium-*-*-*-*-*-*-*-*-*-*-*"),
471 if ( wxTestFontSpec(fontSpec
) )
473 xweight
= wxT("medium");
476 fontSpec
.Printf(wxT("-*-%s-normal-*-*-*-*-*-*-*-*-*-*-*"),
478 if ( wxTestFontSpec(fontSpec
) )
480 xweight
= wxT("normal");
483 fontSpec
.Printf(wxT("-*-%s-regular-*-*-*-*-*-*-*-*-*-*-*"),
485 if ( wxTestFontSpec(fontSpec
) )
487 xweight
= wxT("regular");
493 default: xweight
= wxT("*"); break;
496 // construct the X font spec from our data
497 fontSpec
.Printf(wxT("-*-%s-%s-%s-normal-*-*-%d-*-*-*-*-%s-%s"),
498 xfamily
.c_str(), xweight
.c_str(), xstyle
.c_str(),
499 pointSize
, xregistry
.c_str(), xencoding
.c_str());
501 return wxLoadFont(fontSpec
);
504 // ----------------------------------------------------------------------------
506 // ----------------------------------------------------------------------------
508 class wxFontModule
: public wxModule
515 DECLARE_DYNAMIC_CLASS(wxFontModule
)
518 IMPLEMENT_DYNAMIC_CLASS(wxFontModule
, wxModule
)
520 bool wxFontModule::OnInit()
522 g_fontHash
= new wxHashTable( wxKEY_STRING
);
527 void wxFontModule::OnExit()
531 g_fontHash
= (wxHashTable
*)NULL
;