From: Vadim Zeitlin <vadim@wxwidgets.org>
Date: Tue, 10 Nov 2009 00:13:57 +0000 (+0000)
Subject: Fix discrepancy between different ways of measuring text extents under Mac.
X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/0738b901b17340f09766524b8d9d79e9ed1268e7

Fix discrepancy between different ways of measuring text extents under Mac.

wxGraphicsContext::GetTextExtent() didn't round the returned double result to
int but truncated it instead and so returned different extent than
GetPartialTextExtents() which did round it up.

Moreover, wxGraphicsContext::GetPartialTextExtents() didn't round it up
correctly: it wrongly added 0.5 to the value still stored as double and which
was hence rounded up (correctly, this time) when converted to int in
wxDC::GetPartialTextExtents().

These two errors combined to produce difference of up to 2 pixels between the
last offset returned by wxDC::GetPartialTextExtents() and the total string
extent returned by wxDC::GetTextExtent() which thoroughly confused the code in
wxControlBase::DoEllipsizeSingleLine() (and probably not only there).

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@62580 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
---

diff --git a/src/osx/carbon/graphics.cpp b/src/osx/carbon/graphics.cpp
index ce0a00a507..f1b534870f 100644
--- a/src/osx/carbon/graphics.cpp
+++ b/src/osx/carbon/graphics.cpp
@@ -2349,10 +2349,13 @@ void wxMacCoreGraphicsContext::GetTextExtent( const wxString &str, wxDouble *wid
         wxCFRef<CFAttributedStringRef> attrtext( CFAttributedStringCreate(kCFAllocatorDefault, text, attributes) );
         wxCFRef<CTLineRef> line( CTLineCreateWithAttributedString(attrtext) );
 
-        double w;
+        // round the returned extent: this is probably more correct anyhow but
+        // we also need to do it to be consistent with GetPartialTextExtents()
+        // below and avoid strange situation when the last partial extent
+        // returned by it could have been greater than the full extent returned
+        // by us
         CGFloat a, d, l;
-
-        w = CTLineGetTypographicBounds(line, &a, &d, &l) ;
+        int w = CTLineGetTypographicBounds(line, &a, &d, &l) + 0.5;
 
         if ( height )
             *height = a+d+l;
@@ -2447,7 +2450,7 @@ void wxMacCoreGraphicsContext::GetPartialTextExtents(const wxString& text, wxArr
         int chars = text.length();
         for ( int pos = 0; pos < (int)chars; pos ++ )
         {
-            widths[pos] = CTLineGetOffsetForStringIndex( line, pos+1 , NULL )+0.5;
+            widths[pos] = CTLineGetOffsetForStringIndex( line, pos+1 , NULL );
         }
 
         return;