Trying to fix RTL regarding #2317 - thx darnn :)

This commit is contained in:
Nikolaj Olsson 2017-03-26 18:18:34 +02:00
parent 7f1daa0ed7
commit c937acec5d
3 changed files with 54 additions and 8 deletions

View File

@ -1142,16 +1142,23 @@ namespace Nikse.SubtitleEdit.Core
return Uri.UnescapeDataString(text);
}
private const string PrePostStringsToReverse = @"- !?.""،,():;[]";
private static readonly Regex TwoOrMoreDigitsNumber = new Regex(@"\d\d+", RegexOptions.Compiled);
public static string ReverseStartAndEndingForRightToLeft(string s)
{
var lines = s.SplitToLines();
var newLines = new StringBuilder();
var pre = new StringBuilder();
var post = new StringBuilder();
bool startsWithAssTag = false;
string assTag = string.Empty;
if (s.StartsWith("{\\", StringComparison.Ordinal) && s.IndexOf('}') < 22)
{
startsWithAssTag = true;
int end = s.IndexOf('}') + 1;
assTag = s.Substring(0, end);
s = s.Remove(0, end);
}
var lines = s.SplitToLines();
foreach (var line in lines)
{
string s2 = line;
@ -1172,17 +1179,19 @@ namespace Nikse.SubtitleEdit.Core
pre.Clear();
post.Clear();
int i = 0;
while (i < s2.Length && PrePostStringsToReverse.Contains(s2[i]))
while (i < s2.Length && !char.IsLetterOrDigit(s2[i]) && s2[i] != '{')
{
pre.Append(s2[i]);
i++;
}
int j = s2.Length - 1;
while (j > i && PrePostStringsToReverse.Contains(s2[j]))
while (j > i && !char.IsLetterOrDigit(s2[j]) && s2[j] != '}')
{
post.Append(s2[j]);
j--;
}
if (startsWithAssTag)
newLines.Append(assTag);
if (startsWithItalic)
newLines.Append("<i>");
newLines.Append(ReverseParenthesis(post.ToString()));

View File

@ -11122,7 +11122,7 @@ namespace Nikse.SubtitleEdit.Forms
}
else if (!toolStripMenuItemReverseRightToLeftStartEnd.Visible && _mainEditReverseStartAndEndingForRTL == e.KeyData && inListView)
{
ReverseStartAndEndingForRTL();
ReverseStartAndEndingForRtl();
e.SuppressKeyPress = true;
}
else if (toolStripMenuItemUndo.ShortcutKeys == e.KeyData) // undo
@ -12217,7 +12217,7 @@ namespace Nikse.SubtitleEdit.Forms
RefreshSelectedParagraph();
}
private void ReverseStartAndEndingForRTL()
private void ReverseStartAndEndingForRtl()
{
MakeHistoryForUndo(toolStripMenuItemReverseRightToLeftStartEnd.Text);
int selectedIndex = FirstSelectedIndex;
@ -19127,7 +19127,7 @@ namespace Nikse.SubtitleEdit.Forms
private void toolStripMenuItemReverseRightToLeftStartEnd_Click(object sender, EventArgs e)
{
ReverseStartAndEndingForRTL();
ReverseStartAndEndingForRtl();
}
private void toolStripMenuItemExportCapMakerPlus_Click(object sender, EventArgs e)

View File

@ -483,5 +483,42 @@ namespace Test.Logic
Assert.AreEqual(Utilities.ReverseNumbers("Hallo 009 001 Bye"), "Hallo 900 100 Bye");
}
[TestMethod]
public void ReverseStartAndEndingForRightToLeft1()
{
Assert.AreEqual(Utilities.ReverseStartAndEndingForRightToLeft("-I have a big head."), ".I have a big head-");
}
[TestMethod]
public void ReverseStartAndEndingForRightToLeft2()
{
Assert.AreEqual(Utilities.ReverseStartAndEndingForRightToLeft("~So do I?"), "?So do I~");
}
[TestMethod]
public void ReverseStartAndEndingForRightToLeft3()
{
Assert.AreEqual(Utilities.ReverseStartAndEndingForRightToLeft("+I do too!"), "!I do too+");
}
[TestMethod]
public void ReverseStartAndEndingForRightToLeft4()
{
Assert.AreEqual(Utilities.ReverseStartAndEndingForRightToLeft("(Mom)" + Environment.NewLine + "What are you doing here?"), "(Mom)" + Environment.NewLine + "?What are you doing here");
}
[TestMethod]
public void ReverseStartAndEndingForRightToLeft5()
{
Assert.AreEqual(Utilities.ReverseStartAndEndingForRightToLeft("{\\an8}+I do too!"), "{\\an8}!I do too+");
}
[TestMethod]
public void ReverseStartAndEndingForRightToLeft6()
{
Assert.AreEqual(Utilities.ReverseStartAndEndingForRightToLeft("-I have a big head." + Environment.NewLine + "~So do I?" + Environment.NewLine + "+I do too!"),
".I have a big head-" + Environment.NewLine + "?So do I~" + Environment.NewLine + "!I do too+");
}
}
}