Nuked TextFormatter.CalcRect and unit tests

This commit is contained in:
Tig
2024-07-20 11:45:55 -06:00
parent 71e14c8ad1
commit 4bdaef7ad6
2 changed files with 0 additions and 237 deletions

View File

@@ -1981,148 +1981,6 @@ public class TextFormatter
return lineIdx;
}
/// <summary>Calculates the rectangle required to hold text, assuming no word wrapping or alignment.</summary>
/// <remarks>
/// This API will return incorrect results if the text includes glyphs whose width is dependent on surrounding
/// glyphs (e.g. Arabic).
/// </remarks>
/// <param name="x">The x location of the rectangle</param>
/// <param name="y">The y location of the rectangle</param>
/// <param name="text">The text to measure</param>
/// <param name="direction">The text direction.</param>
/// <param name="tabWidth">The number of columns used for a tab.</param>
/// <returns></returns>
[Obsolete ("CalcRect is deprecated, FormatAndGetSize instead.")]
internal static Rectangle CalcRect (
int x,
int y,
string text,
TextDirection direction = TextDirection.LeftRight_TopBottom,
int tabWidth = 0
)
{
if (string.IsNullOrEmpty (text))
{
return new (new (x, y), System.Drawing.Size.Empty);
}
int w, h;
if (IsHorizontalDirection (direction))
{
var mw = 0;
var ml = 1;
var cols = 0;
foreach (Rune rune in text.EnumerateRunes ())
{
if (rune.Value == '\n')
{
ml++;
if (cols > mw)
{
mw = cols;
}
cols = 0;
}
else if (rune.Value != '\r')
{
cols++;
var rw = 0;
if (rune.Value == '\t')
{
rw += tabWidth - 1;
}
else
{
rw = rune.GetColumns ();
if (rw > 0)
{
rw--;
}
else if (rw == 0)
{
cols--;
}
}
cols += rw;
}
}
if (cols > mw)
{
mw = cols;
}
w = mw;
h = ml;
}
else
{
int vw = 1, cw = 1;
var vh = 0;
var rows = 0;
foreach (Rune rune in text.EnumerateRunes ())
{
if (rune.Value == '\n')
{
vw++;
if (rows > vh)
{
vh = rows;
}
rows = 0;
cw = 1;
}
else if (rune.Value != '\r')
{
rows++;
var rw = 0;
if (rune.Value == '\t')
{
rw += tabWidth - 1;
rows += rw;
}
else
{
rw = rune.GetColumns ();
if (rw == 0)
{
rows--;
}
else if (cw < rw)
{
cw = rw;
vw++;
}
}
}
}
if (rows > vh)
{
vh = rows;
}
w = vw;
h = vh;
}
return new (x, y, w, h);
}
/// <summary>Finds the HotKey and its location in text.</summary>
/// <param name="text">The text to look in.</param>
/// <param name="hotKeySpecifier">The HotKey specifier (e.g. '_') to look for.</param>

View File

