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
) )
282 if ( !wxTheFontMapper
->GetAltForEncoding(encoding
, &info
) )
283 #endif // wxUSE_FONTMAP
285 // unspported encoding - replace it with the default
287 // NB: we can't just return 0 from here because wxGTK code doesn't
288 // check for it (i.e. it supposes that we'll always succeed),
289 // so it would provoke a crash
290 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
295 // OK, we have the correct xregistry/xencoding in info structure
296 wxNativeFont font
= 0;
298 // if we already have the X font name, try to use it
299 if( xFontName
&& !xFontName
->IsEmpty() )
302 // Make sure point size is correct for scale factor.
304 wxStringTokenizer
tokenizer(*xFontName
, _T("-"), wxTOKEN_RET_DELIMS
);
305 wxString newFontName
;
307 for(int i
= 0; i
< 8; i
++)
308 newFontName
+= tokenizer
.NextToken();
310 (void) tokenizer
.NextToken();
312 newFontName
+= wxString::Format("%d-", pointSize
);
314 while(tokenizer
.HasMoreTokens())
315 newFontName
+= tokenizer
.GetNextToken();
317 font
= wxLoadFont(newFontName
);
320 *xFontName
= newFontName
;
323 // try to load exactly the font requested first
326 font
= wxLoadQueryFont( pointSize
, family
, style
, weight
,
327 underlined
, facename
,
328 info
.xregistry
, info
.xencoding
,
334 // search up and down by stepsize 10
335 int max_size
= pointSize
+ 20 * (1 + (pointSize
/180));
336 int min_size
= pointSize
- 20 * (1 + (pointSize
/180));
340 // Search for smaller size (approx.)
341 for ( i
= pointSize
- 10; !font
&& i
>= 10 && i
>= min_size
; i
-= 10 )
343 font
= wxLoadQueryFont(i
, family
, style
, weight
, underlined
,
344 facename
, info
.xregistry
, info
.xencoding
,
348 // Search for larger size (approx.)
349 for ( i
= pointSize
+ 10; !font
&& i
<= max_size
; i
+= 10 )
351 font
= wxLoadQueryFont(i
, family
, style
, weight
, underlined
,
352 facename
, info
.xregistry
, info
.xencoding
,
356 // Try default family
357 if ( !font
&& family
!= wxDEFAULT
)
359 font
= wxLoadQueryFont(pointSize
, wxDEFAULT
, style
, weight
,
360 underlined
, facename
,
361 info
.xregistry
, info
.xencoding
,
365 // ignore size, family, style and weight but try to find font with the
366 // given facename and encoding
369 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
370 underlined
, facename
,
371 info
.xregistry
, info
.xencoding
,
374 // ignore family as well
377 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
378 underlined
, wxEmptyString
,
379 info
.xregistry
, info
.xencoding
,
382 // if it still failed, try to get the font of any size but
383 // with the requested encoding: this can happen if the
384 // encoding is only available in one size which happens to be
385 // different from 120
388 font
= wxLoadQueryFont(-1, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
389 FALSE
, wxEmptyString
,
390 info
.xregistry
, info
.xencoding
,
393 // this should never happen as we had tested for it in the
394 // very beginning, but if it does, do return something non
395 // NULL or we'd crash in wxFont code
398 wxFAIL_MSG( _T("this encoding should be available!") );
400 font
= wxLoadQueryFont(-1,
401 wxDEFAULT
, wxNORMAL
, wxNORMAL
,
402 FALSE
, wxEmptyString
,
414 // ----------------------------------------------------------------------------
416 // ----------------------------------------------------------------------------
418 // returns TRUE if there are any fonts matching this font spec
419 static bool wxTestFontSpec(const wxString
& fontspec
)
421 // some X servers will fail to load this font because there are too many
422 // matches so we must test explicitly for this
423 if ( fontspec
== _T("-*-*-*-*-*-*-*-*-*-*-*-*-*-*") )
428 wxNativeFont test
= (wxNativeFont
) g_fontHash
->Get( fontspec
);
434 test
= wxLoadFont(fontspec
);
435 g_fontHash
->Put( fontspec
, (wxObject
*) test
);
449 static wxNativeFont
wxLoadQueryFont(int pointSize
,
453 bool WXUNUSED(underlined
),
454 const wxString
& facename
,
455 const wxString
& xregistry
,
456 const wxString
& xencoding
,
462 case wxDECORATIVE
: xfamily
= wxT("lucida"); break;
463 case wxROMAN
: xfamily
= wxT("times"); break;
464 case wxMODERN
: xfamily
= wxT("courier"); break;
465 case wxSWISS
: xfamily
= wxT("helvetica"); break;
466 case wxTELETYPE
: xfamily
= wxT("lucidatypewriter"); break;
467 case wxSCRIPT
: xfamily
= wxT("utopia"); break;
468 default: xfamily
= wxT("*");
472 if (!facename
.IsEmpty())
474 fontSpec
.Printf(wxT("-*-%s-*-*-normal-*-*-*-*-*-*-*-*-*"),
477 if ( wxTestFontSpec(fontSpec
) )
481 //else: no such family, use default one instead
488 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
490 if ( wxTestFontSpec(fontSpec
) )
495 // fall through - try wxITALIC now
498 fontSpec
.Printf(wxT("-*-%s-*-i-*-*-*-*-*-*-*-*-*-*"),
500 if ( wxTestFontSpec(fontSpec
) )
504 else if ( style
== wxITALIC
) // and not wxSLANT
507 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
509 if ( wxTestFontSpec(fontSpec
) )
515 // no italic, no slant - leave default
522 wxFAIL_MSG(_T("unknown font style"));
523 // fall back to normal
535 fontSpec
.Printf(wxT("-*-%s-bold-*-*-*-*-*-*-*-*-*-*-*"),
537 if ( wxTestFontSpec(fontSpec
) )
539 xweight
= wxT("bold");
542 fontSpec
.Printf(wxT("-*-%s-heavy-*-*-*-*-*-*-*-*-*-*-*"),
544 if ( wxTestFontSpec(fontSpec
) )
546 xweight
= wxT("heavy");
549 fontSpec
.Printf(wxT("-*-%s-extrabold-*-*-*-*-*-*-*-*-*-*-*"),
551 if ( wxTestFontSpec(fontSpec
) )
553 xweight
= wxT("extrabold");
556 fontSpec
.Printf(wxT("-*-%s-demibold-*-*-*-*-*-*-*-*-*-*-*"),
558 if ( wxTestFontSpec(fontSpec
) )
560 xweight
= wxT("demibold");
563 fontSpec
.Printf(wxT("-*-%s-black-*-*-*-*-*-*-*-*-*-*-*"),
565 if ( wxTestFontSpec(fontSpec
) )
567 xweight
= wxT("black");
570 fontSpec
.Printf(wxT("-*-%s-ultrablack-*-*-*-*-*-*-*-*-*-*-*"),
572 if ( wxTestFontSpec(fontSpec
) )
574 xweight
= wxT("ultrablack");
581 fontSpec
.Printf(wxT("-*-%s-light-*-*-*-*-*-*-*-*-*-*-*"),
583 if ( wxTestFontSpec(fontSpec
) )
585 xweight
= wxT("light");
588 fontSpec
.Printf(wxT("-*-%s-thin-*-*-*-*-*-*-*-*-*-*-*"),
590 if ( wxTestFontSpec(fontSpec
) )
592 xweight
= wxT("thin");
599 fontSpec
.Printf(wxT("-*-%s-medium-*-*-*-*-*-*-*-*-*-*-*"),
601 if ( wxTestFontSpec(fontSpec
) )
603 xweight
= wxT("medium");
606 fontSpec
.Printf(wxT("-*-%s-normal-*-*-*-*-*-*-*-*-*-*-*"),
608 if ( wxTestFontSpec(fontSpec
) )
610 xweight
= wxT("normal");
613 fontSpec
.Printf(wxT("-*-%s-regular-*-*-*-*-*-*-*-*-*-*-*"),
615 if ( wxTestFontSpec(fontSpec
) )
617 xweight
= wxT("regular");
623 default: xweight
= wxT("*"); break;
626 // if pointSize is -1, don't specify any
628 if ( fontSpec
== -1 )
634 sizeSpec
.Printf(_T("%d"), pointSize
);
637 // construct the X font spec from our data
638 fontSpec
.Printf(wxT("-*-%s-%s-%s-normal-*-*-%s-*-*-*-*-%s-%s"),
639 xfamily
.c_str(), xweight
.c_str(), xstyle
.c_str(),
640 sizeSpec
.c_str(), xregistry
.c_str(), xencoding
.c_str());
643 *xFontName
= fontSpec
;
645 return wxLoadFont(fontSpec
);
648 // ----------------------------------------------------------------------------
650 // ----------------------------------------------------------------------------
652 class wxFontModule
: public wxModule
659 DECLARE_DYNAMIC_CLASS(wxFontModule
)
662 IMPLEMENT_DYNAMIC_CLASS(wxFontModule
, wxModule
)
664 bool wxFontModule::OnInit()
666 g_fontHash
= new wxHashTable( wxKEY_STRING
);
671 void wxFontModule::OnExit()
675 g_fontHash
= (wxHashTable
*)NULL
;