Пример #1
0
        public static string GetSccText(string s)
        {
            string[] parts = s.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
            var      sb    = new StringBuilder();

            foreach (string part in ExecuteReplacesAndGetParts(parts))
            {
                try
                {
                    // TODO: How to decode???
                    int num = int.Parse(part, System.Globalization.NumberStyles.HexNumber);
                    if (num >= 32 && num <= 255)
                    {
                        var    encoding = Encoding.GetEncoding("ISO-8859-1");
                        byte[] bytes    = new byte[1];
                        bytes[0] = (byte)num;
                        sb.Append(encoding.GetString(bytes));
                    }
                }
                catch
                {
                    // ignored
                }
            }
            string res = sb.ToString().Replace("<i></i>", string.Empty).Replace("</i><i>", string.Empty);

            res = res.Replace("♪♪", "♪");
            res = res.Replace("'''", "'");
            res = res.Replace("  ", " ").Replace("  ", " ").Replace(Environment.NewLine + " ", Environment.NewLine).Trim();
            return(HtmlUtil.FixInvalidItalicTags(res));
        }
Пример #2
0
        public void FixInvalidItalicTags14()
        {
            var    s1 = "<i>Hallo!<i/>" + Environment.NewLine + "<i>Hallo!<i/>" + Environment.NewLine + "<i>Hallo!";
            string s2 = HtmlUtil.FixInvalidItalicTags(s1);

            Assert.AreEqual(s2, "<i>Hallo!" + Environment.NewLine + "Hallo!" + Environment.NewLine + "Hallo!</i>");
        }
Пример #3
0
        public void FixInvalidItalicTags3()
        {
            string s1 = "<i>Line 1.</i>" + Environment.NewLine + "<i>Line 2.";
            string s2 = HtmlUtil.FixInvalidItalicTags(s1);

            Assert.AreEqual(s2, "<i>Line 1." + Environment.NewLine + "Line 2.</i>");
        }
Пример #4
0
        public void FixInvalidItalicTags4()
        {
            string s1 = "It <i>is</i> a telegram," + Environment.NewLine + "it <i>is</i> ordering an advance,";
            string s2 = HtmlUtil.FixInvalidItalicTags(s1);

            Assert.AreEqual(s2, s1);
        }
        public void Fix(Subtitle subtitle, IFixCallbacks callbacks)
        {
            var          language            = Configuration.Settings.Language.FixCommonErrors;
            const string beginTagUpper       = "<I>";
            const string endTagUpper         = "</I>";
            const string beginTag            = "<i>";
            const string endTag              = "</i>";
            string       fixAction           = language.FixInvalidItalicTag;
            int          noOfInvalidHtmlTags = 0;

            for (int i = 0; i < subtitle.Paragraphs.Count; i++)
            {
                if (callbacks.AllowFix(subtitle.Paragraphs[i], fixAction))
                {
                    var text = subtitle.Paragraphs[i].Text;
                    if (text.Contains('<'))
                    {
                        text = text.Replace(beginTagUpper, beginTag).Replace(endTagUpper, endTag);
                        string oldText = text;

                        text = HtmlUtil.FixInvalidItalicTags(text);
                        if (text != oldText)
                        {
                            subtitle.Paragraphs[i].Text = text;
                            noOfInvalidHtmlTags++;
                            callbacks.AddFixToListView(subtitle.Paragraphs[i], fixAction, oldText, text);
                        }
                    }
                }
            }

            callbacks.UpdateFixStatus(noOfInvalidHtmlTags, language.FixInvalidItalicTags, string.Format(language.XInvalidHtmlTagsFixed, noOfInvalidHtmlTags));
        }
Пример #6
0
        public void FixInvalidItalicTags5()
        {
            string s1 = "- <i>It is a telegram?</i>" + Environment.NewLine + "<i>- It is.</i>";
            string s2 = HtmlUtil.FixInvalidItalicTags(s1);

            Assert.AreEqual(s2, "<i>- It is a telegram?" + Environment.NewLine + "- It is.</i>");
        }
