]> git.saurik.com Git - wxWidgets.git/blob - src/common/fontcmn.cpp
replaced a static buffer with wxString
[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 #else
108 (void)info;
109 #endif
110 }
111
112 wxString wxFontBase::GetNativeFontInfoDesc() const
113 {
114 wxString fontDesc;
115 wxNativeFontInfo *fontInfo = GetNativeFontInfo();
116 if ( fontInfo )
117 {
118 fontDesc = fontInfo->ToString();
119 delete fontInfo;
120 }
121
122 return fontDesc;
123 }
124
125 wxFont& wxFont::operator=(const wxFont& font)
126 {
127 if ( this != &font )
128 Ref(font);
129
130 return (wxFont &)*this;
131 }
132
133 bool wxFontBase::operator==(const wxFont& font) const
134 {
135 // either it is the same font, i.e. they share the same common data or they
136 // have different ref datas but still describe the same font
137 return GetFontData() == font.GetFontData() ||
138 (
139 Ok() == font.Ok() &&
140 GetPointSize() == font.GetPointSize() &&
141 GetFamily() == font.GetFamily() &&
142 GetStyle() == font.GetStyle() &&
143 GetWeight() == font.GetWeight() &&
144 GetUnderlined() == font.GetUnderlined() &&
145 GetFaceName() == font.GetFaceName() &&
146 GetEncoding() == font.GetEncoding()
147 );
148 }
149
150 bool wxFontBase::operator!=(const wxFont& font) const
151 {
152 return !(*this == font);
153 }
154
155 wxString wxFontBase::GetFamilyString() const
156 {
157 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
158
159 switch ( GetFamily() )
160 {
161 case wxDECORATIVE: return wxT("wxDECORATIVE");
162 case wxROMAN: return wxT("wxROMAN");
163 case wxSCRIPT: return wxT("wxSCRIPT");
164 case wxSWISS: return wxT("wxSWISS");
165 case wxMODERN: return wxT("wxMODERN");
166 case wxTELETYPE: return wxT("wxTELETYPE");
167 default: return wxT("wxDEFAULT");
168 }
169 }
170
171 wxString wxFontBase::GetStyleString() const
172 {
173 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
174
175 switch ( GetStyle() )
176 {
177 case wxNORMAL: return wxT("wxNORMAL");
178 case wxSLANT: return wxT("wxSLANT");
179 case wxITALIC: return wxT("wxITALIC");
180 default: return wxT("wxDEFAULT");
181 }
182 }
183
184 wxString wxFontBase::GetWeightString() const
185 {
186 wxCHECK_MSG( Ok(), wxT("wxDEFAULT"), wxT("invalid font") );
187
188 switch ( GetWeight() )
189 {
190 case wxNORMAL: return wxT("wxNORMAL");
191 case wxBOLD: return wxT("wxBOLD");
192 case wxLIGHT: return wxT("wxLIGHT");
193 default: return wxT("wxDEFAULT");
194 }
195 }
196
197 #if !defined(__WXGTK__) && !defined(__WXMSW__)
198
199 // ----------------------------------------------------------------------------
200 // wxNativeFontInfo
201 // ----------------------------------------------------------------------------
202
203 // These are the generic forms of FromString()/ToString.
204 //
205 // convert to/from the string representation: format is
206 // version;pointsize;family;style;weight;underlined;facename;encoding
207
208 bool wxNativeFontInfo::FromString(const wxString& s)
209 {
210 long l;
211
212 wxStringTokenizer tokenizer(s, _T(";"));
213
214 wxString token = tokenizer.GetNextToken();
215 //
216 // Ignore the version for now
217 //
218
219 token = tokenizer.GetNextToken();
220 if ( !token.ToLong(&l) )
221 return FALSE;
222 pointSize = (int)l;
223
224 token = tokenizer.GetNextToken();
225 if ( !token.ToLong(&l) )
226 return FALSE;
227 family = (int)l;
228
229 token = tokenizer.GetNextToken();
230 if ( !token.ToLong(&l) )
231 return FALSE;
232 style = (int)l;
233
234 token = tokenizer.GetNextToken();
235 if ( !token.ToLong(&l) )
236 return FALSE;
237 weight = (int)l;
238
239 token = tokenizer.GetNextToken();
240 if ( !token.ToLong(&l) )
241 return FALSE;
242 underlined = l != 0;
243
244 faceName = tokenizer.GetNextToken();
245 if( !faceName )
246 return FALSE;
247
248 token = tokenizer.GetNextToken();
249 if ( !token.ToLong(&l) )
250 return FALSE;
251 encoding = (wxFontEncoding)l;
252
253 return TRUE;
254 }
255
256 wxString wxNativeFontInfo::ToString() const
257 {
258 wxString s;
259
260 s.Printf(_T("%d;%d;%d;%d;%d;%d;%s;%d"),
261 0, // version
262 pointSize,
263 family,
264 style,
265 weight,
266 underlined,
267 faceName.GetData(),
268 (int)encoding);
269
270 return s;
271 }
272
273 #endif // generic wxNativeFontInfo implementation
274