raven/src/Painter.cpp
2022-05-10 21:14:55 +03:00

98 lines
2.9 KiB
C++

#include "Painter.hpp"
#include "RGB.hpp"
#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;
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();
}
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);
m_cairo->move_to(where.x(), where.y());
pango_cairo_show_layout(m_cairo->cobj(), layout);
g_object_unref(layout);
return true;
}
Point Painter::text(Box &geometry, std::string &text, PaintTextAlign align, PangoEllipsizeMode ellipsize, bool set_size) {
if (m_pango_font_description == nullptr)
return {-1,-1};
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, m_pango_font_description);
if (geometry.width() > 0 && set_size)
pango_layout_set_width(layout, pango_units_from_double(geometry.width()));
if (geometry.height() > 0 && set_size)
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);
pango_layout_get_pixel_size(layout, &font_width, &font_height);
pango_layout_get_size(layout, &pango_width, &pango_height);
double x = -1;
double y = geometry.y() + ((geometry.height() - font_height) / 2);
if (align == PaintTextAlign::Center) {
x = geometry.x() + ((geometry.width() - font_width) / 2);
} else {
x = geometry.x();
}
m_cairo->move_to(x, 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) {
m_cairo->set_source_rgb(source_rgb.r(), source_rgb.g(), source_rgb.b());
}
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();
}
}