adding CoreText implementation

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@50331 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Stefan Csomor
2007-11-29 21:52:28 +00:00
parent 16bdd5e4a3
commit c07e1e2c85
2 changed files with 596 additions and 509 deletions

View File

@@ -40,45 +40,12 @@ class WXDLLEXPORT wxFontRefData: public wxGDIRefData
public:
wxFontRefData()
: m_fontId(0)
, m_pointSize(10)
, m_family(wxDEFAULT)
, m_style(wxNORMAL)
, m_weight(wxNORMAL)
, m_underlined(false)
, m_faceName(wxT("applicationfont"))
, m_encoding(wxFONTENCODING_DEFAULT)
#ifdef __LP64__
#else
, m_macFontFamily(0)
, m_macFontSize(0)
, m_macFontStyle(0)
, m_macATSUFontID(0)
#endif
, m_macATSUStyle(0)
{
Init(m_pointSize, m_family, m_style, m_weight,
m_underlined, m_faceName, m_encoding);
Init(10, wxDEFAULT, wxNORMAL, wxNORMAL,
false, wxT("applicationfont"), wxFONTENCODING_DEFAULT);
}
wxFontRefData(const wxFontRefData& data)
: wxGDIRefData()
, m_fontId(data.m_fontId)
, m_pointSize(data.m_pointSize)
, m_family(data.m_family)
, m_style(data.m_style)
, m_weight(data.m_weight)
, m_underlined(data.m_underlined)
, m_faceName(data.m_faceName)
, m_encoding(data.m_encoding)
#ifdef __LP64__
#else
, m_macFontFamily(data.m_macFontFamily)
, m_macFontSize(data.m_macFontSize)
, m_macFontStyle(data.m_macFontStyle)
, m_macATSUFontID(data.m_macATSUFontID)
#endif
, m_macATSUStyle(0)
{
Init(data.m_pointSize, data.m_family, data.m_style, data.m_weight,
data.m_underlined, data.m_faceName, data.m_encoding);
@@ -91,22 +58,6 @@ public:
bool underlined,
const wxString& faceName,
wxFontEncoding encoding)
: m_fontId(0)
, m_pointSize(size)
, m_family(family)
, m_style(style)
, m_weight(weight)
, m_underlined(underlined)
, m_faceName(faceName)
, m_encoding(encoding)
#ifdef __LP64__
#else
, m_macFontFamily(0)
, m_macFontSize(0)
, m_macFontStyle(0)
, m_macATSUFontID(0)
#endif
, m_macATSUStyle(0)
{
Init(size, family, style, weight, underlined, faceName, encoding);
}
@@ -132,7 +83,6 @@ protected:
wxFontEncoding encoding);
// font characterstics
int m_fontId;
int m_pointSize;
int m_family;
int m_style;
@@ -143,7 +93,7 @@ protected:
bool m_noAA; // No anti-aliasing
public:
#ifndef __LP64__
#if wxMAC_USE_ATSU_TEXT
FMFontFamily m_macFontFamily;
FMFontSize m_macFontSize;
FMFontStyle m_macFontStyle;
@@ -160,11 +110,12 @@ public:
// for true themeing support we must store the correct font
// information here, as this speeds up and optimizes rendering
ThemeFontID m_macThemeFontID ;
#else
CTFontRef m_macFontRef;
ATSUStyle m_macATSUStyle ;
#endif
#if wxMAC_USE_CORE_TEXT
wxCFRef<CTFontRef> m_ctFont;
CTFontUIFontType m_macUIFontType;
#endif
ATSUStyle m_macATSUStyle ;
wxNativeFontInfo m_info;
};
@@ -195,20 +146,19 @@ void wxFontRefData::Init(int pointSize,
m_underlined = underlined;
m_faceName = faceName;
m_encoding = encoding;
#ifdef __LP64__
m_noAA = false;
#ifdef wxMAC_USE_CORE_TEXT
m_macUIFontType = kCTFontNoFontType;
m_macFontRef = 0;
#else
#endif
#if wxMAC_USE_ATSU_TEXT
m_macFontFamily = 0 ;
m_macFontSize = 0;
m_macFontStyle = 0;
m_macATSUFontID = 0;
m_macATSUAdditionalQDStyles = 0 ;
m_macThemeFontID = kThemeCurrentPortFont ;
#endif
m_macATSUStyle = NULL ;
m_noAA = false;
#endif
}
wxFontRefData::~wxFontRefData()
@@ -220,22 +170,92 @@ wxFontRefData::~wxFontRefData()
}
}
#if wxMAC_USE_CORE_TEXT
/* code mixed together from 2 different routines from Core Text Manual Common Operations */
static CTFontRef wxMacCreateCTFont(CFStringRef iFamilyName, CTFontSymbolicTraits iTraits, CGFloat iSize )
{
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) {
// Add a family name to our attributes.
CFDictionaryAddValue(attributes, kCTFontFamilyNameAttribute, iFamilyName);
if ( iTraits ) {
CFMutableDictionaryRef traits;
CFNumberRef symTraits;
// Create the traits dictionary.
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.
descriptor = CTFontDescriptorCreateWithAttributes(attributes);
check(descriptor != NULL);
CFRelease(attributes);
}
// Return our font descriptor.
CTFontRef font = CTFontCreateWithFontDescriptor( descriptor, iSize, NULL);
CFRelease( descriptor );
return font ;
}
#endif
void wxFontRefData::MacFindFont()
{
OSStatus status = noErr;
Str255 qdFontName ;
#ifdef __LP64__
if ( m_faceName.empty() && m_family == wxDEFAULT )
#if wxMAC_USE_CORE_TEXT
if ( CTFontCreateWithName != NULL )
{
if ( m_faceName.empty() && m_family == wxDEFAULT && m_macUIFontType == kCTFontNoFontType )
{
m_macUIFontType = kCTFontSystemFontType;
}
if ( m_macUIFontType != kCTFontNoFontType )
{
m_macFontRef = CTFontCreateUIFontForLanguage( m_macUIFontType, 0.0, NULL );
wxMacCFStringHolder name( CTFontCopyFamilyName( m_macFontRef ) );
m_ctFont.reset(CTFontCreateUIFontForLanguage( m_macUIFontType, 0.0, NULL ));
wxMacCFStringHolder name( CTFontCopyFamilyName( m_ctFont ) );
m_faceName = name.AsString();
if ( CTFontGetSize(m_ctFont) == 0 )
{
m_ctFont.reset(CTFontCreateUIFontForLanguage( m_macUIFontType, 12, NULL ));
}
m_pointSize = CTFontGetSize(m_ctFont) ;
// reset this so that future manipulation don't fall back
m_macUIFontType = kCTFontNoFontType;
}
else
{
@@ -265,78 +285,26 @@ void wxFontRefData::MacFindFont()
}
wxMacCFStringHolder cf( m_faceName, wxLocale::GetSystemEncoding() );
m_macFontRef = CTFontCreateWithName( cf, m_pointSize, NULL);
CTFontSymbolicTraits traits = 0;
if (m_weight == wxBOLD)
traits |= kCTFontBoldTrait;
if (m_style == wxITALIC || m_style == wxSLANT)
traits |= kCTFontItalicTrait;
m_ctFont.reset( wxMacCreateCTFont( cf, traits, m_pointSize ) );
}
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[] =
{
kATSUSizeTag ,
kATSUVerticalCharacterTag,
kATSUQDBoldfaceTag ,
kATSUQDItalicTag ,
kATSUQDUnderlineTag ,
kATSUQDCondensedTag ,
kATSUQDExtendedTag ,
kATSUFontTag ,
};
ByteCount atsuSizes[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
{
sizeof( Fixed ) ,
sizeof( ATSUVerticalCharacterType),
sizeof( Boolean ) ,
sizeof( Boolean ) ,
sizeof( Boolean ) ,
sizeof( Boolean ) ,
sizeof( Boolean ) ,
sizeof( ATSUFontID ) ,
};
Boolean kTrue = true ;
Boolean kFalse = false ;
Fixed atsuSize = IntToFixed( m_pointSize );
short m_macATSUAdditionalQDStyles = 0;
ATSUVerticalCharacterType kHorizontal = kATSUStronglyHorizontal;
ATSUFontID atsuFontID = 0;
int attributeCount = sizeof(atsuTags) / sizeof(ATSUAttributeTag) ;
// attempt to add atsu font
#if 0
status = ATSUFindFontFromName(m_faceName.c_str(), strlen(m_faceName.c_str()), kFontFamilyName, kFontNoPlatform, kFontNoScript, kFontNoLanguage, &atsuFontID);
if ( status != noErr )
{
attributeCount--;
}
#endif
ATSUAttributeValuePtr atsuValues[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
#if wxMAC_USE_ATSU_TEXT
{
&atsuSize ,
&kHorizontal,
(m_macATSUAdditionalQDStyles & bold) ? &kTrue : &kFalse ,
(m_macATSUAdditionalQDStyles & italic) ? &kTrue : &kFalse ,
(m_macATSUAdditionalQDStyles & underline) ? &kTrue : &kFalse ,
(m_macATSUAdditionalQDStyles & condense) ? &kTrue : &kFalse ,
(m_macATSUAdditionalQDStyles & extend) ? &kTrue : &kFalse ,
&atsuFontID ,
};
status = ::ATSUSetAttributes( (ATSUStyle)m_macATSUStyle, attributeCount, atsuTags, atsuSizes, atsuValues);
wxASSERT_MSG( status == noErr , wxT("couldn't modify ATSU style") );
#else
Str255 qdFontName ;
if ( m_macThemeFontID != kThemeCurrentPortFont )
{
Style style ;
GetThemeFont( m_macThemeFontID, GetApplicationScript(), qdFontName, &m_macFontSize, &style );
if ( m_macFontSize == 0 )
m_macFontSize = 12;
m_macFontStyle = style ;
m_faceName = wxMacMakeStringFromPascal( qdFontName );
if ( m_macFontStyle & bold )
@@ -486,6 +454,8 @@ void wxFontRefData::MacFindFont()
atsuTags, atsuSizes, atsuValues);
wxASSERT_MSG( status == noErr , wxT("couldn't modify ATSU style") );
return;
}
#endif
}
@@ -526,7 +496,7 @@ bool wxFont::Create(int pointSize,
return true;
}
#ifdef __LP64__
#if wxMAC_USE_CORE_TEXT
bool wxFont::MacCreateUIFont(wxUint32 ctFontType )
{
@@ -546,9 +516,14 @@ bool wxFont::MacCreateUIFont(wxUint32 ctFontType )
bool wxFont::MacCreateThemeFont(wxUint16 themeFontID)
{
#ifdef __LP64__
#if wxMAC_USE_CORE_TEXT
if ( HIThemeGetUIFontType != NULL )
{
return MacCreateUIFont(HIThemeGetUIFontType(themeFontID));
#else
}
#endif
#if wxMAC_USE_CORE_TEXT
{
UnRef();
m_refData = new wxFontRefData(
@@ -557,7 +532,7 @@ bool wxFont::MacCreateThemeFont(wxUint16 themeFontID)
M_FONTDATA->m_macThemeFontID = themeFontID ;
RealizeResource();
}
return true;
#endif
}
@@ -599,6 +574,9 @@ void wxFont::Unshare()
void wxFont::SetPointSize(int pointSize)
{
if ( M_FONTDATA->m_pointSize == pointSize )
return;
Unshare();
M_FONTDATA->m_pointSize = pointSize;
@@ -739,7 +717,7 @@ bool wxFont::GetNoAntiAliasing() const
return M_FONTDATA->m_noAA;
}
#ifndef __LP64__
#if wxMAC_USE_ATSU_TEXT
short wxFont::MacGetFontNum() const
{
@@ -789,20 +767,15 @@ wxUint16 wxFont::MacGetThemeFontID() const
return M_FONTDATA->m_macThemeFontID;
}
#else
#endif
#if wxMAC_USE_CORE_TEXT
const void * wxFont::MacGetCTFont() const
{
wxCHECK_MSG( M_FONTDATA != NULL , 0, wxT("invalid font") );
return M_FONTDATA->m_macFontRef;
}
// to be removed
void * wxFont::MacGetATSUStyle() const
{
wxCHECK_MSG( M_FONTDATA != NULL , NULL, wxT("invalid font") );
return M_FONTDATA->m_macATSUStyle;
return (CTFontRef)(M_FONTDATA->m_ctFont);
}
#endif

View File

@@ -696,18 +696,37 @@ public:
wxMacCoreGraphicsFontData( wxGraphicsRenderer* renderer, const wxFont &font, const wxColour& col );
~wxMacCoreGraphicsFontData();
#if wxMAC_USE_ATSU_TEXT
virtual ATSUStyle GetATSUStyle() { return m_macATSUIStyle; }
#endif
#if wxMAC_USE_CORE_TEXT
CTFontRef GetCTFont() const { return m_ctFont ; }
#endif
wxColour GetColour() const { return m_colour ; }
bool GetUnderlined() const { return m_underlined ; }
private :
wxColour m_colour;
bool m_underlined;
#if wxMAC_USE_ATSU_TEXT
ATSUStyle m_macATSUIStyle;
#endif
#if wxMAC_USE_CORE_TEXT
wxCFRef< CTFontRef > m_ctFont;
#endif
};
wxMacCoreGraphicsFontData::wxMacCoreGraphicsFontData(wxGraphicsRenderer* renderer, const wxFont &font, const wxColour& col) : wxGraphicsObjectRefData( renderer )
{
m_macATSUIStyle = NULL;
OSStatus status = noErr;
m_colour = col;
m_underlined = font.GetUnderlined();
#if wxMAC_USE_CORE_TEXT
#elif wxMAC_USE_ATSU_TEXT
OSStatus status;
m_ctFont.reset( wxCFRetain((CTFontRef) font.MacGetCTFont()) );
#endif
#if wxMAC_USE_ATSU_TEXT
status = ATSUCreateAndCopyStyle( (ATSUStyle) font.MacGetATSUStyle() , &m_macATSUIStyle );
@@ -739,20 +758,23 @@ wxMacCoreGraphicsFontData::wxMacCoreGraphicsFontData(wxGraphicsRenderer* rendere
atsuTags, atsuSizes, atsuValues);
wxASSERT_MSG( status == noErr , wxT("couldn't modify ATSU style") );
#elif WXMAC_USE_CG_TEXT
#endif
#if WXMAC_USE_CG_TEXT
#endif
}
wxMacCoreGraphicsFontData::~wxMacCoreGraphicsFontData()
{
#if wxMAC_USE_CORE_TEXT
#elif wxMAC_USE_ATSU_TEXT
#endif
#if wxMAC_USE_ATSU_TEXT
if ( m_macATSUIStyle )
{
::ATSUDisposeStyle((ATSUStyle)m_macATSUIStyle);
m_macATSUIStyle = NULL;
}
#elif WXMAC_USE_CG_TEXT
#endif
#if WXMAC_USE_CG_TEXT
#endif
}
@@ -1720,10 +1742,39 @@ void wxMacCoreGraphicsContext::DrawText( const wxString &str, wxDouble x, wxDoub
EnsureIsValid();
#if wxMAC_USE_CORE_TEXT
// TODO core text implementation here
#elif wxMAC_USE_ATSU_TEXT
if ( UMAGetSystemVersion() >= 0x1050 )
{
wxMacCoreGraphicsFontData* fref = (wxMacCoreGraphicsFontData*)m_font.GetRefData();
wxMacCFStringHolder text(str, wxLocale::GetSystemEncoding() );
CTFontRef font = fref->GetCTFont();
CGColorRef col = fref->GetColour().GetPixel();
CTUnderlineStyle ustyle = fref->GetUnderlined() ? kCTUnderlineStyleSingle : kCTUnderlineStyleNone ;
wxCFRef<CFNumberRef> underlined( CFNumberCreate(NULL, kCFNumberSInt32Type, &ustyle) );
CFStringRef keys[] = { kCTFontAttributeName , kCTForegroundColorAttributeName, kCTUnderlineStyleAttributeName };
CFTypeRef values[] = { font, col, underlined };
wxCFRef<CFDictionaryRef> attributes( CFDictionaryCreate(kCFAllocatorDefault, (const void**) &keys, (const void**) &values,
WXSIZEOF( keys ), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks) );
wxCFRef<CFAttributedStringRef> attrtext( CFAttributedStringCreate(kCFAllocatorDefault, text, attributes) );
wxCFRef<CTLineRef> line( CTLineCreateWithAttributedString(attrtext) );
y += CTFontGetAscent(font);
CGContextSaveGState(m_cgContext);
CGContextTranslateCTM(m_cgContext, x, y);
CGContextScaleCTM(m_cgContext, 1, -1);
CGContextSetTextPosition(m_cgContext, 0, 0);
CTLineDraw( line, m_cgContext );
CGContextRestoreGState(m_cgContext);
return;
}
#endif
#if wxMAC_USE_ATSU_TEXT
{
DrawText(str, x, y, 0.0);
#elif WXMAC_USE_CG_TEXT
return;
}
#endif
#if WXMAC_USE_CG_TEXT
// TODO core graphics text implementation here
#endif
}
@@ -1735,9 +1786,15 @@ void wxMacCoreGraphicsContext::DrawText( const wxString &str, wxDouble x, wxDoub
EnsureIsValid();
#if wxMAC_USE_CORE_TEXT
if ( UMAGetSystemVersion() >= 0x1050 )
{
// default implementation takes care of rotation and calls non rotated DrawText afterwards
wxGraphicsContext::DrawText( str, x, y, angle );
#elif wxMAC_USE_ATSU_TEXT
return;
}
#endif
#if wxMAC_USE_ATSU_TEXT
{
OSStatus status = noErr;
ATSUTextLayout atsuLayout;
UniCharCount chars = str.length();
@@ -1843,7 +1900,10 @@ void wxMacCoreGraphicsContext::DrawText( const wxString &str, wxDouble x, wxDoub
#if SIZEOF_WCHAR_T == 4
free( ubuf );
#endif
#elif WXMAC_USE_CG_TEXT
return;
}
#endif
#if WXMAC_USE_CG_TEXT
// default implementation takes care of rotation and calls non rotated DrawText afterwards
wxGraphicsContext::DrawText( str, x, y, angle );
#endif
@@ -1867,8 +1927,36 @@ void wxMacCoreGraphicsContext::GetTextExtent( const wxString &str, wxDouble *wid
return;
#if wxMAC_USE_CORE_TEXT
// TODO core text implementation here
#elif wxMAC_USE_ATSU_TEXT
if ( UMAGetSystemVersion() >= 0x1050 )
{
wxMacCoreGraphicsFontData* fref = (wxMacCoreGraphicsFontData*)m_font.GetRefData();
CTFontRef font = fref->GetCTFont();
wxMacCFStringHolder text(str, wxLocale::GetSystemEncoding() );
CFStringRef keys[] = { kCTFontAttributeName };
CFTypeRef values[] = { font };
wxCFRef<CFDictionaryRef> attributes( CFDictionaryCreate(kCFAllocatorDefault, (const void**) &keys, (const void**) &values,
WXSIZEOF( keys ), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks) );
wxCFRef<CFAttributedStringRef> attrtext( CFAttributedStringCreate(kCFAllocatorDefault, text, attributes) );
wxCFRef<CTLineRef> line( CTLineCreateWithAttributedString(attrtext) );
CGFloat w, a, d, l;
w = CTLineGetTypographicBounds(line, &a, &d, &l) ;
if ( height )
*height = a+d+l;
if ( descent )
*descent = d;
if ( externalLeading )
*externalLeading = l;
if ( width )
*width = w;
return;
}
#endif
#if wxMAC_USE_ATSU_TEXT
{
OSStatus status = noErr;
ATSUTextLayout atsuLayout;
@@ -1923,7 +2011,10 @@ void wxMacCoreGraphicsContext::GetTextExtent( const wxString &str, wxDouble *wid
#if SIZEOF_WCHAR_T == 4
free( ubuf ) ;
#endif
#elif WXMAC_USE_CG_TEXT
return;
}
#endif
#if WXMAC_USE_CG_TEXT
// TODO core graphics text implementation here
#endif
}
@@ -1937,8 +2028,29 @@ void wxMacCoreGraphicsContext::GetPartialTextExtents(const wxString& text, wxArr
return;
#if wxMAC_USE_CORE_TEXT
// TODO core text implementation here
#elif wxMAC_USE_ATSU_TEXT
{
wxMacCoreGraphicsFontData* fref = (wxMacCoreGraphicsFontData*)m_font.GetRefData();
CTFontRef font = fref->GetCTFont();
wxMacCFStringHolder t(text, wxLocale::GetSystemEncoding() );
CFStringRef keys[] = { kCTFontAttributeName };
CFTypeRef values[] = { font };
wxCFRef<CFDictionaryRef> attributes( CFDictionaryCreate(kCFAllocatorDefault, (const void**) &keys, (const void**) &values,
WXSIZEOF( keys ), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks) );
wxCFRef<CFAttributedStringRef> attrtext( CFAttributedStringCreate(kCFAllocatorDefault, t, attributes) );
wxCFRef<CTLineRef> line( CTLineCreateWithAttributedString(attrtext) );
int chars = text.length();
for ( int pos = 0; pos < (int)chars; pos ++ )
{
widths[pos] = CTLineGetOffsetForStringIndex( line, pos+1 , NULL )+0.5;
}
return;
}
#endif
#if wxMAC_USE_ATSU_TEXT
{
ATSUTextLayout atsuLayout;
UniCharCount chars = text.length();
UniChar* ubuf = NULL;
@@ -1990,7 +2102,9 @@ void wxMacCoreGraphicsContext::GetPartialTextExtents(const wxString& text, wxArr
#if SIZEOF_WCHAR_T == 4
free( ubuf ) ;
#endif
#elif WXMAC_USE_CG_TEXT
}
#endif
#if WXMAC_USE_CG_TEXT
// TODO core graphics text implementation here
#endif
}