]> git.saurik.com Git - wxWidgets.git/commitdiff
made width computation in GetTextExtent() more precise, especially for italics fonts...
authorVadim Zeitlin <vadim@wxwidgets.org>
Sat, 15 Apr 2006 15:08:43 +0000 (15:08 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Sat, 15 Apr 2006 15:08:43 +0000 (15:08 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@38743 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

docs/changes.txt
src/msw/dc.cpp

index 9fa84735636bf88c036b134c90f5a7264a7c5192..9c53ced097f8acf69e29832e6ad6ba239b29faed 100644 (file)
@@ -175,6 +175,7 @@ wxMSW:
 - Fixed wxChoice/wxComboBox slow appending and infinite recursion
   if its size is set within a paint handler (for example when embedded in a
   wxHtmlWindow). [Now reverted due to problems in W2K and below.]
+- wxDC::GetTextExtent() width calculation is more precise for italics fonts now
 
 wxGTK:
 
index 2984f14438acd76c76d066810b63612e876df4a4..1bd9b7195c26f3c6eac9a352b323c162df970fd0 100644 (file)
@@ -1753,10 +1753,39 @@ void wxDC::DoGetTextExtent(const wxString& string, wxCoord *x, wxCoord *y,
     }
 
     SIZE sizeRect;
-    TEXTMETRIC tm;
+    const size_t len = string.length();
+    if ( !::GetTextExtentPoint32(GetHdc(), string, len, &sizeRect) )
+    {
+        wxLogLastError(_T("GetTextExtentPoint32()"));
+    }
 
-    ::GetTextExtentPoint32(GetHdc(), string, string.length(), &sizeRect);
-    GetTextMetrics(GetHdc(), &tm);
+    // the result computed by GetTextExtentPoint32() may be too small as it
+    // accounts for under/overhang of the first/last character while we want
+    // just the bounding rect for this string so adjust the width as needed
+    if ( len > 0 )
+    {
+        ABC width;
+        const wxChar chFirst = *string.begin();
+        if ( ::GetCharABCWidths(GetHdc(), chFirst, chFirst, &width) )
+        {
+            if ( width.abcA < 0 )
+                sizeRect.cx -= width.abcA;
+
+            if ( len > 1 )
+            {
+                const wxChar chLast = *string.rbegin();
+                ::GetCharABCWidths(GetHdc(), chLast, chLast, &width);
+            }
+            //else: we already have the width of the last character
+
+            if ( width.abcC < 0 )
+                sizeRect.cx -= width.abcC;
+        }
+        //else: GetCharABCWidths() failed, not a TrueType font?
+    }
+
+    TEXTMETRIC tm;
+    ::GetTextMetrics(GetHdc(), &tm);
 
     if (x)
         *x = sizeRect.cx;