Include wx/utils.h according to precompiled headers of wx/wx.h (with other minor...
[wxWidgets.git] / src / unix / fontutil.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/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 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #ifndef WX_PRECOMP
28 #include "wx/font.h" // wxFont enums
29 #include "wx/encinfo.h"
30 #include "wx/hash.h"
31 #include "wx/utils.h" // for wxGetDisplay()
32 #endif // PCH
33
34 #include "wx/fontutil.h"
35 #include "wx/fontmap.h"
36 #include "wx/tokenzr.h"
37 #include "wx/module.h"
38
39 #if wxUSE_PANGO
40
41 #include "pango/pango.h"
42
43 #ifdef __WXGTK20__
44 #include "wx/gtk/private.h"
45 extern GtkWidget *wxGetRootWindow();
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 void
60 wxNativeFontInfo::Init(const wxNativeFontInfo& info)
61 {
62 if (info.description)
63 description = pango_font_description_copy(info.description);
64 else
65 description = NULL;
66 }
67
68 void wxNativeFontInfo::Free()
69 {
70 if (description)
71 pango_font_description_free(description);
72 }
73
74 int wxNativeFontInfo::GetPointSize() const
75 {
76 return pango_font_description_get_size( description ) / PANGO_SCALE;
77 }
78
79 wxFontStyle wxNativeFontInfo::GetStyle() const
80 {
81 wxFontStyle m_style = wxFONTSTYLE_NORMAL;
82
83 switch (pango_font_description_get_style( description ))
84 {
85 case PANGO_STYLE_NORMAL:
86 m_style = wxFONTSTYLE_NORMAL;
87 break;
88 case PANGO_STYLE_ITALIC:
89 m_style = wxFONTSTYLE_ITALIC;
90 break;
91 case PANGO_STYLE_OBLIQUE:
92 m_style = wxFONTSTYLE_SLANT;
93 break;
94 }
95
96 return m_style;
97 }
98
99 wxFontWeight wxNativeFontInfo::GetWeight() const
100 {
101 #if 0
102 // We seem to currently initialize only by string.
103 // In that case PANGO_FONT_MASK_WEIGHT is always set.
104 if (!(pango_font_description_get_set_fields(description) & PANGO_FONT_MASK_WEIGHT))
105 return wxFONTWEIGHT_NORMAL;
106 #endif
107
108 PangoWeight pango_weight = pango_font_description_get_weight( description );
109
110 // Until the API can be changed the following ranges of weight values are used:
111 // wxFONTWEIGHT_LIGHT: 100 .. 349 - range of 250
112 // wxFONTWEIGHT_NORMAL: 350 .. 599 - range of 250
113 // wxFONTWEIGHT_BOLD: 600 .. 900 - range of 301 (600 is "semibold" already)
114
115 if (pango_weight >= 600)
116 return wxFONTWEIGHT_BOLD;
117
118 if (pango_weight < 350)
119 return wxFONTWEIGHT_LIGHT;
120
121 return wxFONTWEIGHT_NORMAL;
122 }
123
124 bool wxNativeFontInfo::GetUnderlined() const
125 {
126 return false;
127 }
128
129 wxString wxNativeFontInfo::GetFaceName() const
130 {
131 wxString tmp = wxGTK_CONV_BACK( pango_font_description_get_family( description ) );
132
133 return tmp;
134 }
135
136 wxFontFamily wxNativeFontInfo::GetFamily() const
137 {
138 wxFontFamily ret = wxFONTFAMILY_DEFAULT;
139 // note: not passing -1 as the 2nd parameter to g_ascii_strdown to work
140 // around a bug in the 64-bit glib shipped with solaris 10, -1 causes it
141 // to try to allocate 2^32 bytes.
142 const char *family_name = pango_font_description_get_family( description );
143 char *family_text = g_ascii_strdown( family_name, family_name ? strlen( family_name ) : 0 );
144 // Check for some common fonts, to salvage what we can from the current win32 centric wxFont API:
145 if (strncmp( family_text, "monospace", 9 ) == 0)
146 ret = wxFONTFAMILY_TELETYPE; // begins with "Monospace"
147 else if (strncmp( family_text, "courier", 7 ) == 0)
148 ret = wxFONTFAMILY_TELETYPE; // begins with "Courier"
149 #if defined(__WXGTK24__) || defined(HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE)
150 else
151 #ifdef __WXGTK24__
152 if (!gtk_check_version(2,4,0))
153 #endif
154 {
155 PangoFontFamily **families;
156 PangoFontFamily *family = NULL;
157 int n_families;
158 pango_context_list_families(
159 #ifdef __WXGTK20__
160 gtk_widget_get_pango_context( wxGetRootWindow() ),
161 #else
162 wxTheApp->GetPangoContext(),
163 #endif
164 &families, &n_families);
165
166 for (int i = 0;i < n_families;++i)
167 {
168 if (g_ascii_strcasecmp(pango_font_family_get_name( families[i] ), pango_font_description_get_family( description )) == 0 )
169 {
170 family = families[i];
171 break;
172 }
173 }
174
175 g_free(families);
176
177 // Some gtk+ systems might query for a non-existing font from wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT)
178 // on initialization, don't assert until wxSystemSettings::GetFont is checked for this - MR
179 // wxASSERT_MSG( family, wxT("wxNativeFontInfo::GetFamily() - No appropriate PangoFontFamily found for ::description") );
180
181 //BCI: Cache the wxFontFamily inside the class. Validate cache with
182 //BCI: g_ascii_strcasecmp(pango_font_description_get_family(description), pango_font_family_get_name(family)) == 0
183
184 if (family != NULL && pango_font_family_is_monospace( family ))
185 ret = wxFONTFAMILY_TELETYPE; // is deemed a monospace font by pango
186 }
187 #endif // gtk24 || HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE
188
189 if (ret == wxFONTFAMILY_DEFAULT)
190 {
191 if (strstr( family_text, "sans" ) != NULL) // checked before serif, so that "* Sans Serif" fonts are detected correctly
192 ret = wxFONTFAMILY_SWISS; // contains "Sans"
193 else if (strstr( family_text, "serif" ) != NULL)
194 ret = wxFONTFAMILY_ROMAN; // contains "Serif"
195 else if (strncmp( family_text, "times", 5 ) == 0)
196 ret = wxFONTFAMILY_ROMAN; // begins with "Times"
197 else if (strncmp( family_text, "old", 3 ) == 0)
198 ret = wxFONTFAMILY_DECORATIVE; // Begins with "Old" - "Old English", "Old Town"
199 }
200
201 free(family_text);
202 return ret;
203 }
204
205 wxFontEncoding wxNativeFontInfo::GetEncoding() const
206 {
207 return wxFONTENCODING_SYSTEM;
208 }
209
210
211 void wxNativeFontInfo::SetPointSize(int pointsize)
212 {
213 pango_font_description_set_size( description, pointsize * PANGO_SCALE );
214 }
215
216 void wxNativeFontInfo::SetStyle(wxFontStyle style)
217 {
218 switch (style)
219 {
220 case wxFONTSTYLE_ITALIC:
221 pango_font_description_set_style( description, PANGO_STYLE_ITALIC );
222 break;
223 case wxFONTSTYLE_SLANT:
224 pango_font_description_set_style( description, PANGO_STYLE_OBLIQUE );
225 break;
226 default:
227 wxFAIL_MSG( _T("unknown font style") );
228 // fall through
229 case wxFONTSTYLE_NORMAL:
230 pango_font_description_set_style( description, PANGO_STYLE_NORMAL );
231 break;
232 }
233 }
234
235 void wxNativeFontInfo::SetWeight(wxFontWeight weight)
236 {
237 switch (weight)
238 {
239 case wxFONTWEIGHT_BOLD:
240 pango_font_description_set_weight(description, PANGO_WEIGHT_BOLD);
241 break;
242 case wxFONTWEIGHT_LIGHT:
243 pango_font_description_set_weight(description, PANGO_WEIGHT_LIGHT);
244 break;
245 default:
246 wxFAIL_MSG( _T("unknown font weight") );
247 // fall through
248 case wxFONTWEIGHT_NORMAL:
249 pango_font_description_set_weight(description, PANGO_WEIGHT_NORMAL);
250 }
251 }
252
253 void wxNativeFontInfo::SetUnderlined(bool WXUNUSED(underlined))
254 {
255 wxFAIL_MSG( _T("not implemented") );
256 }
257
258 void wxNativeFontInfo::SetFaceName(const wxString& facename)
259 {
260 pango_font_description_set_family(description, wxGTK_CONV_SYS(facename));
261 }
262
263 void wxNativeFontInfo::SetFamily(wxFontFamily WXUNUSED(family))
264 {
265 wxFAIL_MSG( _T("not implemented") );
266 }
267
268 void wxNativeFontInfo::SetEncoding(wxFontEncoding WXUNUSED(encoding))
269 {
270 wxFAIL_MSG( _T("not implemented") );
271 }
272
273
274
275 bool wxNativeFontInfo::FromString(const wxString& s)
276 {
277 if (description)
278 pango_font_description_free( description );
279
280 description = pango_font_description_from_string( wxGTK_CONV_SYS( s ) );
281
282 return true;
283 }
284
285 wxString wxNativeFontInfo::ToString() const
286 {
287 char *str = pango_font_description_to_string( description );
288 wxString tmp = wxGTK_CONV_BACK( str );
289 g_free( str );
290
291 return tmp;
292 }
293
294 bool wxNativeFontInfo::FromUserString(const wxString& s)
295 {
296 return FromString( s );
297 }
298
299 wxString wxNativeFontInfo::ToUserString() const
300 {
301 return ToString();
302 }
303
304 // ----------------------------------------------------------------------------
305 // wxNativeEncodingInfo
306 // ----------------------------------------------------------------------------
307
308 bool wxNativeEncodingInfo::FromString(const wxString& WXUNUSED(s))
309 {
310 return false;
311 }
312
313 wxString wxNativeEncodingInfo::ToString() const
314 {
315 return wxEmptyString;
316 }
317
318 bool wxTestFontEncoding(const wxNativeEncodingInfo& WXUNUSED(info))
319 {
320 return true;
321 }
322
323 bool wxGetNativeFontEncoding(wxFontEncoding encoding,
324 wxNativeEncodingInfo *info)
325 {
326 // all encodings are available in GTK+ 2 because we translate text in any
327 // encoding to UTF-8 internally anyhow
328 info->facename.clear();
329 info->encoding = encoding;
330
331 return true;
332 }
333
334 #else // GTK+ 1.x
335
336 #ifdef __X__
337 #ifdef __VMS__
338 #pragma message disable nosimpint
339 #endif
340
341 #include <X11/Xlib.h>
342
343 #ifdef __VMS__
344 #pragma message enable nosimpint
345 #endif
346
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.empty() )
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, wxEmptyString, _T("invalid XLFD field") );
511
512 if ( !HasElements() )
513 {
514 // const_cast
515 if ( !((wxNativeFontInfo *)this)->FromXFontName(xFontName) )
516 return wxEmptyString;
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(const wxString& facename)
752 {
753 SetXFontComponent(wxXLFD_FAMILY, facename);
754 }
755
756 void wxNativeFontInfo::SetFamily(wxFontFamily WXUNUSED(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->empty() )
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.empty())
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