From 6a53ddcb77f7568e3d37113325d8c3d993fe7c9d Mon Sep 17 00:00:00 2001 From: attila Date: Wed, 23 Apr 2025 12:28:11 +0200 Subject: [PATCH] TextEditor: Restore JUCE 7 behaviour for the first line's alignment when line spacing > 1 --- .../widgets/juce_TextEditor.cpp | 18 +++++++++++++++++- .../widgets/juce_TextEditorModel.cpp | 7 +++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/modules/juce_gui_basics/widgets/juce_TextEditor.cpp b/modules/juce_gui_basics/widgets/juce_TextEditor.cpp index 3c75a822c4..1414c1383e 100644 --- a/modules/juce_gui_basics/widgets/juce_TextEditor.cpp +++ b/modules/juce_gui_basics/widgets/juce_TextEditor.cpp @@ -817,8 +817,24 @@ float TextEditor::getYOffset() const { const auto bottomY = getMaximumTextHeight(); + const auto juce7LineSpacingOffset = std::invoke ([&] + { + if (approximatelyEqual (lineSpacing, 1.0f) || textStorage->isEmpty()) + return 0.0f; + + const auto& lineMetrics = textStorage->front().value->getShapedText().getLineMetricsForGlyphRange(); + + if (lineMetrics.isEmpty()) + return 0.0f; + + const auto& line = lineMetrics.front().value; + + jassert (lineSpacing >= 1.0f); + return line.maxAscent * (1.0f / lineSpacing - 1.0f); + }); + if (justification.testFlags (Justification::top) || isTextStorageHeightGreaterEqualThan ((float) bottomY)) - return 0; + return juce7LineSpacingOffset; auto bottom = jmax (0.0f, (float) bottomY - getTextStorageHeight()); diff --git a/modules/juce_gui_basics/widgets/juce_TextEditorModel.cpp b/modules/juce_gui_basics/widgets/juce_TextEditorModel.cpp index 78c77af74e..2186042db1 100644 --- a/modules/juce_gui_basics/widgets/juce_TextEditorModel.cpp +++ b/modules/juce_gui_basics/widgets/juce_TextEditorModel.cpp @@ -202,6 +202,12 @@ public: bool isEmpty() const { return storage.empty(); } + ParagraphItem front() const + { + jassert (! ranges.isEmpty()); + return { ranges.get (0), storage.front() }; + } + ParagraphItem back() const { jassert (! ranges.isEmpty()); @@ -417,6 +423,7 @@ public: auto end() const { return paragraphs.end(); } auto isEmpty() const { return paragraphs.isEmpty(); } + auto front() const { return paragraphs.front(); } auto back() const { return paragraphs.back(); } std::optional getLastFont() const