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