| 1 |
#ifndef TextParts_h__ |
| 2 |
#define TextParts_h__ |
| 3 |
|
| 4 |
#include "ifonts.h" |
| 5 |
#include "math/Vector2.h" |
| 6 |
#include <string> |
| 7 |
|
| 8 |
namespace gui |
| 9 |
{ |
| 10 |
|
| 11 |
// A single 2D vertex, including U/V coordinates |
| 12 |
struct Vertex2D |
| 13 |
{ |
| 14 |
Vector2 vertex; |
| 15 |
Vector2 texcoord; |
| 16 |
}; |
| 17 |
|
| 18 |
class TextChar |
| 19 |
{ |
| 20 |
private: |
| 21 |
// The actual character |
| 22 |
char _char; |
| 23 |
|
| 24 |
// The font scale needed for the width calculation |
| 25 |
float _fontScale; |
| 26 |
public: |
| 27 |
fonts::IGlyphInfoPtr glyph; |
| 28 |
|
| 29 |
// The 4 coordinates, marking the quad (3D and UV) |
| 30 |
Vertex2D coords[4]; |
| 31 |
|
| 32 |
TextChar(unsigned char c, fonts::IGlyphSet& glyphs, float fontScale) : |
| 33 |
_char(c), |
| 34 |
_fontScale(fontScale) |
| 35 |
{ |
| 36 |
glyph = glyphs.getGlyph(static_cast<std::size_t>(c)); |
| 37 |
|
| 38 |
coords[0].vertex = Vector2(0, -glyph->top * _fontScale); |
| 39 |
coords[1].vertex = Vector2(glyph->imageWidth * _fontScale, -glyph->top * _fontScale); |
| 40 |
coords[2].vertex = Vector2(glyph->imageWidth * _fontScale, (-glyph->top + glyph->imageHeight) * _fontScale); |
| 41 |
coords[3].vertex = Vector2(0, (-glyph->top + glyph->imageHeight) * _fontScale); |
| 42 |
|
| 43 |
coords[0].texcoord = Vector2(glyph->s, glyph->t); |
| 44 |
coords[1].texcoord = Vector2(glyph->s2, glyph->t); |
| 45 |
coords[2].texcoord = Vector2(glyph->s2, glyph->t2); |
| 46 |
coords[3].texcoord = Vector2(glyph->s, glyph->t2); |
| 47 |
} |
| 48 |
|
| 49 |
double getWidth() const |
| 50 |
{ |
| 51 |
return glyph->xSkip * _fontScale; |
| 52 |
} |
| 53 |
|
| 54 |
// Offsets all coordinates by the given vector |
| 55 |
void offset(const Vector2& off) |
| 56 |
{ |
| 57 |
coords[0].vertex += off; |
| 58 |
coords[1].vertex += off; |
| 59 |
coords[2].vertex += off; |
| 60 |
coords[3].vertex += off; |
| 61 |
} |
| 62 |
|
| 63 |
char getChar() const |
| 64 |
{ |
| 65 |
return _char; |
| 66 |
} |
| 67 |
}; |
| 68 |
|
| 69 |
class TextWord : |
| 70 |
public std::vector<TextChar> |
| 71 |
{ |
| 72 |
public: |
| 73 |
double getWidth() const |
| 74 |
{ |
| 75 |
double totalWidth = 0; |
| 76 |
|
| 77 |
for (TextWord::const_iterator i = begin(); i != end(); ++i) |
| 78 |
{ |
| 79 |
totalWidth += i->getWidth(); |
| 80 |
} |
| 81 |
|
| 82 |
return totalWidth; |
| 83 |
} |
| 84 |
|
| 85 |
void offset(const Vector2& off) |
| 86 |
{ |
| 87 |
for (TextWord::iterator i = begin(); i != end(); ++i) |
| 88 |
{ |
| 89 |
i->offset(off); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
// Named constructor, creates a word out of the given string for the given glyphset |
| 94 |
// The word is positioned at 0,0 after construction. |
| 95 |
static TextWord createForString(const std::string& string, fonts::IGlyphSet& glyphs, float fontScale) |
| 96 |
{ |
| 97 |
TextWord result; |
| 98 |
Vector2 wordPos(0,0); |
| 99 |
|
| 100 |
for (std::string::const_iterator i = string.begin(); i != string.end(); ++i) |
| 101 |
{ |
| 102 |
result.push_back(TextChar(*i, glyphs, fontScale)); |
| 103 |
|
| 104 |
// Move character to position in word |
| 105 |
result.back().offset(wordPos); |
| 106 |
|
| 107 |
// Increase position pointer |
| 108 |
wordPos.x() += result.back().getWidth(); |
| 109 |
} |
| 110 |
|
| 111 |
return result; |
| 112 |
} |
| 113 |
}; |
| 114 |
|
| 115 |
class TextLine |
| 116 |
{ |
| 117 |
public: |
| 118 |
typedef std::vector<TextChar> Chars; |
| 119 |
|
| 120 |
private: |
| 121 |
// The width of the current line |
| 122 |
const double _lineWidth; |
| 123 |
|
| 124 |
Chars _chars; |
| 125 |
|
| 126 |
// The total width of all characters |
| 127 |
double _charWidth; |
| 128 |
|
| 129 |
// The font scale of this line |
| 130 |
float _fontScale; |
| 131 |
|
| 132 |
public: |
| 133 |
TextLine(double width, float fontScale) : |
| 134 |
_lineWidth(width), |
| 135 |
_charWidth(0), |
| 136 |
_fontScale(fontScale) |
| 137 |
{} |
| 138 |
|
| 139 |
bool empty() const |
| 140 |
{ |
| 141 |
return _chars.empty(); |
| 142 |
} |
| 143 |
|
| 144 |
const Chars& getChars() const |
| 145 |
{ |
| 146 |
return _chars; |
| 147 |
} |
| 148 |
|
| 149 |
double getWidth() const |
| 150 |
{ |
| 151 |
return _charWidth; |
| 152 |
} |
| 153 |
|
| 154 |
// Add a word to this line |
| 155 |
// returns TRUE on success, FALSE if the word is too wide to fit in this line |
| 156 |
bool addWord(const std::string& word, fonts::IGlyphSet& glyphs, bool noclip = false) |
| 157 |
{ |
| 158 |
// Generate the word |
| 159 |
TextWord tw = TextWord::createForString(word, glyphs, _fontScale); |
| 160 |
return addWord(tw, noclip); |
| 161 |
} |
| 162 |
|
| 163 |
// Adds a single character to this sentence |
| 164 |
// returns TRUE if the character fits, FALSE otherwise (character not added on FALSE). |
| 165 |
// Override the noclip argument with true to ignore width checks |
| 166 |
bool addChar(const char c, fonts::IGlyphSet& glyphs, bool noclip = false) |
| 167 |
{ |
| 168 |
// Generate the character |
| 169 |
TextChar tc(c, glyphs, _fontScale); |
| 170 |
return addChar(tc, noclip); |
| 171 |
} |
| 172 |
|
| 173 |
// Offsets all character coordinates by the given vector |
| 174 |
void offset(const Vector2& off) |
| 175 |
{ |
| 176 |
for (Chars::iterator i = _chars.begin(); i != _chars.end(); ++i) |
| 177 |
{ |
| 178 |
i->offset(off); |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
// If the line ends with a single space, this method will remove it |
| 183 |
void removeTrailingSpace() |
| 184 |
{ |
| 185 |
if (_chars.empty()) return; |
| 186 |
|
| 187 |
if (_chars.rbegin()->getChar() == ' ') |
| 188 |
{ |
| 189 |
_charWidth -= _chars.rbegin()->getWidth(); |
| 190 |
_chars.pop_back(); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
private: |
| 195 |
bool addChar(TextChar& ch, bool noclip) |
| 196 |
{ |
| 197 |
// Check the word length |
| 198 |
double remainingWidth = _lineWidth - _charWidth; |
| 199 |
double charWidth = ch.getWidth(); |
| 200 |
|
| 201 |
if (!noclip && charWidth > remainingWidth + 1e-3) |
| 202 |
{ |
| 203 |
return false; |
| 204 |
} |
| 205 |
|
| 206 |
// Move the word to the current position |
| 207 |
ch.offset(Vector2(_charWidth, 0)); |
| 208 |
|
| 209 |
// Append the characters to our own vector |
| 210 |
_chars.push_back(ch); |
| 211 |
|
| 212 |
// Increase horizontal position |
| 213 |
_charWidth += charWidth; |
| 214 |
|
| 215 |
return true; |
| 216 |
} |
| 217 |
|
| 218 |
bool addWord(TextWord& word, bool noclip) |
| 219 |
{ |
| 220 |
// Check the word length |
| 221 |
double remainingWidth = _lineWidth - _charWidth; |
| 222 |
double wordWidth = word.getWidth(); |
| 223 |
|
| 224 |
// stgatilov: bug compatibility against Doom 3 engine |
| 225 |
// it ignores the last character when checking whether a word fits the line |
| 226 |
// see also: #5914 and https://forums.thedarkmod.com/index.php?/topic/21710-implicit-linebreaks-in-text/ |
| 227 |
double wordWidthFit = wordWidth; |
| 228 |
if (!word.empty()) |
| 229 |
{ |
| 230 |
wordWidthFit -= word.back().getWidth(); |
| 231 |
} |
| 232 |
|
| 233 |
if (!noclip && wordWidthFit > remainingWidth + 1e-3) |
| 234 |
{ |
| 235 |
return false; |
| 236 |
} |
| 237 |
|
| 238 |
// Move the word to the current position |
| 239 |
word.offset(Vector2(_charWidth, 0)); |
| 240 |
|
| 241 |
// Append the characters to our own vector |
| 242 |
_chars.insert(_chars.end(), word.begin(), word.end()); |
| 243 |
|
| 244 |
// Increase horizontal position |
| 245 |
_charWidth += wordWidth; |
| 246 |
|
| 247 |
return true; |
| 248 |
} |
| 249 |
}; |
| 250 |
typedef std::shared_ptr<TextLine> TextLinePtr; |
| 251 |
|
| 252 |
} // namespace |
| 253 |
|
| 254 |
#endif // TextParts_h__ |