]> git.saurik.com Git - wxWidgets.git/blob - src/mac/classic/font.cpp
minor cleanup
[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 void wxFont::Init()
233 {
234 }
235
236 bool wxFont::Create(const wxNativeFontInfo& info)
237 {
238 return Create(info.pointSize, info.family, info.style, info.weight,
239 info.underlined, info.faceName, info.encoding);
240 }
241
242 wxFont::wxFont(const wxString& fontdesc)
243 {
244 wxNativeFontInfo info;
245 if ( info.FromString(fontdesc) )
246 (void)Create(info);
247 }
248
249 bool wxFont::Create(int pointSize,
250 int family,
251 int style,
252 int weight,
253 bool underlined,
254 const wxString& faceName,
255 wxFontEncoding encoding)
256 {
257 UnRef();
258 m_refData = new wxFontRefData(pointSize, family, style, weight,
259 underlined, faceName, encoding);
260
261 RealizeResource();
262
263 return TRUE;
264 }
265
266 wxFont::~wxFont()
267 {
268 }
269
270 bool wxFont::RealizeResource()
271 {
272 M_FONTDATA->MacFindFont() ;
273 return TRUE;
274 }
275
276 void wxFont::SetEncoding(wxFontEncoding encoding)
277 {
278 Unshare();
279
280 M_FONTDATA->m_encoding = encoding;
281
282 RealizeResource();
283 }
284
285 void wxFont::Unshare()
286 {
287 // Don't change shared data
288 if (!m_refData)
289 {
290 m_refData = new wxFontRefData();
291 }
292 else
293 {
294 wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData);
295 UnRef();
296 m_refData = ref;
297 }
298 }
299
300 void wxFont::SetPointSize(int pointSize)
301 {
302 Unshare();
303
304 M_FONTDATA->m_pointSize = pointSize;
305
306 RealizeResource();
307 }
308
309 void wxFont::SetFamily(int family)
310 {
311 Unshare();
312
313 M_FONTDATA->m_family = family;
314
315 RealizeResource();
316 }
317
318 void wxFont::SetStyle(int style)
319 {
320 Unshare();
321
322 M_FONTDATA->m_style = style;
323
324 RealizeResource();
325 }
326
327 void wxFont::SetWeight(int weight)
328 {
329 Unshare();
330
331 M_FONTDATA->m_weight = weight;
332
333 RealizeResource();
334 }
335
336 void wxFont::SetFaceName(const wxString& faceName)
337 {
338 Unshare();
339
340 M_FONTDATA->m_faceName = faceName;
341
342 RealizeResource();
343 }
344
345 void wxFont::SetUnderlined(bool underlined)
346 {
347 Unshare();
348
349 M_FONTDATA->m_underlined = underlined;
350
351 RealizeResource();
352 }
353
354 void wxFont::SetNoAntiAliasing( bool no )
355 {
356 Unshare();
357
358 M_FONTDATA->SetNoAntiAliasing( no );
359
360 RealizeResource();
361 }
362
363 // ----------------------------------------------------------------------------
364 // accessors
365 // ----------------------------------------------------------------------------
366
367 // TODO: insert checks everywhere for M_FONTDATA == NULL!
368
369 int wxFont::GetPointSize() const
370 {
371 return M_FONTDATA->m_pointSize;
372 }
373
374 int wxFont::GetFamily() const
375 {
376 return M_FONTDATA->m_family;
377 }
378
379 int wxFont::GetStyle() const
380 {
381 return M_FONTDATA->m_style;
382 }
383
384 int wxFont::GetWeight() const
385 {
386 return M_FONTDATA->m_weight;
387 }
388
389 bool wxFont::GetUnderlined() const
390 {
391 return M_FONTDATA->m_underlined;
392 }
393
394 wxString wxFont::GetFaceName() const
395 {
396 wxString str;
397 if ( M_FONTDATA )
398 str = M_FONTDATA->m_faceName ;
399 return str;
400 }
401
402 wxFontEncoding wxFont::GetEncoding() const
403 {
404 return M_FONTDATA->m_encoding;
405 }
406
407 bool wxFont::GetNoAntiAliasing() const
408 {
409 return M_FONTDATA->m_noAA;
410 }
411
412 short wxFont::GetMacFontNum() const
413 {
414 return M_FONTDATA->m_macFontNum;
415 }
416
417 short wxFont::GetMacFontSize() const
418 {
419 return M_FONTDATA->m_macFontSize;
420 }
421
422 wxByte wxFont::GetMacFontStyle() const
423 {
424 return M_FONTDATA->m_macFontStyle;
425 }
426
427 wxUint32 wxFont::GetMacATSUFontID() const
428 {
429 return M_FONTDATA->m_macATSUFontID;
430 }
431
432 const wxNativeFontInfo *wxFont::GetNativeFontInfo() const
433 {
434 wxCHECK_MSG( Ok(), NULL, wxT("invalid font") );
435
436 M_FONTDATA->m_info.InitFromFont(*this);
437
438 return &(M_FONTDATA->m_info);
439 }
440