Try to fix Netflix dfxp issue - thx IngunnHelge :)

Fix #5752
This commit is contained in:
niksedk 2023-03-09 08:53:22 +01:00
parent c3e436b0c8
commit 3211934551
3 changed files with 197 additions and 16 deletions

View File

@ -0,0 +1,149 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Nikse.SubtitleEdit.Core.Common;
using Nikse.SubtitleEdit.Core.SubtitleFormats;
using System;
namespace Test.Logic.SubtitleFormats
{
[TestClass]
public class NetflixTimedTextTest
{
[TestMethod]
public void Italic()
{
// Arrange
var input = "This is an <i>italic</i> word!";
var format = new NetflixTimedText();
var sub = new Subtitle();
sub.Paragraphs.Add(new Paragraph(input, 0, 3000));
// Act
var raw = sub.ToText(format);
sub = new Subtitle();
format.LoadSubtitle(sub, raw.SplitToLines(), null);
// Assert
Assert.AreEqual(1, sub.Paragraphs.Count);
Assert.AreEqual(input, sub.Paragraphs[0].Text);
}
[TestMethod]
public void ItalicAndFont()
{
// Arrange
var input = "This is <i><font color=\"red\">red</font></i>" + Environment.NewLine +
"<i><font color=\"blue\">and blue</font></i>";
var format = new NetflixTimedText();
var sub = new Subtitle();
sub.Paragraphs.Add(new Paragraph(input, 0, 3000));
// Act
var raw = sub.ToText(format);
sub = new Subtitle();
format.LoadSubtitle(sub, raw.SplitToLines(), null);
// Assert
Assert.AreEqual(1, sub.Paragraphs.Count);
Assert.AreEqual(input, sub.Paragraphs[0].Text);
}
[TestMethod]
public void BoldAndFont()
{
// Arrange
var input = "This is <b><font color=\"red\">red</font></b>" + Environment.NewLine +
"<b><font color=\"blue\">and blue</font></b>";
var format = new NetflixTimedText();
var sub = new Subtitle();
sub.Paragraphs.Add(new Paragraph(input, 0, 3000));
// Act
var raw = sub.ToText(format);
sub = new Subtitle();
format.LoadSubtitle(sub, raw.SplitToLines(), null);
// Assert
Assert.AreEqual(1, sub.Paragraphs.Count);
Assert.AreEqual(input, sub.Paragraphs[0].Text);
}
[TestMethod]
public void FontAndItalic()
{
// Arrange
var input = "This is <font color=\"red\"><i>red</i></font>" + Environment.NewLine +
"<font color=\"blue\"><i>and blue</i></font>";
var format = new NetflixTimedText();
var sub = new Subtitle();
sub.Paragraphs.Add(new Paragraph(input, 0, 3000));
// Act
var raw = sub.ToText(format);
sub = new Subtitle();
format.LoadSubtitle(sub, raw.SplitToLines(), null);
// Assert
Assert.AreEqual(1, sub.Paragraphs.Count);
Assert.AreEqual(input, sub.Paragraphs[0].Text);
}
[TestMethod]
public void FontAndBold()
{
// Arrange
var input = "This is <font color=\"red\"><b>red</b></font>" + Environment.NewLine +
"<font color=\"blue\"><b>and blue</b></font>";
var format = new NetflixTimedText();
var sub = new Subtitle();
sub.Paragraphs.Add(new Paragraph(input, 0, 3000));
// Act
var raw = sub.ToText(format);
sub = new Subtitle();
format.LoadSubtitle(sub, raw.SplitToLines(), null);
// Assert
Assert.AreEqual(1, sub.Paragraphs.Count);
Assert.AreEqual(input, sub.Paragraphs[0].Text);
}
[TestMethod]
public void FontAndItalicSeparate()
{
// Arrange
var input = "This is <font color=\"red\">red</font> and <i>italic</i>";
var format = new NetflixTimedText();
var sub = new Subtitle();
sub.Paragraphs.Add(new Paragraph(input, 0, 3000));
// Act
var raw = sub.ToText(format);
sub = new Subtitle();
format.LoadSubtitle(sub, raw.SplitToLines(), null);
// Assert
Assert.AreEqual(1, sub.Paragraphs.Count);
Assert.AreEqual(input, sub.Paragraphs[0].Text);
}
[TestMethod]
public void ItalicAndBold()
{
// Arrange
var input = "This is <i><b>ib</b></i>" + Environment.NewLine +
"<i><b>and ib</b></i>";
var format = new NetflixTimedText();
var sub = new Subtitle();
sub.Paragraphs.Add(new Paragraph(input, 0, 3000));
// Act
var raw = sub.ToText(format);
sub = new Subtitle();
format.LoadSubtitle(sub, raw.SplitToLines(), null);
// Assert
Assert.AreEqual(1, sub.Paragraphs.Count);
Assert.AreEqual(input, sub.Paragraphs[0].Text);
}
}
}

View File

@ -69,6 +69,7 @@
<Compile Include="Logic\ConvertColorsToDialogTest.cs" />
<Compile Include="Logic\NetflixHelperTest.cs" />
<Compile Include="Logic\SubtitleFormats\EbuStlTest.cs" />
<Compile Include="Logic\SubtitleFormats\NetflixTimedTextTest.cs" />
<Compile Include="Logic\SubtitleFormats\PacTest.cs" />
<Compile Include="Core\UUEncodingTest.cs" />
<Compile Include="Core\CharUtilsTest.cs" />

