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"
34 IMPLEMENT_DYNAMIC_CLASS(wxFont
, wxGDIObject
)
36 class WXDLLEXPORT wxFontRefData
: public wxGDIRefData
43 m_info
.Init(10, wxFONTFAMILY_DEFAULT
, wxFONTSTYLE_NORMAL
, wxFONTWEIGHT_NORMAL
,
44 false, wxEmptyString
, wxFONTENCODING_DEFAULT
);
47 wxFontRefData(const wxFontRefData
& data
);
49 wxFontRefData( const wxNativeFontInfo
& info
) : m_info(info
)
54 wxFontRefData(wxOSXSystemFont font
, int size
);
56 #if wxOSX_USE_CORE_TEXT
57 wxFontRefData( wxUint32 coreTextFontType
);
58 wxFontRefData( CTFontRef font
);
59 wxFontRefData( CTFontDescriptorRef fontdescriptor
, int size
);
62 virtual ~wxFontRefData();
64 void SetNoAntiAliasing( bool no
= true ) { m_noAA
= no
; }
66 bool GetNoAntiAliasing() const { return m_noAA
; }
68 void SetPointSize( int size
)
70 if( GetPointSize() != size
)
72 m_info
.SetPointSize(size
);
77 int GetPointSize() const { return m_info
.GetPointSize(); }
79 void SetFamily( wxFontFamily family
)
81 if ( m_info
.m_family
!= family
)
83 m_info
.SetFamily( family
);
88 wxFontFamily
GetFamily() const { return m_info
.GetFamily(); }
90 void SetStyle( wxFontStyle style
)
92 if ( m_info
.m_style
!= style
)
94 m_info
.SetStyle( style
);
100 wxFontStyle
GetStyle() const { return m_info
.GetStyle(); }
102 void SetWeight( wxFontWeight weight
)
104 if ( m_info
.m_weight
!= weight
)
106 m_info
.SetWeight( weight
);
112 wxFontWeight
GetWeight() const { return m_info
.GetWeight(); }
114 void SetUnderlined( bool u
)
116 if ( m_info
.m_underlined
!= u
)
118 m_info
.SetUnderlined( u
);
123 bool GetUnderlined() const { return m_info
.GetUnderlined(); }
125 void SetFaceName( const wxString
& facename
)
127 if ( m_info
.m_faceName
!= facename
)
129 m_info
.SetFaceName( facename
);
134 wxString
GetFaceName() const { return m_info
.GetFaceName(); }
136 void SetEncoding( wxFontEncoding encoding
)
138 if ( m_info
.m_encoding
!= encoding
)
140 m_info
.SetEncoding( encoding
);
145 wxFontEncoding
GetEncoding() const { return m_info
.GetEncoding(); }
152 // common part of all ctors
154 #if wxOSX_USE_CORE_TEXT
155 // void Init( CTFontRef font );
157 bool m_noAA
; // No anti-aliasing
160 #if wxOSX_USE_CARBON && wxOSX_USE_ATSU_TEXT
161 // for true theming support we must store the correct font
162 // information here, as this speeds up and optimizes rendering
163 ThemeFontID m_macThemeFontID
;
165 #if wxOSX_USE_CORE_TEXT
166 wxCFRef
<CTFontRef
> m_ctFont
;
168 #if wxOSX_USE_ATSU_TEXT
169 ATSUStyle m_macATSUStyle
;
171 wxCFRef
<CGFontRef
> m_cgFont
;
178 wxNativeFontInfo m_info
;
181 #define M_FONTDATA ((wxFontRefData*)m_refData)
183 wxFontRefData::wxFontRefData(const wxFontRefData
& data
)
186 m_info
= data
.m_info
;
187 m_noAA
= data
.m_noAA
;
188 m_fontValid
= data
.m_fontValid
;
189 #if wxOSX_USE_CARBON && wxOSX_USE_ATSU_TEXT
190 m_macThemeFontID
= data
.m_macThemeFontID
;
192 #if wxOSX_USE_CORE_TEXT
193 m_ctFont
= data
.m_ctFont
;
195 m_cgFont
= data
.m_cgFont
;
196 #if wxOSX_USE_ATSU_TEXT
197 if ( data
.m_macATSUStyle
!= NULL
)
199 ATSUCreateStyle(&m_macATSUStyle
) ;
200 ATSUCopyAttributes(data
.m_macATSUStyle
, m_macATSUStyle
);
204 m_nsFont
= (NSFont
*) wxMacCocoaRetain(data
.m_nsFont
);
207 m_uiFont
= (UIFont
*) wxMacCocoaRetain(data
.m_uiFont
);
212 // ============================================================================
214 // ============================================================================
216 // ----------------------------------------------------------------------------
218 // ----------------------------------------------------------------------------
220 void wxFontRefData::Init()
223 #if wxOSX_USE_CARBON && wxOSX_USE_ATSU_TEXT
224 m_macThemeFontID
= kThemeCurrentPortFont
;
226 #if wxOSX_USE_ATSU_TEXT
227 m_macATSUStyle
= NULL
;
238 wxFontRefData::~wxFontRefData()
243 void wxFontRefData::Free()
245 #if wxOSX_USE_CORE_TEXT
249 #if wxOSX_USE_ATSU_TEXT
251 m_macThemeFontID
= kThemeCurrentPortFont
;
253 if ( m_macATSUStyle
)
255 ::ATSUDisposeStyle((ATSUStyle
)m_macATSUStyle
);
256 m_macATSUStyle
= NULL
;
260 if (m_nsFont
!= NULL
)
262 wxMacCocoaRelease(m_nsFont
);
267 if (m_uiFont
!= NULL
)
269 wxMacCocoaRelease(m_uiFont
);
276 wxFontRefData::wxFontRefData(wxOSXSystemFont font
, int size
)
278 wxASSERT( font
!= wxOSX_SYSTEM_FONT_NONE
);
281 #if wxOSX_USE_CORE_TEXT
282 if ( UMAGetSystemVersion() >= 0x1050 )
284 CTFontUIFontType uifont
= kCTFontSystemFontType
;
287 case wxOSX_SYSTEM_FONT_NORMAL
:
288 uifont
= kCTFontSystemFontType
;
290 case wxOSX_SYSTEM_FONT_BOLD
:
291 uifont
= kCTFontEmphasizedSystemFontType
;
293 case wxOSX_SYSTEM_FONT_SMALL
:
294 uifont
= kCTFontSmallSystemFontType
;
296 case wxOSX_SYSTEM_FONT_SMALL_BOLD
:
297 uifont
= kCTFontSmallEmphasizedSystemFontType
;
299 case wxOSX_SYSTEM_FONT_MINI
:
300 uifont
= kCTFontMiniSystemFontType
;
302 case wxOSX_SYSTEM_FONT_MINI_BOLD
:
303 uifont
= kCTFontMiniEmphasizedSystemFontType
;
305 case wxOSX_SYSTEM_FONT_LABELS
:
306 uifont
= kCTFontLabelFontType
;
308 case wxOSX_SYSTEM_FONT_VIEWS
:
309 uifont
= kCTFontViewsFontType
;
314 m_ctFont
.reset(CTFontCreateUIFontForLanguage( uifont
, (CGFloat
) size
, NULL
));
315 wxCFRef
<CTFontDescriptorRef
> descr
;
316 descr
.reset( CTFontCopyFontDescriptor( m_ctFont
) );
320 #if wxOSX_USE_ATSU_TEXT
322 #if !wxOSX_USE_CARBON
323 // not needed outside
324 ThemeFontID m_macThemeFontID
= kThemeSystemFont
;
328 case wxOSX_SYSTEM_FONT_NORMAL
:
329 m_macThemeFontID
= kThemeSystemFont
;
331 case wxOSX_SYSTEM_FONT_BOLD
:
332 m_macThemeFontID
= kThemeEmphasizedSystemFont
;
334 case wxOSX_SYSTEM_FONT_SMALL
:
335 m_macThemeFontID
= kThemeSmallSystemFont
;
337 case wxOSX_SYSTEM_FONT_SMALL_BOLD
:
338 m_macThemeFontID
= kThemeSmallEmphasizedSystemFont
;
340 case wxOSX_SYSTEM_FONT_MINI
:
341 m_macThemeFontID
= kThemeMiniSystemFont
;
343 case wxOSX_SYSTEM_FONT_MINI_BOLD
:
344 // bold not available under theming
345 m_macThemeFontID
= kThemeMiniSystemFont
;
347 case wxOSX_SYSTEM_FONT_LABELS
:
348 m_macThemeFontID
= kThemeLabelFont
;
350 case wxOSX_SYSTEM_FONT_VIEWS
:
351 m_macThemeFontID
= kThemeViewsFont
;
356 if ( m_info
.m_faceName
.empty() )
362 GetThemeFont( m_macThemeFontID
, GetApplicationScript(), qdFontName
, &fontSize
, &style
);
366 wxFontStyle fontstyle
= wxFONTSTYLE_NORMAL
;
367 wxFontWeight fontweight
= wxFONTWEIGHT_NORMAL
;
368 bool underlined
= false;
371 fontweight
= wxFONTWEIGHT_BOLD
;
373 fontweight
= wxFONTWEIGHT_NORMAL
;
374 if ( style
& italic
)
375 fontstyle
= wxFONTSTYLE_ITALIC
;
376 if ( style
& underline
)
379 m_info
.Init(fontSize
,wxFONTFAMILY_DEFAULT
,fontstyle
,fontweight
,underlined
,
380 wxMacMakeStringFromPascal( qdFontName
), wxFONTENCODING_DEFAULT
);
385 m_nsFont
= wxFont::OSXCreateNSFont( font
, &m_info
);
388 m_uiFont
= wxFont::OSXCreateUIFont( font
, &m_info
);
392 void wxFontRefData::MacFindFont()
397 m_info
.EnsureValid();
399 #if wxOSX_USE_CORE_TEXT
400 if ( UMAGetSystemVersion() >= 0x1050 )
402 CTFontSymbolicTraits traits
= 0;
404 if (m_info
.m_weight
== wxFONTWEIGHT_BOLD
)
405 traits
|= kCTFontBoldTrait
;
406 if (m_info
.m_style
== wxFONTSTYLE_ITALIC
|| m_info
.m_style
== wxFONTSTYLE_SLANT
)
407 traits
|= kCTFontItalicTrait
;
410 wxString lookupnameWithSize
= wxString::Format( "%s_%ld_%ld", m_info
.m_faceName
.c_str(), traits
, m_info
.m_pointSize
);
412 static std::map
< std::wstring
, wxCFRef
< CTFontRef
> > fontcache
;
413 m_ctFont
= fontcache
[ std::wstring(lookupnameWithSize
.wc_str()) ];
416 m_ctFont
.reset( CTFontCreateWithFontDescriptor( m_info
.m_ctFontDescriptor
, 0/*m_pointSize */, NULL
) );
418 m_cgFont
.reset(CTFontCopyGraphicsFont(m_ctFont
, NULL
));
422 #if wxOSX_USE_ATSU_TEXT
424 // we try to get as much styles as possible into ATSU
426 OSStatus status
= ::ATSUCreateStyle(&m_macATSUStyle
);
427 wxASSERT_MSG( status
== noErr
, wxT("couldn't create ATSU style") );
429 ATSUAttributeTag atsuTags
[] =
433 kATSUVerticalCharacterTag
,
436 kATSUQDUnderlineTag
,
437 kATSUQDCondensedTag
,
440 ByteCount atsuSizes
[WXSIZEOF(atsuTags
)] =
442 sizeof( ATSUFontID
) ,
444 sizeof( ATSUVerticalCharacterType
),
452 Boolean kTrue
= true ;
453 Boolean kFalse
= false ;
455 Fixed atsuSize
= IntToFixed( m_info
.m_pointSize
);
456 ATSUVerticalCharacterType kHorizontal
= kATSUStronglyHorizontal
;
457 FMFontStyle addQDStyle
= m_info
.m_atsuAdditionalQDStyles
;
458 ATSUAttributeValuePtr atsuValues
[WXSIZEOF(atsuTags
)] =
460 &m_info
.m_atsuFontID
,
463 (addQDStyle
& bold
) ? &kTrue
: &kFalse
,
464 (addQDStyle
& italic
) ? &kTrue
: &kFalse
,
465 (addQDStyle
& underline
) ? &kTrue
: &kFalse
,
466 (addQDStyle
& condense
) ? &kTrue
: &kFalse
,
467 (addQDStyle
& extend
) ? &kTrue
: &kFalse
,
470 status
= ::ATSUSetAttributes(
471 (ATSUStyle
)m_macATSUStyle
,
473 atsuTags
, atsuSizes
, atsuValues
);
475 wxASSERT_MSG( status
== noErr
, wxT("couldn't modify ATSU style") );
476 if ( m_cgFont
.get() == NULL
)
478 ATSFontRef fontRef
= FMGetATSFontRefFromFont(m_info
.m_atsuFontID
);
479 m_cgFont
.reset( CGFontCreateWithPlatformFont( &fontRef
) );
484 m_nsFont
= wxFont::OSXCreateNSFont( &m_info
);
487 m_uiFont
= wxFont::OSXCreateUIFont( &m_info
);
492 // ----------------------------------------------------------------------------
494 // ----------------------------------------------------------------------------
496 bool wxFont::Create(const wxNativeFontInfo
& info
)
500 m_refData
= new wxFontRefData( info
);
506 wxFont::wxFont(const wxString
& fontdesc
)
508 wxNativeFontInfo info
;
509 if ( info
.FromString(fontdesc
) )
513 bool wxFont::Create(int pointSize
,
518 const wxString
& faceNameParam
,
519 wxFontEncoding encoding
)
523 wxString faceName
= faceNameParam
;
525 if ( faceName
.empty() )
529 case wxFONTFAMILY_DEFAULT
:
530 faceName
= wxT("Lucida Grande");
533 case wxFONTFAMILY_SCRIPT
:
534 case wxFONTFAMILY_ROMAN
:
535 case wxFONTFAMILY_DECORATIVE
:
536 faceName
= wxT("Times");
539 case wxFONTFAMILY_SWISS
:
540 faceName
= wxT("Helvetica");
543 case wxFONTFAMILY_MODERN
:
544 case wxFONTFAMILY_TELETYPE
:
545 faceName
= wxT("Courier");
549 faceName
= wxT("Times");
554 wxNativeFontInfo info
;
556 info
.Init(pointSize
, family
, style
, weight
,
557 underlined
, faceName
, encoding
);
559 m_refData
= new wxFontRefData(info
);
564 bool wxFont::CreateSystemFont(wxOSXSystemFont font
)
568 m_refData
= new wxFontRefData( font
, 0 );
577 void wxFont::DoSetNativeFontInfo(const wxNativeFontInfo
& info
)
581 m_refData
= new wxFontRefData( info
);
585 bool wxFont::RealizeResource()
587 M_FONTDATA
->MacFindFont();
592 void wxFont::SetEncoding(wxFontEncoding encoding
)
596 M_FONTDATA
->SetEncoding( encoding
);
599 wxGDIRefData
*wxFont::CreateGDIRefData() const
601 return new wxFontRefData
;
604 wxGDIRefData
*wxFont::CloneGDIRefData(const wxGDIRefData
*data
) const
606 return new wxFontRefData(*static_cast<const wxFontRefData
*>(data
));
609 void wxFont::SetPointSize(int pointSize
)
611 if ( M_FONTDATA
->GetPointSize() == pointSize
)
616 M_FONTDATA
->SetPointSize( pointSize
);
619 void wxFont::SetFamily(wxFontFamily family
)
623 M_FONTDATA
->SetFamily( family
);
626 void wxFont::SetStyle(wxFontStyle style
)
630 M_FONTDATA
->SetStyle( style
);
633 void wxFont::SetWeight(wxFontWeight weight
)
637 M_FONTDATA
->SetWeight( weight
);
640 bool wxFont::SetFaceName(const wxString
& faceName
)
644 M_FONTDATA
->SetFaceName( faceName
);
646 return wxFontBase::SetFaceName(faceName
);
649 void wxFont::SetUnderlined(bool underlined
)
653 M_FONTDATA
->SetUnderlined( underlined
);
656 void wxFont::SetNoAntiAliasing( bool no
)
660 M_FONTDATA
->SetNoAntiAliasing( no
);
663 // ----------------------------------------------------------------------------
665 // ----------------------------------------------------------------------------
667 // TODO: insert checks everywhere for M_FONTDATA == NULL!
669 int wxFont::GetPointSize() const
671 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
673 return M_FONTDATA
->GetPointSize();
676 wxSize
wxFont::GetPixelSize() const
678 #if wxUSE_GRAPHICS_CONTEXT
679 // TODO: consider caching the value
680 wxGraphicsContext
* dc
= wxGraphicsContext::CreateFromNative((CGContextRef
) NULL
);
681 dc
->SetFont(*(wxFont
*)this,*wxBLACK
);
682 wxDouble width
, height
= 0;
683 dc
->GetTextExtent( wxT("g"), &width
, &height
, NULL
, NULL
);
685 return wxSize((int)width
, (int)height
);
687 return wxFontBase::GetPixelSize();
691 wxFontFamily
wxFont::GetFamily() const
693 wxCHECK_MSG( M_FONTDATA
!= NULL
, wxFONTFAMILY_MAX
, wxT("invalid font") );
695 return M_FONTDATA
->GetFamily();
698 wxFontStyle
wxFont::GetStyle() const
700 wxCHECK_MSG( M_FONTDATA
!= NULL
, wxFONTSTYLE_MAX
, wxT("invalid font") );
702 return M_FONTDATA
->GetStyle() ;
705 wxFontWeight
wxFont::GetWeight() const
707 wxCHECK_MSG( M_FONTDATA
!= NULL
, wxFONTWEIGHT_MAX
, wxT("invalid font") );
709 return M_FONTDATA
->GetWeight();
712 bool wxFont::GetUnderlined() const
714 wxCHECK_MSG( M_FONTDATA
!= NULL
, false, wxT("invalid font") );
716 return M_FONTDATA
->GetUnderlined();
719 wxString
wxFont::GetFaceName() const
721 wxCHECK_MSG( M_FONTDATA
!= NULL
, wxEmptyString
, wxT("invalid font") );
723 return M_FONTDATA
->GetFaceName() ;
726 wxFontEncoding
wxFont::GetEncoding() const
728 wxCHECK_MSG( M_FONTDATA
!= NULL
, wxFONTENCODING_DEFAULT
, wxT("invalid font") );
730 return M_FONTDATA
->GetEncoding() ;
733 bool wxFont::GetNoAntiAliasing() const
735 wxCHECK_MSG( M_FONTDATA
!= NULL
, false, wxT("invalid font") );
737 return M_FONTDATA
->GetNoAntiAliasing();
740 #if wxOSX_USE_ATSU_TEXT && wxOSX_USE_CARBON
742 short wxFont::MacGetFontNum() const
744 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
746 // cast away constness otherwise lazy font resolution is not possible
747 const_cast<wxFont
*>(this)->RealizeResource();
749 return M_FONTDATA
->m_info
.m_qdFontFamily
;
752 wxByte
wxFont::MacGetFontStyle() const
754 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
756 // cast away constness otherwise lazy font resolution is not possible
757 const_cast<wxFont
*>(this)->RealizeResource();
759 return M_FONTDATA
->m_info
.m_qdFontStyle
;
762 wxUint16
wxFont::MacGetThemeFontID() const
764 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
766 return M_FONTDATA
->m_macThemeFontID
;
771 #if wxOSX_USE_ATSU_TEXT
772 void * wxFont::MacGetATSUStyle() const
774 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
776 // cast away constness otherwise lazy font resolution is not possible
777 const_cast<wxFont
*>(this)->RealizeResource();
779 return M_FONTDATA
->m_macATSUStyle
;
782 #if WXWIN_COMPATIBILITY_2_8
783 wxUint32
wxFont::MacGetATSUFontID() const
785 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
787 // cast away constness otherwise lazy font resolution is not possible
788 const_cast<wxFont
*>(this)->RealizeResource();
790 return M_FONTDATA
->m_info
.m_atsuFontID
;
793 wxUint32
wxFont::MacGetATSUAdditionalQDStyles() const
795 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
797 // cast away constness otherwise lazy font resolution is not possible
798 const_cast<wxFont
*>(this)->RealizeResource();
800 return M_FONTDATA
->m_info
.m_atsuAdditionalQDStyles
;
806 #if wxOSX_USE_CORE_TEXT
808 CTFontRef
wxFont::OSXGetCTFont() const
810 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
812 // cast away constness otherwise lazy font resolution is not possible
813 const_cast<wxFont
*>(this)->RealizeResource();
815 return (CTFontRef
)(M_FONTDATA
->m_ctFont
);
820 #if wxOSX_USE_COCOA_OR_CARBON
822 CGFontRef
wxFont::OSXGetCGFont() const
824 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
826 // cast away constness otherwise lazy font resolution is not possible
827 const_cast<wxFont
*>(this)->RealizeResource();
829 return (M_FONTDATA
->m_cgFont
);
837 NSFont
* wxFont::OSXGetNSFont() const
839 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
841 // cast away constness otherwise lazy font resolution is not possible
842 const_cast<wxFont
*>(this)->RealizeResource();
844 return (M_FONTDATA
->m_nsFont
);
849 const wxNativeFontInfo
* wxFont::GetNativeFontInfo() const
851 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
852 wxCHECK_MSG( Ok(), NULL
, wxT("invalid font") );
854 // cast away constness otherwise lazy font resolution is not possible
855 const_cast<wxFont
*>(this)->RealizeResource();
857 // M_FONTDATA->m_info.InitFromFont(*this);
859 return &(M_FONTDATA
->m_info
);
862 // ----------------------------------------------------------------------------
864 // ----------------------------------------------------------------------------
866 #if wxOSX_USE_CORE_TEXT
868 /* from Core Text Manual Common Operations */
870 static CTFontDescriptorRef
wxMacCreateCTFontDescriptor(CFStringRef iFamilyName
, CTFontSymbolicTraits iTraits
)
872 CTFontDescriptorRef descriptor
= NULL
;
873 CFMutableDictionaryRef attributes
;
875 wxASSERT(iFamilyName
!= NULL
);
876 // Create a mutable dictionary to hold our attributes.
877 attributes
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
878 &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
879 wxASSERT(attributes
!= NULL
);
881 if (attributes
!= NULL
) {
882 // Add a family name to our attributes.
883 CFDictionaryAddValue(attributes
, kCTFontFamilyNameAttribute
, iFamilyName
);
887 CFMutableDictionaryRef traits
;
888 CFNumberRef symTraits
;
890 // Create the traits dictionary.
891 symTraits
= CFNumberCreate(kCFAllocatorDefault
, kCFNumberSInt32Type
,
893 wxASSERT(symTraits
!= NULL
);
895 if (symTraits
!= NULL
) {
896 // Create a dictionary to hold our traits values.
897 traits
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
898 &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
899 wxASSERT(traits
!= NULL
);
901 if (traits
!= NULL
) {
902 // Add the symbolic traits value to the traits dictionary.
903 CFDictionaryAddValue(traits
, kCTFontSymbolicTrait
, symTraits
);
905 // Add the traits attribute to our attributes.
906 CFDictionaryAddValue(attributes
, kCTFontTraitsAttribute
, traits
);
909 CFRelease(symTraits
);
912 // Create the font descriptor with our attributes
913 descriptor
= CTFontDescriptorCreateWithAttributes(attributes
);
914 wxASSERT(descriptor
!= NULL
);
916 CFRelease(attributes
);
918 // Return our font descriptor.
924 void wxNativeFontInfo::Init()
926 #if wxOSX_USE_CORE_TEXT
927 m_ctFontDescriptor
= NULL
;
929 #if wxOSX_USE_ATSU_TEXT
931 m_atsuAdditionalQDStyles
= 0;
932 m_atsuFontValid
= false;
939 m_nsFontDescriptor
= NULL
;
942 m_family
= wxFONTFAMILY_DEFAULT
;
943 m_style
= wxFONTSTYLE_NORMAL
;
944 m_weight
= wxFONTWEIGHT_NORMAL
;
945 m_underlined
= false;
947 m_encoding
= wxFont::GetDefaultEncoding();
948 m_descriptorValid
= false;
951 #if wxOSX_USE_CORE_TEXT
952 void wxNativeFontInfo::Init(CTFontDescriptorRef descr
)
955 m_ctFontDescriptor
= wxCFRetain(descr
);
957 wxCFRef
< CFNumberRef
> sizevalue( (CFNumberRef
) CTFontDescriptorCopyAttribute( m_ctFontDescriptor
, kCTFontSizeAttribute
) );
959 if ( CFNumberGetValue( sizevalue
, kCFNumberFloatType
, &fsize
) )
960 m_pointSize
= (int)( fsize
+ 0.5 );
962 wxCFRef
< CFDictionaryRef
> traitsvalue( (CFDictionaryRef
) CTFontDescriptorCopyAttribute( m_ctFontDescriptor
, kCTFontTraitsAttribute
) );
963 CTFontSymbolicTraits traits
;
964 if ( CFNumberGetValue((CFNumberRef
) CFDictionaryGetValue(traitsvalue
,kCTFontSymbolicTrait
),kCFNumberIntType
,&traits
) )
966 if ( traits
& kCTFontItalicTrait
)
967 m_style
= wxFONTSTYLE_ITALIC
;
968 if ( traits
& kCTFontBoldTrait
)
969 m_weight
= wxFONTWEIGHT_BOLD
;
972 wxCFStringRef
familyName( (CFStringRef
) CTFontDescriptorCopyAttribute(m_ctFontDescriptor
, kCTFontFamilyNameAttribute
));
973 m_faceName
= familyName
.AsString();
977 void wxNativeFontInfo::EnsureValid()
979 if ( m_descriptorValid
)
982 #if wxOSX_USE_CORE_TEXT
983 if ( m_ctFontDescriptor
== NULL
&& UMAGetSystemVersion() >= 0x1050 )
985 CTFontSymbolicTraits traits
= 0;
987 if (m_weight
== wxFONTWEIGHT_BOLD
)
988 traits
|= kCTFontBoldTrait
;
989 if (m_style
== wxFONTSTYLE_ITALIC
|| m_style
== wxFONTSTYLE_SLANT
)
990 traits
|= kCTFontItalicTrait
;
993 wxString lookupnameWithSize
= wxString::Format( "%s_%ld_%ld", m_faceName
.c_str(), traits
, m_pointSize
);
995 static std::map
< std::wstring
, wxCFRef
< CTFontDescriptorRef
> > fontdescriptorcache
;
996 m_ctFontDescriptor
= wxCFRetain((CTFontDescriptorRef
)fontdescriptorcache
[ std::wstring(lookupnameWithSize
.wc_str()) ]);
997 if ( !m_ctFontDescriptor
)
999 // QD selection algorithm is the fastest by orders of magnitude on 10.5
1000 if ( m_faceName
.IsAscii() )
1002 uint8_t qdstyle
= 0;
1003 if (m_weight
== wxFONTWEIGHT_BOLD
)
1005 if (m_style
== wxFONTSTYLE_ITALIC
|| m_style
== wxFONTSTYLE_SLANT
)
1009 wxMacStringToPascal( m_faceName
, qdFontName
);
1010 wxCFRef
< CTFontRef
> font
;
1011 font
.reset( CTFontCreateWithQuickdrawInstance(qdFontName
, 0 , qdstyle
, m_pointSize
) );
1012 m_ctFontDescriptor
= CTFontCopyFontDescriptor(font
);
1016 m_ctFontDescriptor
= wxMacCreateCTFontDescriptor( wxCFStringRef(m_faceName
),traits
);
1018 fontdescriptorcache
[ std::wstring(lookupnameWithSize
.wc_str()) ].reset(wxCFRetain(m_ctFontDescriptor
));
1022 #if wxOSX_USE_ATSU_TEXT
1023 if ( !m_atsuFontValid
)
1025 #if !wxOSX_USE_CARBON
1026 // not needed outside
1027 wxInt16 m_qdFontFamily
;
1028 wxInt16 m_qdFontStyle
;
1030 wxCFStringRef
cf( m_faceName
, wxLocale::GetSystemEncoding() );
1031 ATSFontFamilyRef atsfamily
= ATSFontFamilyFindFromName( cf
, kATSOptionFlagsDefault
);
1032 if ( atsfamily
== (ATSFontFamilyRef
) -1 )
1034 wxLogDebug( wxT("ATSFontFamilyFindFromName failed for ") + m_faceName
);
1035 m_qdFontFamily
= GetAppFont();
1039 m_qdFontFamily
= FMGetFontFamilyFromATSFontFamilyRef( atsfamily
);
1043 if (m_weight
== wxFONTWEIGHT_BOLD
)
1044 m_qdFontStyle
|= bold
;
1045 if (m_style
== wxFONTSTYLE_ITALIC
|| m_style
== wxFONTSTYLE_SLANT
)
1046 m_qdFontStyle
|= italic
;
1048 m_qdFontStyle
|= underline
;
1051 // we try to get as much styles as possible into ATSU
1053 // ATSUFontID and FMFont are equivalent
1054 FMFontStyle intrinsicStyle
= 0 ;
1055 OSStatus status
= FMGetFontFromFontFamilyInstance( m_qdFontFamily
, m_qdFontStyle
, (FMFont
*)&m_atsuFontID
, &intrinsicStyle
);
1056 if ( status
!= noErr
)
1058 wxFAIL_MSG( wxT("couldn't get an ATSUFont from font family") );
1060 m_atsuAdditionalQDStyles
= m_qdFontStyle
& (~intrinsicStyle
);
1061 m_atsuFontValid
= true;
1065 if ( m_nsFontDescriptor
== NULL
)
1066 OSXValidateNSFontDescriptor();
1068 #if wxOSX_USE_IPHONE
1071 m_descriptorValid
= true;
1074 void wxNativeFontInfo::Init(const wxNativeFontInfo
& info
)
1077 #if wxOSX_USE_CORE_TEXT
1078 m_ctFontDescriptor
= wxCFRetain(info
.m_ctFontDescriptor
);
1080 #if wxOSX_USE_ATSU_TEXT
1081 m_atsuFontValid
= info
.m_atsuFontValid
;
1082 m_atsuFontID
= info
.m_atsuFontID
;
1083 m_atsuAdditionalQDStyles
= info
.m_atsuAdditionalQDStyles
;
1084 #if wxOSX_USE_CARBON
1085 m_qdFontFamily
= info
.m_qdFontFamily
;
1086 m_qdFontStyle
= info
.m_qdFontStyle
;
1090 m_nsFontDescriptor
= (NSFontDescriptor
*) wxMacCocoaRetain(info
.m_nsFontDescriptor
);
1092 m_pointSize
= info
.m_pointSize
;
1093 m_family
= info
.m_family
;
1094 m_style
= info
.m_style
;
1095 m_weight
= info
.m_weight
;
1096 m_underlined
= info
.m_underlined
;
1097 m_faceName
= info
.m_faceName
;
1098 m_encoding
= info
.m_encoding
;
1099 m_descriptorValid
= info
.m_descriptorValid
;
1102 void wxNativeFontInfo::Init(int size
,
1103 wxFontFamily family
,
1105 wxFontWeight weight
,
1107 const wxString
& faceName
,
1108 wxFontEncoding encoding
)
1115 m_underlined
= underlined
;
1116 m_faceName
= faceName
;
1117 if ( encoding
== wxFONTENCODING_DEFAULT
)
1118 encoding
= wxFont::GetDefaultEncoding();
1119 m_encoding
= encoding
;
1123 void wxNativeFontInfo::Free()
1125 #if wxOSX_USE_CORE_TEXT
1126 wxCFRelease(m_ctFontDescriptor
);
1127 m_ctFontDescriptor
= NULL
;
1129 #if wxOSX_USE_ATSU_TEXT
1131 m_atsuAdditionalQDStyles
= 0;
1132 m_atsuFontValid
= false;
1135 wxMacCocoaRelease(m_nsFontDescriptor
);
1136 m_nsFontDescriptor
= NULL
;
1138 m_descriptorValid
= false;
1141 bool wxNativeFontInfo::FromString(const wxString
& s
)
1145 wxStringTokenizer
tokenizer(s
, wxT(";"));
1147 wxString token
= tokenizer
.GetNextToken();
1149 // Ignore the version for now
1152 token
= tokenizer
.GetNextToken();
1153 if ( !token
.ToLong(&l
) )
1155 m_pointSize
= (int)l
;
1157 token
= tokenizer
.GetNextToken();
1158 if ( !token
.ToLong(&l
) )
1160 m_family
= (wxFontFamily
)l
;
1162 token
= tokenizer
.GetNextToken();
1163 if ( !token
.ToLong(&l
) )
1165 m_style
= (wxFontStyle
)l
;
1167 token
= tokenizer
.GetNextToken();
1168 if ( !token
.ToLong(&l
) )
1170 m_weight
= (wxFontWeight
)l
;
1172 token
= tokenizer
.GetNextToken();
1173 if ( !token
.ToLong(&l
) )
1175 m_underlined
= l
!= 0;
1177 m_faceName
= tokenizer
.GetNextToken();
1184 token
= tokenizer
.GetNextToken();
1185 if ( !token
.ToLong(&l
) )
1187 m_encoding
= (wxFontEncoding
)l
;
1192 wxString
wxNativeFontInfo::ToString() const
1196 s
.Printf(wxT("%d;%d;%d;%d;%d;%d;%s;%d"),
1203 m_faceName
.GetData(),
1209 int wxNativeFontInfo::GetPointSize() const
1214 wxFontStyle
wxNativeFontInfo::GetStyle() const
1219 wxFontWeight
wxNativeFontInfo::GetWeight() const
1224 bool wxNativeFontInfo::GetUnderlined() const
1226 return m_underlined
;
1229 wxString
wxNativeFontInfo::GetFaceName() const
1234 wxFontFamily
wxNativeFontInfo::GetFamily() const
1239 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
1244 // changing the font descriptor
1246 void wxNativeFontInfo::SetPointSize(int pointsize
)
1248 if ( m_pointSize
!= pointsize
)
1250 m_pointSize
= pointsize
;
1255 void wxNativeFontInfo::SetStyle(wxFontStyle style_
)
1257 if ( m_style
!= style_
)
1264 void wxNativeFontInfo::SetWeight(wxFontWeight weight_
)
1266 if ( m_weight
!= weight_
)
1273 void wxNativeFontInfo::SetUnderlined(bool underlined_
)
1275 if ( m_underlined
!= underlined_
)
1277 m_underlined
= underlined_
;
1282 bool wxNativeFontInfo::SetFaceName(const wxString
& facename_
)
1284 if ( m_faceName
!= facename_
)
1286 m_faceName
= facename_
;
1292 void wxNativeFontInfo::SetFamily(wxFontFamily family_
)
1294 if ( m_family
!= family_
)
1301 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding_
)
1303 if ( encoding_
== wxFONTENCODING_DEFAULT
)
1304 encoding_
= wxFont::GetDefaultEncoding();
1305 m_encoding
= encoding_
;
1306 // not reflected in native descriptors