/
RenderableText.cpp
304 lines (245 loc) · 7.4 KB
/
RenderableText.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include "RenderableText.h"
#include "itextstream.h"
#include "registry/registry.h"
#include "math/Matrix4.h"
#include <vector>
#include <list>
#include "gamelib.h"
#include "string/split.h"
#include "TextParts.h"
#include "GuiWindowDef.h"
#include "math/FloatTools.h"
namespace gui
{
namespace
{
const std::string GKEY_SMALLFONT_LIMIT("/defaults/guiSmallFontLimit");
const std::string GKEY_MEDIUMFONT_LIMIT("/defaults/guiMediumFontLimit");
}
RenderableText::RenderableText(const IGuiWindowDef& owner) :
_owner(owner)
{}
void RenderableText::printMissingGlyphSetError() const
{
rConsoleError() << "[dm.gui] Font '" << _font->getName() << "'"
<< " does not have glyph set for resolution "
<< _resolution << std::endl;
}
void RenderableText::realiseFontShaders()
{
fonts::IGlyphSetPtr glyphSet = _font->getGlyphSet(_resolution);
if (glyphSet)
{
glyphSet->realiseShaders();
}
else
{
printMissingGlyphSetError();
}
}
void RenderableText::render()
{
// Add each renderable character batch to the collector
for (CharBatches::const_iterator i = _charBatches.begin(); i != _charBatches.end(); ++i)
{
// Switch to this shader
glBindTexture(GL_TEXTURE_2D, i->first->getMaterial()->getEditorImage()->getGLTexNum());
// Submit geometry
i->second->render();
}
}
void RenderableText::recompile()
{
_charBatches.clear();
ensureFont();
if (_font == NULL) return; // Rendering not possible
std::string text = _owner.getText();
typedef std::vector<TextLinePtr> TextLines;
TextLines lines;
// Split the text into paragraphs
std::vector<std::string> paragraphs;
string::split(paragraphs, text, "\n", false);
fonts::IGlyphSetPtr gsp = _font->getGlyphSet(_resolution);
if (!gsp)
{
printMissingGlyphSetError();
return;
}
fonts::IGlyphSet& glyphSet = *gsp;
// Calculate the final scale of the glyphs
float scale = _owner.textscale * glyphSet.getGlyphScale();
// We need the maximum glyph height of the highest resolution font to calculate the line width
std::size_t maxGlyphHeight = _font->getGlyphSet(fonts::Resolution48)->getMaxGlyphHeight();
// Calculate the line height, this is usually max glyph height + 5 pixels
double lineHeight = lrint(_owner.textscale * maxGlyphHeight + 5);
// The distance from the top of the rectangle to the baseline
double startingBaseLine = lrint(_owner.textscale * maxGlyphHeight + 2) + _owner.textaligny;
for (std::size_t p = 0; p < paragraphs.size(); ++p)
{
// Check if more lines are possible, if at least one line is in the rectangle
if (!lines.empty())
{
double curYPos = lineHeight * lines.size() + startingBaseLine;
if (curYPos > _owner.rect[3] - 2) break;
}
// Split the paragraphs into words
std::list<std::string> words;
string::split(words, paragraphs[p], " \t", false);
// Add the words to lines
TextLinePtr curLine(new TextLine(_owner.rect[2] - 2 - _owner.textalignx, scale));
while (!words.empty())
{
// Empty words are stemming from an extra space character, re-add that
if (words.front().empty())
{
curLine->addChar(' ', glyphSet, true);
words.pop_front();
continue;
}
// If nowrap set to true, force words into this line
bool added = curLine->addWord(words.front(), glyphSet, _owner.nowrap);
if (added)
{
words.pop_front();
if (!words.empty())
{
// Add a space after each word (noclipped) if more words are following
curLine->addChar(' ', glyphSet, true);
}
continue;
}
// Not added
if (curLine->empty())
{
// Line empty, but still not fitting, do it character-wise
std::string word = words.front();
words.pop_front();
while (!word.empty())
{
// Take the first character
if (curLine->addChar(word[0], glyphSet))
{
// Character added, remove from string
word.erase(0, 1);
continue;
}
else
{
// Not enough space, add one more character then break
curLine->addChar(word[0], glyphSet, true);
word.erase(0, 1);
break;
}
}
// add the rest of the word to the front of the queue
if (!word.empty())
{
words.push_front(word);
}
}
// Trim any extra space from the end of the line
curLine->removeTrailingSpace();
// Line finished, consider alignment and vertical offset
curLine->offset(Vector2(
getAlignmentCorrection(curLine->getWidth()) + _owner.textalignx, // horizontal correction
lineHeight * lines.size() + startingBaseLine // vertical correction
));
lines.push_back(curLine);
// Clear the current line
curLine = TextLinePtr();
// Check if more lines are possible
double curYPos = lineHeight * lines.size() + startingBaseLine;
if (curYPos > _owner.rect[3] - 2) break;
// Allocate a new line, but only if we have any more words in this paragraph
if (!words.empty())
{
curLine = TextLinePtr(new TextLine(_owner.rect[2] - 2 - _owner.textalignx, scale));
}
}
if (curLine != NULL)
{
// Trim any extra space from the end of the line
curLine->removeTrailingSpace();
// Add that line we started, even if it's an empty one
curLine->offset(
Vector2(
getAlignmentCorrection(curLine->getWidth()) + _owner.textalignx,
lineHeight * lines.size() + + startingBaseLine
)
);
lines.push_back(curLine);
}
}
// Now sort the aligned characters into separate renderables, one per shader
for (TextLines::const_iterator line = lines.begin(); line != lines.end(); ++line)
{
// Move the lines into our GUI rectangle
(*line)->offset(Vector2(_owner.rect[0], _owner.rect[1]));
for (TextLine::Chars::const_iterator c = (*line)->getChars().begin();
c != (*line)->getChars().end(); ++c)
{
CharBatches::iterator batch = _charBatches.find(c->glyph->shader);
if (batch == _charBatches.end())
{
RenderableCharacterBatchPtr b(new RenderableCharacterBatch);
std::pair<CharBatches::iterator, bool> result = _charBatches.insert(
CharBatches::value_type(c->glyph->shader, b)
);
batch = result.first;
}
batch->second->addGlyph(*c);
}
}
// Compile the vertex buffer objects
for (CharBatches::iterator i = _charBatches.begin(); i != _charBatches.end(); ++i)
{
i->second->compile();
}
}
double RenderableText::getAlignmentCorrection(double lineWidth)
{
double xoffset = 0;
switch (_owner.textalign)
{
case 0: // left
// Somehow D3 adds a 2 pixel offset to the left (see idSimpleWindow::CalcClientRect)
xoffset = 2;
break;
case 1: // center
// Somehow D3 adds a 1 pixel offset to the left
xoffset = 1 + (_owner.rect[2] - lineWidth) / 2;
break;
case 2: // right
xoffset = _owner.rect[2] - 2 - lineWidth;
break;
};
return xoffset;
}
void RenderableText::ensureFont()
{
if (_owner.font.empty()) return; // no font specified
if (_font != NULL) return; // already realised
_font = GlobalFontManager().findFontInfo(_owner.font);
if (_font == NULL)
{
rWarning() << "Cannot find font " << _owner.font
<< " in windowDef " << _owner.name << std::endl;
return;
}
// Determine resolution
if (_owner.textscale <= game::current::getValue<float>(GKEY_SMALLFONT_LIMIT))
{
_resolution = fonts::Resolution12;
}
else if (_owner.textscale <= game::current::getValue<float>(GKEY_MEDIUMFONT_LIMIT))
{
_resolution = fonts::Resolution24;
}
else
{
_resolution = fonts::Resolution48;
}
// Ensure that the font shaders are realised
realiseFontShaders();
}
} // namespace