|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Text.RegularExpressions; |
| 6 | + |
| 7 | +namespace Nikse.SubtitleEdit.Core.SubtitleFormats |
| 8 | +{ |
| 9 | + public class ImageLogicAutocaption : SubtitleFormat |
| 10 | + { |
| 11 | + private static readonly Regex RegexTimeCode1 = new Regex(@"^\s*\d+\t\d\d:\d\d:\d\d;\d\d", RegexOptions.Compiled); |
| 12 | + |
| 13 | + public override string Extension |
| 14 | + { |
| 15 | + get { return ".rtf"; } |
| 16 | + } |
| 17 | + |
| 18 | + public override string Name |
| 19 | + { |
| 20 | + get { return "Image Logic Autocaption"; } |
| 21 | + } |
| 22 | + |
| 23 | + public override bool IsTimeBased |
| 24 | + { |
| 25 | + get { return true; } |
| 26 | + } |
| 27 | + |
| 28 | + public override bool IsMine(List<string> lines, string fileName) |
| 29 | + { |
| 30 | + var subtitle = new Subtitle(); |
| 31 | + LoadSubtitle(subtitle, lines, fileName); |
| 32 | + return subtitle.Paragraphs.Count > _errorCount; |
| 33 | + } |
| 34 | + |
| 35 | + private static string MakeTimeCode(TimeCode tc) |
| 36 | + { |
| 37 | + return string.Format("{0:00}:{1:00}:{2:00};{3:00}", tc.Hours, tc.Minutes, tc.Seconds, MillisecondsToFramesMaxFrameRate(tc.Milliseconds)); |
| 38 | + } |
| 39 | + |
| 40 | + public override string ToText(Subtitle subtitle, string title) |
| 41 | + { |
| 42 | + var sb = new StringBuilder(); |
| 43 | + sb.AppendLine("#\tAppearance\tCaption\t"); |
| 44 | + sb.AppendLine(); |
| 45 | + int count = 1; |
| 46 | + for (int i = 0; i < subtitle.Paragraphs.Count; i++) |
| 47 | + { |
| 48 | + Paragraph p = subtitle.Paragraphs[i]; |
| 49 | + string text = HtmlUtil.RemoveHtmlTags(p.Text); |
| 50 | + sb.AppendLine(string.Format("{0}\t{1}\t{2}\t", count.ToString().PadLeft(5, ' '), MakeTimeCode(p.StartTime), text)); |
| 51 | + sb.AppendLine("\t\t\t\t"); |
| 52 | + Paragraph next = subtitle.GetParagraphOrDefault(i + 1); |
| 53 | + if (next == null || Math.Abs(p.EndTime.TotalMilliseconds - next.StartTime.TotalMilliseconds) > 50) |
| 54 | + { |
| 55 | + count++; |
| 56 | + sb.AppendLine(string.Format("{0}\t{1}", count.ToString().PadLeft(5, ' '), MakeTimeCode(p.EndTime))); |
| 57 | + } |
| 58 | + count++; |
| 59 | + } |
| 60 | + |
| 61 | + return sb.ToString().ToRtf(); |
| 62 | + } |
| 63 | + |
| 64 | + public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName) |
| 65 | + { |
| 66 | + _errorCount = 0; |
| 67 | + var sb = new StringBuilder(); |
| 68 | + foreach (string line in lines) |
| 69 | + sb.AppendLine(line); |
| 70 | + |
| 71 | + string rtf = sb.ToString().Trim(); |
| 72 | + if (!rtf.StartsWith("{\\rtf")) |
| 73 | + return; |
| 74 | + |
| 75 | + lines = rtf.FromRtf().SplitToLines().ToList(); |
| 76 | + _errorCount = 0; |
| 77 | + Paragraph p = null; |
| 78 | + char[] splitChars = { ':', ';', ',' }; |
| 79 | + foreach (string line in lines) |
| 80 | + { |
| 81 | + string s = line.TrimEnd(); |
| 82 | + if (RegexTimeCode1.IsMatch(s)) |
| 83 | + { |
| 84 | + try |
| 85 | + { |
| 86 | + if (p != null) |
| 87 | + subtitle.Paragraphs.Add(p); |
| 88 | + string[] arr = s.Split('\t'); |
| 89 | + if (arr.Length > 2) |
| 90 | + p = new Paragraph(DecodeTimeCodeFrames(arr[1], splitChars), new TimeCode(0, 0, 0, 0), arr[2].Trim()); |
| 91 | + else |
| 92 | + p = new Paragraph(DecodeTimeCodeFrames(arr[1], splitChars), new TimeCode(0, 0, 0, 0), string.Empty); |
| 93 | + } |
| 94 | + catch |
| 95 | + { |
| 96 | + _errorCount++; |
| 97 | + p = null; |
| 98 | + } |
| 99 | + } |
| 100 | + else if (s.StartsWith("\t\t")) |
| 101 | + { |
| 102 | + if (p != null) |
| 103 | + p.Text = p.Text + Environment.NewLine + s.Trim(); |
| 104 | + } |
| 105 | + else if (!string.IsNullOrWhiteSpace(s)) |
| 106 | + { |
| 107 | + _errorCount++; |
| 108 | + } |
| 109 | + } |
| 110 | + if (p != null) |
| 111 | + subtitle.Paragraphs.Add(p); |
| 112 | + |
| 113 | + for (int j = 0; j < subtitle.Paragraphs.Count - 1; j++) |
| 114 | + { |
| 115 | + p = subtitle.Paragraphs[j]; |
| 116 | + Paragraph next = subtitle.Paragraphs[j + 1]; |
| 117 | + p.EndTime.TotalMilliseconds = next.StartTime.TotalMilliseconds - Configuration.Settings.General.MinimumMillisecondsBetweenLines; |
| 118 | + } |
| 119 | + if (subtitle.Paragraphs.Count > 0) |
| 120 | + { |
| 121 | + p = subtitle.Paragraphs[subtitle.Paragraphs.Count - 1]; |
| 122 | + p.EndTime.TotalMilliseconds = p.StartTime.TotalMilliseconds + Utilities.GetOptimalDisplayMilliseconds(p.Text); |
| 123 | + } |
| 124 | + subtitle.RemoveEmptyLines(); |
| 125 | + subtitle.Renumber(); |
| 126 | + } |
| 127 | + |
| 128 | + } |
| 129 | +} |
0 commit comments