#include <ATSUnicode.h>
#endif
+#include <map>
IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
{
Init(size, family, style, weight, underlined, faceName, encoding);
}
-
+
#if wxMAC_USE_CORE_TEXT
wxFontRefData( wxUint32 coreTextFontType );
wxFontRefData( CTFontRef font );
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,
#if wxMAC_USE_CORE_TEXT
void Init( CTFontRef font );
-#endif
+#endif
// font characterstics
int m_pointSize;
int m_family;
#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;
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 and input size.
+ // Create the font descriptor with our attributes
descriptor = CTFontDescriptorCreateWithAttributes(attributes);
check(descriptor != NULL);
-
+
CFRelease(attributes);
}
// Return our font descriptor.
if ( size == 0 )
{
wxCFRef< CFNumberRef > value( (CFNumberRef) CTFontDescriptorCopyAttribute( fontdescriptor, kCTFontSizeAttribute ) );
-
+
float fsize;
if ( CFNumberGetValue( value , kCFNumberFloatType , &fsize ) )
{
{
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) ;
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 ;
}
}
- wxCFStringRef 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;
};
Boolean kTrue = true ;
Boolean kFalse = false ;
-
+
Fixed atsuSize = IntToFixed( m_pointSize );
ATSUVerticalCharacterType kHorizontal = kATSUStronglyHorizontal;
ATSUAttributeValuePtr atsuValues[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
(m_style == wxITALIC || m_style == wxSLANT) ? &kTrue : &kFalse ,
(m_underlined) ? &kTrue : &kFalse ,
};
-
+
if ( m_macATSUStyle )
{
::ATSUDisposeStyle((ATSUStyle)m_macATSUStyle);
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 ;
m_macFontFamily = FMGetFontFamilyFromATSFontFamilyRef( atsfamily );
}
}
-
+
m_macFontStyle = 0;
if (m_weight == wxBOLD)
m_macFontStyle |= bold;
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 ,
sizeof( Boolean ) ,
sizeof( Boolean ) ,
};
-
+
Boolean kTrue = true ;
Boolean kFalse = false ;
-
+
Fixed atsuSize = IntToFixed( m_macFontSize );
ATSUVerticalCharacterType kHorizontal = kATSUStronglyHorizontal;
ATSUAttributeValuePtr atsuValues[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
(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;
}
bool wxFont::MacCreateFromCTFontDescriptor( const void * ctFontDescriptor , int size )
{
UnRef();
-
+
m_refData = new wxFontRefData((CTFontDescriptorRef)ctFontDescriptor, size);;
RealizeResource();
-
+
return true;
}
}
}
+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 );
void * wxFont::MacGetATSUStyle() const
{
wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
-
+
return M_FONTDATA->m_macATSUStyle;
}
#endif
const void * wxFont::MacGetCTFontDescriptor() const
{
wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
-
+
return (CTFontDescriptorRef)(M_FONTDATA->m_ctFontDescriptor);
}