From: Vadim Zeitlin Date: Sat, 15 Apr 2006 15:08:43 +0000 (+0000) Subject: made width computation in GetTextExtent() more precise, especially for italics fonts... X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/481203cb23ab64f570b2046f7154a94e0561f3e7 made width computation in GetTextExtent() more precise, especially for italics fonts (based on the code from Wlodek Szafran) git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@38743 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/docs/changes.txt b/docs/changes.txt index 9fa8473563..9c53ced097 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -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: diff --git a/src/msw/dc.cpp b/src/msw/dc.cpp index 2984f14438..1bd9b7195c 100644 --- a/src/msw/dc.cpp +++ b/src/msw/dc.cpp @@ -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;