mirror of
https://github.com/panda3d/panda3d.git
synced 2025-10-19 05:06:25 -04:00
improved wordwrapping functionality
This commit is contained in:
parent
d710dce638
commit
1d0db9558a
@ -128,7 +128,8 @@ calc_width(const string &line) const {
|
|||||||
// possible). Returns the new string.
|
// possible). Returns the new string.
|
||||||
////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////
|
||||||
string TextFont::
|
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;
|
string output_text;
|
||||||
|
|
||||||
size_t p = 0;
|
size_t p = 0;
|
||||||
@ -150,19 +151,24 @@ wordwrap_to(const string &text, float wordwrap_width) const {
|
|||||||
bool any_spaces = false;
|
bool any_spaces = false;
|
||||||
|
|
||||||
float width = 0.0;
|
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])) {
|
if (isspace(text[q])) {
|
||||||
any_spaces = true;
|
any_spaces = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
width += calc_width(text[q]);
|
width += calc_width(text[q]);
|
||||||
q++;
|
q++;
|
||||||
|
|
||||||
|
if (width > wordwrap_width) {
|
||||||
|
// Oops, too many.
|
||||||
|
q--;
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (q < text.length() && any_spaces) {
|
if (q < text.length() && any_spaces) {
|
||||||
// If we stopped because we exceeded the wordwrap width, then
|
// If we stopped because we exceeded the wordwrap width, then
|
||||||
// back up to the end of the last complete word.
|
// back up to the end of the last complete word.
|
||||||
|
|
||||||
while (q > p && !isspace(text[q])) {
|
while (q > p && !isspace(text[q])) {
|
||||||
q--;
|
q--;
|
||||||
}
|
}
|
||||||
@ -193,6 +199,10 @@ wordwrap_to(const string &text, float wordwrap_width) const {
|
|||||||
output_text += '\n';
|
output_text += '\n';
|
||||||
}
|
}
|
||||||
first_line = false;
|
first_line = false;
|
||||||
|
|
||||||
|
if (preserve_end_whitespace) {
|
||||||
|
q = next_start;
|
||||||
|
}
|
||||||
output_text += text.substr(p, q - p);
|
output_text += text.substr(p, q - p);
|
||||||
|
|
||||||
// Now prepare to wrap the next line.
|
// Now prepare to wrap the next line.
|
||||||
|
@ -47,7 +47,8 @@ PUBLISHED:
|
|||||||
|
|
||||||
float calc_width(char ch) const;
|
float calc_width(char ch) const;
|
||||||
float calc_width(const string &line) 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;
|
void write(ostream &out, int indent_level) const;
|
||||||
|
|
||||||
|
@ -1055,9 +1055,10 @@ calc_width(const string &line) const {
|
|||||||
// possible). Returns the new string.
|
// possible). Returns the new string.
|
||||||
////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////
|
||||||
INLINE string TextNode::
|
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);
|
nassertr(_font != (TextFont *)NULL, text);
|
||||||
return _font->wordwrap_to(text, wordwrap_width);
|
return _font->wordwrap_to(text, wordwrap_width, preserve_end_whitespace);
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////
|
||||||
|
@ -241,7 +241,7 @@ generate() {
|
|||||||
|
|
||||||
string text = _text;
|
string text = _text;
|
||||||
if (has_wordwrap()) {
|
if (has_wordwrap()) {
|
||||||
text = wordwrap_to(text, _wordwrap_width);
|
text = wordwrap_to(text, _wordwrap_width, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Assemble the text.
|
// Assemble the text.
|
||||||
@ -406,7 +406,7 @@ do_measure() {
|
|||||||
|
|
||||||
string text = _text;
|
string text = _text;
|
||||||
if (has_wordwrap()) {
|
if (has_wordwrap()) {
|
||||||
text = wordwrap_to(text, _wordwrap_width);
|
text = wordwrap_to(text, _wordwrap_width, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
LVector2f ul, lr;
|
LVector2f ul, lr;
|
||||||
|
@ -156,7 +156,8 @@ PUBLISHED:
|
|||||||
|
|
||||||
INLINE float calc_width(char ch) const;
|
INLINE float calc_width(char ch) const;
|
||||||
INLINE float calc_width(const string &line) 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;
|
virtual void write(ostream &out, int indent_level = 0) const;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user