diff --git a/libse/Forms/DurationsBridgeGaps.cs b/libse/Forms/DurationsBridgeGaps.cs index 10902cebc..a2d514202 100644 --- a/libse/Forms/DurationsBridgeGaps.cs +++ b/libse/Forms/DurationsBridgeGaps.cs @@ -5,48 +5,47 @@ namespace Nikse.SubtitleEdit.Core.Forms { public static class DurationsBridgeGaps { - public static int BridgeGaps(Subtitle fixedSubtitle, int minMsBetweenLines, bool divideEven, double maxMs, List fixedIndexes, Dictionary dic) + public static int BridgeGaps(Subtitle subtitle, int minMsBetweenLines, bool divideEven, double maxMs, List fixedIndexes, Dictionary dic) { - int fixedCount = 0; - for (int i = 0; i < fixedSubtitle.Paragraphs.Count - 1; i++) + if (minMsBetweenLines > maxMs) { - Paragraph cur = fixedSubtitle.Paragraphs[i]; - Paragraph next = fixedSubtitle.Paragraphs[i + 1]; - string before = null; - var difMs = Math.Abs(cur.EndTime.TotalMilliseconds - next.StartTime.TotalMilliseconds); - if (difMs < maxMs && difMs > minMsBetweenLines && maxMs > minMsBetweenLines) - { - before = $"{(next.StartTime.TotalMilliseconds - cur.EndTime.TotalMilliseconds) / TimeCode.BaseUnit:0.000}"; - if (divideEven && next.StartTime.TotalMilliseconds > cur.EndTime.TotalMilliseconds) - { - double half = (next.StartTime.TotalMilliseconds - cur.EndTime.TotalMilliseconds) / 2.0; - next.StartTime.TotalMilliseconds -= half; - } - cur.EndTime.TotalMilliseconds = next.StartTime.TotalMilliseconds - minMsBetweenLines; - if (fixedIndexes != null) - { - fixedIndexes.Add(i); - fixedIndexes.Add(i + 1); - } - fixedCount++; - } - var msToNext = next.StartTime.TotalMilliseconds - cur.EndTime.TotalMilliseconds; - if (msToNext < 2000) - { - string info; - if (!string.IsNullOrEmpty(before)) - { - info = $"{before} => {msToNext / TimeCode.BaseUnit:0.000}"; - } - else - { - info = $"{msToNext / TimeCode.BaseUnit:0.000}"; - } - - dic?.Add(cur.ID, info); - } + string message = $"{nameof(DurationsBridgeGaps)}: {nameof(minMsBetweenLines)} cannot be smaller than {nameof(maxMs)}!"; + SeLogger.Error(new InvalidOperationException(message), message); + return 0; } - return fixedCount; + + int count = subtitle.Paragraphs.Count - 1; + for (int i = 0; i < count; i++) + { + Paragraph cur = subtitle.Paragraphs[i]; + Paragraph next = subtitle.Paragraphs[i + 1]; + + double currentGaps = next.StartTime.TotalMilliseconds - cur.EndTime.TotalMilliseconds; + + // there shouldn't be adjustment if current gaps is shorter than minimum gaps or greater than maximum gaps + if (currentGaps < minMsBetweenLines || currentGaps > maxMs) + { + continue; + } + + // next paragraph start-time will be pull to try to meet the current parragraph + if (divideEven) + { + next.StartTime.TotalMilliseconds -= currentGaps / 2.0; + } + + cur.EndTime.TotalMilliseconds = next.StartTime.TotalMilliseconds - minMsBetweenLines; + if (fixedIndexes != null) + { + fixedIndexes.Add(i); + fixedIndexes.Add(i + 1); + } + + double newGaps = next.StartTime.TotalMilliseconds - cur.EndTime.TotalMilliseconds; + dic?.Add(cur.ID, $"{currentGaps / TimeCode.BaseUnit:0.000} => {newGaps / TimeCode.BaseUnit:0.000}"); + } + + return fixedIndexes.Count / 2; } } } diff --git a/src/Test/Logic/BridgeGapsTest.cs b/src/Test/Logic/BridgeGapsTest.cs new file mode 100644 index 000000000..55884c719 --- /dev/null +++ b/src/Test/Logic/BridgeGapsTest.cs @@ -0,0 +1,45 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Nikse.SubtitleEdit.Core; +using Nikse.SubtitleEdit.Core.Forms; +using System.Collections.Generic; + +namespace Test.Logic +{ + [TestClass] + public class BridgeGapsTest + { + [TestMethod] + public void InvalidMinMaxTest() + { + Assert.AreEqual(0, DurationsBridgeGaps.BridgeGaps(GetSubtitle(), 1000, true, 10, null, null)); + } + + [TestMethod] + public void AdjustGapsTest() + { + var stubDic = new Dictionary(); + var stubList = new List(); + + int result = DurationsBridgeGaps.BridgeGaps(GetSubtitle(), 24, true, 100, stubList, stubDic); + + Assert.AreNotEqual(0, result); + // expedtec to contains both p and p + 1 index of adjusted paragraph + Assert.AreEqual(6, stubList.Count); + // expected to contains only index of adjusted paragraph + Assert.AreEqual(3, stubDic.Count); + } + + public static Subtitle GetSubtitle() + { + var paragraphs = new List + { + new Paragraph("", TimeCode.ParseToMilliseconds("00:00:49,520"), TimeCode.ParseToMilliseconds("00:00:52,390")), + new Paragraph("", TimeCode.ParseToMilliseconds("00:00:52,470"), TimeCode.ParseToMilliseconds("00:00:55,100")), + new Paragraph("", TimeCode.ParseToMilliseconds("00:00:55,180"), TimeCode.ParseToMilliseconds("00:00:57,060")), + new Paragraph("", TimeCode.ParseToMilliseconds("00:00:57,140"), TimeCode.ParseToMilliseconds("00:01:01,100")), + }; + + return new Subtitle(paragraphs); + } + } +} diff --git a/src/Test/Test.csproj b/src/Test/Test.csproj index 801096bb6..4bfde74cc 100644 --- a/src/Test/Test.csproj +++ b/src/Test/Test.csproj @@ -51,6 +51,7 @@ +