HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
text_style.hpp
1// Copyright Take Vos 2024.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "../color/color.hpp"
8#include "../utility/utility.hpp"
9#include "../font/font.hpp"
10#include "../container/container.hpp"
11#include "../macros.hpp"
12#include <ostream>
13#include <vector>
14#include <algorithm>
15
16hi_export_module(hikogui.text : text_style);
17
18hi_export namespace hi::inline v1 {
21class text_style {
22public:
23 constexpr text_style() noexcept = default;
24 text_style(text_style const&) noexcept = default;
25 text_style(text_style&&) noexcept = default;
26 text_style& operator=(text_style const&) noexcept = default;
27 text_style& operator=(text_style&&) noexcept = default;
28 [[nodiscard]] friend bool operator==(text_style const&, text_style const&) noexcept = default;
29
30 [[nodiscard]] constexpr bool empty() const noexcept
31 {
32 uint32_t tmp = 0;
33 tmp |= _font_valid;
34 tmp |= _color_valid;
35 tmp |= _size_valid;
36 tmp |= _line_spacing_valid;
37 tmp |= _paragraph_spacing_valid;
38 return not tmp;
39 }
40
41 [[nodiscard]] constexpr bool complete() const noexcept
42 {
43 uint32_t tmp = 1;
44 tmp &= _font_valid;
45 tmp &= _color_valid;
46 tmp &= _size_valid;
47 tmp &= _line_spacing_valid;
48 tmp &= _paragraph_spacing_valid;
49 return tmp;
50 }
51
52 [[nodiscard]] lean_vector<font_id> const& font_chain() const
53 {
54 hi_axiom(_font_valid);
55 return _font_chain;
56 }
57
65 void set_font_chain(lean_vector<font_id> font_chain, bool important = false)
66 {
67 if (important or not _font_important) {
68 _font_important |= static_cast<uint32_t>(important);
69 _font_valid = 1;
70 if (important) {
71 _font_chain = std::move(font_chain);
72 } else {
73 _font_chain.insert(_font_chain.begin(), font_chain.begin(), font_chain.end());
74 }
75 }
76 }
77
78 [[nodiscard]] constexpr hi::color color() const
79 {
80 hi_axiom(_color_valid);
81 return _color;
82 }
83
84 constexpr void set_color(hi::color color, bool important = false)
85 {
86 if (important or not _color_important) {
87 _color_important |= static_cast<uint32_t>(important);
88 _color_valid = 1;
89 _color = color;
90 }
91 }
92
93 [[nodiscard]] constexpr unit::font_size_s size() const
94 {
95 hi_axiom(_size_valid);
96 return _size;
97 }
98
99 constexpr void set_size(unit::font_size_s size, bool important = false)
100 {
101 if (important or not _size_important) {
102 _size_important |= static_cast<uint32_t>(important);
103 _size_valid = 1;
104 _size = size;
105 }
106 }
107
108 [[nodiscard]] constexpr float line_spacing() const
109 {
110 hi_axiom(_line_spacing_valid);
111 return _line_spacing;
112 }
113
114 constexpr void set_line_spacing(float line_spacing, bool important = false)
115 {
116 if (important or not _line_spacing_important) {
117 _line_spacing_important |= static_cast<uint32_t>(important);
118 _line_spacing_valid = 1;
119 _line_spacing = line_spacing;
120 }
121 }
122
123 [[nodiscard]] constexpr float paragraph_spacing() const
124 {
125 hi_axiom(_paragraph_spacing_valid);
126 return _paragraph_spacing;
127 }
128
129 constexpr void set_paragraph_spacing(float paragraph_spacing, bool important = false)
130 {
131 if (important or not _paragraph_spacing_important) {
132 _paragraph_spacing_important |= static_cast<uint32_t>(important);
133 _paragraph_spacing_valid = 1;
134 _paragraph_spacing = paragraph_spacing;
135 }
136 }
137
138 void clear() noexcept
139 {
140 *this = text_style{};
141 }
142
145 void apply(text_style const& other) noexcept
146 {
147 if (other._font_valid) {
148 set_font_chain(other._font_chain, other._font_important);
149 }
150
151 if (other._color_valid) {
152 set_color(other._color, other._color_important);
153 }
154
155 if (other._size_valid) {
156 set_size(other._size, other._size_important);
157 }
158
159 if (other._line_spacing_valid) {
160 set_line_spacing(other._line_spacing, other._line_spacing_important);
161 }
162
163 if (other._paragraph_spacing_valid) {
164 set_paragraph_spacing(other._paragraph_spacing, other._paragraph_spacing_important);
165 }
166 }
167
168private:
169 lean_vector<font_id> _font_chain = {};
170 hi::color _color = {};
171 unit::font_size_s _size = {};
172 float _line_spacing = 1.0f;
173 float _paragraph_spacing = 1.5f;
174
175 uint32_t _color_valid : 1 = 0;
176 uint32_t _color_important : 1 = 0;
177 uint32_t _font_valid : 1 = 0;
178 uint32_t _font_important : 1 = 0;
179 uint32_t _size_valid : 1 = 0;
180 uint32_t _size_important : 1 = 0;
181 uint32_t _line_spacing_valid : 1 = 0;
182 uint32_t _line_spacing_important : 1 = 0;
183 uint32_t _paragraph_spacing_valid : 1 = 0;
184 uint32_t _paragraph_spacing_important : 1 = 0;
185};
186
187} // namespace hi::inline v1
Defined the color type.
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
This is a RGBA floating point color.
Definition color_intf.hpp:49
void set_font_chain(lean_vector< font_id > font_chain, bool important=false)
Set the font-chain for this text style.
Definition text_style.hpp:65
void apply(text_style const &other) noexcept
Apply the given text-style on top of this style.
Definition text_style.hpp:145
T move(T... args)