raven/src/Painter.cpp

99 lines
2.9 KiB
C++
Raw Normal View History

#include "Painter.hpp"
2022-03-18 03:22:30 +02:00
#include "RGB.hpp"
2022-03-29 09:27:59 +03:00
#include "pango/pango-layout.h"
#include "pango/pango-types.h"
namespace Raven {
void Painter::rounded_rectangle(Box &geometry, double border_radius) {
double aspect = 1.0;
double radius = border_radius / aspect;
double degrees = M_PI / 180.0;
2022-04-30 09:40:09 +03:00
double x = geometry.x();
double y = geometry.y();
double w = geometry.width();
double h = geometry.height();
m_cairo->begin_new_sub_path();
m_cairo->arc(x + w - radius, y + radius, radius, -90 * degrees, 0 * degrees);
m_cairo->arc(x + w - radius, y + h - radius, radius, 0 * degrees, 90 * degrees);
m_cairo->arc(x + radius, y + h - radius, radius, 90 * degrees, 180 * degrees);
m_cairo->arc(x + radius, y + radius, radius, 180 * degrees, 270 * degrees);
m_cairo->close_path();
}
2022-03-18 02:49:16 +02:00
bool Painter::text(Point &where, std::string &text) {
if (m_pango_font_description == nullptr)
return false;
PangoLayout *layout = pango_cairo_create_layout(m_cairo->cobj());
pango_layout_set_font_description(layout, m_pango_font_description);
pango_layout_set_text(layout, text.c_str(), -1);
2022-04-30 09:40:09 +03:00
m_cairo->move_to(where.x(), where.y());
2022-03-18 02:49:16 +02:00
pango_cairo_show_layout(m_cairo->cobj(), layout);
g_object_unref(layout);
return true;
}
2022-05-10 21:14:55 +03:00
Point Painter::text(Box &geometry, std::string &text, PaintTextAlign align, PangoEllipsizeMode ellipsize, bool set_size) {
2022-03-18 02:49:16 +02:00
if (m_pango_font_description == nullptr)
2022-05-08 19:31:31 +03:00
return {-1,-1};
2022-03-18 02:49:16 +02:00
PangoLayout *layout = pango_cairo_create_layout(m_cairo->cobj());
int font_width;
int font_height;
2022-05-08 19:31:31 +03:00
int pango_width;
int pango_height;
2022-03-18 02:49:16 +02:00
pango_layout_set_font_description(layout, m_pango_font_description);
2022-05-10 21:14:55 +03:00
if (geometry.width() > 0 && set_size)
2022-05-08 19:31:31 +03:00
pango_layout_set_width(layout, pango_units_from_double(geometry.width()));
2022-05-10 21:14:55 +03:00
if (geometry.height() > 0 && set_size)
2022-05-08 19:31:31 +03:00
pango_layout_set_height(layout, pango_units_from_double(geometry.height()));
2022-03-29 09:27:59 +03:00
pango_layout_set_ellipsize(layout, ellipsize);
2022-03-18 02:49:16 +02:00
pango_layout_set_text(layout, text.c_str(), -1);
pango_layout_get_pixel_size(layout, &font_width, &font_height);
2022-05-08 19:31:31 +03:00
pango_layout_get_size(layout, &pango_width, &pango_height);
2022-03-18 02:49:16 +02:00
double x = -1;
2022-04-30 09:40:09 +03:00
double y = geometry.y() + ((geometry.height() - font_height) / 2);
2022-03-18 02:49:16 +02:00
if (align == PaintTextAlign::Center) {
2022-04-30 09:40:09 +03:00
x = geometry.x() + ((geometry.width() - font_width) / 2);
2022-03-18 02:49:16 +02:00
} else {
2022-04-30 09:40:09 +03:00
x = geometry.x();
2022-03-18 02:49:16 +02:00
}
m_cairo->move_to(x, y);
pango_cairo_show_layout(m_cairo->cobj(), layout);
g_object_unref(layout);
2022-05-08 19:31:31 +03:00
return {pango_units_to_double(pango_width), pango_units_to_double(pango_height)};
2022-03-18 02:49:16 +02:00
}
void Painter::source_rgb(RGB &source_rgb) {
2022-04-30 09:40:09 +03:00
m_cairo->set_source_rgb(source_rgb.r(), source_rgb.g(), source_rgb.b());
2022-03-18 02:49:16 +02:00
}
void Painter::fill() {
m_cairo->fill();
}
void Painter::begin_paint_group() {
m_cairo->push_group();
}
void Painter::end_paint_group() {
m_cairo->pop_group_to_source();
m_cairo->paint();
}
}