From 919d2a4adc874aab7a1dfce80bb0d7d2cfd671b1 Mon Sep 17 00:00:00 2001 From: niksedk Date: Sat, 24 Jul 2021 23:47:53 +0200 Subject: [PATCH] Add support for .dtc subs - thx wesskywalker :) work on #5218 --- src/libse/SubtitleFormats/Drtic.cs | 85 +++++++++++++++++++++ src/libse/SubtitleFormats/MicroDvd.cs | 7 +- src/libse/SubtitleFormats/SubtitleFormat.cs | 1 + 3 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 src/libse/SubtitleFormats/Drtic.cs diff --git a/src/libse/SubtitleFormats/Drtic.cs b/src/libse/SubtitleFormats/Drtic.cs new file mode 100644 index 000000000..9a37b2d4b --- /dev/null +++ b/src/libse/SubtitleFormats/Drtic.cs @@ -0,0 +1,85 @@ +using Nikse.SubtitleEdit.Core.Common; +using System; +using System.Collections.Generic; + +namespace Nikse.SubtitleEdit.Core.SubtitleFormats +{ + public class Drtic : MicroDvd + { + public override string Extension => ".dtc"; + + public override string Name => "Drtic"; + + public override bool IsTimeBased => false; + + public override bool IsMine(List lines, string fileName) + { + var headerFound = false; + var trimmedLines = new List(); + int errors = 0; + foreach (string line in lines) + { + if (!string.IsNullOrWhiteSpace(line)) + { + if (line.StartsWith("[JRT2:", StringComparison.Ordinal)) + { + headerFound = true; + } + else if (line.Contains('{')) + { + string s = RemoveIllegalSpacesAndFixEmptyCodes(line); + if (RegexMicroDvdLine.IsMatch(s)) + { + trimmedLines.Add(s); + } + else + { + errors++; + } + } + else + { + errors++; + } + } + } + + return headerFound && trimmedLines.Count > errors; + } + + private static string RemoveIllegalSpacesAndFixEmptyCodes(string line) + { + if (string.IsNullOrEmpty(line) || line.Length > 2000) + { + return line; + } + + int index = line.IndexOf('}'); + if (index >= 0 && index + 1 < line.Length) + { + index = line.IndexOf('}', index + 1); + if (index >= 0 && index + 1 < line.Length) + { + var indexOfBrackets = line.IndexOf("{}", StringComparison.Ordinal); + if (indexOfBrackets >= 0 && indexOfBrackets < index) + { + line = line.Insert(indexOfBrackets + 1, "0"); // set empty time codes to zero + index++; + } + + while (line.Contains(' ') && line.IndexOf(' ') < index) + { + line = line.Remove(line.IndexOf(' '), 1); + index--; + } + } + } + return line; + } + + public override string ToText(Subtitle subtitle, string title) + { + return $"[JRT2: {subtitle.Paragraphs.Count} 0 ]{Environment.NewLine}{base.ToText(subtitle, title)}{Environment.NewLine}"; + } + } +} diff --git a/src/libse/SubtitleFormats/MicroDvd.cs b/src/libse/SubtitleFormats/MicroDvd.cs index e9351b4e9..2e5016393 100644 --- a/src/libse/SubtitleFormats/MicroDvd.cs +++ b/src/libse/SubtitleFormats/MicroDvd.cs @@ -8,7 +8,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats { public class MicroDvd : SubtitleFormat { - private static readonly Regex RegexMicroDvdLine = new Regex(@"^\{-?\d+}\{-?\d+}.*$", RegexOptions.Compiled); + internal static readonly Regex RegexMicroDvdLine = new Regex(@"^\{-?\d+}\{-?\d+}.*$", RegexOptions.Compiled); public string Errors { get; private set; } private StringBuilder _errors; private int _lineNumber; @@ -39,12 +39,17 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats errors++; } } + else if (line.StartsWith("[JRT2:", StringComparison.Ordinal)) + { + return false; + } else { errors++; } } } + Errors = null; return trimmedLines.Count > errors; } diff --git a/src/libse/SubtitleFormats/SubtitleFormat.cs b/src/libse/SubtitleFormats/SubtitleFormat.cs index 23424ae9b..f00990cb5 100644 --- a/src/libse/SubtitleFormats/SubtitleFormat.cs +++ b/src/libse/SubtitleFormats/SubtitleFormat.cs @@ -69,6 +69,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats new DCinemaSmpte2014(), new DfxpBasic(), new DigiBeta(), + new Drtic(), new DvdStudioPro(), new DvdStudioProSpaceOne(), new DvdStudioProSpaceOneSemicolon(),