]> git.saurik.com Git - wxWidgets.git/blame - src/unix/fontutil.cpp
wxRTC: save and load the 'shown' status in case there's a situation where layout...
[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
7beba2fc 7// Copyright: (c) Vadim Zeitlin
65571936 8// Licence: wxWindows licence
7beba2fc
VZ
9/////////////////////////////////////////////////////////////////////////////
10
11// ============================================================================
12// declarations
13// ============================================================================
14
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
18
7beba2fc
VZ
19// For compilers that support precompilation, includes "wx.h".
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
23 #pragma hdrstop
24#endif
25
d48d1eae
WS
26#include "wx/fontutil.h"
27
7beba2fc 28#ifndef WX_PRECOMP
d48d1eae 29 #include "wx/app.h"
0f6858b6 30 #include "wx/font.h" // wxFont enums
32d4c30a 31 #include "wx/hash.h"
de6185e2 32 #include "wx/utils.h" // for wxGetDisplay()
02761f6c 33 #include "wx/module.h"
7beba2fc
VZ
34#endif // PCH
35
c8723b86 36#include "wx/encinfo.h"
db16cab4
RR
37#include "wx/fontmap.h"
38#include "wx/tokenzr.h"
85ab460e 39#include "wx/fontenum.h"
db16cab4 40
2b5f62a0
VZ
41#if wxUSE_PANGO
42
43#include "pango/pango.h"
db16cab4 44
2b5f62a0 45#ifdef __WXGTK20__
29131e8a
VZ
46 #include "wx/gtk/private.h"
47 extern GtkWidget *wxGetRootWindow();
48
49 #define wxPANGO_CONV wxGTK_CONV_SYS
47239bd6 50 #define wxPANGO_CONV_BACK wxGTK_CONV_BACK_SYS
2b5f62a0 51#else
29131e8a
VZ
52 #include "wx/x11/private.h"
53 #include "wx/gtk/private/string.h"
54
0b2a860f
VZ
55 #define wxPANGO_CONV(s) s.utf8_str()
56 #define wxPANGO_CONV_BACK(s) wxString::FromUTF8Unchecked(s)
2b5f62a0 57#endif
db16cab4
RR
58
59// ----------------------------------------------------------------------------
60// wxNativeFontInfo
61// ----------------------------------------------------------------------------
62
63void wxNativeFontInfo::Init()
64{
65 description = NULL;
a349dc10
VZ
66 m_underlined = false;
67 m_strikethrough = false;
db16cab4
RR
68}
69
c45f5ae8 70void wxNativeFontInfo::Init(const wxNativeFontInfo& info)
fdf7514a
VS
71{
72 if (info.description)
a349dc10 73 {
fdf7514a 74 description = pango_font_description_copy(info.description);
a349dc10
VZ
75 m_underlined = info.GetUnderlined();
76 m_strikethrough = info.GetStrikethrough();
77 }
fdf7514a 78 else
a349dc10 79 {
82680055 80 description = NULL;
a349dc10
VZ
81 m_underlined = false;
82 m_strikethrough = false;
83 }
fdf7514a
VS
84}
85
82680055 86void wxNativeFontInfo::Free()
fdf7514a
VS
87{
88 if (description)
89 pango_font_description_free(description);
90}
91
db16cab4
RR
92int wxNativeFontInfo::GetPointSize() const
93{
94 return pango_font_description_get_size( description ) / PANGO_SCALE;
95}
96
97wxFontStyle 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 }
2b5f62a0 113
db16cab4
RR
114 return m_style;
115}
116
117wxFontWeight wxNativeFontInfo::GetWeight() const
118{
0f6858b6
RR
119 // We seem to currently initialize only by string.
120 // In that case PANGO_FONT_MASK_WEIGHT is always set.
6aea1e4a
FM
121 // if (!(pango_font_description_get_set_fields(description) & PANGO_FONT_MASK_WEIGHT))
122 // return wxFONTWEIGHT_NORMAL;
db16cab4 123
0f6858b6
RR
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;
2b5f62a0 136
0f6858b6 137 return wxFONTWEIGHT_NORMAL;
db16cab4
RR
138}
139
140bool wxNativeFontInfo::GetUnderlined() const
141{
a349dc10 142 return m_underlined;
db16cab4
RR
143}
144
ed7f03bb
VZ
145bool wxNativeFontInfo::GetStrikethrough() const
146{
a349dc10 147 return m_strikethrough;
ed7f03bb
VZ
148}
149
db16cab4
RR
150wxString wxNativeFontInfo::GetFaceName() const
151{
c45f5ae8 152 // the Pango "family" is the wx "face name"
47239bd6 153 return wxPANGO_CONV_BACK(pango_font_description_get_family(description));
db16cab4
RR
154}
155
156wxFontFamily wxNativeFontInfo::GetFamily() const
157{
6aea1e4a 158 wxFontFamily ret = wxFONTFAMILY_UNKNOWN;
c45f5ae8
FM
159
160 const char *family_name = pango_font_description_get_family( description );
161
34a35823
MW
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.
23f4f495
VZ
165 if ( !family_name )
166 return ret;
8361f92b
VZ
167 wxGtkString family_text(g_ascii_strdown(family_name, strlen(family_name)));
168
6aea1e4a
FM
169 // Check for some common fonts, to salvage what we can from the current
170 // win32 centric wxFont API:
36cc7650 171 if (wxStrnicmp( family_text, "monospace", 9 ) == 0)
6aea1e4a 172 ret = wxFONTFAMILY_TELETYPE; // begins with "Monospace"
36cc7650 173 else if (wxStrnicmp( family_text, "courier", 7 ) == 0)
6aea1e4a 174 ret = wxFONTFAMILY_TELETYPE; // begins with "Courier"
ff654490 175#if defined(__WXGTK20__) || defined(HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE)
b67d14be
MR
176 else
177 {
178 PangoFontFamily **families;
b20cb045 179 PangoFontFamily *family = NULL;
b67d14be
MR
180 int n_families;
181 pango_context_list_families(
38d446db 182#ifdef __WXGTK20__
b67d14be 183 gtk_widget_get_pango_context( wxGetRootWindow() ),
38d446db 184#else
b67d14be 185 wxTheApp->GetPangoContext(),
38d446db 186#endif
b67d14be 187 &families, &n_families);
38d446db 188
c45f5ae8 189 for (int i = 0; i < n_families; ++i)
38d446db 190 {
c45f5ae8
FM
191 if (g_ascii_strcasecmp(pango_font_family_get_name( families[i] ),
192 pango_font_description_get_family( description )) == 0 )
b67d14be
MR
193 {
194 family = families[i];
195 break;
196 }
38d446db 197 }
38d446db 198
b67d14be 199 g_free(families);
38d446db 200
03647350 201 // Some gtk+ systems might query for a non-existing font from
c45f5ae8
FM
202 // wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT) on initialization,
203 // don't assert until wxSystemSettings::GetFont is checked for this - MR
6aea1e4a 204 // wxASSERT_MSG( family, "No appropriate PangoFontFamily found for ::description" );
38d446db 205
b20cb045 206 if (family != NULL && pango_font_family_is_monospace( family ))
b67d14be
MR
207 ret = wxFONTFAMILY_TELETYPE; // is deemed a monospace font by pango
208 }
ff654490 209#endif // GTK+ 2 || HAVE_PANGO_FONT_FAMILY_IS_MONOSPACE
38d446db 210
6aea1e4a 211 if (ret == wxFONTFAMILY_UNKNOWN)
b67d14be 212 {
6aea1e4a 213 if (strstr( family_text, "sans" ) != NULL || strstr( family_text, "Sans" ) != NULL)
c45f5ae8 214 // checked before serif, so that "* Sans Serif" fonts are detected correctly
6aea1e4a
FM
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"
36cc7650 218 else if (wxStrnicmp( family_text, "times", 5 ) == 0)
6aea1e4a 219 ret = wxFONTFAMILY_ROMAN; // begins with "Times"
36cc7650 220 else if (wxStrnicmp( family_text, "old", 3 ) == 0)
6aea1e4a 221 ret = wxFONTFAMILY_DECORATIVE; // begins with "Old" - "Old English", "Old Town"
b67d14be
MR
222 }
223
b67d14be 224 return ret;
db16cab4
RR
225}
226
227wxFontEncoding wxNativeFontInfo::GetEncoding() const
228{
229 return wxFONTENCODING_SYSTEM;
230}
231
8a15e8ba 232void wxNativeFontInfo::SetPointSize(int pointsize)
3398cf2c 233{
8a15e8ba 234 pango_font_description_set_size( description, pointsize * PANGO_SCALE );
3398cf2c
VZ
235}
236
7533ba25 237void wxNativeFontInfo::SetStyle(wxFontStyle style)
3398cf2c 238{
7533ba25
MR
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:
6aea1e4a 248 wxFAIL_MSG( "unknown font style" );
7533ba25
MR
249 // fall through
250 case wxFONTSTYLE_NORMAL:
251 pango_font_description_set_style( description, PANGO_STYLE_NORMAL );
252 break;
253 }
3398cf2c
VZ
254}
255
7533ba25 256void wxNativeFontInfo::SetWeight(wxFontWeight weight)
3398cf2c 257{
7533ba25
MR
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:
6aea1e4a 267 wxFAIL_MSG( "unknown font weight" );
7533ba25
MR
268 // fall through
269 case wxFONTWEIGHT_NORMAL:
270 pango_font_description_set_weight(description, PANGO_WEIGHT_NORMAL);
271 }
3398cf2c
VZ
272}
273
a349dc10 274void wxNativeFontInfo::SetUnderlined(bool underlined)
3398cf2c 275{
a349dc10
VZ
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;
3398cf2c
VZ
279}
280
a349dc10 281void wxNativeFontInfo::SetStrikethrough(bool strikethrough)
e9270aaa 282{
a349dc10
VZ
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;
e9270aaa
VZ
286}
287
85ab460e 288bool wxNativeFontInfo::SetFaceName(const wxString& facename)
3398cf2c 289{
29131e8a 290 pango_font_description_set_family(description, wxPANGO_CONV(facename));
6aea1e4a 291
3a6a0082
FM
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
85ab460e 295 return true;
3398cf2c
VZ
296}
297
6aea1e4a 298void wxNativeFontInfo::SetFamily(wxFontFamily family)
3398cf2c 299{
6aea1e4a
FM
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
1b636985 320 facename.Add(wxS("Serif"));
6aea1e4a 321 facename.Add(wxS("DejaVu Serif"));
1b636985
VZ
322 facename.Add(wxS("DejaVu LGC Serif"));
323 facename.Add(wxS("Bitstream Vera Serif"));
324 facename.Add(wxS("Liberation Serif"));
6aea1e4a 325 facename.Add(wxS("FreeSerif"));
1b636985 326 facename.Add(wxS("Luxi Serif"));
6aea1e4a 327 facename.Add(wxS("Times New Roman"));
1b636985
VZ
328 facename.Add(wxS("Century Schoolbook L"));
329 facename.Add(wxS("URW Bookman L"));
330 facename.Add(wxS("URW Palladio L"));
6aea1e4a
FM
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
1b636985 337 facename.Add(wxS("Monospace"));
6aea1e4a 338 facename.Add(wxS("DejaVu Sans Mono"));
1b636985 339 facename.Add(wxS("DejaVu LGC Sans Mono"));
6aea1e4a 340 facename.Add(wxS("Bitstream Vera Sans Mono"));
1b636985 341 facename.Add(wxS("Liberation Mono"));
6aea1e4a 342 facename.Add(wxS("FreeMono"));
1b636985 343 facename.Add(wxS("Luxi Mono"));
6aea1e4a 344 facename.Add(wxS("Courier New"));
1b636985
VZ
345 facename.Add(wxS("Lucida Sans Typewriter"));
346 facename.Add(wxS("Nimbus Mono L"));
347 facename.Add(wxS("Andale Mono"));
6aea1e4a
FM
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
1b636985 355 facename.Add(wxS("Sans"));
6aea1e4a 356 facename.Add(wxS("DejaVu Sans"));
1b636985 357 facename.Add(wxS("DejaVu LGC Sans"));
6aea1e4a 358 facename.Add(wxS("Bitstream Vera Sans"));
1b636985 359 facename.Add(wxS("Liberation Sans"));
6aea1e4a 360 facename.Add(wxS("FreeSans"));
1b636985
VZ
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"));
6aea1e4a
FM
366 break;
367 }
368
369 SetFaceName(facename);
3398cf2c
VZ
370}
371
372void wxNativeFontInfo::SetEncoding(wxFontEncoding WXUNUSED(encoding))
373{
6aea1e4a 374 wxFAIL_MSG( "not implemented: Pango encoding is always UTF8" );
3398cf2c
VZ
375}
376
db16cab4
RR
377bool wxNativeFontInfo::FromString(const wxString& s)
378{
a349dc10
VZ
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
db16cab4
RR
387 if (description)
388 pango_font_description_free( description );
389
7c55c50e
VZ
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 !!
6aea1e4a 397 const size_t pos = str.find_last_of(wxS(" "));
7c55c50e
VZ
398 double size;
399 if ( pos != wxString::npos && wxString(str, pos + 1).ToDouble(&size) )
400 {
401 wxString sizeStr;
402 if ( size < 1 )
6aea1e4a 403 sizeStr = wxS("1");
c23d4004 404 else if ( size >= 1E6 )
6aea1e4a 405 sizeStr = wxS("1E6");
7c55c50e
VZ
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
29131e8a 414 description = pango_font_description_from_string(wxPANGO_CONV(str));
db16cab4 415
8d22935d 416#if wxUSE_FONTENUM
85ab460e
VZ
417 // ensure a valid facename is selected
418 if (!wxFontEnumerator::IsValidFacename(GetFaceName()))
419 SetFaceName(wxNORMAL_FONT->GetFaceName());
8d22935d 420#endif // wxUSE_FONTENUM
85ab460e 421
55034339 422 return true;
db16cab4
RR
423}
424
425wxString wxNativeFontInfo::ToString() const
426{
8361f92b 427 wxGtkString str(pango_font_description_to_string( description ));
a349dc10
VZ
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;
db16cab4
RR
440}
441
442bool wxNativeFontInfo::FromUserString(const wxString& s)
443{
444 return FromString( s );
445}
446
447wxString wxNativeFontInfo::ToUserString() const
448{
449 return ToString();
450}
451
2b5f62a0 452#else // GTK+ 1.x
db16cab4 453
79e4b627 454#ifdef __X__
4ea45e6b
VZ
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
79e4b627 464
79e4b627 465#elif defined(__WXGTK__)
4ea45e6b
VZ
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>
79e4b627
VZ
473#endif
474
2e0e025e
RR
475
476// ----------------------------------------------------------------------------
477// private data
478// ----------------------------------------------------------------------------
479
d3b9f782 480static wxHashTable *g_fontHash = NULL;
7beba2fc
VZ
481
482// ----------------------------------------------------------------------------
483// private functions
484// ----------------------------------------------------------------------------
485
486// define the functions to create and destroy native fonts for this toolkit
487#ifdef __X__
3fe34ed0 488 wxNativeFont wxLoadFont(const wxString& fontSpec)
7beba2fc
VZ
489 {
490 return XLoadQueryFont((Display *)wxGetDisplay(), fontSpec);
491 }
492
409d5a58 493 inline void wxFreeFont(wxNativeFont font)
7beba2fc 494 {
a5fc62a1 495 XFreeFont((Display *)wxGetDisplay(), (XFontStruct *)font);
7beba2fc
VZ
496 }
497#elif defined(__WXGTK__)
3fe34ed0 498 wxNativeFont wxLoadFont(const wxString& fontSpec)
7beba2fc 499 {
8fa3a431
VZ
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) );
7beba2fc
VZ
507 }
508
409d5a58 509 inline void wxFreeFont(wxNativeFont font)
7beba2fc
VZ
510 {
511 gdk_font_unref(font);
512 }
513#else
514 #error "Unknown GUI toolkit"
515#endif
516
517static bool wxTestFontSpec(const wxString& fontspec);
518
519static wxNativeFont wxLoadQueryFont(int pointSize,
520 int family,
521 int style,
522 int weight,
523 bool underlined,
524 const wxString& facename,
525 const wxString& xregistry,
30764ab5
VZ
526 const wxString& xencoding,
527 wxString* xFontName);
7beba2fc
VZ
528
529// ============================================================================
530// implementation
531// ============================================================================
532
533// ----------------------------------------------------------------------------
534// wxNativeEncodingInfo
535// ----------------------------------------------------------------------------
536
537// convert to/from the string representation: format is
1e1d0be1 538// encodingid;registry;encoding[;facename]
7beba2fc
VZ
539bool wxNativeEncodingInfo::FromString(const wxString& s)
540{
6c49baf2 541 // use ";", not "-" because it may be part of encoding name
9a83f860 542 wxStringTokenizer tokenizer(s, wxT(";"));
1e1d0be1
VS
543
544 wxString encid = tokenizer.GetNextToken();
545 long enc;
546 if ( !encid.ToLong(&enc) )
55034339 547 return false;
1e1d0be1 548 encoding = (wxFontEncoding)enc;
7beba2fc
VZ
549
550 xregistry = tokenizer.GetNextToken();
551 if ( !xregistry )
55034339 552 return false;
7beba2fc
VZ
553
554 xencoding = tokenizer.GetNextToken();
555 if ( !xencoding )
55034339 556 return false;
7beba2fc
VZ
557
558 // ok even if empty
559 facename = tokenizer.GetNextToken();
560
55034339 561 return true;
7beba2fc
VZ
562}
563
564wxString wxNativeEncodingInfo::ToString() const
565{
566 wxString s;
9a83f860 567 s << (long)encoding << wxT(';') << xregistry << wxT(';') << xencoding;
55034339 568 if ( !facename.empty() )
7beba2fc 569 {
9a83f860 570 s << wxT(';') << facename;
7beba2fc
VZ
571 }
572
573 return s;
574}
575
ab5fe833
VZ
576// ----------------------------------------------------------------------------
577// wxNativeFontInfo
578// ----------------------------------------------------------------------------
579
580void wxNativeFontInfo::Init()
581{
55034339 582 m_isDefault = true;
ab5fe833
VZ
583}
584
585bool wxNativeFontInfo::FromString(const wxString& s)
586{
9a83f860 587 wxStringTokenizer tokenizer(s, wxT(";"));
ab5fe833
VZ
588
589 // check the version
590 wxString token = tokenizer.GetNextToken();
9a83f860 591 if ( token != wxT('0') )
55034339 592 return false;
ab5fe833
VZ
593
594 xFontName = tokenizer.GetNextToken();
595
596 // this should be the end
597 if ( tokenizer.HasMoreTokens() )
55034339 598 return false;
ab5fe833
VZ
599
600 return FromXFontName(xFontName);
601}
602
603wxString wxNativeFontInfo::ToString() const
604{
605 // 0 is the version
9a83f860 606 return wxString::Format(wxT("%d;%s"), 0, GetXFontName().c_str());
ab5fe833
VZ
607}
608
609bool wxNativeFontInfo::FromUserString(const wxString& s)
610{
611 return FromXFontName(s);
612}
613
614wxString wxNativeFontInfo::ToUserString() const
615{
616 return GetXFontName();
617}
618
409d5a58
VZ
619bool 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
53f6aab7
VZ
626wxString wxNativeFontInfo::GetXFontComponent(wxXLFDField field) const
627{
9a83f860 628 wxCHECK_MSG( field < wxXLFD_MAX, wxEmptyString, wxT("invalid XLFD field") );
53f6aab7 629
409d5a58 630 if ( !HasElements() )
53f6aab7 631 {
f48a1159 632 if ( !const_cast<wxNativeFontInfo *>(this)->FromXFontName(xFontName) )
55034339 633 return wxEmptyString;
53f6aab7
VZ
634 }
635
636 return fontElements[field];
637}
638
295272bd 639bool wxNativeFontInfo::FromXFontName(const wxString& fontname)
ab5fe833
VZ
640{
641 // TODO: we should be able to handle the font aliases here, but how?
9a83f860 642 wxStringTokenizer tokenizer(fontname, wxT("-"));
409d5a58
VZ
643
644 // skip the leading, usually empty field (font name registry)
645 if ( !tokenizer.HasMoreTokens() )
55034339 646 return false;
409d5a58
VZ
647
648 (void)tokenizer.GetNextToken();
ab5fe833
VZ
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
55034339 655 return false;
ab5fe833
VZ
656 }
657
b4e4abb5 658 wxString field = tokenizer.GetNextToken();
9a83f860 659 if ( !field.empty() && field != wxT('*') )
b4e4abb5
VZ
660 {
661 // we're really initialized now
55034339 662 m_isDefault = false;
b4e4abb5
VZ
663 }
664
665 fontElements[n] = field;
ab5fe833
VZ
666 }
667
668 // this should be all
011ba5ed 669 if ( tokenizer.HasMoreTokens() )
55034339 670 return false;
011ba5ed 671
55034339 672 return true;
ab5fe833
VZ
673}
674
675wxString 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];
409d5a58 684 if ( elt.empty() && n != wxXLFD_ADDSTYLE )
ab5fe833 685 {
9a83f860 686 elt = wxT('*');
ab5fe833
VZ
687 }
688
f48a1159 689 const_cast<wxNativeFontInfo *>(this)->xFontName << wxT('-') << elt;
ab5fe833
VZ
690 }
691 }
692
693 return xFontName;
694}
695
409d5a58
VZ
696void
697wxNativeFontInfo::SetXFontComponent(wxXLFDField field, const wxString& value)
698{
9a83f860 699 wxCHECK_RET( field < wxXLFD_MAX, wxT("invalid XLFD field") );
409d5a58
VZ
700
701 // this class should be initialized with a valid font spec first and only
702 // then the fields may be modified!
9a83f860 703 wxASSERT_MSG( !IsDefault(), wxT("can't modify an uninitialized XLFD") );
409d5a58
VZ
704
705 if ( !HasElements() )
706 {
f48a1159 707 if ( !const_cast<wxNativeFontInfo *>(this)->FromXFontName(xFontName) )
409d5a58 708 {
9a83f860 709 wxFAIL_MSG( wxT("can't set font element for invalid XLFD") );
409d5a58
VZ
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
721void 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
55034339 728 m_isDefault = false;
409d5a58
VZ
729}
730
2b5f62a0
VZ
731int 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
740wxFontStyle 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
102798af 751 switch ( s[0].GetValue() )
2b5f62a0
VZ
752 {
753 default:
754 // again, unknown but consider normal by default
755
9a83f860 756 case wxT('r'):
2b5f62a0
VZ
757 return wxFONTSTYLE_NORMAL;
758
9a83f860 759 case wxT('i'):
2b5f62a0
VZ
760 return wxFONTSTYLE_ITALIC;
761
9a83f860 762 case wxT('o'):
2b5f62a0
VZ
763 return wxFONTSTYLE_SLANT;
764 }
765}
766
767wxFontWeight wxNativeFontInfo::GetWeight() const
768{
769 const wxString s = GetXFontComponent(wxXLFD_WEIGHT).MakeLower();
9a83f860 770 if ( s.find(wxT("bold")) != wxString::npos || s == wxT("black") )
2b5f62a0 771 return wxFONTWEIGHT_BOLD;
9a83f860 772 else if ( s == wxT("light") )
2b5f62a0
VZ
773 return wxFONTWEIGHT_LIGHT;
774
775 return wxFONTWEIGHT_NORMAL;
776}
777
778bool wxNativeFontInfo::GetUnderlined() const
779{
780 // X fonts are never underlined
55034339 781 return false;
2b5f62a0
VZ
782}
783
784wxString wxNativeFontInfo::GetFaceName() const
785{
77ffb593 786 // wxWidgets facename probably more accurately corresponds to X family
2b5f62a0
VZ
787 return GetXFontComponent(wxXLFD_FAMILY);
788}
789
790wxFontFamily wxNativeFontInfo::GetFamily() const
791{
77ffb593 792 // and wxWidgets family -- to X foundry, but we have to translate it to
2b5f62a0 793 // wxFontFamily somehow...
9a83f860 794 wxFAIL_MSG(wxT("not implemented")); // GetXFontComponent(wxXLFD_FOUNDRY);
2b5f62a0
VZ
795
796 return wxFONTFAMILY_DEFAULT;
797}
798
799wxFontEncoding wxNativeFontInfo::GetEncoding() const
800{
801 // we already have the code for this but need to refactor it first
9a83f860 802 wxFAIL_MSG( wxT("not implemented") );
2b5f62a0
VZ
803
804 return wxFONTENCODING_MAX;
805}
806
807void wxNativeFontInfo::SetPointSize(int pointsize)
808{
9a83f860 809 SetXFontComponent(wxXLFD_POINTSIZE, wxString::Format(wxT("%d"), pointsize));
2b5f62a0
VZ
810}
811
812void wxNativeFontInfo::SetStyle(wxFontStyle style)
813{
814 wxString s;
815 switch ( style )
816 {
817 case wxFONTSTYLE_ITALIC:
9a83f860 818 s = wxT('i');
2b5f62a0
VZ
819 break;
820
821 case wxFONTSTYLE_SLANT:
9a83f860 822 s = wxT('o');
2b5f62a0
VZ
823 break;
824
825 case wxFONTSTYLE_NORMAL:
9a83f860 826 s = wxT('r');
2b5f62a0
VZ
827
828 default:
9a83f860 829 wxFAIL_MSG( wxT("unknown wxFontStyle in wxNativeFontInfo::SetStyle") );
2b5f62a0
VZ
830 return;
831 }
832
833 SetXFontComponent(wxXLFD_SLANT, s);
834}
835
836void wxNativeFontInfo::SetWeight(wxFontWeight weight)
837{
838 wxString s;
839 switch ( weight )
840 {
841 case wxFONTWEIGHT_BOLD:
9a83f860 842 s = wxT("bold");
2b5f62a0
VZ
843 break;
844
845 case wxFONTWEIGHT_LIGHT:
9a83f860 846 s = wxT("light");
2b5f62a0
VZ
847 break;
848
849 case wxFONTWEIGHT_NORMAL:
9a83f860 850 s = wxT("medium");
2b5f62a0
VZ
851 break;
852
853 default:
9a83f860 854 wxFAIL_MSG( wxT("unknown wxFontWeight in wxNativeFontInfo::SetWeight") );
2b5f62a0
VZ
855 return;
856 }
857
858 SetXFontComponent(wxXLFD_WEIGHT, s);
859}
860
861void wxNativeFontInfo::SetUnderlined(bool WXUNUSED(underlined))
862{
863 // can't do this under X
864}
865
ed7f03bb
VZ
866void wxNativeFontInfo::SetStrikethrough(bool WXUNUSED(strikethrough))
867{
868 // this is not supported by Pango fonts neither
869}
870
85ab460e 871bool wxNativeFontInfo::SetFaceName(const wxString& facename)
2b5f62a0
VZ
872{
873 SetXFontComponent(wxXLFD_FAMILY, facename);
85ab460e 874 return true;
2b5f62a0
VZ
875}
876
55034339 877void wxNativeFontInfo::SetFamily(wxFontFamily WXUNUSED(family))
2b5f62a0
VZ
878{
879 // wxFontFamily -> X foundry, anyone?
9a83f860 880 wxFAIL_MSG( wxT("not implemented") );
2b5f62a0
VZ
881
882 // SetXFontComponent(wxXLFD_FOUNDRY, ...);
883}
884
885void 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
7beba2fc
VZ
895// ----------------------------------------------------------------------------
896// common functions
897// ----------------------------------------------------------------------------
898
899bool wxGetNativeFontEncoding(wxFontEncoding encoding,
900 wxNativeEncodingInfo *info)
901{
9a83f860 902 wxCHECK_MSG( info, false, wxT("bad pointer in wxGetNativeFontEncoding") );
7beba2fc
VZ
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:
80a24267 922 case wxFONTENCODING_ISO8859_12:
7beba2fc
VZ
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
bb84929e 933 case wxFONTENCODING_UTF8:
5707316c 934 info->xregistry = wxT("iso10646");
bb84929e
VZ
935 info->xencoding = wxT("*");
936 break;
937
2b5f62a0 938 case wxFONTENCODING_GB2312:
82680055 939 info->xregistry = wxT("GB2312"); // or the otherway round?
2b5f62a0
VZ
940 info->xencoding = wxT("*");
941 break;
942
7beba2fc 943 case wxFONTENCODING_KOI8:
15ad38c3 944 case wxFONTENCODING_KOI8_U:
7beba2fc
VZ
945 info->xregistry = wxT("koi8");
946
5707316c 947 // we don't make distinction between koi8-r, koi8-u and koi8-ru (so far)
7beba2fc
VZ
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
d946915c
MB
966 case wxFONTENCODING_EUC_JP:
967 case wxFONTENCODING_SHIFT_JIS:
968 info->xregistry = "jis*";
969 info->xencoding = "*";
970 break;
971
7beba2fc
VZ
972 case wxFONTENCODING_SYSTEM:
973 info->xregistry =
81c67e27 974 info->xencoding = wxT("*");
7beba2fc
VZ
975 break;
976
977 default:
978 // don't know how to translate this encoding into X fontspec
55034339 979 return false;
7beba2fc
VZ
980 }
981
ab5fe833 982 info->encoding = encoding;
6b0eebc5 983
55034339 984 return true;
7beba2fc
VZ
985}
986
987bool wxTestFontEncoding(const wxNativeEncodingInfo& info)
988{
989 wxString fontspec;
9a83f860 990 fontspec.Printf(wxT("-*-%s-*-*-*-*-*-*-*-*-*-*-%s-%s"),
35f6f71b
VZ
991 info.facename.empty() ? wxString("*") : info.facename,
992 info.xregistry,
993 info.xencoding);
7beba2fc
VZ
994
995 return wxTestFontSpec(fontspec);
996}
997
998// ----------------------------------------------------------------------------
999// X-specific functions
1000// ----------------------------------------------------------------------------
1001
1002wxNativeFont wxLoadQueryNearestFont(int pointSize,
1003 int family,
1004 int style,
1005 int weight,
1006 bool underlined,
1007 const wxString &facename,
30764ab5
VZ
1008 wxFontEncoding encoding,
1009 wxString* xFontName)
7beba2fc 1010{
97d3f0ee
VZ
1011 if ( encoding == wxFONTENCODING_DEFAULT )
1012 {
1013 encoding = wxFont::GetDefaultEncoding();
1014 }
1015
7beba2fc
VZ
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;
97d3f0ee 1020 if ( encoding == wxFONTENCODING_SYSTEM )
81c67e27
RR
1021 {
1022 // This will always work so we don't test to save time
1023 wxGetNativeFontEncoding(wxFONTENCODING_SYSTEM, &info);
1024 }
1025 else
7beba2fc 1026 {
81c67e27
RR
1027 if ( !wxGetNativeFontEncoding(encoding, &info) ||
1028 !wxTestFontEncoding(info) )
7beba2fc 1029 {
1e6feb95 1030#if wxUSE_FONTMAP
142b3bc2 1031 if ( !wxFontMapper::Get()->GetAltForEncoding(encoding, &info) )
1e6feb95 1032#endif // wxUSE_FONTMAP
81c67e27
RR
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 }
97d3f0ee 1041 }
7beba2fc 1042 }
6c49baf2 1043
81c67e27 1044 // OK, we have the correct xregistry/xencoding in info structure
30764ab5
VZ
1045 wxNativeFont font = 0;
1046
1047 // if we already have the X font name, try to use it
55034339 1048 if( xFontName && !xFontName->empty() )
6c49baf2
VZ
1049 {
1050 //
1051 // Make sure point size is correct for scale factor.
1052 //
9a83f860 1053 wxStringTokenizer tokenizer(*xFontName, wxT("-"), wxTOKEN_RET_DELIMS);
6c49baf2
VZ
1054 wxString newFontName;
1055
1056 for(int i = 0; i < 8; i++)
1057 newFontName += tokenizer.NextToken();
1058
1059 (void) tokenizer.NextToken();
1060
401eb3de 1061 newFontName += wxString::Format(wxT("%d-"), pointSize);
6c49baf2
VZ
1062
1063 while(tokenizer.HasMoreTokens())
1064 newFontName += tokenizer.GetNextToken();
1065
1066 font = wxLoadFont(newFontName);
1067
1068 if(font)
1069 *xFontName = newFontName;
1070 }
30764ab5 1071
7beba2fc
VZ
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
34791896 1078 int i, round; // counters
7beba2fc 1079
03647350 1080 // first round: search for equal, then for smaller and for larger size
c45f5ae8 1081 // with the given weight and style
34791896
RR
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 )
b12401e0 1089 {
34791896
RR
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,
30764ab5
VZ
1116 facename, info.xregistry, info.xencoding,
1117 xFontName);
b12401e0 1118 }
7beba2fc 1119
b12401e0
MB
1120 // Search for larger size (approx.)
1121 for ( i = pointSize + 10; !font && i <= max_size; i += 10 )
1122 {
34791896 1123 font = wxLoadQueryFont(i, family, teststyle, testweight, underlined,
30764ab5
VZ
1124 facename, info.xregistry, info.xencoding,
1125 xFontName);
34791896 1126 }
7beba2fc
VZ
1127 }
1128
1129 // Try default family
1130 if ( !font && family != wxDEFAULT )
1131 {
1132 font = wxLoadQueryFont(pointSize, wxDEFAULT, style, weight,
1133 underlined, facename,
30764ab5
VZ
1134 info.xregistry, info.xencoding,
1135 xFontName );
7beba2fc
VZ
1136 }
1137
f139dfe8
VZ
1138 // ignore size, family, style and weight but try to find font with the
1139 // given facename and encoding
7beba2fc
VZ
1140 if ( !font )
1141 {
1142 font = wxLoadQueryFont(120, wxDEFAULT, wxNORMAL, wxNORMAL,
1143 underlined, facename,
30764ab5
VZ
1144 info.xregistry, info.xencoding,
1145 xFontName);
7beba2fc 1146
f139dfe8
VZ
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,
55034339 1162 false, wxEmptyString,
f139dfe8
VZ
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 {
9a83f860 1171 wxFAIL_MSG( wxT("this encoding should be available!") );
f139dfe8
VZ
1172
1173 font = wxLoadQueryFont(-1,
1174 wxDEFAULT, wxNORMAL, wxNORMAL,
55034339 1175 false, wxEmptyString,
9a83f860 1176 wxT("*"), wxT("*"),
f139dfe8
VZ
1177 xFontName);
1178 }
1179 }
1180 }
7beba2fc
VZ
1181 }
1182 }
1183
1184 return font;
1185}
1186
1187// ----------------------------------------------------------------------------
1188// private functions
1189// ----------------------------------------------------------------------------
1190
55034339 1191// returns true if there are any fonts matching this font spec
7beba2fc
VZ
1192static bool wxTestFontSpec(const wxString& fontspec)
1193{
97d3f0ee
VZ
1194 // some X servers will fail to load this font because there are too many
1195 // matches so we must test explicitly for this
9a83f860 1196 if ( fontspec == wxT("-*-*-*-*-*-*-*-*-*-*-*-*-*-*") )
97d3f0ee 1197 {
55034339 1198 return true;
97d3f0ee
VZ
1199 }
1200
2e0e025e
RR
1201 wxNativeFont test = (wxNativeFont) g_fontHash->Get( fontspec );
1202 if (test)
1203 {
55034339 1204 return true;
2e0e025e
RR
1205 }
1206
1207 test = wxLoadFont(fontspec);
1208 g_fontHash->Put( fontspec, (wxObject*) test );
6c49baf2 1209
7beba2fc
VZ
1210 if ( test )
1211 {
1212 wxFreeFont(test);
1213
55034339 1214 return true;
7beba2fc
VZ
1215 }
1216 else
1217 {
55034339 1218 return false;
7beba2fc
VZ
1219 }
1220}
1221
1222static 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,
30764ab5
VZ
1229 const wxString& xencoding,
1230 wxString* xFontName)
7beba2fc
VZ
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 }
16d865f7 1243#if wxUSE_NANOX
c2ff68d3
JS
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 }
16d865f7
JS
1269 GR_SCREEN_INFO screenInfo;
1270 GrGetScreenInfo(& screenInfo);
1271
1272 int yPixelsPerCM = screenInfo.ydpcm;
1273
0a68bc69 1274 // A point is 1/72 of an inch.
16d865f7 1275 // An inch is 2.541 cm.
0a68bc69 1276 // So pixelHeight = (pointSize / 72) (inches) * 2.541 (for cm) * yPixelsPerCM (for pixels)
c2ff68d3
JS
1277 // In fact pointSize is 10 * the normal point size so
1278 // divide by 10.
1279
15d5a947 1280 int pixelHeight = (int) ( (((float)pointSize) / 720.0) * 2.541 * (float) yPixelsPerCM) ;
c2ff68d3
JS
1281
1282 // An alternative: assume that the screen is 72 dpi.
0a68bc69
JS
1283 //int pixelHeight = (int) (((float)pointSize / 720.0) * 72.0) ;
1284 //int pixelHeight = (int) ((float)pointSize / 10.0) ;
2b5f62a0 1285
16d865f7
JS
1286 GR_LOGFONT logFont;
1287 logFont.lfHeight = pixelHeight;
1288 logFont.lfWidth = 0;
1289 logFont.lfEscapement = 0;
1290 logFont.lfOrientation = 0;
c2ff68d3 1291 logFont.lfWeight = xweight;
16d865f7
JS
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;
2b5f62a0 1312
16d865f7 1313#else
7beba2fc 1314 wxString fontSpec;
55034339 1315 if (!facename.empty())
7beba2fc
VZ
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 {
f9dbf34f
VZ
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:
9a83f860 1365 wxFAIL_MSG(wxT("unknown font style"));
f9dbf34f
VZ
1366 // fall back to normal
1367
1368 case wxNORMAL:
1369 xstyle = wxT("r");
1370 break;
7beba2fc
VZ
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
f139dfe8
VZ
1469 // if pointSize is -1, don't specify any
1470 wxString sizeSpec;
73b0423d 1471 if ( pointSize == -1 )
f139dfe8 1472 {
9a83f860 1473 sizeSpec = wxT('*');
f139dfe8
VZ
1474 }
1475 else
1476 {
9a83f860 1477 sizeSpec.Printf(wxT("%d"), pointSize);
f139dfe8
VZ
1478 }
1479
7beba2fc 1480 // construct the X font spec from our data
f139dfe8 1481 fontSpec.Printf(wxT("-*-%s-%s-%s-normal-*-*-%s-*-*-*-*-%s-%s"),
7beba2fc 1482 xfamily.c_str(), xweight.c_str(), xstyle.c_str(),
f139dfe8 1483 sizeSpec.c_str(), xregistry.c_str(), xencoding.c_str());
7beba2fc 1484
30764ab5
VZ
1485 if( xFontName )
1486 *xFontName = fontSpec;
1487
7beba2fc 1488 return wxLoadFont(fontSpec);
16d865f7
JS
1489#endif
1490 // wxUSE_NANOX
7beba2fc
VZ
1491}
1492
2e0e025e
RR
1493// ----------------------------------------------------------------------------
1494// wxFontModule
1495// ----------------------------------------------------------------------------
1496
1497class wxFontModule : public wxModule
1498{
1499public:
1500 bool OnInit();
1501 void OnExit();
1502
1503private:
1504 DECLARE_DYNAMIC_CLASS(wxFontModule)
1505};
1506
1507IMPLEMENT_DYNAMIC_CLASS(wxFontModule, wxModule)
1508
1509bool wxFontModule::OnInit()
1510{
1511 g_fontHash = new wxHashTable( wxKEY_STRING );
1512
55034339 1513 return true;
2e0e025e
RR
1514}
1515
1516void wxFontModule::OnExit()
1517{
5276b0a5 1518 wxDELETE(g_fontHash);
2e0e025e 1519}
db16cab4 1520
2b5f62a0 1521#endif // GTK 2.0/1.x