]> git.saurik.com Git - wxWidgets.git/blob - src/common/fontcmn.cpp
was missing in xti merge
[wxWidgets.git] / src / common / fontcmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/fontcmn.cpp
3 // Purpose: implementation of wxFontBase methods
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 20.09.99
7 // RCS-ID: $Id$
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
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__
24 #pragma hdrstop
25 #endif
26
27 #include "wx/font.h"
28
29 #ifndef WX_PRECOMP
30 #include "wx/dc.h"
31 #include "wx/intl.h"
32 #include "wx/dcscreen.h"
33 #include "wx/log.h"
34 #include "wx/gdicmn.h"
35 #endif // WX_PRECOMP
36
37 #if defined(__WXMSW__)
38 #include "wx/msw/private.h" // includes windows.h for LOGFONT
39 #include "wx/msw/winundef.h"
40 #endif
41
42 #include "wx/fontutil.h" // for wxNativeFontInfo
43 #include "wx/fontmap.h"
44 #include "wx/fontenum.h"
45
46 #include "wx/tokenzr.h"
47
48 // debugger helper: this function can be called from a debugger to show what
49 // the date really is
50 extern 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 ? wxT("normal")
61 : weight == wxFONTWEIGHT_BOLD
62 ? wxT("bold")
63 : wxT("light"),
64 font->GetStyle() == wxFONTSTYLE_NORMAL
65 ? wxT("regular")
66 : wxT("italic"),
67 font->GetPointSize(),
68 font->GetEncoding());
69
70 wxStrlcpy(buf, s.mb_str(), WXSIZEOF(buf));
71 return buf;
72 }
73
74 // ----------------------------------------------------------------------------
75 // XTI
76 // ----------------------------------------------------------------------------
77
78 wxBEGIN_ENUM( wxFontFamily )
79 wxENUM_MEMBER( wxDEFAULT )
80 wxENUM_MEMBER( wxDECORATIVE )
81 wxENUM_MEMBER( wxROMAN )
82 wxENUM_MEMBER( wxSCRIPT )
83 wxENUM_MEMBER( wxSWISS )
84 wxENUM_MEMBER( wxMODERN )
85 wxENUM_MEMBER( wxTELETYPE )
86 wxEND_ENUM( wxFontFamily )
87
88 wxBEGIN_ENUM( wxFontStyle )
89 wxENUM_MEMBER( wxNORMAL )
90 wxENUM_MEMBER( wxITALIC )
91 wxENUM_MEMBER( wxSLANT )
92 wxEND_ENUM( wxFontStyle )
93
94 wxBEGIN_ENUM( wxFontWeight )
95 wxENUM_MEMBER( wxNORMAL )
96 wxENUM_MEMBER( wxLIGHT )
97 wxENUM_MEMBER( wxBOLD )
98 wxEND_ENUM( wxFontWeight )
99
100 wxIMPLEMENT_DYNAMIC_CLASS_WITH_COPY_XTI(wxFont, wxGDIObject, "wx/font.h")
101
102 wxBEGIN_PROPERTIES_TABLE(wxFont)
103 wxPROPERTY( Size,int, SetPointSize, GetPointSize, 12, 0 /*flags*/, \
104 wxT("Helpstring"), wxT("group"))
105 wxPROPERTY( Family, int , SetFamily, GetFamily, (int)wxDEFAULT, \
106 0 /*flags*/, wxT("Helpstring"), wxT("group")) // wxFontFamily
107 wxPROPERTY( Style, int, SetStyle, GetStyle, (int)wxNORMAL, 0 /*flags*/, \
108 wxT("Helpstring"), wxT("group")) // wxFontStyle
109 wxPROPERTY( Weight, int, SetWeight, GetWeight, (int)wxNORMAL, 0 /*flags*/, \
110 wxT("Helpstring"), wxT("group")) // wxFontWeight
111 wxPROPERTY( Underlined, bool, SetUnderlined, GetUnderlined, false, 0 /*flags*/, \
112 wxT("Helpstring"), wxT("group"))
113 wxPROPERTY( Face, wxString, SetFaceName, GetFaceName, wxEMPTY_PARAMETER_VALUE, \
114 0 /*flags*/, wxT("Helpstring"), wxT("group"))
115 wxPROPERTY( Encoding, wxFontEncoding, SetEncoding, GetEncoding, \
116 wxFONTENCODING_DEFAULT, 0 /*flags*/, wxT("Helpstring"), wxT("group"))
117 wxEND_PROPERTIES_TABLE()
118
119 wxCONSTRUCTOR_6( wxFont, int, Size, int, Family, int, Style, int, Weight, \
120 bool, Underlined, wxString, Face )
121
122 wxEMPTY_HANDLERS_TABLE(wxFont)
123
124 // ============================================================================
125 // implementation
126 // ============================================================================
127
128 // ----------------------------------------------------------------------------
129 // helper functions
130 // ----------------------------------------------------------------------------
131
132 static inline int flags2Style(int flags)
133 {
134 return flags & wxFONTFLAG_ITALIC
135 ? wxFONTSTYLE_ITALIC
136 : flags & wxFONTFLAG_SLANT
137 ? wxFONTSTYLE_SLANT
138 : wxFONTSTYLE_NORMAL;
139 }
140
141 static inline int flags2Weight(int flags)
142 {
143 return flags & wxFONTFLAG_LIGHT
144 ? wxFONTWEIGHT_LIGHT
145 : flags & wxFONTFLAG_BOLD
146 ? wxFONTWEIGHT_BOLD
147 : wxFONTWEIGHT_NORMAL;
148 }
149
150 static inline bool flags2Underlined(int flags)
151 {
152 return (flags & wxFONTFLAG_UNDERLINED) != 0;
153 }
154
155 // ----------------------------------------------------------------------------
156 // wxFontBase
157 // ----------------------------------------------------------------------------
158
159 wxFontEncoding wxFontBase::ms_encodingDefault = wxFONTENCODING_SYSTEM;
160
161 /* static */
162 void wxFontBase::SetDefaultEncoding(wxFontEncoding encoding)
163 {
164 // GetDefaultEncoding() should return something != wxFONTENCODING_DEFAULT
165 // and, besides, using this value here doesn't make any sense
166 wxCHECK_RET( encoding != wxFONTENCODING_DEFAULT,
167 wxT("can't set default encoding to wxFONTENCODING_DEFAULT") );
168
169 ms_encodingDefault = encoding;
170 }
171
172 wxFontBase::~wxFontBase()
173 {
174 // this destructor is required for Darwin
175 }
176
177 /* static */
178 wxFont *wxFontBase::New(int size,
179 wxFontFamily family,
180 wxFontStyle style,
181 wxFontWeight weight,
182 bool underlined,
183 const wxString& face,
184 wxFontEncoding encoding)
185 {
186 return new wxFont(size, family, style, weight, underlined, face, encoding);
187 }
188
189 /* static */
190 wxFont *wxFontBase::New(const wxSize& pixelSize,
191 wxFontFamily family,
192 wxFontStyle style,
193 wxFontWeight weight,
194 bool underlined,
195 const wxString& face,
196 wxFontEncoding encoding)
197 {
198 return new wxFont(pixelSize, family, style, weight, underlined,
199 face, encoding);
200 }
201
202 /* static */
203 wxFont *wxFontBase::New(int pointSize,
204 wxFontFamily family,
205 int flags,
206 const wxString& face,
207 wxFontEncoding encoding)
208 {
209 return New(pointSize, family, flags2Style(flags), flags2Weight(flags),
210 flags2Underlined(flags), face, encoding);
211 }
212
213 /* static */
214 wxFont *wxFontBase::New(const wxSize& pixelSize,
215 wxFontFamily family,
216 int flags,
217 const wxString& face,
218 wxFontEncoding encoding)
219 {
220 return New(pixelSize, family, flags2Style(flags), flags2Weight(flags),
221 flags2Underlined(flags), face, encoding);
222 }
223
224 /* static */
225 wxFont *wxFontBase::New(const wxNativeFontInfo& info)
226 {
227 return new wxFont(info);
228 }
229
230 /* static */
231 wxFont *wxFontBase::New(const wxString& strNativeFontDesc)
232 {
233 wxNativeFontInfo fontInfo;
234 if ( !fontInfo.FromString(strNativeFontDesc) )
235 return new wxFont(*wxNORMAL_FONT);
236
237 return New(fontInfo);
238 }
239
240 bool wxFontBase::IsFixedWidth() const
241 {
242 return GetFamily() == wxFONTFAMILY_TELETYPE;
243 }
244
245 wxSize wxFontBase::GetPixelSize() const
246 {
247 wxScreenDC dc;
248 dc.SetFont(*(wxFont *)this);
249 return wxSize(dc.GetCharWidth(), dc.GetCharHeight());
250 }
251
252 bool wxFontBase::IsUsingSizeInPixels() const
253 {
254 return false;
255 }
256
257 void wxFontBase::SetPixelSize( const wxSize& pixelSize )
258 {
259 wxCHECK_RET( pixelSize.GetWidth() >= 0 && pixelSize.GetHeight() > 0,
260 "Negative values for the pixel size or zero pixel height are not allowed" );
261
262 wxScreenDC dc;
263
264 // NOTE: this algorithm for adjusting the font size is used by all
265 // implementations of wxFont except under wxMSW and wxGTK where
266 // native support to font creation using pixel-size is provided.
267
268 int largestGood = 0;
269 int smallestBad = 0;
270
271 bool initialGoodFound = false;
272 bool initialBadFound = false;
273
274 // NB: this assignment was separated from the variable definition
275 // in order to fix a gcc v3.3.3 compiler crash
276 int currentSize = GetPointSize();
277 while (currentSize > 0)
278 {
279 dc.SetFont(*static_cast<wxFont*>(this));
280
281 // if currentSize (in points) results in a font that is smaller
282 // than required by pixelSize it is considered a good size
283 // NOTE: the pixel size width may be zero
284 if (dc.GetCharHeight() <= pixelSize.GetHeight() &&
285 (pixelSize.GetWidth() == 0 ||
286 dc.GetCharWidth() <= pixelSize.GetWidth()))
287 {
288 largestGood = currentSize;
289 initialGoodFound = true;
290 }
291 else
292 {
293 smallestBad = currentSize;
294 initialBadFound = true;
295 }
296 if (!initialGoodFound)
297 {
298 currentSize /= 2;
299 }
300 else if (!initialBadFound)
301 {
302 currentSize *= 2;
303 }
304 else
305 {
306 int distance = smallestBad - largestGood;
307 if (distance == 1)
308 break;
309
310 currentSize = largestGood + distance / 2;
311 }
312
313 SetPointSize(currentSize);
314 }
315
316 if (currentSize != largestGood)
317 SetPointSize(largestGood);
318 }
319
320 void wxFontBase::DoSetNativeFontInfo(const wxNativeFontInfo& info)
321 {
322 #ifdef wxNO_NATIVE_FONTINFO
323 SetPointSize(info.pointSize);
324 SetFamily(info.family);
325 SetStyle(info.style);
326 SetWeight(info.weight);
327 SetUnderlined(info.underlined);
328 SetFaceName(info.faceName);
329 SetEncoding(info.encoding);
330 #else
331 (void)info;
332 #endif
333 }
334
335 wxString wxFontBase::GetNativeFontInfoDesc() const
336 {
337 wxCHECK_MSG( IsOk(), wxEmptyString, wxT("invalid font") );
338
339 wxString fontDesc;
340 const wxNativeFontInfo *fontInfo = GetNativeFontInfo();
341 if ( fontInfo )
342 {
343 fontDesc = fontInfo->ToString();
344 wxASSERT_MSG(!fontDesc.empty(), wxT("This should be a non-empty string!"));
345 }
346 else
347 {
348 wxFAIL_MSG(wxT("Derived class should have created the wxNativeFontInfo!"));
349 }
350
351 return fontDesc;
352 }
353
354 wxString wxFontBase::GetNativeFontInfoUserDesc() const
355 {
356 wxCHECK_MSG( IsOk(), wxEmptyString, wxT("invalid font") );
357
358 wxString fontDesc;
359 const wxNativeFontInfo *fontInfo = GetNativeFontInfo();
360 if ( fontInfo )
361 {
362 fontDesc = fontInfo->ToUserString();
363 wxASSERT_MSG(!fontDesc.empty(), wxT("This should be a non-empty string!"));
364 }
365 else
366 {
367 wxFAIL_MSG(wxT("Derived class should have created the wxNativeFontInfo!"));
368 }
369
370 return fontDesc;
371 }
372
373 bool wxFontBase::SetNativeFontInfo(const wxString& info)
374 {
375 wxNativeFontInfo fontInfo;
376 if ( !info.empty() && fontInfo.FromString(info) )
377 {
378 SetNativeFontInfo(fontInfo);
379 return true;
380 }
381
382 return false;
383 }
384
385 bool wxFontBase::SetNativeFontInfoUserDesc(const wxString& info)
386 {
387 wxNativeFontInfo fontInfo;
388 if ( !info.empty() && fontInfo.FromUserString(info) )
389 {
390 SetNativeFontInfo(fontInfo);
391 return true;
392 }
393
394 return false;
395 }
396
397 bool wxFontBase::operator==(const wxFont& font) const
398 {
399 // either it is the same font, i.e. they share the same common data or they
400 // have different ref datas but still describe the same font
401 return IsSameAs(font) ||
402 (
403 IsOk() == font.IsOk() &&
404 GetPointSize() == font.GetPointSize() &&
405 // in wxGTK1 GetPixelSize() calls GetInternalFont() which uses
406 // operator==() resulting in infinite recursion so we can't use it
407 // in that port
408 #if !defined(__WXGTK__) || defined(__WXGTK20__)
409 GetPixelSize() == font.GetPixelSize() &&
410 #endif
411 GetFamily() == font.GetFamily() &&
412 GetStyle() == font.GetStyle() &&
413 GetWeight() == font.GetWeight() &&
414 GetUnderlined() == font.GetUnderlined() &&
415 GetFaceName().IsSameAs(font.GetFaceName(), false) &&
416 GetEncoding() == font.GetEncoding()
417 );
418 }
419
420 wxFontFamily wxFontBase::GetFamily() const
421 {
422 wxCHECK_MSG( IsOk(), wxFONTFAMILY_UNKNOWN, wxS("invalid font") );
423
424 // Don't return wxFONTFAMILY_UNKNOWN from here because it prevents the code
425 // like wxFont(size, wxNORMAL_FONT->GetFamily(), ...) from working (see
426 // #12330). This is really just a hack but it allows to keep compatibility
427 // and doesn't really have any bad drawbacks so do this until someone comes
428 // up with a better idea.
429 const wxFontFamily family = DoGetFamily();
430
431 return family == wxFONTFAMILY_UNKNOWN ? wxFONTFAMILY_DEFAULT : family;
432 }
433
434 wxString wxFontBase::GetFamilyString() const
435 {
436 wxCHECK_MSG( IsOk(), "wxFONTFAMILY_DEFAULT", "invalid font" );
437
438 switch ( GetFamily() )
439 {
440 case wxFONTFAMILY_DECORATIVE: return "wxFONTFAMILY_DECORATIVE";
441 case wxFONTFAMILY_ROMAN: return "wxFONTFAMILY_ROMAN";
442 case wxFONTFAMILY_SCRIPT: return "wxFONTFAMILY_SCRIPT";
443 case wxFONTFAMILY_SWISS: return "wxFONTFAMILY_SWISS";
444 case wxFONTFAMILY_MODERN: return "wxFONTFAMILY_MODERN";
445 case wxFONTFAMILY_TELETYPE: return "wxFONTFAMILY_TELETYPE";
446 case wxFONTFAMILY_UNKNOWN: return "wxFONTFAMILY_UNKNOWN";
447 default: return "wxFONTFAMILY_DEFAULT";
448 }
449 }
450
451 wxString wxFontBase::GetStyleString() const
452 {
453 wxCHECK_MSG( IsOk(), "wxFONTSTYLE_DEFAULT", "invalid font" );
454
455 switch ( GetStyle() )
456 {
457 case wxFONTSTYLE_NORMAL: return "wxFONTSTYLE_NORMAL";
458 case wxFONTSTYLE_SLANT: return "wxFONTSTYLE_SLANT";
459 case wxFONTSTYLE_ITALIC: return "wxFONTSTYLE_ITALIC";
460 default: return "wxFONTSTYLE_DEFAULT";
461 }
462 }
463
464 wxString wxFontBase::GetWeightString() const
465 {
466 wxCHECK_MSG( IsOk(), "wxFONTWEIGHT_DEFAULT", "invalid font" );
467
468 switch ( GetWeight() )
469 {
470 case wxFONTWEIGHT_NORMAL: return "wxFONTWEIGHT_NORMAL";
471 case wxFONTWEIGHT_BOLD: return "wxFONTWEIGHT_BOLD";
472 case wxFONTWEIGHT_LIGHT: return "wxFONTWEIGHT_LIGHT";
473 default: return "wxFONTWEIGHT_DEFAULT";
474 }
475 }
476
477 bool wxFontBase::SetFaceName(const wxString& facename)
478 {
479 #if wxUSE_FONTENUM
480 if (!wxFontEnumerator::IsValidFacename(facename))
481 {
482 UnRef(); // make IsOk() return false
483 return false;
484 }
485 #else // !wxUSE_FONTENUM
486 wxUnusedVar(facename);
487 #endif // wxUSE_FONTENUM/!wxUSE_FONTENUM
488
489 return true;
490 }
491
492 wxFont& wxFont::MakeBold()
493 {
494 SetWeight(wxFONTWEIGHT_BOLD);
495 return *this;
496 }
497
498 wxFont wxFont::Bold() const
499 {
500 wxFont font(*this);
501 font.MakeBold();
502 return font;
503 }
504
505 wxFont& wxFont::MakeItalic()
506 {
507 SetStyle(wxFONTSTYLE_ITALIC);
508 return *this;
509 }
510
511 wxFont wxFont::Italic() const
512 {
513 wxFont font(*this);
514 font.SetStyle(wxFONTSTYLE_ITALIC);
515 return font;
516 }
517
518 wxFont& wxFont::Scale(float x)
519 {
520 SetPointSize(int(x*GetPointSize() + 0.5));
521 return *this;
522 }
523
524 wxFont wxFont::Scaled(float x) const
525 {
526 wxFont font(*this);
527 font.Scale(x);
528 return font;
529 }
530
531 // ----------------------------------------------------------------------------
532 // wxNativeFontInfo
533 // ----------------------------------------------------------------------------
534
535 // Up to now, there are no native implementations of this function:
536 void wxNativeFontInfo::SetFaceName(const wxArrayString& facenames)
537 {
538 #if wxUSE_FONTENUM
539 for (size_t i=0; i < facenames.GetCount(); i++)
540 {
541 if (wxFontEnumerator::IsValidFacename(facenames[i]))
542 {
543 SetFaceName(facenames[i]);
544 return;
545 }
546 }
547
548 // set the first valid facename we can find on this system
549 wxString validfacename = wxFontEnumerator::GetFacenames().Item(0);
550 wxLogTrace(wxT("font"), wxT("Falling back to '%s'"), validfacename.c_str());
551 SetFaceName(validfacename);
552 #else // !wxUSE_FONTENUM
553 SetFaceName(facenames[0]);
554 #endif // wxUSE_FONTENUM/!wxUSE_FONTENUM
555 }
556
557
558 #ifdef wxNO_NATIVE_FONTINFO
559
560 // These are the generic forms of FromString()/ToString.
561 //
562 // convert to/from the string representation: format is
563 // version;pointsize;family;style;weight;underlined;facename;encoding
564
565 bool wxNativeFontInfo::FromString(const wxString& s)
566 {
567 long l;
568
569 wxStringTokenizer tokenizer(s, wxT(";"));
570
571 wxString token = tokenizer.GetNextToken();
572 //
573 // Ignore the version for now
574 //
575
576 token = tokenizer.GetNextToken();
577 if ( !token.ToLong(&l) )
578 return false;
579 pointSize = (int)l;
580
581 token = tokenizer.GetNextToken();
582 if ( !token.ToLong(&l) )
583 return false;
584 family = (wxFontFamily)l;
585
586 token = tokenizer.GetNextToken();
587 if ( !token.ToLong(&l) )
588 return false;
589 style = (wxFontStyle)l;
590
591 token = tokenizer.GetNextToken();
592 if ( !token.ToLong(&l) )
593 return false;
594 weight = (wxFontWeight)l;
595
596 token = tokenizer.GetNextToken();
597 if ( !token.ToLong(&l) )
598 return false;
599 underlined = l != 0;
600
601 faceName = tokenizer.GetNextToken();
602
603 #ifndef __WXMAC__
604 if( !faceName )
605 return false;
606 #endif
607
608 token = tokenizer.GetNextToken();
609 if ( !token.ToLong(&l) )
610 return false;
611 encoding = (wxFontEncoding)l;
612
613 return true;
614 }
615
616 wxString wxNativeFontInfo::ToString() const
617 {
618 wxString s;
619
620 s.Printf(wxT("%d;%d;%d;%d;%d;%d;%s;%d"),
621 0, // version
622 pointSize,
623 family,
624 (int)style,
625 (int)weight,
626 underlined,
627 faceName.GetData(),
628 (int)encoding);
629
630 return s;
631 }
632
633 void wxNativeFontInfo::Init()
634 {
635 pointSize = 0;
636 family = wxFONTFAMILY_DEFAULT;
637 style = wxFONTSTYLE_NORMAL;
638 weight = wxFONTWEIGHT_NORMAL;
639 underlined = false;
640 faceName.clear();
641 encoding = wxFONTENCODING_DEFAULT;
642 }
643
644 int wxNativeFontInfo::GetPointSize() const
645 {
646 return pointSize;
647 }
648
649 wxFontStyle wxNativeFontInfo::GetStyle() const
650 {
651 return style;
652 }
653
654 wxFontWeight wxNativeFontInfo::GetWeight() const
655 {
656 return weight;
657 }
658
659 bool wxNativeFontInfo::GetUnderlined() const
660 {
661 return underlined;
662 }
663
664 wxString wxNativeFontInfo::GetFaceName() const
665 {
666 return faceName;
667 }
668
669 wxFontFamily wxNativeFontInfo::GetFamily() const
670 {
671 return family;
672 }
673
674 wxFontEncoding wxNativeFontInfo::GetEncoding() const
675 {
676 return encoding;
677 }
678
679 void wxNativeFontInfo::SetPointSize(int pointsize)
680 {
681 pointSize = pointsize;
682 }
683
684 void wxNativeFontInfo::SetStyle(wxFontStyle style_)
685 {
686 style = style_;
687 }
688
689 void wxNativeFontInfo::SetWeight(wxFontWeight weight_)
690 {
691 weight = weight_;
692 }
693
694 void wxNativeFontInfo::SetUnderlined(bool underlined_)
695 {
696 underlined = underlined_;
697 }
698
699 bool wxNativeFontInfo::SetFaceName(const wxString& facename_)
700 {
701 faceName = facename_;
702 return true;
703 }
704
705 void wxNativeFontInfo::SetFamily(wxFontFamily family_)
706 {
707 family = family_;
708 }
709
710 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding_)
711 {
712 encoding = encoding_;
713 }
714
715 #endif // generic wxNativeFontInfo implementation
716
717 // conversion to/from user-readable string: this is used in the generic
718 // versions and under MSW as well because there is no standard font description
719 // format there anyhow (but there is a well-defined standard for X11 fonts used
720 // by wxGTK and wxMotif)
721
722 #if defined(wxNO_NATIVE_FONTINFO) || defined(__WXMSW__) || defined (__WXPM__) || defined(__WXOSX__)
723
724 wxString wxNativeFontInfo::ToUserString() const
725 {
726 wxString desc;
727
728 // first put the adjectives, if any - this is English-centric, of course,
729 // but what else can we do?
730 if ( GetUnderlined() )
731 {
732 desc << _("underlined");
733 }
734
735 switch ( GetWeight() )
736 {
737 default:
738 wxFAIL_MSG( wxT("unknown font weight") );
739 // fall through
740
741 case wxFONTWEIGHT_NORMAL:
742 break;
743
744 case wxFONTWEIGHT_LIGHT:
745 desc << _(" light");
746 break;
747
748 case wxFONTWEIGHT_BOLD:
749 desc << _(" bold");
750 break;
751 }
752
753 switch ( GetStyle() )
754 {
755 default:
756 wxFAIL_MSG( wxT("unknown font style") );
757 // fall through
758
759 case wxFONTSTYLE_NORMAL:
760 break;
761
762 // we don't distinguish between the two for now anyhow...
763 case wxFONTSTYLE_ITALIC:
764 case wxFONTSTYLE_SLANT:
765 desc << _(" italic");
766 break;
767 }
768
769 wxString face = GetFaceName();
770 if ( !face.empty() )
771 {
772 if (face.Contains(' ') || face.Contains(';') || face.Contains(','))
773 {
774 face.Replace("'", "");
775 // eventually remove quote characters: most systems do not
776 // allow them in a facename anyway so this usually does nothing
777
778 // make it possible for FromUserString() function to understand
779 // that the different words which compose this facename are
780 // not different adjectives or other data but rather all parts
781 // of the facename
782 desc << wxT(" '") << face << _("'");
783 }
784 else
785 desc << wxT(' ') << face;
786 }
787 else // no face name specified
788 {
789 // use the family
790 wxString familyStr;
791 switch ( GetFamily() )
792 {
793 case wxFONTFAMILY_DECORATIVE:
794 familyStr = "decorative";
795 break;
796
797 case wxFONTFAMILY_ROMAN:
798 familyStr = "roman";
799 break;
800
801 case wxFONTFAMILY_SCRIPT:
802 familyStr = "script";
803 break;
804
805 case wxFONTFAMILY_SWISS:
806 familyStr = "swiss";
807 break;
808
809 case wxFONTFAMILY_MODERN:
810 familyStr = "modern";
811 break;
812
813 case wxFONTFAMILY_TELETYPE:
814 familyStr = "teletype";
815 break;
816
817 case wxFONTFAMILY_DEFAULT:
818 case wxFONTFAMILY_UNKNOWN:
819 break;
820
821 default:
822 wxFAIL_MSG( "unknown font family" );
823 }
824
825 if ( !familyStr.empty() )
826 desc << " '" << familyStr << " family'";
827 }
828
829 int size = GetPointSize();
830 if ( size != wxNORMAL_FONT->GetPointSize() )
831 {
832 desc << wxT(' ') << size;
833 }
834
835 #if wxUSE_FONTMAP
836 wxFontEncoding enc = GetEncoding();
837 if ( enc != wxFONTENCODING_DEFAULT && enc != wxFONTENCODING_SYSTEM )
838 {
839 desc << wxT(' ') << wxFontMapper::GetEncodingName(enc);
840 }
841 #endif // wxUSE_FONTMAP
842
843 return desc.Strip(wxString::both).MakeLower();
844 }
845
846 bool wxNativeFontInfo::FromUserString(const wxString& s)
847 {
848 // reset to the default state
849 Init();
850
851 // ToUserString() will quote the facename if it contains spaces, commas
852 // or semicolons: we must be able to understand that quoted text is
853 // a single token:
854 wxString toparse(s);
855
856 // parse a more or less free form string
857 wxStringTokenizer tokenizer(toparse, wxT(";, "), wxTOKEN_STRTOK);
858
859 wxString face;
860 unsigned long size;
861 bool weightfound = false, pointsizefound = false;
862 #if wxUSE_FONTMAP
863 bool encodingfound = false;
864 #endif
865 bool insideQuotes = false;
866
867 while ( tokenizer.HasMoreTokens() )
868 {
869 wxString token = tokenizer.GetNextToken();
870
871 // normalize it
872 token.Trim(true).Trim(false).MakeLower();
873 if (insideQuotes)
874 {
875 if (token.StartsWith("'") ||
876 token.EndsWith("'"))
877 {
878 insideQuotes = false;
879
880 // add this last token to the facename:
881 face += " " + token;
882
883 // normalize facename:
884 face = face.Trim(true).Trim(false);
885 face.Replace("'", "");
886
887 continue;
888 }
889 }
890 else
891 {
892 if (token.StartsWith("'"))
893 insideQuotes = true;
894 }
895
896 // look for the known tokens
897 if ( insideQuotes )
898 {
899 // only the facename may be quoted:
900 face += " " + token;
901 continue;
902 }
903 if ( token == wxT("underlined") || token == _("underlined") )
904 {
905 SetUnderlined(true);
906 }
907 else if ( token == wxT("light") || token == _("light") )
908 {
909 SetWeight(wxFONTWEIGHT_LIGHT);
910 weightfound = true;
911 }
912 else if ( token == wxT("bold") || token == _("bold") )
913 {
914 SetWeight(wxFONTWEIGHT_BOLD);
915 weightfound = true;
916 }
917 else if ( token == wxT("italic") || token == _("italic") )
918 {
919 SetStyle(wxFONTSTYLE_ITALIC);
920 }
921 else if ( token.ToULong(&size) )
922 {
923 SetPointSize(size);
924 pointsizefound = true;
925 }
926 else
927 {
928 #if wxUSE_FONTMAP
929 // try to interpret this as an encoding
930 wxFontEncoding encoding = wxFontMapper::Get()->CharsetToEncoding(token, false);
931 if ( encoding != wxFONTENCODING_DEFAULT &&
932 encoding != wxFONTENCODING_SYSTEM ) // returned when the recognition failed
933 {
934 SetEncoding(encoding);
935 encodingfound = true;
936 }
937 else
938 {
939 #endif // wxUSE_FONTMAP
940
941 // assume it is the face name
942 if ( !face.empty() )
943 {
944 face += wxT(' ');
945 }
946
947 face += token;
948
949 // skip the code which resets face below
950 continue;
951
952 #if wxUSE_FONTMAP
953 }
954 #endif // wxUSE_FONTMAP
955 }
956
957 // if we had had the facename, we shouldn't continue appending tokens
958 // to it (i.e. "foo bold bar" shouldn't result in the facename "foo
959 // bar")
960 if ( !face.empty() )
961 {
962 wxString familyStr;
963 if ( face.EndsWith(" family", &familyStr) )
964 {
965 // it's not a facename but rather a font family
966 wxFontFamily family;
967 if ( familyStr == "decorative" )
968 family = wxFONTFAMILY_DECORATIVE;
969 else if ( familyStr == "roman" )
970 family = wxFONTFAMILY_ROMAN;
971 else if ( familyStr == "script" )
972 family = wxFONTFAMILY_SCRIPT;
973 else if ( familyStr == "swiss" )
974 family = wxFONTFAMILY_SWISS;
975 else if ( familyStr == "modern" )
976 family = wxFONTFAMILY_MODERN;
977 else if ( familyStr == "teletype" )
978 family = wxFONTFAMILY_TELETYPE;
979 else
980 return false;
981
982 SetFamily(family);
983 }
984 // NB: the check on the facename is implemented in wxFontBase::SetFaceName
985 // and not in wxNativeFontInfo::SetFaceName thus we need to explicitely
986 // call here wxFontEnumerator::IsValidFacename
987 else if (
988 #if wxUSE_FONTENUM
989 !wxFontEnumerator::IsValidFacename(face) ||
990 #endif // wxUSE_FONTENUM
991 !SetFaceName(face) )
992 {
993 SetFaceName(wxNORMAL_FONT->GetFaceName());
994 }
995
996 face.clear();
997 }
998 }
999
1000 // we might not have flushed it inside the loop
1001 if ( !face.empty() )
1002 {
1003 // NB: the check on the facename is implemented in wxFontBase::SetFaceName
1004 // and not in wxNativeFontInfo::SetFaceName thus we need to explicitely
1005 // call here wxFontEnumerator::IsValidFacename
1006 if (
1007 #if wxUSE_FONTENUM
1008 !wxFontEnumerator::IsValidFacename(face) ||
1009 #endif // wxUSE_FONTENUM
1010 !SetFaceName(face) )
1011 {
1012 SetFaceName(wxNORMAL_FONT->GetFaceName());
1013 }
1014 }
1015
1016 // set point size to default value if size was not given
1017 if ( !pointsizefound )
1018 SetPointSize(wxNORMAL_FONT->GetPointSize());
1019
1020 // set font weight to default value if weight was not given
1021 if ( !weightfound )
1022 SetWeight(wxFONTWEIGHT_NORMAL);
1023
1024 #if wxUSE_FONTMAP
1025 // set font encoding to default value if encoding was not given
1026 if ( !encodingfound )
1027 SetEncoding(wxFONTENCODING_SYSTEM);
1028 #endif // wxUSE_FONTMAP
1029
1030 return true;
1031 }
1032
1033 #endif // generic or wxMSW or wxOS2
1034
1035
1036 // wxFont <-> wxString utilities, used by wxConfig
1037 wxString wxToString(const wxFontBase& font)
1038 {
1039 return font.IsOk() ? font.GetNativeFontInfoDesc()
1040 : wxString();
1041 }
1042
1043 bool wxFromString(const wxString& str, wxFontBase *font)
1044 {
1045 wxCHECK_MSG( font, false, wxT("NULL output parameter") );
1046
1047 if ( str.empty() )
1048 {
1049 *font = wxNullFont;
1050 return true;
1051 }
1052
1053 return font->SetNativeFontInfo(str);
1054 }
1055
1056