Пример #7
0
        public void FixInvalidItalicTagsBadStartTag()
        {
            var    s1 = "<i<Hallo!<i/>";
            string s2 = HtmlUtil.FixInvalidItalicTags(s1);

            Assert.AreEqual("<i>Hallo!</i>", s2);
        }
Пример #8
0
 public void Check(Subtitle subtitle, NetflixQualityController controller)
 {
     foreach (Paragraph p in subtitle.Paragraphs)
     {
         if (p.Text.Contains("i>", StringComparison.OrdinalIgnoreCase))
         {
             if (controller.AllowItalics)
             {
                 var fixedParagraph = new Paragraph(p, false);
                 fixedParagraph.Text = HtmlUtil.FixInvalidItalicTags(fixedParagraph.Text);
                 if (fixedParagraph.Text != p.Text)
                 {
                     string comment = "Fixed italics";
                     controller.AddRecord(p, fixedParagraph, comment);
                 }
             }
             else
             {
                 var fixedParagraph = new Paragraph(p, false);
                 fixedParagraph.Text = HtmlUtil.RemoveHtmlTags(fixedParagraph.Text);
                 if (fixedParagraph.Text != p.Text)
                 {
                     string comment = "Italics not allowed";
                     controller.AddRecord(p, fixedParagraph, comment);
                 }
             }
         }
     }
 }
Пример #9
0
        public void FixInvalidItalicTags6()
        {
            string s1 = "- <i>Text1!</i>" + Environment.NewLine + "- <i>Text2.</i>";
            string s2 = HtmlUtil.FixInvalidItalicTags(s1);

            Assert.AreEqual(s2, "<i>- Text1!" + Environment.NewLine + "- Text2.</i>");
        }
Пример #10
0
        public void FixInvalidItalicTags8()
        {
            string s1 = "<i>- You think they're they gone?</i>" + Environment.NewLine + "<i>- That can't be.<i>";
            string s2 = HtmlUtil.FixInvalidItalicTags(s1);

            Assert.AreEqual(s2, "<i>- You think they're they gone?" + Environment.NewLine + "- That can't be.</i>");
        }
Пример #11
0
        public void FixInvalidItalicTags9()
        {
            const string s1 = "FALCONE:<i> I didn't think</i>\r\n<i>it was going to be you,</i>";
            string       s2 = HtmlUtil.FixInvalidItalicTags(s1);

            Assert.AreEqual(s2, "FALCONE: <i>I didn't think\r\nit was going to be you,</i>");
        }
Пример #12
0
        public void FixInvalidItalicTags12()
        {
            const string s1 = "< I >Hallo!<I/>";
            string       s2 = HtmlUtil.FixInvalidItalicTags(s1);

            Assert.AreEqual(s2, "<i>Hallo!</i>");
        }
Пример #13
0
        public void FixInvalidItalicTags2()
        {
            const string s1 = "Gledaj prema kameri i rici <i>zdravo!";
            string       s2 = HtmlUtil.FixInvalidItalicTags(s1);

            Assert.AreEqual(s2, "Gledaj prema kameri i rici zdravo!");
        }
Пример #14
0
        public void Fix(Subtitle subtitle, IFixCallbacks callbacks)
        {
            string fixAction           = Language.FixInvalidItalicTag;
            int    noOfInvalidHtmlTags = 0;

            for (int i = 0; i < subtitle.Paragraphs.Count; i++)
            {
                if (callbacks.AllowFix(subtitle.Paragraphs[i], fixAction))
                {
                    var text = subtitle.Paragraphs[i].Text;
                    if (text.Contains('<'))
                    {
                        string oldText = text;
                        text = HtmlUtil.FixInvalidItalicTags(text);
                        if (text != oldText)
                        {
                            subtitle.Paragraphs[i].Text = text;
                            noOfInvalidHtmlTags++;
                            callbacks.AddFixToListView(subtitle.Paragraphs[i], fixAction, oldText, text);
                        }
                    }
                }
            }

            callbacks.UpdateFixStatus(noOfInvalidHtmlTags, Language.FixInvalidItalicTags);
        }
