]> git.saurik.com Git - wxWidgets.git/blob - src/mac/classic/font.cpp
uninitialized variable warning fixed (modified patch 1434065)
[wxWidgets.git] / src / mac / classic / font.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: font.cpp
3 // Purpose: wxFont class
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/defs.h"
13 #include "wx/string.h"
14 #include "wx/font.h"
15 #include "wx/fontutil.h"
16 #include "wx/gdicmn.h"
17 #include "wx/utils.h"
18
19 #include "wx/fontutil.h"
20
21 #include "wx/mac/private.h"
22 #include <ATSUnicode.h>
23
24 IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
25
26 class WXDLLEXPORT wxFontRefData: public wxGDIRefData
27 {
28 friend class WXDLLEXPORT wxFont;
29 public:
30 wxFontRefData()
31 : m_fontId(0)
32 , m_pointSize(10)
33 , m_family(wxDEFAULT)
34 , m_style(wxNORMAL)
35 , m_weight(wxNORMAL)
36 , m_underlined(FALSE)
37 , m_faceName(wxT("Geneva"))
38 , m_encoding(wxFONTENCODING_DEFAULT)
39 , m_macFontNum(0)
40 , m_macFontSize(0)
41 , m_macFontStyle(0)
42 , m_macATSUFontID()
43 {
44 Init(10, wxDEFAULT, wxNORMAL, wxNORMAL, FALSE,
45 wxT("Geneva"), wxFONTENCODING_DEFAULT);
46 }
47
48 wxFontRefData(const wxFontRefData& data)
49 : wxGDIRefData()
50 , m_fontId(data.m_fontId)
51 , m_pointSize(data.m_pointSize)
52 , m_family(data.m_family)
53 , m_style(data.m_style)
54 , m_weight(data.m_weight)
55 , m_underlined(data.m_underlined)
56 , m_faceName(data.m_faceName)
57 , m_encoding(data.m_encoding)
58 , m_macFontNum(data.m_macFontNum)
59 , m_macFontSize(data.m_macFontSize)
60 , m_macFontStyle(data.m_macFontStyle)
61 , m_macATSUFontID(data.m_macATSUFontID)
62 {
63 Init(data.m_pointSize, data.m_family, data.m_style, data.m_weight,
64 data.m_underlined, data.m_faceName, data.m_encoding);
65 }
66
67 wxFontRefData(int size,
68 int family,
69 int style,
70 int weight,
71 bool underlined,
72 const wxString& faceName,
73 wxFontEncoding encoding)
74 : m_fontId(0)
75 , m_pointSize(size)
76 , m_family(family)
77 , m_style(style)
78 , m_weight(weight)
79 , m_underlined(underlined)
80 , m_faceName(faceName)
81 , m_encoding(encoding)
82 , m_macFontNum(0)
83 , m_macFontSize(0)
84 , m_macFontStyle(0)
85 , m_macATSUFontID(0)
86 {
87 Init(size, family, style, weight, underlined, faceName, encoding);
88 }
89
90 virtual ~wxFontRefData();
91 void SetNoAntiAliasing( bool no = TRUE ) { m_noAA = no; }
92 bool GetNoAntiAliasing() const { return m_noAA; }
93
94 protected:
95 // common part of all ctors
96 void Init(int size,
97 int family,
98 int style,
99 int weight,
100 bool underlined,
101 const wxString& faceName,
102 wxFontEncoding encoding);
103
104 // font characterstics
105 int m_fontId;
106 int m_pointSize;
107 int m_family;
108 int m_style;
109 int m_weight;
110 bool m_underlined;
111 wxString m_faceName;
112 wxFontEncoding m_encoding;
113 bool m_noAA; // No anti-aliasing
114
115 public:
116 short m_macFontNum;
117 short m_macFontSize;
118 unsigned char m_macFontStyle;
119 wxUint32 m_macATSUFontID;
120
121 wxNativeFontInfo m_info;
122
123 public:
124 void MacFindFont() ;
125 };
126 // ============================================================================
127 // implementation
128 // ============================================================================
129
130 // ----------------------------------------------------------------------------
131 // wxFontRefData
132 // ----------------------------------------------------------------------------
133
134 void wxFontRefData::Init(int pointSize,
135 int family,
136 int style,
137 int weight,
138 bool underlined,
139 const wxString& faceName,
140 wxFontEncoding encoding)
141 {
142 m_style = style;
143 m_pointSize = pointSize;
144 m_family = family;
145 m_style = style;
146 m_weight = weight;
147 m_underlined = underlined;
148 m_faceName = faceName;
149 m_encoding = encoding;
150
151 m_macFontNum = 0 ;
152 m_macFontSize = 0;
153 m_macFontStyle = 0;
154 m_fontId = 0;
155 m_noAA = FALSE;
156 }
157
158 wxFontRefData::~wxFontRefData()
159 {
160 }
161
162 void wxFontRefData::MacFindFont()
163 {
164 if( m_faceName.Length() == 0 )
165 {
166 switch( m_family )
167 {
168 case wxDEFAULT :
169 m_macFontNum = ::GetAppFont() ;
170 break ;
171 case wxDECORATIVE :
172 ::GetFNum( "\pTimes" , &m_macFontNum) ;
173 break ;
174 case wxROMAN :
175 ::GetFNum( "\pTimes" , &m_macFontNum) ;
176 break ;
177 case wxSCRIPT :
178 ::GetFNum( "\pTimes" , &m_macFontNum) ;
179 break ;
180 case wxSWISS :
181 ::GetFNum( "\pGeneva" , &m_macFontNum) ;
182 break ;
183 case wxMODERN :
184 ::GetFNum( "\pMonaco" , &m_macFontNum) ;
185 break ;
186 }
187 Str255 name ;
188 GetFontName( m_macFontNum , name ) ;
189 m_faceName = wxMacMakeStringFromPascal( name ) ;
190 }
191 else
192 {
193 if ( m_faceName == wxT("systemfont") )
194 m_macFontNum = ::GetSysFont() ;
195 else if ( m_faceName == wxT("applicationfont") )
196 m_macFontNum = ::GetAppFont() ;
197 else
198 {
199 Str255 fontname ;
200 wxMacStringToPascal( m_faceName , fontname ) ;
201 ::GetFNum( fontname, &m_macFontNum);
202 }
203 }
204
205 m_macFontStyle = 0;
206 if (m_weight == wxBOLD)
207 m_macFontStyle |= bold;
208 if (m_style == wxITALIC || m_style == wxSLANT)
209 m_macFontStyle |= italic;
210 if (m_underlined)
211 m_macFontStyle |= underline;
212 m_macFontSize = m_pointSize ;
213
214 //TODO:if we supply the style as an additional parameter we must make a testing
215 //sequence in order to degrade gracefully while trying to maintain most of the style
216 //information, meanwhile we just take the normal font and apply the features after
217 #ifdef __WXDEBUG__
218 OSStatus status =
219 #endif // __WXDEBUG__
220 ::ATSUFONDtoFontID(m_macFontNum, normal /*qdStyle*/, (UInt32*)&m_macATSUFontID);
221 /*
222 status = ATSUFindFontFromName ( (Ptr) m_faceName , strlen( m_faceName ) ,
223 kFontFullName, kFontMacintoshPlatform, kFontRomanScript , kFontNoLanguage , (UInt32*)&m_macATSUFontID ) ;
224 */
225 wxASSERT_MSG( status == noErr , wxT("couldn't retrieve font identifier") ) ;
226 }
227
228 // ----------------------------------------------------------------------------
229 // wxFont
230 // ----------------------------------------------------------------------------
231 }
232
233 bool wxFont::Create(const wxNativeFontInfo& info)
234 {
235 return Create(info.pointSize, info.family, info.style, info.weight,
236 info.underlined, info.faceName, info.encoding);
237 }
238
239 wxFont::wxFont(const wxString& fontdesc)
240 {
241 wxNativeFontInfo info;
242 if ( info.FromString(fontdesc) )
243 (void)Create(info);
244 }
245
246 bool wxFont::Create(int pointSize,
247 int family,
248 int style,
249 int weight,
250 bool underlined,
251 const wxString& faceName,
252 wxFontEncoding encoding)
253 {
254 UnRef();
255 m_refData = new wxFontRefData(pointSize, family, style, weight,
256 underlined, faceName, encoding);
257
258 RealizeResource();
259
260 return TRUE;
261 }
262
263 wxFont::~wxFont()
264 {
265 }
266
267 bool wxFont::RealizeResource()
268 {
269 M_FONTDATA->MacFindFont() ;
270 return TRUE;
271 }
272
273 void wxFont::SetEncoding(wxFontEncoding encoding)
274 {
275 Unshare();
276
277 M_FONTDATA->m_encoding = encoding;
278
279 RealizeResource();
280 }
281
282 void wxFont::Unshare()
283 {
284 // Don't change shared data
285 if (!m_refData)
286 {
287 m_refData = new wxFontRefData();
288 }
289 else
290 {
291 wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData);
292 UnRef();
293 m_refData = ref;
294 }
295 }
296
297 void wxFont::SetPointSize(int pointSize)
298 {
299 Unshare();
300
301 M_FONTDATA->m_pointSize = pointSize;
302
303 RealizeResource();
304 }
305
306 void wxFont::SetFamily(int family)
307 {
308 Unshare();
309
310 M_FONTDATA->m_family = family;
311
312 RealizeResource();
313 }
314
315 void wxFont::SetStyle(int style)
316 {
317 Unshare();
318
319 M_FONTDATA->m_style = style;
320
321 RealizeResource();
322 }
323
324 void wxFont::SetWeight(int weight)
325 {
326 Unshare();
327
328 M_FONTDATA->m_weight = weight;
329
330 RealizeResource();
331 }
332
333 void wxFont::SetFaceName(const wxString& faceName)
334 {
335 Unshare();
336
337 M_FONTDATA->m_faceName = faceName;
338
339 RealizeResource();
340 }
341
342 void wxFont::SetUnderlined(bool underlined)
343 {
344 Unshare();
345
346 M_FONTDATA->m_underlined = underlined;
347
348 RealizeResource();
349 }
350
351 void wxFont::SetNoAntiAliasing( bool no )
352 {
353 Unshare();
354
355 M_FONTDATA->SetNoAntiAliasing( no );
356
357 RealizeResource();
358 }
359
360 // ----------------------------------------------------------------------------
361 // accessors
362 // ----------------------------------------------------------------------------
363
364 // TODO: insert checks everywhere for M_FONTDATA == NULL!
365
366 int wxFont::GetPointSize() const
367 {
368 return M_FONTDATA->m_pointSize;
369 }
370
371 int wxFont::GetFamily() const
372 {
373 return M_FONTDATA->m_family;
374 }
375
376 int wxFont::GetStyle() const
377 {
378 return M_FONTDATA->m_style;
379 }
380
381 int wxFont::GetWeight() const
382 {
383 return M_FONTDATA->m_weight;
384 }
385
386 bool wxFont::GetUnderlined() const
387 {
388 return M_FONTDATA->m_underlined;
389 }
390
391 wxString wxFont::GetFaceName() const
392 {
393 wxString str;
394 if ( M_FONTDATA )
395 str = M_FONTDATA->m_faceName ;
396 return str;
397 }
398
399 wxFontEncoding wxFont::GetEncoding() const
400 {
401 return M_FONTDATA->m_encoding;
402 }
403
404 bool wxFont::GetNoAntiAliasing() const
405 {
406 return M_FONTDATA->m_noAA;
407 }
408
409 short wxFont::GetMacFontNum() const
410 {
411 return M_FONTDATA->m_macFontNum;
412 }
413
414 short wxFont::GetMacFontSize() const
415 {
416 return M_FONTDATA->m_macFontSize;
417 }
418
419 wxByte wxFont::GetMacFontStyle() const
420 {
421 return M_FONTDATA->m_macFontStyle;
422 }
423
424 wxUint32 wxFont::GetMacATSUFontID() const
425 {
426 return M_FONTDATA->m_macATSUFontID;
427 }
428
429 const wxNativeFontInfo *wxFont::GetNativeFontInfo() const
430 {
431 wxCHECK_MSG( Ok(), NULL, wxT("invalid font") );
432
433 M_FONTDATA->m_info.InitFromFont(*this);
434
435 return &(M_FONTDATA->m_info);
436 }
437