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.
This commit is contained in:
Vadim Zeitlin 2020-07-15 02:01:36 +02:00
parent a52a27ad90
commit 46d6866c9f
5 changed files with 21 additions and 2 deletions

View File

@ -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

View File

@ -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

View File

@ -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);

View File

@ -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;

View File

@ -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() );
}
};