rich label: align small inline images with text vertically

This commit is contained in:
Subhraman Sarkar 2025-03-10 12:35:31 +05:30
parent eb476bb625
commit 2b5d8b96bb
3 changed files with 27 additions and 7 deletions

View File

@ -261,7 +261,9 @@ public:
* @returns The number of lines in the text.
*
*/
unsigned get_lines_count() const { return pango_layout_get_line_count(layout_.get()); };
unsigned get_lines_count() const {
return pango_layout_get_line_count(layout_.get());
};
/**
* Gets the length of the text in bytes.
@ -271,6 +273,13 @@ 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.
*

View File

@ -20,12 +20,9 @@
#include "gui/core/log.hpp"
#include "gui/core/widget_definition.hpp"
#include "gui/core/register_widget.hpp"
#include "gui/dialogs/message.hpp"
#include "gui/widgets/settings.hpp"
#include "cursor.hpp"
#include "desktop/clipboard.hpp"
#include "desktop/open.hpp"
#include "font/constants.hpp"
#include "font/sdl_ttf_compat.hpp"
#include "help/help_impl.hpp"
@ -58,6 +55,8 @@ using namespace std::string_literals;
const std::array format_tags{ "bold"s, "b"s, "italic"s, "i"s, "underline"s, "u"s };
}
// TODO Get rid of set_var calls, calculate and inject position values from C++ instead.
// ------------ WIDGET -----------{
REGISTER_WIDGET(rich_label)
@ -172,7 +171,13 @@ void rich_label::add_image(config& curr_item, const std::string& name, std::stri
actions << "])";
curr_item["actions"] = actions.str();
actions.str("");
// Correction in y coordinate of image
if (!floating) {
unsigned dy = baseline_correction(get_image_size(curr_item).y);
curr_item["y"] = "(pos_y + " + std::to_string(dy) + ")";
}
}
void rich_label::add_link(config& curr_item, const std::string& name, const std::string& dest, const point& origin, int img_width) {
@ -550,6 +555,7 @@ std::pair<config, point> rich_label::get_parsed_text(
DBG_GUI_RL << "ref: dst=" << child["dst"];
} else if(std::find(format_tags.begin(), format_tags.end(), key) != format_tags.end()) {
// TODO only the formatting tags here support nesting
add_text_with_attribute(*curr_item, line, key);
config parsed_children = get_parsed_text(child, point(x, prev_blk_height), init_width).first;

View File

@ -18,11 +18,9 @@
#include "color.hpp"
#include "gui/widgets/styled_widget.hpp"
#include "font/standard_colors.hpp"
#include "gui/core/canvas_private.hpp"
#include "gui/core/widget_definition.hpp"
#include "help/help_impl.hpp"
#include "serialization/parser.hpp"
namespace gui2
{
@ -281,6 +279,13 @@ private:
return font::get_text_renderer().get_cursor_position(offset);
}
// 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();
return (text_height > img_height) ? (text_height - img_height)/2 : 0;
}
point calculate_best_size() const override
{
if(size_ == point{}) {