trying to simplify and optimize font handling
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@63918 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -154,9 +154,6 @@ public:
|
||||
void EnsureValid();
|
||||
|
||||
bool m_descriptorValid;
|
||||
#if wxOSX_USE_CORE_TEXT
|
||||
CTFontDescriptorRef m_ctFontDescriptor;
|
||||
#endif
|
||||
|
||||
#if wxOSX_USE_ATSU_TEXT
|
||||
bool m_atsuFontValid;
|
||||
@@ -168,13 +165,6 @@ public:
|
||||
wxInt16 m_qdFontFamily;
|
||||
wxInt16 m_qdFontStyle;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if wxOSX_USE_COCOA
|
||||
WX_NSFontDescriptor m_nsFontDescriptor;
|
||||
void OSXValidateNSFontDescriptor();
|
||||
#endif
|
||||
#if wxOSX_USE_IPHONE
|
||||
#endif
|
||||
|
||||
int m_pointSize;
|
||||
|
@@ -161,6 +161,8 @@ public:
|
||||
wxCFRef<CTFontRef> m_ctFont;
|
||||
#endif
|
||||
#if wxOSX_USE_ATSU_TEXT
|
||||
void CreateATSUFont();
|
||||
|
||||
ATSUStyle m_macATSUStyle ;
|
||||
#endif
|
||||
wxCFRef<CGFontRef> m_cgFont;
|
||||
@@ -380,42 +382,17 @@ wxFontRefData::wxFontRefData(wxOSXSystemFont font, int size)
|
||||
#if wxOSX_USE_IPHONE
|
||||
m_uiFont = wxFont::OSXCreateUIFont( font, &m_info );
|
||||
#endif
|
||||
m_info.EnsureValid();
|
||||
#if wxOSX_USE_ATSU_TEXT
|
||||
CreateATSUFont();
|
||||
#endif
|
||||
|
||||
m_fontValid = true;
|
||||
}
|
||||
|
||||
void wxFontRefData::MacFindFont()
|
||||
{
|
||||
if ( m_fontValid )
|
||||
return;
|
||||
|
||||
wxCHECK_RET( m_info.m_pointSize > 0, wxT("Point size should not be zero.") );
|
||||
|
||||
m_info.EnsureValid();
|
||||
|
||||
#if wxOSX_USE_CORE_TEXT
|
||||
if ( UMAGetSystemVersion() >= 0x1050 )
|
||||
{
|
||||
CTFontSymbolicTraits traits = 0;
|
||||
|
||||
if (m_info.m_weight == wxFONTWEIGHT_BOLD)
|
||||
traits |= kCTFontBoldTrait;
|
||||
if (m_info.m_style == wxFONTSTYLE_ITALIC || m_info.m_style == wxFONTSTYLE_SLANT)
|
||||
traits |= kCTFontItalicTrait;
|
||||
|
||||
// use font caching
|
||||
wxString lookupnameWithSize = wxString::Format( "%s_%ld_%ld", m_info.m_faceName.c_str(), traits, m_info.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_info.m_ctFontDescriptor, 0/*m_pointSize */, NULL ) );
|
||||
}
|
||||
m_cgFont.reset(CTFontCopyGraphicsFont(m_ctFont, NULL));
|
||||
}
|
||||
|
||||
#endif
|
||||
#if wxOSX_USE_ATSU_TEXT
|
||||
{
|
||||
void wxFontRefData::CreateATSUFont()
|
||||
{
|
||||
// we try to get as much styles as possible into ATSU
|
||||
|
||||
OSStatus status = ::ATSUCreateStyle(&m_macATSUStyle);
|
||||
@@ -474,7 +451,48 @@ void wxFontRefData::MacFindFont()
|
||||
ATSFontRef fontRef = FMGetATSFontRefFromFont(m_info.m_atsuFontID);
|
||||
m_cgFont.reset( CGFontCreateWithPlatformFont( &fontRef ) );
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
void wxFontRefData::MacFindFont()
|
||||
{
|
||||
if ( m_fontValid )
|
||||
return;
|
||||
|
||||
wxCHECK_RET( m_info.m_pointSize > 0, wxT("Point size should not be zero.") );
|
||||
|
||||
m_info.EnsureValid();
|
||||
|
||||
#if wxOSX_USE_CORE_TEXT
|
||||
if ( UMAGetSystemVersion() >= 0x1050 )
|
||||
{
|
||||
CTFontSymbolicTraits traits = 0;
|
||||
|
||||
if (m_info.m_weight == wxFONTWEIGHT_BOLD)
|
||||
traits |= kCTFontBoldTrait;
|
||||
if (m_info.m_style == wxFONTSTYLE_ITALIC || m_info.m_style == wxFONTSTYLE_SLANT)
|
||||
traits |= kCTFontItalicTrait;
|
||||
|
||||
// use font caching
|
||||
wxString lookupnameWithSize = wxString::Format( "%s_%ld_%ld", m_info.m_faceName.c_str(), traits, m_info.m_pointSize );
|
||||
|
||||
static std::map< std::wstring , wxCFRef< CTFontRef > > fontcache ;
|
||||
m_ctFont = fontcache[ std::wstring(lookupnameWithSize.wc_str()) ];
|
||||
if ( !m_ctFont )
|
||||
{
|
||||
m_ctFont.reset(CTFontCreateWithName( wxCFStringRef(m_info.m_faceName), m_info.m_pointSize , NULL ));
|
||||
if ( traits != 0 )
|
||||
{
|
||||
m_ctFont.reset(CTFontCreateCopyWithSymbolicTraits( m_ctFont, 0, NULL, traits, traits ));
|
||||
}
|
||||
}
|
||||
|
||||
m_cgFont.reset(CTFontCopyGraphicsFont(m_ctFont, NULL));
|
||||
}
|
||||
|
||||
#endif
|
||||
#if wxOSX_USE_ATSU_TEXT
|
||||
CreateATSUFont();
|
||||
#endif
|
||||
#if wxOSX_USE_COCOA
|
||||
m_nsFont = wxFont::OSXCreateNSFont( &m_info );
|
||||
@@ -901,9 +919,6 @@ static CTFontDescriptorRef wxMacCreateCTFontDescriptor(CFStringRef iFamilyName,
|
||||
|
||||
void wxNativeFontInfo::Init()
|
||||
{
|
||||
#if wxOSX_USE_CORE_TEXT
|
||||
m_ctFontDescriptor = NULL;
|
||||
#endif
|
||||
#if wxOSX_USE_ATSU_TEXT
|
||||
m_atsuFontID = 0 ;
|
||||
m_atsuAdditionalQDStyles = 0;
|
||||
@@ -912,9 +927,6 @@ void wxNativeFontInfo::Init()
|
||||
m_qdFontStyle = 0;
|
||||
m_qdFontFamily = 0;
|
||||
#endif
|
||||
#endif
|
||||
#if wxOSX_USE_COCOA
|
||||
m_nsFontDescriptor = NULL;
|
||||
#endif
|
||||
m_pointSize = 0;
|
||||
m_family = wxFONTFAMILY_DEFAULT;
|
||||
@@ -930,14 +942,13 @@ void wxNativeFontInfo::Init()
|
||||
void wxNativeFontInfo::Init(CTFontDescriptorRef descr)
|
||||
{
|
||||
Init();
|
||||
m_ctFontDescriptor = wxCFRetain(descr);
|
||||
|
||||
wxCFRef< CFNumberRef > sizevalue( (CFNumberRef) CTFontDescriptorCopyAttribute( m_ctFontDescriptor, kCTFontSizeAttribute ) );
|
||||
wxCFRef< CFNumberRef > sizevalue( (CFNumberRef) CTFontDescriptorCopyAttribute( descr, kCTFontSizeAttribute ) );
|
||||
float fsize;
|
||||
if ( CFNumberGetValue( sizevalue , kCFNumberFloatType , &fsize ) )
|
||||
m_pointSize = (int)( fsize + 0.5 );
|
||||
|
||||
wxCFRef< CFDictionaryRef > traitsvalue( (CFDictionaryRef) CTFontDescriptorCopyAttribute( m_ctFontDescriptor, kCTFontTraitsAttribute ) );
|
||||
wxCFRef< CFDictionaryRef > traitsvalue( (CFDictionaryRef) CTFontDescriptorCopyAttribute( descr, kCTFontTraitsAttribute ) );
|
||||
CTFontSymbolicTraits traits;
|
||||
if ( CFNumberGetValue((CFNumberRef) CFDictionaryGetValue(traitsvalue,kCTFontSymbolicTrait),kCFNumberIntType,&traits) )
|
||||
{
|
||||
@@ -947,7 +958,7 @@ void wxNativeFontInfo::Init(CTFontDescriptorRef descr)
|
||||
m_weight = wxFONTWEIGHT_BOLD ;
|
||||
}
|
||||
|
||||
wxCFStringRef familyName( (CFStringRef) CTFontDescriptorCopyAttribute(m_ctFontDescriptor, kCTFontFamilyNameAttribute));
|
||||
wxCFStringRef familyName( (CFStringRef) CTFontDescriptorCopyAttribute(descr, kCTFontFamilyNameAttribute));
|
||||
m_faceName = familyName.AsString();
|
||||
}
|
||||
#endif
|
||||
@@ -957,46 +968,6 @@ 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 )
|
||||
{
|
||||
@@ -1038,13 +1009,6 @@ void wxNativeFontInfo::EnsureValid()
|
||||
m_atsuAdditionalQDStyles = m_qdFontStyle & (~intrinsicStyle );
|
||||
m_atsuFontValid = true;
|
||||
}
|
||||
#endif
|
||||
#if wxOSX_USE_COCOA
|
||||
if ( m_nsFontDescriptor == NULL )
|
||||
OSXValidateNSFontDescriptor();
|
||||
#endif
|
||||
#if wxOSX_USE_IPHONE
|
||||
// TODO
|
||||
#endif
|
||||
m_descriptorValid = true;
|
||||
}
|
||||
@@ -1052,9 +1016,6 @@ void wxNativeFontInfo::EnsureValid()
|
||||
void wxNativeFontInfo::Init(const wxNativeFontInfo& info)
|
||||
{
|
||||
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 ;
|
||||
@@ -1063,9 +1024,6 @@ void wxNativeFontInfo::Init(const wxNativeFontInfo& info)
|
||||
m_qdFontFamily = info.m_qdFontFamily;
|
||||
m_qdFontStyle = info.m_qdFontStyle;
|
||||
#endif
|
||||
#endif
|
||||
#if wxOSX_USE_COCOA
|
||||
m_nsFontDescriptor = (NSFontDescriptor*) wxMacCocoaRetain(info.m_nsFontDescriptor);
|
||||
#endif
|
||||
m_pointSize = info.m_pointSize;
|
||||
m_family = info.m_family;
|
||||
@@ -1100,18 +1058,10 @@ void wxNativeFontInfo::Init(int size,
|
||||
|
||||
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
|
||||
m_descriptorValid = false;
|
||||
}
|
||||
|
@@ -208,20 +208,7 @@ int RunMixedFontDialog(wxFontDialog* dialog)
|
||||
#if wxOSX_USE_COCOA
|
||||
NSFont* theFont = [fontPanel panelConvertFont:[NSFont userFontOfSize:0]];
|
||||
|
||||
//Get more information about the user's chosen font
|
||||
NSFontTraitMask theTraits = [[NSFontManager sharedFontManager] traitsOfFont:theFont];
|
||||
int theFontWeight = [[NSFontManager sharedFontManager] weightOfFont:theFont];
|
||||
int theFontSize = (int) [theFont pointSize];
|
||||
|
||||
wxFontFamily fontFamily = wxFONTFAMILY_DEFAULT;
|
||||
//Set the wx font to the appropriate data
|
||||
if(theTraits & NSFixedPitchFontMask)
|
||||
fontFamily = wxFONTFAMILY_TELETYPE;
|
||||
|
||||
fontdata.m_chosenFont = wxFont( theFontSize, fontFamily,
|
||||
theTraits & NSItalicFontMask ? wxFONTSTYLE_ITALIC : 0,
|
||||
theFontWeight < 5 ? wxLIGHT : theFontWeight >= 9 ? wxBOLD : wxNORMAL,
|
||||
false, wxStringWithNSString([theFont familyName]) );
|
||||
fontdata.m_chosenFont = wxFont( theFont );
|
||||
|
||||
//Get the shared color panel along with the chosen color and set the chosen color
|
||||
NSColor* theColor = [[[NSColorPanel sharedColorPanel] color] colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
|
||||
|
@@ -23,6 +23,10 @@
|
||||
|
||||
#include "wx/fontutil.h"
|
||||
|
||||
#if wxOSX_USE_COCOA
|
||||
#include "wx/cocoa/string.h"
|
||||
#endif
|
||||
|
||||
#ifdef __WXMAC__
|
||||
|
||||
#if wxOSX_USE_CARBON
|
||||
@@ -110,31 +114,39 @@ wxFont::wxFont(WX_NSFont nsfont)
|
||||
Create(info);
|
||||
}
|
||||
|
||||
void wxFont::SetNativeInfoFromNSFont(WX_NSFont nsfont, wxNativeFontInfo* info)
|
||||
void wxFont::SetNativeInfoFromNSFont(WX_NSFont theFont, wxNativeFontInfo* info)
|
||||
{
|
||||
NSFontDescriptor*desc = [[nsfont fontDescriptor] retain];
|
||||
if ( info->m_faceName.empty())
|
||||
{
|
||||
//Get more information about the user's chosen font
|
||||
NSFontTraitMask theTraits = [[NSFontManager sharedFontManager] traitsOfFont:theFont];
|
||||
int theFontWeight = [[NSFontManager sharedFontManager] weightOfFont:theFont];
|
||||
|
||||
wxFontFamily fontFamily = wxFONTFAMILY_DEFAULT;
|
||||
//Set the wx font to the appropriate data
|
||||
if(theTraits & NSFixedPitchFontMask)
|
||||
fontFamily = wxFONTFAMILY_TELETYPE;
|
||||
|
||||
wxFontStyle fontstyle = wxFONTSTYLE_NORMAL;
|
||||
wxFontWeight fontweight = wxFONTWEIGHT_NORMAL;
|
||||
bool underlined = false;
|
||||
|
||||
int size = (int) ([desc pointSize]+0.5);
|
||||
NSFontSymbolicTraits traits = [desc symbolicTraits];
|
||||
int size = (int) ([theFont pointSize]+0.5);
|
||||
|
||||
if ( traits & NSFontBoldTrait )
|
||||
if ( theFontWeight >= 9 )
|
||||
fontweight = wxFONTWEIGHT_BOLD ;
|
||||
else if ( theFontWeight < 5 )
|
||||
fontweight = wxFONTWEIGHT_LIGHT;
|
||||
else
|
||||
fontweight = wxFONTWEIGHT_NORMAL ;
|
||||
if ( traits & NSFontItalicTrait )
|
||||
|
||||
if ( theTraits & NSItalicFontMask )
|
||||
fontstyle = wxFONTSTYLE_ITALIC ;
|
||||
|
||||
wxCFStringRef fontname( [desc postscriptName] );
|
||||
info->Init(size,wxFONTFAMILY_DEFAULT,fontstyle,fontweight,underlined,
|
||||
fontname.AsString(), wxFONTENCODING_DEFAULT);
|
||||
info->Init(size,fontFamily,fontstyle,fontweight,underlined,
|
||||
wxStringWithNSString([theFont familyName]), wxFONTENCODING_DEFAULT);
|
||||
|
||||
}
|
||||
info->m_nsFontDescriptor = desc;
|
||||
}
|
||||
|
||||
WX_NSFont wxFont::OSXCreateNSFont(wxOSXSystemFont font, wxNativeFontInfo* info)
|
||||
@@ -176,43 +188,26 @@ WX_NSFont wxFont::OSXCreateNSFont(wxOSXSystemFont font, wxNativeFontInfo* info)
|
||||
return nsfont;
|
||||
}
|
||||
|
||||
void wxNativeFontInfo::OSXValidateNSFontDescriptor()
|
||||
{
|
||||
NSFontDescriptor* desc = nil;
|
||||
NSFontSymbolicTraits traits = 0;
|
||||
float weight = 0;
|
||||
|
||||
if (m_weight == wxFONTWEIGHT_BOLD)
|
||||
{
|
||||
traits |= NSFontBoldTrait;
|
||||
weight = 1.0;
|
||||
}
|
||||
else if (m_weight == wxFONTWEIGHT_LIGHT)
|
||||
weight = -1;
|
||||
|
||||
if (m_style == wxFONTSTYLE_ITALIC || m_style == wxFONTSTYLE_SLANT)
|
||||
traits |= NSFontItalicTrait;
|
||||
|
||||
NSDictionary* traitsdict = [NSDictionary dictionaryWithObjectsAndKeys:
|
||||
[NSNumber numberWithUnsignedInt:traits], NSFontSymbolicTrait,
|
||||
nil] ;
|
||||
|
||||
desc = [NSFontDescriptor fontDescriptorWithFontAttributes:
|
||||
[NSDictionary dictionaryWithObjectsAndKeys:
|
||||
wxCFStringRef(m_faceName).AsNSString(), NSFontFamilyAttribute,
|
||||
[NSNumber numberWithFloat:m_pointSize], NSFontSizeAttribute,
|
||||
traitsdict, NSFontTraitsAttribute,
|
||||
[NSNumber numberWithFloat:weight],NSFontWeightTrait,
|
||||
nil]];
|
||||
|
||||
wxMacCocoaRetain(desc);
|
||||
m_nsFontDescriptor = desc;
|
||||
}
|
||||
|
||||
WX_NSFont wxFont::OSXCreateNSFont(const wxNativeFontInfo* info)
|
||||
{
|
||||
NSFont* nsFont;
|
||||
nsFont = [NSFont fontWithDescriptor:info->m_nsFontDescriptor size:info->m_pointSize];
|
||||
int weight = 5;
|
||||
NSFontTraitMask traits = 0;
|
||||
if (info->m_weight == wxFONTWEIGHT_BOLD)
|
||||
{
|
||||
traits |= NSBoldFontMask;
|
||||
weight = 9;
|
||||
}
|
||||
else if (info->m_weight == wxFONTWEIGHT_LIGHT)
|
||||
weight = 3;
|
||||
|
||||
if (info->m_style == wxFONTSTYLE_ITALIC || info->m_style == wxFONTSTYLE_SLANT)
|
||||
traits |= NSItalicFontMask;
|
||||
|
||||
nsFont = [[NSFontManager sharedFontManager] fontWithFamily:wxCFStringRef(info->m_faceName).AsNSString()
|
||||
traits:traits weight:weight size:info->m_pointSize];
|
||||
|
||||
wxASSERT_MSG(nsFont != nil,wxT("Couldn't create nsFont")) ;
|
||||
wxMacCocoaRetain(nsFont);
|
||||
return nsFont;
|
||||
}
|
||||
|
Reference in New Issue
Block a user