RTF refact

This commit is contained in:
niksedk 2015-11-25 06:00:29 +01:00
parent 3f0b3af58d
commit 9ccf126f14
18 changed files with 143 additions and 255 deletions

View File

@ -0,0 +1,8 @@
namespace Nikse.SubtitleEdit.Core.Interfaces
{
public interface IRtfTextConverter
{
string RtfToText(string rtf);
string TextToRtf(string text);
}
}

View File

@ -171,6 +171,7 @@
<Compile Include="IfoParser.cs" />
<Compile Include="ImageSplitter.cs" />
<Compile Include="ImageSplitterItem.cs" />
<Compile Include="Interfaces\IRtfTextConverter.cs" />
<Compile Include="Language.cs" />
<Compile Include="LanguageAutoDetect.cs" />
<Compile Include="LanguageDeserializer.cs" />

View File

@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using Nikse.SubtitleEdit.Core.Interfaces;
namespace Nikse.SubtitleEdit.Core
{
@ -14,6 +16,9 @@ namespace Nikse.SubtitleEdit.Core
/// </remarks>
public static class RichTextToPlainText
{
public static IRtfTextConverter NativeRtfTextConverter { get; set; }
private class StackEntry
{
public int NumberOfCharactersToSkip { get; private set; }
@ -96,13 +101,19 @@ namespace Nikse.SubtitleEdit.Core
/// </summary>
/// <param name="inputRtf">RTF formatted text</param>
/// <returns>Plain text from RTF</returns>
public static string StripRichTextFormat(string inputRtf)
public static string ConvertToText(string inputRtf)
{
if (inputRtf == null)
{
return null;
}
// use interface converter if available
if (NativeRtfTextConverter != null)
{
NativeRtfTextConverter.RtfToText(inputRtf);
}
var stack = new Stack<StackEntry>();
bool ignorable = false; // Whether this group (and all inside it) are "ignorable".
int ucskip = 1; // Number of ASCII characters to skip after a unicode character.
@ -217,6 +228,39 @@ namespace Nikse.SubtitleEdit.Core
return String.Join(String.Empty, outList.ToArray());
}
public static string ConvertToRtf(this string value)
{
if (string.IsNullOrWhiteSpace(value))
{
return string.Empty;
}
// use interface converter if available
if (NativeRtfTextConverter != null)
{
NativeRtfTextConverter.TextToRtf(value);
}
// special RTF chars
var backslashed = new StringBuilder(value);
backslashed.Replace(@"\", @"\\");
backslashed.Replace(@"{", @"\{");
backslashed.Replace(@"}", @"\}");
backslashed.Replace(Environment.NewLine, @"\par" + Environment.NewLine);
// convert string char by char
var sb = new StringBuilder();
foreach (char character in backslashed.ToString())
{
if (character <= 0x7f)
sb.Append(character);
else
sb.Append("\\u" + Convert.ToUInt32(character) + "?");
}
return @"{\rtf1\ansi\ansicpg1252\deff0{\fonttbl\f0\fswiss Helvetica;}\f0\pard " + sb + @"\par" + Environment.NewLine + "}";
}
}
}

View File

@ -222,7 +222,7 @@ namespace Nikse.SubtitleEdit.Core
public static string FromRtf(this string value)
{
return RichTextToPlainText.StripRichTextFormat(value);
return RichTextToPlainText.ConvertToText(value);
}
}

View File

@ -33,11 +33,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
public override string ToText(Subtitle subtitle, string title)
{
System.Windows.Forms.RichTextBox rtBox = new System.Windows.Forms.RichTextBox();
rtBox.Text = ToF4Text(subtitle);
string rtf = rtBox.Rtf;
rtBox.Dispose();
return rtf;
return ToF4Text(subtitle).ToRtf();
}
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
@ -51,19 +47,8 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
if (!rtf.StartsWith("{\\rtf"))
return;
var rtBox = new System.Windows.Forms.RichTextBox();
try
{
rtBox.Rtf = rtf;
}
catch (Exception exception)
{
System.Diagnostics.Debug.WriteLine(exception.Message);
return;
}
string text = rtBox.Text;
rtBox.Dispose();
LoadF4TextSubtitle(subtitle, text);
LoadF4TextSubtitle(subtitle, rtf.FromRtf());
}
}
}

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
@ -57,11 +58,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
count++;
}
var rtBox = new System.Windows.Forms.RichTextBox();
rtBox.Text = sb.ToString();
string rtf = rtBox.Rtf;
rtBox.Dispose();
return rtf;
return sb.ToString().ToRtf();
}
private static TimeCode DecodeTimeCode(string timeCode)
@ -81,29 +78,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
if (!rtf.StartsWith("{\\rtf"))
return;
System.Windows.Forms.RichTextBox rtBox = null;
try
{
rtBox = new System.Windows.Forms.RichTextBox
{
Rtf = rtf
};
lines = new List<string>(rtBox.Text.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None));
}
catch (ArgumentException)
{
// Invalid format
return;
}
finally
{
if (rtBox != null)
{
rtBox.Dispose();
}
}
lines = rtf.FromRtf().SplitToLines().ToList();
_errorCount = 0;
Paragraph p = null;
foreach (string line in lines)

