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
);
100 // ============================================================================
102 // ============================================================================
104 // ----------------------------------------------------------------------------
105 // wxNativeEncodingInfo
106 // ----------------------------------------------------------------------------
108 // convert to/from the string representation: format is
109 // encodingid;registry;encoding[;facename]
110 bool wxNativeEncodingInfo::FromString(const wxString
& s
)
112 wxStringTokenizer
tokenizer(s
, _T(";"));
113 // cannot use "-" because it may be part of encoding name
115 wxString encid
= tokenizer
.GetNextToken();
117 if ( !encid
.ToLong(&enc
) )
119 encoding
= (wxFontEncoding
)enc
;
121 xregistry
= tokenizer
.GetNextToken();
125 xencoding
= tokenizer
.GetNextToken();
130 facename
= tokenizer
.GetNextToken();
135 wxString
wxNativeEncodingInfo::ToString() const
138 s
<< (long)encoding
<< _T(';') << xregistry
<< _T(';') << xencoding
;
141 s
<< _T(';') << facename
;
147 // ----------------------------------------------------------------------------
149 // ----------------------------------------------------------------------------
151 bool wxGetNativeFontEncoding(wxFontEncoding encoding
,
152 wxNativeEncodingInfo
*info
)
154 wxCHECK_MSG( info
, FALSE
, _T("bad pointer in wxGetNativeFontEncoding") );
156 if ( encoding
== wxFONTENCODING_DEFAULT
)
158 encoding
= wxFont::GetDefaultEncoding();
163 case wxFONTENCODING_ISO8859_1
:
164 case wxFONTENCODING_ISO8859_2
:
165 case wxFONTENCODING_ISO8859_3
:
166 case wxFONTENCODING_ISO8859_4
:
167 case wxFONTENCODING_ISO8859_5
:
168 case wxFONTENCODING_ISO8859_6
:
169 case wxFONTENCODING_ISO8859_7
:
170 case wxFONTENCODING_ISO8859_8
:
171 case wxFONTENCODING_ISO8859_9
:
172 case wxFONTENCODING_ISO8859_10
:
173 case wxFONTENCODING_ISO8859_11
:
174 case wxFONTENCODING_ISO8859_12
:
175 case wxFONTENCODING_ISO8859_13
:
176 case wxFONTENCODING_ISO8859_14
:
177 case wxFONTENCODING_ISO8859_15
:
179 int cp
= encoding
- wxFONTENCODING_ISO8859_1
+ 1;
180 info
->xregistry
= wxT("iso8859");
181 info
->xencoding
.Printf(wxT("%d"), cp
);
185 case wxFONTENCODING_KOI8
:
186 info
->xregistry
= wxT("koi8");
188 // we don't make distinction between koi8-r and koi8-u (so far)
189 info
->xencoding
= wxT("*");
192 case wxFONTENCODING_CP1250
:
193 case wxFONTENCODING_CP1251
:
194 case wxFONTENCODING_CP1252
:
195 case wxFONTENCODING_CP1253
:
196 case wxFONTENCODING_CP1254
:
197 case wxFONTENCODING_CP1255
:
198 case wxFONTENCODING_CP1256
:
199 case wxFONTENCODING_CP1257
:
201 int cp
= encoding
- wxFONTENCODING_CP1250
+ 1250;
202 info
->xregistry
= wxT("microsoft");
203 info
->xencoding
.Printf(wxT("cp%d"), cp
);
207 case wxFONTENCODING_SYSTEM
:
209 info
->xencoding
= wxT("*");
213 // don't know how to translate this encoding into X fontspec
217 info
->encoding
= encoding
;
222 bool wxTestFontEncoding(const wxNativeEncodingInfo
& info
)
225 fontspec
.Printf(_T("-*-%s-*-*-*-*-*-*-*-*-*-*-%s-%s"),
226 !info
.facename
? _T("*") : info
.facename
.c_str(),
227 info
.xregistry
.c_str(),
228 info
.xencoding
.c_str());
230 return wxTestFontSpec(fontspec
);
233 // ----------------------------------------------------------------------------
234 // X-specific functions
235 // ----------------------------------------------------------------------------
237 wxNativeFont
wxLoadQueryNearestFont(int pointSize
,
242 const wxString
&facename
,
243 wxFontEncoding encoding
)
245 if ( encoding
== wxFONTENCODING_DEFAULT
)
247 encoding
= wxFont::GetDefaultEncoding();
250 // first determine the encoding - if the font doesn't exist at all in this
251 // encoding, it's useless to do all other approximations (i.e. size,
252 // family &c don't matter much)
253 wxNativeEncodingInfo info
;
254 if ( encoding
== wxFONTENCODING_SYSTEM
)
256 // This will always work so we don't test to save time
257 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
261 if ( !wxGetNativeFontEncoding(encoding
, &info
) ||
262 !wxTestFontEncoding(info
) )
264 if ( !wxTheFontMapper
->GetAltForEncoding(encoding
, &info
) )
266 // unspported encoding - replace it with the default
268 // NB: we can't just return 0 from here because wxGTK code doesn't
269 // check for it (i.e. it supposes that we'll always succeed),
270 // so it would provoke a crash
271 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM
, &info
);
276 // OK, we have the correct xregistry/xencoding in info structure
277 wxNativeFont font
= wxLoadQueryFont( pointSize
, family
, style
, weight
,
278 underlined
, facename
,
279 info
.xregistry
, info
.xencoding
);
283 // search up and down by stepsize 10
284 int max_size
= pointSize
+ 20 * (1 + (pointSize
/180));
285 int min_size
= pointSize
- 20 * (1 + (pointSize
/180));
289 // Search for smaller size (approx.)
290 for ( i
= pointSize
- 10; !font
&& i
>= 10 && i
>= min_size
; i
-= 10 )
292 font
= wxLoadQueryFont(i
, family
, style
, weight
, underlined
,
293 facename
, info
.xregistry
, info
.xencoding
);
296 // Search for larger size (approx.)
297 for ( i
= pointSize
+ 10; !font
&& i
<= max_size
; i
+= 10 )
299 font
= wxLoadQueryFont(i
, family
, style
, weight
, underlined
,
300 facename
, info
.xregistry
, info
.xencoding
);
303 // Try default family
304 if ( !font
&& family
!= wxDEFAULT
)
306 font
= wxLoadQueryFont(pointSize
, wxDEFAULT
, style
, weight
,
307 underlined
, facename
,
308 info
.xregistry
, info
.xencoding
);
314 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
315 underlined
, facename
,
316 info
.xregistry
, info
.xencoding
);
322 font
= wxLoadQueryFont(120, wxDEFAULT
, wxNORMAL
, wxNORMAL
,
323 underlined
, wxEmptyString
,
324 info
.xregistry
, info
.xencoding
);
331 // ----------------------------------------------------------------------------
333 // ----------------------------------------------------------------------------
335 // returns TRUE if there are any fonts matching this font spec
336 static bool wxTestFontSpec(const wxString
& fontspec
)
338 // some X servers will fail to load this font because there are too many
339 // matches so we must test explicitly for this
340 if ( fontspec
== _T("-*-*-*-*-*-*-*-*-*-*-*-*-*-*") )
345 wxNativeFont test
= (wxNativeFont
) g_fontHash
->Get( fontspec
);
348 // printf( "speed up\n" );
352 test
= wxLoadFont(fontspec
);
353 g_fontHash
->Put( fontspec
, (wxObject
*) test
);
367 static wxNativeFont
wxLoadQueryFont(int pointSize
,
371 bool WXUNUSED(underlined
),
372 const wxString
& facename
,
373 const wxString
& xregistry
,
374 const wxString
& xencoding
)
379 case wxDECORATIVE
: xfamily
= wxT("lucida"); break;
380 case wxROMAN
: xfamily
= wxT("times"); break;
381 case wxMODERN
: xfamily
= wxT("courier"); break;
382 case wxSWISS
: xfamily
= wxT("helvetica"); break;
383 case wxTELETYPE
: xfamily
= wxT("lucidatypewriter"); break;
384 case wxSCRIPT
: xfamily
= wxT("utopia"); break;
385 default: xfamily
= wxT("*");
389 if (!facename
.IsEmpty())
391 fontSpec
.Printf(wxT("-*-%s-*-*-normal-*-*-*-*-*-*-*-*-*"),
394 if ( wxTestFontSpec(fontSpec
) )
398 //else: no such family, use default one instead
405 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
407 if ( wxTestFontSpec(fontSpec
) )
412 // fall through - try wxITALIC now
415 fontSpec
.Printf(wxT("-*-%s-*-i-*-*-*-*-*-*-*-*-*-*"),
417 if ( wxTestFontSpec(fontSpec
) )
421 else if ( style
== wxITALIC
) // and not wxSLANT
424 fontSpec
.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
426 if ( wxTestFontSpec(fontSpec
) )
432 // no italic, no slant - leave default
439 wxFAIL_MSG(_T("unknown font style"));
440 // fall back to normal
452 fontSpec
.Printf(wxT("-*-%s-bold-*-*-*-*-*-*-*-*-*-*-*"),
454 if ( wxTestFontSpec(fontSpec
) )
456 xweight
= wxT("bold");
459 fontSpec
.Printf(wxT("-*-%s-heavy-*-*-*-*-*-*-*-*-*-*-*"),
461 if ( wxTestFontSpec(fontSpec
) )
463 xweight
= wxT("heavy");
466 fontSpec
.Printf(wxT("-*-%s-extrabold-*-*-*-*-*-*-*-*-*-*-*"),
468 if ( wxTestFontSpec(fontSpec
) )
470 xweight
= wxT("extrabold");
473 fontSpec
.Printf(wxT("-*-%s-demibold-*-*-*-*-*-*-*-*-*-*-*"),
475 if ( wxTestFontSpec(fontSpec
) )
477 xweight
= wxT("demibold");
480 fontSpec
.Printf(wxT("-*-%s-black-*-*-*-*-*-*-*-*-*-*-*"),
482 if ( wxTestFontSpec(fontSpec
) )
484 xweight
= wxT("black");
487 fontSpec
.Printf(wxT("-*-%s-ultrablack-*-*-*-*-*-*-*-*-*-*-*"),
489 if ( wxTestFontSpec(fontSpec
) )
491 xweight
= wxT("ultrablack");
498 fontSpec
.Printf(wxT("-*-%s-light-*-*-*-*-*-*-*-*-*-*-*"),
500 if ( wxTestFontSpec(fontSpec
) )
502 xweight
= wxT("light");
505 fontSpec
.Printf(wxT("-*-%s-thin-*-*-*-*-*-*-*-*-*-*-*"),
507 if ( wxTestFontSpec(fontSpec
) )
509 xweight
= wxT("thin");
516 fontSpec
.Printf(wxT("-*-%s-medium-*-*-*-*-*-*-*-*-*-*-*"),
518 if ( wxTestFontSpec(fontSpec
) )
520 xweight
= wxT("medium");
523 fontSpec
.Printf(wxT("-*-%s-normal-*-*-*-*-*-*-*-*-*-*-*"),
525 if ( wxTestFontSpec(fontSpec
) )
527 xweight
= wxT("normal");
530 fontSpec
.Printf(wxT("-*-%s-regular-*-*-*-*-*-*-*-*-*-*-*"),
532 if ( wxTestFontSpec(fontSpec
) )
534 xweight
= wxT("regular");
540 default: xweight
= wxT("*"); break;
543 // construct the X font spec from our data
544 fontSpec
.Printf(wxT("-*-%s-%s-%s-normal-*-*-%d-*-*-*-*-%s-%s"),
545 xfamily
.c_str(), xweight
.c_str(), xstyle
.c_str(),
546 pointSize
, xregistry
.c_str(), xencoding
.c_str());
548 return wxLoadFont(fontSpec
);
551 // ----------------------------------------------------------------------------
553 // ----------------------------------------------------------------------------
555 class wxFontModule
: public wxModule
562 DECLARE_DYNAMIC_CLASS(wxFontModule
)
565 IMPLEMENT_DYNAMIC_CLASS(wxFontModule
, wxModule
)
567 bool wxFontModule::OnInit()
569 g_fontHash
= new wxHashTable( wxKEY_STRING
);
574 void wxFontModule::OnExit()
578 g_fontHash
= (wxHashTable
*)NULL
;