SubtitleEdit/libse/Paragraph.cs

148 lines
4.7 KiB
C#
Raw Normal View History

2016-02-08 21:11:03 +01:00
using System;
namespace Nikse.SubtitleEdit.Core
{
public class Paragraph
{
public int Number { get; set; }
public string Text { get; set; }
public TimeCode StartTime { get; set; }
public TimeCode EndTime { get; set; }
2019-09-29 14:33:26 +02:00
public TimeCode Duration => new TimeCode(EndTime.TotalMilliseconds - StartTime.TotalMilliseconds);
2016-02-08 21:11:03 +01:00
public int StartFrame { get; set; }
public int EndFrame { get; set; }
public bool Forced { get; set; }
public string Extra { get; set; }
public bool IsComment { get; set; }
public string Actor { get; set; }
public string Region { get; set; }
2016-02-08 21:11:03 +01:00
public string MarginL { get; set; }
public string MarginR { get; set; }
public string MarginV { get; set; }
public string Effect { get; set; }
public int Layer { get; set; }
2019-09-29 14:33:26 +02:00
public string Id { get; }
2016-02-08 21:11:03 +01:00
public string Language { get; set; }
public string Style { get; set; }
public bool NewSection { get; set; }
2018-12-25 00:08:23 +01:00
public string Bookmark { get; set; }
2019-09-29 14:33:26 +02:00
public bool IsDefault => Math.Abs(StartTime.TotalMilliseconds) < 0.01 && Math.Abs(EndTime.TotalMilliseconds) < 0.01 && string.IsNullOrEmpty(Text);
2019-09-29 14:33:26 +02:00
private static string GenerateId()
2016-02-08 21:11:03 +01:00
{
return Guid.NewGuid().ToString();
}
public Paragraph() : this(new TimeCode(), new TimeCode(), string.Empty)
2016-02-08 21:11:03 +01:00
{
}
public Paragraph(TimeCode startTime, TimeCode endTime, string text)
{
StartTime = startTime;
EndTime = endTime;
Text = text;
2019-09-29 14:33:26 +02:00
Id = GenerateId();
2016-02-08 21:11:03 +01:00
}
public Paragraph(Paragraph paragraph, bool generateNewId = true)
{
Number = paragraph.Number;
Text = paragraph.Text;
StartTime = new TimeCode(paragraph.StartTime.TotalMilliseconds);
EndTime = new TimeCode(paragraph.EndTime.TotalMilliseconds);
StartFrame = paragraph.StartFrame;
EndFrame = paragraph.EndFrame;
Forced = paragraph.Forced;
Extra = paragraph.Extra;
IsComment = paragraph.IsComment;
Actor = paragraph.Actor;
Region = paragraph.Region;
2016-02-08 21:11:03 +01:00
MarginL = paragraph.MarginL;
MarginR = paragraph.MarginR;
MarginV = paragraph.MarginV;
Effect = paragraph.Effect;
Layer = paragraph.Layer;
2019-09-29 14:33:26 +02:00
Id = generateNewId ? GenerateId() : paragraph.Id;
2016-02-08 21:11:03 +01:00
Language = paragraph.Language;
Style = paragraph.Style;
NewSection = paragraph.NewSection;
2018-12-25 00:08:23 +01:00
Bookmark = paragraph.Bookmark;
2016-02-08 21:11:03 +01:00
}
public Paragraph(int startFrame, int endFrame, string text) :
this(new TimeCode(), new TimeCode(), text)
2016-02-08 21:11:03 +01:00
{
StartFrame = startFrame;
EndFrame = endFrame;
}
public Paragraph(string text, double startTotalMilliseconds, double endTotalMilliseconds)
: this(new TimeCode(startTotalMilliseconds), new TimeCode(endTotalMilliseconds), text)
2016-02-08 21:11:03 +01:00
{
}
public void Adjust(double factor, double adjustmentInSeconds)
{
if (StartTime.IsMaxTime)
2019-01-19 14:40:37 +01:00
{
2016-02-08 21:11:03 +01:00
return;
2019-01-19 14:40:37 +01:00
}
2016-02-08 21:11:03 +01:00
2019-09-29 14:33:26 +02:00
StartTime.TotalMilliseconds = StartTime.TotalMilliseconds * factor + adjustmentInSeconds * TimeCode.BaseUnit;
EndTime.TotalMilliseconds = EndTime.TotalMilliseconds * factor + adjustmentInSeconds * TimeCode.BaseUnit;
2016-02-08 21:11:03 +01:00
}
public void CalculateFrameNumbersFromTimeCodes(double frameRate)
{
2019-09-29 14:33:26 +02:00
StartFrame = (int)Math.Round(StartTime.TotalMilliseconds / TimeCode.BaseUnit * frameRate);
EndFrame = (int)Math.Round(EndTime.TotalMilliseconds / TimeCode.BaseUnit * frameRate);
2016-02-08 21:11:03 +01:00
}
public void CalculateTimeCodesFromFrameNumbers(double frameRate)
{
StartTime.TotalMilliseconds = StartFrame * (TimeCode.BaseUnit / frameRate);
EndTime.TotalMilliseconds = EndFrame * (TimeCode.BaseUnit / frameRate);
}
public override string ToString()
{
2019-09-29 14:33:26 +02:00
return $"{StartTime} --> {EndTime} {Text}";
2016-02-08 21:11:03 +01:00
}
2019-09-29 14:33:26 +02:00
public int NumberOfLines => Utilities.GetNumberOfLines(Text);
2016-02-08 21:11:03 +01:00
public double WordsPerMinute
{
get
{
if (string.IsNullOrEmpty(Text))
2019-01-19 14:40:37 +01:00
{
2016-02-08 21:11:03 +01:00
return 0;
2019-01-19 14:40:37 +01:00
}
2019-09-29 14:33:26 +02:00
return 60.0 / Duration.TotalSeconds * Text.CountWords();
2016-02-08 21:11:03 +01:00
}
}
}
}