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