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
209 info
->encoding
= encoding
;
214 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
217 fontspec
.Printf(_T("-*-%s-*-*-*-*-*-*-*-*-*-*-%s-%s"),
218 !info
.facename
? _T("*") : info
.facename
.c_str(),
219 info
.xregistry
.c_str(),
220 info
.xencoding
.c_str());
222 return wxTestFontSpec(fontspec
);
225 // ----------------------------------------------------------------------------
226 // X-specific functions
227 // ----------------------------------------------------------------------------
229 wxNativeFont
wxLoadQueryNearestFont(int pointSize
,
234 const wxString
&facename
,
235 wxFontEncoding encoding
)
237 if ( encoding
== wxFONTENCODING_DEFAULT
)
239 encoding
= wxFont::GetDefaultEncoding();
242 // first determine the encoding - if the font doesn't exist at all in this
243 // encoding, it's useless to do all other approximations (i.e. size,
244 // family &c don't matter much)
245 wxNativeEncodingInfo info
;
246 if ( encoding
== wxFONTENCODING_SYSTEM
)
248 // This will always work so we don't test to save time
249 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
253 if ( !wxGetNativeFontEncoding(encoding
, &info
) ||
254 !wxTestFontEncoding(info
) )
256 if ( !wxTheFontMapper
->GetAltForEncoding(encoding
, &info
) )
258 // unspported encoding - replace it with the default
260 // NB: we can't just return 0 from here because wxGTK code doesn't
261 // check for it (i.e. it supposes that we'll always succeed),
262 // so it would provoke a crash
263 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
268 // OK, we have the correct xregistry/xencoding in info structure
269 wxNativeFont font
= wxLoadQueryFont( pointSize
, family
, style
, weight
,
270 underlined
, facename
,
271 info
.xregistry
, info
.xencoding
);
275 // search up and down by stepsize 10
276 int max_size
= pointSize
+ 20 * (1 + (pointSize
/180));
277 int min_size
= pointSize
- 20 * (1 + (pointSize
/180));
281 // Search for smaller size (approx.)
282 for ( i
= pointSize
- 10; !font
&& i
>= 10 && i
>= min_size
; i
-= 10 )
284 font
= wxLoadQueryFont(i
, family
, style
, weight
, underlined
,
285 facename
, info
.xregistry
, info
.xencoding
);
288 // Search for larger size (approx.)
289 for ( i
= pointSize
+ 10; !font
&& i
<= max_size
; i
+= 10 )
291 font
= wxLoadQueryFont(i
, family
, style
, weight
, underlined
,
292 facename
, info
.xregistry
, info
.xencoding
);
295 // Try default family
296 if ( !font
&& family
!= wxDEFAULT
)
298 font
= wxLoadQueryFont(pointSize
, wxDEFAULT
, style
, weight
,
299 underlined
, facename
,
300 info
.xregistry
, info
.xencoding
);
306 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
307 underlined
, facename
,
308 info
.xregistry
, info
.xencoding
);
314 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
315 underlined
, wxEmptyString
,
316 info
.xregistry
, info
.xencoding
);
323 // ----------------------------------------------------------------------------
325 // ----------------------------------------------------------------------------
327 // returns TRUE if there are any fonts matching this font spec
328 static bool wxTestFontSpec(const wxString
& fontspec
)
330 // some X servers will fail to load this font because there are too many
331 // matches so we must test explicitly for this
332 if ( fontspec
== _T("-*-*-*-*-*-*-*-*-*-*-*-*-*-*") )
337 wxNativeFont test
= (wxNativeFont
) g_fontHash
->Get( fontspec
);
340 // printf( "speed up\n" );
344 test
= wxLoadFont(fontspec
);
345 g_fontHash
->Put( fontspec
, (wxObject
*) test
);
359 static wxNativeFont
wxLoadQueryFont(int pointSize
,
363 bool WXUNUSED(underlined
),
364 const wxString
& facename
,
365 const wxString
& xregistry
,
366 const wxString
& xencoding
)
371 case wxDECORATIVE
: xfamily
= wxT("lucida"); break;
372 case wxROMAN
: xfamily
= wxT("times"); break;
373 case wxMODERN
: xfamily
= wxT("courier"); break;
374 case wxSWISS
: xfamily
= wxT("helvetica"); break;
375 case wxTELETYPE
: xfamily
= wxT("lucidatypewriter"); break;
376 case wxSCRIPT
: xfamily
= wxT("utopia"); break;
377 default: xfamily
= wxT("*");
381 if (!facename
.IsEmpty())
383 fontSpec
.Printf(wxT("-*-%s-*-*-normal-*-*-*-*-*-*-*-*-*"),
386 if ( wxTestFontSpec(fontSpec
) )
390 //else: no such family, use default one instead
397 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
399 if ( wxTestFontSpec(fontSpec
) )
404 // fall through - try wxITALIC now
407 fontSpec
.Printf(wxT("-*-%s-*-i-*-*-*-*-*-*-*-*-*-*"),
409 if ( wxTestFontSpec(fontSpec
) )
413 else if ( style
== wxITALIC
) // and not wxSLANT
416 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
418 if ( wxTestFontSpec(fontSpec
) )
424 // no italic, no slant - leave default
431 wxFAIL_MSG(_T("unknown font style"));
432 // fall back to normal
444 fontSpec
.Printf(wxT("-*-%s-bold-*-*-*-*-*-*-*-*-*-*-*"),
446 if ( wxTestFontSpec(fontSpec
) )
448 xweight
= wxT("bold");
451 fontSpec
.Printf(wxT("-*-%s-heavy-*-*-*-*-*-*-*-*-*-*-*"),
453 if ( wxTestFontSpec(fontSpec
) )
455 xweight
= wxT("heavy");
458 fontSpec
.Printf(wxT("-*-%s-extrabold-*-*-*-*-*-*-*-*-*-*-*"),
460 if ( wxTestFontSpec(fontSpec
) )
462 xweight
= wxT("extrabold");
465 fontSpec
.Printf(wxT("-*-%s-demibold-*-*-*-*-*-*-*-*-*-*-*"),
467 if ( wxTestFontSpec(fontSpec
) )
469 xweight
= wxT("demibold");
472 fontSpec
.Printf(wxT("-*-%s-black-*-*-*-*-*-*-*-*-*-*-*"),
474 if ( wxTestFontSpec(fontSpec
) )
476 xweight
= wxT("black");
479 fontSpec
.Printf(wxT("-*-%s-ultrablack-*-*-*-*-*-*-*-*-*-*-*"),
481 if ( wxTestFontSpec(fontSpec
) )
483 xweight
= wxT("ultrablack");
490 fontSpec
.Printf(wxT("-*-%s-light-*-*-*-*-*-*-*-*-*-*-*"),
492 if ( wxTestFontSpec(fontSpec
) )
494 xweight
= wxT("light");
497 fontSpec
.Printf(wxT("-*-%s-thin-*-*-*-*-*-*-*-*-*-*-*"),
499 if ( wxTestFontSpec(fontSpec
) )
501 xweight
= wxT("thin");
508 fontSpec
.Printf(wxT("-*-%s-medium-*-*-*-*-*-*-*-*-*-*-*"),
510 if ( wxTestFontSpec(fontSpec
) )
512 xweight
= wxT("medium");
515 fontSpec
.Printf(wxT("-*-%s-normal-*-*-*-*-*-*-*-*-*-*-*"),
517 if ( wxTestFontSpec(fontSpec
) )
519 xweight
= wxT("normal");
522 fontSpec
.Printf(wxT("-*-%s-regular-*-*-*-*-*-*-*-*-*-*-*"),
524 if ( wxTestFontSpec(fontSpec
) )
526 xweight
= wxT("regular");
532 default: xweight
= wxT("*"); break;
535 // construct the X font spec from our data
536 fontSpec
.Printf(wxT("-*-%s-%s-%s-normal-*-*-%d-*-*-*-*-%s-%s"),
537 xfamily
.c_str(), xweight
.c_str(), xstyle
.c_str(),
538 pointSize
, xregistry
.c_str(), xencoding
.c_str());
540 return wxLoadFont(fontSpec
);
543 // ----------------------------------------------------------------------------
545 // ----------------------------------------------------------------------------
547 class wxFontModule
: public wxModule
554 DECLARE_DYNAMIC_CLASS(wxFontModule
)
557 IMPLEMENT_DYNAMIC_CLASS(wxFontModule
, wxModule
)
559 bool wxFontModule::OnInit()
561 g_fontHash
= new wxHashTable( wxKEY_STRING
);
566 void wxFontModule::OnExit()
570 g_fontHash
= (wxHashTable
*)NULL
;