From 1eca35970833a9520ee41b5a7b74ec717328295b Mon Sep 17 00:00:00 2001 From: kamenf Date: Tue, 1 Aug 2017 03:30:19 +0300 Subject: [PATCH 1/5] TSB4: handling codepage and correct text extract --- libse/SubtitleFormats/TSB4.cs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/libse/SubtitleFormats/TSB4.cs b/libse/SubtitleFormats/TSB4.cs index b65737db6..cd2a25c32 100644 --- a/libse/SubtitleFormats/TSB4.cs +++ b/libse/SubtitleFormats/TSB4.cs @@ -54,29 +54,39 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats { return; } + + int CodePage = 0; // use default code page if not found + 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; + } 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 { - int endOfText = array[i + 4]; + int endOfText = array[i + 4] + array[i+5]*256; // get only first two bytes of size int start = array[i + 16] + array[i + 17] * 256; - if (array[i + 18] != 32) + // if (array[i + 18] != 32) start += array[i + 18] * 256 * 256; int end = array[i + 20] + array[i + 21] * 256; - if (array[i + 22] != 32) + // if (array[i + 22] != 32) end += array[i + 22] * 256 * 256; int textStart = i; - while (textStart < i + endOfText && !(array[textStart] == 0x4C && array[textStart + 1] == 0x49 && array[textStart + 2] == 0x4E && array[textStart + 3] == 0x45)) // LINE + while (textStart < i + endOfText - 8 && !(array[textStart] == 0x4C && array[textStart + 1] == 0x49 && array[textStart + 2] == 0x4E && array[textStart + 3] == 0x45)) // LINE { textStart++; } - int length = i + endOfText - textStart - 2; + // int length = i + endOfText - textStart - 2; + int length = array[textStart+4] + array[textStart+5]*256; textStart += 8; - string text = Encoding.Default.GetString(array, textStart, length); + // string text = Encoding.Default.GetString(array, textStart, length); + string text = Encoding(CodePage).GetString(array, textStart, length); // text = Encoding.Default.GetString(array, i + 53, endOfText - 47); text = text.Trim('\0').Replace("\0", " ").Trim(); var item = new Paragraph(text, FramesToMilliseconds(start), FramesToMilliseconds(end)); From f2cb8b38b6041309689dab27b7bce25502a1dd1c Mon Sep 17 00:00:00 2001 From: kamenf Date: Tue, 1 Aug 2017 03:49:29 +0300 Subject: [PATCH 2/5] Update TSB4.cs --- libse/SubtitleFormats/TSB4.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libse/SubtitleFormats/TSB4.cs b/libse/SubtitleFormats/TSB4.cs index cd2a25c32..096daa2ad 100644 --- a/libse/SubtitleFormats/TSB4.cs +++ b/libse/SubtitleFormats/TSB4.cs @@ -86,7 +86,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats textStart += 8; // string text = Encoding.Default.GetString(array, textStart, length); - string text = Encoding(CodePage).GetString(array, textStart, length); + string text = Encoding.GetEncoding(CodePage).GetString(array, textStart, length); // text = Encoding.Default.GetString(array, i + 53, endOfText - 47); text = text.Trim('\0').Replace("\0", " ").Trim(); var item = new Paragraph(text, FramesToMilliseconds(start), FramesToMilliseconds(end)); From bc2be229787d15e7dba85d2b16ecb19c2b0279a8 Mon Sep 17 00:00:00 2001 From: kamenf Date: Tue, 1 Aug 2017 18:38:28 +0300 Subject: [PATCH 3/5] conform line endings to CRLF --- libse/SubtitleFormats/TSB4.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/libse/SubtitleFormats/TSB4.cs b/libse/SubtitleFormats/TSB4.cs index 096daa2ad..f4ab9a13d 100644 --- a/libse/SubtitleFormats/TSB4.cs +++ b/libse/SubtitleFormats/TSB4.cs @@ -89,6 +89,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats string text = Encoding.GetEncoding(CodePage).GetString(array, textStart, length); // text = Encoding.Default.GetString(array, i + 53, endOfText - 47); text = text.Trim('\0').Replace("\0", " ").Trim(); + text = text.Replace("\r\n", "\n").Replace("\r", "\n").Replace("\n", "\r\n"); //conform to CRLF var item = new Paragraph(text, FramesToMilliseconds(start), FramesToMilliseconds(end)); subtitle.Paragraphs.Add(item); i += endOfText + 5; From 8b0d69a4f226dfc0ef78f1039f8dd402c1838672 Mon Sep 17 00:00:00 2001 From: kamenf Date: Tue, 1 Aug 2017 19:36:10 +0300 Subject: [PATCH 4/5] Update TSB4.cs --- libse/SubtitleFormats/TSB4.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libse/SubtitleFormats/TSB4.cs b/libse/SubtitleFormats/TSB4.cs index f4ab9a13d..d48e8a5d5 100644 --- a/libse/SubtitleFormats/TSB4.cs +++ b/libse/SubtitleFormats/TSB4.cs @@ -89,7 +89,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats string text = Encoding.GetEncoding(CodePage).GetString(array, textStart, length); // text = Encoding.Default.GetString(array, i + 53, endOfText - 47); text = text.Trim('\0').Replace("\0", " ").Trim(); - text = text.Replace("\r\n", "\n").Replace("\r", "\n").Replace("\n", "\r\n"); //conform to CRLF + text = string.join(Environment.NewLine, text.SplitToLines()); //conform to CRLF var item = new Paragraph(text, FramesToMilliseconds(start), FramesToMilliseconds(end)); subtitle.Paragraphs.Add(item); i += endOfText + 5; From c4bc7a1ddf65785441c7cad46f802d73bd4b6f59 Mon Sep 17 00:00:00 2001 From: kamenf Date: Tue, 1 Aug 2017 20:18:56 +0300 Subject: [PATCH 5/5] Update TSB4.cs --- libse/SubtitleFormats/TSB4.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libse/SubtitleFormats/TSB4.cs b/libse/SubtitleFormats/TSB4.cs index d48e8a5d5..6bfc1ed1b 100644 --- a/libse/SubtitleFormats/TSB4.cs +++ b/libse/SubtitleFormats/TSB4.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Text; +using System; namespace Nikse.SubtitleEdit.Core.SubtitleFormats { @@ -89,7 +90,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats string text = Encoding.GetEncoding(CodePage).GetString(array, textStart, length); // text = Encoding.Default.GetString(array, i + 53, endOfText - 47); text = text.Trim('\0').Replace("\0", " ").Trim(); - text = string.join(Environment.NewLine, text.SplitToLines()); //conform to CRLF + text = string.Join(Environment.NewLine, text.SplitToLines()); //conform to CRLF var item = new Paragraph(text, FramesToMilliseconds(start), FramesToMilliseconds(end)); subtitle.Paragraphs.Add(item); i += endOfText + 5;