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