]> git.saurik.com Git - wxWidgets.git/blob - src/unix/fontutil.cpp
Applied patch [ 1207531 ] Fix for wxCalendarCtrl being UNUSABLE with some languages
[wxWidgets.git] / src / unix / fontutil.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: unix/fontutil.cpp
3 // Purpose: Font helper functions for X11 (GDK/X)
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 05.11.99
7 // RCS-ID: $Id$
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "fontutil.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include "wx/font.h" // wxFont enums
33 #include "wx/encinfo.h"
34 #endif // PCH
35
36 #include "wx/fontutil.h"
37 #include "wx/fontmap.h"
38 #include "wx/tokenzr.h"
39 #include "wx/hash.h"
40 #include "wx/module.h"
41
42 #if wxUSE_PANGO
43
44 #include "pango/pango.h"
45
46 #ifdef __WXGTK20__
47 #include "wx/gtk/private.h"
48 #else
49 #include "wx/x11/private.h"
50 #endif
51
52 // ----------------------------------------------------------------------------
53 // wxNativeFontInfo
54 // ----------------------------------------------------------------------------
55
56 void wxNativeFontInfo::Init()
57 {
58 description = NULL;
59 }
60
61 void
62 wxNativeFontInfo::Init(const wxNativeFontInfo& info)
63 {
64 if (info.description)
65 description = pango_font_description_copy(info.description);
66 else
67 description = NULL;
68 }
69
70 void wxNativeFontInfo::Free()
71 {
72 if (description)
73 pango_font_description_free(description);
74 }
75
76 int wxNativeFontInfo::GetPointSize() const
77 {
78 return pango_font_description_get_size( description ) / PANGO_SCALE;
79 }
80
81 wxFontStyle wxNativeFontInfo::GetStyle() const
82 {
83 wxFontStyle m_style = wxFONTSTYLE_NORMAL;
84
85 switch (pango_font_description_get_style( description ))
86 {
87 case PANGO_STYLE_NORMAL:
88 m_style = wxFONTSTYLE_NORMAL;
89 break;
90 case PANGO_STYLE_ITALIC:
91 m_style = wxFONTSTYLE_ITALIC;
92 break;
93 case PANGO_STYLE_OBLIQUE:
94 m_style = wxFONTSTYLE_SLANT;
95 break;
96 }
97
98 return m_style;
99 }
100
101 wxFontWeight wxNativeFontInfo::GetWeight() const
102 {
103 #if 0
104 // We seem to currently initialize only by string.
105 // In that case PANGO_FONT_MASK_WEIGHT is always set.
106 if (!(pango_font_description_get_set_fields(description) & PANGO_FONT_MASK_WEIGHT))
107 return wxFONTWEIGHT_NORMAL;
108 #endif
109
110 PangoWeight pango_weight = pango_font_description_get_weight( description );
111
112 // Until the API can be changed the following ranges of weight values are used:
113 // wxFONTWEIGHT_LIGHT: 100 .. 349 - range of 250
114 // wxFONTWEIGHT_NORMAL: 350 .. 599 - range of 250
115 // wxFONTWEIGHT_BOLD: 600 .. 900 - range of 301 (600 is "semibold" already)
116
117 if (pango_weight >= 600)
118 return wxFONTWEIGHT_BOLD;
119
120 if (pango_weight < 350)
121 return wxFONTWEIGHT_LIGHT;
122
123 return wxFONTWEIGHT_NORMAL;
124 }
125
126 bool wxNativeFontInfo::GetUnderlined() const
127 {
128 return FALSE;
129 }
130
131 wxString wxNativeFontInfo::GetFaceName() const
132 {
133 wxString tmp = wxGTK_CONV_BACK( pango_font_description_get_family( description ) );
134
135 return tmp;
136 }
137
138 wxFontFamily wxNativeFontInfo::GetFamily() const
139 {
140 return wxFONTFAMILY_SWISS;
141 }
142
143 wxFontEncoding wxNativeFontInfo::GetEncoding() const
144 {
145 return wxFONTENCODING_SYSTEM;
146 }
147
148
149 void wxNativeFontInfo::SetPointSize(int WXUNUSED(pointsize))
150 {
151 wxFAIL_MSG( _T("not implemented") );
152 }
153
154 void wxNativeFontInfo::SetStyle(wxFontStyle WXUNUSED(style))
155 {
156 wxFAIL_MSG( _T("not implemented") );
157 }
158
159 void wxNativeFontInfo::SetWeight(wxFontWeight WXUNUSED(weight))
160 {
161 wxFAIL_MSG( _T("not implemented") );
162 }
163
164 void wxNativeFontInfo::SetUnderlined(bool WXUNUSED(underlined))
165 {
166 wxFAIL_MSG( _T("not implemented") );
167 }
168
169 void wxNativeFontInfo::SetFaceName(wxString WXUNUSED(facename))
170 {
171 wxFAIL_MSG( _T("not implemented") );
172 }
173
174 void wxNativeFontInfo::SetFamily(wxFontFamily WXUNUSED(family))
175 {
176 wxFAIL_MSG( _T("not implemented") );
177 }
178
179 void wxNativeFontInfo::SetEncoding(wxFontEncoding WXUNUSED(encoding))
180 {
181 wxFAIL_MSG( _T("not implemented") );
182 }
183
184
185
186 bool wxNativeFontInfo::FromString(const wxString& s)
187 {
188 if (description)
189 pango_font_description_free( description );
190
191 description = pango_font_description_from_string( wxGTK_CONV( s ) );
192
193 return TRUE;
194 }
195
196 wxString wxNativeFontInfo::ToString() const
197 {
198 char *str = pango_font_description_to_string( description );
199 wxString tmp = wxGTK_CONV_BACK( str );
200 g_free( str );
201
202 return tmp;
203 }
204
205 bool wxNativeFontInfo::FromUserString(const wxString& s)
206 {
207 return FromString( s );
208 }
209
210 wxString wxNativeFontInfo::ToUserString() const
211 {
212 return ToString();
213 }
214
215 // ----------------------------------------------------------------------------
216 // wxNativeEncodingInfo
217 // ----------------------------------------------------------------------------
218
219 bool wxNativeEncodingInfo::FromString(const wxString& s)
220 {
221 return FALSE;
222 }
223
224 wxString wxNativeEncodingInfo::ToString() const
225 {
226 return wxEmptyString;
227 }
228
229 bool wxTestFontEncoding(const wxNativeEncodingInfo& info)
230 {
231 return TRUE;
232 }
233
234 bool wxGetNativeFontEncoding(wxFontEncoding encoding,
235 wxNativeEncodingInfo *info)
236 {
237 // we *must* return true for default encoding as otherwise wxFontMapper
238 // considers that we can't load any font and aborts with wxLogFatalError!
239 if ( encoding == wxFONTENCODING_SYSTEM )
240 {
241 info->facename.clear();
242 info->encoding = wxFONTENCODING_SYSTEM;
243 }
244
245 // pretend that we support everything, it's better than to always return
246 // false as the old code did
247 return true;
248 }
249
250 #else // GTK+ 1.x
251
252 #ifdef __X__
253 #ifdef __VMS__
254 #pragma message disable nosimpint
255 #endif
256
257 #include <X11/Xlib.h>
258
259 #ifdef __VMS__
260 #pragma message enable nosimpint
261 #endif
262
263 #include "wx/utils.h" // for wxGetDisplay()
264 #elif defined(__WXGTK__)
265 // we have to declare struct tm to avoid problems with first forward
266 // declaring it in C code (glib.h included from gdk.h does it) and then
267 // defining it when time.h is included from the headers below - this is
268 // known not to work at least with Sun CC 6.01
269 #include <time.h>
270
271 #include <gdk/gdk.h>
272 #endif
273
274
275 // ----------------------------------------------------------------------------
276 // private data
277 // ----------------------------------------------------------------------------
278
279 static wxHashTable *g_fontHash = (wxHashTable*) NULL;
280
281 // ----------------------------------------------------------------------------
282 // private functions
283 // ----------------------------------------------------------------------------
284
285 // define the functions to create and destroy native fonts for this toolkit
286 #ifdef __X__
287 wxNativeFont wxLoadFont(const wxString& fontSpec)
288 {
289 return XLoadQueryFont((Display *)wxGetDisplay(), fontSpec);
290 }
291
292 inline void wxFreeFont(wxNativeFont font)
293 {
294 XFreeFont((Display *)wxGetDisplay(), (XFontStruct *)font);
295 }
296 #elif defined(__WXGTK__)
297 wxNativeFont wxLoadFont(const wxString& fontSpec)
298 {
299 // VZ: we should use gdk_fontset_load() instead of gdk_font_load()
300 // here to be able to display Japanese fonts correctly (at least
301 // this is what people report) but unfortunately doing it results
302 // in tons of warnings when using GTK with "normal" European
303 // languages and so we can't always do it and I don't know enough
304 // to determine when should this be done... (FIXME)
305 return gdk_font_load( wxConvertWX2MB(fontSpec) );
306 }
307
308 inline void wxFreeFont(wxNativeFont font)
309 {
310 gdk_font_unref(font);
311 }
312 #else
313 #error "Unknown GUI toolkit"
314 #endif
315
316 static bool wxTestFontSpec(const wxString& fontspec);
317
318 static wxNativeFont wxLoadQueryFont(int pointSize,
319 int family,
320 int style,
321 int weight,
322 bool underlined,
323 const wxString& facename,
324 const wxString& xregistry,
325 const wxString& xencoding,
326 wxString* xFontName);
327
328 // ============================================================================
329 // implementation
330 // ============================================================================
331
332 // ----------------------------------------------------------------------------
333 // wxNativeEncodingInfo
334 // ----------------------------------------------------------------------------
335
336 // convert to/from the string representation: format is
337 // encodingid;registry;encoding[;facename]
338 bool wxNativeEncodingInfo::FromString(const wxString& s)
339 {
340 // use ";", not "-" because it may be part of encoding name
341 wxStringTokenizer tokenizer(s, _T(";"));
342
343 wxString encid = tokenizer.GetNextToken();
344 long enc;
345 if ( !encid.ToLong(&enc) )
346 return FALSE;
347 encoding = (wxFontEncoding)enc;
348
349 xregistry = tokenizer.GetNextToken();
350 if ( !xregistry )
351 return FALSE;
352
353 xencoding = tokenizer.GetNextToken();
354 if ( !xencoding )
355 return FALSE;
356
357 // ok even if empty
358 facename = tokenizer.GetNextToken();
359
360 return TRUE;
361 }
362
363 wxString wxNativeEncodingInfo::ToString() const
364 {
365 wxString s;
366 s << (long)encoding << _T(';') << xregistry << _T(';') << xencoding;
367 if ( !!facename )
368 {
369 s << _T(';') << facename;
370 }
371
372 return s;
373 }
374
375 // ----------------------------------------------------------------------------
376 // wxNativeFontInfo
377 // ----------------------------------------------------------------------------
378
379 void wxNativeFontInfo::Init()
380 {
381 m_isDefault = TRUE;
382 }
383
384 bool wxNativeFontInfo::FromString(const wxString& s)
385 {
386 wxStringTokenizer tokenizer(s, _T(";"));
387
388 // check the version
389 wxString token = tokenizer.GetNextToken();
390 if ( token != _T('0') )
391 return FALSE;
392
393 xFontName = tokenizer.GetNextToken();
394
395 // this should be the end
396 if ( tokenizer.HasMoreTokens() )
397 return FALSE;
398
399 return FromXFontName(xFontName);
400 }
401
402 wxString wxNativeFontInfo::ToString() const
403 {
404 // 0 is the version
405 return wxString::Format(_T("%d;%s"), 0, GetXFontName().c_str());
406 }
407
408 bool wxNativeFontInfo::FromUserString(const wxString& s)
409 {
410 return FromXFontName(s);
411 }
412
413 wxString wxNativeFontInfo::ToUserString() const
414 {
415 return GetXFontName();
416 }
417
418 bool wxNativeFontInfo::HasElements() const
419 {
420 // we suppose that the foundry is never empty, so if it is it means that we
421 // had never parsed the XLFD
422 return !fontElements[0].empty();
423 }
424
425 wxString wxNativeFontInfo::GetXFontComponent(wxXLFDField field) const
426 {
427 wxCHECK_MSG( field < wxXLFD_MAX, _T(""), _T("invalid XLFD field") );
428
429 if ( !HasElements() )
430 {
431 // const_cast
432 if ( !((wxNativeFontInfo *)this)->FromXFontName(xFontName) )
433 return _T("");
434 }
435
436 return fontElements[field];
437 }
438
439 bool wxNativeFontInfo::FromXFontName(const wxString& fontname)
440 {
441 // TODO: we should be able to handle the font aliases here, but how?
442 wxStringTokenizer tokenizer(fontname, _T("-"));
443
444 // skip the leading, usually empty field (font name registry)
445 if ( !tokenizer.HasMoreTokens() )
446 return FALSE;
447
448 (void)tokenizer.GetNextToken();
449
450 for ( size_t n = 0; n < WXSIZEOF(fontElements); n++ )
451 {
452 if ( !tokenizer.HasMoreTokens() )
453 {
454 // not enough elements in the XLFD - or maybe an alias
455 return FALSE;
456 }
457
458 wxString field = tokenizer.GetNextToken();
459 if ( !field.empty() && field != _T('*') )
460 {
461 // we're really initialized now
462 m_isDefault = FALSE;
463 }
464
465 fontElements[n] = field;
466 }
467
468 // this should be all
469 if ( tokenizer.HasMoreTokens() )
470 return FALSE;
471
472 return TRUE;
473 }
474
475 wxString wxNativeFontInfo::GetXFontName() const
476 {
477 if ( xFontName.empty() )
478 {
479 for ( size_t n = 0; n < WXSIZEOF(fontElements); n++ )
480 {
481 // replace the non specified elements with '*' except for the
482 // additional style which is usually just omitted
483 wxString elt = fontElements[n];
484 if ( elt.empty() && n != wxXLFD_ADDSTYLE )
485 {
486 elt = _T('*');
487 }
488
489 // const_cast
490 ((wxNativeFontInfo *)this)->xFontName << _T('-') << elt;
491 }
492 }
493
494 return xFontName;
495 }
496
497 void
498 wxNativeFontInfo::SetXFontComponent(wxXLFDField field, const wxString& value)
499 {
500 wxCHECK_RET( field < wxXLFD_MAX, _T("invalid XLFD field") );
501
502 // this class should be initialized with a valid font spec first and only
503 // then the fields may be modified!
504 wxASSERT_MSG( !IsDefault(), _T("can't modify an uninitialized XLFD") );
505
506 if ( !HasElements() )
507 {
508 // const_cast
509 if ( !((wxNativeFontInfo *)this)->FromXFontName(xFontName) )
510 {
511 wxFAIL_MSG( _T("can't set font element for invalid XLFD") );
512
513 return;
514 }
515 }
516
517 fontElements[field] = value;
518
519 // invalidate the XFLD, it doesn't correspond to the font elements any more
520 xFontName.clear();
521 }
522
523 void wxNativeFontInfo::SetXFontName(const wxString& xFontName_)
524 {
525 // invalidate the font elements, GetXFontComponent() will reparse the XLFD
526 fontElements[0].clear();
527
528 xFontName = xFontName_;
529
530 m_isDefault = FALSE;
531 }
532
533 int wxNativeFontInfo::GetPointSize() const
534 {
535 const wxString s = GetXFontComponent(wxXLFD_POINTSIZE);
536
537 // return -1 to indicate that the size is unknown
538 long l;
539 return s.ToLong(&l) ? l : -1;
540 }
541
542 wxFontStyle wxNativeFontInfo::GetStyle() const
543 {
544 const wxString s = GetXFontComponent(wxXLFD_SLANT);
545
546 if ( s.length() != 1 )
547 {
548 // it is really unknown but we don't have any way to return it from
549 // here
550 return wxFONTSTYLE_NORMAL;
551 }
552
553 switch ( s[0] )
554 {
555 default:
556 // again, unknown but consider normal by default
557
558 case _T('r'):
559 return wxFONTSTYLE_NORMAL;
560
561 case _T('i'):
562 return wxFONTSTYLE_ITALIC;
563
564 case _T('o'):
565 return wxFONTSTYLE_SLANT;
566 }
567 }
568
569 wxFontWeight wxNativeFontInfo::GetWeight() const
570 {
571 const wxString s = GetXFontComponent(wxXLFD_WEIGHT).MakeLower();
572 if ( s.find(_T("bold")) != wxString::npos || s == _T("black") )
573 return wxFONTWEIGHT_BOLD;
574 else if ( s == _T("light") )
575 return wxFONTWEIGHT_LIGHT;
576
577 return wxFONTWEIGHT_NORMAL;
578 }
579
580 bool wxNativeFontInfo::GetUnderlined() const
581 {
582 // X fonts are never underlined
583 return FALSE;
584 }
585
586 wxString wxNativeFontInfo::GetFaceName() const
587 {
588 // wxWidgets facename probably more accurately corresponds to X family
589 return GetXFontComponent(wxXLFD_FAMILY);
590 }
591
592 wxFontFamily wxNativeFontInfo::GetFamily() const
593 {
594 // and wxWidgets family -- to X foundry, but we have to translate it to
595 // wxFontFamily somehow...
596 wxFAIL_MSG(_T("not implemented")); // GetXFontComponent(wxXLFD_FOUNDRY);
597
598 return wxFONTFAMILY_DEFAULT;
599 }
600
601 wxFontEncoding wxNativeFontInfo::GetEncoding() const
602 {
603 // we already have the code for this but need to refactor it first
604 wxFAIL_MSG( _T("not implemented") );
605
606 return wxFONTENCODING_MAX;
607 }
608
609 void wxNativeFontInfo::SetPointSize(int pointsize)
610 {
611 SetXFontComponent(wxXLFD_POINTSIZE, wxString::Format(_T("%d"), pointsize));
612 }
613
614 void wxNativeFontInfo::SetStyle(wxFontStyle style)
615 {
616 wxString s;
617 switch ( style )
618 {
619 case wxFONTSTYLE_ITALIC:
620 s = _T('i');
621 break;
622
623 case wxFONTSTYLE_SLANT:
624 s = _T('o');
625 break;
626
627 case wxFONTSTYLE_NORMAL:
628 s = _T('r');
629
630 default:
631 wxFAIL_MSG( _T("unknown wxFontStyle in wxNativeFontInfo::SetStyle") );
632 return;
633 }
634
635 SetXFontComponent(wxXLFD_SLANT, s);
636 }
637
638 void wxNativeFontInfo::SetWeight(wxFontWeight weight)
639 {
640 wxString s;
641 switch ( weight )
642 {
643 case wxFONTWEIGHT_BOLD:
644 s = _T("bold");
645 break;
646
647 case wxFONTWEIGHT_LIGHT:
648 s = _T("light");
649 break;
650
651 case wxFONTWEIGHT_NORMAL:
652 s = _T("medium");
653 break;
654
655 default:
656 wxFAIL_MSG( _T("unknown wxFontWeight in wxNativeFontInfo::SetWeight") );
657 return;
658 }
659
660 SetXFontComponent(wxXLFD_WEIGHT, s);
661 }
662
663 void wxNativeFontInfo::SetUnderlined(bool WXUNUSED(underlined))
664 {
665 // can't do this under X
666 }
667
668 void wxNativeFontInfo::SetFaceName(wxString facename)
669 {
670 SetXFontComponent(wxXLFD_FAMILY, facename);
671 }
672
673 void wxNativeFontInfo::SetFamily(wxFontFamily family)
674 {
675 // wxFontFamily -> X foundry, anyone?
676 wxFAIL_MSG( _T("not implemented") );
677
678 // SetXFontComponent(wxXLFD_FOUNDRY, ...);
679 }
680
681 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding)
682 {
683 wxNativeEncodingInfo info;
684 if ( wxGetNativeFontEncoding(encoding, &info) )
685 {
686 SetXFontComponent(wxXLFD_ENCODING, info.xencoding);
687 SetXFontComponent(wxXLFD_REGISTRY, info.xregistry);
688 }
689 }
690
691 // ----------------------------------------------------------------------------
692 // common functions
693 // ----------------------------------------------------------------------------
694
695 bool wxGetNativeFontEncoding(wxFontEncoding encoding,
696 wxNativeEncodingInfo *info)
697 {
698 wxCHECK_MSG( info, FALSE, _T("bad pointer in wxGetNativeFontEncoding") );
699
700 if ( encoding == wxFONTENCODING_DEFAULT )
701 {
702 encoding = wxFont::GetDefaultEncoding();
703 }
704
705 switch ( encoding )
706 {
707 case wxFONTENCODING_ISO8859_1:
708 case wxFONTENCODING_ISO8859_2:
709 case wxFONTENCODING_ISO8859_3:
710 case wxFONTENCODING_ISO8859_4:
711 case wxFONTENCODING_ISO8859_5:
712 case wxFONTENCODING_ISO8859_6:
713 case wxFONTENCODING_ISO8859_7:
714 case wxFONTENCODING_ISO8859_8:
715 case wxFONTENCODING_ISO8859_9:
716 case wxFONTENCODING_ISO8859_10:
717 case wxFONTENCODING_ISO8859_11:
718 case wxFONTENCODING_ISO8859_12:
719 case wxFONTENCODING_ISO8859_13:
720 case wxFONTENCODING_ISO8859_14:
721 case wxFONTENCODING_ISO8859_15:
722 {
723 int cp = encoding - wxFONTENCODING_ISO8859_1 + 1;
724 info->xregistry = wxT("iso8859");
725 info->xencoding.Printf(wxT("%d"), cp);
726 }
727 break;
728
729 case wxFONTENCODING_UTF8:
730 info->xregistry = wxT("iso10646");
731 info->xencoding = wxT("*");
732 break;
733
734 case wxFONTENCODING_GB2312:
735 info->xregistry = wxT("GB2312"); // or the otherway round?
736 info->xencoding = wxT("*");
737 break;
738
739 case wxFONTENCODING_KOI8:
740 case wxFONTENCODING_KOI8_U:
741 info->xregistry = wxT("koi8");
742
743 // we don't make distinction between koi8-r, koi8-u and koi8-ru (so far)
744 info->xencoding = wxT("*");
745 break;
746
747 case wxFONTENCODING_CP1250:
748 case wxFONTENCODING_CP1251:
749 case wxFONTENCODING_CP1252:
750 case wxFONTENCODING_CP1253:
751 case wxFONTENCODING_CP1254:
752 case wxFONTENCODING_CP1255:
753 case wxFONTENCODING_CP1256:
754 case wxFONTENCODING_CP1257:
755 {
756 int cp = encoding - wxFONTENCODING_CP1250 + 1250;
757 info->xregistry = wxT("microsoft");
758 info->xencoding.Printf(wxT("cp%d"), cp);
759 }
760 break;
761
762 case wxFONTENCODING_SYSTEM:
763 info->xregistry =
764 info->xencoding = wxT("*");
765 break;
766
767 default:
768 // don't know how to translate this encoding into X fontspec
769 return FALSE;
770 }
771
772 info->encoding = encoding;
773
774 return TRUE;
775 }
776
777 bool wxTestFontEncoding(const wxNativeEncodingInfo& info)
778 {
779 wxString fontspec;
780 fontspec.Printf(_T("-*-%s-*-*-*-*-*-*-*-*-*-*-%s-%s"),
781 !info.facename ? _T("*") : info.facename.c_str(),
782 info.xregistry.c_str(),
783 info.xencoding.c_str());
784
785 return wxTestFontSpec(fontspec);
786 }
787
788 // ----------------------------------------------------------------------------
789 // X-specific functions
790 // ----------------------------------------------------------------------------
791
792 wxNativeFont wxLoadQueryNearestFont(int pointSize,
793 int family,
794 int style,
795 int weight,
796 bool underlined,
797 const wxString &facename,
798 wxFontEncoding encoding,
799 wxString* xFontName)
800 {
801 if ( encoding == wxFONTENCODING_DEFAULT )
802 {
803 encoding = wxFont::GetDefaultEncoding();
804 }
805
806 // first determine the encoding - if the font doesn't exist at all in this
807 // encoding, it's useless to do all other approximations (i.e. size,
808 // family &c don't matter much)
809 wxNativeEncodingInfo info;
810 if ( encoding == wxFONTENCODING_SYSTEM )
811 {
812 // This will always work so we don't test to save time
813 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM, &info);
814 }
815 else
816 {
817 if ( !wxGetNativeFontEncoding(encoding, &info) ||
818 !wxTestFontEncoding(info) )
819 {
820 #if wxUSE_FONTMAP
821 if ( !wxFontMapper::Get()->GetAltForEncoding(encoding, &info) )
822 #endif // wxUSE_FONTMAP
823 {
824 // unspported encoding - replace it with the default
825 //
826 // NB: we can't just return 0 from here because wxGTK code doesn't
827 // check for it (i.e. it supposes that we'll always succeed),
828 // so it would provoke a crash
829 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM, &info);
830 }
831 }
832 }
833
834 // OK, we have the correct xregistry/xencoding in info structure
835 wxNativeFont font = 0;
836
837 // if we already have the X font name, try to use it
838 if( xFontName && !xFontName->IsEmpty() )
839 {
840 //
841 // Make sure point size is correct for scale factor.
842 //
843 wxStringTokenizer tokenizer(*xFontName, _T("-"), wxTOKEN_RET_DELIMS);
844 wxString newFontName;
845
846 for(int i = 0; i < 8; i++)
847 newFontName += tokenizer.NextToken();
848
849 (void) tokenizer.NextToken();
850
851 newFontName += wxString::Format(wxT("%d-"), pointSize);
852
853 while(tokenizer.HasMoreTokens())
854 newFontName += tokenizer.GetNextToken();
855
856 font = wxLoadFont(newFontName);
857
858 if(font)
859 *xFontName = newFontName;
860 }
861
862 if ( !font )
863 {
864 // search up and down by stepsize 10
865 int max_size = pointSize + 20 * (1 + (pointSize/180));
866 int min_size = pointSize - 20 * (1 + (pointSize/180));
867
868 int i, round; // counters
869
870 // first round: search for equal, then for smaller and for larger size with the given weight and style
871 int testweight = weight;
872 int teststyle = style;
873
874 for ( round = 0; round < 3; round++ )
875 {
876 // second round: use normal weight
877 if ( round == 1 )
878 {
879 if ( testweight != wxNORMAL )
880 {
881 testweight = wxNORMAL;
882 }
883 else
884 {
885 ++round; // fall through to third round
886 }
887 }
888
889 // third round: ... and use normal style
890 if ( round == 2 )
891 {
892 if ( teststyle != wxNORMAL )
893 {
894 teststyle = wxNORMAL;
895 }
896 else
897 {
898 break;
899 }
900 }
901 // Search for equal or smaller size (approx.)
902 for ( i = pointSize; !font && i >= 10 && i >= min_size; i -= 10 )
903 {
904 font = wxLoadQueryFont(i, family, teststyle, testweight, underlined,
905 facename, info.xregistry, info.xencoding,
906 xFontName);
907 }
908
909 // Search for larger size (approx.)
910 for ( i = pointSize + 10; !font && i <= max_size; i += 10 )
911 {
912 font = wxLoadQueryFont(i, family, teststyle, testweight, underlined,
913 facename, info.xregistry, info.xencoding,
914 xFontName);
915 }
916 }
917
918 // Try default family
919 if ( !font && family != wxDEFAULT )
920 {
921 font = wxLoadQueryFont(pointSize, wxDEFAULT, style, weight,
922 underlined, facename,
923 info.xregistry, info.xencoding,
924 xFontName );
925 }
926
927 // ignore size, family, style and weight but try to find font with the
928 // given facename and encoding
929 if ( !font )
930 {
931 font = wxLoadQueryFont(120, wxDEFAULT, wxNORMAL, wxNORMAL,
932 underlined, facename,
933 info.xregistry, info.xencoding,
934 xFontName);
935
936 // ignore family as well
937 if ( !font )
938 {
939 font = wxLoadQueryFont(120, wxDEFAULT, wxNORMAL, wxNORMAL,
940 underlined, wxEmptyString,
941 info.xregistry, info.xencoding,
942 xFontName);
943
944 // if it still failed, try to get the font of any size but
945 // with the requested encoding: this can happen if the
946 // encoding is only available in one size which happens to be
947 // different from 120
948 if ( !font )
949 {
950 font = wxLoadQueryFont(-1, wxDEFAULT, wxNORMAL, wxNORMAL,
951 FALSE, wxEmptyString,
952 info.xregistry, info.xencoding,
953 xFontName);
954
955 // this should never happen as we had tested for it in the
956 // very beginning, but if it does, do return something non
957 // NULL or we'd crash in wxFont code
958 if ( !font )
959 {
960 wxFAIL_MSG( _T("this encoding should be available!") );
961
962 font = wxLoadQueryFont(-1,
963 wxDEFAULT, wxNORMAL, wxNORMAL,
964 FALSE, wxEmptyString,
965 _T("*"), _T("*"),
966 xFontName);
967 }
968 }
969 }
970 }
971 }
972
973 return font;
974 }
975
976 // ----------------------------------------------------------------------------
977 // private functions
978 // ----------------------------------------------------------------------------
979
980 // returns TRUE if there are any fonts matching this font spec
981 static bool wxTestFontSpec(const wxString& fontspec)
982 {
983 // some X servers will fail to load this font because there are too many
984 // matches so we must test explicitly for this
985 if ( fontspec == _T("-*-*-*-*-*-*-*-*-*-*-*-*-*-*") )
986 {
987 return TRUE;
988 }
989
990 wxNativeFont test = (wxNativeFont) g_fontHash->Get( fontspec );
991 if (test)
992 {
993 return TRUE;
994 }
995
996 test = wxLoadFont(fontspec);
997 g_fontHash->Put( fontspec, (wxObject*) test );
998
999 if ( test )
1000 {
1001 wxFreeFont(test);
1002
1003 return TRUE;
1004 }
1005 else
1006 {
1007 return FALSE;
1008 }
1009 }
1010
1011 static wxNativeFont wxLoadQueryFont(int pointSize,
1012 int family,
1013 int style,
1014 int weight,
1015 bool WXUNUSED(underlined),
1016 const wxString& facename,
1017 const wxString& xregistry,
1018 const wxString& xencoding,
1019 wxString* xFontName)
1020 {
1021 wxString xfamily;
1022 switch (family)
1023 {
1024 case wxDECORATIVE: xfamily = wxT("lucida"); break;
1025 case wxROMAN: xfamily = wxT("times"); break;
1026 case wxMODERN: xfamily = wxT("courier"); break;
1027 case wxSWISS: xfamily = wxT("helvetica"); break;
1028 case wxTELETYPE: xfamily = wxT("lucidatypewriter"); break;
1029 case wxSCRIPT: xfamily = wxT("utopia"); break;
1030 default: xfamily = wxT("*");
1031 }
1032 #if wxUSE_NANOX
1033 int xweight;
1034 switch (weight)
1035 {
1036 case wxBOLD:
1037 {
1038 xweight = MWLF_WEIGHT_BOLD;
1039 break;
1040 }
1041 case wxLIGHT:
1042 {
1043 xweight = MWLF_WEIGHT_LIGHT;
1044 break;
1045 }
1046 case wxNORMAL:
1047 {
1048 xweight = MWLF_WEIGHT_NORMAL;
1049 break;
1050 }
1051
1052 default:
1053 {
1054 xweight = MWLF_WEIGHT_DEFAULT;
1055 break;
1056 }
1057 }
1058 GR_SCREEN_INFO screenInfo;
1059 GrGetScreenInfo(& screenInfo);
1060
1061 int yPixelsPerCM = screenInfo.ydpcm;
1062
1063 // A point is 1/72 of an inch.
1064 // An inch is 2.541 cm.
1065 // So pixelHeight = (pointSize / 72) (inches) * 2.541 (for cm) * yPixelsPerCM (for pixels)
1066 // In fact pointSize is 10 * the normal point size so
1067 // divide by 10.
1068
1069 int pixelHeight = (int) ( (((float)pointSize) / 720.0) * 2.541 * (float) yPixelsPerCM) ;
1070
1071 // An alternative: assume that the screen is 72 dpi.
1072 //int pixelHeight = (int) (((float)pointSize / 720.0) * 72.0) ;
1073 //int pixelHeight = (int) ((float)pointSize / 10.0) ;
1074
1075 GR_LOGFONT logFont;
1076 logFont.lfHeight = pixelHeight;
1077 logFont.lfWidth = 0;
1078 logFont.lfEscapement = 0;
1079 logFont.lfOrientation = 0;
1080 logFont.lfWeight = xweight;
1081 logFont.lfItalic = (style == wxNORMAL ? 0 : 1) ;
1082 logFont.lfUnderline = 0;
1083 logFont.lfStrikeOut = 0;
1084 logFont.lfCharSet = MWLF_CHARSET_DEFAULT; // TODO: select appropriate one
1085 logFont.lfOutPrecision = MWLF_TYPE_DEFAULT;
1086 logFont.lfClipPrecision = 0; // Not used
1087 logFont.lfRoman = (family == wxROMAN ? 1 : 0) ;
1088 logFont.lfSerif = (family == wxSWISS ? 0 : 1) ;
1089 logFont.lfSansSerif = !logFont.lfSerif ;
1090 logFont.lfModern = (family == wxMODERN ? 1 : 0) ;
1091 logFont.lfProportional = (family == wxTELETYPE ? 0 : 1) ;
1092 logFont.lfOblique = 0;
1093 logFont.lfSmallCaps = 0;
1094 logFont.lfPitch = 0; // 0 = default
1095 strcpy(logFont.lfFaceName, facename.c_str());
1096
1097 XFontStruct* fontInfo = (XFontStruct*) malloc(sizeof(XFontStruct));
1098 fontInfo->fid = GrCreateFont((GR_CHAR*) facename.c_str(), pixelHeight, & logFont);
1099 GrGetFontInfo(fontInfo->fid, & fontInfo->info);
1100 return (wxNativeFont) fontInfo;
1101
1102 #else
1103 wxString fontSpec;
1104 if (!facename.IsEmpty())
1105 {
1106 fontSpec.Printf(wxT("-*-%s-*-*-normal-*-*-*-*-*-*-*-*-*"),
1107 facename.c_str());
1108
1109 if ( wxTestFontSpec(fontSpec) )
1110 {
1111 xfamily = facename;
1112 }
1113 //else: no such family, use default one instead
1114 }
1115
1116 wxString xstyle;
1117 switch (style)
1118 {
1119 case wxSLANT:
1120 fontSpec.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1121 xfamily.c_str());
1122 if ( wxTestFontSpec(fontSpec) )
1123 {
1124 xstyle = wxT("o");
1125 break;
1126 }
1127 // fall through - try wxITALIC now
1128
1129 case wxITALIC:
1130 fontSpec.Printf(wxT("-*-%s-*-i-*-*-*-*-*-*-*-*-*-*"),
1131 xfamily.c_str());
1132 if ( wxTestFontSpec(fontSpec) )
1133 {
1134 xstyle = wxT("i");
1135 }
1136 else if ( style == wxITALIC ) // and not wxSLANT
1137 {
1138 // try wxSLANT
1139 fontSpec.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1140 xfamily.c_str());
1141 if ( wxTestFontSpec(fontSpec) )
1142 {
1143 xstyle = wxT("o");
1144 }
1145 else
1146 {
1147 // no italic, no slant - leave default
1148 xstyle = wxT("*");
1149 }
1150 }
1151 break;
1152
1153 default:
1154 wxFAIL_MSG(_T("unknown font style"));
1155 // fall back to normal
1156
1157 case wxNORMAL:
1158 xstyle = wxT("r");
1159 break;
1160 }
1161
1162 wxString xweight;
1163 switch (weight)
1164 {
1165 case wxBOLD:
1166 {
1167 fontSpec.Printf(wxT("-*-%s-bold-*-*-*-*-*-*-*-*-*-*-*"),
1168 xfamily.c_str());
1169 if ( wxTestFontSpec(fontSpec) )
1170 {
1171 xweight = wxT("bold");
1172 break;
1173 }
1174 fontSpec.Printf(wxT("-*-%s-heavy-*-*-*-*-*-*-*-*-*-*-*"),
1175 xfamily.c_str());
1176 if ( wxTestFontSpec(fontSpec) )
1177 {
1178 xweight = wxT("heavy");
1179 break;
1180 }
1181 fontSpec.Printf(wxT("-*-%s-extrabold-*-*-*-*-*-*-*-*-*-*-*"),
1182 xfamily.c_str());
1183 if ( wxTestFontSpec(fontSpec) )
1184 {
1185 xweight = wxT("extrabold");
1186 break;
1187 }
1188 fontSpec.Printf(wxT("-*-%s-demibold-*-*-*-*-*-*-*-*-*-*-*"),
1189 xfamily.c_str());
1190 if ( wxTestFontSpec(fontSpec) )
1191 {
1192 xweight = wxT("demibold");
1193 break;
1194 }
1195 fontSpec.Printf(wxT("-*-%s-black-*-*-*-*-*-*-*-*-*-*-*"),
1196 xfamily.c_str());
1197 if ( wxTestFontSpec(fontSpec) )
1198 {
1199 xweight = wxT("black");
1200 break;
1201 }
1202 fontSpec.Printf(wxT("-*-%s-ultrablack-*-*-*-*-*-*-*-*-*-*-*"),
1203 xfamily.c_str());
1204 if ( wxTestFontSpec(fontSpec) )
1205 {
1206 xweight = wxT("ultrablack");
1207 break;
1208 }
1209 }
1210 break;
1211 case wxLIGHT:
1212 {
1213 fontSpec.Printf(wxT("-*-%s-light-*-*-*-*-*-*-*-*-*-*-*"),
1214 xfamily.c_str());
1215 if ( wxTestFontSpec(fontSpec) )
1216 {
1217 xweight = wxT("light");
1218 break;
1219 }
1220 fontSpec.Printf(wxT("-*-%s-thin-*-*-*-*-*-*-*-*-*-*-*"),
1221 xfamily.c_str());
1222 if ( wxTestFontSpec(fontSpec) )
1223 {
1224 xweight = wxT("thin");
1225 break;
1226 }
1227 }
1228 break;
1229 case wxNORMAL:
1230 {
1231 fontSpec.Printf(wxT("-*-%s-medium-*-*-*-*-*-*-*-*-*-*-*"),
1232 xfamily.c_str());
1233 if ( wxTestFontSpec(fontSpec) )
1234 {
1235 xweight = wxT("medium");
1236 break;
1237 }
1238 fontSpec.Printf(wxT("-*-%s-normal-*-*-*-*-*-*-*-*-*-*-*"),
1239 xfamily.c_str());
1240 if ( wxTestFontSpec(fontSpec) )
1241 {
1242 xweight = wxT("normal");
1243 break;
1244 }
1245 fontSpec.Printf(wxT("-*-%s-regular-*-*-*-*-*-*-*-*-*-*-*"),
1246 xfamily.c_str());
1247 if ( wxTestFontSpec(fontSpec) )
1248 {
1249 xweight = wxT("regular");
1250 break;
1251 }
1252 xweight = wxT("*");
1253 }
1254 break;
1255 default: xweight = wxT("*"); break;
1256 }
1257
1258 // if pointSize is -1, don't specify any
1259 wxString sizeSpec;
1260 if ( pointSize == -1 )
1261 {
1262 sizeSpec = _T('*');
1263 }
1264 else
1265 {
1266 sizeSpec.Printf(_T("%d"), pointSize);
1267 }
1268
1269 // construct the X font spec from our data
1270 fontSpec.Printf(wxT("-*-%s-%s-%s-normal-*-*-%s-*-*-*-*-%s-%s"),
1271 xfamily.c_str(), xweight.c_str(), xstyle.c_str(),
1272 sizeSpec.c_str(), xregistry.c_str(), xencoding.c_str());
1273
1274 if( xFontName )
1275 *xFontName = fontSpec;
1276
1277 return wxLoadFont(fontSpec);
1278 #endif
1279 // wxUSE_NANOX
1280 }
1281
1282 // ----------------------------------------------------------------------------
1283 // wxFontModule
1284 // ----------------------------------------------------------------------------
1285
1286 class wxFontModule : public wxModule
1287 {
1288 public:
1289 bool OnInit();
1290 void OnExit();
1291
1292 private:
1293 DECLARE_DYNAMIC_CLASS(wxFontModule)
1294 };
1295
1296 IMPLEMENT_DYNAMIC_CLASS(wxFontModule, wxModule)
1297
1298 bool wxFontModule::OnInit()
1299 {
1300 g_fontHash = new wxHashTable( wxKEY_STRING );
1301
1302 return TRUE;
1303 }
1304
1305 void wxFontModule::OnExit()
1306 {
1307 delete g_fontHash;
1308
1309 g_fontHash = (wxHashTable *)NULL;
1310 }
1311
1312 #endif // GTK 2.0/1.x
1313