Return the correct encoding for X font spec construction
[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_EUC_JP:
763 case wxFONTENCODING_SHIFT_JIS:
764 info->xregistry = "jis*";
765 info->xencoding = "*";
766 break;
767
768 case wxFONTENCODING_SYSTEM:
769 info->xregistry =
770 info->xencoding = wxT("*");
771 break;
772
773 default:
774 // don't know how to translate this encoding into X fontspec
775 return FALSE;
776 }
777
778 info->encoding = encoding;
779
780 return TRUE;
781 }
782
783 bool wxTestFontEncoding(const wxNativeEncodingInfo& info)
784 {
785 wxString fontspec;
786 fontspec.Printf(_T("-*-%s-*-*-*-*-*-*-*-*-*-*-%s-%s"),
787 !info.facename ? _T("*") : info.facename.c_str(),
788 info.xregistry.c_str(),
789 info.xencoding.c_str());
790
791 return wxTestFontSpec(fontspec);
792 }
793
794 // ----------------------------------------------------------------------------
795 // X-specific functions
796 // ----------------------------------------------------------------------------
797
798 wxNativeFont wxLoadQueryNearestFont(int pointSize,
799 int family,
800 int style,
801 int weight,
802 bool underlined,
803 const wxString &facename,
804 wxFontEncoding encoding,
805 wxString* xFontName)
806 {
807 if ( encoding == wxFONTENCODING_DEFAULT )
808 {
809 encoding = wxFont::GetDefaultEncoding();
810 }
811
812 // first determine the encoding - if the font doesn't exist at all in this
813 // encoding, it's useless to do all other approximations (i.e. size,
814 // family &c don't matter much)
815 wxNativeEncodingInfo info;
816 if ( encoding == wxFONTENCODING_SYSTEM )
817 {
818 // This will always work so we don't test to save time
819 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM, &info);
820 }
821 else
822 {
823 if ( !wxGetNativeFontEncoding(encoding, &info) ||
824 !wxTestFontEncoding(info) )
825 {
826 #if wxUSE_FONTMAP
827 if ( !wxFontMapper::Get()->GetAltForEncoding(encoding, &info) )
828 #endif // wxUSE_FONTMAP
829 {
830 // unspported encoding - replace it with the default
831 //
832 // NB: we can't just return 0 from here because wxGTK code doesn't
833 // check for it (i.e. it supposes that we'll always succeed),
834 // so it would provoke a crash
835 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM, &info);
836 }
837 }
838 }
839
840 // OK, we have the correct xregistry/xencoding in info structure
841 wxNativeFont font = 0;
842
843 // if we already have the X font name, try to use it
844 if( xFontName && !xFontName->IsEmpty() )
845 {
846 //
847 // Make sure point size is correct for scale factor.
848 //
849 wxStringTokenizer tokenizer(*xFontName, _T("-"), wxTOKEN_RET_DELIMS);
850 wxString newFontName;
851
852 for(int i = 0; i < 8; i++)
853 newFontName += tokenizer.NextToken();
854
855 (void) tokenizer.NextToken();
856
857 newFontName += wxString::Format(wxT("%d-"), pointSize);
858
859 while(tokenizer.HasMoreTokens())
860 newFontName += tokenizer.GetNextToken();
861
862 font = wxLoadFont(newFontName);
863
864 if(font)
865 *xFontName = newFontName;
866 }
867
868 if ( !font )
869 {
870 // search up and down by stepsize 10
871 int max_size = pointSize + 20 * (1 + (pointSize/180));
872 int min_size = pointSize - 20 * (1 + (pointSize/180));
873
874 int i, round; // counters
875
876 // first round: search for equal, then for smaller and for larger size with the given weight and style
877 int testweight = weight;
878 int teststyle = style;
879
880 for ( round = 0; round < 3; round++ )
881 {
882 // second round: use normal weight
883 if ( round == 1 )
884 {
885 if ( testweight != wxNORMAL )
886 {
887 testweight = wxNORMAL;
888 }
889 else
890 {
891 ++round; // fall through to third round
892 }
893 }
894
895 // third round: ... and use normal style
896 if ( round == 2 )
897 {
898 if ( teststyle != wxNORMAL )
899 {
900 teststyle = wxNORMAL;
901 }
902 else
903 {
904 break;
905 }
906 }
907 // Search for equal or smaller size (approx.)
908 for ( i = pointSize; !font && i >= 10 && i >= min_size; i -= 10 )
909 {
910 font = wxLoadQueryFont(i, family, teststyle, testweight, underlined,
911 facename, info.xregistry, info.xencoding,
912 xFontName);
913 }
914
915 // Search for larger size (approx.)
916 for ( i = pointSize + 10; !font && i <= max_size; i += 10 )
917 {
918 font = wxLoadQueryFont(i, family, teststyle, testweight, underlined,
919 facename, info.xregistry, info.xencoding,
920 xFontName);
921 }
922 }
923
924 // Try default family
925 if ( !font && family != wxDEFAULT )
926 {
927 font = wxLoadQueryFont(pointSize, wxDEFAULT, style, weight,
928 underlined, facename,
929 info.xregistry, info.xencoding,
930 xFontName );
931 }
932
933 // ignore size, family, style and weight but try to find font with the
934 // given facename and encoding
935 if ( !font )
936 {
937 font = wxLoadQueryFont(120, wxDEFAULT, wxNORMAL, wxNORMAL,
938 underlined, facename,
939 info.xregistry, info.xencoding,
940 xFontName);
941
942 // ignore family as well
943 if ( !font )
944 {
945 font = wxLoadQueryFont(120, wxDEFAULT, wxNORMAL, wxNORMAL,
946 underlined, wxEmptyString,
947 info.xregistry, info.xencoding,
948 xFontName);
949
950 // if it still failed, try to get the font of any size but
951 // with the requested encoding: this can happen if the
952 // encoding is only available in one size which happens to be
953 // different from 120
954 if ( !font )
955 {
956 font = wxLoadQueryFont(-1, wxDEFAULT, wxNORMAL, wxNORMAL,
957 FALSE, wxEmptyString,
958 info.xregistry, info.xencoding,
959 xFontName);
960
961 // this should never happen as we had tested for it in the
962 // very beginning, but if it does, do return something non
963 // NULL or we'd crash in wxFont code
964 if ( !font )
965 {
966 wxFAIL_MSG( _T("this encoding should be available!") );
967
968 font = wxLoadQueryFont(-1,
969 wxDEFAULT, wxNORMAL, wxNORMAL,
970 FALSE, wxEmptyString,
971 _T("*"), _T("*"),
972 xFontName);
973 }
974 }
975 }
976 }
977 }
978
979 return font;
980 }
981
982 // ----------------------------------------------------------------------------
983 // private functions
984 // ----------------------------------------------------------------------------
985
986 // returns TRUE if there are any fonts matching this font spec
987 static bool wxTestFontSpec(const wxString& fontspec)
988 {
989 // some X servers will fail to load this font because there are too many
990 // matches so we must test explicitly for this
991 if ( fontspec == _T("-*-*-*-*-*-*-*-*-*-*-*-*-*-*") )
992 {
993 return TRUE;
994 }
995
996 wxNativeFont test = (wxNativeFont) g_fontHash->Get( fontspec );
997 if (test)
998 {
999 return TRUE;
1000 }
1001
1002 test = wxLoadFont(fontspec);
1003 g_fontHash->Put( fontspec, (wxObject*) test );
1004
1005 if ( test )
1006 {
1007 wxFreeFont(test);
1008
1009 return TRUE;
1010 }
1011 else
1012 {
1013 return FALSE;
1014 }
1015 }
1016
1017 static wxNativeFont wxLoadQueryFont(int pointSize,
1018 int family,
1019 int style,
1020 int weight,
1021 bool WXUNUSED(underlined),
1022 const wxString& facename,
1023 const wxString& xregistry,
1024 const wxString& xencoding,
1025 wxString* xFontName)
1026 {
1027 wxString xfamily;
1028 switch (family)
1029 {
1030 case wxDECORATIVE: xfamily = wxT("lucida"); break;
1031 case wxROMAN: xfamily = wxT("times"); break;
1032 case wxMODERN: xfamily = wxT("courier"); break;
1033 case wxSWISS: xfamily = wxT("helvetica"); break;
1034 case wxTELETYPE: xfamily = wxT("lucidatypewriter"); break;
1035 case wxSCRIPT: xfamily = wxT("utopia"); break;
1036 default: xfamily = wxT("*");
1037 }
1038 #if wxUSE_NANOX
1039 int xweight;
1040 switch (weight)
1041 {
1042 case wxBOLD:
1043 {
1044 xweight = MWLF_WEIGHT_BOLD;
1045 break;
1046 }
1047 case wxLIGHT:
1048 {
1049 xweight = MWLF_WEIGHT_LIGHT;
1050 break;
1051 }
1052 case wxNORMAL:
1053 {
1054 xweight = MWLF_WEIGHT_NORMAL;
1055 break;
1056 }
1057
1058 default:
1059 {
1060 xweight = MWLF_WEIGHT_DEFAULT;
1061 break;
1062 }
1063 }
1064 GR_SCREEN_INFO screenInfo;
1065 GrGetScreenInfo(& screenInfo);
1066
1067 int yPixelsPerCM = screenInfo.ydpcm;
1068
1069 // A point is 1/72 of an inch.
1070 // An inch is 2.541 cm.
1071 // So pixelHeight = (pointSize / 72) (inches) * 2.541 (for cm) * yPixelsPerCM (for pixels)
1072 // In fact pointSize is 10 * the normal point size so
1073 // divide by 10.
1074
1075 int pixelHeight = (int) ( (((float)pointSize) / 720.0) * 2.541 * (float) yPixelsPerCM) ;
1076
1077 // An alternative: assume that the screen is 72 dpi.
1078 //int pixelHeight = (int) (((float)pointSize / 720.0) * 72.0) ;
1079 //int pixelHeight = (int) ((float)pointSize / 10.0) ;
1080
1081 GR_LOGFONT logFont;
1082 logFont.lfHeight = pixelHeight;
1083 logFont.lfWidth = 0;
1084 logFont.lfEscapement = 0;
1085 logFont.lfOrientation = 0;
1086 logFont.lfWeight = xweight;
1087 logFont.lfItalic = (style == wxNORMAL ? 0 : 1) ;
1088 logFont.lfUnderline = 0;
1089 logFont.lfStrikeOut = 0;
1090 logFont.lfCharSet = MWLF_CHARSET_DEFAULT; // TODO: select appropriate one
1091 logFont.lfOutPrecision = MWLF_TYPE_DEFAULT;
1092 logFont.lfClipPrecision = 0; // Not used
1093 logFont.lfRoman = (family == wxROMAN ? 1 : 0) ;
1094 logFont.lfSerif = (family == wxSWISS ? 0 : 1) ;
1095 logFont.lfSansSerif = !logFont.lfSerif ;
1096 logFont.lfModern = (family == wxMODERN ? 1 : 0) ;
1097 logFont.lfProportional = (family == wxTELETYPE ? 0 : 1) ;
1098 logFont.lfOblique = 0;
1099 logFont.lfSmallCaps = 0;
1100 logFont.lfPitch = 0; // 0 = default
1101 strcpy(logFont.lfFaceName, facename.c_str());
1102
1103 XFontStruct* fontInfo = (XFontStruct*) malloc(sizeof(XFontStruct));
1104 fontInfo->fid = GrCreateFont((GR_CHAR*) facename.c_str(), pixelHeight, & logFont);
1105 GrGetFontInfo(fontInfo->fid, & fontInfo->info);
1106 return (wxNativeFont) fontInfo;
1107
1108 #else
1109 wxString fontSpec;
1110 if (!facename.IsEmpty())
1111 {
1112 fontSpec.Printf(wxT("-*-%s-*-*-normal-*-*-*-*-*-*-*-*-*"),
1113 facename.c_str());
1114
1115 if ( wxTestFontSpec(fontSpec) )
1116 {
1117 xfamily = facename;
1118 }
1119 //else: no such family, use default one instead
1120 }
1121
1122 wxString xstyle;
1123 switch (style)
1124 {
1125 case wxSLANT:
1126 fontSpec.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1127 xfamily.c_str());
1128 if ( wxTestFontSpec(fontSpec) )
1129 {
1130 xstyle = wxT("o");
1131 break;
1132 }
1133 // fall through - try wxITALIC now
1134
1135 case wxITALIC:
1136 fontSpec.Printf(wxT("-*-%s-*-i-*-*-*-*-*-*-*-*-*-*"),
1137 xfamily.c_str());
1138 if ( wxTestFontSpec(fontSpec) )
1139 {
1140 xstyle = wxT("i");
1141 }
1142 else if ( style == wxITALIC ) // and not wxSLANT
1143 {
1144 // try wxSLANT
1145 fontSpec.Printf(wxT("-*-%s-*-o-*-*-*-*-*-*-*-*-*-*"),
1146 xfamily.c_str());
1147 if ( wxTestFontSpec(fontSpec) )
1148 {
1149 xstyle = wxT("o");
1150 }
1151 else
1152 {
1153 // no italic, no slant - leave default
1154 xstyle = wxT("*");
1155 }
1156 }
1157 break;
1158
1159 default:
1160 wxFAIL_MSG(_T("unknown font style"));
1161 // fall back to normal
1162
1163 case wxNORMAL:
1164 xstyle = wxT("r");
1165 break;
1166 }
1167
1168 wxString xweight;
1169 switch (weight)
1170 {
1171 case wxBOLD:
1172 {
1173 fontSpec.Printf(wxT("-*-%s-bold-*-*-*-*-*-*-*-*-*-*-*"),
1174 xfamily.c_str());
1175 if ( wxTestFontSpec(fontSpec) )
1176 {
1177 xweight = wxT("bold");
1178 break;
1179 }
1180 fontSpec.Printf(wxT("-*-%s-heavy-*-*-*-*-*-*-*-*-*-*-*"),
1181 xfamily.c_str());
1182 if ( wxTestFontSpec(fontSpec) )
1183 {
1184 xweight = wxT("heavy");
1185 break;
1186 }
1187 fontSpec.Printf(wxT("-*-%s-extrabold-*-*-*-*-*-*-*-*-*-*-*"),
1188 xfamily.c_str());
1189 if ( wxTestFontSpec(fontSpec) )
1190 {
1191 xweight = wxT("extrabold");
1192 break;
1193 }
1194 fontSpec.Printf(wxT("-*-%s-demibold-*-*-*-*-*-*-*-*-*-*-*"),
1195 xfamily.c_str());
1196 if ( wxTestFontSpec(fontSpec) )
1197 {
1198 xweight = wxT("demibold");
1199 break;
1200 }
1201 fontSpec.Printf(wxT("-*-%s-black-*-*-*-*-*-*-*-*-*-*-*"),
1202 xfamily.c_str());
1203 if ( wxTestFontSpec(fontSpec) )
1204 {
1205 xweight = wxT("black");
1206 break;
1207 }
1208 fontSpec.Printf(wxT("-*-%s-ultrablack-*-*-*-*-*-*-*-*-*-*-*"),
1209 xfamily.c_str());
1210 if ( wxTestFontSpec(fontSpec) )
1211 {
1212 xweight = wxT("ultrablack");
1213 break;
1214 }
1215 }
1216 break;
1217 case wxLIGHT:
1218 {
1219 fontSpec.Printf(wxT("-*-%s-light-*-*-*-*-*-*-*-*-*-*-*"),
1220 xfamily.c_str());
1221 if ( wxTestFontSpec(fontSpec) )
1222 {
1223 xweight = wxT("light");
1224 break;
1225 }
1226 fontSpec.Printf(wxT("-*-%s-thin-*-*-*-*-*-*-*-*-*-*-*"),
1227 xfamily.c_str());
1228 if ( wxTestFontSpec(fontSpec) )
1229 {
1230 xweight = wxT("thin");
1231 break;
1232 }
1233 }
1234 break;
1235 case wxNORMAL:
1236 {
1237 fontSpec.Printf(wxT("-*-%s-medium-*-*-*-*-*-*-*-*-*-*-*"),
1238 xfamily.c_str());
1239 if ( wxTestFontSpec(fontSpec) )
1240 {
1241 xweight = wxT("medium");
1242 break;
1243 }
1244 fontSpec.Printf(wxT("-*-%s-normal-*-*-*-*-*-*-*-*-*-*-*"),
1245 xfamily.c_str());
1246 if ( wxTestFontSpec(fontSpec) )
1247 {
1248 xweight = wxT("normal");
1249 break;
1250 }
1251 fontSpec.Printf(wxT("-*-%s-regular-*-*-*-*-*-*-*-*-*-*-*"),
1252 xfamily.c_str());
1253 if ( wxTestFontSpec(fontSpec) )
1254 {
1255 xweight = wxT("regular");
1256 break;
1257 }
1258 xweight = wxT("*");
1259 }
1260 break;
1261 default: xweight = wxT("*"); break;
1262 }
1263
1264 // if pointSize is -1, don't specify any
1265 wxString sizeSpec;
1266 if ( pointSize == -1 )
1267 {
1268 sizeSpec = _T('*');
1269 }
1270 else
1271 {
1272 sizeSpec.Printf(_T("%d"), pointSize);
1273 }
1274
1275 // construct the X font spec from our data
1276 fontSpec.Printf(wxT("-*-%s-%s-%s-normal-*-*-%s-*-*-*-*-%s-%s"),
1277 xfamily.c_str(), xweight.c_str(), xstyle.c_str(),
1278 sizeSpec.c_str(), xregistry.c_str(), xencoding.c_str());
1279
1280 if( xFontName )
1281 *xFontName = fontSpec;
1282
1283 return wxLoadFont(fontSpec);
1284 #endif
1285 // wxUSE_NANOX
1286 }
1287
1288 // ----------------------------------------------------------------------------
1289 // wxFontModule
1290 // ----------------------------------------------------------------------------
1291
1292 class wxFontModule : public wxModule
1293 {
1294 public:
1295 bool OnInit();
1296 void OnExit();
1297
1298 private:
1299 DECLARE_DYNAMIC_CLASS(wxFontModule)
1300 };
1301
1302 IMPLEMENT_DYNAMIC_CLASS(wxFontModule, wxModule)
1303
1304 bool wxFontModule::OnInit()
1305 {
1306 g_fontHash = new wxHashTable( wxKEY_STRING );
1307
1308 return TRUE;
1309 }
1310
1311 void wxFontModule::OnExit()
1312 {
1313 delete g_fontHash;
1314
1315 g_fontHash = (wxHashTable *)NULL;
1316 }
1317
1318 #endif // GTK 2.0/1.x
1319