]> git.saurik.com Git - wxWidgets.git/blob - src/mgl/font.cpp
wxMGL bugfixes
[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 }
129
130 wxFontRefData::wxFontRefData(int size, int family, int style,
131 int weight, bool underlined,
132 const wxString& faceName,
133 wxFontEncoding encoding)
134 {
135 Init(size, family, style, weight, underlined, faceName, encoding);
136 }
137
138 wxFontRefData::~wxFontRefData()
139 {
140 if ( m_library )
141 m_library->DecRef();
142 }
143
144 // ----------------------------------------------------------------------------
145 // wxFont
146 // ----------------------------------------------------------------------------
147
148 IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
149
150 void wxFont::Init()
151 {
152 if (wxTheFontList)
153 wxTheFontList->Append(this);
154 }
155
156 bool wxFont::Create(const wxNativeFontInfo& info)
157 {
158 return Create(info.pointSize, info.family, info.style, info.weight,
159 info.underlined, info.faceName, info.encoding);
160 }
161
162 bool wxFont::Create(int pointSize,
163 int family,
164 int style,
165 int weight,
166 bool underlined,
167 const wxString& face,
168 wxFontEncoding encoding)
169 {
170 m_refData = new wxFontRefData(pointSize, family, style, weight,
171 underlined, face, encoding);
172 return TRUE;
173 }
174
175 struct font_t *wxFont::GetMGLfont_t(float scale, bool antialiased)
176 {
177 if ( !M_FONTDATA->m_valid )
178 {
179 wxMGLFontLibrary *old = M_FONTDATA->m_library;
180 M_FONTDATA->m_library = wxTheFontsManager->GetFontLibrary(this);
181 M_FONTDATA->m_library->IncRef();
182 if ( old )
183 old->DecRef();
184 }
185
186 wxMGLFontInstance *instance =
187 M_FONTDATA->m_library->GetFontInstance(this, scale, antialiased);
188
189 return instance->GetMGLfont_t();
190 }
191
192 void wxFont::Unshare()
193 {
194 if ( !m_refData )
195 {
196 m_refData = new wxFontRefData();
197 }
198 else
199 {
200 wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData);
201 UnRef();
202 m_refData = ref;
203 }
204 }
205
206 wxFont::~wxFont()
207 {
208 if (wxTheFontList)
209 wxTheFontList->DeleteObject(this);
210 }
211
212 // ----------------------------------------------------------------------------
213 // accessors
214 // ----------------------------------------------------------------------------
215
216 int wxFont::GetPointSize() const
217 {
218 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
219
220 return M_FONTDATA->m_pointSize;
221 }
222
223 wxString wxFont::GetFaceName() const
224 {
225 wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
226
227 return M_FONTDATA->m_faceName;
228 }
229
230 int wxFont::GetFamily() const
231 {
232 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
233
234 return M_FONTDATA->m_family;
235 }
236
237 int wxFont::GetStyle() const
238 {
239 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
240
241 return M_FONTDATA->m_style;
242 }
243
244 int wxFont::GetWeight() const
245 {
246 wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
247
248 return M_FONTDATA->m_weight;
249 }
250
251 bool wxFont::GetUnderlined() const
252 {
253 wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") );
254
255 return M_FONTDATA->m_underlined;
256 }
257
258
259 wxFontEncoding wxFont::GetEncoding() const
260 {
261 wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") );
262
263 return M_FONTDATA->m_encoding;
264 }
265
266
267 // ----------------------------------------------------------------------------
268 // change font attributes
269 // ----------------------------------------------------------------------------
270
271 void wxFont::SetPointSize(int pointSize)
272 {
273 Unshare();
274
275 M_FONTDATA->m_pointSize = pointSize;
276 M_FONTDATA->m_valid = FALSE;
277 }
278
279 void wxFont::SetFamily(int family)
280 {
281 Unshare();
282
283 M_FONTDATA->m_family = family;
284 M_FONTDATA->m_valid = FALSE;
285 }
286
287 void wxFont::SetStyle(int style)
288 {
289 Unshare();
290
291 M_FONTDATA->m_style = style;
292 M_FONTDATA->m_valid = FALSE;
293 }
294
295 void wxFont::SetWeight(int weight)
296 {
297 Unshare();
298
299 M_FONTDATA->m_weight = weight;
300 M_FONTDATA->m_valid = FALSE;
301 }
302
303 void wxFont::SetFaceName(const wxString& faceName)
304 {
305 Unshare();
306
307 M_FONTDATA->m_faceName = faceName;
308 M_FONTDATA->m_valid = FALSE;
309 }
310
311 void wxFont::SetUnderlined(bool underlined)
312 {
313 Unshare();
314
315 M_FONTDATA->m_underlined = underlined;
316 }
317
318 void wxFont::SetEncoding(wxFontEncoding encoding)
319 {
320 Unshare();
321
322 M_FONTDATA->m_encoding = encoding;
323 M_FONTDATA->m_valid = FALSE;
324 }