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