Improve WidgetUtils.WrapText performance on long lines.
This commit is contained in:
@@ -203,29 +203,29 @@ namespace OpenRA.Widgets
|
|||||||
for (var i = 0; i < lines.Count; i++)
|
for (var i = 0; i < lines.Count; i++)
|
||||||
{
|
{
|
||||||
var line = lines[i];
|
var line = lines[i];
|
||||||
var m = font.Measure(line);
|
if (font.Measure(line).X <= width)
|
||||||
|
|
||||||
if (m.X <= width)
|
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
var bestSpaceIndex = -1;
|
// Scan forwards until we find the last word that fits
|
||||||
var start = line.Length - 1;
|
// This guarantees a small bound on the amount of string we need to search before a linebreak
|
||||||
|
var start = 0;
|
||||||
while (m.X > width)
|
while (true)
|
||||||
{
|
{
|
||||||
var spaceIndex = line.LastIndexOf(' ', start);
|
var spaceIndex = line.IndexOf(' ', start);
|
||||||
if (spaceIndex == -1)
|
if (spaceIndex == -1)
|
||||||
break;
|
break;
|
||||||
bestSpaceIndex = spaceIndex;
|
|
||||||
|
|
||||||
start = spaceIndex - 1;
|
var fragmentWidth = font.Measure(line.Substring(0, spaceIndex)).X;
|
||||||
m = font.Measure(line.Substring(0, spaceIndex));
|
if (fragmentWidth > width)
|
||||||
|
break;
|
||||||
|
|
||||||
|
start = spaceIndex + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (bestSpaceIndex != -1)
|
if (start > 0)
|
||||||
{
|
{
|
||||||
lines[i] = line.Substring(0, bestSpaceIndex);
|
lines[i] = line.Substring(0, start - 1);
|
||||||
lines.Insert(i + 1, line.Substring(bestSpaceIndex + 1));
|
lines.Insert(i + 1, line.Substring(start));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user