]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/font.cpp
added test for wxScopeGuard
[wxWidgets.git] / src / mac / carbon / font.cpp
CommitLineData
e9576ca5
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: font.cpp
3// Purpose: wxFont class
a31a5f85 4// Author: Stefan Csomor
e9576ca5 5// Modified by:
a31a5f85 6// Created: 1998-01-01
e9576ca5 7// RCS-ID: $Id$
a31a5f85 8// Copyright: (c) Stefan Csomor
65571936 9// Licence: wxWindows licence
e9576ca5
SC
10/////////////////////////////////////////////////////////////////////////////
11
3e527073 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
e9576ca5
SC
13#pragma implementation "font.h"
14#endif
15
3e527073
SC
16#include "wx/wxprec.h"
17
e9576ca5
SC
18#include "wx/string.h"
19#include "wx/font.h"
5b781a67 20#include "wx/fontutil.h"
e9576ca5 21#include "wx/gdicmn.h"
03e11df5 22#include "wx/utils.h"
e9576ca5 23
3b7e6277
GD
24#include "wx/fontutil.h"
25
76a5e5d2 26#include "wx/mac/private.h"
7f0c3a63 27#include <ATSUnicode.h>
76a5e5d2 28
2f1ae414 29#if !USE_SHARED_LIBRARIES
e9576ca5 30IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
2f1ae414 31#endif
e9576ca5 32
3bf5a59b
VZ
33class WXDLLEXPORT wxFontRefData: public wxGDIRefData
34{
35 friend class WXDLLEXPORT wxFont;
36public:
37 wxFontRefData()
38 : m_fontId(0)
39 , m_pointSize(10)
40 , m_family(wxDEFAULT)
41 , m_style(wxNORMAL)
42 , m_weight(wxNORMAL)
43 , m_underlined(FALSE)
c277569a 44 , m_faceName(wxT("applicationfont"))
3bf5a59b
VZ
45 , m_encoding(wxFONTENCODING_DEFAULT)
46 , m_macFontNum(0)
47 , m_macFontSize(0)
48 , m_macFontStyle(0)
3e527073 49 , m_macATSUStyle(0)
e369bddb 50 , m_macATSUFontID(0)
3bf5a59b 51 {
c277569a
SC
52 Init(m_pointSize, m_family, m_style, m_weight,
53 m_underlined, m_faceName, m_encoding);
3bf5a59b
VZ
54 }
55
56 wxFontRefData(const wxFontRefData& data)
57 : wxGDIRefData()
58 , m_fontId(data.m_fontId)
59 , m_pointSize(data.m_pointSize)
60 , m_family(data.m_family)
61 , m_style(data.m_style)
62 , m_weight(data.m_weight)
63 , m_underlined(data.m_underlined)
64 , m_faceName(data.m_faceName)
65 , m_encoding(data.m_encoding)
66 , m_macFontNum(data.m_macFontNum)
67 , m_macFontSize(data.m_macFontSize)
68 , m_macFontStyle(data.m_macFontStyle)
3e527073 69 , m_macATSUStyle(0)
e369bddb 70 , m_macATSUFontID(data.m_macATSUFontID)
3bf5a59b
VZ
71 {
72 Init(data.m_pointSize, data.m_family, data.m_style, data.m_weight,
73 data.m_underlined, data.m_faceName, data.m_encoding);
74 }
75
76 wxFontRefData(int size,
77 int family,
78 int style,
79 int weight,
80 bool underlined,
81 const wxString& faceName,
82 wxFontEncoding encoding)
83 : m_fontId(0)
84 , m_pointSize(size)
85 , m_family(family)
86 , m_style(style)
87 , m_weight(weight)
88 , m_underlined(underlined)
89 , m_faceName(faceName)
90 , m_encoding(encoding)
91 , m_macFontNum(0)
92 , m_macFontSize(0)
93 , m_macFontStyle(0)
3e527073 94 , m_macATSUStyle(0)
e369bddb 95 , m_macATSUFontID(0)
3bf5a59b
VZ
96 {
97 Init(size, family, style, weight, underlined, faceName, encoding);
98 }
99
100 virtual ~wxFontRefData();
101 void SetNoAntiAliasing( bool no = TRUE ) { m_noAA = no; }
5ac2e80c 102 bool GetNoAntiAliasing() const { return m_noAA; }
3bf5a59b
VZ
103
104protected:
105 // common part of all ctors
106 void Init(int size,
107 int family,
108 int style,
109 int weight,
110 bool underlined,
111 const wxString& faceName,
112 wxFontEncoding encoding);
113
114 // font characterstics
e369bddb
GD
115 int m_fontId;
116 int m_pointSize;
117 int m_family;
118 int m_style;
119 int m_weight;
120 bool m_underlined;
121 wxString m_faceName;
122 wxFontEncoding m_encoding;
3bf5a59b
VZ
123 bool m_noAA; // No anti-aliasing
124
125public:
facd6764
SC
126 short m_macFontNum;
127 short m_macFontSize;
128 Style m_macFontStyle;
129
130 // ATSU Font Information
131
3103e8a9 132 // this is split into an ATSU font id that may
facd6764
SC
133 // contain some styles (special bold fonts etc) and
134 // these are the additional qd styles that are not
135 // included in the ATSU font id
3e527073 136 ATSUStyle m_macATSUStyle ;
facd6764
SC
137 ATSUFontID m_macATSUFontID;
138 Style m_macATSUAdditionalQDStyles ;
139
140 // for true themeing support we must store the correct font
141 // information here, as this speeds up and optimizes rendering
142 ThemeFontID m_macThemeFontID ;
143
3bf5a59b
VZ
144 wxNativeFontInfo m_info;
145
146public:
147 void MacFindFont() ;
148};
e7549107
SC
149// ============================================================================
150// implementation
151// ============================================================================
152
153// ----------------------------------------------------------------------------
154// wxFontRefData
155// ----------------------------------------------------------------------------
156
157void wxFontRefData::Init(int pointSize,
158 int family,
159 int style,
160 int weight,
161 bool underlined,
162 const wxString& faceName,
163 wxFontEncoding encoding)
e9576ca5 164{
e7549107
SC
165 m_style = style;
166 m_pointSize = pointSize;
167 m_family = family;
168 m_style = style;
169 m_weight = weight;
170 m_underlined = underlined;
171 m_faceName = faceName;
172 m_encoding = encoding;
173
d84afea9
GD
174 m_macFontNum = 0 ;
175 m_macFontSize = 0;
176 m_macFontStyle = 0;
facd6764
SC
177 m_macATSUFontID = 0;
178 m_macATSUAdditionalQDStyles = 0 ;
3e527073 179 m_macATSUStyle = NULL ;
facd6764
SC
180
181 m_macThemeFontID = kThemeCurrentPortFont ;
ac17f9b1 182 m_noAA = FALSE;
e9576ca5
SC
183}
184
185wxFontRefData::~wxFontRefData()
186{
3e527073
SC
187 if ( m_macATSUStyle )
188 {
189 ::ATSUDisposeStyle((ATSUStyle)m_macATSUStyle);
190 m_macATSUStyle = NULL ;
191 }
e9576ca5
SC
192}
193
519cb848
SC
194void wxFontRefData::MacFindFont()
195{
facd6764 196 if ( m_macThemeFontID != kThemeCurrentPortFont )
e40298d5 197 {
facd6764
SC
198 Str255 fontName ;
199 GetThemeFont(m_macThemeFontID , GetApplicationScript() , fontName , &m_macFontSize , &m_macFontStyle ) ;
200 m_faceName = wxMacMakeStringFromPascal( fontName ) ;
201 if ( m_macFontStyle & bold )
202 m_weight = wxBOLD ;
203 else
204 m_weight = wxNORMAL ;
205 if ( m_macFontStyle & italic )
206 m_style = wxITALIC ;
207 if ( m_macFontStyle & underline )
208 m_underlined = true ;
209 ::GetFNum( fontName, &m_macFontNum);
210 m_pointSize = m_macFontSize ;
e40298d5
JS
211 }
212 else
213 {
facd6764
SC
214 if( m_faceName.Length() == 0 )
215 {
216 switch( m_family )
217 {
218 case wxDEFAULT :
219 m_macFontNum = ::GetAppFont() ;
220 break ;
facd6764 221 case wxSCRIPT :
4b8f2c2b
SC
222 case wxROMAN :
223 case wxDECORATIVE :
224#ifdef __WXMAC_OSX__
3e527073 225 ::GetFNum( "\pTimes" , &m_macFontNum) ;
4b8f2c2b 226#else
facd6764 227 ::GetFNum( "\pTimes" , &m_macFontNum) ;
4b8f2c2b 228#endif
facd6764
SC
229 break ;
230 case wxSWISS :
4b8f2c2b
SC
231#ifdef __WXMAC_OSX__
232 ::GetFNum( "\pLucida Grande" , &m_macFontNum) ;
233#else
facd6764 234 ::GetFNum( "\pGeneva" , &m_macFontNum) ;
4b8f2c2b 235#endif
facd6764
SC
236 break ;
237 case wxMODERN :
4b8f2c2b
SC
238#ifdef __WXMAC_OSX__
239 ::GetFNum( "\pMonaco" , &m_macFontNum) ;
240#else
facd6764 241 ::GetFNum( "\pMonaco" , &m_macFontNum) ;
4b8f2c2b 242#endif
facd6764
SC
243 break ;
244 }
245 Str255 name ;
246 ::GetFontName( m_macFontNum , name ) ;
247 m_faceName = wxMacMakeStringFromPascal( name ) ;
248 }
e40298d5
JS
249 else
250 {
facd6764
SC
251 if ( m_faceName == wxT("systemfont") )
252 m_macFontNum = ::GetSysFont() ;
253 else if ( m_faceName == wxT("applicationfont") )
254 m_macFontNum = ::GetAppFont() ;
255 else
256 {
257 Str255 fontname ;
258 wxMacStringToPascal( m_faceName , fontname ) ;
259 ::GetFNum( fontname, &m_macFontNum);
260 }
e40298d5 261 }
facd6764
SC
262
263 m_macFontStyle = 0;
264 if (m_weight == wxBOLD)
265 m_macFontStyle |= bold;
266 if (m_style == wxITALIC || m_style == wxSLANT)
267 m_macFontStyle |= italic;
268 if (m_underlined)
269 m_macFontStyle |= underline;
270 m_macFontSize = m_pointSize ;
e40298d5
JS
271 }
272
facd6764 273 // we try to get as much styles as possible into ATSU
3e527073
SC
274
275 Fixed atsuSize = IntToFixed( m_macFontSize ) ;
276
facd6764
SC
277 Style atsuStyle = normal ;
278 verify_noerr(::ATSUFONDtoFontID(m_macFontNum, atsuStyle , (UInt32*)&m_macATSUFontID) );
279 if ( m_macFontStyle & bold )
280 {
281 ATSUFontID test ;
282 if ( ::ATSUFONDtoFontID(m_macFontNum, atsuStyle | bold , &test) == noErr )
283 {
284 atsuStyle |= bold ;
285 m_macATSUFontID = test ;
286 }
287 }
288 if ( m_macFontStyle & italic )
289 {
290 ATSUFontID test ;
291 if ( ::ATSUFONDtoFontID(m_macFontNum, atsuStyle | italic , &test) == noErr )
292 {
293 atsuStyle |= italic ;
294 m_macATSUFontID = test ;
295 }
296 }
297 if ( m_macFontStyle & underline )
298 {
299 ATSUFontID test ;
300 if ( ::ATSUFONDtoFontID(m_macFontNum, atsuStyle | underline , &test) == noErr )
301 {
302 atsuStyle |= underline ;
303 m_macATSUFontID = test ;
304 }
305 }
3e527073 306
facd6764 307 m_macATSUAdditionalQDStyles = m_macFontStyle & (~atsuStyle ) ;
3e527073
SC
308
309 if ( m_macATSUStyle )
310 {
311 ::ATSUDisposeStyle((ATSUStyle)m_macATSUStyle);
312 m_macATSUStyle = NULL ;
313 }
314 OSStatus status = ::ATSUCreateStyle((ATSUStyle *)&m_macATSUStyle) ;
315 wxASSERT_MSG( status == noErr , wxT("couldn't create ATSU style") ) ;
316 ATSUAttributeTag atsuTags[] =
317 {
318 kATSUFontTag ,
319 kATSUSizeTag ,
320 kATSUVerticalCharacterTag,
321 kATSUQDBoldfaceTag ,
322 kATSUQDItalicTag ,
323 kATSUQDUnderlineTag ,
324 kATSUQDCondensedTag ,
325 kATSUQDExtendedTag ,
326 } ;
327 ByteCount atsuSizes[sizeof(atsuTags)/sizeof(ATSUAttributeTag)] =
328 {
329 sizeof( ATSUFontID ) ,
330 sizeof( Fixed ) ,
331 sizeof( ATSUVerticalCharacterType),
332 sizeof( Boolean ) ,
333 sizeof( Boolean ) ,
334 sizeof( Boolean ) ,
335 sizeof( Boolean ) ,
336 sizeof( Boolean ) ,
337 } ;
338 Boolean kTrue = true ;
339 Boolean kFalse = false ;
340
341 ATSUVerticalCharacterType kHorizontal = kATSUStronglyHorizontal;
342 ATSUAttributeValuePtr atsuValues[sizeof(atsuTags)/sizeof(ATSUAttributeTag)] =
343 {
344 &m_macATSUFontID ,
345 &atsuSize ,
346 &kHorizontal,
347 (m_macATSUAdditionalQDStyles & bold) ? &kTrue : &kFalse ,
348 (m_macATSUAdditionalQDStyles & italic) ? &kTrue : &kFalse ,
349 (m_macATSUAdditionalQDStyles & underline) ? &kTrue : &kFalse ,
350 (m_macATSUAdditionalQDStyles & condense) ? &kTrue : &kFalse ,
351 (m_macATSUAdditionalQDStyles & extend) ? &kTrue : &kFalse ,
352 } ;
353 status = ::ATSUSetAttributes((ATSUStyle)m_macATSUStyle, sizeof(atsuTags)/sizeof(ATSUAttributeTag) ,
354 atsuTags, atsuSizes, atsuValues);
355
356 wxASSERT_MSG( status == noErr , wxT("couldn't Modify ATSU style") ) ;
519cb848
SC
357}
358
e7549107
SC
359// ----------------------------------------------------------------------------
360// wxFont
361// ----------------------------------------------------------------------------
e9576ca5 362
e7549107 363void wxFont::Init()
e9576ca5 364{
e9576ca5
SC
365}
366
5b781a67
SC
367bool wxFont::Create(const wxNativeFontInfo& info)
368{
369 return Create(info.pointSize, info.family, info.style, info.weight,
370 info.underlined, info.faceName, info.encoding);
371}
372
3b7e6277
GD
373wxFont::wxFont(const wxString& fontdesc)
374{
375 wxNativeFontInfo info;
376 if ( info.FromString(fontdesc) )
377 (void)Create(info);
378}
379
e7549107
SC
380bool wxFont::Create(int pointSize,
381 int family,
382 int style,
383 int weight,
384 bool underlined,
385 const wxString& faceName,
386 wxFontEncoding encoding)
e9576ca5
SC
387{
388 UnRef();
e7549107
SC
389 m_refData = new wxFontRefData(pointSize, family, style, weight,
390 underlined, faceName, encoding);
e9576ca5
SC
391
392 RealizeResource();
393
394 return TRUE;
395}
396
facd6764
SC
397bool wxFont::MacCreateThemeFont(wxUint16 themeFontID )
398{
399 UnRef();
3e527073 400 m_refData = new wxFontRefData(12, wxDEFAULT , wxFONTSTYLE_NORMAL , wxFONTWEIGHT_NORMAL ,false, wxEmptyString, wxFONTENCODING_DEFAULT);
facd6764
SC
401 M_FONTDATA->m_macThemeFontID = themeFontID ;
402 RealizeResource();
403
404 return TRUE;
405}
406
e9576ca5
SC
407wxFont::~wxFont()
408{
e9576ca5
SC
409}
410
411bool wxFont::RealizeResource()
412{
e40298d5 413 M_FONTDATA->MacFindFont() ;
519cb848 414 return TRUE;
e9576ca5
SC
415}
416
51abe921
SC
417void wxFont::SetEncoding(wxFontEncoding encoding)
418{
419 Unshare();
420
421 M_FONTDATA->m_encoding = encoding;
422
423 RealizeResource();
424}
425
e9576ca5
SC
426void wxFont::Unshare()
427{
e40298d5
JS
428 // Don't change shared data
429 if (!m_refData)
e9576ca5 430 {
e40298d5
JS
431 m_refData = new wxFontRefData();
432 }
e9576ca5
SC
433 else
434 {
e40298d5
JS
435 wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData);
436 UnRef();
437 m_refData = ref;
438 }
e9576ca5
SC
439}
440
441void wxFont::SetPointSize(int pointSize)
442{
443 Unshare();
444
445 M_FONTDATA->m_pointSize = pointSize;
446
447 RealizeResource();
448}
449
450void wxFont::SetFamily(int family)
451{
452 Unshare();
453
454 M_FONTDATA->m_family = family;
455
456 RealizeResource();
457}
458
459void wxFont::SetStyle(int style)
460{
461 Unshare();
462
463 M_FONTDATA->m_style = style;
464
465 RealizeResource();
466}
467
468void wxFont::SetWeight(int weight)
469{
470 Unshare();
471
472 M_FONTDATA->m_weight = weight;
473
474 RealizeResource();
475}
476
477void wxFont::SetFaceName(const wxString& faceName)
478{
479 Unshare();
480
481 M_FONTDATA->m_faceName = faceName;
482
483 RealizeResource();
484}
485
486void wxFont::SetUnderlined(bool underlined)
487{
488 Unshare();
489
490 M_FONTDATA->m_underlined = underlined;
491
492 RealizeResource();
493}
494
ac17f9b1
SC
495void wxFont::SetNoAntiAliasing( bool no )
496{
497 Unshare();
498
499 M_FONTDATA->SetNoAntiAliasing( no );
500
501 RealizeResource();
502}
503
e7549107
SC
504// ----------------------------------------------------------------------------
505// accessors
506// ----------------------------------------------------------------------------
507
fcb35beb
VZ
508// TODO: insert checks everywhere for M_FONTDATA == NULL!
509
e7549107 510int wxFont::GetPointSize() const
e9576ca5 511{
77eddfb7 512 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
e7549107 513 return M_FONTDATA->m_pointSize;
e9576ca5
SC
514}
515
e7549107 516int wxFont::GetFamily() const
e9576ca5 517{
77eddfb7 518 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
e7549107 519 return M_FONTDATA->m_family;
e9576ca5
SC
520}
521
e7549107 522int wxFont::GetStyle() const
e9576ca5 523{
77eddfb7 524 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
e7549107
SC
525 return M_FONTDATA->m_style;
526}
527
528int wxFont::GetWeight() const
529{
77eddfb7 530 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
e7549107
SC
531 return M_FONTDATA->m_weight;
532}
533
534bool wxFont::GetUnderlined() const
535{
77eddfb7 536 wxCHECK_MSG( M_FONTDATA != NULL , false, wxT("invalid font") );
e7549107
SC
537 return M_FONTDATA->m_underlined;
538}
539
540wxString wxFont::GetFaceName() const
541{
facd6764
SC
542 wxCHECK_MSG( M_FONTDATA != NULL , wxEmptyString , wxT("invalid font") );
543 return M_FONTDATA->m_faceName;
e7549107
SC
544}
545
546wxFontEncoding wxFont::GetEncoding() const
547{
facd6764 548 wxCHECK_MSG( M_FONTDATA != NULL , wxFONTENCODING_DEFAULT , wxT("invalid font") );
e7549107 549 return M_FONTDATA->m_encoding;
e9576ca5
SC
550}
551
5ac2e80c 552bool wxFont::GetNoAntiAliasing() const
ac17f9b1 553{
77eddfb7 554 wxCHECK_MSG( M_FONTDATA != NULL , false, wxT("invalid font") );
ac17f9b1
SC
555 return M_FONTDATA->m_noAA;
556}
557
facd6764 558short wxFont::MacGetFontNum() const
fcb35beb 559{
77eddfb7 560 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
fcb35beb
VZ
561 return M_FONTDATA->m_macFontNum;
562}
563
facd6764 564short wxFont::MacGetFontSize() const
fcb35beb 565{
77eddfb7 566 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
fcb35beb
VZ
567 return M_FONTDATA->m_macFontSize;
568}
569
facd6764 570wxByte wxFont::MacGetFontStyle() const
fcb35beb 571{
77eddfb7 572 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
fcb35beb
VZ
573 return M_FONTDATA->m_macFontStyle;
574}
575
facd6764 576wxUint32 wxFont::MacGetATSUFontID() const
fcb35beb 577{
77eddfb7 578 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
fcb35beb
VZ
579 return M_FONTDATA->m_macATSUFontID;
580}
581
3e527073
SC
582void* wxFont::MacGetATSUStyle() const
583{
584 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
585 return M_FONTDATA->m_macATSUStyle;
586}
587
facd6764
SC
588wxUint32 wxFont::MacGetATSUAdditionalQDStyles() const
589{
77eddfb7 590 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
facd6764
SC
591 return M_FONTDATA->m_macATSUAdditionalQDStyles;
592}
593
594wxUint16 wxFont::MacGetThemeFontID() const
595{
77eddfb7 596 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
facd6764
SC
597 return M_FONTDATA->m_macThemeFontID;
598}
599
600
3bf5a59b
VZ
601const wxNativeFontInfo *wxFont::GetNativeFontInfo() const
602{
facd6764 603 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
3bf5a59b
VZ
604 wxCHECK_MSG( Ok(), NULL, wxT("invalid font") );
605
606 M_FONTDATA->m_info.InitFromFont(*this);
607
608 return &(M_FONTDATA->m_info);
609}
610