From 09c7f4fa5bdbe7b631d33728d7a8a78df2058b72 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Thu, 29 May 2025 12:39:20 -0700 Subject: [PATCH] [Impeller] libImpeller: Allow setting ellipses. (#169610) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes https://github.com/flutter/flutter/issues/168750 Screenshot 2025-05-28 at 11 41 24 AM --- .../impeller/toolkit/interop/impeller.cc | 6 +++++ .../impeller/toolkit/interop/impeller.h | 12 +++++++++ .../impeller/toolkit/interop/impeller.hpp | 9 +++++++ .../toolkit/interop/impeller_unittests.cc | 25 +++++++++++++++++++ .../toolkit/interop/paragraph_style.cc | 10 ++++++++ .../toolkit/interop/paragraph_style.h | 2 ++ 6 files changed, 64 insertions(+) diff --git a/engine/src/flutter/impeller/toolkit/interop/impeller.cc b/engine/src/flutter/impeller/toolkit/interop/impeller.cc index 3c75dd0b3fb..ea54089bee0 100644 --- a/engine/src/flutter/impeller/toolkit/interop/impeller.cc +++ b/engine/src/flutter/impeller/toolkit/interop/impeller.cc @@ -1159,6 +1159,12 @@ void ImpellerParagraphStyleSetLocale(ImpellerParagraphStyle paragraph_style, GetPeer(paragraph_style)->SetLocale(ReadString(locale)); } +IMPELLER_EXTERN_C +void ImpellerParagraphStyleSetEllipsis(ImpellerParagraphStyle paragraph_style, + const char* ellipsis) { + GetPeer(paragraph_style)->SetEllipsis(ReadString(ellipsis)); +} + IMPELLER_EXTERN_C void ImpellerDisplayListBuilderDrawParagraph(ImpellerDisplayListBuilder builder, ImpellerParagraph paragraph, diff --git a/engine/src/flutter/impeller/toolkit/interop/impeller.h b/engine/src/flutter/impeller/toolkit/interop/impeller.h index 4353fc599a8..78f47645ae2 100644 --- a/engine/src/flutter/impeller/toolkit/interop/impeller.h +++ b/engine/src/flutter/impeller/toolkit/interop/impeller.h @@ -2472,6 +2472,18 @@ void ImpellerParagraphStyleSetLocale( ImpellerParagraphStyle IMPELLER_NONNULL paragraph_style, const char* IMPELLER_NONNULL locale); +//------------------------------------------------------------------------------ +/// @brief Set the UTF-8 string to use as the ellipsis. Pass `nullptr` to +/// clear the setting to default. +/// +/// @param[in] paragraph_style The paragraph style. +/// @param[in] data The ellipsis string UTF-8 data, or null. +/// +IMPELLER_EXPORT +void ImpellerParagraphStyleSetEllipsis( + ImpellerParagraphStyle IMPELLER_NONNULL paragraph_style, + const char* IMPELLER_NULLABLE ellipsis); + //------------------------------------------------------------------------------ // Paragraph Builder //------------------------------------------------------------------------------ diff --git a/engine/src/flutter/impeller/toolkit/interop/impeller.hpp b/engine/src/flutter/impeller/toolkit/interop/impeller.hpp index a5dd4cc40d4..7085671911c 100644 --- a/engine/src/flutter/impeller/toolkit/interop/impeller.hpp +++ b/engine/src/flutter/impeller/toolkit/interop/impeller.hpp @@ -168,6 +168,7 @@ struct Proc { PROC(ImpellerParagraphStyleRelease) \ PROC(ImpellerParagraphStyleRetain) \ PROC(ImpellerParagraphStyleSetBackground) \ + PROC(ImpellerParagraphStyleSetEllipsis) \ PROC(ImpellerParagraphStyleSetFontFamily) \ PROC(ImpellerParagraphStyleSetFontSize) \ PROC(ImpellerParagraphStyleSetFontStyle) \ @@ -1088,6 +1089,14 @@ class ParagraphStyle return *this; } + //---------------------------------------------------------------------------- + /// @see ImpellerParagraphStyleSetEllipsis + /// + ParagraphStyle& SetEllipsis(const char* ellipsis) { + gGlobalProcTable.ImpellerParagraphStyleSetEllipsis(Get(), ellipsis); + return *this; + } + //---------------------------------------------------------------------------- /// @see ImpellerParagraphStyleSetMaxLines /// diff --git a/engine/src/flutter/impeller/toolkit/interop/impeller_unittests.cc b/engine/src/flutter/impeller/toolkit/interop/impeller_unittests.cc index 8e36a18f499..48812e71385 100644 --- a/engine/src/flutter/impeller/toolkit/interop/impeller_unittests.cc +++ b/engine/src/flutter/impeller/toolkit/interop/impeller_unittests.cc @@ -631,4 +631,29 @@ TEST_P(InteropPlaygroundTest, CanGetPathBounds) { ASSERT_EQ(bounds.height, 100); } +TEST_P(InteropPlaygroundTest, CanControlEllipses) { + hpp::TypographyContext context; + auto style = hpp::ParagraphStyle{}; + style.SetFontSize(50); + style.SetForeground(hpp::Paint{}.SetColor({.red = 1.0, .alpha = 1.0})); + const auto text = std::string{"The quick brown fox jumped over the lazy dog"}; + style.SetEllipsis("🐶"); + auto para1 = + hpp::ParagraphBuilder{context}.PushStyle(style).AddText(text).Build(250); + style.SetForeground(hpp::Paint{}.SetColor({.green = 1.0, .alpha = 1.0})); + style.SetEllipsis(nullptr); + auto para2 = + hpp::ParagraphBuilder{context}.PushStyle(style).AddText(text).Build(250); + auto dl = hpp::DisplayListBuilder{} + .DrawParagraph(para1, {100, 100}) + .DrawParagraph(para2, {100, 200}) + .Build(); + ASSERT_TRUE( + OpenPlaygroundHere([&](const auto& context, const auto& surface) -> bool { + hpp::Surface window(surface.GetC()); + window.Draw(dl); + return true; + })); +} + } // namespace impeller::interop::testing diff --git a/engine/src/flutter/impeller/toolkit/interop/paragraph_style.cc b/engine/src/flutter/impeller/toolkit/interop/paragraph_style.cc index 25982bf27c5..780d6e03fc9 100644 --- a/engine/src/flutter/impeller/toolkit/interop/paragraph_style.cc +++ b/engine/src/flutter/impeller/toolkit/interop/paragraph_style.cc @@ -4,6 +4,8 @@ #include "impeller/toolkit/interop/paragraph_style.h" +#include "flutter/fml/string_conversion.h" + namespace impeller::interop { ParagraphStyle::ParagraphStyle() = default; @@ -84,4 +86,12 @@ void ParagraphStyle::SetTextDecoration( decoration_ = decoration; } +void ParagraphStyle::SetEllipsis(const std::string& string) { + if (string.empty()) { + style_.ellipsis = {}; + return; + } + style_.ellipsis = fml::Utf8ToUtf16(string); +} + } // namespace impeller::interop diff --git a/engine/src/flutter/impeller/toolkit/interop/paragraph_style.h b/engine/src/flutter/impeller/toolkit/interop/paragraph_style.h index 26e33d18558..d3bab1f0602 100644 --- a/engine/src/flutter/impeller/toolkit/interop/paragraph_style.h +++ b/engine/src/flutter/impeller/toolkit/interop/paragraph_style.h @@ -48,6 +48,8 @@ class ParagraphStyle final void SetLocale(std::string locale); + void SetEllipsis(const std::string& string); + txt::TextStyle CreateTextStyle() const; const txt::ParagraphStyle& GetParagraphStyle() const;