X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/489468feaa08b8f504735eecca522fb8d0f825d2..689d801314845c6db093358a81c7879b672aa5e6:/src/osx/carbon/font.cpp diff --git a/src/osx/carbon/font.cpp b/src/osx/carbon/font.cpp index d069f3c89e..d1c72dac07 100644 --- a/src/osx/carbon/font.cpp +++ b/src/osx/carbon/font.cpp @@ -1,5 +1,5 @@ ///////////////////////////////////////////////////////////////////////////// -// Name: src/mac/carbon/font.cpp +// Name: src/osx/carbon/font.cpp // Purpose: wxFont class // Author: Stefan Csomor // Modified by: @@ -25,10 +25,11 @@ #include "wx/graphics.h" #include "wx/settings.h" -#include "wx/mac/uma.h" +#include "wx/osx/private.h" -#ifndef __DARWIN__ -#include +#if wxOSX_USE_ATSU_TEXT && !wxOSX_USE_CARBON +// include themeing support +#include #endif #include @@ -42,7 +43,7 @@ class WXDLLEXPORT wxFontRefData: public wxGDIRefData public: wxFontRefData() { - Init(10, wxDEFAULT, wxNORMAL, wxNORMAL, + Init(10, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL, false, wxT("applicationfont"), wxFONTENCODING_DEFAULT); } @@ -53,9 +54,9 @@ public: } wxFontRefData(int size, - int family, - int style, - int weight, + wxFontFamily family, + wxFontStyle style, + wxFontWeight weight, bool underlined, const wxString& faceName, wxFontEncoding encoding) @@ -63,7 +64,7 @@ public: Init(size, family, style, weight, underlined, faceName, encoding); } -#if wxMAC_USE_CORE_TEXT +#if wxOSX_USE_CORE_TEXT wxFontRefData( wxUint32 coreTextFontType ); wxFontRefData( CTFontRef font ); wxFontRefData( CTFontDescriptorRef fontdescriptor, int size ); @@ -83,32 +84,32 @@ public: int GetPointSize() const { return m_pointSize; } - void SetFamily( int family ) + void SetFamily( wxFontFamily family ) { m_family = family; MacInvalidateNativeFont(); } - int GetFamily() const { return m_family; } + wxFontFamily GetFamily() const { return m_family; } - void SetStyle( int style ) + void SetStyle( wxFontStyle style ) { m_style = style; MacInvalidateNativeFont(); } - int GetStyle() const { return m_style; } + wxFontStyle GetStyle() const { return m_style; } - void SetWeight( int weight ) + void SetWeight( wxFontWeight weight ) { m_weight = weight; MacInvalidateNativeFont(); } - int GetWeight() const { return m_weight; } + wxFontWeight GetWeight() const { return m_weight; } void SetUnderlined( bool u ) { @@ -141,28 +142,28 @@ public: protected: // common part of all ctors void Init(int size, - int family, - int style, - int weight, + wxFontFamily family, + wxFontStyle style, + wxFontWeight weight, bool underlined, const wxString& faceName, wxFontEncoding encoding); -#if wxMAC_USE_CORE_TEXT +#if wxOSX_USE_CORE_TEXT void Init( CTFontRef font ); #endif // font characterstics int m_pointSize; - int m_family; - int m_style; - int m_weight; + 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 wxMAC_USE_ATSU_TEXT +#if wxOSX_USE_ATSU_TEXT FMFontFamily m_macFontFamily; FMFontSize m_macFontSize; FMFontStyle m_macFontStyle; @@ -180,11 +181,10 @@ public: // information here, as this speeds up and optimizes rendering ThemeFontID m_macThemeFontID ; #endif -#if wxMAC_USE_CORE_TEXT +#if wxOSX_USE_CORE_TEXT wxCFRef m_ctFont; - wxCFRef m_ctFontDescriptor; #endif -#if wxMAC_USE_CORE_TEXT || wxMAC_USE_ATSU_TEXT +#if wxOSX_USE_CORE_TEXT || wxOSX_USE_ATSU_TEXT ATSUStyle m_macATSUStyle ; #endif wxNativeFontInfo m_info; @@ -202,9 +202,9 @@ public: // ---------------------------------------------------------------------------- void wxFontRefData::Init(int pointSize, - int family, - int style, - int weight, + wxFontFamily family, + wxFontStyle style, + wxFontWeight weight, bool underlined, const wxString& faceName, wxFontEncoding encoding) @@ -218,7 +218,7 @@ void wxFontRefData::Init(int pointSize, m_faceName = faceName; m_encoding = encoding; m_noAA = false; -#if wxMAC_USE_ATSU_TEXT +#if wxOSX_USE_ATSU_TEXT m_macFontFamily = 0 ; m_macFontSize = 0; m_macFontStyle = 0; @@ -226,14 +226,14 @@ void wxFontRefData::Init(int pointSize, m_macATSUAdditionalQDStyles = 0 ; m_macThemeFontID = kThemeCurrentPortFont ; #endif -#if wxMAC_USE_CORE_TEXT || wxMAC_USE_ATSU_TEXT +#if wxOSX_USE_CORE_TEXT || wxOSX_USE_ATSU_TEXT m_macATSUStyle = NULL ; #endif } wxFontRefData::~wxFontRefData() { -#if wxMAC_USE_CORE_TEXT || wxMAC_USE_ATSU_TEXT +#if wxOSX_USE_CORE_TEXT || wxOSX_USE_ATSU_TEXT if ( m_macATSUStyle ) { ::ATSUDisposeStyle((ATSUStyle)m_macATSUStyle); @@ -244,11 +244,10 @@ wxFontRefData::~wxFontRefData() void wxFontRefData::MacInvalidateNativeFont() { -#if wxMAC_USE_CORE_TEXT +#if wxOSX_USE_CORE_TEXT m_ctFont.reset(); - m_ctFontDescriptor.reset(); #endif -#if wxMAC_USE_CORE_TEXT || wxMAC_USE_ATSU_TEXT +#if wxOSX_USE_CORE_TEXT || wxOSX_USE_ATSU_TEXT if ( m_macATSUStyle ) { ::ATSUDisposeStyle((ATSUStyle)m_macATSUStyle); @@ -257,7 +256,7 @@ void wxFontRefData::MacInvalidateNativeFont() #endif } -#if wxMAC_USE_CORE_TEXT +#if wxOSX_USE_CORE_TEXT /* from Core Text Manual Common Operations */ @@ -316,7 +315,7 @@ static CTFontDescriptorRef wxMacCreateCTFontDescriptor(CFStringRef iFamilyName, wxFontRefData::wxFontRefData( wxUint32 coreTextFontType ) { CTFontRef font = CTFontCreateUIFontForLanguage( coreTextFontType, 0.0, NULL ) ; - if ( CTFontGetSize(m_ctFont) == 0 ) + if ( CTFontGetSize(font) == 0 ) { CFRelease(font); font = CTFontCreateUIFontForLanguage( coreTextFontType, 12.0, NULL ); @@ -338,7 +337,7 @@ wxFontRefData::wxFontRefData( CTFontDescriptorRef fontdescriptor, int size ) float fsize; if ( CFNumberGetValue( value , kCFNumberFloatType , &fsize ) ) { - size = (int) fsize + 0.5 ; + size = (int)( fsize + 0.5 ); } } Init( CTFontCreateWithFontDescriptor(fontdescriptor, size,NULL) ); @@ -346,7 +345,7 @@ wxFontRefData::wxFontRefData( CTFontDescriptorRef fontdescriptor, int size ) void wxFontRefData::Init( CTFontRef font ) { - Init(10, wxDEFAULT, wxNORMAL, wxNORMAL, + Init(10, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL, false, wxT("applicationfont"), wxFONTENCODING_DEFAULT); m_ctFont.reset( font ); @@ -357,10 +356,10 @@ void wxFontRefData::Init( CTFontRef font ) void wxFontRefData::MacFindFont() { -#if wxMAC_USE_CORE_TEXT +#if wxOSX_USE_CORE_TEXT if ( UMAGetSystemVersion() >= 0x1050 ) { - if ( m_faceName.empty() && m_family == wxDEFAULT ) + if ( m_faceName.empty() && m_family == wxFONTFAMILY_DEFAULT ) { m_ctFont.reset(CTFontCreateUIFontForLanguage( kCTFontSystemFontType, 0.0, NULL )); } @@ -372,11 +371,9 @@ void wxFontRefData::MacFindFont() m_pointSize = CTFontGetSize(m_ctFont) ; CTFontSymbolicTraits traits = CTFontGetSymbolicTraits( m_ctFont ); if ( traits & kCTFontItalicTrait ) - m_style = wxITALIC; + m_style = wxFONTSTYLE_ITALIC; if ( traits & kCTFontBoldTrait ) - m_weight = wxBOLD ; - if ( !m_ctFontDescriptor.get() ) - m_ctFontDescriptor.reset( CTFontCopyFontDescriptor( m_ctFont ) ); + m_weight = wxFONTWEIGHT_BOLD ; } else { @@ -384,19 +381,23 @@ void wxFontRefData::MacFindFont() { switch ( m_family ) { - case wxSCRIPT : - case wxROMAN : - case wxDECORATIVE : + case wxFONTFAMILY_SCRIPT : + case wxFONTFAMILY_ROMAN : + case wxFONTFAMILY_DECORATIVE : m_faceName = wxT("Times"); break ; - case wxSWISS : - m_faceName = wxT("Lucida Grande"); + case wxFONTFAMILY_SWISS : + m_faceName = wxT("Helvetica"); break ; - case wxMODERN : - case wxTELETYPE: - m_faceName = wxT("Monaco"); + 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: @@ -405,51 +406,80 @@ void wxFontRefData::MacFindFont() } } - + CTFontSymbolicTraits traits = 0; - if (m_weight == wxBOLD) + if (m_weight == wxFONTWEIGHT_BOLD) traits |= kCTFontBoldTrait; - if (m_style == wxITALIC || m_style == wxSLANT) + if (m_style == wxFONTSTYLE_ITALIC || m_style == wxFONTSTYLE_SLANT) traits |= kCTFontItalicTrait; - -// use font descriptor caching -#if 1 - wxString lookupname = wxString::Format( "%s_%ld", m_faceName.c_str(), traits ); - - static std::map< std::wstring , wxCFRef< CTFontDescriptorRef > > fontdescriptorcache ; - - m_ctFontDescriptor = fontdescriptorcache[ std::wstring(lookupname.wc_str()) ]; - if ( !m_ctFontDescriptor ) - { - wxCFStringRef cf( m_faceName, wxLocale::GetSystemEncoding() ); - m_ctFontDescriptor.reset( wxMacCreateCTFontDescriptor( cf, traits ) ); - fontdescriptorcache[ std::wstring(lookupname.wc_str()) ] = m_ctFontDescriptor; - } -#else - wxCFStringRef cf( m_faceName, wxLocale::GetSystemEncoding() ); - m_ctFontDescriptor.reset( wxMacCreateCTFontDescriptor( cf, traits ) ); -#endif - -// use font caching -#if 1 + + // 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 ) { - m_ctFont.reset( CTFontCreateWithFontDescriptor( m_ctFontDescriptor, m_pointSize, NULL ) ); + // QD selection algorithm is the fastest by orders of magnitude on 10.5 + if ( m_faceName.IsAscii() ) + { + uint8 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; - } -#else - m_ctFont.reset( CTFontCreateWithFontDescriptor( m_ctFontDescriptor, m_pointSize, NULL ) ); +#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 wxMAC_USE_ATSU_TEXT == 0 +#if wxOSX_USE_ATSU_TEXT OSStatus status = noErr; - CTFontDescriptorRef desc = m_ctFontDescriptor ; - ATSFontRef atsfont = CTFontGetPlatformFont( m_ctFont, &desc ); + ATSFontRef atsfont = CTFontGetPlatformFont( m_ctFont, NULL ); FMFont fmfont = FMGetFontFromATSFontRef( atsfont ); ATSUAttributeTag atsuTags[] = { @@ -479,8 +509,8 @@ void wxFontRefData::MacFindFont() &fmfont , &atsuSize , &kHorizontal, - (m_weight == wxBOLD) ? &kTrue : &kFalse , - (m_style == wxITALIC || m_style == wxSLANT) ? &kTrue : &kFalse , + (m_weight == wxFONTWEIGHT_BOLD) ? &kTrue : &kFalse , + (m_style == wxFONTSTYLE_ITALIC || m_style == wxFONTSTYLE_SLANT) ? &kTrue : &kFalse , (m_underlined) ? &kTrue : &kFalse , }; @@ -498,7 +528,7 @@ void wxFontRefData::MacFindFont() #endif } #endif -#if wxMAC_USE_ATSU_TEXT +#if wxOSX_USE_ATSU_TEXT { OSStatus status = noErr; Str255 qdFontName ; @@ -511,11 +541,11 @@ void wxFontRefData::MacFindFont() m_macFontStyle = style ; m_faceName = wxMacMakeStringFromPascal( qdFontName ); if ( m_macFontStyle & bold ) - m_weight = wxBOLD ; + m_weight = wxFONTWEIGHT_BOLD ; else - m_weight = wxNORMAL ; + m_weight = wxFONTWEIGHT_NORMAL ; if ( m_macFontStyle & italic ) - m_style = wxITALIC ; + m_style = wxFONTSTYLE_ITALIC ; if ( m_macFontStyle & underline ) m_underlined = true ; m_pointSize = m_macFontSize ; @@ -525,7 +555,7 @@ void wxFontRefData::MacFindFont() { if ( m_faceName.empty() ) { - if ( m_family == wxDEFAULT ) + if ( m_family == wxFONTFAMILY_DEFAULT ) { m_macFontFamily = GetAppFont(); FMGetFontFamilyName(m_macFontFamily,qdFontName); @@ -535,19 +565,19 @@ void wxFontRefData::MacFindFont() { switch ( m_family ) { - case wxSCRIPT : - case wxROMAN : - case wxDECORATIVE : + case wxFONTFAMILY_SCRIPT : + case wxFONTFAMILY_ROMAN : + case wxFONTFAMILY_DECORATIVE : m_faceName = wxT("Times"); break ; - case wxSWISS : - m_faceName = wxT("Lucida Grande"); + case wxFONTFAMILY_SWISS : + m_faceName = wxT("Helvetica"); break ; - case wxMODERN : - case wxTELETYPE: - m_faceName = wxT("Monaco"); + case wxFONTFAMILY_MODERN : + case wxFONTFAMILY_TELETYPE: + m_faceName = wxT("Courier"); break ; default: @@ -584,9 +614,9 @@ void wxFontRefData::MacFindFont() } m_macFontStyle = 0; - if (m_weight == wxBOLD) + if (m_weight == wxFONTWEIGHT_BOLD) m_macFontStyle |= bold; - if (m_style == wxITALIC || m_style == wxSLANT) + if (m_style == wxFONTSTYLE_ITALIC || m_style == wxFONTSTYLE_SLANT) m_macFontStyle |= italic; if (m_underlined) m_macFontStyle |= underline; @@ -681,12 +711,12 @@ wxFont::wxFont(const wxString& fontdesc) } bool wxFont::Create(int pointSize, - int family, - int style, - int weight, - bool underlined, - const wxString& faceName, - wxFontEncoding encoding) + wxFontFamily family, + wxFontStyle style, + wxFontWeight weight, + bool underlined, + const wxString& faceName, + wxFontEncoding encoding) { UnRef(); @@ -699,7 +729,7 @@ bool wxFont::Create(int pointSize, return true; } -#if wxMAC_USE_CORE_TEXT +#if wxOSX_USE_CORE_TEXT bool wxFont::MacCreateFromUIFont(wxUint32 ctFontType ) { @@ -724,29 +754,29 @@ bool wxFont::MacCreateFromCTFontDescriptor( const void * ctFontDescriptor , int #endif +#if wxOSX_USE_ATSU_TEXT bool wxFont::MacCreateFromThemeFont(wxUint16 themeFontID) { -#if wxMAC_USE_CORE_TEXT +#if wxOSX_USE_CORE_TEXT if ( UMAGetSystemVersion() >= 0x1050) { return MacCreateFromUIFont(HIThemeGetUIFontType(themeFontID)); } #endif -#if wxMAC_USE_ATSU_TEXT { UnRef(); m_refData = new wxFontRefData( - 12, wxDEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL, + 12, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL, false, wxEmptyString, wxFONTENCODING_DEFAULT ); M_FONTDATA->m_macThemeFontID = themeFontID ; RealizeResource(); return true; } -#endif return false; } +#endif wxFont::~wxFont() { @@ -790,7 +820,7 @@ wxGDIRefData *wxFont::CreateGDIRefData() const wxGDIRefData *wxFont::CloneGDIRefData(const wxGDIRefData *data) const { - return new wxFontRefData(*wx_static_cast(const wxFontRefData *, data)); + return new wxFontRefData(*static_cast(data)); } void wxFont::SetPointSize(int pointSize) @@ -805,7 +835,7 @@ void wxFont::SetPointSize(int pointSize) RealizeResource(); } -void wxFont::SetFamily(int family) +void wxFont::SetFamily(wxFontFamily family) { Unshare(); @@ -814,7 +844,7 @@ void wxFont::SetFamily(int family) RealizeResource(); } -void wxFont::SetStyle(int style) +void wxFont::SetStyle(wxFontStyle style) { Unshare(); @@ -823,7 +853,7 @@ void wxFont::SetStyle(int style) RealizeResource(); } -void wxFont::SetWeight(int weight) +void wxFont::SetWeight(wxFontWeight weight) { Unshare(); @@ -889,23 +919,23 @@ wxSize wxFont::GetPixelSize() const #endif } -int wxFont::GetFamily() const +wxFontFamily wxFont::GetFamily() const { - wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") ); + wxCHECK_MSG( M_FONTDATA != NULL , wxFONTFAMILY_MAX, wxT("invalid font") ); return M_FONTDATA->GetFamily(); } -int wxFont::GetStyle() const +wxFontStyle wxFont::GetStyle() const { - wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") ); + wxCHECK_MSG( M_FONTDATA != NULL , wxFONTSTYLE_MAX, wxT("invalid font") ); return M_FONTDATA->GetStyle() ; } -int wxFont::GetWeight() const +wxFontWeight wxFont::GetWeight() const { - wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") ); + wxCHECK_MSG( M_FONTDATA != NULL , wxFONTWEIGHT_MAX, wxT("invalid font") ); return M_FONTDATA->GetWeight(); } @@ -938,7 +968,7 @@ bool wxFont::GetNoAntiAliasing() const return M_FONTDATA->GetNoAntiAliasing(); } -#if wxMAC_USE_ATSU_TEXT +#if wxOSX_USE_ATSU_TEXT short wxFont::MacGetFontNum() const { @@ -983,7 +1013,7 @@ wxUint16 wxFont::MacGetThemeFontID() const } #endif -#if wxMAC_USE_CORE_TEXT || wxMAC_USE_ATSU_TEXT +#if wxOSX_USE_CORE_TEXT || wxOSX_USE_ATSU_TEXT void * wxFont::MacGetATSUStyle() const { wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") ); @@ -992,7 +1022,7 @@ void * wxFont::MacGetATSUStyle() const } #endif -#if wxMAC_USE_CORE_TEXT +#if wxOSX_USE_CORE_TEXT const void * wxFont::MacGetCTFont() const { @@ -1001,13 +1031,6 @@ const void * wxFont::MacGetCTFont() const return (CTFontRef)(M_FONTDATA->m_ctFont); } -const void * wxFont::MacGetCTFontDescriptor() const -{ - wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") ); - - return (CTFontDescriptorRef)(M_FONTDATA->m_ctFontDescriptor); -} - #endif const wxNativeFontInfo * wxFont::GetNativeFontInfo() const