From bc202cd4bbcfffabf4feca68274ba1fa6bdd136b Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Thu, 25 Feb 2010 18:17:20 +0000 Subject: [PATCH] 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." --- src/osgText/Text.cpp | 45 +++++++++++++++++++----------------------- src/osgText/Text3D.cpp | 45 +++++++++++++++++++----------------------- 2 files changed, 40 insertions(+), 50 deletions(-) diff --git a/src/osgText/Text.cpp b/src/osgText/Text.cpp index 3bdd4e9f2..b184e6963 100644 --- a/src/osgText/Text.cpp +++ b/src/osgText/Text.cpp @@ -119,16 +119,6 @@ String::iterator Text::computeLastCharacterOnLine(osg::Vec2& cursor, String::ite String::iterator lastChar = first; - std::set 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; diff --git a/src/osgText/Text3D.cpp b/src/osgText/Text3D.cpp index 5fe9da253..000429edb 100644 --- a/src/osgText/Text3D.cpp +++ b/src/osgText/Text3D.cpp @@ -106,16 +106,6 @@ String::iterator Text3D::computeLastCharacterOnLine(osg::Vec2& cursor, String::i String::iterator lastChar = first; - std::set 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; }