]> git.saurik.com Git - wxWidgets.git/blob - src/mgl/font.cpp
fixed off-by-one error
[wxWidgets.git] / src / mgl / font.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: font.cpp
3 // Author: Vaclav Slavik
4 // Id: $Id$
5 // Copyright: (c) 2001 SciTech Software, Inc. (www.scitechsoft.com)
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
8
9 // ============================================================================
10 // declarations
11 // ============================================================================
12
13 // ----------------------------------------------------------------------------
14 // headers
15 // ----------------------------------------------------------------------------
16
17 #ifdef __GNUG__
18 #pragma implementation "font.h"
19 #endif
20
21 #include "wx/font.h"
22 #include "wx/fontutil.h"
23 #include "wx/cmndata.h"
24 #include "wx/utils.h"
25 #include "wx/log.h"
26 #include "wx/gdicmn.h"
27 #include "wx/tokenzr.h"
28 #include "wx/settings.h"
29
30 #include <strings.h>
31
32 // ----------------------------------------------------------------------------
33 // wxFontRefData
34 // ----------------------------------------------------------------------------
35
36 class wxFontRefData : public wxObjectRefData
37 {
38 public:
39 wxFontRefData(int size = wxDEFAULT,
40 int family = wxDEFAULT,
41 int style = wxDEFAULT,
42 int weight = wxDEFAULT,
43 bool underlined = FALSE,
44 const wxString& faceName = wxEmptyString,
45 wxFontEncoding encoding = wxFONTENCODING_DEFAULT);
46 wxFontRefData(const wxFontRefData& data);
47 virtual ~wxFontRefData();
48
49 protected:
50 // common part of all ctors
51 void Init(int pointSize,
52 int family,
53 int style,
54 int weight,
55 bool underlined,
56 const wxString& faceName,
57 wxFontEncoding encoding);
58
59 private:
60 int m_pointSize;
61 int m_family,
62 m_style,
63 m_weight;
64 bool m_underlined;
65 wxString m_faceName;
66 wxFontEncoding m_encoding;
67
68 wxMGLFontLibrary *m_library;
69 bool m_valid;
70
71 friend class wxFont;
72 };
73
74 // ============================================================================
75 // implementation
76 // ============================================================================
77
78 // ----------------------------------------------------------------------------
79 // wxFontRefData
80 // ----------------------------------------------------------------------------
81
82 void wxFontRefData::Init(int pointSize,
83 int family,
84 int style,
85 int weight,
86 bool underlined,
87 const wxString& faceName,
88 wxFontEncoding encoding)
89 {
90 if ( family == wxDEFAULT )
91 m_family = wxSWISS;
92 else
93 m_family = family;
94
95 m_faceName = faceName;
96
97 if ( style == wxDEFAULT )
98 m_style = wxNORMAL;
99 else
100 m_style = style;
101
102 if ( weight == wxDEFAULT )
103 m_weight = wxNORMAL;
104 else
105 m_weight = weight;
106
107 if ( pointSize == wxDEFAULT )
108 m_pointSize = 12;
109 else
110 m_pointSize = pointSize;
111
112 m_underlined = underlined;
113 m_encoding = encoding;
114
115 m_library = NULL;
116 m_valid = FALSE;
117 }
118
119 wxFontRefData::wxFontRefData(const wxFontRefData& data)
120 {
121 Init(data.m_pointSize, data.m_family, data.m_style, data.m_weight,
122 data.m_underlined, data.m_faceName, data.m_encoding);
123
124 m_library = data.m_library;
125 m_valid = data.m_valid;
126 if ( m_library )
127 m_library->IncRef();
128 wxLogTrace("mgl_font", "created fntrefdata %p, library is %p", this, m_library);
129 }
130
131 wxFontRefData::wxFontRefData(int size, int family, int style,
132 int weight, bool underlined,
133 const wxString& faceName,
134 wxFontEncoding encoding)
135 {
136 Init(size, family, style, weight, underlined, faceName, encoding);
137 wxLogTrace("mgl_font", "created fntrefdata %p, library is %p", this, m_library);
138 }
139
140 wxFontRefData::~wxFontRefData()
141 {
142 wxLogTrace("mgl_font", "destructing fntrefdata %p, library is %p", this, m_library);
143 if ( m_library )
144 m_library->DecRef();
145 }
146
147 // ----------------------------------------------------------------------------
148 // wxFont
149 // ----------------------------------------------------------------------------
150
151 IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
152
153 void wxFont::Init()
154 {
155 if (wxTheFontList)
156 wxTheFontList->Append(this);
157 }
158
159 bool wxFont::Create(const wxNativeFontInfo& info)
160 {
161 return Create(info.pointSize, info.family, info.style, info.weight,
162 info.underlined, info.faceName, info.encoding);
163 }
164
165 bool wxFont::Create(int pointSize,
166 int family,
167 int style,
168 int weight,
169 bool underlined,
170 const wxString& face,
171 wxFontEncoding encoding)
172 {
173 m_refData = new wxFontRefData(pointSize, family, style, weight,
174 underlined, face, encoding);
175 return TRUE;
176 }
177
178 struct font_t *wxFont::GetMGLfont_t(float scale, bool antialiased)
179 {
180 if ( !M_FONTDATA->m_valid )
181 {
182 wxMGLFontLibrary *old = M_FONTDATA->m_library;
183 M_FONTDATA->m_library = wxTheFontsManager->GetFontLibrary(this);
184 M_FONTDATA->m_library->IncRef();
185 if ( old )
186 old->DecRef();
187 }
188
189 wxMGLFontInstance *instance =
190 M_FONTDATA->m_library->GetFontInstance(this, scale, antialiased);
191
192 return instance->GetMGLfont_t();
193 }
194
195 void wxFont::Unshare()
196 {
197 if ( !m_refData )
198 {
199 m_refData = new wxFontRefData();
200 }
201 else
202 {
203 wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData);
204 UnRef();
205 m_refData = ref;
206 }
207 }
208
209 wxFont::~wxFont()
210 {
211 if (wxTheFontList)
212 wxTheFontList->DeleteObject(this);
213 }
214
215 // ----------------------------------------------------------------------------
216 // accessors
217 // ----------------------------------------------------------------------------
218
219 int wxFont::GetPointSize() const
220 {
221 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
222
223 return M_FONTDATA->m_pointSize;
224 }
225
226 wxString wxFont::GetFaceName() const
227 {
228 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
229
230 return M_FONTDATA->m_faceName;
231 }
232
233 int wxFont::GetFamily() const
234 {
235 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
236
237 return M_FONTDATA->m_family;
238 }
239
240 int wxFont::GetStyle() const
241 {
242 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
243
244 return M_FONTDATA->m_style;
245 }
246
247 int wxFont::GetWeight() const
248 {
249 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
250
251 return M_FONTDATA->m_weight;
252 }
253
254 bool wxFont::GetUnderlined() const
255 {
256 wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") );
257
258 return M_FONTDATA->m_underlined;
259 }
260
261
262 wxFontEncoding wxFont::GetEncoding() const
263 {
264 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") );
265
266 return M_FONTDATA->m_encoding;
267 }
268
269
270 // ----------------------------------------------------------------------------
271 // change font attributes
272 // ----------------------------------------------------------------------------
273
274 void wxFont::SetPointSize(int pointSize)
275 {
276 Unshare();
277
278 M_FONTDATA->m_pointSize = pointSize;
279 M_FONTDATA->m_valid = FALSE;
280 }
281
282 void wxFont::SetFamily(int family)
283 {
284 Unshare();
285
286 M_FONTDATA->m_family = family;
287 M_FONTDATA->m_valid = FALSE;
288 }
289
290 void wxFont::SetStyle(int style)
291 {
292 Unshare();
293
294 M_FONTDATA->m_style = style;
295 M_FONTDATA->m_valid = FALSE;
296 }
297
298 void wxFont::SetWeight(int weight)
299 {
300 Unshare();
301
302 M_FONTDATA->m_weight = weight;
303 M_FONTDATA->m_valid = FALSE;
304 }
305
306 void wxFont::SetFaceName(const wxString& faceName)
307 {
308 Unshare();
309
310 M_FONTDATA->m_faceName = faceName;
311 M_FONTDATA->m_valid = FALSE;
312 }
313
314 void wxFont::SetUnderlined(bool underlined)
315 {
316 Unshare();
317
318 M_FONTDATA->m_underlined = underlined;
319 }
320
321 void wxFont::SetEncoding(wxFontEncoding encoding)
322 {
323 Unshare();
324
325 M_FONTDATA->m_encoding = encoding;
326 M_FONTDATA->m_valid = FALSE;
327 }