From 69d8e0a6894457fdb6c1c4b28b70755b84c0e555 Mon Sep 17 00:00:00 2001 From: hippoz <10706925-hippoz@users.noreply.gitlab.com> Date: Sat, 11 Jun 2022 16:19:02 +0300 Subject: [PATCH] tidy: tidy Painter's text() and compute_text_size() methods --- src/Button.cpp | 3 +-- src/Label.cpp | 4 ++-- src/Painter.cpp | 18 ++++++------------ src/Painter.hpp | 8 ++------ src/Widget.cpp | 4 ++-- 5 files changed, 13 insertions(+), 24 deletions(-) diff --git a/src/Button.cpp b/src/Button.cpp index 999a528..6eec3c4 100644 --- a/src/Button.cpp +++ b/src/Button.cpp @@ -27,8 +27,7 @@ void Button::on_paint() { auto painter = window()->painter(); painter.source_rgb(style()->foreground()); - painter.set_pango_font_description(style()->font_description()); - painter.text(rect(), m_text, PaintTextAlign::Center, PANGO_ELLIPSIZE_END, true); + painter.text(rect(), m_text, PaintTextAlign::Center, PANGO_ELLIPSIZE_END, style()->font_description()); painter.fill(); } diff --git a/src/Label.cpp b/src/Label.cpp index 26ec22e..49887dc 100644 --- a/src/Label.cpp +++ b/src/Label.cpp @@ -13,6 +13,7 @@ void Label::set_text(std::string text) { fit_text(text); } } + void Label::on_init() { set_style(&default_label_style); fit_text(m_text); @@ -23,8 +24,7 @@ void Label::on_paint() { auto painter = window()->painter(); painter.source_rgb(style()->foreground()); - painter.set_pango_font_description(style()->font_description()); - painter.text(rect(), m_text, PaintTextAlign::Left, PANGO_ELLIPSIZE_NONE, true); + painter.text(rect(), m_text, PaintTextAlign::Left, PANGO_ELLIPSIZE_NONE, style()->font_description()); painter.fill(); } diff --git a/src/Painter.cpp b/src/Painter.cpp index d21879b..bd3d66b 100644 --- a/src/Painter.cpp +++ b/src/Painter.cpp @@ -23,16 +23,13 @@ void Painter::rounded_rectangle(Box &geometry, double border_radius) { m_cairo->close_path(); } -Point Painter::compute_text_size(Box &widget_geometry, std::string &text) { - if (m_pango_font_description == nullptr) - return {-1, -1}; - +Point Painter::compute_text_size(Box &widget_geometry, std::string &text, PangoFontDescription *pango_font_description) { PangoLayout *layout = pango_cairo_create_layout(m_cairo->cobj()); int font_width; int font_height; - pango_layout_set_font_description(layout, m_pango_font_description); + pango_layout_set_font_description(layout, pango_font_description); if (widget_geometry.max_width() > 0) pango_layout_set_width(layout, pango_units_from_double(widget_geometry.max_width())); if (widget_geometry.max_height() > 0) @@ -46,10 +43,7 @@ Point Painter::compute_text_size(Box &widget_geometry, std::string &text) { return { pango_units_to_double(font_width), pango_units_to_double(font_height) }; } -Point Painter::text(Box &geometry, std::string &text, PaintTextAlign align, PangoEllipsizeMode ellipsize, bool set_size) { - if (m_pango_font_description == nullptr) - return {-1,-1}; - +Point Painter::text(Box &geometry, std::string &text, PaintTextAlign align, PangoEllipsizeMode ellipsize, PangoFontDescription *pango_font_description) { PangoLayout *layout = pango_cairo_create_layout(m_cairo->cobj()); int font_width; @@ -57,10 +51,10 @@ Point Painter::text(Box &geometry, std::string &text, PaintTextAlign align, Pang int pango_width; int pango_height; - pango_layout_set_font_description(layout, m_pango_font_description); - if (geometry.width() > 0 && set_size) + pango_layout_set_font_description(layout, pango_font_description); + if (geometry.width() > 0) pango_layout_set_width(layout, pango_units_from_double(geometry.width())); - if (geometry.height() > 0 && set_size) + if (geometry.height() > 0) pango_layout_set_height(layout, pango_units_from_double(geometry.height())); pango_layout_set_ellipsize(layout, ellipsize); pango_layout_set_text(layout, text.c_str(), -1); diff --git a/src/Painter.hpp b/src/Painter.hpp index 880dbcf..5b94704 100644 --- a/src/Painter.hpp +++ b/src/Painter.hpp @@ -17,19 +17,15 @@ enum class PaintTextAlign { class Painter { private: Cairo::RefPtr m_cairo { nullptr }; - PangoFontDescription *m_pango_font_description { nullptr }; public: Painter() {} Cairo::RefPtr cairo() { return m_cairo; } void set_cairo(Cairo::RefPtr cairo) { m_cairo = cairo; } - void set_pango_font_description(PangoFontDescription *pango_font_description) { m_pango_font_description = pango_font_description; } - PangoFontDescription *pango_font_description() { return m_pango_font_description; } - void rounded_rectangle(Box &geometry, double border_radius); - Point text(Box &geometry, std::string &text, PaintTextAlign align, PangoEllipsizeMode ellipsize, bool set_size); - Point compute_text_size(Box &widget_geometry, std::string &text); + Point compute_text_size(Box &widget_geometry, std::string &text, PangoFontDescription *pango_font_description); + Point text(Box &geometry, std::string &text, PaintTextAlign align, PangoEllipsizeMode ellipsize, PangoFontDescription *pango_font_description); bool can_paint() { if (m_cairo) return true; else return false; } diff --git a/src/Widget.cpp b/src/Widget.cpp index c92c396..d81f562 100644 --- a/src/Widget.cpp +++ b/src/Widget.cpp @@ -21,8 +21,8 @@ void Widget::fit_text(std::string &text) { if (!window()) return; - window()->painter().set_pango_font_description(m_style->font_description()); - auto size = window()->painter().compute_text_size(rect(), text); + auto size = window()->painter().compute_text_size(rect(), text, style()->font_description()); + // always reflow, even if resize doesn't if (!resize(size)) { reflow(); }