diff --git a/panda/src/text/textFont.cxx b/panda/src/text/textFont.cxx index 92b5730578..4b8b9bd70a 100644 --- a/panda/src/text/textFont.cxx +++ b/panda/src/text/textFont.cxx @@ -128,7 +128,8 @@ calc_width(const string &line) const { // possible). Returns the new string. //////////////////////////////////////////////////////////////////// string TextFont:: -wordwrap_to(const string &text, float wordwrap_width) const { +wordwrap_to(const string &text, float wordwrap_width, + bool preserve_end_whitespace) const { string output_text; size_t p = 0; @@ -150,19 +151,24 @@ wordwrap_to(const string &text, float wordwrap_width) const { bool any_spaces = false; float width = 0.0; - while (q < text.length() && text[q] != '\n' && width <= wordwrap_width) { + while (q < text.length() && text[q] != '\n') { if (isspace(text[q])) { any_spaces = true; } width += calc_width(text[q]); q++; + + if (width > wordwrap_width) { + // Oops, too many. + q--; + break; + } } if (q < text.length() && any_spaces) { // If we stopped because we exceeded the wordwrap width, then // back up to the end of the last complete word. - while (q > p && !isspace(text[q])) { q--; } @@ -193,6 +199,10 @@ wordwrap_to(const string &text, float wordwrap_width) const { output_text += '\n'; } first_line = false; + + if (preserve_end_whitespace) { + q = next_start; + } output_text += text.substr(p, q - p); // Now prepare to wrap the next line. diff --git a/panda/src/text/textFont.h b/panda/src/text/textFont.h index a2a63e4521..f7294d1c8f 100644 --- a/panda/src/text/textFont.h +++ b/panda/src/text/textFont.h @@ -47,7 +47,8 @@ PUBLISHED: float calc_width(char ch) const; float calc_width(const string &line) const; - string wordwrap_to(const string &text, float wordwrap_width) const; + string wordwrap_to(const string &text, float wordwrap_width, + bool preserve_end_whitespace) const; void write(ostream &out, int indent_level) const; diff --git a/panda/src/text/textNode.I b/panda/src/text/textNode.I index f9e8942e60..35d6be65aa 100644 --- a/panda/src/text/textNode.I +++ b/panda/src/text/textNode.I @@ -1055,9 +1055,10 @@ calc_width(const string &line) const { // possible). Returns the new string. //////////////////////////////////////////////////////////////////// INLINE string TextNode:: -wordwrap_to(const string &text, float wordwrap_width) const { +wordwrap_to(const string &text, float wordwrap_width, + bool preserve_end_whitespace) const { nassertr(_font != (TextFont *)NULL, text); - return _font->wordwrap_to(text, wordwrap_width); + return _font->wordwrap_to(text, wordwrap_width, preserve_end_whitespace); } //////////////////////////////////////////////////////////////////// diff --git a/panda/src/text/textNode.cxx b/panda/src/text/textNode.cxx index a44362540f..63bde71acd 100644 --- a/panda/src/text/textNode.cxx +++ b/panda/src/text/textNode.cxx @@ -241,7 +241,7 @@ generate() { string text = _text; if (has_wordwrap()) { - text = wordwrap_to(text, _wordwrap_width); + text = wordwrap_to(text, _wordwrap_width, false); } // Assemble the text. @@ -406,7 +406,7 @@ do_measure() { string text = _text; if (has_wordwrap()) { - text = wordwrap_to(text, _wordwrap_width); + text = wordwrap_to(text, _wordwrap_width, false); } LVector2f ul, lr; diff --git a/panda/src/text/textNode.h b/panda/src/text/textNode.h index c40205772a..883e41523c 100644 --- a/panda/src/text/textNode.h +++ b/panda/src/text/textNode.h @@ -156,7 +156,8 @@ PUBLISHED: INLINE float calc_width(char ch) const; INLINE float calc_width(const string &line) const; - INLINE string wordwrap_to(const string &text, float wordwrap_width) const; + INLINE string wordwrap_to(const string &text, float wordwrap_width, + bool preserve_end_whitespace) const; virtual void write(ostream &out, int indent_level = 0) const;