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
40 #pragma message enable nosimpint
43 #include "wx/utils.h" // for wxGetDisplay()
44 #elif defined(__WXGTK__)
48 #include "wx/fontutil.h"
49 #include "wx/fontmap.h"
50 #include "wx/tokenzr.h"
52 #include "wx/module.h"
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 static wxHashTable
*g_fontHash
= (wxHashTable
*) NULL
;
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
64 // define the functions to create and destroy native fonts for this toolkit
66 static inline wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
68 return XLoadQueryFont((Display
*)wxGetDisplay(), fontSpec
);
71 static inline void wxFreeFont(wxNativeFont font
)
73 XFreeFont((Display
*)wxGetDisplay(), (XFontStruct
*)font
);
75 #elif defined(__WXGTK__)
76 static inline wxNativeFont
wxLoadFont(const wxString
& fontSpec
)
78 return gdk_font_load( wxConvertWX2MB(fontSpec
) );
81 static inline void wxFreeFont(wxNativeFont font
)
86 #error "Unknown GUI toolkit"
89 static bool wxTestFontSpec(const wxString
& fontspec
);
91 static wxNativeFont
wxLoadQueryFont(int pointSize
,
96 const wxString
& facename
,
97 const wxString
& xregistry
,
98 const wxString
& xencoding
,
101 // ============================================================================
103 // ============================================================================
105 // ----------------------------------------------------------------------------
106 // wxNativeEncodingInfo
107 // ----------------------------------------------------------------------------
109 // convert to/from the string representation: format is
110 // encodingid;registry;encoding[;facename]
111 bool wxNativeEncodingInfo::FromString(const wxString
& s
)
113 // use ";", not "-" because it may be part of encoding name
114 wxStringTokenizer
tokenizer(s
, _T(";"));
116 wxString encid
= tokenizer
.GetNextToken();
118 if ( !encid
.ToLong(&enc
) )
120 encoding
= (wxFontEncoding
)enc
;
122 xregistry
= tokenizer
.GetNextToken();
126 xencoding
= tokenizer
.GetNextToken();
131 facename
= tokenizer
.GetNextToken();
136 wxString
wxNativeEncodingInfo::ToString() const
139 s
<< (long)encoding
<< _T(';') << xregistry
<< _T(';') << xencoding
;
142 s
<< _T(';') << facename
;
148 // ----------------------------------------------------------------------------
150 // ----------------------------------------------------------------------------
152 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
153 wxNativeEncodingInfo
*info
)
155 wxCHECK_MSG( info
, FALSE
, _T("bad pointer in wxGetNativeFontEncoding") );
157 if ( encoding
== wxFONTENCODING_DEFAULT
)
159 encoding
= wxFont::GetDefaultEncoding();
164 case wxFONTENCODING_ISO8859_1
:
165 case wxFONTENCODING_ISO8859_2
:
166 case wxFONTENCODING_ISO8859_3
:
167 case wxFONTENCODING_ISO8859_4
:
168 case wxFONTENCODING_ISO8859_5
:
169 case wxFONTENCODING_ISO8859_6
:
170 case wxFONTENCODING_ISO8859_7
:
171 case wxFONTENCODING_ISO8859_8
:
172 case wxFONTENCODING_ISO8859_9
:
173 case wxFONTENCODING_ISO8859_10
:
174 case wxFONTENCODING_ISO8859_11
:
175 case wxFONTENCODING_ISO8859_12
:
176 case wxFONTENCODING_ISO8859_13
:
177 case wxFONTENCODING_ISO8859_14
:
178 case wxFONTENCODING_ISO8859_15
:
180 int cp
= encoding
- wxFONTENCODING_ISO8859_1
+ 1;
181 info
->xregistry
= wxT("iso8859");
182 info
->xencoding
.Printf(wxT("%d"), cp
);
186 case wxFONTENCODING_KOI8
:
187 info
->xregistry
= wxT("koi8");
189 // we don't make distinction between koi8-r and koi8-u (so far)
190 info
->xencoding
= wxT("*");
193 case wxFONTENCODING_CP1250
:
194 case wxFONTENCODING_CP1251
:
195 case wxFONTENCODING_CP1252
:
196 case wxFONTENCODING_CP1253
:
197 case wxFONTENCODING_CP1254
:
198 case wxFONTENCODING_CP1255
:
199 case wxFONTENCODING_CP1256
:
200 case wxFONTENCODING_CP1257
:
202 int cp
= encoding
- wxFONTENCODING_CP1250
+ 1250;
203 info
->xregistry
= wxT("microsoft");
204 info
->xencoding
.Printf(wxT("cp%d"), cp
);
208 case wxFONTENCODING_SYSTEM
:
210 info
->xencoding
= wxT("*");
214 // don't know how to translate this encoding into X fontspec
218 info
->encoding
= encoding
;
223 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
226 fontspec
.Printf(_T("-*-%s-*-*-*-*-*-*-*-*-*-*-%s-%s"),
227 !info
.facename
? _T("*") : info
.facename
.c_str(),
228 info
.xregistry
.c_str(),
229 info
.xencoding
.c_str());
231 return wxTestFontSpec(fontspec
);
234 // ----------------------------------------------------------------------------
235 // X-specific functions
236 // ----------------------------------------------------------------------------
238 wxNativeFont
wxLoadQueryNearestFont(int pointSize
,
243 const wxString
&facename
,
244 wxFontEncoding encoding
,
247 if ( encoding
== wxFONTENCODING_DEFAULT
)
249 encoding
= wxFont::GetDefaultEncoding();
252 // first determine the encoding - if the font doesn't exist at all in this
253 // encoding, it's useless to do all other approximations (i.e. size,
254 // family &c don't matter much)
255 wxNativeEncodingInfo info
;
256 if ( encoding
== wxFONTENCODING_SYSTEM
)
258 // This will always work so we don't test to save time
259 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
263 if ( !wxGetNativeFontEncoding(encoding
, &info
) ||
264 !wxTestFontEncoding(info
) )
266 if ( !wxTheFontMapper
->GetAltForEncoding(encoding
, &info
) )
268 // unspported encoding - replace it with the default
270 // NB: we can't just return 0 from here because wxGTK code doesn't
271 // check for it (i.e. it supposes that we'll always succeed),
272 // so it would provoke a crash
273 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
278 // OK, we have the correct xregistry/xencoding in info structure
279 wxNativeFont font
= 0;
281 // if we already have the X font name, try to use it
282 if( xFontName
&& !xFontName
->IsEmpty() )
285 // Make sure point size is correct for scale factor.
287 wxStringTokenizer
tokenizer(*xFontName
, _T("-"), wxTOKEN_RET_DELIMS
);
288 wxString newFontName
;
290 for(int i
= 0; i
< 8; i
++)
291 newFontName
+= tokenizer
.NextToken();
293 (void) tokenizer
.NextToken();
295 newFontName
+= wxString::Format("%d-", pointSize
);
297 while(tokenizer
.HasMoreTokens())
298 newFontName
+= tokenizer
.GetNextToken();
300 font
= wxLoadFont(newFontName
);
303 *xFontName
= newFontName
;
307 font
= wxLoadQueryFont( pointSize
, family
, style
, weight
,
308 underlined
, facename
,
309 info
.xregistry
, info
.xencoding
,
314 // search up and down by stepsize 10
315 int max_size
= pointSize
+ 20 * (1 + (pointSize
/180));
316 int min_size
= pointSize
- 20 * (1 + (pointSize
/180));
320 // Search for smaller size (approx.)
321 for ( i
= pointSize
- 10; !font
&& i
>= 10 && i
>= min_size
; i
-= 10 )
323 font
= wxLoadQueryFont(i
, family
, style
, weight
, underlined
,
324 facename
, info
.xregistry
, info
.xencoding
,
328 // Search for larger size (approx.)
329 for ( i
= pointSize
+ 10; !font
&& i
<= max_size
; i
+= 10 )
331 font
= wxLoadQueryFont(i
, family
, style
, weight
, underlined
,
332 facename
, info
.xregistry
, info
.xencoding
,
336 // Try default family
337 if ( !font
&& family
!= wxDEFAULT
)
339 font
= wxLoadQueryFont(pointSize
, wxDEFAULT
, style
, weight
,
340 underlined
, facename
,
341 info
.xregistry
, info
.xencoding
,
348 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
349 underlined
, facename
,
350 info
.xregistry
, info
.xencoding
,
357 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
358 underlined
, wxEmptyString
,
359 info
.xregistry
, info
.xencoding
,
367 // ----------------------------------------------------------------------------
369 // ----------------------------------------------------------------------------
371 // returns TRUE if there are any fonts matching this font spec
372 static bool wxTestFontSpec(const wxString
& fontspec
)
374 // some X servers will fail to load this font because there are too many
375 // matches so we must test explicitly for this
376 if ( fontspec
== _T("-*-*-*-*-*-*-*-*-*-*-*-*-*-*") )
381 wxNativeFont test
= (wxNativeFont
) g_fontHash
->Get( fontspec
);
387 test
= wxLoadFont(fontspec
);
388 g_fontHash
->Put( fontspec
, (wxObject
*) test
);
402 static wxNativeFont
wxLoadQueryFont(int pointSize
,
406 bool WXUNUSED(underlined
),
407 const wxString
& facename
,
408 const wxString
& xregistry
,
409 const wxString
& xencoding
,
415 case wxDECORATIVE
: xfamily
= wxT("lucida"); break;
416 case wxROMAN
: xfamily
= wxT("times"); break;
417 case wxMODERN
: xfamily
= wxT("courier"); break;
418 case wxSWISS
: xfamily
= wxT("helvetica"); break;
419 case wxTELETYPE
: xfamily
= wxT("lucidatypewriter"); break;
420 case wxSCRIPT
: xfamily
= wxT("utopia"); break;
421 default: xfamily
= wxT("*");
425 if (!facename
.IsEmpty())
427 fontSpec
.Printf(wxT("-*-%s-*-*-normal-*-*-*-*-*-*-*-*-*"),
430 if ( wxTestFontSpec(fontSpec
) )
434 //else: no such family, use default one instead
441 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
443 if ( wxTestFontSpec(fontSpec
) )
448 // fall through - try wxITALIC now
451 fontSpec
.Printf(wxT("-*-%s-*-i-*-*-*-*-*-*-*-*-*-*"),
453 if ( wxTestFontSpec(fontSpec
) )
457 else if ( style
== wxITALIC
) // and not wxSLANT
460 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
462 if ( wxTestFontSpec(fontSpec
) )
468 // no italic, no slant - leave default
475 wxFAIL_MSG(_T("unknown font style"));
476 // fall back to normal
488 fontSpec
.Printf(wxT("-*-%s-bold-*-*-*-*-*-*-*-*-*-*-*"),
490 if ( wxTestFontSpec(fontSpec
) )
492 xweight
= wxT("bold");
495 fontSpec
.Printf(wxT("-*-%s-heavy-*-*-*-*-*-*-*-*-*-*-*"),
497 if ( wxTestFontSpec(fontSpec
) )
499 xweight
= wxT("heavy");
502 fontSpec
.Printf(wxT("-*-%s-extrabold-*-*-*-*-*-*-*-*-*-*-*"),
504 if ( wxTestFontSpec(fontSpec
) )
506 xweight
= wxT("extrabold");
509 fontSpec
.Printf(wxT("-*-%s-demibold-*-*-*-*-*-*-*-*-*-*-*"),
511 if ( wxTestFontSpec(fontSpec
) )
513 xweight
= wxT("demibold");
516 fontSpec
.Printf(wxT("-*-%s-black-*-*-*-*-*-*-*-*-*-*-*"),
518 if ( wxTestFontSpec(fontSpec
) )
520 xweight
= wxT("black");
523 fontSpec
.Printf(wxT("-*-%s-ultrablack-*-*-*-*-*-*-*-*-*-*-*"),
525 if ( wxTestFontSpec(fontSpec
) )
527 xweight
= wxT("ultrablack");
534 fontSpec
.Printf(wxT("-*-%s-light-*-*-*-*-*-*-*-*-*-*-*"),
536 if ( wxTestFontSpec(fontSpec
) )
538 xweight
= wxT("light");
541 fontSpec
.Printf(wxT("-*-%s-thin-*-*-*-*-*-*-*-*-*-*-*"),
543 if ( wxTestFontSpec(fontSpec
) )
545 xweight
= wxT("thin");
552 fontSpec
.Printf(wxT("-*-%s-medium-*-*-*-*-*-*-*-*-*-*-*"),
554 if ( wxTestFontSpec(fontSpec
) )
556 xweight
= wxT("medium");
559 fontSpec
.Printf(wxT("-*-%s-normal-*-*-*-*-*-*-*-*-*-*-*"),
561 if ( wxTestFontSpec(fontSpec
) )
563 xweight
= wxT("normal");
566 fontSpec
.Printf(wxT("-*-%s-regular-*-*-*-*-*-*-*-*-*-*-*"),
568 if ( wxTestFontSpec(fontSpec
) )
570 xweight
= wxT("regular");
576 default: xweight
= wxT("*"); break;
579 // construct the X font spec from our data
580 fontSpec
.Printf(wxT("-*-%s-%s-%s-normal-*-*-%d-*-*-*-*-%s-%s"),
581 xfamily
.c_str(), xweight
.c_str(), xstyle
.c_str(),
582 pointSize
, xregistry
.c_str(), xencoding
.c_str());
585 *xFontName
= fontSpec
;
587 return wxLoadFont(fontSpec
);
590 // ----------------------------------------------------------------------------
592 // ----------------------------------------------------------------------------
594 class wxFontModule
: public wxModule
601 DECLARE_DYNAMIC_CLASS(wxFontModule
)
604 IMPLEMENT_DYNAMIC_CLASS(wxFontModule
, wxModule
)
606 bool wxFontModule::OnInit()
608 g_fontHash
= new wxHashTable( wxKEY_STRING
);
613 void wxFontModule::OnExit()
617 g_fontHash
= (wxHashTable
*)NULL
;