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