From 46d6866c9f6235a75c894e4d49db4e7e82afad09 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 15 Jul 2020 02:01:36 +0200 Subject: [PATCH] Make wxGCDC::GetTextExtent("") return (0, 0) This seems more logical and is compatible with wxDC in wxMSW and wxGTK2, as well as other kinds of DC, e.g. wxPostScriptDC. It also looks like the current behaviour was unintentional as it happened only because wxGCDCImpl::DoGetTextExtent() always passed all non-null parameters to wxGraphicsContext::GetTextExtent(), even if it didn't need the values for all of them, and thus bypassed the special case for the empty string which was already present in the latter function. Fix this, making DoGetTextExtent() more efficient as a side effect (we now avoid unnecessary calls to pango_layout_iter_get_baseline() in the most common case), and also add another test for empty string to wxGraphicsContext itself, for non-GTK case. Also document this behaviour and add a test checking for it. --- docs/changes.txt | 3 +++ interface/wx/dc.h | 2 ++ src/common/dcgraph.cpp | 11 ++++++++++- src/generic/graphicc.cpp | 2 +- tests/graphics/measuring.cpp | 5 +++++ 5 files changed, 21 insertions(+), 2 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index 156e0bf467..e590145773 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -68,6 +68,9 @@ Changes in behaviour not resulting in compilation errors - wxGTK wxTextCtrl doesn't generate any wxEVT_TEXT when it's created with non-empty value, for consistency with the other ports. +- wxDC::GetTextExtent() returns height of 0 for empty string in wxGTK and wxOSX + too now, for consistency with wxMSW and other kinds of wxDC. + - wxMSW wxToolBar height now adapts to the height of embedded controls, making the toolbar taller if necessary, rather than making the controls smaller. To return to the previous behaviour, you need to explicitly create controls of diff --git a/interface/wx/dc.h b/interface/wx/dc.h index 9a2cbe21da..114b9cc951 100644 --- a/interface/wx/dc.h +++ b/interface/wx/dc.h @@ -954,6 +954,8 @@ public: used for the text extent calculation. Otherwise the currently selected font is. + If @a string is empty, its extent is 0 in both directions, as expected. + @note This function only works with single-line strings. @beginWxPerlOnly diff --git a/src/common/dcgraph.cpp b/src/common/dcgraph.cpp index fa5a7558be..f519943100 100644 --- a/src/common/dcgraph.cpp +++ b/src/common/dcgraph.cpp @@ -1236,7 +1236,16 @@ void wxGCDCImpl::DoGetTextExtent( const wxString &str, wxCoord *width, wxCoord * wxDouble h , d , e , w; - m_graphicContext->GetTextExtent( str, &w, &h, &d, &e ); + // Don't pass non-NULL pointers for the parts we don't need, this could + // result in doing extra unnecessary work inside GetTextExtent(). + m_graphicContext->GetTextExtent + ( + str, + width ? &w : NULL, + height ? &h : NULL, + descent ? &d : NULL, + externalLeading ? &e : NULL + ); if ( height ) *height = (wxCoord)(h+0.5); diff --git a/src/generic/graphicc.cpp b/src/generic/graphicc.cpp index b98a62ecf7..0eb6646099 100644 --- a/src/generic/graphicc.cpp +++ b/src/generic/graphicc.cpp @@ -2866,7 +2866,7 @@ void wxCairoContext::GetTextExtent( const wxString &str, wxDouble *width, wxDoub fe.height = fe.ascent + fe.descent; } - if (height) + if (height && !str.empty()) *height = fe.height; if ( descent ) *descent = fe.descent; diff --git a/tests/graphics/measuring.cpp b/tests/graphics/measuring.cpp index 723ec9778a..ad654cfe96 100644 --- a/tests/graphics/measuring.cpp +++ b/tests/graphics/measuring.cpp @@ -33,6 +33,8 @@ #include "wx/dcps.h" #include "wx/metafile.h" +#include "asserthelper.h" + // ---------------------------------------------------------------------------- // helper for XXXTextExtent() methods // ---------------------------------------------------------------------------- @@ -52,6 +54,9 @@ struct GetTextExtentTester wxSize size = obj.GetTextExtent("Hello"); CHECK( size.x > 1 ); CHECK( size.y == y ); + + // Test that getting text extent of an empty string returns (0, 0). + CHECK( obj.GetTextExtent(wxString()) == wxSize() ); } };