more refactoring and rudimentary nested formatting tags

This commit is contained in:
Subhraman Sarkar 2024-09-13 11:37:18 +05:30 committed by Celtic Minstrel
parent 9e891fbd78
commit 449689a37c
2 changed files with 43 additions and 54 deletions

View File

@ -99,24 +99,28 @@ const point rich_label::get_image_size(const config& img_cfg) {
return point(variables.query_value("image_width").as_int(), variables.query_value("image_height").as_int()); return point(variables.query_value("image_width").as_int(), variables.query_value("image_height").as_int());
} }
void rich_label::add_text_with_attributes(config& curr_item, std::string text, std::vector<std::string> attr_names, std::vector<std::string> extra_data) { std::pair<size_t, size_t> rich_label::add_text(config& curr_item, std::string text) {
size_t start = curr_item["text"].str().size(); size_t start = curr_item["text"].str().size();
curr_item["text"] = curr_item["text"].str() + text; curr_item["text"] = curr_item["text"].str() + text;
size_t end = curr_item["text"].str().size(); size_t end = curr_item["text"].str().size();
return std::make_pair(start, end);
}
if (!attr_names.empty()) { void rich_label::add_attribute(config& curr_item, std::string attr_name, size_t start, size_t end, std::string extra_data) {
for (size_t i = 0; i < attr_names.size(); i++) { config& attr_cfg = curr_item.add_child("attribute");
config& attr_cfg = curr_item.add_child("attribute"); attr_cfg["name"] = attr_name;
attr_cfg["name"] = attr_names[i]; attr_cfg["start"] = start;
attr_cfg["start"] = start; attr_cfg["end"] = end == 0 ? curr_item["text"].str().size() : end;
attr_cfg["end"] = end;
if (!extra_data.empty()) { if (!extra_data.empty()) {
attr_cfg["value"] = extra_data[i]; attr_cfg["value"] = extra_data;
}
}
} }
}
std::pair<size_t, size_t> rich_label::add_text_with_attribute(config& curr_item, std::string text, std::string attr_name, std::string extra_data) {
const auto& [start, end] = add_text(curr_item, text);
add_attribute(curr_item, attr_name, start, end, extra_data);
return std::make_pair(start, end);
} }
void rich_label::add_image(config& curr_item, std::string name, std::string align, bool has_prev_image, bool floating) { void rich_label::add_image(config& curr_item, std::string name, std::string align, bool has_prev_image, bool floating) {
@ -284,7 +288,7 @@ config rich_label::get_parsed_text(const config& parsed_text, const point& origi
point img_size; point img_size;
point float_size; point float_size;
DBG_GUI_RL << parsed_text.debug(); PLAIN_LOG << parsed_text.debug();
for(config::any_child tag : parsed_text.all_children_range()) { for(config::any_child tag : parsed_text.all_children_range()) {
config& child = tag.cfg; config& child = tag.cfg;
@ -488,36 +492,26 @@ config rich_label::get_parsed_text(const config& parsed_text, const point& origi
DBG_GUI_RL << "ref: dst=" << child["dst"]; DBG_GUI_RL << "ref: dst=" << child["dst"];
} else if(tag.key == "bold" || tag.key == "b") { } else if(std::find(format_tags_.begin(), format_tags_.end(), tag.key) != format_tags_.end()) {
add_text_with_attribute(*curr_item, line, "bold");
const auto& [start, end] = add_text_with_attribute(*curr_item, line, tag.key);
config parsed_children = get_parsed_text(tag.cfg, point(x, prev_blk_height));
for (config& text : parsed_children.child_range("text")) {
add_attribute(text, tag.key, start, end);
}
text_dom.append_children(parsed_children);
is_image = false; is_image = false;
DBG_GUI_RL << "bold: text=" << line; DBG_GUI_RL << tag.key << ": text=" << gui2::debug_truncate(line);
} else if(tag.key == "italic" || tag.key == "i") {
add_text_with_attribute(*curr_item, line, "italic");
is_image = false;
DBG_GUI_RL << "italic: text=" << line;
} else if(tag.key == "underline" || tag.key == "u") {
add_text_with_attribute(*curr_item, line, "underline");
is_image = false;
DBG_GUI_RL << "u: text=" << line;
} else if(tag.key == "header" || tag.key == "h") { } else if(tag.key == "header" || tag.key == "h") {
std::vector<std::string> attrs = {"face", "color", "size"}; const auto& [start, end] = add_text(*curr_item, line);
std::vector<std::string> attr_data; add_attribute(*curr_item, "face", start, end, "serif");
attr_data.push_back("serif"); add_attribute(*curr_item, "color", start, end, font::string_to_color("white").to_hex_string());
attr_data.push_back(font::string_to_color("white").to_hex_string()); add_attribute(*curr_item, "size", start, end, std::to_string(font::SIZE_TITLE - 2));
attr_data.push_back(std::to_string(font::SIZE_TITLE - 2));
add_text_with_attributes((*curr_item), line, attrs, attr_data);
is_image = false; is_image = false;
@ -526,12 +520,9 @@ config rich_label::get_parsed_text(const config& parsed_text, const point& origi
} else if(tag.key == "character_entity") { } else if(tag.key == "character_entity") {
line = "&" + child["name"].str() + ";"; line = "&" + child["name"].str() + ";";
std::vector<std::string> attrs = {"face", "color"}; const auto& [start, end] = add_text(*curr_item, line);
std::vector<std::string> attr_data; add_attribute(*curr_item, "face", start, end, "monospace");
attr_data.push_back("monospace"); add_attribute(*curr_item, "color", start, end, font::string_to_color("red").to_hex_string());
attr_data.push_back(font::string_to_color("red").to_hex_string());
add_text_with_attributes((*curr_item), line, attrs, attr_data);
is_image = false; is_image = false;
@ -539,29 +530,24 @@ config rich_label::get_parsed_text(const config& parsed_text, const point& origi
} else if(tag.key == "span" || tag.key == "format") { } else if(tag.key == "span" || tag.key == "format") {
std::vector<std::string> attrs; const auto& [start, end] = add_text(*curr_item, line);
std::vector<std::string> attr_data;
DBG_GUI_RL << "span/format: text=" << line; DBG_GUI_RL << "span/format: text=" << line;
DBG_GUI_RL << "attributes:"; DBG_GUI_RL << "attributes:";
for (const auto& attr : child.attribute_range()) { for (const auto& attr : child.attribute_range()) {
if (attr.first != "text") { if (attr.first != "text") {
attrs.push_back(attr.first); add_attribute(*curr_item, attr.first, start, end, attr.second);
attr_data.push_back(attr.second);
DBG_GUI_RL << attr.first << "=" << attr.second; DBG_GUI_RL << attr.first << "=" << attr.second;
} }
} }
add_text_with_attributes((*curr_item), line, attrs, attr_data);
is_image = false; is_image = false;
} else if (tag.key == "text") { } else if (tag.key == "text") {
DBG_GUI_RL << "text: text=" << gui2::debug_truncate(line) << "..."; DBG_GUI_RL << "text: text=" << gui2::debug_truncate(line) << "...";
(*curr_item)["font_size"] = font::SIZE_NORMAL; add_text(*curr_item, line);
(*curr_item)["text"] = (*curr_item)["text"].str() + line;
point text_size = get_text_size(*curr_item, w_ - (x == 0 ? float_size.x : x)); point text_size = get_text_size(*curr_item, w_ - (x == 0 ? float_size.x : x));
text_size.x -= x; text_size.x -= x;

View File

@ -203,13 +203,16 @@ private:
/** Padding */ /** Padding */
unsigned padding_; unsigned padding_;
/** Possible formatting tags, must be the same as those in gui2::text_shape::draw */
const std::vector<std::string> format_tags_ = {"bold", "b", "italic", "i", "underline", "u"};
/** Create template for text config that can be shown in canvas */ /** Create template for text config that can be shown in canvas */
void default_text_config(config* txt_ptr, t_string text = ""); void default_text_config(config* txt_ptr, t_string text = "");
void add_text_with_attribute(config& curr_item, std::string text, std::string attr_name = "", std::string extra_data = "") { std::pair<size_t, size_t> add_text(config& curr_item, std::string text);
add_text_with_attributes(curr_item, text, {attr_name}, {extra_data}); void add_attribute(config& curr_item, std::string attr_name, size_t start = 0, size_t end = 0, std::string extra_data = "");
} std::pair<size_t, size_t> add_text_with_attribute(config& curr_item, std::string text, std::string attr_name = "", std::string extra_data = "");
void add_text_with_attributes(config& curr_item, std::string text, std::vector<std::string> attr_names, std::vector<std::string> extra_data);
void add_image(config& curr_item, std::string name, std::string align, bool has_prev_image, bool floating); void add_image(config& curr_item, std::string name, std::string align, bool has_prev_image, bool floating);
void add_link(config& curr_item, std::string name, std::string dest, const point& origin, int img_width); void add_link(config& curr_item, std::string name, std::string dest, const point& origin, int img_width);