@@ -39,101 +39,6 @@ public class TextFormatterTests
}
};
[Theory]
[InlineData (null)]
[InlineData ("")]
public void CalcRect_Invalid_Returns_Empty (string text)
{
Assert.Equal (Rectangle.Empty, TextFormatter.CalcRect (0, 0, text));
Assert.Equal (new (new (1, 2), Size.Empty), TextFormatter.CalcRect (1, 2, text));
Assert.Equal (new (new (-1, -2), Size.Empty), TextFormatter.CalcRect (-1, -2, text));
}
[Theory]
[InlineData ("line1\nline2", 5, 2)]
[InlineData ("\nline2", 5, 2)]
[InlineData ("\n\n", 0, 3)]
[InlineData ("\n\n\n", 0, 4)]
[InlineData ("line1\nline2\nline3long!", 10, 3)]
[InlineData ("line1\nline2\n\n", 5, 4)]
[InlineData ("line1\r\nline2", 5, 2)]
[InlineData (" ~  s  gui.cs   master ↑10\n", 31, 2)]
[InlineData ("\n ~  s  gui.cs   master ↑10", 31, 2)]
[InlineData (" ~  s  gui.cs   master\n↑10", 27, 2)]
public void CalcRect_MultiLine_Returns_nHigh (string text, int expectedWidth, int expectedLines)
{
Assert.Equal (new (0, 0, expectedWidth, expectedLines), TextFormatter.CalcRect (0, 0, text));
string [] lines = text.Split (text.Contains (Environment.NewLine) ? Environment.NewLine : "\n");
int maxWidth = lines.Max (s => s.GetColumns ());
var lineWider = 0;
for (var i = 0; i < lines.Length; i++)
{
int w = lines [i].GetColumns ();
if (w == maxWidth)
{
lineWider = i;
}
}
Assert.Equal (new (0, 0, maxWidth, expectedLines), TextFormatter.CalcRect (0, 0, text));
Assert.Equal (
new (
0,
0,
lines [lineWider].ToRuneList ().Sum (r => Math.Max (r.GetColumns (), 0)),
expectedLines
),
TextFormatter.CalcRect (0, 0, text)
);
}
[Theory]
[InlineData ("test")]
[InlineData (" ~  s  gui.cs   master ↑10")]
public void CalcRect_SingleLine_Returns_1High (string text)
{
Assert.Equal (new (0, 0, text.GetRuneCount (), 1), TextFormatter.CalcRect (0, 0, text));
Assert.Equal (new (0, 0, text.GetColumns (), 1), TextFormatter.CalcRect (0, 0, text));
}
[Theory]
[InlineData (14, 1, TextDirection.LeftRight_TopBottom)]
[InlineData (1, 14, TextDirection.TopBottom_LeftRight)]
public void CalcRect_With_Combining_Runes (int width, int height, TextDirection textDirection)
{
var text = "Les Mise\u0328\u0301rables";
Assert.Equal (new (0, 0, width, height), TextFormatter.CalcRect (0, 0, text, textDirection));
}
[Theory]
[InlineData ("test", TextDirection.LeftRight_TopBottom)]
[InlineData (" ~  s  gui.cs   master ↑10", TextDirection.LeftRight_TopBottom)]
[InlineData ("Say Hello view4 你", TextDirection.LeftRight_TopBottom)]
[InlineData ("Say Hello view4 你", TextDirection.RightLeft_TopBottom)]
[InlineData ("Say Hello view4 你", TextDirection.LeftRight_BottomTop)]
[InlineData ("Say Hello view4 你", TextDirection.RightLeft_BottomTop)]
public void CalcRect_Horizontal_Width_Correct (string text, TextDirection textDirection)
{
// The width is the number of columns in the text
Assert.Equal (new (text.GetColumns (), 1), TextFormatter.CalcRect (0, 0, text, textDirection).Size);
}
[Theory]
[InlineData ("test", TextDirection.TopBottom_LeftRight)]
[InlineData (" ~  s  gui.cs   master ↑10", TextDirection.TopBottom_LeftRight)]
[InlineData ("Say Hello view4 你", TextDirection.TopBottom_LeftRight)]
[InlineData ("Say Hello view4 你", TextDirection.TopBottom_RightLeft)]
[InlineData ("Say Hello view4 你", TextDirection.BottomTop_LeftRight)]
[InlineData ("Say Hello view4 你", TextDirection.BottomTop_RightLeft)]
public void CalcRect_Vertical_Height_Correct (string text, TextDirection textDirection)
{
// The height is based both the number of lines and the number of wide chars
Assert.Equal (new (1 + text.GetColumns () - text.Length, text.Length), TextFormatter.CalcRect (0, 0, text, textDirection).Size);
}
[Theory]
[InlineData ("")]
[InlineData (null)]