public static void WordWrap(this StringBuilder stringBuilder, ref StringBuilder target, SpriteFont font, Vector2 bounds, Vector2 scale)
 {
     int lastWhiteSpaceIndex = 0;
     float currentLineWidth = 0;
     float lengthSinceLastWhiteSpace = 0;
     Vector2 characterSize = Vector2.Zero;
     int lines = 0;
     for (int i = 0; i < stringBuilder.Length; i++)
     {
         characterSize = font.MeasureCharacter(stringBuilder[i]) * scale;
         currentLineWidth += characterSize.X;
         lengthSinceLastWhiteSpace += characterSize.X;
         if ((stringBuilder[i] != '\r') && (stringBuilder[i] != '\n'))
         {
             if (currentLineWidth > bounds.X)
             {
                 if ((lines + 1) * font.LineSpacing > bounds.Y)
                     return;
                 lines++;
                 if (char.IsWhiteSpace(stringBuilder[i]))
                 {
                     target.Insert(i, _newLineChars);
                     currentLineWidth = 0;
                     lengthSinceLastWhiteSpace = 0;
                     continue;
                 }
                 else
                 {
                     target.Insert(lastWhiteSpaceIndex, _newLineChars);
                     target.Remove(lastWhiteSpaceIndex + _newLineChars.Length, 1);
                     currentLineWidth = lengthSinceLastWhiteSpace;
                     lengthSinceLastWhiteSpace = 0;
                 }
             }
             else
             {
                 if (char.IsWhiteSpace(stringBuilder[i]))
                 {
                     lastWhiteSpaceIndex = target.Length;
                     lengthSinceLastWhiteSpace = 0;
                 }
             }
         }
         else
         {
             lengthSinceLastWhiteSpace = 0;
             currentLineWidth = 0;
         }
         target.Append(stringBuilder[i]);
     }
 }
Ejemplo n.º 2
0
 public static void WrapWord(StringBuilder original, StringBuilder target, SpriteFont font, Rectangle bounds, float scale)
 {
     int lastWhiteSpace = 0;
     float currentLength = 0;
     float lengthSinceLastWhiteSpace = 0;
     float characterWidth = 0;
     for (int i = 0; i < original.Length; i++)
     {
         //get the character
         char character = original[i];
         //measure the length of the current line
         characterWidth = font.MeasureCharacter(character).X * scale;
         currentLength += characterWidth;
         //find the length since last white space
         lengthSinceLastWhiteSpace += characterWidth;
         //are we at a new line?
         if ((character != '\r') && (character != '\n'))
         {
             //time for a new line?
             if (currentLength > bounds.Width)
             {
                 //if so are we at white space?
                 if (char.IsWhiteSpace(character))
                 {
                     //if so insert newline here
                     target.Insert(i, NewLine);
                     //reset lengths
                     currentLength = 0;
                     lengthSinceLastWhiteSpace = 0;
                     // return to the top of the loop as to not append white space
                     continue;
                 }
                 else
                 {
                     //not at white space so we insert a new line at the previous recorded white space
                     target.Insert(lastWhiteSpace, NewLine);
                     //remove the white space
                     target.Remove(lastWhiteSpace + NewLine.Length, 1);
                     //make sure the the characters at the line break are accounted for
                     currentLength = lengthSinceLastWhiteSpace;
                     lengthSinceLastWhiteSpace = 0;
                 }
             }
             else
             {
                 //not time for a line break? are we at white space?
                 if (char.IsWhiteSpace(character))
                 {
                     //record it's location
                     lastWhiteSpace = target.Length;
                     lengthSinceLastWhiteSpace = 0;
                 }
             }
         }
         else
         {
             lengthSinceLastWhiteSpace = 0;
             currentLength = 0;
         }
         //always append
         target.Append(character);
     }
 }