]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/osx/carbon/font.cpp
Significantly improved wxPropertyGrid's top-level parent change detection code (fixes...
[wxWidgets.git] / src / osx / carbon / font.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/osx/carbon/font.cpp
3// Purpose: wxFont class
4// Author: Stefan Csomor
5// Modified by:
6// Created: 1998-01-01
7// RCS-ID: $Id$
8// Copyright: (c) Stefan Csomor
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12#include "wx/wxprec.h"
13
14#include "wx/font.h"
15
16#ifndef WX_PRECOMP
17 #include "wx/string.h"
18 #include "wx/utils.h"
19 #include "wx/intl.h"
20 #include "wx/gdicmn.h"
21 #include "wx/log.h"
22#endif
23
24#include "wx/fontutil.h"
25#include "wx/graphics.h"
26#include "wx/settings.h"
27#include "wx/tokenzr.h"
28
29#include "wx/osx/private.h"
30
31#if wxOSX_USE_ATSU_TEXT && !wxOSX_USE_CARBON
32// include themeing support
33#include <Carbon/Carbon.h>
34#endif
35
36#include <map>
37#include <string>
38
39IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
40
41class WXDLLEXPORT wxFontRefData: public wxGDIRefData
42{
43public:
44
45 wxFontRefData()
46 {
47 Init();
48 m_info.Init(10, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL,
49 false, wxEmptyString, wxFONTENCODING_DEFAULT);
50 }
51
52 wxFontRefData(const wxFontRefData& data);
53
54 wxFontRefData( const wxNativeFontInfo& info ) : m_info(info)
55 {
56 Init();
57 }
58
59 wxFontRefData(wxOSXSystemFont font, int size);
60
61#if wxOSX_USE_CORE_TEXT
62 wxFontRefData( wxUint32 coreTextFontType );
63 wxFontRefData( CTFontRef font );
64 wxFontRefData( CTFontDescriptorRef fontdescriptor, int size );
65#endif
66
67 virtual ~wxFontRefData();
68
69 void SetNoAntiAliasing( bool no = true ) { m_noAA = no; }
70
71 bool GetNoAntiAliasing() const { return m_noAA; }
72
73 void SetPointSize( int size )
74 {
75 if( GetPointSize() != size )
76 {
77 m_info.SetPointSize(size);
78 Free();
79 }
80 }
81
82 int GetPointSize() const { return m_info.GetPointSize(); }
83
84 void SetFamily( wxFontFamily family )
85 {
86 if ( m_info.m_family != family )
87 {
88 m_info.SetFamily( family );
89 Free();
90 }
91 }
92
93 wxFontFamily GetFamily() const { return m_info.GetFamily(); }
94
95 void SetStyle( wxFontStyle style )
96 {
97 if ( m_info.m_style != style )
98 {
99 m_info.SetStyle( style );
100 Free();
101 }
102 }
103
104
105 wxFontStyle GetStyle() const { return m_info.GetStyle(); }
106
107 void SetWeight( wxFontWeight weight )
108 {
109 if ( m_info.m_weight != weight )
110 {
111 m_info.SetWeight( weight );
112 Free();
113 }
114 }
115
116
117 wxFontWeight GetWeight() const { return m_info.GetWeight(); }
118
119 void SetUnderlined( bool u )
120 {
121 if ( m_info.m_underlined != u )
122 {
123 m_info.SetUnderlined( u );
124 Free();
125 }
126 }
127
128 bool GetUnderlined() const { return m_info.GetUnderlined(); }
129
130 void SetFaceName( const wxString& facename )
131 {
132 if ( m_info.m_faceName != facename )
133 {
134 m_info.SetFaceName( facename );
135 Free();
136 }
137 }
138
139 wxString GetFaceName() const { return m_info.GetFaceName(); }
140
141 void SetEncoding( wxFontEncoding encoding )
142 {
143 if ( m_info.m_encoding != encoding )
144 {
145 m_info.SetEncoding( encoding );
146 Free();
147 }
148 }
149
150 wxFontEncoding GetEncoding() const { return m_info.GetEncoding(); }
151
152 void Free();
153
154 void MacFindFont();
155
156protected:
157 // common part of all ctors
158 void Init();
159#if wxOSX_USE_CORE_TEXT
160 // void Init( CTFontRef font );
161#endif
162 bool m_noAA; // No anti-aliasing
163public:
164 bool m_fontValid;
165#if wxOSX_USE_CARBON && wxOSX_USE_ATSU_TEXT
166 // for true themeing support we must store the correct font
167 // information here, as this speeds up and optimizes rendering
168 ThemeFontID m_macThemeFontID ;
169#endif
170#if wxOSX_USE_CORE_TEXT
171 wxCFRef<CTFontRef> m_ctFont;
172#endif
173#if wxOSX_USE_ATSU_TEXT
174 ATSUStyle m_macATSUStyle ;
175#endif
176 wxCFRef<CGFontRef> m_cgFont;
177#if wxOSX_USE_COCOA
178 WX_NSFont m_nsFont;
179#endif
180#if wxOSX_USE_IPHONE
181 WX_UIFont m_uiFont;
182#endif
183 wxNativeFontInfo m_info;
184};
185
186#define M_FONTDATA ((wxFontRefData*)m_refData)
187
188wxFontRefData::wxFontRefData(const wxFontRefData& data)
189{
190 Init();
191 m_info = data.m_info;
192 m_noAA = data.m_noAA;
193 m_fontValid = data.m_fontValid;
194#if wxOSX_USE_CARBON && wxOSX_USE_ATSU_TEXT
195 m_macThemeFontID = data.m_macThemeFontID;
196#endif
197#if wxOSX_USE_CORE_TEXT
198 m_ctFont = data.m_ctFont;
199#endif
200 m_cgFont = data.m_cgFont;
201#if wxOSX_USE_ATSU_TEXT
202 if ( data.m_macATSUStyle != NULL )
203 {
204 ATSUCreateStyle(&m_macATSUStyle) ;
205 ATSUCopyAttributes(data.m_macATSUStyle, m_macATSUStyle);
206 }
207#endif
208#if wxOSX_USE_COCOA
209 m_nsFont = (NSFont*) wxMacCocoaRetain(data.m_nsFont);
210#endif
211#if wxOSX_USE_IPHONE
212 m_uiFont = (UIFont*) wxMacCocoaRetain(data.m_uiFont);
213#endif
214
215}
216
217// ============================================================================
218// implementation
219// ============================================================================
220
221// ----------------------------------------------------------------------------
222// wxFontRefData
223// ----------------------------------------------------------------------------
224
225void wxFontRefData::Init()
226{
227 m_noAA = false;
228#if wxOSX_USE_CARBON && wxOSX_USE_ATSU_TEXT
229 m_macThemeFontID = kThemeCurrentPortFont ;
230#endif
231#if wxOSX_USE_ATSU_TEXT
232 m_macATSUStyle = NULL ;
233#endif
234#if wxOSX_USE_COCOA
235 m_nsFont = NULL;
236#endif
237#if wxOSX_USE_IPHONE
238 m_uiFont = NULL;
239#endif
240 m_fontValid = false;
241}
242
243wxFontRefData::~wxFontRefData()
244{
245 Free();
246}
247
248void wxFontRefData::Free()
249{
250#if wxOSX_USE_CORE_TEXT
251 m_ctFont.reset();
252#endif
253 m_cgFont.reset();
254#if wxOSX_USE_ATSU_TEXT
255#if wxOSX_USE_CARBON
256 m_macThemeFontID = kThemeCurrentPortFont ;
257#endif
258 if ( m_macATSUStyle )
259 {
260 ::ATSUDisposeStyle((ATSUStyle)m_macATSUStyle);
261 m_macATSUStyle = NULL ;
262 }
263#endif
264#if wxOSX_USE_COCOA
265 if (m_nsFont != NULL)
266 {
267 wxMacCocoaRelease(m_nsFont);
268 m_nsFont = NULL;
269 }
270#endif
271#if wxOSX_USE_IPHONE
272 if (m_uiFont != NULL)
273 {
274 wxMacCocoaRelease(m_uiFont);
275 m_uiFont = NULL;
276 }
277#endif
278 m_fontValid = false;
279}
280
281wxFontRefData::wxFontRefData(wxOSXSystemFont font, int size)
282{
283 wxASSERT( font != wxOSX_SYSTEM_FONT_NONE );
284 Init();
285
286#if wxOSX_USE_CORE_TEXT
287 if ( UMAGetSystemVersion() >= 0x1050 )
288 {
289 CTFontUIFontType uifont = kCTFontSystemFontType;
290 switch( font )
291 {
292 case wxOSX_SYSTEM_FONT_NORMAL:
293 uifont = kCTFontSystemFontType;
294 break;
295 case wxOSX_SYSTEM_FONT_BOLD:
296 uifont = kCTFontEmphasizedSystemFontType;
297 break;
298 case wxOSX_SYSTEM_FONT_SMALL:
299 uifont = kCTFontSmallSystemFontType;
300 break;
301 case wxOSX_SYSTEM_FONT_SMALL_BOLD:
302 uifont = kCTFontSmallEmphasizedSystemFontType;
303 break;
304 case wxOSX_SYSTEM_FONT_MINI:
305 uifont = kCTFontMiniSystemFontType;
306 break;
307 case wxOSX_SYSTEM_FONT_MINI_BOLD:
308 uifont = kCTFontMiniEmphasizedSystemFontType;
309 break;
310 case wxOSX_SYSTEM_FONT_LABELS:
311 uifont = kCTFontLabelFontType;
312 break;
313 case wxOSX_SYSTEM_FONT_VIEWS:
314 uifont = kCTFontViewsFontType;
315 break;
316 default:
317 break;
318 }
319 m_ctFont.reset(CTFontCreateUIFontForLanguage( uifont, (CGFloat) size, NULL ));
320 wxCFRef<CTFontDescriptorRef> descr;
321 descr.reset( CTFontCopyFontDescriptor( m_ctFont ) );
322 m_info.Init(descr);
323 }
324#endif
325#if wxOSX_USE_ATSU_TEXT
326 {
327#if !wxOSX_USE_CARBON
328 // not needed outside
329 ThemeFontID m_macThemeFontID = kThemeSystemFont;
330#endif
331 switch( font )
332 {
333 case wxOSX_SYSTEM_FONT_NORMAL:
334 m_macThemeFontID = kThemeSystemFont;
335 break;
336 case wxOSX_SYSTEM_FONT_BOLD:
337 m_macThemeFontID = kThemeEmphasizedSystemFont;
338 break;
339 case wxOSX_SYSTEM_FONT_SMALL:
340 m_macThemeFontID = kThemeSmallSystemFont;
341 break;
342 case wxOSX_SYSTEM_FONT_SMALL_BOLD:
343 m_macThemeFontID = kThemeSmallEmphasizedSystemFont;
344 break;
345 case wxOSX_SYSTEM_FONT_MINI:
346 m_macThemeFontID = kThemeMiniSystemFont;
347 break;
348 case wxOSX_SYSTEM_FONT_MINI_BOLD:
349 // bold not available under themeing
350 m_macThemeFontID = kThemeMiniSystemFont;
351 break;
352 case wxOSX_SYSTEM_FONT_LABELS:
353 m_macThemeFontID = kThemeLabelFont;
354 break;
355 case wxOSX_SYSTEM_FONT_VIEWS:
356 m_macThemeFontID = kThemeViewsFont;
357 break;
358 default:
359 break;
360 }
361 if ( m_info.m_faceName.empty() )
362 {
363 Style style ;
364 FMFontSize fontSize;
365 Str255 qdFontName ;
366
367 GetThemeFont( m_macThemeFontID, GetApplicationScript(), qdFontName, &fontSize, &style );
368 if ( size != 0 )
369 fontSize = size;
370
371 wxFontStyle fontstyle = wxFONTSTYLE_NORMAL;
372 wxFontWeight fontweight = wxFONTWEIGHT_NORMAL;
373 bool underlined = false;
374
375 if ( style & bold )
376 fontweight = wxFONTWEIGHT_BOLD ;
377 else
378 fontweight = wxFONTWEIGHT_NORMAL ;
379 if ( style & italic )
380 fontstyle = wxFONTSTYLE_ITALIC ;
381 if ( style & underline )
382 underlined = true ;
383
384 m_info.Init(fontSize,wxFONTFAMILY_DEFAULT,fontstyle,fontweight,underlined,
385 wxMacMakeStringFromPascal( qdFontName ), wxFONTENCODING_DEFAULT);
386 }
387 }
388#endif
389#if wxOSX_USE_COCOA
390 m_nsFont = wxFont::OSXCreateNSFont( font, &m_info );
391#endif
392#if wxOSX_USE_IPHONE
393 m_uiFont = wxFont::OSXCreateUIFont( font, &m_info );
394#endif
395}
396
397void wxFontRefData::MacFindFont()
398{
399 if ( m_fontValid )
400 return;
401
402 m_info.EnsureValid();
403
404#if wxOSX_USE_CORE_TEXT
405 if ( UMAGetSystemVersion() >= 0x1050 )
406 {
407 CTFontSymbolicTraits traits = 0;
408
409 if (m_info.m_weight == wxFONTWEIGHT_BOLD)
410 traits |= kCTFontBoldTrait;
411 if (m_info.m_style == wxFONTSTYLE_ITALIC || m_info.m_style == wxFONTSTYLE_SLANT)
412 traits |= kCTFontItalicTrait;
413
414 // use font caching
415 wxString lookupnameWithSize = wxString::Format( "%s_%ld_%ld", m_info.m_faceName.c_str(), traits, m_info.m_pointSize );
416
417 static std::map< std::wstring , wxCFRef< CTFontRef > > fontcache ;
418 m_ctFont = fontcache[ std::wstring(lookupnameWithSize.wc_str()) ];
419 if ( !m_ctFont )
420 {
421 m_ctFont.reset( CTFontCreateWithFontDescriptor( m_info.m_ctFontDescriptor, 0/*m_pointSize */, NULL ) );
422 }
423 m_cgFont.reset(CTFontCopyGraphicsFont(m_ctFont, NULL));
424 }
425
426#endif
427#if wxOSX_USE_ATSU_TEXT
428 {
429 // we try to get as much styles as possible into ATSU
430
431 OSStatus status = ::ATSUCreateStyle(&m_macATSUStyle);
432 wxASSERT_MSG( status == noErr , wxT("couldn't create ATSU style") );
433
434 ATSUAttributeTag atsuTags[] =
435 {
436 kATSUFontTag ,
437 kATSUSizeTag ,
438 kATSUVerticalCharacterTag,
439 kATSUQDBoldfaceTag ,
440 kATSUQDItalicTag ,
441 kATSUQDUnderlineTag ,
442 kATSUQDCondensedTag ,
443 kATSUQDExtendedTag ,
444 };
445 ByteCount atsuSizes[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
446 {
447 sizeof( ATSUFontID ) ,
448 sizeof( Fixed ) ,
449 sizeof( ATSUVerticalCharacterType),
450 sizeof( Boolean ) ,
451 sizeof( Boolean ) ,
452 sizeof( Boolean ) ,
453 sizeof( Boolean ) ,
454 sizeof( Boolean ) ,
455 };
456
457 Boolean kTrue = true ;
458 Boolean kFalse = false ;
459
460 Fixed atsuSize = IntToFixed( m_info.m_pointSize );
461 ATSUVerticalCharacterType kHorizontal = kATSUStronglyHorizontal;
462 FMFontStyle addQDStyle = m_info.m_atsuAdditionalQDStyles;
463 ATSUAttributeValuePtr atsuValues[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
464 {
465 &m_info.m_atsuFontID ,
466 &atsuSize ,
467 &kHorizontal,
468 (addQDStyle & bold) ? &kTrue : &kFalse ,
469 (addQDStyle & italic) ? &kTrue : &kFalse ,
470 (addQDStyle & underline) ? &kTrue : &kFalse ,
471 (addQDStyle & condense) ? &kTrue : &kFalse ,
472 (addQDStyle & extend) ? &kTrue : &kFalse ,
473 };
474
475 status = ::ATSUSetAttributes(
476 (ATSUStyle)m_macATSUStyle,
477 sizeof(atsuTags) / sizeof(ATSUAttributeTag) ,
478 atsuTags, atsuSizes, atsuValues);
479
480 wxASSERT_MSG( status == noErr , wxT("couldn't modify ATSU style") );
481 if ( m_cgFont.get() == NULL )
482 {
483 ATSFontRef fontRef = FMGetATSFontRefFromFont(m_info.m_atsuFontID);
484 m_cgFont.reset( CGFontCreateWithPlatformFont( &fontRef ) );
485 }
486 }
487#endif
488#if wxOSX_USE_COCOA
489 m_nsFont = wxFont::OSXCreateNSFont( &m_info );
490#endif
491#if wxOSX_USE_IPHONE
492 m_uiFont = wxFont::OSXCreateUIFont( &m_info );
493#endif
494 m_fontValid = true;
495}
496
497// ----------------------------------------------------------------------------
498// wxFont
499// ----------------------------------------------------------------------------
500
501bool wxFont::Create(const wxNativeFontInfo& info)
502{
503 UnRef();
504
505 m_refData = new wxFontRefData( info );
506 RealizeResource();
507
508 return true;
509}
510
511wxFont::wxFont(const wxString& fontdesc)
512{
513 wxNativeFontInfo info;
514 if ( info.FromString(fontdesc) )
515 (void)Create(info);
516}
517
518bool wxFont::Create(int pointSize,
519 wxFontFamily family,
520 wxFontStyle style,
521 wxFontWeight weight,
522 bool underlined,
523 const wxString& faceNameParam,
524 wxFontEncoding encoding)
525{
526 UnRef();
527
528 wxString faceName = faceNameParam;
529
530 if ( faceName.empty() )
531 {
532 switch ( family )
533 {
534 case wxFONTFAMILY_DEFAULT :
535 faceName = wxT("Lucida Grande");
536 break;
537
538 case wxFONTFAMILY_SCRIPT :
539 case wxFONTFAMILY_ROMAN :
540 case wxFONTFAMILY_DECORATIVE :
541 faceName = wxT("Times");
542 break ;
543
544 case wxFONTFAMILY_SWISS :
545 faceName = wxT("Helvetica");
546 break ;
547
548 case wxFONTFAMILY_MODERN :
549 case wxFONTFAMILY_TELETYPE:
550 faceName = wxT("Courier");
551 break ;
552
553 default:
554 faceName = wxT("Times");
555 break ;
556 }
557 }
558
559 wxNativeFontInfo info;
560
561 info.Init(pointSize, family, style, weight,
562 underlined, faceName, encoding);
563
564 m_refData = new wxFontRefData(info);
565
566 return true;
567}
568
569bool wxFont::CreateSystemFont(wxOSXSystemFont font)
570{
571 UnRef();
572
573 m_refData = new wxFontRefData( font, 0 );
574
575 return true;
576}
577
578wxFont::~wxFont()
579{
580}
581
582void wxFont::DoSetNativeFontInfo(const wxNativeFontInfo& info)
583{
584 UnRef();
585
586 m_refData = new wxFontRefData( info);
587}
588
589
590bool wxFont::RealizeResource()
591{
592 M_FONTDATA->MacFindFont();
593
594 return true;
595}
596
597void wxFont::SetEncoding(wxFontEncoding encoding)
598{
599 AllocExclusive();
600
601 M_FONTDATA->SetEncoding( encoding );
602}
603
604wxGDIRefData *wxFont::CreateGDIRefData() const
605{
606 return new wxFontRefData;
607}
608
609wxGDIRefData *wxFont::CloneGDIRefData(const wxGDIRefData *data) const
610{
611 return new wxFontRefData(*static_cast<const wxFontRefData *>(data));
612}
613
614void wxFont::SetPointSize(int pointSize)
615{
616 if ( M_FONTDATA->GetPointSize() == pointSize )
617 return;
618
619 AllocExclusive();
620
621 M_FONTDATA->SetPointSize( pointSize );
622}
623
624void wxFont::SetFamily(wxFontFamily family)
625{
626 AllocExclusive();
627
628 M_FONTDATA->SetFamily( family );
629}
630
631void wxFont::SetStyle(wxFontStyle style)
632{
633 AllocExclusive();
634
635 M_FONTDATA->SetStyle( style );
636}
637
638void wxFont::SetWeight(wxFontWeight weight)
639{
640 AllocExclusive();
641
642 M_FONTDATA->SetWeight( weight );
643}
644
645bool wxFont::SetFaceName(const wxString& faceName)
646{
647 AllocExclusive();
648
649 M_FONTDATA->SetFaceName( faceName );
650
651 return wxFontBase::SetFaceName(faceName);
652}
653
654void wxFont::SetUnderlined(bool underlined)
655{
656 AllocExclusive();
657
658 M_FONTDATA->SetUnderlined( underlined );
659}
660
661void wxFont::SetNoAntiAliasing( bool no )
662{
663 AllocExclusive();
664
665 M_FONTDATA->SetNoAntiAliasing( no );
666}
667
668// ----------------------------------------------------------------------------
669// accessors
670// ----------------------------------------------------------------------------
671
672// TODO: insert checks everywhere for M_FONTDATA == NULL!
673
674int wxFont::GetPointSize() const
675{
676 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
677
678 return M_FONTDATA->GetPointSize();
679}
680
681wxSize wxFont::GetPixelSize() const
682{
683#if wxUSE_GRAPHICS_CONTEXT
684 // TODO: consider caching the value
685 wxGraphicsContext* dc = wxGraphicsContext::CreateFromNative((CGContextRef) NULL);
686 dc->SetFont(*(wxFont *)this,*wxBLACK);
687 wxDouble width, height = 0;
688 dc->GetTextExtent( wxT("g"), &width, &height, NULL, NULL);
689 delete dc;
690 return wxSize((int)width, (int)height);
691#else
692 return wxFontBase::GetPixelSize();
693#endif
694}
695
696wxFontFamily wxFont::GetFamily() const
697{
698 wxCHECK_MSG( M_FONTDATA != NULL , wxFONTFAMILY_MAX, wxT("invalid font") );
699
700 return M_FONTDATA->GetFamily();
701}
702
703wxFontStyle wxFont::GetStyle() const
704{
705 wxCHECK_MSG( M_FONTDATA != NULL , wxFONTSTYLE_MAX, wxT("invalid font") );
706
707 return M_FONTDATA->GetStyle() ;
708}
709
710wxFontWeight wxFont::GetWeight() const
711{
712 wxCHECK_MSG( M_FONTDATA != NULL , wxFONTWEIGHT_MAX, wxT("invalid font") );
713
714 return M_FONTDATA->GetWeight();
715}
716
717bool wxFont::GetUnderlined() const
718{
719 wxCHECK_MSG( M_FONTDATA != NULL , false, wxT("invalid font") );
720
721 return M_FONTDATA->GetUnderlined();
722}
723
724wxString wxFont::GetFaceName() const
725{
726 wxCHECK_MSG( M_FONTDATA != NULL , wxEmptyString , wxT("invalid font") );
727
728 return M_FONTDATA->GetFaceName() ;
729}
730
731wxFontEncoding wxFont::GetEncoding() const
732{
733 wxCHECK_MSG( M_FONTDATA != NULL , wxFONTENCODING_DEFAULT , wxT("invalid font") );
734
735 return M_FONTDATA->GetEncoding() ;
736}
737
738bool wxFont::GetNoAntiAliasing() const
739{
740 wxCHECK_MSG( M_FONTDATA != NULL , false, wxT("invalid font") );
741
742 return M_FONTDATA->GetNoAntiAliasing();
743}
744
745#if wxOSX_USE_ATSU_TEXT && wxOSX_USE_CARBON
746
747short wxFont::MacGetFontNum() const
748{
749 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
750
751 // cast away constness otherwise lazy font resolution is not possible
752 const_cast<wxFont *>(this)->RealizeResource();
753
754 return M_FONTDATA->m_info.m_qdFontFamily;
755}
756
757wxByte wxFont::MacGetFontStyle() const
758{
759 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
760
761 // cast away constness otherwise lazy font resolution is not possible
762 const_cast<wxFont *>(this)->RealizeResource();
763
764 return M_FONTDATA->m_info.m_qdFontStyle;
765}
766
767wxUint16 wxFont::MacGetThemeFontID() const
768{
769 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
770
771 return M_FONTDATA->m_macThemeFontID;
772}
773
774#endif
775
776#if wxOSX_USE_ATSU_TEXT
777void * wxFont::MacGetATSUStyle() const
778{
779 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
780
781 // cast away constness otherwise lazy font resolution is not possible
782 const_cast<wxFont *>(this)->RealizeResource();
783
784 return M_FONTDATA->m_macATSUStyle;
785}
786
787#if WXWIN_COMPATIBILITY_2_8
788wxUint32 wxFont::MacGetATSUFontID() const
789{
790 wxCHECK_MSG( M_FONTDATA != NULL, 0, wxT("invalid font") );
791
792 // cast away constness otherwise lazy font resolution is not possible
793 const_cast<wxFont *>(this)->RealizeResource();
794
795 return M_FONTDATA->m_info.m_atsuFontID;
796}
797
798wxUint32 wxFont::MacGetATSUAdditionalQDStyles() const
799{
800 wxCHECK_MSG( M_FONTDATA != NULL, 0, wxT("invalid font") );
801
802 // cast away constness otherwise lazy font resolution is not possible
803 const_cast<wxFont *>(this)->RealizeResource();
804
805 return M_FONTDATA->m_info.m_atsuAdditionalQDStyles;
806}
807#endif
808
809#endif
810
811#if wxOSX_USE_CORE_TEXT
812
813CTFontRef wxFont::OSXGetCTFont() const
814{
815 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
816
817 // cast away constness otherwise lazy font resolution is not possible
818 const_cast<wxFont *>(this)->RealizeResource();
819
820 return (CTFontRef)(M_FONTDATA->m_ctFont);
821}
822
823#endif
824
825#if wxOSX_USE_COCOA_OR_CARBON
826
827CGFontRef wxFont::OSXGetCGFont() const
828{
829 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
830
831 // cast away constness otherwise lazy font resolution is not possible
832 const_cast<wxFont *>(this)->RealizeResource();
833
834 return (M_FONTDATA->m_cgFont);
835}
836
837#endif
838
839
840#if wxOSX_USE_COCOA
841
842NSFont* wxFont::OSXGetNSFont() const
843{
844 wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
845
846 // cast away constness otherwise lazy font resolution is not possible
847 const_cast<wxFont *>(this)->RealizeResource();
848
849 return (M_FONTDATA->m_nsFont);
850}
851
852#endif
853
854const wxNativeFontInfo * wxFont::GetNativeFontInfo() const
855{
856 wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
857 wxCHECK_MSG( Ok(), NULL, wxT("invalid font") );
858
859 // cast away constness otherwise lazy font resolution is not possible
860 const_cast<wxFont *>(this)->RealizeResource();
861
862 // M_FONTDATA->m_info.InitFromFont(*this);
863
864 return &(M_FONTDATA->m_info);
865}
866
867// ----------------------------------------------------------------------------
868// wxNativeFontInfo
869// ----------------------------------------------------------------------------
870
871#if wxOSX_USE_CORE_TEXT
872
873/* from Core Text Manual Common Operations */
874
875static CTFontDescriptorRef wxMacCreateCTFontDescriptor(CFStringRef iFamilyName, CTFontSymbolicTraits iTraits )
876{
877 CTFontDescriptorRef descriptor = NULL;
878 CFMutableDictionaryRef attributes;
879
880 assert(iFamilyName != NULL);
881 // Create a mutable dictionary to hold our attributes.
882 attributes = CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
883 &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
884 check(attributes != NULL);
885
886 if (attributes != NULL) {
887 // Add a family name to our attributes.
888 CFDictionaryAddValue(attributes, kCTFontFamilyNameAttribute, iFamilyName);
889
890
891 if ( iTraits ) {
892 CFMutableDictionaryRef traits;
893 CFNumberRef symTraits;
894
895 // Create the traits dictionary.
896 symTraits = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type,
897 &iTraits);
898 check(symTraits != NULL);
899
900 if (symTraits != NULL) {
901 // Create a dictionary to hold our traits values.
902 traits = CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
903 &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
904 check(traits != NULL);
905
906 if (traits != NULL) {
907 // Add the symbolic traits value to the traits dictionary.
908 CFDictionaryAddValue(traits, kCTFontSymbolicTrait, symTraits);
909
910 // Add the traits attribute to our attributes.
911 CFDictionaryAddValue(attributes, kCTFontTraitsAttribute, traits);
912 CFRelease(traits);
913 }
914 CFRelease(symTraits);
915 }
916 }
917 // Create the font descriptor with our attributes
918 descriptor = CTFontDescriptorCreateWithAttributes(attributes);
919 check(descriptor != NULL);
920
921 CFRelease(attributes);
922 }
923 // Return our font descriptor.
924 return descriptor ;
925}
926
927#endif
928
929void wxNativeFontInfo::Init()
930{
931#if wxOSX_USE_CORE_TEXT
932 m_ctFontDescriptor = NULL;
933#endif
934#if wxOSX_USE_ATSU_TEXT
935 m_atsuFontID = 0 ;
936 m_atsuAdditionalQDStyles = 0;
937 m_atsuFontValid = false;
938#if wxOSX_USE_CARBON
939 m_qdFontStyle = 0;
940 m_qdFontFamily = 0;
941#endif
942#endif
943#if wxOSX_USE_COCOA
944 m_nsFontDescriptor = NULL;
945#endif
946 m_pointSize = 0;
947 m_family = wxFONTFAMILY_DEFAULT;
948 m_style = wxFONTSTYLE_NORMAL;
949 m_weight = wxFONTWEIGHT_NORMAL;
950 m_underlined = false;
951 m_faceName.clear();
952 m_encoding = wxFont::GetDefaultEncoding();
953 m_descriptorValid = false;
954}
955
956#if wxOSX_USE_CORE_TEXT
957void wxNativeFontInfo::Init(CTFontDescriptorRef descr)
958{
959 Init();
960 m_ctFontDescriptor = wxCFRetain(descr);
961
962 wxCFRef< CFNumberRef > sizevalue( (CFNumberRef) CTFontDescriptorCopyAttribute( m_ctFontDescriptor, kCTFontSizeAttribute ) );
963 float fsize;
964 if ( CFNumberGetValue( sizevalue , kCFNumberFloatType , &fsize ) )
965 m_pointSize = (int)( fsize + 0.5 );
966
967 wxCFRef< CFDictionaryRef > traitsvalue( (CFDictionaryRef) CTFontDescriptorCopyAttribute( m_ctFontDescriptor, kCTFontTraitsAttribute ) );
968 CTFontSymbolicTraits traits;
969 if ( CFNumberGetValue((CFNumberRef) CFDictionaryGetValue(traitsvalue,kCTFontSymbolicTrait),kCFNumberIntType,&traits) )
970 {
971 if ( traits & kCTFontItalicTrait )
972 m_style = wxFONTSTYLE_ITALIC;
973 if ( traits & kCTFontBoldTrait )
974 m_weight = wxFONTWEIGHT_BOLD ;
975 }
976
977 wxCFStringRef familyName( (CFStringRef) CTFontDescriptorCopyAttribute(m_ctFontDescriptor, kCTFontFamilyNameAttribute));
978 m_faceName = familyName.AsString();
979}
980#endif
981
982void wxNativeFontInfo::EnsureValid()
983{
984 if ( m_descriptorValid )
985 return;
986
987#if wxOSX_USE_CORE_TEXT
988 if ( m_ctFontDescriptor == NULL && UMAGetSystemVersion() >= 0x1050 )
989 {
990 CTFontSymbolicTraits traits = 0;
991
992 if (m_weight == wxFONTWEIGHT_BOLD)
993 traits |= kCTFontBoldTrait;
994 if (m_style == wxFONTSTYLE_ITALIC || m_style == wxFONTSTYLE_SLANT)
995 traits |= kCTFontItalicTrait;
996
997 // use font caching
998 wxString lookupnameWithSize = wxString::Format( "%s_%ld_%ld", m_faceName.c_str(), traits, m_pointSize );
999
1000 static std::map< std::wstring , wxCFRef< CTFontDescriptorRef > > fontdescriptorcache ;
1001 m_ctFontDescriptor = wxCFRetain((CTFontDescriptorRef)fontdescriptorcache[ std::wstring(lookupnameWithSize.wc_str()) ]);
1002 if ( !m_ctFontDescriptor )
1003 {
1004 // QD selection algorithm is the fastest by orders of magnitude on 10.5
1005 if ( m_faceName.IsAscii() )
1006 {
1007 uint8_t qdstyle = 0;
1008 if (m_weight == wxFONTWEIGHT_BOLD)
1009 qdstyle |= bold;
1010 if (m_style == wxFONTSTYLE_ITALIC || m_style == wxFONTSTYLE_SLANT)
1011 qdstyle |= italic;
1012
1013 Str255 qdFontName ;
1014 wxMacStringToPascal( m_faceName , qdFontName );
1015 wxCFRef< CTFontRef > font;
1016 font.reset( CTFontCreateWithQuickdrawInstance(qdFontName, 0 , qdstyle, m_pointSize) );
1017 m_ctFontDescriptor = CTFontCopyFontDescriptor(font);
1018 }
1019 else
1020 {
1021 m_ctFontDescriptor = wxMacCreateCTFontDescriptor( wxCFStringRef(m_faceName),traits );
1022 }
1023 fontdescriptorcache[ std::wstring(lookupnameWithSize.wc_str()) ].reset(wxCFRetain(m_ctFontDescriptor));
1024 }
1025 }
1026#endif
1027#if wxOSX_USE_ATSU_TEXT
1028 if ( !m_atsuFontValid )
1029 {
1030#if !wxOSX_USE_CARBON
1031 // not needed outside
1032 wxInt16 m_qdFontFamily;
1033 wxInt16 m_qdFontStyle;
1034#endif
1035 wxCFStringRef cf( m_faceName, wxLocale::GetSystemEncoding() );
1036 ATSFontFamilyRef atsfamily = ATSFontFamilyFindFromName( cf , kATSOptionFlagsDefault );
1037 if ( atsfamily == (ATSFontFamilyRef) -1 )
1038 {
1039 wxLogDebug( wxT("ATSFontFamilyFindFromName failed for ") + m_faceName );
1040 m_qdFontFamily = GetAppFont();
1041 }
1042 else
1043 {
1044 m_qdFontFamily = FMGetFontFamilyFromATSFontFamilyRef( atsfamily );
1045 }
1046
1047 m_qdFontStyle = 0;
1048 if (m_weight == wxFONTWEIGHT_BOLD)
1049 m_qdFontStyle |= bold;
1050 if (m_style == wxFONTSTYLE_ITALIC || m_style == wxFONTSTYLE_SLANT)
1051 m_qdFontStyle |= italic;
1052 if (m_underlined)
1053 m_qdFontStyle |= underline;
1054
1055
1056 // we try to get as much styles as possible into ATSU
1057
1058 // ATSUFontID and FMFont are equivalent
1059 FMFontStyle intrinsicStyle = 0 ;
1060 OSStatus status = FMGetFontFromFontFamilyInstance( m_qdFontFamily , m_qdFontStyle , (FMFont*)&m_atsuFontID , &intrinsicStyle);
1061 wxASSERT_MSG( status == noErr , wxT("couldn't get an ATSUFont from font family") );
1062 m_atsuAdditionalQDStyles = m_qdFontStyle & (~intrinsicStyle );
1063 m_atsuFontValid = true;
1064 }
1065#endif
1066#if wxOSX_USE_COCOA
1067 if ( m_nsFontDescriptor == NULL )
1068 OSXValidateNSFontDescriptor();
1069#endif
1070#if wxOSX_USE_IPHONE
1071 // TODO
1072#endif
1073 m_descriptorValid = true;
1074}
1075
1076void wxNativeFontInfo::Init(const wxNativeFontInfo& info)
1077{
1078 Init();
1079#if wxOSX_USE_CORE_TEXT
1080 m_ctFontDescriptor = wxCFRetain(info.m_ctFontDescriptor);
1081#endif
1082#if wxOSX_USE_ATSU_TEXT
1083 m_atsuFontValid = info.m_atsuFontValid;
1084 m_atsuFontID = info.m_atsuFontID ;
1085 m_atsuAdditionalQDStyles = info.m_atsuAdditionalQDStyles;
1086#if wxOSX_USE_CARBON
1087 m_qdFontFamily = info.m_qdFontFamily;
1088 m_qdFontStyle = info.m_qdFontStyle;
1089#endif
1090#endif
1091#if wxOSX_USE_COCOA
1092 m_nsFontDescriptor = (NSFontDescriptor*) wxMacCocoaRetain(info.m_nsFontDescriptor);
1093#endif
1094 m_pointSize = info.m_pointSize;
1095 m_family = info.m_family;
1096 m_style = info.m_style;
1097 m_weight = info.m_weight;
1098 m_underlined = info.m_underlined;
1099 m_faceName = info.m_faceName;
1100 m_encoding = info.m_encoding;
1101 m_descriptorValid = info.m_descriptorValid;
1102}
1103
1104void wxNativeFontInfo::Init(int size,
1105 wxFontFamily family,
1106 wxFontStyle style,
1107 wxFontWeight weight,
1108 bool underlined,
1109 const wxString& faceName,
1110 wxFontEncoding encoding)
1111{
1112 Init();
1113 m_pointSize = size;
1114 m_family = family;
1115 m_style = style;
1116 m_weight = weight;
1117 m_underlined = underlined;
1118 m_faceName = faceName;
1119 if ( encoding == wxFONTENCODING_DEFAULT )
1120 encoding = wxFont::GetDefaultEncoding();
1121 m_encoding = encoding;
1122
1123}
1124
1125void wxNativeFontInfo::Free()
1126{
1127#if wxOSX_USE_CORE_TEXT
1128 wxCFRelease(m_ctFontDescriptor);
1129 m_ctFontDescriptor = NULL;
1130#endif
1131#if wxOSX_USE_ATSU_TEXT
1132 m_atsuFontID = 0 ;
1133 m_atsuAdditionalQDStyles = 0;
1134 m_atsuFontValid = false;
1135#endif
1136#if wxOSX_USE_COCOA
1137 wxMacCocoaRelease(m_nsFontDescriptor);
1138 m_nsFontDescriptor = NULL;
1139#endif
1140 m_descriptorValid = false;
1141}
1142
1143bool wxNativeFontInfo::FromString(const wxString& s)
1144{
1145 long l;
1146
1147 wxStringTokenizer tokenizer(s, _T(";"));
1148
1149 wxString token = tokenizer.GetNextToken();
1150 //
1151 // Ignore the version for now
1152 //
1153
1154 token = tokenizer.GetNextToken();
1155 if ( !token.ToLong(&l) )
1156 return false;
1157 m_pointSize = (int)l;
1158
1159 token = tokenizer.GetNextToken();
1160 if ( !token.ToLong(&l) )
1161 return false;
1162 m_family = (wxFontFamily)l;
1163
1164 token = tokenizer.GetNextToken();
1165 if ( !token.ToLong(&l) )
1166 return false;
1167 m_style = (wxFontStyle)l;
1168
1169 token = tokenizer.GetNextToken();
1170 if ( !token.ToLong(&l) )
1171 return false;
1172 m_weight = (wxFontWeight)l;
1173
1174 token = tokenizer.GetNextToken();
1175 if ( !token.ToLong(&l) )
1176 return false;
1177 m_underlined = l != 0;
1178
1179 m_faceName = tokenizer.GetNextToken();
1180
1181#ifndef __WXMAC__
1182 if( !faceName )
1183 return false;
1184#endif
1185
1186 token = tokenizer.GetNextToken();
1187 if ( !token.ToLong(&l) )
1188 return false;
1189 m_encoding = (wxFontEncoding)l;
1190
1191 return true;
1192}
1193
1194wxString wxNativeFontInfo::ToString() const
1195{
1196 wxString s;
1197
1198 s.Printf(_T("%d;%d;%d;%d;%d;%d;%s;%d"),
1199 0, // version
1200 m_pointSize,
1201 m_family,
1202 (int)m_style,
1203 (int)m_weight,
1204 m_underlined,
1205 m_faceName.GetData(),
1206 (int)m_encoding);
1207
1208 return s;
1209}
1210
1211int wxNativeFontInfo::GetPointSize() const
1212{
1213 return m_pointSize;
1214}
1215
1216wxFontStyle wxNativeFontInfo::GetStyle() const
1217{
1218 return m_style;
1219}
1220
1221wxFontWeight wxNativeFontInfo::GetWeight() const
1222{
1223 return m_weight;
1224}
1225
1226bool wxNativeFontInfo::GetUnderlined() const
1227{
1228 return m_underlined;
1229}
1230
1231wxString wxNativeFontInfo::GetFaceName() const
1232{
1233 return m_faceName;
1234}
1235
1236wxFontFamily wxNativeFontInfo::GetFamily() const
1237{
1238 return m_family;
1239}
1240
1241wxFontEncoding wxNativeFontInfo::GetEncoding() const
1242{
1243 return m_encoding;
1244}
1245
1246// changing the font descriptor
1247
1248void wxNativeFontInfo::SetPointSize(int pointsize)
1249{
1250 if ( m_pointSize != pointsize )
1251 {
1252 m_pointSize = pointsize;
1253 Free();
1254 }
1255}
1256
1257void wxNativeFontInfo::SetStyle(wxFontStyle style_)
1258{
1259 if ( m_style != style_ )
1260 {
1261 m_style = style_;
1262 Free();
1263 }
1264}
1265
1266void wxNativeFontInfo::SetWeight(wxFontWeight weight_)
1267{
1268 if ( m_weight != weight_ )
1269 {
1270 m_weight = weight_;
1271 Free();
1272 }
1273}
1274
1275void wxNativeFontInfo::SetUnderlined(bool underlined_)
1276{
1277 if ( m_underlined != underlined_ )
1278 {
1279 m_underlined = underlined_;
1280 Free();
1281 }
1282}
1283
1284bool wxNativeFontInfo::SetFaceName(const wxString& facename_)
1285{
1286 if ( m_faceName != facename_ )
1287 {
1288 m_faceName = facename_;
1289 Free();
1290 }
1291 return true;
1292}
1293
1294void wxNativeFontInfo::SetFamily(wxFontFamily family_)
1295{
1296 if ( m_family != family_ )
1297 {
1298 m_family = family_;
1299 Free();
1300 }
1301}
1302
1303void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding_)
1304{
1305 if ( encoding_ == wxFONTENCODING_DEFAULT )
1306 encoding_ = wxFont::GetDefaultEncoding();
1307 m_encoding = encoding_;
1308 // not reflected in native descriptors
1309}