Пример #15
0
        private static void MostUsedWordsAdd(Dictionary <string, string> hashtable, string lastLine)
        {
            if (lastLine.Contains("< "))
            {
                lastLine = HtmlUtil.FixInvalidItalicTags(lastLine);
            }
            lastLine = lastLine.Trim('\'');
            lastLine = lastLine.Replace("\"", "");
            lastLine = lastLine.Replace("<i>", "");
            lastLine = lastLine.Replace("</i>", ".");
            lastLine = lastLine.Replace("<I>", "");
            lastLine = lastLine.Replace("</I>", ".");
            lastLine = lastLine.Replace("<b>", "");
            lastLine = lastLine.Replace("</b>", ".");
            lastLine = lastLine.Replace("<B>", "");
            lastLine = lastLine.Replace("</B>", ".");
            lastLine = lastLine.Replace("<u>", "");
            lastLine = lastLine.Replace("</u>", ".");
            lastLine = lastLine.Replace("<U>", "");
            lastLine = lastLine.Replace("</U>", ".");

            var idx   = lastLine.IndexOf("<font", StringComparison.OrdinalIgnoreCase);
            var error = false;

            while (idx >= 0)
            {
                var endIdx = lastLine.IndexOf('>', idx + 5);
                if (endIdx < idx)
                {
                    error = !error;
                    break;
                }
                endIdx++;
                lastLine = lastLine.Remove(idx, (endIdx - idx));
                idx      = lastLine.IndexOf("<font", StringComparison.OrdinalIgnoreCase);
            }
            if (!error)
            {
                lastLine = lastLine.Replace("</font>", ".");
            }

            string[] words = lastLine.Split(new[] { ' ', ',', '!', '?', '.', ':', ';', '-', '_', '@', '<', '>', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }, StringSplitOptions.RemoveEmptyEntries);

            foreach (string word in words)
            {
                string s = word.Trim();

                if (s.Length > 1 && hashtable.ContainsKey(s))
                {
                    int hits = int.Parse(hashtable[s]);
                    hits++;
                    hashtable[s] = hits.ToString(CultureInfo.InvariantCulture);
                }
                else if (s.Length > 1)
                {
                    hashtable.Add(s, "1");
                }
            }
        }
Пример #16
0
        private void MostUsedWordsAdd(Dictionary <string, int> hashtable, string input)
        {
            var text = input;

            if (text.Contains("< "))
            {
                text = HtmlUtil.FixInvalidItalicTags(text);
            }

            text = StripHtmlTags(text);

            var idx   = text.IndexOf("<font", StringComparison.OrdinalIgnoreCase);
            var error = false;

            while (idx >= 0)
            {
                var endIdx = text.IndexOf('>', idx + 5);
                if (endIdx < idx)
                {
                    error = true;
                    break;
                }
                endIdx++;
                text = text.Remove(idx, endIdx - idx);
                idx  = text.IndexOf("<font", idx, StringComparison.OrdinalIgnoreCase);
            }
            if (!error)
            {
                text = text.Replace("</font>", ".");
            }

            foreach (var word in Utilities.RemoveSsaTags(text).Split(ExpectedChars, StringSplitOptions.RemoveEmptyEntries))
            {
                var s = word.Trim();
                if (s.Length > 1 && hashtable.ContainsKey(s))
                {
                    hashtable[s]++;
                }
                else if (s.Length > 1)
                {
                    hashtable.Add(s, 1);
                }
            }
        }
Пример #17
0
 private static void FixItalics(Subtitle subtitle)
 {
     foreach (var p in subtitle.Paragraphs)
     {
         if (p.Text.Contains('<') && p.Text.Contains('>'))
         {
             var sb = new StringBuilder();
             foreach (var line in p.Text.SplitToLines())
             {
                 if (line.StartsWith('<') && line.EndsWith('>'))
                 {
                     sb.AppendLine("<i>" + line.TrimStart('<').TrimEnd('>') + "</i>");
                 }
                 else
                 {
                     sb.AppendLine(line);
                 }
             }
             p.Text = HtmlUtil.FixInvalidItalicTags(sb.ToString().TrimEnd());
         }
     }
 }
