Improve remove format tags for ASS

This commit is contained in:
Nikolaj Olsson 2020-04-04 20:12:04 +02:00
parent e63720ab0d
commit 9e513e4a79
2 changed files with 101 additions and 3 deletions

View File

@ -1498,11 +1498,30 @@ Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour,
{
if (newFormat != null && newFormat.Name == SubStationAlpha.NameOfFormat)
{
foreach (Paragraph p in subtitle.Paragraphs)
foreach (var p in subtitle.Paragraphs)
{
string s = p.Text;
if (s.Contains('{') && s.Contains('}'))
{
var p1Index = s.IndexOf("\\p1", StringComparison.Ordinal);
var p0Index = s.IndexOf("{\\p0}", StringComparison.Ordinal);
if (p1Index > 0 && (p0Index > p1Index || p0Index == -1))
{
var startTagIndex = s.Substring(0, p1Index).LastIndexOf('{');
if (startTagIndex >= 0)
{
if (p0Index > p1Index)
{
s = s.Remove(startTagIndex, p0Index - startTagIndex + "{\\p0}".Length);
}
else
{
s = s.Remove(startTagIndex);
}
}
}
s = s.Replace(@"\u0", string.Empty);
s = s.Replace(@"\u1", string.Empty);
s = s.Replace(@"\s0", string.Empty);
@ -1537,8 +1556,40 @@ Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour,
}
else
{
foreach (Paragraph p in subtitle.Paragraphs)
foreach (var p in subtitle.Paragraphs)
{
var noTags = Utilities.RemoveSsaTags(p.Text).Trim();
if (noTags.Length == 0)
{
p.Text = string.Empty;
continue;
}
if (noTags.StartsWith("m ", StringComparison.Ordinal))
{
var test = noTags.Remove(0, 2)
.RemoveChar('0')
.RemoveChar('1')
.RemoveChar('2')
.RemoveChar('3')
.RemoveChar('4')
.RemoveChar('5')
.RemoveChar('6')
.RemoveChar('7')
.RemoveChar('8')
.RemoveChar('9')
.RemoveChar('-')
.RemoveChar('l')
.RemoveChar('m')
.RemoveChar(' ')
.RemoveChar('.');
if (test.Length == 0)
{
p.Text = string.Empty;
continue;
}
}
int indexOfBegin = p.Text.IndexOf('{');
string pre = string.Empty;
while (indexOfBegin >= 0 && p.Text.IndexOf('}') > indexOfBegin)
@ -1569,7 +1620,7 @@ Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour,
pre = s.Substring(0, 5) + "}";
}
int indexOfEnd = p.Text.IndexOf('}');
p.Text = p.Text.Remove(indexOfBegin, (indexOfEnd - indexOfBegin) + 1);
p.Text = p.Text.Remove(indexOfBegin, indexOfEnd - indexOfBegin + 1);
indexOfBegin = p.Text.IndexOf('{');
}

View File

@ -721,6 +721,28 @@ namespace Nikse.SubtitleEdit.Core
public static string RemoveSsaTags(string input)
{
var s = input;
if (s.Contains('{') && s.Contains('}'))
{
var p1Index = s.IndexOf("\\p1", StringComparison.Ordinal);
var p0Index = s.IndexOf("{\\p0}", StringComparison.Ordinal);
if (p1Index > 0 && (p0Index > p1Index || p0Index == -1))
{
var startTagIndex = s.Substring(0, p1Index).LastIndexOf('{');
if (startTagIndex >= 0)
{
if (p0Index > p1Index)
{
s = s.Remove(startTagIndex, p0Index - startTagIndex + "{\\p0}".Length);
}
else
{
s = s.Remove(startTagIndex);
}
}
}
}
int k = s.IndexOf("{\\", StringComparison.Ordinal);
while (k >= 0)
{
@ -733,6 +755,31 @@ namespace Nikse.SubtitleEdit.Core
s = s.Remove(k, l - k + 1);
k = s.IndexOf('{', k);
}
if (s.StartsWith("m ", StringComparison.Ordinal))
{
var test = s.Remove(0, 2)
.RemoveChar('0')
.RemoveChar('1')
.RemoveChar('2')
.RemoveChar('3')
.RemoveChar('4')
.RemoveChar('5')
.RemoveChar('6')
.RemoveChar('7')
.RemoveChar('8')
.RemoveChar('9')
.RemoveChar('-')
.RemoveChar('l')
.RemoveChar('m')
.RemoveChar(' ')
.RemoveChar('.');
if (test.Length == 0)
{
return string.Empty;
}
}
return s;
}