using System.Drawing; namespace RenderPath; using System.Xml; public class SVGRenderer : Renderer { private readonly XmlDocument _image; private XmlElement _document; public SVGRenderer(int width, int height) { _image = new XmlDocument(); CreateTree(width, height); } public SVGRenderer(XmlDocument renderOver) { _image = renderOver; _document = _image.GetElementById("svg")!; } private void CreateTree(int width, int height) { XmlDeclaration xmlDeclaration = _image.CreateXmlDeclaration( "1.0", "UTF-8", null ); _image.InsertBefore(xmlDeclaration, _image.DocumentElement); XmlElement pElement = _image.CreateElement("svg"); XmlAttribute xmlns = _image.CreateAttribute("xmlns"); xmlns.Value = "http://www.w3.org/2000/svg"; pElement.Attributes.Append(xmlns); XmlAttribute aWidth = _image.CreateAttribute("width"); aWidth.Value = width.ToString(); pElement.Attributes.Append(aWidth); XmlAttribute aHeight = _image.CreateAttribute("height"); aHeight.Value = height.ToString(); pElement.Attributes.Append(aHeight); _image.AppendChild(pElement); _document = pElement; } public override void DrawLine(float x1, float y1, float x2, float y2, int width, Color color) { XmlElement newLine = _image.CreateElement("line"); XmlAttribute aX1 = _image.CreateAttribute("x1"); aX1.Value = Math.Floor(x1).ToString("0"); newLine.Attributes.Append(aX1); XmlAttribute aY1 = _image.CreateAttribute("y1"); aY1.Value = Math.Floor(y1).ToString("0"); newLine.Attributes.Append(aY1); XmlAttribute aX2 = _image.CreateAttribute("x2"); aX2.Value = Math.Floor(x2).ToString("0"); newLine.Attributes.Append(aX2); XmlAttribute aY2 = _image.CreateAttribute("y2"); aY2.Value = Math.Floor(y2).ToString("0"); newLine.Attributes.Append(aY2); XmlAttribute stroke = _image.CreateAttribute("stroke-width"); stroke.Value = width.ToString(); newLine.Attributes.Append(stroke); XmlAttribute aColor = _image.CreateAttribute("stroke"); aColor.Value = HexFromColor(color); newLine.Attributes.Append(aColor); _document.AppendChild(newLine); } public override void DrawDot(float x, float y, int radius, Color color) { XmlElement newCircle = _image.CreateElement("circle"); XmlAttribute aX = _image.CreateAttribute("cx"); aX.Value = Math.Floor(x).ToString("0"); newCircle.Attributes.Append(aX); XmlAttribute aY = _image.CreateAttribute("cy"); aY.Value = Math.Floor(y).ToString("0"); newCircle.Attributes.Append(aY); XmlAttribute aR = _image.CreateAttribute("r"); aR.Value = radius.ToString(); newCircle.Attributes.Append(aR); XmlAttribute fill = _image.CreateAttribute("fill"); fill.Value = HexFromColor(color); newCircle.Attributes.Append(fill); _document.AppendChild(newCircle); } public override void Save(string path) { _image.Save($"{path}.svg"); } private static string HexFromColor(Color color) { return $"#{color.R:X2}{color.G:X2}{color.B:X2}"; } }