Пример #18
0
        public void Fix(Subtitle subtitle, IFixCallbacks callbacks)
        {
            const string beginTagUpper       = "<I>";
            const string endTagUpper         = "</I>";
            const string beginTag            = "<i>";
            const string endTag              = "</i>";
            string       fixAction           = Language.FixInvalidItalicTag;
            int          noOfInvalidHtmlTags = 0;

            for (int i = 0; i < subtitle.Paragraphs.Count; i++)
            {
                if (callbacks.AllowFix(subtitle.Paragraphs[i], fixAction))
                {
                    var text = subtitle.Paragraphs[i].Text;
                    if (text.Contains('<'))
                    {
                        if (!text.Contains("i>") && !text.Contains("<i"))
                        {
                            text = text.Replace(beginTagUpper, beginTag).Replace(endTagUpper, endTag);
                        }

                        string oldText = text;

                        text = HtmlUtil.FixInvalidItalicTags(text);
                        if (text != oldText)
                        {
                            subtitle.Paragraphs[i].Text = text;
                            noOfInvalidHtmlTags++;
                            callbacks.AddFixToListView(subtitle.Paragraphs[i], fixAction, oldText, text);
                        }
                    }
                }
            }

            callbacks.UpdateFixStatus(noOfInvalidHtmlTags, Language.FixInvalidItalicTags);
        }
Пример #19
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().Trim());
            if (xml.DocumentElement == null)
            {
                return;
            }

            var ss = Configuration.Settings.SubtitleSettings;

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

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

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

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

                node = xml.DocumentElement.SelectSingleNode("LoadFont");
                if (node?.Attributes?["URI"] != null)
                {
                    ss.CurrentDCinemaFontUri = node.Attributes["URI"].InnerText;
                }

                node = xml.DocumentElement.SelectSingleNode("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 = 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 textLines     = new List <SubtitleLine>();
                    var pText         = new StringBuilder();
                    var vAlignment    = string.Empty;
                    var vPosition     = string.Empty;
                    var lastVPosition = string.Empty;
                    foreach (XmlNode innerNode in node.ChildNodes)
                    {
                        if (innerNode.Name == "Text")
                        {
                            if (innerNode.Attributes["VPosition"] != null)
                            {
                                vPosition = innerNode.Attributes["VPosition"].InnerText;
                                var vAlignmentNode = innerNode.Attributes["VAlign"];
                                if (vAlignmentNode != null)
                                {
                                    vAlignment = vAlignmentNode.InnerText;
                                }

                                if (vPosition != lastVPosition)
                                {
                                    if (pText.Length > 0 && lastVPosition.Length > 0)
                                    {
                                        textLines.Add(new SubtitleLine(pText.ToString(), lastVPosition, vAlignment));
                                        pText.Clear();
                                    }

                                    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=\"" + 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=\"" + GetColorStringFromDCinema(innerInnerNode.Attributes["Color"].Value) + "\">" + innerInnerNode.InnerText + "</font><i>");
                                        }
                                        else
                                        {
                                            pText.Append("<font color=\"" + GetColorStringFromDCinema(innerInnerNode.Attributes["Color"].Value) + "\">" + innerInnerNode.InnerText + "</font>");
                                        }
                                    }
                                    else
                                    {
                                        pText.Append(innerInnerNode.InnerText);
                                    }
                                }
                            }
                        }
                        else if (innerNode.Name == "Font")
                        {
                            var italic = innerNode.Name == "Font" &&
                                         innerNode.Attributes["Italic"] != null &&
                                         innerNode.Attributes["Italic"].InnerText.Equals("yes", StringComparison.OrdinalIgnoreCase);

                            var pre = string.Empty;
                            if (italic)
                            {
                                pre = "<i>";
                            }

                            foreach (XmlNode innerInnerNode in innerNode)
                            {
                                if (innerInnerNode.Attributes["VPosition"] != null)
                                {
                                    vPosition = innerInnerNode.Attributes["VPosition"].InnerText;
                                    if (vPosition != lastVPosition)
                                    {
                                        if (pText.Length > 0 && lastVPosition.Length > 0)
                                        {
                                            pText.AppendLine();
                                            pText.Append(pre);
                                            pre = string.Empty;
                                        }

                                        lastVPosition = vPosition;
                                    }
                                }

                                pText.Append(pre);
                                pre = string.Empty;
                                pText.Append(innerInnerNode.InnerText);
                            }

                            if (italic)
                            {
                                pText.Append("</i>");
                            }
                        }
                        else
                        {
                            pText.Append(innerNode.InnerText);
                        }
                    }

                    if (pText.Length > 0)
                    {
                        textLines.Add(new SubtitleLine(pText.ToString(), lastVPosition, vAlignment));
                    }

                    string text;
                    if (textLines.All(p => string.Equals(p.VerticalAlignment, "bottom", StringComparison.InvariantCultureIgnoreCase)))
                    {
                        text = string.Join(Environment.NewLine, textLines.OrderByDescending(p => p.GetVerticalPositionAsNumber()).Select(p => p.Text));
                    }
                    else
                    {
                        text = string.Join(Environment.NewLine, textLines.OrderBy(p => p.GetVerticalPositionAsNumber()).Select(p => p.Text));
                    }

                    text = text.Replace(Environment.NewLine + "{\\an1}", Environment.NewLine);
                    text = text.Replace(Environment.NewLine + "{\\an2}", Environment.NewLine);
                    text = text.Replace(Environment.NewLine + "{\\an3}", Environment.NewLine);
                    text = text.Replace(Environment.NewLine + "{\\an4}", Environment.NewLine);
                    text = text.Replace(Environment.NewLine + "{\\an5}", Environment.NewLine);
                    text = text.Replace(Environment.NewLine + "{\\an6}", Environment.NewLine);
                    text = text.Replace(Environment.NewLine + "{\\an7}", Environment.NewLine);
                    text = text.Replace(Environment.NewLine + "{\\an8}", Environment.NewLine);
                    text = text.Replace(Environment.NewLine + "{\\an9}", Environment.NewLine);

                    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) &&
                        !text.Contains("<i>"))
                    {
                        if (text.StartsWith("{\\an", StringComparison.Ordinal) && text.Length > 6)
                        {
                            text = text.Insert(6, "<i>") + "</i>";
                        }
                        else
                        {
                            text = "<i>" + text + "</i>";
                        }
                    }

                    subtitle.Paragraphs.Add(new Paragraph(GetTimeCode(start), GetTimeCode(end), HtmlUtil.FixInvalidItalicTags(text)));
                }
                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();
        }
