]> git.saurik.com Git - wxWidgets.git/blob - src/common/fontcmn.cpp
wxDFB: implemented Raise() and Lower() for TLWs
[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 return false;
307 }
308
309 bool wxFontBase::SetNativeFontInfoUserDesc(const wxString& info)
310 {
311 wxNativeFontInfo fontInfo;
312 if ( !info.empty() && fontInfo.FromUserString(info) )
313 {
314 SetNativeFontInfo(fontInfo);
315 return true;
316 }
317
318 return false;
319 }
320
321 bool wxFontBase::operator==(const wxFont& font) const
322 {
323 // either it is the same font, i.e. they share the same common data or they
324 // have different ref datas but still describe the same font
325 return IsSameAs(font) ||
326 (
327 Ok() == font.Ok() &&
328 GetPointSize() == font.GetPointSize() &&
329 // in wxGTK1 GetPixelSize() calls GetInternalFont() which uses
330 // operator==() resulting in infinite recursion so we can't use it
331 // in that port
332 #if !defined(__WXGTK__) || defined(__WXGTK20__)
333 GetPixelSize() == font.GetPixelSize() &&
334 #endif
335 GetFamily() == font.GetFamily() &&
336 GetStyle() == font.GetStyle() &&
337 GetWeight() == font.GetWeight() &&
338 GetUnderlined() == font.GetUnderlined() &&
339 GetFaceName().IsSameAs(font.GetFaceName(), false) &&
340 GetEncoding() == font.GetEncoding()
341 );
342 }
343
344 wxString wxFontBase::GetFamilyString() const
345 {
346 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
347
348 switch ( GetFamily() )
349 {
350 case wxDECORATIVE: return wxT("wxDECORATIVE");
351 case wxROMAN: return wxT("wxROMAN");
352 case wxSCRIPT: return wxT("wxSCRIPT");
353 case wxSWISS: return wxT("wxSWISS");
354 case wxMODERN: return wxT("wxMODERN");
355 case wxTELETYPE: return wxT("wxTELETYPE");
356 default: return wxT("wxDEFAULT");
357 }
358 }
359
360 wxString wxFontBase::GetStyleString() const
361 {
362 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
363
364 switch ( GetStyle() )
365 {
366 case wxNORMAL: return wxT("wxNORMAL");
367 case wxSLANT: return wxT("wxSLANT");
368 case wxITALIC: return wxT("wxITALIC");
369 default: return wxT("wxDEFAULT");
370 }
371 }
372
373 wxString wxFontBase::GetWeightString() const
374 {
375 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
376
377 switch ( GetWeight() )
378 {
379 case wxNORMAL: return wxT("wxNORMAL");
380 case wxBOLD: return wxT("wxBOLD");
381 case wxLIGHT: return wxT("wxLIGHT");
382 default: return wxT("wxDEFAULT");
383 }
384 }
385
386 bool wxFontBase::SetFaceName(const wxString& facename)
387 {
388 #if wxUSE_FONTENUM
389 if (!wxFontEnumerator::IsValidFacename(facename))
390 {
391 UnRef(); // make Ok() return false
392 return false;
393 }
394 #else // !wxUSE_FONTENUM
395 wxUnusedVar(facename);
396 #endif // wxUSE_FONTENUM/!wxUSE_FONTENUM
397
398 return true;
399 }
400
401
402 // ----------------------------------------------------------------------------
403 // wxNativeFontInfo
404 // ----------------------------------------------------------------------------
405
406 // Up to now, there are no native implementations of this function:
407 void wxNativeFontInfo::SetFaceName(const wxArrayString& facenames)
408 {
409 #if wxUSE_FONTENUM
410 for (size_t i=0; i < facenames.GetCount(); i++)
411 {
412 if (wxFontEnumerator::IsValidFacename(facenames[i]))
413 {
414 SetFaceName(facenames[i]);
415 return;
416 }
417 }
418
419 // set the first valid facename we can find on this system
420 wxString validfacename = wxFontEnumerator::GetFacenames().Item(0);
421 wxLogTrace(wxT("font"), wxT("Falling back to '%s'"), validfacename.c_str());
422 SetFaceName(validfacename);
423 #else // !wxUSE_FONTENUM
424 SetFaceName(facenames[0]);
425 #endif // wxUSE_FONTENUM/!wxUSE_FONTENUM
426 }
427
428
429 #ifdef wxNO_NATIVE_FONTINFO
430
431 // These are the generic forms of FromString()/ToString.
432 //
433 // convert to/from the string representation: format is
434 // version;pointsize;family;style;weight;underlined;facename;encoding
435
436 bool wxNativeFontInfo::FromString(const wxString& s)
437 {
438 long l;
439
440 wxStringTokenizer tokenizer(s, _T(";"));
441
442 wxString token = tokenizer.GetNextToken();
443 //
444 // Ignore the version for now
445 //
446
447 token = tokenizer.GetNextToken();
448 if ( !token.ToLong(&l) )
449 return false;
450 pointSize = (int)l;
451
452 token = tokenizer.GetNextToken();
453 if ( !token.ToLong(&l) )
454 return false;
455 family = (wxFontFamily)l;
456
457 token = tokenizer.GetNextToken();
458 if ( !token.ToLong(&l) )
459 return false;
460 style = (wxFontStyle)l;
461
462 token = tokenizer.GetNextToken();
463 if ( !token.ToLong(&l) )
464 return false;
465 weight = (wxFontWeight)l;
466
467 token = tokenizer.GetNextToken();
468 if ( !token.ToLong(&l) )
469 return false;
470 underlined = l != 0;
471
472 faceName = tokenizer.GetNextToken();
473
474 #ifndef __WXMAC__
475 if( !faceName )
476 return false;
477 #endif
478
479 token = tokenizer.GetNextToken();
480 if ( !token.ToLong(&l) )
481 return false;
482 encoding = (wxFontEncoding)l;
483
484 return true;
485 }
486
487 wxString wxNativeFontInfo::ToString() const
488 {
489 wxString s;
490
491 s.Printf(_T("%d;%d;%d;%d;%d;%d;%s;%d"),
492 0, // version
493 pointSize,
494 family,
495 (int)style,
496 (int)weight,
497 underlined,
498 faceName.GetData(),
499 (int)encoding);
500
501 return s;
502 }
503
504 void wxNativeFontInfo::Init()
505 {
506 pointSize = 0;
507 family = wxFONTFAMILY_DEFAULT;
508 style = wxFONTSTYLE_NORMAL;
509 weight = wxFONTWEIGHT_NORMAL;
510 underlined = false;
511 faceName.clear();
512 encoding = wxFONTENCODING_DEFAULT;
513 }
514
515 int wxNativeFontInfo::GetPointSize() const
516 {
517 return pointSize;
518 }
519
520 wxFontStyle wxNativeFontInfo::GetStyle() const
521 {
522 return style;
523 }
524
525 wxFontWeight wxNativeFontInfo::GetWeight() const
526 {
527 return weight;
528 }
529
530 bool wxNativeFontInfo::GetUnderlined() const
531 {
532 return underlined;
533 }
534
535 wxString wxNativeFontInfo::GetFaceName() const
536 {
537 return faceName;
538 }
539
540 wxFontFamily wxNativeFontInfo::GetFamily() const
541 {
542 return family;
543 }
544
545 wxFontEncoding wxNativeFontInfo::GetEncoding() const
546 {
547 return encoding;
548 }
549
550 void wxNativeFontInfo::SetPointSize(int pointsize)
551 {
552 pointSize = pointsize;
553 }
554
555 void wxNativeFontInfo::SetStyle(wxFontStyle style_)
556 {
557 style = style_;
558 }
559
560 void wxNativeFontInfo::SetWeight(wxFontWeight weight_)
561 {
562 weight = weight_;
563 }
564
565 void wxNativeFontInfo::SetUnderlined(bool underlined_)
566 {
567 underlined = underlined_;
568 }
569
570 bool wxNativeFontInfo::SetFaceName(const wxString& facename_)
571 {
572 faceName = facename_;
573 return true;
574 }
575
576 void wxNativeFontInfo::SetFamily(wxFontFamily family_)
577 {
578 family = family_;
579 }
580
581 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding_)
582 {
583 encoding = encoding_;
584 }
585
586 #endif // generic wxNativeFontInfo implementation
587
588 // conversion to/from user-readable string: this is used in the generic
589 // versions and under MSW as well because there is no standard font description
590 // format there anyhow (but there is a well-defined standard for X11 fonts used
591 // by wxGTK and wxMotif)
592
593 #if defined(wxNO_NATIVE_FONTINFO) || defined(__WXMSW__) || defined (__WXPM__)
594
595 wxString wxNativeFontInfo::ToUserString() const
596 {
597 wxString desc;
598
599 // first put the adjectives, if any - this is English-centric, of course,
600 // but what else can we do?
601 if ( GetUnderlined() )
602 {
603 desc << _("underlined");
604 }
605
606 switch ( GetWeight() )
607 {
608 default:
609 wxFAIL_MSG( _T("unknown font weight") );
610 // fall through
611
612 case wxFONTWEIGHT_NORMAL:
613 break;
614
615 case wxFONTWEIGHT_LIGHT:
616 desc << _(" light");
617 break;
618
619 case wxFONTWEIGHT_BOLD:
620 desc << _(" bold");
621 break;
622 }
623
624 switch ( GetStyle() )
625 {
626 default:
627 wxFAIL_MSG( _T("unknown font style") );
628 // fall through
629
630 case wxFONTSTYLE_NORMAL:
631 break;
632
633 // we don't distinguish between the two for now anyhow...
634 case wxFONTSTYLE_ITALIC:
635 case wxFONTSTYLE_SLANT:
636 desc << _(" italic");
637 break;
638 }
639
640 wxString face = GetFaceName();
641 if ( !face.empty() )
642 {
643 desc << _T(' ') << face;
644 }
645
646 int size = GetPointSize();
647 if ( size != wxNORMAL_FONT->GetPointSize() )
648 {
649 desc << _T(' ') << size;
650 }
651
652 #if wxUSE_FONTMAP
653 wxFontEncoding enc = GetEncoding();
654 if ( enc != wxFONTENCODING_DEFAULT && enc != wxFONTENCODING_SYSTEM )
655 {
656 desc << _T(' ') << wxFontMapper::GetEncodingName(enc);
657 }
658 #endif // wxUSE_FONTMAP
659
660 return desc.Strip(wxString::both).MakeLower();
661 }
662
663 bool wxNativeFontInfo::FromUserString(const wxString& s)
664 {
665 // reset to the default state
666 Init();
667
668 // parse a more or less free form string
669 //
670 // TODO: we should handle at least the quoted facenames
671 wxStringTokenizer tokenizer(s, _T(";, "), wxTOKEN_STRTOK);
672
673 wxString face;
674 unsigned long size;
675 bool weightfound = false, pointsizefound = false;
676 #if wxUSE_FONTMAP
677 bool encodingfound = false;
678 #endif
679
680 while ( tokenizer.HasMoreTokens() )
681 {
682 wxString token = tokenizer.GetNextToken();
683
684 // normalize it
685 token.Trim(true).Trim(false).MakeLower();
686
687 // look for the known tokens
688 if ( token == _T("underlined") || token == _("underlined") )
689 {
690 SetUnderlined(true);
691 }
692 else if ( token == _T("light") || token == _("light") )
693 {
694 SetWeight(wxFONTWEIGHT_LIGHT);
695 weightfound = true;
696 }
697 else if ( token == _T("bold") || token == _("bold") )
698 {
699 SetWeight(wxFONTWEIGHT_BOLD);
700 weightfound = true;
701 }
702 else if ( token == _T("italic") || token == _("italic") )
703 {
704 SetStyle(wxFONTSTYLE_ITALIC);
705 }
706 else if ( token.ToULong(&size) )
707 {
708 SetPointSize(size);
709 pointsizefound = true;
710 }
711 else
712 {
713 #if wxUSE_FONTMAP
714 // try to interpret this as an encoding
715 wxFontEncoding encoding = wxFontMapper::Get()->CharsetToEncoding(token, false);
716 if ( encoding != wxFONTENCODING_DEFAULT &&
717 encoding != wxFONTENCODING_SYSTEM ) // returned when the recognition failed
718 {
719 SetEncoding(encoding);
720 encodingfound = true;
721 }
722 else
723 {
724 #endif // wxUSE_FONTMAP
725
726 // assume it is the face name
727 if ( !face.empty() )
728 {
729 face += _T(' ');
730 }
731
732 face += token;
733
734 // skip the code which resets face below
735 continue;
736
737 #if wxUSE_FONTMAP
738 }
739 #endif // wxUSE_FONTMAP
740 }
741
742 // if we had had the facename, we shouldn't continue appending tokens
743 // to it (i.e. "foo bold bar" shouldn't result in the facename "foo
744 // bar")
745 if ( !face.empty() )
746 {
747 // NB: the check on the facename is implemented in wxFontBase::SetFaceName
748 // and not in wxNativeFontInfo::SetFaceName thus we need to explicitely
749 // call here wxFontEnumerator::IsValidFacename
750 if (
751 #if wxUSE_FONTENUM
752 !wxFontEnumerator::IsValidFacename(face) ||
753 #endif // wxUSE_FONTENUM
754 !SetFaceName(face) )
755 {
756 SetFaceName(wxNORMAL_FONT->GetFaceName());
757 }
758
759 face.clear();
760 }
761 }
762
763 // we might not have flushed it inside the loop
764 if ( !face.empty() )
765 {
766 // NB: the check on the facename is implemented in wxFontBase::SetFaceName
767 // and not in wxNativeFontInfo::SetFaceName thus we need to explicitely
768 // call here wxFontEnumerator::IsValidFacename
769 if (
770 #if wxUSE_FONTENUM
771 !wxFontEnumerator::IsValidFacename(face) ||
772 #endif // wxUSE_FONTENUM
773 !SetFaceName(face) )
774 {
775 SetFaceName(wxNORMAL_FONT->GetFaceName());
776 }
777 }
778
779 // set point size to default value if size was not given
780 if ( !pointsizefound )
781 SetPointSize(wxNORMAL_FONT->GetPointSize());
782
783 // set font weight to default value if weight was not given
784 if ( !weightfound )
785 SetWeight(wxFONTWEIGHT_NORMAL);
786
787 #if wxUSE_FONTMAP
788 // set font encoding to default value if encoding was not given
789 if ( !encodingfound )
790 SetEncoding(wxFONTENCODING_SYSTEM);
791 #endif // wxUSE_FONTMAP
792
793 return true;
794 }
795
796 #endif // generic or wxMSW or wxOS2
797
798
799 // wxFont <-> wxString utilities, used by wxConfig
800 wxString wxToString(const wxFontBase& font)
801 {
802 return font.IsOk() ? font.GetNativeFontInfoDesc()
803 : wxString();
804 }
805
806 bool wxFromString(const wxString& str, wxFontBase *font)
807 {
808 wxCHECK_MSG( font, false, _T("NULL output parameter") );
809
810 if ( str.empty() )
811 {
812 *font = wxNullFont;
813 return true;
814 }
815
816 return font->SetNativeFontInfo(str);
817 }
818
819