SubtitleEdit/libse/Forms/DurationsBridgeGaps.cs
2019-02-17 16:02:10 +00:00

52 lines
2.0 KiB
C#

using System;
using System.Collections.Generic;
namespace Nikse.SubtitleEdit.Core.Forms
{
public static class DurationsBridgeGaps
{
public static int BridgeGaps(Subtitle subtitle, int minMsBetweenLines, bool divideEven, double maxMs, List<int> fixedIndexes, Dictionary<string, string> dic)
{
if (minMsBetweenLines > maxMs)
{
string message = $"{nameof(DurationsBridgeGaps)}: {nameof(minMsBetweenLines)} cannot be smaller than {nameof(maxMs)}!";
SeLogger.Error(new InvalidOperationException(message), message);
return 0;
}
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;
}
}
}