Skip to content

Commit 313adf7

Browse files
committed
Merge branch 'v2_3873_textformatter-combining-marks-fix' into v2_tabview-left-right-feature
2 parents 97c2d52 + 1c77b07 commit 313adf7

File tree

2 files changed

+90
-6
lines changed

2 files changed

+90
-6
lines changed

Terminal.Gui/Text/TextFormatter.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1411,7 +1411,7 @@ public static string ClipAndJustify (
14111411

14121412
if (textFormatter is { Alignment: Alignment.Center })
14131413
{
1414-
return GetRangeThatFits (runes, Math.Max ((runes.Count - width) / 2, 0), text, width, tabWidth, textDirection);
1414+
return GetRangeThatFits (runes, Math.Max ((runes.Count - width - zeroLength) / 2, 0), text, width, tabWidth, textDirection);
14151415
}
14161416

14171417
return GetRangeThatFits (runes, 0, text, width, tabWidth, textDirection);
@@ -1426,7 +1426,7 @@ public static string ClipAndJustify (
14261426

14271427
if (textFormatter is { VerticalAlignment: Alignment.Center })
14281428
{
1429-
return GetRangeThatFits (runes, Math.Max ((runes.Count - width) / 2, 0), text, width, tabWidth, textDirection);
1429+
return GetRangeThatFits (runes, Math.Max ((runes.Count - width - zeroLength) / 2, 0), text, width, tabWidth, textDirection);
14301430
}
14311431

14321432
return GetRangeThatFits (runes, 0, text, width, tabWidth, textDirection);
@@ -1451,7 +1451,7 @@ public static string ClipAndJustify (
14511451
}
14521452
else if (textFormatter is { Alignment: Alignment.Center })
14531453
{
1454-
return GetRangeThatFits (runes, Math.Max ((runes.Count - width) / 2, 0), text, width, tabWidth, textDirection);
1454+
return GetRangeThatFits (runes, Math.Max ((runes.Count - width - zeroLength) / 2, 0), text, width, tabWidth, textDirection);
14551455
}
14561456
else if (GetRuneWidth (text, tabWidth, textDirection) > width)
14571457
{
@@ -1470,7 +1470,7 @@ public static string ClipAndJustify (
14701470
}
14711471
else if (textFormatter is { VerticalAlignment: Alignment.Center })
14721472
{
1473-
return GetRangeThatFits (runes, Math.Max ((runes.Count - width) / 2, 0), text, width, tabWidth, textDirection);
1473+
return GetRangeThatFits (runes, Math.Max ((runes.Count - width - zeroLength) / 2, 0), text, width, tabWidth, textDirection);
14741474
}
14751475
else if (runes.Count - zeroLength > width)
14761476
{
@@ -1526,7 +1526,7 @@ public static string Justify (
15261526
}
15271527
else
15281528
{
1529-
textCount = words.Sum (arg => arg.GetRuneCount ());
1529+
textCount = words.Sum (arg => arg.GetRuneCount ()) - text.EnumerateRunes ().Sum (r => r.GetColumns () == 0 ? 1 : 0);
15301530
}
15311531

15321532
int spaces = words.Length > 1 ? (width - textCount) / (words.Length - 1) : 0;
@@ -1936,7 +1936,7 @@ private static int GetRuneWidth (List<Rune> runes, int tabWidth, TextDirection t
19361936

19371937
private static int GetRuneWidth (Rune rune, int tabWidth, TextDirection textDirection = TextDirection.LeftRight_TopBottom)
19381938
{
1939-
int runeWidth = IsHorizontalDirection (textDirection) ? rune.GetColumns () : 1;
1939+
int runeWidth = IsHorizontalDirection (textDirection) ? rune.GetColumns () : rune.GetColumns () == 0 ? 0 : 1;
19401940

19411941
if (rune.Value == '\t')
19421942
{

UnitTests/Text/TextFormatterTests.cs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4629,6 +4629,90 @@ string expectedWrappedText
46294629
Assert.Equal (expectedWrappedText, wrappedText);
46304630
}
46314631

4632+
[Theory]
4633+
[InlineData (
4634+
"Les Mise\u0301rables",
4635+
14,
4636+
-1,
4637+
false,
4638+
new [] { "Les Misérables" },
4639+
"Les Misérables"
4640+
)]
4641+
[InlineData (
4642+
"Les Mise\u0328\u0301rables",
4643+
14,
4644+
-2,
4645+
false,
4646+
new [] { "Les Misę́rables" },
4647+
"Les Misę́rables"
4648+
)]
4649+
public void Format_Combining_Marks_Alignments (
4650+
string text,
4651+
int maxWidth,
4652+
int widthOffset,
4653+
bool wrap,
4654+
IEnumerable<string> resultLines,
4655+
string expectedText
4656+
)
4657+
{
4658+
Assert.Equal (maxWidth, text.GetRuneCount () + widthOffset);
4659+
4660+
// Horizontal text direction
4661+
foreach (Alignment alignment in Enum.GetValues (typeof (Alignment)))
4662+
{
4663+
TextFormatter tf = new () { Text = text, ConstrainToSize = new (maxWidth, 1), WordWrap = wrap, Alignment = alignment };
4664+
4665+
List<string> list = TextFormatter.Format (
4666+
text,
4667+
maxWidth,
4668+
alignment,
4669+
wrap,
4670+
tf.PreserveTrailingSpaces,
4671+
tf.TabWidth,
4672+
tf.Direction,
4673+
tf.MultiLine,
4674+
tf);
4675+
Assert.Equal (list.Count, resultLines.Count ());
4676+
Assert.Equal (resultLines, list);
4677+
var formattedText = string.Empty;
4678+
4679+
foreach (string txt in list)
4680+
{
4681+
formattedText += txt;
4682+
}
4683+
4684+
Assert.Equal (expectedText, formattedText);
4685+
}
4686+
4687+
// Vertical text direction
4688+
foreach (Alignment alignment in Enum.GetValues (typeof (Alignment)))
4689+
{
4690+
TextFormatter tf = new ()
4691+
{ Text = text, ConstrainToSize = new (1, maxWidth), WordWrap = wrap, VerticalAlignment = alignment, Direction = TextDirection.TopBottom_LeftRight };
4692+
4693+
List<string> list = TextFormatter.Format (
4694+
text,
4695+
maxWidth,
4696+
alignment,
4697+
wrap,
4698+
tf.PreserveTrailingSpaces,
4699+
tf.TabWidth,
4700+
tf.Direction,
4701+
tf.MultiLine,
4702+
tf);
4703+
Assert.Equal (list.Count, resultLines.Count ());
4704+
Assert.Equal (resultLines, list);
4705+
var formattedText = string.Empty;
4706+
4707+
foreach (string txt in list)
4708+
{
4709+
formattedText += txt;
4710+
}
4711+
4712+
Assert.Equal (expectedText, formattedText);
4713+
}
4714+
}
4715+
46324716
public static IEnumerable<object []> FormatEnvironmentNewLine =>
46334717
new List<object []>
46344718
{

0 commit comments

Comments
 (0)