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
,
101 // ============================================================================
103 // ============================================================================
105 // ----------------------------------------------------------------------------
106 // wxNativeEncodingInfo
107 // ----------------------------------------------------------------------------
109 // convert to/from the string representation: format is
110 // encodingid;registry;encoding[;facename]
111 bool wxNativeEncodingInfo::FromString(const wxString
& s
)
113 wxStringTokenizer
tokenizer(s
, _T(";"));
114 // cannot use "-" because it may be part of encoding name
116 wxString encid
= tokenizer
.GetNextToken();
118 if ( !encid
.ToLong(&enc
) )
120 encoding
= (wxFontEncoding
)enc
;
122 xregistry
= tokenizer
.GetNextToken();
126 xencoding
= tokenizer
.GetNextToken();
131 facename
= tokenizer
.GetNextToken();
136 wxString
wxNativeEncodingInfo::ToString() const
139 s
<< (long)encoding
<< _T(';') << xregistry
<< _T(';') << xencoding
;
142 s
<< _T(';') << facename
;
148 // ----------------------------------------------------------------------------
150 // ----------------------------------------------------------------------------
152 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
153 wxNativeEncodingInfo
*info
)
155 wxCHECK_MSG( info
, FALSE
, _T("bad pointer in wxGetNativeFontEncoding") );
157 if ( encoding
== wxFONTENCODING_DEFAULT
)
159 encoding
= wxFont::GetDefaultEncoding();
164 case wxFONTENCODING_ISO8859_1
:
165 case wxFONTENCODING_ISO8859_2
:
166 case wxFONTENCODING_ISO8859_3
:
167 case wxFONTENCODING_ISO8859_4
:
168 case wxFONTENCODING_ISO8859_5
:
169 case wxFONTENCODING_ISO8859_6
:
170 case wxFONTENCODING_ISO8859_7
:
171 case wxFONTENCODING_ISO8859_8
:
172 case wxFONTENCODING_ISO8859_9
:
173 case wxFONTENCODING_ISO8859_10
:
174 case wxFONTENCODING_ISO8859_11
:
175 case wxFONTENCODING_ISO8859_12
:
176 case wxFONTENCODING_ISO8859_13
:
177 case wxFONTENCODING_ISO8859_14
:
178 case wxFONTENCODING_ISO8859_15
:
180 int cp
= encoding
- wxFONTENCODING_ISO8859_1
+ 1;
181 info
->xregistry
= wxT("iso8859");
182 info
->xencoding
.Printf(wxT("%d"), cp
);
186 case wxFONTENCODING_KOI8
:
187 info
->xregistry
= wxT("koi8");
189 // we don't make distinction between koi8-r and koi8-u (so far)
190 info
->xencoding
= wxT("*");
193 case wxFONTENCODING_CP1250
:
194 case wxFONTENCODING_CP1251
:
195 case wxFONTENCODING_CP1252
:
196 case wxFONTENCODING_CP1253
:
197 case wxFONTENCODING_CP1254
:
198 case wxFONTENCODING_CP1255
:
199 case wxFONTENCODING_CP1256
:
200 case wxFONTENCODING_CP1257
:
202 int cp
= encoding
- wxFONTENCODING_CP1250
+ 1250;
203 info
->xregistry
= wxT("microsoft");
204 info
->xencoding
.Printf(wxT("cp%d"), cp
);
208 case wxFONTENCODING_SYSTEM
:
210 info
->xencoding
= wxT("*");
214 // don't know how to translate this encoding into X fontspec
218 info
->encoding
= encoding
;
223 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
226 fontspec
.Printf(_T("-*-%s-*-*-*-*-*-*-*-*-*-*-%s-%s"),
227 !info
.facename
? _T("*") : info
.facename
.c_str(),
228 info
.xregistry
.c_str(),
229 info
.xencoding
.c_str());
231 return wxTestFontSpec(fontspec
);
234 // ----------------------------------------------------------------------------
235 // X-specific functions
236 // ----------------------------------------------------------------------------
238 wxNativeFont
wxLoadQueryNearestFont(int pointSize
,
243 const wxString
&facename
,
244 wxFontEncoding encoding
,
247 if ( encoding
== wxFONTENCODING_DEFAULT
)
249 encoding
= wxFont::GetDefaultEncoding();
252 // first determine the encoding - if the font doesn't exist at all in this
253 // encoding, it's useless to do all other approximations (i.e. size,
254 // family &c don't matter much)
255 wxNativeEncodingInfo info
;
256 if ( encoding
== wxFONTENCODING_SYSTEM
)
258 // This will always work so we don't test to save time
259 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
263 if ( !wxGetNativeFontEncoding(encoding
, &info
) ||
264 !wxTestFontEncoding(info
) )
266 if ( !wxTheFontMapper
->GetAltForEncoding(encoding
, &info
) )
268 // unspported encoding - replace it with the default
270 // NB: we can't just return 0 from here because wxGTK code doesn't
271 // check for it (i.e. it supposes that we'll always succeed),
272 // so it would provoke a crash
273 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
278 // OK, we have the correct xregistry/xencoding in info structure
279 wxNativeFont font
= 0;
281 // if we already have the X font name, try to use it
282 if( xFontName
&& !xFontName
->IsEmpty() )
283 font
= wxLoadFont(*xFontName
);
286 font
= wxLoadQueryFont( pointSize
, family
, style
, weight
,
287 underlined
, facename
,
288 info
.xregistry
, info
.xencoding
,
293 // search up and down by stepsize 10
294 int max_size
= pointSize
+ 20 * (1 + (pointSize
/180));
295 int min_size
= pointSize
- 20 * (1 + (pointSize
/180));
299 // Search for smaller size (approx.)
300 for ( i
= pointSize
- 10; !font
&& i
>= 10 && i
>= min_size
; i
-= 10 )
302 font
= wxLoadQueryFont(i
, family
, style
, weight
, underlined
,
303 facename
, info
.xregistry
, info
.xencoding
,
307 // Search for larger size (approx.)
308 for ( i
= pointSize
+ 10; !font
&& i
<= max_size
; i
+= 10 )
310 font
= wxLoadQueryFont(i
, family
, style
, weight
, underlined
,
311 facename
, info
.xregistry
, info
.xencoding
,
315 // Try default family
316 if ( !font
&& family
!= wxDEFAULT
)
318 font
= wxLoadQueryFont(pointSize
, wxDEFAULT
, style
, weight
,
319 underlined
, facename
,
320 info
.xregistry
, info
.xencoding
,
327 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
328 underlined
, facename
,
329 info
.xregistry
, info
.xencoding
,
336 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
337 underlined
, wxEmptyString
,
338 info
.xregistry
, info
.xencoding
,
346 // ----------------------------------------------------------------------------
348 // ----------------------------------------------------------------------------
350 // returns TRUE if there are any fonts matching this font spec
351 static bool wxTestFontSpec(const wxString
& fontspec
)
353 // some X servers will fail to load this font because there are too many
354 // matches so we must test explicitly for this
355 if ( fontspec
== _T("-*-*-*-*-*-*-*-*-*-*-*-*-*-*") )
360 wxNativeFont test
= (wxNativeFont
) g_fontHash
->Get( fontspec
);
363 // printf( "speed up\n" );
367 test
= wxLoadFont(fontspec
);
368 g_fontHash
->Put( fontspec
, (wxObject
*) test
);
382 static wxNativeFont
wxLoadQueryFont(int pointSize
,
386 bool WXUNUSED(underlined
),
387 const wxString
& facename
,
388 const wxString
& xregistry
,
389 const wxString
& xencoding
,
395 case wxDECORATIVE
: xfamily
= wxT("lucida"); break;
396 case wxROMAN
: xfamily
= wxT("times"); break;
397 case wxMODERN
: xfamily
= wxT("courier"); break;
398 case wxSWISS
: xfamily
= wxT("helvetica"); break;
399 case wxTELETYPE
: xfamily
= wxT("lucidatypewriter"); break;
400 case wxSCRIPT
: xfamily
= wxT("utopia"); break;
401 default: xfamily
= wxT("*");
405 if (!facename
.IsEmpty())
407 fontSpec
.Printf(wxT("-*-%s-*-*-normal-*-*-*-*-*-*-*-*-*"),
410 if ( wxTestFontSpec(fontSpec
) )
414 //else: no such family, use default one instead
421 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
423 if ( wxTestFontSpec(fontSpec
) )
428 // fall through - try wxITALIC now
431 fontSpec
.Printf(wxT("-*-%s-*-i-*-*-*-*-*-*-*-*-*-*"),
433 if ( wxTestFontSpec(fontSpec
) )
437 else if ( style
== wxITALIC
) // and not wxSLANT
440 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
442 if ( wxTestFontSpec(fontSpec
) )
448 // no italic, no slant - leave default
455 wxFAIL_MSG(_T("unknown font style"));
456 // fall back to normal
468 fontSpec
.Printf(wxT("-*-%s-bold-*-*-*-*-*-*-*-*-*-*-*"),
470 if ( wxTestFontSpec(fontSpec
) )
472 xweight
= wxT("bold");
475 fontSpec
.Printf(wxT("-*-%s-heavy-*-*-*-*-*-*-*-*-*-*-*"),
477 if ( wxTestFontSpec(fontSpec
) )
479 xweight
= wxT("heavy");
482 fontSpec
.Printf(wxT("-*-%s-extrabold-*-*-*-*-*-*-*-*-*-*-*"),
484 if ( wxTestFontSpec(fontSpec
) )
486 xweight
= wxT("extrabold");
489 fontSpec
.Printf(wxT("-*-%s-demibold-*-*-*-*-*-*-*-*-*-*-*"),
491 if ( wxTestFontSpec(fontSpec
) )
493 xweight
= wxT("demibold");
496 fontSpec
.Printf(wxT("-*-%s-black-*-*-*-*-*-*-*-*-*-*-*"),
498 if ( wxTestFontSpec(fontSpec
) )
500 xweight
= wxT("black");
503 fontSpec
.Printf(wxT("-*-%s-ultrablack-*-*-*-*-*-*-*-*-*-*-*"),
505 if ( wxTestFontSpec(fontSpec
) )
507 xweight
= wxT("ultrablack");
514 fontSpec
.Printf(wxT("-*-%s-light-*-*-*-*-*-*-*-*-*-*-*"),
516 if ( wxTestFontSpec(fontSpec
) )
518 xweight
= wxT("light");
521 fontSpec
.Printf(wxT("-*-%s-thin-*-*-*-*-*-*-*-*-*-*-*"),
523 if ( wxTestFontSpec(fontSpec
) )
525 xweight
= wxT("thin");
532 fontSpec
.Printf(wxT("-*-%s-medium-*-*-*-*-*-*-*-*-*-*-*"),
534 if ( wxTestFontSpec(fontSpec
) )
536 xweight
= wxT("medium");
539 fontSpec
.Printf(wxT("-*-%s-normal-*-*-*-*-*-*-*-*-*-*-*"),
541 if ( wxTestFontSpec(fontSpec
) )
543 xweight
= wxT("normal");
546 fontSpec
.Printf(wxT("-*-%s-regular-*-*-*-*-*-*-*-*-*-*-*"),
548 if ( wxTestFontSpec(fontSpec
) )
550 xweight
= wxT("regular");
556 default: xweight
= wxT("*"); break;
559 // construct the X font spec from our data
560 fontSpec
.Printf(wxT("-*-%s-%s-%s-normal-*-*-%d-*-*-*-*-%s-%s"),
561 xfamily
.c_str(), xweight
.c_str(), xstyle
.c_str(),
562 pointSize
, xregistry
.c_str(), xencoding
.c_str());
565 *xFontName
= fontSpec
;
567 return wxLoadFont(fontSpec
);
570 // ----------------------------------------------------------------------------
572 // ----------------------------------------------------------------------------
574 class wxFontModule
: public wxModule
581 DECLARE_DYNAMIC_CLASS(wxFontModule
)
584 IMPLEMENT_DYNAMIC_CLASS(wxFontModule
, wxModule
)
586 bool wxFontModule::OnInit()
588 g_fontHash
= new wxHashTable( wxKEY_STRING
);
593 void wxFontModule::OnExit()
597 g_fontHash
= (wxHashTable
*)NULL
;