Allow setting text to completely empty. Fix for issue caught by Pocketwatch where setting all the runs to empty strings results in no changes to the displayed text. We now handle this gracefully and update dimensions and paths to render accordingly. Diffs= c6b867df9 Allow setting text to completely empty. (#5924) Co-authored-by: Luigi Rosso <luigi-rosso@users.noreply.github.com>
diff --git a/.rive_head b/.rive_head index a2ac71a..9c0ace2 100644 --- a/.rive_head +++ b/.rive_head
@@ -1 +1 @@ -06a1872884b3b7cf2be58673e5f5246c57c3d39c +c6b867df9f26298c1990143a26e495f56aa2ec61
diff --git a/include/rive/simple_array.hpp b/include/rive/simple_array.hpp index 25ef8dd..110c46f 100644 --- a/include/rive/simple_array.hpp +++ b/include/rive/simple_array.hpp
@@ -224,13 +224,13 @@ T* m_write; }; -template <typename T> -SimpleArray<T>::SimpleArray(SimpleArrayBuilder<T>&& other) : m_size(other.size()) +template <typename T> SimpleArray<T>::SimpleArray(SimpleArrayBuilder<T>&& other) { // Bring the capacity down to the actual size (this should keep the same // ptr, but that's not guaranteed, so we copy the ptr after the realloc). other.resize(other.size()); m_ptr = other.m_ptr; + m_size = other.m_size; other.m_ptr = nullptr; other.m_size = 0; }
diff --git a/src/text/text.cpp b/src/text/text.cpp index b2bb540..089a865 100644 --- a/src/text/text.cpp +++ b/src/text/text.cpp
@@ -232,13 +232,17 @@ void Text::buildRenderStyles() { - const float paragraphSpace = paragraphSpacing(); - for (TextStyle* style : m_renderStyles) { style->rewindPath(); } m_renderStyles.clear(); + if (m_shape.size() == 0) + { + m_bounds = AABB(0.0f, 0.0f, 0.0f, 0.0f); + return; + } + const float paragraphSpace = paragraphSpacing(); // Build up ordered runs as we go. int paragraphIndex = 0; @@ -684,6 +688,12 @@ } } } + else + { + m_shape = SimpleArray<Paragraph>(); + m_lines = SimpleArray<SimpleArray<GlyphLine>>(); + m_glyphLookup.clear(); + } m_orderedLines.clear(); m_ellipsisRun = {};
diff --git a/test/font_test.cpp b/test/font_test.cpp index 2bdfce0..8c5ac0b 100644 --- a/test/font_test.cpp +++ b/test/font_test.cpp
@@ -69,6 +69,8 @@ auto paragraphs = font->shapeText(unichars, truns); REQUIRE(paragraphs.size() == 1); + paragraphs = SimpleArray<Paragraph>(); + REQUIRE(paragraphs.size() == 0); fallbackFonts.clear(); Font::gFallbackProc = nullptr; }
diff --git a/test/text_test.cpp b/test/text_test.cpp index 237baf0..6e7298a 100644 --- a/test/text_test.cpp +++ b/test/text_test.cpp
@@ -41,10 +41,50 @@ auto runObjects = artboard->find<rive::TextValueRun>(); REQUIRE(runObjects.size() == 1); + REQUIRE(runObjects[0]->text() == "Hello World!"); + + rive::NoOpRenderer renderer; artboard->advance(0.0f); - rive::NoOpRenderer renderer; artboard->draw(&renderer); + { + REQUIRE(textObjects[0]->shape().size() == 1); + const rive::Paragraph& paragraph = textObjects[0]->shape()[0]; + REQUIRE(paragraph.runs.size() == 1); + REQUIRE(paragraph.runs[0].glyphs.size() == 12); + } + + // Changing to "Just Hello" works. + runObjects[0]->text("Just Hello"); + artboard->advance(0.0f); + artboard->draw(&renderer); + { + REQUIRE(textObjects[0]->shape().size() == 1); + const rive::Paragraph& paragraph = textObjects[0]->shape()[0]; + REQUIRE(paragraph.runs.size() == 1); + REQUIRE(paragraph.runs[0].glyphs.size() == 10); + } + + // Changing to an empty space " " works. + runObjects[0]->text(" "); + artboard->advance(0.0f); + artboard->draw(&renderer); + { + REQUIRE(textObjects[0]->shape().size() == 1); + const rive::Paragraph& paragraph = textObjects[0]->shape()[0]; + REQUIRE(paragraph.runs.size() == 1); + REQUIRE(paragraph.runs[0].glyphs.size() == 1); + } + + // Changing to completely empty works. + runObjects[0]->text(""); + artboard->advance(0.0f); + artboard->draw(&renderer); + { + REQUIRE(textObjects[0]->shape().size() == 0); + REQUIRE(textObjects[0]->localBounds().width() == 0.0f); + REQUIRE(textObjects[0]->localBounds().height() == 0.0f); + } } TEST_CASE("ellipsis is shown", "[text]")