using System;
using System.IO;
using System.Xml;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
namespace fmtxml
{
	/// 
	/// Convert genmac xml files to svg. svg (Scalar Vector Graphics
	/// in XML format), so that genmac can be used in the 32-bit proms
	/// system. The generated files will be used by developers and
	/// customer service persons to do macros.
	/// 
	public class GenXmlToSvg
	{
		private string genName;
		private XmlDocument xmlDoc;
		private XmlDocument xmlOutDoc;
		private XmlElement topOutElement;
		private int Convertn = 300, I = 300, C = 118, D = 1, N = 0;
		private float Wid = 3;
		private int RTFFontSize = 20;
		private int RTFFontFamily = 3;
		private string [] FontChoice = 
		{
			"Times New Roman",
			"VESymb XXXXXX",
			"VolianDraw XXXXXX",
			"Prestige Elite Tall",
			"Courier New",
			"Arial",
			"Letter Gothic",
			"Times New Roman",
			"Letter Gothic Tall",
			"Letter Gothic Tall",
			"Gothic Ultra",
			"VolianScript"
		};
		private string [] OldName = 
		{
			"BOX1",
			"BOX2",
			"BOX3",
			"BOX4",
			"BOX5",
			"HDR1",
			"HDR2",
			"HDR3",
			"HDR4",
			"HDR5",
			"BOLDX",
			"CNUM",
			"CHKBOX",
			"CHKBOX2",
			"CHKBOX3",
			"CHKBOX4",
			"CHKBOX5",
			"CHKBOX6",
			"CHKBOX7",
			"CHKBOX8",
			"CHKBOX9",
			"CHKBOX10",
			"CHKBOX11",
			"CHKBOX12",
			"CHKBOX13",
			"CHKBOX14",
			"CHKBOX15",
			"CHKBOX16",
			"CHKBOX17",
			"PLNTPLMAC",
			"PLNTPLMAC2",
			"GRAPHMAC0",
			"GRAPHMAC1",
			"RIGHTCHECKOFF",
			"HPGLCOMMAND0",
			"HPGLCOMMAND1",
			"HPGLCOMMAND2",
			"HPGLCOMMAND3",
			"HPGLCOMMAND4",
			"HPGLCOMMAND5",
			"HPGLCOMMAND6",
			"HPGLCOMMAND7",
			"HPGLCOMMAND8",
			"HPGLCOMMAND9",
			"LTCO",
			"SECDBLBXVERT",
			"BOX6",
			"BOX7",
			"BOX8",
			"ACAS_CHECKBOX",
			"ACAS_CHECKLINE",
		};
		private string [] NewName = 
		{
			"B1",
			"B2",
			"B3",
			"B4",
			"B5",
			"H1",
			"H2",
			"H3",
			"H4",
			"H5",
			"m33",
			"C0",
			"C1",
			"C2",
			"C3",
			"C4",
			"C5",
			"C6",
			"C7",
			"C8",
			"C9",
			"C10",
			"C11",
			"C12",
			"C13",
			"C14",
			"C15",
			"C16",
			"C17",
			"m18",
			"m19",
			"m20",
			"m21",
			"C22",
			"m23",
			"m24",
			"m25",
			"m26",
			"m27",
			"m28",
			"m29",
			"m30",
			"m31",
			"m32",
			"m20",
			"m34",
			"B6",
			"B7",
			"B8",
			"m35",
			"m36",
		};
		private int RTFBUI = 0;
		private int Sp = -1;
		private float cx = 0;
		private float cy = 0;
		private int fidx=0;
		private int [] fontfam;
		private int [] fontsiz;
		private float [] PPx;
		private float [] PPy;
		
		// the following define RTFBUI values
		private static int BOLD = 0x01;
		private static int UNDERLINE = 0x02;
		private static int ITALICS = 0x04;
		private string MyResultPath;
		/* 
		 *  The following svg group must be added for WCN2 for its box on the cover page:
		 * 
		 
			 
			
			
			
			
			
		
		 */
		public GenXmlToSvg(string nm, string resPath)
		{
			MyResultPath = resPath;
			genName = nm;
			try
			{
				fontfam = new int[20];
				fontsiz = new int[20];
				PPx = new float[12];
				PPy = new float[12];
				ReadXml();
				ConvertGenXmlToDIXml();
				WriteXml();
			}
			catch (Exception ex)
			{
				Console.WriteLine("Genmac.xml name = " + nm + ". Error is " +  ex.Message);
			}	
		}
		private float CPoint(float x)
		{
			if (Convertn == C)
			{
				// convert from x (in centimeters) to inches & then to points
				return (float) (.3937 * x * 72);
			}	// convert from x (in inches) to points
			else if (Convertn == I)
				return (float)(x * 72);
			else if (Convertn == N)
				return (float)x;
			else  // convert from x (in dots) to twips & then points
				return (float)(((4.8 * x + 0.5)/1440f)*72);
		}
		private void WriteXml()
		{
			string path = MyResultPath + @"\genmacall\" + genName.Substring(0, genName.LastIndexOf(".")) + ".svg";
			XmlTextWriter writer = new XmlTextWriter(path,System.Text.Encoding.Unicode);
			writer.Formatting=System.Xml.Formatting.Indented;
			xmlOutDoc.Save(writer);
		}
		private void ReadXml()
		{
			string fnm = MyResultPath + @"\genmacall\convert\" + genName;
			xmlDoc = new XmlDocument();
			XmlTextReader reader = new XmlTextReader(fnm);
			xmlDoc.Load(reader);
			reader.Close();
		}
		private void ConvertGenXmlToDIXml()
		{
			// first create the document for the svg version of the macro files
			// and add the top svg element.
			xmlOutDoc = new XmlDocument();
			topOutElement = xmlOutDoc.CreateElement("svg");
			topOutElement.SetAttribute("width","8in");
			topOutElement.SetAttribute("height","10in");
			topOutElement.SetAttribute("viewBox","0 0 576 720");
			//			topOutNode.Attributes.Append(attr);
			//			topOutNode.Attributes.Append