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_ATSU_TEXT
174 ATSUStyle m_macATSUStyle
;
176 wxCFRef
<CGFontRef
> m_cgFont
;
183 wxNativeFontInfo m_info
;
186 #define M_FONTDATA ((wxFontRefData*)m_refData)
188 wxFontRefData::wxFontRefData(const wxFontRefData
& data
)
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
;
197 #if wxOSX_USE_CORE_TEXT
198 m_ctFont
= data
.m_ctFont
;
200 m_cgFont
= data
.m_cgFont
;
201 #if wxOSX_USE_ATSU_TEXT
202 if ( data
.m_macATSUStyle
!= NULL
)
204 ATSUCreateStyle(&m_macATSUStyle
) ;
205 ATSUCopyAttributes(data
.m_macATSUStyle
, m_macATSUStyle
);
209 m_nsFont
= (NSFont
*) wxMacCocoaRetain(data
.m_nsFont
);
212 m_uiFont
= wxMacCocoaRetain(data
.m_uiFont
);
217 // ============================================================================
219 // ============================================================================
221 // ----------------------------------------------------------------------------
223 // ----------------------------------------------------------------------------
225 void wxFontRefData::Init()
228 #if wxOSX_USE_CARBON && wxOSX_USE_ATSU_TEXT
229 m_macThemeFontID
= kThemeCurrentPortFont
;
231 #if wxOSX_USE_ATSU_TEXT
232 m_macATSUStyle
= NULL
;
243 wxFontRefData::~wxFontRefData()
248 void wxFontRefData::Free()
250 #if wxOSX_USE_CORE_TEXT
254 #if wxOSX_USE_ATSU_TEXT
255 if ( m_macATSUStyle
)
257 ::ATSUDisposeStyle((ATSUStyle
)m_macATSUStyle
);
258 m_macATSUStyle
= NULL
;
262 if (m_nsFont
!= NULL
)
264 wxMacCocoaRelease(m_nsFont
);
269 if (m_uiFont
!= NULL
)
271 wxMacCocoaRelease(m_uiFont
);
278 wxFontRefData::wxFontRefData(wxOSXSystemFont font
, int size
)
280 wxASSERT( font
!= wxOSX_SYSTEM_FONT_NONE
);
283 #if wxOSX_USE_CORE_TEXT
284 if ( UMAGetSystemVersion() >= 0x1050 )
286 CTFontUIFontType uifont
= kCTFontSystemFontType
;
289 case wxOSX_SYSTEM_FONT_NORMAL
:
290 uifont
= kCTFontSystemFontType
;
292 case wxOSX_SYSTEM_FONT_BOLD
:
293 uifont
= kCTFontEmphasizedSystemFontType
;
295 case wxOSX_SYSTEM_FONT_SMALL
:
296 uifont
= kCTFontSmallSystemFontType
;
298 case wxOSX_SYSTEM_FONT_SMALL_BOLD
:
299 uifont
= kCTFontSmallEmphasizedSystemFontType
;
301 case wxOSX_SYSTEM_FONT_MINI
:
302 uifont
= kCTFontMiniSystemFontType
;
304 case wxOSX_SYSTEM_FONT_MINI_BOLD
:
305 uifont
= kCTFontMiniEmphasizedSystemFontType
;
307 case wxOSX_SYSTEM_FONT_LABELS
:
308 uifont
= kCTFontLabelFontType
;
310 case wxOSX_SYSTEM_FONT_VIEWS
:
311 uifont
= kCTFontViewsFontType
;
316 m_ctFont
.reset(CTFontCreateUIFontForLanguage( uifont
, (CGFloat
) size
, NULL
));
317 wxCFRef
<CTFontDescriptorRef
> descr
;
318 descr
.reset( CTFontCopyFontDescriptor( m_ctFont
) );
322 #if wxOSX_USE_ATSU_TEXT
324 #if !wxOSX_USE_CARBON
325 // not needed outside
326 ThemeFontID m_macThemeFontID
= kThemeSystemFont
;
330 case wxOSX_SYSTEM_FONT_NORMAL
:
331 m_macThemeFontID
= kThemeSystemFont
;
333 case wxOSX_SYSTEM_FONT_BOLD
:
334 m_macThemeFontID
= kThemeEmphasizedSystemFont
;
336 case wxOSX_SYSTEM_FONT_SMALL
:
337 m_macThemeFontID
= kThemeSmallSystemFont
;
339 case wxOSX_SYSTEM_FONT_SMALL_BOLD
:
340 m_macThemeFontID
= kThemeSmallEmphasizedSystemFont
;
342 case wxOSX_SYSTEM_FONT_MINI
:
343 m_macThemeFontID
= kThemeMiniSystemFont
;
345 case wxOSX_SYSTEM_FONT_MINI_BOLD
:
346 // bold not available under themeing
347 m_macThemeFontID
= kThemeMiniSystemFont
;
349 case wxOSX_SYSTEM_FONT_LABELS
:
350 m_macThemeFontID
= kThemeLabelFont
;
352 case wxOSX_SYSTEM_FONT_VIEWS
:
353 m_macThemeFontID
= kThemeViewsFont
;
358 if ( m_info
.m_faceName
.empty() )
364 GetThemeFont( m_macThemeFontID
, GetApplicationScript(), qdFontName
, &fontSize
, &style
);
368 wxFontStyle fontstyle
= wxFONTSTYLE_NORMAL
;
369 wxFontWeight fontweight
= wxFONTWEIGHT_NORMAL
;
370 bool underlined
= false;
373 fontweight
= wxFONTWEIGHT_BOLD
;
375 fontweight
= wxFONTWEIGHT_NORMAL
;
376 if ( style
& italic
)
377 fontstyle
= wxFONTSTYLE_ITALIC
;
378 if ( style
& underline
)
381 m_info
.Init(size
,wxFONTFAMILY_DEFAULT
,fontstyle
,fontweight
,underlined
,
382 wxMacMakeStringFromPascal( qdFontName
), wxFONTENCODING_DEFAULT
);
387 m_nsFont
= wxFont::OSXCreateNSFont( font
, &m_info
);
390 m_uiFont
= wxFont::OSXCreateUIFont( font
, &m_info
);
394 void wxFontRefData::MacFindFont()
399 m_info
.EnsureValid();
401 #if wxOSX_USE_CORE_TEXT
402 if ( UMAGetSystemVersion() >= 0x1050 )
404 CTFontSymbolicTraits traits
= 0;
406 if (m_info
.m_weight
== wxFONTWEIGHT_BOLD
)
407 traits
|= kCTFontBoldTrait
;
408 if (m_info
.m_style
== wxFONTSTYLE_ITALIC
|| m_info
.m_style
== wxFONTSTYLE_SLANT
)
409 traits
|= kCTFontItalicTrait
;
412 wxString lookupnameWithSize
= wxString::Format( "%s_%ld_%ld", m_info
.m_faceName
.c_str(), traits
, m_info
.m_pointSize
);
414 static std::map
< std::wstring
, wxCFRef
< CTFontRef
> > fontcache
;
415 m_ctFont
= fontcache
[ std::wstring(lookupnameWithSize
.wc_str()) ];
418 m_ctFont
.reset( CTFontCreateWithFontDescriptor( m_info
.m_ctFontDescriptor
, 0/*m_pointSize */, NULL
) );
420 m_cgFont
.reset(CTFontCopyGraphicsFont(m_ctFont
, NULL
));
424 #if wxOSX_USE_ATSU_TEXT
426 // we try to get as much styles as possible into ATSU
428 OSStatus status
= ::ATSUCreateStyle(&m_macATSUStyle
);
429 wxASSERT_MSG( status
== noErr
, wxT("couldn't create ATSU style") );
431 ATSUAttributeTag atsuTags
[] =
435 kATSUVerticalCharacterTag
,
438 kATSUQDUnderlineTag
,
439 kATSUQDCondensedTag
,
442 ByteCount atsuSizes
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
444 sizeof( ATSUFontID
) ,
446 sizeof( ATSUVerticalCharacterType
),
454 Boolean kTrue
= true ;
455 Boolean kFalse
= false ;
457 Fixed atsuSize
= IntToFixed( m_info
.m_pointSize
);
458 ATSUVerticalCharacterType kHorizontal
= kATSUStronglyHorizontal
;
459 FMFontStyle addQDStyle
= m_info
.m_atsuAdditionalQDStyles
;
460 ATSUAttributeValuePtr atsuValues
[sizeof(atsuTags
) / sizeof(ATSUAttributeTag
)] =
462 &m_info
.m_atsuFontID
,
465 (addQDStyle
& bold
) ? &kTrue
: &kFalse
,
466 (addQDStyle
& italic
) ? &kTrue
: &kFalse
,
467 (addQDStyle
& underline
) ? &kTrue
: &kFalse
,
468 (addQDStyle
& condense
) ? &kTrue
: &kFalse
,
469 (addQDStyle
& extend
) ? &kTrue
: &kFalse
,
472 status
= ::ATSUSetAttributes(
473 (ATSUStyle
)m_macATSUStyle
,
474 sizeof(atsuTags
) / sizeof(ATSUAttributeTag
) ,
475 atsuTags
, atsuSizes
, atsuValues
);
477 wxASSERT_MSG( status
== noErr
, wxT("couldn't modify ATSU style") );
478 if ( m_cgFont
.get() == NULL
)
480 ATSFontRef fontRef
= FMGetATSFontRefFromFont(m_info
.m_atsuFontID
);
481 m_cgFont
.reset( CGFontCreateWithPlatformFont( &fontRef
) );
486 m_nsFont
= wxFont::OSXCreateNSFont( &m_info
);
489 m_uiFont
= wxFont::OSXCreateUIFont( &m_info
);
494 // ----------------------------------------------------------------------------
496 // ----------------------------------------------------------------------------
498 bool wxFont::Create(const wxNativeFontInfo
& info
)
502 m_refData
= new wxFontRefData( info
);
508 wxFont::wxFont(const wxString
& fontdesc
)
510 wxNativeFontInfo info
;
511 if ( info
.FromString(fontdesc
) )
515 bool wxFont::Create(int pointSize
,
520 const wxString
& faceNameParam
,
521 wxFontEncoding encoding
)
525 wxString faceName
= faceNameParam
;
527 if ( faceName
.empty() )
531 case wxFONTFAMILY_DEFAULT
:
532 faceName
= wxT("Lucida Grande");
535 case wxFONTFAMILY_SCRIPT
:
536 case wxFONTFAMILY_ROMAN
:
537 case wxFONTFAMILY_DECORATIVE
:
538 faceName
= wxT("Times");
541 case wxFONTFAMILY_SWISS
:
542 faceName
= wxT("Helvetica");
545 case wxFONTFAMILY_MODERN
:
546 case wxFONTFAMILY_TELETYPE
:
547 faceName
= wxT("Courier");
551 faceName
= wxT("Times");
556 wxNativeFontInfo info
;
558 info
.Init(pointSize
, family
, style
, weight
,
559 underlined
, faceName
, encoding
);
561 m_refData
= new wxFontRefData(info
);
566 bool wxFont::CreateSystemFont(wxOSXSystemFont font
)
570 m_refData
= new wxFontRefData( font
, 0 );
579 bool wxFont::RealizeResource()
581 M_FONTDATA
->MacFindFont();
586 void wxFont::SetEncoding(wxFontEncoding encoding
)
590 M_FONTDATA
->SetEncoding( encoding
);
593 wxGDIRefData
*wxFont::CreateGDIRefData() const
595 return new wxFontRefData
;
598 wxGDIRefData
*wxFont::CloneGDIRefData(const wxGDIRefData
*data
) const
600 return new wxFontRefData(*static_cast<const wxFontRefData
*>(data
));
603 void wxFont::SetPointSize(int pointSize
)
605 if ( M_FONTDATA
->GetPointSize() == pointSize
)
610 M_FONTDATA
->SetPointSize( pointSize
);
613 void wxFont::SetFamily(wxFontFamily family
)
617 M_FONTDATA
->SetFamily( family
);
620 void wxFont::SetStyle(wxFontStyle style
)
624 M_FONTDATA
->SetStyle( style
);
627 void wxFont::SetWeight(wxFontWeight weight
)
631 M_FONTDATA
->SetWeight( weight
);
634 bool wxFont::SetFaceName(const wxString
& faceName
)
638 M_FONTDATA
->SetFaceName( faceName
);
640 return wxFontBase::SetFaceName(faceName
);
643 void wxFont::SetUnderlined(bool underlined
)
647 M_FONTDATA
->SetUnderlined( underlined
);
650 void wxFont::SetNoAntiAliasing( bool no
)
654 M_FONTDATA
->SetNoAntiAliasing( no
);
657 // ----------------------------------------------------------------------------
659 // ----------------------------------------------------------------------------
661 // TODO: insert checks everywhere for M_FONTDATA == NULL!
663 int wxFont::GetPointSize() const
665 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
667 return M_FONTDATA
->GetPointSize();
670 wxSize
wxFont::GetPixelSize() const
672 #if wxUSE_GRAPHICS_CONTEXT
673 // TODO: consider caching the value
674 wxGraphicsContext
* dc
= wxGraphicsContext::CreateFromNative((CGContextRef
) NULL
);
675 dc
->SetFont(*(wxFont
*)this,*wxBLACK
);
676 wxDouble width
, height
= 0;
677 dc
->GetTextExtent( wxT("g"), &width
, &height
, NULL
, NULL
);
679 return wxSize((int)width
, (int)height
);
681 return wxFontBase::GetPixelSize();
685 wxFontFamily
wxFont::GetFamily() const
687 wxCHECK_MSG( M_FONTDATA
!= NULL
, wxFONTFAMILY_MAX
, wxT("invalid font") );
689 return M_FONTDATA
->GetFamily();
692 wxFontStyle
wxFont::GetStyle() const
694 wxCHECK_MSG( M_FONTDATA
!= NULL
, wxFONTSTYLE_MAX
, wxT("invalid font") );
696 return M_FONTDATA
->GetStyle() ;
699 wxFontWeight
wxFont::GetWeight() const
701 wxCHECK_MSG( M_FONTDATA
!= NULL
, wxFONTWEIGHT_MAX
, wxT("invalid font") );
703 return M_FONTDATA
->GetWeight();
706 bool wxFont::GetUnderlined() const
708 wxCHECK_MSG( M_FONTDATA
!= NULL
, false, wxT("invalid font") );
710 return M_FONTDATA
->GetUnderlined();
713 wxString
wxFont::GetFaceName() const
715 wxCHECK_MSG( M_FONTDATA
!= NULL
, wxEmptyString
, wxT("invalid font") );
717 return M_FONTDATA
->GetFaceName() ;
720 wxFontEncoding
wxFont::GetEncoding() const
722 wxCHECK_MSG( M_FONTDATA
!= NULL
, wxFONTENCODING_DEFAULT
, wxT("invalid font") );
724 return M_FONTDATA
->GetEncoding() ;
727 bool wxFont::GetNoAntiAliasing() const
729 wxCHECK_MSG( M_FONTDATA
!= NULL
, false, wxT("invalid font") );
731 return M_FONTDATA
->GetNoAntiAliasing();
734 #if wxOSX_USE_ATSU_TEXT && wxOSX_USE_CARBON
736 short wxFont::MacGetFontNum() const
738 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
740 // cast away constness otherwise lazy font resolution is not possible
741 const_cast<wxFont
*>(this)->RealizeResource();
743 return M_FONTDATA
->m_info
.m_qdFontFamily
;
746 wxByte
wxFont::MacGetFontStyle() const
748 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
750 // cast away constness otherwise lazy font resolution is not possible
751 const_cast<wxFont
*>(this)->RealizeResource();
753 return M_FONTDATA
->m_info
.m_qdFontStyle
;
756 wxUint16
wxFont::MacGetThemeFontID() const
758 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
760 return M_FONTDATA
->m_macThemeFontID
;
765 #if wxOSX_USE_ATSU_TEXT
766 void * wxFont::MacGetATSUStyle() const
768 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
770 // cast away constness otherwise lazy font resolution is not possible
771 const_cast<wxFont
*>(this)->RealizeResource();
773 return M_FONTDATA
->m_macATSUStyle
;
776 #if WXWIN_COMPATIBILITY_2_8
777 wxUint32
wxFont::MacGetATSUFontID() const
779 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
781 // cast away constness otherwise lazy font resolution is not possible
782 const_cast<wxFont
*>(this)->RealizeResource();
784 return M_FONTDATA
->m_info
.m_atsuFontID
;
787 wxUint32
wxFont::MacGetATSUAdditionalQDStyles() const
789 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
791 // cast away constness otherwise lazy font resolution is not possible
792 const_cast<wxFont
*>(this)->RealizeResource();
794 return M_FONTDATA
->m_info
.m_atsuAdditionalQDStyles
;
800 #if wxOSX_USE_CORE_TEXT
802 CTFontRef
wxFont::OSXGetCTFont() const
804 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
806 // cast away constness otherwise lazy font resolution is not possible
807 const_cast<wxFont
*>(this)->RealizeResource();
809 return (CTFontRef
)(M_FONTDATA
->m_ctFont
);
814 #if wxOSX_USE_COCOA_OR_CARBON
816 CGFontRef
wxFont::OSXGetCGFont() const
818 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
820 // cast away constness otherwise lazy font resolution is not possible
821 const_cast<wxFont
*>(this)->RealizeResource();
823 return (M_FONTDATA
->m_cgFont
);
831 NSFont
* wxFont::OSXGetNSFont() const
833 wxCHECK_MSG( M_FONTDATA
!= NULL
, 0, wxT("invalid font") );
835 // cast away constness otherwise lazy font resolution is not possible
836 const_cast<wxFont
*>(this)->RealizeResource();
838 return (M_FONTDATA
->m_nsFont
);
843 const wxNativeFontInfo
* wxFont::GetNativeFontInfo() const
845 wxCHECK_MSG( M_FONTDATA
!= NULL
, NULL
, wxT("invalid font") );
846 wxCHECK_MSG( Ok(), NULL
, wxT("invalid font") );
848 // cast away constness otherwise lazy font resolution is not possible
849 const_cast<wxFont
*>(this)->RealizeResource();
851 // M_FONTDATA->m_info.InitFromFont(*this);
853 return &(M_FONTDATA
->m_info
);
856 // ----------------------------------------------------------------------------
858 // ----------------------------------------------------------------------------
860 #if wxOSX_USE_CORE_TEXT
862 /* from Core Text Manual Common Operations */
864 static CTFontDescriptorRef
wxMacCreateCTFontDescriptor(CFStringRef iFamilyName
, CTFontSymbolicTraits iTraits
)
866 CTFontDescriptorRef descriptor
= NULL
;
867 CFMutableDictionaryRef attributes
;
869 assert(iFamilyName
!= NULL
);
870 // Create a mutable dictionary to hold our attributes.
871 attributes
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
872 &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
873 check(attributes
!= NULL
);
875 if (attributes
!= NULL
) {
876 // Add a family name to our attributes.
877 CFDictionaryAddValue(attributes
, kCTFontFamilyNameAttribute
, iFamilyName
);
881 CFMutableDictionaryRef traits
;
882 CFNumberRef symTraits
;
884 // Create the traits dictionary.
885 symTraits
= CFNumberCreate(kCFAllocatorDefault
, kCFNumberSInt32Type
,
887 check(symTraits
!= NULL
);
889 if (symTraits
!= NULL
) {
890 // Create a dictionary to hold our traits values.
891 traits
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
892 &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
893 check(traits
!= NULL
);
895 if (traits
!= NULL
) {
896 // Add the symbolic traits value to the traits dictionary.
897 CFDictionaryAddValue(traits
, kCTFontSymbolicTrait
, symTraits
);
899 // Add the traits attribute to our attributes.
900 CFDictionaryAddValue(attributes
, kCTFontTraitsAttribute
, traits
);
903 CFRelease(symTraits
);
906 // Create the font descriptor with our attributes
907 descriptor
= CTFontDescriptorCreateWithAttributes(attributes
);
908 check(descriptor
!= NULL
);
910 CFRelease(attributes
);
912 // Return our font descriptor.
918 void wxNativeFontInfo::Init()
920 #if wxOSX_USE_CORE_TEXT
921 m_ctFontDescriptor
= NULL
;
923 #if wxOSX_USE_ATSU_TEXT
925 m_atsuAdditionalQDStyles
= 0;
926 m_atsuFontValid
= false;
933 m_nsFontDescriptor
= NULL
;
936 m_uiFontDescriptor
= NULL
;
939 m_family
= wxFONTFAMILY_DEFAULT
;
940 m_style
= wxFONTSTYLE_NORMAL
;
941 m_weight
= wxFONTWEIGHT_NORMAL
;
942 m_underlined
= false;
944 m_encoding
= wxFONTENCODING_DEFAULT
;
945 m_descriptorValid
= false;
948 #if wxOSX_USE_CORE_TEXT
949 void wxNativeFontInfo::Init(CTFontDescriptorRef descr
)
952 m_ctFontDescriptor
= wxCFRetain(descr
);
954 wxCFRef
< CFNumberRef
> sizevalue( (CFNumberRef
) CTFontDescriptorCopyAttribute( m_ctFontDescriptor
, kCTFontSizeAttribute
) );
956 if ( CFNumberGetValue( sizevalue
, kCFNumberFloatType
, &fsize
) )
957 m_pointSize
= (int)( fsize
+ 0.5 );
959 wxCFRef
< CFDictionaryRef
> traitsvalue( (CFDictionaryRef
) CTFontDescriptorCopyAttribute( m_ctFontDescriptor
, kCTFontTraitsAttribute
) );
960 CTFontSymbolicTraits traits
;
961 if ( CFNumberGetValue((CFNumberRef
) CFDictionaryGetValue(traitsvalue
,kCTFontSymbolicTrait
),kCFNumberIntType
,&traits
) )
963 if ( traits
& kCTFontItalicTrait
)
964 m_style
= wxFONTSTYLE_ITALIC
;
965 if ( traits
& kCTFontBoldTrait
)
966 m_weight
= wxFONTWEIGHT_BOLD
;
969 wxCFStringRef
familyName( (CFStringRef
) CTFontDescriptorCopyAttribute(m_ctFontDescriptor
, kCTFontFamilyNameAttribute
));
970 m_faceName
= familyName
.AsString();
974 void wxNativeFontInfo::EnsureValid()
976 if ( m_descriptorValid
)
979 #if wxOSX_USE_CORE_TEXT
980 if ( m_ctFontDescriptor
== NULL
&& UMAGetSystemVersion() >= 0x1050 )
982 CTFontSymbolicTraits traits
= 0;
984 if (m_weight
== wxFONTWEIGHT_BOLD
)
985 traits
|= kCTFontBoldTrait
;
986 if (m_style
== wxFONTSTYLE_ITALIC
|| m_style
== wxFONTSTYLE_SLANT
)
987 traits
|= kCTFontItalicTrait
;
990 wxString lookupnameWithSize
= wxString::Format( "%s_%ld_%ld", m_faceName
.c_str(), traits
, m_pointSize
);
992 static std::map
< std::wstring
, wxCFRef
< CTFontDescriptorRef
> > fontdescriptorcache
;
993 m_ctFontDescriptor
= wxCFRetain((CTFontDescriptorRef
)fontdescriptorcache
[ std::wstring(lookupnameWithSize
.wc_str()) ]);
994 if ( !m_ctFontDescriptor
)
996 // QD selection algorithm is the fastest by orders of magnitude on 10.5
997 if ( m_faceName
.IsAscii() )
1000 if (m_weight
== wxFONTWEIGHT_BOLD
)
1002 if (m_style
== wxFONTSTYLE_ITALIC
|| m_style
== wxFONTSTYLE_SLANT
)
1006 wxMacStringToPascal( m_faceName
, qdFontName
);
1007 wxCFRef
< CTFontRef
> font
;
1008 font
.reset( CTFontCreateWithQuickdrawInstance(qdFontName
, 0 , qdstyle
, m_pointSize
) );
1009 m_ctFontDescriptor
= CTFontCopyFontDescriptor(font
);
1013 m_ctFontDescriptor
= wxMacCreateCTFontDescriptor( wxCFStringRef(m_faceName
),traits
);
1015 fontdescriptorcache
[ std::wstring(lookupnameWithSize
.wc_str()) ].reset(wxCFRetain(m_ctFontDescriptor
));
1019 #if wxOSX_USE_ATSU_TEXT
1020 if ( !m_atsuFontValid
)
1022 #if !wxOSX_USE_CARBON
1023 // not needed outside
1024 wxInt16 m_qdFontFamily
;
1025 wxInt16 m_qdFontStyle
;
1027 wxCFStringRef
cf( m_faceName
, wxLocale::GetSystemEncoding() );
1028 ATSFontFamilyRef atsfamily
= ATSFontFamilyFindFromName( cf
, kATSOptionFlagsDefault
);
1029 if ( atsfamily
== (ATSFontFamilyRef
) -1 )
1031 wxLogDebug( wxT("ATSFontFamilyFindFromName failed for ") + m_faceName
);
1032 m_qdFontFamily
= GetAppFont();
1036 m_qdFontFamily
= FMGetFontFamilyFromATSFontFamilyRef( atsfamily
);
1040 if (m_weight
== wxFONTWEIGHT_BOLD
)
1041 m_qdFontStyle
|= bold
;
1042 if (m_style
== wxFONTSTYLE_ITALIC
|| m_style
== wxFONTSTYLE_SLANT
)
1043 m_qdFontStyle
|= italic
;
1045 m_qdFontStyle
|= underline
;
1048 // we try to get as much styles as possible into ATSU
1050 // ATSUFontID and FMFont are equivalent
1051 FMFontStyle intrinsicStyle
= 0 ;
1052 OSStatus status
= FMGetFontFromFontFamilyInstance( m_qdFontFamily
, m_qdFontStyle
, (FMFont
*)&m_atsuFontID
, &intrinsicStyle
);
1053 wxASSERT_MSG( status
== noErr
, wxT("couldn't get an ATSUFont from font family") );
1054 m_atsuAdditionalQDStyles
= m_qdFontStyle
& (~intrinsicStyle
);
1055 m_atsuFontValid
= true;
1059 if ( m_nsFontDescriptor
== NULL
)
1060 OSXValidateNSFontDescriptor();
1062 #if wxOSX_USE_IPHONE
1065 m_descriptorValid
= true;
1068 void wxNativeFontInfo::Init(const wxNativeFontInfo
& info
)
1071 #if wxOSX_USE_CORE_TEXT
1072 m_ctFontDescriptor
= wxCFRetain(info
.m_ctFontDescriptor
);
1074 #if wxOSX_USE_ATSU_TEXT
1075 m_atsuFontValid
= info
.m_atsuFontValid
;
1076 m_atsuFontID
= info
.m_atsuFontID
;
1077 m_atsuAdditionalQDStyles
= info
.m_atsuAdditionalQDStyles
;
1078 #if wxOSX_USE_CARBON
1079 m_qdFontFamily
= info
.m_qdFontFamily
;
1080 m_qdFontStyle
= info
.m_qdFontStyle
;
1084 m_nsFontDescriptor
= (NSFontDescriptor
*) wxMacCocoaRetain(info
.m_nsFontDescriptor
);
1086 #if wxOSX_USE_IPHONE
1087 m_uiFontDescriptor
= wxMacCocoaRetain(info
.m_uiFontDescriptor
);;
1089 m_pointSize
= info
.m_pointSize
;
1090 m_family
= info
.m_family
;
1091 m_style
= info
.m_style
;
1092 m_weight
= info
.m_weight
;
1093 m_underlined
= info
.m_underlined
;
1094 m_faceName
= info
.m_faceName
;
1095 m_encoding
= info
.m_encoding
;
1096 m_descriptorValid
= info
.m_descriptorValid
;
1099 void wxNativeFontInfo::Init(int size
,
1100 wxFontFamily family
,
1102 wxFontWeight weight
,
1104 const wxString
& faceName
,
1105 wxFontEncoding encoding
)
1112 m_underlined
= underlined
;
1113 m_faceName
= faceName
;
1114 m_encoding
= encoding
;
1118 void wxNativeFontInfo::Free()
1120 #if wxOSX_USE_CORE_TEXT
1121 wxCFRelease(m_ctFontDescriptor
);
1122 m_ctFontDescriptor
= NULL
;
1124 #if wxOSX_USE_ATSU_TEXT
1126 m_atsuAdditionalQDStyles
= 0;
1127 m_atsuFontValid
= false;
1130 wxMacCocoaRelease(m_nsFontDescriptor
);
1131 m_nsFontDescriptor
= NULL
;
1133 #if wxOSX_USE_IPHONE
1134 wxMacCocoaRelease(m_uiFontDescriptor
);
1135 m_uiFontDescriptor
= NULL
1137 m_descriptorValid
= false;
1140 bool wxNativeFontInfo::FromString(const wxString
& s
)
1144 wxStringTokenizer
tokenizer(s
, _T(";"));
1146 wxString token
= tokenizer
.GetNextToken();
1148 // Ignore the version for now
1151 token
= tokenizer
.GetNextToken();
1152 if ( !token
.ToLong(&l
) )
1154 m_pointSize
= (int)l
;
1156 token
= tokenizer
.GetNextToken();
1157 if ( !token
.ToLong(&l
) )
1159 m_family
= (wxFontFamily
)l
;
1161 token
= tokenizer
.GetNextToken();
1162 if ( !token
.ToLong(&l
) )
1164 m_style
= (wxFontStyle
)l
;
1166 token
= tokenizer
.GetNextToken();
1167 if ( !token
.ToLong(&l
) )
1169 m_weight
= (wxFontWeight
)l
;
1171 token
= tokenizer
.GetNextToken();
1172 if ( !token
.ToLong(&l
) )
1174 m_underlined
= l
!= 0;
1176 m_faceName
= tokenizer
.GetNextToken();
1183 token
= tokenizer
.GetNextToken();
1184 if ( !token
.ToLong(&l
) )
1186 m_encoding
= (wxFontEncoding
)l
;
1191 wxString
wxNativeFontInfo::ToString() const
1195 s
.Printf(_T("%d;%d;%d;%d;%d;%d;%s;%d"),
1202 m_faceName
.GetData(),
1208 int wxNativeFontInfo::GetPointSize() const
1213 wxFontStyle
wxNativeFontInfo::GetStyle() const
1218 wxFontWeight
wxNativeFontInfo::GetWeight() const
1223 bool wxNativeFontInfo::GetUnderlined() const
1225 return m_underlined
;
1228 wxString
wxNativeFontInfo::GetFaceName() const
1233 wxFontFamily
wxNativeFontInfo::GetFamily() const
1238 wxFontEncoding
wxNativeFontInfo::GetEncoding() const
1243 // changing the font descriptor
1245 void wxNativeFontInfo::SetPointSize(int pointsize
)
1247 if ( m_pointSize
!= pointsize
)
1249 m_pointSize
= pointsize
;
1254 void wxNativeFontInfo::SetStyle(wxFontStyle style_
)
1256 if ( m_style
!= style_
)
1263 void wxNativeFontInfo::SetWeight(wxFontWeight weight_
)
1265 if ( m_weight
!= weight_
)
1272 void wxNativeFontInfo::SetUnderlined(bool underlined_
)
1274 if ( m_underlined
!= underlined_
)
1276 m_underlined
= underlined_
;
1281 bool wxNativeFontInfo::SetFaceName(const wxString
& facename_
)
1283 if ( m_faceName
!= facename_
)
1285 m_faceName
= facename_
;
1291 void wxNativeFontInfo::SetFamily(wxFontFamily family_
)
1293 if ( m_family
!= family_
)
1300 void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding_
)
1302 m_encoding
= encoding_
;
1303 // not reflected in native descriptors