From 14619f10b0bdb630206607abd0ce0319d45e095a Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 15 Jul 2013 01:31:56 +0000 Subject: [PATCH] Support using GetTextExtent() with empty string to get descent in wxOSX. Allow measuring the descent and external leading of an empty string by measuring just a space instead in wxOSX. This makes the behaviour more consistent with wxMSW and makes the unit test added in r74464 pass under OS X too. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@74524 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/osx/carbon/graphics.cpp | 48 ++++++++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/src/osx/carbon/graphics.cpp b/src/osx/carbon/graphics.cpp index c42ffba..31df459 100644 --- a/src/osx/carbon/graphics.cpp +++ b/src/osx/carbon/graphics.cpp @@ -2512,15 +2512,19 @@ void wxMacCoreGraphicsContext::GetTextExtent( const wxString &str, wxDouble *wid if ( externalLeading ) *externalLeading = 0; + // In wxWidgets (MSW-inspired) API it is possible to call GetTextExtent() + // with an empty string to get just the descent and the leading of the + // font, so support this (mis)use. + wxString strToMeasure(str); if (str.empty()) - return; + strToMeasure = wxS(" "); #if wxOSX_USE_CORE_TEXT { wxMacCoreGraphicsFontData* fref = (wxMacCoreGraphicsFontData*)m_font.GetRefData(); CTFontRef font = fref->OSXGetCTFont(); - wxCFStringRef text(str, wxLocale::GetSystemEncoding() ); + wxCFStringRef text(strToMeasure, wxLocale::GetSystemEncoding() ); CFStringRef keys[] = { kCTFontAttributeName }; CFTypeRef values[] = { font }; wxCFRef attributes( CFDictionaryCreate(kCFAllocatorDefault, (const void**) &keys, (const void**) &values, @@ -2531,14 +2535,18 @@ void wxMacCoreGraphicsContext::GetTextExtent( const wxString &str, wxDouble *wid CGFloat a, d, l, w; w = CTLineGetTypographicBounds(line, &a, &d, &l); - if ( height ) - *height = a+d+l; + if ( !str.empty() ) + { + if ( width ) + *width = w; + if ( height ) + *height = a+d+l; + } + if ( descent ) *descent = d; if ( externalLeading ) *externalLeading = l; - if ( width ) - *width = w; return; } #endif @@ -2547,7 +2555,7 @@ void wxMacCoreGraphicsContext::GetTextExtent( const wxString &str, wxDouble *wid OSStatus status = noErr; ATSUTextLayout atsuLayout; - wxMacUniCharBuffer unibuf( str ); + wxMacUniCharBuffer unibuf( strToMeasure ); UniCharCount chars = unibuf.GetChars(); ATSUStyle style = (((wxMacCoreGraphicsFontData*)m_font.GetRefData())->GetATSUStyle()); @@ -2565,14 +2573,18 @@ void wxMacCoreGraphicsContext::GetTextExtent( const wxString &str, wxDouble *wid status = ::ATSUGetUnjustifiedBounds( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd, &textBefore , &textAfter, &textAscent , &textDescent ); - if ( height ) - *height = FixedToFloat(textAscent + textDescent); + if ( !str.empty() ) + { + if ( width ) + *width = FixedToFloat(textAfter - textBefore); + if ( height ) + *height = FixedToFloat(textAscent + textDescent); + } + if ( descent ) *descent = FixedToFloat(textDescent); if ( externalLeading ) *externalLeading = 0; - if ( width ) - *width = FixedToFloat(textAfter - textBefore); ::ATSUDisposeTextLayout(atsuLayout); @@ -2582,19 +2594,23 @@ void wxMacCoreGraphicsContext::GetTextExtent( const wxString &str, wxDouble *wid #if wxOSX_USE_IPHONE wxMacCoreGraphicsFontData* fref = (wxMacCoreGraphicsFontData*)m_font.GetRefData(); - wxCFStringRef text(str, wxLocale::GetSystemEncoding() ); + wxCFStringRef text(strToMeasure, wxLocale::GetSystemEncoding() ); CGSize sz = MeasureTextInContext( fref->GetUIFont() , text.AsNSString() ); - if ( height ) - *height = sz.height; + if ( !str.empty() ) + { + if ( width ) + *width = sz.width; + if ( height ) + *height = sz.height; + } + /* if ( descent ) *descent = FixedToFloat(textDescent); if ( externalLeading ) *externalLeading = 0; */ - if ( width ) - *width = sz.width; #endif CheckInvariants(); -- 2.7.4