handle text bounds using pango

This commit is contained in:
hippoz 2022-03-29 09:27:59 +03:00
parent 5744ee7add
commit 992ef1e3d6
Signed by: hippoz
GPG key ID: 7C52899193467641
5 changed files with 12 additions and 5 deletions

View file

@ -3,6 +3,7 @@
#include "Box.hpp" #include "Box.hpp"
#include "Window.hpp" #include "Window.hpp"
#include "Painter.hpp" #include "Painter.hpp"
#include "pango/pango-layout.h"
#include <pango/pangocairo.h> #include <pango/pangocairo.h>
namespace Raven { namespace Raven {
@ -30,7 +31,7 @@ void Button::on_paint() {
auto text_color = get_styles()->get_button_text_color(); auto text_color = get_styles()->get_button_text_color();
painter.source_rgb(text_color); painter.source_rgb(text_color);
painter.set_pango_font_description(get_styles()->get_controls_font_description()); painter.set_pango_font_description(get_styles()->get_controls_font_description());
painter.text(get_current_geometry(), m_text, PaintTextAlign::Center); painter.text(get_current_geometry(), m_text, PaintTextAlign::Center, PANGO_ELLIPSIZE_END);
painter.fill(); painter.fill();
} }

View file

@ -1,5 +1,6 @@
#include "Label.hpp" #include "Label.hpp"
#include "Window.hpp" #include "Window.hpp"
#include "pango/pango-layout.h"
namespace Raven { namespace Raven {
@ -18,7 +19,7 @@ void Label::on_paint() {
painter.source_rgb(text_color); painter.source_rgb(text_color);
painter.set_pango_font_description(get_styles()->get_controls_font_description()); painter.set_pango_font_description(get_styles()->get_controls_font_description());
painter.text(get_current_geometry(), m_text, PaintTextAlign::Center); painter.text(get_current_geometry(), m_text, PaintTextAlign::Center, PANGO_ELLIPSIZE_NONE);
painter.fill(); painter.fill();
} }

View file

@ -1,5 +1,7 @@
#include "Painter.hpp" #include "Painter.hpp"
#include "RGB.hpp" #include "RGB.hpp"
#include "pango/pango-layout.h"
#include "pango/pango-types.h"
namespace Raven { namespace Raven {
@ -37,7 +39,7 @@ bool Painter::text(Point &where, std::string &text) {
return true; return true;
} }
bool Painter::text(Box &geometry, std::string &text, PaintTextAlign align) { bool Painter::text(Box &geometry, std::string &text, PaintTextAlign align, PangoEllipsizeMode ellipsize) {
if (m_pango_font_description == nullptr) if (m_pango_font_description == nullptr)
return false; return false;
@ -47,6 +49,9 @@ bool Painter::text(Box &geometry, std::string &text, PaintTextAlign align) {
int font_height; int font_height;
pango_layout_set_font_description(layout, m_pango_font_description); 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_set_text(layout, text.c_str(), -1);
pango_layout_get_pixel_size(layout, &font_width, &font_height); pango_layout_get_pixel_size(layout, &font_width, &font_height);

View file

@ -29,7 +29,7 @@ public:
void rounded_rectangle(Box &geometry, double border_radius); void rounded_rectangle(Box &geometry, double border_radius);
bool text(Point &where, std::string &text); bool text(Point &where, std::string &text);
bool text(Box &geometry, std::string &text, PaintTextAlign align); bool text(Box &geometry, std::string &text, PaintTextAlign align, PangoEllipsizeMode ellipsize);
void source_rgb(RGB &source_rgb); void source_rgb(RGB &source_rgb);
void fill(); void fill();

View file

@ -10,7 +10,7 @@ int main() {
Raven::Window window {}; Raven::Window window {};
Raven::Widget main_widget {}; Raven::Widget main_widget {};
Raven::Button button {"click me"}; Raven::Button button {"click meeeeeeeeeeee"};
Raven::Label label {"click it!"}; Raven::Label label {"click it!"};
window.spawn_window(); window.spawn_window();