public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
 {
     _errorCount = 0;
     subtitle.Paragraphs.Clear();
     foreach (string line in lines)
     {
         if (regexTimeCodes.IsMatch(line))
         {
             int splitter = line.IndexOf(':') + 3;
             string text = line.Remove(0, splitter);
             var p = new Paragraph(DecodeTimeCode(line.Substring(0, splitter)), new TimeCode(0, 0, 0, 0), text);
             subtitle.Paragraphs.Add(p);
             text = text.Trim().Trim('–', '.', ';', ':').Trim();
             if (text.Length > 0 && char.IsDigit(text[0]))
                 _errorCount++;
         }
         else
         {
             _errorCount += 2;
         }
     }
     foreach (Paragraph p2 in subtitle.Paragraphs)
     {
         p2.Text = Utilities.AutoBreakLine(p2.Text);
     }
     subtitle.RecalculateDisplayTimes(Configuration.Settings.General.SubtitleMaximumDisplayMilliseconds, null);
     subtitle.Renumber();
 }
 private void AddToListView(Paragraph p, int index)
 {
     var item = new ListViewItem(string.Empty) { Tag = index, Checked = true };
     item.SubItems.Add(p.Number.ToString());
     item.SubItems.Add(p.Text.Replace(Environment.NewLine, Configuration.Settings.General.ListViewLineSeparatorString));
     listViewFixes.Items.Add(item);
 }
        public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
        {
            //00:01:54:19,00:01:56:17,We should be thankful|they accepted our offer.
            _errorCount = 0;
            subtitle.Paragraphs.Clear();
            var regexTimeCodes = new Regex(@"^\d\d:\d\d:\d\d:\d\d,\d\d:\d\d:\d\d:\d\d,.+", RegexOptions.Compiled);
            if (fileName != null && fileName.EndsWith(".stl", StringComparison.OrdinalIgnoreCase)) // allow empty text if extension is ".stl"...
                regexTimeCodes = new Regex(@"^\d\d:\d\d:\d\d:\d\d,\d\d:\d\d:\d\d:\d\d,", RegexOptions.Compiled);
            foreach (string line in lines)
            {
                if (line.IndexOf(':') == 2 && regexTimeCodes.IsMatch(line))
                {
                    string start = line.Substring(0, 11);
                    string end = line.Substring(12, 11);

                    try
                    {
                        Paragraph p = new Paragraph(DecodeTimeCode(start), DecodeTimeCode(end), DecodeText(line.Substring(24)));
                        subtitle.Paragraphs.Add(p);
                    }
                    catch
                    {
                        _errorCount++;
                    }
                }
                else if (!string.IsNullOrWhiteSpace(line) && !line.StartsWith("//") && !line.StartsWith('$'))
                {
                    _errorCount++;
                }
            }
            subtitle.Renumber();
        }
        public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
        {
            _errorCount = 0;
            Paragraph p = null;
            foreach (string line in lines)
            {
                string s = line.Trim();
                if (regexTimeCodes.Match(s).Success)
                {
                    if (p != null && !string.IsNullOrEmpty(p.Text))
                        subtitle.Paragraphs.Add(p);
                    p = new Paragraph();

                    try
                    {
                        string[] arr = s.Substring(0, 11).Split(':');
                        if (arr.Length == 4)
                        {
                            int hours = int.Parse(arr[0]);
                            int minutes = int.Parse(arr[1]);
                            int seconds = int.Parse(arr[2]);
                            int frames = int.Parse(arr[3]);
                            p.StartTime = new TimeCode(hours, minutes, seconds, FramesToMillisecondsMax999(frames));
                            string text = s.Remove(0, 11).Trim();
                            p.Text = text;
                            if (text.Length > 1 && Utilities.IsInteger(text.Substring(0, 2)))
                                _errorCount++;
                        }
                    }
                    catch
                    {
                        _errorCount++;
                    }
                }
                else if (s.Length > 0)
                {
                    _errorCount++;
                }
            }
            if (p != null && !string.IsNullOrEmpty(p.Text))
                subtitle.Paragraphs.Add(p);

            int index = 1;
            foreach (Paragraph paragraph in subtitle.Paragraphs)
            {
                paragraph.Text = paragraph.Text.Replace("\\M", "♪");

                Paragraph next = subtitle.GetParagraphOrDefault(index);
                if (next != null)
                {
                    paragraph.EndTime.TotalMilliseconds = next.StartTime.TotalMilliseconds - 1;
                }
                else
                {
                    paragraph.EndTime.TotalMilliseconds = paragraph.StartTime.TotalMilliseconds + Utilities.GetOptimalDisplayMilliseconds(paragraph.Text);
                }
                index++;
            }
            subtitle.RemoveEmptyLines();
        }
        public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
        {
            //00:03:15:22 00:03:23:10 This is line one.
            //This is line two.
            subtitle.Paragraphs.Clear();
            _errorCount = 0;
            foreach (string line in lines)
            {
                if (regexTimeCodes.IsMatch(line))
                {
                    string temp = line.Substring(0, regexTimeCodes.Match(line).Length);
                    string start = temp.Substring(0, 11);
                    string end = temp.Substring(12, 11);

                    string[] startParts = start.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                    string[] endParts = end.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                    if (startParts.Length == 4 && endParts.Length == 4)
                    {
                        string text = line.Remove(0, regexTimeCodes.Match(line).Length - 1).Trim();
                        text = text.Replace("//", Environment.NewLine);
                        var p = new Paragraph(DecodeTimeCode(startParts), DecodeTimeCode(endParts), text);
                        subtitle.Paragraphs.Add(p);
                    }
                }
                else
                {
                    _errorCount += 10;
                }
            }

            subtitle.Renumber();
        }
        public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
        {
            _errorCount = 0;

            var sb = new StringBuilder();
            foreach (string s in lines)
                sb.Append(s);

            string allText = sb.ToString();
            if (!allText.Contains("text_tees"))
                return;

            var times = Json.ReadArray(allText, "text_tees");
            var texts = Json.ReadArray(allText, "text_content");

            for (int i = 0; i < Math.Min(times.Count, texts.Count); i++)
            {
                try
                {

                    string text = texts[i];
                    if (text.StartsWith('['))
                    {
                        var textLines = Json.ReadArray("{\"text\":" + texts[i] + "}", "text");
                        var textSb = new StringBuilder();
                        foreach (string line in textLines)
                        {
                            string t = Json.DecodeJsonText(line);
                            if (t.StartsWith("[\"", StringComparison.Ordinal) && t.EndsWith("\"]", StringComparison.Ordinal))
                            {
                                var innerSb = new StringBuilder();
                                var innerTextLines = Json.ReadArray("{\"text\":" + t + "}", "text");
                                foreach (string innerLine in innerTextLines)
                                {
                                    innerSb.Append(' ');
                                    innerSb.Append(innerLine);
                                }
                                textSb.AppendLine(innerSb.ToString().Trim());
                            }
                            else
                            {
                                textSb.AppendLine(t);
                            }
                        }
                        text = textSb.ToString().Trim();
                        text = text.Replace(Environment.NewLine + Environment.NewLine, Environment.NewLine);
                    }
                    var p = new Paragraph(text, int.Parse(times[i]), 0);
                    if (i + 1 < times.Count)
                        p.EndTime.TotalMilliseconds = int.Parse(times[i + 1]);
                    subtitle.Paragraphs.Add(p);
                }
                catch
                {
                    _errorCount++;
                }
            }
            subtitle.RemoveEmptyLines();
            subtitle.Renumber();
        }
        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());

            XmlNode div = xml.DocumentElement.SelectSingleNode("subtitles");
            foreach (XmlNode node in div.ChildNodes)
            {
                try
                {
                    //<subtitle duration="2256" effect="" end="124581" layer="0" margin-l="0" margin-r="0" margin-v="0" name="" note="" path="0" start="122325" style="Default" text="The fever hath weakened thee." translation="" />
                    var p = new Paragraph { StartTime = { TotalMilliseconds = int.Parse(node.Attributes["start"].Value) } };
                    p.EndTime.TotalMilliseconds = p.StartTime.TotalMilliseconds + int.Parse(node.Attributes["duration"].Value);
                    p.Text = node.Attributes["text"].Value;

                    subtitle.Paragraphs.Add(p);
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex.Message);
                    _errorCount++;
                }
            }
            subtitle.Renumber();
        }
 private static void SetEndTimeAndStartTime(string name, Paragraph p)
 {
     if (name.Contains("-to-"))
     {
         var arr = name.Replace("-to-", "_").Split('_');
         if (arr.Length == 3)
         {
             int startTime, endTime;
             if (int.TryParse(arr[1], out startTime) && int.TryParse(arr[2], out endTime))
             {
                 p.StartTime.TotalMilliseconds = startTime;
                 p.EndTime.TotalMilliseconds = endTime;
             }
         }
     }
     else if (TimeCodeFormat1.IsMatch(name) || TimeCodeFormat2.IsMatch(name))
     {
         var arr = name.Replace("__", "_").Split('_');
         if (arr.Length >= 8)
         {
             try
             {
                 p.StartTime = new TimeCode(int.Parse(arr[0]), int.Parse(arr[1]), int.Parse(arr[2]), int.Parse(arr[3]));
                 p.EndTime = new TimeCode(int.Parse(arr[4]), int.Parse(arr[5]), int.Parse(arr[6]), int.Parse(arr[7]));
             }
             catch (Exception)
             {
             }
         }
     }
 }
 private void GeneratePreview()
 {
     var subtitle = new Subtitle();
     var p1 = new Paragraph("Line 1a." + Environment.NewLine + "Line 1b.", 1000, 3500);
     string start1 = GetTimeCode(p1.StartTime, comboBoxTimeCode.Text);
     string end1 = GetTimeCode(p1.EndTime, comboBoxTimeCode.Text);
     var p2 = new Paragraph("Line 2a." + Environment.NewLine + "Line 2b.", 1000, 3500);
     string start2 = GetTimeCode(p2.StartTime, comboBoxTimeCode.Text);
     string end2 = GetTimeCode(p2.EndTime, comboBoxTimeCode.Text);
     subtitle.Paragraphs.Add(p1);
     subtitle.Paragraphs.Add(p2);
     try
     {
         string newLine = comboBoxNewLine.Text.Replace(Configuration.Settings.Language.ExportCustomTextFormat.DoNotModify, EnglishDoNoModify);
         string template = GetParagraphTemplate(textBoxParagraph.Text);
         textBoxPreview.Text = GetHeaderOrFooter("Demo", subtitle, textBoxHeader.Text) +
                               GetParagraph(template, start1, end1, GetText(p1.Text, newLine), GetText("Line 1a." + Environment.NewLine + "Line 1b.", newLine), 0, p1.Duration, comboBoxTimeCode.Text) +
                               GetParagraph(template, start2, end2, GetText(p2.Text, newLine), GetText("Line 2a." + Environment.NewLine + "Line 2b.", newLine), 1, p2.Duration, comboBoxTimeCode.Text) +
                               GetHeaderOrFooter("Demo", subtitle, textBoxFooter.Text);
     }
     catch (Exception ex)
     {
         textBoxPreview.Text = ex.Message;
     }
 }
 public ParagraphEventArgs(double seconds, Paragraph p, Paragraph b, MouseDownParagraphType mouseDownParagraphType)
 {
     Seconds = seconds;
     Paragraph = p;
     BeforeParagraph = b;
     MouseDownParagraphType = mouseDownParagraphType;
 }
        public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
        {
            //0001 : 01:07:25:08,01:07:29:00,10
            _errorCount = 0;
            Paragraph p = null;
            subtitle.Paragraphs.Clear();
            foreach (string line in lines)
            {
                if (line.IndexOf(':') == 5 && RegexTimeCodes.IsMatch(line))
                {
                    if (p != null)
                        subtitle.Paragraphs.Add(p);

                    string start = line.Substring(7, 11);
                    string end = line.Substring(19, 11);

                    string[] startParts = start.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                    string[] endParts = end.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                    if (startParts.Length == 4 && endParts.Length == 4)
                    {
                        p = new Paragraph(DecodeTimeCode(startParts), DecodeTimeCode(endParts), string.Empty);
                    }
                }
                else if (p != null && RegexText.IsMatch(line))
                {
                    if (string.IsNullOrEmpty(p.Text))
                        p.Text = line.Substring(5).Trim();
                    else
                        p.Text += Environment.NewLine + line.Substring(5).Trim();
                }
                else if (line.Length < 10 && RegexSomeCodes.IsMatch(line))
                {
                }
                else if (string.IsNullOrWhiteSpace(line))
                {
                    // skip these lines
                }
                else if (p != null)
                {
                    if (p.Text != null && Utilities.GetNumberOfLines(p.Text) > 3)
                    {
                        _errorCount++;
                    }
                    else
                    {
                        if (!line.TrimEnd().EndsWith(": --:--:--:--,--:--:--:--,-1", StringComparison.Ordinal))
                        {
                            if (string.IsNullOrEmpty(p.Text))
                                p.Text = line.Trim();
                            else
                                p.Text += Environment.NewLine + line.Trim();
                        }
                    }
                }
            }
            if (p != null && !string.IsNullOrEmpty(p.Text))
                subtitle.Paragraphs.Add(p);

            subtitle.Renumber();
        }
        public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
        {
            _errorCount = 0;
            int number = 0;
            foreach (string line in lines)
            {
                if (string.IsNullOrWhiteSpace(line) || string.IsNullOrWhiteSpace(line.Trim('-')))
                {
                    continue;
                }

                if (RegexTimeCodes.Match(line).Success)
                {
                    string[] threePart = line.Split(new[] { ',' }, StringSplitOptions.None);
                    var p = new Paragraph();
                    if (threePart.Length > 2 &&
                        line.Length > 58 &&
                        GetTimeCode(p.StartTime, threePart[0].Trim()) &&
                        GetTimeCode(p.EndTime, threePart[1].Trim()))
                    {
                        number++;
                        p.Number = number;
                        p.Text = line.Remove(0, 57).Trim().Replace(" | ", Environment.NewLine).Replace("|", Environment.NewLine);
                        subtitle.Paragraphs.Add(p);
                    }
                }
                else
                {
                    _errorCount++;
                }
            }
        }
        public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
        {
            Paragraph paragraph = new Paragraph();
            this._errorCount = 0;
            subtitle.Paragraphs.Clear();
            for (int i = 0; i < lines.Count; i++)
            {
                string line = lines[i].TrimEnd();
                string next = string.Empty;
                if (i + 1 < lines.Count)
                {
                    next = lines[i + 1];
                }

                if (line.Contains(':') && !next.Contains(':') && RegexTimeCodes.IsMatch(line) && !RegexTimeCodes.IsMatch(next))
                {
                    paragraph = new Paragraph();
                    if (TryReadTimeCodesLine(line, paragraph))
                    {
                        paragraph.Text = next;
                        if (!string.IsNullOrWhiteSpace(paragraph.Text))
                        {
                            subtitle.Paragraphs.Add(paragraph);
                        }
                    }
                    else if (!string.IsNullOrWhiteSpace(line))
                    {
                        this._errorCount++;
                    }
                }
                else
                {
                    this._errorCount++;
                }
            }

            foreach (Paragraph p in subtitle.Paragraphs)
            {
                p.Text = p.Text.Replace(Environment.NewLine + Environment.NewLine, Environment.NewLine);
            }

            int index = 0;
            foreach (Paragraph p in subtitle.Paragraphs)
            {
                index++;
                Paragraph nextParagraph = subtitle.GetParagraphOrDefault(index);
                if (nextParagraph != null)
                {
                    p.EndTime.TotalMilliseconds = nextParagraph.StartTime.TotalMilliseconds - 1;
                }
                else
                {
                    p.EndTime.TotalMilliseconds = p.StartTime.TotalMilliseconds + 2500;
                }

                p.Text = p.Text.Replace(Environment.NewLine + Environment.NewLine, Environment.NewLine);
            }

            subtitle.Renumber();
        }
        public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
        {
            //00:01:54:19,00:01:56:17,We should be thankful|they accepted our offer.
            _errorCount = 0;
            subtitle.Paragraphs.Clear();
            foreach (string line in lines)
            {
                if (line.IndexOf(':') == 2 && regexTimeCodes.IsMatch(line))
                {
                    string start = line.Substring(0, 11);
                    string end = line.Substring(13, 11);

                    try
                    {
                        Paragraph p = new Paragraph(DecodeTimeCode(start), DecodeTimeCode(end), DecodeText(line.Substring(25).Trim()));
                        subtitle.Paragraphs.Add(p);
                    }
                    catch
                    {
                        _errorCount++;
                    }
                }
                else if (!string.IsNullOrWhiteSpace(line) && !line.StartsWith("//") && !line.StartsWith('$'))
                {
                    _errorCount++;
                }
            }
            subtitle.Renumber();
        }
