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