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 info
->xregistry
= wxT("iso10646");
196 info
->xencoding
= wxT("*");
199 case wxFONTENCODING_KOI8
:
200 info
->xregistry
= wxT("koi8");
202 // we don't make distinction between koi8-r, koi8-u and koi8-ru (so far)
203 info
->xencoding
= wxT("*");
206 case wxFONTENCODING_CP1250
:
207 case wxFONTENCODING_CP1251
:
208 case wxFONTENCODING_CP1252
:
209 case wxFONTENCODING_CP1253
:
210 case wxFONTENCODING_CP1254
:
211 case wxFONTENCODING_CP1255
:
212 case wxFONTENCODING_CP1256
:
213 case wxFONTENCODING_CP1257
:
215 int cp
= encoding
- wxFONTENCODING_CP1250
+ 1250;
216 info
->xregistry
= wxT("microsoft");
217 info
->xencoding
.Printf(wxT("cp%d"), cp
);
221 case wxFONTENCODING_SYSTEM
:
223 info
->xencoding
= wxT("*");
227 // don't know how to translate this encoding into X fontspec
231 info
->encoding
= encoding
;
236 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
239 fontspec
.Printf(_T("-*-%s-*-*-*-*-*-*-*-*-*-*-%s-%s"),
240 !info
.facename
? _T("*") : info
.facename
.c_str(),
241 info
.xregistry
.c_str(),
242 info
.xencoding
.c_str());
244 return wxTestFontSpec(fontspec
);
247 // ----------------------------------------------------------------------------
248 // X-specific functions
249 // ----------------------------------------------------------------------------
251 wxNativeFont
wxLoadQueryNearestFont(int pointSize
,
256 const wxString
&facename
,
257 wxFontEncoding encoding
,
260 if ( encoding
== wxFONTENCODING_DEFAULT
)
262 encoding
= wxFont::GetDefaultEncoding();
265 // first determine the encoding - if the font doesn't exist at all in this
266 // encoding, it's useless to do all other approximations (i.e. size,
267 // family &c don't matter much)
268 wxNativeEncodingInfo info
;
269 if ( encoding
== wxFONTENCODING_SYSTEM
)
271 // This will always work so we don't test to save time
272 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
276 if ( !wxGetNativeFontEncoding(encoding
, &info
) ||
277 !wxTestFontEncoding(info
) )
280 if ( !wxTheFontMapper
->GetAltForEncoding(encoding
, &info
) )
281 #endif // wxUSE_FONTMAP
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
;
321 // try to load exactly the font requested first
324 font
= wxLoadQueryFont( pointSize
, family
, style
, weight
,
325 underlined
, facename
,
326 info
.xregistry
, info
.xencoding
,
332 // search up and down by stepsize 10
333 int max_size
= pointSize
+ 20 * (1 + (pointSize
/180));
334 int min_size
= pointSize
- 20 * (1 + (pointSize
/180));
338 // Search for smaller size (approx.)
339 for ( i
= pointSize
- 10; !font
&& i
>= 10 && i
>= min_size
; i
-= 10 )
341 font
= wxLoadQueryFont(i
, family
, style
, weight
, underlined
,
342 facename
, info
.xregistry
, info
.xencoding
,
346 // Search for larger size (approx.)
347 for ( i
= pointSize
+ 10; !font
&& i
<= max_size
; i
+= 10 )
349 font
= wxLoadQueryFont(i
, family
, style
, weight
, underlined
,
350 facename
, info
.xregistry
, info
.xencoding
,
354 // Try default family
355 if ( !font
&& family
!= wxDEFAULT
)
357 font
= wxLoadQueryFont(pointSize
, wxDEFAULT
, style
, weight
,
358 underlined
, facename
,
359 info
.xregistry
, info
.xencoding
,
363 // ignore size, family, style and weight but try to find font with the
364 // given facename and encoding
367 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
368 underlined
, facename
,
369 info
.xregistry
, info
.xencoding
,
372 // ignore family as well
375 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
376 underlined
, wxEmptyString
,
377 info
.xregistry
, info
.xencoding
,
380 // if it still failed, try to get the font of any size but
381 // with the requested encoding: this can happen if the
382 // encoding is only available in one size which happens to be
383 // different from 120
386 font
= wxLoadQueryFont(-1, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
387 FALSE
, wxEmptyString
,
388 info
.xregistry
, info
.xencoding
,
391 // this should never happen as we had tested for it in the
392 // very beginning, but if it does, do return something non
393 // NULL or we'd crash in wxFont code
396 wxFAIL_MSG( _T("this encoding should be available!") );
398 font
= wxLoadQueryFont(-1,
399 wxDEFAULT
, wxNORMAL
, wxNORMAL
,
400 FALSE
, wxEmptyString
,
412 // ----------------------------------------------------------------------------
414 // ----------------------------------------------------------------------------
416 // returns TRUE if there are any fonts matching this font spec
417 static bool wxTestFontSpec(const wxString
& fontspec
)
419 // some X servers will fail to load this font because there are too many
420 // matches so we must test explicitly for this
421 if ( fontspec
== _T("-*-*-*-*-*-*-*-*-*-*-*-*-*-*") )
426 wxNativeFont test
= (wxNativeFont
) g_fontHash
->Get( fontspec
);
432 test
= wxLoadFont(fontspec
);
433 g_fontHash
->Put( fontspec
, (wxObject
*) test
);
447 static wxNativeFont
wxLoadQueryFont(int pointSize
,
451 bool WXUNUSED(underlined
),
452 const wxString
& facename
,
453 const wxString
& xregistry
,
454 const wxString
& xencoding
,
460 case wxDECORATIVE
: xfamily
= wxT("lucida"); break;
461 case wxROMAN
: xfamily
= wxT("times"); break;
462 case wxMODERN
: xfamily
= wxT("courier"); break;
463 case wxSWISS
: xfamily
= wxT("helvetica"); break;
464 case wxTELETYPE
: xfamily
= wxT("lucidatypewriter"); break;
465 case wxSCRIPT
: xfamily
= wxT("utopia"); break;
466 default: xfamily
= wxT("*");
470 if (!facename
.IsEmpty())
472 fontSpec
.Printf(wxT("-*-%s-*-*-normal-*-*-*-*-*-*-*-*-*"),
475 if ( wxTestFontSpec(fontSpec
) )
479 //else: no such family, use default one instead
486 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
488 if ( wxTestFontSpec(fontSpec
) )
493 // fall through - try wxITALIC now
496 fontSpec
.Printf(wxT("-*-%s-*-i-*-*-*-*-*-*-*-*-*-*"),
498 if ( wxTestFontSpec(fontSpec
) )
502 else if ( style
== wxITALIC
) // and not wxSLANT
505 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
507 if ( wxTestFontSpec(fontSpec
) )
513 // no italic, no slant - leave default
520 wxFAIL_MSG(_T("unknown font style"));
521 // fall back to normal
533 fontSpec
.Printf(wxT("-*-%s-bold-*-*-*-*-*-*-*-*-*-*-*"),
535 if ( wxTestFontSpec(fontSpec
) )
537 xweight
= wxT("bold");
540 fontSpec
.Printf(wxT("-*-%s-heavy-*-*-*-*-*-*-*-*-*-*-*"),
542 if ( wxTestFontSpec(fontSpec
) )
544 xweight
= wxT("heavy");
547 fontSpec
.Printf(wxT("-*-%s-extrabold-*-*-*-*-*-*-*-*-*-*-*"),
549 if ( wxTestFontSpec(fontSpec
) )
551 xweight
= wxT("extrabold");
554 fontSpec
.Printf(wxT("-*-%s-demibold-*-*-*-*-*-*-*-*-*-*-*"),
556 if ( wxTestFontSpec(fontSpec
) )
558 xweight
= wxT("demibold");
561 fontSpec
.Printf(wxT("-*-%s-black-*-*-*-*-*-*-*-*-*-*-*"),
563 if ( wxTestFontSpec(fontSpec
) )
565 xweight
= wxT("black");
568 fontSpec
.Printf(wxT("-*-%s-ultrablack-*-*-*-*-*-*-*-*-*-*-*"),
570 if ( wxTestFontSpec(fontSpec
) )
572 xweight
= wxT("ultrablack");
579 fontSpec
.Printf(wxT("-*-%s-light-*-*-*-*-*-*-*-*-*-*-*"),
581 if ( wxTestFontSpec(fontSpec
) )
583 xweight
= wxT("light");
586 fontSpec
.Printf(wxT("-*-%s-thin-*-*-*-*-*-*-*-*-*-*-*"),
588 if ( wxTestFontSpec(fontSpec
) )
590 xweight
= wxT("thin");
597 fontSpec
.Printf(wxT("-*-%s-medium-*-*-*-*-*-*-*-*-*-*-*"),
599 if ( wxTestFontSpec(fontSpec
) )
601 xweight
= wxT("medium");
604 fontSpec
.Printf(wxT("-*-%s-normal-*-*-*-*-*-*-*-*-*-*-*"),
606 if ( wxTestFontSpec(fontSpec
) )
608 xweight
= wxT("normal");
611 fontSpec
.Printf(wxT("-*-%s-regular-*-*-*-*-*-*-*-*-*-*-*"),
613 if ( wxTestFontSpec(fontSpec
) )
615 xweight
= wxT("regular");
621 default: xweight
= wxT("*"); break;
624 // if pointSize is -1, don't specify any
626 if ( fontSpec
== -1 )
632 sizeSpec
.Printf(_T("%d"), pointSize
);
635 // construct the X font spec from our data
636 fontSpec
.Printf(wxT("-*-%s-%s-%s-normal-*-*-%s-*-*-*-*-%s-%s"),
637 xfamily
.c_str(), xweight
.c_str(), xstyle
.c_str(),
638 sizeSpec
.c_str(), xregistry
.c_str(), xencoding
.c_str());
641 *xFontName
= fontSpec
;
643 return wxLoadFont(fontSpec
);
646 // ----------------------------------------------------------------------------
648 // ----------------------------------------------------------------------------
650 class wxFontModule
: public wxModule
657 DECLARE_DYNAMIC_CLASS(wxFontModule
)
660 IMPLEMENT_DYNAMIC_CLASS(wxFontModule
, wxModule
)
662 bool wxFontModule::OnInit()
664 g_fontHash
= new wxHashTable( wxKEY_STRING
);
669 void wxFontModule::OnExit()
673 g_fontHash
= (wxHashTable
*)NULL
;