119 lines
3.7 KiB
C#
119 lines
3.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace Volian.Base.Library
|
|
{
|
|
public class RtfTools
|
|
{
|
|
public static List<string> SplitText(string text, int len)
|
|
{
|
|
{
|
|
List<string> results = new List<string>();
|
|
if (text.Contains("\\LINE ") || text.Contains("\r\n") || text.Contains("\\line "))
|
|
{
|
|
string[] mySplit = { "\\LINE ", "\r\n", "\\line " };
|
|
return new List<string>(text.Split(mySplit, StringSplitOptions.None));
|
|
|
|
}
|
|
int width = 0; // width of text, non-rtf
|
|
int start = 0; // start of line (index into string 'text'), includes rtf
|
|
int lastspace = 0; // location of lastspace (index into string 'text'), includes rtf
|
|
int startNonRtf = 0; // start of line, non-rtf (used for determining starting position to determine width if there was a break)
|
|
string rtfprefix = "";
|
|
string nextprefix = "";
|
|
for (int indx = 0; indx < text.Length; indx++)
|
|
{
|
|
if (text[indx] == '\\') //rtf command
|
|
{
|
|
// look for three things at beginning of string: hex, unicode, rtfcommand.
|
|
Match m = Regex.Match(text.Substring(indx), @"^\\'[a-fA-F0-9][a-fA-F0-9]"); //hex
|
|
if (m.Success)
|
|
{
|
|
indx += m.Length - 1;
|
|
width++;
|
|
}
|
|
else
|
|
{
|
|
m = Regex.Match(text.Substring(indx), @"^\\[uU][a-fA-F0-9][a-fA-F0-9][a-fA-F0-9][?]"); // 3 char unicode, for example \u160? (hardspace)
|
|
if (m.Success)
|
|
{
|
|
indx += m.Length - 1;
|
|
width++;
|
|
}
|
|
else
|
|
{
|
|
m = Regex.Match(text.Substring(indx), @"^\\[uU][a-fA-F0-9][a-fA-F0-9][a-fA-F0-9][a-fA-F0-9][?]");
|
|
if (m.Success)
|
|
{
|
|
indx += m.Length - 1;
|
|
width++;
|
|
}
|
|
else
|
|
{
|
|
m = Regex.Match(text.Substring(indx), @"^\\[^ ]*? ");
|
|
if (m.Success)
|
|
{
|
|
indx += m.Length - 1;
|
|
rtfprefix = AdjustRtfPrefix(rtfprefix, m.Value);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
else
|
|
{
|
|
if (text[indx] == ' ')
|
|
{
|
|
lastspace = indx;
|
|
startNonRtf = width;
|
|
}
|
|
width++;
|
|
if (width > len)
|
|
{
|
|
// what should be done if lastspace == 0
|
|
// cannot find space char to split on, so break the word
|
|
// not ideal but PROMS was bombing otherwise - jsj 7/7/2014
|
|
if (lastspace == 0)
|
|
{
|
|
lastspace = indx;
|
|
startNonRtf = width - 1;
|
|
}
|
|
results.Add(nextprefix + text.Substring(start, lastspace - start).Trim(" ".ToCharArray()));
|
|
nextprefix = rtfprefix;
|
|
if (nextprefix != "") nextprefix += " ";
|
|
start = lastspace + 1;
|
|
width = (width - startNonRtf - 1) > 0 ? width - startNonRtf - 1 : 0;
|
|
lastspace = 0;
|
|
}
|
|
}
|
|
|
|
}
|
|
if (width > 0 || start < text.Length) results.Add(nextprefix + text.Substring(start).Trim(" ".ToCharArray()));
|
|
return results;
|
|
}
|
|
}
|
|
private static string AdjustRtfPrefix(string rtfprefix, string rtfcommand)
|
|
{
|
|
if (rtfcommand.Contains(@"\ulnone") || rtfcommand.Contains(@"\ul0")) // off
|
|
rtfprefix = rtfprefix.Replace(@"\ul", "");
|
|
else if (rtfcommand.Contains(@"\ul"))
|
|
rtfprefix += @"\ul";
|
|
if (rtfcommand.Contains(@"\up0") || rtfcommand.Contains(@"\dn0")) rtfprefix = rtfprefix.Replace(@"\up2", "").Replace(@"\dn2", "");
|
|
else if (rtfcommand.Contains(@"\up")) rtfprefix += @"\up2";
|
|
else if (rtfcommand.Contains(@"\dn")) rtfprefix += @"\dn2";
|
|
if (rtfcommand.Contains(@"\b0"))
|
|
rtfprefix = rtfprefix.Replace(@"\b", "");
|
|
else if (rtfcommand.Contains(@"\b"))
|
|
rtfprefix += @"\b";
|
|
if (rtfcommand.Contains(@"\i0"))
|
|
rtfprefix = rtfprefix.Replace(@"\i", "");
|
|
else if (rtfcommand.Contains(@"\i"))
|
|
rtfprefix += @"\i";
|
|
return rtfprefix;
|
|
}
|
|
}
|
|
}
|