Add first support for WebVTT colors

This commit is contained in:
Martijn van Berkel (Flitskikker) 2023-07-12 22:47:30 +02:00
parent 344e05ecf5
commit 98dadfcf99
3 changed files with 57 additions and 4 deletions

View File

@ -374,5 +374,33 @@ namespace Test.Logic
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)
{
@ -85,11 +96,18 @@ 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
@ -164,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

@ -29674,7 +29674,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)