Пример #20
0
        public override void LoadSubtitle(Subtitle subtitle, List <string> lines, string fileName)
        {
            _errorCount = 0;
            var sb = new StringBuilder();

            lines.ForEach(line => sb.AppendLine(line));
            string xmlString = sb.ToString();

            if (!xmlString.Contains("<subtitlelist"))
            {
                return;
            }

            var xml = new XmlDocument {
                XmlResolver = null
            };

            try
            {
                xml.LoadXml(xmlString);
            }
            catch
            {
                _errorCount = 1;
                return;
            }

            subtitle.Header = xmlString;
            char[] timeCodeSeparators = { ':' };
            var    ns = new XmlNamespaceManager(xml.NameTable);

            ns.AddNamespace("esub-xf", NameSpaceUri);
            var isTimebaseSmtp = xml.DocumentElement.Attributes["timebase"]?.Value != "msec"; // "msec" or "smtp"

            foreach (XmlNode node in xml.DocumentElement.SelectNodes("//esub-xf:subtitlelist/esub-xf:subtitle", ns))
            {
                try
                {
                    string start = node.Attributes["display"].InnerText;
                    string end   = node.Attributes["clear"].InnerText;
                    sb = new StringBuilder();
                    var hregion  = node.SelectSingleNode("esub-xf:hregion", ns);
                    var topAlign = hregion.Attributes["vposition"]?.Value == "top";
                    foreach (XmlNode lineNode in node.SelectNodes("esub-xf:hregion/esub-xf:line", ns))
                    {
                        var spanNodes = lineNode.SelectNodes("esub-xf:span", ns);
                        if (spanNodes.Count > 0)
                        {
                            var first = true;
                            foreach (XmlNode spanChild in spanNodes)
                            {
                                if (!first)
                                {
                                    sb.Append(" "); // put space between all spans
                                }
                                sb.Append(GetTextWithStyle(spanChild.InnerText, spanChild.Attributes["italic"]?.Value == "on", spanChild.Attributes["bold"]?.Value == "on", spanChild.Attributes["underline"]?.Value == "on", GetColor(spanChild)));
                                first = false;
                            }
                            sb.AppendLine();
                        }
                        else
                        {
                            sb.AppendLine(lineNode.InnerText);
                        }
                    }
                    var text = sb.ToString().Trim();
                    text = HtmlUtil.FixInvalidItalicTags(text);
                    if (topAlign)
                    {
                        text = "{\\an8}" + text;
                    }
                    subtitle.Paragraphs.Add(new Paragraph(DecodeTimeCodeFrames(start, timeCodeSeparators, isTimebaseSmtp), DecodeTimeCodeFrames(end, timeCodeSeparators, isTimebaseSmtp), text));
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex.Message);
                    _errorCount++;
                }
            }
            subtitle.Renumber();
        }
