From Terry Welsh, "As discussed on the osg-users list, I have implemented these rules in

Text and Text3D:

1.  A new line should be started after a line's last hyphen or before
its last whitespace.
2.  If no suitable place to break a line is found, just start new line
after the last character that fits on the line.
3.  Whitespace should be removed from the beginning of the new line
(already worked in Text, but not in Text3D).

Line wrapping looks a lot better now with no more lone periods
appearing at the beginning of lines.

Also, right-justified text is more accurate now (slashes would hang
off the end of lines before).  With this new code I spotted one
instance where a hyphen stuck out too far, but in general it looks
better.  Centered text was not perfect before and still isn't, but I
can't see any significant increase or decrease in quality.  The casual
observer would probably never notice a problem.

Also fixed a whitespace problem in Text3D.  Not all whitespace was
being removed from the beginning of lines.  Now it is all being
removed in the same manner as in Text."
This commit is contained in:
Robert Osfield 2010-02-25 18:17:20 +00:00
parent 7ca071192e
commit bc202cd4bb
2 changed files with 40 additions and 50 deletions

View File

@ -119,16 +119,6 @@ String::iterator Text::computeLastCharacterOnLine(osg::Vec2& cursor, String::ite
String::iterator lastChar = first;
std::set<unsigned int> deliminatorSet;
deliminatorSet.insert(' ');
deliminatorSet.insert('\n');
deliminatorSet.insert(':');
deliminatorSet.insert('/');
deliminatorSet.insert(',');
deliminatorSet.insert(';');
deliminatorSet.insert(':');
deliminatorSet.insert('.');
for(bool outOfSpace=false;lastChar!=last;++lastChar)
{
unsigned int charcode = *lastChar;
@ -213,19 +203,27 @@ String::iterator Text::computeLastCharacterOnLine(osg::Vec2& cursor, String::ite
}
}
// word boundary detection & wrapping
if (lastChar!=last)
{
if (deliminatorSet.count(*lastChar)==0)
{
String::iterator lastValidChar = lastChar;
while (lastValidChar!=first && deliminatorSet.count(*lastValidChar)==0)
String::iterator lastValidChar = lastChar;
String::iterator prevChar;
while (lastValidChar != first){
prevChar = lastValidChar - 1;
// last char is after a hyphen
if(*lastValidChar == '-')
return lastValidChar + 1;
// last char is start of whitespace
if((*lastValidChar == ' ' || *lastValidChar == '\n') && (*prevChar != ' ' && *prevChar != '\n'))
return lastValidChar;
// Subtract off glyphs from the cursor position (to correctly center text)
if(*prevChar != '-')
{
--lastValidChar;
// Subtract off glyphs from the cursor position (to correctly center text)
Font::Glyph* glyph = activefont->getGlyph(_fontSize, *lastValidChar);
Font::Glyph* glyph = activefont->getGlyph(_fontSize, *prevChar);
if (glyph)
{
switch(_layout)
@ -236,12 +234,9 @@ String::iterator Text::computeLastCharacterOnLine(osg::Vec2& cursor, String::ite
}
}
}
if (first!=lastValidChar)
{
++lastValidChar;
lastChar = lastValidChar;
}
}
lastValidChar = prevChar;
}
}
return lastChar;

View File

@ -106,16 +106,6 @@ String::iterator Text3D::computeLastCharacterOnLine(osg::Vec2& cursor, String::i
String::iterator lastChar = first;
std::set<unsigned int> deliminatorSet;
deliminatorSet.insert(' ');
deliminatorSet.insert('\n');
deliminatorSet.insert(':');
deliminatorSet.insert('/');
deliminatorSet.insert(',');
deliminatorSet.insert(';');
deliminatorSet.insert(':');
deliminatorSet.insert('.');
float maximumHeight = _maximumHeight / _font->getScale();
float maximumWidth = _maximumWidth / _font->getScale();
@ -222,15 +212,23 @@ String::iterator Text3D::computeLastCharacterOnLine(osg::Vec2& cursor, String::i
// word boundary detection & wrapping
if (lastChar!=last)
{
if (deliminatorSet.count(*lastChar)==0)
{
String::iterator lastValidChar = lastChar;
while (lastValidChar!=first && deliminatorSet.count(*lastValidChar)==0)
{
--lastValidChar;
String::iterator lastValidChar = lastChar;
String::iterator prevChar;
while (lastValidChar != first){
prevChar = lastValidChar - 1;
//Substract off glyphs from the cursor position (to correctly center text)
Font3D::Glyph3D* glyph = _font->getGlyph(*lastValidChar);
// last char is after a hyphen
if(*lastValidChar == '-')
return lastValidChar + 1;
// last char is start of whitespace
if((*lastValidChar == ' ' || *lastValidChar == '\n') && (*prevChar != ' ' && *prevChar != '\n'))
return lastValidChar;
// Subtract off glyphs from the cursor position (to correctly center text)
if(*prevChar != '-')
{
Font3D::Glyph3D* glyph = _font->getGlyph(*prevChar);
if (glyph)
{
switch(_layout)
@ -242,12 +240,8 @@ String::iterator Text3D::computeLastCharacterOnLine(osg::Vec2& cursor, String::i
}
}
if (first!=lastValidChar)
{
++lastValidChar;
lastChar = lastValidChar;
}
}
lastValidChar = prevChar;
}
}
return lastChar;
@ -406,7 +400,8 @@ void Text3D::computeGlyphRepresentation()
if (itr!=_text.end())
{
// skip over return.
// skip over spaces and return.
while (*itr==' ') ++itr;
if (*itr=='\n') ++itr;
}