Ensure that pointers in GetMultiLineTextExtent() are always valid

Do the same thing as in GetTextExtent() to simplify the code a little
and also avoid temporary variables.

No real changes.
This commit is contained in:
Vadim Zeitlin 2020-07-15 01:17:20 +02:00
parent 2280e43fe9
commit 55c71fdac9

View File

@ -112,20 +112,30 @@ void wxTextMeasureBase::GetMultiLineTextExtent(const wxString& text,
wxCoord *height,
wxCoord *heightOneLine)
{
// To make the code simpler, make sure that the width and height pointers
// are always valid, by making them point to dummy variables if necessary.
int unusedWidth, unusedHeight;
if ( !width )
width = &unusedWidth;
if ( !height )
height = &unusedHeight;
*width = 0;
*height = 0;
// It's noticeably faster to handle the case of a string which isn't
// actually multiline specially here, to skip iteration above in this case.
if ( text.find('\n') == wxString::npos )
{
GetTextExtent(text, width, height);
if ( heightOneLine && height )
if ( heightOneLine )
*heightOneLine = *height;
return;
}
MeasuringGuard guard(*this);
wxCoord widthTextMax = 0, widthLine,
heightTextTotal = 0, heightLineDefault = 0, heightLine = 0;
wxCoord widthLine, heightLine = 0, heightLineDefault = 0;
wxString::const_iterator lineStart = text.begin();
for ( wxString::const_iterator pc = text.begin(); ; ++pc )
@ -147,14 +157,14 @@ void wxTextMeasureBase::GetMultiLineTextExtent(const wxString& text,
if ( !heightLineDefault )
heightLineDefault = GetEmptyLineHeight();
heightTextTotal += heightLineDefault;
*height += heightLineDefault;
}
else
{
CallGetTextExtent(wxString(lineStart, pc), &widthLine, &heightLine);
if ( widthLine > widthTextMax )
widthTextMax = widthLine;
heightTextTotal += heightLine;
if ( widthLine > *width )
*width = widthLine;
*height += heightLine;
}
if ( pc == text.end() )
@ -169,10 +179,6 @@ void wxTextMeasureBase::GetMultiLineTextExtent(const wxString& text,
}
}
if ( width )
*width = widthTextMax;
if ( height )
*height = heightTextTotal;
if ( heightOneLine )
*heightOneLine = heightLine;
}