void
Console::addLine(std::string s)
{
- std::cerr << s << std::endl;
- while (s.length() > 99) {
- lines.push_front(s.substr(0, 99-3)+"...");
- s = "..."+s.substr(99-3);
- }
- lines.push_front(s);
-
+ std::string overflow;
+ do {
+ lines.push_front(Font::wrap_to_chars(s, 99, &overflow));
+ s = overflow;
+ } while (s.length() > 0);
+
while (lines.size() >= 1000)
lines.pop_back();
continue;
}
- // if we are dealing with text, wrap long lines
- while ((int)s.length() > line_length) {
- int split_at = line_length;
- while ((split_at > 0) && (s[split_at] != ' ')) split_at--;
- if (split_at == 0) split_at = line_length;
-
- lines.push_back(new InfoBoxLine(format_char, s.substr(0, split_at)));
- if (s[split_at] == ' ') split_at++;
- s = s.substr(split_at);
- }
- lines.push_back(new InfoBoxLine(format_char, s));
+ // append wrapped parts of line into list
+ std::string overflow;
+ do {
+ lines.push_back(new InfoBoxLine(format_char, Font::wrap_to_chars(s, line_length, &overflow)));
+ s = overflow;
+ } while (s.length() > 0);
}
return h;
}
+std::string
+Font::wrap_to_width(const std::string& s, int max_width, std::string* overflow) const
+{
+ return wrap_to_chars(s, max_width / w, overflow);
+}
+
+std::string
+Font::wrap_to_chars(const std::string& s, int line_length, std::string* overflow)
+{
+ // if text is already smaller, return full text
+ if ((int)s.length() <= line_length) {
+ if (overflow) *overflow = "";
+ return s;
+ }
+
+ // if we can find a whitespace character to break at, return text up to this character
+ int i = line_length;
+ while ((i > 0) && (s[i] != ' ')) i--;
+ if (i > 0) {
+ if (overflow) *overflow = s.substr(i+1);
+ return s.substr(0, i);
+ }
+
+ // FIXME: wrap at line_length, taking care of multibyte characters
+ if (overflow) *overflow = "";
+ return s;
+}
+
void
Font::draw(const std::string& text, const Vector& pos_, FontAlignment alignment,
DrawingEffect drawing_effect, float alpha) const
float get_text_height(const std::string& text) const;
/// returns the height of the font.
float get_height() const;
+
+ /**
+ * returns the given string, truncated (preferrably at whitespace) to be at most max_width pixels long
+ */
+ std::string wrap_to_width(const std::string& text, int max_width, std::string* overflow) const;
+
+ /**
+ * returns the given string, truncated (preferrably at whitespace) to be at most max_chars characters long
+ */
+ static std::string wrap_to_chars(const std::string& text, int max_chars, std::string* overflow);
/** Draws the given text to the screen. Also needs the position.
* Type of alignment, drawing effect and alpha are optional. */