lxgui
gui_font_string_parser.cpp
1 #include "lxgui/gui_font_string.hpp"
2 #include "lxgui/gui_layout_node.hpp"
3 #include "lxgui/gui_localizer.hpp"
4 #include "lxgui/gui_manager.hpp"
5 #include "lxgui/gui_out.hpp"
6 
7 namespace lxgui::gui {
8 
11 
12  if (const layout_node* color_node = node.try_get_child("Color"))
13  set_text_color(parse_color_node_(*color_node));
14 
15  parse_shadow_node_(node);
16 }
17 
18 void font_string::parse_attributes_(const layout_node& node) {
20 
21  set_font(
22  node.get_attribute_value_or<std::string>("font", ""),
23  node.get_attribute_value_or<float>("fontHeight", 0.0f));
24 
25  if (const auto attr = node.try_get_attribute_value<std::string>("text"))
26  set_text(utils::utf8_to_unicode(get_manager().get_localizer().localize(attr.value())));
27  if (const auto attr = node.try_get_attribute_value<bool>("nonSpaceWrap"))
28  set_non_space_wrap_enabled(attr.value());
29  if (const auto attr = node.try_get_attribute_value<float>("spacing"))
30  set_spacing(attr.value());
31  if (const auto attr = node.try_get_attribute_value<float>("lineSpacing"))
32  set_line_spacing(attr.value());
33 
34  if (const auto attr = node.try_get_attribute_value<std::string>("outline")) {
35  std::string outline = utils::to_lower(attr.value());
36  if (outline == "normal" || outline == "thick") {
37  // TODO: fix this in https://github.com/cschreib/lxgui/issues/121
38  set_outlined(true);
39  } else if (outline == "none")
40  set_outlined(false);
41  else {
42  gui::out << gui::warning << node.get_location() << ": "
43  << "Unknown outline type for " << name_ << ": \"" << outline << "\"."
44  << std::endl;
45  }
46  }
47 
48  if (const auto attr = node.try_get_attribute_value<alignment_x>("alignX"))
49  set_alignment_x(attr.value());
50  if (const auto attr = node.try_get_attribute_value<alignment_y>("alignY"))
51  set_alignment_y(attr.value());
52  if (const auto attr =
53  node.try_get_attribute_value<vertex_cache_strategy>("vertexCacheStrategy"))
54  set_vertex_cache_strategy(attr.value());
55 }
56 
57 void font_string::parse_shadow_node_(const layout_node& node) {
58  if (const layout_node* shadow_node = node.try_get_child("Shadow")) {
59  enable_shadow();
60 
61  if (const layout_node* color_node = shadow_node->try_get_child("Color"))
62  set_shadow_color(parse_color_node_(*color_node));
63 
64  if (const layout_node* offset_node = shadow_node->try_get_child("Offset")) {
65  set_shadow_offset(parse_offset_node_or_(*offset_node, 0.0f));
66  }
67  }
68 }
69 
70 } // namespace lxgui::gui
void set_non_space_wrap_enabled(bool enabled)
Sets whether large text without whitespace is truncated or wrapped.
void set_shadow_color(const color &shadow_color)
Sets this font_string's shadow color.
void set_line_spacing(float line_spacing)
Sets the space between each line as a fraction of the font height.
void set_alignment_x(alignment_x align_x)
Sets this font_string's horizontal alignment behavior.
void enable_shadow()
Makes this font_string draw a shadow under its text.
void set_vertex_cache_strategy(vertex_cache_strategy strategy)
Selects the strategy for using vertex caches.
void set_alignment_y(alignment_y align_y)
Sets this font_string's vertical alignment behavior.
void set_outlined(bool outlined)
Adds or remove the outline around the text.
void set_shadow_offset(const vector2f &shadow_offset)
Sets this font_string's shadow offset.
void set_text(const utils::ustring &content)
Sets the rendered text.
void parse_layout(const layout_node &node) override
Parses data from a layout_node.
void set_text_color(const color &text_color)
Sets the text color.
void set_font(const std::string &font_name, float height)
Sets this font_string's font (file and size).
void set_spacing(float spacing)
Sets the space between each letter.
void parse_attributes_(const layout_node &node) override
void parse_layout(const layout_node &node) override
Parses data from a layout_node.
std::string_view get_location() const noexcept
Returns this node's location in the file as {file}:{line}.
An node in a layout file.
std::optional< std::string_view > try_get_attribute_value(std::string_view name) const noexcept
Returns the value of the attribute with the provided name, nullopt if not found.
const layout_node * try_get_child(std::string_view name) const noexcept
Returns the first child with a given name, or null if none.
T get_attribute_value_or(std::string_view name, T fallback) const noexcept
Returns the value of the attribute with the provided name, or a default if not found or parsing faile...
color parse_color_node_(const layout_node &node)
manager & get_manager()
Returns this region's manager.
Definition: gui_region.hpp:693
std::string name_
Definition: gui_region.hpp:804
vector2< float > parse_offset_node_or_(const layout_node &node, float fallback)
vertex_cache_strategy
Strategy for using a vertex cache.
std::ostream out
const std::string warning
Definition: gui_out.cpp:6