Use toll-free bridges from CTFontRef to NSFont or UIFont from (#507)

This commit is contained in:
Stefan Csomor 2017-06-24 11:09:29 +02:00 committed by GitHub
parent 36de95bd8e
commit a77066d530
3 changed files with 2 additions and 243 deletions

View File

@ -148,15 +148,11 @@ public:
#if wxOSX_USE_COCOA
WX_NSFont OSXGetNSFont() const;
static WX_NSFont OSXCreateNSFont(wxOSXSystemFont font, wxNativeFontInfo* info);
static WX_NSFont OSXCreateNSFont(const wxNativeFontInfo* info);
static void SetNativeInfoFromNSFont(WX_NSFont nsfont, wxNativeFontInfo* info);
#endif
#if wxOSX_USE_IPHONE
WX_UIFont OSXGetUIFont() const;
static WX_UIFont OSXCreateUIFont(wxOSXSystemFont font, wxNativeFontInfo* info);
static WX_UIFont OSXCreateUIFont(const wxNativeFontInfo* info);
#endif
protected:

View File

@ -160,12 +160,6 @@ public:
wxCFRef<CTFontRef> m_ctFont;
wxCFRef<CFDictionaryRef> m_ctFontAttributes;
wxCFRef<CGFontRef> m_cgFont;
#if wxOSX_USE_COCOA
WX_NSFont m_nsFont;
#endif
#if wxOSX_USE_IPHONE
WX_UIFont m_uiFont;
#endif
wxNativeFontInfo m_info;
};
@ -179,13 +173,6 @@ wxFontRefData::wxFontRefData(const wxFontRefData& data) : wxGDIRefData()
m_ctFont = data.m_ctFont;
m_ctFontAttributes = data.m_ctFontAttributes;
m_cgFont = data.m_cgFont;
#if wxOSX_USE_COCOA
m_nsFont = (NSFont*) wxMacCocoaRetain(data.m_nsFont);
#endif
#if wxOSX_USE_IPHONE
m_uiFont = (UIFont*) wxMacCocoaRetain(data.m_uiFont);
#endif
}
// ============================================================================
@ -201,12 +188,6 @@ static CTFontDescriptorRef wxMacCreateCTFontDescriptor(CFStringRef iFamilyName,
void wxFontRefData::Init()
{
#if wxOSX_USE_COCOA
m_nsFont = NULL;
#endif
#if wxOSX_USE_IPHONE
m_uiFont = NULL;
#endif
m_fontValid = false;
}
@ -219,20 +200,6 @@ void wxFontRefData::Free()
{
m_ctFont.reset();
m_cgFont.reset();
#if wxOSX_USE_COCOA
if (m_nsFont != NULL)
{
wxMacCocoaRelease(m_nsFont);
m_nsFont = NULL;
}
#endif
#if wxOSX_USE_IPHONE
if (m_uiFont != NULL)
{
wxMacCocoaRelease(m_uiFont);
m_uiFont = NULL;
}
#endif
m_fontValid = false;
}
@ -283,13 +250,6 @@ wxFontRefData::wxFontRefData(wxOSXSystemFont font, int size)
m_info.Init(descr);
}
#if wxOSX_USE_COCOA
m_nsFont = wxFont::OSXCreateNSFont( font, &m_info );
#endif
#if wxOSX_USE_IPHONE
m_uiFont = wxFont::OSXCreateUIFont( font, &m_info );
#endif
m_fontValid = true;
}
@ -413,12 +373,6 @@ void wxFontRefData::MacFindFont()
m_cgFont.reset(CTFontCopyGraphicsFont(m_ctFont, NULL));
}
#if wxOSX_USE_COCOA
m_nsFont = wxFont::OSXCreateNSFont( &m_info );
#endif
#if wxOSX_USE_IPHONE
m_uiFont = wxFont::OSXCreateUIFont( &m_info );
#endif
m_fontValid = true;
}
@ -736,7 +690,7 @@ NSFont* wxFont::OSXGetNSFont() const
// cast away constness otherwise lazy font resolution is not possible
const_cast<wxFont *>(this)->RealizeResource();
return (M_FONTDATA->m_nsFont);
return const_cast<NSFont*>(reinterpret_cast<const NSFont*>(OSXGetCTFont()));
}
#endif
@ -750,7 +704,7 @@ UIFont* wxFont::OSXGetUIFont() const
// cast away constness otherwise lazy font resolution is not possible
const_cast<wxFont *>(this)->RealizeResource();
return (M_FONTDATA->m_uiFont);
return const_cast<UIFont*>(reinterpret_cast<const UIFont*>(OSXGetCTFont()));
}
#endif

