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