]> git.saurik.com Git - wxWidgets.git/blame - src/common/fontcmn.cpp
Handle hot spots in wxImage::Rotate90().
[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"
8d3ba63b 33 #include "wx/log.h"
dd05139a 34 #include "wx/gdicmn.h"
0c5d3e1c
VZ
35#endif // WX_PRECOMP
36
82ef81ed 37#if defined(__WXMSW__)
7bd236e6
WS
38 #include "wx/msw/private.h" // includes windows.h for LOGFONT
39 #include "wx/msw/winundef.h"
1c193821
JS
40#endif
41
76e23cdb 42#include "wx/fontutil.h" // for wxNativeFontInfo
ab5fe833 43#include "wx/fontmap.h"
85ab460e 44#include "wx/fontenum.h"
76e23cdb 45
30764ab5
VZ
46#include "wx/tokenzr.h"
47
4b6a582b
VZ
48// debugger helper: this function can be called from a debugger to show what
49// the date really is
50extern const char *wxDumpFont(const wxFont *font)
51{
52 static char buf[256];
53
54 const wxFontWeight weight = font->GetWeight();
55
56 wxString s;
57 s.Printf(wxS("%s-%s-%s-%d-%d"),
58 font->GetFaceName(),
59 weight == wxFONTWEIGHT_NORMAL
9a83f860 60 ? wxT("normal")
4b6a582b 61 : weight == wxFONTWEIGHT_BOLD
9a83f860
VZ
62 ? wxT("bold")
63 : wxT("light"),
4b6a582b 64 font->GetStyle() == wxFONTSTYLE_NORMAL
9a83f860
VZ
65 ? wxT("regular")
66 : wxT("italic"),
4b6a582b
VZ
67 font->GetPointSize(),
68 font->GetEncoding());
69
61f24aea 70 wxStrlcpy(buf, s.mb_str(), WXSIZEOF(buf));
4b6a582b
VZ
71 return buf;
72}
73
0c5d3e1c
VZ
74// ============================================================================
75// implementation
76// ============================================================================
77
544229d1
VZ
78// ----------------------------------------------------------------------------
79// helper functions
80// ----------------------------------------------------------------------------
81
b5791cc7 82static inline int flags2Style(int flags)
544229d1 83{
b5791cc7
FM
84 return flags & wxFONTFLAG_ITALIC
85 ? wxFONTSTYLE_ITALIC
86 : flags & wxFONTFLAG_SLANT
87 ? wxFONTSTYLE_SLANT
88 : wxFONTSTYLE_NORMAL;
89}
544229d1 90
b5791cc7
FM
91static inline int flags2Weight(int flags)
92{
93 return flags & wxFONTFLAG_LIGHT
94 ? wxFONTWEIGHT_LIGHT
95 : flags & wxFONTFLAG_BOLD
96 ? wxFONTWEIGHT_BOLD
97 : wxFONTWEIGHT_NORMAL;
98}
544229d1 99
b5791cc7
FM
100static inline bool flags2Underlined(int flags)
101{
102 return (flags & wxFONTFLAG_UNDERLINED) != 0;
544229d1
VZ
103}
104
0c5d3e1c
VZ
105// ----------------------------------------------------------------------------
106// wxFontBase
107// ----------------------------------------------------------------------------
108
109wxFontEncoding wxFontBase::ms_encodingDefault = wxFONTENCODING_SYSTEM;
110
cafbf6fb
VZ
111/* static */
112void wxFontBase::SetDefaultEncoding(wxFontEncoding encoding)
113{
114 // GetDefaultEncoding() should return something != wxFONTENCODING_DEFAULT
115 // and, besides, using this value here doesn't make any sense
116 wxCHECK_RET( encoding != wxFONTENCODING_DEFAULT,
9a83f860 117 wxT("can't set default encoding to wxFONTENCODING_DEFAULT") );
cafbf6fb
VZ
118
119 ms_encodingDefault = encoding;
120}
121
799ea011
GD
122wxFontBase::~wxFontBase()
123{
124 // this destructor is required for Darwin
125}
126
7beba2fc 127/* static */
0c5d3e1c 128wxFont *wxFontBase::New(int size,
0c14b6c3
FM
129 wxFontFamily family,
130 wxFontStyle style,
131 wxFontWeight weight,
0c5d3e1c
VZ
132 bool underlined,
133 const wxString& face,
134 wxFontEncoding encoding)
135{
136 return new wxFont(size, family, style, weight, underlined, face, encoding);
137}
138
b5791cc7
FM
139/* static */
140wxFont *wxFontBase::New(const wxSize& pixelSize,
141 wxFontFamily family,
142 wxFontStyle style,
143 wxFontWeight weight,
144 bool underlined,
145 const wxString& face,
146 wxFontEncoding encoding)
544229d1 147{
b5791cc7
FM
148 return new wxFont(pixelSize, family, style, weight, underlined,
149 face, encoding);
544229d1
VZ
150}
151
152/* static */
153wxFont *wxFontBase::New(int pointSize,
154 wxFontFamily family,
155 int flags,
156 const wxString& face,
157 wxFontEncoding encoding)
158{
159 return New(pointSize, family, flags2Style(flags), flags2Weight(flags),
160 flags2Underlined(flags), face, encoding);
161}
162
163/* static */
164wxFont *wxFontBase::New(const wxSize& pixelSize,
0c14b6c3 165 wxFontFamily family,
b5791cc7 166 int flags,
544229d1
VZ
167 const wxString& face,
168 wxFontEncoding encoding)
169{
b5791cc7
FM
170 return New(pixelSize, family, flags2Style(flags), flags2Weight(flags),
171 flags2Underlined(flags), face, encoding);
544229d1
VZ
172}
173
174/* static */
b5791cc7 175wxFont *wxFontBase::New(const wxNativeFontInfo& info)
544229d1 176{
b5791cc7
FM
177 return new wxFont(info);
178}
179
180/* static */
181wxFont *wxFontBase::New(const wxString& strNativeFontDesc)
182{
183 wxNativeFontInfo fontInfo;
184 if ( !fontInfo.FromString(strNativeFontDesc) )
185 return new wxFont(*wxNORMAL_FONT);
186
187 return New(fontInfo);
188}
189
190bool wxFontBase::IsFixedWidth() const
191{
192 return GetFamily() == wxFONTFAMILY_TELETYPE;
544229d1
VZ
193}
194
195wxSize wxFontBase::GetPixelSize() const
196{
197 wxScreenDC dc;
198 dc.SetFont(*(wxFont *)this);
199 return wxSize(dc.GetCharWidth(), dc.GetCharHeight());
200}
201
202bool wxFontBase::IsUsingSizeInPixels() const
203{
204 return false;
205}
206
207void wxFontBase::SetPixelSize( const wxSize& pixelSize )
208{
b5791cc7
FM
209 wxCHECK_RET( pixelSize.GetWidth() >= 0 && pixelSize.GetHeight() > 0,
210 "Negative values for the pixel size or zero pixel height are not allowed" );
211
544229d1 212 wxScreenDC dc;
01cb1c26 213
b5791cc7
FM
214 // NOTE: this algorithm for adjusting the font size is used by all
215 // implementations of wxFont except under wxMSW and wxGTK where
216 // native support to font creation using pixel-size is provided.
03647350 217
b5791cc7
FM
218 int largestGood = 0;
219 int smallestBad = 0;
30764ab5 220
b5791cc7
FM
221 bool initialGoodFound = false;
222 bool initialBadFound = false;
7826e2dd 223
b5791cc7
FM
224 // NB: this assignment was separated from the variable definition
225 // in order to fix a gcc v3.3.3 compiler crash
226 int currentSize = GetPointSize();
227 while (currentSize > 0)
228 {
229 dc.SetFont(*static_cast<wxFont*>(this));
7826e2dd 230
b5791cc7
FM
231 // if currentSize (in points) results in a font that is smaller
232 // than required by pixelSize it is considered a good size
233 // NOTE: the pixel size width may be zero
234 if (dc.GetCharHeight() <= pixelSize.GetHeight() &&
235 (pixelSize.GetWidth() == 0 ||
236 dc.GetCharWidth() <= pixelSize.GetWidth()))
237 {
238 largestGood = currentSize;
239 initialGoodFound = true;
240 }
241 else
242 {
243 smallestBad = currentSize;
244 initialBadFound = true;
245 }
246 if (!initialGoodFound)
247 {
248 currentSize /= 2;
249 }
250 else if (!initialBadFound)
251 {
252 currentSize *= 2;
253 }
254 else
255 {
256 int distance = smallestBad - largestGood;
257 if (distance == 1)
258 break;
259
260 currentSize = largestGood + distance / 2;
261 }
262
263 SetPointSize(currentSize);
264 }
265
266 if (currentSize != largestGood)
267 SetPointSize(largestGood);
53f6aab7
VZ
268}
269
9045ad9d 270void wxFontBase::DoSetNativeFontInfo(const wxNativeFontInfo& info)
30764ab5 271{
ab5fe833 272#ifdef wxNO_NATIVE_FONTINFO
30764ab5
VZ
273 SetPointSize(info.pointSize);
274 SetFamily(info.family);
275 SetStyle(info.style);
276 SetWeight(info.weight);
277 SetUnderlined(info.underlined);
278 SetFaceName(info.faceName);
279 SetEncoding(info.encoding);
33ac7e6f 280#else
1e6feb95 281 (void)info;
30764ab5
VZ
282#endif
283}
284
7826e2dd
VZ
285wxString wxFontBase::GetNativeFontInfoDesc() const
286{
64932e41
VZ
287 wxCHECK_MSG( IsOk(), wxEmptyString, wxT("invalid font") );
288
7826e2dd 289 wxString fontDesc;
3bf5a59b 290 const wxNativeFontInfo *fontInfo = GetNativeFontInfo();
7826e2dd
VZ
291 if ( fontInfo )
292 {
293 fontDesc = fontInfo->ToString();
dd05139a 294 wxASSERT_MSG(!fontDesc.empty(), wxT("This should be a non-empty string!"));
85ab460e
VZ
295 }
296 else
297 {
7bd236e6 298 wxFAIL_MSG(wxT("Derived class should have created the wxNativeFontInfo!"));
7826e2dd
VZ
299 }
300
301 return fontDesc;
302}
303
ab5fe833
VZ
304wxString wxFontBase::GetNativeFontInfoUserDesc() const
305{
64932e41
VZ
306 wxCHECK_MSG( IsOk(), wxEmptyString, wxT("invalid font") );
307
ab5fe833 308 wxString fontDesc;
3bf5a59b 309 const wxNativeFontInfo *fontInfo = GetNativeFontInfo();
ab5fe833
VZ
310 if ( fontInfo )
311 {
312 fontDesc = fontInfo->ToUserString();
dd05139a 313 wxASSERT_MSG(!fontDesc.empty(), wxT("This should be a non-empty string!"));
85ab460e
VZ
314 }
315 else
316 {
7456f382 317 wxFAIL_MSG(wxT("Derived class should have created the wxNativeFontInfo!"));
ab5fe833
VZ
318 }
319
320 return fontDesc;
321}
322
85ab460e 323bool wxFontBase::SetNativeFontInfo(const wxString& info)
31d1b66e
VZ
324{
325 wxNativeFontInfo fontInfo;
326 if ( !info.empty() && fontInfo.FromString(info) )
327 {
328 SetNativeFontInfo(fontInfo);
85ab460e 329 return true;
31d1b66e 330 }
85ab460e 331
85ab460e 332 return false;
31d1b66e
VZ
333}
334
85ab460e 335bool wxFontBase::SetNativeFontInfoUserDesc(const wxString& info)
ab5fe833
VZ
336{
337 wxNativeFontInfo fontInfo;
338 if ( !info.empty() && fontInfo.FromUserString(info) )
339 {
340 SetNativeFontInfo(fontInfo);
85ab460e 341 return true;
ab5fe833 342 }
85ab460e 343
85ab460e 344 return false;
ab5fe833
VZ
345}
346
0c5d3e1c
VZ
347bool wxFontBase::operator==(const wxFont& font) const
348{
8bf30fe9
VZ
349 // either it is the same font, i.e. they share the same common data or they
350 // have different ref datas but still describe the same font
a38cd629 351 return IsSameAs(font) ||
8bf30fe9 352 (
70f70818 353 IsOk() == font.IsOk() &&
8bf30fe9 354 GetPointSize() == font.GetPointSize() &&
82d0e7fe
VZ
355 // in wxGTK1 GetPixelSize() calls GetInternalFont() which uses
356 // operator==() resulting in infinite recursion so we can't use it
357 // in that port
358#if !defined(__WXGTK__) || defined(__WXGTK20__)
cc3de8a3 359 GetPixelSize() == font.GetPixelSize() &&
82d0e7fe 360#endif
8bf30fe9
VZ
361 GetFamily() == font.GetFamily() &&
362 GetStyle() == font.GetStyle() &&
e6adf058 363 GetWeight() == font.GetWeight() &&
8bf30fe9 364 GetUnderlined() == font.GetUnderlined() &&
85ab460e 365 GetFaceName().IsSameAs(font.GetFaceName(), false) &&
8bf30fe9
VZ
366 GetEncoding() == font.GetEncoding()
367 );
0c5d3e1c
VZ
368}
369
59b7da02
VZ
370wxFontFamily wxFontBase::GetFamily() const
371{
372 wxCHECK_MSG( IsOk(), wxFONTFAMILY_UNKNOWN, wxS("invalid font") );
373
374 // Don't return wxFONTFAMILY_UNKNOWN from here because it prevents the code
375 // like wxFont(size, wxNORMAL_FONT->GetFamily(), ...) from working (see
376 // #12330). This is really just a hack but it allows to keep compatibility
377 // and doesn't really have any bad drawbacks so do this until someone comes
378 // up with a better idea.
379 const wxFontFamily family = DoGetFamily();
380
381 return family == wxFONTFAMILY_UNKNOWN ? wxFONTFAMILY_DEFAULT : family;
382}
383
0c5d3e1c
VZ
384wxString wxFontBase::GetFamilyString() const
385{
70f70818 386 wxCHECK_MSG( IsOk(), "wxFONTFAMILY_DEFAULT", "invalid font" );
0c5d3e1c
VZ
387
388 switch ( GetFamily() )
389 {
70f70818
FM
390 case wxFONTFAMILY_DECORATIVE: return "wxFONTFAMILY_DECORATIVE";
391 case wxFONTFAMILY_ROMAN: return "wxFONTFAMILY_ROMAN";
392 case wxFONTFAMILY_SCRIPT: return "wxFONTFAMILY_SCRIPT";
393 case wxFONTFAMILY_SWISS: return "wxFONTFAMILY_SWISS";
394 case wxFONTFAMILY_MODERN: return "wxFONTFAMILY_MODERN";
395 case wxFONTFAMILY_TELETYPE: return "wxFONTFAMILY_TELETYPE";
f2c1b903 396 case wxFONTFAMILY_UNKNOWN: return "wxFONTFAMILY_UNKNOWN";
70f70818 397 default: return "wxFONTFAMILY_DEFAULT";
0c5d3e1c
VZ
398 }
399}
400
401wxString wxFontBase::GetStyleString() const
402{
70f70818 403 wxCHECK_MSG( IsOk(), "wxFONTSTYLE_DEFAULT", "invalid font" );
0c5d3e1c
VZ
404
405 switch ( GetStyle() )
406 {
70f70818
FM
407 case wxFONTSTYLE_NORMAL: return "wxFONTSTYLE_NORMAL";
408 case wxFONTSTYLE_SLANT: return "wxFONTSTYLE_SLANT";
409 case wxFONTSTYLE_ITALIC: return "wxFONTSTYLE_ITALIC";
410 default: return "wxFONTSTYLE_DEFAULT";
0c5d3e1c
VZ
411 }
412}
413
414wxString wxFontBase::GetWeightString() const
415{
70f70818 416 wxCHECK_MSG( IsOk(), "wxFONTWEIGHT_DEFAULT", "invalid font" );
0c5d3e1c
VZ
417
418 switch ( GetWeight() )
419 {
70f70818
FM
420 case wxFONTWEIGHT_NORMAL: return "wxFONTWEIGHT_NORMAL";
421 case wxFONTWEIGHT_BOLD: return "wxFONTWEIGHT_BOLD";
422 case wxFONTWEIGHT_LIGHT: return "wxFONTWEIGHT_LIGHT";
423 default: return "wxFONTWEIGHT_DEFAULT";
0c5d3e1c
VZ
424 }
425}
426
ff427585 427bool wxFontBase::SetFaceName(const wxString& facename)
85ab460e 428{
ff427585 429#if wxUSE_FONTENUM
85ab460e
VZ
430 if (!wxFontEnumerator::IsValidFacename(facename))
431 {
70f70818 432 UnRef(); // make IsOk() return false
85ab460e
VZ
433 return false;
434 }
ff427585
VZ
435#else // !wxUSE_FONTENUM
436 wxUnusedVar(facename);
437#endif // wxUSE_FONTENUM/!wxUSE_FONTENUM
85ab460e
VZ
438
439 return true;
440}
441
6e7d2550
VZ
442wxFont& wxFont::MakeBold()
443{
444 SetWeight(wxFONTWEIGHT_BOLD);
445 return *this;
446}
447
448wxFont wxFont::Bold() const
f76c0758
VZ
449{
450 wxFont font(*this);
6e7d2550 451 font.MakeBold();
f76c0758
VZ
452 return font;
453}
454
6e7d2550
VZ
455wxFont& wxFont::MakeItalic()
456{
457 SetStyle(wxFONTSTYLE_ITALIC);
458 return *this;
459}
460
461wxFont wxFont::Italic() const
f76c0758
VZ
462{
463 wxFont font(*this);
464 font.SetStyle(wxFONTSTYLE_ITALIC);
465 return font;
466}
467
6e7d2550
VZ
468wxFont& wxFont::Scale(float x)
469{
470 SetPointSize(int(x*GetPointSize() + 0.5));
471 return *this;
472}
473
474wxFont wxFont::Scaled(float x) const
f76c0758
VZ
475{
476 wxFont font(*this);
6e7d2550 477 font.Scale(x);
f76c0758
VZ
478 return font;
479}
85ab460e 480
30764ab5
VZ
481// ----------------------------------------------------------------------------
482// wxNativeFontInfo
483// ----------------------------------------------------------------------------
484
85ab460e 485// Up to now, there are no native implementations of this function:
ff427585 486void wxNativeFontInfo::SetFaceName(const wxArrayString& facenames)
85ab460e 487{
ff427585 488#if wxUSE_FONTENUM
85ab460e
VZ
489 for (size_t i=0; i < facenames.GetCount(); i++)
490 {
491 if (wxFontEnumerator::IsValidFacename(facenames[i]))
492 {
493 SetFaceName(facenames[i]);
494 return;
495 }
496 }
497
498 // set the first valid facename we can find on this system
499 wxString validfacename = wxFontEnumerator::GetFacenames().Item(0);
500 wxLogTrace(wxT("font"), wxT("Falling back to '%s'"), validfacename.c_str());
501 SetFaceName(validfacename);
ff427585
VZ
502#else // !wxUSE_FONTENUM
503 SetFaceName(facenames[0]);
504#endif // wxUSE_FONTENUM/!wxUSE_FONTENUM
85ab460e
VZ
505}
506
507
ab5fe833
VZ
508#ifdef wxNO_NATIVE_FONTINFO
509
30764ab5
VZ
510// These are the generic forms of FromString()/ToString.
511//
512// convert to/from the string representation: format is
09fcd889 513// version;pointsize;family;style;weight;underlined;facename;encoding
30764ab5
VZ
514
515bool wxNativeFontInfo::FromString(const wxString& s)
516{
517 long l;
518
9a83f860 519 wxStringTokenizer tokenizer(s, wxT(";"));
30764ab5
VZ
520
521 wxString token = tokenizer.GetNextToken();
09fcd889
VZ
522 //
523 // Ignore the version for now
524 //
33ac7e6f 525
09fcd889 526 token = tokenizer.GetNextToken();
30764ab5 527 if ( !token.ToLong(&l) )
a62848fd 528 return false;
30764ab5
VZ
529 pointSize = (int)l;
530
531 token = tokenizer.GetNextToken();
532 if ( !token.ToLong(&l) )
a62848fd 533 return false;
f7b301fa 534 family = (wxFontFamily)l;
30764ab5
VZ
535
536 token = tokenizer.GetNextToken();
537 if ( !token.ToLong(&l) )
a62848fd 538 return false;
ab5fe833 539 style = (wxFontStyle)l;
30764ab5
VZ
540
541 token = tokenizer.GetNextToken();
542 if ( !token.ToLong(&l) )
a62848fd 543 return false;
ab5fe833 544 weight = (wxFontWeight)l;
30764ab5
VZ
545
546 token = tokenizer.GetNextToken();
547 if ( !token.ToLong(&l) )
a62848fd 548 return false;
189e08b4 549 underlined = l != 0;
30764ab5
VZ
550
551 faceName = tokenizer.GetNextToken();
0a9f0ef7
JS
552
553#ifndef __WXMAC__
30764ab5 554 if( !faceName )
a62848fd 555 return false;
0a9f0ef7 556#endif
30764ab5
VZ
557
558 token = tokenizer.GetNextToken();
559 if ( !token.ToLong(&l) )
a62848fd 560 return false;
30764ab5
VZ
561 encoding = (wxFontEncoding)l;
562
a62848fd 563 return true;
30764ab5
VZ
564}
565
566wxString wxNativeFontInfo::ToString() const
567{
568 wxString s;
569
9a83f860 570 s.Printf(wxT("%d;%d;%d;%d;%d;%d;%s;%d"),
09fcd889 571 0, // version
30764ab5
VZ
572 pointSize,
573 family,
ab5fe833
VZ
574 (int)style,
575 (int)weight,
30764ab5
VZ
576 underlined,
577 faceName.GetData(),
578 (int)encoding);
579
580 return s;
581}
582
ab5fe833
VZ
583void wxNativeFontInfo::Init()
584{
3bf5a59b 585 pointSize = 0;
ab5fe833
VZ
586 family = wxFONTFAMILY_DEFAULT;
587 style = wxFONTSTYLE_NORMAL;
588 weight = wxFONTWEIGHT_NORMAL;
a62848fd 589 underlined = false;
ab5fe833
VZ
590 faceName.clear();
591 encoding = wxFONTENCODING_DEFAULT;
592}
593
594int wxNativeFontInfo::GetPointSize() const
595{
596 return pointSize;
597}
598
599wxFontStyle wxNativeFontInfo::GetStyle() const
600{
601 return style;
602}
603
604wxFontWeight wxNativeFontInfo::GetWeight() const
605{
606 return weight;
607}
608
609bool wxNativeFontInfo::GetUnderlined() const
610{
611 return underlined;
612}
613
614wxString wxNativeFontInfo::GetFaceName() const
615{
616 return faceName;
617}
618
7936354d
VZ
619wxFontFamily wxNativeFontInfo::GetFamily() const
620{
621 return family;
622}
623
ab5fe833
VZ
624wxFontEncoding wxNativeFontInfo::GetEncoding() const
625{
626 return encoding;
627}
628
629void wxNativeFontInfo::SetPointSize(int pointsize)
630{
631 pointSize = pointsize;
632}
633
634void wxNativeFontInfo::SetStyle(wxFontStyle style_)
635{
636 style = style_;
637}
638
639void wxNativeFontInfo::SetWeight(wxFontWeight weight_)
640{
641 weight = weight_;
642}
643
644void wxNativeFontInfo::SetUnderlined(bool underlined_)
645{
646 underlined = underlined_;
647}
648
85ab460e 649bool wxNativeFontInfo::SetFaceName(const wxString& facename_)
ab5fe833 650{
f7b301fa 651 faceName = facename_;
85ab460e 652 return true;
ab5fe833
VZ
653}
654
3f1d1373 655void wxNativeFontInfo::SetFamily(wxFontFamily family_)
7936354d
VZ
656{
657 family = family_;
658}
659
ab5fe833
VZ
660void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding_)
661{
662 encoding = encoding_;
663}
664
7826e2dd 665#endif // generic wxNativeFontInfo implementation
30764ab5 666
ab5fe833
VZ
667// conversion to/from user-readable string: this is used in the generic
668// versions and under MSW as well because there is no standard font description
669// format there anyhow (but there is a well-defined standard for X11 fonts used
670// by wxGTK and wxMotif)
671
f1c40652 672#if defined(wxNO_NATIVE_FONTINFO) || defined(__WXMSW__) || defined (__WXPM__) || defined(__WXOSX__)
ab5fe833
VZ
673
674wxString wxNativeFontInfo::ToUserString() const
675{
676 wxString desc;
677
678 // first put the adjectives, if any - this is English-centric, of course,
679 // but what else can we do?
680 if ( GetUnderlined() )
681 {
85ab460e 682 desc << _("underlined");
ab5fe833
VZ
683 }
684
685 switch ( GetWeight() )
686 {
687 default:
9a83f860 688 wxFAIL_MSG( wxT("unknown font weight") );
ab5fe833
VZ
689 // fall through
690
691 case wxFONTWEIGHT_NORMAL:
692 break;
693
694 case wxFONTWEIGHT_LIGHT:
85ab460e 695 desc << _(" light");
ab5fe833
VZ
696 break;
697
698 case wxFONTWEIGHT_BOLD:
85ab460e 699 desc << _(" bold");
ab5fe833
VZ
700 break;
701 }
702
703 switch ( GetStyle() )
704 {
705 default:
9a83f860 706 wxFAIL_MSG( wxT("unknown font style") );
ab5fe833
VZ
707 // fall through
708
709 case wxFONTSTYLE_NORMAL:
710 break;
711
712 // we don't distinguish between the two for now anyhow...
713 case wxFONTSTYLE_ITALIC:
714 case wxFONTSTYLE_SLANT:
85ab460e 715 desc << _(" italic");
ab5fe833
VZ
716 break;
717 }
718
a9249b2e
VZ
719 wxString face = GetFaceName();
720 if ( !face.empty() )
ab5fe833 721 {
c1ab2be0
FM
722 if (face.Contains(' ') || face.Contains(';') || face.Contains(','))
723 {
724 face.Replace("'", "");
725 // eventually remove quote characters: most systems do not
726 // allow them in a facename anyway so this usually does nothing
727
728 // make it possible for FromUserString() function to understand
729 // that the different words which compose this facename are
730 // not different adjectives or other data but rather all parts
731 // of the facename
9a83f860 732 desc << wxT(" '") << face << _("'");
c1ab2be0
FM
733 }
734 else
9a83f860 735 desc << wxT(' ') << face;
ab5fe833 736 }
1a2ca1d6
VZ
737 else // no face name specified
738 {
739 // use the family
740 wxString familyStr;
741 switch ( GetFamily() )
742 {
743 case wxFONTFAMILY_DECORATIVE:
744 familyStr = "decorative";
745 break;
746
747 case wxFONTFAMILY_ROMAN:
748 familyStr = "roman";
749 break;
750
751 case wxFONTFAMILY_SCRIPT:
752 familyStr = "script";
753 break;
754
755 case wxFONTFAMILY_SWISS:
756 familyStr = "swiss";
757 break;
758
759 case wxFONTFAMILY_MODERN:
760 familyStr = "modern";
761 break;
762
763 case wxFONTFAMILY_TELETYPE:
764 familyStr = "teletype";
765 break;
766
767 case wxFONTFAMILY_DEFAULT:
768 case wxFONTFAMILY_UNKNOWN:
769 break;
770
771 default:
772 wxFAIL_MSG( "unknown font family" );
773 }
774
775 if ( !familyStr.empty() )
776 desc << " '" << familyStr << " family'";
777 }
ab5fe833 778
a9249b2e
VZ
779 int size = GetPointSize();
780 if ( size != wxNORMAL_FONT->GetPointSize() )
ab5fe833 781 {
9a83f860 782 desc << wxT(' ') << size;
ab5fe833 783 }
a9249b2e 784
e7e52b6d 785#if wxUSE_FONTMAP
a9249b2e
VZ
786 wxFontEncoding enc = GetEncoding();
787 if ( enc != wxFONTENCODING_DEFAULT && enc != wxFONTENCODING_SYSTEM )
788 {
9a83f860 789 desc << wxT(' ') << wxFontMapper::GetEncodingName(enc);
a9249b2e 790 }
e7e52b6d 791#endif // wxUSE_FONTMAP
a9249b2e 792
85ab460e 793 return desc.Strip(wxString::both).MakeLower();
ab5fe833
VZ
794}
795
796bool wxNativeFontInfo::FromUserString(const wxString& s)
797{
798 // reset to the default state
799 Init();
800
c1ab2be0
FM
801 // ToUserString() will quote the facename if it contains spaces, commas
802 // or semicolons: we must be able to understand that quoted text is
803 // a single token:
804 wxString toparse(s);
c1ab2be0 805
ab5fe833 806 // parse a more or less free form string
9a83f860 807 wxStringTokenizer tokenizer(toparse, wxT(";, "), wxTOKEN_STRTOK);
ab5fe833
VZ
808
809 wxString face;
810 unsigned long size;
77f15ffe
VS
811 bool weightfound = false, pointsizefound = false;
812#if wxUSE_FONTMAP
813 bool encodingfound = false;
814#endif
c1ab2be0 815 bool insideQuotes = false;
ab5fe833
VZ
816
817 while ( tokenizer.HasMoreTokens() )
818 {
819 wxString token = tokenizer.GetNextToken();
820
821 // normalize it
a62848fd 822 token.Trim(true).Trim(false).MakeLower();
c1ab2be0
FM
823 if (insideQuotes)
824 {
03647350 825 if (token.StartsWith("'") ||
c1ab2be0
FM
826 token.EndsWith("'"))
827 {
828 insideQuotes = false;
829
830 // add this last token to the facename:
831 face += " " + token;
832
833 // normalize facename:
834 face = face.Trim(true).Trim(false);
835 face.Replace("'", "");
836
837 continue;
838 }
839 }
840 else
841 {
842 if (token.StartsWith("'"))
843 insideQuotes = true;
844 }
ab5fe833
VZ
845
846 // look for the known tokens
c1ab2be0
FM
847 if ( insideQuotes )
848 {
849 // only the facename may be quoted:
850 face += " " + token;
851 continue;
852 }
9a83f860 853 if ( token == wxT("underlined") || token == _("underlined") )
ab5fe833 854 {
a62848fd 855 SetUnderlined(true);
ab5fe833 856 }
9a83f860 857 else if ( token == wxT("light") || token == _("light") )
ab5fe833
VZ
858 {
859 SetWeight(wxFONTWEIGHT_LIGHT);
85ab460e 860 weightfound = true;
ab5fe833 861 }
9a83f860 862 else if ( token == wxT("bold") || token == _("bold") )
ab5fe833
VZ
863 {
864 SetWeight(wxFONTWEIGHT_BOLD);
85ab460e 865 weightfound = true;
ab5fe833 866 }
9a83f860 867 else if ( token == wxT("italic") || token == _("italic") )
ab5fe833
VZ
868 {
869 SetStyle(wxFONTSTYLE_ITALIC);
870 }
871 else if ( token.ToULong(&size) )
872 {
a9249b2e 873 SetPointSize(size);
85ab460e 874 pointsizefound = true;
ab5fe833 875 }
85ab460e
VZ
876 else
877 {
e7e52b6d 878#if wxUSE_FONTMAP
85ab460e
VZ
879 // try to interpret this as an encoding
880 wxFontEncoding encoding = wxFontMapper::Get()->CharsetToEncoding(token, false);
881 if ( encoding != wxFONTENCODING_DEFAULT &&
882 encoding != wxFONTENCODING_SYSTEM ) // returned when the recognition failed
ab5fe833
VZ
883 {
884 SetEncoding(encoding);
85ab460e 885 encodingfound = true;
ab5fe833 886 }
85ab460e 887 else
ab5fe833 888 {
85ab460e
VZ
889#endif // wxUSE_FONTMAP
890
891 // assume it is the face name
ab5fe833
VZ
892 if ( !face.empty() )
893 {
9a83f860 894 face += wxT(' ');
ab5fe833
VZ
895 }
896
897 face += token;
898
899 // skip the code which resets face below
900 continue;
85ab460e
VZ
901
902#if wxUSE_FONTMAP
903 }
904#endif // wxUSE_FONTMAP
ab5fe833
VZ
905 }
906
907 // if we had had the facename, we shouldn't continue appending tokens
908 // to it (i.e. "foo bold bar" shouldn't result in the facename "foo
909 // bar")
910 if ( !face.empty() )
911 {
1a2ca1d6
VZ
912 wxString familyStr;
913 if ( face.EndsWith(" family", &familyStr) )
914 {
915 // it's not a facename but rather a font family
916 wxFontFamily family;
917 if ( familyStr == "decorative" )
918 family = wxFONTFAMILY_DECORATIVE;
919 else if ( familyStr == "roman" )
920 family = wxFONTFAMILY_ROMAN;
921 else if ( familyStr == "script" )
922 family = wxFONTFAMILY_SCRIPT;
923 else if ( familyStr == "swiss" )
924 family = wxFONTFAMILY_SWISS;
925 else if ( familyStr == "modern" )
926 family = wxFONTFAMILY_MODERN;
927 else if ( familyStr == "teletype" )
928 family = wxFONTFAMILY_TELETYPE;
929 else
930 return false;
03647350 931
1a2ca1d6
VZ
932 SetFamily(family);
933 }
85ab460e
VZ
934 // NB: the check on the facename is implemented in wxFontBase::SetFaceName
935 // and not in wxNativeFontInfo::SetFaceName thus we need to explicitely
936 // call here wxFontEnumerator::IsValidFacename
1a2ca1d6 937 else if (
ff427585
VZ
938#if wxUSE_FONTENUM
939 !wxFontEnumerator::IsValidFacename(face) ||
940#endif // wxUSE_FONTENUM
941 !SetFaceName(face) )
942 {
85ab460e 943 SetFaceName(wxNORMAL_FONT->GetFaceName());
ff427585
VZ
944 }
945
ab5fe833
VZ
946 face.clear();
947 }
948 }
949
950 // we might not have flushed it inside the loop
951 if ( !face.empty() )
952 {
85ab460e
VZ
953 // NB: the check on the facename is implemented in wxFontBase::SetFaceName
954 // and not in wxNativeFontInfo::SetFaceName thus we need to explicitely
955 // call here wxFontEnumerator::IsValidFacename
ff427585
VZ
956 if (
957#if wxUSE_FONTENUM
958 !wxFontEnumerator::IsValidFacename(face) ||
959#endif // wxUSE_FONTENUM
960 !SetFaceName(face) )
961 {
962 SetFaceName(wxNORMAL_FONT->GetFaceName());
963 }
ab5fe833
VZ
964 }
965
85ab460e
VZ
966 // set point size to default value if size was not given
967 if ( !pointsizefound )
968 SetPointSize(wxNORMAL_FONT->GetPointSize());
969
970 // set font weight to default value if weight was not given
971 if ( !weightfound )
972 SetWeight(wxFONTWEIGHT_NORMAL);
973
974#if wxUSE_FONTMAP
975 // set font encoding to default value if encoding was not given
976 if ( !encodingfound )
977 SetEncoding(wxFONTENCODING_SYSTEM);
978#endif // wxUSE_FONTMAP
979
a62848fd 980 return true;
ab5fe833
VZ
981}
982
0eb529d9 983#endif // generic or wxMSW or wxOS2
fc9361e3
VZ
984
985
986// wxFont <-> wxString utilities, used by wxConfig
987wxString wxToString(const wxFontBase& font)
988{
989 return font.IsOk() ? font.GetNativeFontInfoDesc()
990 : wxString();
991}
992
993bool wxFromString(const wxString& str, wxFontBase *font)
994{
9a83f860 995 wxCHECK_MSG( font, false, wxT("NULL output parameter") );
fc9361e3
VZ
996
997 if ( str.empty() )
998 {
999 *font = wxNullFont;
1000 return true;
1001 }
1002
1003 return font->SetNativeFontInfo(str);
1004}
1005
1006