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