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