View File

@ -1,5 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Nikse.SubtitleEdit.Core;
namespace Nikse.SubtitleEdit.Core.SubtitleFormats
{
@ -30,10 +32,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
public override string ToText(Subtitle subtitle, string title)
{
var u52 = new UnknownSubtitle52();
using (var rtBox = new System.Windows.Forms.RichTextBox { Text = u52.ToText(subtitle, title) })
{
return rtBox.Rtf;
}
return u52.ToText(subtitle, title).ToRtf();
}
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
@ -47,16 +46,11 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
var text = new StringBuilder();
foreach (string s in lines)
text.AppendLine(s);
using (var rtBox = new System.Windows.Forms.RichTextBox())
{
rtBox.Rtf = text.ToString();
var lines2 = new List<string>();
foreach (string line in rtBox.Lines)
lines2.Add(line);
var u52 = new UnknownSubtitle52();
u52.LoadSubtitle(subtitle, lines2, fileName);
_errorCount = u52.ErrorCount;
}
var lines2 = text.ToString().FromRtf().SplitToLines().ToList();
var u52 = new UnknownSubtitle52();
u52.LoadSubtitle(subtitle, lines2, fileName);
_errorCount = u52.ErrorCount;
}
}
}

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
@ -57,11 +58,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
count++;
}
var rtBox = new System.Windows.Forms.RichTextBox();
rtBox.Text = sb.ToString();
string rtf = rtBox.Rtf;
rtBox.Dispose();
return rtf;
return sb.ToString().ToRtf();
}
private static TimeCode DecodeTimeCode(string timeCode)
@ -81,27 +78,8 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
if (!rtf.StartsWith("{\\rtf"))
return;
string text = string.Empty;
var rtBox = new System.Windows.Forms.RichTextBox();
try
{
rtBox.Rtf = rtf;
text = rtBox.Text.Replace("\r\n", "\n");
}
catch (Exception exception)
{
System.Diagnostics.Debug.WriteLine(exception.Message);
return;
}
finally
{
rtBox.Dispose();
}
lines = new List<string>();
foreach (string line in text.Split('\n'))
lines.Add(line);
lines = rtf.FromRtf().SplitToLines().ToList();
_errorCount = 0;
Paragraph p = null;
foreach (string line in lines)

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
@ -60,10 +61,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
count++;
}
using (var rtBox = new System.Windows.Forms.RichTextBox { Text = sb.ToString().Trim() })
{
return rtBox.Rtf;
}
return sb.ToString().Trim().ToRtf();
}
private static string MakeTimeCode(TimeCode timeCode)
@ -88,27 +86,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
if (!rtf.StartsWith("{\\rtf"))
return;
string text = string.Empty;
var rtBox = new System.Windows.Forms.RichTextBox();
try
{
rtBox.Rtf = rtf;
text = rtBox.Text.Replace("\r\n", "\n");
}
catch (Exception exception)
{
System.Diagnostics.Debug.WriteLine(exception.Message);
return;
}
finally
{
rtBox.Dispose();
}
lines = new List<string>();
foreach (string line in text.Split('\n'))
lines.Add(line);
lines = rtf.FromRtf().SplitToLines().ToList();
_errorCount = 0;
Paragraph p = null;
sb = new StringBuilder();

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Nikse.SubtitleEdit.Core.SubtitleFormats
@ -33,11 +34,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
public override string ToText(Subtitle subtitle, string title)
{
System.Windows.Forms.RichTextBox rtBox = new System.Windows.Forms.RichTextBox();
rtBox.Text = base.ToText(subtitle, title);
string rtf = rtBox.Rtf;
rtBox.Dispose();
return rtf;
return base.ToText(subtitle, title).ToRtf();
}
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
@ -51,27 +48,9 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
if (!rtf.StartsWith("{\\rtf"))
return;
string[] arr = null;
var rtBox = new System.Windows.Forms.RichTextBox();
try
{
rtBox.Rtf = rtf;
arr = rtBox.Text.SplitToLines();
}
catch (Exception exception)
{
System.Diagnostics.Debug.WriteLine(exception.Message);
return;
}
finally
{
rtBox.Dispose();
}
lines = new List<string>();
foreach (string s in arr)
lines.Add(s);
lines = rtf.FromRtf().SplitToLines().ToList();
base.LoadSubtitle(subtitle, lines, fileName);
}
}
}

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Nikse.SubtitleEdit.Core.SubtitleFormats
@ -33,11 +34,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
public override string ToText(Subtitle subtitle, string title)
{
System.Windows.Forms.RichTextBox rtBox = new System.Windows.Forms.RichTextBox();
rtBox.Text = base.ToText(subtitle, title);
string rtf = rtBox.Rtf;
rtBox.Dispose();
return rtf;
return base.ToText(subtitle, title).ToRtf();
}
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
@ -51,27 +48,9 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
if (!rtf.StartsWith("{\\rtf"))
return;
string[] arr = null;
var rtBox = new System.Windows.Forms.RichTextBox();
try
{
rtBox.Rtf = rtf;
arr = rtBox.Text.Replace("\r\n", "\n").Split('\n');
}
catch (Exception exception)
{
System.Diagnostics.Debug.WriteLine(exception.Message);
return;
}
finally
{
rtBox.Dispose();
}
var list = new List<string>();
foreach (string s in arr)
list.Add(s);
var list = rtf.FromRtf().SplitToLines().ToList();
base.LoadSubtitle(subtitle, list, fileName);
}
}
}

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Nikse.SubtitleEdit.Core.SubtitleFormats
@ -33,11 +34,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
public override string ToText(Subtitle subtitle, string title)
{
System.Windows.Forms.RichTextBox rtBox = new System.Windows.Forms.RichTextBox();
rtBox.Text = base.ToText(subtitle, title);
string rtf = rtBox.Rtf;
rtBox.Dispose();
return rtf;
return base.ToText(subtitle, title).ToRtf();
}
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
@ -51,27 +48,9 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
if (!rtf.StartsWith("{\\rtf"))
return;
string[] arr = null;
var rtBox = new System.Windows.Forms.RichTextBox();
try
{
rtBox.Rtf = rtf;
arr = rtBox.Text.Replace("\r\n", "\n").Split('\n');
}
catch (Exception exception)
{
System.Diagnostics.Debug.WriteLine(exception.Message);
return;
}
finally
{
rtBox.Dispose();
}
var list = new List<string>();
foreach (string s in arr)
list.Add(s);
var list = rtf.FromRtf().SplitToLines().ToList();
base.LoadSubtitle(subtitle, list, fileName);
}
}
}

