]> git.saurik.com Git - wxWidgets.git/blame - src/common/fontcmn.cpp
wxGTK1 compilation fixes after last changes
[wxWidgets.git] / src / common / fontcmn.cpp
CommitLineData
8f7fa6f8 1/////////////////////////////////////////////////////////////////////////////
da80ae71 2// Name: src/common/fontcmn.cpp
0c5d3e1c
VZ
3// Purpose: implementation of wxFontBase methods
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 20.09.99
7// RCS-ID: $Id$
77ffb593 8// Copyright: (c) wxWidgets team
65571936 9// Licence: wxWindows licence
0c5d3e1c
VZ
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
da80ae71 24 #pragma hdrstop
0c5d3e1c
VZ
25#endif
26
da80ae71
WS
27#include "wx/font.h"
28
0c5d3e1c 29#ifndef WX_PRECOMP
452aa069 30 #include "wx/dc.h"
9342fdbe
VZ
31 #include "wx/intl.h"
32 #include "wx/dcscreen.h"
8d3ba63b 33 #include "wx/log.h"
dd05139a 34 #include "wx/gdicmn.h"
0c5d3e1c
VZ
35#endif // WX_PRECOMP
36
82ef81ed 37#if defined(__WXMSW__)
7bd236e6
WS
38 #include "wx/msw/private.h" // includes windows.h for LOGFONT
39 #include "wx/msw/winundef.h"
1c193821
JS
40#endif
41
76e23cdb 42#include "wx/fontutil.h" // for wxNativeFontInfo
ab5fe833 43#include "wx/fontmap.h"
85ab460e 44#include "wx/fontenum.h"
76e23cdb 45
30764ab5
VZ
46#include "wx/tokenzr.h"
47
4b6a582b
VZ
48// debugger helper: this function can be called from a debugger to show what
49// the date really is
50extern const char *wxDumpFont(const wxFont *font)
51{
52 static char buf[256];
53
54 const wxFontWeight weight = font->GetWeight();
55
56 wxString s;
57 s.Printf(wxS("%s-%s-%s-%d-%d"),
58 font->GetFaceName(),
59 weight == wxFONTWEIGHT_NORMAL
60 ? _T("normal")
61 : weight == wxFONTWEIGHT_BOLD
62 ? _T("bold")
63 : _T("light"),
64 font->GetStyle() == wxFONTSTYLE_NORMAL
65 ? _T("regular")
66 : _T("italic"),
67 font->GetPointSize(),
68 font->GetEncoding());
69
70 wxStrlcpy(buf, s, WXSIZEOF(buf));
71 return buf;
72}
73
0c5d3e1c
VZ
74// ============================================================================
75// implementation
76// ============================================================================
77
544229d1
VZ
78// ----------------------------------------------------------------------------
79// helper functions
80// ----------------------------------------------------------------------------
81
b5791cc7 82static inline int flags2Style(int flags)
544229d1 83{
b5791cc7
FM
84 return flags & wxFONTFLAG_ITALIC
85 ? wxFONTSTYLE_ITALIC
86 : flags & wxFONTFLAG_SLANT
87 ? wxFONTSTYLE_SLANT
88 : wxFONTSTYLE_NORMAL;
89}
544229d1 90
b5791cc7
FM
91static inline int flags2Weight(int flags)
92{
93 return flags & wxFONTFLAG_LIGHT
94 ? wxFONTWEIGHT_LIGHT
95 : flags & wxFONTFLAG_BOLD
96 ? wxFONTWEIGHT_BOLD
97 : wxFONTWEIGHT_NORMAL;
98}
544229d1 99
b5791cc7
FM
100static inline bool flags2Underlined(int flags)
101{
102 return (flags & wxFONTFLAG_UNDERLINED) != 0;
544229d1
VZ
103}
104
0c5d3e1c
VZ
105// ----------------------------------------------------------------------------
106// wxFontBase
107// ----------------------------------------------------------------------------
108
109wxFontEncoding wxFontBase::ms_encodingDefault = wxFONTENCODING_SYSTEM;
110
cafbf6fb
VZ
111/* static */
112void wxFontBase::SetDefaultEncoding(wxFontEncoding encoding)
113{
114 // GetDefaultEncoding() should return something != wxFONTENCODING_DEFAULT
115 // and, besides, using this value here doesn't make any sense
116 wxCHECK_RET( encoding != wxFONTENCODING_DEFAULT,
117 _T("can't set default encoding to wxFONTENCODING_DEFAULT") );
118
119 ms_encodingDefault = encoding;
120}
121
799ea011
GD
122wxFontBase::~wxFontBase()
123{
124 // this destructor is required for Darwin
125}
126
7beba2fc 127/* static */
0c5d3e1c 128wxFont *wxFontBase::New(int size,
0c14b6c3
FM
129 wxFontFamily family,
130 wxFontStyle style,
131 wxFontWeight weight,
0c5d3e1c
VZ
132 bool underlined,
133 const wxString& face,
134 wxFontEncoding encoding)
135{
136 return new wxFont(size, family, style, weight, underlined, face, encoding);
137}
138
b5791cc7
FM
139/* static */
140wxFont *wxFontBase::New(const wxSize& pixelSize,
141 wxFontFamily family,
142 wxFontStyle style,
143 wxFontWeight weight,
144 bool underlined,
145 const wxString& face,
146 wxFontEncoding encoding)
544229d1 147{
b5791cc7
FM
148 return new wxFont(pixelSize, family, style, weight, underlined,
149 face, encoding);
544229d1
VZ
150}
151
152/* static */
153wxFont *wxFontBase::New(int pointSize,
154 wxFontFamily family,
155 int flags,
156 const wxString& face,
157 wxFontEncoding encoding)
158{
159 return New(pointSize, family, flags2Style(flags), flags2Weight(flags),
160 flags2Underlined(flags), face, encoding);
161}
162
163/* static */
164wxFont *wxFontBase::New(const wxSize& pixelSize,
0c14b6c3 165 wxFontFamily family,
b5791cc7 166 int flags,
544229d1
VZ
167 const wxString& face,
168 wxFontEncoding encoding)
169{
b5791cc7
FM
170 return New(pixelSize, family, flags2Style(flags), flags2Weight(flags),
171 flags2Underlined(flags), face, encoding);
544229d1
VZ
172}
173
174/* static */
b5791cc7 175wxFont *wxFontBase::New(const wxNativeFontInfo& info)
544229d1 176{
b5791cc7
FM
177 return new wxFont(info);
178}
179
180/* static */
181wxFont *wxFontBase::New(const wxString& strNativeFontDesc)
182{
183 wxNativeFontInfo fontInfo;
184 if ( !fontInfo.FromString(strNativeFontDesc) )
185 return new wxFont(*wxNORMAL_FONT);
186
187 return New(fontInfo);
188}
189
190bool wxFontBase::IsFixedWidth() const
191{
192 return GetFamily() == wxFONTFAMILY_TELETYPE;
544229d1
VZ
193}
194
195wxSize wxFontBase::GetPixelSize() const
196{
197 wxScreenDC dc;
198 dc.SetFont(*(wxFont *)this);
199 return wxSize(dc.GetCharWidth(), dc.GetCharHeight());
200}
201
202bool wxFontBase::IsUsingSizeInPixels() const
203{
204 return false;
205}
206
207void wxFontBase::SetPixelSize( const wxSize& pixelSize )
208{
b5791cc7
FM
209 wxCHECK_RET( pixelSize.GetWidth() >= 0 && pixelSize.GetHeight() > 0,
210 "Negative values for the pixel size or zero pixel height are not allowed" );
211
544229d1 212 wxScreenDC dc;
01cb1c26 213
b5791cc7
FM
214 // NOTE: this algorithm for adjusting the font size is used by all
215 // implementations of wxFont except under wxMSW and wxGTK where
216 // native support to font creation using pixel-size is provided.
217
218 int largestGood = 0;
219 int smallestBad = 0;
30764ab5 220
b5791cc7
FM
221 bool initialGoodFound = false;
222 bool initialBadFound = false;
7826e2dd 223
b5791cc7
FM
224 // NB: this assignment was separated from the variable definition
225 // in order to fix a gcc v3.3.3 compiler crash
226 int currentSize = GetPointSize();
227 while (currentSize > 0)
228 {
229 dc.SetFont(*static_cast<wxFont*>(this));
7826e2dd 230
b5791cc7
FM
231 // if currentSize (in points) results in a font that is smaller
232 // than required by pixelSize it is considered a good size
233 // NOTE: the pixel size width may be zero
234 if (dc.GetCharHeight() <= pixelSize.GetHeight() &&
235 (pixelSize.GetWidth() == 0 ||
236 dc.GetCharWidth() <= pixelSize.GetWidth()))
237 {
238 largestGood = currentSize;
239 initialGoodFound = true;
240 }
241 else
242 {
243 smallestBad = currentSize;
244 initialBadFound = true;
245 }
246 if (!initialGoodFound)
247 {
248 currentSize /= 2;
249 }
250 else if (!initialBadFound)
251 {
252 currentSize *= 2;
253 }
254 else
255 {
256 int distance = smallestBad - largestGood;
257 if (distance == 1)
258 break;
259
260 currentSize = largestGood + distance / 2;
261 }
262
263 SetPointSize(currentSize);
264 }
265
266 if (currentSize != largestGood)
267 SetPointSize(largestGood);
53f6aab7
VZ
268}
269
9045ad9d 270void wxFontBase::DoSetNativeFontInfo(const wxNativeFontInfo& info)
30764ab5 271{
ab5fe833 272#ifdef wxNO_NATIVE_FONTINFO
30764ab5
VZ
273 SetPointSize(info.pointSize);
274 SetFamily(info.family);
275 SetStyle(info.style);
276 SetWeight(info.weight);
277 SetUnderlined(info.underlined);
278 SetFaceName(info.faceName);
279 SetEncoding(info.encoding);
33ac7e6f 280#else
1e6feb95 281 (void)info;
30764ab5
VZ
282#endif
283}
284
7826e2dd
VZ
285wxString wxFontBase::GetNativeFontInfoDesc() const
286{
287 wxString fontDesc;
3bf5a59b 288 const wxNativeFontInfo *fontInfo = GetNativeFontInfo();
7826e2dd
VZ
289 if ( fontInfo )
290 {
291 fontDesc = fontInfo->ToString();
dd05139a 292 wxASSERT_MSG(!fontDesc.empty(), wxT("This should be a non-empty string!"));
85ab460e
VZ
293 }
294 else
295 {
7bd236e6 296 wxFAIL_MSG(wxT("Derived class should have created the wxNativeFontInfo!"));
7826e2dd
VZ
297 }
298
299 return fontDesc;
300}
301
ab5fe833
VZ
302wxString wxFontBase::GetNativeFontInfoUserDesc() const
303{
304 wxString fontDesc;
3bf5a59b 305 const wxNativeFontInfo *fontInfo = GetNativeFontInfo();
ab5fe833
VZ
306 if ( fontInfo )
307 {
308 fontDesc = fontInfo->ToUserString();
dd05139a 309 wxASSERT_MSG(!fontDesc.empty(), wxT("This should be a non-empty string!"));
85ab460e
VZ
310 }
311 else
312 {
7456f382 313 wxFAIL_MSG(wxT("Derived class should have created the wxNativeFontInfo!"));
ab5fe833
VZ
314 }
315
316 return fontDesc;
317}
318
85ab460e 319bool wxFontBase::SetNativeFontInfo(const wxString& info)
31d1b66e
VZ
320{
321 wxNativeFontInfo fontInfo;
322 if ( !info.empty() && fontInfo.FromString(info) )
323 {
324 SetNativeFontInfo(fontInfo);
85ab460e 325 return true;
31d1b66e 326 }
85ab460e 327
85ab460e 328 return false;
31d1b66e
VZ
329}
330
85ab460e 331bool wxFontBase::SetNativeFontInfoUserDesc(const wxString& info)
ab5fe833
VZ
332{
333 wxNativeFontInfo fontInfo;
334 if ( !info.empty() && fontInfo.FromUserString(info) )
335 {
336 SetNativeFontInfo(fontInfo);
85ab460e 337 return true;
ab5fe833 338 }
85ab460e 339
85ab460e 340 return false;
ab5fe833
VZ
341}
342
0c5d3e1c
VZ
343bool wxFontBase::operator==(const wxFont& font) const
344{
8bf30fe9
VZ
345 // either it is the same font, i.e. they share the same common data or they
346 // have different ref datas but still describe the same font
a38cd629 347 return IsSameAs(font) ||
8bf30fe9 348 (
70f70818 349 IsOk() == font.IsOk() &&
8bf30fe9 350 GetPointSize() == font.GetPointSize() &&
82d0e7fe
VZ
351 // in wxGTK1 GetPixelSize() calls GetInternalFont() which uses
352 // operator==() resulting in infinite recursion so we can't use it
353 // in that port
354#if !defined(__WXGTK__) || defined(__WXGTK20__)
cc3de8a3 355 GetPixelSize() == font.GetPixelSize() &&
82d0e7fe 356#endif
8bf30fe9
VZ
357 GetFamily() == font.GetFamily() &&
358 GetStyle() == font.GetStyle() &&
e6adf058 359 GetWeight() == font.GetWeight() &&
8bf30fe9 360 GetUnderlined() == font.GetUnderlined() &&
85ab460e 361 GetFaceName().IsSameAs(font.GetFaceName(), false) &&
8bf30fe9
VZ
362 GetEncoding() == font.GetEncoding()
363 );
0c5d3e1c
VZ
364}
365
0c5d3e1c
VZ
366wxString wxFontBase::GetFamilyString() const
367{
70f70818 368 wxCHECK_MSG( IsOk(), "wxFONTFAMILY_DEFAULT", "invalid font" );
0c5d3e1c
VZ
369
370 switch ( GetFamily() )
371 {
70f70818
FM
372 case wxFONTFAMILY_DECORATIVE: return "wxFONTFAMILY_DECORATIVE";
373 case wxFONTFAMILY_ROMAN: return "wxFONTFAMILY_ROMAN";
374 case wxFONTFAMILY_SCRIPT: return "wxFONTFAMILY_SCRIPT";
375 case wxFONTFAMILY_SWISS: return "wxFONTFAMILY_SWISS";
376 case wxFONTFAMILY_MODERN: return "wxFONTFAMILY_MODERN";
377 case wxFONTFAMILY_TELETYPE: return "wxFONTFAMILY_TELETYPE";
378 default: return "wxFONTFAMILY_DEFAULT";
0c5d3e1c
VZ
379 }
380}
381
382wxString wxFontBase::GetStyleString() const
383{
70f70818 384 wxCHECK_MSG( IsOk(), "wxFONTSTYLE_DEFAULT", "invalid font" );
0c5d3e1c
VZ
385
386 switch ( GetStyle() )
387 {
70f70818
FM
388 case wxFONTSTYLE_NORMAL: return "wxFONTSTYLE_NORMAL";
389 case wxFONTSTYLE_SLANT: return "wxFONTSTYLE_SLANT";
390 case wxFONTSTYLE_ITALIC: return "wxFONTSTYLE_ITALIC";
391 default: return "wxFONTSTYLE_DEFAULT";
0c5d3e1c
VZ
392 }
393}
394
395wxString wxFontBase::GetWeightString() const
396{
70f70818 397 wxCHECK_MSG( IsOk(), "wxFONTWEIGHT_DEFAULT", "invalid font" );
0c5d3e1c
VZ
398
399 switch ( GetWeight() )
400 {
70f70818
FM
401 case wxFONTWEIGHT_NORMAL: return "wxFONTWEIGHT_NORMAL";
402 case wxFONTWEIGHT_BOLD: return "wxFONTWEIGHT_BOLD";
403 case wxFONTWEIGHT_LIGHT: return "wxFONTWEIGHT_LIGHT";
404 default: return "wxFONTWEIGHT_DEFAULT";
0c5d3e1c
VZ
405 }
406}
407
ff427585 408bool wxFontBase::SetFaceName(const wxString& facename)
85ab460e 409{
ff427585 410#if wxUSE_FONTENUM
85ab460e
VZ
411 if (!wxFontEnumerator::IsValidFacename(facename))
412 {
70f70818 413 UnRef(); // make IsOk() return false
85ab460e
VZ
414 return false;
415 }
ff427585
VZ
416#else // !wxUSE_FONTENUM
417 wxUnusedVar(facename);
418#endif // wxUSE_FONTENUM/!wxUSE_FONTENUM
85ab460e
VZ
419
420 return true;
421}
422
423
30764ab5
VZ
424// ----------------------------------------------------------------------------
425// wxNativeFontInfo
426// ----------------------------------------------------------------------------
427
85ab460e 428// Up to now, there are no native implementations of this function:
ff427585 429void wxNativeFontInfo::SetFaceName(const wxArrayString& facenames)
85ab460e 430{
ff427585 431#if wxUSE_FONTENUM
85ab460e
VZ
432 for (size_t i=0; i < facenames.GetCount(); i++)
433 {
434 if (wxFontEnumerator::IsValidFacename(facenames[i]))
435 {
436 SetFaceName(facenames[i]);
437 return;
438 }
439 }
440
441 // set the first valid facename we can find on this system
442 wxString validfacename = wxFontEnumerator::GetFacenames().Item(0);
443 wxLogTrace(wxT("font"), wxT("Falling back to '%s'"), validfacename.c_str());
444 SetFaceName(validfacename);
ff427585
VZ
445#else // !wxUSE_FONTENUM
446 SetFaceName(facenames[0]);
447#endif // wxUSE_FONTENUM/!wxUSE_FONTENUM
85ab460e
VZ
448}
449
450
ab5fe833
VZ
451#ifdef wxNO_NATIVE_FONTINFO
452
30764ab5
VZ
453// These are the generic forms of FromString()/ToString.
454//
455// convert to/from the string representation: format is
09fcd889 456// version;pointsize;family;style;weight;underlined;facename;encoding
30764ab5
VZ
457
458bool wxNativeFontInfo::FromString(const wxString& s)
459{
460 long l;
461
462 wxStringTokenizer tokenizer(s, _T(";"));
463
464 wxString token = tokenizer.GetNextToken();
09fcd889
VZ
465 //
466 // Ignore the version for now
467 //
33ac7e6f 468
09fcd889 469 token = tokenizer.GetNextToken();
30764ab5 470 if ( !token.ToLong(&l) )
a62848fd 471 return false;
30764ab5
VZ
472 pointSize = (int)l;
473
474 token = tokenizer.GetNextToken();
475 if ( !token.ToLong(&l) )
a62848fd 476 return false;
f7b301fa 477 family = (wxFontFamily)l;
30764ab5
VZ
478
479 token = tokenizer.GetNextToken();
480 if ( !token.ToLong(&l) )
a62848fd 481 return false;
ab5fe833 482 style = (wxFontStyle)l;
30764ab5
VZ
483
484 token = tokenizer.GetNextToken();
485 if ( !token.ToLong(&l) )
a62848fd 486 return false;
ab5fe833 487 weight = (wxFontWeight)l;
30764ab5
VZ
488
489 token = tokenizer.GetNextToken();
490 if ( !token.ToLong(&l) )
a62848fd 491 return false;
189e08b4 492 underlined = l != 0;
30764ab5
VZ
493
494 faceName = tokenizer.GetNextToken();
0a9f0ef7
JS
495
496#ifndef __WXMAC__
30764ab5 497 if( !faceName )
a62848fd 498 return false;
0a9f0ef7 499#endif
30764ab5
VZ
500
501 token = tokenizer.GetNextToken();
502 if ( !token.ToLong(&l) )
a62848fd 503 return false;
30764ab5
VZ
504 encoding = (wxFontEncoding)l;
505
a62848fd 506 return true;
30764ab5
VZ
507}
508
509wxString wxNativeFontInfo::ToString() const
510{
511 wxString s;
512
09fcd889
VZ
513 s.Printf(_T("%d;%d;%d;%d;%d;%d;%s;%d"),
514 0, // version
30764ab5
VZ
515 pointSize,
516 family,
ab5fe833
VZ
517 (int)style,
518 (int)weight,
30764ab5
VZ
519 underlined,
520 faceName.GetData(),
521 (int)encoding);
522
523 return s;
524}
525
ab5fe833
VZ
526void wxNativeFontInfo::Init()
527{
3bf5a59b 528 pointSize = 0;
ab5fe833
VZ
529 family = wxFONTFAMILY_DEFAULT;
530 style = wxFONTSTYLE_NORMAL;
531 weight = wxFONTWEIGHT_NORMAL;
a62848fd 532 underlined = false;
ab5fe833
VZ
533 faceName.clear();
534 encoding = wxFONTENCODING_DEFAULT;
535}
536
537int wxNativeFontInfo::GetPointSize() const
538{
539 return pointSize;
540}
541
542wxFontStyle wxNativeFontInfo::GetStyle() const
543{
544 return style;
545}
546
547wxFontWeight wxNativeFontInfo::GetWeight() const
548{
549 return weight;
550}
551
552bool wxNativeFontInfo::GetUnderlined() const
553{
554 return underlined;
555}
556
557wxString wxNativeFontInfo::GetFaceName() const
558{
559 return faceName;
560}
561
7936354d
VZ
562wxFontFamily wxNativeFontInfo::GetFamily() const
563{
564 return family;
565}
566
ab5fe833
VZ
567wxFontEncoding wxNativeFontInfo::GetEncoding() const
568{
569 return encoding;
570}
571
572void wxNativeFontInfo::SetPointSize(int pointsize)
573{
574 pointSize = pointsize;
575}
576
577void wxNativeFontInfo::SetStyle(wxFontStyle style_)
578{
579 style = style_;
580}
581
582void wxNativeFontInfo::SetWeight(wxFontWeight weight_)
583{
584 weight = weight_;
585}
586
587void wxNativeFontInfo::SetUnderlined(bool underlined_)
588{
589 underlined = underlined_;
590}
591
85ab460e 592bool wxNativeFontInfo::SetFaceName(const wxString& facename_)
ab5fe833 593{
f7b301fa 594 faceName = facename_;
85ab460e 595 return true;
ab5fe833
VZ
596}
597
3f1d1373 598void wxNativeFontInfo::SetFamily(wxFontFamily family_)
7936354d
VZ
599{
600 family = family_;
601}
602
ab5fe833
VZ
603void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding_)
604{
605 encoding = encoding_;
606}
607
7826e2dd 608#endif // generic wxNativeFontInfo implementation
30764ab5 609
ab5fe833
VZ
610// conversion to/from user-readable string: this is used in the generic
611// versions and under MSW as well because there is no standard font description
612// format there anyhow (but there is a well-defined standard for X11 fonts used
613// by wxGTK and wxMotif)
614
f1c40652 615#if defined(wxNO_NATIVE_FONTINFO) || defined(__WXMSW__) || defined (__WXPM__) || defined(__WXOSX__)
ab5fe833
VZ
616
617wxString wxNativeFontInfo::ToUserString() const
618{
619 wxString desc;
620
621 // first put the adjectives, if any - this is English-centric, of course,
622 // but what else can we do?
623 if ( GetUnderlined() )
624 {
85ab460e 625 desc << _("underlined");
ab5fe833
VZ
626 }
627
628 switch ( GetWeight() )
629 {
630 default:
631 wxFAIL_MSG( _T("unknown font weight") );
632 // fall through
633
634 case wxFONTWEIGHT_NORMAL:
635 break;
636
637 case wxFONTWEIGHT_LIGHT:
85ab460e 638 desc << _(" light");
ab5fe833
VZ
639 break;
640
641 case wxFONTWEIGHT_BOLD:
85ab460e 642 desc << _(" bold");
ab5fe833
VZ
643 break;
644 }
645
646 switch ( GetStyle() )
647 {
648 default:
649 wxFAIL_MSG( _T("unknown font style") );
650 // fall through
651
652 case wxFONTSTYLE_NORMAL:
653 break;
654
655 // we don't distinguish between the two for now anyhow...
656 case wxFONTSTYLE_ITALIC:
657 case wxFONTSTYLE_SLANT:
85ab460e 658 desc << _(" italic");
ab5fe833
VZ
659 break;
660 }
661
a9249b2e
VZ
662 wxString face = GetFaceName();
663 if ( !face.empty() )
ab5fe833 664 {
a9249b2e 665 desc << _T(' ') << face;
ab5fe833
VZ
666 }
667
a9249b2e
VZ
668 int size = GetPointSize();
669 if ( size != wxNORMAL_FONT->GetPointSize() )
ab5fe833 670 {
a9249b2e 671 desc << _T(' ') << size;
ab5fe833 672 }
a9249b2e 673
e7e52b6d 674#if wxUSE_FONTMAP
a9249b2e
VZ
675 wxFontEncoding enc = GetEncoding();
676 if ( enc != wxFONTENCODING_DEFAULT && enc != wxFONTENCODING_SYSTEM )
677 {
2a1f999f 678 desc << _T(' ') << wxFontMapper::GetEncodingName(enc);
a9249b2e 679 }
e7e52b6d 680#endif // wxUSE_FONTMAP
a9249b2e 681
85ab460e 682 return desc.Strip(wxString::both).MakeLower();
ab5fe833
VZ
683}
684
685bool wxNativeFontInfo::FromUserString(const wxString& s)
686{
687 // reset to the default state
688 Init();
689
690 // parse a more or less free form string
691 //
692 // TODO: we should handle at least the quoted facenames
693 wxStringTokenizer tokenizer(s, _T(";, "), wxTOKEN_STRTOK);
694
695 wxString face;
696 unsigned long size;
77f15ffe
VS
697 bool weightfound = false, pointsizefound = false;
698#if wxUSE_FONTMAP
699 bool encodingfound = false;
700#endif
ab5fe833
VZ
701
702 while ( tokenizer.HasMoreTokens() )
703 {
704 wxString token = tokenizer.GetNextToken();
705
706 // normalize it
a62848fd 707 token.Trim(true).Trim(false).MakeLower();
ab5fe833
VZ
708
709 // look for the known tokens
710 if ( token == _T("underlined") || token == _("underlined") )
711 {
a62848fd 712 SetUnderlined(true);
ab5fe833
VZ
713 }
714 else if ( token == _T("light") || token == _("light") )
715 {
716 SetWeight(wxFONTWEIGHT_LIGHT);
85ab460e 717 weightfound = true;
ab5fe833
VZ
718 }
719 else if ( token == _T("bold") || token == _("bold") )
720 {
721 SetWeight(wxFONTWEIGHT_BOLD);
85ab460e 722 weightfound = true;
ab5fe833
VZ
723 }
724 else if ( token == _T("italic") || token == _("italic") )
725 {
726 SetStyle(wxFONTSTYLE_ITALIC);
727 }
728 else if ( token.ToULong(&size) )
729 {
a9249b2e 730 SetPointSize(size);
85ab460e 731 pointsizefound = true;
ab5fe833 732 }
85ab460e
VZ
733 else
734 {
e7e52b6d 735#if wxUSE_FONTMAP
85ab460e
VZ
736 // try to interpret this as an encoding
737 wxFontEncoding encoding = wxFontMapper::Get()->CharsetToEncoding(token, false);
738 if ( encoding != wxFONTENCODING_DEFAULT &&
739 encoding != wxFONTENCODING_SYSTEM ) // returned when the recognition failed
ab5fe833
VZ
740 {
741 SetEncoding(encoding);
85ab460e 742 encodingfound = true;
ab5fe833 743 }
85ab460e 744 else
ab5fe833 745 {
85ab460e
VZ
746#endif // wxUSE_FONTMAP
747
748 // assume it is the face name
ab5fe833
VZ
749 if ( !face.empty() )
750 {
751 face += _T(' ');
752 }
753
754 face += token;
755
756 // skip the code which resets face below
757 continue;
85ab460e
VZ
758
759#if wxUSE_FONTMAP
760 }
761#endif // wxUSE_FONTMAP
ab5fe833
VZ
762 }
763
764 // if we had had the facename, we shouldn't continue appending tokens
765 // to it (i.e. "foo bold bar" shouldn't result in the facename "foo
766 // bar")
767 if ( !face.empty() )
768 {
85ab460e
VZ
769 // NB: the check on the facename is implemented in wxFontBase::SetFaceName
770 // and not in wxNativeFontInfo::SetFaceName thus we need to explicitely
771 // call here wxFontEnumerator::IsValidFacename
ff427585
VZ
772 if (
773#if wxUSE_FONTENUM
774 !wxFontEnumerator::IsValidFacename(face) ||
775#endif // wxUSE_FONTENUM
776 !SetFaceName(face) )
777 {
85ab460e 778 SetFaceName(wxNORMAL_FONT->GetFaceName());
ff427585
VZ
779 }
780
ab5fe833
VZ
781 face.clear();
782 }
783 }
784
785 // we might not have flushed it inside the loop
786 if ( !face.empty() )
787 {
85ab460e
VZ
788 // NB: the check on the facename is implemented in wxFontBase::SetFaceName
789 // and not in wxNativeFontInfo::SetFaceName thus we need to explicitely
790 // call here wxFontEnumerator::IsValidFacename
ff427585
VZ
791 if (
792#if wxUSE_FONTENUM
793 !wxFontEnumerator::IsValidFacename(face) ||
794#endif // wxUSE_FONTENUM
795 !SetFaceName(face) )
796 {
797 SetFaceName(wxNORMAL_FONT->GetFaceName());
798 }
ab5fe833
VZ
799 }
800
85ab460e
VZ
801 // set point size to default value if size was not given
802 if ( !pointsizefound )
803 SetPointSize(wxNORMAL_FONT->GetPointSize());
804
805 // set font weight to default value if weight was not given
806 if ( !weightfound )
807 SetWeight(wxFONTWEIGHT_NORMAL);
808
809#if wxUSE_FONTMAP
810 // set font encoding to default value if encoding was not given
811 if ( !encodingfound )
812 SetEncoding(wxFONTENCODING_SYSTEM);
813#endif // wxUSE_FONTMAP
814
a62848fd 815 return true;
ab5fe833
VZ
816}
817
0eb529d9 818#endif // generic or wxMSW or wxOS2
fc9361e3
VZ
819
820
821// wxFont <-> wxString utilities, used by wxConfig
822wxString wxToString(const wxFontBase& font)
823{
824 return font.IsOk() ? font.GetNativeFontInfoDesc()
825 : wxString();
826}
827
828bool wxFromString(const wxString& str, wxFontBase *font)
829{
830 wxCHECK_MSG( font, false, _T("NULL output parameter") );
831
832 if ( str.empty() )
833 {
834 *font = wxNullFont;
835 return true;
836 }
837
838 return font->SetNativeFontInfo(str);
839}
840
841