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