public void DcinemaInteropItalic()
 {
     var target = new DCSubtitle();
     var subtitle = new Subtitle();
     subtitle.Paragraphs.Add(new Paragraph("<i>Italic</i>", 1000, 5000));
     string text = target.ToText(subtitle, "title");
     Assert.IsTrue(text.Contains("<Font Italic=\"yes\""));
 }
 public void DcinemaInteropColorAndItalic()
 {
     var target = new DCSubtitle();
     var subtitle = new Subtitle();
     subtitle.Paragraphs.Add(new Paragraph("<font color=\"#ff0000\"><i>Red</i></font>", 1000, 5000));
     string text = target.ToText(subtitle, "title");
     Assert.IsTrue(text.Contains(" Italic=\"yes\"") && text.Contains(" Color=\"FFFF0000\""));
 }
示例#3
0
        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().Replace("<dcst:", "<").Replace("</dcst:", "</").Replace("xmlns=\"http://www.smpte-ra.org/schemas/428-7/2010/DCST\"", string.Empty)); // tags might be prefixed with namespace (or not)... so we just remove them

            var ss = Configuration.Settings.SubtitleSettings;

            try
            {
                ss.InitializeDCinameSettings(true);
                XmlNode node = xml.DocumentElement.SelectSingleNode("Id");
                if (node != null)
                {
                    ss.CurrentDCinemaSubtitleId = node.InnerText;
                }

                node = xml.DocumentElement.SelectSingleNode("ReelNumber");
                if (node != null)
                {
                    ss.CurrentDCinemaReelNumber = node.InnerText;
                }

                node = xml.DocumentElement.SelectSingleNode("EditRate");
                if (node != null)
                {
                    ss.CurrentDCinemaEditRate = node.InnerText;
                }

                node = xml.DocumentElement.SelectSingleNode("TimeCodeRate");
                if (node != null)
                {
                    ss.CurrentDCinemaTimeCodeRate = node.InnerText;
                    if (ss.CurrentDCinemaEditRate == "24")
                    {
                        Configuration.Settings.General.CurrentFrameRate = 24;
                    }
                    else if (ss.CurrentDCinemaEditRate == "25")
                    {
                        Configuration.Settings.General.CurrentFrameRate = 24;
                    }

                    if (BatchSourceFrameRate.HasValue)
                    {
                        Configuration.Settings.General.CurrentFrameRate = BatchSourceFrameRate.Value;
                    }
                }

                node = xml.DocumentElement.SelectSingleNode("StartTime");
                if (node != null)
                {
                    ss.CurrentDCinemaStartTime = node.InnerText;
                }

                node = xml.DocumentElement.SelectSingleNode("Language");
                if (node != null)
                {
                    ss.CurrentDCinemaLanguage = node.InnerText;
                }

                node = xml.DocumentElement.SelectSingleNode("ContentTitleText");
                if (node != null)
                {
                    ss.CurrentDCinemaMovieTitle = node.InnerText;
                }

                node = xml.DocumentElement.SelectSingleNode("IssueDate");
                if (node != null)
                {
                    ss.CurrentDCinemaIssueDate = node.InnerText;
                }

                node = xml.DocumentElement.SelectSingleNode("LoadFont");
                if (node != null)
                {
                    ss.CurrentDCinemaFontUri = node.InnerText;
                }

                node = xml.DocumentElement.SelectSingleNode("SubtitleList/Font");
                if (node != null)
                {
                    if (node.Attributes["ID"] != null)
                    {
                        ss.CurrentDCinemaFontId = node.Attributes["ID"].InnerText;
                    }
                    if (node.Attributes["Size"] != null)
                    {
                        ss.CurrentDCinemaFontSize = Convert.ToInt32(node.Attributes["Size"].InnerText);
                    }
                    if (node.Attributes["Color"] != null)
                    {
                        ss.CurrentDCinemaFontColor = System.Drawing.ColorTranslator.FromHtml("#" + node.Attributes["Color"].InnerText);
                    }
                    if (node.Attributes["Effect"] != null)
                    {
                        ss.CurrentDCinemaFontEffect = node.Attributes["Effect"].InnerText;
                    }
                    if (node.Attributes["EffectColor"] != null)
                    {
                        ss.CurrentDCinemaFontEffectColor = System.Drawing.ColorTranslator.FromHtml("#" + node.Attributes["EffectColor"].InnerText);
                    }
                }
            }
            catch (Exception exception)
            {
                System.Diagnostics.Debug.WriteLine(exception.Message);
            }

            foreach (XmlNode node in xml.DocumentElement.SelectNodes("//Subtitle"))
            {
                try
                {
                    var    pText         = new StringBuilder();
                    string lastVPosition = string.Empty;
                    foreach (XmlNode innerNode in node.ChildNodes)
                    {
                        switch (innerNode.Name)
                        {
                        case "Text":
                            if (innerNode.Attributes["Vposition"] != null)
                            {
                                string vPosition = innerNode.Attributes["Vposition"].InnerText;
                                if (vPosition != lastVPosition)
                                {
                                    if (pText.Length > 0 && lastVPosition.Length > 0)
                                    {
                                        pText.AppendLine();
                                    }
                                    lastVPosition = vPosition;
                                }
                            }
                            bool alignLeft    = false;
                            bool alignRight   = false;
                            bool alignVTop    = false;
                            bool alignVCenter = false;
                            if (innerNode.Attributes["Halign"] != null)
                            {
                                string hAlign = innerNode.Attributes["Halign"].InnerText;
                                if (hAlign == "left")
                                {
                                    alignLeft = true;
                                }
                                else if (hAlign == "right")
                                {
                                    alignRight = true;
                                }
                            }
                            if (innerNode.Attributes["Valign"] != null)
                            {
                                string hAlign = innerNode.Attributes["Valign"].InnerText;
                                if (hAlign == "top")
                                {
                                    alignVTop = true;
                                }
                                else if (hAlign == "center")
                                {
                                    alignVCenter = true;
                                }
                            }
                            if (alignLeft || alignRight || alignVCenter || alignVTop)
                            {
                                if (!pText.ToString().StartsWith("{\\an", StringComparison.Ordinal))
                                {
                                    string pre = string.Empty;
                                    if (alignVTop)
                                    {
                                        if (alignLeft)
                                        {
                                            pre = "{\\an7}";
                                        }
                                        else if (alignRight)
                                        {
                                            pre = "{\\an9}";
                                        }
                                        else
                                        {
                                            pre = "{\\an8}";
                                        }
                                    }
                                    else if (alignVCenter)
                                    {
                                        if (alignLeft)
                                        {
                                            pre = "{\\an4}";
                                        }
                                        else if (alignRight)
                                        {
                                            pre = "{\\an6}";
                                        }
                                        else
                                        {
                                            pre = "{\\an5}";
                                        }
                                    }
                                    else
                                    {
                                        if (alignLeft)
                                        {
                                            pre = "{\\an1}";
                                        }
                                        else if (alignRight)
                                        {
                                            pre = "{\\an3}";
                                        }
                                    }
                                    string temp = pre + pText;
                                    pText.Clear();
                                    pText.Append(temp);
                                }
                            }

                            if (innerNode.ChildNodes.Count == 0)
                            {
                                pText.Append(innerNode.InnerText);
                            }
                            else
                            {
                                foreach (XmlNode innerInnerNode in innerNode)
                                {
                                    if (innerInnerNode.Name == "Font" && innerInnerNode.Attributes["Italic"] != null &&
                                        innerInnerNode.Attributes["Italic"].InnerText.Equals("yes", StringComparison.OrdinalIgnoreCase))
                                    {
                                        if (innerInnerNode.Attributes["Color"] != null)
                                        {
                                            pText.Append("<i><font color=\"" + DCSubtitle.GetColorStringFromDCinema(innerInnerNode.Attributes["Color"].Value) + "\">" + innerInnerNode.InnerText + "</font><i>");
                                        }
                                        else
                                        {
                                            pText.Append("<i>" + innerInnerNode.InnerText + "</i>");
                                        }
                                    }
                                    else if (innerInnerNode.Name == "Font" && innerInnerNode.Attributes["Color"] != null)
                                    {
                                        if (innerInnerNode.Attributes["Italic"] != null && innerInnerNode.Attributes["Italic"].InnerText.Equals("yes", StringComparison.OrdinalIgnoreCase))
                                        {
                                            pText.Append("<i><font color=\"" + DCSubtitle.GetColorStringFromDCinema(innerInnerNode.Attributes["Color"].Value) + "\">" + innerInnerNode.InnerText + "</font><i>");
                                        }
                                        else
                                        {
                                            pText.Append("<font color=\"" + DCSubtitle.GetColorStringFromDCinema(innerInnerNode.Attributes["Color"].Value) + "\">" + innerInnerNode.InnerText + "</font>");
                                        }
                                    }
                                    else
                                    {
                                        pText.Append(innerInnerNode.InnerText);
                                    }
                                }
                            }
                            break;

                        default:
                            pText.Append(innerNode.InnerText);
                            break;
                        }
                    }
                    string start = node.Attributes["TimeIn"].InnerText;
                    string end   = node.Attributes["TimeOut"].InnerText;

                    if (node.ParentNode.Name == "Font" && node.ParentNode.Attributes["Italic"] != null && node.ParentNode.Attributes["Italic"].InnerText.Equals("yes", StringComparison.OrdinalIgnoreCase) &&
                        !pText.ToString().Contains("<i>"))
                    {
                        string text = pText.ToString();
                        if (text.StartsWith("{\\an", StringComparison.Ordinal) && text.Length > 6)
                        {
                            text = text.Insert(6, "<i>") + "</i>";
                        }
                        else
                        {
                            text = "<i>" + text + "</i>";
                        }
                        pText = new StringBuilder(text);
                    }

                    subtitle.Paragraphs.Add(new Paragraph(GetTimeCode(start), GetTimeCode(end), pText.ToString()));
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex.Message);
                    _errorCount++;
                }
            }

            if (subtitle.Paragraphs.Count > 0)
            {
                subtitle.Header = xml.OuterXml; // save id/language/font for later use
            }
            subtitle.Renumber();
        }