xoreos  0.0.5
font.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #include "src/common/util.h"
26 #include "src/common/maths.h"
27 
28 #include "src/graphics/types.h"
29 #include "src/graphics/font.h"
30 
31 namespace Graphics {
32 
34 }
35 
37 }
38 
39 float Font::getLineSpacing() const {
40  return 0.0f;
41 }
42 
43 size_t Font::getLineCount(const Common::UString &text, float maxWidth, float maxHeight) const {
44  std::vector<Common::UString> lines;
45 
46  split(text, lines, maxWidth, maxHeight);
47 
48  return lines.size();
49 }
50 
51 float Font::getWidth(const Common::UString &text, float maxWidth) const {
52  std::vector<Common::UString> lines;
53 
54  return split(text, lines, maxWidth);
55 }
56 
57 float Font::getHeight(const Common::UString &text, float maxWidth, float maxHeight) const {
58  size_t lines = getLineCount(text, maxWidth, maxHeight);
59 
60  if (lines == 0)
61  return 0.0f;
62 
63  return (lines * getHeight()) + ((lines - 1) * getLineSpacing());
64 }
65 
67 }
68 
69 float Font::split(const Common::UString &line, std::vector<Common::UString> &lines,
70  float maxWidth, float maxHeight, bool trim) const {
71 
72  if (line.empty())
73  // Nothing to do
74  return 0.0f;
75 
76  if (maxWidth <= 0.0f)
77  maxWidth = FLT_MAX;
78 
79  if (maxHeight <= 0.0f)
80  maxHeight = FLT_MAX;
81 
82  // Wrap the line into several lines of at max maxWidth pixel length, breaking
83  // the line at font-specific word boundaries.
84 
85  float length = 0.0f;
86  float wordLength = 0.0f;
87  float lineLength = 0.0f;
88 
89  Common::UString currentWord;
90  Common::UString currentLine;
91 
92  Common::UString wordColor;
93  Common::UString lineColor;
94 
95  std::vector<Common::UString> tokens;
97 
98  bool plain = false;
99  for (std::vector<Common::UString>::iterator t = tokens.begin(); t != tokens.end(); ++t) {
100  plain = !plain;
101 
102  // Color tokens always have length 0
103  if (!plain) {
104  if (((t->size() == 11) && t->beginsWith("<c") && t->endsWith(">")) ||
105  (*t == "</c>")) {
106 
107  currentWord += *t;
108  wordColor = *t;
109  continue;
110  }
111  }
112 
113  for (Common::UString::iterator p = t->begin(); p != t->end(); ++p) {
114  uint32 c = *p;
115 
116  // Ignore \r
117  if (c == '\r')
118  continue;
119 
120  if (((c == '\n') || Common::UString::isSpace(c)) && !currentWord.empty()) {
121  // We can break and there's already something in the word buffer
122 
123  if ((lineLength + wordLength) > maxWidth) {
124  // Adding the word to the line would overflow
125 
126  // Commit the line first
127  if (!addLine(lines, currentLine, maxHeight))
128  break;
129 
130  length = MAX(length, lineLength);
131 
132  currentLine.clear();
133  lineLength = 0.0f;
134 
135  currentLine += lineColor;
136  }
137 
138  // Add the word to the line
139 
140  currentLine += currentWord;
141  lineLength += wordLength;
142 
143  lineColor = wordColor;
144 
145  currentWord.clear();
146  wordLength = 0.0f;
147  }
148 
149  float charWidth = getWidth(c);
150 
151  if ((wordLength + charWidth) > maxWidth) {
152  // The word itself overflows the max width
153 
154  // Commit the line
155  if (!currentLine.empty()) {
156  if (!addLine(lines, currentLine, maxHeight))
157  break;
158  }
159 
160  // Commit the word fragment in a new line
161  if (!addLine(lines, lineColor + currentWord, maxHeight))
162  break;
163 
164  length = MAX(length, MAX(lineLength, wordLength));
165 
166  currentLine.clear();
167  lineLength = 0.0f;
168 
169  currentWord.clear();
170  wordLength = 0.0f;
171 
172  lineColor = wordColor;
173  currentLine += lineColor;
174  }
175 
176  if (c == '\n') {
177  // Mandatory line break
178 
179  // Commit the line
180  if (!addLine(lines, currentLine, maxHeight))
181  break;
182 
183  length = MAX(length, lineLength);
184 
185  currentLine.clear();
186  lineLength = 0.0f;
187 
188  currentWord.clear();
189  wordLength = 0.0f;
190 
191  currentLine += lineColor;
192  } else {
193  // Add the character to the word
194 
195  wordLength += charWidth;
196 
197  currentWord += c;
198  }
199 
200  }
201 
202  }
203 
204  if (!currentWord.empty()) {
205  // We've got a dangling word fragment
206 
207  if ((lineLength + wordLength) > maxWidth && addLine(lines, currentLine, maxHeight)) {
208  // The dangling word would overflow the line, commit that first
209 
210  length = MAX(length, lineLength);
211 
212  currentLine.clear();
213  lineLength = 0.0f;
214 
215  currentLine += lineColor;
216  }
217 
218  // Add the dangling word to the line
219 
220  currentLine += currentWord;
221  lineLength += wordLength;
222  }
223 
224  if (!currentLine.empty() && addLine(lines, currentLine, maxHeight)) {
225  // We've got a dangling line, commit it
226  length = MAX(length, lineLength);
227  }
228 
229  // Trim the lines
230  if (trim)
231  for (std::vector<Common::UString>::iterator l = lines.begin(); l != lines.end(); ++l)
232  l->trim();
233 
234  return length;
235 }
236 
237 float Font::split(Common::UString &line, float maxWidth, float maxHeight, bool trim) const {
238  return split(line, line, maxWidth, maxHeight, trim);
239 }
240 
241 float Font::split(const Common::UString &line, Common::UString &lines, float maxWidth, float maxHeight, bool trim) const {
242  std::vector<Common::UString> sLines;
243 
244  float width = split(line, sLines, maxWidth, maxHeight, trim);
245 
246  lines.clear();
247  for (std::vector<Common::UString>::const_iterator l = sLines.begin(); l != sLines.end(); ++l) {
248  if (!lines.empty())
249  lines += '\n';
250 
251  lines += *l;
252  }
253 
254  return width;
255 }
256 
257 float Font::getLineWidth(const Common::UString &text) const {
258  float width = 0.0f;
259 
260  for (Common::UString::iterator c = text.begin(); c != text.end(); ++c)
261  width += getWidth(*c);
262 
263  return width;
264 }
265 
266 bool Font::addLine(std::vector<Common::UString> &lines, const Common::UString &newLine,
267  float maxHeight) const {
268 
269  if ((lines.size() + 1) * getHeight() <= maxHeight) {
270  lines.push_back(newLine);
271  return true;
272  }
273 
274  return false;
275 }
276 
277 } // End of namespace Graphics
virtual ~Font()
Definition: font.cpp:36
A class holding an UTF-8 string.
Definition: ustring.h:48
float getLineWidth(const Common::UString &text) const
Return the width of this string.
Definition: font.cpp:257
virtual float getLineSpacing() const
Return the size of space between lines.
Definition: font.cpp:39
Mathematical helpers.
size_t getLineCount(const Common::UString &text, float maxWidth=0.0f, float maxHeight=0.0f) const
Return the number of lines this text spans.
Definition: font.cpp:43
float split(const Common::UString &line, std::vector< Common::UString > &lines, float maxWidth=0.0f, float maxHeight=0.0f, bool trim=true) const
Definition: font.cpp:69
iterator begin() const
Definition: ustring.cpp:253
Basic graphics types.
bool addLine(std::vector< Common::UString > &lines, const Common::UString &newLine, float maxHeight) const
Definition: font.cpp:266
utf8::iterator< std::string::const_iterator > iterator
Definition: ustring.h:50
#define UNUSED(x)
Definition: system.h:170
Utility templates and functions.
static void splitTextTokens(const UString &text, std::vector< UString > &tokens)
Definition: ustring.cpp:645
bool empty() const
Is the string empty?
Definition: ustring.cpp:245
uint32_t uint32
Definition: types.h:204
iterator end() const
Definition: ustring.cpp:257
virtual void buildChars(const Common::UString &str)
Build all necessary characters to display this string.
Definition: font.cpp:66
T MAX(T a, T b)
Definition: util.h:71
virtual float getHeight() const =0
Return the height of a character.
void clear()
Clear the string&#39;s contents.
Definition: ustring.cpp:236
A font.
#define FLT_MAX
Definition: maths.h:47
virtual float getWidth(uint32 c) const =0
Return the width of a character.
static bool isSpace(uint32 c)
Is the character an ASCII space character?
Definition: ustring.cpp:789