SubtitleEdit/libse/SubtitleFormats/TSB4.cs

138 lines
5.2 KiB
C#
Raw Normal View History

2016-02-08 21:11:03 +01:00
using System.Collections.Generic;
using System.Text;
2017-08-01 19:18:56 +02:00
using System;
2016-02-08 21:11:03 +01:00
namespace Nikse.SubtitleEdit.Core.SubtitleFormats
{
/// <summary>
/// SubtitleNext
/// </summary>
2016-02-08 21:11:03 +01:00
public class TSB4 : SubtitleFormat
{
public override string Extension => ".sub";
2016-02-08 21:11:03 +01:00
public override string Name => "TSB4";
2016-02-08 21:11:03 +01:00
public override bool IsTimeBased => true;
2016-02-08 21:11:03 +01:00
public override bool IsMine(List<string> lines, string fileName)
{
var subtitle = new Subtitle();
LoadSubtitle(subtitle, lines, fileName);
2016-06-07 06:31:03 +02:00
return subtitle.Paragraphs.Count > _errorCount;
2016-02-08 21:11:03 +01:00
}
public override string ToText(Subtitle subtitle, string title)
{
return "Not supported!";
}
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
{
_errorCount = 0;
var spaceMode = true;
2016-02-08 21:11:03 +01:00
subtitle.Paragraphs.Clear();
if (string.IsNullOrEmpty(fileName))
{
return;
}
byte[] array;
try
{
array = FileUtil.ReadAllBytesShared(fileName);
}
catch
{
_errorCount++;
return;
}
if (array.Length < 100 || array[0] != 84 || array[1] != 83 || array[2] != 66 || array[3] != 52)
{
return;
}
int codePage = 0; // use default code page if not found
2016-02-08 21:11:03 +01:00
for (int i = 0; i < array.Length - 20; i++)
{
if (array[i] == 67 && array[i + 1] == 80 && array[i + 2] == 65 && array[i + 3] == 71 && array[i + 4] == 4 && array[i + 5] == 0 && array[i + 6] == 0 && array[i + 7] == 0) // CPAG
{
codePage = array[i + 8] + array[i + 9] * 256;
i += 12;
}
2016-02-08 21:11:03 +01:00
if (array[i] == 84 && array[i + 1] == 73 && array[i + 2] == 84 && array[i + 3] == 76 && array[i + 8] == 84 && array[i + 9] == 73 && array[i + 10] == 77 && array[i + 11] == 69) // TITL + TIME
{
if (array[i + 5] != 32)
spaceMode = false;
int endOfText = array[i + 4];
if (!spaceMode)
endOfText += array[i + 5] * 256;
2016-02-08 21:11:03 +01:00
int start = array[i + 16] + array[i + 17] * 256;
if (array[i + 18] != 32 || !spaceMode)
2016-02-08 21:11:03 +01:00
start += array[i + 18] * 256 * 256;
int end = array[i + 20] + array[i + 21] * 256;
if (array[i + 22] != 32 || !spaceMode)
2016-02-08 21:11:03 +01:00
end += array[i + 22] * 256 * 256;
int textStart = i;
while (textStart <= i + endOfText + 4 && !(array[textStart] == 0x4C && array[textStart + 1] == 0x49 && array[textStart + 2] == 0x4E && array[textStart + 3] == 0x45)) // LINE
2016-02-08 21:11:03 +01:00
{
textStart++;
}
int length = array[textStart + 4];
if (array[textStart + 5] != 32 || !spaceMode)
length += array[textStart + 5] * 256;
2016-02-08 21:11:03 +01:00
textStart += 8;
string text = string.Empty;
if (textStart + length > array.Length)
{
if (textStart < array.Length)
text = Encoding.GetEncoding(codePage).GetString(array, textStart, array.Length - textStart);
}
else
{
text = Encoding.GetEncoding(codePage).GetString(array, textStart, length);
}
text = FixItalicsAndBinaryZero(text);
2017-08-01 19:18:56 +02:00
text = string.Join(Environment.NewLine, text.SplitToLines()); //conform to CRLF
text = text.Replace(" " + Environment.NewLine, Environment.NewLine).Trim();
2016-02-08 21:11:03 +01:00
var item = new Paragraph(text, FramesToMilliseconds(start), FramesToMilliseconds(end));
subtitle.Paragraphs.Add(item);
i += endOfText + 5;
}
}
subtitle.RemoveEmptyLines();
subtitle.Renumber();
}
private static string FixItalicsAndBinaryZero(string text)
{
var sb = new StringBuilder(text.Length + 5);
var italicOn = false;
for (int i = 0; i < text.Length; i++)
{
var ch = text[i];
switch (ch)
{
case '\u007f':
sb.Append(italicOn ? "</i>" : "<i>");
italicOn = !italicOn;
break;
case '\0':
sb.Append(" ");
break;
default:
sb.Append(ch);
break;
}
}
return sb.ToString().Trim();
}
2016-02-08 21:11:03 +01:00
}
}