SvgFragment.cs 7.41 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
    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");
tebjan's avatar
tebjan committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
        
        private SvgUnit _x;
        private SvgUnit _y;
        
        /// <summary>
        /// Gets or sets the position where the left point of the svg should start.
        /// </summary>
        [SvgAttribute("x")]
        public SvgUnit X
        {
        	get { return _x; }
        	set
        	{
        		if(_x != value)
        		{
        			_x = value;
        			OnAttributeChanged(new AttributeEventArgs{ Attribute = "x", Value = value });
        		}
        	}
        }

        /// <summary>
        /// Gets or sets the position where the top point of the svg should start.
        /// </summary>
        [SvgAttribute("y")]
        public SvgUnit Y
        {
        	get { return _y; }
        	set
        	{
        		if(_y != value)
        		{
        			_y = value;
        			OnAttributeChanged(new AttributeEventArgs{ Attribute = "y", Value = value });
        		}
        	}
        }
davescriven's avatar
davescriven committed
58
59

        /// <summary>
60
        /// Gets or sets the width of the fragment.
davescriven's avatar
davescriven committed
61
62
63
64
65
        /// </summary>
        /// <value>The width.</value>
        [SvgAttribute("width")]
        public SvgUnit Width
        {
66
67
            get { return this.Attributes.GetAttribute<SvgUnit>("width"); }
			set { this.Attributes["width"] = value; }
davescriven's avatar
davescriven committed
68
69
70
        }

        /// <summary>
71
        /// Gets or sets the height of the fragment.
davescriven's avatar
davescriven committed
72
73
74
75
76
        /// </summary>
        /// <value>The height.</value>
        [SvgAttribute("height")]
        public SvgUnit Height
        {
77
78
            get { return this.Attributes.GetAttribute<SvgUnit>("height"); }
			set { this.Attributes["height"] = value; }
davescriven's avatar
davescriven committed
79
80
        }

81
82
83
84
85
86
87
		[SvgAttribute("overflow")]
		public virtual SvgOverflow Overflow
		{
			get { return this.Attributes.GetAttribute<SvgOverflow>("overflow"); }
			set { this.Attributes["overflow"] = value; }
		}

davescriven's avatar
davescriven committed
88
89
90
91
92
93
94
        /// <summary>
        /// Gets or sets the viewport of the element.
        /// </summary>
        /// <value></value>
        [SvgAttribute("viewBox")]
        public SvgViewBox ViewBox
        {
95
96
            get { return this.Attributes.GetAttribute<SvgViewBox>("viewBox"); }
            set { this.Attributes["viewBox"] = value; }
davescriven's avatar
davescriven committed
97
        }
98
99
100
101
102
        
        /// <summary>
        /// Gets or sets the aspect of the viewport.
        /// </summary>
        /// <value></value>
103
		[SvgAttribute("preserveAspectRatio")]
104
105
        public SvgAspectRatio AspectRatio
        {
106
107
			get { return this.Attributes.GetAttribute<SvgAspectRatio>("preserveAspectRatio"); }
			set { this.Attributes["preserveAspectRatio"] = value; }
108
        }
davescriven's avatar
davescriven committed
109
110

        /// <summary>
111
        /// Applies the required transforms to <see cref="SvgRenderer"/>.
davescriven's avatar
davescriven committed
112
        /// </summary>
113
114
        /// <param name="renderer">The <see cref="SvgRenderer"/> to be transformed.</param>
        protected internal override void PushTransforms(SvgRenderer renderer)
