raven/src/Painter.cpp
2022-03-30 16:12:25 +03:00

93 lines
2.7 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.get_x();
double y = geometry.get_y();
double w = geometry.get_width();
double h = geometry.get_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.get_x(), where.get_y());
pango_cairo_show_layout(m_cairo->cobj(), layout);
g_object_unref(layout);
return true;
}
bool Painter::text(Box &geometry, std::string &text, PaintTextAlign align, PangoEllipsizeMode ellipsize) {
if (m_pango_font_description == nullptr)
return false;
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_width(layout, pango_units_from_double(geometry.get_width()));
pango_layout_set_height(layout, pango_units_from_double(geometry.get_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);
double x = -1;
double y = geometry.get_y() + ((geometry.get_height() - font_height) / 2);
if (align == PaintTextAlign::Center) {
x = geometry.get_x() + ((geometry.get_width() - font_width) / 2);
} else {
x = geometry.get_x();
}
m_cairo->move_to(x, y);
pango_cairo_show_layout(m_cairo->cobj(), layout);
g_object_unref(layout);
return true;
}
void Painter::source_rgb(RGB &source_rgb) {
m_cairo->set_source_rgb(source_rgb.get_r(), source_rgb.get_g(), source_rgb.get_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();
}
}