From 6f8c4647473da5d9be893f8a937fb3faaa429c25 Mon Sep 17 00:00:00 2001 From: ed Date: Fri, 13 Aug 2021 16:31:33 +0100 Subject: [PATCH] TextEditor: Fix a highlighting bug --- .../widgets/juce_TextEditor.cpp | 56 ++++--------------- 1 file changed, 10 insertions(+), 46 deletions(-) diff --git a/modules/juce_gui_basics/widgets/juce_TextEditor.cpp b/modules/juce_gui_basics/widgets/juce_TextEditor.cpp index ba1153cf80..3fa7112e65 100644 --- a/modules/juce_gui_basics/widgets/juce_TextEditor.cpp +++ b/modules/juce_gui_basics/widgets/juce_TextEditor.cpp @@ -415,8 +415,6 @@ struct TextEditor::Iterator void beginNewLine() { - ++currentLineIndex; - lineY += lineHeight * lineSpacing; float lineWidth = 0; @@ -657,36 +655,16 @@ struct TextEditor::Iterator return roundToInt (maxWidth); } - std::vector> getLineRanges() + Rectangle getTextBounds (Range range) const { - std::vector> ranges; + auto startX = indexToX (range.getStart()); + auto endX = indexToX (range.getEnd()); - int index = currentLineIndex; - Range currentLineRange; - - while (next()) - { - if (index < currentLineIndex) - { - currentLineRange.setEnd (indexInText - 1); - ranges.push_back (currentLineRange); - - currentLineRange = { indexInText, indexInText }; - index = currentLineIndex; - } - } - - currentLineRange.setEnd (atom != nullptr ? indexInText + atom->numChars - : indexInText); - - if (! currentLineRange.isEmpty()) - ranges.push_back (currentLineRange); - - return ranges; + return Rectangle (startX, lineY, endX - startX, lineHeight * lineSpacing).toNearestInt(); } //============================================================================== - int indexInText = 0, currentLineIndex = 0; + int indexInText = 0; float lineY = 0, lineHeight = 0, maxDescent = 0; float atomX = 0, atomRight = 0; const TextAtom* atom = nullptr; @@ -730,14 +708,9 @@ private: if (shouldStartNewLine) { if (split == numRemaining) - { beginNewLine(); - } else - { - ++currentLineIndex; lineY += lineHeight * lineSpacing; - } } atomRight = atomX + longAtom.width; @@ -1490,23 +1463,14 @@ Point TextEditor::getTextOffset() const noexcept RectangleList TextEditor::getTextBounds (Range textRange) { RectangleList boundingBox; + Iterator i (*this); - for (auto lineRange : Iterator { *this }.getLineRanges()) + while (i.next()) { - auto intersection = lineRange.getIntersectionWith (textRange); - - if (! intersection.isEmpty()) + if (textRange.intersects ({ i.indexInText, + i.indexInText + i.atom->numChars })) { - Point anchorStart, anchorEnd; - float lineHeight = 0.0f; - - getCharPosition (intersection.getStart(), anchorStart, lineHeight); - getCharPosition (intersection.getEnd(), anchorEnd, lineHeight); - - boundingBox.add (Rectangle (anchorStart.x, anchorStart.y, anchorEnd.x - anchorStart.x, lineHeight).toNearestInt()); - - if (intersection == textRange) - break; + boundingBox.add (i.getTextBounds (textRange)); } }