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(); auto painter = window()->painter();
painter.source_rgb(style()->foreground()); painter.source_rgb(style()->foreground());
painter.set_pango_font_description(style()->font_description()); painter.text(rect(), m_text, PaintTextAlign::Center, PANGO_ELLIPSIZE_END, style()->font_description());
painter.text(rect(), m_text, PaintTextAlign::Center, PANGO_ELLIPSIZE_END, true);
painter.fill(); painter.fill();
} }

View file

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

View file

@ -23,16 +23,13 @@ void Painter::rounded_rectangle(Box &geometry, double border_radius) {
m_cairo->close_path(); m_cairo->close_path();
} }
Point Painter::compute_text_size(Box &widget_geometry, std::string &text) { Point Painter::compute_text_size(Box &widget_geometry, std::string &text, PangoFontDescription *pango_font_description) {
if (m_pango_font_description == nullptr)
return {-1, -1};
PangoLayout *layout = pango_cairo_create_layout(m_cairo->cobj()); PangoLayout *layout = pango_cairo_create_layout(m_cairo->cobj());
int font_width; int font_width;
int font_height; 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) if (widget_geometry.max_width() > 0)
pango_layout_set_width(layout, pango_units_from_double(widget_geometry.max_width())); pango_layout_set_width(layout, pango_units_from_double(widget_geometry.max_width()));
if (widget_geometry.max_height() > 0) 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) }; 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) { Point Painter::text(Box &geometry, std::string &text, PaintTextAlign align, PangoEllipsizeMode ellipsize, PangoFontDescription *pango_font_description) {
if (m_pango_font_description == nullptr)
return {-1,-1};
PangoLayout *layout = pango_cairo_create_layout(m_cairo->cobj()); PangoLayout *layout = pango_cairo_create_layout(m_cairo->cobj());
int font_width; int font_width;
@ -57,10 +51,10 @@ Point Painter::text(Box &geometry, std::string &text, PaintTextAlign align, Pang
int pango_width; int pango_width;
int pango_height; int pango_height;
pango_layout_set_font_description(layout, m_pango_font_description); pango_layout_set_font_description(layout, pango_font_description);
if (geometry.width() > 0 && set_size) if (geometry.width() > 0)
pango_layout_set_width(layout, pango_units_from_double(geometry.width())); 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_height(layout, pango_units_from_double(geometry.height()));
pango_layout_set_ellipsize(layout, ellipsize); pango_layout_set_ellipsize(layout, ellipsize);
pango_layout_set_text(layout, text.c_str(), -1); pango_layout_set_text(layout, text.c_str(), -1);

View file

@ -17,19 +17,15 @@ enum class PaintTextAlign {
class Painter { class Painter {
private: private:
Cairo::RefPtr<Cairo::Context> m_cairo { nullptr }; Cairo::RefPtr<Cairo::Context> m_cairo { nullptr };
PangoFontDescription *m_pango_font_description { nullptr };
public: public:
Painter() {} Painter() {}
Cairo::RefPtr<Cairo::Context> cairo() { return m_cairo; } Cairo::RefPtr<Cairo::Context> cairo() { return m_cairo; }
void set_cairo(Cairo::RefPtr<Cairo::Context> cairo) { m_cairo = 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); 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, PangoFontDescription *pango_font_description);
Point compute_text_size(Box &widget_geometry, std::string &text); 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; } 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()) if (!window())
return; return;
window()->painter().set_pango_font_description(m_style->font_description()); auto size = window()->painter().compute_text_size(rect(), text, style()->font_description());
auto size = window()->painter().compute_text_size(rect(), text); // always reflow, even if resize doesn't
if (!resize(size)) { if (!resize(size)) {
reflow(); reflow();
} }