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