View File

@ -151,12 +151,12 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
id = node.Attributes["id"].Value;
}
if (id != null && id == "bottomCenter")
if (id is "bottomCenter")
{
hasBottomCenterRegion = true;
}
if (id != null && id == "topCenter")
if (id is "topCenter")
{
hasTopCenterRegion = true;
}
@ -168,19 +168,19 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
XmlNode paragraph = xml.CreateElement("p", "http://www.w3.org/ns/ttml");
var text = p.Text;
XmlAttribute pid = xml.CreateAttribute("xml:id");
var pid = xml.CreateAttribute("xml:id");
pid.InnerText = "p" + ++no;
paragraph.Attributes.Append(pid);
XmlAttribute start = xml.CreateAttribute("begin");
var start = xml.CreateAttribute("begin");
start.InnerText = string.Format(CultureInfo.InvariantCulture, "{0:00}:{1:00}:{2:00}.{3:000}", p.StartTime.Hours, p.StartTime.Minutes, p.StartTime.Seconds, p.StartTime.Milliseconds);
paragraph.Attributes.Append(start);
XmlAttribute end = xml.CreateAttribute("end");
var end = xml.CreateAttribute("end");
end.InnerText = string.Format(CultureInfo.InvariantCulture, "{0:00}:{1:00}:{2:00}.{3:000}", p.EndTime.Hours, p.EndTime.Minutes, p.EndTime.Seconds, p.EndTime.Milliseconds);
paragraph.Attributes.Append(end);
XmlAttribute regionP = xml.CreateAttribute("region");
var regionP = xml.CreateAttribute("region");
if (text.StartsWith("{\\an7}", StringComparison.Ordinal) || text.StartsWith("{\\an8}", StringComparison.Ordinal) || text.StartsWith("{\\an9}", StringComparison.Ordinal))
{
if (hasTopCenterRegion)
@ -231,9 +231,19 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
}
else if (line.Substring(i).StartsWith("<i>", StringComparison.OrdinalIgnoreCase))
{
styles.Push(currentStyle);
currentStyle = xml.CreateNode(XmlNodeType.Element, "span", null);
paragraph.AppendChild(currentStyle);
if (currentStyle.Name == "span")
{
var parent = currentStyle;
currentStyle = xml.CreateNode(XmlNodeType.Element, "span", null);
parent.AppendChild(currentStyle);
}
else
{
styles.Push(currentStyle);
currentStyle = xml.CreateNode(XmlNodeType.Element, "span", null);
paragraph.AppendChild(currentStyle);
}
XmlAttribute attr = xml.CreateAttribute("tts:fontStyle", "http://www.w3.org/ns/10/ttml#style");
attr.InnerText = "italic";
currentStyle.Attributes.Append(attr);
@ -242,8 +252,19 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
}
else if (line.Substring(i).StartsWith("<b>", StringComparison.OrdinalIgnoreCase))
{
currentStyle = xml.CreateNode(XmlNodeType.Element, "span", null);
paragraph.AppendChild(currentStyle);
if (currentStyle.Name == "span")
{
var parent = currentStyle;
currentStyle = xml.CreateNode(XmlNodeType.Element, "span", null);
parent.AppendChild(currentStyle);
}
else
{
styles.Push(currentStyle);
currentStyle = xml.CreateNode(XmlNodeType.Element, "span", null);
paragraph.AppendChild(currentStyle);
}
XmlAttribute attr = xml.CreateAttribute("tts:fontWeight", "http://www.w3.org/ns/10/ttml#style");
attr.InnerText = "bold";
currentStyle.Attributes.Append(attr);
@ -263,10 +284,20 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
var arr = fontContent.Substring(fontContent.IndexOf(" color=", StringComparison.Ordinal) + 7).Trim().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
if (arr.Length > 0)
{
if (currentStyle.Name == "span")
{
var parent = currentStyle;
currentStyle = xml.CreateNode(XmlNodeType.Element, "span", null);
parent.AppendChild(currentStyle);
}
else
{
currentStyle = xml.CreateNode(XmlNodeType.Element, "span", null);
paragraph.AppendChild(currentStyle);
}
var attr = xml.CreateAttribute("tts:color", "http://www.w3.org/ns/10/ttml#style");
var fontColor = arr[0].Trim('\'').Trim('"').Trim('\'');
currentStyle = xml.CreateNode(XmlNodeType.Element, "span", null);
paragraph.AppendChild(currentStyle);
XmlAttribute attr = xml.CreateAttribute("tts:color", "http://www.w3.org/ns/10/ttml#style");
attr.InnerText = fontColor;
currentStyle.Attributes.Append(attr);
fontColors.Push(fontColor);
@ -312,9 +343,9 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
}
paragraph.AppendChild(currentStyle);
if (line.Substring(i).StartsWith("</font>", StringComparison.OrdinalIgnoreCase))
if (line.Substring(i).StartsWith("</i></font>", StringComparison.OrdinalIgnoreCase))
{
skipCount = 6;
skipCount = 10;
fontColors.Pop();
}
else