SvgExtentions.cs 2.37 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;
        }
tebjan's avatar
tebjan committed
25
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
52
            var result = "";

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

60
                }
Tebjan Halm's avatar
Tebjan Halm committed
61
            }
62
63
64
            Thread.CurrentThread.CurrentCulture = currentCulture;

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