SubtitleEdit/libse/RulesProfile.cs

130 lines
7.7 KiB
C#
Raw Normal View History

using System;
2019-03-15 05:49:06 +01:00
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using Nikse.SubtitleEdit.Core.Enums;
2019-03-15 05:49:06 +01:00
using Nikse.SubtitleEdit.Core.SubtitleFormats;
namespace Nikse.SubtitleEdit.Core
{
public class RulesProfile
{
public Guid Id { get; set; }
public string Name { get; set; }
public int SubtitleLineMaximumLength { get; set; }
public decimal SubtitleOptimalCharactersPerSeconds { get; set; }
public decimal SubtitleMaximumWordsPerMinute { get; set; }
public decimal SubtitleMaximumCharactersPerSeconds { get; set; }
public int SubtitleMinimumDisplayMilliseconds { get; set; }
public int SubtitleMaximumDisplayMilliseconds { get; set; }
public int MinimumMillisecondsBetweenLines { get; set; }
public bool CpsIncludesSpace { get; set; }
public int MaxNumberOfLines { get; set; }
public int MergeLinesShorterThan { get; set; }
public DialogType DialogStyle { get; set; }
2020-04-01 19:27:20 +02:00
public ContinuationStyle ContinuationStyle { get; set; }
public RulesProfile()
{
Id = Guid.NewGuid();
DialogStyle = DialogType.DashBothLinesWithSpace;
2020-04-01 19:27:20 +02:00
ContinuationStyle = ContinuationStyle.NoneLeadingTrailingDots;
}
public RulesProfile(RulesProfile profile)
{
Id = profile.Id;
Name = profile.Name;
SubtitleLineMaximumLength = profile.SubtitleLineMaximumLength;
SubtitleOptimalCharactersPerSeconds = profile.SubtitleOptimalCharactersPerSeconds;
SubtitleMaximumWordsPerMinute = profile.SubtitleMaximumWordsPerMinute;
SubtitleMaximumCharactersPerSeconds = profile.SubtitleMaximumCharactersPerSeconds;
SubtitleMinimumDisplayMilliseconds = profile.SubtitleMinimumDisplayMilliseconds;
SubtitleMaximumDisplayMilliseconds = profile.SubtitleMaximumDisplayMilliseconds;
MinimumMillisecondsBetweenLines = profile.MinimumMillisecondsBetweenLines;
CpsIncludesSpace = profile.CpsIncludesSpace;
MaxNumberOfLines = profile.MaxNumberOfLines;
MergeLinesShorterThan = profile.MergeLinesShorterThan;
DialogStyle = profile.DialogStyle;
2020-04-01 19:27:20 +02:00
ContinuationStyle = profile.ContinuationStyle;
}
2019-03-15 05:49:06 +01:00
public static string Serialize(List<RulesProfile> profiles)
{
int count = 0;
var sb = new StringBuilder();
sb.AppendLine("{\"profiles\":[");
for (int i = 0; i < profiles.Count; i++)
{
var p = profiles[i];
count++;
if (count > 1)
{
sb.Append(",");
}
2020-02-19 21:23:25 +01:00
sb.Append("{\"name\":\"" + Json.EncodeJsonText(p.Name) + "\", " +
2019-03-15 06:11:56 +01:00
"\"maxNumberOfLines\":\"" + p.MaxNumberOfLines.ToString(CultureInfo.InvariantCulture) + "\"," +
2019-03-15 05:49:06 +01:00
"\"cpsIncludesSpace\":\"" + p.CpsIncludesSpace.ToString(CultureInfo.InvariantCulture) + "\"," +
"\"mergeLinesShorterThan\":\"" + p.MergeLinesShorterThan.ToString(CultureInfo.InvariantCulture) + "\"," +
"\"minimumMillisecondsBetweenLines\":\"" + p.MinimumMillisecondsBetweenLines.ToString(CultureInfo.InvariantCulture) + "\"," +
"\"subtitleLineMaximumLength\":\"" + p.SubtitleLineMaximumLength.ToString(CultureInfo.InvariantCulture) + "\"," +
"\"subtitleMaximumCharactersPerSeconds\":\"" + p.SubtitleMaximumCharactersPerSeconds.ToString(CultureInfo.InvariantCulture) + "\"," +
"\"subtitleMaximumDisplayMilliseconds\":\"" + p.SubtitleMaximumDisplayMilliseconds.ToString(CultureInfo.InvariantCulture) + "\"," +
"\"subtitleMaximumWordsPerMinute\":\"" + p.SubtitleMaximumWordsPerMinute.ToString(CultureInfo.InvariantCulture) + "\"," +
"\"subtitleMinimumDisplayMilliseconds\":\"" + p.SubtitleMinimumDisplayMilliseconds.ToString(CultureInfo.InvariantCulture) + "\"," +
2020-02-19 21:23:25 +01:00
"\"subtitleOptimalCharactersPerSeconds\":\"" + p.SubtitleOptimalCharactersPerSeconds.ToString(CultureInfo.InvariantCulture) + "\"," +
2020-04-01 19:27:20 +02:00
"\"dialogStyle\":\"" + p.DialogStyle + "\"," +
"\"continuationStyle\":\"" + p.ContinuationStyle + "\"" +
2019-03-15 05:49:06 +01:00
"}");
}
sb.AppendLine("]}");
return sb.ToString();
}
public static List<RulesProfile> Deserialize(string input)
{
var list = new List<RulesProfile>();
var s = input.Trim();
var profiles = Json.ReadObjectArray(s.Substring(s.IndexOf('[')).TrimEnd('}'));
if (profiles == null || profiles.Count == 0)
{
return list;
}
foreach (var p in profiles)
{
var name = Json.DecodeJsonText(Json.ReadTag(p, "name"));
var maxNumberOfLines = Convert.ToInt32(Json.ReadTag(p, "maxNumberOfLines"), CultureInfo.InvariantCulture);
var cpsIncludesSpace = Convert.ToBoolean(Json.ReadTag(p, "cpsIncludesSpace"), CultureInfo.InvariantCulture);
var mergeLinesShorterThan = Convert.ToInt32(Json.ReadTag(p, "mergeLinesShorterThan"), CultureInfo.InvariantCulture);
var minimumMillisecondsBetweenLines = Convert.ToInt32(Json.ReadTag(p, "minimumMillisecondsBetweenLines"), CultureInfo.InvariantCulture);
var subtitleLineMaximumLength = Convert.ToInt32(Json.ReadTag(p, "subtitleLineMaximumLength"), CultureInfo.InvariantCulture);
var subtitleMaximumCharactersPerSeconds = Convert.ToDecimal(Json.ReadTag(p, "subtitleMaximumCharactersPerSeconds"), CultureInfo.InvariantCulture);
var subtitleMaximumWordsPerMinute = Convert.ToDecimal(Json.ReadTag(p, "subtitleMaximumWordsPerMinute"), CultureInfo.InvariantCulture);
var subtitleMaximumDisplayMilliseconds = Convert.ToInt32(Json.ReadTag(p, "subtitleMaximumDisplayMilliseconds"), CultureInfo.InvariantCulture);
var subtitleMinimumDisplayMilliseconds = Convert.ToInt32(Json.ReadTag(p, "subtitleMinimumDisplayMilliseconds"), CultureInfo.InvariantCulture);
var subtitleOptimalCharactersPerSeconds = Convert.ToDecimal(Json.ReadTag(p, "subtitleOptimalCharactersPerSeconds"), CultureInfo.InvariantCulture);
var dialogStyle = (DialogType)Enum.Parse(typeof(DialogType), Json.ReadTag(p, "dialogStyle"));
2020-04-01 19:27:20 +02:00
var continuationStyle = (ContinuationStyle)Enum.Parse(typeof(ContinuationStyle), Json.ReadTag(p, "continuationStyle"));
2019-03-15 05:49:06 +01:00
list.Add(new RulesProfile
{
Name = name,
MaxNumberOfLines = maxNumberOfLines,
CpsIncludesSpace = cpsIncludesSpace,
MergeLinesShorterThan = mergeLinesShorterThan,
MinimumMillisecondsBetweenLines = minimumMillisecondsBetweenLines,
SubtitleLineMaximumLength = subtitleLineMaximumLength,
SubtitleMaximumCharactersPerSeconds = subtitleMaximumCharactersPerSeconds,
SubtitleMaximumWordsPerMinute = subtitleMaximumWordsPerMinute,
SubtitleMaximumDisplayMilliseconds = subtitleMaximumDisplayMilliseconds,
SubtitleMinimumDisplayMilliseconds = subtitleMinimumDisplayMilliseconds,
SubtitleOptimalCharactersPerSeconds = subtitleOptimalCharactersPerSeconds,
2020-04-01 19:27:20 +02:00
DialogStyle = dialogStyle,
ContinuationStyle = continuationStyle
2019-03-15 05:49:06 +01:00
});
}
return list;
}
}
}