improved wordwrapping functionality

This commit is contained in:
David Rose 2001-07-11 18:45:37 +00:00
parent d710dce638
commit 1d0db9558a
5 changed files with 22 additions and 9 deletions

View File

@ -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.

View File

@ -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;

View File

@ -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);
}
////////////////////////////////////////////////////////////////////

View File

@ -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;

View File

@ -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;