SubtitleEdit/libse/Forms/DurationsBridgeGaps.cs

52 lines
2.0 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
namespace Nikse.SubtitleEdit.Core.Forms
{
public static class DurationsBridgeGaps
{
2019-02-17 16:59:02 +01:00
public static int BridgeGaps(Subtitle subtitle, int minMsBetweenLines, bool divideEven, double maxMs, List<int> fixedIndexes, Dictionary<string, string> dic)
{
2019-02-17 16:59:02 +01:00
if (minMsBetweenLines > maxMs)
{
2019-02-17 16:59:02 +01:00
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)
{
2019-02-17 16:59:02 +01:00
continue;
}
2019-02-17 16:59:02 +01:00
// next paragraph start-time will be pull to try to meet the current parragraph
if (divideEven)
{
2019-02-17 16:59:02 +01:00
next.StartTime.TotalMilliseconds -= currentGaps / 2.0;
}
2019-02-17 16:59:02 +01:00
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}");
}
2019-02-17 16:59:02 +01:00
return fixedIndexes.Count / 2;
}
}
}