From 2c15569339a6e0ce248c09be7bd316a2291db873 Mon Sep 17 00:00:00 2001 From: hippoz <10706925-hippoz@users.noreply.gitlab.com> Date: Wed, 15 Jun 2022 18:26:07 +0300 Subject: [PATCH] slightly reduce amount of work needed when painting text or rectangles --- src/Painter.cpp | 13 +++++++------ src/Painter.hpp | 2 +- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/Painter.cpp b/src/Painter.cpp index bd3d66b..8af132b 100644 --- a/src/Painter.cpp +++ b/src/Painter.cpp @@ -7,6 +7,11 @@ namespace Raven { void Painter::rounded_rectangle(Box &geometry, double border_radius) { + if (border_radius == 0.0) { + m_cairo->rectangle(geometry.x(), geometry.y(), geometry.width(), geometry.height()); + return; + } + double aspect = 1.0; double radius = border_radius / aspect; double degrees = M_PI / 180.0; @@ -43,13 +48,11 @@ Point Painter::compute_text_size(Box &widget_geometry, std::string &text, PangoF 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, PangoFontDescription *pango_font_description) { +void 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; int font_height; - int pango_width; - int pango_height; pango_layout_set_font_description(layout, pango_font_description); if (geometry.width() > 0) @@ -60,7 +63,6 @@ Point Painter::text(Box &geometry, std::string &text, PaintTextAlign align, Pang pango_layout_set_text(layout, text.c_str(), -1); pango_layout_get_pixel_size(layout, &font_width, &font_height); - pango_layout_get_size(layout, &pango_width, &pango_height); double x = 0; double y = ((geometry.height() - font_height) / 2); @@ -69,12 +71,11 @@ Point Painter::text(Box &geometry, std::string &text, PaintTextAlign align, Pang x = ((geometry.width() - font_width) / 2); } - m_cairo->move_to(x, y); + m_cairo->move_to(std::floor(x), std::floor(y)); pango_cairo_show_layout(m_cairo->cobj(), layout); g_object_unref(layout); - return {pango_units_to_double(pango_width), pango_units_to_double(pango_height)}; } void Painter::source_rgb(RGB source_rgb) { diff --git a/src/Painter.hpp b/src/Painter.hpp index 5b94704..b862104 100644 --- a/src/Painter.hpp +++ b/src/Painter.hpp @@ -25,7 +25,7 @@ public: void rounded_rectangle(Box &geometry, double border_radius); 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); + void 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; }