SvgFragment.cs 4.45 KB
Newer Older
davescriven's avatar
davescriven committed
1
2
3
4
5
6
7
8
9
10
11
12
13
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Drawing.Drawing2D;
using System.Drawing;
using System.ComponentModel;

namespace Svg
{
    /// <summary>
    /// An <see cref="SvgFragment"/> represents an SVG fragment that can be the root element or an embedded fragment of an SVG document.
    /// </summary>
14
    [SvgElement("svg")]
davescriven's avatar
davescriven committed
15
16
17
18
19
20
21
22
    public class SvgFragment : SvgElement, ISvgViewPort
    {
        /// <summary>
        /// Gets the SVG namespace string.
        /// </summary>
        public static readonly Uri Namespace = new Uri("http://www.w3.org/2000/svg");

        /// <summary>
23
        /// Gets or sets the width of the fragment.
davescriven's avatar
davescriven committed
24
25
26
27
28
        /// </summary>
        /// <value>The width.</value>
        [SvgAttribute("width")]
        public SvgUnit Width
        {
29
30
            get { return this.Attributes.GetAttribute<SvgUnit>("width"); }
			set { this.Attributes["width"] = value; }
davescriven's avatar
davescriven committed
31
32
33
        }

        /// <summary>
34
        /// Gets or sets the height of the fragment.
davescriven's avatar
davescriven committed
35
36
37
38
39
        /// </summary>
        /// <value>The height.</value>
        [SvgAttribute("height")]
        public SvgUnit Height
        {
40
41
            get { return this.Attributes.GetAttribute<SvgUnit>("height"); }
			set { this.Attributes["height"] = value; }
davescriven's avatar
davescriven committed
42
43
        }

44
45
46
47
48
49
50
		[SvgAttribute("overflow")]
		public virtual SvgOverflow Overflow
		{
			get { return this.Attributes.GetAttribute<SvgOverflow>("overflow"); }
			set { this.Attributes["overflow"] = value; }
		}

davescriven's avatar
davescriven committed
51
52
53
54
55
56
57
        /// <summary>
        /// Gets or sets the viewport of the element.
        /// </summary>
        /// <value></value>
        [SvgAttribute("viewBox")]
        public SvgViewBox ViewBox
        {
58
59
            get { return this.Attributes.GetAttribute<SvgViewBox>("viewBox"); }
            set { this.Attributes["viewBox"] = value; }
davescriven's avatar
davescriven committed
60
        }
61
62
63
64
65
66
67
68
        
        /// <summary>
        /// Gets or sets the aspect of the viewport.
        /// </summary>
        /// <value></value>
        [SvgAttribute("preserveAspectRatio")]
        public SvgAspectRatio AspectRatio
        {
69
70
            get {return this.Attributes.GetAttribute<SvgAspectRatio>("preserveAspectRatio"); }
            set { this.Attributes["preserveAspectRatio"] = value; }
71
        }
davescriven's avatar
davescriven committed
72
73

        /// <summary>
74
        /// Applies the required transforms to <see cref="SvgRenderer"/>.
davescriven's avatar
davescriven committed
75
        /// </summary>
76
77
        /// <param name="renderer">The <see cref="SvgRenderer"/> to be transformed.</param>
        protected internal override void PushTransforms(SvgRenderer renderer)
davescriven's avatar
davescriven committed
78
        {
79
            base.PushTransforms(renderer);
davescriven's avatar
davescriven committed
80
81
82

            if (!this.ViewBox.Equals(SvgViewBox.Empty))
            {
Tebjan Halm's avatar
Tebjan Halm committed
83
                renderer.TranslateTransform(-this.ViewBox.MinX, -this.ViewBox.MinY, MatrixOrder.Append);
davescriven's avatar
davescriven committed
84

85
                renderer.ScaleTransform(this.Width.ToDeviceValue() / this.ViewBox.Width, this.Height.ToDeviceValue() / this.ViewBox.Height, MatrixOrder.Append);
davescriven's avatar
davescriven committed
86
87
            }
        }
Tebjan Halm's avatar
Tebjan Halm committed
88
89
90
91
92
93
94
95
96
97
98
99
100
        
        /// <summary>
        /// Gets the <see cref="GraphicsPath"/> for this element.
        /// </summary>
        /// <value></value>
        public GraphicsPath Path
        {
            get 
            { 
            	var path = new GraphicsPath();

            	AddPaths(this, path);
  
101
            	return path;
Tebjan Halm's avatar
Tebjan Halm committed
102
103
            }
        }
104
105
106
107
108
109
110
111
112
113
114
115
        
        /// <summary>
        /// Gets the bounds of the svg element.
        /// </summary>
        /// <value>The bounds.</value>
        public RectangleF Bounds 
        { 
        	get
        	{
        		return this.Path.GetBounds();
        	}
        }
davescriven's avatar
davescriven committed
116
117
118
119
120
121

        /// <summary>
        /// Initializes a new instance of the <see cref="SvgFragment"/> class.
        /// </summary>
        public SvgFragment()
        {
122
123
            this.Height = new SvgUnit(SvgUnitType.Percentage, 100.0f);
            this.Width = new SvgUnit(SvgUnitType.Percentage, 100.0f);
davescriven's avatar
davescriven committed
124
            this.ViewBox = SvgViewBox.Empty;
125
            this.AspectRatio = new SvgAspectRatio(SvgPreserveAspectRatio.None);
davescriven's avatar
davescriven committed
126
        }
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143


		public override SvgElement DeepCopy()
		{
			return DeepCopy<SvgFragment>();
		}

		public override SvgElement DeepCopy<T>()
		{
			var newObj = base.DeepCopy<T>() as SvgFragment;
			newObj.Height = this.Height;
			newObj.Width = this.Width;
			newObj.Overflow = this.Overflow;
			newObj.ViewBox = this.ViewBox;
			newObj.AspectRatio = this.AspectRatio;
			return newObj;
		}
davescriven's avatar
davescriven committed
144
145
    }
}