SvgExtentions.cs 3.02 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
83
84
85
86
87
88
89
            action(elem);

            if (!(elem is SvgDocument)) //don't apply action to subtree of documents
            {
                foreach (var element in elem.Children)
                {
                    element.ApplyRecursive(action);
                }
            }
90
        }
91

92
93
        public static void ApplyRecursiveDepthFirst(this SvgElement elem, Action<SvgElement> action)
        {
94
95
96
97
98
99
100
101
102
            if (!(elem is SvgDocument)) //don't apply action to subtree of documents
            {
                foreach (var element in elem.Children)
                {
                    element.ApplyRecursiveDepthFirst(action);
                }
            }

            action(elem);
103
        }
Tebjan Halm's avatar
Tebjan Halm committed
104
105
    }
}