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 SetPointSize( int size
)
66 if( GetPointSize() != size
)
68 m_info
.SetPointSize(size
);
73 int GetPointSize() const { return m_info
.GetPointSize(); }
75 void SetFamily( wxFontFamily family
)
77 if ( m_info
.m_family
!= family
)
79 m_info
.SetFamily( family
);
84 wxFontFamily
GetFamily() const { return m_info
.GetFamily(); }
86 void SetStyle( wxFontStyle style
)
88 if ( m_info
.m_style
!= style
)
90 m_info
.SetStyle( style
);
96 wxFontStyle
GetStyle() const { return m_info
.GetStyle(); }
98 void SetWeight( wxFontWeight weight
)
100 if ( m_info
.m_weight
!= weight
)
102 m_info
.SetWeight( weight
);
108 wxFontWeight
GetWeight() const { return m_info
.GetWeight(); }
110 void SetUnderlined( bool u
)
112 if ( m_info
.m_underlined
!= u
)
114 m_info
.SetUnderlined( u
);
119 bool GetUnderlined() const { return m_info
.GetUnderlined(); }
121 void SetFaceName( const wxString
& facename
)
123 if ( m_info
.m_faceName
!= facename
)
125 m_info
.SetFaceName( facename
);
130 wxString
GetFaceName() const { return m_info
.GetFaceName(); }
132 void SetEncoding( wxFontEncoding encoding
)
134 if ( m_info
.m_encoding
!= encoding
)
136 m_info
.SetEncoding( encoding
);
141 wxFontEncoding
GetEncoding() const { return m_info
.GetEncoding(); }
148 // common part of all ctors
150 #if wxOSX_USE_CORE_TEXT
151 // void Init( CTFontRef font );
155 #if wxOSX_USE_CARBON && wxOSX_USE_ATSU_TEXT
156 // for true theming support we must store the correct font
157 // information here, as this speeds up and optimizes rendering
158 ThemeFontID m_macThemeFontID
;
160 #if wxOSX_USE_CORE_TEXT
161 wxCFRef
<CTFontRef
> m_ctFont
;
163 #if wxOSX_USE_ATSU_TEXT
164 ATSUStyle m_macATSUStyle
;
166 wxCFRef
<CGFontRef
> m_cgFont
;
173 wxNativeFontInfo m_info
;
176 #define M_FONTDATA ((wxFontRefData*)m_refData)
178 wxFontRefData::wxFontRefData(const wxFontRefData
& data
)
181 m_info
= data
.m_info
;
182 m_fontValid
= data
.m_fontValid
;
183 #if wxOSX_USE_CARBON && wxOSX_USE_ATSU_TEXT
184 m_macThemeFontID
= data
.m_macThemeFontID
;
186 #if wxOSX_USE_CORE_TEXT
187 m_ctFont
= data
.m_ctFont
;
189 m_cgFont
= data
.m_cgFont
;
190 #if wxOSX_USE_ATSU_TEXT
191 if ( data
.m_macATSUStyle
!= NULL
)
193 ATSUCreateStyle(&m_macATSUStyle
) ;
194 ATSUCopyAttributes(data
.m_macATSUStyle
, m_macATSUStyle
);
198 m_nsFont
= (NSFont
*) wxMacCocoaRetain(data
.m_nsFont
);
201 m_uiFont
= (UIFont
*) wxMacCocoaRetain(data
.m_uiFont
);
206 // ============================================================================
208 // ============================================================================
210 // ----------------------------------------------------------------------------
212 // ----------------------------------------------------------------------------
214 void wxFontRefData::Init()
216 #if wxOSX_USE_CARBON && wxOSX_USE_ATSU_TEXT
217 m_macThemeFontID
= kThemeCurrentPortFont
;
219 #if wxOSX_USE_ATSU_TEXT
220 m_macATSUStyle
= NULL
;
231 wxFontRefData::~wxFontRefData()
236 void wxFontRefData::Free()
238 #if wxOSX_USE_CORE_TEXT
242 #if wxOSX_USE_ATSU_TEXT
244 m_macThemeFontID
= kThemeCurrentPortFont
;
246 if ( m_macATSUStyle
)
248 ::ATSUDisposeStyle((ATSUStyle
)m_macATSUStyle
);
249 m_macATSUStyle
= NULL
;
253 if (m_nsFont
!= NULL
)
255 wxMacCocoaRelease(m_nsFont
);
260 if (m_uiFont
!= NULL
)
262 wxMacCocoaRelease(m_uiFont
);
269 wxFontRefData::wxFontRefData(wxOSXSystemFont font
, int size
)
271 wxASSERT( font
!= wxOSX_SYSTEM_FONT_NONE
);
274 #if wxOSX_USE_CORE_TEXT
275 if ( UMAGetSystemVersion() >= 0x1050 )
277 CTFontUIFontType uifont
= kCTFontSystemFontType
;
280 case wxOSX_SYSTEM_FONT_NORMAL
:
281 uifont
= kCTFontSystemFontType
;
283 case wxOSX_SYSTEM_FONT_BOLD
:
284 uifont
= kCTFontEmphasizedSystemFontType
;
286 case wxOSX_SYSTEM_FONT_SMALL
:
287 uifont
= kCTFontSmallSystemFontType
;
289 case wxOSX_SYSTEM_FONT_SMALL_BOLD
:
290 uifont
= kCTFontSmallEmphasizedSystemFontType
;
292 case wxOSX_SYSTEM_FONT_MINI
:
293 uifont
= kCTFontMiniSystemFontType
;
295 case wxOSX_SYSTEM_FONT_MINI_BOLD
:
296 uifont
= kCTFontMiniEmphasizedSystemFontType
;
298 case wxOSX_SYSTEM_FONT_LABELS
:
299 uifont
= kCTFontLabelFontType
;
301 case wxOSX_SYSTEM_FONT_VIEWS
:
302 uifont
= kCTFontViewsFontType
;
307 m_ctFont
.reset(CTFontCreateUIFontForLanguage( uifont
, (CGFloat
) size
, NULL
));
308 wxCFRef
<CTFontDescriptorRef
> descr
;
309 descr
.reset( CTFontCopyFontDescriptor( m_ctFont
) );
313 #if wxOSX_USE_ATSU_TEXT
315 #if !wxOSX_USE_CARBON
316 // not needed outside
317 ThemeFontID m_macThemeFontID
= kThemeSystemFont
;
321 case wxOSX_SYSTEM_FONT_NORMAL
:
322 m_macThemeFontID
= kThemeSystemFont
;
324 case wxOSX_SYSTEM_FONT_BOLD
:
325 m_macThemeFontID
= kThemeEmphasizedSystemFont
;
327 case wxOSX_SYSTEM_FONT_SMALL
:
328 m_macThemeFontID
= kThemeSmallSystemFont
;
330 case wxOSX_SYSTEM_FONT_SMALL_BOLD
:
331 m_macThemeFontID
= kThemeSmallEmphasizedSystemFont
;
333 case wxOSX_SYSTEM_FONT_MINI
:
334 m_macThemeFontID
= kThemeMiniSystemFont
;
336 case wxOSX_SYSTEM_FONT_MINI_BOLD
:
337 // bold not available under theming
338 m_macThemeFontID
= kThemeMiniSystemFont
;
340 case wxOSX_SYSTEM_FONT_LABELS
:
341 m_macThemeFontID
= kThemeLabelFont
;
343 case wxOSX_SYSTEM_FONT_VIEWS
:
344 m_macThemeFontID
= kThemeViewsFont
;
349 if ( m_info
.m_faceName
.empty() )
355 GetThemeFont( m_macThemeFontID
, GetApplicationScript(), qdFontName
, &fontSize
, &style
);
359 wxFontStyle fontstyle
= wxFONTSTYLE_NORMAL
;
360 wxFontWeight fontweight
= wxFONTWEIGHT_NORMAL
;
361 bool underlined
= false;
364 fontweight
= wxFONTWEIGHT_BOLD
;
366 fontweight
= wxFONTWEIGHT_NORMAL
;
367 if ( style
& italic
)
368 fontstyle
= wxFONTSTYLE_ITALIC
;
369 if ( style
& underline
)
372 m_info
.Init(fontSize
,wxFONTFAMILY_DEFAULT
,fontstyle
,fontweight
,underlined
,
373 wxMacMakeStringFromPascal( qdFontName
), wxFONTENCODING_DEFAULT
);
378 m_nsFont
= wxFont::OSXCreateNSFont( font
, &m_info
);
381 m_uiFont
= wxFont::OSXCreateUIFont( font
, &m_info
);
385 void wxFontRefData::MacFindFont()
390 wxCHECK_RET( m_info
.m_pointSize
> 0, wxT("Point size should not be zero.") );
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
) );
413 m_cgFont
.reset(CTFontCopyGraphicsFont(m_ctFont
, NULL
));
417 #if wxOSX_USE_ATSU_TEXT
419 // we try to get as much styles as possible into ATSU
421 OSStatus status
= ::ATSUCreateStyle(&m_macATSUStyle
);
422 wxASSERT_MSG( status
== noErr
, wxT("couldn't create ATSU style") );
424 ATSUAttributeTag atsuTags
[] =
428 kATSUVerticalCharacterTag
,
431 kATSUQDUnderlineTag
,
432 kATSUQDCondensedTag
,
435 ByteCount atsuSizes
[WXSIZEOF(atsuTags
)] =
437 sizeof( ATSUFontID
) ,
439 sizeof( ATSUVerticalCharacterType
),
447 Boolean kTrue
= true ;
448 Boolean kFalse
= false ;
450 Fixed atsuSize
= IntToFixed( m_info
.m_pointSize
);
451 ATSUVerticalCharacterType kHorizontal
= kATSUStronglyHorizontal
;
452 FMFontStyle addQDStyle
= m_info
.m_atsuAdditionalQDStyles
;
453 ATSUAttributeValuePtr atsuValues
[WXSIZEOF(atsuTags
)] =
455 &m_info
.m_atsuFontID
,
458 (addQDStyle
& bold
) ? &kTrue
: &kFalse
,
459 (addQDStyle
& italic
) ? &kTrue
: &kFalse
,
460 (addQDStyle
& underline
) ? &kTrue
: &kFalse
,
461 (addQDStyle
& condense
) ? &kTrue
: &kFalse
,
462 (addQDStyle
& extend
) ? &kTrue
: &kFalse
,
465 status
= ::ATSUSetAttributes(
466 (ATSUStyle
)m_macATSUStyle
,
468 atsuTags
, atsuSizes
, atsuValues
);
470 wxASSERT_MSG( status
== noErr
, wxString::Format(wxT("couldn't modify ATSU style. Status was %d"), (int) status
).c_str() );
472 if ( m_cgFont
.get() == NULL
)
474 ATSFontRef fontRef
= FMGetATSFontRefFromFont(m_info
.m_atsuFontID
);
475 m_cgFont
.reset( CGFontCreateWithPlatformFont( &fontRef
) );
480 m_nsFont
= wxFont::OSXCreateNSFont( &m_info
);
483 m_uiFont
= wxFont::OSXCreateUIFont( &m_info
);
488 // ----------------------------------------------------------------------------
490 // ----------------------------------------------------------------------------
492 bool wxFont::Create(const wxNativeFontInfo
& info
)
496 m_refData
= new wxFontRefData( info
);
502 wxFont::wxFont(wxOSXSystemFont font
)
504 m_refData
= new wxFontRefData( font
, 0 );
507 wxFont::wxFont(const wxString
& fontdesc
)
509 wxNativeFontInfo info
;
510 if ( info
.FromString(fontdesc
) )
514 bool wxFont::Create(int pointSize
,
519 const wxString
& faceNameParam
,
520 wxFontEncoding encoding
)
524 wxString faceName
= faceNameParam
;
526 if ( faceName
.empty() )
530 case wxFONTFAMILY_DEFAULT
:
531 faceName
= wxT("Lucida Grande");
534 case wxFONTFAMILY_SCRIPT
:
535 case wxFONTFAMILY_ROMAN
:
536 case wxFONTFAMILY_DECORATIVE
:
537 faceName
= wxT("Times");
540 case wxFONTFAMILY_SWISS
:
541 faceName
= wxT("Helvetica");
544 case wxFONTFAMILY_MODERN
:
545 case wxFONTFAMILY_TELETYPE
:
546 faceName
= wxT("Courier");
550 faceName
= wxT("Times");
555 wxNativeFontInfo info
;
557 info
.Init(pointSize
, family
, style
, weight
,
558 underlined
, faceName
, encoding
);
560 m_refData
= new wxFontRefData(info
);
569 void wxFont::DoSetNativeFontInfo(const wxNativeFontInfo
& info
)
573 m_refData
= new wxFontRefData( info
);
577 bool wxFont::RealizeResource()
579 M_FONTDATA
->MacFindFont();
584 void wxFont::SetEncoding(wxFontEncoding encoding
)
588 M_FONTDATA
->SetEncoding( encoding
);
591 wxGDIRefData
*wxFont::CreateGDIRefData() const
593 return new wxFontRefData
;
596 wxGDIRefData
*wxFont::CloneGDIRefData(const wxGDIRefData
*data
) const
598 return new wxFontRefData(*static_cast<const wxFontRefData
*>(data
));
601 void wxFont::SetPointSize(int pointSize
)
603 if ( M_FONTDATA
->GetPointSize() == pointSize
)
608 M_FONTDATA
->SetPointSize( pointSize
);
611 void wxFont::SetFamily(wxFontFamily family
)
615 M_FONTDATA
->SetFamily( family
);
618 void wxFont::SetStyle(wxFontStyle style
)
622 M_FONTDATA
->SetStyle( style
);
625 void wxFont::SetWeight(wxFontWeight weight
)
629 M_FONTDATA
->SetWeight( weight
);
632 bool wxFont::SetFaceName(const wxString
& faceName
)
636 M_FONTDATA
->SetFaceName( faceName
);
638 return wxFontBase::SetFaceName(faceName
);
641 void wxFont::SetUnderlined(bool underlined
)
645 M_FONTDATA
->SetUnderlined( underlined
);
648 // ----------------------------------------------------------------------------
650 // ----------------------------------------------------------------------------
652 // TODO: insert checks everywhere for M_FONTDATA == NULL!
654 int wxFont::GetPointSize() const
656 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
658 return M_FONTDATA
->GetPointSize();
661 wxSize
wxFont::GetPixelSize() const
663 #if wxUSE_GRAPHICS_CONTEXT
664 // TODO: consider caching the value
665 wxGraphicsContext
* dc
= wxGraphicsContext::CreateFromNative((CGContextRef
) NULL
);
666 dc
->SetFont(*(wxFont
*)this,*wxBLACK
);
667 wxDouble width
, height
= 0;
668 dc
->GetTextExtent( wxT("g"), &width
, &height
, NULL
, NULL
);
670 return wxSize((int)width
, (int)height
);
672 return wxFontBase::GetPixelSize();
676 wxFontFamily
wxFont::GetFamily() const
678 wxCHECK_MSG( M_FONTDATA
!= NULL
, wxFONTFAMILY_MAX
, wxT("invalid font") );
680 return M_FONTDATA
->GetFamily();
683 wxFontStyle
wxFont::GetStyle() const
685 wxCHECK_MSG( M_FONTDATA
!= NULL
, wxFONTSTYLE_MAX
, wxT("invalid font") );
687 return M_FONTDATA
->GetStyle() ;
690 wxFontWeight
wxFont::GetWeight() const
692 wxCHECK_MSG( M_FONTDATA
!= NULL
, wxFONTWEIGHT_MAX
, wxT("invalid font") );
694 return M_FONTDATA
->GetWeight();
697 bool wxFont::GetUnderlined() const
699 wxCHECK_MSG( M_FONTDATA
!= NULL
, false, wxT("invalid font") );
701 return M_FONTDATA
->GetUnderlined();
704 wxString
wxFont::GetFaceName() const
706 wxCHECK_MSG( M_FONTDATA
!= NULL
, wxEmptyString
, wxT("invalid font") );
708 return M_FONTDATA
->GetFaceName() ;
711 wxFontEncoding
wxFont::GetEncoding() const
713 wxCHECK_MSG( M_FONTDATA
!= NULL
, wxFONTENCODING_DEFAULT
, wxT("invalid font") );
715 return M_FONTDATA
->GetEncoding() ;
718 #if wxOSX_USE_ATSU_TEXT && wxOSX_USE_CARBON
720 short wxFont::MacGetFontNum() const
722 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
724 // cast away constness otherwise lazy font resolution is not possible
725 const_cast<wxFont
*>(this)->RealizeResource();
727 return M_FONTDATA
->m_info
.m_qdFontFamily
;
730 wxByte
wxFont::MacGetFontStyle() const
732 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
734 // cast away constness otherwise lazy font resolution is not possible
735 const_cast<wxFont
*>(this)->RealizeResource();
737 return M_FONTDATA
->m_info
.m_qdFontStyle
;
740 wxUint16
wxFont::MacGetThemeFontID() const
742 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
744 return M_FONTDATA
->m_macThemeFontID
;
749 #if wxOSX_USE_ATSU_TEXT
750 void * wxFont::MacGetATSUStyle() const
752 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
754 // cast away constness otherwise lazy font resolution is not possible
755 const_cast<wxFont
*>(this)->RealizeResource();
757 return M_FONTDATA
->m_macATSUStyle
;
760 #if WXWIN_COMPATIBILITY_2_8
761 wxUint32
wxFont::MacGetATSUFontID() const
763 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
765 // cast away constness otherwise lazy font resolution is not possible
766 const_cast<wxFont
*>(this)->RealizeResource();
768 return M_FONTDATA
->m_info
.m_atsuFontID
;
771 wxUint32
wxFont::MacGetATSUAdditionalQDStyles() const
773 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
775 // cast away constness otherwise lazy font resolution is not possible
776 const_cast<wxFont
*>(this)->RealizeResource();
778 return M_FONTDATA
->m_info
.m_atsuAdditionalQDStyles
;
784 #if wxOSX_USE_CORE_TEXT
786 CTFontRef
wxFont::OSXGetCTFont() const
788 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
790 // cast away constness otherwise lazy font resolution is not possible
791 const_cast<wxFont
*>(this)->RealizeResource();
793 return (CTFontRef
)(M_FONTDATA
->m_ctFont
);
798 #if wxOSX_USE_COCOA_OR_CARBON
800 CGFontRef
wxFont::OSXGetCGFont() const
802 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
804 // cast away constness otherwise lazy font resolution is not possible
805 const_cast<wxFont
*>(this)->RealizeResource();
807 return (M_FONTDATA
->m_cgFont
);
815 NSFont
* wxFont::OSXGetNSFont() const
817 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
819 // cast away constness otherwise lazy font resolution is not possible
820 const_cast<wxFont
*>(this)->RealizeResource();
822 return (M_FONTDATA
->m_nsFont
);
827 const wxNativeFontInfo
* wxFont::GetNativeFontInfo() const
829 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
830 wxCHECK_MSG( Ok(), NULL
, wxT("invalid font") );
832 // cast away constness otherwise lazy font resolution is not possible
833 const_cast<wxFont
*>(this)->RealizeResource();
835 // M_FONTDATA->m_info.InitFromFont(*this);
837 return &(M_FONTDATA
->m_info
);
840 // ----------------------------------------------------------------------------
842 // ----------------------------------------------------------------------------
844 #if wxOSX_USE_CORE_TEXT
846 /* from Core Text Manual Common Operations */
848 static CTFontDescriptorRef
wxMacCreateCTFontDescriptor(CFStringRef iFamilyName
, CTFontSymbolicTraits iTraits
)
850 CTFontDescriptorRef descriptor
= NULL
;
851 CFMutableDictionaryRef attributes
;
853 wxASSERT(iFamilyName
!= NULL
);
854 // Create a mutable dictionary to hold our attributes.
855 attributes
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
856 &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
857 wxASSERT(attributes
!= NULL
);
859 if (attributes
!= NULL
) {
860 // Add a family name to our attributes.
861 CFDictionaryAddValue(attributes
, kCTFontFamilyNameAttribute
, iFamilyName
);
865 CFMutableDictionaryRef traits
;
866 CFNumberRef symTraits
;
868 // Create the traits dictionary.
869 symTraits
= CFNumberCreate(kCFAllocatorDefault
, kCFNumberSInt32Type
,
871 wxASSERT(symTraits
!= NULL
);
873 if (symTraits
!= NULL
) {
874 // Create a dictionary to hold our traits values.
875 traits
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
876 &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
877 wxASSERT(traits
!= NULL
);
879 if (traits
!= NULL
) {
880 // Add the symbolic traits value to the traits dictionary.
881 CFDictionaryAddValue(traits
, kCTFontSymbolicTrait
, symTraits
);
883 // Add the traits attribute to our attributes.
884 CFDictionaryAddValue(attributes
, kCTFontTraitsAttribute
, traits
);
887 CFRelease(symTraits
);
890 // Create the font descriptor with our attributes
891 descriptor
= CTFontDescriptorCreateWithAttributes(attributes
);
892 wxASSERT(descriptor
!= NULL
);
894 CFRelease(attributes
);
896 // Return our font descriptor.
902 void wxNativeFontInfo::Init()
904 #if wxOSX_USE_CORE_TEXT
905 m_ctFontDescriptor
= NULL
;
907 #if wxOSX_USE_ATSU_TEXT
909 m_atsuAdditionalQDStyles
= 0;
910 m_atsuFontValid
= false;
917 m_nsFontDescriptor
= NULL
;
920 m_family
= wxFONTFAMILY_DEFAULT
;
921 m_style
= wxFONTSTYLE_NORMAL
;
922 m_weight
= wxFONTWEIGHT_NORMAL
;
923 m_underlined
= false;
925 m_encoding
= wxFont::GetDefaultEncoding();
926 m_descriptorValid
= false;
929 #if wxOSX_USE_CORE_TEXT
930 void wxNativeFontInfo::Init(CTFontDescriptorRef descr
)
933 m_ctFontDescriptor
= wxCFRetain(descr
);
935 wxCFRef
< CFNumberRef
> sizevalue( (CFNumberRef
) CTFontDescriptorCopyAttribute( m_ctFontDescriptor
, kCTFontSizeAttribute
) );
937 if ( CFNumberGetValue( sizevalue
, kCFNumberFloatType
, &fsize
) )
938 m_pointSize
= (int)( fsize
+ 0.5 );
940 wxCFRef
< CFDictionaryRef
> traitsvalue( (CFDictionaryRef
) CTFontDescriptorCopyAttribute( m_ctFontDescriptor
, kCTFontTraitsAttribute
) );
941 CTFontSymbolicTraits traits
;
942 if ( CFNumberGetValue((CFNumberRef
) CFDictionaryGetValue(traitsvalue
,kCTFontSymbolicTrait
),kCFNumberIntType
,&traits
) )
944 if ( traits
& kCTFontItalicTrait
)
945 m_style
= wxFONTSTYLE_ITALIC
;
946 if ( traits
& kCTFontBoldTrait
)
947 m_weight
= wxFONTWEIGHT_BOLD
;
950 wxCFStringRef
familyName( (CFStringRef
) CTFontDescriptorCopyAttribute(m_ctFontDescriptor
, kCTFontFamilyNameAttribute
));
951 m_faceName
= familyName
.AsString();
955 void wxNativeFontInfo::EnsureValid()
957 if ( m_descriptorValid
)
960 #if wxOSX_USE_CORE_TEXT
961 if ( m_ctFontDescriptor
== NULL
&& UMAGetSystemVersion() >= 0x1050 )
963 CTFontSymbolicTraits traits
= 0;
965 if (m_weight
== wxFONTWEIGHT_BOLD
)
966 traits
|= kCTFontBoldTrait
;
967 if (m_style
== wxFONTSTYLE_ITALIC
|| m_style
== wxFONTSTYLE_SLANT
)
968 traits
|= kCTFontItalicTrait
;
971 wxString lookupnameWithSize
= wxString::Format( "%s_%ld_%ld", m_faceName
.c_str(), traits
, m_pointSize
);
973 static std::map
< std::wstring
, wxCFRef
< CTFontDescriptorRef
> > fontdescriptorcache
;
974 m_ctFontDescriptor
= wxCFRetain((CTFontDescriptorRef
)fontdescriptorcache
[ std::wstring(lookupnameWithSize
.wc_str()) ]);
975 if ( !m_ctFontDescriptor
)
977 // QD selection algorithm is the fastest by orders of magnitude on 10.5
978 if ( m_faceName
.IsAscii() )
981 if (m_weight
== wxFONTWEIGHT_BOLD
)
983 if (m_style
== wxFONTSTYLE_ITALIC
|| m_style
== wxFONTSTYLE_SLANT
)
987 wxMacStringToPascal( m_faceName
, qdFontName
);
988 wxCFRef
< CTFontRef
> font
;
989 font
.reset( CTFontCreateWithQuickdrawInstance(qdFontName
, 0 , qdstyle
, m_pointSize
) );
990 m_ctFontDescriptor
= CTFontCopyFontDescriptor(font
);
994 m_ctFontDescriptor
= wxMacCreateCTFontDescriptor( wxCFStringRef(m_faceName
),traits
);
996 fontdescriptorcache
[ std::wstring(lookupnameWithSize
.wc_str()) ].reset(wxCFRetain(m_ctFontDescriptor
));
1000 #if wxOSX_USE_ATSU_TEXT
1001 if ( !m_atsuFontValid
)
1003 #if !wxOSX_USE_CARBON
1004 // not needed outside
1005 wxInt16 m_qdFontFamily
;
1006 wxInt16 m_qdFontStyle
;
1008 wxCFStringRef
cf( m_faceName
, wxLocale::GetSystemEncoding() );
1009 ATSFontFamilyRef atsfamily
= ATSFontFamilyFindFromName( cf
, kATSOptionFlagsDefault
);
1010 if ( atsfamily
== (ATSFontFamilyRef
) -1 )
1012 wxLogDebug( wxT("ATSFontFamilyFindFromName failed for ") + m_faceName
);
1013 m_qdFontFamily
= GetAppFont();
1017 m_qdFontFamily
= FMGetFontFamilyFromATSFontFamilyRef( atsfamily
);
1021 if (m_weight
== wxFONTWEIGHT_BOLD
)
1022 m_qdFontStyle
|= bold
;
1023 if (m_style
== wxFONTSTYLE_ITALIC
|| m_style
== wxFONTSTYLE_SLANT
)
1024 m_qdFontStyle
|= italic
;
1026 m_qdFontStyle
|= underline
;
1029 // we try to get as much styles as possible into ATSU
1031 // ATSUFontID and FMFont are equivalent
1032 FMFontStyle intrinsicStyle
= 0 ;
1033 OSStatus status
= FMGetFontFromFontFamilyInstance( m_qdFontFamily
, m_qdFontStyle
, (FMFont
*)&m_atsuFontID
, &intrinsicStyle
);
1034 if ( status
!= noErr
)
1036 wxFAIL_MSG( wxT("couldn't get an ATSUFont from font family") );
1038 m_atsuAdditionalQDStyles
= m_qdFontStyle
& (~intrinsicStyle
);
1039 m_atsuFontValid
= true;
1043 if ( m_nsFontDescriptor
== NULL
)
1044 OSXValidateNSFontDescriptor();
1046 #if wxOSX_USE_IPHONE
1049 m_descriptorValid
= true;
1052 void wxNativeFontInfo::Init(const wxNativeFontInfo
& info
)
1055 #if wxOSX_USE_CORE_TEXT
1056 m_ctFontDescriptor
= wxCFRetain(info
.m_ctFontDescriptor
);
1058 #if wxOSX_USE_ATSU_TEXT
1059 m_atsuFontValid
= info
.m_atsuFontValid
;
1060 m_atsuFontID
= info
.m_atsuFontID
;
1061 m_atsuAdditionalQDStyles
= info
.m_atsuAdditionalQDStyles
;
1062 #if wxOSX_USE_CARBON
1063 m_qdFontFamily
= info
.m_qdFontFamily
;
1064 m_qdFontStyle
= info
.m_qdFontStyle
;
1068 m_nsFontDescriptor
= (NSFontDescriptor
*) wxMacCocoaRetain(info
.m_nsFontDescriptor
);
1070 m_pointSize
= info
.m_pointSize
;
1071 m_family
= info
.m_family
;
1072 m_style
= info
.m_style
;
1073 m_weight
= info
.m_weight
;
1074 m_underlined
= info
.m_underlined
;
1075 m_faceName
= info
.m_faceName
;
1076 m_encoding
= info
.m_encoding
;
1077 m_descriptorValid
= info
.m_descriptorValid
;
1080 void wxNativeFontInfo::Init(int size
,
1081 wxFontFamily family
,
1083 wxFontWeight weight
,
1085 const wxString
& faceName
,
1086 wxFontEncoding encoding
)
1093 m_underlined
= underlined
;
1094 m_faceName
= faceName
;
1095 if ( encoding
== wxFONTENCODING_DEFAULT
)
1096 encoding
= wxFont::GetDefaultEncoding();
1097 m_encoding
= encoding
;
1101 void wxNativeFontInfo::Free()
1103 #if wxOSX_USE_CORE_TEXT
1104 wxCFRelease(m_ctFontDescriptor
);
1105 m_ctFontDescriptor
= NULL
;
1107 #if wxOSX_USE_ATSU_TEXT
1109 m_atsuAdditionalQDStyles
= 0;
1110 m_atsuFontValid
= false;
1113 wxMacCocoaRelease(m_nsFontDescriptor
);
1114 m_nsFontDescriptor
= NULL
;
1116 m_descriptorValid
= false;
1119 bool wxNativeFontInfo::FromString(const wxString
& s
)
1123 wxStringTokenizer
tokenizer(s
, wxT(";"));
1125 wxString token
= tokenizer
.GetNextToken();
1127 // Ignore the version for now
1130 token
= tokenizer
.GetNextToken();
1131 if ( !token
.ToLong(&l
) )
1133 m_pointSize
= (int)l
;
1135 token
= tokenizer
.GetNextToken();
1136 if ( !token
.ToLong(&l
) )
1138 m_family
= (wxFontFamily
)l
;
1140 token
= tokenizer
.GetNextToken();
1141 if ( !token
.ToLong(&l
) )
1143 m_style
= (wxFontStyle
)l
;
1145 token
= tokenizer
.GetNextToken();
1146 if ( !token
.ToLong(&l
) )
1148 m_weight
= (wxFontWeight
)l
;
1150 token
= tokenizer
.GetNextToken();
1151 if ( !token
.ToLong(&l
) )
1153 m_underlined
= l
!= 0;
1155 m_faceName
= tokenizer
.GetNextToken();
1162 token
= tokenizer
.GetNextToken();
1163 if ( !token
.ToLong(&l
) )
1165 m_encoding
= (wxFontEncoding
)l
;
1170 wxString
wxNativeFontInfo::ToString() const
1174 s
.Printf(wxT("%d;%d;%d;%d;%d;%d;%s;%d"),
1181 m_faceName
.GetData(),
1187 int wxNativeFontInfo::GetPointSize() const
1192 wxFontStyle
wxNativeFontInfo::GetStyle() const
1197 wxFontWeight
wxNativeFontInfo::GetWeight() const
1202 bool wxNativeFontInfo::GetUnderlined() const
1204 return m_underlined
;
1207 wxString
wxNativeFontInfo::GetFaceName() const
1212 wxFontFamily
wxNativeFontInfo::GetFamily() const
1217 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
1222 // changing the font descriptor
1224 void wxNativeFontInfo::SetPointSize(int pointsize
)
1226 if ( m_pointSize
!= pointsize
)
1228 m_pointSize
= pointsize
;
1233 void wxNativeFontInfo::SetStyle(wxFontStyle style_
)
1235 if ( m_style
!= style_
)
1242 void wxNativeFontInfo::SetWeight(wxFontWeight weight_
)
1244 if ( m_weight
!= weight_
)
1251 void wxNativeFontInfo::SetUnderlined(bool underlined_
)
1253 if ( m_underlined
!= underlined_
)
1255 m_underlined
= underlined_
;
1260 bool wxNativeFontInfo::SetFaceName(const wxString
& facename_
)
1262 if ( m_faceName
!= facename_
)
1264 m_faceName
= facename_
;
1270 void wxNativeFontInfo::SetFamily(wxFontFamily family_
)
1272 if ( m_family
!= family_
)
1279 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding_
)
1281 if ( encoding_
== wxFONTENCODING_DEFAULT
)
1282 encoding_
= wxFont::GetDefaultEncoding();
1283 m_encoding
= encoding_
;
1284 // not reflected in native descriptors