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