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
42 #pragma message enable nosimpint
45 #include "wx/utils.h" // for wxGetDisplay()
46 #elif defined(__WXGTK__)
47 // we have to declare struct tm to avoid problems with first forward
48 // declaring it in C code (glib.h included from gdk.h does it) and then
49 // defining it when time.h is included from the headers below - this is
50 // known not to work at least with Sun CC 6.01
56 #include "wx/fontutil.h"
57 #include "wx/fontmap.h"
58 #include "wx/tokenzr.h"
60 #include "wx/module.h"
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 static wxHashTable
*g_fontHash
= (wxHashTable
*) NULL
;
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
72 // define the functions to create and destroy native fonts for this toolkit
74 static inline wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
76 return XLoadQueryFont((Display
*)wxGetDisplay(), fontSpec
);
79 static inline void wxFreeFont(wxNativeFont font
)
81 XFreeFont((Display
*)wxGetDisplay(), (XFontStruct
*)font
);
83 #elif defined(__WXGTK__)
84 static inline wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
86 return gdk_font_load( wxConvertWX2MB(fontSpec
) );
89 static inline void wxFreeFont(wxNativeFont font
)
94 #error "Unknown GUI toolkit"
97 static bool wxTestFontSpec(const wxString
& fontspec
);
99 static wxNativeFont
wxLoadQueryFont(int pointSize
,
104 const wxString
& facename
,
105 const wxString
& xregistry
,
106 const wxString
& xencoding
,
107 wxString
* xFontName
);
109 // ============================================================================
111 // ============================================================================
113 // ----------------------------------------------------------------------------
114 // wxNativeEncodingInfo
115 // ----------------------------------------------------------------------------
117 // convert to/from the string representation: format is
118 // encodingid;registry;encoding[;facename]
119 bool wxNativeEncodingInfo::FromString(const wxString
& s
)
121 // use ";", not "-" because it may be part of encoding name
122 wxStringTokenizer
tokenizer(s
, _T(";"));
124 wxString encid
= tokenizer
.GetNextToken();
126 if ( !encid
.ToLong(&enc
) )
128 encoding
= (wxFontEncoding
)enc
;
130 xregistry
= tokenizer
.GetNextToken();
134 xencoding
= tokenizer
.GetNextToken();
139 facename
= tokenizer
.GetNextToken();
144 wxString
wxNativeEncodingInfo::ToString() const
147 s
<< (long)encoding
<< _T(';') << xregistry
<< _T(';') << xencoding
;
150 s
<< _T(';') << facename
;
156 // ----------------------------------------------------------------------------
158 // ----------------------------------------------------------------------------
160 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
161 wxNativeEncodingInfo
*info
)
163 wxCHECK_MSG( info
, FALSE
, _T("bad pointer in wxGetNativeFontEncoding") );
165 if ( encoding
== wxFONTENCODING_DEFAULT
)
167 encoding
= wxFont::GetDefaultEncoding();
172 case wxFONTENCODING_ISO8859_1
:
173 case wxFONTENCODING_ISO8859_2
:
174 case wxFONTENCODING_ISO8859_3
:
175 case wxFONTENCODING_ISO8859_4
:
176 case wxFONTENCODING_ISO8859_5
:
177 case wxFONTENCODING_ISO8859_6
:
178 case wxFONTENCODING_ISO8859_7
:
179 case wxFONTENCODING_ISO8859_8
:
180 case wxFONTENCODING_ISO8859_9
:
181 case wxFONTENCODING_ISO8859_10
:
182 case wxFONTENCODING_ISO8859_11
:
183 case wxFONTENCODING_ISO8859_12
:
184 case wxFONTENCODING_ISO8859_13
:
185 case wxFONTENCODING_ISO8859_14
:
186 case wxFONTENCODING_ISO8859_15
:
188 int cp
= encoding
- wxFONTENCODING_ISO8859_1
+ 1;
189 info
->xregistry
= wxT("iso8859");
190 info
->xencoding
.Printf(wxT("%d"), cp
);
194 case wxFONTENCODING_UTF8
:
195 // FIXME: this is probably false, but this is how they are called on
196 // my system and I don't know what the standard XFLD is (VZ)
197 info
->xregistry
= wxT("iso646.1991");
198 info
->xencoding
= wxT("*");
201 case wxFONTENCODING_KOI8
:
202 info
->xregistry
= wxT("koi8");
204 // we don't make distinction between koi8-r and koi8-u (so far)
205 info
->xencoding
= wxT("*");
208 case wxFONTENCODING_CP1250
:
209 case wxFONTENCODING_CP1251
:
210 case wxFONTENCODING_CP1252
:
211 case wxFONTENCODING_CP1253
:
212 case wxFONTENCODING_CP1254
:
213 case wxFONTENCODING_CP1255
:
214 case wxFONTENCODING_CP1256
:
215 case wxFONTENCODING_CP1257
:
217 int cp
= encoding
- wxFONTENCODING_CP1250
+ 1250;
218 info
->xregistry
= wxT("microsoft");
219 info
->xencoding
.Printf(wxT("cp%d"), cp
);
223 case wxFONTENCODING_SYSTEM
:
225 info
->xencoding
= wxT("*");
229 // don't know how to translate this encoding into X fontspec
233 info
->encoding
= encoding
;
238 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
241 fontspec
.Printf(_T("-*-%s-*-*-*-*-*-*-*-*-*-*-%s-%s"),
242 !info
.facename
? _T("*") : info
.facename
.c_str(),
243 info
.xregistry
.c_str(),
244 info
.xencoding
.c_str());
246 return wxTestFontSpec(fontspec
);
249 // ----------------------------------------------------------------------------
250 // X-specific functions
251 // ----------------------------------------------------------------------------
253 wxNativeFont
wxLoadQueryNearestFont(int pointSize
,
258 const wxString
&facename
,
259 wxFontEncoding encoding
,
262 if ( encoding
== wxFONTENCODING_DEFAULT
)
264 encoding
= wxFont::GetDefaultEncoding();
267 // first determine the encoding - if the font doesn't exist at all in this
268 // encoding, it's useless to do all other approximations (i.e. size,
269 // family &c don't matter much)
270 wxNativeEncodingInfo info
;
271 if ( encoding
== wxFONTENCODING_SYSTEM
)
273 // This will always work so we don't test to save time
274 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
278 if ( !wxGetNativeFontEncoding(encoding
, &info
) ||
279 !wxTestFontEncoding(info
) )
281 if ( !wxTheFontMapper
->GetAltForEncoding(encoding
, &info
) )
283 // unspported encoding - replace it with the default
285 // NB: we can't just return 0 from here because wxGTK code doesn't
286 // check for it (i.e. it supposes that we'll always succeed),
287 // so it would provoke a crash
288 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
293 // OK, we have the correct xregistry/xencoding in info structure
294 wxNativeFont font
= 0;
296 // if we already have the X font name, try to use it
297 if( xFontName
&& !xFontName
->IsEmpty() )
300 // Make sure point size is correct for scale factor.
302 wxStringTokenizer
tokenizer(*xFontName
, _T("-"), wxTOKEN_RET_DELIMS
);
303 wxString newFontName
;
305 for(int i
= 0; i
< 8; i
++)
306 newFontName
+= tokenizer
.NextToken();
308 (void) tokenizer
.NextToken();
310 newFontName
+= wxString::Format("%d-", pointSize
);
312 while(tokenizer
.HasMoreTokens())
313 newFontName
+= tokenizer
.GetNextToken();
315 font
= wxLoadFont(newFontName
);
318 *xFontName
= newFontName
;
322 font
= wxLoadQueryFont( pointSize
, family
, style
, weight
,
323 underlined
, facename
,
324 info
.xregistry
, info
.xencoding
,
329 // search up and down by stepsize 10
330 int max_size
= pointSize
+ 20 * (1 + (pointSize
/180));
331 int min_size
= pointSize
- 20 * (1 + (pointSize
/180));
335 // Search for smaller size (approx.)
336 for ( i
= pointSize
- 10; !font
&& i
>= 10 && i
>= min_size
; i
-= 10 )
338 font
= wxLoadQueryFont(i
, family
, style
, weight
, underlined
,
339 facename
, info
.xregistry
, info
.xencoding
,
343 // Search for larger size (approx.)
344 for ( i
= pointSize
+ 10; !font
&& i
<= max_size
; i
+= 10 )
346 font
= wxLoadQueryFont(i
, family
, style
, weight
, underlined
,
347 facename
, info
.xregistry
, info
.xencoding
,
351 // Try default family
352 if ( !font
&& family
!= wxDEFAULT
)
354 font
= wxLoadQueryFont(pointSize
, wxDEFAULT
, style
, weight
,
355 underlined
, facename
,
356 info
.xregistry
, info
.xencoding
,
363 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
364 underlined
, facename
,
365 info
.xregistry
, info
.xencoding
,
372 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
373 underlined
, wxEmptyString
,
374 info
.xregistry
, info
.xencoding
,
382 // ----------------------------------------------------------------------------
384 // ----------------------------------------------------------------------------
386 // returns TRUE if there are any fonts matching this font spec
387 static bool wxTestFontSpec(const wxString
& fontspec
)
389 // some X servers will fail to load this font because there are too many
390 // matches so we must test explicitly for this
391 if ( fontspec
== _T("-*-*-*-*-*-*-*-*-*-*-*-*-*-*") )
396 wxNativeFont test
= (wxNativeFont
) g_fontHash
->Get( fontspec
);
402 test
= wxLoadFont(fontspec
);
403 g_fontHash
->Put( fontspec
, (wxObject
*) test
);
417 static wxNativeFont
wxLoadQueryFont(int pointSize
,
421 bool WXUNUSED(underlined
),
422 const wxString
& facename
,
423 const wxString
& xregistry
,
424 const wxString
& xencoding
,
430 case wxDECORATIVE
: xfamily
= wxT("lucida"); break;
431 case wxROMAN
: xfamily
= wxT("times"); break;
432 case wxMODERN
: xfamily
= wxT("courier"); break;
433 case wxSWISS
: xfamily
= wxT("helvetica"); break;
434 case wxTELETYPE
: xfamily
= wxT("lucidatypewriter"); break;
435 case wxSCRIPT
: xfamily
= wxT("utopia"); break;
436 default: xfamily
= wxT("*");
440 if (!facename
.IsEmpty())
442 fontSpec
.Printf(wxT("-*-%s-*-*-normal-*-*-*-*-*-*-*-*-*"),
445 if ( wxTestFontSpec(fontSpec
) )
449 //else: no such family, use default one instead
456 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
458 if ( wxTestFontSpec(fontSpec
) )
463 // fall through - try wxITALIC now
466 fontSpec
.Printf(wxT("-*-%s-*-i-*-*-*-*-*-*-*-*-*-*"),
468 if ( wxTestFontSpec(fontSpec
) )
472 else if ( style
== wxITALIC
) // and not wxSLANT
475 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
477 if ( wxTestFontSpec(fontSpec
) )
483 // no italic, no slant - leave default
490 wxFAIL_MSG(_T("unknown font style"));
491 // fall back to normal
503 fontSpec
.Printf(wxT("-*-%s-bold-*-*-*-*-*-*-*-*-*-*-*"),
505 if ( wxTestFontSpec(fontSpec
) )
507 xweight
= wxT("bold");
510 fontSpec
.Printf(wxT("-*-%s-heavy-*-*-*-*-*-*-*-*-*-*-*"),
512 if ( wxTestFontSpec(fontSpec
) )
514 xweight
= wxT("heavy");
517 fontSpec
.Printf(wxT("-*-%s-extrabold-*-*-*-*-*-*-*-*-*-*-*"),
519 if ( wxTestFontSpec(fontSpec
) )
521 xweight
= wxT("extrabold");
524 fontSpec
.Printf(wxT("-*-%s-demibold-*-*-*-*-*-*-*-*-*-*-*"),
526 if ( wxTestFontSpec(fontSpec
) )
528 xweight
= wxT("demibold");
531 fontSpec
.Printf(wxT("-*-%s-black-*-*-*-*-*-*-*-*-*-*-*"),
533 if ( wxTestFontSpec(fontSpec
) )
535 xweight
= wxT("black");
538 fontSpec
.Printf(wxT("-*-%s-ultrablack-*-*-*-*-*-*-*-*-*-*-*"),
540 if ( wxTestFontSpec(fontSpec
) )
542 xweight
= wxT("ultrablack");
549 fontSpec
.Printf(wxT("-*-%s-light-*-*-*-*-*-*-*-*-*-*-*"),
551 if ( wxTestFontSpec(fontSpec
) )
553 xweight
= wxT("light");
556 fontSpec
.Printf(wxT("-*-%s-thin-*-*-*-*-*-*-*-*-*-*-*"),
558 if ( wxTestFontSpec(fontSpec
) )
560 xweight
= wxT("thin");
567 fontSpec
.Printf(wxT("-*-%s-medium-*-*-*-*-*-*-*-*-*-*-*"),
569 if ( wxTestFontSpec(fontSpec
) )
571 xweight
= wxT("medium");
574 fontSpec
.Printf(wxT("-*-%s-normal-*-*-*-*-*-*-*-*-*-*-*"),
576 if ( wxTestFontSpec(fontSpec
) )
578 xweight
= wxT("normal");
581 fontSpec
.Printf(wxT("-*-%s-regular-*-*-*-*-*-*-*-*-*-*-*"),
583 if ( wxTestFontSpec(fontSpec
) )
585 xweight
= wxT("regular");
591 default: xweight
= wxT("*"); break;
594 // construct the X font spec from our data
595 fontSpec
.Printf(wxT("-*-%s-%s-%s-normal-*-*-%d-*-*-*-*-%s-%s"),
596 xfamily
.c_str(), xweight
.c_str(), xstyle
.c_str(),
597 pointSize
, xregistry
.c_str(), xencoding
.c_str());
600 *xFontName
= fontSpec
;
602 return wxLoadFont(fontSpec
);
605 // ----------------------------------------------------------------------------
607 // ----------------------------------------------------------------------------
609 class wxFontModule
: public wxModule
616 DECLARE_DYNAMIC_CLASS(wxFontModule
)
619 IMPLEMENT_DYNAMIC_CLASS(wxFontModule
, wxModule
)
621 bool wxFontModule::OnInit()
623 g_fontHash
= new wxHashTable( wxKEY_STRING
);
628 void wxFontModule::OnExit()
632 g_fontHash
= (wxHashTable
*)NULL
;