Пример #21
0
        public static Subtitle SplitLongLinesInSubtitle(Subtitle subtitle, int totalLineMaxCharacters, int singleLineMaxCharacters)
        {
            var splittedSubtitle = new Subtitle(subtitle);
            var language         = LanguageAutoDetect.AutoDetectGoogleLanguage(subtitle);

            // calculate gaps
            var halfMinGaps     = Configuration.Settings.General.MinimumMillisecondsBetweenLines / 2.0;
            var halfMinGapsMood = halfMinGaps + Configuration.Settings.General.MinimumMillisecondsBetweenLines % 2;

            const int FirstLine  = 0;
            const int SecondLine = 1;

            for (int i = splittedSubtitle.Paragraphs.Count - 1; i >= 0; i--)
            {
                var oldParagraph = splittedSubtitle.Paragraphs[i];

                // don't split into two paragraph if it can be balanced
                var text        = oldParagraph.Text;
                var noHtmlLines = HtmlUtil.RemoveHtmlTags(text, true).SplitToLines();

                bool autoBreak = true;
                if (noHtmlLines.Count == 2 && noHtmlLines[0].Length <= totalLineMaxCharacters && noHtmlLines[1].Length < totalLineMaxCharacters &&
                    (noHtmlLines[0].EndsWith('.') || noHtmlLines[0].EndsWith('!') || noHtmlLines[0].EndsWith('?') || noHtmlLines[0].EndsWith('؟')))
                {
                    autoBreak = false;
                }
                if (autoBreak)
                {
                    text = Utilities.AutoBreakLine(oldParagraph.Text, language, true);
                }

                if (noHtmlLines.Count == 1 && text.SplitToLines().Count <= 2 && noHtmlLines[0].Length > singleLineMaxCharacters && !QualifiesForSplit(text, singleLineMaxCharacters, totalLineMaxCharacters))
                {
                    oldParagraph.Text = text;
                    continue;
                }

                if (!QualifiesForSplit(text, singleLineMaxCharacters, totalLineMaxCharacters))
                {
                    continue;
                }

                // continue if paragraph doesn't contain exactly two lines
                var lines = text.SplitToLines();
                if (lines.Count > 2)
                {
                    continue; // ignore 3+ lines
                }

                // calculate milliseconds per char
                var millisecondsPerChar = oldParagraph.Duration.TotalMilliseconds / (HtmlUtil.RemoveHtmlTags(text, true).Length - Environment.NewLine.Length);

                oldParagraph.Text = lines[FirstLine];

                // use optimal time to adjust duration
                oldParagraph.EndTime.TotalMilliseconds = oldParagraph.StartTime.TotalMilliseconds + millisecondsPerChar * HtmlUtil.RemoveHtmlTags(oldParagraph.Text, true).Length - halfMinGaps;

                // build second paragraph
                var newParagraph = new Paragraph(oldParagraph)
                {
                    Text = lines[SecondLine]
                };
                newParagraph.StartTime.TotalMilliseconds = oldParagraph.EndTime.TotalMilliseconds + halfMinGapsMood;
                newParagraph.EndTime.TotalMilliseconds   = newParagraph.StartTime.TotalMilliseconds + millisecondsPerChar * HtmlUtil.RemoveHtmlTags(newParagraph.Text, true).Length;

                // only remove dash (if dialog) if first line is fully closed and "Split removes dashes" is true
                if (Configuration.Settings.General.SplitRemovesDashes && IsTextClosed(oldParagraph.Text))
                {
                    RemoveInvalidDash(oldParagraph, newParagraph);
                }

                // handle invalid tags
                if (oldParagraph.Text.Contains('<'))
                {
                    oldParagraph.Text = HtmlUtil.FixInvalidItalicTags(oldParagraph.Text);
                }
                if (newParagraph.Text.Contains('<'))
                {
                    newParagraph.Text = HtmlUtil.FixInvalidItalicTags(newParagraph.Text);
                }

                oldParagraph.Text = Utilities.AutoBreakLine(oldParagraph.Text, language);
                newParagraph.Text = Utilities.AutoBreakLine(newParagraph.Text, language);

                // insert new paragraph after the current/old one
                splittedSubtitle.Paragraphs.Insert(i + 1, newParagraph);
            }

            splittedSubtitle.Renumber();
            return(splittedSubtitle);
        }
