Skip to content

Commit

Permalink
Improve WidgetUtils.WrapText performance on long lines.
Browse files Browse the repository at this point in the history
  • Loading branch information
pchote authored and reaperrr committed Oct 25, 2017
1 parent 88c94ee commit 77a7453
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions OpenRA.Game/Widgets/WidgetUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,29 +203,29 @@ public static string WrapText(string text, int width, SpriteFont font)
for (var i = 0; i < lines.Count; i++)
{
var line = lines[i];
var m = font.Measure(line);

if (m.X <= width)
if (font.Measure(line).X <= width)
continue;

var bestSpaceIndex = -1;
var start = line.Length - 1;

while (m.X > width)
// Scan forwards until we find the last word that fits
// This guarantees a small bound on the amount of string we need to search before a linebreak
var start = 0;
while (true)
{
var spaceIndex = line.LastIndexOf(' ', start);
var spaceIndex = line.IndexOf(' ', start);
if (spaceIndex == -1)
break;
bestSpaceIndex = spaceIndex;

start = spaceIndex - 1;
m = font.Measure(line.Substring(0, spaceIndex));
var fragmentWidth = font.Measure(line.Substring(0, spaceIndex)).X;
if (fragmentWidth > width)
break;

start = spaceIndex + 1;
}

if (bestSpaceIndex != -1)
if (start > 0)
{
lines[i] = line.Substring(0, bestSpaceIndex);
lines.Insert(i + 1, line.Substring(bestSpaceIndex + 1));
lines[i] = line.Substring(0, start - 1);
lines.Insert(i + 1, line.Substring(start));
}
}

Expand Down

0 comments on commit 77a7453

Please sign in to comment.