#include <ATSUnicode.h>
#endif
+#include <map>
+#include <string>
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;
// 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<CTFontRef> m_ctFont;
wxCFRef<CTFontDescriptorRef> m_ctFontDescriptor;
+#endif
+#if wxMAC_USE_CORE_TEXT || wxMAC_USE_ATSU_TEXT
+ ATSUStyle m_macATSUStyle ;
#endif
wxNativeFontInfo m_info;
};
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);
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);
#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.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 );
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
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 = GetAppFont();
else
{
- wxMacCFStringHolder cf( m_faceName, wxLocale::GetSystemEncoding() );
+ wxCFStringRef cf( m_faceName, wxLocale::GetSystemEncoding() );
ATSFontFamilyRef atsfamily = ATSFontFamilyFindFromName( cf , kATSOptionFlagsDefault );
if ( atsfamily == (ATSFontFamilyRef) -1 )
{
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 );
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") );
}
#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
const void * wxFont::MacGetCTFontDescriptor() const
{
wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
-
+
return (CTFontDescriptorRef)(M_FONTDATA->m_ctFontDescriptor);
}