applied wxNativeFontInfo patch #103089
[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 license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
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 #endif // WX_PRECOMP
34
35 #include "wx/gdicmn.h"
36 #include "wx/fontutil.h" // for wxNativeFontInfo
37
38 #include "wx/tokenzr.h"
39
40 // ============================================================================
41 // implementation
42 // ============================================================================
43
44 // ----------------------------------------------------------------------------
45 // wxFontBase
46 // ----------------------------------------------------------------------------
47
48 wxFontEncoding wxFontBase::ms_encodingDefault = wxFONTENCODING_SYSTEM;
49
50 /* static */
51 wxFont *wxFontBase::New(int size,
52 int family,
53 int style,
54 int weight,
55 bool underlined,
56 const wxString& face,
57 wxFontEncoding encoding)
58 {
59 return new wxFont(size, family, style, weight, underlined, face, encoding);
60 }
61
62 /* static */
63 wxFont *wxFontBase::New(const wxNativeFontInfo& info)
64 {
65 return new wxFont(info);
66 }
67
68 /* static */
69 wxFont *wxFontBase::New(const wxString& strNativeFontDesc)
70 {
71 wxNativeFontInfo fontInfo;
72 if ( !fontInfo.FromString(strNativeFontDesc) )
73 return new wxFont(*wxNORMAL_FONT);
74
75 return New(fontInfo);
76 }
77
78 wxNativeFontInfo *wxFontBase::GetNativeFontInfo() const
79 {
80 #if !defined(__WXGTK__) && !defined(__WXMSW__)
81 wxNativeFontInfo *fontInfo = new wxNativeFontInfo;
82
83 fontInfo->pointSize = GetPointSize();
84 fontInfo->family = GetFamily();
85 fontInfo->style = GetStyle();
86 fontInfo->weight = GetWeight();
87 fontInfo->underlined = GetUnderlined();
88 fontInfo->faceName = GetFaceName();
89 fontInfo->encoding = GetEncoding();
90
91 return fontInfo;
92 #else
93 return (wxNativeFontInfo *)NULL;
94 #endif
95 }
96
97 void wxFontBase::SetNativeFontInfo(const wxNativeFontInfo& info)
98 {
99 #if !defined(__WXGTK__) && !defined(__WXMSW__)
100 SetPointSize(info.pointSize);
101 SetFamily(info.family);
102 SetStyle(info.style);
103 SetWeight(info.weight);
104 SetUnderlined(info.underlined);
105 SetFaceName(info.faceName);
106 SetEncoding(info.encoding);
107 #endif
108 }
109
110 wxString wxFontBase::GetNativeFontInfoDesc() const
111 {
112 wxString fontDesc;
113 wxNativeFontInfo *fontInfo = GetNativeFontInfo();
114 if ( fontInfo )
115 {
116 fontDesc = fontInfo->ToString();
117 delete fontInfo;
118 }
119
120 return fontDesc;
121 }
122
123 wxFont& wxFont::operator=(const wxFont& font)
124 {
125 if ( this != &font )
126 Ref(font);
127
128 return (wxFont &)*this;
129 }
130
131 // VZ: is it correct to compare pointers and not the contents? (FIXME)
132 bool wxFontBase::operator==(const wxFont& font) const
133 {
134 return GetFontData() == font.GetFontData();
135 }
136
137 bool wxFontBase::operator!=(const wxFont& font) const
138 {
139 return GetFontData() != font.GetFontData();
140 }
141
142 wxString wxFontBase::GetFamilyString() const
143 {
144 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
145
146 switch ( GetFamily() )
147 {
148 case wxDECORATIVE: return wxT("wxDECORATIVE");
149 case wxROMAN: return wxT("wxROMAN");
150 case wxSCRIPT: return wxT("wxSCRIPT");
151 case wxSWISS: return wxT("wxSWISS");
152 case wxMODERN: return wxT("wxMODERN");
153 case wxTELETYPE: return wxT("wxTELETYPE");
154 default: return wxT("wxDEFAULT");
155 }
156 }
157
158 wxString wxFontBase::GetStyleString() const
159 {
160 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
161
162 switch ( GetStyle() )
163 {
164 case wxNORMAL: return wxT("wxNORMAL");
165 case wxSLANT: return wxT("wxSLANT");
166 case wxITALIC: return wxT("wxITALIC");
167 default: return wxT("wxDEFAULT");
168 }
169 }
170
171 wxString wxFontBase::GetWeightString() const
172 {
173 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
174
175 switch ( GetWeight() )
176 {
177 case wxNORMAL: return wxT("wxNORMAL");
178 case wxBOLD: return wxT("wxBOLD");
179 case wxLIGHT: return wxT("wxLIGHT");
180 default: return wxT("wxDEFAULT");
181 }
182 }
183
184 #if !defined(__WXGTK__) && !defined(__WXMSW__)
185
186 // ----------------------------------------------------------------------------
187 // wxNativeFontInfo
188 // ----------------------------------------------------------------------------
189
190 // These are the generic forms of FromString()/ToString.
191 //
192 // convert to/from the string representation: format is
193 // version;pointsize;family;style;weight;underlined;facename;encoding
194
195 bool wxNativeFontInfo::FromString(const wxString& s)
196 {
197 long l;
198
199 wxStringTokenizer tokenizer(s, _T(";"));
200
201 wxString token = tokenizer.GetNextToken();
202 //
203 // Ignore the version for now
204 //
205
206 token = tokenizer.GetNextToken();
207 if ( !token.ToLong(&l) )
208 return FALSE;
209 pointSize = (int)l;
210
211 token = tokenizer.GetNextToken();
212 if ( !token.ToLong(&l) )
213 return FALSE;
214 family = (int)l;
215
216 token = tokenizer.GetNextToken();
217 if ( !token.ToLong(&l) )
218 return FALSE;
219 style = (int)l;
220
221 token = tokenizer.GetNextToken();
222 if ( !token.ToLong(&l) )
223 return FALSE;
224 weight = (int)l;
225
226 token = tokenizer.GetNextToken();
227 if ( !token.ToLong(&l) )
228 return FALSE;
229 underlined = l != 0;
230
231 faceName = tokenizer.GetNextToken();
232 if( !faceName )
233 return FALSE;
234
235 token = tokenizer.GetNextToken();
236 if ( !token.ToLong(&l) )
237 return FALSE;
238 encoding = (wxFontEncoding)l;
239
240 return TRUE;
241 }
242
243 wxString wxNativeFontInfo::ToString() const
244 {
245 wxString s;
246
247 s.Printf(_T("%d;%d;%d;%d;%d;%d;%s;%d"),
248 0, // version
249 pointSize,
250 family,
251 style,
252 weight,
253 underlined,
254 faceName.GetData(),
255 (int)encoding);
256
257 return s;
258 }
259
260 #endif // generic wxNativeFontInfo implementation
261