|
| 1 | +namespace SubLib.Formats |
| 2 | +{ |
| 3 | + using SubLib.Models; |
| 4 | + using SubLib.NET.Utils; |
| 5 | + using SubLib.Utils; |
| 6 | + using System; |
| 7 | + using System.Text; |
| 8 | + |
| 9 | + public class MacSub : Subtitle |
| 10 | + { |
| 11 | + private enum Expecting |
| 12 | + { |
| 13 | + StartFrame, |
| 14 | + Text, |
| 15 | + EndFrame |
| 16 | + } |
| 17 | + |
| 18 | + public MacSub(string file) : base(file) |
| 19 | + { |
| 20 | + } |
| 21 | + |
| 22 | + public override string Name => "MacSub"; |
| 23 | + |
| 24 | + public override string Extension => ".txt"; |
| 25 | + |
| 26 | + public override bool IsFrameBased => true; |
| 27 | + |
| 28 | + public override void Save(string file, Encoding encoding) |
| 29 | + { |
| 30 | + throw new NotImplementedException(); |
| 31 | + } |
| 32 | + |
| 33 | + public string ToText() |
| 34 | + { |
| 35 | + // Startframe |
| 36 | + // Text |
| 37 | + // Endframe. |
| 38 | + const string writeFormat = "/{0}{3}{1}{3}/{2}{3}"; |
| 39 | + var sb = new StringBuilder(); |
| 40 | + foreach (var p in Paragraphs) |
| 41 | + { |
| 42 | + sb.AppendFormat(writeFormat, TimeUtils.MillisecondsToFrames(p.StartTime.TotalMilliseconds), HtmlUtils.RemoveHtmlTags(p.Text, true), |
| 43 | + TimeUtils.MillisecondsToFrames(p.EndTime.TotalMilliseconds), Environment.NewLine); |
| 44 | + } |
| 45 | + return sb.ToString(); |
| 46 | + } |
| 47 | + |
| 48 | + public override void Load(string[] lines) |
| 49 | + { |
| 50 | + var expecting = Expecting.StartFrame; |
| 51 | + Paragraphs.Clear(); |
| 52 | + _errorCount = 0; |
| 53 | + char[] trimChar = { '/' }; |
| 54 | + var p = new Paragraph(); |
| 55 | + for (int i = 0, lineNumber = 1; i < lines.Length; i++) |
| 56 | + { |
| 57 | + string line = lines[i].Trim(); |
| 58 | + string nextLine = null; |
| 59 | + if (i + 1 < lines.Length) |
| 60 | + { |
| 61 | + nextLine = lines[i + 1].Trim(); |
| 62 | + } |
| 63 | + try |
| 64 | + { |
| 65 | + switch (expecting) |
| 66 | + { |
| 67 | + case Expecting.StartFrame: |
| 68 | + if (ContainsOnlyNumber(line)) |
| 69 | + { |
| 70 | + double ms = TimeUtils.FramesToMilliseconds(int.Parse(line.TrimStart(trimChar))); |
| 71 | + p.StartTime = new TimeCode(ms); |
| 72 | + expecting = Expecting.Text; |
| 73 | + } |
| 74 | + else |
| 75 | + { |
| 76 | + _errorCount++; |
| 77 | + } |
| 78 | + break; |
| 79 | + |
| 80 | + case Expecting.Text: |
| 81 | + line = HtmlUtils.RemoveHtmlTags(line, true); |
| 82 | + p.Text += string.IsNullOrEmpty(p.Text) ? line : Environment.NewLine + line; |
| 83 | + // Next reading is going to be endframe if next line starts with (/) delimeter which indicates frame start. |
| 84 | + if ((nextLine == null) || (nextLine.Length > 0 && nextLine[0] == '/')) |
| 85 | + { |
| 86 | + expecting = Expecting.EndFrame; |
| 87 | + p.Number = lineNumber++; |
| 88 | + } |
| 89 | + break; |
| 90 | + |
| 91 | + case Expecting.EndFrame: |
| 92 | + if (ContainsOnlyNumber(line)) |
| 93 | + { |
| 94 | + double ms = TimeUtils.FramesToMilliseconds(int.Parse(line.TrimStart(trimChar))); |
| 95 | + p.EndTime = new TimeCode(ms); |
| 96 | + Paragraphs.Add(p); |
| 97 | + // Prepare for next reading. |
| 98 | + p = new Paragraph(); |
| 99 | + expecting = Expecting.StartFrame; |
| 100 | + } |
| 101 | + else |
| 102 | + { |
| 103 | + _errorCount++; |
| 104 | + } |
| 105 | + break; |
| 106 | + } |
| 107 | + } |
| 108 | + catch |
| 109 | + { |
| 110 | + _errorCount++; |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + private static bool ContainsOnlyNumber(string input) |
| 116 | + { |
| 117 | + int len = input.Length; |
| 118 | + // 10 = length of int.MaxValue (2147483647); +1 if starts with '/' |
| 119 | + if (len == 0 || len > 11 || input[0] != '/') |
| 120 | + { |
| 121 | + return false; |
| 122 | + } |
| 123 | + int halfLen = len / 2; |
| 124 | + for (int i = 1; i <= halfLen; i++) // /10.0 (Do not parse double) |
| 125 | + { |
| 126 | + if (!(CharUtils.IsDigit(input[i]) && CharUtils.IsDigit(input[len - i]))) |
| 127 | + { |
| 128 | + return false; |
| 129 | + } |
| 130 | + } |
| 131 | + return true; |
| 132 | + } |
| 133 | + |
| 134 | + } |
| 135 | +} |
0 commit comments