1 #include "lxgui/gui_font_string.hpp"
3 #include "lxgui/gui_layered_region.hpp"
4 #include "lxgui/gui_localizer.hpp"
5 #include "lxgui/gui_manager.hpp"
6 #include "lxgui/gui_out.hpp"
7 #include "lxgui/gui_region_tpl.hpp"
8 #include "lxgui/gui_renderer.hpp"
30 text_->set_use_vertex_cache(is_vertex_cache_used_());
34 if (std::isinf(text_->get_box_width())) {
44 if (std::isinf(text_->get_box_height())) {
58 if (is_shadow_enabled_) {
59 text_->set_color(shadow_color_,
true);
63 text_->set_color(text_color_);
68 std::ostringstream str;
72 str << tab <<
" # Font name : " << font_name_ <<
"\n";
73 str << tab <<
" # Font height: " << height_ <<
"\n";
74 str << tab <<
" # Text ready : " << (text_ !=
nullptr) <<
"\n";
75 str << tab <<
" # Text : \"" << utils::unicode_to_utf8(content_) <<
"\"\n";
76 str << tab <<
" # Outlined : " << is_outlined_ <<
"\n";
77 str << tab <<
" # Text color : " << text_color_ <<
"\n";
78 str << tab <<
" # Spacing : " << spacing_ <<
"\n";
79 str << tab <<
" # Justify :\n";
80 str << tab <<
" #-###\n";
81 str << tab <<
" | # horizontal: " << utils::to_string(align_x_) <<
"\n";
82 str << tab <<
" | # vertical : " << utils::to_string(align_y_) <<
"\n";
83 str << tab <<
" #-###\n";
84 str << tab <<
" # NonSpaceW. : " << non_space_wrap_enabled_ <<
"\n";
85 if (is_shadow_enabled_) {
86 str << tab <<
" # Shadow off.: (" << shadow_offset_.
x <<
", " << shadow_offset_.
y <<
")\n";
87 str << tab <<
" # Shadow col.: " << shadow_color_ <<
"\n";
96 const font_string* fstr_obj = down_cast<font_string>(&obj);
102 if (!font_name.empty() && height != 0)
135 create_text_object_();
154 return shadow_color_;
158 return shadow_offset_;
170 return line_spacing_;
184 void font_string::create_text_object_() {
185 if (font_name_.empty())
188 std::size_t pixel_height =
static_cast<std::size_t
>(
189 std::round(
get_manager().get_interface_scaling_factor() * height_));
197 std::shared_ptr<gui::font> outline_font;
200 "GUI", font_name_, pixel_height,
201 std::min<std::size_t>(2u,
static_cast<std::size_t
>(std::round(0.2 * pixel_height))),
202 code_points, default_code_point);
205 auto fnt = renderer.create_atlas_font(
206 "GUI", font_name_, pixel_height, 0u, code_points, default_code_point);
208 text_ = std::unique_ptr<text>(
new text(renderer, fnt, outline_font));
210 text_->set_scaling_factor(1.0f /
get_manager().get_interface_scaling_factor());
211 text_->set_remove_starting_spaces(
true);
212 text_->set_text(content_);
213 text_->set_alignment_x(align_x_);
214 text_->set_alignment_y(align_y_);
215 text_->set_tracking(spacing_);
216 text_->set_word_wrap_enabled(word_wrap_enabled_);
217 text_->set_word_ellipsis_enabled(ellipsis_enabled_);
218 text_->set_formatting_enabled(formatting_enabled_);
225 create_text_object_();
234 if (align_x_ == justify_h)
237 align_x_ = justify_h;
239 text_->set_alignment_x(align_x_);
247 if (align_y_ == justify_v)
250 align_y_ = justify_v;
252 text_->set_alignment_y(align_y_);
260 if (shadow_color_ == shadow_color)
263 shadow_color_ = shadow_color;
269 if (shadow_offset_ == shadow_offset)
272 shadow_offset_ = shadow_offset;
278 if (offset_ == offset)
287 if (spacing_ == spacing)
292 text_->set_tracking(spacing_);
299 if (line_spacing_ == line_spacing)
302 line_spacing_ = line_spacing;
304 text_->set_line_spacing(line_spacing_);
311 if (text_color_ == text_color)
314 text_color_ = text_color;
320 return non_space_wrap_enabled_;
325 return text_->get_text_height();
332 return text_->get_text_width();
339 return text_->get_string_width(content);
359 return is_shadow_enabled_;
373 if (word_wrap_enabled_ == enabled)
376 word_wrap_enabled_ = enabled;
379 text_->set_word_wrap_enabled(word_wrap_enabled_);
386 return word_wrap_enabled_;
390 if (ellipsis_enabled_ == enabled)
393 ellipsis_enabled_ = enabled;
396 text_->set_word_ellipsis_enabled(ellipsis_enabled_);
403 return ellipsis_enabled_;
407 if (formatting_enabled_ == formatting)
410 formatting_enabled_ = formatting;
413 text_->set_formatting_enabled(formatting_enabled_);
420 return formatting_enabled_;
424 if (content_ == content)
430 text_->set_text(content_);
438 bool font_string::is_vertex_cache_used_()
const {
440 switch (vertex_cache_strategy_) {
446 default:
return false;
451 vertex_cache_strategy_ = strategy;
455 return vertex_cache_strategy_;
466 void font_string::update_borders_() {
471 #define DEBUG_LOG(msg)
474 const auto old_border_list =
borders_;
479 float x_center = 0.0f, y_center = 0.0f;
481 DEBUG_LOG(
" Read anchors");
484 float box_width = std::numeric_limits<float>::infinity();
490 float box_height = std::numeric_limits<float>::infinity();
499 text_->set_box_dimensions(box_width, box_height);
501 DEBUG_LOG(
" Make borders");
502 if (std::isinf(box_height))
503 box_height = text_->get_height();
504 if (std::isinf(box_width))
505 box_width = text_->get_width();
532 if (box_width == 0.0) {
533 box_width = text_->get_width();
537 if (box_height == 0.0) {
538 box_height = text_->get_height();
551 DEBUG_LOG(
" Fire redraw");
557 const std::vector<std::string>& font_string::get_type_list_()
const {
558 return get_type_list_impl_<font_string>();
Holds a single color (float RGBA, 128 bits)
A layered_region that can draw text on the screen.
const color & get_text_color() const
Returns the text color.
void set_word_wrap_enabled(bool enabled)
Enables/disables word wrap.
void set_non_space_wrap_enabled(bool enabled)
Sets whether large text without whitespace is truncated or wrapped.
const std::string & get_font_name() const
Returns the name of the font file.
const vector2f & get_offset() const
Returns this font_string's offset.
void set_formatting_enabled(bool enabled)
Enables color formatting.
const color & get_shadow_color() const
Returns this font_string's shadow color.
float get_string_width() const
Returns the width of the string if no format or wrapping is applied.
bool is_word_ellipsis_enabled() const
Checks if word ellipsis is enabled.
void set_shadow_color(const color &shadow_color)
Sets this font_string's shadow color.
void copy_from(const region &obj) override
Copies a region's parameters into this font_string (inheritance).
bool is_non_space_wrap_enabled() const
Checks if large text is truncated or wrapped.
float get_string_height() const
Returns the height of the string if no format or wrapping is applied.
const vector2f & get_shadow_offset() const
Returns this font_string's shadow offset.
void set_line_spacing(float line_spacing)
Sets the space between each line as a fraction of the font height.
std::string serialize(const std::string &tab) const override
Prints all relevant information about this region in a string.
void set_alignment_x(alignment_x align_x)
Sets this font_string's horizontal alignment behavior.
bool is_formatting_enabled() const
Checks if color formatting is enabled.
float get_font_height() const
Returns the height of the font.
void render() const override
Renders this region on the current render target.
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 notify_scaling_factor_updated() override
Tells this region that the global interface scaling factor has changed.
text * get_text_object()
Returns the text used to render this font_string.
void set_offset(const vector2f &offset)
Sets this font_string's offset.
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.
bool is_outlined() const
Check if this font_string is outlined.
void set_text(const utils::ustring &content)
Sets the rendered text.
float get_line_spacing() const
Returns the space between each line as a fraction of the font height.
bool is_word_wrap_enabled() const
Checks if word wrap is enabled.
font_string(utils::control_block &block, manager &mgr, const region_core_attributes &attr)
Constructor.
void set_text_color(const color &text_color)
Sets the text color.
alignment_y get_alignment_y() const
Returns the vertical alignment behavior.
void set_shadow_enabled(bool enabled)
Sets whether this font_string should draw a shadow under its text.
vertex_cache_strategy get_vertex_cache_strategy() const
Gets the strategy for using vertex caches.
bool is_shadow_enabled() const
Checks if this font_string draws a shadow under its text.
void set_word_ellipsis_enabled(bool enabled)
Sets whether to show an ellipsis "..." if words don't fit in the text box.
float get_spacing() const
Returns the space between each letter.
void set_font(const std::string &font_name, float height)
Sets this font_string's font (file and size).
const utils::ustring & get_text() const
Returns the rendered text (with format tags).
void set_spacing(float spacing)
Sets the space between each letter.
alignment_x get_alignment_x() const
Returns the horizontal alignment behavior.
A region that can be rendered in a layer.
void notify_renderer_need_redraw() override
Notifies the renderer of this region that it needs to be redrawn.
std::string serialize(const std::string &tab) const override
Prints all relevant information about this region in a string.
Utility class to translate strings for display in GUI.
char32_t get_fallback_code_point() const
Returns the default character to display if a character is missing from a font.
const std::vector< code_point_range > & get_allowed_code_points() const
Returns the list of allowed code points (Unicode characters), for text rendering.
Manages the user interface.
localizer & get_localizer()
Returns the object used for localizing strings.
const renderer & get_renderer() const
Returns the renderer implementation.
The base class of all elements in the GUI.
void initialize_(T &self, const region_core_attributes &attr)
Set up function to call in all derived class constructors.
float get_effective_alpha() const
Returns this region's effective alpha (opacity).
bool is_visible() const
Checks if this region can be seen on the screen.
manager & get_manager()
Returns this region's manager.
float round_to_pixel(float value, utils::rounding_method method=utils::rounding_method::nearest) const
Round an absolute position on screen to the nearest physical pixel.
void read_anchors_(float &left, float &right, float &top, float &bottom, float &x_center, float &y_center) const
virtual void render() const
Renders this region on the current render target.
const vector2f & get_dimensions() const
Returns this region's explicitly-defined width and height (in pixels).
std::array< std::optional< anchor >, 9 > anchor_list_
virtual void notify_borders_need_update()
Tells this region that its borders need updating.
bounds2< bool > defined_borders_
bool make_borders_(float &min, float &max, float center, float size) const
virtual void copy_from(const region &obj)
Copies a region's parameters into this region (inheritance).
std::string parse_file_name(const std::string &file_name) const
Convert an addon-relative file path to a application-relative path.
virtual void notify_scaling_factor_updated()
Tells this region that the global interface scaling factor has changed.
virtual void update_borders_()
Abstract type for implementation specific management.
bool is_quad_batching_enabled() const
Checks if the renderer has quad render batching enabled.
std::shared_ptr< font > create_atlas_font(const std::string &atlas_category, const std::string &font_file, std::size_t size, std::size_t outline, const std::vector< code_point_range > &code_points, char32_t default_code_point)
Creates a new font.
bool is_vertex_cache_enabled() const
Checks if the renderer has enabled support for vertex caches.
Used to draw some text on the screen.
bounds2< float > bounds2f
Holds 2D bounds of a region (as floats).
vertex_cache_strategy
Strategy for using a vertex cache.
@ prefer_enabled
Use vertex cache if renderer supports and allows.
@ automatic
Choose automatically to maximize performance on common case.
@ always_disabled
Never use vertex cache.
@ always_enabled
Use vertex cache if renderer supports, even if not allowed.
@ nearest_not_zero
Equivalent to round() but only returns 0 if input is exactly 0.
static const bounds2 zero
static matrix4f translation(const vector2f &dx) noexcept
Struct holding all the core information about a region necessary for its creation.