remove unparsed text and store the dom correctly

This commit is contained in:
Subhraman Sarkar 2025-03-21 14:10:39 +05:30
parent 11f3bbf979
commit e7a684afbf
4 changed files with 15 additions and 37 deletions

View File

@ -181,7 +181,7 @@ void help_browser::show_topic(std::string topic_id, bool add_to_history)
data.emplace("topic_title", item);
find_widget<label>("topic_title").set_label(topic->title);
find_widget<rich_label>("topic_text").set_topic(topic);
find_widget<rich_label>("topic_text").set_dom(topic->text.parsed_text());
invalidate_layout();
scrollbar_panel& scroll = find_widget<scrollbar_panel>("topic_scroll_panel");

View File

@ -25,7 +25,6 @@
#include "cursor.hpp"
#include "font/constants.hpp"
#include "font/sdl_ttf_compat.hpp"
#include "help/help_impl.hpp"
#include "gettext.hpp"
#include "log.hpp"
#include "serialization/markup.hpp"
@ -71,7 +70,6 @@ rich_label::rich_label(const implementation::builder_rich_label& builder)
, font_size_(font::SIZE_NORMAL)
, can_shrink_(true)
, text_alpha_(ALPHA_OPAQUE)
, unparsed_text_()
, init_w_(builder.width(get_screen_size_variables()))
, size_(0, 0)
, padding_(builder.padding)
@ -86,7 +84,7 @@ rich_label::rich_label(const implementation::builder_rich_label& builder)
link_color_ = conf->link_color;
predef_colors_.insert(conf->colors.begin(), conf->colors.end());
set_text_alignment(builder.text_alignment);
set_label(get_label());
set_label(builder.label_string);
}
color_t rich_label::get_color(const std::string& color)
@ -246,22 +244,18 @@ std::vector<std::string> rich_label::split_in_width(
res.push_back(s.substr(first_line.size()));
}
} catch (utf8::invalid_utf8_exception&) {
throw markup::parse_error (_("corrupted original file"));
throw markup::parse_error(_("corrupted original file"));
}
return res;
}
void rich_label::set_topic(const help::topic* topic) {
styled_widget::set_label(topic->text.parsed_text().debug());
std::tie(text_dom_, size_) = get_parsed_text(topic->text.parsed_text(), point(0,0), init_w_, true);
void rich_label::set_dom(const config& dom) {
std::tie(shapes_, size_) = get_parsed_text(dom, point(0,0), init_w_, true);
}
void rich_label::set_label(const t_string& text) {
styled_widget::set_label(text);
unparsed_text_ = text;
help::topic_text marked_up_text(text);
std::tie(text_dom_, size_) = get_parsed_text(marked_up_text.parsed_text(), point(0,0), init_w_, true);
set_dom(markup::parse_text(text));
}
std::pair<config, point> rich_label::get_parsed_text(
@ -861,7 +855,7 @@ void rich_label::default_text_config(
void rich_label::update_canvas()
{
for(canvas& tmp : get_canvases()) {
tmp.set_shapes(text_dom_, true);
tmp.set_shapes(shapes_, true);
tmp.set_variable("width", wfl::variant(init_w_));
tmp.set_variable("padding", wfl::variant(padding_));
// Disable ellipsization so that text wrapping can work

View File

@ -20,7 +20,6 @@
#include "gui/core/canvas_private.hpp"
#include "gui/core/widget_definition.hpp"
#include "help/help_impl.hpp"
namespace gui2
{
@ -84,6 +83,8 @@ public:
return !tooltip().empty() || get_link_aware();
}
virtual void request_reduce_height(const unsigned /*maximum_height*/) override {};
virtual void update_canvas() override;
/* **** ***** ***** setters / getters for members ***** ****** **** */
@ -119,16 +120,11 @@ public:
void set_text_alpha(unsigned short alpha);
const t_string& get_label() const
{
return unparsed_text_.empty() ? styled_widget::get_label() : unparsed_text_;
}
// Show text marked up with help markup
void set_label(const t_string& text) override;
// Show a help topic
void set_topic(const help::topic* topic);
// Show a given DOM (given as a config)
void set_dom(const config& dom);
// Given a parsed config from help markup,
// layout it into a config that can be understood by canvas
@ -223,11 +219,8 @@ private:
return can_shrink_;
}
/** structure tree of the marked up text after parsing */
config text_dom_;
/** The unparsed/raw text */
t_string unparsed_text_;
/** Final list of shapes to be drawn on the canvas. */
config shapes_;
/** Width and height of the canvas */
const unsigned init_w_;
@ -279,14 +272,7 @@ private:
return (text_height > img_height) ? (text_height - img_height)/2 : 0;
}
point calculate_best_size() const override
{
if(size_ == point{}) {
return styled_widget::calculate_best_size();
} else {
return size_;
}
}
point calculate_best_size() const override { return size_; };
public:
/** Static type getter that does not rely on the widget being constructed. */

View File

@ -215,9 +215,7 @@ void styled_widget::request_reduce_width(const unsigned maximum_width)
void styled_widget::request_reduce_height(const unsigned maximum_height)
{
if(!label_.empty()) {
// Do nothing
} else {
if(label_.empty()) {
point size = get_best_size();
point min_size = get_config_minimum_size();
size.y = std::min(size.y, std::max<int>(maximum_height, min_size.y));