From 992ef1e3d608c4658e798c172fd1a17af0c104fd Mon Sep 17 00:00:00 2001 From: hippoz <10706925-hippoz@users.noreply.gitlab.com> Date: Tue, 29 Mar 2022 09:27:59 +0300 Subject: [PATCH] handle text bounds using pango --- src/Button.cpp | 3 ++- src/Label.cpp | 3 ++- src/Painter.cpp | 7 ++++++- src/Painter.hpp | 2 +- src/main.cpp | 2 +- 5 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/Button.cpp b/src/Button.cpp index 6c55852..4a67742 100644 --- a/src/Button.cpp +++ b/src/Button.cpp @@ -3,6 +3,7 @@ #include "Box.hpp" #include "Window.hpp" #include "Painter.hpp" +#include "pango/pango-layout.h" #include namespace Raven { @@ -30,7 +31,7 @@ void Button::on_paint() { auto text_color = get_styles()->get_button_text_color(); painter.source_rgb(text_color); painter.set_pango_font_description(get_styles()->get_controls_font_description()); - painter.text(get_current_geometry(), m_text, PaintTextAlign::Center); + painter.text(get_current_geometry(), m_text, PaintTextAlign::Center, PANGO_ELLIPSIZE_END); painter.fill(); } diff --git a/src/Label.cpp b/src/Label.cpp index e35b784..dbd7254 100644 --- a/src/Label.cpp +++ b/src/Label.cpp @@ -1,5 +1,6 @@ #include "Label.hpp" #include "Window.hpp" +#include "pango/pango-layout.h" namespace Raven { @@ -18,7 +19,7 @@ void Label::on_paint() { painter.source_rgb(text_color); painter.set_pango_font_description(get_styles()->get_controls_font_description()); - painter.text(get_current_geometry(), m_text, PaintTextAlign::Center); + painter.text(get_current_geometry(), m_text, PaintTextAlign::Center, PANGO_ELLIPSIZE_NONE); painter.fill(); } diff --git a/src/Painter.cpp b/src/Painter.cpp index 9a6290a..e4265f1 100644 --- a/src/Painter.cpp +++ b/src/Painter.cpp @@ -1,5 +1,7 @@ #include "Painter.hpp" #include "RGB.hpp" +#include "pango/pango-layout.h" +#include "pango/pango-types.h" namespace Raven { @@ -37,7 +39,7 @@ bool Painter::text(Point &where, std::string &text) { return true; } -bool Painter::text(Box &geometry, std::string &text, PaintTextAlign align) { +bool Painter::text(Box &geometry, std::string &text, PaintTextAlign align, PangoEllipsizeMode ellipsize) { if (m_pango_font_description == nullptr) return false; @@ -47,6 +49,9 @@ bool Painter::text(Box &geometry, std::string &text, PaintTextAlign align) { int font_height; pango_layout_set_font_description(layout, m_pango_font_description); + pango_layout_set_width(layout, pango_units_from_double(geometry.get_width())); + pango_layout_set_height(layout, pango_units_from_double(geometry.get_height())); + pango_layout_set_ellipsize(layout, ellipsize); pango_layout_set_text(layout, text.c_str(), -1); pango_layout_get_pixel_size(layout, &font_width, &font_height); diff --git a/src/Painter.hpp b/src/Painter.hpp index 5c8c307..03819d0 100644 --- a/src/Painter.hpp +++ b/src/Painter.hpp @@ -29,7 +29,7 @@ public: void rounded_rectangle(Box &geometry, double border_radius); bool text(Point &where, std::string &text); - bool text(Box &geometry, std::string &text, PaintTextAlign align); + bool text(Box &geometry, std::string &text, PaintTextAlign align, PangoEllipsizeMode ellipsize); void source_rgb(RGB &source_rgb); void fill(); diff --git a/src/main.cpp b/src/main.cpp index 4ba1994..9bc0d9c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,7 +10,7 @@ int main() { Raven::Window window {}; Raven::Widget main_widget {}; - Raven::Button button {"click me"}; + Raven::Button button {"click meeeeeeeeeeee"}; Raven::Label label {"click it!"}; window.spawn_window();