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