/// <summary> /// The load styles from timed text 10. /// </summary> /// <param name="subtitle"> /// The subtitle. /// </param> /// <param name="title"> /// The title. /// </param> /// <param name="header"> /// The header. /// </param> /// <param name="headerNoStyles"> /// The header no styles. /// </param> /// <param name="sb"> /// The sb. /// </param> private static void LoadStylesFromTimedText10(Subtitle subtitle, string title, string header, string headerNoStyles, StringBuilder sb) { try { List<string> lines = new List<string>(); foreach (string s in subtitle.Header.SplitToLines()) { lines.Add(s); } TimedText10 tt = new TimedText10(); Subtitle sub = new Subtitle(); tt.LoadSubtitle(sub, lines, string.Empty); XmlDocument xml = new XmlDocument(); xml.LoadXml(subtitle.Header); XmlNamespaceManager nsmgr = new XmlNamespaceManager(xml.NameTable); nsmgr.AddNamespace("ttml", "http://www.w3.org/ns/ttml"); XmlNode head = xml.DocumentElement.SelectSingleNode("ttml:head", nsmgr); int stylexmlCount = 0; StringBuilder ttStyles = new StringBuilder(); foreach (XmlNode node in head.SelectNodes("//ttml:style", nsmgr)) { string name = null; if (node.Attributes["xml:id"] != null) { name = node.Attributes["xml:id"].Value; } else if (node.Attributes["id"] != null) { name = node.Attributes["id"].Value; } if (name != null) { stylexmlCount++; string fontFamily = "Arial"; if (node.Attributes["tts:fontFamily"] != null) { fontFamily = node.Attributes["tts:fontFamily"].Value; } string fontWeight = "normal"; if (node.Attributes["tts:fontWeight"] != null) { fontWeight = node.Attributes["tts:fontWeight"].Value; } string fontStyle = "normal"; if (node.Attributes["tts:fontStyle"] != null) { fontStyle = node.Attributes["tts:fontStyle"].Value; } string color = "#ffffff"; if (node.Attributes["tts:color"] != null) { color = node.Attributes["tts:color"].Value.Trim(); } Color c = Color.White; try { if (color.StartsWith("rgb(", StringComparison.Ordinal)) { string[] arr = color.Remove(0, 4).TrimEnd(')').Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); c = Color.FromArgb(int.Parse(arr[0]), int.Parse(arr[1]), int.Parse(arr[2])); } else { c = ColorTranslator.FromHtml(color); } } catch { } string fontSize = "20"; if (node.Attributes["tts:fontSize"] != null) { fontSize = node.Attributes["tts:fontSize"].Value.Replace("px", string.Empty).Replace("em", string.Empty); } int fSize; if (!int.TryParse(fontSize, out fSize)) { fSize = 20; } string italic = "0"; if (fontStyle == "italic") { italic = "1"; } string bold = "0"; if (fontWeight == "bold") { bold = "1"; } const string styleFormat = "Style: {0},{1},{2},{3},&H0300FFFF,&H00000000,&H02000000,{4},{5},0,0,100,100,0,0,1,2,2,2,10,10,10,1"; ttStyles.AppendLine(string.Format(styleFormat, name, fontFamily, fSize, GetSsaColorString(c), bold, italic)); } } if (stylexmlCount > 0) { sb.AppendLine(string.Format(headerNoStyles, title, ttStyles)); subtitle.Header = sb.ToString(); } else { sb.AppendLine(string.Format(header, title)); } // Set correct style on paragraphs foreach (Paragraph p in subtitle.Paragraphs) { if (p.Extra != null && p.Extra.Contains('/')) { p.Extra = p.Extra.Split('/')[0].Trim(); } } } catch { sb.AppendLine(string.Format(header, title)); } }
private static void LoadStylesFromTimedText10(Subtitle subtitle, string title, string header, string headerNoStyles, StringBuilder sb) { try { var lines = new List<string>(); foreach (string s in subtitle.Header.Replace(Environment.NewLine, "\n").Split('\n')) lines.Add(s); var tt = new TimedText10(); var sub = new Subtitle(); tt.LoadSubtitle(sub, lines, string.Empty); var xml = new XmlDocument(); xml.LoadXml(subtitle.Header); var nsmgr = new XmlNamespaceManager(xml.NameTable); nsmgr.AddNamespace("ttml", "http://www.w3.org/ns/ttml"); XmlNode head = xml.DocumentElement.SelectSingleNode("ttml:head", nsmgr); int stylexmlCount = 0; var ttStyles = new StringBuilder(); foreach (XmlNode node in head.SelectNodes("//ttml:style", nsmgr)) { string name = null; if (node.Attributes["xml:id"] != null) name = node.Attributes["xml:id"].Value; else if (node.Attributes["id"] != null) name = node.Attributes["id"].Value; if (name != null) { stylexmlCount++; string fontFamily = "Arial"; if (node.Attributes["tts:fontFamily"] != null) fontFamily = node.Attributes["tts:fontFamily"].Value; string fontWeight = "normal"; if (node.Attributes["tts:fontWeight"] != null) fontWeight = node.Attributes["tts:fontWeight"].Value; string fontStyle = "normal"; if (node.Attributes["tts:fontStyle"] != null) fontStyle = node.Attributes["tts:fontStyle"].Value; string color = "#ffffff"; if (node.Attributes["tts:color"] != null) color = node.Attributes["tts:color"].Value.Trim(); Color c = Color.White; try { if (color.StartsWith("rgb(", StringComparison.Ordinal)) { string[] arr = color.Remove(0, 4).TrimEnd(')').Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); c = Color.FromArgb(int.Parse(arr[0]), int.Parse(arr[1]), int.Parse(arr[2])); } else { c = ColorTranslator.FromHtml(color); } } catch { } string fontSize = "20"; if (node.Attributes["tts:fontSize"] != null) fontSize = node.Attributes["tts:fontSize"].Value.Replace("px", string.Empty).Replace("em", string.Empty); int fSize; if (!int.TryParse(fontSize, out fSize)) fSize = 20; string styleFormat = "Style: {0},{1},{2},{3},65535,65535,-2147483640,-1,0,1,3,0,2,10,10,10,0,1"; ttStyles.AppendLine(string.Format(styleFormat, name, fontFamily, fSize.ToString(), c.ToArgb())); } } if (stylexmlCount > 0) { sb.AppendLine(string.Format(headerNoStyles, title, ttStyles)); subtitle.Header = sb.ToString(); } else { sb.AppendLine(string.Format(header, title)); } } catch { sb.AppendLine(string.Format(header, title)); } }
public override void LoadSubtitle(Subtitle subtitle, List <string> lines, string fileName) { _errorCount = 0; var sb = new StringBuilder(); lines.ForEach(line => sb.AppendLine(line)); var xml = new XmlDocument { XmlResolver = null }; xml.LoadXml(sb.ToString().Trim()); var nsmgr = new XmlNamespaceManager(xml.NameTable); nsmgr.AddNamespace("ttaf1", xml.DocumentElement.NamespaceURI); XmlNode div = xml.DocumentElement.SelectSingleNode("//ttaf1:body", nsmgr).SelectSingleNode("ttaf1:div", nsmgr); if (div == null) { div = xml.DocumentElement.SelectSingleNode("//ttaf1:body", nsmgr).FirstChild; } var styleDic = new Dictionary <string, string>(); foreach (XmlNode node in xml.DocumentElement.SelectNodes("//ttaf1:style", nsmgr)) { if (node.Attributes["tts:fontStyle"] != null && node.Attributes["xml:id"] != null) { styleDic.Add(node.Attributes["xml:id"].Value, node.Attributes["tts:fontStyle"].Value); } } foreach (XmlNode node in div.ChildNodes) { try { var pText = new StringBuilder(); foreach (XmlNode innerNode in node.ChildNodes) { switch (innerNode.Name) { case "br": pText.AppendLine(); break; case "span": bool italic = false; if (innerNode.Attributes["style"] != null && styleDic.ContainsKey(innerNode.Attributes["style"].Value)) { if (styleDic[innerNode.Attributes["style"].Value].Contains("italic")) { italic = true; pText.Append("<i>"); } } if (!italic && innerNode.Attributes != null) { var fs = innerNode.Attributes.GetNamedItem("tts:fontStyle"); if (fs != null && fs.Value == "italic") { italic = true; pText.Append("<i>"); } } if (innerNode.HasChildNodes) { foreach (XmlNode innerInnerNode in innerNode.ChildNodes) { if (innerInnerNode.Name == "br") { pText.AppendLine(); } else { pText.Append(innerInnerNode.InnerText); } } } else { pText.Append(innerNode.InnerText); } if (italic) { pText.Append("</i>"); } break; case "i": pText.Append("<i>" + innerNode.InnerText + "</i>"); break; case "b": pText.Append("<b>" + innerNode.InnerText + "</b>"); break; default: pText.Append(innerNode.InnerText); break; } } string start = null; // = node.Attributes["begin"].InnerText; string end = null; // = node.Attributes["begin"].InnerText; string dur = null; // = node.Attributes["begin"].InnerText; foreach (XmlAttribute attr in node.Attributes) { if (attr.Name.EndsWith("begin", StringComparison.Ordinal)) { start = attr.InnerText; } else if (attr.Name.EndsWith("end", StringComparison.Ordinal)) { end = attr.InnerText; } else if (attr.Name.EndsWith("duration", StringComparison.Ordinal)) { dur = attr.InnerText; } } //string start = node.Attributes["begin"].InnerText; string text = pText.ToString(); text = text.Replace(Environment.NewLine + "</i>", "</i>" + Environment.NewLine); text = text.Replace("<i></i>", string.Empty).Trim(); if (end != null) { //string end = node.Attributes["end"].InnerText; double dBegin, dEnd; if (!start.Contains(':') && Utilities.CountTagInText(start, '.') == 1 && !start.Contains(':') && Utilities.CountTagInText(start, '.') == 1 && double.TryParse(start, out dBegin) && double.TryParse(end, out dEnd)) { subtitle.Paragraphs.Add(new Paragraph(text, dBegin * 1000.0, dEnd * 1000.0)); } else { if (start.Length == 8 && start[2] == ':' && start[5] == ':' && end.Length == 8 && end[2] == ':' && end[5] == ':') { var p = new Paragraph(); string[] parts = start.Split(new[] { ':' }); p.StartTime = new TimeCode(int.Parse(parts[0]), int.Parse(parts[1]), int.Parse(parts[2]), 0); parts = end.Split(new[] { ':' }); p.EndTime = new TimeCode(int.Parse(parts[0]), int.Parse(parts[1]), int.Parse(parts[2]), 0); p.Text = text; subtitle.Paragraphs.Add(p); } else { subtitle.Paragraphs.Add(new Paragraph(TimedText10.GetTimeCode(start, false), TimedText10.GetTimeCode(end, false), text)); } } } else if (dur != null) { TimeCode duration = TimedText10.GetTimeCode(dur, false); TimeCode startTime = TimedText10.GetTimeCode(start, false); var endTime = new TimeCode(startTime.TotalMilliseconds + duration.TotalMilliseconds); subtitle.Paragraphs.Add(new Paragraph(startTime, endTime, text)); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); _errorCount++; } } subtitle.RemoveEmptyLines(); subtitle.Renumber(1); }
private static void LoadStylesFromTimedText10(Subtitle subtitle, string title, string header, string headerNoStyles, StringBuilder sb) { try { var lines = new List <string>(); foreach (string s in subtitle.Header.Replace(Environment.NewLine, "\n").Split('\n')) { lines.Add(s); } var tt = new TimedText10(); var sub = new Subtitle(); tt.LoadSubtitle(sub, lines, string.Empty); var xml = new XmlDocument(); xml.LoadXml(subtitle.Header); var nsmgr = new XmlNamespaceManager(xml.NameTable); nsmgr.AddNamespace("ttml", "http://www.w3.org/ns/ttml"); XmlNode head = xml.DocumentElement.SelectSingleNode("ttml:head", nsmgr); int stylexmlCount = 0; var ttStyles = new StringBuilder(); foreach (XmlNode node in head.SelectNodes("//ttml:style", nsmgr)) { string name = null; if (node.Attributes["xml:id"] != null) { name = node.Attributes["xml:id"].Value; } else if (node.Attributes["id"] != null) { name = node.Attributes["id"].Value; } if (name != null) { stylexmlCount++; string fontFamily = "Arial"; if (node.Attributes["tts:fontFamily"] != null) { fontFamily = node.Attributes["tts:fontFamily"].Value; } string fontWeight = "normal"; if (node.Attributes["tts:fontWeight"] != null) { fontWeight = node.Attributes["tts:fontWeight"].Value; } string fontStyle = "normal"; if (node.Attributes["tts:fontStyle"] != null) { fontStyle = node.Attributes["tts:fontStyle"].Value; } string color = "#ffffff"; if (node.Attributes["tts:color"] != null) { color = node.Attributes["tts:color"].Value.Trim(); } Color c = Color.White; try { if (color.StartsWith("rgb(", StringComparison.Ordinal)) { string[] arr = color.Remove(0, 4).TrimEnd(')').Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); c = Color.FromArgb(int.Parse(arr[0]), int.Parse(arr[1]), int.Parse(arr[2])); } else { c = ColorTranslator.FromHtml(color); } } catch { } string fontSize = "20"; if (node.Attributes["tts:fontSize"] != null) { fontSize = node.Attributes["tts:fontSize"].Value.Replace("px", string.Empty).Replace("em", string.Empty); } int fSize; if (!int.TryParse(fontSize, out fSize)) { fSize = 20; } const string styleFormat = "Style: {0},{1},{2},{3},65535,65535,-2147483640,-1,0,1,3,0,2,10,10,10,0,1"; ttStyles.AppendLine(string.Format(styleFormat, name, fontFamily, fSize, c.ToArgb())); } } if (stylexmlCount > 0) { sb.AppendLine(string.Format(headerNoStyles, title, ttStyles)); subtitle.Header = sb.ToString(); } else { sb.AppendLine(string.Format(header, title)); } } catch { sb.AppendLine(string.Format(header, title)); } }
public override void LoadSubtitle(Subtitle subtitle, List <string> lines, string fileName) { _errorCount = 0; var sb = new StringBuilder(); lines.ForEach(line => sb.AppendLine(line)); var xml = new XmlDocument(); xml.LoadXml(sb.ToString().Trim()); var nsmgr = new XmlNamespaceManager(xml.NameTable); nsmgr.AddNamespace("ttaf1", xml.DocumentElement.NamespaceURI); XmlNode div; var body = xml.DocumentElement.SelectSingleNode("//ttaf1:body", nsmgr); if (body == null) { div = xml.DocumentElement; } else { div = xml.DocumentElement.SelectSingleNode("//ttaf1:body", nsmgr).SelectSingleNode("ttaf1:div", nsmgr); } if (div == null) { div = xml.DocumentElement.SelectSingleNode("//ttaf1:body", nsmgr).FirstChild; } foreach (XmlNode node in div.ChildNodes) { try { var pText = new StringBuilder(); foreach (XmlNode innerNode in node.ChildNodes) { switch (innerNode.Name) { case "br": pText.AppendLine(); break; case "span": bool italic = false; if (innerNode.Attributes != null) { var fs = innerNode.Attributes.GetNamedItem("tts:fontStyle"); if (fs != null && fs.Value == "italic") { italic = true; pText.Append("<i>"); } } if (innerNode.HasChildNodes) { foreach (XmlNode innerInnerNode in innerNode.ChildNodes) { if (innerInnerNode.Name == "br") { pText.AppendLine(); } else { pText.Append(innerInnerNode.InnerText); } } } else { pText.Append(innerNode.InnerText); } if (italic) { pText.Append("</i>"); } break; default: pText.Append(innerNode.InnerText); break; } } string start = node.Attributes["begin"].InnerText; string text = pText.ToString(); text = text.Replace(Environment.NewLine + "</i>", "</i>" + Environment.NewLine); text = text.Replace("<i></i>", string.Empty); if (node.Attributes["end"] != null) { string end = node.Attributes["end"].InnerText; subtitle.Paragraphs.Add(new Paragraph(TimedText10.GetTimeCode(start, false), TimedText10.GetTimeCode(end, false), text)); } else if (node.Attributes["dur"] != null) { TimeCode duration = TimedText10.GetTimeCode(node.Attributes["dur"].InnerText, false); TimeCode startTime = TimedText10.GetTimeCode(start, false); TimeCode endTime = new TimeCode(TimeSpan.FromMilliseconds(startTime.TotalMilliseconds + duration.TotalMilliseconds)); subtitle.Paragraphs.Add(new Paragraph(startTime, endTime, text)); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); _errorCount++; } } bool allBelow100 = true; foreach (Paragraph p in subtitle.Paragraphs) { if (p.StartTime.Milliseconds >= 100 || p.EndTime.Milliseconds >= 100) { allBelow100 = false; } } if (allBelow100) { foreach (Paragraph p in subtitle.Paragraphs) { p.StartTime.Milliseconds *= 10; p.EndTime.Milliseconds *= 10; } } subtitle.Renumber(1); }