X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/8b534a7bb0dbf082a1dd4888bc9acfb24ac1e3cc..6cef0db28018fd2644ee4e38af715872e5242459:/src/mac/carbon/font.cpp?ds=sidebyside diff --git a/src/mac/carbon/font.cpp b/src/mac/carbon/font.cpp index 0129fec637..e5ebd6dc39 100644 --- a/src/mac/carbon/font.cpp +++ b/src/mac/carbon/font.cpp @@ -30,6 +30,8 @@ #include #endif +#include +#include IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject) @@ -59,7 +61,7 @@ public: { Init(size, family, style, weight, underlined, faceName, encoding); } - + #if wxMAC_USE_CORE_TEXT wxFontRefData( wxUint32 coreTextFontType ); wxFontRefData( CTFontRef font ); @@ -72,69 +74,69 @@ public: bool GetNoAntiAliasing() const { return m_noAA; } - void SetPointSize( int size ) + void SetPointSize( int size ) { m_pointSize = size; MacInvalidateNativeFont(); } - + int GetPointSize() const { return m_pointSize; } - - void SetFamily( int family ) + + void SetFamily( int family ) { m_family = family; MacInvalidateNativeFont(); } - - + + int GetFamily() const { return m_family; } - - void SetStyle( int style ) + + void SetStyle( int style ) { m_style = style; MacInvalidateNativeFont(); } - - + + int GetStyle() const { return m_style; } - - void SetWeight( int weight ) + + void SetWeight( int weight ) { m_weight = weight; MacInvalidateNativeFont(); } - - + + int GetWeight() const { return m_weight; } - - void SetUnderlined( bool u ) + + void SetUnderlined( bool u ) { m_underlined = u; MacInvalidateNativeFont(); } - + bool GetUnderlined() const { return m_underlined; } - - void SetFaceName( const wxString& facename ) + + void SetFaceName( const wxString& facename ) { m_faceName = facename; MacInvalidateNativeFont(); } - + const wxString& GetFaceName() const { return m_faceName; } - - void SetEncoding( wxFontEncoding encoding ) + + void SetEncoding( wxFontEncoding encoding ) { m_encoding = encoding; MacInvalidateNativeFont(); - } - - wxFontEncoding GetEncoding() const { return m_encoding; } - + } + + wxFontEncoding GetEncoding() const { return m_encoding; } + void MacInvalidateNativeFont(); - + void MacFindFont(); - + protected: // common part of all ctors void Init(int size, @@ -147,7 +149,7 @@ protected: #if wxMAC_USE_CORE_TEXT void Init( CTFontRef font ); -#endif +#endif // font characterstics int m_pointSize; int m_family; @@ -176,11 +178,13 @@ public: // for true themeing support we must store the correct font // information here, as this speeds up and optimizes rendering ThemeFontID m_macThemeFontID ; - ATSUStyle m_macATSUStyle ; #endif #if wxMAC_USE_CORE_TEXT wxCFRef m_ctFont; wxCFRef m_ctFontDescriptor; +#endif +#if wxMAC_USE_CORE_TEXT || wxMAC_USE_ATSU_TEXT + ATSUStyle m_macATSUStyle ; #endif wxNativeFontInfo m_info; }; @@ -220,13 +224,15 @@ void wxFontRefData::Init(int pointSize, m_macATSUFontID = 0; m_macATSUAdditionalQDStyles = 0 ; m_macThemeFontID = kThemeCurrentPortFont ; +#endif +#if wxMAC_USE_CORE_TEXT || wxMAC_USE_ATSU_TEXT m_macATSUStyle = NULL ; #endif } wxFontRefData::~wxFontRefData() { -#if wxMAC_USE_ATSU_TEXT +#if wxMAC_USE_CORE_TEXT || wxMAC_USE_ATSU_TEXT if ( m_macATSUStyle ) { ::ATSUDisposeStyle((ATSUStyle)m_macATSUStyle); @@ -241,7 +247,7 @@ void wxFontRefData::MacInvalidateNativeFont() m_ctFont.reset(); m_ctFontDescriptor.reset(); #endif -#if wxMAC_USE_ATSU_TEXT +#if wxMAC_USE_CORE_TEXT || wxMAC_USE_ATSU_TEXT if ( m_macATSUStyle ) { ::ATSUDisposeStyle((ATSUStyle)m_macATSUStyle); @@ -252,24 +258,24 @@ void wxFontRefData::MacInvalidateNativeFont() #if wxMAC_USE_CORE_TEXT -/* from Core Text Manual Common Operations */ +/* 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) { + + if (attributes != NULL) { // Add a family name to our attributes. CFDictionaryAddValue(attributes, kCTFontFamilyNameAttribute, iFamilyName); - - + + if ( iTraits ) { CFMutableDictionaryRef traits; CFNumberRef symTraits; @@ -278,17 +284,17 @@ static CTFontDescriptorRef wxMacCreateCTFontDescriptor(CFStringRef iFamilyName, 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); @@ -296,10 +302,10 @@ static CTFontDescriptorRef wxMacCreateCTFontDescriptor(CFStringRef iFamilyName, CFRelease(symTraits); } } - // Create the font descriptor with our attributes and input size. + // Create the font descriptor with our attributes descriptor = CTFontDescriptorCreateWithAttributes(attributes); check(descriptor != NULL); - + CFRelease(attributes); } // Return our font descriptor. @@ -327,7 +333,7 @@ wxFontRefData::wxFontRefData( CTFontDescriptorRef fontdescriptor, int size ) if ( size == 0 ) { wxCFRef< CFNumberRef > value( (CFNumberRef) CTFontDescriptorCopyAttribute( fontdescriptor, kCTFontSizeAttribute ) ); - + float fsize; if ( CFNumberGetValue( value , kCFNumberFloatType , &fsize ) ) { @@ -357,10 +363,10 @@ void wxFontRefData::MacFindFont() { m_ctFont.reset(CTFontCreateUIFontForLanguage( kCTFontSystemFontType, 0.0, NULL )); } - - if ( m_ctFont.get() ) - { - wxMacCFStringHolder name( CTFontCopyFamilyName( m_ctFont ) ); + + if ( m_ctFont ) + { + wxCFStringRef name( CTFontCopyFamilyName( m_ctFont ) ); m_faceName = name.AsString(); m_pointSize = CTFontGetSize(m_ctFont) ; CTFontSymbolicTraits traits = CTFontGetSymbolicTraits( m_ctFont ); @@ -382,34 +388,113 @@ void wxFontRefData::MacFindFont() case wxDECORATIVE : m_faceName = wxT("Times"); break ; - + case wxSWISS : m_faceName = wxT("Lucida Grande"); break ; - + case wxMODERN : case wxTELETYPE: m_faceName = wxT("Monaco"); break ; - + default: m_faceName = wxT("Times"); break ; } } - wxMacCFStringHolder cf( m_faceName, wxLocale::GetSystemEncoding() ); + CTFontSymbolicTraits traits = 0; - + if (m_weight == wxBOLD) traits |= kCTFontBoldTrait; if (m_style == wxITALIC || m_style == wxSLANT) 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 + 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 ) ); + fontcache[ std::wstring(lookupnameWithSize.wc_str()) ] = m_ctFont; + } +#else m_ctFont.reset( CTFontCreateWithFontDescriptor( m_ctFontDescriptor, m_pointSize, NULL ) ); +#endif + } +#if wxMAC_USE_ATSU_TEXT == 0 + OSStatus status = noErr; + CTFontDescriptorRef desc = m_ctFontDescriptor ; + ATSFontRef atsfont = CTFontGetPlatformFont( m_ctFont, &desc ); + 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 ; + + Fixed atsuSize = IntToFixed( m_pointSize ); + ATSUVerticalCharacterType kHorizontal = kATSUStronglyHorizontal; + ATSUAttributeValuePtr atsuValues[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] = + { + &fmfont , + &atsuSize , + &kHorizontal, + (m_weight == wxBOLD) ? &kTrue : &kFalse , + (m_style == wxITALIC || m_style == wxSLANT) ? &kTrue : &kFalse , + (m_underlined) ? &kTrue : &kFalse , + }; + + if ( m_macATSUStyle ) + { + ::ATSUDisposeStyle((ATSUStyle)m_macATSUStyle); + m_macATSUStyle = 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 wxMAC_USE_ATSU_TEXT @@ -454,16 +539,16 @@ void wxFontRefData::MacFindFont() case wxDECORATIVE : m_faceName = wxT("Times"); break ; - + case wxSWISS : m_faceName = wxT("Lucida Grande"); break ; - + case wxMODERN : case wxTELETYPE: m_faceName = wxT("Monaco"); break ; - + default: m_faceName = wxT("Times"); break ; @@ -485,7 +570,7 @@ void wxFontRefData::MacFindFont() m_macFontFamily = GetAppFont(); else { - wxMacCFStringHolder cf( m_faceName, wxLocale::GetSystemEncoding() ); + wxCFStringRef cf( m_faceName, wxLocale::GetSystemEncoding() ); ATSFontFamilyRef atsfamily = ATSFontFamilyFindFromName( cf , kATSOptionFlagsDefault ); if ( atsfamily == (ATSFontFamilyRef) -1 ) { @@ -496,7 +581,7 @@ void wxFontRefData::MacFindFont() m_macFontFamily = FMGetFontFamilyFromATSFontFamilyRef( atsfamily ); } } - + m_macFontStyle = 0; if (m_weight == wxBOLD) m_macFontStyle |= bold; @@ -506,25 +591,25 @@ void wxFontRefData::MacFindFont() 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); wxASSERT_MSG( status == noErr , wxT("couldn't create ATSU style") ); - + ATSUAttributeTag atsuTags[] = { kATSUFontTag , @@ -547,10 +632,10 @@ void wxFontRefData::MacFindFont() sizeof( Boolean ) , sizeof( Boolean ) , }; - + Boolean kTrue = true ; Boolean kFalse = false ; - + Fixed atsuSize = IntToFixed( m_macFontSize ); ATSUVerticalCharacterType kHorizontal = kATSUStronglyHorizontal; ATSUAttributeValuePtr atsuValues[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] = @@ -564,12 +649,12 @@ void wxFontRefData::MacFindFont() (m_macATSUAdditionalQDStyles & condense) ? &kTrue : &kFalse , (m_macATSUAdditionalQDStyles & extend) ? &kTrue : &kFalse , }; - + status = ::ATSUSetAttributes( (ATSUStyle)m_macATSUStyle, sizeof(atsuTags) / sizeof(ATSUAttributeTag) , atsuTags, atsuSizes, atsuValues); - + wxASSERT_MSG( status == noErr , wxT("couldn't modify ATSU style") ); return; } @@ -628,10 +713,10 @@ bool wxFont::MacCreateFromUIFont(wxUint32 ctFontType ) bool wxFont::MacCreateFromCTFontDescriptor( const void * ctFontDescriptor , int size ) { UnRef(); - + m_refData = new wxFontRefData((CTFontDescriptorRef)ctFontDescriptor, size);; RealizeResource(); - + return true; } @@ -697,11 +782,21 @@ void wxFont::Unshare() } } +wxGDIRefData *wxFont::CreateGDIRefData() const +{ + return new wxFontRefData; +} + +wxGDIRefData *wxFont::CloneGDIRefData(const wxGDIRefData *data) const +{ + return new wxFontRefData(*wx_static_cast(const wxFontRefData *, data)); +} + void wxFont::SetPointSize(int pointSize) { if ( M_FONTDATA->GetPointSize() == pointSize ) return; - + Unshare(); M_FONTDATA->SetPointSize( pointSize ); @@ -872,13 +967,6 @@ wxUint32 wxFont::MacGetATSUFontID() const return M_FONTDATA->m_macATSUFontID; } -void * wxFont::MacGetATSUStyle() const -{ - wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") ); - - return M_FONTDATA->m_macATSUStyle; -} - wxUint32 wxFont::MacGetATSUAdditionalQDStyles() const { wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") ); @@ -894,6 +982,15 @@ wxUint16 wxFont::MacGetThemeFontID() const } #endif +#if wxMAC_USE_CORE_TEXT || wxMAC_USE_ATSU_TEXT +void * wxFont::MacGetATSUStyle() const +{ + wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") ); + + return M_FONTDATA->m_macATSUStyle; +} +#endif + #if wxMAC_USE_CORE_TEXT const void * wxFont::MacGetCTFont() const @@ -906,7 +1003,7 @@ const void * wxFont::MacGetCTFont() const const void * wxFont::MacGetCTFontDescriptor() const { wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") ); - + return (CTFontDescriptorRef)(M_FONTDATA->m_ctFontDescriptor); }