tidy: tidy Painter's text() and compute_text_size() methods

This commit is contained in:
hippoz 2022-06-11 16:19:02 +03:00
parent 9ee4acfba5
commit 69d8e0a689
No known key found for this signature in database
GPG key ID: 7C52899193467641
5 changed files with 13 additions and 24 deletions

View file

@ -27,8 +27,7 @@ void Button::on_paint() {
auto painter = window()->painter();
painter.source_rgb(style()->foreground());
painter.set_pango_font_description(style()->font_description());
painter.text(rect(), m_text, PaintTextAlign::Center, PANGO_ELLIPSIZE_END, true);
painter.text(rect(), m_text, PaintTextAlign::Center, PANGO_ELLIPSIZE_END, style()->font_description());
painter.fill();
}

View file

@ -13,6 +13,7 @@ void Label::set_text(std::string text) {
fit_text(text);
}
}
void Label::on_init() {
set_style(&default_label_style);
fit_text(m_text);
@ -23,8 +24,7 @@ void Label::on_paint() {
auto painter = window()->painter();
painter.source_rgb(style()->foreground());
painter.set_pango_font_description(style()->font_description());
painter.text(rect(), m_text, PaintTextAlign::Left, PANGO_ELLIPSIZE_NONE, true);
painter.text(rect(), m_text, PaintTextAlign::Left, PANGO_ELLIPSIZE_NONE, style()->font_description());
painter.fill();
}

View file

@ -23,16 +23,13 @@ void Painter::rounded_rectangle(Box &geometry, double border_radius) {
m_cairo->close_path();
}
Point Painter::compute_text_size(Box &widget_geometry, std::string &text) {
if (m_pango_font_description == nullptr)
return {-1, -1};
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, m_pango_font_description);
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)
@ -46,10 +43,7 @@ Point Painter::compute_text_size(Box &widget_geometry, std::string &text) {
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, bool set_size) {
if (m_pango_font_description == nullptr)
return {-1,-1};
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;
@ -57,10 +51,10 @@ Point Painter::text(Box &geometry, std::string &text, PaintTextAlign align, Pang
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_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 && set_size)
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);

View file

@ -17,19 +17,15 @@ enum class PaintTextAlign {
class Painter {
private:
Cairo::RefPtr<Cairo::Context> m_cairo { nullptr };
PangoFontDescription *m_pango_font_description { nullptr };
public:
Painter() {}
Cairo::RefPtr<Cairo::Context> cairo() { return m_cairo; }
void set_cairo(Cairo::RefPtr<Cairo::Context> cairo) { m_cairo = cairo; }
void set_pango_font_description(PangoFontDescription *pango_font_description) { m_pango_font_description = pango_font_description; }
PangoFontDescription *pango_font_description() { return m_pango_font_description; }
void rounded_rectangle(Box &geometry, double border_radius);
Point text(Box &geometry, std::string &text, PaintTextAlign align, PangoEllipsizeMode ellipsize, bool set_size);
Point compute_text_size(Box &widget_geometry, std::string &text);
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);
bool can_paint() { if (m_cairo) return true; else return false; }

View file

@ -21,8 +21,8 @@ void Widget::fit_text(std::string &text) {
if (!window())
return;
window()->painter().set_pango_font_description(m_style->font_description());
auto size = window()->painter().compute_text_size(rect(), text);
auto size = window()->painter().compute_text_size(rect(), text, style()->font_description());
// always reflow, even if resize doesn't
if (!resize(size)) {
reflow();
}