1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/font.cpp
3 // Purpose: wxFont class
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
17 #include "wx/string.h"
20 #include "wx/gdicmn.h"
24 #include "wx/fontutil.h"
25 #include "wx/graphics.h"
26 #include "wx/settings.h"
27 #include "wx/tokenzr.h"
29 #include "wx/osx/private.h"
31 #if wxOSX_USE_ATSU_TEXT && !wxOSX_USE_CARBON
32 // include themeing support
33 #include <Carbon/Carbon.h>
39 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
41 class WXDLLEXPORT wxFontRefData
: public wxGDIRefData
48 m_info
.Init(10, wxFONTFAMILY_DEFAULT
, wxFONTSTYLE_NORMAL
, wxFONTWEIGHT_NORMAL
,
49 false, wxEmptyString
, wxFONTENCODING_DEFAULT
);
52 wxFontRefData(const wxFontRefData
& data
);
54 wxFontRefData( const wxNativeFontInfo
& info
) : m_info(info
)
59 wxFontRefData(wxOSXSystemFont font
, int size
);
61 #if wxOSX_USE_CORE_TEXT
62 wxFontRefData( wxUint32 coreTextFontType
);
63 wxFontRefData( CTFontRef font
);
64 wxFontRefData( CTFontDescriptorRef fontdescriptor
, int size
);
67 virtual ~wxFontRefData();
69 void SetNoAntiAliasing( bool no
= true ) { m_noAA
= no
; }
71 bool GetNoAntiAliasing() const { return m_noAA
; }
73 void SetPointSize( int size
)
75 if( GetPointSize() != size
)
77 m_info
.SetPointSize(size
);
82 int GetPointSize() const { return m_info
.GetPointSize(); }
84 void SetFamily( wxFontFamily family
)
86 if ( m_info
.m_family
!= family
)
88 m_info
.SetFamily( family
);
93 wxFontFamily
GetFamily() const { return m_info
.GetFamily(); }
95 void SetStyle( wxFontStyle style
)
97 if ( m_info
.m_style
!= style
)
99 m_info
.SetStyle( style
);
105 wxFontStyle
GetStyle() const { return m_info
.GetStyle(); }
107 void SetWeight( wxFontWeight weight
)
109 if ( m_info
.m_weight
!= weight
)
111 m_info
.SetWeight( weight
);
117 wxFontWeight
GetWeight() const { return m_info
.GetWeight(); }
119 void SetUnderlined( bool u
)
121 if ( m_info
.m_underlined
!= u
)
123 m_info
.SetUnderlined( u
);
128 bool GetUnderlined() const { return m_info
.GetUnderlined(); }
130 void SetFaceName( const wxString
& facename
)
132 if ( m_info
.m_faceName
!= facename
)
134 m_info
.SetFaceName( facename
);
139 wxString
GetFaceName() const { return m_info
.GetFaceName(); }
141 void SetEncoding( wxFontEncoding encoding
)
143 if ( m_info
.m_encoding
!= encoding
)
145 m_info
.SetEncoding( encoding
);
150 wxFontEncoding
GetEncoding() const { return m_info
.GetEncoding(); }
157 // common part of all ctors
159 #if wxOSX_USE_CORE_TEXT
160 // void Init( CTFontRef font );
162 bool m_noAA
; // No anti-aliasing
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
;
170 #if wxOSX_USE_CORE_TEXT
171 wxCFRef
<CTFontRef
> m_ctFont
;
173 #if wxOSX_USE_CORE_TEXT || wxOSX_USE_ATSU_TEXT
174 ATSUStyle m_macATSUStyle
;
182 wxNativeFontInfo m_info
;
185 #define M_FONTDATA ((wxFontRefData*)m_refData)
187 wxFontRefData::wxFontRefData(const wxFontRefData
& data
)
190 m_info
= data
.m_info
;
191 m_noAA
= data
.m_noAA
;
192 m_fontValid
= data
.m_fontValid
;
193 #if wxOSX_USE_CARBON && wxOSX_USE_ATSU_TEXT
194 m_macThemeFontID
= data
.m_macThemeFontID
;
196 #if wxOSX_USE_CORE_TEXT
197 m_ctFont
= data
.m_ctFont
;
199 #if wxOSX_USE_CORE_TEXT || wxOSX_USE_ATSU_TEXT
200 if ( data
.m_macATSUStyle
!= NULL
)
202 ATSUCreateStyle(&m_macATSUStyle
) ;
203 ATSUCopyAttributes(data
.m_macATSUStyle
, m_macATSUStyle
);
207 m_nsFont
= (NSFont
*) wxMacCocoaRetain(data
.m_nsFont
);
210 m_uiFont
= wxMacCocoaRetain(data
.m_uiFont
);
215 // ============================================================================
217 // ============================================================================
219 // ----------------------------------------------------------------------------
221 // ----------------------------------------------------------------------------
223 void wxFontRefData::Init()
226 #if wxOSX_USE_CARBON && wxOSX_USE_ATSU_TEXT
227 m_macThemeFontID
= kThemeCurrentPortFont
;
229 #if wxOSX_USE_CORE_TEXT || wxOSX_USE_ATSU_TEXT
230 m_macATSUStyle
= NULL
;
241 wxFontRefData::~wxFontRefData()
246 void wxFontRefData::Free()
248 #if wxOSX_USE_CORE_TEXT
251 #if wxOSX_USE_CORE_TEXT || wxOSX_USE_ATSU_TEXT
252 if ( m_macATSUStyle
)
254 ::ATSUDisposeStyle((ATSUStyle
)m_macATSUStyle
);
255 m_macATSUStyle
= NULL
;
259 if (m_nsFont
!= NULL
)
261 wxMacCocoaRelease(m_nsFont
);
266 if (m_uiFont
!= NULL
)
268 wxMacCocoaRelease(m_uiFont
);
275 wxFontRefData::wxFontRefData(wxOSXSystemFont font
, int size
)
277 wxASSERT( font
!= wxOSX_SYSTEM_FONT_NONE
);
280 #if wxOSX_USE_CORE_TEXT
281 if ( UMAGetSystemVersion() >= 0x1050 )
283 CTFontUIFontType uifont
;
286 case wxOSX_SYSTEM_FONT_NORMAL
:
287 uifont
= kCTFontSystemFontType
;
289 case wxOSX_SYSTEM_FONT_BOLD
:
290 uifont
= kCTFontEmphasizedSystemFontType
;
292 case wxOSX_SYSTEM_FONT_SMALL
:
293 uifont
= kCTFontSmallSystemFontType
;
295 case wxOSX_SYSTEM_FONT_SMALL_BOLD
:
296 uifont
= kCTFontSmallEmphasizedSystemFontType
;
298 case wxOSX_SYSTEM_FONT_MINI
:
299 uifont
= kCTFontMiniSystemFontType
;
301 case wxOSX_SYSTEM_FONT_MINI_BOLD
:
302 uifont
= kCTFontMiniEmphasizedSystemFontType
;
304 case wxOSX_SYSTEM_FONT_LABELS
:
305 uifont
= kCTFontLabelFontType
;
307 case wxOSX_SYSTEM_FONT_VIEWS
:
308 uifont
= kCTFontViewsFontType
;
313 m_ctFont
.reset(CTFontCreateUIFontForLanguage( uifont
, (CGFloat
) size
, NULL
));
314 wxCFRef
<CTFontDescriptorRef
> descr
;
315 descr
.reset( CTFontCopyFontDescriptor( m_ctFont
) );
319 #if wxOSX_USE_ATSU_TEXT
323 case wxOSX_SYSTEM_FONT_NORMAL
:
324 m_macThemeFontID
= kThemeSystemFont
;
326 case wxOSX_SYSTEM_FONT_BOLD
:
327 m_macThemeFontID
= kThemeEmphasizedSystemFont
;
329 case wxOSX_SYSTEM_FONT_SMALL
:
330 m_macThemeFontID
= kThemeSmallSystemFont
;
332 case wxOSX_SYSTEM_FONT_SMALL_BOLD
:
333 m_macThemeFontID
= kThemeSmallEmphasizedSystemFont
;
335 case wxOSX_SYSTEM_FONT_MINI
:
336 m_macThemeFontID
= kThemeMiniSystemFont
;
338 case wxOSX_SYSTEM_FONT_MINI_BOLD
:
339 // bold not available under themeing
340 m_macThemeFontID
= kThemeMiniSystemFont
;
342 case wxOSX_SYSTEM_FONT_LABELS
:
343 m_macThemeFontID
= kThemeLabelFont
;
345 case wxOSX_SYSTEM_FONT_VIEWS
:
346 m_macThemeFontID
= kThemeViewsFont
;
351 if ( m_info
.m_faceName
.empty() )
357 GetThemeFont( m_macThemeFontID
, GetApplicationScript(), qdFontName
, &fontSize
, &style
);
361 wxFontStyle fontstyle
= wxFONTSTYLE_NORMAL
;
362 wxFontWeight fontweight
= wxFONTWEIGHT_NORMAL
;
363 bool underlined
= false;
366 fontweight
= wxFONTWEIGHT_BOLD
;
368 fontweight
= wxFONTWEIGHT_NORMAL
;
369 if ( style
& italic
)
370 fontstyle
= wxFONTSTYLE_ITALIC
;
371 if ( style
& underline
)
374 m_info
.Init(size
,wxFONTFAMILY_DEFAULT
,fontstyle
,fontweight
,underlined
,
375 wxMacMakeStringFromPascal( qdFontName
), wxFONTENCODING_DEFAULT
);
380 m_nsFont
= wxFont::CreateNSFont( font
, &m_info
);
383 m_uiFont
= wxFont::CreateUIFont( font
, &m_info
);
387 void wxFontRefData::MacFindFont()
392 m_info
.EnsureValid();
394 #if wxOSX_USE_CORE_TEXT
395 if ( UMAGetSystemVersion() >= 0x1050 )
397 CTFontSymbolicTraits traits
= 0;
399 if (m_info
.m_weight
== wxFONTWEIGHT_BOLD
)
400 traits
|= kCTFontBoldTrait
;
401 if (m_info
.m_style
== wxFONTSTYLE_ITALIC
|| m_info
.m_style
== wxFONTSTYLE_SLANT
)
402 traits
|= kCTFontItalicTrait
;
405 wxString lookupnameWithSize
= wxString::Format( "%s_%ld_%ld", m_info
.m_faceName
.c_str(), traits
, m_info
.m_pointSize
);
407 static std::map
< std::wstring
, wxCFRef
< CTFontRef
> > fontcache
;
408 m_ctFont
= fontcache
[ std::wstring(lookupnameWithSize
.wc_str()) ];
411 m_ctFont
.reset( CTFontCreateWithFontDescriptor( m_info
.m_ctFontDescriptor
, 0/*m_pointSize */, NULL
) );
416 #if wxOSX_USE_ATSU_TEXT
418 // we try to get as much styles as possible into ATSU
420 OSStatus status
= ::ATSUCreateStyle(&m_macATSUStyle
);
421 wxASSERT_MSG( status
== noErr
, wxT("couldn't create ATSU style") );
423 ATSUAttributeTag atsuTags
[] =
427 kATSUVerticalCharacterTag
,
430 kATSUQDUnderlineTag
,
431 kATSUQDCondensedTag
,
434 ByteCount atsuSizes
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
436 sizeof( ATSUFontID
) ,
438 sizeof( ATSUVerticalCharacterType
),
446 Boolean kTrue
= true ;
447 Boolean kFalse
= false ;
449 Fixed atsuSize
= IntToFixed( m_info
.m_pointSize
);
450 ATSUVerticalCharacterType kHorizontal
= kATSUStronglyHorizontal
;
451 FMFontStyle addQDStyle
= m_info
.m_atsuAdditionalQDStyles
;
452 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
454 &m_info
.m_atsuFontID
,
457 (addQDStyle
& bold
) ? &kTrue
: &kFalse
,
458 (addQDStyle
& italic
) ? &kTrue
: &kFalse
,
459 (addQDStyle
& underline
) ? &kTrue
: &kFalse
,
460 (addQDStyle
& condense
) ? &kTrue
: &kFalse
,
461 (addQDStyle
& extend
) ? &kTrue
: &kFalse
,
464 status
= ::ATSUSetAttributes(
465 (ATSUStyle
)m_macATSUStyle
,
466 sizeof(atsuTags
) / sizeof(ATSUAttributeTag
) ,
467 atsuTags
, atsuSizes
, atsuValues
);
469 wxASSERT_MSG( status
== noErr
, wxT("couldn't modify ATSU style") );
474 m_nsFont
= wxFont::CreateNSFont( &m_info
);
477 m_uiFont
= wxFont::CreateUIFont( &m_info
);
482 // ----------------------------------------------------------------------------
484 // ----------------------------------------------------------------------------
486 bool wxFont::Create(const wxNativeFontInfo
& info
)
490 m_refData
= new wxFontRefData( info
);
496 wxFont::wxFont(const wxString
& fontdesc
)
498 wxNativeFontInfo info
;
499 if ( info
.FromString(fontdesc
) )
503 bool wxFont::Create(int pointSize
,
508 const wxString
& faceNameParam
,
509 wxFontEncoding encoding
)
513 wxString faceName
= faceNameParam
;
515 if ( faceName
.empty() )
519 case wxFONTFAMILY_DEFAULT
:
520 faceName
= wxT("Lucida Grande");
523 case wxFONTFAMILY_SCRIPT
:
524 case wxFONTFAMILY_ROMAN
:
525 case wxFONTFAMILY_DECORATIVE
:
526 faceName
= wxT("Times");
529 case wxFONTFAMILY_SWISS
:
530 faceName
= wxT("Helvetica");
533 case wxFONTFAMILY_MODERN
:
534 case wxFONTFAMILY_TELETYPE
:
535 faceName
= wxT("Courier");
539 faceName
= wxT("Times");
544 wxNativeFontInfo info
;
546 info
.Init(pointSize
, family
, style
, weight
,
547 underlined
, faceName
, encoding
);
549 m_refData
= new wxFontRefData(info
);
554 bool wxFont::CreateSystemFont(wxOSXSystemFont font
)
558 m_refData
= new wxFontRefData( font
, 0 );
567 bool wxFont::RealizeResource()
569 M_FONTDATA
->MacFindFont();
574 void wxFont::SetEncoding(wxFontEncoding encoding
)
578 M_FONTDATA
->SetEncoding( encoding
);
581 wxGDIRefData
*wxFont::CreateGDIRefData() const
583 return new wxFontRefData
;
586 wxGDIRefData
*wxFont::CloneGDIRefData(const wxGDIRefData
*data
) const
588 return new wxFontRefData(*static_cast<const wxFontRefData
*>(data
));
591 void wxFont::SetPointSize(int pointSize
)
593 if ( M_FONTDATA
->GetPointSize() == pointSize
)
598 M_FONTDATA
->SetPointSize( pointSize
);
601 void wxFont::SetFamily(wxFontFamily family
)
605 M_FONTDATA
->SetFamily( family
);
608 void wxFont::SetStyle(wxFontStyle style
)
612 M_FONTDATA
->SetStyle( style
);
615 void wxFont::SetWeight(wxFontWeight weight
)
619 M_FONTDATA
->SetWeight( weight
);
622 bool wxFont::SetFaceName(const wxString
& faceName
)
626 M_FONTDATA
->SetFaceName( faceName
);
628 return wxFontBase::SetFaceName(faceName
);
631 void wxFont::SetUnderlined(bool underlined
)
635 M_FONTDATA
->SetUnderlined( underlined
);
638 void wxFont::SetNoAntiAliasing( bool no
)
642 M_FONTDATA
->SetNoAntiAliasing( no
);
645 // ----------------------------------------------------------------------------
647 // ----------------------------------------------------------------------------
649 // TODO: insert checks everywhere for M_FONTDATA == NULL!
651 int wxFont::GetPointSize() const
653 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
655 return M_FONTDATA
->GetPointSize();
658 wxSize
wxFont::GetPixelSize() const
660 #if wxUSE_GRAPHICS_CONTEXT
661 // TODO: consider caching the value
662 wxGraphicsContext
* dc
= wxGraphicsContext::CreateFromNative((CGContextRef
) NULL
);
663 dc
->SetFont(*(wxFont
*)this,*wxBLACK
);
664 wxDouble width
, height
= 0;
665 dc
->GetTextExtent( wxT("g"), &width
, &height
, NULL
, NULL
);
667 return wxSize((int)width
, (int)height
);
669 return wxFontBase::GetPixelSize();
673 wxFontFamily
wxFont::GetFamily() const
675 wxCHECK_MSG( M_FONTDATA
!= NULL
, wxFONTFAMILY_MAX
, wxT("invalid font") );
677 return M_FONTDATA
->GetFamily();
680 wxFontStyle
wxFont::GetStyle() const
682 wxCHECK_MSG( M_FONTDATA
!= NULL
, wxFONTSTYLE_MAX
, wxT("invalid font") );
684 return M_FONTDATA
->GetStyle() ;
687 wxFontWeight
wxFont::GetWeight() const
689 wxCHECK_MSG( M_FONTDATA
!= NULL
, wxFONTWEIGHT_MAX
, wxT("invalid font") );
691 return M_FONTDATA
->GetWeight();
694 bool wxFont::GetUnderlined() const
696 wxCHECK_MSG( M_FONTDATA
!= NULL
, false, wxT("invalid font") );
698 return M_FONTDATA
->GetUnderlined();
701 wxString
wxFont::GetFaceName() const
703 wxCHECK_MSG( M_FONTDATA
!= NULL
, wxEmptyString
, wxT("invalid font") );
705 return M_FONTDATA
->GetFaceName() ;
708 wxFontEncoding
wxFont::GetEncoding() const
710 wxCHECK_MSG( M_FONTDATA
!= NULL
, wxFONTENCODING_DEFAULT
, wxT("invalid font") );
712 return M_FONTDATA
->GetEncoding() ;
715 bool wxFont::GetNoAntiAliasing() const
717 wxCHECK_MSG( M_FONTDATA
!= NULL
, false, wxT("invalid font") );
719 return M_FONTDATA
->GetNoAntiAliasing();
722 #if wxOSX_USE_ATSU_TEXT && wxOSX_USE_CARBON
724 short wxFont::MacGetFontNum() const
726 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
728 // cast away constness otherwise lazy font resolution is not possible
729 const_cast<wxFont
*>(this)->RealizeResource();
731 return M_FONTDATA
->m_info
.m_qdFontFamily
;
734 wxByte
wxFont::MacGetFontStyle() const
736 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
738 // cast away constness otherwise lazy font resolution is not possible
739 const_cast<wxFont
*>(this)->RealizeResource();
741 return M_FONTDATA
->m_info
.m_qdFontStyle
;
744 wxUint16
wxFont::MacGetThemeFontID() const
746 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
748 return M_FONTDATA
->m_macThemeFontID
;
753 #if wxOSX_USE_CORE_TEXT || wxOSX_USE_ATSU_TEXT
754 void * wxFont::MacGetATSUStyle() const
756 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
758 // cast away constness otherwise lazy font resolution is not possible
759 const_cast<wxFont
*>(this)->RealizeResource();
761 return M_FONTDATA
->m_macATSUStyle
;
765 #if wxOSX_USE_CORE_TEXT
767 CTFontRef
wxFont::GetCTFont() const
769 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
771 // cast away constness otherwise lazy font resolution is not possible
772 const_cast<wxFont
*>(this)->RealizeResource();
774 return (CTFontRef
)(M_FONTDATA
->m_ctFont
);
781 NSFont
* wxFont::GetNSFont() const
783 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
785 // cast away constness otherwise lazy font resolution is not possible
786 const_cast<wxFont
*>(this)->RealizeResource();
788 return (M_FONTDATA
->m_nsFont
);
793 const wxNativeFontInfo
* wxFont::GetNativeFontInfo() const
795 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
796 wxCHECK_MSG( Ok(), NULL
, wxT("invalid font") );
798 // cast away constness otherwise lazy font resolution is not possible
799 const_cast<wxFont
*>(this)->RealizeResource();
801 // M_FONTDATA->m_info.InitFromFont(*this);
803 return &(M_FONTDATA
->m_info
);
806 // ----------------------------------------------------------------------------
808 // ----------------------------------------------------------------------------
810 #if wxOSX_USE_CORE_TEXT
812 /* from Core Text Manual Common Operations */
814 static CTFontDescriptorRef
wxMacCreateCTFontDescriptor(CFStringRef iFamilyName
, CTFontSymbolicTraits iTraits
)
816 CTFontDescriptorRef descriptor
= NULL
;
817 CFMutableDictionaryRef attributes
;
819 assert(iFamilyName
!= NULL
);
820 // Create a mutable dictionary to hold our attributes.
821 attributes
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
822 &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
823 check(attributes
!= NULL
);
825 if (attributes
!= NULL
) {
826 // Add a family name to our attributes.
827 CFDictionaryAddValue(attributes
, kCTFontFamilyNameAttribute
, iFamilyName
);
831 CFMutableDictionaryRef traits
;
832 CFNumberRef symTraits
;
834 // Create the traits dictionary.
835 symTraits
= CFNumberCreate(kCFAllocatorDefault
, kCFNumberSInt32Type
,
837 check(symTraits
!= NULL
);
839 if (symTraits
!= NULL
) {
840 // Create a dictionary to hold our traits values.
841 traits
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
842 &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
843 check(traits
!= NULL
);
845 if (traits
!= NULL
) {
846 // Add the symbolic traits value to the traits dictionary.
847 CFDictionaryAddValue(traits
, kCTFontSymbolicTrait
, symTraits
);
849 // Add the traits attribute to our attributes.
850 CFDictionaryAddValue(attributes
, kCTFontTraitsAttribute
, traits
);
853 CFRelease(symTraits
);
856 // Create the font descriptor with our attributes
857 descriptor
= CTFontDescriptorCreateWithAttributes(attributes
);
858 check(descriptor
!= NULL
);
860 CFRelease(attributes
);
862 // Return our font descriptor.
868 void wxNativeFontInfo::Init()
870 #if wxOSX_USE_CORE_TEXT
871 m_ctFontDescriptor
= NULL
;
873 #if wxOSX_USE_ATSU_TEXT
875 m_atsuAdditionalQDStyles
= 0;
876 m_atsuFontValid
= false;
883 m_nsFontDescriptor
= NULL
;
886 m_uiFontDescriptor
= NULL
;
889 m_family
= wxFONTFAMILY_DEFAULT
;
890 m_style
= wxFONTSTYLE_NORMAL
;
891 m_weight
= wxFONTWEIGHT_NORMAL
;
892 m_underlined
= false;
894 m_encoding
= wxFONTENCODING_DEFAULT
;
895 m_descriptorValid
= false;
898 #if wxOSX_USE_CORE_TEXT
899 void wxNativeFontInfo::Init(CTFontDescriptorRef descr
)
902 m_ctFontDescriptor
= wxCFRetain(descr
);
904 wxCFRef
< CFNumberRef
> sizevalue( (CFNumberRef
) CTFontDescriptorCopyAttribute( m_ctFontDescriptor
, kCTFontSizeAttribute
) );
906 if ( CFNumberGetValue( sizevalue
, kCFNumberFloatType
, &fsize
) )
907 m_pointSize
= (int)( fsize
+ 0.5 );
909 wxCFRef
< CFDictionaryRef
> traitsvalue( (CFDictionaryRef
) CTFontDescriptorCopyAttribute( m_ctFontDescriptor
, kCTFontTraitsAttribute
) );
910 CTFontSymbolicTraits traits
;
911 if ( CFNumberGetValue((CFNumberRef
) CFDictionaryGetValue(traitsvalue
,kCTFontSymbolicTrait
),kCFNumberIntType
,&traits
) )
913 if ( traits
& kCTFontItalicTrait
)
914 m_style
= wxFONTSTYLE_ITALIC
;
915 if ( traits
& kCTFontBoldTrait
)
916 m_weight
= wxFONTWEIGHT_BOLD
;
919 wxCFStringRef
familyName( (CFStringRef
) CTFontDescriptorCopyAttribute(m_ctFontDescriptor
, kCTFontFamilyNameAttribute
));
920 m_faceName
= familyName
.AsString();
924 void wxNativeFontInfo::EnsureValid()
926 if ( m_descriptorValid
)
929 #if wxOSX_USE_CORE_TEXT
930 if ( m_ctFontDescriptor
== NULL
&& UMAGetSystemVersion() >= 0x1050 )
932 CTFontSymbolicTraits traits
= 0;
934 if (m_weight
== wxFONTWEIGHT_BOLD
)
935 traits
|= kCTFontBoldTrait
;
936 if (m_style
== wxFONTSTYLE_ITALIC
|| m_style
== wxFONTSTYLE_SLANT
)
937 traits
|= kCTFontItalicTrait
;
940 wxString lookupnameWithSize
= wxString::Format( "%s_%ld_%ld", m_faceName
.c_str(), traits
, m_pointSize
);
942 static std::map
< std::wstring
, wxCFRef
< CTFontDescriptorRef
> > fontdescriptorcache
;
943 m_ctFontDescriptor
= wxCFRetain((CTFontDescriptorRef
)fontdescriptorcache
[ std::wstring(lookupnameWithSize
.wc_str()) ]);
944 if ( !m_ctFontDescriptor
)
946 // QD selection algorithm is the fastest by orders of magnitude on 10.5
947 if ( m_faceName
.IsAscii() )
950 if (m_weight
== wxFONTWEIGHT_BOLD
)
952 if (m_style
== wxFONTSTYLE_ITALIC
|| m_style
== wxFONTSTYLE_SLANT
)
956 wxMacStringToPascal( m_faceName
, qdFontName
);
957 wxCFRef
< CTFontRef
> font
;
958 font
.reset( CTFontCreateWithQuickdrawInstance(qdFontName
, 0 , qdstyle
, m_pointSize
) );
959 m_ctFontDescriptor
= CTFontCopyFontDescriptor(font
);
963 m_ctFontDescriptor
= wxMacCreateCTFontDescriptor( wxCFStringRef(m_faceName
),traits
);
965 fontdescriptorcache
[ std::wstring(lookupnameWithSize
.wc_str()) ].reset(wxCFRetain(m_ctFontDescriptor
));
969 #if wxOSX_USE_ATSU_TEXT
970 if ( !m_atsuFontValid
)
972 wxCFStringRef
cf( m_faceName
, wxLocale::GetSystemEncoding() );
973 ATSFontFamilyRef atsfamily
= ATSFontFamilyFindFromName( cf
, kATSOptionFlagsDefault
);
974 if ( atsfamily
== (ATSFontFamilyRef
) -1 )
976 wxLogDebug( wxT("ATSFontFamilyFindFromName failed for ") + m_faceName
);
977 m_qdFontFamily
= GetAppFont();
981 m_qdFontFamily
= FMGetFontFamilyFromATSFontFamilyRef( atsfamily
);
985 if (m_weight
== wxFONTWEIGHT_BOLD
)
986 m_qdFontStyle
|= bold
;
987 if (m_style
== wxFONTSTYLE_ITALIC
|| m_style
== wxFONTSTYLE_SLANT
)
988 m_qdFontStyle
|= italic
;
990 m_qdFontStyle
|= underline
;
993 // we try to get as much styles as possible into ATSU
995 // ATSUFontID and FMFont are equivalent
996 FMFontStyle intrinsicStyle
= 0 ;
997 OSStatus status
= FMGetFontFromFontFamilyInstance( m_qdFontFamily
, m_qdFontStyle
, (FMFont
*)&m_atsuFontID
, &intrinsicStyle
);
998 wxASSERT_MSG( status
== noErr
, wxT("couldn't get an ATSUFont from font family") );
999 m_atsuAdditionalQDStyles
= m_qdFontStyle
& (~intrinsicStyle
);
1000 m_atsuFontValid
= true;
1004 if ( m_nsFontDescriptor
== NULL
)
1005 ValidateNSFontDescriptor();
1007 #if wxOSX_USE_IPHONE
1010 m_descriptorValid
= true;
1013 void wxNativeFontInfo::Init(const wxNativeFontInfo
& info
)
1016 #if wxOSX_USE_CORE_TEXT
1017 m_ctFontDescriptor
= wxCFRetain(info
.m_ctFontDescriptor
);
1019 #if wxOSX_USE_ATSU_TEXT
1020 m_atsuFontValid
= info
.m_atsuFontValid
;
1021 m_atsuFontID
= info
.m_atsuFontID
;
1022 m_atsuAdditionalQDStyles
= info
.m_atsuAdditionalQDStyles
;
1023 #if wxOSX_USE_CARBON
1024 m_qdFontFamily
= info
.m_qdFontFamily
;
1025 m_qdFontStyle
= info
.m_qdFontStyle
;
1029 m_nsFontDescriptor
= (NSFontDescriptor
*) wxMacCocoaRetain(info
.m_nsFontDescriptor
);
1031 #if wxOSX_USE_IPHONE
1032 m_uiFontDescriptor
= wxMacCocoaRetain(info
.m_uiFontDescriptor
);;
1034 m_pointSize
= info
.m_pointSize
;
1035 m_family
= info
.m_family
;
1036 m_style
= info
.m_style
;
1037 m_weight
= info
.m_weight
;
1038 m_underlined
= info
.m_underlined
;
1039 m_faceName
= info
.m_faceName
;
1040 m_encoding
= info
.m_encoding
;
1041 m_descriptorValid
= info
.m_descriptorValid
;
1044 void wxNativeFontInfo::Init(int size
,
1045 wxFontFamily family
,
1047 wxFontWeight weight
,
1049 const wxString
& faceName
,
1050 wxFontEncoding encoding
)
1057 m_underlined
= underlined
;
1058 m_faceName
= faceName
;
1059 m_encoding
= encoding
;
1063 void wxNativeFontInfo::Free()
1065 #if wxOSX_USE_CORE_TEXT
1066 wxCFRelease(m_ctFontDescriptor
);
1067 m_ctFontDescriptor
= NULL
;
1069 #if wxOSX_USE_ATSU_TEXT
1071 m_atsuAdditionalQDStyles
= 0;
1072 m_atsuFontValid
= false;
1075 wxMacCocoaRelease(m_nsFontDescriptor
);
1076 m_nsFontDescriptor
= NULL
;
1078 #if wxOSX_USE_IPHONE
1079 wxMacCocoaRelease(m_uiFontDescriptor
);
1080 m_uiFontDescriptor
= NULL
1082 m_descriptorValid
= false;
1085 bool wxNativeFontInfo::FromString(const wxString
& s
)
1089 wxStringTokenizer
tokenizer(s
, _T(";"));
1091 wxString token
= tokenizer
.GetNextToken();
1093 // Ignore the version for now
1096 token
= tokenizer
.GetNextToken();
1097 if ( !token
.ToLong(&l
) )
1099 m_pointSize
= (int)l
;
1101 token
= tokenizer
.GetNextToken();
1102 if ( !token
.ToLong(&l
) )
1104 m_family
= (wxFontFamily
)l
;
1106 token
= tokenizer
.GetNextToken();
1107 if ( !token
.ToLong(&l
) )
1109 m_style
= (wxFontStyle
)l
;
1111 token
= tokenizer
.GetNextToken();
1112 if ( !token
.ToLong(&l
) )
1114 m_weight
= (wxFontWeight
)l
;
1116 token
= tokenizer
.GetNextToken();
1117 if ( !token
.ToLong(&l
) )
1119 m_underlined
= l
!= 0;
1121 m_faceName
= tokenizer
.GetNextToken();
1128 token
= tokenizer
.GetNextToken();
1129 if ( !token
.ToLong(&l
) )
1131 m_encoding
= (wxFontEncoding
)l
;
1136 wxString
wxNativeFontInfo::ToString() const
1140 s
.Printf(_T("%d;%d;%d;%d;%d;%d;%s;%d"),
1147 m_faceName
.GetData(),
1153 int wxNativeFontInfo::GetPointSize() const
1158 wxFontStyle
wxNativeFontInfo::GetStyle() const
1163 wxFontWeight
wxNativeFontInfo::GetWeight() const
1168 bool wxNativeFontInfo::GetUnderlined() const
1170 return m_underlined
;
1173 wxString
wxNativeFontInfo::GetFaceName() const
1178 wxFontFamily
wxNativeFontInfo::GetFamily() const
1183 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
1188 // changing the font descriptor
1190 void wxNativeFontInfo::SetPointSize(int pointsize
)
1192 if ( m_pointSize
!= pointsize
)
1194 m_pointSize
= pointsize
;
1199 void wxNativeFontInfo::SetStyle(wxFontStyle style_
)
1201 if ( m_style
!= style_
)
1208 void wxNativeFontInfo::SetWeight(wxFontWeight weight_
)
1210 if ( m_weight
!= weight_
)
1217 void wxNativeFontInfo::SetUnderlined(bool underlined_
)
1219 if ( m_underlined
!= underlined_
)
1221 m_underlined
= underlined_
;
1226 bool wxNativeFontInfo::SetFaceName(const wxString
& facename_
)
1228 if ( m_faceName
!= facename_
)
1230 m_faceName
= facename_
;
1236 void wxNativeFontInfo::SetFamily(wxFontFamily family_
)
1238 if ( m_family
!= family_
)
1245 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding_
)
1247 m_encoding
= encoding_
;
1248 // not reflected in native descriptors