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