View File

@ -53,11 +53,7 @@ ST 0 EB 3.10
index++;
sb.AppendLine(string.Format("* {0}-{1} 00.00 00.0 1 {2} 00 16-090-090{3}{4}{3}@", EncodeTimeCode(p.StartTime), EncodeTimeCode(p.EndTime), index.ToString().PadLeft(4, '0'), Environment.NewLine, HtmlUtil.RemoveHtmlTags(p.Text)));
}
System.Windows.Forms.RichTextBox rtBox = new System.Windows.Forms.RichTextBox();
rtBox.Text = sb.ToString();
string rtf = rtBox.Rtf;
rtBox.Dispose();
return rtf;
return sb.ToString().ToRtf();
}
private static string EncodeTimeCode(TimeCode time)
@ -80,23 +76,7 @@ ST 0 EB 3.10
if (!rtf.StartsWith("{\\rtf"))
return;
string[] arr = null;
var rtBox = new System.Windows.Forms.RichTextBox();
try
{
rtBox.Rtf = rtf;
arr = rtBox.Text.Replace("\r\n", "\n").Split('\n');
}
catch (Exception exception)
{
System.Diagnostics.Debug.WriteLine(exception.Message);
return;
}
finally
{
rtBox.Dispose();
}
string[] arr = rtf.FromRtf().SplitToLines();
Paragraph p = null;
subtitle.Paragraphs.Clear();
foreach (string line in arr)

View File

