using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Drawing;
using System.Xml;
namespace fmtxml
{
public partial class RtfToSvg
{
public RtfToSvg(string rtfFileName)
{
RtfFileName = rtfFileName;
_FormatFolder = RtfFile.Directory;
}
private static DirectoryInfo _FormatFolder;
public RtfToSvg(FileInfo rtfFile)
{
RtfFile = rtfFile;
_FormatFolder = RtfFile.Directory;
}
private bool? _IsGenMac;
public bool IsGenMac
{
get
{
if (_IsGenMac == null)
Convert();
return (bool)_IsGenMac;
}
set { _IsGenMac = value; }
}
private string _RtfFileName;
public string RtfFileName
{
get { return _RtfFileName; }
set
{
_RtfFileName = value;
_RtfFile = new FileInfo(_RtfFileName);
}
}
private FileInfo _RtfFile;
public FileInfo RtfFile
{
get { return _RtfFile; }
set
{
_RtfFile = value;
_RtfFileName = _RtfFile.FullName;
}
}
private string _Svg;
public string Svg
{
get
{
if (_Svg == null)
Convert();
return _Svg;
}
}
private string Convert()
{
string buff = LoadFile(RtfFile);// Load Text
_Svg = CreateSvg(RtfFile, buff);// Convert to SVG
return _Svg;
}
///
/// Load the file into a string by bytes
///
///
///
private static string LoadFile(FileInfo file)
{
byte[] bytes = new byte[file.Length];
using (FileStream fs = file.OpenRead())
{
fs.Read(bytes, 0, (int)file.Length);
fs.Close();
}
StringBuilder sb = new StringBuilder();
foreach (byte byt in bytes)
sb.Append((char)byt);
return sb.ToString();
}
private const string _TripleBang = "\xAD\xAD\xAD\xA8\xA8";
Regex regLines = new Regex(@"([^\r\n]*)?\r?\n", RegexOptions.Singleline);
// If processing any of the RGE (except RGEDEV) C0 macros, adjust the x location of the ellipse, so that
// 32bit printing matches 16bit. This flag shows that we're in a format that needs this adjustment.
private static bool _InRge = false;
private static bool _DoingRgeC0 = false;
private string CreateSvg(FileInfo file, string buff)
{
IsGenMac = (buff.Substring(0, 5) == _TripleBang);
if(!IsGenMac)
return null;
_InRge = (file.Name.ToUpper().Contains("RGE") && !file.Name.ToUpper().Contains("RGEDEV"));
StringBuilder sb = new StringBuilder();
MatchCollection lines = regLines.Matches(buff);
bool svg = false;
bool group = false;
bool hasC0 = false;
foreach (Match line in lines)
{
string text = line.Groups[1].Value;
if (text == _TripleBang)
if (!svg)
svg = AddSvgPrefix(file, sb, svg);
else
AddSvgSuffix(sb, hasC0);
else if (text.StartsWith("\xAD\xAD"))
{
hasC0 |= AddGroupStart(sb, ref group, text).ToLower() == "c0";
_DoingRgeC0 = (hasC0 && _InRge);
}
else if (text == "END")
{
group = AddGroupEnd(sb, group);
_DoingRgeC0 = false;
}
else if (text != "")
ProcessCommand(sb, text, file);
}
return sb.ToString();
}
private static void AddSvgSuffix(StringBuilder sb, bool hasC0)
{
if (!hasC0)
{
AddC0(sb);
}
sb.Append(string.Format("\r\n"));
}
private static bool AddGroupEnd(StringBuilder sb, bool group)
{
if (group) sb.Append(" \r\n");
group = false;
_Xoff = 0;
_Yoff = 0;
return group;
}
private static string AddGroupStart(StringBuilder sb, ref bool group, string text)
{
AddGroupEnd(sb, group);
group = true;
string grpID = text.Substring(2).ToUpper();
sb.Append(string.Format(" \r\n", grpID));
return grpID;
}
private static void AddC0(StringBuilder sb)
{
// note that cx = 1 has been tested with NSP.
sb.Append(" \r\n");
sb.Append(" \r\n");
sb.Append(" \r\n");
}
private static bool AddSvgPrefix(FileInfo file, StringBuilder sb, bool svg)
{
sb.Append("\r\n");
sb.Append("\r\n");
sb.Append("