davescriven's avatar
davescriven committed
115
        {
116
            base.PushTransforms(renderer);
davescriven's avatar
davescriven committed
117
118
119

            if (!this.ViewBox.Equals(SvgViewBox.Empty))
            {
120
121
122
123
124
125
126
				float fScaleX = this.Width.ToDeviceValue() / this.ViewBox.Width;
				float fScaleY = this.Height.ToDeviceValue() / this.ViewBox.Height;
				float fMinX = -this.ViewBox.MinX;
				float fMinY = -this.ViewBox.MinY;

				if (AspectRatio.Align != SvgPreserveAspectRatio.none)
				{
127
128
129
130
131
132
133
134
135
136
					if (AspectRatio.Slice)
					{
						fScaleX = Math.Max(fScaleX, fScaleY);
						fScaleY = Math.Max(fScaleX, fScaleY);
					}
					else
					{
						fScaleX = Math.Min(fScaleX, fScaleY);
						fScaleY = Math.Min(fScaleX, fScaleY);
					}
137
138
139
140
					float fViewMidX = (this.ViewBox.Width / 2) * fScaleX;
					float fViewMidY = (this.ViewBox.Height / 2) * fScaleY;
					float fMidX = this.Width.ToDeviceValue() / 2;
					float fMidY = this.Height.ToDeviceValue() / 2;
davescriven's avatar
davescriven committed
141

142
143
					switch (AspectRatio.Align)
					{
Brian C. Barnes's avatar
Brian C. Barnes committed
144
						case SvgPreserveAspectRatio.xMinYMin:
145
							break;
Brian C. Barnes's avatar
Brian C. Barnes committed
146
						case SvgPreserveAspectRatio.xMidYMin:
147
148
							fMinX += (fMidX - fViewMidX) / fScaleX;
							break;
Brian C. Barnes's avatar
Brian C. Barnes committed
149
						case SvgPreserveAspectRatio.xMaxYMin:
150
							fMinX += (this.Width.ToDeviceValue() / fScaleX) - this.ViewBox.Width;
151
							break;
Brian C. Barnes's avatar
Brian C. Barnes committed
152
						case SvgPreserveAspectRatio.xMinYMid:
153
154
							fMinY += (fMidY - fViewMidY) / fScaleY;
							break;
Brian C. Barnes's avatar
Brian C. Barnes committed
155
						case SvgPreserveAspectRatio.xMidYMid:
156
157
158
							fMinX += (fMidX - fViewMidX) / fScaleX;
							fMinY += (fMidY - fViewMidY) / fScaleY;
							break;
Brian C. Barnes's avatar
Brian C. Barnes committed
159
						case SvgPreserveAspectRatio.xMaxYMid:
160
							fMinX += (this.Width.ToDeviceValue() / fScaleX) - this.ViewBox.Width;
161
162
							fMinY += (fMidY - fViewMidY) / fScaleY;
							break;
Brian C. Barnes's avatar
Brian C. Barnes committed
163
						case SvgPreserveAspectRatio.xMinYMax:
164
							fMinY += (this.Height.ToDeviceValue() / fScaleY) - this.ViewBox.Height;
165
							break;
Brian C. Barnes's avatar
Brian C. Barnes committed
166
						case SvgPreserveAspectRatio.xMidYMax:
167
							fMinX += (fMidX - fViewMidX) / fScaleX;
168
							fMinY += (this.Height.ToDeviceValue() / fScaleY) - this.ViewBox.Height;
169
							break;
Brian C. Barnes's avatar
Brian C. Barnes committed
170
						case SvgPreserveAspectRatio.xMaxYMax:
171
172
							fMinX += (this.Width.ToDeviceValue() / fScaleX) - this.ViewBox.Width;
							fMinY += (this.Height.ToDeviceValue() / fScaleY) - this.ViewBox.Height;
173
174
175
176
177
178
							break;
						default:
							break;
					}
				}

179
180
181
            	renderer.TranslateTransform(_x, _y);
				renderer.TranslateTransform(fMinX, fMinY);
				renderer.ScaleTransform(fScaleX, fScaleY);
davescriven's avatar
davescriven committed
182
183
            }
        }
Tebjan Halm's avatar
Tebjan Halm committed
184
185
186
187
188
189
190
191
192
193
194
195
196
        
        /// <summary>
        /// Gets the <see cref="GraphicsPath"/> for this element.
        /// </summary>
        /// <value></value>
        public GraphicsPath Path
        {
            get 
            { 
            	var path = new GraphicsPath();

            	AddPaths(this, path);
  
197
            	return path;
Tebjan Halm's avatar
Tebjan Halm committed
198
199
            }
        }
200
201
202
203
204
205
206
207
208
209
210
211
        
        /// <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
212
213
214
215
216
217

        /// <summary>
        /// Initializes a new instance of the <see cref="SvgFragment"/> class.
        /// </summary>
        public SvgFragment()
        {
tebjan's avatar
tebjan committed
218
219
        	_x = 0.0f;
            _y = 0.0f;
220
221
            this.Height = new SvgUnit(SvgUnitType.Percentage, 100.0f);
            this.Width = new SvgUnit(SvgUnitType.Percentage, 100.0f);
davescriven's avatar
davescriven committed
222
            this.ViewBox = SvgViewBox.Empty;
Brian C. Barnes's avatar
Brian C. Barnes committed
223
            this.AspectRatio = new SvgAspectRatio(SvgPreserveAspectRatio.xMidYMid);
davescriven's avatar
davescriven committed
224
        }
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241


		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
242
243
    }
}