SubtitleEdit/libse/SubtitleFormats/Spruce.cs

215 lines
7.8 KiB
C#
Raw Normal View History

2016-02-08 21:11:03 +01:00
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace Nikse.SubtitleEdit.Core.SubtitleFormats
{
public class Spruce : SubtitleFormat
{
private const string Italic = "^I";
private const string Bold = "^B";
private const string Underline = "^U";
2016-04-09 18:16:39 +02:00
private static readonly Regex RegexTimeCodes1 = new Regex(@"^\d\d:\d\d:\d\d:\d\d,\d\d:\d\d:\d\d:\d\d,.+", RegexOptions.Compiled);
private static readonly Regex RegexTimeCodes2 = new Regex(@"^\d\d:\d\d:\d\d:\d\d,\d\d:\d\d:\d\d:\d\d,", RegexOptions.Compiled); // Matches time-only.
2017-08-03 12:43:52 +02:00
public override string Extension => ".stl";
2016-02-08 21:11:03 +01:00
2017-08-03 12:43:52 +02:00
public override string Name => "Spruce Subtitle File";
2016-02-08 21:11:03 +01:00
public override string ToText(Subtitle subtitle, string title)
{
2016-06-07 06:31:03 +02:00
const string header = @"//Font select and font size
2016-02-08 21:11:03 +01:00
$FontName = Arial
$FontSize = 30
//Character attributes (global)
$Bold = FALSE
$UnderLined = FALSE
$Italic = FALSE
//Position Control
$HorzAlign = Center
$VertAlign = Bottom
$XOffset = 0
$YOffset = 0
//Contrast Control
$TextContrast = 15
$Outline1Contrast = 8
$Outline2Contrast = 15
$BackgroundContrast = 0
//Effects Control
$ForceDisplay = FALSE
$FadeIn = 0
$FadeOut = 0
//Other Controls
$TapeOffset = FALSE
//$SetFilePathToken = <<:>>
//Colors
$ColorIndex1 = 0
$ColorIndex2 = 1
$ColorIndex3 = 2
$ColorIndex4 = 3
//Subtitles";
var lastVerticalAlign = "$VertAlign = Bottom";
var lastHorizontalcalAlign = "$HorzAlign = Center";
2016-04-09 18:16:39 +02:00
var sb = new StringBuilder();
2016-06-07 06:31:03 +02:00
sb.AppendLine(header);
2016-02-08 21:11:03 +01:00
foreach (Paragraph p in subtitle.Paragraphs)
{
DvdStudioPro.ToTextAlignment(p, sb, ref lastVerticalAlign, ref lastHorizontalcalAlign);
sb.AppendLine($"{EncodeTimeCode(p.StartTime)},{EncodeTimeCode(p.EndTime)},{EncodeText(p.Text)}");
2016-02-08 21:11:03 +01:00
}
return sb.ToString();
}
private static string EncodeText(string input)
2016-02-08 21:11:03 +01:00
{
var text = HtmlUtil.FixUpperTags(input);
2016-04-09 18:16:39 +02:00
bool allItalic = text.StartsWith("<i>", StringComparison.Ordinal) && text.EndsWith("</i>", StringComparison.Ordinal) && Utilities.CountTagInText(text, "<i>") == 1;
2016-02-08 21:11:03 +01:00
text = text.Replace("<b>", Bold);
text = text.Replace("</b>", Bold);
text = text.Replace("<i>", Italic);
text = text.Replace("</i>", Italic);
text = text.Replace("<u>", Underline);
text = text.Replace("</u>", Underline);
text = HtmlUtil.RemoveHtmlTags(text, true);
2016-02-08 21:11:03 +01:00
if (allItalic)
2019-01-19 14:40:37 +01:00
{
2016-02-08 21:11:03 +01:00
return text.Replace(Environment.NewLine, "|^I");
2019-01-19 14:40:37 +01:00
}
2016-02-08 21:11:03 +01:00
return text.Replace(Environment.NewLine, "|");
}
private static string EncodeTimeCode(TimeCode time)
{
//00:01:54:19
2018-12-28 21:22:34 +01:00
int frames = MillisecondsToFramesMaxFrameRate(time.Milliseconds);
return $"{time.Hours:00}:{time.Minutes:00}:{time.Seconds:00}:{frames:00}";
2016-02-08 21:11:03 +01:00
}
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
{
//00:01:54:19,00:01:56:17,We should be thankful|they accepted our offer.
_errorCount = 0;
subtitle.Paragraphs.Clear();
2016-04-09 18:16:39 +02:00
// Copy reference of static compiled regex (RegexTimeCodes1).
Regex timeCodeRegex = RegexTimeCodes1;
2016-02-08 21:11:03 +01:00
if (fileName != null && fileName.EndsWith(".stl", StringComparison.OrdinalIgnoreCase)) // allow empty text if extension is ".stl"...
2019-01-19 14:40:37 +01:00
{
2016-04-09 18:16:39 +02:00
timeCodeRegex = RegexTimeCodes2;
2019-01-19 14:40:37 +01:00
}
2016-04-09 18:16:39 +02:00
var verticalAlign = "$VertAlign=Bottom";
var horizontalAlign = "$HorzAlign=Center";
2016-02-08 21:11:03 +01:00
foreach (string line in lines)
{
2016-04-09 18:16:39 +02:00
if (line.IndexOf(':') == 2 && timeCodeRegex.IsMatch(line))
2016-02-08 21:11:03 +01:00
{
string start = line.Substring(0, 11);
string end = line.Substring(12, 11);
try
{
var text = DecodeText(line.Substring(24));
text = DvdStudioPro.GetAlignment(verticalAlign, horizontalAlign) + text;
Paragraph p = new Paragraph(DecodeTimeCode(start), DecodeTimeCode(end), text);
2016-02-08 21:11:03 +01:00
subtitle.Paragraphs.Add(p);
}
catch
{
_errorCount++;
}
}
else if (line.TrimStart().StartsWith("$VertAlign", StringComparison.OrdinalIgnoreCase))
{
verticalAlign = line.RemoveChar(' ').RemoveChar('\t');
}
else if (line.TrimStart().StartsWith("$HorzAlign", StringComparison.OrdinalIgnoreCase))
{
horizontalAlign = line.RemoveChar(' ').RemoveChar('\t');
}
2016-04-09 18:16:39 +02:00
else if (!string.IsNullOrWhiteSpace(line) && !line.StartsWith("//", StringComparison.Ordinal) && !line.StartsWith('$'))
2016-02-08 21:11:03 +01:00
{
_errorCount++;
}
}
subtitle.Renumber();
}
private static TimeCode DecodeTimeCode(string time)
{
//00:01:54:19
string hour = time.Substring(0, 2);
string minutes = time.Substring(3, 2);
string seconds = time.Substring(6, 2);
string frames = time.Substring(9, 2);
int milliseconds = FramesToMillisecondsMax999(int.Parse(frames));
2016-04-09 18:16:39 +02:00
return new TimeCode(int.Parse(hour), int.Parse(minutes), int.Parse(seconds), milliseconds);
2016-02-08 21:11:03 +01:00
}
2019-02-02 12:42:30 +01:00
private static string DecodeText(string input)
2016-02-08 21:11:03 +01:00
{
2019-02-02 12:42:30 +01:00
var text = input.Replace("|", Environment.NewLine);
2016-02-08 21:11:03 +01:00
//^IBrillstein^I
if (text.Contains(Bold))
{
text = DecoderTextExtension(text, Bold, "<b>");
}
if (text.Contains(Italic))
{
text = DecoderTextExtension(text, Italic, "<i>");
}
if (text.Contains(Underline))
{
text = DecoderTextExtension(text, Underline, "<u>");
}
return text;
}
2019-02-02 12:42:30 +01:00
private static string DecoderTextExtension(string input, string spruceTag, string htmlOpenTag)
2016-02-08 21:11:03 +01:00
{
var htmlCloseTag = htmlOpenTag.Insert(1, "/");
2019-02-02 12:42:30 +01:00
var text = input;
2016-06-07 06:31:03 +02:00
var idx = text.IndexOf(spruceTag, StringComparison.Ordinal);
var c = Utilities.CountTagInText(text, spruceTag);
2016-02-08 21:11:03 +01:00
if (c == 1)
{
2016-06-07 06:31:03 +02:00
var l = idx + spruceTag.Length;
2016-02-08 21:11:03 +01:00
if (l < text.Length)
{
2016-06-07 06:31:03 +02:00
text = text.Replace(spruceTag, htmlOpenTag) + htmlCloseTag;
2016-02-08 21:11:03 +01:00
}
else if (l == text.Length) // Brillstein^I
{
text = text.Remove(text.Length - Italic.Length);
}
}
else if (c > 1)
{
var isOpen = true;
while (idx >= 0)
{
var htmlTag = isOpen ? htmlOpenTag : htmlCloseTag;
2016-06-07 06:31:03 +02:00
text = text.Remove(idx, spruceTag.Length).Insert(idx, htmlTag);
2016-02-08 21:11:03 +01:00
isOpen = !isOpen;
2016-06-07 06:31:03 +02:00
idx = text.IndexOf(spruceTag, idx + htmlTag.Length, StringComparison.Ordinal);
2016-02-08 21:11:03 +01:00
}
}
return text;
}
}
}