]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/font.cpp
Updated list of subprojects.
[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 ;
4913272f 223#ifndef __LP64__
2a0155df 224 m_macFontFamily = FMGetFontFamilyFromName( qdFontName );
4913272f 225#endif
e40298d5
JS
226 }
227 else
228 {
df91131c 229 if ( m_faceName.empty() )
facd6764 230 {
2a0155df 231 if ( m_family == wxDEFAULT )
facd6764 232 {
4913272f 233#ifndef __LP64__
2a0155df
SC
234 m_macFontFamily = GetAppFont();
235 FMGetFontFamilyName(m_macFontFamily,qdFontName);
236 m_faceName = wxMacMakeStringFromPascal( qdFontName );
4913272f 237#endif
2a0155df
SC
238 }
239 else
240 {
241 switch ( m_family )
242 {
243 case wxSCRIPT :
244 case wxROMAN :
245 case wxDECORATIVE :
246 m_faceName = wxT("Times");
247 break ;
248
249 case wxSWISS :
250 m_faceName = wxT("Lucida Grande");
251 break ;
252
253 case wxMODERN :
254 m_faceName = wxT("Monaco");
255 break ;
256
257 default:
258 m_faceName = wxT("Times");
259 break ;
260 }
4913272f 261#ifndef __LP64__
2a0155df
SC
262 wxMacStringToPascal( m_faceName , qdFontName );
263 m_macFontFamily = FMGetFontFamilyFromName( qdFontName );
4913272f 264#endif
facd6764 265 }
facd6764 266 }
e40298d5
JS
267 else
268 {
4913272f 269#ifndef __LP64__
facd6764 270 if ( m_faceName == wxT("systemfont") )
2a0155df 271 m_macFontFamily = GetSysFont();
facd6764 272 else if ( m_faceName == wxT("applicationfont") )
2a0155df 273 m_macFontFamily = GetAppFont();
facd6764 274 else
4913272f
SC
275#else
276 if ( m_faceName == wxT("systemfont") )
277 m_faceName = wxT("Lucida Grande");
278 else if ( m_faceName == wxT("applicationfont") )
279 m_faceName = wxT("Lucida Grande");
280#endif
facd6764 281 {
2a0155df
SC
282 wxMacCFStringHolder cf( m_faceName, wxLocale::GetSystemEncoding() );
283 ATSFontFamilyRef atsfamily = ATSFontFamilyFindFromName( cf , kATSOptionFlagsDefault );
45d5159f 284 wxASSERT_MSG( atsfamily != (ATSFontFamilyRef) -1 , wxT("ATSFontFamilyFindFromName failed") );
2a0155df 285 m_macFontFamily = FMGetFontFamilyFromATSFontFamilyRef( atsfamily );
facd6764 286 }
e40298d5 287 }
facd6764
SC
288
289 m_macFontStyle = 0;
290 if (m_weight == wxBOLD)
291 m_macFontStyle |= bold;
6eaa4426 292 if (m_style == wxITALIC || m_style == wxSLANT)
facd6764 293 m_macFontStyle |= italic;
6eaa4426 294 if (m_underlined)
facd6764
SC
295 m_macFontStyle |= underline;
296 m_macFontSize = m_pointSize ;
e40298d5
JS
297 }
298
facd6764 299 // we try to get as much styles as possible into ATSU
3e527073 300
6eaa4426 301
2a0155df
SC
302 // ATSUFontID and FMFont are equivalent
303 FMFontStyle intrinsicStyle = 0 ;
4913272f 304#ifndef __LP64__
2a0155df
SC
305 status = FMGetFontFromFontFamilyInstance( m_macFontFamily , m_macFontStyle , &m_macATSUFontID , &intrinsicStyle);
306 wxASSERT_MSG( status == noErr , wxT("couldn't get an ATSUFont from font family") );
4913272f 307#endif
2a0155df 308 m_macATSUAdditionalQDStyles = m_macFontStyle & (~intrinsicStyle );
de6185e2 309
3e527073
SC
310 if ( m_macATSUStyle )
311 {
312 ::ATSUDisposeStyle((ATSUStyle)m_macATSUStyle);
313 m_macATSUStyle = NULL ;
314 }
6eaa4426 315
2a0155df
SC
316 status = ::ATSUCreateStyle((ATSUStyle *)&m_macATSUStyle);
317 wxASSERT_MSG( status == noErr , wxT("couldn't create ATSU style") );
6eaa4426 318
3e527073
SC
319 ATSUAttributeTag atsuTags[] =
320 {
2a0155df
SC
321 kATSUFontTag ,
322 kATSUSizeTag ,
323 kATSUVerticalCharacterTag,
324 kATSUQDBoldfaceTag ,
325 kATSUQDItalicTag ,
326 kATSUQDUnderlineTag ,
327 kATSUQDCondensedTag ,
328 kATSUQDExtendedTag ,
6eaa4426
DS
329 };
330 ByteCount atsuSizes[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
3e527073 331 {
2a0155df
SC
332 sizeof( ATSUFontID ) ,
333 sizeof( Fixed ) ,
334 sizeof( ATSUVerticalCharacterType),
335 sizeof( Boolean ) ,
336 sizeof( Boolean ) ,
337 sizeof( Boolean ) ,
338 sizeof( Boolean ) ,
339 sizeof( Boolean ) ,
6eaa4426
DS
340 };
341
3e527073
SC
342 Boolean kTrue = true ;
343 Boolean kFalse = false ;
344
2a0155df 345 Fixed atsuSize = IntToFixed( m_macFontSize );
3e527073 346 ATSUVerticalCharacterType kHorizontal = kATSUStronglyHorizontal;
6eaa4426 347 ATSUAttributeValuePtr atsuValues[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
3e527073
SC
348 {
349 &m_macATSUFontID ,
350 &atsuSize ,
351 &kHorizontal,
352 (m_macATSUAdditionalQDStyles & bold) ? &kTrue : &kFalse ,
353 (m_macATSUAdditionalQDStyles & italic) ? &kTrue : &kFalse ,
354 (m_macATSUAdditionalQDStyles & underline) ? &kTrue : &kFalse ,
355 (m_macATSUAdditionalQDStyles & condense) ? &kTrue : &kFalse ,
356 (m_macATSUAdditionalQDStyles & extend) ? &kTrue : &kFalse ,
6eaa4426
DS
357 };
358
359 status = ::ATSUSetAttributes(
360 (ATSUStyle)m_macATSUStyle,
361 sizeof(atsuTags) / sizeof(ATSUAttributeTag) ,
3e527073
SC
362 atsuTags, atsuSizes, atsuValues);
363
2a0155df 364 wxASSERT_MSG( status == noErr , wxT("couldn't modify ATSU style") );
519cb848
SC
365}
366
e7549107
SC
367// ----------------------------------------------------------------------------
368// wxFont
369// ----------------------------------------------------------------------------
e9576ca5 370
5b781a67
SC
371bool wxFont::Create(const wxNativeFontInfo& info)
372{
6eaa4426
DS
373 return Create(
374 info.pointSize, info.family, info.style, info.weight,
375 info.underlined, info.faceName, info.encoding );
5b781a67
SC
376}
377
3b7e6277
GD
378wxFont::wxFont(const wxString& fontdesc)
379{
380 wxNativeFontInfo info;
381 if ( info.FromString(fontdesc) )
382 (void)Create(info);
383}
384
e7549107 385bool wxFont::Create(int pointSize,
6eaa4426
DS
386 int family,
387 int style,
388 int weight,
389 bool underlined,
390 const wxString& faceName,
391 wxFontEncoding encoding)
e9576ca5
SC
392{
393 UnRef();
6eaa4426
DS
394
395 m_refData = new wxFontRefData(
396 pointSize, family, style, weight,
397 underlined, faceName, encoding);
e9576ca5
SC
398
399 RealizeResource();
400
6eaa4426 401 return true;
e9576ca5
SC
402}
403
6eaa4426 404bool wxFont::MacCreateThemeFont(wxUint16 themeFontID)
facd6764
SC
405{
406 UnRef();
6eaa4426
DS
407
408 m_refData = new wxFontRefData(
409 12, wxDEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL,
410 false, wxEmptyString, wxFONTENCODING_DEFAULT );
411
facd6764
SC
412 M_FONTDATA->m_macThemeFontID = themeFontID ;
413 RealizeResource();
414
6eaa4426 415 return true;
facd6764
SC
416}
417
e9576ca5
SC
418wxFont::~wxFont()
419{
e9576ca5
SC
420}
421
422bool wxFont::RealizeResource()
423{
2a0155df 424 M_FONTDATA->MacFindFont();
6eaa4426
DS
425
426 return true;
e9576ca5
SC
427}
428
51abe921
SC
429void wxFont::SetEncoding(wxFontEncoding encoding)
430{
431 Unshare();
432
433 M_FONTDATA->m_encoding = encoding;
434
435 RealizeResource();
436}
437
e9576ca5
SC
438void wxFont::Unshare()
439{
e40298d5
JS
440 // Don't change shared data
441 if (!m_refData)
e9576ca5 442 {
e40298d5
JS
443 m_refData = new wxFontRefData();
444 }
e9576ca5
SC
445 else
446 {
e40298d5
JS
447 wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData);
448 UnRef();
449 m_refData = ref;
450 }
e9576ca5
SC
451}
452
453void wxFont::SetPointSize(int pointSize)
454{
455 Unshare();
456
457 M_FONTDATA->m_pointSize = pointSize;
458
459 RealizeResource();
460}
461
462void wxFont::SetFamily(int family)
463{
464 Unshare();
465
466 M_FONTDATA->m_family = family;
467
468 RealizeResource();
469}
470
471void wxFont::SetStyle(int style)
472{
473 Unshare();
474
475 M_FONTDATA->m_style = style;
476
477 RealizeResource();
478}
479
480void wxFont::SetWeight(int weight)
481{
482 Unshare();
483
484 M_FONTDATA->m_weight = weight;
485
486 RealizeResource();
487}
488
85ab460e 489bool wxFont::SetFaceName(const wxString& faceName)
e9576ca5
SC
490{
491 Unshare();
492
493 M_FONTDATA->m_faceName = faceName;
494
495 RealizeResource();
85ab460e
VZ
496
497 return wxFontBase::SetFaceName(faceName);
e9576ca5
SC
498}
499
500void wxFont::SetUnderlined(bool underlined)
501{
502 Unshare();
503
504 M_FONTDATA->m_underlined = underlined;
505
506 RealizeResource();
507}
508
ac17f9b1
SC
509void wxFont::SetNoAntiAliasing( bool no )
510{
511 Unshare();
512
513 M_FONTDATA->SetNoAntiAliasing( no );
514
515 RealizeResource();
516}
517
e7549107
SC
518// ----------------------------------------------------------------------------
519// accessors
520// ----------------------------------------------------------------------------
521
fcb35beb
VZ
522// TODO: insert checks everywhere for M_FONTDATA == NULL!
523
e7549107 524int wxFont::GetPointSize() const
e9576ca5 525{
77eddfb7 526 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
6eaa4426 527
e7549107 528 return M_FONTDATA->m_pointSize;
e9576ca5
SC
529}
530
e7549107 531int wxFont::GetFamily() const
e9576ca5 532{
77eddfb7 533 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
6eaa4426 534
e7549107 535 return M_FONTDATA->m_family;
e9576ca5
SC
536}
537
e7549107 538int wxFont::GetStyle() const
e9576ca5 539{
77eddfb7 540 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
6eaa4426 541
e7549107
SC
542 return M_FONTDATA->m_style;
543}
544
545int wxFont::GetWeight() const
546{
77eddfb7 547 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
6eaa4426 548
e7549107
SC
549 return M_FONTDATA->m_weight;
550}
551
552bool wxFont::GetUnderlined() const
553{
77eddfb7 554 wxCHECK_MSG( M_FONTDATA != NULL , false, wxT("invalid font") );
6eaa4426 555
e7549107
SC
556 return M_FONTDATA->m_underlined;
557}
558
559wxString wxFont::GetFaceName() const
560{
facd6764 561 wxCHECK_MSG( M_FONTDATA != NULL , wxEmptyString , wxT("invalid font") );
6eaa4426 562
facd6764 563 return M_FONTDATA->m_faceName;
e7549107
SC
564}
565
566wxFontEncoding wxFont::GetEncoding() const
567{
facd6764 568 wxCHECK_MSG( M_FONTDATA != NULL , wxFONTENCODING_DEFAULT , wxT("invalid font") );
6eaa4426 569
e7549107 570 return M_FONTDATA->m_encoding;
e9576ca5
SC
571}
572
5ac2e80c 573bool wxFont::GetNoAntiAliasing() const
ac17f9b1 574{
77eddfb7 575 wxCHECK_MSG( M_FONTDATA != NULL , false, wxT("invalid font") );
6eaa4426 576
ac17f9b1
SC
577 return M_FONTDATA->m_noAA;
578}
579
facd6764 580short wxFont::MacGetFontNum() const
fcb35beb 581{
77eddfb7 582 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
6eaa4426 583
2a0155df 584 return M_FONTDATA->m_macFontFamily;
fcb35beb
VZ
585}
586
facd6764 587short wxFont::MacGetFontSize() const
fcb35beb 588{
77eddfb7 589 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
6eaa4426 590
fcb35beb
VZ
591 return M_FONTDATA->m_macFontSize;
592}
593
facd6764 594wxByte wxFont::MacGetFontStyle() const
fcb35beb 595{
77eddfb7 596 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
6eaa4426 597
fcb35beb
VZ
598 return M_FONTDATA->m_macFontStyle;
599}
600
facd6764 601wxUint32 wxFont::MacGetATSUFontID() const
fcb35beb 602{
77eddfb7 603 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
6eaa4426 604
fcb35beb
VZ
605 return M_FONTDATA->m_macATSUFontID;
606}
607
6eaa4426 608void * wxFont::MacGetATSUStyle() const
3e527073
SC
609{
610 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
6eaa4426 611
3e527073
SC
612 return M_FONTDATA->m_macATSUStyle;
613}
614
facd6764
SC
615wxUint32 wxFont::MacGetATSUAdditionalQDStyles() const
616{
77eddfb7 617 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
6eaa4426 618
facd6764
SC
619 return M_FONTDATA->m_macATSUAdditionalQDStyles;
620}
621
6eaa4426 622wxUint16 wxFont::MacGetThemeFontID() const
facd6764 623{
77eddfb7 624 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
6eaa4426 625
facd6764
SC
626 return M_FONTDATA->m_macThemeFontID;
627}
628
6eaa4426 629const wxNativeFontInfo * wxFont::GetNativeFontInfo() const
3bf5a59b 630{
facd6764 631 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
3bf5a59b
VZ
632 wxCHECK_MSG( Ok(), NULL, wxT("invalid font") );
633
634 M_FONTDATA->m_info.InitFromFont(*this);
635
636 return &(M_FONTDATA->m_info);
637}