From f1c40652a0c2d6752c57df1b8aed22fae7e118dd Mon Sep 17 00:00:00 2001 From: Stefan Csomor Date: Fri, 20 Mar 2009 13:56:57 +0000 Subject: [PATCH] reworked font handling for osx git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@59644 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/fontutil.h | 72 ++ include/wx/osx/core/private.h | 2 +- include/wx/osx/font.h | 56 +- src/common/fontcmn.cpp | 2 +- src/generic/graphicc.cpp | 2 +- src/generic/listctrl.cpp | 8 +- src/generic/treectlg.cpp | 6 +- src/osx/carbon/dataview.cpp | 2 +- src/osx/carbon/font.cpp | 1377 ++++++++++++++++++------------- src/osx/carbon/fontdlg.cpp | 7 +- src/osx/carbon/gdiobj.cpp | 24 +- src/osx/carbon/graphics.cpp | 4 +- src/osx/carbon/listctrl_mac.cpp | 10 +- src/osx/carbon/utilscocoa.mm | 100 ++- src/osx/carbon/window.cpp | 4 +- src/osx/cocoa/stattext.mm | 116 +-- src/osx/cocoa/window.mm | 6 +- src/osx/listbox_osx.cpp | 6 +- src/osx/stattext_osx.cpp | 6 +- 19 files changed, 1064 insertions(+), 746 deletions(-) diff --git a/include/wx/fontutil.h b/include/wx/fontutil.h index 4c4b6a7bbe..050e470fa0 100644 --- a/include/wx/fontutil.h +++ b/include/wx/fontutil.h @@ -65,6 +65,7 @@ enum wxXLFDField // functions, the user code can only get the objects of this type from // somewhere and pass it somewhere else (possibly save them somewhere using // ToString() and restore them using FromString()) + class WXDLLIMPEXP_CORE wxNativeFontInfo { public: @@ -113,6 +114,77 @@ public: FATTRS fa; FONTMETRICS fm; FACENAMEDESC fn; +#elif defined(__WXOSX__) +public: + wxNativeFontInfo(const wxNativeFontInfo& info) { Init(info); } + wxNativeFontInfo( int size, + wxFontFamily family, + wxFontStyle style, + wxFontWeight weight, + bool underlined, + const wxString& faceName, + wxFontEncoding encoding) + { Init(size,family,style,weight,underlined,faceName,encoding); } + + ~wxNativeFontInfo() { Free(); } + + wxNativeFontInfo& operator=(const wxNativeFontInfo& info) + { + if (this != &info) + { + Free(); + Init(info); + } + return *this; + } + +#if wxOSX_USE_CORE_TEXT + void Init(CTFontDescriptorRef descr); +#endif + void Init(const wxNativeFontInfo& info); + void Init(int size, + wxFontFamily family, + wxFontStyle style, + wxFontWeight weight, + bool underlined, + const wxString& faceName , + wxFontEncoding encoding); + + void Free(); + void EnsureValid(); + + bool m_descriptorValid; +#if wxOSX_USE_CORE_TEXT + CTFontDescriptorRef m_ctFontDescriptor; +#endif + +#if wxOSX_USE_ATSU_TEXT + bool m_atsuFontValid; + // the atsu font ID + wxUint32 m_atsuFontID; + // the qd styles that are not intrinsic to the font above + wxInt16 m_atsuAdditionalQDStyles; +#if wxOSX_USE_CARBON + wxInt16 m_qdFontFamily; + wxInt16 m_qdFontStyle; +#endif +#endif + +#if wxOSX_USE_COCOA + WX_NSFontDescriptor m_nsFontDescriptor; + void ValidateNSFontDescriptor(); +#endif +#if wxOSX_USE_IPHONE +#endif + + int m_pointSize; + wxFontFamily m_family; + wxFontStyle m_style; + wxFontWeight m_weight; + bool m_underlined; + wxString m_faceName; + wxFontEncoding m_encoding; +public : #else // other platforms // // This is a generic implementation that should work on all ports diff --git a/include/wx/osx/core/private.h b/include/wx/osx/core/private.h index a35da2fb5f..b514797d89 100644 --- a/include/wx/osx/core/private.h +++ b/include/wx/osx/core/private.h @@ -726,7 +726,7 @@ private : void wxMacCocoaRelease( void* obj ); void wxMacCocoaAutorelease( void* obj ); -void wxMacCocoaRetain( void* obj ); +void* wxMacCocoaRetain( void* obj ); #endif diff --git a/include/wx/osx/font.h b/include/wx/osx/font.h index 270f93015d..25691b12a4 100644 --- a/include/wx/osx/font.h +++ b/include/wx/osx/font.h @@ -16,6 +16,21 @@ // wxFont // ---------------------------------------------------------------------------- +// font styles +enum wxOSXSystemFont +{ + wxOSX_SYSTEM_FONT_NONE = 0, + wxOSX_SYSTEM_FONT_NORMAL, + wxOSX_SYSTEM_FONT_BOLD, + wxOSX_SYSTEM_FONT_SMALL, + wxOSX_SYSTEM_FONT_SMALL_BOLD, + wxOSX_SYSTEM_FONT_MINI, + wxOSX_SYSTEM_FONT_MINI_BOLD, + wxOSX_SYSTEM_FONT_LABELS, + wxOSX_SYSTEM_FONT_VIEWS +}; + + class WXDLLIMPEXP_CORE wxFont : public wxFontBase { public: @@ -75,15 +90,8 @@ public: bool Create(const wxNativeFontInfo& info); -#if wxOSX_USE_ATSU_TEXT - bool MacCreateFromThemeFont( wxUint16 themeFontID ) ; -#endif -#if wxOSX_USE_CORE_TEXT - bool MacCreateFromUIFont( wxUint32 coreTextFontType ); - bool MacCreateFromCTFontDescriptor( const void * ctFontDescriptor, int pointSize = 0 ); - bool MacCreateFromCTFont( const void * ctFont ); -#endif - + bool CreateSystemFont(wxOSXSystemFont font); + virtual ~wxFont(); // implement base class pure virtuals @@ -118,32 +126,42 @@ public: // Mac-specific, risks to change, don't use in portable code -#if wxOSX_USE_ATSU_TEXT +#if wxOSX_USE_CARBON && wxOSX_USE_ATSU_TEXT + wxUint16 MacGetThemeFontID() const ; // 'old' Quickdraw accessors short MacGetFontNum() const; - short MacGetFontSize() const; wxByte MacGetFontStyle() const; +#endif - // 'new' ATSUI accessors - wxUint32 MacGetATSUFontID() const; - wxUint32 MacGetATSUAdditionalQDStyles() const; - wxUint16 MacGetThemeFontID() const ; - - // Returns an ATSUStyle not ATSUStyle* +#if wxOSX_USE_COCOA_OR_CARBON + CGFontRef GetCGFont() const; #endif + #if wxOSX_USE_CORE_TEXT - const void * MacGetCTFont() const; + CTFontRef GetCTFont() const; #endif + #if wxOSX_USE_CORE_TEXT || wxOSX_USE_ATSU_TEXT + // Returns an ATSUStyle not ATSUStyle* void* MacGetATSUStyle() const ; #endif +#if wxOSX_USE_COCOA + WX_NSFont GetNSFont() const; + static WX_NSFont CreateNSFont(wxOSXSystemFont font, wxNativeFontInfo* info); + static WX_NSFont CreateNSFont(const wxNativeFontInfo* info); +#endif + +#if wxOSX_USE_IPHONE + WX_UIFont GetUIFont() const; + static WX_NSFont CreateUIFont(wxOSXSystemFont font, wxNativeFontInfo* info); +#endif + protected: virtual wxGDIRefData *CreateGDIRefData() const; virtual wxGDIRefData *CloneGDIRefData(const wxGDIRefData *data) const; private: - void Unshare(); DECLARE_DYNAMIC_CLASS(wxFont) }; diff --git a/src/common/fontcmn.cpp b/src/common/fontcmn.cpp index 96407bbfd6..67ee5bff64 100644 --- a/src/common/fontcmn.cpp +++ b/src/common/fontcmn.cpp @@ -586,7 +586,7 @@ void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding_) // format there anyhow (but there is a well-defined standard for X11 fonts used // by wxGTK and wxMotif) -#if defined(wxNO_NATIVE_FONTINFO) || defined(__WXMSW__) || defined (__WXPM__) +#if defined(wxNO_NATIVE_FONTINFO) || defined(__WXMSW__) || defined (__WXPM__) || defined(__WXOSX__) wxString wxNativeFontInfo::ToUserString() const { diff --git a/src/generic/graphicc.cpp b/src/generic/graphicc.cpp index 6642ea5a45..3a1c5bc01b 100644 --- a/src/generic/graphicc.cpp +++ b/src/generic/graphicc.cpp @@ -692,7 +692,7 @@ wxCairoFontData::wxCairoFontData( wxGraphicsRenderer* renderer, const wxFont &fo m_underlined = font.GetUnderlined(); #ifdef __WXMAC__ - m_font = cairo_atsui_font_face_create_for_atsu_font_id( font.MacGetATSUFontID() ); + m_font = cairo_quartz_font_face_create_for_cgfont( font.GetCGFont() ); #elif defined(__WXGTK__) m_font = pango_font_description_copy( font.GetNativeFontInfo()->description ); #else diff --git a/src/generic/listctrl.cpp b/src/generic/listctrl.cpp index 4e16824db1..dbc1eeec4d 100644 --- a/src/generic/listctrl.cpp +++ b/src/generic/listctrl.cpp @@ -4305,13 +4305,9 @@ void wxGenericListCtrl::CreateOrDestroyHeaderWindowAsNeeded() wxTAB_TRAVERSAL ); -#if defined( __WXMAC__ ) && wxOSX_USE_COCOA_OR_CARBON +#if defined( __WXMAC__ ) wxFont font; -#if wxOSX_USE_ATSU_TEXT - font.MacCreateFromThemeFont( kThemeSmallSystemFont ); -#else - font.MacCreateFromUIFont( kCTFontSystemFontType ); -#endif + font.CreateSystemFont( wxOSX_SYSTEM_FONT_SMALL ); m_headerWin->SetFont( font ); #endif diff --git a/src/generic/treectlg.cpp b/src/generic/treectlg.cpp index 9dcc3281cb..e63d023e05 100644 --- a/src/generic/treectlg.cpp +++ b/src/generic/treectlg.cpp @@ -973,11 +973,7 @@ void wxGenericTreeCtrl::Init() m_lastOnSame = false; #if defined( __WXMAC__ ) -#if wxOSX_USE_ATSU_TEXT - m_normalFont.MacCreateFromThemeFont( kThemeViewsFont ) ; -#else - m_normalFont.MacCreateFromUIFont( kCTFontViewsFontType ) ; -#endif + m_normalFont.CreateSystemFont(wxOSX_SYSTEM_FONT_VIEWS); #else m_normalFont = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT ); #endif diff --git a/src/osx/carbon/dataview.cpp b/src/osx/carbon/dataview.cpp index 89c9c75dd5..f4a2c7fdd4 100644 --- a/src/osx/carbon/dataview.cpp +++ b/src/osx/carbon/dataview.cpp @@ -1043,7 +1043,7 @@ wxVisualAttributes wxDataViewCtrl::GetClassDefaultAttributes(wxWindowVariant var attr.colFg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT ); attr.colBg = wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOX ); - //attr.font.MacCreateFromThemeFont(kThemeViewsFont); + //attr.font.CreateSystemFont(wxOSX_SYSTEM_FONT_VIEWS); return attr; } diff --git a/src/osx/carbon/font.cpp b/src/osx/carbon/font.cpp index 3d295d4bb9..6db6190920 100644 --- a/src/osx/carbon/font.cpp +++ b/src/osx/carbon/font.cpp @@ -24,6 +24,7 @@ #include "wx/fontutil.h" #include "wx/graphics.h" #include "wx/settings.h" +#include "wx/tokenzr.h" #include "wx/osx/private.h" @@ -37,33 +38,26 @@ IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject) - class WXDLLEXPORT wxFontRefData: public wxGDIRefData { public: - wxFontRefData() - { - Init(10, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL, - false, wxT("applicationfont"), wxFONTENCODING_DEFAULT); - } - wxFontRefData(const wxFontRefData& data) + wxFontRefData() { - Init(data.m_pointSize, data.m_family, data.m_style, data.m_weight, - data.m_underlined, data.m_faceName, data.m_encoding); + Init(); + m_info.Init(10, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL, + false, wxEmptyString, wxFONTENCODING_DEFAULT); } - wxFontRefData(int size, - wxFontFamily family, - wxFontStyle style, - wxFontWeight weight, - bool underlined, - const wxString& faceName, - wxFontEncoding encoding) + wxFontRefData(const wxFontRefData& data); + + wxFontRefData( const wxNativeFontInfo& info ) : m_info(info) { - Init(size, family, style, weight, underlined, faceName, encoding); + Init(); } + wxFontRefData(wxOSXSystemFont font, int size); + #if wxOSX_USE_CORE_TEXT wxFontRefData( wxUint32 coreTextFontType ); wxFontRefData( CTFontRef font ); @@ -78,105 +72,97 @@ public: void SetPointSize( int size ) { - m_pointSize = size; - MacInvalidateNativeFont(); + if( GetPointSize() != size ) + { + m_info.SetPointSize(size); + Free(); + } } - int GetPointSize() const { return m_pointSize; } + int GetPointSize() const { return m_info.GetPointSize(); } void SetFamily( wxFontFamily family ) { - m_family = family; - MacInvalidateNativeFont(); + if ( m_info.m_family != family ) + { + m_info.SetFamily( family ); + Free(); + } } - - wxFontFamily GetFamily() const { return m_family; } + wxFontFamily GetFamily() const { return m_info.GetFamily(); } void SetStyle( wxFontStyle style ) { - m_style = style; - MacInvalidateNativeFont(); + if ( m_info.m_style != style ) + { + m_info.SetStyle( style ); + Free(); + } } - wxFontStyle GetStyle() const { return m_style; } + wxFontStyle GetStyle() const { return m_info.GetStyle(); } void SetWeight( wxFontWeight weight ) { - m_weight = weight; - MacInvalidateNativeFont(); + if ( m_info.m_weight != weight ) + { + m_info.SetWeight( weight ); + Free(); + } } - wxFontWeight GetWeight() const { return m_weight; } + wxFontWeight GetWeight() const { return m_info.GetWeight(); } void SetUnderlined( bool u ) { - m_underlined = u; - MacInvalidateNativeFont(); + if ( m_info.m_underlined != u ) + { + m_info.SetUnderlined( u ); + Free(); + } } - bool GetUnderlined() const { return m_underlined; } + bool GetUnderlined() const { return m_info.GetUnderlined(); } void SetFaceName( const wxString& facename ) { - m_faceName = facename; - MacInvalidateNativeFont(); + if ( m_info.m_faceName != facename ) + { + m_info.SetFaceName( facename ); + Free(); + } } - const wxString& GetFaceName() const { return m_faceName; } + wxString GetFaceName() const { return m_info.GetFaceName(); } void SetEncoding( wxFontEncoding encoding ) { - m_encoding = encoding; - MacInvalidateNativeFont(); + if ( m_info.m_encoding != encoding ) + { + m_info.SetEncoding( encoding ); + Free(); + } } - wxFontEncoding GetEncoding() const { return m_encoding; } + wxFontEncoding GetEncoding() const { return m_info.GetEncoding(); } - void MacInvalidateNativeFont(); + void Free(); void MacFindFont(); protected: // common part of all ctors - void Init(int size, - wxFontFamily family, - wxFontStyle style, - wxFontWeight weight, - bool underlined, - const wxString& faceName, - wxFontEncoding encoding); - + void Init(); #if wxOSX_USE_CORE_TEXT - void Init( CTFontRef font ); + // void Init( CTFontRef font ); #endif - // font characterstics - int m_pointSize; - wxFontFamily m_family; - wxFontStyle m_style; - wxFontWeight m_weight; - bool m_underlined; - wxString m_faceName; - wxFontEncoding m_encoding; bool m_noAA; // No anti-aliasing - public: -#if wxOSX_USE_ATSU_TEXT - FMFontFamily m_macFontFamily; - FMFontSize m_macFontSize; - FMFontStyle m_macFontStyle; - - // ATSU Font Information - - // this is split into an ATSU font id that may - // contain some styles (special bold fonts etc) and - // these are the additional qd styles that are not - // included in the ATSU font id - ATSUFontID m_macATSUFontID; - FMFontStyle m_macATSUAdditionalQDStyles ; - + bool m_fontValid; +#if wxOSX_USE_CARBON && wxOSX_USE_ATSU_TEXT // for true themeing support we must store the correct font // information here, as this speeds up and optimizes rendering ThemeFontID m_macThemeFontID ; @@ -186,12 +172,45 @@ public: #endif #if wxOSX_USE_CORE_TEXT || wxOSX_USE_ATSU_TEXT ATSUStyle m_macATSUStyle ; +#endif +#if wxOSX_USE_COCOA + WX_NSFont m_nsFont; +#endif +#if wxOSX_USE_IPHONE + WX_UIFont m_uiFont; #endif wxNativeFontInfo m_info; }; #define M_FONTDATA ((wxFontRefData*)m_refData) +wxFontRefData::wxFontRefData(const wxFontRefData& data) +{ + Init(); + m_info = data.m_info; + m_noAA = data.m_noAA; + m_fontValid = data.m_fontValid; +#if wxOSX_USE_CARBON && wxOSX_USE_ATSU_TEXT + m_macThemeFontID = data.m_macThemeFontID; +#endif +#if wxOSX_USE_CORE_TEXT + m_ctFont = data.m_ctFont; +#endif +#if wxOSX_USE_CORE_TEXT || wxOSX_USE_ATSU_TEXT + if ( data.m_macATSUStyle != NULL ) + { + ATSUCreateStyle(&m_macATSUStyle) ; + ATSUCopyAttributes(data.m_macATSUStyle, m_macATSUStyle); + } +#endif +#if wxOSX_USE_COCOA + m_nsFont = (NSFont*) wxMacCocoaRetain(data.m_nsFont); +#endif +#if wxOSX_USE_IPHONE + m_uiFont = wxMacCocoaRetain(data.m_uiFont); +#endif + +} // ============================================================================ // implementation @@ -201,48 +220,30 @@ public: // wxFontRefData // ---------------------------------------------------------------------------- -void wxFontRefData::Init(int pointSize, - wxFontFamily family, - wxFontStyle style, - wxFontWeight weight, - bool underlined, - const wxString& faceName, - wxFontEncoding encoding) +void wxFontRefData::Init() { - m_style = style; - m_pointSize = (pointSize == -1) ? wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT).GetPointSize() : pointSize; - m_family = family; - m_style = style; - m_weight = weight; - m_underlined = underlined; - m_faceName = faceName; - m_encoding = encoding; m_noAA = false; -#if wxOSX_USE_ATSU_TEXT - m_macFontFamily = 0 ; - m_macFontSize = 0; - m_macFontStyle = 0; - m_macATSUFontID = 0; - m_macATSUAdditionalQDStyles = 0 ; +#if wxOSX_USE_CARBON && wxOSX_USE_ATSU_TEXT m_macThemeFontID = kThemeCurrentPortFont ; #endif #if wxOSX_USE_CORE_TEXT || wxOSX_USE_ATSU_TEXT m_macATSUStyle = NULL ; #endif +#if wxOSX_USE_COCOA + m_nsFont = NULL; +#endif +#if wxOSX_USE_IPHONE + m_uiFont = NULL; +#endif + m_fontValid = false; } wxFontRefData::~wxFontRefData() { -#if wxOSX_USE_CORE_TEXT || wxOSX_USE_ATSU_TEXT - if ( m_macATSUStyle ) - { - ::ATSUDisposeStyle((ATSUStyle)m_macATSUStyle); - m_macATSUStyle = NULL ; - } -#endif + Free(); } -void wxFontRefData::MacInvalidateNativeFont() +void wxFontRefData::Free() { #if wxOSX_USE_CORE_TEXT m_ctFont.reset(); @@ -254,391 +255,169 @@ void wxFontRefData::MacInvalidateNativeFont() m_macATSUStyle = NULL ; } #endif -} - -#if wxOSX_USE_CORE_TEXT - -/* from Core Text Manual Common Operations */ - -static CTFontDescriptorRef wxMacCreateCTFontDescriptor(CFStringRef iFamilyName, CTFontSymbolicTraits iTraits ) -{ - CTFontDescriptorRef descriptor = NULL; - CFMutableDictionaryRef attributes; - - assert(iFamilyName != NULL); - // Create a mutable dictionary to hold our attributes. - attributes = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, - &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); - check(attributes != NULL); - - if (attributes != NULL) { - // Add a family name to our attributes. - CFDictionaryAddValue(attributes, kCTFontFamilyNameAttribute, iFamilyName); - - - if ( iTraits ) { - CFMutableDictionaryRef traits; - CFNumberRef symTraits; - - // Create the traits dictionary. - symTraits = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, - &iTraits); - check(symTraits != NULL); - - if (symTraits != NULL) { - // Create a dictionary to hold our traits values. - traits = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, - &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); - check(traits != NULL); - - if (traits != NULL) { - // Add the symbolic traits value to the traits dictionary. - CFDictionaryAddValue(traits, kCTFontSymbolicTrait, symTraits); - - // Add the traits attribute to our attributes. - CFDictionaryAddValue(attributes, kCTFontTraitsAttribute, traits); - CFRelease(traits); - } - CFRelease(symTraits); - } - } - // Create the font descriptor with our attributes - descriptor = CTFontDescriptorCreateWithAttributes(attributes); - check(descriptor != NULL); - - CFRelease(attributes); +#if wxOSX_USE_COCOA + if (m_nsFont != NULL) + { + wxMacCocoaRelease(m_nsFont); + m_nsFont = NULL; } - // Return our font descriptor. - return descriptor ; -} - -wxFontRefData::wxFontRefData( wxUint32 coreTextFontType ) -{ - CTFontRef font = CTFontCreateUIFontForLanguage( coreTextFontType, 0.0, NULL ) ; - if ( CTFontGetSize(font) == 0 ) +#endif +#if wxOSX_USE_IPHONE + if (m_uiFont != NULL) { - CFRelease(font); - font = CTFontCreateUIFontForLanguage( coreTextFontType, 12.0, NULL ); + wxMacCocoaRelease(m_uiFont); + m_uiFont = NULL; } - Init( font ); -} - -wxFontRefData::wxFontRefData( CTFontRef font ) -{ - Init( font ); +#endif + m_fontValid = false; } -wxFontRefData::wxFontRefData( CTFontDescriptorRef fontdescriptor, int size ) +wxFontRefData::wxFontRefData(wxOSXSystemFont font, int size) { - if ( size == 0 ) + wxASSERT( font != wxOSX_SYSTEM_FONT_NONE ); + Init(); + +#if wxOSX_USE_CORE_TEXT + if ( UMAGetSystemVersion() >= 0x1050 ) { - wxCFRef< CFNumberRef > value( (CFNumberRef) CTFontDescriptorCopyAttribute( fontdescriptor, kCTFontSizeAttribute ) ); - - float fsize; - if ( CFNumberGetValue( value , kCFNumberFloatType , &fsize ) ) + CTFontUIFontType uifont; + switch( font ) { - size = (int)( fsize + 0.5 ); + case wxOSX_SYSTEM_FONT_NORMAL: + uifont = kCTFontSystemFontType; + break; + case wxOSX_SYSTEM_FONT_BOLD: + uifont = kCTFontEmphasizedSystemFontType; + break; + case wxOSX_SYSTEM_FONT_SMALL: + uifont = kCTFontSmallSystemFontType; + break; + case wxOSX_SYSTEM_FONT_SMALL_BOLD: + uifont = kCTFontSmallEmphasizedSystemFontType; + break; + case wxOSX_SYSTEM_FONT_MINI: + uifont = kCTFontMiniSystemFontType; + break; + case wxOSX_SYSTEM_FONT_MINI_BOLD: + uifont = kCTFontMiniEmphasizedSystemFontType; + break; + case wxOSX_SYSTEM_FONT_LABELS: + uifont = kCTFontLabelFontType; + break; + case wxOSX_SYSTEM_FONT_VIEWS: + uifont = kCTFontViewsFontType; + break; + default: + break; } + m_ctFont.reset(CTFontCreateUIFontForLanguage( uifont, (CGFloat) size, NULL )); + wxCFRef descr; + descr.reset( CTFontCopyFontDescriptor( m_ctFont ) ); + m_info.Init(descr); + } +#endif +#if wxOSX_USE_ATSU_TEXT + { + switch( font ) + { + case wxOSX_SYSTEM_FONT_NORMAL: + m_macThemeFontID = kThemeSystemFont; + break; + case wxOSX_SYSTEM_FONT_BOLD: + m_macThemeFontID = kThemeEmphasizedSystemFont; + break; + case wxOSX_SYSTEM_FONT_SMALL: + m_macThemeFontID = kThemeSmallSystemFont; + break; + case wxOSX_SYSTEM_FONT_SMALL_BOLD: + m_macThemeFontID = kThemeSmallEmphasizedSystemFont; + break; + case wxOSX_SYSTEM_FONT_MINI: + m_macThemeFontID = kThemeMiniSystemFont; + break; + case wxOSX_SYSTEM_FONT_MINI_BOLD: + // bold not available under themeing + m_macThemeFontID = kThemeMiniSystemFont; + break; + case wxOSX_SYSTEM_FONT_LABELS: + m_macThemeFontID = kThemeLabelFont; + break; + case wxOSX_SYSTEM_FONT_VIEWS: + m_macThemeFontID = kThemeViewsFont; + break; + default: + break; + } + if ( m_info.m_faceName.empty() ) + { + Style style ; + FMFontSize fontSize; + Str255 qdFontName ; + + GetThemeFont( m_macThemeFontID, GetApplicationScript(), qdFontName, &fontSize, &style ); + if ( size != 0 ) + fontSize = size; + + wxFontStyle fontstyle = wxFONTSTYLE_NORMAL; + wxFontWeight fontweight = wxFONTWEIGHT_NORMAL; + bool underlined = false; + + if ( style & bold ) + fontweight = wxFONTWEIGHT_BOLD ; + else + fontweight = wxFONTWEIGHT_NORMAL ; + if ( style & italic ) + fontstyle = wxFONTSTYLE_ITALIC ; + if ( style & underline ) + underlined = true ; + + m_info.Init(size,wxFONTFAMILY_DEFAULT,fontstyle,fontweight,underlined, + wxMacMakeStringFromPascal( qdFontName ), wxFONTENCODING_DEFAULT); + } } - Init( CTFontCreateWithFontDescriptor(fontdescriptor, size,NULL) ); -} - -void wxFontRefData::Init( CTFontRef font ) -{ - Init(10, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL, - false, wxT("applicationfont"), wxFONTENCODING_DEFAULT); - - m_ctFont.reset( font ); -} - #endif +#if wxOSX_USE_COCOA + m_nsFont = wxFont::CreateNSFont( font, &m_info ); +#endif +#if wxOSX_USE_IPHONE + m_uiFont = wxFont::CreateUIFont( font, &m_info ); +#endif +} void wxFontRefData::MacFindFont() { - + if ( m_fontValid ) + return; + + m_info.EnsureValid(); + #if wxOSX_USE_CORE_TEXT if ( UMAGetSystemVersion() >= 0x1050 ) { - if ( m_faceName.empty() && m_family == wxFONTFAMILY_DEFAULT ) - { - m_ctFont.reset(CTFontCreateUIFontForLanguage( kCTFontSystemFontType, 0.0, NULL )); - } - - if ( m_ctFont ) - { - wxCFStringRef name( CTFontCopyFamilyName( m_ctFont ) ); - m_faceName = name.AsString(); - m_pointSize = CTFontGetSize(m_ctFont) ; - CTFontSymbolicTraits traits = CTFontGetSymbolicTraits( m_ctFont ); - if ( traits & kCTFontItalicTrait ) - m_style = wxFONTSTYLE_ITALIC; - if ( traits & kCTFontBoldTrait ) - m_weight = wxFONTWEIGHT_BOLD ; - } - else - { - if ( m_faceName.empty() ) - { - switch ( m_family ) - { - case wxFONTFAMILY_SCRIPT : - case wxFONTFAMILY_ROMAN : - case wxFONTFAMILY_DECORATIVE : - m_faceName = wxT("Times"); - break ; - - case wxFONTFAMILY_SWISS : - m_faceName = wxT("Helvetica"); - break ; - - case wxFONTFAMILY_MODERN : - case wxFONTFAMILY_TELETYPE: - m_faceName = wxT("Courier"); - if ( m_style == wxFONTSTYLE_ITALIC && m_weight == wxFONTWEIGHT_NORMAL ) - { - m_style = wxFONTSTYLE_ITALIC; - } - break ; - - default: - m_faceName = wxT("Times"); - break ; - } - } - + CTFontSymbolicTraits traits = 0; - CTFontSymbolicTraits traits = 0; - - if (m_weight == wxFONTWEIGHT_BOLD) - traits |= kCTFontBoldTrait; - if (m_style == wxFONTSTYLE_ITALIC || m_style == wxFONTSTYLE_SLANT) - traits |= kCTFontItalicTrait; - - // use font caching - wxString lookupnameWithSize = wxString::Format( "%s_%ld_%ld", m_faceName.c_str(), traits, m_pointSize ); - - static std::map< std::wstring , wxCFRef< CTFontRef > > fontcache ; - m_ctFont = fontcache[ std::wstring(lookupnameWithSize.wc_str()) ]; - if ( !m_ctFont ) - { - // QD selection algorithm is the fastest by orders of magnitude on 10.5 - if ( m_faceName.IsAscii() ) - { - uint8_t qdstyle = 0; - if (m_weight == wxFONTWEIGHT_BOLD) - qdstyle |= bold; - if (m_style == wxFONTSTYLE_ITALIC || m_style == wxFONTSTYLE_SLANT) - qdstyle |= italic; - - Str255 qdFontName ; - wxMacStringToPascal( m_faceName , qdFontName ); - m_ctFont.reset( CTFontCreateWithQuickdrawInstance(qdFontName, 0 , qdstyle, m_pointSize) ); - } - else - { - - static std::map< std::wstring , wxCFRef< CTFontDescriptorRef > > fontdescriptorcache ; - wxString lookupname = wxString::Format( "%s_%ld", m_faceName.c_str(), traits ); - // descriptor caching - wxCFRef< CTFontDescriptorRef > descriptor = fontdescriptorcache[ std::wstring(lookupname.wc_str()) ]; - if ( !descriptor ) - { - wxCFStringRef cf( m_faceName, wxLocale::GetSystemEncoding() ); - descriptor.reset( wxMacCreateCTFontDescriptor( cf, traits ) ); - fontdescriptorcache[ std::wstring(lookupname.wc_str()) ] = descriptor; - } - m_ctFont.reset( CTFontCreateWithFontDescriptor( descriptor, m_pointSize, NULL ) ); - CTFontSymbolicTraits received = CTFontGetSymbolicTraits( m_ctFont ) & 0x03; - if ( traits != received ) - { - // TODO further fallbacks, synthesizing bold and italic, trying direct PostScript names etc - } - } - - fontcache[ std::wstring(lookupnameWithSize.wc_str()) ] = m_ctFont; -#if 1 // debugging coretext font matching - CTFontSymbolicTraits received = CTFontGetSymbolicTraits( m_ctFont ) & 0x03; - if ( received != traits ) - { - float angle = CTFontGetSlantAngle( m_ctFont ); - CFDictionaryRef dict = CTFontCopyTraits( m_ctFont ); - CFNumberRef number = (CFNumberRef) CFDictionaryGetValue(dict, kCTFontWeightTrait ); - float floatnumber; - CFNumberGetValue( number, kCFNumberFloatType, &floatnumber ); - { - wxString msg = wxString::Format( "font %s expected %d but got %d traits, %f angle \n" , - m_faceName.c_str(), traits, received, angle ); - printf( msg.c_str() ); - } - CFShow( dict ); - CFRelease( dict ); - } -#endif - } - - } -#if wxOSX_USE_ATSU_TEXT - OSStatus status = noErr; - ATSFontRef atsfont = CTFontGetPlatformFont( m_ctFont, NULL ); - FMFont fmfont = FMGetFontFromATSFontRef( atsfont ); - ATSUAttributeTag atsuTags[] = - { - kATSUFontTag , - kATSUSizeTag , - kATSUVerticalCharacterTag, - kATSUQDBoldfaceTag , - kATSUQDItalicTag , - kATSUQDUnderlineTag , - }; - ByteCount atsuSizes[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] = - { - sizeof( ATSUFontID ) , - sizeof( Fixed ) , - sizeof( ATSUVerticalCharacterType), - sizeof( Boolean ) , - sizeof( Boolean ) , - sizeof( Boolean ) , - }; - Boolean kTrue = true ; - Boolean kFalse = false ; + if (m_info.m_weight == wxFONTWEIGHT_BOLD) + traits |= kCTFontBoldTrait; + if (m_info.m_style == wxFONTSTYLE_ITALIC || m_info.m_style == wxFONTSTYLE_SLANT) + traits |= kCTFontItalicTrait; - Fixed atsuSize = IntToFixed( m_pointSize ); - ATSUVerticalCharacterType kHorizontal = kATSUStronglyHorizontal; - ATSUAttributeValuePtr atsuValues[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] = - { - &fmfont , - &atsuSize , - &kHorizontal, - (m_weight == wxFONTWEIGHT_BOLD) ? &kTrue : &kFalse , - (m_style == wxFONTSTYLE_ITALIC || m_style == wxFONTSTYLE_SLANT) ? &kTrue : &kFalse , - (m_underlined) ? &kTrue : &kFalse , - }; + // use font caching + wxString lookupnameWithSize = wxString::Format( "%s_%ld_%ld", m_info.m_faceName.c_str(), traits, m_info.m_pointSize ); - if ( m_macATSUStyle ) + static std::map< std::wstring , wxCFRef< CTFontRef > > fontcache ; + m_ctFont = fontcache[ std::wstring(lookupnameWithSize.wc_str()) ]; + if ( !m_ctFont ) { - ::ATSUDisposeStyle((ATSUStyle)m_macATSUStyle); - m_macATSUStyle = NULL ; + m_ctFont.reset( CTFontCreateWithFontDescriptor( m_info.m_ctFontDescriptor, 0/*m_pointSize */, NULL ) ); } - status = ::ATSUCreateStyle((ATSUStyle *)&m_macATSUStyle); - wxASSERT_MSG( status == noErr , wxT("couldn't create ATSU style") ); - status = ::ATSUSetAttributes( - (ATSUStyle)m_macATSUStyle, - sizeof(atsuTags) / sizeof(ATSUAttributeTag) , - atsuTags, atsuSizes, atsuValues); -#endif } + #endif #if wxOSX_USE_ATSU_TEXT { - OSStatus status = noErr; - Str255 qdFontName ; - if ( m_macThemeFontID != kThemeCurrentPortFont ) - { - Style style ; - GetThemeFont( m_macThemeFontID, GetApplicationScript(), qdFontName, &m_macFontSize, &style ); - if ( m_macFontSize == 0 ) - m_macFontSize = 12; - m_macFontStyle = style ; - m_faceName = wxMacMakeStringFromPascal( qdFontName ); - if ( m_macFontStyle & bold ) - m_weight = wxFONTWEIGHT_BOLD ; - else - m_weight = wxFONTWEIGHT_NORMAL ; - if ( m_macFontStyle & italic ) - m_style = wxFONTSTYLE_ITALIC ; - if ( m_macFontStyle & underline ) - m_underlined = true ; - m_pointSize = m_macFontSize ; - m_macFontFamily = FMGetFontFamilyFromName( qdFontName ); - } - else - { - if ( m_faceName.empty() ) - { - if ( m_family == wxFONTFAMILY_DEFAULT ) - { - m_macFontFamily = GetAppFont(); - FMGetFontFamilyName(m_macFontFamily,qdFontName); - m_faceName = wxMacMakeStringFromPascal( qdFontName ); - } - else - { - switch ( m_family ) - { - case wxFONTFAMILY_SCRIPT : - case wxFONTFAMILY_ROMAN : - case wxFONTFAMILY_DECORATIVE : - m_faceName = wxT("Times"); - break ; - - case wxFONTFAMILY_SWISS : - m_faceName = wxT("Helvetica"); - break ; - - case wxFONTFAMILY_MODERN : - case wxFONTFAMILY_TELETYPE: - m_faceName = wxT("Courier"); - break ; - - default: - m_faceName = wxT("Times"); - break ; - } - wxMacStringToPascal( m_faceName , qdFontName ); - m_macFontFamily = FMGetFontFamilyFromName( qdFontName ); - if ( m_macFontFamily == kInvalidFontFamily ) - { - wxLogDebug( wxT("ATSFontFamilyFindFromName failed for %s"), m_faceName.c_str() ); - m_macFontFamily = GetAppFont(); - } - } - } - else - { - if ( m_faceName == wxT("systemfont") ) - m_macFontFamily = GetSysFont(); - else if ( m_faceName == wxT("applicationfont") ) - m_macFontFamily = GetAppFont(); - else - { - wxCFStringRef cf( m_faceName, wxLocale::GetSystemEncoding() ); - ATSFontFamilyRef atsfamily = ATSFontFamilyFindFromName( cf , kATSOptionFlagsDefault ); - if ( atsfamily == (ATSFontFamilyRef) -1 ) - { - wxLogDebug( wxT("ATSFontFamilyFindFromName failed for ") + m_faceName ); - m_macFontFamily = GetAppFont(); - } - else - m_macFontFamily = FMGetFontFamilyFromATSFontFamilyRef( atsfamily ); - } - } - - m_macFontStyle = 0; - if (m_weight == wxFONTWEIGHT_BOLD) - m_macFontStyle |= bold; - if (m_style == wxFONTSTYLE_ITALIC || m_style == wxFONTSTYLE_SLANT) - m_macFontStyle |= italic; - if (m_underlined) - m_macFontStyle |= underline; - m_macFontSize = m_pointSize ; - } - // we try to get as much styles as possible into ATSU - - // ATSUFontID and FMFont are equivalent - FMFontStyle intrinsicStyle = 0 ; - status = FMGetFontFromFontFamilyInstance( m_macFontFamily , m_macFontStyle , &m_macATSUFontID , &intrinsicStyle); - wxASSERT_MSG( status == noErr , wxT("couldn't get an ATSUFont from font family") ); - m_macATSUAdditionalQDStyles = m_macFontStyle & (~intrinsicStyle ); - - if ( m_macATSUStyle ) - { - ::ATSUDisposeStyle((ATSUStyle)m_macATSUStyle); - m_macATSUStyle = NULL ; - } - - status = ::ATSUCreateStyle((ATSUStyle *)&m_macATSUStyle); + OSStatus status = ::ATSUCreateStyle(&m_macATSUStyle); wxASSERT_MSG( status == noErr , wxT("couldn't create ATSU style") ); ATSUAttributeTag atsuTags[] = @@ -667,18 +446,19 @@ void wxFontRefData::MacFindFont() Boolean kTrue = true ; Boolean kFalse = false ; - Fixed atsuSize = IntToFixed( m_macFontSize ); + Fixed atsuSize = IntToFixed( m_info.m_pointSize ); ATSUVerticalCharacterType kHorizontal = kATSUStronglyHorizontal; + FMFontStyle addQDStyle = m_info.m_atsuAdditionalQDStyles; ATSUAttributeValuePtr atsuValues[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] = { - &m_macATSUFontID , + &m_info.m_atsuFontID , &atsuSize , &kHorizontal, - (m_macATSUAdditionalQDStyles & bold) ? &kTrue : &kFalse , - (m_macATSUAdditionalQDStyles & italic) ? &kTrue : &kFalse , - (m_macATSUAdditionalQDStyles & underline) ? &kTrue : &kFalse , - (m_macATSUAdditionalQDStyles & condense) ? &kTrue : &kFalse , - (m_macATSUAdditionalQDStyles & extend) ? &kTrue : &kFalse , + (addQDStyle & bold) ? &kTrue : &kFalse , + (addQDStyle & italic) ? &kTrue : &kFalse , + (addQDStyle & underline) ? &kTrue : &kFalse , + (addQDStyle & condense) ? &kTrue : &kFalse , + (addQDStyle & extend) ? &kTrue : &kFalse , }; status = ::ATSUSetAttributes( @@ -690,6 +470,13 @@ void wxFontRefData::MacFindFont() return; } #endif +#if wxOSX_USE_COCOA + m_nsFont = wxFont::CreateNSFont( &m_info ); +#endif +#if wxOSX_USE_IPHONE + m_uiFont = wxFont::CreateUIFont( &m_info ); +#endif + m_fontValid = true; } // ---------------------------------------------------------------------------- @@ -698,9 +485,12 @@ void wxFontRefData::MacFindFont() bool wxFont::Create(const wxNativeFontInfo& info) { - return Create( - info.pointSize, info.family, info.style, info.weight, - info.underlined, info.faceName, info.encoding ); + UnRef(); + + m_refData = new wxFontRefData( info ); + RealizeResource(); + + return true; } wxFont::wxFont(const wxString& fontdesc) @@ -715,102 +505,77 @@ bool wxFont::Create(int pointSize, wxFontStyle style, wxFontWeight weight, bool underlined, - const wxString& faceName, + const wxString& faceNameParam, wxFontEncoding encoding) { UnRef(); + + wxString faceName = faceNameParam; - m_refData = new wxFontRefData( - pointSize, family, style, weight, + if ( faceName.empty() ) + { + switch ( family ) + { + case wxFONTFAMILY_DEFAULT : + faceName = wxT("Lucida Grande"); + break; + + case wxFONTFAMILY_SCRIPT : + case wxFONTFAMILY_ROMAN : + case wxFONTFAMILY_DECORATIVE : + faceName = wxT("Times"); + break ; + + case wxFONTFAMILY_SWISS : + faceName = wxT("Helvetica"); + break ; + + case wxFONTFAMILY_MODERN : + case wxFONTFAMILY_TELETYPE: + faceName = wxT("Courier"); + break ; + + default: + faceName = wxT("Times"); + break ; + } + } + + wxNativeFontInfo info; + + info.Init(pointSize, family, style, weight, underlined, faceName, encoding); - RealizeResource(); + m_refData = new wxFontRefData(info); return true; } -#if wxOSX_USE_CORE_TEXT - -bool wxFont::MacCreateFromUIFont(wxUint32 ctFontType ) +bool wxFont::CreateSystemFont(wxOSXSystemFont font) { UnRef(); - - m_refData = new wxFontRefData(ctFontType); - RealizeResource(); - + + m_refData = new wxFontRefData( font, 0 ); + return true; } -bool wxFont::MacCreateFromCTFontDescriptor( const void * ctFontDescriptor , int size ) +wxFont::~wxFont() { - UnRef(); +} - m_refData = new wxFontRefData((CTFontDescriptorRef)ctFontDescriptor, size);; - RealizeResource(); +bool wxFont::RealizeResource() +{ + M_FONTDATA->MacFindFont(); return true; } - -#endif - -#if wxOSX_USE_ATSU_TEXT -bool wxFont::MacCreateFromThemeFont(wxUint16 themeFontID) -{ -#if wxOSX_USE_CORE_TEXT - if ( UMAGetSystemVersion() >= 0x1050) - { - return MacCreateFromUIFont(HIThemeGetUIFontType(themeFontID)); - } -#endif - { - UnRef(); - - m_refData = new wxFontRefData( - 12, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL, - false, wxEmptyString, wxFONTENCODING_DEFAULT ); - - M_FONTDATA->m_macThemeFontID = themeFontID ; - RealizeResource(); - return true; - } - return false; -} -#endif - -wxFont::~wxFont() -{ -} - -bool wxFont::RealizeResource() -{ - M_FONTDATA->MacFindFont(); - - return true; -} - -void wxFont::SetEncoding(wxFontEncoding encoding) -{ - Unshare(); +void wxFont::SetEncoding(wxFontEncoding encoding) +{ + AllocExclusive(); M_FONTDATA->SetEncoding( encoding ); - - RealizeResource(); -} - -void wxFont::Unshare() -{ - // Don't change shared data - if (!m_refData) - { - m_refData = new wxFontRefData(); - } - else - { - wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData); - UnRef(); - m_refData = ref; - } } wxGDIRefData *wxFont::CreateGDIRefData() const @@ -828,67 +593,53 @@ void wxFont::SetPointSize(int pointSize) if ( M_FONTDATA->GetPointSize() == pointSize ) return; - Unshare(); + AllocExclusive(); M_FONTDATA->SetPointSize( pointSize ); - - RealizeResource(); } void wxFont::SetFamily(wxFontFamily family) { - Unshare(); + AllocExclusive(); M_FONTDATA->SetFamily( family ); - - RealizeResource(); } void wxFont::SetStyle(wxFontStyle style) { - Unshare(); + AllocExclusive(); M_FONTDATA->SetStyle( style ); - - RealizeResource(); } void wxFont::SetWeight(wxFontWeight weight) { - Unshare(); + AllocExclusive(); M_FONTDATA->SetWeight( weight ); - - RealizeResource(); } bool wxFont::SetFaceName(const wxString& faceName) { - Unshare(); + AllocExclusive(); M_FONTDATA->SetFaceName( faceName ); - RealizeResource(); - return wxFontBase::SetFaceName(faceName); } void wxFont::SetUnderlined(bool underlined) { - Unshare(); + AllocExclusive(); M_FONTDATA->SetUnderlined( underlined ); - - RealizeResource(); } void wxFont::SetNoAntiAliasing( bool no ) { - Unshare(); + AllocExclusive(); M_FONTDATA->SetNoAntiAliasing( no ); - - RealizeResource(); } // ---------------------------------------------------------------------------- @@ -968,77 +719,531 @@ bool wxFont::GetNoAntiAliasing() const return M_FONTDATA->GetNoAntiAliasing(); } -#if wxOSX_USE_ATSU_TEXT +#if wxOSX_USE_ATSU_TEXT && wxOSX_USE_CARBON short wxFont::MacGetFontNum() const { wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") ); - return M_FONTDATA->m_macFontFamily; + // cast away constness otherwise lazy font resolution is not possible + const_cast(this)->RealizeResource(); + + return M_FONTDATA->m_info.m_qdFontFamily; } -short wxFont::MacGetFontSize() const +wxByte wxFont::MacGetFontStyle() const { wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") ); - return M_FONTDATA->m_macFontSize; + // cast away constness otherwise lazy font resolution is not possible + const_cast(this)->RealizeResource(); + + return M_FONTDATA->m_info.m_qdFontStyle; } -wxByte wxFont::MacGetFontStyle() const +wxUint16 wxFont::MacGetThemeFontID() const { wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") ); - return M_FONTDATA->m_macFontStyle; + return M_FONTDATA->m_macThemeFontID; } -wxUint32 wxFont::MacGetATSUFontID() const +#endif + +#if wxOSX_USE_CORE_TEXT || wxOSX_USE_ATSU_TEXT +void * wxFont::MacGetATSUStyle() const { - wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") ); + wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") ); - return M_FONTDATA->m_macATSUFontID; + // cast away constness otherwise lazy font resolution is not possible + const_cast(this)->RealizeResource(); + + return M_FONTDATA->m_macATSUStyle; } +#endif + +#if wxOSX_USE_CORE_TEXT -wxUint32 wxFont::MacGetATSUAdditionalQDStyles() const +CTFontRef wxFont::GetCTFont() const { wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") ); - return M_FONTDATA->m_macATSUAdditionalQDStyles; + // cast away constness otherwise lazy font resolution is not possible + const_cast(this)->RealizeResource(); + + return (CTFontRef)(M_FONTDATA->m_ctFont); } -wxUint16 wxFont::MacGetThemeFontID() const +#endif + +#if wxOSX_USE_COCOA + +NSFont* wxFont::GetNSFont() const { wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") ); - return M_FONTDATA->m_macThemeFontID; + // cast away constness otherwise lazy font resolution is not possible + const_cast(this)->RealizeResource(); + + return (M_FONTDATA->m_nsFont); } + #endif -#if wxOSX_USE_CORE_TEXT || wxOSX_USE_ATSU_TEXT -void * wxFont::MacGetATSUStyle() const +const wxNativeFontInfo * wxFont::GetNativeFontInfo() const { wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") ); + wxCHECK_MSG( Ok(), NULL, wxT("invalid font") ); - return M_FONTDATA->m_macATSUStyle; + // cast away constness otherwise lazy font resolution is not possible + const_cast(this)->RealizeResource(); + + // M_FONTDATA->m_info.InitFromFont(*this); + + return &(M_FONTDATA->m_info); +} + +// ---------------------------------------------------------------------------- +// wxNativeFontInfo +// ---------------------------------------------------------------------------- + +#if wxOSX_USE_CORE_TEXT + +/* from Core Text Manual Common Operations */ + +static CTFontDescriptorRef wxMacCreateCTFontDescriptor(CFStringRef iFamilyName, CTFontSymbolicTraits iTraits ) +{ + CTFontDescriptorRef descriptor = NULL; + CFMutableDictionaryRef attributes; + + assert(iFamilyName != NULL); + // Create a mutable dictionary to hold our attributes. + attributes = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, + &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); + check(attributes != NULL); + + if (attributes != NULL) { + // Add a family name to our attributes. + CFDictionaryAddValue(attributes, kCTFontFamilyNameAttribute, iFamilyName); + + + if ( iTraits ) { + CFMutableDictionaryRef traits; + CFNumberRef symTraits; + + // Create the traits dictionary. + symTraits = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, + &iTraits); + check(symTraits != NULL); + + if (symTraits != NULL) { + // Create a dictionary to hold our traits values. + traits = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, + &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); + check(traits != NULL); + + if (traits != NULL) { + // Add the symbolic traits value to the traits dictionary. + CFDictionaryAddValue(traits, kCTFontSymbolicTrait, symTraits); + + // Add the traits attribute to our attributes. + CFDictionaryAddValue(attributes, kCTFontTraitsAttribute, traits); + CFRelease(traits); + } + CFRelease(symTraits); + } + } + // Create the font descriptor with our attributes + descriptor = CTFontDescriptorCreateWithAttributes(attributes); + check(descriptor != NULL); + + CFRelease(attributes); + } + // Return our font descriptor. + return descriptor ; +} + +#endif + +void wxNativeFontInfo::Init() +{ +#if wxOSX_USE_CORE_TEXT + m_ctFontDescriptor = NULL; +#endif +#if wxOSX_USE_ATSU_TEXT + m_atsuFontID = 0 ; + m_atsuAdditionalQDStyles = 0; + m_atsuFontValid = false; +#if wxOSX_USE_CARBON + m_qdFontStyle = 0; + m_qdFontFamily = 0; +#endif +#endif +#if wxOSX_USE_COCOA + m_nsFontDescriptor = NULL; +#endif +#if wxOSX_USE_IPHONE + m_uiFontDescriptor = NULL; +#endif + m_pointSize = 0; + m_family = wxFONTFAMILY_DEFAULT; + m_style = wxFONTSTYLE_NORMAL; + m_weight = wxFONTWEIGHT_NORMAL; + m_underlined = false; + m_faceName.clear(); + m_encoding = wxFONTENCODING_DEFAULT; + m_descriptorValid = false; +} + +#if wxOSX_USE_CORE_TEXT +void wxNativeFontInfo::Init(CTFontDescriptorRef descr) +{ + Init(); + m_ctFontDescriptor = wxCFRetain(descr); + + wxCFRef< CFNumberRef > sizevalue( (CFNumberRef) CTFontDescriptorCopyAttribute( m_ctFontDescriptor, kCTFontSizeAttribute ) ); + float fsize; + if ( CFNumberGetValue( sizevalue , kCFNumberFloatType , &fsize ) ) + m_pointSize = (int)( fsize + 0.5 ); + + wxCFRef< CFDictionaryRef > traitsvalue( (CFDictionaryRef) CTFontDescriptorCopyAttribute( m_ctFontDescriptor, kCTFontTraitsAttribute ) ); + CTFontSymbolicTraits traits; + if ( CFNumberGetValue((CFNumberRef) CFDictionaryGetValue(traitsvalue,kCTFontSymbolicTrait),kCFNumberIntType,&traits) ) + { + if ( traits & kCTFontItalicTrait ) + m_style = wxFONTSTYLE_ITALIC; + if ( traits & kCTFontBoldTrait ) + m_weight = wxFONTWEIGHT_BOLD ; + } + + wxCFStringRef familyName( (CFStringRef) CTFontDescriptorCopyAttribute(m_ctFontDescriptor, kCTFontFamilyNameAttribute)); + m_faceName = familyName.AsString(); } #endif +void wxNativeFontInfo::EnsureValid() +{ + if ( m_descriptorValid ) + return; + #if wxOSX_USE_CORE_TEXT + if ( m_ctFontDescriptor == NULL && UMAGetSystemVersion() >= 0x1050 ) + { + CTFontSymbolicTraits traits = 0; + + if (m_weight == wxFONTWEIGHT_BOLD) + traits |= kCTFontBoldTrait; + if (m_style == wxFONTSTYLE_ITALIC || m_style == wxFONTSTYLE_SLANT) + traits |= kCTFontItalicTrait; + + // use font caching + wxString lookupnameWithSize = wxString::Format( "%s_%ld_%ld", m_faceName.c_str(), traits, m_pointSize ); + + static std::map< std::wstring , wxCFRef< CTFontDescriptorRef > > fontdescriptorcache ; + m_ctFontDescriptor = wxCFRetain((CTFontDescriptorRef)fontdescriptorcache[ std::wstring(lookupnameWithSize.wc_str()) ]); + if ( !m_ctFontDescriptor ) + { + // QD selection algorithm is the fastest by orders of magnitude on 10.5 + if ( m_faceName.IsAscii() ) + { + uint8_t qdstyle = 0; + if (m_weight == wxFONTWEIGHT_BOLD) + qdstyle |= bold; + if (m_style == wxFONTSTYLE_ITALIC || m_style == wxFONTSTYLE_SLANT) + qdstyle |= italic; + + Str255 qdFontName ; + wxMacStringToPascal( m_faceName , qdFontName ); + wxCFRef< CTFontRef > font; + font.reset( CTFontCreateWithQuickdrawInstance(qdFontName, 0 , qdstyle, m_pointSize) ); + m_ctFontDescriptor = CTFontCopyFontDescriptor(font); + } + else + { + m_ctFontDescriptor = wxMacCreateCTFontDescriptor( wxCFStringRef(m_faceName),traits ); + } + fontdescriptorcache[ std::wstring(lookupnameWithSize.wc_str()) ].reset(wxCFRetain(m_ctFontDescriptor)); + } + } +#endif +#if wxOSX_USE_ATSU_TEXT + if ( !m_atsuFontValid ) + { + wxCFStringRef cf( m_faceName, wxLocale::GetSystemEncoding() ); + ATSFontFamilyRef atsfamily = ATSFontFamilyFindFromName( cf , kATSOptionFlagsDefault ); + if ( atsfamily == (ATSFontFamilyRef) -1 ) + { + wxLogDebug( wxT("ATSFontFamilyFindFromName failed for ") + m_faceName ); + m_qdFontFamily = GetAppFont(); + } + else + { + m_qdFontFamily = FMGetFontFamilyFromATSFontFamilyRef( atsfamily ); + } + + m_qdFontStyle = 0; + if (m_weight == wxFONTWEIGHT_BOLD) + m_qdFontStyle |= bold; + if (m_style == wxFONTSTYLE_ITALIC || m_style == wxFONTSTYLE_SLANT) + m_qdFontStyle |= italic; + if (m_underlined) + m_qdFontStyle |= underline; + -const void * wxFont::MacGetCTFont() const + // we try to get as much styles as possible into ATSU + + // ATSUFontID and FMFont are equivalent + FMFontStyle intrinsicStyle = 0 ; + OSStatus status = FMGetFontFromFontFamilyInstance( m_qdFontFamily , m_qdFontStyle , (FMFont*)&m_atsuFontID , &intrinsicStyle); + wxASSERT_MSG( status == noErr , wxT("couldn't get an ATSUFont from font family") ); + m_atsuAdditionalQDStyles = m_qdFontStyle & (~intrinsicStyle ); + m_atsuFontValid = true; + } +#endif +#if wxOSX_USE_COCOA + if ( m_nsFontDescriptor == NULL ) + ValidateNSFontDescriptor(); +#endif +#if wxOSX_USE_IPHONE + // TODO +#endif + m_descriptorValid = true; +} + +void wxNativeFontInfo::Init(const wxNativeFontInfo& info) { - wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") ); + Init(); +#if wxOSX_USE_CORE_TEXT + m_ctFontDescriptor = wxCFRetain(info.m_ctFontDescriptor); +#endif +#if wxOSX_USE_ATSU_TEXT + m_atsuFontValid = info.m_atsuFontValid; + m_atsuFontID = info.m_atsuFontID ; + m_atsuAdditionalQDStyles = info.m_atsuAdditionalQDStyles; +#if wxOSX_USE_CARBON + m_qdFontFamily = info.m_qdFontFamily; + m_qdFontStyle = info.m_qdFontStyle; +#endif +#endif +#if wxOSX_USE_COCOA + m_nsFontDescriptor = (NSFontDescriptor*) wxMacCocoaRetain(info.m_nsFontDescriptor); +#endif +#if wxOSX_USE_IPHONE + m_uiFontDescriptor = wxMacCocoaRetain(info.m_uiFontDescriptor);; +#endif + m_pointSize = info.m_pointSize; + m_family = info.m_family; + m_style = info.m_style; + m_weight = info.m_weight; + m_underlined = info.m_underlined; + m_faceName = info.m_faceName; + m_encoding = info.m_encoding; + m_descriptorValid = info.m_descriptorValid; +} + +void wxNativeFontInfo::Init(int size, + wxFontFamily family, + wxFontStyle style, + wxFontWeight weight, + bool underlined, + const wxString& faceName, + wxFontEncoding encoding) +{ + Init(); + m_pointSize = size; + m_family = family; + m_style = style; + m_weight = weight; + m_underlined = underlined; + m_faceName = faceName; + m_encoding = encoding; - return (CTFontRef)(M_FONTDATA->m_ctFont); } +void wxNativeFontInfo::Free() +{ +#if wxOSX_USE_CORE_TEXT + wxCFRelease(m_ctFontDescriptor); + m_ctFontDescriptor = NULL; +#endif +#if wxOSX_USE_ATSU_TEXT + m_atsuFontID = 0 ; + m_atsuAdditionalQDStyles = 0; + m_atsuFontValid = false; +#endif +#if wxOSX_USE_COCOA + wxMacCocoaRelease(m_nsFontDescriptor); + m_nsFontDescriptor = NULL; +#endif +#if wxOSX_USE_IPHONE + wxMacCocoaRelease(m_uiFontDescriptor); + m_uiFontDescriptor = NULL #endif + m_descriptorValid = false; +} -const wxNativeFontInfo * wxFont::GetNativeFontInfo() const +bool wxNativeFontInfo::FromString(const wxString& s) { - wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") ); - wxCHECK_MSG( Ok(), NULL, wxT("invalid font") ); + long l; - M_FONTDATA->m_info.InitFromFont(*this); + wxStringTokenizer tokenizer(s, _T(";")); - return &(M_FONTDATA->m_info); + wxString token = tokenizer.GetNextToken(); + // + // Ignore the version for now + // + + token = tokenizer.GetNextToken(); + if ( !token.ToLong(&l) ) + return false; + m_pointSize = (int)l; + + token = tokenizer.GetNextToken(); + if ( !token.ToLong(&l) ) + return false; + m_family = (wxFontFamily)l; + + token = tokenizer.GetNextToken(); + if ( !token.ToLong(&l) ) + return false; + m_style = (wxFontStyle)l; + + token = tokenizer.GetNextToken(); + if ( !token.ToLong(&l) ) + return false; + m_weight = (wxFontWeight)l; + + token = tokenizer.GetNextToken(); + if ( !token.ToLong(&l) ) + return false; + m_underlined = l != 0; + + m_faceName = tokenizer.GetNextToken(); + +#ifndef __WXMAC__ + if( !faceName ) + return false; +#endif + + token = tokenizer.GetNextToken(); + if ( !token.ToLong(&l) ) + return false; + m_encoding = (wxFontEncoding)l; + + return true; +} + +wxString wxNativeFontInfo::ToString() const +{ + wxString s; + + s.Printf(_T("%d;%d;%d;%d;%d;%d;%s;%d"), + 0, // version + m_pointSize, + m_family, + (int)m_style, + (int)m_weight, + m_underlined, + m_faceName.GetData(), + (int)m_encoding); + + return s; +} + +int wxNativeFontInfo::GetPointSize() const +{ + return m_pointSize; +} + +wxFontStyle wxNativeFontInfo::GetStyle() const +{ + return m_style; +} + +wxFontWeight wxNativeFontInfo::GetWeight() const +{ + return m_weight; +} + +bool wxNativeFontInfo::GetUnderlined() const +{ + return m_underlined; +} + +wxString wxNativeFontInfo::GetFaceName() const +{ + return m_faceName; +} + +wxFontFamily wxNativeFontInfo::GetFamily() const +{ + return m_family; } + +wxFontEncoding wxNativeFontInfo::GetEncoding() const +{ + return m_encoding; +} + +// changing the font descriptor + +void wxNativeFontInfo::SetPointSize(int pointsize) +{ + if ( m_pointSize != pointsize ) + { + m_pointSize = pointsize; + Free(); + } +} + +void wxNativeFontInfo::SetStyle(wxFontStyle style_) +{ + if ( m_style != style_ ) + { + m_style = style_; + Free(); + } +} + +void wxNativeFontInfo::SetWeight(wxFontWeight weight_) +{ + if ( m_weight != weight_ ) + { + m_weight = weight_; + Free(); + } +} + +void wxNativeFontInfo::SetUnderlined(bool underlined_) +{ + if ( m_underlined != underlined_ ) + { + m_underlined = underlined_; + Free(); + } +} + +bool wxNativeFontInfo::SetFaceName(const wxString& facename_) +{ + if ( m_faceName != facename_ ) + { + m_faceName = facename_; + Free(); + } + return true; +} + +void wxNativeFontInfo::SetFamily(wxFontFamily family_) +{ + if ( m_family != family_ ) + { + m_family = family_; + Free(); + } +} + +void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding_) +{ + m_encoding = encoding_; + // not reflected in native descriptors +} \ No newline at end of file diff --git a/src/osx/carbon/fontdlg.cpp b/src/osx/carbon/fontdlg.cpp index 221f769ed8..9846fffdc1 100644 --- a/src/osx/carbon/fontdlg.cpp +++ b/src/osx/carbon/fontdlg.cpp @@ -41,6 +41,7 @@ #endif #include "wx/fontdlg.h" +#include "wx/fontutil.h" #if wxOSX_USE_EXPERIMENTAL_FONTDIALOG @@ -82,7 +83,9 @@ wxMacCarbonFontPanelHandler(EventHandlerCallRef WXUNUSED(nextHandler), if ( cEvent.GetParameter( kEventParamCTFontDescriptor, typeCTFontDescriptorRef, &descr ) == noErr ) { wxFont font; - font.MacCreateFromCTFontDescriptor(descr); + wxNativeFontInfo fontinfo; + fontinfo.Init(descr); + font.Create(fontinfo); fontdata.SetChosenFont( font ) ; setup = true; } @@ -229,7 +232,7 @@ int wxFontDialog::ShowModal() #if wxOSX_USE_CORE_TEXT if ( UMAGetSystemVersion() >= 0x1050 ) { - CTFontDescriptorRef descr = (CTFontDescriptorRef) CTFontCopyFontDescriptor( (CTFontRef) font.MacGetCTFont() ); + CTFontDescriptorRef descr = (CTFontDescriptorRef) CTFontCopyFontDescriptor( (CTFontRef) font.GetCTFont() ); err = SetFontInfoForSelection (kFontSelectionCoreTextType,1, &descr , NULL); CFRelease( descr ); setup = true; diff --git a/src/osx/carbon/gdiobj.cpp b/src/osx/carbon/gdiobj.cpp index f768d01bea..31ec9c8017 100644 --- a/src/osx/carbon/gdiobj.cpp +++ b/src/osx/carbon/gdiobj.cpp @@ -51,9 +51,6 @@ void wxStockGDIMac::OnExit() { } -extern wxFont* CreateNormalFont(); -extern wxFont* CreateSmallFont(); - const wxFont* wxStockGDIMac::GetFont(Item item) { wxFont* font = static_cast(ms_stockObject[item]); @@ -61,31 +58,14 @@ const wxFont* wxStockGDIMac::GetFont(Item item) { switch (item) { -#if wxOSX_USE_COCOA_OR_CARBON case FONT_NORMAL: font = new wxFont; -#if wxOSX_USE_ATSU_TEXT - font->MacCreateFromThemeFont(kThemeSystemFont); -#else - font->MacCreateFromUIFont(kCTFontSystemFontType); -#endif + font->CreateSystemFont(wxOSX_SYSTEM_FONT_NORMAL); break; case FONT_SMALL: font = new wxFont; -#if wxOSX_USE_ATSU_TEXT - font->MacCreateFromThemeFont(kThemeSmallSystemFont); -#else - font->MacCreateFromUIFont(kCTFontSmallSystemFontType); -#endif - break; -#else - case FONT_NORMAL: - font = CreateNormalFont() ; - break; - case FONT_SMALL: - font = CreateSmallFont(); + font->CreateSystemFont(wxOSX_SYSTEM_FONT_SMALL); break; -#endif default: font = const_cast(super::GetFont(item)); break; diff --git a/src/osx/carbon/graphics.cpp b/src/osx/carbon/graphics.cpp index 98ce41f0d8..27a0424788 100644 --- a/src/osx/carbon/graphics.cpp +++ b/src/osx/carbon/graphics.cpp @@ -171,7 +171,7 @@ CGColorRef wxMacCreateCGColor( const wxColour& col ) CTFontRef wxMacCreateCTFont( const wxFont& font ) { #ifdef __WXMAC__ - return wxCFRetain((CTFontRef) font.MacGetCTFont()); + return wxCFRetain((CTFontRef) font.GetCTFont()); #else return CTFontCreateWithName( wxCFStringRef( font.GetFaceName(), wxLocale::GetSystemEncoding() ) , font.GetPointSize() , NULL ); #endif @@ -859,7 +859,7 @@ wxMacCoreGraphicsFontData::wxMacCoreGraphicsFontData(wxGraphicsRenderer* rendere // we need the scale here ... - Fixed atsuSize = IntToFixed( int( 1 * font.MacGetFontSize()) ); + Fixed atsuSize = IntToFixed( int( 1 * font.GetPointSize()) ); RGBColor atsuColor ; col.GetRGBColor( &atsuColor ); ATSUAttributeTag atsuTags[] = diff --git a/src/osx/carbon/listctrl_mac.cpp b/src/osx/carbon/listctrl_mac.cpp index f16559a92f..2f5c444d6d 100644 --- a/src/osx/carbon/listctrl_mac.cpp +++ b/src/osx/carbon/listctrl_mac.cpp @@ -783,11 +783,7 @@ wxListCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) attr.colFg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT ); attr.colBg = wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOX ); -#if wxOSX_USE_ATSU_TEXT - attr.font.MacCreateFromThemeFont( kThemeViewsFont ) ; -#else - attr.font.MacCreateFromUIFont( kCTFontViewsFontType ) ; -#endif + attr.font.CreateSystemFont(wxOSX_SYSTEM_FONT_VIEWS); return attr; } @@ -2857,7 +2853,7 @@ void wxMacDataBrowserListCtrlControl::DrawItem( if (font.Ok()) { info.fontID = kThemeSpecifiedFont; - info.font = (CTFontRef) font.MacGetCTFont(); + info.font = (CTFontRef) font.GetCTFont(); setup = true; } } @@ -2872,7 +2868,7 @@ void wxMacDataBrowserListCtrlControl::DrawItem( { info.fontID = font.MacGetThemeFontID(); - ::TextSize( (short)(font.MacGetFontSize()) ) ; + ::TextSize( (short)(font.GetPointSize()) ) ; ::TextFace( font.MacGetFontStyle() ) ; } } diff --git a/src/osx/carbon/utilscocoa.mm b/src/osx/carbon/utilscocoa.mm index 1617dd37f6..3b33707031 100644 --- a/src/osx/carbon/utilscocoa.mm +++ b/src/osx/carbon/utilscocoa.mm @@ -21,6 +21,8 @@ #include "wx/osx/private.h" #endif +#include "wx/fontutil.h" + #ifdef __WXMAC__ #if wxOSX_USE_CARBON @@ -79,17 +81,113 @@ void wxMacCocoaAutorelease( void* obj ) [(NSObject*)obj autorelease]; } -void wxMacCocoaRetain( void* obj ) +void* wxMacCocoaRetain( void* obj ) { [(NSObject*)obj retain]; + return obj; } +// ---------------------------------------------------------------------------- +// NSFont Utils +// ---------------------------------------------------------------------------- + #if wxOSX_USE_COCOA +WX_NSFont wxFont::CreateNSFont(wxOSXSystemFont font, wxNativeFontInfo* info) +{ + NSFont* nsfont; + switch( font ) + { + case wxOSX_SYSTEM_FONT_NORMAL: + nsfont = [NSFont systemFontOfSize:[NSFont systemFontSize]]; + break; + case wxOSX_SYSTEM_FONT_BOLD: + nsfont = [NSFont boldSystemFontOfSize:[NSFont systemFontSize]]; + break; + case wxOSX_SYSTEM_FONT_SMALL: + nsfont = [NSFont systemFontOfSize:[NSFont smallSystemFontSize]]; + break; + case wxOSX_SYSTEM_FONT_SMALL_BOLD: + nsfont = [NSFont boldSystemFontOfSize:[NSFont smallSystemFontSize]]; + break; + case wxOSX_SYSTEM_FONT_MINI: + nsfont = [NSFont systemFontOfSize:[NSFont systemFontSize]]; + break; + case wxOSX_SYSTEM_FONT_MINI_BOLD: + nsfont = [NSFont boldSystemFontOfSize: + [NSFont systemFontSizeForControlSize:NSMiniControlSize]]; + break; + case wxOSX_SYSTEM_FONT_LABELS: + nsfont = [NSFont labelFontOfSize: + [NSFont systemFontSizeForControlSize:NSMiniControlSize]]; + break; + case wxOSX_SYSTEM_FONT_VIEWS: + nsfont = [NSFont controlContentFontOfSize:0]; + break; + default: + break; + } + [nsfont retain]; + NSFontDescriptor*desc = [[nsfont fontDescriptor] retain]; + if ( info->m_faceName.empty()) + { + wxFontStyle fontstyle = wxFONTSTYLE_NORMAL; + wxFontWeight fontweight = wxFONTWEIGHT_NORMAL; + bool underlined = false; + + int size = (int) ([desc pointSize]+0.5); + NSFontSymbolicTraits traits = [desc symbolicTraits]; + + if ( traits & NSFontBoldTrait ) + fontweight = wxFONTWEIGHT_BOLD ; + else + fontweight = wxFONTWEIGHT_NORMAL ; + if ( traits & NSFontItalicTrait ) + fontstyle = wxFONTSTYLE_ITALIC ; + + wxCFStringRef fontname( [desc postscriptName] ); + info->Init(size,wxFONTFAMILY_DEFAULT,fontstyle,fontweight,underlined, + fontname.AsString(), wxFONTENCODING_DEFAULT); + + } + info->m_nsFontDescriptor = desc; + return nsfont; +} + +void wxNativeFontInfo::ValidateNSFontDescriptor() +{ + NSFontDescriptor* desc = [NSFont fontWithName:wxCFStringRef(m_faceName).AsNSString() size:m_pointSize]; + NSFontSymbolicTraits traits = 0; + + if (m_weight == wxFONTWEIGHT_BOLD) + traits |= NSFontBoldTrait; + if (m_style == wxFONTSTYLE_ITALIC || m_style == wxFONTSTYLE_SLANT) + traits |= NSFontItalicTrait; + + if ( traits != 0 ) + { + [desc autorelease]; + desc = [desc fontDescriptorWithSymbolicTraits:traits]; + } + m_nsFontDescriptor = desc; +} + +WX_NSFont wxFont::CreateNSFont(const wxNativeFontInfo* info) +{ + NSFont* nsFont; + nsFont = [NSFont fontWithDescriptor:info->m_nsFontDescriptor size:info->m_pointSize]; + + return nsFont; +} + +#endif + // ---------------------------------------------------------------------------- // NSImage Utils // ---------------------------------------------------------------------------- +#if wxOSX_USE_COCOA + // From "Cocoa Drawing Guide:Working with Images" WX_NSImage wxOSXCreateNSImageFromCGImage( CGImageRef image ) { diff --git a/src/osx/carbon/window.cpp b/src/osx/carbon/window.cpp index 87c1eb5d89..af31a5cce9 100644 --- a/src/osx/carbon/window.cpp +++ b/src/osx/carbon/window.cpp @@ -1228,7 +1228,7 @@ void wxMacControl::SetFont( const wxFont & font , const wxColour& foreground , l flush = kHIThemeTextHorizontalFlushCenter; else if ( ( windowStyle & wxALIGN_MASK ) & wxALIGN_RIGHT ) flush = kHIThemeTextHorizontalFlushRight; - HIViewSetTextFont( m_controlRef , part , (CTFontRef) font.MacGetCTFont() ); + HIViewSetTextFont( m_controlRef , part , (CTFontRef) font.GetCTFont() ); HIViewSetTextHorizontalFlush( m_controlRef, part, flush ); if ( foreground != *wxBLACK || ignoreBlack == false ) @@ -1269,7 +1269,7 @@ void wxMacControl::SetFont( const wxFont & font , const wxColour& foreground , l { fontStyle.font = font.MacGetFontNum(); fontStyle.style = font.MacGetFontStyle(); - fontStyle.size = font.MacGetFontSize(); + fontStyle.size = font.GetPointSize(); fontStyle.flags = kControlUseFontMask | kControlUseFaceMask | kControlUseSizeMask; } diff --git a/src/osx/cocoa/stattext.mm b/src/osx/cocoa/stattext.mm index d37673bc8c..0ec3ff70c3 100644 --- a/src/osx/cocoa/stattext.mm +++ b/src/osx/cocoa/stattext.mm @@ -27,14 +27,12 @@ #include -@interface wxNSStaticTextView : NSTextView +@interface wxNSStaticTextView : NSTextField { wxWidgetCocoaImpl* impl; } - -- (void) setImplementation:(wxWidgetCocoaImpl*) item; -- (wxWidgetCocoaImpl*) implementation; @end + @implementation wxNSStaticTextView + (void)initialize @@ -47,83 +45,46 @@ } } -- (wxWidgetCocoaImpl*) implementation -{ - return impl; -} - -- (void) setImplementation:(wxWidgetCocoaImpl*) item -{ - impl = item; -} @end class wxStaticTextCocoaImpl : public wxWidgetCocoaImpl { public: - wxStaticTextCocoaImpl( wxWindowMac* peer , WXWidget w ) : wxWidgetCocoaImpl(peer, w) + wxStaticTextCocoaImpl( wxWindowMac* peer , WXWidget w , NSLineBreakMode lineBreak) : wxWidgetCocoaImpl(peer, w) { + m_lineBreak = lineBreak; } virtual void SetLabel(const wxString& title, wxFontEncoding encoding) { wxNSStaticTextView* v = (wxNSStaticTextView*)GetWXWidget(); wxWindow* wxpeer = GetWXPeer(); - [v setString: wxCFStringRef( title , wxpeer->GetFont().GetEncoding() ).AsNSString()]; - + NSCell* cell = [v cell]; + wxCFStringRef text( title , wxpeer->GetFont().GetEncoding() ); + + NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; + [paragraphStyle setLineBreakMode:m_lineBreak]; int style = wxpeer->GetWindowStyleFlag(); - NSRange allText = NSMakeRange(0, title.length()); if (style & wxALIGN_CENTER) - [v setAlignment: NSCenterTextAlignment range: allText]; + [paragraphStyle setAlignment: NSCenterTextAlignment]; else if (style & wxALIGN_RIGHT) - [v setAlignment: NSRightTextAlignment range: allText]; + [paragraphStyle setAlignment: NSRightTextAlignment]; + + NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:paragraphStyle, NSParagraphStyleAttributeName, nil]; + NSAttributedString *attrstring = [[NSAttributedString alloc] initWithString:text.AsNSString() attributes:dict]; + [cell setAttributedStringValue:attrstring]; + [attrstring release]; + [paragraphStyle release]; } +private : + NSLineBreakMode m_lineBreak; }; wxSize wxStaticText::DoGetBestSize() const { - Point bounds; - -#if wxOSX_USE_ATSU_TEXT - OSStatus err = noErr; - wxCFStringRef str( m_label, GetFont().GetEncoding() ); - - SInt16 baseline; - if ( m_font.MacGetThemeFontID() != kThemeCurrentPortFont ) - { - err = GetThemeTextDimensions( - (!m_label.empty() ? (CFStringRef)str : CFSTR(" ")), - m_font.MacGetThemeFontID(), kThemeStateActive, false, &bounds, &baseline ); - verify_noerr( err ); - } - else -#endif - { - wxClientDC dc(const_cast(this)); - wxCoord width, height ; - dc.GetMultiLineTextExtent( m_label , &width, &height); - // FIXME: The calculations returned by this function are too small - // for some strings, so we adjust manually. - bounds.h = width+12; - bounds.v = height+4; - } - - if ( m_label.empty() ) - bounds.h = 0; - - bounds.h += MacGetLeftBorderSize() + MacGetRightBorderSize(); - bounds.v += MacGetTopBorderSize() + MacGetBottomBorderSize(); - - return wxSize( bounds.h, bounds.v ); + return wxWindowMac::DoGetBestSize() ; } -// for wxST_ELLIPSIZE_* support: - -/* - FIXME: UpdateLabel() should be called on size events when wxST_ELLIPSIZE_START is set - to allow correct dynamic ellipsizing of the label -*/ - wxWidgetImplType* wxWidgetImpl::CreateStaticText( wxWindowMac* wxpeer, wxWindowMac* parent, wxWindowID id, @@ -139,29 +100,26 @@ wxWidgetImplType* wxWidgetImpl::CreateStaticText( wxWindowMac* wxpeer, [v setEditable:NO]; [v setDrawsBackground:NO]; [v setSelectable: NO]; - - wxWidgetCocoaImpl* c = new wxStaticTextCocoaImpl( wxpeer, v ); - return c; -/* - Rect bounds = wxMacGetBoundsForControl( wxpeer, pos, size ); - - wxMacControl* peer = new wxMacControl( wxpeer ); - OSStatus err = CreateStaticTextControl( - MAC_WXHWND(parent->MacGetTopLevelWindowRef()), - &bounds, NULL, NULL, peer->GetControlRefAddr() ); - verify_noerr( err ); - - if ( ( style & wxST_ELLIPSIZE_END ) || ( style & wxST_ELLIPSIZE_MIDDLE ) ) + [v setBezeled:NO]; + [v setBordered:NO]; + + NSLineBreakMode linebreak = NSLineBreakByWordWrapping; + if ( ((wxStaticText*)wxpeer)->IsEllipsized() ) { - TruncCode tCode = truncEnd; if ( style & wxST_ELLIPSIZE_MIDDLE ) - tCode = truncMiddle; - - err = peer->SetData( kControlStaticTextTruncTag, tCode ); - err = peer->SetData( kControlStaticTextIsMultilineTag, (Boolean)0 ); + linebreak = NSLineBreakByTruncatingMiddle; + else if (style & wxST_ELLIPSIZE_END ) + linebreak = NSLineBreakByTruncatingTail; + else if (style & wxST_ELLIPSIZE_START ) + linebreak = NSLineBreakByTruncatingHead; } - return peer; - */ + else + { + [[v cell] setWraps:YES]; + } + + wxWidgetCocoaImpl* c = new wxStaticTextCocoaImpl( wxpeer, v, linebreak ); + return c; } #endif //if wxUSE_STATTEXT diff --git a/src/osx/cocoa/window.mm b/src/osx/cocoa/window.mm index ed2ddf329a..f81e0d63b4 100644 --- a/src/osx/cocoa/window.mm +++ b/src/osx/cocoa/window.mm @@ -1352,11 +1352,7 @@ void wxWidgetCocoaImpl::SetControlSize( wxWindowVariant variant ) void wxWidgetCocoaImpl::SetFont(wxFont const& font, wxColour const&, long, bool) { if ([m_osxView respondsToSelector:@selector(setFont:)]) -#if wxOSX_USE_CORE_TEXT - [m_osxView setFont: (NSFont*)font.MacGetCTFont()]; -#else - -#endif + [m_osxView setFont: font.GetNSFont()]; } void wxWidgetCocoaImpl::InstallEventHandler( WXWidget control ) diff --git a/src/osx/listbox_osx.cpp b/src/osx/listbox_osx.cpp index 3b74cc851e..9ab8efb30e 100644 --- a/src/osx/listbox_osx.cpp +++ b/src/osx/listbox_osx.cpp @@ -272,11 +272,7 @@ wxListBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) attr.colFg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT ); attr.colBg = wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOX ); -#if wxOSX_USE_ATSU_TEXT - attr.font.MacCreateFromThemeFont(kThemeViewsFont); -#else - attr.font.MacCreateFromUIFont(kCTFontViewsFontType); -#endif + attr.font.CreateSystemFont(wxOSX_SYSTEM_FONT_VIEWS); return attr; } diff --git a/src/osx/stattext_osx.cpp b/src/osx/stattext_osx.cpp index 6968b2778a..30b871a442 100644 --- a/src/osx/stattext_osx.cpp +++ b/src/osx/stattext_osx.cpp @@ -57,7 +57,11 @@ void wxStaticText::SetLabel(const wxString& label) m_labelOrig = label; // middle/end ellipsization is handled by the OS: - if ( HasFlag(wxST_ELLIPSIZE_END) || HasFlag(wxST_ELLIPSIZE_MIDDLE) ) + if ( HasFlag(wxST_ELLIPSIZE_END) || HasFlag(wxST_ELLIPSIZE_MIDDLE) +#if wxOSX_USE_COCOA // Cocoa has all three modes + || HasFlag(wxST_ELLIPSIZE_START) +#endif + ) { // remove markup wxString str(label); -- 2.45.2