]> git.lizzy.rs Git - dragonfireclient.git/blob - src/util/enriched_string.cpp
StaticText/EnrichedString: Styling support (#9187)
[dragonfireclient.git] / src / util / enriched_string.cpp
1 /*
2 Copyright (C) 2013 xyz, Ilya Zhuravlev <whatever@xyz.is>
3 Copyright (C) 2016 Nore, NathanaĆ«l Courant <nore@mesecons.net>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "enriched_string.h"
21 #include "util/string.h"
22 #include "log.h"
23 using namespace irr::video;
24
25 EnrichedString::EnrichedString()
26 {
27         clear();
28 }
29
30 EnrichedString::EnrichedString(const std::wstring &string,
31                 const std::vector<SColor> &colors):
32         m_string(string),
33         m_colors(colors)
34 {}
35
36 EnrichedString::EnrichedString(const std::wstring &s, const SColor &color)
37 {
38         clear();
39         addAtEnd(translate_string(s), color);
40 }
41
42 EnrichedString::EnrichedString(const wchar_t *str, const SColor &color)
43 {
44         clear();
45         addAtEnd(translate_string(std::wstring(str)), color);
46 }
47
48 void EnrichedString::clear()
49 {
50         m_string.clear();
51         m_colors.clear();
52         m_has_background = false;
53         m_default_length = 0;
54         m_default_color = irr::video::SColor(255, 255, 255, 255);
55 }
56
57 void EnrichedString::operator=(const wchar_t *str)
58 {
59         clear();
60         addAtEnd(translate_string(std::wstring(str)), m_default_color);
61 }
62
63 void EnrichedString::addAtEnd(const std::wstring &s, const SColor &initial_color)
64 {
65         SColor color(initial_color);
66         bool use_default = (m_default_length == m_string.size() &&
67                 color == m_default_color);
68
69         size_t i = 0;
70         while (i < s.length()) {
71                 if (s[i] != L'\x1b') {
72                         m_string += s[i];
73                         m_colors.push_back(color);
74                         ++i;
75                         continue;
76                 }
77                 ++i;
78                 size_t start_index = i;
79                 size_t length;
80                 if (i == s.length()) {
81                         break;
82                 }
83                 if (s[i] == L'(') {
84                         ++i;
85                         ++start_index;
86                         while (i < s.length() && s[i] != L')') {
87                                 if (s[i] == L'\\') {
88                                         ++i;
89                                 }
90                                 ++i;
91                         }
92                         length = i - start_index;
93                         ++i;
94                 } else {
95                         ++i;
96                         length = 1;
97                 }
98                 std::wstring escape_sequence(s, start_index, length);
99                 std::vector<std::wstring> parts = split(escape_sequence, L'@');
100                 if (parts[0] == L"c") {
101                         if (parts.size() < 2) {
102                                 continue;
103                         }
104                         parseColorString(wide_to_utf8(parts[1]), color, true);
105
106                         // No longer use default color after first escape
107                         if (use_default) {
108                                 m_default_length = m_string.size();
109                                 use_default = false;
110                         }
111                 } else if (parts[0] == L"b") {
112                         if (parts.size() < 2) {
113                                 continue;
114                         }
115                         parseColorString(wide_to_utf8(parts[1]), m_background, true);
116                         m_has_background = true;
117                 }
118         }
119
120         // Update if no escape character was found
121         if (use_default)
122                 m_default_length = m_string.size();
123 }
124
125 void EnrichedString::addChar(const EnrichedString &source, size_t i)
126 {
127         m_string += source.m_string[i];
128         m_colors.push_back(source.m_colors[i]);
129 }
130
131 void EnrichedString::addCharNoColor(wchar_t c)
132 {
133         m_string += c;
134         if (m_colors.empty()) {
135                 m_colors.emplace_back(m_default_color);
136         } else {
137                 m_colors.push_back(m_colors[m_colors.size() - 1]);
138         }
139 }
140
141 EnrichedString EnrichedString::operator+(const EnrichedString &other) const
142 {
143         EnrichedString result = *this;
144         result += other;
145         return result;
146 }
147
148 void EnrichedString::operator+=(const EnrichedString &other)
149 {
150         bool update_default_color = m_default_length == m_string.size();
151
152         m_string += other.m_string;
153         m_colors.insert(m_colors.end(), other.m_colors.begin(), other.m_colors.end());
154
155         if (update_default_color) {
156                 m_default_length += other.m_default_length;
157                 updateDefaultColor();
158         }
159 }
160
161 EnrichedString EnrichedString::substr(size_t pos, size_t len) const
162 {
163         if (pos >= m_string.length())
164                 return EnrichedString();
165
166         if (len == std::string::npos || pos + len > m_string.length())
167                 len = m_string.length() - pos;
168
169         EnrichedString str(
170                 m_string.substr(pos, len),
171                 std::vector<SColor>(m_colors.begin() + pos, m_colors.begin() + pos + len)
172         );
173         if (pos < m_default_length)
174                 str.m_default_length = m_default_length - pos;
175         str.setDefaultColor(m_default_color);
176         return str;
177 }
178
179 const wchar_t *EnrichedString::c_str() const
180 {
181         return m_string.c_str();
182 }
183
184 const std::vector<SColor> &EnrichedString::getColors() const
185 {
186         return m_colors;
187 }
188
189 const std::wstring &EnrichedString::getString() const
190 {
191         return m_string;
192 }
193
194 void EnrichedString::setDefaultColor(const irr::video::SColor &color)
195 {
196         m_default_color = color;
197         updateDefaultColor();
198 }
199
200 void EnrichedString::updateDefaultColor()
201 {
202         for (size_t i = 0; i < m_default_length; ++i)
203                 m_colors[i] = m_default_color;
204 }