https://gcc.gnu.org/g:52f07783f7c238e39a83fed66e9a9749b816146c
commit r17-5947-g52f07783f7c238e39a83fed66e9a9749b816146c Author: David Malcolm <[email protected]> Date: Wed Nov 4 10:43:58 2026 -0500 text_art: support newlines in text_widget Properly handle newlines in text_widget by splitting the input into lines. I have a followup patch which uses this to put the results of program_point::print_source_line into a tree widget. gcc/ChangeLog: * text-art/types.h (styled_string::styled_string): New ctor from std::vector<styled_unichar> * text-art/widget.cc (text_widget::text_widget): Move here from header. Reimplement to support multiple lines. (text_widget::calc_req_size): Reimplement to support multiple lines. (text_widget::paint_to_canvas): Likewise. (selftest::test_multiline_text_widget): New selftest. (selftest::text_art_widget_cc_tests): Call it. * text-art/widget.h (text_widget::text_widget): Move ctor to widget.cc and reimplement. (text_widget::m_str): Drop field. (text_widget::m_lines): New field. (text_widget::m_max_line_width): New field. Signed-off-by: David Malcolm <[email protected]> Diff: --- gcc/text-art/types.h | 5 ++++ gcc/text-art/widget.cc | 66 ++++++++++++++++++++++++++++++++++++++++++++++++-- gcc/text-art/widget.h | 8 +++--- 3 files changed, 72 insertions(+), 7 deletions(-) diff --git a/gcc/text-art/types.h b/gcc/text-art/types.h index a31184146129..34842ea6b2e0 100644 --- a/gcc/text-art/types.h +++ b/gcc/text-art/types.h @@ -433,6 +433,11 @@ class styled_string explicit styled_string (style_manager &sm, const char *str); explicit styled_string (cppchar_t cppchar, bool emoji = false); + explicit styled_string (std::vector<styled_unichar> chars) + : m_chars (std::move (chars)) + { + } + styled_string (styled_string &&) = default; styled_string &operator= (styled_string &&) = default; diff --git a/gcc/text-art/widget.cc b/gcc/text-art/widget.cc index 63f6daa30651..59e076ec6ab1 100644 --- a/gcc/text-art/widget.cc +++ b/gcc/text-art/widget.cc @@ -85,6 +85,34 @@ vbox_widget::update_child_alloc_rects () /* class text_art::text_widget : public text_art::leaf_widget. */ +text_widget::text_widget (styled_string str) +: m_max_line_width (0) +{ + // Split STR into multiple styled strings, one per line. + std::vector<styled_unichar> current_line; + int current_line_width = 0; + for (auto ch : str) + { + if (ch.get_code () == '\n') + { + m_max_line_width = std::max (m_max_line_width, current_line_width); + m_lines.push_back (styled_string (std::move (current_line))); + current_line.clear (); + current_line_width = 0; + } + else + { + current_line.push_back (ch); + current_line_width += ch.get_canvas_width (); + } + } + if (!current_line.empty ()) + { + m_max_line_width = std::max (m_max_line_width, current_line_width); + m_lines.push_back (styled_string (std::move (current_line))); + } +} + const char * text_widget::get_desc () const { @@ -94,13 +122,18 @@ text_widget::get_desc () const canvas::size_t text_widget::calc_req_size () { - return canvas::size_t (m_str.size (), 1); + return canvas::size_t (m_max_line_width, m_lines.size ()); } void text_widget::paint_to_canvas (canvas &canvas) { - canvas.paint_text (get_top_left (), m_str); + canvas::coord_t line_xy = get_top_left (); + for (auto &line : m_lines) + { + canvas.paint_text (line_xy, line); + ++line_xy.y; + } } /* class text_art::canvas_widget : public text_art::leaf_widget. */ @@ -186,6 +219,34 @@ test_text_widget () ("hello world\n")); } +static void +test_multiline_text_widget () +{ + style_manager sm; + + // Without trailing newline: + { + text_widget w (styled_string (sm, "hello\n world")); + ASSERT_EQ (w.get_req_size ().w, 6); + ASSERT_EQ (w.get_req_size ().h, 2); + canvas c (w.to_canvas (sm)); + ASSERT_CANVAS_STREQ + (c, false, + ("hello\n world\n")); + } + + // With trailing newline: + { + text_widget w (styled_string (sm, "hello\n world\n")); + ASSERT_EQ (w.get_req_size ().w, 6); + ASSERT_EQ (w.get_req_size ().h, 2); + canvas c (w.to_canvas (sm)); + ASSERT_CANVAS_STREQ + (c, false, + ("hello\n world\n")); + } +} + static void test_wrapper_widget () { @@ -271,6 +332,7 @@ text_art_widget_cc_tests () { test_test_widget (); test_text_widget (); + test_multiline_text_widget (); test_wrapper_widget (); test_empty_wrapper_widget (); test_vbox_1 (); diff --git a/gcc/text-art/widget.h b/gcc/text-art/widget.h index ff527fffc169..c0d23db3b86c 100644 --- a/gcc/text-art/widget.h +++ b/gcc/text-art/widget.h @@ -214,17 +214,15 @@ class leaf_widget : public widget class text_widget : public leaf_widget { public: - text_widget (styled_string str) - : leaf_widget (), m_str (std::move (str)) - { - } + text_widget (styled_string str); const char *get_desc () const override; canvas::size_t calc_req_size () final override; void paint_to_canvas (canvas &canvas) final override; private: - styled_string m_str; + std::vector<styled_string> m_lines; + int m_max_line_width; }; /* Concrete widget subclass for a pre-rendered canvas. */
