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