Merge pull request #7094 from Flitskikker/feature/fix-convert-colors-to-dialog-positioning

Ignore ASS tags when converting colors to dialog + WebVTT colors support
This commit is contained in:
Nikolaj Olsson 2023-07-15 01:25:21 -04:00 committed by GitHub
commit d9f15571d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 138 additions and 6 deletions

View File

@ -318,5 +318,89 @@ namespace Test.Logic
Assert.AreEqual("- No, don't touch that-- - That was stupid." + Environment.NewLine + "- I know.", result);
}
[TestMethod]
public void TestPositioning()
{
Configuration.Settings.General.DialogStyle = Nikse.SubtitleEdit.Core.Enums.DialogType.DashBothLinesWithSpace;
var subtitle = new Subtitle(new List<Paragraph>() { new Paragraph("{\\an8}<font color=\"#ffff00\">That was really delicious.</font>" + Environment.NewLine +
"I know.", 0, 2000) });
ConvertColorsToDialogUtils.ConvertColorsToDialogInSubtitle(subtitle, true, false, false);
var result = subtitle.Paragraphs.First().Text;
Assert.AreEqual("{\\an8}- That was really delicious." + Environment.NewLine + "- I know.", result);
}
[TestMethod]
public void TestPositioning2()
{
Configuration.Settings.General.DialogStyle = Nikse.SubtitleEdit.Core.Enums.DialogType.DashBothLinesWithSpace;
var subtitle = new Subtitle(new List<Paragraph>() { new Paragraph("{\\an8}That was really delicious." + Environment.NewLine +
"<font color=\"#ffff00\">I know.</font>", 0, 2000) });
ConvertColorsToDialogUtils.ConvertColorsToDialogInSubtitle(subtitle, true, false, false);
var result = subtitle.Paragraphs.First().Text;
Assert.AreEqual("{\\an8}- That was really delicious." + Environment.NewLine + "- I know.", result);
}
[TestMethod]
public void TestPositioning3()
{
Configuration.Settings.General.DialogStyle = Nikse.SubtitleEdit.Core.Enums.DialogType.DashSecondLineWithoutSpace;
var subtitle = new Subtitle(new List<Paragraph>() { new Paragraph("{\\an8}<font color=\"#ffff00\">That was really delicious.</font>" + Environment.NewLine +
"I know.", 0, 2000) });
ConvertColorsToDialogUtils.ConvertColorsToDialogInSubtitle(subtitle, true, false, false);
var result = subtitle.Paragraphs.First().Text;
Assert.AreEqual("{\\an8}That was really delicious." + Environment.NewLine + "-I know.", result);
}
[TestMethod]
public void TestPositioning4()
{
Configuration.Settings.General.DialogStyle = Nikse.SubtitleEdit.Core.Enums.DialogType.DashSecondLineWithoutSpace;
var subtitle = new Subtitle(new List<Paragraph>() { new Paragraph("{\\an8}That was really delicious." + Environment.NewLine +
"<font color=\"#ffff00\">I know.</font>", 0, 2000) });
ConvertColorsToDialogUtils.ConvertColorsToDialogInSubtitle(subtitle, true, false, false);
var result = subtitle.Paragraphs.First().Text;
Assert.AreEqual("{\\an8}That was really delicious." + Environment.NewLine + "-I know.", result);
}
[TestMethod]
public void TestVttDialog1()
{
Configuration.Settings.General.DialogStyle = Nikse.SubtitleEdit.Core.Enums.DialogType.DashBothLinesWithSpace;
var subtitle = new Subtitle(new List<Paragraph>() { new Paragraph("<c.cyan>That was really delicious.</c>" + Environment.NewLine +
"I know.", 0, 2000) });
ConvertColorsToDialogUtils.ConvertColorsToDialogInSubtitle(subtitle, true, false, false);
var result = subtitle.Paragraphs.First().Text;
Assert.AreEqual("- That was really delicious." + Environment.NewLine + "- I know.", result);
}
[TestMethod]
public void TestVttDialog2()
{
Configuration.Settings.General.DialogStyle = Nikse.SubtitleEdit.Core.Enums.DialogType.DashBothLinesWithSpace;
var subtitle = new Subtitle(new List<Paragraph>() { new Paragraph("That's it!" + Environment.NewLine +
"<c.cyan>..sped to victory.</c>", 0, 2000) });
ConvertColorsToDialogUtils.ConvertColorsToDialogInSubtitle(subtitle, true, false, false);
var result = subtitle.Paragraphs.First().Text;
Assert.AreEqual("- That's it!" + Environment.NewLine + "- ..sped to victory.", result);
}
}
}

View File

