JUCE icon indicating copy to clipboard operation
JUCE copied to clipboard

[Bug]: TextEditor::getTotalTextHeight() adds height for a newline twice

Open stijnfrishert opened this issue 1 year ago • 0 comments

Detailed steps on how to reproduce the bug

  1. Create a juce::TextEditor, enable multi-line support and set the text to "a".
  2. Set a breakpoint in juce::TextEditor::getTotalTextHeight()
  3. Type in a newline after the "a"

The juce::TextEditor::getTotalTextHeight() function loops over all sections and atoms using while (next()) {}.

int getTotalTextHeight()
{
    while (next()) {}

    auto height = lineY + lineHeight + getYOffset();

    if (atom != nullptr && atom->isNewLine())
        height += lineHeight;

    return roundToInt (height);
}

The last of the next() calls ends up in moveToEndOfLastAtom():

void moveToEndOfLastAtom()
    {
        if (atom != nullptr)
        {
            atomX = atomRight;

            if (atom->isNewLine())
            {
                atomX = getJustificationOffsetX (0);
                lineY += lineHeight * lineSpacing;
            }
        }
    }

As you can ssee, movetoEndOfLastAtom() already adds a lineHeight if the last atom is a new line. But then when we step out back into getTotalTextHeight(), this function also adds extra height when the last character is a new line!

The total height ends up with an extra line, as can be verified in the debugger. This all results in issues we're seeing in when editing in a multi-line text editor that auto-resizes if the line count changes.

What is the expected behaviour?

getTotalTextHeight() returns the actual total text height

Operating systems

macOS

What versions of the operating systems?

macOS Ventura 13.3.1

Architectures

64-bit

Stacktrace

No response

Plug-in formats (if applicable)

No response

Plug-in host applications (DAWs) (if applicable)

No response

Testing on the develop branch

I have not tested against the develop branch

Code of Conduct

  • [X] I agree to follow the Code of Conduct

stijnfrishert avatar May 26 '23 08:05 stijnfrishert