View File

@ -140,197 +140,6 @@ void wxFont::SetNativeInfoFromNSFont(WX_NSFont theFont, wxNativeFontInfo* info)
}
}
WX_NSFont wxFont::OSXCreateNSFont(wxOSXSystemFont font, wxNativeFontInfo* info)
{
NSFont* nsfont = nil;
switch( font )
{
case wxOSX_SYSTEM_FONT_NORMAL:
nsfont = [NSFont systemFontOfSize:[NSFont systemFontSize]];
break;
case wxOSX_SYSTEM_FONT_BOLD:
nsfont = [NSFont boldSystemFontOfSize:[NSFont systemFontSize]];
break;
case wxOSX_SYSTEM_FONT_SMALL:
nsfont = [NSFont systemFontOfSize:[NSFont smallSystemFontSize]];
break;
case wxOSX_SYSTEM_FONT_SMALL_BOLD:
nsfont = [NSFont boldSystemFontOfSize:[NSFont smallSystemFontSize]];
break;
case wxOSX_SYSTEM_FONT_MINI:
nsfont = [NSFont systemFontOfSize:
[NSFont systemFontSizeForControlSize:NSMiniControlSize]];
break;
case wxOSX_SYSTEM_FONT_MINI_BOLD:
nsfont = [NSFont boldSystemFontOfSize:
[NSFont systemFontSizeForControlSize:NSMiniControlSize]];
break;
case wxOSX_SYSTEM_FONT_LABELS:
nsfont = [NSFont labelFontOfSize:[NSFont labelFontSize]];
break;
case wxOSX_SYSTEM_FONT_VIEWS:
nsfont = [NSFont controlContentFontOfSize:0];
break;
default:
break;
}
[nsfont retain];
SetNativeInfoFromNSFont(nsfont, info);
return nsfont;
}
static const NSAffineTransformStruct kSlantNSTransformStruct = { 1, 0, static_cast<CGFloat>(tan(wxDegToRad(11))), 1, 0, 0 };
WX_NSFont wxFont::OSXCreateNSFont(const wxNativeFontInfo* info)
{
NSFont* nsFont;
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];
if ( nsFont == nil )
{
NSFontTraitMask remainingTraits = traits;
nsFont = [[NSFontManager sharedFontManager] fontWithFamily:wxCFStringRef(info->m_faceName).AsNSString()
traits:0 weight:5 size:info->m_pointSize];
if ( nsFont == nil )
{
if ( info->m_weight == wxFONTWEIGHT_BOLD )
{
nsFont = [NSFont boldSystemFontOfSize:info->m_pointSize];
remainingTraits &= ~NSBoldFontMask;
}
else
nsFont = [NSFont systemFontOfSize:info->m_pointSize];
}
// fallback - if in doubt, let go of the bold attribute
if ( nsFont && (remainingTraits & NSItalicFontMask) )
{
NSFont* nsFontWithTraits = nil;
if ( remainingTraits & NSBoldFontMask)
{
nsFontWithTraits = [[NSFontManager sharedFontManager] convertFont:nsFont toHaveTrait:NSBoldFontMask];
if ( nsFontWithTraits == nil )
{
nsFontWithTraits = [[NSFontManager sharedFontManager] convertFont:nsFont toHaveTrait:NSItalicFontMask];
if ( nsFontWithTraits != nil )
remainingTraits &= ~NSItalicFontMask;
}
else
{
remainingTraits &= ~NSBoldFontMask;
}
}
// the code below causes crashes, because fontDescriptorWithMatrix is not returning a valid font descriptor
// it adds a NSCTFontMatrixAttribute as well which cannot be disposed of correctly by the autorelease pool
// so at the moment we have to disable this and cannot synthesize italic fonts if they are not available on the system
#if 0
if ( remainingTraits & NSItalicFontMask )
{
if ( nsFontWithTraits == nil )
nsFontWithTraits = nsFont;
NSAffineTransform* transform = [NSAffineTransform transform];
[transform setTransformStruct:kSlantNSTransformStruct];
[transform scaleBy:info->m_pointSize];
NSFontDescriptor* italicDesc = [[nsFontWithTraits fontDescriptor] fontDescriptorWithMatrix:transform];
if ( italicDesc != nil )
{
NSFont* f = [NSFont fontWithDescriptor:italicDesc size:(CGFloat)(info->m_pointSize)];
if ( f != nil )
nsFontWithTraits = f;
}
}
#endif
if ( nsFontWithTraits != nil )
nsFont = nsFontWithTraits;
}
}
wxASSERT_MSG(nsFont != nil,wxT("Couldn't create nsFont")) ;
wxMacCocoaRetain(nsFont);
return nsFont;
}
#endif
#if wxOSX_USE_IPHONE
WX_UIFont wxFont::OSXCreateUIFont(wxOSXSystemFont font, wxNativeFontInfo* info)
{
UIFont* uifont;
switch( font )
{
case wxOSX_SYSTEM_FONT_NORMAL:
uifont = [UIFont systemFontOfSize:[UIFont systemFontSize]];
break;
case wxOSX_SYSTEM_FONT_BOLD:
uifont = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
break;
case wxOSX_SYSTEM_FONT_MINI:
case wxOSX_SYSTEM_FONT_SMALL:
uifont = [UIFont systemFontOfSize:[UIFont smallSystemFontSize]];
break;
case wxOSX_SYSTEM_FONT_MINI_BOLD:
case wxOSX_SYSTEM_FONT_SMALL_BOLD:
uifont = [UIFont boldSystemFontOfSize:[UIFont smallSystemFontSize]];
break;
case wxOSX_SYSTEM_FONT_VIEWS:
case wxOSX_SYSTEM_FONT_LABELS:
uifont = [UIFont systemFontOfSize:[UIFont labelFontSize]];
break;
default:
break;
}
[uifont retain];
if ( info->m_faceName.empty())
{
wxFontStyle fontstyle = wxFONTSTYLE_NORMAL;
wxFontWeight fontweight = wxFONTWEIGHT_NORMAL;
bool underlined = false;
bool strikethrough = false;
int size = (int) ([uifont pointSize]+0.5);
/*
NSFontSymbolicTraits traits = [desc symbolicTraits];
if ( traits & NSFontBoldTrait )
fontweight = wxFONTWEIGHT_BOLD ;
else
fontweight = wxFONTWEIGHT_NORMAL ;
if ( traits & NSFontItalicTrait )
fontstyle = wxFONTSTYLE_ITALIC ;
*/
wxCFStringRef fontname( wxCFRetain([uifont familyName]) );
info->Init(size, wxFONTFAMILY_DEFAULT, fontstyle, fontweight,
underlined, strikethrough,
fontname.AsString(), wxFONTENCODING_DEFAULT);
}
return uifont;
}
WX_UIFont wxFont::OSXCreateUIFont(const wxNativeFontInfo* info)
{
UIFont* uiFont;
uiFont = [UIFont fontWithName:wxCFStringRef(info->m_faceName).AsNSString() size:info->m_pointSize];
wxMacCocoaRetain(uiFont);
return uiFont;
}
#endif
// ----------------------------------------------------------------------------