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