@ -1,4 +1,5 @@
using System;
using System.Text.RegularExpressions;
namespace Nikse.SubtitleEdit.Core.Common
{
@ -29,10 +30,20 @@ namespace Nikse.SubtitleEdit.Core.Common
while (index < p.Text.Length)
{
if (index + "<font color".Length <= p.Text.Length && p.Text.SafeSubstring(index, "<font color".Length).ToLowerInvariant() == "<font color")
bool isHtmlColor = index + "<font color".Length <= p.Text.Length && p.Text.SafeSubstring(index, "<font color".Length).ToLowerInvariant() == "<font color";
bool isVttColor = index + "<c.".Length <= p.Text.Length && p.Text.SafeSubstring(index, "<c.".Length).ToLowerInvariant() == "<c.";
if (isHtmlColor || isVttColor)
{
// New color
newColor = p.Text.SafeSubstring(p.Text.IndexOf("=", index) + 1, p.Text.IndexOf(">", index) - p.Text.IndexOf("=", index) - 1).Replace("\"", "");
if (isVttColor)
{
newColor = p.Text.SafeSubstring(p.Text.IndexOf(".", index) + 1, p.Text.IndexOf(">", index) - p.Text.IndexOf(".", index) - 1);
}
else
{
newColor = p.Text.SafeSubstring(p.Text.IndexOf("=", index) + 1, p.Text.IndexOf(">", index) - p.Text.IndexOf("=", index) - 1).Replace("\"", "");
}
if (currentColor == null)
{
@ -46,7 +57,16 @@ namespace Nikse.SubtitleEdit.Core.Common
{
if (dashFirstLine && !firstLineAdded)
{
p.Text = dash + p.Text;
if (p.Text.StartsWith("{"))
{
var lastBraceIndex = p.Text.LastIndexOf("}");
p.Text = p.Text.SafeSubstring(0, lastBraceIndex + 1) + dash + p.Text.SafeSubstring(lastBraceIndex + 1);
}
else
{
p.Text = dash + p.Text;
}
index += dash.Length;
firstLineAdded = true;
@ -76,11 +96,23 @@ namespace Nikse.SubtitleEdit.Core.Common
}
else if (index + "</font>".Length <= p.Text.Length && p.Text.SafeSubstring(index, "</font>".Length).ToLowerInvariant() == "</font>")
{
// End of color
// End of HTML color
endOfColor = true;
index += "</font>".Length;
}
else if (index + "</c>".Length <= p.Text.Length && p.Text.SafeSubstring(index, "</c>".Length).ToLowerInvariant() == "</c>")
{
// End of VTT color
endOfColor = true;
index += "</c>".Length;
}
else if (index + "{".Length <= p.Text.Length && p.Text.SafeSubstring(index, "{".Length) == "{")
{
// ASS tag, jump over
index = p.Text.IndexOf("}", index) + 1;
}
else if (index + 1 <= p.Text.Length && p.Text.SafeSubstring(index, 1) == " " || p.Text.SafeSubstring(index, 1) == "\r" || p.Text.SafeSubstring(index, 1) == "\n")
{
// Whitespace, ignore
@ -107,7 +139,16 @@ namespace Nikse.SubtitleEdit.Core.Common
{
if (dashFirstLine && !firstLineAdded)
{
p.Text = dash + p.Text;
if (p.Text.StartsWith("{"))
{
var lastBraceIndex = p.Text.LastIndexOf("}");
p.Text = p.Text.SafeSubstring(0, lastBraceIndex + 1) + dash + p.Text.SafeSubstring(lastBraceIndex + 1);
}
else
{
p.Text = dash + p.Text;
}
index += dash.Length;
firstLineAdded = true;
@ -141,11 +182,18 @@ namespace Nikse.SubtitleEdit.Core.Common
if (removeColorTags)
{
p.Text = HtmlUtil.RemoveColorTags(p.Text);
if (p.Text.Contains("<c."))
{
p.Text = Regex.Replace(p.Text, @"<c(\.[\w\d]+)?>(.*?)</c>", "$2");
}
p.Text = p.Text.Replace(" ", " ").Replace(" " + Environment.NewLine, Environment.NewLine);
}
else
{
p.Text = p.Text.Replace(" </font> ", "</font> ").Replace(" </font>" + Environment.NewLine, "</font>" + Environment.NewLine);
p.Text = p.Text.Replace(" </c> ", "</c> ").Replace(" </c>" + Environment.NewLine, "</c>" + Environment.NewLine);
}
p.Text = p.Text.Trim();

View File

@ -29677,7 +29677,7 @@ namespace Nikse.SubtitleEdit.Forms
var formatType = f.GetType();
actorToolStripMenuItem.Visible = formatType == typeof(AdvancedSubStationAlpha) || formatType == typeof(SubStationAlpha);
convertColorsToDialogToolStripMenuItem.Visible = _subtitle.Paragraphs.Any(p => p.Text.Contains("<font color"));
convertColorsToDialogToolStripMenuItem.Visible = _subtitle.Paragraphs.Any(p => p.Text.Contains("<font color") || p.Text.Contains("<c."));
}
private void ContextMenuStripWaveformOpening(object sender, CancelEventArgs e)