@ -51,11 +51,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
count++;
}
System.Windows.Forms.RichTextBox rtBox = new System.Windows.Forms.RichTextBox();
rtBox.Text = sb.ToString();
string rtf = rtBox.Rtf;
rtBox.Dispose();
return rtf;
return sb.ToString().ToRtf();
}
private static string EncodeTimeCode(TimeCode time)
@ -74,23 +70,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
if (!rtf.StartsWith("{\\rtf"))
return;
string[] arr = null;
var rtBox = new System.Windows.Forms.RichTextBox();
try
{
rtBox.Rtf = rtf;
arr = rtBox.Text.SplitToLines();
}
catch (Exception exception)
{
System.Diagnostics.Debug.WriteLine(exception.Message);
return;
}
finally
{
rtBox.Dispose();
}
string[] arr = rtf.FromRtf().SplitToLines();
bool expectStartTime = true;
var p = new Paragraph();
subtitle.Paragraphs.Clear();

View File

@ -46,12 +46,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
{
sb.AppendLine(string.Format(format, EncodeTimeCode(p.StartTime), EncodeTimeCode(p.EndTime), p.Text, Environment.NewLine));
}
System.Windows.Forms.RichTextBox rtBox = new System.Windows.Forms.RichTextBox();
rtBox.Text = sb.ToString();
string rtf = rtBox.Rtf;
rtBox.Dispose();
return rtf;
return sb.ToString().ToRtf();
}
private static string EncodeTimeCode(TimeCode time)
@ -70,23 +65,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
if (!rtf.StartsWith("{\\rtf"))
return;
string[] arr = null;
var rtBox = new System.Windows.Forms.RichTextBox();
try
{
rtBox.Rtf = rtf;
arr = rtBox.Text.SplitToLines();
}
catch (Exception exception)
{
System.Diagnostics.Debug.WriteLine(exception.Message);
return;
}
finally
{
rtBox.Dispose();
}
string[] arr = rtf.FromRtf().SplitToLines();
var p = new Paragraph();
subtitle.Paragraphs.Clear();
foreach (string line in arr)

View File

@ -341,10 +341,11 @@ namespace Nikse.SubtitleEdit.Forms
}
SetEncoding(Configuration.Settings.General.DefaultEncoding);
// set up UI interfaces in subtitle formats
// set up UI interfaces / injections
YouTubeAnnotations.GetYouTubeAnnotationStyles = new UiGetYouTubeAnnotationStyles();
Ebu.EbuUiHelper = new UiEbuSaveHelper();
Pac.GetPacEncodingImplementation = new UiGetPacEncoding();
RichTextToPlainText.NativeRtfTextConverter = new RtfTextConverterRichTextBox();
toolStripComboBoxFrameRate.Items.Add((23.976).ToString());
toolStripComboBoxFrameRate.Items.Add((24.0).ToString());

View File

@ -0,0 +1,47 @@
using Nikse.SubtitleEdit.Core.Interfaces;
using System;
namespace Nikse.SubtitleEdit.Logic
{
public class RtfTextConverterRichTextBox : IRtfTextConverter
{
public string RtfToText(string rtf)
{
var rtBox = new System.Windows.Forms.RichTextBox();
try
{
rtBox.Rtf = rtf;
return rtBox.Text;
}
catch (Exception exception)
{
System.Diagnostics.Debug.WriteLine("RtfTextConverterRichTextBox.RtfToText: " + exception.Message);
return string.Empty;
}
finally
{
rtBox.Dispose();
}
}
public string TextToRtf(string text)
{
var rtBox = new System.Windows.Forms.RichTextBox();
try
{
rtBox.Text = text;
return rtBox.Rtf;
}
catch (Exception exception)
{
System.Diagnostics.Debug.WriteLine("RtfTextConverterRichTextBox.TextToRtf: " + exception.Message);
return string.Empty;
}
finally
{
rtBox.Dispose();
}
}
}
}

View File

@ -826,6 +826,7 @@
<Compile Include="Logic\Ocr\OcrImage.cs" />
<Compile Include="Logic\Ocr\NOcrPoint.cs" />
<Compile Include="Logic\Ocr\SpellCheckOcrTextResult.cs" />
<Compile Include="Logic\RtfTextConverterRichTextBox.cs" />
<Compile Include="Logic\SpellCheck\Hunspell.cs" />
<Compile Include="Logic\SpellCheck\LinuxHunspell.cs" />
<Compile Include="Logic\SpellCheck\MacHunspell.cs" />