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