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 // use ";", not "-" because it may be part of encoding name
114 wxStringTokenizer
tokenizer(s
, _T(";"));
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_UTF8
:
187 // FIXME: this is probably false, but this is how they are called on
188 // my system and I don't know what the standard XFLD is (VZ)
189 info
->xregistry
= wxT("iso646.1991");
190 info
->xencoding
= wxT("*");
193 case wxFONTENCODING_KOI8
:
194 info
->xregistry
= wxT("koi8");
196 // we don't make distinction between koi8-r and koi8-u (so far)
197 info
->xencoding
= wxT("*");
200 case wxFONTENCODING_CP1250
:
201 case wxFONTENCODING_CP1251
:
202 case wxFONTENCODING_CP1252
:
203 case wxFONTENCODING_CP1253
:
204 case wxFONTENCODING_CP1254
:
205 case wxFONTENCODING_CP1255
:
206 case wxFONTENCODING_CP1256
:
207 case wxFONTENCODING_CP1257
:
209 int cp
= encoding
- wxFONTENCODING_CP1250
+ 1250;
210 info
->xregistry
= wxT("microsoft");
211 info
->xencoding
.Printf(wxT("cp%d"), cp
);
215 case wxFONTENCODING_SYSTEM
:
217 info
->xencoding
= wxT("*");
221 // don't know how to translate this encoding into X fontspec
225 info
->encoding
= encoding
;
230 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
233 fontspec
.Printf(_T("-*-%s-*-*-*-*-*-*-*-*-*-*-%s-%s"),
234 !info
.facename
? _T("*") : info
.facename
.c_str(),
235 info
.xregistry
.c_str(),
236 info
.xencoding
.c_str());
238 return wxTestFontSpec(fontspec
);
241 // ----------------------------------------------------------------------------
242 // X-specific functions
243 // ----------------------------------------------------------------------------
245 wxNativeFont
wxLoadQueryNearestFont(int pointSize
,
250 const wxString
&facename
,
251 wxFontEncoding encoding
,
254 if ( encoding
== wxFONTENCODING_DEFAULT
)
256 encoding
= wxFont::GetDefaultEncoding();
259 // first determine the encoding - if the font doesn't exist at all in this
260 // encoding, it's useless to do all other approximations (i.e. size,
261 // family &c don't matter much)
262 wxNativeEncodingInfo info
;
263 if ( encoding
== wxFONTENCODING_SYSTEM
)
265 // This will always work so we don't test to save time
266 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
270 if ( !wxGetNativeFontEncoding(encoding
, &info
) ||
271 !wxTestFontEncoding(info
) )
273 if ( !wxTheFontMapper
->GetAltForEncoding(encoding
, &info
) )
275 // unspported encoding - replace it with the default
277 // NB: we can't just return 0 from here because wxGTK code doesn't
278 // check for it (i.e. it supposes that we'll always succeed),
279 // so it would provoke a crash
280 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
285 // OK, we have the correct xregistry/xencoding in info structure
286 wxNativeFont font
= 0;
288 // if we already have the X font name, try to use it
289 if( xFontName
&& !xFontName
->IsEmpty() )
292 // Make sure point size is correct for scale factor.
294 wxStringTokenizer
tokenizer(*xFontName
, _T("-"), wxTOKEN_RET_DELIMS
);
295 wxString newFontName
;
297 for(int i
= 0; i
< 8; i
++)
298 newFontName
+= tokenizer
.NextToken();
300 (void) tokenizer
.NextToken();
302 newFontName
+= wxString::Format("%d-", pointSize
);
304 while(tokenizer
.HasMoreTokens())
305 newFontName
+= tokenizer
.GetNextToken();
307 font
= wxLoadFont(newFontName
);
310 *xFontName
= newFontName
;
314 font
= wxLoadQueryFont( pointSize
, family
, style
, weight
,
315 underlined
, facename
,
316 info
.xregistry
, info
.xencoding
,
321 // search up and down by stepsize 10
322 int max_size
= pointSize
+ 20 * (1 + (pointSize
/180));
323 int min_size
= pointSize
- 20 * (1 + (pointSize
/180));
327 // Search for smaller size (approx.)
328 for ( i
= pointSize
- 10; !font
&& i
>= 10 && i
>= min_size
; i
-= 10 )
330 font
= wxLoadQueryFont(i
, family
, style
, weight
, underlined
,
331 facename
, info
.xregistry
, info
.xencoding
,
335 // Search for larger size (approx.)
336 for ( i
= pointSize
+ 10; !font
&& i
<= max_size
; i
+= 10 )
338 font
= wxLoadQueryFont(i
, family
, style
, weight
, underlined
,
339 facename
, info
.xregistry
, info
.xencoding
,
343 // Try default family
344 if ( !font
&& family
!= wxDEFAULT
)
346 font
= wxLoadQueryFont(pointSize
, wxDEFAULT
, style
, weight
,
347 underlined
, facename
,
348 info
.xregistry
, info
.xencoding
,
355 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
356 underlined
, facename
,
357 info
.xregistry
, info
.xencoding
,
364 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
365 underlined
, wxEmptyString
,
366 info
.xregistry
, info
.xencoding
,
374 // ----------------------------------------------------------------------------
376 // ----------------------------------------------------------------------------
378 // returns TRUE if there are any fonts matching this font spec
379 static bool wxTestFontSpec(const wxString
& fontspec
)
381 // some X servers will fail to load this font because there are too many
382 // matches so we must test explicitly for this
383 if ( fontspec
== _T("-*-*-*-*-*-*-*-*-*-*-*-*-*-*") )
388 wxNativeFont test
= (wxNativeFont
) g_fontHash
->Get( fontspec
);
394 test
= wxLoadFont(fontspec
);
395 g_fontHash
->Put( fontspec
, (wxObject
*) test
);
409 static wxNativeFont
wxLoadQueryFont(int pointSize
,
413 bool WXUNUSED(underlined
),
414 const wxString
& facename
,
415 const wxString
& xregistry
,
416 const wxString
& xencoding
,
422 case wxDECORATIVE
: xfamily
= wxT("lucida"); break;
423 case wxROMAN
: xfamily
= wxT("times"); break;
424 case wxMODERN
: xfamily
= wxT("courier"); break;
425 case wxSWISS
: xfamily
= wxT("helvetica"); break;
426 case wxTELETYPE
: xfamily
= wxT("lucidatypewriter"); break;
427 case wxSCRIPT
: xfamily
= wxT("utopia"); break;
428 default: xfamily
= wxT("*");
432 if (!facename
.IsEmpty())
434 fontSpec
.Printf(wxT("-*-%s-*-*-normal-*-*-*-*-*-*-*-*-*"),
437 if ( wxTestFontSpec(fontSpec
) )
441 //else: no such family, use default one instead
448 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
450 if ( wxTestFontSpec(fontSpec
) )
455 // fall through - try wxITALIC now
458 fontSpec
.Printf(wxT("-*-%s-*-i-*-*-*-*-*-*-*-*-*-*"),
460 if ( wxTestFontSpec(fontSpec
) )
464 else if ( style
== wxITALIC
) // and not wxSLANT
467 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
469 if ( wxTestFontSpec(fontSpec
) )
475 // no italic, no slant - leave default
482 wxFAIL_MSG(_T("unknown font style"));
483 // fall back to normal
495 fontSpec
.Printf(wxT("-*-%s-bold-*-*-*-*-*-*-*-*-*-*-*"),
497 if ( wxTestFontSpec(fontSpec
) )
499 xweight
= wxT("bold");
502 fontSpec
.Printf(wxT("-*-%s-heavy-*-*-*-*-*-*-*-*-*-*-*"),
504 if ( wxTestFontSpec(fontSpec
) )
506 xweight
= wxT("heavy");
509 fontSpec
.Printf(wxT("-*-%s-extrabold-*-*-*-*-*-*-*-*-*-*-*"),
511 if ( wxTestFontSpec(fontSpec
) )
513 xweight
= wxT("extrabold");
516 fontSpec
.Printf(wxT("-*-%s-demibold-*-*-*-*-*-*-*-*-*-*-*"),
518 if ( wxTestFontSpec(fontSpec
) )
520 xweight
= wxT("demibold");
523 fontSpec
.Printf(wxT("-*-%s-black-*-*-*-*-*-*-*-*-*-*-*"),
525 if ( wxTestFontSpec(fontSpec
) )
527 xweight
= wxT("black");
530 fontSpec
.Printf(wxT("-*-%s-ultrablack-*-*-*-*-*-*-*-*-*-*-*"),
532 if ( wxTestFontSpec(fontSpec
) )
534 xweight
= wxT("ultrablack");
541 fontSpec
.Printf(wxT("-*-%s-light-*-*-*-*-*-*-*-*-*-*-*"),
543 if ( wxTestFontSpec(fontSpec
) )
545 xweight
= wxT("light");
548 fontSpec
.Printf(wxT("-*-%s-thin-*-*-*-*-*-*-*-*-*-*-*"),
550 if ( wxTestFontSpec(fontSpec
) )
552 xweight
= wxT("thin");
559 fontSpec
.Printf(wxT("-*-%s-medium-*-*-*-*-*-*-*-*-*-*-*"),
561 if ( wxTestFontSpec(fontSpec
) )
563 xweight
= wxT("medium");
566 fontSpec
.Printf(wxT("-*-%s-normal-*-*-*-*-*-*-*-*-*-*-*"),
568 if ( wxTestFontSpec(fontSpec
) )
570 xweight
= wxT("normal");
573 fontSpec
.Printf(wxT("-*-%s-regular-*-*-*-*-*-*-*-*-*-*-*"),
575 if ( wxTestFontSpec(fontSpec
) )
577 xweight
= wxT("regular");
583 default: xweight
= wxT("*"); break;
586 // construct the X font spec from our data
587 fontSpec
.Printf(wxT("-*-%s-%s-%s-normal-*-*-%d-*-*-*-*-%s-%s"),
588 xfamily
.c_str(), xweight
.c_str(), xstyle
.c_str(),
589 pointSize
, xregistry
.c_str(), xencoding
.c_str());
592 *xFontName
= fontSpec
;
594 return wxLoadFont(fontSpec
);
597 // ----------------------------------------------------------------------------
599 // ----------------------------------------------------------------------------
601 class wxFontModule
: public wxModule
608 DECLARE_DYNAMIC_CLASS(wxFontModule
)
611 IMPLEMENT_DYNAMIC_CLASS(wxFontModule
, wxModule
)
613 bool wxFontModule::OnInit()
615 g_fontHash
= new wxHashTable( wxKEY_STRING
);
620 void wxFontModule::OnExit()
624 g_fontHash
= (wxHashTable
*)NULL
;