From 68cf97e36eca54252cf1c09df45fc6f7937501a0 Mon Sep 17 00:00:00 2001 From: Subhraman Sarkar Date: Thu, 13 Mar 2025 14:42:04 +0530 Subject: [PATCH] rich label: inline image alignment fix Fixes inline image alignment when scaling > 1. Also some cleanup. --- src/font/text.hpp | 7 ------- src/gui/widgets/rich_label.cpp | 24 ++++++++---------------- src/gui/widgets/rich_label.hpp | 4 ++-- 3 files changed, 10 insertions(+), 25 deletions(-) diff --git a/src/font/text.hpp b/src/font/text.hpp index 98c929e3066..76d259a1c6a 100644 --- a/src/font/text.hpp +++ b/src/font/text.hpp @@ -273,13 +273,6 @@ public: */ std::size_t get_length() const { return length_; } - unsigned get_text_height() const { - // return pango_layout_get_baseline(layout_.get())/PANGO_SCALE; - int height; - pango_layout_get_pixel_size(layout_.get(), nullptr, &height); - return height; - } - /** * Sets the text to render. * diff --git a/src/gui/widgets/rich_label.cpp b/src/gui/widgets/rich_label.cpp index 119e22a99e3..bf8b319b7f5 100644 --- a/src/gui/widgets/rich_label.cpp +++ b/src/gui/widgets/rich_label.cpp @@ -333,9 +333,7 @@ std::pair rich_label::get_parsed_text( } else if(key == "table") { if (curr_item == nullptr) { curr_item = &(text_dom.add_child("text")); - default_text_config(curr_item); - (*curr_item)["x"] = pos.x; - (*curr_item)["y"] = pos.y; + default_text_config(curr_item, pos, init_width); new_text_block = false; } @@ -449,7 +447,7 @@ std::pair rich_label::get_parsed_text( } else if(key == "break" || key == "br") { if (curr_item == nullptr) { curr_item = &(text_dom.add_child("text")); - default_text_config(curr_item); + default_text_config(curr_item, pos, init_width); new_text_block = false; } @@ -529,10 +527,7 @@ std::pair rich_label::get_parsed_text( if (curr_item == nullptr || new_text_block) { curr_item = &(text_dom.add_child("text")); - default_text_config(curr_item); - (*curr_item)["x"] = pos.x; - (*curr_item)["y"] = pos.y; - (*curr_item)["maximum_width"] = init_width - pos.x - float_size.x; + default_text_config(curr_item, pos, init_width - pos.x - float_size.x); new_text_block = false; } @@ -652,10 +647,7 @@ std::pair rich_label::get_parsed_text( // rest of the text curr_item = &(text_dom.add_child("text")); - default_text_config(curr_item); - (*curr_item)["x"] = pos.x; - (*curr_item)["y"] = pos.y; - (*curr_item)["maximum_width"] = init_width - pos.x - float_size.x; + default_text_config(curr_item, pos, init_width - pos.x - float_size.x); tmp_h = get_text_size(*curr_item, init_width).y; add_text_with_attribute(*curr_item, removed_part); @@ -733,7 +725,7 @@ std::pair rich_label::get_parsed_text( return { text_dom, point(w, h - origin.y) }; } // function ends -void rich_label::default_text_config(config* txt_ptr, const t_string& text) { +void rich_label::default_text_config(config* txt_ptr, const point& pos, const int max_width, const t_string& text) { if (txt_ptr != nullptr) { (*txt_ptr)["text"] = text; (*txt_ptr)["color"] = text_color_enabled_.to_rgba_string(); @@ -741,12 +733,12 @@ void rich_label::default_text_config(config* txt_ptr, const t_string& text) { (*txt_ptr)["font_size"] = font_size_; (*txt_ptr)["font_style"] = font_style_; (*txt_ptr)["text_alignment"] = encode_text_alignment(get_text_alignment()); + (*txt_ptr)["x"] = pos.x; + (*txt_ptr)["y"] = pos.y; (*txt_ptr)["w"] = "(text_width)"; (*txt_ptr)["h"] = "(text_height)"; + (*txt_ptr)["maximum_width"] = max_width; (*txt_ptr)["parse_text_as_formula"] = false; - // tw -> table width, used for wrapping text inside table cols - // ww -> wrap width, used for wrapping around floating image - // max text width shouldn't go beyond the rich_label's specified width } } diff --git a/src/gui/widgets/rich_label.hpp b/src/gui/widgets/rich_label.hpp index fdab2bfd299..b7e61aa83b0 100644 --- a/src/gui/widgets/rich_label.hpp +++ b/src/gui/widgets/rich_label.hpp @@ -246,7 +246,7 @@ private: unsigned padding_; /** Create template for text config that can be shown in canvas */ - void default_text_config(config* txt_ptr, const t_string& text = ""); + void default_text_config(config* txt_ptr, const point& pos, const int max_width, const t_string& text = ""); std::pair add_text(config& curr_item, const std::string& text); void add_attribute(config& curr_item, const std::string& attr_name, size_t start = 0, size_t end = 0, const std::string& extra_data = ""); @@ -281,7 +281,7 @@ private: // A correction to allow inline image to stay at the same height // as the text following it. unsigned baseline_correction(unsigned img_height) { - unsigned text_height = font::get_text_renderer().get_text_height(); + unsigned text_height = font::get_text_renderer().get_size().y; return (text_height > img_height) ? (text_height - img_height)/2 : 0; }