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