Unravel Engine C++ Reference
Loading...
Searching...
No Matches
text_component.h
Go to the documentation of this file.
1#pragma once
2#include <base/basetypes.hpp>
6#include <hpp/small_vector.hpp>
7#include <math/math.h>
8
9namespace unravel
10{
11
12enum align : uint32_t
13{
15 // Horizontal align (general)
16 left = 1 << 0,
17 center = 1 << 1,
18 right = 1 << 2,
20
21 // Vertical align (general)
22 top = 1 << 3,
23 middle = 1 << 4,
24 bottom = 1 << 5,
26
27 // Vertical align (text only)
28 capline = 1 << 6, // capline of first line
29 midline = 1 << 7, // half distance between cap_height and baseline
30 baseline = 1 << 10, // baseline of last line
32
34};
35
37{
39};
40
41template<typename T, size_t StaticCapacity = 16>
42using text_vector = hpp::small_vector<T, StaticCapacity>;
43
44
46{
48};
49
51{
52 float opacity = 1.0f;
60 float outline_width = 0.0f;
61 math::vec2 shadow_offsets{0.0f, 0.0f};
63 float shadow_softener = 1.0f;
65
66 void set_opacity(float opacity) { this->opacity = opacity; }
67 auto get_opacity() const -> float { return opacity; }
68
69 void set_text_color(math::color color) { text_color = static_cast<uint32_t>(color); }
70 auto get_text_color() const -> math::color { return math::color(text_color); }
71
72 void set_background_color(math::color color) { background_color = static_cast<uint32_t>(color); }
73 auto get_background_color() const -> math::color { return math::color(background_color); }
74
75 void set_foreground_color(math::color color) { foreground_color = static_cast<uint32_t>(color); }
76 auto get_foreground_color() const -> math::color { return math::color(foreground_color); }
77
78 void set_overline_color(math::color color) { overline_color = static_cast<uint32_t>(color); }
79 auto get_overline_color() const -> math::color { return math::color(overline_color); }
80
81 void set_underline_color(math::color color) { underline_color = static_cast<uint32_t>(color); }
82 auto get_underline_color() const -> math::color { return math::color(underline_color); }
83
84 void set_strike_color(math::color color) { strike_color = static_cast<uint32_t>(color); }
85 auto get_strike_color() const -> math::color { return math::color(strike_color); }
86
87 void set_outline_color(math::color color) { outline_color = static_cast<uint32_t>(color); }
88 auto get_outline_color() const -> math::color { return math::color(outline_color); }
89
90 void set_shadow_color(math::color color) { shadow_color = static_cast<uint32_t>(color); }
91 auto get_shadow_color() const -> math::color { return math::color(shadow_color); }
92
94 auto get_style_flags() const -> text_style_flags { return {style_flags}; }
95
96 auto operator==(const text_style& other) const -> bool
97 {
98 constexpr float epsilon = 0.0001f;
99 return std::fabs(opacity - other.opacity) < epsilon &&
100 text_color == other.text_color &&
101 background_color == other.background_color &&
102 foreground_color == other.foreground_color &&
103 overline_color == other.overline_color &&
104 underline_color == other.underline_color &&
105 strike_color == other.strike_color &&
106 outline_color == other.outline_color &&
107 std::fabs(outline_width - other.outline_width) < epsilon &&
108 shadow_offsets == other.shadow_offsets &&
109 shadow_color == other.shadow_color &&
110 std::fabs(shadow_softener - other.shadow_softener) < epsilon &&
111 style_flags == other.style_flags;
112 }
113};
114
115// All the per buffer style state
117{
119 bool no_break{};
120};
121
122enum class break_type : uint8_t
123{
124 nobreak, // normal
125 mustbreak, // LINEBREAK_MUSTBREAK immediately after
126 allowbreak // punctuation/blank: never wrap before
127};
128
130{
131 std::string_view txt;
132 std::string_view brk_symbol;
134 break_type brk; // one of none, must_break, no_wrap_before
135 float base_width; // measured at base size
136 float scaled_width; // base_width * scale
137};
138
140
147
149{
150 hpp::string_view text;
152};
154
156{
158 hpp::string_view brk_symbol;
159 float width{};
160};
162
174
176{
177 std::string line;
178 std::string break_symbol;
179};
180
185class text_component : public component_crtp<text_component>
186{
187public:
188 enum class buffer_type : uint32_t
189 {
193 };
194
195 enum class overflow_type : uint32_t
196 {
197 none,
198 word,
200 };
201
206 void set_text(const std::string& text);
207
212 auto get_text() const -> const std::string&;
213
218 void set_style(const text_style& style);
219
224 auto get_style() const -> const text_style&;
225
230 void set_buffer_type(const buffer_type& type);
231
236 auto get_buffer_type() const -> const buffer_type&;
237
243
248 auto get_overflow_type() const -> const overflow_type&;
249
254 void set_font(const asset_handle<font>& font);
255
260 auto get_font() const -> const asset_handle<font>&;
261
266 void set_font_size(uint32_t font_size);
267
272 auto get_font_size() const -> uint32_t;
273
278 void set_auto_size(bool auto_size);
279
284 auto get_auto_size() const -> bool;
285
290 auto get_render_font_size() const -> uint32_t;
291
296 void set_area(const fsize_t& area);
297
302 auto get_area() const -> const fsize_t&;
303
309
314 auto get_auto_size_range() const -> const urange32_t&;
315
320 void set_is_rich_text(bool is_rich);
321
326 auto get_is_rich_text() const -> bool;
327
332 void set_apply_kerning(bool apply_kerning);
333
338 auto get_apply_kerning() const -> bool;
339
344 void set_alignment(const alignment& align);
345
350 auto get_alignment() const -> const alignment&;
351
356 auto get_scaled_font() const -> const scaled_font&;
357
362 auto get_render_area() const -> fsize_t;
363
368 auto can_be_rendered() const -> bool;
369
374 auto get_bounds() const -> math::bbox;
375
380 auto get_render_bounds() const -> math::bbox;
381
386 auto get_render_buffers_count() const -> size_t;
387
393 auto get_lines(bool include_breaks = true) const -> text_vector<text_line>;
394
400 auto meters_to_px(float meters) const -> float;
401
407 auto px_to_meters(float px) const -> float;
408
415 void submit(gfx::view_id id, const math::transform& world, uint64_t state);
416
417private:
418 auto get_builder() const -> text_buffer_builder&;
419 void recreate_scaled_font() const;
420 void recreate_text() const;
421
422 asset_handle<font> font_ = font::default_regular();
423
424 mutable uintptr_t font_version_{};
425 mutable bool scaled_font_dirty_{true};
426
427 mutable std::shared_ptr<scaled_font> scaled_font_;
428
429 bool is_rich_{true};
430 bool apply_kerning_{true};
431
432 std::string text_;
433 mutable bool text_dirty_{true};
434
435 uint32_t font_size_{36};
436 mutable uint32_t calculated_font_size_{};
437 fsize_t area_{20.0f, 10.0f};
438 fsize_t render_area_;
439
441 overflow_type overflow_type_{overflow_type::word};
442
443 alignment align_{};
444 mutable bool align_dirty_{true};
445
446 bool auto_size_{};
447 urange32_t auto_size_font_range_{18, 72};
448
449 text_style style_{};
450
451 std::shared_ptr<text_buffer_builder> builder_ = std::make_shared<text_buffer_builder>();
452 std::shared_ptr<text_buffer_builder> debug_builder_ = std::make_shared<text_buffer_builder>();
453
454 mutable scratch_cache scratch_{};
455};
456
457} // namespace unravel
manifold_type type
void set_buffer_type(const buffer_type &type)
Sets the buffer type for text rendering.
auto get_style() const -> const text_style &
Gets the current text style settings.
auto get_apply_kerning() const -> bool
Checks if kerning is enabled.
auto get_area() const -> const fsize_t &
Gets the current text area bounds.
void set_alignment(const alignment &align)
Sets the text alignment properties.
auto get_render_font_size() const -> uint32_t
Gets the actual font size being used for rendering.
void set_font(const asset_handle< font > &font)
Sets the font to be used for rendering text.
auto get_auto_size() const -> bool
Checks if auto-sizing is enabled.
auto get_auto_size_range() const -> const urange32_t &
Gets the current auto-size range.
auto get_font_size() const -> uint32_t
Gets the current font size.
auto get_bounds() const -> math::bbox
Gets the bounding box of the text.
auto get_lines(bool include_breaks=true) const -> text_vector< text_line >
Gets the text content split into lines.
auto get_font() const -> const asset_handle< font > &
Gets the current font.
void set_area(const fsize_t &area)
Sets the area bounds for text rendering.
void set_is_rich_text(bool is_rich)
Enables or disables rich text processing.
auto get_text() const -> const std::string &
Gets the current text content.
void set_overflow_type(const overflow_type &type)
Sets how text should overflow when it exceeds its bounds.
auto get_alignment() const -> const alignment &
Gets the current text alignment settings.
auto get_is_rich_text() const -> bool
Checks if rich text processing is enabled.
auto meters_to_px(float meters) const -> float
Converts meters to pixels based on current font metrics.
auto get_render_area() const -> fsize_t
Gets the actual area used for rendering.
void set_text(const std::string &text)
Sets the text content to be rendered.
auto get_scaled_font() const -> const scaled_font &
Gets the scaled font instance used for rendering.
auto px_to_meters(float px) const -> float
Converts pixels to meters based on current font metrics.
void submit(gfx::view_id id, const math::transform &world, uint64_t state)
Submits the text for rendering.
void set_auto_size(bool auto_size)
Enables or disables automatic font sizing.
void set_font_size(uint32_t font_size)
Sets the font size in pixels.
auto get_render_bounds() const -> math::bbox
Gets the bounding box used for rendering.
auto get_overflow_type() const -> const overflow_type &
Gets the current overflow handling type.
void set_auto_size_range(const urange32_t &range)
Sets the range for automatic font sizing.
auto get_render_buffers_count() const -> size_t
Gets the number of render buffers being used.
auto get_buffer_type() const -> const buffer_type &
Gets the current buffer type.
auto can_be_rendered() const -> bool
Checks if the text can be rendered.
void set_style(const text_style &style)
Sets the text styling properties.
void set_apply_kerning(bool apply_kerning)
Enables or disables kerning in text rendering.
Definition bbox.cpp:5
text_vector< word_frag > fragment_list
text_vector< rich_segment > segment_list
@ vertical_text_mask
hpp::small_vector< T, StaticCapacity > text_vector
text_vector< wrapped_line > text_layout
Represents a handle to an asset, providing access and management functions.
static color white()
Definition math.h:287
static color black()
Definition math.h:292
static color transparent()
Definition math.h:297
CRTP (Curiously Recurring Template Pattern) base structure for components.
text_vector< word_frag > parts
hpp::string_view text
text_vector< frag_atom > atoms
text_vector< char, 256 > wb
text_vector< size_t > offsets
text_vector< char, 256 > lb
std::string break_symbol
auto operator==(const text_style &other) const -> bool
void set_opacity(float opacity)
auto get_background_color() const -> math::color
void set_overline_color(math::color color)
auto get_opacity() const -> float
void set_shadow_color(math::color color)
auto get_underline_color() const -> math::color
void set_foreground_color(math::color color)
void set_text_color(math::color color)
auto get_shadow_color() const -> math::color
void set_strike_color(math::color color)
void set_background_color(math::color color)
auto get_strike_color() const -> math::color
void set_outline_color(math::color color)
void set_style_flags(text_style_flags flags)
math::vec2 shadow_offsets
void set_underline_color(math::color color)
auto get_overline_color() const -> math::color
auto get_outline_color() const -> math::color
auto get_foreground_color() const -> math::color
auto get_style_flags() const -> text_style_flags
auto get_text_color() const -> math::color
std::string_view brk_symbol
std::string_view txt
hpp::string_view brk_symbol
text_vector< rich_segment > segments