Changeset 11119

Show
Ignore:
Timestamp:
02/25/10 19:17:20 (5 years ago)
Author:
robert
Message:

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

Location:
OpenSceneGraph/trunk/src/osgText
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • OpenSceneGraph/trunk/src/osgText/Text.cpp

    r10822 r11119  
    119119 
    120120    String::iterator lastChar = first; 
    121  
    122     std::set<unsigned int> deliminatorSet; 
    123     deliminatorSet.insert(' '); 
    124     deliminatorSet.insert('\n'); 
    125     deliminatorSet.insert(':'); 
    126     deliminatorSet.insert('/'); 
    127     deliminatorSet.insert(','); 
    128     deliminatorSet.insert(';'); 
    129     deliminatorSet.insert(':'); 
    130     deliminatorSet.insert('.'); 
    131121 
    132122    for(bool outOfSpace=false;lastChar!=last;++lastChar) 
     
    214204 
    215205    } 
    216      
     206 
    217207    // word boundary detection & wrapping 
    218208    if (lastChar!=last) 
    219209    { 
    220         if (deliminatorSet.count(*lastChar)==0)  
    221         { 
    222             String::iterator lastValidChar = lastChar; 
    223             while (lastValidChar!=first && deliminatorSet.count(*lastValidChar)==0) 
    224             { 
    225                 --lastValidChar; 
    226                  
    227                 // Subtract off glyphs from the cursor position (to correctly center text) 
    228                 Font::Glyph* glyph = activefont->getGlyph(_fontSize, *lastValidChar); 
     210        String::iterator lastValidChar = lastChar; 
     211          String::iterator prevChar; 
     212        while (lastValidChar != first){ 
     213            prevChar = lastValidChar - 1; 
     214 
     215            // last char is after a hyphen 
     216                if(*lastValidChar == '-') 
     217                return lastValidChar + 1; 
     218 
     219            // last char is start of whitespace 
     220            if((*lastValidChar == ' ' || *lastValidChar == '\n') && (*prevChar != ' ' && *prevChar != '\n')) 
     221                return lastValidChar; 
     222 
     223            // Subtract off glyphs from the cursor position (to correctly center text) 
     224                if(*prevChar != '-') 
     225            { 
     226                Font::Glyph* glyph = activefont->getGlyph(_fontSize, *prevChar); 
    229227                if (glyph) 
    230228                { 
     
    237235                } 
    238236            } 
    239             if (first!=lastValidChar) 
    240             { 
    241                 ++lastValidChar; 
    242                 lastChar = lastValidChar; 
    243             } 
    244         } 
     237 
     238            lastValidChar = prevChar; 
     239          } 
    245240    } 
    246241 
  • OpenSceneGraph/trunk/src/osgText/Text3D.cpp

    r10879 r11119  
    106106 
    107107    String::iterator lastChar = first; 
    108  
    109     std::set<unsigned int> deliminatorSet; 
    110     deliminatorSet.insert(' '); 
    111     deliminatorSet.insert('\n'); 
    112     deliminatorSet.insert(':'); 
    113     deliminatorSet.insert('/'); 
    114     deliminatorSet.insert(','); 
    115     deliminatorSet.insert(';'); 
    116     deliminatorSet.insert(':'); 
    117     deliminatorSet.insert('.'); 
    118108 
    119109    float maximumHeight = _maximumHeight / _font->getScale(); 
     
    223213    if (lastChar!=last) 
    224214    { 
    225         if (deliminatorSet.count(*lastChar)==0) 
    226         { 
    227             String::iterator lastValidChar = lastChar; 
    228             while (lastValidChar!=first && deliminatorSet.count(*lastValidChar)==0) 
    229             { 
    230                 --lastValidChar; 
    231  
    232                 //Substract off glyphs from the cursor position (to correctly center text) 
    233                 Font3D::Glyph3D* glyph = _font->getGlyph(*lastValidChar); 
     215        String::iterator lastValidChar = lastChar; 
     216          String::iterator prevChar; 
     217        while (lastValidChar != first){ 
     218            prevChar = lastValidChar - 1; 
     219 
     220            // last char is after a hyphen 
     221                if(*lastValidChar == '-') 
     222                return lastValidChar + 1; 
     223 
     224            // last char is start of whitespace 
     225            if((*lastValidChar == ' ' || *lastValidChar == '\n') && (*prevChar != ' ' && *prevChar != '\n')) 
     226                return lastValidChar; 
     227 
     228            // Subtract off glyphs from the cursor position (to correctly center text) 
     229                if(*prevChar != '-') 
     230            { 
     231                Font3D::Glyph3D* glyph = _font->getGlyph(*prevChar); 
    234232                if (glyph) 
    235233                { 
     
    243241            } 
    244242 
    245             if (first!=lastValidChar) 
    246             { 
    247                 ++lastValidChar; 
    248                 lastChar = lastValidChar; 
    249             } 
    250         } 
     243            lastValidChar = prevChar; 
     244          } 
    251245    } 
    252246 
     
    407401        if (itr!=_text.end()) 
    408402        { 
    409             // skip over return. 
     403            // skip over spaces and return. 
     404            while (*itr==' ') ++itr; 
    410405            if (*itr=='\n') ++itr; 
    411406        }