From 4bdaef7ad6831bf6c7e480d639d6137ca05a0c28 Mon Sep 17 00:00:00 2001 From: Tig Date: Sat, 20 Jul 2024 11:45:55 -0600 Subject: [PATCH] Nuked TextFormatter.CalcRect and unit tests --- Terminal.Gui/Text/TextFormatter.cs | 142 --------------------------- UnitTests/Text/TextFormatterTests.cs | 95 ------------------ 2 files changed, 237 deletions(-) diff --git a/Terminal.Gui/Text/TextFormatter.cs b/Terminal.Gui/Text/TextFormatter.cs index 7436823ff..a46b73df7 100644 --- a/Terminal.Gui/Text/TextFormatter.cs +++ b/Terminal.Gui/Text/TextFormatter.cs @@ -1981,148 +1981,6 @@ public class TextFormatter return lineIdx; } - /// Calculates the rectangle required to hold text, assuming no word wrapping or alignment. - /// - /// This API will return incorrect results if the text includes glyphs whose width is dependent on surrounding - /// glyphs (e.g. Arabic). - /// - /// The x location of the rectangle - /// The y location of the rectangle - /// The text to measure - /// The text direction. - /// The number of columns used for a tab. - /// - [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); - } - /// Finds the HotKey and its location in text. /// The text to look in. /// The HotKey specifier (e.g. '_') to look for. diff --git a/UnitTests/Text/TextFormatterTests.cs b/UnitTests/Text/TextFormatterTests.cs index a177b9dae..a83b7205c 100644 --- a/UnitTests/Text/TextFormatterTests.cs +++ b/UnitTests/Text/TextFormatterTests.cs @@ -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)]