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