Пример #22
0
        private static string DecodeText(StringBuilder sb)
        {
            var s = sb.ToString().Trim();

            s = s.Replace(Environment.NewLine + Environment.NewLine, Environment.NewLine).Replace(Environment.NewLine + Environment.NewLine, Environment.NewLine);
            if (s.StartsWith("/STYLE RIGHT" + Environment.NewLine, StringComparison.Ordinal))
            {
                s = "{\\an3}" + s.Remove(0, 12).Trim();
            }

            if (s.StartsWith("/STYLE LEFT" + Environment.NewLine, StringComparison.Ordinal))
            {
                s = "{\\an1}" + s.Remove(0, 11).Trim();
            }

            if (s.StartsWith("/STYLE TOP" + Environment.NewLine, StringComparison.Ordinal))
            {
                s = "{\\an8}" + s.Remove(0, 10).Trim();
            }

            if (s.StartsWith("/STYLE VERTICAL(-25)" + Environment.NewLine, StringComparison.Ordinal))
            {
                s = "{\\an8}" + s.Remove(0, 20).Trim();
            }

            if (s.StartsWith("/STYLE VERTICAL(-24)" + Environment.NewLine, StringComparison.Ordinal))
            {
                s = "{\\an8}" + s.Remove(0, 20).Trim();
            }

            if (s.StartsWith("/STYLE VERTICAL(-23)" + Environment.NewLine, StringComparison.Ordinal))
            {
                s = "{\\an8}" + s.Remove(0, 20).Trim();
            }

            if (s.StartsWith("/STYLE VERTICAL(-22)" + Environment.NewLine, StringComparison.Ordinal))
            {
                s = "{\\an8}" + s.Remove(0, 20).Trim();
            }

            if (s.StartsWith("/STYLE VERTICAL(-21)" + Environment.NewLine, StringComparison.Ordinal))
            {
                s = "{\\an8}" + s.Remove(0, 20).Trim();
            }

            if (s.StartsWith("/STYLE VERTICAL(-20)" + Environment.NewLine, StringComparison.Ordinal))
            {
                s = "{\\an8}" + s.Remove(0, 20).Trim();
            }

            if (s.StartsWith("/STYLE VERTICAL(-19)" + Environment.NewLine, StringComparison.Ordinal))
            {
                s = "{\\an8}" + s.Remove(0, 20).Trim();
            }

            if (s.StartsWith("/STYLE VERTICAL(-18)" + Environment.NewLine, StringComparison.Ordinal))
            {
                s = "{\\an5}" + s.Remove(0, 20).Trim();
            }

            if (s.StartsWith("/STYLE VERTICAL(-17)" + Environment.NewLine, StringComparison.Ordinal))
            {
                s = "{\\an5}" + s.Remove(0, 20).Trim();
            }

            if (s.StartsWith("/STYLE VERTICAL(-16)" + Environment.NewLine, StringComparison.Ordinal))
            {
                s = "{\\an5}" + s.Remove(0, 20).Trim();
            }

            if (s.StartsWith("/STYLE VERTICAL(-15)" + Environment.NewLine, StringComparison.Ordinal))
            {
                s = "{\\an5}" + s.Remove(0, 20).Trim();
            }

            if (s.StartsWith("/STYLE VERTICAL(-14)" + Environment.NewLine, StringComparison.Ordinal))
            {
                s = "{\\an5}" + s.Remove(0, 20).Trim();
            }

            if (s.StartsWith("/STYLE VERTICAL(-13)" + Environment.NewLine, StringComparison.Ordinal))
            {
                s = "{\\an5}" + s.Remove(0, 20).Trim();
            }

            if (s.StartsWith("/STYLE VERTICAL(-12)" + Environment.NewLine, StringComparison.Ordinal))
            {
                s = "{\\an5}" + s.Remove(0, 20).Trim();
            }

            if (s.StartsWith("/STYLE VERTICAL(-11)" + Environment.NewLine, StringComparison.Ordinal))
            {
                s = "{\\an5}" + s.Remove(0, 20).Trim();
            }

            if (s.StartsWith("/STYLE VERTICAL(-10)" + Environment.NewLine, StringComparison.Ordinal))
            {
                s = "{\\an5}" + s.Remove(0, 20).Trim();
            }

            s = HtmlUtil.FixInvalidItalicTags(s);
            return(s);
        }
