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