diff --git a/src/Logic/SubtitleFormats/Spruce.cs b/src/Logic/SubtitleFormats/Spruce.cs index 5be753f8d..7d4cb4d68 100644 --- a/src/Logic/SubtitleFormats/Spruce.cs +++ b/src/Logic/SubtitleFormats/Spruce.cs @@ -107,7 +107,6 @@ $ColorIndex4 = 3 //00:01:54:19,00:01:56:17,We should be thankful|they accepted our offer. 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); - var regexTimeCodes2 = 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 (regexTimeCodes.IsMatch(line)) @@ -125,21 +124,6 @@ $ColorIndex4 = 3 _errorCount++; } } - else if (regexTimeCodes2.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 (line.Trim().Length > 0 && !line.StartsWith("//") && !line.StartsWith("$")) { _errorCount++; diff --git a/src/Logic/SubtitleFormats/SpruceWithSpace.cs b/src/Logic/SubtitleFormats/SpruceWithSpace.cs new file mode 100644 index 000000000..d56e9171b --- /dev/null +++ b/src/Logic/SubtitleFormats/SpruceWithSpace.cs @@ -0,0 +1,154 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Text.RegularExpressions; + +namespace Nikse.SubtitleEdit.Logic.SubtitleFormats +{ + class SpruceWithSpace : SubtitleFormat + { + public override string Extension + { + get { return ".stl"; } + } + + public override string Name + { + get { return "Spruce Subtitle With Space"; } + } + + public override bool HasLineNumber + { + get { return false; } + } + + public override bool IsTimeBased + { + get { return true; } + } + + public override bool IsMine(List lines, string fileName) + { + var subtitle = new Subtitle(); + LoadSubtitle(subtitle, lines, fileName); + return subtitle.Paragraphs.Count > _errorCount; + } + + public override string ToText(Subtitle subtitle, string title) + { + const string Header = @"$FontName = Arial +$FontSize = 34 +$HorzAlign = Left +$VertAlign = Bottom +$XOffset = 0 +$YOffset = 0 +$Bold = FALSE +$UnderLined = FALSE +$Italic = FALSE +$TextContrast = 15 +$Outline1Contrast = 15 +$Outline2Contrast = 15 +$BackgroundContrast = 0 +$ForceDisplay = FALSE +$FadeIn = 0 +$FadeOut = 0 +$TapeOffset = FALSE + +\\Colour 0 = Black +\\Colour 1 = Red +\\Colour 2 = Green +\\Colour 3 = Yellow +\\Colour 4 = Blue +\\Colour 5 = Magenta +\\Colour 6 = Cyan +\\Colour 7 = White +"; + StringBuilder sb = new StringBuilder(); + sb.AppendLine(Header); + foreach (Paragraph p in subtitle.Paragraphs) + { + sb.AppendLine(string.Format("$HorzAlign = Center\r\n{0}, {1}, {2}", EncodeTimeCode(p.StartTime), EncodeTimeCode(p.EndTime), EncodeText(p.Text))); + } + return sb.ToString(); + } + + private string EncodeText(string text) + { + text = text.Replace("", "^B"); + text = text.Replace("", string.Empty); + text = text.Replace("", "^I"); + text = text.Replace("", string.Empty); + text = text.Replace("", "^U"); + text = text.Replace("", string.Empty); + return text.Replace(Environment.NewLine, "|"); + } + + private string EncodeTimeCode(TimeCode time) + { + //00:01:54:19 + + int frames = (int) (time.Milliseconds / (1000.0 / Configuration.Settings.General.CurrentFrameRate)); + + return string.Format("{0:00}:{1:00}:{2:00}:{3:00}", time.Hours, time.Minutes, time.Seconds, frames); + } + + public override void LoadSubtitle(Subtitle subtitle, List lines, string fileName) + { + //00:01:54:19,00:01:56:17,We should be thankful|they accepted our offer. + 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); + foreach (string line in lines) + { + if (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 (line.Trim().Length > 0 && !line.StartsWith("//") && !line.StartsWith("$")) + { + _errorCount++; + } + } + subtitle.Renumber(1); + } + + private TimeCode DecodeTimeCode(string time) + { + //00:01:54:19 + + string hour = time.Substring(0, 2); + string minutes = time.Substring(3, 2); + string seconds = time.Substring(6, 2); + string frames = time.Substring(9, 2); + + int milliseconds = (int)((1000.0 / Configuration.Settings.General.CurrentFrameRate) * int.Parse(frames)); + if (milliseconds > 999) + milliseconds = 999; + + TimeCode tc = new TimeCode(int.Parse(hour), int.Parse(minutes), int.Parse(seconds), milliseconds); + return tc; + } + + private string DecodeText(string text) + { //TODO: improve end tags + text = text.Replace("|", Environment.NewLine); + if (text.Contains("^B")) + text = text.Replace("^B", "") + ""; + if (text.Contains("^I")) + text = text.Replace("^I", "") + ""; + if (text.Contains("^U")) + text = text.Replace("^U", "") + ""; + return text; + } + } +} diff --git a/src/Logic/SubtitleFormats/SubtitleFormat.cs b/src/Logic/SubtitleFormats/SubtitleFormat.cs index cb72b0ced..522a5b66b 100644 --- a/src/Logic/SubtitleFormats/SubtitleFormat.cs +++ b/src/Logic/SubtitleFormats/SubtitleFormat.cs @@ -48,6 +48,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats new SubViewer20(), new Sami(), new Spruce(), + new SpruceWithSpace(), new SubtitleEditorProject(), new TimedText(), new TimedText10(), diff --git a/src/SubtitleEdit.csproj b/src/SubtitleEdit.csproj index b031f9eb0..ce858561c 100644 --- a/src/SubtitleEdit.csproj +++ b/src/SubtitleEdit.csproj @@ -560,6 +560,7 @@ +