From 0646084eb258e28101c2cc3dc0173f665c2dfa2d Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 10 Jul 2013 01:28:18 +0000 Subject: [PATCH] Allow retrieving the descent and external leading of empty strings. This used to work before wxTextMeasure changes so make it work again instead of optimizing the case of empty string away and not returning anything in the descent and externalLeading output parameters in this case. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@74464 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/common/textmeasurecmn.cpp | 4 +++- src/gtk/textmeasure.cpp | 2 +- tests/graphics/measuring.cpp | 26 ++++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/common/textmeasurecmn.cpp b/src/common/textmeasurecmn.cpp index 6381059dd8..f583662cd1 100644 --- a/src/common/textmeasurecmn.cpp +++ b/src/common/textmeasurecmn.cpp @@ -86,7 +86,9 @@ void wxTextMeasureBase::GetTextExtent(const wxString& string, if ( !height ) height = &unusedHeight; - if ( string.empty() ) + // Avoid even setting up the DC for measuring if we don't actually need to + // measure anything. + if ( string.empty() && !descent && !externalLeading ) { *width = *height = 0; diff --git a/src/gtk/textmeasure.cpp b/src/gtk/textmeasure.cpp index eafd06848d..87bc7c90d5 100644 --- a/src/gtk/textmeasure.cpp +++ b/src/gtk/textmeasure.cpp @@ -126,7 +126,7 @@ void wxTextMeasure::DoGetTextExtent(const wxString& string, // Set layout's text const wxCharBuffer dataUTF8 = wxGTK_CONV_FONT(string, GetFont()); - if ( !dataUTF8 ) + if ( !dataUTF8 && !string.empty() ) { // hardly ideal, but what else can we do if conversion failed? wxLogLastError(wxT("GetTextExtent")); diff --git a/tests/graphics/measuring.cpp b/tests/graphics/measuring.cpp index ae02d27239..bc83433dbb 100644 --- a/tests/graphics/measuring.cpp +++ b/tests/graphics/measuring.cpp @@ -46,6 +46,7 @@ public: private: CPPUNIT_TEST_SUITE( MeasuringTextTestCase ); CPPUNIT_TEST( DCGetTextExtent ); + CPPUNIT_TEST( LeadingAndDescent ); CPPUNIT_TEST( WindowGetTextExtent ); CPPUNIT_TEST( GetPartialTextExtent ); #ifdef TEST_GC @@ -54,6 +55,7 @@ private: CPPUNIT_TEST_SUITE_END(); void DCGetTextExtent(); + void LeadingAndDescent(); void WindowGetTextExtent(); void GetPartialTextExtent(); @@ -127,6 +129,30 @@ void MeasuringTextTestCase::DCGetTextExtent() #endif } +void MeasuringTextTestCase::LeadingAndDescent() +{ + wxClientDC dc(wxTheApp->GetTopWindow()); + + // Retrieving just the descent should work. + int descent = -17; + dc.GetTextExtent("foo", NULL, NULL, &descent); + CPPUNIT_ASSERT( descent != -17 ); + + // Same for external leading. + int leading = -289; + dc.GetTextExtent("foo", NULL, NULL, NULL, &leading); + CPPUNIT_ASSERT( leading != -289 ); + + // And both should also work for the empty string as they retrieve the + // values valid for the entire font and not just this string. + int descent2, + leading2; + dc.GetTextExtent("", NULL, NULL, &descent2, &leading2); + + CPPUNIT_ASSERT_EQUAL( descent, descent2 ); + CPPUNIT_ASSERT_EQUAL( leading, leading2 ); +} + void MeasuringTextTestCase::WindowGetTextExtent() { wxWindow* const win = wxTheApp->GetTopWindow(); -- 2.45.2