SvgExtentions.cs 2.21 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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

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;
        }

        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)
        {
44
45
46
47
            var result = "";

            var currentCulture = Thread.CurrentThread.CurrentCulture;
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
Tebjan Halm's avatar
Tebjan Halm committed
48
49
            using (StringWriter str = new StringWriter())
            {
50
51
52
53
                using (XmlTextWriter xml = new XmlTextWriter(str))
                {
                    elem.WriteElement(xml);
                    result = str.ToString();
Tebjan Halm's avatar
Tebjan Halm committed
54

55
                }
Tebjan Halm's avatar
Tebjan Halm committed
56
            }
57
58
59
            Thread.CurrentThread.CurrentCulture = currentCulture;

            return result;
Tebjan Halm's avatar
Tebjan Halm committed
60
        }
61
62
63
64
65
        
        public static bool HasNonEmptyCustomAttribute(this SvgElement element, string name)
        {
        	return element.CustomAttributes.ContainsKey(name) && !string.IsNullOrEmpty(element.CustomAttributes[name]);
        }
66
67
68
69
70
        
        public static void ApplyRecursive(this SvgElement elem, Action<SvgElement> action)
        {
        	action(elem);
        	
71
        	if(!(elem is SvgDocument)) //don't apply action to subtree of documents
72
        	{
73
74
        		foreach (var element in elem.Children)
        		{
75
        			element.ApplyRecursive(action);
76
        		}
77
78
        	}
        }
Tebjan Halm's avatar
Tebjan Halm committed
79
80
    }
}