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