slightly reduce amount of work needed when painting text or rectangles

This commit is contained in:
hippoz 2022-06-15 18:26:07 +03:00
parent b37e587808
commit 2c15569339
Signed by: hippoz
GPG key ID: 7C52899193467641
2 changed files with 8 additions and 7 deletions

View file

@ -7,6 +7,11 @@
namespace Raven { namespace Raven {
void Painter::rounded_rectangle(Box &geometry, double border_radius) { 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 aspect = 1.0;
double radius = border_radius / aspect; double radius = border_radius / aspect;
double degrees = M_PI / 180.0; 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) }; 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()); PangoLayout *layout = pango_cairo_create_layout(m_cairo->cobj());
int font_width; int font_width;
int font_height; int font_height;
int pango_width;
int pango_height;
pango_layout_set_font_description(layout, pango_font_description); pango_layout_set_font_description(layout, pango_font_description);
if (geometry.width() > 0) 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_set_text(layout, text.c_str(), -1);
pango_layout_get_pixel_size(layout, &font_width, &font_height); pango_layout_get_pixel_size(layout, &font_width, &font_height);
pango_layout_get_size(layout, &pango_width, &pango_height);
double x = 0; double x = 0;
double y = ((geometry.height() - font_height) / 2); 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); 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); pango_cairo_show_layout(m_cairo->cobj(), layout);
g_object_unref(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) { void Painter::source_rgb(RGB source_rgb) {

View file

@ -25,7 +25,7 @@ public:
void rounded_rectangle(Box &geometry, double border_radius); void rounded_rectangle(Box &geometry, double border_radius);
Point compute_text_size(Box &widget_geometry, std::string &text, PangoFontDescription *pango_font_description); 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; } bool can_paint() { if (m_cairo) return true; else return false; }