Пример #23
0
        public void FixInvalidItalicTags2()
        {
            string s = "<i>foobar?" + Environment.NewLine + "<i>foobar?";

            Assert.AreEqual("<i>foobar?</i>" + Environment.NewLine + "<i>foobar?</i>", HtmlUtil.FixInvalidItalicTags(s));
        }
Пример #24
0
        public override void LoadSubtitle(Subtitle subtitle, List <string> lines, string fileName)
        {
            //00:01:48:22,       00:01:52:17,       - I need those samples, fast!//- Yes, professor.
            Paragraph p = null;

            subtitle.Paragraphs.Clear();
            _errorCount = 0;
            foreach (string line in lines)
            {
                string s = line.Replace("       ", "\t");
                if (RegexTimeCodes.IsMatch(s))
                {
                    var temp = s.Split('\t');
                    if (temp.Length > 1)
                    {
                        string start = temp[0].TrimEnd(',');
                        string end   = temp[1].TrimEnd(',');

                        string[] startParts = start.Split(SplitCharColon, StringSplitOptions.RemoveEmptyEntries);
                        string[] endParts   = end.Split(SplitCharColon, StringSplitOptions.RemoveEmptyEntries);
                        if (startParts.Length == 4 && endParts.Length == 4)
                        {
                            try
                            {
                                string text = s.Remove(0, RegexTimeCodes.Match(s).Length - 1).Trim();
                                if (!text.Contains(Environment.NewLine))
                                {
                                    text = text.Replace("//", Environment.NewLine);
                                }
                                if (text.Contains("@Italic@"))
                                {
                                    bool italicOn = false;
                                    while (text.Contains("@Italic@"))
                                    {
                                        var    index     = text.IndexOf("@Italic@", StringComparison.Ordinal);
                                        string italicTag = "<i>";
                                        if (italicOn)
                                        {
                                            italicTag = "</i>";
                                        }
                                        text     = text.Remove(index, "@Italic@".Length).Insert(index, italicTag);
                                        italicOn = !italicOn;
                                    }
                                    text = HtmlUtil.FixInvalidItalicTags(text);
                                }
                                p = new Paragraph(DecodeTimeCodeFramesFourParts(startParts), DecodeTimeCodeFramesFourParts(endParts), text);
                                subtitle.Paragraphs.Add(p);
                            }
                            catch (Exception exception)
                            {
                                _errorCount++;
                                System.Diagnostics.Debug.WriteLine(exception.Message);
                            }
                        }
                    }
                }
                else if (string.IsNullOrWhiteSpace(line))
                {
                    // skip empty lines
                }
                else if (p != null)
                {
                    _errorCount++;
                }
            }

            subtitle.Renumber();
        }
Пример #25
0
        public void FixInvalidItalicTags1()
        {
            const string s = "<i>foobar<i>?";

            Assert.AreEqual("<i>foobar</i>?", HtmlUtil.FixInvalidItalicTags(s));
        }