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