using System; using System.Collections.Generic; using System.Xml; namespace Nikse.SubtitleEdit.Core.SubtitleFormats { /// /// Crappy format... should always be saved as UTF-8 without BOM (hacked Main.cs) and
tags should be oldstyle
///
public class ItunesTimedText : TimedText10 { public override string Extension { get { return ".itt"; } } public new const string NameOfFormat = "iTunes Timed Text"; public override string Name { get { return NameOfFormat; } } public override bool IsTimeBased { get { return true; } } public override bool IsMine(List lines, string fileName) { if (fileName != null && !fileName.EndsWith(Extension, StringComparison.OrdinalIgnoreCase)) return false; if (new NetflixTimedText().IsMine(lines, fileName)) return false; return base.IsMine(lines, fileName); } public override string ToText(Subtitle subtitle, string title) { XmlNode styleHead = null; bool convertedFromSubStationAlpha = false; if (subtitle.Header != null) { try { var x = new XmlDocument(); x.LoadXml(subtitle.Header); var xnsmgr = new XmlNamespaceManager(x.NameTable); xnsmgr.AddNamespace("ttml", "http://www.w3.org/ns/ttml"); styleHead = x.DocumentElement.SelectSingleNode("ttml:head", xnsmgr); } catch { styleHead = null; } if (styleHead == null && (subtitle.Header.Contains("[V4+ Styles]") || subtitle.Header.Contains("[V4 Styles]"))) { var x = new XmlDocument(); x.LoadXml(new ItunesTimedText().ToText(new Subtitle(), "tt")); // load default xml var xnsmgr = new XmlNamespaceManager(x.NameTable); xnsmgr.AddNamespace("ttml", "http://www.w3.org/ns/ttml"); styleHead = x.DocumentElement.SelectSingleNode("ttml:head", xnsmgr); styleHead.SelectSingleNode("ttml:styling", xnsmgr).RemoveAll(); foreach (string styleName in AdvancedSubStationAlpha.GetStylesFromHeader(subtitle.Header)) { try { var ssaStyle = AdvancedSubStationAlpha.GetSsaStyle(styleName, subtitle.Header); if (ssaStyle != null) { string fontStyle = "normal"; if (ssaStyle.Italic) fontStyle = "italic"; string fontWeight = "normal"; if (ssaStyle.Bold) fontWeight = "bold"; AddStyleToXml(x, styleHead, xnsmgr, ssaStyle.Name, ssaStyle.FontName, fontWeight, fontStyle, Utilities.ColorToHex(ssaStyle.Primary), ssaStyle.FontSize.ToString()); convertedFromSubStationAlpha = true; } } catch { } } subtitle.Header = x.OuterXml; // save new xml with styles in header } } var xml = new XmlDocument { XmlResolver = null }; var nsmgr = new XmlNamespaceManager(xml.NameTable); nsmgr.AddNamespace("ttml", "http://www.w3.org/ns/ttml"); nsmgr.AddNamespace("ttp", "http://www.w3.org/ns/10/ttml#parameter"); nsmgr.AddNamespace("tts", "http://www.w3.org/ns/10/ttml#style"); nsmgr.AddNamespace("ttm", "http://www.w3.org/ns/10/ttml#metadata"); string frameRate = ((int)Math.Round(Configuration.Settings.General.CurrentFrameRate)).ToString(); string frameRateMultiplier = "999 1000"; if (Configuration.Settings.General.CurrentFrameRate % 1.0 < 0.01) { frameRateMultiplier = "1 1"; } string dropMode = "nonDrop"; if (Math.Abs(Configuration.Settings.General.CurrentFrameRate - 29.97) < 0.01) { dropMode = "dropNTSC"; } const string language = "en-US"; string xmlStructure = "" + Environment.NewLine + "" + Environment.NewLine + " " + Environment.NewLine + " " + Environment.NewLine + "