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