]> git.saurik.com Git - wxWidgets.git/blob - src/common/fontcmn.cpp
[ 1509599 ] 'Split pickers page in widgets sample' with more icons and rebaking.
[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 // ============================================================================
49 // implementation
50 // ============================================================================
51
52 // ----------------------------------------------------------------------------
53 // helper functions
54 // ----------------------------------------------------------------------------
55
56 static void AdjustFontSize(wxFont& font, wxDC& dc, const wxSize& pixelSize)
57 {
58 int currentSize = 0;
59 int largestGood = 0;
60 int smallestBad = 0;
61
62 bool initialGoodFound = false;
63 bool initialBadFound = false;
64
65 // NB: this assignment was separated from the variable definition
66 // in order to fix a gcc v3.3.3 compiler crash
67 currentSize = font.GetPointSize();
68 while (currentSize > 0)
69 {
70 dc.SetFont(font);
71
72 // if currentSize (in points) results in a font that is smaller
73 // than required by pixelSize it is considered a good size
74 if (dc.GetCharHeight() <= pixelSize.GetHeight() &&
75 (!pixelSize.GetWidth() ||
76 dc.GetCharWidth() <= pixelSize.GetWidth()))
77 {
78 largestGood = currentSize;
79 initialGoodFound = true;
80 }
81 else
82 {
83 smallestBad = currentSize;
84 initialBadFound = true;
85 }
86 if (!initialGoodFound)
87 {
88 currentSize /= 2;
89 }
90 else if (!initialBadFound)
91 {
92 currentSize *= 2;
93 }
94 else
95 {
96 int distance = smallestBad - largestGood;
97 if (distance == 1)
98 break;
99
100 currentSize = largestGood + distance / 2;
101 }
102
103 font.SetPointSize(currentSize);
104 }
105
106 if (currentSize != largestGood)
107 font.SetPointSize(largestGood);
108 }
109
110 // ----------------------------------------------------------------------------
111 // wxFontBase
112 // ----------------------------------------------------------------------------
113
114 wxFontEncoding wxFontBase::ms_encodingDefault = wxFONTENCODING_SYSTEM;
115
116 /* static */
117 void wxFontBase::SetDefaultEncoding(wxFontEncoding encoding)
118 {
119 // GetDefaultEncoding() should return something != wxFONTENCODING_DEFAULT
120 // and, besides, using this value here doesn't make any sense
121 wxCHECK_RET( encoding != wxFONTENCODING_DEFAULT,
122 _T("can't set default encoding to wxFONTENCODING_DEFAULT") );
123
124 ms_encodingDefault = encoding;
125 }
126
127 wxFontBase::~wxFontBase()
128 {
129 // this destructor is required for Darwin
130 }
131
132 /* static */
133 wxFont *wxFontBase::New(int size,
134 int family,
135 int style,
136 int weight,
137 bool underlined,
138 const wxString& face,
139 wxFontEncoding encoding)
140 {
141 return new wxFont(size, family, style, weight, underlined, face, encoding);
142 }
143
144 static inline int flags2Style(int flags)
145 {
146 return flags & wxFONTFLAG_ITALIC
147 ? wxFONTSTYLE_ITALIC
148 : flags & wxFONTFLAG_SLANT
149 ? wxFONTSTYLE_SLANT
150 : wxFONTSTYLE_NORMAL;
151 }
152
153 static inline int flags2Weight(int flags)
154 {
155 return flags & wxFONTFLAG_LIGHT
156 ? wxFONTWEIGHT_LIGHT
157 : flags & wxFONTFLAG_BOLD
158 ? wxFONTWEIGHT_BOLD
159 : wxFONTWEIGHT_NORMAL;
160 }
161
162 static inline bool flags2Underlined(int flags)
163 {
164 return (flags & wxFONTFLAG_UNDERLINED) != 0;
165 }
166
167 /* static */
168 wxFont *wxFontBase::New(int pointSize,
169 wxFontFamily family,
170 int flags,
171 const wxString& face,
172 wxFontEncoding encoding)
173 {
174 return New(pointSize, family, flags2Style(flags), flags2Weight(flags),
175 flags2Underlined(flags), face, encoding);
176 }
177
178 /* static */
179 wxFont *wxFontBase::New(const wxSize& pixelSize,
180 int family,
181 int style,
182 int weight,
183 bool underlined,
184 const wxString& face,
185 wxFontEncoding encoding)
186 {
187 #if defined(__WXMSW__)
188 return new wxFont(pixelSize, family, style, weight, underlined,
189 face, encoding);
190 #else
191 wxFont *self = New(10, family, style, weight, underlined, face, encoding);
192 wxScreenDC dc;
193 AdjustFontSize(*(wxFont *)self, dc, pixelSize);
194 return self;
195 #endif
196 }
197
198 /* static */
199 wxFont *wxFontBase::New(const wxSize& pixelSize,
200 wxFontFamily family,
201 int flags,
202 const wxString& face,
203 wxFontEncoding encoding)
204 {
205 return New(pixelSize, family, flags2Style(flags), flags2Weight(flags),
206 flags2Underlined(flags), face, encoding);
207 }
208
209 wxSize wxFontBase::GetPixelSize() const
210 {
211 wxScreenDC dc;
212 dc.SetFont(*(wxFont *)this);
213 return wxSize(dc.GetCharWidth(), dc.GetCharHeight());
214 }
215
216 bool wxFontBase::IsUsingSizeInPixels() const
217 {
218 return false;
219 }
220
221 void wxFontBase::SetPixelSize( const wxSize& pixelSize )
222 {
223 wxScreenDC dc;
224 AdjustFontSize(*(wxFont *)this, dc, pixelSize);
225 }
226
227 /* static */
228 wxFont *wxFontBase::New(const wxNativeFontInfo& info)
229 {
230 return new wxFont(info);
231 }
232
233 /* static */
234 wxFont *wxFontBase::New(const wxString& strNativeFontDesc)
235 {
236 wxNativeFontInfo fontInfo;
237 if ( !fontInfo.FromString(strNativeFontDesc) )
238 return new wxFont(*wxNORMAL_FONT);
239
240 return New(fontInfo);
241 }
242
243 bool wxFontBase::IsFixedWidth() const
244 {
245 return GetFamily() == wxFONTFAMILY_TELETYPE;
246 }
247
248 void wxFontBase::DoSetNativeFontInfo(const wxNativeFontInfo& info)
249 {
250 #ifdef wxNO_NATIVE_FONTINFO
251 SetPointSize(info.pointSize);
252 SetFamily(info.family);
253 SetStyle(info.style);
254 SetWeight(info.weight);
255 SetUnderlined(info.underlined);
256 SetFaceName(info.faceName);
257 SetEncoding(info.encoding);
258 #else
259 (void)info;
260 #endif
261 }
262
263 wxString wxFontBase::GetNativeFontInfoDesc() const
264 {
265 wxString fontDesc;
266 const wxNativeFontInfo *fontInfo = GetNativeFontInfo();
267 if ( fontInfo )
268 {
269 fontDesc = fontInfo->ToString();
270 wxASSERT_MSG(!fontDesc.empty(), wxT("This should be a non-empty string!"));
271 }
272 else
273 {
274 wxFAIL_MSG(wxT("Derived class should have created the wxNativeFontInfo!"));
275 }
276
277 return fontDesc;
278 }
279
280 wxString wxFontBase::GetNativeFontInfoUserDesc() const
281 {
282 wxString fontDesc;
283 const wxNativeFontInfo *fontInfo = GetNativeFontInfo();
284 if ( fontInfo )
285 {
286 fontDesc = fontInfo->ToUserString();
287 wxASSERT_MSG(!fontDesc.empty(), wxT("This should be a non-empty string!"));
288 }
289 else
290 {
291 wxFAIL_MSG(wxT("Derived class should have created the wxNativeFontInfo!"));
292 }
293
294 return fontDesc;
295 }
296
297 bool wxFontBase::SetNativeFontInfo(const wxString& info)
298 {
299 wxNativeFontInfo fontInfo;
300 if ( !info.empty() && fontInfo.FromString(info) )
301 {
302 SetNativeFontInfo(fontInfo);
303 return true;
304 }
305
306 UnRef();
307 return false;
308 }
309
310 bool wxFontBase::SetNativeFontInfoUserDesc(const wxString& info)
311 {
312 wxNativeFontInfo fontInfo;
313 if ( !info.empty() && fontInfo.FromUserString(info) )
314 {
315 SetNativeFontInfo(fontInfo);
316 return true;
317 }
318
319 UnRef();
320 return false;
321 }
322
323 bool wxFontBase::operator==(const wxFont& font) const
324 {
325 // either it is the same font, i.e. they share the same common data or they
326 // have different ref datas but still describe the same font
327 return GetFontData() == font.GetFontData() ||
328 (
329 Ok() == font.Ok() &&
330 GetPointSize() == font.GetPointSize() &&
331 GetFamily() == font.GetFamily() &&
332 GetStyle() == font.GetStyle() &&
333 GetWeight() == font.GetWeight() &&
334 GetUnderlined() == font.GetUnderlined() &&
335 GetFaceName().IsSameAs(font.GetFaceName(), false) &&
336 GetEncoding() == font.GetEncoding()
337 );
338 }
339
340 bool wxFontBase::operator!=(const wxFont& font) const
341 {
342 return !(*this == font);
343 }
344
345 wxString wxFontBase::GetFamilyString() const
346 {
347 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
348
349 switch ( GetFamily() )
350 {
351 case wxDECORATIVE: return wxT("wxDECORATIVE");
352 case wxROMAN: return wxT("wxROMAN");
353 case wxSCRIPT: return wxT("wxSCRIPT");
354 case wxSWISS: return wxT("wxSWISS");
355 case wxMODERN: return wxT("wxMODERN");
356 case wxTELETYPE: return wxT("wxTELETYPE");
357 default: return wxT("wxDEFAULT");
358 }
359 }
360
361 wxString wxFontBase::GetStyleString() const
362 {
363 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
364
365 switch ( GetStyle() )
366 {
367 case wxNORMAL: return wxT("wxNORMAL");
368 case wxSLANT: return wxT("wxSLANT");
369 case wxITALIC: return wxT("wxITALIC");
370 default: return wxT("wxDEFAULT");
371 }
372 }
373
374 wxString wxFontBase::GetWeightString() const
375 {
376 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
377
378 switch ( GetWeight() )
379 {
380 case wxNORMAL: return wxT("wxNORMAL");
381 case wxBOLD: return wxT("wxBOLD");
382 case wxLIGHT: return wxT("wxLIGHT");
383 default: return wxT("wxDEFAULT");
384 }
385 }
386
387 bool wxFontBase::SetFaceName(const wxString &facename)
388 {
389 if (!wxFontEnumerator::IsValidFacename(facename))
390 {
391 UnRef(); // make Ok() return false
392 return false;
393 }
394
395 return true;
396 }
397
398
399 // ----------------------------------------------------------------------------
400 // wxNativeFontInfo
401 // ----------------------------------------------------------------------------
402
403 // Up to now, there are no native implementations of this function:
404 void wxNativeFontInfo::SetFaceName(const wxArrayString &facenames)
405 {
406 for (size_t i=0; i < facenames.GetCount(); i++)
407 {
408 if (wxFontEnumerator::IsValidFacename(facenames[i]))
409 {
410 SetFaceName(facenames[i]);
411 return;
412 }
413 }
414
415 // set the first valid facename we can find on this system
416 wxString validfacename = wxFontEnumerator::GetFacenames().Item(0);
417 wxLogTrace(wxT("font"), wxT("Falling back to '%s'"), validfacename.c_str());
418 SetFaceName(validfacename);
419 }
420
421
422 #ifdef wxNO_NATIVE_FONTINFO
423
424 // These are the generic forms of FromString()/ToString.
425 //
426 // convert to/from the string representation: format is
427 // version;pointsize;family;style;weight;underlined;facename;encoding
428
429 bool wxNativeFontInfo::FromString(const wxString& s)
430 {
431 long l;
432
433 wxStringTokenizer tokenizer(s, _T(";"));
434
435 wxString token = tokenizer.GetNextToken();
436 //
437 // Ignore the version for now
438 //
439
440 token = tokenizer.GetNextToken();
441 if ( !token.ToLong(&l) )
442 return false;
443 pointSize = (int)l;
444
445 token = tokenizer.GetNextToken();
446 if ( !token.ToLong(&l) )
447 return false;
448 family = (wxFontFamily)l;
449
450 token = tokenizer.GetNextToken();
451 if ( !token.ToLong(&l) )
452 return false;
453 style = (wxFontStyle)l;
454
455 token = tokenizer.GetNextToken();
456 if ( !token.ToLong(&l) )
457 return false;
458 weight = (wxFontWeight)l;
459
460 token = tokenizer.GetNextToken();
461 if ( !token.ToLong(&l) )
462 return false;
463 underlined = l != 0;
464
465 faceName = tokenizer.GetNextToken();
466
467 #ifndef __WXMAC__
468 if( !faceName )
469 return false;
470 #endif
471
472 token = tokenizer.GetNextToken();
473 if ( !token.ToLong(&l) )
474 return false;
475 encoding = (wxFontEncoding)l;
476
477 return true;
478 }
479
480 wxString wxNativeFontInfo::ToString() const
481 {
482 wxString s;
483
484 s.Printf(_T("%d;%d;%d;%d;%d;%d;%s;%d"),
485 0, // version
486 pointSize,
487 family,
488 (int)style,
489 (int)weight,
490 underlined,
491 faceName.GetData(),
492 (int)encoding);
493
494 return s;
495 }
496
497 void wxNativeFontInfo::Init()
498 {
499 pointSize = 0;
500 family = wxFONTFAMILY_DEFAULT;
501 style = wxFONTSTYLE_NORMAL;
502 weight = wxFONTWEIGHT_NORMAL;
503 underlined = false;
504 faceName.clear();
505 encoding = wxFONTENCODING_DEFAULT;
506 }
507
508 int wxNativeFontInfo::GetPointSize() const
509 {
510 return pointSize;
511 }
512
513 wxFontStyle wxNativeFontInfo::GetStyle() const
514 {
515 return style;
516 }
517
518 wxFontWeight wxNativeFontInfo::GetWeight() const
519 {
520 return weight;
521 }
522
523 bool wxNativeFontInfo::GetUnderlined() const
524 {
525 return underlined;
526 }
527
528 wxString wxNativeFontInfo::GetFaceName() const
529 {
530 return faceName;
531 }
532
533 wxFontFamily wxNativeFontInfo::GetFamily() const
534 {
535 return family;
536 }
537
538 wxFontEncoding wxNativeFontInfo::GetEncoding() const
539 {
540 return encoding;
541 }
542
543 void wxNativeFontInfo::SetPointSize(int pointsize)
544 {
545 pointSize = pointsize;
546 }
547
548 void wxNativeFontInfo::SetStyle(wxFontStyle style_)
549 {
550 style = style_;
551 }
552
553 void wxNativeFontInfo::SetWeight(wxFontWeight weight_)
554 {
555 weight = weight_;
556 }
557
558 void wxNativeFontInfo::SetUnderlined(bool underlined_)
559 {
560 underlined = underlined_;
561 }
562
563 bool wxNativeFontInfo::SetFaceName(const wxString& facename_)
564 {
565 faceName = facename_;
566 return true;
567 }
568
569 void wxNativeFontInfo::SetFamily(wxFontFamily family_)
570 {
571 family = family_;
572 }
573
574 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding_)
575 {
576 encoding = encoding_;
577 }
578
579 #endif // generic wxNativeFontInfo implementation
580
581 // conversion to/from user-readable string: this is used in the generic
582 // versions and under MSW as well because there is no standard font description
583 // format there anyhow (but there is a well-defined standard for X11 fonts used
584 // by wxGTK and wxMotif)
585
586 #if defined(wxNO_NATIVE_FONTINFO) || defined(__WXMSW__) || defined (__WXPM__)
587
588 wxString wxNativeFontInfo::ToUserString() const
589 {
590 wxString desc;
591
592 // first put the adjectives, if any - this is English-centric, of course,
593 // but what else can we do?
594 if ( GetUnderlined() )
595 {
596 desc << _("underlined");
597 }
598
599 switch ( GetWeight() )
600 {
601 default:
602 wxFAIL_MSG( _T("unknown font weight") );
603 // fall through
604
605 case wxFONTWEIGHT_NORMAL:
606 break;
607
608 case wxFONTWEIGHT_LIGHT:
609 desc << _(" light");
610 break;
611
612 case wxFONTWEIGHT_BOLD:
613 desc << _(" bold");
614 break;
615 }
616
617 switch ( GetStyle() )
618 {
619 default:
620 wxFAIL_MSG( _T("unknown font style") );
621 // fall through
622
623 case wxFONTSTYLE_NORMAL:
624 break;
625
626 // we don't distinguish between the two for now anyhow...
627 case wxFONTSTYLE_ITALIC:
628 case wxFONTSTYLE_SLANT:
629 desc << _(" italic");
630 break;
631 }
632
633 wxString face = GetFaceName();
634 if ( !face.empty() )
635 {
636 desc << _T(' ') << face;
637 }
638
639 int size = GetPointSize();
640 if ( size != wxNORMAL_FONT->GetPointSize() )
641 {
642 desc << _T(' ') << size;
643 }
644
645 #if wxUSE_FONTMAP
646 wxFontEncoding enc = GetEncoding();
647 if ( enc != wxFONTENCODING_DEFAULT && enc != wxFONTENCODING_SYSTEM )
648 {
649 desc << _T(' ') << wxFontMapper::GetEncodingName(enc);
650 }
651 #endif // wxUSE_FONTMAP
652
653 return desc.Strip(wxString::both).MakeLower();
654 }
655
656 bool wxNativeFontInfo::FromUserString(const wxString& s)
657 {
658 // reset to the default state
659 Init();
660
661 // parse a more or less free form string
662 //
663 // TODO: we should handle at least the quoted facenames
664 wxStringTokenizer tokenizer(s, _T(";, "), wxTOKEN_STRTOK);
665
666 wxString face;
667 unsigned long size;
668 bool weightfound = false, pointsizefound = false, encodingfound = false;
669
670 while ( tokenizer.HasMoreTokens() )
671 {
672 wxString token = tokenizer.GetNextToken();
673
674 // normalize it
675 token.Trim(true).Trim(false).MakeLower();
676
677 // look for the known tokens
678 if ( token == _T("underlined") || token == _("underlined") )
679 {
680 SetUnderlined(true);
681 }
682 else if ( token == _T("light") || token == _("light") )
683 {
684 SetWeight(wxFONTWEIGHT_LIGHT);
685 weightfound = true;
686 }
687 else if ( token == _T("bold") || token == _("bold") )
688 {
689 SetWeight(wxFONTWEIGHT_BOLD);
690 weightfound = true;
691 }
692 else if ( token == _T("italic") || token == _("italic") )
693 {
694 SetStyle(wxFONTSTYLE_ITALIC);
695 }
696 else if ( token.ToULong(&size) )
697 {
698 SetPointSize(size);
699 pointsizefound = true;
700 }
701 else
702 {
703 #if wxUSE_FONTMAP
704 // try to interpret this as an encoding
705 wxFontEncoding encoding = wxFontMapper::Get()->CharsetToEncoding(token, false);
706 if ( encoding != wxFONTENCODING_DEFAULT &&
707 encoding != wxFONTENCODING_SYSTEM ) // returned when the recognition failed
708 {
709 SetEncoding(encoding);
710 encodingfound = true;
711 }
712 else
713 {
714 #endif // wxUSE_FONTMAP
715
716 // assume it is the face name
717 if ( !face.empty() )
718 {
719 face += _T(' ');
720 }
721
722 face += token;
723
724 // skip the code which resets face below
725 continue;
726
727 #if wxUSE_FONTMAP
728 }
729 #endif // wxUSE_FONTMAP
730 }
731
732 // if we had had the facename, we shouldn't continue appending tokens
733 // to it (i.e. "foo bold bar" shouldn't result in the facename "foo
734 // bar")
735 if ( !face.empty() )
736 {
737 // NB: the check on the facename is implemented in wxFontBase::SetFaceName
738 // and not in wxNativeFontInfo::SetFaceName thus we need to explicitely
739 // call here wxFontEnumerator::IsValidFacename
740 if (!wxFontEnumerator::IsValidFacename(face) ||
741 !SetFaceName(face))
742 SetFaceName(wxNORMAL_FONT->GetFaceName());
743 face.clear();
744 }
745 }
746
747 // we might not have flushed it inside the loop
748 if ( !face.empty() )
749 {
750 // NB: the check on the facename is implemented in wxFontBase::SetFaceName
751 // and not in wxNativeFontInfo::SetFaceName thus we need to explicitely
752 // call here wxFontEnumerator::IsValidFacename
753 if (!wxFontEnumerator::IsValidFacename(face) ||
754 !SetFaceName(face))
755 SetFaceName(wxNORMAL_FONT->GetFaceName());
756 }
757
758 // set point size to default value if size was not given
759 if ( !pointsizefound )
760 SetPointSize(wxNORMAL_FONT->GetPointSize());
761
762 // set font weight to default value if weight was not given
763 if ( !weightfound )
764 SetWeight(wxFONTWEIGHT_NORMAL);
765
766 #if wxUSE_FONTMAP
767 // set font encoding to default value if encoding was not given
768 if ( !encodingfound )
769 SetEncoding(wxFONTENCODING_SYSTEM);
770 #endif // wxUSE_FONTMAP
771
772 return true;
773 }
774
775 #endif // generic or wxMSW or wxOS2