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 void wxNativeFontInfo::Init()
165 bool wxNativeFontInfo::FromString(const wxString
& s
)
167 wxStringTokenizer
tokenizer(s
, _T(";"));
170 wxString token
= tokenizer
.GetNextToken();
171 if ( token
!= _T('0') )
174 xFontName
= tokenizer
.GetNextToken();
176 // this should be the end
177 if ( tokenizer
.HasMoreTokens() )
180 return FromXFontName(xFontName
);
183 wxString
wxNativeFontInfo::ToString() const
186 return wxString::Format(_T("%d;%s"), 0, GetXFontName().c_str());
189 bool wxNativeFontInfo::FromUserString(const wxString
& s
)
191 return FromXFontName(s
);
194 wxString
wxNativeFontInfo::ToUserString() const
196 return GetXFontName();
199 bool wxNativeFontInfo::FromXFontName(const wxString
& xFontName
)
201 // TODO: we should be able to handle the font aliases here, but how?
202 wxStringTokenizer
tokenizer(xFontName
, _T("-"), wxTOKEN_STRTOK
);
204 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
206 if ( !tokenizer
.HasMoreTokens() )
208 // not enough elements in the XLFD - or maybe an alias
212 fontElements
[n
] = tokenizer
.GetNextToken();
215 // this should be all
216 return !tokenizer
.HasMoreTokens();
219 wxString
wxNativeFontInfo::GetXFontName() const
221 if ( xFontName
.empty() )
223 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
225 // replace the non specified elements with '*' except for the
226 // additional style which is usually just omitted
227 wxString elt
= fontElements
[n
];
228 if ( elt
.empty() && n
!= 5 )
234 ((wxNativeFontInfo
*)this)->xFontName
<< _T('-') << elt
;
241 // ----------------------------------------------------------------------------
243 // ----------------------------------------------------------------------------
245 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
246 wxNativeEncodingInfo
*info
)
248 wxCHECK_MSG( info
, FALSE
, _T("bad pointer in wxGetNativeFontEncoding") );
250 if ( encoding
== wxFONTENCODING_DEFAULT
)
252 encoding
= wxFont::GetDefaultEncoding();
257 case wxFONTENCODING_ISO8859_1
:
258 case wxFONTENCODING_ISO8859_2
:
259 case wxFONTENCODING_ISO8859_3
:
260 case wxFONTENCODING_ISO8859_4
:
261 case wxFONTENCODING_ISO8859_5
:
262 case wxFONTENCODING_ISO8859_6
:
263 case wxFONTENCODING_ISO8859_7
:
264 case wxFONTENCODING_ISO8859_8
:
265 case wxFONTENCODING_ISO8859_9
:
266 case wxFONTENCODING_ISO8859_10
:
267 case wxFONTENCODING_ISO8859_11
:
268 case wxFONTENCODING_ISO8859_12
:
269 case wxFONTENCODING_ISO8859_13
:
270 case wxFONTENCODING_ISO8859_14
:
271 case wxFONTENCODING_ISO8859_15
:
273 int cp
= encoding
- wxFONTENCODING_ISO8859_1
+ 1;
274 info
->xregistry
= wxT("iso8859");
275 info
->xencoding
.Printf(wxT("%d"), cp
);
279 case wxFONTENCODING_UTF8
:
280 info
->xregistry
= wxT("iso10646");
281 info
->xencoding
= wxT("*");
284 case wxFONTENCODING_KOI8
:
285 info
->xregistry
= wxT("koi8");
287 // we don't make distinction between koi8-r, koi8-u and koi8-ru (so far)
288 info
->xencoding
= wxT("*");
291 case wxFONTENCODING_CP1250
:
292 case wxFONTENCODING_CP1251
:
293 case wxFONTENCODING_CP1252
:
294 case wxFONTENCODING_CP1253
:
295 case wxFONTENCODING_CP1254
:
296 case wxFONTENCODING_CP1255
:
297 case wxFONTENCODING_CP1256
:
298 case wxFONTENCODING_CP1257
:
300 int cp
= encoding
- wxFONTENCODING_CP1250
+ 1250;
301 info
->xregistry
= wxT("microsoft");
302 info
->xencoding
.Printf(wxT("cp%d"), cp
);
306 case wxFONTENCODING_SYSTEM
:
308 info
->xencoding
= wxT("*");
312 // don't know how to translate this encoding into X fontspec
316 info
->encoding
= encoding
;
321 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
324 fontspec
.Printf(_T("-*-%s-*-*-*-*-*-*-*-*-*-*-%s-%s"),
325 !info
.facename
? _T("*") : info
.facename
.c_str(),
326 info
.xregistry
.c_str(),
327 info
.xencoding
.c_str());
329 return wxTestFontSpec(fontspec
);
332 // ----------------------------------------------------------------------------
333 // X-specific functions
334 // ----------------------------------------------------------------------------
336 wxNativeFont
wxLoadQueryNearestFont(int pointSize
,
341 const wxString
&facename
,
342 wxFontEncoding encoding
,
345 if ( encoding
== wxFONTENCODING_DEFAULT
)
347 encoding
= wxFont::GetDefaultEncoding();
350 // first determine the encoding - if the font doesn't exist at all in this
351 // encoding, it's useless to do all other approximations (i.e. size,
352 // family &c don't matter much)
353 wxNativeEncodingInfo info
;
354 if ( encoding
== wxFONTENCODING_SYSTEM
)
356 // This will always work so we don't test to save time
357 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
361 if ( !wxGetNativeFontEncoding(encoding
, &info
) ||
362 !wxTestFontEncoding(info
) )
365 if ( !wxTheFontMapper
->GetAltForEncoding(encoding
, &info
) )
366 #endif // wxUSE_FONTMAP
368 // unspported encoding - replace it with the default
370 // NB: we can't just return 0 from here because wxGTK code doesn't
371 // check for it (i.e. it supposes that we'll always succeed),
372 // so it would provoke a crash
373 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
378 // OK, we have the correct xregistry/xencoding in info structure
379 wxNativeFont font
= 0;
381 // if we already have the X font name, try to use it
382 if( xFontName
&& !xFontName
->IsEmpty() )
385 // Make sure point size is correct for scale factor.
387 wxStringTokenizer
tokenizer(*xFontName
, _T("-"), wxTOKEN_RET_DELIMS
);
388 wxString newFontName
;
390 for(int i
= 0; i
< 8; i
++)
391 newFontName
+= tokenizer
.NextToken();
393 (void) tokenizer
.NextToken();
395 newFontName
+= wxString::Format("%d-", pointSize
);
397 while(tokenizer
.HasMoreTokens())
398 newFontName
+= tokenizer
.GetNextToken();
400 font
= wxLoadFont(newFontName
);
403 *xFontName
= newFontName
;
406 // try to load exactly the font requested first
409 font
= wxLoadQueryFont( pointSize
, family
, style
, weight
,
410 underlined
, facename
,
411 info
.xregistry
, info
.xencoding
,
417 // search up and down by stepsize 10
418 int max_size
= pointSize
+ 20 * (1 + (pointSize
/180));
419 int min_size
= pointSize
- 20 * (1 + (pointSize
/180));
423 // Search for smaller size (approx.)
424 for ( i
= pointSize
- 10; !font
&& i
>= 10 && i
>= min_size
; i
-= 10 )
426 font
= wxLoadQueryFont(i
, family
, style
, weight
, underlined
,
427 facename
, info
.xregistry
, info
.xencoding
,
431 // Search for larger size (approx.)
432 for ( i
= pointSize
+ 10; !font
&& i
<= max_size
; i
+= 10 )
434 font
= wxLoadQueryFont(i
, family
, style
, weight
, underlined
,
435 facename
, info
.xregistry
, info
.xencoding
,
439 // Try default family
440 if ( !font
&& family
!= wxDEFAULT
)
442 font
= wxLoadQueryFont(pointSize
, wxDEFAULT
, style
, weight
,
443 underlined
, facename
,
444 info
.xregistry
, info
.xencoding
,
448 // ignore size, family, style and weight but try to find font with the
449 // given facename and encoding
452 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
453 underlined
, facename
,
454 info
.xregistry
, info
.xencoding
,
457 // ignore family as well
460 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
461 underlined
, wxEmptyString
,
462 info
.xregistry
, info
.xencoding
,
465 // if it still failed, try to get the font of any size but
466 // with the requested encoding: this can happen if the
467 // encoding is only available in one size which happens to be
468 // different from 120
471 font
= wxLoadQueryFont(-1, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
472 FALSE
, wxEmptyString
,
473 info
.xregistry
, info
.xencoding
,
476 // this should never happen as we had tested for it in the
477 // very beginning, but if it does, do return something non
478 // NULL or we'd crash in wxFont code
481 wxFAIL_MSG( _T("this encoding should be available!") );
483 font
= wxLoadQueryFont(-1,
484 wxDEFAULT
, wxNORMAL
, wxNORMAL
,
485 FALSE
, wxEmptyString
,
497 // ----------------------------------------------------------------------------
499 // ----------------------------------------------------------------------------
501 // returns TRUE if there are any fonts matching this font spec
502 static bool wxTestFontSpec(const wxString
& fontspec
)
504 // some X servers will fail to load this font because there are too many
505 // matches so we must test explicitly for this
506 if ( fontspec
== _T("-*-*-*-*-*-*-*-*-*-*-*-*-*-*") )
511 wxNativeFont test
= (wxNativeFont
) g_fontHash
->Get( fontspec
);
517 test
= wxLoadFont(fontspec
);
518 g_fontHash
->Put( fontspec
, (wxObject
*) test
);
532 static wxNativeFont
wxLoadQueryFont(int pointSize
,
536 bool WXUNUSED(underlined
),
537 const wxString
& facename
,
538 const wxString
& xregistry
,
539 const wxString
& xencoding
,
545 case wxDECORATIVE
: xfamily
= wxT("lucida"); break;
546 case wxROMAN
: xfamily
= wxT("times"); break;
547 case wxMODERN
: xfamily
= wxT("courier"); break;
548 case wxSWISS
: xfamily
= wxT("helvetica"); break;
549 case wxTELETYPE
: xfamily
= wxT("lucidatypewriter"); break;
550 case wxSCRIPT
: xfamily
= wxT("utopia"); break;
551 default: xfamily
= wxT("*");
555 if (!facename
.IsEmpty())
557 fontSpec
.Printf(wxT("-*-%s-*-*-normal-*-*-*-*-*-*-*-*-*"),
560 if ( wxTestFontSpec(fontSpec
) )
564 //else: no such family, use default one instead
571 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
573 if ( wxTestFontSpec(fontSpec
) )
578 // fall through - try wxITALIC now
581 fontSpec
.Printf(wxT("-*-%s-*-i-*-*-*-*-*-*-*-*-*-*"),
583 if ( wxTestFontSpec(fontSpec
) )
587 else if ( style
== wxITALIC
) // and not wxSLANT
590 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
592 if ( wxTestFontSpec(fontSpec
) )
598 // no italic, no slant - leave default
605 wxFAIL_MSG(_T("unknown font style"));
606 // fall back to normal
618 fontSpec
.Printf(wxT("-*-%s-bold-*-*-*-*-*-*-*-*-*-*-*"),
620 if ( wxTestFontSpec(fontSpec
) )
622 xweight
= wxT("bold");
625 fontSpec
.Printf(wxT("-*-%s-heavy-*-*-*-*-*-*-*-*-*-*-*"),
627 if ( wxTestFontSpec(fontSpec
) )
629 xweight
= wxT("heavy");
632 fontSpec
.Printf(wxT("-*-%s-extrabold-*-*-*-*-*-*-*-*-*-*-*"),
634 if ( wxTestFontSpec(fontSpec
) )
636 xweight
= wxT("extrabold");
639 fontSpec
.Printf(wxT("-*-%s-demibold-*-*-*-*-*-*-*-*-*-*-*"),
641 if ( wxTestFontSpec(fontSpec
) )
643 xweight
= wxT("demibold");
646 fontSpec
.Printf(wxT("-*-%s-black-*-*-*-*-*-*-*-*-*-*-*"),
648 if ( wxTestFontSpec(fontSpec
) )
650 xweight
= wxT("black");
653 fontSpec
.Printf(wxT("-*-%s-ultrablack-*-*-*-*-*-*-*-*-*-*-*"),
655 if ( wxTestFontSpec(fontSpec
) )
657 xweight
= wxT("ultrablack");
664 fontSpec
.Printf(wxT("-*-%s-light-*-*-*-*-*-*-*-*-*-*-*"),
666 if ( wxTestFontSpec(fontSpec
) )
668 xweight
= wxT("light");
671 fontSpec
.Printf(wxT("-*-%s-thin-*-*-*-*-*-*-*-*-*-*-*"),
673 if ( wxTestFontSpec(fontSpec
) )
675 xweight
= wxT("thin");
682 fontSpec
.Printf(wxT("-*-%s-medium-*-*-*-*-*-*-*-*-*-*-*"),
684 if ( wxTestFontSpec(fontSpec
) )
686 xweight
= wxT("medium");
689 fontSpec
.Printf(wxT("-*-%s-normal-*-*-*-*-*-*-*-*-*-*-*"),
691 if ( wxTestFontSpec(fontSpec
) )
693 xweight
= wxT("normal");
696 fontSpec
.Printf(wxT("-*-%s-regular-*-*-*-*-*-*-*-*-*-*-*"),
698 if ( wxTestFontSpec(fontSpec
) )
700 xweight
= wxT("regular");
706 default: xweight
= wxT("*"); break;
709 // if pointSize is -1, don't specify any
711 if ( fontSpec
== -1 )
717 sizeSpec
.Printf(_T("%d"), pointSize
);
720 // construct the X font spec from our data
721 fontSpec
.Printf(wxT("-*-%s-%s-%s-normal-*-*-%s-*-*-*-*-%s-%s"),
722 xfamily
.c_str(), xweight
.c_str(), xstyle
.c_str(),
723 sizeSpec
.c_str(), xregistry
.c_str(), xencoding
.c_str());
726 *xFontName
= fontSpec
;
728 return wxLoadFont(fontSpec
);
731 // ----------------------------------------------------------------------------
733 // ----------------------------------------------------------------------------
735 class wxFontModule
: public wxModule
742 DECLARE_DYNAMIC_CLASS(wxFontModule
)
745 IMPLEMENT_DYNAMIC_CLASS(wxFontModule
, wxModule
)
747 bool wxFontModule::OnInit()
749 g_fontHash
= new wxHashTable( wxKEY_STRING
);
754 void wxFontModule::OnExit()
758 g_fontHash
= (wxHashTable
*)NULL
;