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