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