raven/src/Painter.cpp

103 lines
3.3 KiB
C++

#include "Painter.hpp"
#include "RGB.hpp"
#include "pango/pango-layout.h"
#include "pango/pango-types.h"
#include "pango/pangocairo.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();
}
Point Painter::compute_text_size(Box &widget_geometry, std::string &text, PangoFontDescription *pango_font_description) {
PangoLayout *layout = pango_cairo_create_layout(m_cairo->cobj());
int font_width;
int font_height;
pango_layout_set_font_description(layout, pango_font_description);
if (widget_geometry.max_width() > 0)
pango_layout_set_width(layout, pango_units_from_double(widget_geometry.max_width()));
if (widget_geometry.max_height() > 0)
pango_layout_set_height(layout, pango_units_from_double(widget_geometry.max_height()));
pango_layout_set_text(layout, text.c_str(), -1);
pango_layout_get_size(layout, &font_width, &font_height);
g_object_unref(layout);
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) {
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)
pango_layout_set_width(layout, pango_units_from_double(geometry.width()));
if (geometry.height() > 0)
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 = 0;
double y = ((geometry.height() - font_height) / 2);
if (align == PaintTextAlign::Center) {
x = ((geometry.width() - font_width) / 2);
}
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();
}
void Painter::flush_paint_group() {
m_cairo->pop_group_to_source();
m_cairo->paint();
m_cairo->get_target()->flush();
}
}