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