240 lines
8.9 KiB
C#
240 lines
8.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace VEPROMS.CSLA.Library
|
|
{
|
|
public class OutlineRTFTable
|
|
{
|
|
public OutlineRTFTable(string rtf, bool withBorder)
|
|
{
|
|
Rtf = rtf;
|
|
WithBorder = withBorder;
|
|
}
|
|
|
|
public string Rtf { get; set; }
|
|
public bool WithBorder { get; set; }
|
|
private RtfLines _Lines;
|
|
public RtfLines Lines
|
|
{
|
|
get
|
|
{
|
|
if (_Lines == null)
|
|
_Lines = new RtfLines(Rtf);
|
|
return _Lines;
|
|
}
|
|
}
|
|
public void OutlineTable()
|
|
{
|
|
int maxWidth = Lines.MaximumWidth;// Determine maximum width
|
|
// spaces here if not an outlined table:
|
|
Lines.Pad(maxWidth, WithBorder ? @"\u9474?" : " "); // Pad the lines to the appropriate width
|
|
Lines.Insert(0, new RtfLine(maxWidth, WithBorder ? @"\u9484?" : " ", WithBorder ? @"\u9472?" : " ", WithBorder ? @"\u9488?" : " ")); // Insert top line
|
|
Lines.Add(new RtfLine(maxWidth, WithBorder ? @"\u9492?" : " ", WithBorder ? @"\u9472?" : " ", WithBorder ? @"\u9496?" : " ")); // Insert bottom line
|
|
Lines.ReplaceLines(WithBorder);
|
|
}
|
|
public override string ToString() => Lines.ToString();
|
|
}
|
|
public class RtfLines : List<RtfLine>
|
|
{
|
|
public static Regex _RegReplaceLine = new Regex(@"\\line((\\[^ \[\]\\\(\)]*) )");
|
|
public RtfLines(string text)
|
|
{
|
|
// replace RTF \line with crlf
|
|
string txt = _RegReplaceLine.Replace(text, "$1\r\n");
|
|
// split text into lines
|
|
string[] breaks = { "\r\n" };
|
|
string[] lines = txt.Split(breaks, StringSplitOptions.None);
|
|
foreach (string line in lines)
|
|
Add(new RtfLine(line.Replace("\\bullet","*")));
|
|
}
|
|
public int MaximumWidth
|
|
{
|
|
get
|
|
{
|
|
int retval = 0;
|
|
foreach (RtfLine myLine in this)
|
|
if (myLine.MaximumWidth > retval) retval = myLine.MaximumWidth;
|
|
return retval;
|
|
}
|
|
}
|
|
public void Pad(int maxWidth, string edge)
|
|
{
|
|
foreach (RtfLine myLine in this)
|
|
myLine.Pad(maxWidth, edge);
|
|
|
|
}
|
|
public override string ToString()
|
|
{
|
|
StringBuilder sb = new StringBuilder();
|
|
string sep = "";
|
|
foreach (RtfLine myLine in this)
|
|
{
|
|
sb.Append(sep + myLine.ToString());
|
|
sep = "\r\n";
|
|
}
|
|
return sb.ToString();
|
|
}
|
|
private static readonly Regex _RegRightCheck = new Regex(@"-|\\u9472?|\\u8209\?");
|
|
private static readonly Regex _RegAboveCheck = new Regex(@"\||\\u9474\?|\\u9492\?|\\u9516\?|\\u9532\?|\\u9500\?|\\u9484\?|\\u9524\?|\\u9488\?|\\u9508\?");
|
|
private static readonly Regex _RegLeftCheck = new Regex(@"-|\\u9472\?|\\u9508\?|\\u9516\?|\\u9500\?|\\u9532\?|\\u9484\?|\\u9488\?|\\u9492\?|\\u8209\?");
|
|
private static readonly Regex _RegBelowCheck = new Regex(@"\||\\u9474\?");
|
|
|
|
private static readonly Regex _RegRightCheck1 = new Regex(@"\\u9474\?");
|
|
private static readonly Regex _RegAboveCheck1 = new Regex(@"\\u9472\?|\\u8209\?");
|
|
private static readonly Regex _RegLeftCheck1 = new Regex(@"\\u9474\?");
|
|
private static readonly Regex _RegBelowCheck1 = new Regex(@"\\u9472\?|\\u8209\?");
|
|
|
|
private static readonly Regex _RegHorizontalCheck = new Regex(@"-|\\u9472\?|\\u8209\?");
|
|
private static readonly Regex _RegVerticalCheck = new Regex(@"\||\\u9474\?");
|
|
private static readonly string[] TableCharsU = {
|
|
"\x0", // HEX"\x0", // No character
|
|
@"\u9472?", // HEX@"\u2500",// - Horizontal line - 16-bit char: '\xC4'
|
|
@"\u9474?", // HEX@"\u2502",// | Vertical line - 16-bit char: '\xB3'
|
|
@"\u9492?", // HEX@"\u2514",// L Bottom Left corner - 16-bit char: '\xC0'
|
|
@"\u9472?", // HEX@"\u2500",// - Horizontal line - 16-bit char: '\xC4'
|
|
@"\u9472?", // HEX@"\u2500",// - Horizontal line - 16-bit char: '\xC4'
|
|
@"\u9496?", // HEX@"\u2518",// Bottom Right Corner - 16-bit char: '\xD9'
|
|
@"\u9524?", // HEX@"\u2534",// Bottom Tee - 16-bit char: '\xC1'
|
|
@"\u9474?", // HEX@"\u2502",// | Vertical Bar - 16-bit char: '\xB3'
|
|
@"\u9484?", // HEX@"\u250c",// Upper Left corner - 16-bit char: '\xDA'
|
|
@"\u9474?", // HEX@"\u2502",// | Vertical Bar - 16-bit char: '\xB3'
|
|
@"\u9500?", // HEX@"\u251c",// Left Tee - 16-bit char: '\xC3'
|
|
@"\u9488?", // HEX@"\u2510",// Upper Right corner - 16-bit char: '\xBF'
|
|
@"\u9516?", // HEX@"\u252c",// T Top Tee - 16-bit char: '\xC2'
|
|
@"\u9508?", // HEX@"\u2524",// Right Tee - 16-bit char: '\xB4'
|
|
@"\u9532?", // HEX@"\u253c" // + Plus - 16-bit char: '\xC5'
|
|
};
|
|
internal void ReplaceLines(bool withBorder)
|
|
{
|
|
int maxWidth = this[0].Count;
|
|
for (int row = 1; row < Count - 1; row++)
|
|
{
|
|
RtfLine lineAbove = this[row - 1];
|
|
RtfLine line = this[row];
|
|
RtfLine lineBelow = this[row + 1];
|
|
for (int col = 1; col < maxWidth - 1; col++)
|
|
{
|
|
RtfPiece curPiece = line[col];
|
|
string current = curPiece.Text;
|
|
bool horizontal = _RegHorizontalCheck.IsMatch(current); // Horizontal
|
|
bool vertical = _RegVerticalCheck.IsMatch(current); // Vertical
|
|
if (horizontal || vertical)
|
|
{
|
|
int index = _RegRightCheck.IsMatch(line[col + 1].Text) ||
|
|
(horizontal && _RegRightCheck1.IsMatch(line[col + 1].Text)) ? 1 : 0; // Right
|
|
index += _RegAboveCheck.IsMatch(lineAbove[col].Text) ||
|
|
(vertical && _RegAboveCheck1.IsMatch(lineAbove[col].Text)) ? 2 : 0; // Above
|
|
index += _RegLeftCheck.IsMatch(line[col - 1].Text) ||
|
|
(horizontal && _RegLeftCheck1.IsMatch(line[col - 1].Text)) ? 4 : 0; // Left
|
|
index += _RegBelowCheck.IsMatch(lineBelow[col].Text) ||
|
|
(vertical && _RegBelowCheck1.IsMatch(lineBelow[col].Text)) ? 8 : 0; // Below
|
|
if (index > 0)
|
|
{
|
|
curPiece.Text = TableCharsU[index];
|
|
// this connects the lines to the border, so only do it if there is a border
|
|
if (withBorder)
|
|
{
|
|
if (vertical && row == 1) lineAbove[col].Text = TableCharsU[13]; // Upper Tee
|
|
if (vertical && row == Count - 2) lineBelow[col].Text = TableCharsU[7]; // Lower Tee
|
|
if (horizontal && col == 1) line[col - 1].Text = TableCharsU[11];// Left Tee
|
|
if (horizontal && col == maxWidth - 2) line[col + 1].Text = TableCharsU[14];// Right Tee
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
public class RtfLine : List<RtfPiece>
|
|
{
|
|
private static readonly Regex _RegRtfText = new Regex(@"((\\[^'u ]*|\\up[0-9]|\\ul|\\ulnone)+( |$|(?=\\['u])))?(<START\]|#Link.*?\[END>|\\'[0-9a-fA-F]{2}|\\u[0-9]*\?|[^\\]{1}|$|)");
|
|
public RtfLine(string text)
|
|
{
|
|
MatchCollection matches = _RegRtfText.Matches(text);
|
|
int nextIndex = 0;
|
|
foreach (Match match in matches)
|
|
{
|
|
if (nextIndex != match.Index) // This should never happen
|
|
{
|
|
throw new Exception(string.Format("\"RtfPiece Missing Content\",{0},{1},\"{2}\",\"{3}\",\"{4}\"", nextIndex, match.Index, text.Substring(nextIndex, match.Index - nextIndex), match.Groups[2].Value, match.Groups[4].Value));
|
|
//Console.WriteLine("\"RtfPiece Missing Content\",{0},{1},\"{2}\",\"{3}\",\"{4}\"", nextIndex, match.Index, text.Substring(nextIndex, match.Index - nextIndex), match.Groups[2].Value, match.Groups[4].Value);
|
|
//Console.WriteLine(text.Substring(nextIndex));
|
|
}
|
|
nextIndex = match.Index + match.Length;
|
|
string rtfPart = match.Groups[1].ToString();
|
|
if (match.Groups[1].ToString().TrimEnd(" ".ToCharArray()).Length + match.Groups[4].ToString().Length > 0)
|
|
Add(new RtfPiece(match.Groups[1].ToString().TrimEnd(" ".ToCharArray()), match.Groups[4].ToString()));
|
|
}
|
|
}
|
|
public RtfLine(int width, string left, string fill, string right)
|
|
{
|
|
Add(new RtfPiece("", left));
|
|
for (int i = 0; i < width; i++) Add(new RtfPiece("", fill)); // Pad
|
|
Add(new RtfPiece("", right));
|
|
}
|
|
public int MaximumWidth
|
|
{
|
|
get
|
|
{
|
|
int retval = 0;
|
|
foreach (RtfPiece myPiece in this)
|
|
retval += myPiece.MaximumWidth;
|
|
return retval;
|
|
}
|
|
}
|
|
public override string ToString()
|
|
{
|
|
StringBuilder sb = new StringBuilder();
|
|
foreach (RtfPiece myPiece in this)
|
|
sb.Append(myPiece.ToString());
|
|
return sb.ToString();
|
|
}
|
|
internal void Pad(int maxWidth, string edge)
|
|
{
|
|
int pad = maxWidth - MaximumWidth;
|
|
Insert(0, new RtfPiece("", edge)); // Left Edge
|
|
for (int i = 0; i < pad; i++) Add(new RtfPiece("", " ")); // Pad
|
|
Add(new RtfPiece("", edge)); // Right Edge
|
|
}
|
|
public new RtfPiece this[int index]
|
|
{
|
|
get
|
|
{
|
|
int j = 0;
|
|
foreach (RtfPiece myPiece in this)
|
|
{
|
|
if (myPiece.Text != "")
|
|
{
|
|
if (j == index) return myPiece;
|
|
j++;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
public class RtfPiece
|
|
{
|
|
public string Rtf { get; set; }
|
|
public string Text { get; set; } = "";
|
|
public int TextLength => Text.Length;
|
|
public int MaximumWidth => Text == "" ? 0 : 1;
|
|
private static readonly Regex _RegStartLink = new Regex(@"<START\]");
|
|
private static readonly Regex _RegEndLink = new Regex(@"#Link.*?\[END>");
|
|
public RtfPiece(string rtf, string text)
|
|
{
|
|
Rtf = rtf;
|
|
if (_RegStartLink.IsMatch(text) || _RegEndLink.IsMatch(text))
|
|
Rtf += (rtf == "" ? "" : " ") + text;
|
|
else
|
|
{
|
|
Rtf += rtf == "" ? "" : " ";
|
|
Text = text;
|
|
}
|
|
}
|
|
public override string ToString() => Rtf + Text;
|
|
}
|
|
}
|