TermOx
glyph_string.hpp
1 #ifndef TERMOX_PAINTER_GLYPH_STRING_HPP
2 #define TERMOX_PAINTER_GLYPH_STRING_HPP
3 #include <string>
4 #include <string_view>
5 #include <vector>
6 
7 #include <termox/common/mb_to_u32.hpp>
8 #include <termox/painter/brush.hpp>
9 #include <termox/painter/color.hpp>
10 #include <termox/painter/glyph.hpp>
11 #include <termox/painter/trait.hpp>
12 
13 namespace ox {
14 
15 // TODO hold a vector instead of inherit from one, explicitly defer to vector
16 // methods instead of using = statements.
17 
19 class Glyph_string : private std::vector<Glyph> {
20  public:
22  static constexpr auto npos = -1;
23 
24  public:
26  Glyph_string() = default;
27 
29  explicit Glyph_string(Glyph glyph, int count = 1);
30 
32 
33  template <typename... Attributes>
34  Glyph_string(std::u32string_view symbols, Attributes... attrs)
35  {
36  for (char32_t c : symbols)
37  this->append(Glyph{c, Brush{attrs...}});
38  }
39 
40  template <typename... Attributes>
41  Glyph_string(char32_t const* symbols, Attributes... attrs)
42  : Glyph_string{std::u32string_view{symbols}, attrs...}
43  {}
44 
45  template <typename... Attributes>
46  Glyph_string(std::u32string const& symbols, Attributes... attrs)
47  : Glyph_string{std::u32string_view{symbols}, attrs...}
48  {}
49 
51 
54  template <typename... Attributes>
55  Glyph_string(std::string_view symbols, Attributes... attrs)
56  : Glyph_string{mb_to_u32(symbols), attrs...}
57  {}
58 
59  template <typename... Attributes>
60  Glyph_string(char const* symbols, Attributes... attrs)
61  : Glyph_string{std::string_view{symbols}, attrs...}
62  {}
63 
64  template <typename... Attributes>
65  Glyph_string(std::string const& symbols, Attributes... attrs)
66  : Glyph_string{std::string_view{symbols}, attrs...}
67  {}
68 
70 
71  template <typename InputIterator>
72  Glyph_string(InputIterator first, InputIterator last)
73  : vector<Glyph>::vector(first, last)
74  {}
75 
76  public:
78  auto append(Glyph g) -> Glyph_string&;
79 
81  auto append(Glyph_string const& gs) -> Glyph_string&;
82 
83  public:
85 
86  [[nodiscard]] auto length() const -> int;
87 
89 
90  [[nodiscard]] auto size() const -> int;
91 
92  public:
94 
95  [[nodiscard]] auto u32str() const -> std::u32string;
96 
98 
100  [[nodiscard]] auto str() const -> std::string;
101 
102  public:
104  void add_traits(Traits traits);
105 
107  void clear_traits();
108 
110  void add_color(Background_color bg);
111 
113  void add_color(Foreground_color fg);
114 
116  void remove_traits(Traits traits);
117 
119  void remove_background();
120 
122  void remove_foreground();
123 
124  public:
125  using size_type = int;
126  using std::vector<Glyph>::value_type;
127  using std::vector<Glyph>::allocator_type;
128  using std::vector<Glyph>::difference_type;
129  using std::vector<Glyph>::reference;
130  using std::vector<Glyph>::const_reference;
131  using std::vector<Glyph>::pointer;
132  using std::vector<Glyph>::const_pointer;
133  using std::vector<Glyph>::iterator;
134  using std::vector<Glyph>::const_iterator;
135  using std::vector<Glyph>::reverse_iterator;
136  using std::vector<Glyph>::const_reverse_iterator;
137 
138  using std::vector<Glyph>::operator[];
139  using std::vector<Glyph>::size;
140  using std::vector<Glyph>::assign;
141  using std::vector<Glyph>::get_allocator;
142  using std::vector<Glyph>::at;
143  using std::vector<Glyph>::front;
144  using std::vector<Glyph>::back;
145  using std::vector<Glyph>::data;
146  using std::vector<Glyph>::begin;
147  using std::vector<Glyph>::cbegin;
148  using std::vector<Glyph>::end;
149  using std::vector<Glyph>::cend;
150  using std::vector<Glyph>::rbegin;
151  using std::vector<Glyph>::crbegin;
152  using std::vector<Glyph>::rend;
153  using std::vector<Glyph>::crend;
154  using std::vector<Glyph>::empty;
155  using std::vector<Glyph>::max_size;
156  using std::vector<Glyph>::reserve;
157  using std::vector<Glyph>::capacity;
158  using std::vector<Glyph>::shrink_to_fit;
159  using std::vector<Glyph>::clear;
160  using std::vector<Glyph>::insert;
161  using std::vector<Glyph>::erase;
162  using std::vector<Glyph>::push_back;
163  using std::vector<Glyph>::pop_back;
164  using std::vector<Glyph>::resize;
165  using std::vector<Glyph>::swap;
166 };
167 
168 // Traits ----------------------------------------------------------------------
169 
170 auto operator|=(Glyph_string& gs, Traits ts) -> Glyph_string&;
171 
172 [[nodiscard]] auto operator|(Glyph_string gs, Traits ts) -> Glyph_string;
173 
174 } // namespace ox
175 
176 namespace esc { // For ADL; Trait(s) is really in namespace::esc.
177 
178 [[nodiscard]] auto operator|(char32_t const* gs, Trait t) -> ox::Glyph_string;
179 
180 [[nodiscard]] auto operator|(char32_t const* gs, Traits ts) -> ox::Glyph_string;
181 
182 [[nodiscard]] auto operator|(char const* gs, Trait t) -> ox::Glyph_string;
183 
184 [[nodiscard]] auto operator|(char const* gs, Traits ts) -> ox::Glyph_string;
185 
186 } // namespace esc
187 
188 namespace ox {
189 
190 // Background_color ------------------------------------------------------------
191 auto operator|=(Glyph_string& gs, Background_color c) -> Glyph_string&;
192 
193 [[nodiscard]] auto operator|(Glyph_string gs, Background_color c)
194  -> Glyph_string;
195 
196 [[nodiscard]] auto operator|(char32_t const* gs, Background_color c)
197  -> Glyph_string;
198 
199 // Foreground_color ------------------------------------------------------------
200 auto operator|=(Glyph_string& gs, Foreground_color c) -> Glyph_string&;
201 
202 [[nodiscard]] auto operator|(Glyph_string gs, Foreground_color c)
203  -> Glyph_string;
204 
205 [[nodiscard]] auto operator|(char32_t const* gs, Foreground_color c)
206  -> Glyph_string;
207 
208 // Brush -----------------------------------------------------------------------
210 auto operator|=(Glyph_string& gs, Brush b) -> Glyph_string&;
211 
212 [[nodiscard]] auto operator|(Glyph_string gs, Brush b) -> Glyph_string;
213 
215 [[nodiscard]] auto operator|(char32_t const* gs, Brush b) -> Glyph_string;
216 
217 // Comparison ------------------------------------------------------------------
218 
220 [[nodiscard]] auto operator==(Glyph_string const& x, Glyph_string const& y)
221  -> bool;
222 
224 [[nodiscard]] auto operator!=(Glyph_string const& x, Glyph_string const& y)
225  -> bool;
226 
227 } // namespace ox
228 #endif // TERMOX_PAINTER_GLYPH_STRING_HPP
Holds the look of any paintable object with Traits and Colors.
Definition: brush.hpp:13
Holds a collection of Glyphs with a similar interface to std::string.
Definition: glyph_string.hpp:19
Glyph_string(std::u32string_view symbols, Attributes... attrs)
Construct with a Glyph for each char32_t in symbols and Brush b.
Definition: glyph_string.hpp:34
Glyph_string()=default
Default constructs an empty Glyph_string.
Glyph_string(std::string_view symbols, Attributes... attrs)
Construct with a Glyph for each character in symbols and Brush b.
Definition: glyph_string.hpp:55
void remove_traits(Traits traits)
Remove a series of Traits from every Glyph within the Glyph_string.
Definition: glyph_string.cpp:76
auto append(Glyph g) -> Glyph_string &
Append a single Glyph to the end of *this.
Definition: glyph_string.cpp:21
auto length() const -> int
Return the number of Glyphs in *this Glyph_string.
Definition: glyph_string.cpp:34
auto size() const -> int
Return the number of Glyphs in *this Glyph_string.
Definition: glyph_string.cpp:36
Glyph_string(InputIterator first, InputIterator last)
Construct with iterators from any container providing Input Iterators.
Definition: glyph_string.hpp:72
static constexpr auto npos
Used to indicate 'Until the end of the string'.
Definition: glyph_string.hpp:22
void clear_traits()
Remove all currently set traits on each Glyph.
Definition: glyph_string.cpp:58
void add_color(Background_color bg)
Add bg as the background color to every Glyph contained in *this.
Definition: glyph_string.cpp:64
void remove_background()
Set the background color as the default for every Glyph in *this.
Definition: glyph_string.cpp:82
void add_traits(Traits traits)
Add traits to every Glyph contained in *this.
Definition: glyph_string.cpp:52
auto str() const -> std::string
Convert to a std::string.
Definition: glyph_string.cpp:47
void remove_foreground()
Set the foreground color as the default for every Glyph in *this.
Definition: glyph_string.cpp:88
auto u32str() const -> std::u32string
Convert to a std::u32string, each Glyph being a char32_t.
Definition: glyph_string.cpp:38
Definition: color.hpp:64
Definition: color.hpp:68
Holds a description of a paintable tile on the screen.
Definition: glyph.hpp:11