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 wxString
wxNativeFontInfo::GetXFontComponent(wxXLFDField field
) const
201 wxCHECK_MSG( field
< wxXLFD_MAX
, _T(""), _T("invalid XLFD field") );
203 if ( fontElements
[0].empty() )
206 if ( !((wxNativeFontInfo
*)this)->FromXFontName(xFontName
) )
210 return fontElements
[field
];
213 bool wxNativeFontInfo::FromXFontName(const wxString
& fontname
)
215 // TODO: we should be able to handle the font aliases here, but how?
216 wxStringTokenizer
tokenizer(fontname
, _T("-"), wxTOKEN_STRTOK
);
218 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
220 if ( !tokenizer
.HasMoreTokens() )
222 // not enough elements in the XLFD - or maybe an alias
226 fontElements
[n
] = tokenizer
.GetNextToken();
229 // this should be all
230 return !tokenizer
.HasMoreTokens();
233 wxString
wxNativeFontInfo::GetXFontName() const
235 if ( xFontName
.empty() )
237 for ( size_t n
= 0; n
< WXSIZEOF(fontElements
); n
++ )
239 // replace the non specified elements with '*' except for the
240 // additional style which is usually just omitted
241 wxString elt
= fontElements
[n
];
242 if ( elt
.empty() && n
!= 5 )
248 ((wxNativeFontInfo
*)this)->xFontName
<< _T('-') << elt
;
255 // ----------------------------------------------------------------------------
257 // ----------------------------------------------------------------------------
259 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
260 wxNativeEncodingInfo
*info
)
262 wxCHECK_MSG( info
, FALSE
, _T("bad pointer in wxGetNativeFontEncoding") );
264 if ( encoding
== wxFONTENCODING_DEFAULT
)
266 encoding
= wxFont::GetDefaultEncoding();
271 case wxFONTENCODING_ISO8859_1
:
272 case wxFONTENCODING_ISO8859_2
:
273 case wxFONTENCODING_ISO8859_3
:
274 case wxFONTENCODING_ISO8859_4
:
275 case wxFONTENCODING_ISO8859_5
:
276 case wxFONTENCODING_ISO8859_6
:
277 case wxFONTENCODING_ISO8859_7
:
278 case wxFONTENCODING_ISO8859_8
:
279 case wxFONTENCODING_ISO8859_9
:
280 case wxFONTENCODING_ISO8859_10
:
281 case wxFONTENCODING_ISO8859_11
:
282 case wxFONTENCODING_ISO8859_12
:
283 case wxFONTENCODING_ISO8859_13
:
284 case wxFONTENCODING_ISO8859_14
:
285 case wxFONTENCODING_ISO8859_15
:
287 int cp
= encoding
- wxFONTENCODING_ISO8859_1
+ 1;
288 info
->xregistry
= wxT("iso8859");
289 info
->xencoding
.Printf(wxT("%d"), cp
);
293 case wxFONTENCODING_UTF8
:
294 info
->xregistry
= wxT("iso10646");
295 info
->xencoding
= wxT("*");
298 case wxFONTENCODING_KOI8
:
299 info
->xregistry
= wxT("koi8");
301 // we don't make distinction between koi8-r, koi8-u and koi8-ru (so far)
302 info
->xencoding
= wxT("*");
305 case wxFONTENCODING_CP1250
:
306 case wxFONTENCODING_CP1251
:
307 case wxFONTENCODING_CP1252
:
308 case wxFONTENCODING_CP1253
:
309 case wxFONTENCODING_CP1254
:
310 case wxFONTENCODING_CP1255
:
311 case wxFONTENCODING_CP1256
:
312 case wxFONTENCODING_CP1257
:
314 int cp
= encoding
- wxFONTENCODING_CP1250
+ 1250;
315 info
->xregistry
= wxT("microsoft");
316 info
->xencoding
.Printf(wxT("cp%d"), cp
);
320 case wxFONTENCODING_SYSTEM
:
322 info
->xencoding
= wxT("*");
326 // don't know how to translate this encoding into X fontspec
330 info
->encoding
= encoding
;
335 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
338 fontspec
.Printf(_T("-*-%s-*-*-*-*-*-*-*-*-*-*-%s-%s"),
339 !info
.facename
? _T("*") : info
.facename
.c_str(),
340 info
.xregistry
.c_str(),
341 info
.xencoding
.c_str());
343 return wxTestFontSpec(fontspec
);
346 // ----------------------------------------------------------------------------
347 // X-specific functions
348 // ----------------------------------------------------------------------------
350 wxNativeFont
wxLoadQueryNearestFont(int pointSize
,
355 const wxString
&facename
,
356 wxFontEncoding encoding
,
359 if ( encoding
== wxFONTENCODING_DEFAULT
)
361 encoding
= wxFont::GetDefaultEncoding();
364 // first determine the encoding - if the font doesn't exist at all in this
365 // encoding, it's useless to do all other approximations (i.e. size,
366 // family &c don't matter much)
367 wxNativeEncodingInfo info
;
368 if ( encoding
== wxFONTENCODING_SYSTEM
)
370 // This will always work so we don't test to save time
371 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
375 if ( !wxGetNativeFontEncoding(encoding
, &info
) ||
376 !wxTestFontEncoding(info
) )
379 if ( !wxTheFontMapper
->GetAltForEncoding(encoding
, &info
) )
380 #endif // wxUSE_FONTMAP
382 // unspported encoding - replace it with the default
384 // NB: we can't just return 0 from here because wxGTK code doesn't
385 // check for it (i.e. it supposes that we'll always succeed),
386 // so it would provoke a crash
387 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
392 // OK, we have the correct xregistry/xencoding in info structure
393 wxNativeFont font
= 0;
395 // if we already have the X font name, try to use it
396 if( xFontName
&& !xFontName
->IsEmpty() )
399 // Make sure point size is correct for scale factor.
401 wxStringTokenizer
tokenizer(*xFontName
, _T("-"), wxTOKEN_RET_DELIMS
);
402 wxString newFontName
;
404 for(int i
= 0; i
< 8; i
++)
405 newFontName
+= tokenizer
.NextToken();
407 (void) tokenizer
.NextToken();
409 newFontName
+= wxString::Format("%d-", pointSize
);
411 while(tokenizer
.HasMoreTokens())
412 newFontName
+= tokenizer
.GetNextToken();
414 font
= wxLoadFont(newFontName
);
417 *xFontName
= newFontName
;
420 // try to load exactly the font requested first
423 font
= wxLoadQueryFont( pointSize
, family
, style
, weight
,
424 underlined
, facename
,
425 info
.xregistry
, info
.xencoding
,
431 // search up and down by stepsize 10
432 int max_size
= pointSize
+ 20 * (1 + (pointSize
/180));
433 int min_size
= pointSize
- 20 * (1 + (pointSize
/180));
437 // Search for smaller size (approx.)
438 for ( i
= pointSize
- 10; !font
&& i
>= 10 && i
>= min_size
; i
-= 10 )
440 font
= wxLoadQueryFont(i
, family
, style
, weight
, underlined
,
441 facename
, info
.xregistry
, info
.xencoding
,
445 // Search for larger size (approx.)
446 for ( i
= pointSize
+ 10; !font
&& i
<= max_size
; i
+= 10 )
448 font
= wxLoadQueryFont(i
, family
, style
, weight
, underlined
,
449 facename
, info
.xregistry
, info
.xencoding
,
453 // Try default family
454 if ( !font
&& family
!= wxDEFAULT
)
456 font
= wxLoadQueryFont(pointSize
, wxDEFAULT
, style
, weight
,
457 underlined
, facename
,
458 info
.xregistry
, info
.xencoding
,
462 // ignore size, family, style and weight but try to find font with the
463 // given facename and encoding
466 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
467 underlined
, facename
,
468 info
.xregistry
, info
.xencoding
,
471 // ignore family as well
474 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
475 underlined
, wxEmptyString
,
476 info
.xregistry
, info
.xencoding
,
479 // if it still failed, try to get the font of any size but
480 // with the requested encoding: this can happen if the
481 // encoding is only available in one size which happens to be
482 // different from 120
485 font
= wxLoadQueryFont(-1, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
486 FALSE
, wxEmptyString
,
487 info
.xregistry
, info
.xencoding
,
490 // this should never happen as we had tested for it in the
491 // very beginning, but if it does, do return something non
492 // NULL or we'd crash in wxFont code
495 wxFAIL_MSG( _T("this encoding should be available!") );
497 font
= wxLoadQueryFont(-1,
498 wxDEFAULT
, wxNORMAL
, wxNORMAL
,
499 FALSE
, wxEmptyString
,
511 // ----------------------------------------------------------------------------
513 // ----------------------------------------------------------------------------
515 // returns TRUE if there are any fonts matching this font spec
516 static bool wxTestFontSpec(const wxString
& fontspec
)
518 // some X servers will fail to load this font because there are too many
519 // matches so we must test explicitly for this
520 if ( fontspec
== _T("-*-*-*-*-*-*-*-*-*-*-*-*-*-*") )
525 wxNativeFont test
= (wxNativeFont
) g_fontHash
->Get( fontspec
);
531 test
= wxLoadFont(fontspec
);
532 g_fontHash
->Put( fontspec
, (wxObject
*) test
);
546 static wxNativeFont
wxLoadQueryFont(int pointSize
,
550 bool WXUNUSED(underlined
),
551 const wxString
& facename
,
552 const wxString
& xregistry
,
553 const wxString
& xencoding
,
559 case wxDECORATIVE
: xfamily
= wxT("lucida"); break;
560 case wxROMAN
: xfamily
= wxT("times"); break;
561 case wxMODERN
: xfamily
= wxT("courier"); break;
562 case wxSWISS
: xfamily
= wxT("helvetica"); break;
563 case wxTELETYPE
: xfamily
= wxT("lucidatypewriter"); break;
564 case wxSCRIPT
: xfamily
= wxT("utopia"); break;
565 default: xfamily
= wxT("*");
569 if (!facename
.IsEmpty())
571 fontSpec
.Printf(wxT("-*-%s-*-*-normal-*-*-*-*-*-*-*-*-*"),
574 if ( wxTestFontSpec(fontSpec
) )
578 //else: no such family, use default one instead
585 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
587 if ( wxTestFontSpec(fontSpec
) )
592 // fall through - try wxITALIC now
595 fontSpec
.Printf(wxT("-*-%s-*-i-*-*-*-*-*-*-*-*-*-*"),
597 if ( wxTestFontSpec(fontSpec
) )
601 else if ( style
== wxITALIC
) // and not wxSLANT
604 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
606 if ( wxTestFontSpec(fontSpec
) )
612 // no italic, no slant - leave default
619 wxFAIL_MSG(_T("unknown font style"));
620 // fall back to normal
632 fontSpec
.Printf(wxT("-*-%s-bold-*-*-*-*-*-*-*-*-*-*-*"),
634 if ( wxTestFontSpec(fontSpec
) )
636 xweight
= wxT("bold");
639 fontSpec
.Printf(wxT("-*-%s-heavy-*-*-*-*-*-*-*-*-*-*-*"),
641 if ( wxTestFontSpec(fontSpec
) )
643 xweight
= wxT("heavy");
646 fontSpec
.Printf(wxT("-*-%s-extrabold-*-*-*-*-*-*-*-*-*-*-*"),
648 if ( wxTestFontSpec(fontSpec
) )
650 xweight
= wxT("extrabold");
653 fontSpec
.Printf(wxT("-*-%s-demibold-*-*-*-*-*-*-*-*-*-*-*"),
655 if ( wxTestFontSpec(fontSpec
) )
657 xweight
= wxT("demibold");
660 fontSpec
.Printf(wxT("-*-%s-black-*-*-*-*-*-*-*-*-*-*-*"),
662 if ( wxTestFontSpec(fontSpec
) )
664 xweight
= wxT("black");
667 fontSpec
.Printf(wxT("-*-%s-ultrablack-*-*-*-*-*-*-*-*-*-*-*"),
669 if ( wxTestFontSpec(fontSpec
) )
671 xweight
= wxT("ultrablack");
678 fontSpec
.Printf(wxT("-*-%s-light-*-*-*-*-*-*-*-*-*-*-*"),
680 if ( wxTestFontSpec(fontSpec
) )
682 xweight
= wxT("light");
685 fontSpec
.Printf(wxT("-*-%s-thin-*-*-*-*-*-*-*-*-*-*-*"),
687 if ( wxTestFontSpec(fontSpec
) )
689 xweight
= wxT("thin");
696 fontSpec
.Printf(wxT("-*-%s-medium-*-*-*-*-*-*-*-*-*-*-*"),
698 if ( wxTestFontSpec(fontSpec
) )
700 xweight
= wxT("medium");
703 fontSpec
.Printf(wxT("-*-%s-normal-*-*-*-*-*-*-*-*-*-*-*"),
705 if ( wxTestFontSpec(fontSpec
) )
707 xweight
= wxT("normal");
710 fontSpec
.Printf(wxT("-*-%s-regular-*-*-*-*-*-*-*-*-*-*-*"),
712 if ( wxTestFontSpec(fontSpec
) )
714 xweight
= wxT("regular");
720 default: xweight
= wxT("*"); break;
723 // if pointSize is -1, don't specify any
725 if ( fontSpec
== -1 )
731 sizeSpec
.Printf(_T("%d"), pointSize
);
734 // construct the X font spec from our data
735 fontSpec
.Printf(wxT("-*-%s-%s-%s-normal-*-*-%s-*-*-*-*-%s-%s"),
736 xfamily
.c_str(), xweight
.c_str(), xstyle
.c_str(),
737 sizeSpec
.c_str(), xregistry
.c_str(), xencoding
.c_str());
740 *xFontName
= fontSpec
;
742 return wxLoadFont(fontSpec
);
745 // ----------------------------------------------------------------------------
747 // ----------------------------------------------------------------------------
749 class wxFontModule
: public wxModule
756 DECLARE_DYNAMIC_CLASS(wxFontModule
)
759 IMPLEMENT_DYNAMIC_CLASS(wxFontModule
, wxModule
)
761 bool wxFontModule::OnInit()
763 g_fontHash
= new wxHashTable( wxKEY_STRING
);
768 void wxFontModule::OnExit()
772 g_fontHash
= (wxHashTable
*)NULL
;