Fix for DCiname and italic - thx Marco :)

This commit is contained in:
Nikolaj Olsson 2021-06-06 20:50:14 +02:00
parent f0688308b8
commit 65f47f4517
4 changed files with 61 additions and 64 deletions

View File

@ -83,10 +83,10 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
string languageEnglishName; string languageEnglishName;
try try
{ {
string languageShortName = LanguageAutoDetect.AutoDetectGoogleLanguage(subtitle); var languageShortName = LanguageAutoDetect.AutoDetectGoogleLanguage(subtitle);
var ci = CultureInfo.CreateSpecificCulture(languageShortName); var ci = CultureInfo.CreateSpecificCulture(languageShortName);
languageEnglishName = ci.EnglishName; languageEnglishName = ci.EnglishName;
int indexOfStartP = languageEnglishName.IndexOf('('); var indexOfStartP = languageEnglishName.IndexOf('(');
if (indexOfStartP > 1) if (indexOfStartP > 1)
{ {
languageEnglishName = languageEnglishName.Remove(indexOfStartP).Trim(); languageEnglishName = languageEnglishName.Remove(indexOfStartP).Trim();
@ -97,10 +97,10 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
languageEnglishName = "English"; languageEnglishName = "English";
} }
string hex = Guid.NewGuid().ToString().RemoveChar('-'); var hex = Guid.NewGuid().ToString().RemoveChar('-');
hex = hex.Insert(8, "-").Insert(13, "-").Insert(18, "-").Insert(23, "-"); hex = hex.Insert(8, "-").Insert(13, "-").Insert(18, "-").Insert(23, "-");
string xmlStructure = "<DCSubtitle Version=\"1.0\">" + Environment.NewLine + var xmlStructure = "<DCSubtitle Version=\"1.0\">" + Environment.NewLine +
" <SubtitleID>" + hex.ToLowerInvariant() + "</SubtitleID>" + Environment.NewLine + " <SubtitleID>" + hex.ToLowerInvariant() + "</SubtitleID>" + Environment.NewLine +
" <MovieTitle></MovieTitle>" + Environment.NewLine + " <MovieTitle></MovieTitle>" + Environment.NewLine +
" <ReelNumber>1</ReelNumber>" + Environment.NewLine + " <ReelNumber>1</ReelNumber>" + Environment.NewLine +
@ -115,7 +115,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
xml.PreserveWhitespace = true; xml.PreserveWhitespace = true;
var ss = Configuration.Settings.SubtitleSettings; var ss = Configuration.Settings.SubtitleSettings;
string loadedFontId = "Font1"; var loadedFontId = "Font1";
if (!string.IsNullOrEmpty(ss.CurrentDCinemaFontId)) if (!string.IsNullOrEmpty(ss.CurrentDCinemaFontId))
{ {
loadedFontId = ss.CurrentDCinemaFontId; loadedFontId = ss.CurrentDCinemaFontId;
@ -137,7 +137,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
xml.DocumentElement.SelectSingleNode("Language").InnerText = ss.CurrentDCinemaLanguage; xml.DocumentElement.SelectSingleNode("Language").InnerText = ss.CurrentDCinemaLanguage;
xml.DocumentElement.SelectSingleNode("LoadFont").Attributes["URI"].InnerText = ss.CurrentDCinemaFontUri; xml.DocumentElement.SelectSingleNode("LoadFont").Attributes["URI"].InnerText = ss.CurrentDCinemaFontUri;
xml.DocumentElement.SelectSingleNode("LoadFont").Attributes["Id"].InnerText = loadedFontId; xml.DocumentElement.SelectSingleNode("LoadFont").Attributes["Id"].InnerText = loadedFontId;
int fontSize = ss.CurrentDCinemaFontSize; var fontSize = ss.CurrentDCinemaFontSize;
xml.DocumentElement.SelectSingleNode("Font").Attributes["Id"].InnerText = loadedFontId; xml.DocumentElement.SelectSingleNode("Font").Attributes["Id"].InnerText = loadedFontId;
xml.DocumentElement.SelectSingleNode("Font").Attributes["Color"].InnerText = "FF" + Utilities.ColorToHex(ss.CurrentDCinemaFontColor).TrimStart('#').ToUpperInvariant(); xml.DocumentElement.SelectSingleNode("Font").Attributes["Color"].InnerText = "FF" + Utilities.ColorToHex(ss.CurrentDCinemaFontColor).TrimStart('#').ToUpperInvariant();
xml.DocumentElement.SelectSingleNode("Font").Attributes["Effect"].InnerText = ss.CurrentDCinemaFontEffect; xml.DocumentElement.SelectSingleNode("Font").Attributes["Effect"].InnerText = ss.CurrentDCinemaFontEffect;
@ -145,8 +145,8 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
xml.DocumentElement.SelectSingleNode("Font").Attributes["Size"].InnerText = ss.CurrentDCinemaFontSize.ToString(); xml.DocumentElement.SelectSingleNode("Font").Attributes["Size"].InnerText = ss.CurrentDCinemaFontSize.ToString();
var mainListFont = xml.DocumentElement.SelectSingleNode("Font"); var mainListFont = xml.DocumentElement.SelectSingleNode("Font");
int no = 0; var no = 0;
foreach (Paragraph p in subtitle.Paragraphs) foreach (var p in subtitle.Paragraphs)
{ {
if (!string.IsNullOrEmpty(p.Text)) if (!string.IsNullOrEmpty(p.Text))
{ {
@ -184,11 +184,11 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
bool alignVCenter = p.Text.StartsWith("{\\a9}", StringComparison.Ordinal) || p.Text.StartsWith("{\\a10}", StringComparison.Ordinal) || p.Text.StartsWith("{\\a11}", StringComparison.Ordinal) || // sub station alpha bool alignVCenter = p.Text.StartsWith("{\\a9}", StringComparison.Ordinal) || p.Text.StartsWith("{\\a10}", StringComparison.Ordinal) || p.Text.StartsWith("{\\a11}", StringComparison.Ordinal) || // sub station alpha
p.Text.StartsWith("{\\an4}", StringComparison.Ordinal) || p.Text.StartsWith("{\\an5}", StringComparison.Ordinal) || p.Text.StartsWith("{\\an6}", StringComparison.Ordinal); // advanced sub station alpha p.Text.StartsWith("{\\an4}", StringComparison.Ordinal) || p.Text.StartsWith("{\\an5}", StringComparison.Ordinal) || p.Text.StartsWith("{\\an6}", StringComparison.Ordinal); // advanced sub station alpha
string text = Utilities.RemoveSsaTags(p.Text); var text = Utilities.RemoveSsaTags(p.Text);
var lines = text.SplitToLines(); var lines = text.SplitToLines();
int vPos; int vPos;
int vPosFactor = (int)Math.Round(fontSize / 7.4); var vPosFactor = (int)Math.Round(fontSize / 7.4);
if (alignVTop) if (alignVTop)
{ {
vPos = Configuration.Settings.SubtitleSettings.DCinemaBottomMargin; // Bottom margin is normally 8 vPos = Configuration.Settings.SubtitleSettings.DCinemaBottomMargin; // Bottom margin is normally 8
@ -202,10 +202,10 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
vPos = (lines.Count * vPosFactor) - vPosFactor + Configuration.Settings.SubtitleSettings.DCinemaBottomMargin; // Bottom margin is normally 8 vPos = (lines.Count * vPosFactor) - vPosFactor + Configuration.Settings.SubtitleSettings.DCinemaBottomMargin; // Bottom margin is normally 8
} }
bool isItalic = false; var isItalic = false;
int fontNo = 0; var fontNo = 0;
var fontColors = new Stack<string>(); var fontColors = new Stack<string>();
foreach (string line in lines) foreach (var line in lines)
{ {
var textNode = xml.CreateElement("Text"); var textNode = xml.CreateElement("Text");
@ -256,7 +256,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
direction.InnerText = "horizontal"; direction.InnerText = "horizontal";
textNode.Attributes.Append(direction); textNode.Attributes.Append(direction);
int i = 0; var i = 0;
var txt = new StringBuilder(); var txt = new StringBuilder();
var html = new StringBuilder(); var html = new StringBuilder();
XmlNode nodeTemp = xml.CreateElement("temp"); XmlNode nodeTemp = xml.CreateElement("temp");
@ -308,14 +308,14 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
} }
else if (line.Substring(i).StartsWith("<font color=", StringComparison.Ordinal) && line.Substring(i + 3).Contains('>')) else if (line.Substring(i).StartsWith("<font color=", StringComparison.Ordinal) && line.Substring(i + 3).Contains('>'))
{ {
int endOfFont = line.IndexOf('>', i); var endOfFont = line.IndexOf('>', i);
if (txt.Length > 0) if (txt.Length > 0)
{ {
nodeTemp.InnerText = txt.ToString(); nodeTemp.InnerText = txt.ToString();
html.Append(nodeTemp.InnerXml); html.Append(nodeTemp.InnerXml);
txt.Clear(); txt.Clear();
} }
string c = GetDCinemaColorString(line.Substring(i + 12, endOfFont - (i + 12))); var c = GetDCinemaColorString(line.Substring(i + 12, endOfFont - (i + 12)));
fontColors.Push(c); fontColors.Push(c);
fontNo++; fontNo++;
i = endOfFont; i = endOfFont;
@ -464,7 +464,8 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
no++; no++;
} }
} }
string s = ToUtf8XmlString(xml).Replace("encoding=\"utf-8\"", "encoding=\"UTF-8\"");
var s = ToUtf8XmlString(xml).Replace("encoding=\"utf-8\"", "encoding=\"UTF-8\"");
while (s.Contains("</Font> ") || s.Contains(" <Font ") || s.Contains(Environment.NewLine + "<Font ") || s.Contains("</Font>" + Environment.NewLine)) while (s.Contains("</Font> ") || s.Contains(" <Font ") || s.Contains(Environment.NewLine + "<Font ") || s.Contains("</Font>" + Environment.NewLine))
{ {
while (s.Contains(" Font")) while (s.Contains(" Font"))
@ -577,7 +578,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
if (node.Attributes?["Color"] != null) if (node.Attributes?["Color"] != null)
{ {
ss.CurrentDCinemaFontColor = System.Drawing.ColorTranslator.FromHtml("#" + node.Attributes["Color"].InnerText); ss.CurrentDCinemaFontColor = ColorTranslator.FromHtml("#" + node.Attributes["Color"].InnerText);
} }
if (node.Attributes?["Effect"] != null) if (node.Attributes?["Effect"] != null)
@ -822,13 +823,14 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
} }
var hex = s.TrimStart('#'); var hex = s.TrimStart('#');
for (int i = s.Length - 1; i >= 0; i--) for (var i = s.Length - 1; i >= 0; i--)
{ {
if (!CharUtils.IsHexadecimal(s[i])) if (!CharUtils.IsHexadecimal(s[i]))
{ {
return s; return s;
} }
} }
return "#" + hex; return "#" + hex;
} }
@ -836,7 +838,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
{ {
var parts = s.Split(':', '.', ','); var parts = s.Split(':', '.', ',');
int milliseconds = int.Parse(parts[3]) * 4; // 000 to 249 var milliseconds = int.Parse(parts[3]) * 4; // 000 to 249
if (s.Contains('.')) if (s.Contains('.'))
{ {
milliseconds = int.Parse(parts[3].PadRight(3, '0')); milliseconds = int.Parse(parts[3].PadRight(3, '0'));
@ -854,6 +856,5 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
{ {
return $"{time.Hours:00}:{time.Minutes:00}:{time.Seconds:00}:{time.Milliseconds / 4:000}"; return $"{time.Hours:00}:{time.Minutes:00}:{time.Seconds:00}:{time.Milliseconds / 4:000}";
} }
} }
} }

View File

@ -231,18 +231,18 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
vPos = lines.Count * vPosFactor - vPosFactor + Configuration.Settings.SubtitleSettings.DCinemaBottomMargin; // Bottom margin is normally 8 vPos = lines.Count * vPosFactor - vPosFactor + Configuration.Settings.SubtitleSettings.DCinemaBottomMargin; // Bottom margin is normally 8
} }
bool isItalic = false; var isItalic = false;
int fontNo = 0; var fontNo = 0;
Stack<string> fontColors = new Stack<string>(); var fontColors = new Stack<string>();
foreach (string line in lines) foreach (var line in lines)
{ {
XmlNode textNode = xml.CreateElement("dcst:Text", "dcst"); var textNode = xml.CreateElement("dcst:Text", "dcst");
XmlAttribute vPosition = xml.CreateAttribute("Vposition"); var vPosition = xml.CreateAttribute("Vposition");
vPosition.InnerText = vPos.ToString(); vPosition.InnerText = vPos.ToString();
textNode.Attributes.Append(vPosition); textNode.Attributes.Append(vPosition);
XmlAttribute vAlign = xml.CreateAttribute("Valign"); var vAlign = xml.CreateAttribute("Valign");
if (alignVTop) if (alignVTop)
{ {
vAlign.InnerText = "top"; vAlign.InnerText = "top";
@ -258,7 +258,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
textNode.Attributes.Append(vAlign); textNode.Attributes.Append(vAlign); textNode.Attributes.Append(vAlign); textNode.Attributes.Append(vAlign);
XmlAttribute hAlign = xml.CreateAttribute("Halign"); var hAlign = xml.CreateAttribute("Halign");
if (alignLeft) if (alignLeft)
{ {
hAlign.InnerText = "left"; hAlign.InnerText = "left";
@ -274,14 +274,14 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
textNode.Attributes.Append(hAlign); textNode.Attributes.Append(hAlign);
XmlAttribute direction = xml.CreateAttribute("Direction"); var direction = xml.CreateAttribute("Direction");
direction.InnerText = "ltr"; direction.InnerText = "ltr";
textNode.Attributes.Append(direction); textNode.Attributes.Append(direction);
int i = 0; var i = 0;
var txt = new StringBuilder(); var txt = new StringBuilder();
var html = new StringBuilder(); var html = new StringBuilder();
XmlNode nodeTemp = xml.CreateElement("temp"); var nodeTemp = xml.CreateElement("temp");
while (i < line.Length) while (i < line.Length)
{ {
if (!isItalic && line.Substring(i).StartsWith("<i>")) if (!isItalic && line.Substring(i).StartsWith("<i>"))
@ -299,15 +299,14 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
{ {
if (txt.Length > 0) if (txt.Length > 0)
{ {
XmlNode fontNode = xml.CreateElement("dcst:Font", "dcst"); var fontNode = xml.CreateElement("dcst:Font", "dcst");
var italic = xml.CreateAttribute("Italic");
XmlAttribute italic = xml.CreateAttribute("Italic");
italic.InnerText = "yes"; italic.InnerText = "yes";
fontNode.Attributes.Append(italic); fontNode.Attributes.Append(italic);
if (line.Length > i + 5 && line.Substring(i + 4).StartsWith("</font>")) if (line.Length > i + 5 && line.Substring(i + 4).StartsWith("</font>"))
{ {
XmlAttribute fontColor = xml.CreateAttribute("Color"); var fontColor = xml.CreateAttribute("Color");
fontColor.InnerText = fontColors.Pop(); fontColor.InnerText = fontColors.Pop();
fontNode.Attributes.Append(fontColor); fontNode.Attributes.Append(fontColor);
fontNo--; fontNo--;
@ -323,7 +322,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
} }
else if (line.Substring(i).StartsWith("<font color=") && line.Substring(i + 3).Contains('>')) else if (line.Substring(i).StartsWith("<font color=") && line.Substring(i + 3).Contains('>'))
{ {
int endOfFont = line.IndexOf('>', i); var endOfFont = line.IndexOf('>', i);
if (txt.Length > 0) if (txt.Length > 0)
{ {
nodeTemp.InnerText = txt.ToString(); nodeTemp.InnerText = txt.ToString();
@ -339,15 +338,14 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
{ {
if (txt.Length > 0) if (txt.Length > 0)
{ {
XmlNode fontNode = xml.CreateElement("dcst:Font", "dcst"); var fontNode = xml.CreateElement("dcst:Font", "dcst");
var fontColor = xml.CreateAttribute("Color");
XmlAttribute fontColor = xml.CreateAttribute("Color");
fontColor.InnerText = fontColors.Pop(); fontColor.InnerText = fontColors.Pop();
fontNode.Attributes.Append(fontColor); fontNode.Attributes.Append(fontColor);
if (line.Length > i + 9 && line.Substring(i + 7).StartsWith("</i>")) if (line.Length > i + 9 && line.Substring(i + 7).StartsWith("</i>"))
{ {
XmlAttribute italic = xml.CreateAttribute("Italic"); var italic = xml.CreateAttribute("Italic");
italic.InnerText = "yes"; italic.InnerText = "yes";
fontNode.Attributes.Append(italic); fontNode.Attributes.Append(italic);
isItalic = false; isItalic = false;
@ -372,15 +370,14 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
{ {
if (txt.Length > 0) if (txt.Length > 0)
{ {
XmlNode fontNode = xml.CreateElement("dcst:Font", "dcst"); var fontNode = xml.CreateElement("dcst:Font", "dcst");
var fontColor = xml.CreateAttribute("Color");
XmlAttribute fontColor = xml.CreateAttribute("Color");
fontColor.InnerText = fontColors.Peek(); fontColor.InnerText = fontColors.Peek();
fontNode.Attributes.Append(fontColor); fontNode.Attributes.Append(fontColor);
if (isItalic) if (isItalic)
{ {
XmlAttribute italic = xml.CreateAttribute("Italic"); var italic = xml.CreateAttribute("Italic");
italic.InnerText = "yes"; italic.InnerText = "yes";
fontNode.Attributes.Append(italic); fontNode.Attributes.Append(italic);
} }
@ -390,18 +387,18 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
} }
else if (html.Length > 0 && html.ToString().StartsWith("<dcst:Font ")) else if (html.Length > 0 && html.ToString().StartsWith("<dcst:Font "))
{ {
XmlDocument temp = new XmlDocument(); var temp = new XmlDocument();
temp.LoadXml("<root>" + html.ToString().Replace("dcst:Font", "Font") + "</root>"); temp.LoadXml("<root>" + html.ToString().Replace("dcst:Font", "Font") + "</root>");
XmlNode fontNode = xml.CreateElement("dcst:Font"); var fontNode = xml.CreateElement("dcst:Font");
fontNode.InnerXml = temp.DocumentElement.SelectSingleNode("Font").InnerXml; fontNode.InnerXml = temp.DocumentElement.SelectSingleNode("Font").InnerXml;
foreach (XmlAttribute a in temp.DocumentElement.SelectSingleNode("Font").Attributes) foreach (XmlAttribute a in temp.DocumentElement.SelectSingleNode("Font").Attributes)
{ {
XmlAttribute newA = xml.CreateAttribute(a.Name); var newA = xml.CreateAttribute(a.Name);
newA.InnerText = a.InnerText; newA.InnerText = a.InnerText;
fontNode.Attributes.Append(newA); fontNode.Attributes.Append(newA);
} }
XmlAttribute fontColor = xml.CreateAttribute("Color"); var fontColor = xml.CreateAttribute("Color");
fontColor.InnerText = fontColors.Peek(); fontColor.InnerText = fontColors.Peek();
fontNode.Attributes.Append(fontColor); fontNode.Attributes.Append(fontColor);
@ -413,13 +410,11 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
{ {
if (txt.Length > 0) if (txt.Length > 0)
{ {
XmlNode fontNode = xml.CreateElement("dcst:Font", "dcst"); var fontNode = xml.CreateElement("dcst:Font", "dcst");
var italic = xml.CreateAttribute("Italic");
XmlAttribute italic = xml.CreateAttribute("Italic");
italic.InnerText = "yes"; italic.InnerText = "yes";
fontNode.Attributes.Append(italic); fontNode.Attributes.Append(italic);
fontNode.InnerText = HtmlUtil.RemoveHtmlTags(txt.ToString());
fontNode.InnerText = HtmlUtil.RemoveHtmlTags(line);
html.Append(fontNode.OuterXml); html.Append(fontNode.OuterXml);
} }
} }
@ -431,6 +426,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
html.Append(nodeTemp.InnerXml); html.Append(nodeTemp.InnerXml);
} }
} }
textNode.InnerXml = html.ToString(); textNode.InnerXml = html.ToString();
if (html.Length == 0) if (html.Length == 0)
{ {

View File

@ -420,7 +420,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
italic.InnerText = "yes"; italic.InnerText = "yes";
fontNode.Attributes.Append(italic); fontNode.Attributes.Append(italic);
fontNode.InnerText = HtmlUtil.RemoveHtmlTags(line); fontNode.InnerText = HtmlUtil.RemoveHtmlTags(txt.ToString());
html.Append(fontNode.OuterXml); html.Append(fontNode.OuterXml);
} }
} }

View File

@ -420,7 +420,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
italic.InnerText = "yes"; italic.InnerText = "yes";
fontNode.Attributes.Append(italic); fontNode.Attributes.Append(italic);
fontNode.InnerText = HtmlUtil.RemoveHtmlTags(line); fontNode.InnerText = HtmlUtil.RemoveHtmlTags(txt.ToString());
html.Append(fontNode.OuterXml); html.Append(fontNode.OuterXml);
} }
} }