John b7e0e78ca9 Fixed logic for Find/Replace of backslash and symbol characters B2016-156
Moved repeated logic to a static function RTFConvertedSymbolsToUnicode which is needed for Find/Replace. Used in DisplayText.cs, FindReplace.cs, and VlnFlexGrid  B2016-156
Fixed finding backslash and symbols B2016-156
Fixed finding backslash and symbols in tables (grids) B2016-156
2016-07-06 15:23:30 +00:00

135 lines
4.6 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;
}
public static string RTFConvertedSymbolsToUnicode(string str)
{
string rtnStr = str;
// convert \~ to a hard spece. RTF is automatically converting \u160? to \~ but will then convert
// the \~ to a regular space!
rtnStr = rtnStr.Replace(@"\~", @"\u160?");
rtnStr = rtnStr.Replace(@"\'a0", @"\u160?");
// convert \'99 to \u8482? this is for the trade mark symbol. RTF is automatically
// converting the unicode \u8482? to \'99, but once this is done, PROMS StepRTB (edit windows) does not show it
rtnStr = rtnStr.Replace(@"\'99", @"\u8482?");
// convert \'ae to \u174? this is for the registered symbol. RTF converts the unicode character to \'ae
rtnStr = rtnStr.Replace(@"\'ae",@"\u174?");
// convert \'a9 to \u169? this is for the copyright symbol. RTF converts the unicode character to \'a9
rtnStr = rtnStr.Replace(@"\'a9",@"\u169?");
return rtnStr;
}
}
}