Merge pull request #1083 from ivandrofly/subformat

Refactor (src/Logic/SubtitleFormats) part 6
This commit is contained in:
Nikolaj Olsson 2015-08-01 13:00:52 +02:00
commit 2c5857209e
19 changed files with 160 additions and 151 deletions

View File

@ -33,28 +33,28 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
0x95, // ®
};
private static readonly List<string> LatinLetters = new List<string> {
"♪",
"á",
"é",
"í",
"ó",
"ú",
"â",
"ê",
"î",
"ô",
"û",
"à",
"è",
"Ñ",
"ñ",
"ç",
"¢",
"£",
"¿",
"½",
"®",
private static readonly IList<char> LatinLetters = new List<char> {
'♪',
'á',
'é',
'í',
'ó',
'ú',
'â',
'ê',
'î',
'ô',
'û',
'à',
'è',
'Ñ',
'ñ',
'ç',
'¢',
'£',
'¿',
'½',
'®',
};
public override string Extension
@ -129,36 +129,36 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
//Right : 12 01 00 00 00 00 03 0F 1E
//Left : 12 03 00 00 00 00 03 0F 07
if (text.StartsWith("{\\an7}") || text.StartsWith("{\\an8}") || text.StartsWith("{\\an9}"))
if (text.StartsWith("{\\an7}", StringComparison.Ordinal) || text.StartsWith("{\\an8}", StringComparison.Ordinal) || text.StartsWith("{\\an9}", StringComparison.Ordinal))
{
buffer[7] = 1; // align top (vertial)
bufferShort[3] = 1; // align top (vertial)
}
else if (text.StartsWith("{\\an4}") || text.StartsWith("{\\an5}") || text.StartsWith("{\\an6}"))
else if (text.StartsWith("{\\an4}", StringComparison.Ordinal) || text.StartsWith("{\\an5}", StringComparison.Ordinal) || text.StartsWith("{\\an6}", StringComparison.Ordinal))
{
buffer[7] = 8; // center (vertical)
bufferShort[3] = 8; // align top (vertial)
}
if (text.StartsWith("{\\an7}") || text.StartsWith("{\\an4}") || text.StartsWith("{\\an1}"))
if (text.StartsWith("{\\an7}", StringComparison.Ordinal) || text.StartsWith("{\\an4}", StringComparison.Ordinal) || text.StartsWith("{\\an1}"))
{
buffer[8] = 2; // align left (horizontal)
bufferShort[4] = 2; // align left (horizontal)
}
else if (text.StartsWith("{\\an9}") || text.StartsWith("{\\an6}") || text.StartsWith("{\\an3}"))
else if (text.StartsWith("{\\an9}", StringComparison.Ordinal) || text.StartsWith("{\\an6}", StringComparison.Ordinal) || text.StartsWith("{\\an3}", StringComparison.Ordinal))
{
buffer[8] = 0x1e; // align right (vertical)
bufferShort[4] = 0x1e; // align right (vertical)
}
int startTag = text.IndexOf('}');
if (text.StartsWith("{\\") && startTag > 0 && startTag < 10)
if (text.StartsWith("{\\", StringComparison.Ordinal) && startTag > 0 && startTag < 10)
{
text = text.Remove(0, startTag + 1);
}
var textBytes = new List<byte>();
var italic = p.Text.StartsWith("<i>") && p.Text.EndsWith("</i>");
var italic = p.Text.StartsWith("<i>", StringComparison.Ordinal) && p.Text.EndsWith("</i>", StringComparison.Ordinal);
text = HtmlUtil.RemoveHtmlTags(text);
int j = 0;
if (italic)
@ -177,7 +177,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
}
else
{
int idx = LatinLetters.IndexOf(text.Substring(j, 1));
int idx = LatinLetters.IndexOf(text[j]);
if (idx >= 0)
textBytes.Add((byte)LatinCodes[idx]);
else
@ -278,6 +278,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
int i = 128;
Paragraph last = null;
var sb = new StringBuilder();
while (i < buffer.Length - 20)
{
var p = new Paragraph();
@ -301,9 +302,10 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
p.EndTime = DecodeTimestamp(buffer, i + 6);
var sb = new StringBuilder();
sb.Clear();
int j = 0;
bool italics = false;
var encoding = Encoding.GetEncoding(1252);
while (j < textLength)
{
int index = i + start + j;
@ -329,7 +331,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
}
else
{
sb.Append(Encoding.GetEncoding(1252).GetString(buffer, index, 1));
sb.Append(encoding.GetString(buffer, index, 1));
}
j++;
}

View File

@ -7,7 +7,6 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
{
public class PE2 : SubtitleFormat
{
private static readonly Regex RegexTimeCode = new Regex(@"^\d\d:\d\d:\d\d:\d\d ", RegexOptions.Compiled);
private static readonly Regex RegexTimeCodeEnd = new Regex(@"^\d\d:\d\d:\d\d:\d\d$", RegexOptions.Compiled);
@ -59,13 +58,13 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
//10:00:14:15 We will sanction your loan.
//10:00:16:00
const string paragraphWriteFormat = "{0} {2}{3}{1}";
const string writeFormat = "{0} {2}{3}{1}";
var sb = new StringBuilder();
sb.AppendLine("#PE2 Format file");
foreach (Paragraph p in subtitle.Paragraphs)
{
string text = p.Text.Replace(Environment.NewLine, "//");
sb.AppendLine(string.Format(paragraphWriteFormat, EncodeTimeCode(p.StartTime), EncodeTimeCode(p.EndTime), text, Environment.NewLine));
sb.AppendLine(string.Format(writeFormat, EncodeTimeCode(p.StartTime), EncodeTimeCode(p.EndTime), text, Environment.NewLine));
}
return sb.ToString().Trim();
}
@ -77,11 +76,12 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
_errorCount = 0;
subtitle.Paragraphs.Clear();
char[] splitChar = { ':' };
foreach (string line in lines)
{
if (RegexTimeCode.IsMatch(line))
{
string[] parts = line.Substring(0, 11).Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
string[] parts = line.Substring(0, 11).Split(splitChar, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length == 4)
{
try
@ -105,7 +105,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
}
else if (RegexTimeCodeEnd.IsMatch(line))
{
string[] parts = line.Substring(0, 11).Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
string[] parts = line.Substring(0, 11).Split(splitChar, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length == 4)
{
var tc = DecodeTimeCode(parts);
@ -145,17 +145,17 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
private static string EncodeTimeCode(TimeCode time)
{
return string.Format("{0:00}:{1:00}:{2:00}:{3:00}", time.Hours, time.Minutes, time.Seconds, MillisecondsToFramesMaxFrameRate(time.Milliseconds));
return time.ToHHMMSSFF();
}
private static TimeCode DecodeTimeCode(string[] parts)
{
string hour = parts[0];
string minutes = parts[1];
string seconds = parts[2];
string frames = parts[3];
var hour = int.Parse(parts[0]);
var minutes = int.Parse(parts[1]);
var seconds = int.Parse(parts[2]);
var frames = int.Parse(parts[3]);
return new TimeCode(int.Parse(hour), int.Parse(minutes), int.Parse(seconds), FramesToMillisecondsMax999(int.Parse(frames)));
return new TimeCode(hour, minutes, seconds, FramesToMillisecondsMax999(frames));
}
}

View File

@ -30,7 +30,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
public override string ToText(Subtitle subtitle, string title)
{
StringBuilder sb = new StringBuilder();
var sb = new StringBuilder();
int index = 0;
sb.AppendLine("<Window" + Environment.NewLine +
" Width = \"640\"" + Environment.NewLine +
@ -46,11 +46,11 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
">" + Environment.NewLine +
"<center>" + Environment.NewLine +
"<b>" + Environment.NewLine);
const string writeFormat = "<Time begin=\"{0}\" end=\"{1}\" /><clear/>{2}";
foreach (Paragraph p in subtitle.Paragraphs)
{
//<Time begin="0:03:24.8" end="0:03:29.4" /><clear/>Man stjæler ikke fra Chavo, nej.
sb.AppendLine(string.Format("<Time begin=\"{0}\" end=\"{1}\" /><clear/>{2}", EncodeTimeCode(p.StartTime), EncodeTimeCode(p.EndTime), p.Text.Replace(Environment.NewLine, " ")));
sb.AppendLine(string.Format(writeFormat, EncodeTimeCode(p.StartTime), EncodeTimeCode(p.EndTime), p.Text.Replace(Environment.NewLine, " ")));
index++;
}
sb.AppendLine("</b>");

View File

@ -110,6 +110,8 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
int count = 1;
var sb = new StringBuilder();
sb.AppendLine(header.Replace("_TITLE_", title).Replace("_LANGUAGE-STYLE_", languageStyle));
var total = new StringBuilder();
var partial = new StringBuilder();
foreach (Paragraph p in subtitle.Paragraphs)
{
Paragraph next = subtitle.GetParagraphOrDefault(count);
@ -117,20 +119,19 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
if (text.Contains('<') && text.Contains('>'))
{
var total = new StringBuilder();
var partial = new StringBuilder();
bool tagOn = false;
for (int i = 0; i < text.Length; i++)
{
if (text.Substring(i).StartsWith("<font") ||
text.Substring(i).StartsWith("<div") ||
text.Substring(i).StartsWith("<i") ||
text.Substring(i).StartsWith("<b") ||
text.Substring(i).StartsWith("<s") ||
text.Substring(i).StartsWith("</"))
if (text.Substring(i).StartsWith("<font", StringComparison.Ordinal) ||
text.Substring(i).StartsWith("<div", StringComparison.Ordinal) ||
text.Substring(i).StartsWith("<i", StringComparison.Ordinal) ||
text.Substring(i).StartsWith("<b", StringComparison.Ordinal) ||
text.Substring(i).StartsWith("<s", StringComparison.Ordinal) ||
text.Substring(i).StartsWith("</", StringComparison.Ordinal))
{
total.Append(EncodeText(partial.ToString()));
partial = new StringBuilder();
partial.Clear();
tagOn = true;
total.Append('<');
}
@ -375,13 +376,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
text = WebUtility.HtmlDecode(text);
}
string cleanText = text;
while (cleanText.Contains(" "))
cleanText = cleanText.Replace(" ", " ");
while (cleanText.Contains(Environment.NewLine + " "))
cleanText = cleanText.Replace(Environment.NewLine + " ", Environment.NewLine);
while (cleanText.Contains(" " + Environment.NewLine))
cleanText = cleanText.Replace(" " + Environment.NewLine, Environment.NewLine);
var cleanText = text.FixExtraSpaces();
cleanText = cleanText.Trim();
if (!string.IsNullOrEmpty(p.Text) && !string.IsNullOrEmpty(millisecAsString))
@ -427,7 +422,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
foreach (Paragraph p2 in subtitle.Paragraphs)
{
p2.Text = p2.Text.Replace(Convert.ToChar(160), ' '); // non-breaking space to normal space
p2.Text = p2.Text.Replace('\u00A0', ' '); // non-breaking space to normal space
}
}

View File

@ -4,7 +4,6 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
{
public class SamiModern : Sami
{
public override string Name
{
get { return "SAMI modern"; }

View File

@ -4,7 +4,6 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
{
public class SamiYouTube : Sami
{
public override string Name
{
get { return "SAMI YouTube"; }

View File

@ -16,7 +16,6 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
/// </summary>
public class ScenaristClosedCaptions : SubtitleFormat
{
public class SccPositionAndStyle
{
public Color ForeColor { get; set; }

View File

@ -106,6 +106,10 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
const string syncTag = "<p ";
var syncStartPos = allInputLower.IndexOf(syncTag, StringComparison.Ordinal);
int index = syncStartPos + syncTag.Length;
var tcBegin = new StringBuilder();
var tcEnd = new StringBuilder();
char[] splitChars = { '.', ':' };
while (syncStartPos >= 0)
{
var syncEndPos = allInputLower.IndexOf("</p>", index, StringComparison.Ordinal);
@ -129,7 +133,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
text = text.Replace(Environment.NewLine + Environment.NewLine, Environment.NewLine);
string begin = s.Substring(indexOfBegin + " data-begin=".Length);
var tcBegin = new StringBuilder();
tcBegin.Clear();
for (int i = 0; i <= 10; i++)
{
if (begin.Length > i && @"0123456789:.".Contains(begin[i]))
@ -138,7 +142,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
}
}
var tcEnd = new StringBuilder();
tcEnd.Clear();
var indexOfEnd = s.IndexOf(" data-end=", StringComparison.Ordinal);
if (indexOfEnd >= 0)
{
@ -152,7 +156,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
}
}
var arr = tcBegin.ToString().Split(new[] { '.', ':' }, StringSplitOptions.RemoveEmptyEntries);
var arr = tcBegin.ToString().Split(splitChars, StringSplitOptions.RemoveEmptyEntries);
if (arr.Length == 3 || arr.Length == 4)
{
var p = new Paragraph();
@ -162,7 +166,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
p.StartTime = DecodeTimeCode(arr);
if (tcEnd.Length > 0)
{
arr = tcEnd.ToString().Split(new[] { '.', ':' }, StringSplitOptions.RemoveEmptyEntries);
arr = tcEnd.ToString().Split(splitChars, StringSplitOptions.RemoveEmptyEntries);
p.EndTime = DecodeTimeCode(arr);
}
subtitle.Paragraphs.Add(p);

View File

@ -11,7 +11,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
/// </summary>
public class SoftNicolonSub : SubtitleFormat
{
private static Regex regexTimeCodes = new Regex(@"^\d\d:\d\d:\d\d:\d\d\\\d\d:\d\d:\d\d:\d\d$", RegexOptions.Compiled);
private static readonly Regex regexTimeCodes = new Regex(@"^\d\d:\d\d:\d\d:\d\d\\\d\d:\d\d:\d\d:\d\d$", RegexOptions.Compiled);
public override string Extension
{
@ -38,6 +38,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
public override string ToText(Subtitle subtitle, string title)
{
var sb = new StringBuilder();
var lineSb = new StringBuilder();
sb.AppendLine("*PART 1*");
sb.AppendLine("00:00:00:00\\00:00:00:00");
foreach (Paragraph p in subtitle.Paragraphs)
@ -46,7 +47,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
bool positionTop = false;
// If text starts with {\an8}, subtitle appears at the top
if (text.StartsWith("{\\an8}"))
if (text.StartsWith("{\\an8}", StringComparison.Ordinal))
{
positionTop = true;
// Remove the tag {\an8}.
@ -56,9 +57,9 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
// Split lines (split a subtitle into its lines)
var lines = text.SplitToLines();
int count = 0;
var lineSb = new StringBuilder();
lineSb.Clear();
string tempLine = string.Empty;
Boolean nextLineInItalics = false;
bool nextLineInItalics = false;
foreach (string line in lines)
{
// Append line break in every line except the first one
@ -136,8 +137,10 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
//—Estoy de licencia.
//01:48:50.07\01:48:52.01
var sb = new StringBuilder();
var lineSb = new StringBuilder();
Paragraph p = null;
subtitle.Paragraphs.Clear();
char[] splitChars = { ':', '.' };
foreach (string line in lines)
{
string s = line.Trim();
@ -150,8 +153,8 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
string start = temp[0];
string end = temp[1];
string[] startParts = start.Split(new[] { ':', '.' }, StringSplitOptions.RemoveEmptyEntries);
string[] endParts = end.Split(new[] { ':', '.' }, StringSplitOptions.RemoveEmptyEntries);
string[] startParts = start.Split(splitChars, StringSplitOptions.RemoveEmptyEntries);
string[] endParts = end.Split(splitChars, StringSplitOptions.RemoveEmptyEntries);
if (startParts.Length == 4 && endParts.Length == 4)
{
try
@ -161,7 +164,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
p.EndTime = DecodeTimeCode(endParts);
string text = sb.ToString().Trim();
Boolean positionTop = false;
bool positionTop = false;
// If text starts with "}", subtitle appears at the top
if (text.StartsWith('}'))
{
@ -176,7 +179,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
// Split subtitle lines (one subtitle has one or more lines)
var subtitleLines = text.SplitToLines();
int count = 0;
var lineSb = new StringBuilder();
lineSb.Clear();
string tempLine = string.Empty;
foreach (string subtitleLine in subtitleLines)
{
@ -203,7 +206,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
p.Text = text;
if (text.Length > 0)
subtitle.Paragraphs.Add(p);
sb = new StringBuilder();
sb.Clear();
}
catch (Exception exception)
{
@ -229,13 +232,12 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
private static TimeCode DecodeTimeCode(string[] parts)
{
//00:00:07:12
string hour = parts[0];
string minutes = parts[1];
string seconds = parts[2];
string frames = parts[3];
var hour = int.Parse(parts[0]);
var minutes = int.Parse(parts[1]);
var seconds = int.Parse(parts[2]);
var frames = int.Parse(parts[3]);
TimeCode tc = new TimeCode(int.Parse(hour), int.Parse(minutes), int.Parse(seconds), FramesToMillisecondsMax999(int.Parse(frames)));
return tc;
return new TimeCode(hour, minutes, seconds, FramesToMillisecondsMax999(frames));
}
}

View File

@ -11,7 +11,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
/// </summary>
public class SoftNiSub : SubtitleFormat
{
private static Regex regexTimeCodes = new Regex(@"^\d\d:\d\d:\d\d\.\d\d\\\d\d:\d\d:\d\d\.\d\d$", RegexOptions.Compiled);
private static readonly Regex regexTimeCodes = new Regex(@"^\d\d:\d\d:\d\d\.\d\d\\\d\d:\d\d:\d\d\.\d\d$", RegexOptions.Compiled);
public override string Extension
{
@ -38,15 +38,17 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
public override string ToText(Subtitle subtitle, string title)
{
var sb = new StringBuilder();
var lineSb = new StringBuilder();
sb.AppendLine("*PART 1*");
sb.AppendLine("00:00:00.00\\00:00:00.00");
const string writeFormat = "{0}{1}{2}\\{3}";
foreach (Paragraph p in subtitle.Paragraphs)
{
string text = p.Text;
bool positionTop = false;
// If text starts with {\an8}, subtitle appears at the top
if (text.StartsWith("{\\an8}"))
if (text.StartsWith("{\\an8}", StringComparison.Ordinal))
{
positionTop = true;
// Remove the tag {\an8}.
@ -56,9 +58,9 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
// Split lines (split a subtitle into its lines)
var lines = text.SplitToLines();
int count = 0;
var lineSb = new StringBuilder();
lineSb.Clear();
string tempLine = string.Empty;
Boolean nextLineInItalics = false;
bool nextLineInItalics = false;
foreach (string line in lines)
{
// Append line break in every line except the first one
@ -84,7 +86,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
// Add new italics tag at the beginning
tempLine = "[" + tempLine;
}
else if (tempLine.StartsWith("<i>") && Utilities.CountTagInText(tempLine, "<i>") > Utilities.CountTagInText(tempLine, "</i>"))
else if (tempLine.StartsWith("<i>", StringComparison.Ordinal) && Utilities.CountTagInText(tempLine, "<i>") > Utilities.CountTagInText(tempLine, "</i>"))
{
// Line starts with <i> but italics are not closed. So the next line should be in italics
nextLineInItalics = true;
@ -103,7 +105,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
if (positionTop)
text = "}" + text;
sb.AppendLine(string.Format("{0}{1}{2}\\{3}", text, Environment.NewLine, p.StartTime.ToHHMMSSPeriodFF(), p.EndTime.ToHHMMSSPeriodFF()));
sb.AppendLine(string.Format(writeFormat, text, Environment.NewLine, p.StartTime.ToHHMMSSPeriodFF(), p.EndTime.ToHHMMSSPeriodFF()));
}
sb.AppendLine(@"*END*");
sb.AppendLine(@"...........\...........");
@ -136,8 +138,10 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
//—Estoy de licencia.
//01:48:50.07\01:48:52.01
var sb = new StringBuilder();
var lineSb = new StringBuilder();
Paragraph p = null;
subtitle.Paragraphs.Clear();
char[] splitChars = { ':', '.' };
foreach (string line in lines)
{
string s = line.Trim();
@ -150,8 +154,8 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
string start = temp[0];
string end = temp[1];
string[] startParts = start.Split(new[] { ':', '.' }, StringSplitOptions.RemoveEmptyEntries);
string[] endParts = end.Split(new[] { ':', '.' }, StringSplitOptions.RemoveEmptyEntries);
string[] startParts = start.Split(splitChars, StringSplitOptions.RemoveEmptyEntries);
string[] endParts = end.Split(splitChars, StringSplitOptions.RemoveEmptyEntries);
if (startParts.Length == 4 && endParts.Length == 4)
{
try
@ -161,7 +165,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
p.EndTime = DecodeTimeCode(endParts);
string text = sb.ToString().Trim();
Boolean positionTop = false;
bool positionTop = false;
// If text starts with "}", subtitle appears at the top
if (text.StartsWith('}'))
{
@ -176,7 +180,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
// Split subtitle lines (one subtitle has one or more lines)
var subtitleLines = text.SplitToLines();
int count = 0;
var lineSb = new StringBuilder();
lineSb.Clear();
string tempLine = string.Empty;
foreach (string subtitleLine in subtitleLines)
{
@ -203,7 +207,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
p.Text = text;
if (text.Length > 0)
subtitle.Paragraphs.Add(p);
sb = new StringBuilder();
sb.Clear();
}
catch (Exception exception)
{
@ -229,13 +233,12 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
private static TimeCode DecodeTimeCode(string[] parts)
{
//00:00:07:12
string hour = parts[0];
string minutes = parts[1];
string seconds = parts[2];
string frames = parts[3];
var hour = int.Parse(parts[0]);
var minutes = int.Parse(parts[1]);
var seconds = int.Parse(parts[2]);
var frames = int.Parse(parts[3]);
TimeCode tc = new TimeCode(int.Parse(hour), int.Parse(minutes), int.Parse(seconds), FramesToMillisecondsMax999(int.Parse(frames)));
return tc;
return new TimeCode(hour, minutes, seconds, FramesToMillisecondsMax999(frames));
}
}

View File

@ -27,7 +27,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
{
var subtitle = new Subtitle();
StringBuilder sb = new StringBuilder();
var sb = new StringBuilder();
foreach (string line in lines)
sb.AppendLine(line);
@ -37,11 +37,12 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
public override string ToText(Subtitle subtitle, string title)
{
StringBuilder sb = new StringBuilder();
var sb = new StringBuilder();
int index = 0;
const string writeFormat = "{0:0000}\t{1}\t{2}\t{3}";
foreach (Paragraph p in subtitle.Paragraphs)
{
sb.AppendLine(string.Format("{0:0000}\t{1}\t{2}\t{3}", index + 1, EncodeTimeCode(p.StartTime), EncodeTimeCode(p.EndTime), HtmlUtil.RemoveHtmlTags(p.Text).Replace(Environment.NewLine, "\t")));
sb.AppendLine(string.Format(writeFormat, index + 1, EncodeTimeCode(p.StartTime), EncodeTimeCode(p.EndTime), HtmlUtil.RemoveHtmlTags(p.Text).Replace(Environment.NewLine, "\t")));
index++;
}
return sb.ToString();
@ -50,7 +51,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
private static string EncodeTimeCode(TimeCode time)
{
//00:03:15:22 (last is frame)
return string.Format("{0:00}:{1:00}:{2:00}:{3:00}", time.Hours, time.Minutes, time.Seconds, MillisecondsToFramesMaxFrameRate(time.Milliseconds));
return time.ToHHMMSSFF();
}
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
@ -61,6 +62,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
_errorCount = 0;
var regexTimeCodes = new Regex(@"^\d\d\d\d[\t]+\d\d:\d\d:\d\d:\d\d\t\d\d:\d\d:\d\d:\d\d\t.+\.(tif|tiff|png|bmp|TIF|TIFF|PNG|BMP)", RegexOptions.Compiled);
int index = 0;
char[] splitChar = { ':' };
foreach (string line in lines)
{
if (regexTimeCodes.IsMatch(line))
@ -69,8 +71,8 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
string start = temp.Substring(5, 11);
string end = temp.Substring(12 + 5, 11);
string[] startParts = start.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
string[] endParts = end.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
string[] startParts = start.Split(splitChar, StringSplitOptions.RemoveEmptyEntries);
string[] endParts = end.Split(splitChar, StringSplitOptions.RemoveEmptyEntries);
if (startParts.Length == 4 && endParts.Length == 4)
{
int lastIndexOfTab = line.LastIndexOf('\t');
@ -97,13 +99,12 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
private static TimeCode DecodeTimeCode(string[] parts)
{
//00:00:07:12
string hour = parts[0];
string minutes = parts[1];
string seconds = parts[2];
string frames = parts[3];
var hour = int.Parse(parts[0]);
var minutes = int.Parse(parts[1]);
var seconds = int.Parse(parts[2]);
var frames = int.Parse(parts[3]);
TimeCode tc = new TimeCode(int.Parse(hour), int.Parse(minutes), int.Parse(seconds), FramesToMillisecondsMax999(int.Parse(frames)));
return tc;
return new TimeCode(hour, minutes, seconds, FramesToMillisecondsMax999(frames));
}
}

View File

@ -42,10 +42,11 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
{
var sb = new StringBuilder();
int index = 0;
const string writeFormat = "{0:0000} {1} {2} {3}";
foreach (Paragraph p in subtitle.Paragraphs)
{
//0001 00:49:26:22 00:49:27:13 t01_v001c001_22_0001.bmp
sb.AppendLine(string.Format("{0:0000} {1} {2} {3}", index + 1, EncodeTimeCode(p.StartTime), EncodeTimeCode(p.EndTime), HtmlUtil.RemoveHtmlTags(p.Text).Replace(Environment.NewLine, "\t")));
sb.AppendLine(string.Format(writeFormat, index + 1, EncodeTimeCode(p.StartTime), EncodeTimeCode(p.EndTime), HtmlUtil.RemoveHtmlTags(p.Text).Replace(Environment.NewLine, "\t")));
index++;
}
return sb.ToString();
@ -65,17 +66,19 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
subtitle.Paragraphs.Clear();
_errorCount = 0;
int index = 0;
char[] splitChar = { ' ' };
var sb = new StringBuilder();
foreach (string line in lines)
{
if (regexTimeCodes.IsMatch(line))
{
string temp = line.Substring(0, regexTimeCodes.Match(line).Length);
string[] parts = temp.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
string[] parts = temp.Split(splitChar, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length > 3)
{
var sb = new StringBuilder();
sb.Clear();
for (int i = 3; i < parts.Length; i++)
sb.Append(parts[i] + " ");
string text = sb.ToString().Trim();
@ -83,7 +86,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
subtitle.Paragraphs.Add(p);
}
}
else if (string.IsNullOrWhiteSpace(line) || line.StartsWith("Display_Area") || line.StartsWith('#') || line.StartsWith("Color") || index < 10)
else if (string.IsNullOrWhiteSpace(line) || line.StartsWith("Display_Area", StringComparison.Ordinal) || line.StartsWith('#') || line.StartsWith("Color", StringComparison.Ordinal) || index < 10)
{
// skip these lines
}
@ -99,16 +102,15 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
private static TimeCode DecodeTimeCode(string[] parts)
{
//00:00:07:12
string hour = parts[0];
string minutes = parts[1];
string seconds = parts[2];
string frames = parts[3];
var hour = int.Parse(parts[0]);
var minutes = int.Parse(parts[1]);
var seconds = int.Parse(parts[2]);
var frames = double.Parse(parts[3]);
int milliseconds = FramesToMillisecondsMax999(double.Parse(frames));
int milliseconds = FramesToMillisecondsMax999(frames);
if (milliseconds > 999)
milliseconds = 999;
return new TimeCode(int.Parse(hour), int.Parse(minutes), int.Parse(seconds), milliseconds);
return new TimeCode(hour, minutes, seconds, milliseconds);
}
}

View File

@ -35,12 +35,12 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
public override string ToText(Subtitle subtitle, string title)
{
var sb = new StringBuilder();
const string writeFormat = "{0:00}:{1:00}:{2:00}:{3:00} - {4:00}:{5:00}:{6:00}:{7:00} \t{8}";
foreach (Paragraph p in subtitle.Paragraphs)
{
string text = HtmlUtil.RemoveHtmlTags(p.Text);
var text = HtmlUtil.RemoveHtmlTags(p.Text, true);
text = text.Replace(Environment.NewLine, "\r");
sb.AppendLine(string.Format("{0:00}:{1:00}:{2:00}:{3:00} - {4:00}:{5:00}:{6:00}:{7:00} \t{8}",
p.StartTime.Hours, p.StartTime.Minutes, p.StartTime.Seconds, p.StartTime.Milliseconds / 10,
sb.AppendLine(string.Format(writeFormat, p.StartTime.Hours, p.StartTime.Minutes, p.StartTime.Seconds, p.StartTime.Milliseconds / 10,
p.EndTime.Hours, p.EndTime.Minutes, p.EndTime.Seconds, p.EndTime.Milliseconds / 10,
text));
}
@ -48,7 +48,8 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
}
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
{ // 00:04:10:92 - 00:04:13:32 Raise Yourself To Help Mankind
{
// 00:04:10:92 - 00:04:13:32 Raise Yourself To Help Mankind
// 00:04:27:92 - 00:04:30:92 الجهة المتولية للمسئولية الاجتماعية لشركتنا.
_errorCount = 0;

View File

@ -8,8 +8,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
{
public class SonyDVDArchitectExplicitDuration : SubtitleFormat
{
private static Regex regex = new Regex(@"^\d\d:\d\d:\d\d\.\d\d\d[ \t]+\d\d:\d\d:\d\d\.\d\d\d[ \t]+\d\d:\d\d:\d\d\.\d\d\d[ \t]+", RegexOptions.Compiled);
private static readonly Regex regex = new Regex(@"^\d\d:\d\d:\d\d\.\d\d\d[ \t]+\d\d:\d\d:\d\d\.\d\d\d[ \t]+\d\d:\d\d:\d\d\.\d\d\d[ \t]+", RegexOptions.Compiled);
public override string Extension
{
@ -36,12 +35,12 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
public override string ToText(Subtitle subtitle, string title)
{
var sb = new StringBuilder();
const string writeFormat = "{0:00}:{1:00}:{2:00}.{3:000}\t{4:00}:{5:00}:{6:00}.{7:000}\t{8:00}:{9:00}:{10:00}.{11:000}\t{12}";
foreach (Paragraph p in subtitle.Paragraphs)
{
string text = HtmlUtil.RemoveHtmlTags(p.Text);
text = text.Replace(Environment.NewLine, "\r");
sb.AppendLine(string.Format("{0:00}:{1:00}:{2:00}.{3:000}\t{4:00}:{5:00}:{6:00}.{7:000}\t{8:00}:{9:00}:{10:00}.{11:000}\t{12}",
p.StartTime.Hours, p.StartTime.Minutes, p.StartTime.Seconds, p.StartTime.Milliseconds,
sb.AppendLine(string.Format(writeFormat, p.StartTime.Hours, p.StartTime.Minutes, p.StartTime.Seconds, p.StartTime.Milliseconds,
p.EndTime.Hours, p.EndTime.Minutes, p.EndTime.Seconds, p.EndTime.Milliseconds,
p.Duration.Hours, p.Duration.Minutes, p.Duration.Seconds, p.Duration.Milliseconds,
text));
@ -74,8 +73,8 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
{
isTimeCode = true;
s = s.Substring(0, match.Length);
s = s.Replace("\t", ":");
s = s.Replace(".", ":");
s = s.Replace('\t', ':');
s = s.Replace('.', ':');
s = s.Replace(" ", string.Empty);
s = s.Trim().TrimEnd(':').TrimEnd();
string[] parts = s.Split(':');

View File

@ -8,7 +8,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
{
public class SonyDVDArchitectLineAndDuration : SubtitleFormat
{
private static Regex regex = new Regex(@"^\d+\t\d\d:\d\d:\d\d:\d\d\t\d\d:\d\d:\d\d:\d\d\t\d\d:\d\d:\d\d:\d\d$", RegexOptions.Compiled);
private static readonly Regex regex = new Regex(@"^\d+\t\d\d:\d\d:\d\d:\d\d\t\d\d:\d\d:\d\d:\d\d\t\d\d:\d\d:\d\d:\d\d$", RegexOptions.Compiled);
public override string Extension
{
@ -49,11 +49,12 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
sb.AppendLine("#\tIn\tOut\tDuration");
sb.AppendLine();
int count = 0;
const string writeFormat = "{13}\t{0:00}:{1:00}:{2:00}:{3:00}\t{4:00}:{5:00}:{6:00}:{7:00}\t{8:00}:{9:00}:{10:00}:{11:00}\r\n{12}";
foreach (Paragraph p in subtitle.Paragraphs)
{
count++;
string text = HtmlUtil.RemoveHtmlTags(p.Text);
sb.AppendLine(string.Format("{13}\t{0:00}:{1:00}:{2:00}:{3:00}\t{4:00}:{5:00}:{6:00}:{7:00}\t{8:00}:{9:00}:{10:00}:{11:00}\r\n{12}" + Environment.NewLine,
var text = HtmlUtil.RemoveHtmlTags(p.Text, true);
sb.AppendLine(string.Format(writeFormat + Environment.NewLine,
p.StartTime.Hours, p.StartTime.Minutes, p.StartTime.Seconds, MillisecondsToFramesMaxFrameRate(p.StartTime.Milliseconds),
p.EndTime.Hours, p.EndTime.Minutes, p.EndTime.Seconds, MillisecondsToFramesMaxFrameRate(p.EndTime.Milliseconds),
p.Duration.Hours, p.Duration.Minutes, p.Duration.Seconds, MillisecondsToFramesMaxFrameRate(p.Duration.Milliseconds),

View File

@ -8,7 +8,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
{
public class SonyDVDArchitectTabs : SubtitleFormat
{
private static Regex regex = new Regex(@"^\d\d:\d\d:\d\d:\d\d[ \t]+\d\d:\d\d:\d\d:\d\d[ \t]+", RegexOptions.Compiled);
private static readonly Regex regex = new Regex(@"^\d\d:\d\d:\d\d:\d\d[ \t]+\d\d:\d\d:\d\d:\d\d[ \t]+", RegexOptions.Compiled);
public override string Extension
{
@ -35,12 +35,12 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
public override string ToText(Subtitle subtitle, string title)
{
var sb = new StringBuilder();
const string writeFormat = "{0:00}:{1:00}:{2:00}:{3:00}\t{4:00}:{5:00}:{6:00}:{7:00}\t{8}";
foreach (Paragraph p in subtitle.Paragraphs)
{
string text = HtmlUtil.RemoveHtmlTags(p.Text);
text = text.Replace(Environment.NewLine, "\r");
sb.AppendLine(string.Format("{0:00}:{1:00}:{2:00}:{3:00}\t{4:00}:{5:00}:{6:00}:{7:00}\t{8}",
p.StartTime.Hours, p.StartTime.Minutes, p.StartTime.Seconds, p.StartTime.Milliseconds / 10,
sb.AppendLine(string.Format(writeFormat, p.StartTime.Hours, p.StartTime.Minutes, p.StartTime.Seconds, p.StartTime.Milliseconds / 10,
p.EndTime.Hours, p.EndTime.Minutes, p.EndTime.Seconds, p.EndTime.Milliseconds / 10,
text));
}
@ -70,7 +70,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
{
isTimeCode = true;
string s = line.Substring(0, match.Length);
s = s.Replace("\t", ":");
s = s.Replace('\t', ':');
s = s.Replace(" ", string.Empty);
s = s.Trim().TrimEnd(':').TrimEnd();
string[] parts = s.Split(':');

View File

@ -8,8 +8,8 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
{
public class SonyDVDArchitectWithLineNumbers : SubtitleFormat
{
private static Regex regexTimeCode = new Regex(@"^\d\d\d\d \d\d:\d\d:\d\d:\d\d \d\d:\d\d:\d\d:\d\d", RegexOptions.Compiled);
private static Regex regex1DigitMilliseconds = new Regex(@"^\d\d\d\d \d\d\d:\d\d:\d\d:\d \d\d\d:\d\d:\d\d:\d", RegexOptions.Compiled);
private static readonly Regex regexTimeCode = new Regex(@"^\d\d\d\d \d\d:\d\d:\d\d:\d\d \d\d:\d\d:\d\d:\d\d", RegexOptions.Compiled);
private static readonly Regex regex1DigitMilliseconds = new Regex(@"^\d\d\d\d \d\d\d:\d\d:\d\d:\d \d\d\d:\d\d:\d\d:\d", RegexOptions.Compiled);
public override string Extension
{

View File

@ -39,7 +39,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
public override string ToText(Subtitle subtitle, string title)
{
StringBuilder sb = new StringBuilder();
var sb = new StringBuilder();
int index = 0;
foreach (Paragraph p in subtitle.Paragraphs)
{
@ -96,10 +96,10 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
{
string[] parts = s.Split(':');
string minutes = parts[0];
string seconds = parts[1];
var minutes = int.Parse(parts[0]);
var seconds = int.Parse(parts[1]);
return new TimeCode(0, int.Parse(minutes), int.Parse(seconds), 0);
return new TimeCode(0, minutes, seconds, 0);
}
}

View File

@ -41,9 +41,10 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
{
var sb = new StringBuilder();
int index = 0;
const string writeFormat = "{0}{1}";
foreach (Paragraph p in subtitle.Paragraphs)
{
sb.AppendLine(string.Format("{0}{1}", EncodeTimeCode(p.StartTime), HtmlUtil.RemoveHtmlTags(p.Text.Replace(Environment.NewLine, " "))));
sb.AppendLine(string.Format(writeFormat, EncodeTimeCode(p.StartTime), HtmlUtil.RemoveHtmlTags(p.Text.Replace(Environment.NewLine, " "))));
index++;
}
return sb.ToString();
@ -58,6 +59,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
{
_errorCount = 0;
subtitle.Paragraphs.Clear();
char[] trimChars = { '', '.', ';', ':' };
foreach (string line in lines)
{
if (regexTimeCodes.IsMatch(line))
@ -66,7 +68,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
string text = line.Remove(0, splitter);
var p = new Paragraph(DecodeTimeCode(line.Substring(0, splitter)), new TimeCode(0, 0, 0, 0), text);
subtitle.Paragraphs.Add(p);
text = text.Trim().Trim('', '.', ';', ':').Trim();
text = text.Trim().Trim(trimChars).Trim();
if (text.Length > 0 && char.IsDigit(text[0]))
_errorCount++;
}