From cea96e02f596a749eaf96637dbec6976fe7f4d24 Mon Sep 17 00:00:00 2001 From: Christoph Sommer Date: Fri, 26 May 2006 13:47:23 +0000 Subject: [PATCH] Worked around bug with wrappping utf-8 text / Made wrapping function a method of Font SVN-Revision: 3593 --- src/console.cpp | 13 ++++++------- src/textscroller.cpp | 17 ++++++----------- src/video/font.cpp | 28 ++++++++++++++++++++++++++++ src/video/font.hpp | 10 ++++++++++ 4 files changed, 50 insertions(+), 18 deletions(-) diff --git a/src/console.cpp b/src/console.cpp index ee6f843ab..6e3e49f8d 100644 --- a/src/console.cpp +++ b/src/console.cpp @@ -202,13 +202,12 @@ Console::autocomplete() 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(); diff --git a/src/textscroller.cpp b/src/textscroller.cpp index 0f0a5c154..ecf8fc765 100644 --- a/src/textscroller.cpp +++ b/src/textscroller.cpp @@ -287,17 +287,12 @@ InfoBoxLine::split(const std::string& text, int line_length) 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); } diff --git a/src/video/font.cpp b/src/video/font.cpp index 5fe050a0a..42aa45583 100644 --- a/src/video/font.cpp +++ b/src/video/font.cpp @@ -95,6 +95,34 @@ Font::get_height() const 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 diff --git a/src/video/font.hpp b/src/video/font.hpp index 9300fd46c..c39df1aaa 100644 --- a/src/video/font.hpp +++ b/src/video/font.hpp @@ -53,6 +53,16 @@ public: 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. */ -- 2.11.0