Exemple #15
0
 public static void WriteSubtitleBlock(FileStream fs, Paragraph p, int number)
 {
     fs.WriteByte(0);
     fs.WriteByte((byte)(number % 256)); // number - low byte
     fs.WriteByte((byte)(number / 256)); // number - high byte
     fs.WriteByte(0xff);
     fs.WriteByte(0);
     WriteTimeCode(fs, p.StartTime);
     WriteTimeCode(fs, p.EndTime);
     fs.WriteByte(1);
     fs.WriteByte(2);
     fs.WriteByte(0);
     var buffer = Encoding.GetEncoding(1252).GetBytes(p.Text.Replace(Environment.NewLine, "Š"));
     if (buffer.Length <= 128)
     {
         fs.Write(buffer, 0, buffer.Length);
         for (int i = buffer.Length; i < TextLength; i++)
         {
             fs.WriteByte(0x8f);
         }
     }
     else
     {
         for (int i = 0; i < TextLength; i++)
         {
             fs.WriteByte(buffer[i]);
         }
     }
 }
        public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
        {
            _errorCount = 0;
            Paragraph p = null;
            subtitle.Paragraphs.Clear();
            foreach (string line in lines)
            {
                string s = line.Trim();
                var match = regexTimeCodes2.Match(s);
                if (match.Success)
                {
                    s = s.Substring(0, match.Index + 13).Trim();
                }
                match = regexTimeCodes1.Match(s);
                if (match.Success && match.Index > 13)
                {
                    string text = s.Substring(0, match.Index).Trim();
                    string timeCode = s.Substring(match.Index).Trim();

                    string[] startParts = timeCode.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                    if (startParts.Length == 4)
                    {
                        try
                        {
                            p = new Paragraph(DecodeTimeCode(startParts), new TimeCode(0, 0, 0, 0), text);
                            subtitle.Paragraphs.Add(p);
                        }
                        catch (Exception exception)
                        {
                            _errorCount++;
                            System.Diagnostics.Debug.WriteLine(exception.Message);
                        }
                    }

                }
                else if (string.IsNullOrWhiteSpace(line) || regexTimeCodes1.IsMatch("   " + s))
                {
                    // skip empty lines
                }
                else if (!string.IsNullOrWhiteSpace(line) && p != null)
                {
                    _errorCount++;
                }
            }

            for (int i = 0; i < subtitle.Paragraphs.Count; i++)
            {
                Paragraph current = subtitle.Paragraphs[i];
                Paragraph next = subtitle.GetParagraphOrDefault(i + 1);
                if (next != null)
                    current.EndTime.TotalMilliseconds = next.StartTime.TotalMilliseconds - Configuration.Settings.General.MinimumMillisecondsBetweenLines;
                else
                    current.EndTime.TotalMilliseconds = current.StartTime.TotalMilliseconds + Utilities.GetOptimalDisplayMilliseconds(current.Text);

                if (current.Duration.TotalMilliseconds > Configuration.Settings.General.SubtitleMaximumDisplayMilliseconds)
                    current.EndTime.TotalMilliseconds = current.StartTime.TotalMilliseconds + Configuration.Settings.General.SubtitleMaximumDisplayMilliseconds;
            }
            subtitle.RemoveEmptyLines();
            subtitle.Renumber();
        }
 public void TestAdjust2()
 {
     var p = new Paragraph { Text = string.Empty, StartTime = new TimeCode(0, 1, 1, 1), EndTime = new TimeCode(0, 1, 4, 1) };
     p.Adjust(2, 10);
     Assert.AreEqual(new TimeCode(0, 2, 12, 2).TotalMilliseconds, p.StartTime.TotalMilliseconds);
     Assert.AreEqual(new TimeCode(0, 2, 18, 2).TotalMilliseconds, p.EndTime.TotalMilliseconds);
 }
 public ParagraphEventArgs(double seconds, Paragraph p, Paragraph b, MouseDownParagraphType mouseDownParagraphType, bool movePreviousOrNext)
 {
     Seconds = seconds;
     Paragraph = p;
     BeforeParagraph = b;
     MouseDownParagraphType = mouseDownParagraphType;
     MovePreviousOrNext = movePreviousOrNext;
 }
 public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
 {
     _errorCount = 0;
     Paragraph p = null;
     foreach (string line in lines)
     {
         if (line.StartsWith("# SUBTITLE"))
         {
             if (p != null)
                 subtitle.Paragraphs.Add(p);
             p = new Paragraph();
         }
         else if (p != null && line.StartsWith("# TIMEIN"))
         {
             string timeCode = line.Remove(0, 8).Trim();
             if (timeCode != "--:--:--:--" && !GetTimeCode(p.StartTime, timeCode))
                 _errorCount++;
         }
         else if (p != null && line.StartsWith("# DURATION"))
         {
             // # DURATION 01:17 AUTO
             string timecode = line.Remove(0, 10).Replace("AUTO", string.Empty).Trim();
             if (timecode != "--:--")
             {
                 var arr = timecode.Split(new[] { ':', ' ' });
                 if (arr.Length > 1)
                 {
                     int sec;
                     int frame;
                     if (int.TryParse(arr[0], out sec) && int.TryParse(arr[1], out frame))
                     {
                         p.EndTime.TotalMilliseconds = p.StartTime.TotalMilliseconds + FramesToMillisecondsMax999(frame);
                         p.EndTime.TotalSeconds += sec;
                     }
                 }
             }
         }
         else if (p != null && line.StartsWith("# TIMEOUT"))
         {
             string timeCode = line.Remove(0, 9).Trim();
             if (timeCode != "--:--:--:--" && !GetTimeCode(p.EndTime, timeCode))
                 _errorCount++;
         }
         else if (p != null && !line.StartsWith('#'))
         {
             if (p.Text.Length > 500)
             {
                 _errorCount += 10;
                 return;
             }
             p.Text = (p.Text + Environment.NewLine + line).Trim();
         }
     }
     if (p != null)
         subtitle.Paragraphs.Add(p);
     subtitle.RemoveEmptyLines();
     subtitle.Renumber();
 }
 private static void SetTimeCodes(string fileName, ListViewItem item)
 {
     string name = Path.GetFileNameWithoutExtension(fileName);
     var p = new Paragraph();
     SetEndTimeAndStartTime(name, p);
     item.SubItems.Add(p.StartTime.ToString());
     item.SubItems.Add(p.EndTime.ToString());
     item.SubItems.Add(p.Duration.ToShortString());
 }
        public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
        {
            var temp = new StringBuilder();
            foreach (string l in lines)
                temp.Append(l);
            string all = temp.ToString();
            if (!all.Contains("{\"content\":\""))
                return;

            var arr = all.Replace("\n", string.Empty).Replace("{\"content\":\"", "\n").Split('\n');

            _errorCount = 0;
            subtitle.Paragraphs.Clear();

            // {"content":"La ce se gandeste  Oh Ha Ni a noastra <br> de la inceputul dimineti?","start_time":314071,"end_time":317833},
            for (int i = 0; i < arr.Length; i++)
            {
                string line = arr[i].Trim();

                int indexStartTime = line.IndexOf("\"start_time\":", StringComparison.Ordinal);
                int indexEndTime = line.IndexOf("\"end_time\":", StringComparison.Ordinal);
                if (indexStartTime > 0 && indexEndTime > 0)
                {
                    int indexEndText = indexStartTime;
                    if (indexStartTime > indexEndTime)
                        indexEndText = indexEndTime;
                    string text = line.Substring(0, indexEndText - 1).Trim().TrimEnd('\"');
                    text = text.Replace("<br>", Environment.NewLine).Replace("<BR>", Environment.NewLine);
                    text = text.Replace("<br/>", Environment.NewLine).Replace("<BR/>", Environment.NewLine);
                    text = text.Replace(Environment.NewLine + " ", Environment.NewLine);
                    text = text.Replace(Environment.NewLine + " ", Environment.NewLine);
                    text = text.Replace(Environment.NewLine + " ", Environment.NewLine);
                    text = text.Replace(" " + Environment.NewLine, Environment.NewLine);
                    text = text.Replace(" " + Environment.NewLine, Environment.NewLine);
                    text = text.Replace(" " + Environment.NewLine, Environment.NewLine);
                    try
                    {
                        string start = line.Substring(indexStartTime);
                        string end = line.Substring(indexEndTime);
                        var paragraph = new Paragraph
                                            {
                                                Text = text,
                                                StartTime = { TotalMilliseconds = GetMilliseconds(start) },
                                                EndTime = { TotalMilliseconds = GetMilliseconds(end) }
                                            };
                        subtitle.Paragraphs.Add(paragraph);
                    }
                    catch (Exception exception)
                    {
                        System.Diagnostics.Debug.WriteLine(exception.Message);
                        _errorCount++;
                    }
                }
            }
            subtitle.Renumber();
        }
        public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
        {
            var paragraph = new Paragraph();
            ExpectingLine expecting = ExpectingLine.TimeStart;
            _errorCount = 0;

            subtitle.Paragraphs.Clear();
            foreach (string line in lines)
            {
                if (line.StartsWith('[') && regexTimeCode.IsMatch(line))
                {
                    string[] parts = line.Split(new[] { ':', ']', '[', ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    if (parts.Length == 3)
                    {
                        try
                        {
                            int startHours = int.Parse(parts[0]);
                            int startMinutes = int.Parse(parts[1]);
                            int startSeconds = int.Parse(parts[2]);
                            var tc = new TimeCode(startHours, startMinutes, startSeconds, 0);
                            if (expecting == ExpectingLine.TimeStart)
                            {
                                paragraph = new Paragraph();
                                paragraph.StartTime = tc;
                                expecting = ExpectingLine.Text;
                            }
                            else if (expecting == ExpectingLine.TimeEnd)
                            {
                                paragraph.EndTime = tc;
                                expecting = ExpectingLine.TimeStart;
                                subtitle.Paragraphs.Add(paragraph);
                                paragraph = new Paragraph();
                            }
                        }
                        catch
                        {
                            _errorCount++;
                            expecting = ExpectingLine.TimeStart;
                        }
                    }
                }
                else
                {
                    if (expecting == ExpectingLine.Text)
                    {
                        if (line.Length > 0)
                        {
                            string text = line.Replace("|", Environment.NewLine);
                            paragraph.Text = text;
                            expecting = ExpectingLine.TimeEnd;
                        }
                    }
                }
            }
            subtitle.Renumber();
        }
        public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
        {
            //  00:18:02:  (斉藤)失礼な大人って!  (悠子)何言ってんのあんた?
            Paragraph p = null;
            subtitle.Paragraphs.Clear();
            _errorCount = 0;
            foreach (string line in lines)
            {
                string s = line.Trim();
                if (regexTimeCodes.IsMatch(s))
                {
                    var temp = s.Substring(0, 8);

                    string[] startParts = temp.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                    if (startParts.Length == 3)
                    {
                        try
                        {
                            string text = s.Remove(0, 10).Trim();
                            text = text.Replace("  ", Environment.NewLine);
                            p = new Paragraph(DecodeTimeCode(startParts), new TimeCode(0, 0, 0, 0), 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++;
                }
            }

            for (int i = 0; i < subtitle.Paragraphs.Count; i++)
            {
                Paragraph current = subtitle.Paragraphs[i];
                Paragraph next = subtitle.GetParagraphOrDefault(i + 1);
                if (next != null)
                    current.EndTime.TotalMilliseconds = next.StartTime.TotalMilliseconds - Configuration.Settings.General.MinimumMillisecondsBetweenLines;
                else
                    current.EndTime.TotalMilliseconds = current.StartTime.TotalMilliseconds + Utilities.GetOptimalDisplayMilliseconds(current.Text);

                if (current.Duration.TotalMilliseconds > Configuration.Settings.General.SubtitleMaximumDisplayMilliseconds)
                    current.EndTime.TotalMilliseconds = current.StartTime.TotalMilliseconds + Configuration.Settings.General.SubtitleMaximumDisplayMilliseconds;
            }

            subtitle.Renumber();
        }
Exemple #24
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();
            xml.XmlResolver = null;
            xml.LoadXml(sb.ToString().Trim());

            XmlNode div = xml.DocumentElement.SelectSingleNode("Lines");
            foreach (XmlNode node in div.ChildNodes)
            {
                try
                {
                    var p = new Paragraph();

                    XmlNode text = node.SelectSingleNode("Text");
                    if (text != null)
                    {
                        sb.Clear();
                        foreach (XmlNode innerNode in text.ChildNodes)
                        {
                            switch (innerNode.Name)
                            {
                                case "br":
                                    sb.AppendLine();
                                    break;
                                default:
                                    sb.Append(innerNode.InnerText);
                                    break;
                            }
                        }
                        p.Text = sb.ToString();
                    }

                    XmlNode timeMS = node.SelectSingleNode("TimeMs");
                    if (timeMS != null)
                    {
                        string ms = timeMS.InnerText;
                        long milliseconds;
                        if (long.TryParse(ms, out milliseconds))
                            p.StartTime = new TimeCode(milliseconds);
                    }
                    p.EndTime = new TimeCode(p.StartTime.TotalMilliseconds + Utilities.GetOptimalDisplayMilliseconds(p.Text));

                    subtitle.Paragraphs.Add(p);
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex.Message);
                    _errorCount++;
                }
            }
            subtitle.Renumber();
        }
        public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
        {
            Paragraph p = null;
            subtitle.Paragraphs.Clear();
            _errorCount = 0;
            foreach (string line in lines)
            {
                string s = line;
                if (regexTimeCodes.IsMatch(s))
                {
                    s = s.Remove(0, s.IndexOf(':') + 1).Trim();
                    var temp = s.Split(' ');
                    if (temp.Length > 1)
                    {
                        string start = temp[0];
                        string end = temp[1];

                        string[] startParts = start.Split(':');
                        string[] endParts = end.Split(':');
                        if (startParts.Length == 4 && endParts.Length == 4)
                        {
                            try
                            {
                                p = new Paragraph(DecodeTimeCode(startParts), DecodeTimeCode(endParts), string.Empty);
                                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++;
                }
                else
                {
                    p.Text = (p.Text + Environment.NewLine + line).Trim();
                    if (p.Text.Length > 1000)
                    {
                        _errorCount += 10;
                        return;
                    }
                }
            }

            subtitle.Renumber();
        }
Exemple #26
0
        public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
        {
            _errorCount = 0;
            subtitle.Paragraphs.Clear();
            if (string.IsNullOrEmpty(fileName))
            {
                return;
            }
            byte[] array;
            try
            {
                array = FileUtil.ReadAllBytesShared(fileName);
            }
            catch
            {
                _errorCount++;
                return;
            }
            if (array.Length < 100 || array[0] != 84 || array[1] != 83 || array[2] != 66 || array[3] != 52)
            {
                return;
            }
            for (int i = 0; i < array.Length - 20; i++)
            {
                if (array[i] == 84 && array[i + 1] == 73 && array[i + 2] == 84 && array[i + 3] == 76 && array[i + 8] == 84 && array[i + 9] == 73 && array[i + 10] == 77 && array[i + 11] == 69) // TITL + TIME
                {
                    int endOfText = array[i + 4];

                    int start = array[i + 16] + array[i + 17] * 256;
                    if (array[i + 18] != 32)
                        start += array[i + 18] * 256 * 256;

                    int end = array[i + 20] + array[i + 21] * 256;
                    if (array[i + 22] != 32)
                        end += array[i + 22] * 256 * 256;

                    int textStart = i;
                    while (textStart < i + endOfText && !(array[textStart] == 0x4C && array[textStart + 1] == 0x49 && array[textStart + 2] == 0x4E && array[textStart + 3] == 0x45)) // LINE
                    {
                        textStart++;
                    }
                    int length = i + endOfText - textStart - 2;
                    textStart += 8;

                    string text = Encoding.Default.GetString(array, textStart, length);
                    // text = Encoding.Default.GetString(array, i + 53, endOfText - 47);
                    text = text.Trim('\0').Replace("\0", " ").Trim();
                    var item = new Paragraph(text, FramesToMilliseconds(start), FramesToMilliseconds(end));
                    subtitle.Paragraphs.Add(item);
                    i += endOfText + 5;
                }
            }
            subtitle.RemoveEmptyLines();
            subtitle.Renumber();
        }
Exemple #27
0
        public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
        {
            //00:03:15:22  00:03:23:10 This is line one.
            //This is line two.
            Paragraph p = null;
            subtitle.Paragraphs.Clear();
            _errorCount = 0;
            bool beginFound = false;
            bool endFound = false;
            foreach (string line in lines)
            {
                string tline = line.Trim();
                if (tline.Equals("<begin subtitles>", StringComparison.OrdinalIgnoreCase))
                {
                    beginFound = true;
                }
                else if (tline.Equals("<end subtitles>", StringComparison.OrdinalIgnoreCase))
                {
                    endFound = true;
                    break;
                }

                if (line.IndexOf(':') == 2 && regexTimeCodes.IsMatch(line))
                {
                    string temp = line.Substring(0, regexTimeCodes.Match(line).Length);
                    string start = temp.Substring(0, 11);
                    string end = temp.Substring(12, 11);

                    string[] startParts = start.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                    string[] endParts = end.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                    if (startParts.Length == 4 && endParts.Length == 4)
                    {
                        p = new Paragraph(DecodeTimeCode(startParts), DecodeTimeCode(endParts), string.Empty);
                        subtitle.Paragraphs.Add(p);
                    }
                }
                else if (tline.Length == 0 || tline[0] == '@')
                {
                    // skip these lines
                }
                else if (tline.Length > 0 && p != null)
                {
                    if (string.IsNullOrEmpty(p.Text))
                        p.Text = line;
                    else
                        p.Text = p.Text.TrimEnd() + Environment.NewLine + line;
                }
            }
            if (!beginFound)
                _errorCount++;
            if (!endFound)
                _errorCount++;

            subtitle.Renumber();
        }
        public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
        {
            var paragraph = new Paragraph();
            ExpectingLine expecting = ExpectingLine.TimeCodes;
            _errorCount = 0;

            subtitle.Paragraphs.Clear();
            foreach (string line in lines)
            {
                string s = line.Trim();
                if (regexTimeCodes.IsMatch(s))
                {
                    if (!string.IsNullOrEmpty(paragraph.Text))
                        subtitle.Paragraphs.Add(paragraph);

                    paragraph = new Paragraph();
                    string[] parts = s.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    if (parts.Length == 2)
                    {
                        try
                        {
                            paragraph.StartTime.TotalMilliseconds = long.Parse(parts[0]) * 10;
                            paragraph.EndTime.TotalMilliseconds = long.Parse(parts[1]) * 10;
                            expecting = ExpectingLine.BeforeText;
                        }
                        catch
                        {
                            expecting = ExpectingLine.TimeCodes;
                        }
                    }
                }
                else if (regexBeforeText.IsMatch(s))
                {
                    expecting = ExpectingLine.Text;
                }
                else
                {
                    if (expecting == ExpectingLine.Text)
                    {
                        if (s.Length > 0)
                        {
                            if (!string.IsNullOrEmpty(paragraph.Text))
                                paragraph.Text += Environment.NewLine + s;
                            else
                                paragraph.Text = s;

                        }
                    }
                }
            }
            if (!string.IsNullOrEmpty(paragraph.Text))
                subtitle.Paragraphs.Add(paragraph);
            subtitle.Renumber();
        }
Exemple #29
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("<captionate>") || !xmlString.Contains("</caption>"))
                return;

            var xml = new XmlDocument();
            xml.XmlResolver = null;
            try
            {
                xml.LoadXml(xmlString);
            }
            catch
            {
                _errorCount = 1;
                return;
            }

            Paragraph p = null;
            foreach (XmlNode node in xml.DocumentElement.SelectNodes("captions/caption"))
            {
                try
                {
                    if (node.Attributes["time"] != null)
                    {
                        string start = node.Attributes["time"].InnerText;
                        double startMilliseconds = double.Parse(start);
                        if (p != null)
                            p.EndTime.TotalMilliseconds = startMilliseconds - 1;
                        if (node.SelectSingleNode("tracks/track0") != null)
                        {
                            string text = node.SelectSingleNode("tracks/track0").InnerText;
                            text = HtmlUtil.RemoveHtmlTags(text);
                            text = text.Replace("<br>", Environment.NewLine).Replace("<br />", Environment.NewLine).Replace("<BR>", Environment.NewLine);
                            p = new Paragraph(text, startMilliseconds, startMilliseconds + 3000);
                            if (!string.IsNullOrWhiteSpace(text))
                                subtitle.Paragraphs.Add(p);
                        }
                    }
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex.Message);
                    _errorCount++;
                }
            }
            subtitle.Renumber();
        }
Exemple #30
0
        public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
        {
            //0001  01:25:59:21 01:26:00:20 0   0   BK02-total_0001.png 0   0
            Paragraph p = null;
            subtitle.Paragraphs.Clear();
            _errorCount = 0;
            foreach (string line in lines)
            {
                string s = line;
                if (regexTimeCodes.IsMatch(s))
                {
                    var temp = s.Split('\t');
                    if (temp.Length > 7)
                    {
                        string start = temp[1];
                        string end = temp[2];
                        string text = temp[5];
                        try
                        {
                            p = new Paragraph(DecodeTimeCode(start.Split(':')), DecodeTimeCode(end.Split(':')), text);
                            subtitle.Paragraphs.Add(p);
                        }
                        catch (Exception exception)
                        {
                            _errorCount++;
                            System.Diagnostics.Debug.WriteLine(exception.Message);
                        }

                    }
                }
                else if (line.StartsWith("$DROP="))
                {
                    s = s.Remove(0, "$DROP=".Length);
                    int frameRate;
                    if (int.TryParse(s, out frameRate))
                    {
                        double f = frameRate / TimeCode.BaseUnit;
                        if (f > 10 && f < 500)
                            Configuration.Settings.General.CurrentFrameRate = f;
                    }
                }
                else if (string.IsNullOrWhiteSpace(line) || line.StartsWith('$'))
                {
                    // skip empty lines or lines starting with $
                }
                else if (!string.IsNullOrWhiteSpace(line) && p != null)
                {
                    _errorCount++;
                }
            }

            subtitle.Renumber();
        }
Exemple #31
0
 public void ShowSubtitle(Nikse.SubtitleEdit.Core.Paragraph p)
 {
     if (p == null)
     {
         _subtitleWebView.MainFrame.LoadHtmlString(new NSString("<body style='background-color:black'></body>"), null);
     }
     else
     {
         var  sb    = new StringBuilder();
         bool first = true;
         foreach (var line in p.Text.SplitToLines())
         {
             if (!first)
             {
                 sb.Append("<br />");
             }
             sb.Append(line);
             first = false;
         }
         _subtitleWebView.MainFrame.LoadHtmlString(new NSString("<body style='background-color:black;color:white;text-align:center'>" + sb.ToString() + "<body>"), null);
     }
 }
Exemple #32
0
        private void PopulateTeletextParagraph(Nikse.SubtitleEdit.Core.Paragraph PesPayload)
        {
            ShowSubsPanels(false, true);

            this.listViewDetails.Items.Clear();
            ListViewGroup grpGeneral = listViewDetails.Groups.Add("General", "General");

            //Console.WriteLine(PesPayload.ToString());

            //Utils.AddListViewEntry(
            //    Lv,
            //    "dds_version_number",
            //    this.DisplayDefinitionVersionNumber.ToString(),
            //    "The version of this display definition segment. When any of the contents of this display definition segment change, this version number is incremented(modulo 16).",
            //    grpGeneral
            //);

            // populated rich text box
            Console.WriteLine(PesPayload.Style);


            this.labelTtext.Text = PesPayload.Text;
        }
Exemple #33
0
        private static List <Paragraph> MakeVerticalParagraphs(Paragraph p, int width)
        {
            var lines      = p.Text.SplitToLines();
            var adjustment = 34;
            var startX     = 9 + lines.Count * 2 * adjustment;
            var leftAlign  = p.Text.StartsWith("{\\an7}", StringComparison.Ordinal);

            if (!leftAlign)
            {
                startX = width - 50;
            }

            string pre          = p.Text.Substring(0, 5);
            var    list         = new List <Paragraph>();
            var    furiganaList = new List <Paragraph>();
            var    rubyOn       = false;
            var    italinOn     = false;

            for (var index = 0; index < lines.Count; index++)
            {
                var line   = lines[index];
                var actual = new StringBuilder();
                int i      = 0;
                if (italinOn)
                {
                    line = "<i>" + line;
                }
                while (i < line.Length)
                {
                    if (line.Substring(i).StartsWith("{\\"))
                    {
                        var end = line.IndexOf('}', i);
                        if (end < 0)
                        {
                            break;
                        }

                        i = end + 1;
                    }
                    else if (line.Substring(i).StartsWith("<i>", StringComparison.Ordinal) || line.Substring(i).StartsWith("<u>", StringComparison.Ordinal) || line.Substring(i).StartsWith("<b>", StringComparison.Ordinal))
                    {
                        actual.Append("{\\i1}");
                        i       += 3;
                        italinOn = true;
                    }
                    else if (line.Substring(i).StartsWith("</i>", StringComparison.Ordinal) || line.Substring(i).StartsWith("</u>", StringComparison.Ordinal) || line.Substring(i).StartsWith("</b>", StringComparison.Ordinal))
                    {
                        actual.Append("{\\i0}");
                        i       += 4;
                        italinOn = false;
                    }
                    else if (line.Substring(i).StartsWith("<horizontalDigit>", StringComparison.Ordinal))
                    {
                        var end = line.IndexOf('>', i);
                        if (end < 0)
                        {
                            break;
                        }

                        var endTagStart = line.IndexOf("</", end, StringComparison.Ordinal);
                        if (endTagStart < 0)
                        {
                            break;
                        }

                        var text = line.Substring(end + 1, endTagStart - end - 1);
                        actual.Append(text);
                        actual.AppendLine();
                        i = endTagStart + "</horizontalDigit>".Length;
                    }
                    else if (line.Substring(i).StartsWith("</horizontalDigit>", StringComparison.Ordinal))
                    {
                        i += "</horizontalDigit>".Length;
                    }
                    else if (line.Substring(i).StartsWith("<bouten-", StringComparison.Ordinal))
                    {
                        var end = line.IndexOf('>', i);
                        if (end < 0)
                        {
                            break;
                        }

                        if (end + 1 >= line.Length)
                        {
                            break;
                        }

                        var endTagStart = line.IndexOf("</", end, StringComparison.Ordinal);
                        if (endTagStart < 0)
                        {
                            break;
                        }

                        var tag  = line.Substring(i + 1, end - i - 1);
                        var text = line.Substring(end + 1, endTagStart - end - 1);
                        foreach (var ch in text)
                        {
                            var furiganaChar = BoutenTagToUnicode(tag);
                            if (!string.IsNullOrWhiteSpace(furiganaChar))
                            {
                                var preFurigana = string.Empty;
                                if (actual.Length > 0)
                                {
                                    preFurigana = $"{{\\alpha&FF&}}{actual}{{\\alpha&0&}}";
                                }
                                furiganaList.Add(new Paragraph($"{preFurigana}{furiganaChar}", p.StartTime.TotalMilliseconds, p.EndTime.TotalMilliseconds));
                            }
                            actual.Append(ch);
                            actual.AppendLine();
                        }

                        var endTagEnd = line.IndexOf('>', endTagStart);
                        if (endTagEnd < 0)
                        {
                            break;
                        }

                        i = endTagEnd + 1;
                    }
                    else if (line.Substring(i).StartsWith("<ruby-container>", StringComparison.Ordinal))
                    {
                        var baseTextStart = line.IndexOf("<ruby-base>", i, StringComparison.Ordinal);
                        var baseTextEnd   = line.IndexOf("</ruby-base>", i, StringComparison.Ordinal);
                        if (baseTextStart < 0 || baseTextEnd < 0)
                        {
                            baseTextStart = line.IndexOf("<ruby-base-italic>", i, StringComparison.Ordinal);
                            baseTextEnd   = line.IndexOf("</ruby-base-italic>", i, StringComparison.Ordinal);
                            if (baseTextStart < 0 || baseTextEnd < 0)
                            {
                                break;
                            }
                        }
                        baseTextStart += "<ruby-base>".Length;
                        var baseText = line.Substring(baseTextStart, baseTextEnd - baseTextStart);

                        var extraText      = string.Empty;
                        var extraTextStart = line.IndexOf("<ruby-text>", i, StringComparison.Ordinal);
                        var extraTextEnd   = line.IndexOf("</ruby-text>", i, StringComparison.Ordinal);
                        if (extraTextStart >= 0 || extraTextEnd >= 0 && extraTextStart < extraTextEnd)
                        {
                            extraTextStart += "<ruby-text>".Length;
                            extraText       = line.Substring(extraTextStart, extraTextEnd - extraTextStart);
                        }

                        if (string.IsNullOrEmpty(extraText))
                        {
                            extraTextStart = line.IndexOf("<ruby-text-italic>", i, StringComparison.Ordinal);
                            extraTextEnd   = line.IndexOf("</ruby-text-italic>", i, StringComparison.Ordinal);
                            if (extraTextStart >= 0 || extraTextEnd >= 0 && extraTextStart < extraTextEnd)
                            {
                                extraTextStart += "<ruby-text-italic>".Length;
                                extraText       = line.Substring(extraTextStart, extraTextEnd - extraTextStart);
                            }
                        }

                        var extraTextAfter      = string.Empty;
                        var extraTextStartAfter = line.IndexOf("<ruby-text-after>", i, StringComparison.Ordinal);
                        var extraTextEndAfter   = line.IndexOf("</ruby-text-after>", i, StringComparison.Ordinal);
                        if (extraTextStartAfter >= 0 || extraTextEndAfter >= 0 && extraTextStartAfter < extraTextEndAfter)
                        {
                            extraTextStartAfter += "<ruby-text-after>".Length;
                            extraText            = line.Substring(extraTextStartAfter, extraTextEndAfter - extraTextStartAfter);
                        }

                        var preFurigana = string.Empty;
                        if (actual.Length > 0)
                        {
                            preFurigana = $"{{\\alpha&FF&}}{actual.ToString().TrimEnd()}{{\\alpha&0&}}";
                        }
                        if (!string.IsNullOrWhiteSpace(extraText))
                        {
                            var sb = new StringBuilder();
                            foreach (var ch in extraText)
                            {
                                sb.Append(ch);
                                sb.AppendLine();
                            }
                            furiganaList.Add(new Paragraph($"{preFurigana}{{\\fs20}}{sb.ToString().TrimEnd()}", p.StartTime.TotalMilliseconds, p.EndTime.TotalMilliseconds));
                        }
                        if (!string.IsNullOrWhiteSpace(extraTextAfter))
                        {
                            var sb = new StringBuilder();
                            foreach (var ch in extraTextAfter)
                            {
                                sb.Append(ch);
                                sb.AppendLine();
                            }
                            furiganaList.Add(new Paragraph($"{preFurigana}{{\\fs20}} {sb.ToString().TrimEnd()}", p.StartTime.TotalMilliseconds, p.EndTime.TotalMilliseconds));
                        }

                        foreach (var ch in baseText)
                        {
                            actual.Append(ch);
                            actual.AppendLine();
                        }

                        var endTagEnd = line.IndexOf("</ruby-container>", i, StringComparison.Ordinal);
                        if (endTagEnd < 0)
                        {
                            break;
                        }
                        i      = endTagEnd + "</ruby-container>".Length;
                        rubyOn = true;
                    }
                    else
                    {
                        actual.AppendLine(line.Substring(i, 1));
                        i++;
                    }
                }

                bool displayBefore = lines.Count == 2 && index == 0 || lines.Count == 1;
                if (displayBefore && furiganaList.Count > 0)
                {
                    foreach (var fp in furiganaList)
                    {
                        var text = pre + "\\pos(" + startX + ",45)}" + fp.Text.TrimEnd();
                        list.Add(new Paragraph(text, p.StartTime.TotalMilliseconds, p.EndTime.TotalMilliseconds));
                    }
                    startX -= adjustment;
                    if (rubyOn && index == 0 && lines.Count == 2)
                    {
                        if (leftAlign)
                        {
                            startX -= 8;
                        }
                        else
                        {
                            startX += 16;
                        }
                    }
                }

                var actualText = actual.ToString().TrimEnd();
                actualText = pre + "\\pos(" + startX + ",40)}" + actualText;

                // change horizontal chars to vertical version
                actualText = actualText
                             .Replace('…', '⋮')
                             .Replace('〈', '︿')
                             .Replace('〉', '﹀')
                             .Replace('—', '︱') // em dash
                             .Replace('⸺', '︱') // double em dash (could not find double em dash vertical)
                             .Replace('ー', '⏐') // prolonged sound mark
                             .Replace('(', '︵')
                             .Replace(')', '︶');

                list.Add(new Paragraph(actualText, p.StartTime.TotalMilliseconds, p.EndTime.TotalMilliseconds));
                startX -= adjustment;

                if (!displayBefore && furiganaList.Count > 0)
                {
                    if (rubyOn && index == 1 && lines.Count == 2)
                    {
                        if (leftAlign)
                        {
                            startX += 14;
                        }
                        else
                        {
                            startX -= 8;
                        }
                    }
                    foreach (var fp in furiganaList)
                    {
                        var text = pre + "\\pos(" + startX + ",45)}" + fp.Text.TrimEnd();
                        list.Add(new Paragraph(text, p.StartTime.TotalMilliseconds, p.EndTime.TotalMilliseconds));
                    }
                    startX -= adjustment;
                }
            }

            return(list);
        }
Exemple #34
0
        private void TreeViewItemChange()
        {
            this.listViewDetails.BeginUpdate();
            try
            {
                if (this.treeViewMain.SelectedNode == null)
                {
                    return;
                }

                if (this.treeViewMain.SelectedNode.Tag != null)
                {
                    bool   getParentBitmap = true;
                    string tagType         = this.treeViewMain.SelectedNode.Tag.GetType().ToString();
                    switch (tagType)
                    {
                    case "Nikse.SubtitleEdit.Core.ContainerFormats.TransportStream.PageCompositionSegment":
                        ((PageCompositionSegment)this.treeViewMain.SelectedNode.Tag).PopulateListViewDetails(this.listViewDetails);
                        break;

                    case "Nikse.SubtitleEdit.Core.ContainerFormats.TransportStream.RegionCompositionSegment":
                        ((RegionCompositionSegment)this.treeViewMain.SelectedNode.Tag).PopulateListViewDetails(this.listViewDetails);
                        break;

                    case "Nikse.SubtitleEdit.Core.ContainerFormats.TransportStream.ClutDefinitionSegment":
                        ((ClutDefinitionSegment)this.treeViewMain.SelectedNode.Tag).PopulateListViewDetails(this.listViewDetails);
                        break;

                    case "Nikse.SubtitleEdit.Core.ContainerFormats.TransportStream.ObjectDataSegment":
                        ((ObjectDataSegment)this.treeViewMain.SelectedNode.Tag).PopulateListViewDetails(this.listViewDetails);
                        break;

                    case "Nikse.SubtitleEdit.Core.ContainerFormats.TransportStream.DisplayDefinitionSegment":
                        ((DisplayDefinitionSegment)this.treeViewMain.SelectedNode.Tag).PopulateListViewDetails(this.listViewDetails);
                        break;

                    case "Nikse.SubtitleEdit.Core.ContainerFormats.TransportStream.EndOfDisplaySetSegment":
                        ((EndOfDisplaySetSegment)this.treeViewMain.SelectedNode.Tag).PopulateListViewDetails(this.listViewDetails);
                        break;

                    case "Nikse.SubtitleEdit.Core.ContainerFormats.TransportStream.DvbSubPes":
                        this.listViewDetails.Items.Clear();
                        DvbSubPes tmp2 = (DvbSubPes)this.treeViewMain.SelectedNode.Tag;
                        PopulateImage(tmp2);
                        break;

                    case "Nikse.SubtitleEdit.Core.Paragraph":

                        // clear details

                        // show subtitle page
                        Console.WriteLine("Nikse.SubtitleEdit.Core.Paragraph");
                        Nikse.SubtitleEdit.Core.Paragraph tmp3 = (Nikse.SubtitleEdit.Core.Paragraph) this.treeViewMain.SelectedNode.Tag;
                        PopulateTeletextParagraph(tmp3);
                        break;

                    default:
                        getParentBitmap = false;
                        Console.WriteLine("Unknown Segment Type");
                        this.listViewDetails.Items.Clear();
                        break;
                    }

                    if ((getParentBitmap) && (this.treeViewMain.SelectedNode.Parent.Tag != null))
                    {
                        DvbSubPes tmp2 = (DvbSubPes)this.treeViewMain.SelectedNode.Parent.Tag;
                        PopulateImage(tmp2);
                    }
                }
                else
                {
                    Console.WriteLine("tag null");
                }
            }
            finally
            {
                this.listViewDetails.EndUpdate();
            }
        }
Exemple #35
0
        private static List <Paragraph> MakeHorizontalParagraphs(Paragraph p, int width, int height)
        {
            var lines      = p.Text.SplitToLines();
            var adjustment = 34;
            var startY     = height - lines.Count * 2 * adjustment + 30;

            if (p.Text.StartsWith("{\\an8", StringComparison.Ordinal))
            {
                startY = 40;
            }

            var list         = new List <Paragraph>();
            var furiganaList = new List <Paragraph>();
            var rubyOn       = false;
            var italinOn     = false;
            int startX;

            using (var g = Graphics.FromHwnd(IntPtr.Zero))
            {
                var actualText     = NetflixImsc11Japanese.RemoveBoutens(HtmlUtil.RemoveHtmlTags(p.Text, true));
                var actualTextSize = g.MeasureString(actualText, new Font(SystemFonts.DefaultFont.FontFamily, 20));
                startX = (int)(width / 2.0 - actualTextSize.Width / 2.0);
                if (p.Text.StartsWith("{\\an5", StringComparison.Ordinal))
                {
                    startY = (int)(height / 2.0 - actualTextSize.Height / 2.0);
                }
            }

            for (var index = 0; index < lines.Count; index++)
            {
                var line = lines[index];
                if (italinOn)
                {
                    line = "<i>" + line;
                }
                var actual = new StringBuilder();
                int i      = 0;
                while (i < line.Length)
                {
                    if (line.Substring(i).StartsWith("{\\"))
                    {
                        var end = line.IndexOf('}', i);
                        if (end < 0)
                        {
                            break;
                        }

                        i = end + 1;
                    }
                    else if (line.Substring(i).StartsWith("<i>", StringComparison.Ordinal) || line.Substring(i).StartsWith("<u>", StringComparison.Ordinal) || line.Substring(i).StartsWith("<b>", StringComparison.Ordinal))
                    {
                        actual.Append("{\\i1}");
                        i       += 3;
                        italinOn = true;
                    }
                    else if (line.Substring(i).StartsWith("</i>", StringComparison.Ordinal) || line.Substring(i).StartsWith("</u>", StringComparison.Ordinal) || line.Substring(i).StartsWith("</b>", StringComparison.Ordinal))
                    {
                        actual.Append("{\\i0}");
                        i       += 4;
                        italinOn = false;
                    }
                    else if (line.Substring(i).StartsWith("<bouten-", StringComparison.Ordinal))
                    {
                        var end = line.IndexOf('>', i);
                        if (end < 0)
                        {
                            break;
                        }

                        if (end + 1 >= line.Length)
                        {
                            break;
                        }

                        var endTagStart = line.IndexOf("</", end, StringComparison.Ordinal);
                        if (endTagStart < 0)
                        {
                            break;
                        }

                        var tag  = line.Substring(i + 1, end - i - 1);
                        var text = line.Substring(end + 1, endTagStart - end - 1);

                        foreach (var ch in text)
                        {
                            var furiganaChar = BoutenTagToUnicode(tag);
                            if (!string.IsNullOrWhiteSpace(furiganaChar))
                            {
                                furiganaList.Add(new Paragraph($"{{\\alpha&FF&}}{actual}{{\\alpha&0&}}{furiganaChar}", p.StartTime.TotalMilliseconds, p.EndTime.TotalMilliseconds));
                            }
                            actual.Append(ch);
                        }

                        var endTagEnd = line.IndexOf('>', endTagStart);
                        if (endTagEnd < 0)
                        {
                            break;
                        }

                        i = endTagEnd + 1;
                    }
                    else if (line.Substring(i).StartsWith("<ruby-container>", StringComparison.Ordinal))
                    {
                        var baseTextStart = line.IndexOf("<ruby-base>", i, StringComparison.Ordinal);
                        var baseTextEnd   = line.IndexOf("</ruby-base>", i, StringComparison.Ordinal);
                        if (baseTextStart < 0 || baseTextEnd < 0)
                        {
                            baseTextStart = line.IndexOf("<ruby-base-italic>", i, StringComparison.Ordinal);
                            baseTextEnd   = line.IndexOf("</ruby-base-italic>", i, StringComparison.Ordinal);
                            if (baseTextStart < 0 || baseTextEnd < 0)
                            {
                                break;
                            }
                        }
                        baseTextStart += "<ruby-base>".Length;
                        var baseText = line.Substring(baseTextStart, baseTextEnd - baseTextStart);

                        var extraText      = string.Empty;
                        var extraTextStart = line.IndexOf("<ruby-text>", i, StringComparison.Ordinal);
                        var extraTextEnd   = line.IndexOf("</ruby-text>", i, StringComparison.Ordinal);
                        if (extraTextStart >= 0 || extraTextEnd >= 0 && extraTextStart < extraTextEnd)
                        {
                            extraTextStart += "<ruby-text>".Length;
                            extraText       = line.Substring(extraTextStart, extraTextEnd - extraTextStart);
                        }

                        if (string.IsNullOrEmpty(extraText))
                        {
                            extraTextStart = line.IndexOf("<ruby-text-italic>", i, StringComparison.Ordinal);
                            extraTextEnd   = line.IndexOf("</ruby-text-italic>", i, StringComparison.Ordinal);
                            if (extraTextStart >= 0 || extraTextEnd >= 0 && extraTextStart < extraTextEnd)
                            {
                                extraTextStart += "<ruby-text-italic>".Length;
                                extraText       = line.Substring(extraTextStart, extraTextEnd - extraTextStart);
                            }
                        }

                        var extraTextAfter      = string.Empty;
                        var extraTextStartAfter = line.IndexOf("<ruby-text-after>", i, StringComparison.Ordinal);
                        var extraTextEndAfter   = line.IndexOf("</ruby-text-after>", i, StringComparison.Ordinal);
                        if (extraTextStartAfter >= 0 || extraTextEndAfter >= 0 && extraTextStartAfter < extraTextEndAfter)
                        {
                            extraTextStartAfter += "<ruby-text-after>".Length;
                            extraText            = line.Substring(extraTextStartAfter, extraTextEndAfter - extraTextStartAfter);
                        }

                        var preFurigana = string.Empty;
                        if (actual.Length > 0)
                        {
                            preFurigana = $"{{\\alpha&FF&}}{actual.ToString().TrimEnd()}{{\\alpha&0&}}";
                        }
                        if (!string.IsNullOrWhiteSpace(extraText))
                        {
                            furiganaList.Add(new Paragraph($"{preFurigana}{{\\fs20}}{extraText}", p.StartTime.TotalMilliseconds, p.EndTime.TotalMilliseconds));
                        }
                        if (!string.IsNullOrWhiteSpace(extraTextAfter))
                        {
                            furiganaList.Add(new Paragraph($"{preFurigana}{{\\fs20}} {extraTextAfter}", p.StartTime.TotalMilliseconds, p.EndTime.TotalMilliseconds));
                        }
                        actual.Append(baseText);

                        var endTagEnd = line.IndexOf("</ruby-container>", i, StringComparison.Ordinal);
                        if (endTagEnd < 0)
                        {
                            break;
                        }
                        i      = endTagEnd + "</ruby-container>".Length;
                        rubyOn = true;
                    }
                    else
                    {
                        actual.Append(line.Substring(i, 1));
                        i++;
                    }
                }

                var  actualText    = actual.ToString().TrimEnd();
                bool displayBefore = lines.Count == 2 && index == 0 || lines.Count == 1;
                if (displayBefore && furiganaList.Count > 0)
                {
                    foreach (var fp in furiganaList)
                    {
                        var beforeText = "{\\an1}{\\pos(" + startX + "," + startY + ")}" + fp.Text;
                        list.Add(new Paragraph(beforeText, p.StartTime.TotalMilliseconds, p.EndTime.TotalMilliseconds));
                    }
                    startY += adjustment;
                    if (rubyOn && index == 0 && lines.Count == 2)
                    {
                        startY += 3;
                    }
                }

                actualText = "{\\an1}{\\pos(" + startX + "," + startY + ")}" + actualText;

                list.Add(new Paragraph(actualText, p.StartTime.TotalMilliseconds, p.EndTime.TotalMilliseconds));
                startY += adjustment;

                if (!displayBefore && furiganaList.Count > 0)
                {
                    if (rubyOn && index == 1 && lines.Count == 2)
                    {
                        startY = (int)(startY - adjustment * 0.4);
                    }

                    foreach (var fp in furiganaList)
                    {
                        var beforeText = "{\\an1}{\\pos(" + startX + "," + startY + ")}" + fp.Text;
                        list.Add(new Paragraph(beforeText, p.StartTime.TotalMilliseconds, p.EndTime.TotalMilliseconds));
                    }
                    startY += adjustment;
                }
                furiganaList.Clear();
            }

            return(list);
        }
Exemple #36
0
 public void ShowSubtitle(Nikse.SubtitleEdit.Core.Paragraph p)
 {
     View.ShowSubtitle(p);
 }