]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/common/fontcmn.cpp
fix memory leak in wxScreenDC, fixes #13249
[wxWidgets.git] / src / common / fontcmn.cpp
... / ...
CommitLineData
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
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 ? 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
78wxBEGIN_ENUM( wxFontFamily )
79wxENUM_MEMBER( wxFONTFAMILY_DEFAULT )
80wxENUM_MEMBER( wxFONTFAMILY_DECORATIVE )
81wxENUM_MEMBER( wxFONTFAMILY_ROMAN )
82wxENUM_MEMBER( wxFONTFAMILY_SCRIPT )
83wxENUM_MEMBER( wxFONTFAMILY_SWISS )
84wxENUM_MEMBER( wxFONTFAMILY_MODERN )
85wxENUM_MEMBER( wxFONTFAMILY_TELETYPE )
86wxEND_ENUM( wxFontFamily )
87
88wxBEGIN_ENUM( wxFontStyle )
89wxENUM_MEMBER( wxFONTSTYLE_NORMAL )
90wxENUM_MEMBER( wxFONTSTYLE_ITALIC )
91wxENUM_MEMBER( wxFONTSTYLE_SLANT )
92wxEND_ENUM( wxFontStyle )
93
94wxBEGIN_ENUM( wxFontWeight )
95wxENUM_MEMBER( wxFONTWEIGHT_NORMAL )
96wxENUM_MEMBER( wxFONTWEIGHT_LIGHT )
97wxENUM_MEMBER( wxFONTWEIGHT_BOLD )
98wxEND_ENUM( wxFontWeight )
99
100wxIMPLEMENT_DYNAMIC_CLASS_WITH_COPY_XTI(wxFont, wxGDIObject, "wx/font.h")
101
102//WX_IMPLEMENT_ANY_VALUE_TYPE(wxAnyValueTypeImpl<wxFont>)
103
104wxBEGIN_PROPERTIES_TABLE(wxFont)
105wxPROPERTY( Size,int, SetPointSize, GetPointSize, 12, 0 /*flags*/, \
106 wxT("Helpstring"), wxT("group"))
107wxPROPERTY( Family, wxFontFamily , SetFamily, GetFamily, (wxFontFamily)wxDEFAULT, \
108 0 /*flags*/, wxT("Helpstring"), wxT("group")) // wxFontFamily
109wxPROPERTY( Style, wxFontStyle, SetStyle, GetStyle, (wxFontStyle)wxNORMAL, 0 /*flags*/, \
110 wxT("Helpstring"), wxT("group")) // wxFontStyle
111wxPROPERTY( Weight, wxFontWeight, SetWeight, GetWeight, (wxFontWeight)wxNORMAL, 0 /*flags*/, \
112 wxT("Helpstring"), wxT("group")) // wxFontWeight
113wxPROPERTY( Underlined, bool, SetUnderlined, GetUnderlined, false, 0 /*flags*/, \
114 wxT("Helpstring"), wxT("group"))
115wxPROPERTY( Face, wxString, SetFaceName, GetFaceName, wxEMPTY_PARAMETER_VALUE, \
116 0 /*flags*/, wxT("Helpstring"), wxT("group"))
117wxPROPERTY( Encoding, wxFontEncoding, SetEncoding, GetEncoding, \
118 wxFONTENCODING_DEFAULT, 0 /*flags*/, wxT("Helpstring"), wxT("group"))
119wxEND_PROPERTIES_TABLE()
120
121wxCONSTRUCTOR_6( wxFont, int, Size, wxFontFamily, Family, wxFontStyle, Style, wxFontWeight, Weight, \
122 bool, Underlined, wxString, Face )
123
124wxEMPTY_HANDLERS_TABLE(wxFont)
125
126// ============================================================================
127// implementation
128// ============================================================================
129
130// ----------------------------------------------------------------------------
131// helper functions
132// ----------------------------------------------------------------------------
133
134static 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
143static 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
152static inline bool flags2Underlined(int flags)
153{
154 return (flags & wxFONTFLAG_UNDERLINED) != 0;
155}
156
157// ----------------------------------------------------------------------------
158// wxFontBase
159// ----------------------------------------------------------------------------
160
161wxFontEncoding wxFontBase::ms_encodingDefault = wxFONTENCODING_SYSTEM;
162
163/* static */
164void 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
174wxFontBase::~wxFontBase()
175{
176 // this destructor is required for Darwin
177}
178
179/* static */
180wxFont *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 */
192wxFont *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 */
205wxFont *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 */
216wxFont *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 */
227wxFont *wxFontBase::New(const wxNativeFontInfo& info)
228{
229 return new wxFont(info);
230}
231
232/* static */
233wxFont *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
242bool wxFontBase::IsFixedWidth() const
243{
244 return GetFamily() == wxFONTFAMILY_TELETYPE;
245}
246
247wxSize wxFontBase::GetPixelSize() const
248{
249 wxScreenDC dc;
250 dc.SetFont(*(wxFont *)this);
251 return wxSize(dc.GetCharWidth(), dc.GetCharHeight());
252}
253
254bool wxFontBase::IsUsingSizeInPixels() const
255{
256 return false;
257}
258
259void 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
322void 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
337wxString 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
356wxString 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
375bool 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
387bool 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
399bool 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
422wxFontFamily 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
436wxString 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
453wxString 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
466wxString 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
479bool 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
494void wxFontBase::SetSymbolicSize(wxFontSymbolicSize size)
495{
496 SetSymbolicSizeRelativeTo(size, wxNORMAL_FONT->GetPointSize());
497}
498
499/* static */
500int wxFontBase::AdjustToSymbolicSize(wxFontSymbolicSize size, int base)
501{
502 // Using a fixed factor (1.2, from CSS2) is a bad idea as explained at
503 // http://www.w3.org/TR/CSS21/fonts.html#font-size-props so use the values
504 // from http://style.cleverchimp.com/font_size_intervals/altintervals.html
505 // instead.
506 static const float factors[] = { 0.60f, 0.75f, 0.89f, 1.f, 1.2f, 1.5f, 2.f };
507
508 wxCOMPILE_TIME_ASSERT
509 (
510 WXSIZEOF(factors) == wxFONTSIZE_XX_LARGE - wxFONTSIZE_XX_SMALL + 1,
511 WrongFontSizeFactorsSize
512 );
513
514 return factors[size - wxFONTSIZE_XX_SMALL]*base;
515}
516
517wxFont& wxFont::MakeBold()
518{
519 SetWeight(wxFONTWEIGHT_BOLD);
520 return *this;
521}
522
523wxFont wxFont::Bold() const
524{
525 wxFont font(*this);
526 font.MakeBold();
527 return font;
528}
529
530wxFont& wxFont::MakeItalic()
531{
532 SetStyle(wxFONTSTYLE_ITALIC);
533 return *this;
534}
535
536wxFont wxFont::Italic() const
537{
538 wxFont font(*this);
539 font.MakeItalic();
540 return font;
541}
542
543wxFont& wxFont::MakeUnderlined()
544{
545 SetUnderlined(true);
546 return *this;
547}
548
549wxFont wxFont::Underlined() const
550{
551 wxFont font(*this);
552 font.MakeUnderlined();
553 return font;
554}
555
556wxFont& wxFont::Scale(float x)
557{
558 SetPointSize(int(x*GetPointSize() + 0.5));
559 return *this;
560}
561
562wxFont wxFont::Scaled(float x) const
563{
564 wxFont font(*this);
565 font.Scale(x);
566 return font;
567}
568
569// ----------------------------------------------------------------------------
570// wxNativeFontInfo
571// ----------------------------------------------------------------------------
572
573// Up to now, there are no native implementations of this function:
574void wxNativeFontInfo::SetFaceName(const wxArrayString& facenames)
575{
576#if wxUSE_FONTENUM
577 for (size_t i=0; i < facenames.GetCount(); i++)
578 {
579 if (wxFontEnumerator::IsValidFacename(facenames[i]))
580 {
581 SetFaceName(facenames[i]);
582 return;
583 }
584 }
585
586 // set the first valid facename we can find on this system
587 wxString validfacename = wxFontEnumerator::GetFacenames().Item(0);
588 wxLogTrace(wxT("font"), wxT("Falling back to '%s'"), validfacename.c_str());
589 SetFaceName(validfacename);
590#else // !wxUSE_FONTENUM
591 SetFaceName(facenames[0]);
592#endif // wxUSE_FONTENUM/!wxUSE_FONTENUM
593}
594
595
596#ifdef wxNO_NATIVE_FONTINFO
597
598// These are the generic forms of FromString()/ToString.
599//
600// convert to/from the string representation: format is
601// version;pointsize;family;style;weight;underlined;facename;encoding
602
603bool wxNativeFontInfo::FromString(const wxString& s)
604{
605 long l;
606
607 wxStringTokenizer tokenizer(s, wxT(";"));
608
609 wxString token = tokenizer.GetNextToken();
610 //
611 // Ignore the version for now
612 //
613
614 token = tokenizer.GetNextToken();
615 if ( !token.ToLong(&l) )
616 return false;
617 pointSize = (int)l;
618
619 token = tokenizer.GetNextToken();
620 if ( !token.ToLong(&l) )
621 return false;
622 family = (wxFontFamily)l;
623
624 token = tokenizer.GetNextToken();
625 if ( !token.ToLong(&l) )
626 return false;
627 style = (wxFontStyle)l;
628
629 token = tokenizer.GetNextToken();
630 if ( !token.ToLong(&l) )
631 return false;
632 weight = (wxFontWeight)l;
633
634 token = tokenizer.GetNextToken();
635 if ( !token.ToLong(&l) )
636 return false;
637 underlined = l != 0;
638
639 faceName = tokenizer.GetNextToken();
640
641#ifndef __WXMAC__
642 if( !faceName )
643 return false;
644#endif
645
646 token = tokenizer.GetNextToken();
647 if ( !token.ToLong(&l) )
648 return false;
649 encoding = (wxFontEncoding)l;
650
651 return true;
652}
653
654wxString wxNativeFontInfo::ToString() const
655{
656 wxString s;
657
658 s.Printf(wxT("%d;%d;%d;%d;%d;%d;%s;%d"),
659 0, // version
660 pointSize,
661 family,
662 (int)style,
663 (int)weight,
664 underlined,
665 faceName.GetData(),
666 (int)encoding);
667
668 return s;
669}
670
671void wxNativeFontInfo::Init()
672{
673 pointSize = 0;
674 family = wxFONTFAMILY_DEFAULT;
675 style = wxFONTSTYLE_NORMAL;
676 weight = wxFONTWEIGHT_NORMAL;
677 underlined = false;
678 faceName.clear();
679 encoding = wxFONTENCODING_DEFAULT;
680}
681
682int wxNativeFontInfo::GetPointSize() const
683{
684 return pointSize;
685}
686
687wxFontStyle wxNativeFontInfo::GetStyle() const
688{
689 return style;
690}
691
692wxFontWeight wxNativeFontInfo::GetWeight() const
693{
694 return weight;
695}
696
697bool wxNativeFontInfo::GetUnderlined() const
698{
699 return underlined;
700}
701
702wxString wxNativeFontInfo::GetFaceName() const
703{
704 return faceName;
705}
706
707wxFontFamily wxNativeFontInfo::GetFamily() const
708{
709 return family;
710}
711
712wxFontEncoding wxNativeFontInfo::GetEncoding() const
713{
714 return encoding;
715}
716
717void wxNativeFontInfo::SetPointSize(int pointsize)
718{
719 pointSize = pointsize;
720}
721
722void wxNativeFontInfo::SetStyle(wxFontStyle style_)
723{
724 style = style_;
725}
726
727void wxNativeFontInfo::SetWeight(wxFontWeight weight_)
728{
729 weight = weight_;
730}
731
732void wxNativeFontInfo::SetUnderlined(bool underlined_)
733{
734 underlined = underlined_;
735}
736
737bool wxNativeFontInfo::SetFaceName(const wxString& facename_)
738{
739 faceName = facename_;
740 return true;
741}
742
743void wxNativeFontInfo::SetFamily(wxFontFamily family_)
744{
745 family = family_;
746}
747
748void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding_)
749{
750 encoding = encoding_;
751}
752
753#endif // generic wxNativeFontInfo implementation
754
755// conversion to/from user-readable string: this is used in the generic
756// versions and under MSW as well because there is no standard font description
757// format there anyhow (but there is a well-defined standard for X11 fonts used
758// by wxGTK and wxMotif)
759
760#if defined(wxNO_NATIVE_FONTINFO) || defined(__WXMSW__) || defined (__WXPM__) || defined(__WXOSX__)
761
762wxString wxNativeFontInfo::ToUserString() const
763{
764 wxString desc;
765
766 // first put the adjectives, if any - this is English-centric, of course,
767 // but what else can we do?
768 if ( GetUnderlined() )
769 {
770 desc << _("underlined");
771 }
772
773 switch ( GetWeight() )
774 {
775 default:
776 wxFAIL_MSG( wxT("unknown font weight") );
777 // fall through
778
779 case wxFONTWEIGHT_NORMAL:
780 break;
781
782 case wxFONTWEIGHT_LIGHT:
783 desc << _(" light");
784 break;
785
786 case wxFONTWEIGHT_BOLD:
787 desc << _(" bold");
788 break;
789 }
790
791 switch ( GetStyle() )
792 {
793 default:
794 wxFAIL_MSG( wxT("unknown font style") );
795 // fall through
796
797 case wxFONTSTYLE_NORMAL:
798 break;
799
800 // we don't distinguish between the two for now anyhow...
801 case wxFONTSTYLE_ITALIC:
802 case wxFONTSTYLE_SLANT:
803 desc << _(" italic");
804 break;
805 }
806
807 wxString face = GetFaceName();
808 if ( !face.empty() )
809 {
810 if (face.Contains(' ') || face.Contains(';') || face.Contains(','))
811 {
812 face.Replace("'", "");
813 // eventually remove quote characters: most systems do not
814 // allow them in a facename anyway so this usually does nothing
815
816 // make it possible for FromUserString() function to understand
817 // that the different words which compose this facename are
818 // not different adjectives or other data but rather all parts
819 // of the facename
820 desc << wxT(" '") << face << _("'");
821 }
822 else
823 desc << wxT(' ') << face;
824 }
825 else // no face name specified
826 {
827 // use the family
828 wxString familyStr;
829 switch ( GetFamily() )
830 {
831 case wxFONTFAMILY_DECORATIVE:
832 familyStr = "decorative";
833 break;
834
835 case wxFONTFAMILY_ROMAN:
836 familyStr = "roman";
837 break;
838
839 case wxFONTFAMILY_SCRIPT:
840 familyStr = "script";
841 break;
842
843 case wxFONTFAMILY_SWISS:
844 familyStr = "swiss";
845 break;
846
847 case wxFONTFAMILY_MODERN:
848 familyStr = "modern";
849 break;
850
851 case wxFONTFAMILY_TELETYPE:
852 familyStr = "teletype";
853 break;
854
855 case wxFONTFAMILY_DEFAULT:
856 case wxFONTFAMILY_UNKNOWN:
857 break;
858
859 default:
860 wxFAIL_MSG( "unknown font family" );
861 }
862
863 if ( !familyStr.empty() )
864 desc << " '" << familyStr << " family'";
865 }
866
867 int size = GetPointSize();
868 if ( size != wxNORMAL_FONT->GetPointSize() )
869 {
870 desc << wxT(' ') << size;
871 }
872
873#if wxUSE_FONTMAP
874 wxFontEncoding enc = GetEncoding();
875 if ( enc != wxFONTENCODING_DEFAULT && enc != wxFONTENCODING_SYSTEM )
876 {
877 desc << wxT(' ') << wxFontMapper::GetEncodingName(enc);
878 }
879#endif // wxUSE_FONTMAP
880
881 return desc.Strip(wxString::both).MakeLower();
882}
883
884bool wxNativeFontInfo::FromUserString(const wxString& s)
885{
886 // reset to the default state
887 Init();
888
889 // ToUserString() will quote the facename if it contains spaces, commas
890 // or semicolons: we must be able to understand that quoted text is
891 // a single token:
892 wxString toparse(s);
893
894 // parse a more or less free form string
895 wxStringTokenizer tokenizer(toparse, wxT(";, "), wxTOKEN_STRTOK);
896
897 wxString face;
898 unsigned long size;
899 bool weightfound = false, pointsizefound = false;
900#if wxUSE_FONTMAP
901 bool encodingfound = false;
902#endif
903 bool insideQuotes = false;
904
905 while ( tokenizer.HasMoreTokens() )
906 {
907 wxString token = tokenizer.GetNextToken();
908
909 // normalize it
910 token.Trim(true).Trim(false).MakeLower();
911 if (insideQuotes)
912 {
913 if (token.StartsWith("'") ||
914 token.EndsWith("'"))
915 {
916 insideQuotes = false;
917
918 // add this last token to the facename:
919 face += " " + token;
920
921 // normalize facename:
922 face = face.Trim(true).Trim(false);
923 face.Replace("'", "");
924
925 continue;
926 }
927 }
928 else
929 {
930 if (token.StartsWith("'"))
931 insideQuotes = true;
932 }
933
934 // look for the known tokens
935 if ( insideQuotes )
936 {
937 // only the facename may be quoted:
938 face += " " + token;
939 continue;
940 }
941 if ( token == wxT("underlined") || token == _("underlined") )
942 {
943 SetUnderlined(true);
944 }
945 else if ( token == wxT("light") || token == _("light") )
946 {
947 SetWeight(wxFONTWEIGHT_LIGHT);
948 weightfound = true;
949 }
950 else if ( token == wxT("bold") || token == _("bold") )
951 {
952 SetWeight(wxFONTWEIGHT_BOLD);
953 weightfound = true;
954 }
955 else if ( token == wxT("italic") || token == _("italic") )
956 {
957 SetStyle(wxFONTSTYLE_ITALIC);
958 }
959 else if ( token.ToULong(&size) )
960 {
961 SetPointSize(size);
962 pointsizefound = true;
963 }
964 else
965 {
966#if wxUSE_FONTMAP
967 // try to interpret this as an encoding
968 wxFontEncoding encoding = wxFontMapper::Get()->CharsetToEncoding(token, false);
969 if ( encoding != wxFONTENCODING_DEFAULT &&
970 encoding != wxFONTENCODING_SYSTEM ) // returned when the recognition failed
971 {
972 SetEncoding(encoding);
973 encodingfound = true;
974 }
975 else
976 {
977#endif // wxUSE_FONTMAP
978
979 // assume it is the face name
980 if ( !face.empty() )
981 {
982 face += wxT(' ');
983 }
984
985 face += token;
986
987 // skip the code which resets face below
988 continue;
989
990#if wxUSE_FONTMAP
991 }
992#endif // wxUSE_FONTMAP
993 }
994
995 // if we had had the facename, we shouldn't continue appending tokens
996 // to it (i.e. "foo bold bar" shouldn't result in the facename "foo
997 // bar")
998 if ( !face.empty() )
999 {
1000 wxString familyStr;
1001 if ( face.EndsWith(" family", &familyStr) )
1002 {
1003 // it's not a facename but rather a font family
1004 wxFontFamily family;
1005 if ( familyStr == "decorative" )
1006 family = wxFONTFAMILY_DECORATIVE;
1007 else if ( familyStr == "roman" )
1008 family = wxFONTFAMILY_ROMAN;
1009 else if ( familyStr == "script" )
1010 family = wxFONTFAMILY_SCRIPT;
1011 else if ( familyStr == "swiss" )
1012 family = wxFONTFAMILY_SWISS;
1013 else if ( familyStr == "modern" )
1014 family = wxFONTFAMILY_MODERN;
1015 else if ( familyStr == "teletype" )
1016 family = wxFONTFAMILY_TELETYPE;
1017 else
1018 return false;
1019
1020 SetFamily(family);
1021 }
1022 // NB: the check on the facename is implemented in wxFontBase::SetFaceName
1023 // and not in wxNativeFontInfo::SetFaceName thus we need to explicitly
1024 // call here wxFontEnumerator::IsValidFacename
1025 else if (
1026#if wxUSE_FONTENUM
1027 !wxFontEnumerator::IsValidFacename(face) ||
1028#endif // wxUSE_FONTENUM
1029 !SetFaceName(face) )
1030 {
1031 SetFaceName(wxNORMAL_FONT->GetFaceName());
1032 }
1033
1034 face.clear();
1035 }
1036 }
1037
1038 // we might not have flushed it inside the loop
1039 if ( !face.empty() )
1040 {
1041 // NB: the check on the facename is implemented in wxFontBase::SetFaceName
1042 // and not in wxNativeFontInfo::SetFaceName thus we need to explicitly
1043 // call here wxFontEnumerator::IsValidFacename
1044 if (
1045#if wxUSE_FONTENUM
1046 !wxFontEnumerator::IsValidFacename(face) ||
1047#endif // wxUSE_FONTENUM
1048 !SetFaceName(face) )
1049 {
1050 SetFaceName(wxNORMAL_FONT->GetFaceName());
1051 }
1052 }
1053
1054 // set point size to default value if size was not given
1055 if ( !pointsizefound )
1056 SetPointSize(wxNORMAL_FONT->GetPointSize());
1057
1058 // set font weight to default value if weight was not given
1059 if ( !weightfound )
1060 SetWeight(wxFONTWEIGHT_NORMAL);
1061
1062#if wxUSE_FONTMAP
1063 // set font encoding to default value if encoding was not given
1064 if ( !encodingfound )
1065 SetEncoding(wxFONTENCODING_SYSTEM);
1066#endif // wxUSE_FONTMAP
1067
1068 return true;
1069}
1070
1071#endif // generic or wxMSW or wxOS2
1072
1073
1074// wxFont <-> wxString utilities, used by wxConfig
1075wxString wxToString(const wxFontBase& font)
1076{
1077 return font.IsOk() ? font.GetNativeFontInfoDesc()
1078 : wxString();
1079}
1080
1081bool wxFromString(const wxString& str, wxFontBase *font)
1082{
1083 wxCHECK_MSG( font, false, wxT("NULL output parameter") );
1084
1085 if ( str.empty() )
1086 {
1087 *font = wxNullFont;
1088 return true;
1089 }
1090
1091 return font->SetNativeFontInfo(str);
1092}
1093
1094