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