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 override string Name
{
get { return "iTunes Timed Text"; }
}
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;
return base.IsMine(lines, fileName);
}
public override string ToText(Subtitle subtitle, string title)
{
XmlNode styleHead = null;
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());
}
}
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 = "24";
string frameRateMultiplier = "999 1000";
if (Math.Abs(Configuration.Settings.General.CurrentFrameRate - 30.0) < 0.2)
{
frameRate = "30";
}
else if (Math.Abs(Configuration.Settings.General.CurrentFrameRate - 25.0) < 0.01)
{
frameRate = "25";
frameRateMultiplier = "1 1";
}
const string language = "en-US";
string xmlStructure = "" + Environment.NewLine +
"" + Environment.NewLine +
" " + Environment.NewLine +
" " + Environment.NewLine +
"