diff --git a/src/WpfMath/Converters/SVGConverter.cs b/src/WpfMath/Converters/SVGConverter.cs index 1c71a9f7..ce7a7023 100644 --- a/src/WpfMath/Converters/SVGConverter.cs +++ b/src/WpfMath/Converters/SVGConverter.cs @@ -1,49 +1,47 @@ using System; +using System.Collections.Generic; using System.Diagnostics; using System.Globalization; -using System.Text; using System.Windows.Media; namespace WpfMath.Converters; public class SVGConverter { - private int m_nestedLevel = 0; - public string ConvertGeometry(Geometry geometry) { - StringBuilder svgOutput = new StringBuilder(); - if (geometry is GeometryGroup group) - { - AddGeometry(svgOutput, group); - } - return svgOutput.ToString(); + if (geometry is not GeometryGroup group) return string.Empty; + return string.Join(string.Empty, AddGeometry(group)); } - private void AddGeometry(StringBuilder svgString, GeometryGroup group) + private static IEnumerable AddGeometry(GeometryGroup group) { - m_nestedLevel++; if (!group.Transform.Value.IsIdentity) { - svgString.AppendFormat(CultureInfo.InvariantCulture, "" - , group.Transform.Value.M11, group.Transform.Value.M12 - , group.Transform.Value.M21, group.Transform.Value.M22, group.Transform.Value.OffsetX, group.Transform.Value.OffsetY); + yield return string.Format( + CultureInfo.InvariantCulture, + "", + group.Transform.Value.M11, group.Transform.Value.M12, + group.Transform.Value.M21, group.Transform.Value.M22, + group.Transform.Value.OffsetX, + group.Transform.Value.OffsetY + ); } foreach (Geometry geometry in group.Children) { switch (geometry) { case GeometryGroup childGroup: - AddGeometry(svgString, childGroup); + foreach (string str in AddGeometry(childGroup)) yield return str; break; case LineGeometry lineGeometry: - AddGeometry(svgString, lineGeometry); + foreach (string str in AddGeometry(lineGeometry)) yield return str; break; case PathGeometry path: - AddGeometry(svgString, path); + foreach (string str in AddGeometry(path)) yield return str; break; case RectangleGeometry rectangle: - AddGeometry(svgString, rectangle); + foreach (string str in AddGeometry(rectangle)) yield return str; break; default: Debug.Assert(false); @@ -52,14 +50,14 @@ private void AddGeometry(StringBuilder svgString, GeometryGroup group) } if (!group.Transform.Value.IsIdentity) { - svgString.AppendLine(""); - svgString.AppendLine(Environment.NewLine); + yield return ""; + yield return Environment.NewLine; + yield return Environment.NewLine; + yield return Environment.NewLine; } - - m_nestedLevel--; } - private void AddGeometry(StringBuilder svgString, LineGeometry line) + private static IEnumerable AddGeometry(LineGeometry line) { var x1 = line.StartPoint.X.ToString(CultureInfo.InvariantCulture); var y1 = line.StartPoint.Y.ToString(CultureInfo.InvariantCulture); @@ -67,18 +65,19 @@ private void AddGeometry(StringBuilder svgString, LineGeometry line) var x2 = line.EndPoint.X.ToString(CultureInfo.InvariantCulture); var y2 = line.EndPoint.Y.ToString(CultureInfo.InvariantCulture); - svgString.AppendLine(@$""); + yield return @$""; + yield return Environment.NewLine; } - private void AddGeometry(StringBuilder svgString, PathGeometry path) + private static IEnumerable AddGeometry(PathGeometry path) { - svgString.Append(""); - svgString.Append(Environment.NewLine); + yield return "\" fill = \"black\" />"; + yield return Environment.NewLine; } - private void AddGeometry(StringBuilder svgString, RectangleGeometry rectangle) + private static IEnumerable AddGeometry(RectangleGeometry rectangle) { - svgString.AppendFormat(CultureInfo.InvariantCulture, "" - , rectangle.Rect.Left, rectangle.Rect.Top, rectangle.Rect.Width, rectangle.Rect.Height); - svgString.Append(Environment.NewLine); + yield return string.Format( + CultureInfo.InvariantCulture, + "", + rectangle.Rect.Left, + rectangle.Rect.Top, + rectangle.Rect.Width, + rectangle.Rect.Height + ); + yield return Environment.NewLine; } }