diff --git a/panda/src/text/textFont.cxx b/panda/src/text/textFont.cxx index 57ef2f27a5..49d50ac2b0 100644 --- a/panda/src/text/textFont.cxx +++ b/panda/src/text/textFont.cxx @@ -131,7 +131,7 @@ calc_width(const string &line) const { //////////////////////////////////////////////////////////////////// string TextFont:: wordwrap_to(const string &text, float wordwrap_width, - bool preserve_end_whitespace) const { + bool preserve_trailing_whitespace) const { string output_text; size_t p = 0; @@ -141,7 +141,7 @@ wordwrap_to(const string &text, float wordwrap_width, output_text += text[p]; p++; } - bool first_line = true; + bool needs_newline = false; while (p < text.length()) { nassertr(!isspace(text[p]), ""); @@ -151,6 +151,7 @@ wordwrap_to(const string &text, float wordwrap_width, size_t q = p; bool any_spaces = false; + bool overflow = false; float width = 0.0; while (q < text.length() && text[q] != '\n') { @@ -164,11 +165,12 @@ wordwrap_to(const string &text, float wordwrap_width, if (width > wordwrap_width) { // Oops, too many. q--; + overflow = true; break; } } - if (q < text.length() && any_spaces) { + if (overflow && 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])) { @@ -196,13 +198,13 @@ wordwrap_to(const string &text, float wordwrap_width, next_start++; } } - - if (!first_line) { + + if (needs_newline) { output_text += '\n'; } - first_line = false; + needs_newline = true; - if (preserve_end_whitespace) { + if (preserve_trailing_whitespace) { q = next_start; } output_text += text.substr(p, q - p); @@ -210,8 +212,10 @@ wordwrap_to(const string &text, float wordwrap_width, // Now prepare to wrap the next line. if (next_start < text.length() && text[next_start] == '\n') { - // Skip a single embedded newline. + // Preserve a single embedded newline. + output_text += '\n'; next_start++; + needs_newline = false; } p = next_start; diff --git a/panda/src/text/textFont.h b/panda/src/text/textFont.h index f7294d1c8f..a5bf57c528 100644 --- a/panda/src/text/textFont.h +++ b/panda/src/text/textFont.h @@ -48,7 +48,7 @@ PUBLISHED: float calc_width(char ch) const; float calc_width(const string &line) const; string wordwrap_to(const string &text, float wordwrap_width, - bool preserve_end_whitespace) const; + bool preserve_trailing_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 35d6be65aa..cf0309f33f 100644 --- a/panda/src/text/textNode.I +++ b/panda/src/text/textNode.I @@ -1056,9 +1056,9 @@ calc_width(const string &line) const { //////////////////////////////////////////////////////////////////// INLINE string TextNode:: wordwrap_to(const string &text, float wordwrap_width, - bool preserve_end_whitespace) const { + bool preserve_trailing_whitespace) const { nassertr(_font != (TextFont *)NULL, text); - return _font->wordwrap_to(text, wordwrap_width, preserve_end_whitespace); + return _font->wordwrap_to(text, wordwrap_width, preserve_trailing_whitespace); } //////////////////////////////////////////////////////////////////// diff --git a/panda/src/text/textNode.h b/panda/src/text/textNode.h index 883e41523c..63c8fa5392 100644 --- a/panda/src/text/textNode.h +++ b/panda/src/text/textNode.h @@ -157,7 +157,7 @@ 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, - bool preserve_end_whitespace) const; + bool preserve_trailing_whitespace) const; virtual void write(ostream &out, int indent_level = 0) const;