MyGUI 3.5.3
MyGUI_TextView.cpp
Go to the documentation of this file.
1/*
2 * This source file is part of MyGUI. For the latest info, see http://mygui.info/
3 * Distributed under the MIT License
4 * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
5 */
6
7#include "MyGUI_Precompiled.h"
8#include "MyGUI_TextView.h"
9
10namespace MyGUI
11{
12 namespace
13 {
14
15 template<typename T>
16 void setMin(T& _var, const T& _newValue)
17 {
18 if (_newValue < _var)
19 _var = _newValue;
20 }
21
22 template<typename T>
23 void setMax(T& _var, const T& _newValue)
24 {
25 if (_var < _newValue)
26 _var = _newValue;
27 }
28
29 }
30
31 class RollBackPoint
32 {
33 public:
34 void set(size_t _position, const UString::const_iterator& _space_point, size_t _count, float _width)
35 {
36 position = _position;
37 space_point = _space_point;
38 count = _count;
39 width = _width;
40 rollback = true;
41 }
42
43 void clear()
44 {
45 rollback = false;
46 }
47
48 bool empty() const
49 {
50 return !rollback;
51 }
52
53 float getWidth() const
54 {
55 MYGUI_DEBUG_ASSERT(rollback, "rollback point not valid");
56 return width;
57 }
58
59 size_t getCount() const
60 {
61 MYGUI_DEBUG_ASSERT(rollback, "rollback point not valid");
62 return count;
63 }
64
65 size_t getPosition() const
66 {
67 MYGUI_DEBUG_ASSERT(rollback, "rollback point not valid");
68 return position;
69 }
70
71 UString::const_iterator getTextIter() const
72 {
73 MYGUI_DEBUG_ASSERT(rollback, "rollback point not valid");
74 return space_point;
75 }
76
77 private:
78 size_t position{0};
79 UString::const_iterator space_point;
80 size_t count{0};
81 float width{0};
82 bool rollback{false};
83 };
84
85 void TextView::update(
86 const UString& _text,
87 IFont* _font,
88 int _height,
89 Align _align,
90 VertexColourType _format,
91 int _maxWidth)
92 {
93 mFontHeight = _height;
94
95 // array for fast colour conversion
96 static const char convert_colour[64] = {
97 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0,
98 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0};
99
100 mViewSize.clear();
101
102 RollBackPoint roll_back;
103 IntSize result;
104 float width = 0.0f;
105 size_t count = 0;
106 mLength = 0;
107 // Clear line contents while retaining symbol capacity for reuse
108 for (auto& line : mLineInfo)
109 line.clear();
110 size_t current_line = 0;
111 int font_height = _font->getDefaultHeight();
112
113 UString::const_iterator end = _text.end();
114 UString::const_iterator index = _text.begin();
115
116 /*if (index == end)
117 return;*/
118
119 result.height += _height;
120
121 Char previousChar = 0;
122
123 for (; index != end; ++index)
124 {
125 if (current_line == mLineInfo.size())
126 mLineInfo.emplace_back();
127 auto& line_info = mLineInfo[current_line];
128 Char character = *index;
129
130 // new line
131 if (character == FontCodeType::CR || character == FontCodeType::NEL || character == FontCodeType::LF)
132 {
133 if (character == FontCodeType::CR)
134 {
135 UString::const_iterator peeki = index;
136 ++peeki;
137 if ((peeki != end) && (*peeki == FontCodeType::LF))
138 index = peeki; // skip both as one newline
139 }
140
141 line_info.width = (int)std::ceil(width);
142 line_info.count = count;
143 mLength += line_info.count + 1;
144
145 result.height += _height;
146 setMax(result.width, line_info.width);
147 width = 0;
148 count = 0;
149
150 ++current_line;
151
152 roll_back.clear();
153 previousChar = 0;
154
155 continue;
156 }
157 // tag
158 if (character == U'#')
159 {
160 // check next character
161 ++index;
162 if (index == end)
163 {
164 --index;
165 continue;
166 }
167
168 character = *index;
169 // "##" converted to visible '#', change colour otherwise
170 if (character != U'#')
171 {
172 // read first character
173 uint32 colour = convert_colour[(character - 48) & 0x3F];
174
175 // and 5 more after '#'
176 for (char i = 0; i < 5; i++)
177 {
178 ++index;
179 if (index == end)
180 {
181 --index;
182 continue;
183 }
184 colour <<= 4;
185 colour += convert_colour[((*index) - 48) & 0x3F];
186 }
187
188 // convert to ABGR if we use that colour format
189 texture_utility::convertColour(colour, _format);
190
191 line_info.symbols.emplace_back(colour);
192
193 continue;
194 }
195 }
196
197 const GlyphInfo* info = _font->getGlyphInfo(character);
198
199 if (info == nullptr)
200 {
201 previousChar = 0;
202 continue;
203 }
204
205 if (FontCodeType::Space == character || FontCodeType::Tab == character)
206 {
207 roll_back.set(line_info.symbols.size(), index, count, width);
208 }
209
210 float char_width = info->width;
211 float char_height = info->height;
212 float char_advance = info->advance;
213 float char_bearingX = info->bearingX;
214 float char_bearingY = info->bearingY;
215 float kerning = (previousChar != 0) ? _font->getKerning(previousChar, character) : 0.0f;
216
217 if (_height != font_height)
218 {
219 float scale = (float)_height / font_height;
220
221 char_width *= scale;
222 char_height *= scale;
223 char_advance *= scale;
224 char_bearingX *= scale;
225 char_bearingY *= scale;
226 kerning *= scale;
227 }
228
229 float char_fullAdvance = char_advance + kerning;
230
231 // word wrap
232 if (_maxWidth != -1 && (width + char_fullAdvance) > _maxWidth)
233 {
234 if (!roll_back.empty())
235 {
236 // roll back to last space
237 width = roll_back.getWidth();
238 count = roll_back.getCount();
239 index = roll_back.getTextIter();
240 line_info.symbols.erase(
241 line_info.symbols.begin() + roll_back.getPosition(),
242 line_info.symbols.end());
243 }
244
245 // remember rollback point as full line
246 line_info.width = (int)std::ceil(width);
247 line_info.count = count;
248 mLength += line_info.count + 1;
249
250 result.height += _height;
251 setMax(result.width, line_info.width);
252 width = 0;
253 count = 0;
254
255 ++current_line;
256 previousChar = 0;
257
258 // cancel rollback
259 if (!roll_back.empty())
260 roll_back.clear();
261
262 continue;
263 }
264
265 line_info.symbols.emplace_back(
266 info->uvRect,
267 char_width,
268 char_height,
269 char_advance,
270 char_bearingX,
271 char_bearingY,
272 kerning);
273 width += char_fullAdvance;
274 previousChar = character;
275 count++;
276 }
277
278 mLineInfo.resize(current_line + 1);
279 auto& line_info = mLineInfo[current_line];
280 line_info.width = (int)std::ceil(width);
281 line_info.count = count;
282 mLength += line_info.count;
283
284 setMax(result.width, line_info.width);
285
286 // now align lines
287 for (auto& line : mLineInfo)
288 {
289 if (_align.isRight())
290 line.offset = result.width - line.width;
291 else if (_align.isHCenter())
292 line.offset = (result.width - line.width) / 2;
293 }
294
295 mViewSize = result;
296 }
297
298 size_t TextView::getCursorPosition(const IntPoint& _value) const
299 {
300 size_t result = 0;
301 int top = 0;
302
303 for (VectorLineInfo::const_iterator line = mLineInfo.begin(); line != mLineInfo.end(); ++line)
304 {
305 bool lastline = line + 1 == mLineInfo.end();
306
307 if (top + mFontHeight <= _value.top && !lastline)
308 {
309 top += mFontHeight;
310 result += line->count + 1;
311 }
312 else
313 {
314 float left = (float)line->offset;
315 int count = 0;
316
317 for (const auto& sym : line->symbols)
318 {
319 if (sym.isColour())
320 continue;
321
322 float fullAdvance = sym.getAdvance() + sym.getKerning();
323 if (left + fullAdvance / 2.0f > _value.left)
324 {
325 break;
326 }
327 left += fullAdvance;
328 count++;
329 }
330
331 result += count;
332 break;
333 }
334 }
335
336 return result;
337 }
338
339 IntPoint TextView::getCursorPoint(size_t _position) const
340 {
341 setMin(_position, mLength);
342
343 size_t position = 0;
344 int top = 0;
345 float left = 0.0f;
346 for (const auto& line : mLineInfo)
347 {
348 left = (float)line.offset;
349 if (position + line.count >= _position)
350 {
351 for (const auto& sym : line.symbols)
352 {
353 if (sym.isColour())
354 continue;
355
356 if (position == _position)
357 break;
358
359 position++;
360 left += sym.getAdvance() + sym.getKerning();
361 }
362 break;
363 }
364 position += line.count + 1;
365 top += mFontHeight;
366 }
367
368 return {(int)left, top};
369 }
370
371 const IntSize& TextView::getViewSize() const
372 {
373 return mViewSize;
374 }
375
376 size_t TextView::getTextLength() const
377 {
378 return mLength;
379 }
380
381 const VectorLineInfo& TextView::getData() const
382 {
383 return mLineInfo;
384 }
385
386} // namespace MyGUI
#define MYGUI_DEBUG_ASSERT(exp, dest)
void convertColour(uint32 &_colour, VertexColourType _format)
uint32_t uint32
Definition MyGUI_Types.h:47
std::vector< LineInfo > VectorLineInfo
types::TPoint< int > IntPoint
Definition MyGUI_Types.h:26
unsigned int Char
Definition MyGUI_Types.h:49
types::TSize< int > IntSize
Definition MyGUI_Types.h:29