SvgExtentions.cs 4.24 KB
Newer Older
Tebjan Halm's avatar
Tebjan Halm committed
1
2
3
4
5
6
7
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;
using System.Xml;
8
9
using System.Threading;
using System.Globalization;
Tebjan Halm's avatar
Tebjan Halm committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

namespace Svg
{
    /// <summary>
    /// Svg helpers
    /// </summary>
    public static class SvgExtentions
    {
        public static void SetRectangle(this SvgRectangle r, RectangleF bounds)
        {
            r.X = bounds.X;
            r.Y = bounds.Y;
            r.Width = bounds.Width;
            r.Height = bounds.Height;
        }
25

tebjan's avatar
tebjan committed
26
27
28
29
        public static RectangleF GetRectangle(this SvgRectangle r)
        {
            return new RectangleF(r.X, r.Y, r.Width, r.Height);
        }
Tebjan Halm's avatar
Tebjan Halm committed
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

        public static string GetXML(this SvgDocument doc)
        {
            var ret = "";

            using (var ms = new MemoryStream())
            {
                doc.Write(ms);
                ms.Position = 0;
                var sr = new StreamReader(ms);
                ret = sr.ReadToEnd();
                sr.Close();
            }

            return ret;
        }

        public static string GetXML(this SvgElement elem)
        {
49
50
51
            var result = "";

            var currentCulture = Thread.CurrentThread.CurrentCulture;
52
            try
Tebjan Halm's avatar
Tebjan Halm committed
53
            {
54
55
                Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
                using (StringWriter str = new StringWriter())
56
                {
57
58
59
60
                    using (XmlTextWriter xml = new XmlTextWriter(str))
                    {
                        elem.Write(xml);
                        result = str.ToString();
Tebjan Halm's avatar
Tebjan Halm committed
61

62
                    }
63
                }
Tebjan Halm's avatar
Tebjan Halm committed
64
            }
65
66
67
68
69
70
            finally
            {
                // Make sure to set back the old culture even an error occurred.
                Thread.CurrentThread.CurrentCulture = currentCulture;
            }
            
71
            return result;
Tebjan Halm's avatar
Tebjan Halm committed
72
        }
73

74
75
        public static bool HasNonEmptyCustomAttribute(this SvgElement element, string name)
        {
76
            return element.CustomAttributes.ContainsKey(name) && !string.IsNullOrEmpty(element.CustomAttributes[name]);
77
        }
78

79
80
        public static void ApplyRecursive(this SvgElement elem, Action<SvgElement> action)
        {
81
82
            foreach (var e in elem
                .Traverse(e => e.Children))
83
            {
84
                action(e);
85
            }
86
        }
87

88
89
        public static void ApplyRecursiveDepthFirst(this SvgElement elem, Action<SvgElement> action)
        {
90
91
            foreach (var e in elem
                .TraverseDepthFirst(e => e.Children))
92
            {
93
94
95
96
97
98
99
100
101
102
103
104
105
106
                action(e);
            }
        }

        public static IEnumerable<T> Traverse<T>(this IEnumerable<T> items, Func<T, IEnumerable<T>> childrenSelector)
        {
            if (childrenSelector == null) throw new ArgumentNullException(nameof(childrenSelector));

            var itemQueue = new Queue<T>(items);
            while (itemQueue.Count > 0)
            {
                var current = itemQueue.Dequeue();
                yield return current;
                foreach (var child in childrenSelector(current) ?? Enumerable.Empty<T>()) itemQueue.Enqueue(child);
107
            }
108
        }
109

110
111
112
113
114
115
116
117
118
119
120
121
122
123
        public static IEnumerable<T> Traverse<T>(this T root, Func<T, IEnumerable<T>> childrenSelector)
            => Enumerable.Repeat(root, 1).Traverse(childrenSelector);

        public static IEnumerable<T> TraverseDepthFirst<T>(this IEnumerable<T> items, Func<T, IEnumerable<T>> childrenSelector)
        {
            if (childrenSelector == null) throw new ArgumentNullException(nameof(childrenSelector));
            var itemStack = new Stack<T>(items ?? Enumerable.Empty<T>());

            while (itemStack.Count > 0)
            {
                var current = itemStack.Pop();
                yield return current;
                foreach (var child in childrenSelector(current) ?? Enumerable.Empty<T>()) itemStack.Push(child);
            }
124
        }
125
126
127

        public static IEnumerable<T> TraverseDepthFirst<T>(this T root, Func<T, IEnumerable<T>> childrenSelector)
            => Enumerable.Repeat(root, 1).TraverseDepthFirst(childrenSelector);
Tebjan Halm's avatar
Tebjan Halm committed
128
129
    }
}