Fixed infinite loop in WrapText if the line length is smaller than an individual character, text scale is taken into account in GUITextBlock wrapping

This commit is contained in:
Regalis
2017-03-27 21:08:14 +03:00
parent 9ef069bf61
commit 0e5de469d3
2 changed files with 25 additions and 16 deletions
+5 -5
View File
@@ -187,21 +187,21 @@ namespace Barotrauma
public void SetTextPos() public void SetTextPos()
{ {
if (text==null) return; if (text == null) return;
wrappedText = text; wrappedText = text;
Vector2 size = MeasureText(text); Vector2 size = MeasureText(text);
if (Wrap && rect.Width>0) if (Wrap && rect.Width > 0)
{ {
wrappedText = ToolBox.WrapText(text, rect.Width - padding.X - padding.Z, Font); wrappedText = ToolBox.WrapText(text, rect.Width - padding.X - padding.Z, Font, textScale);
Vector2 newSize = MeasureText(wrappedText); Vector2 newSize = MeasureText(wrappedText);
size = newSize; size = newSize;
} }
if (LimitText && text.Length>1 && size.Y > rect.Height) if (LimitText && text.Length>1 && size.Y > rect.Height)
{ {
string[] lines = text.Split('\n'); string[] lines = text.Split('\n');
+20 -11
View File
@@ -465,7 +465,7 @@ namespace Barotrauma
return d[n, m]; return d[n, m];
} }
public static string WrapText(string text, float lineLength, ScalableFont font) //TODO: could integrate this into the ScalableFont class directly public static string WrapText(string text, float lineLength, ScalableFont font, float textScale = 1.0f) //TODO: could integrate this into the ScalableFont class directly
{ {
if (font.MeasureString(text).X < lineLength) return text; if (font.MeasureString(text).X < lineLength) return text;
@@ -475,26 +475,35 @@ namespace Barotrauma
StringBuilder wrappedText = new StringBuilder(); StringBuilder wrappedText = new StringBuilder();
float linePos = 0f; float linePos = 0f;
float spaceWidth = font.MeasureString(" ").X; float spaceWidth = font.MeasureString(" ").X * textScale;
for (int i = 0; i < words.Length; ++i) for (int i = 0; i < words.Length; ++i)
{ {
if (string.IsNullOrWhiteSpace(words[i]) && words[i] != "\n") continue; if (string.IsNullOrWhiteSpace(words[i]) && words[i] != "\n") continue;
Vector2 size = font.MeasureString(words[i]); Vector2 size = font.MeasureString(words[i]) * textScale;
if (size.X > lineLength) if (size.X > lineLength)
{ {
while (words[i].Length > 0 && if (linePos == 0.0f)
(size = font.MeasureString((words[i][0]).ToString())).X + linePos < lineLength)
{ {
wrappedText.Append(words[i][0]); wrappedText.AppendLine(words[i]);
words[i] = words[i].Remove(0, 1); }
else
{
do
{
if (words[i].Length == 0) break;
linePos += size.X; wrappedText.Append(words[i][0]);
words[i] = words[i].Remove(0, 1);
linePos += size.X;
} while (words[i].Length > 0 && (size = font.MeasureString((words[i][0]).ToString()) * textScale).X + linePos < lineLength);
wrappedText.Append("\n");
linePos = 0.0f;
i--;
} }
wrappedText.Append("\n");
linePos = 0.0f;
i--;
continue; continue;
} }