SvgFragment.cs 4.4 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
    public class SvgFragment : SvgElement, ISvgViewPort
    {
        private SvgUnit _width;
        private SvgUnit _height;
        private SvgViewBox _viewBox;
20
        private SvgAspectRatio _aspect;
davescriven's avatar
davescriven committed
21
22
23
24
25
26
27

        /// <summary>
        /// Gets the SVG namespace string.
        /// </summary>
        public static readonly Uri Namespace = new Uri("http://www.w3.org/2000/svg");

        /// <summary>
28
        /// Gets or sets the width of the fragment.
davescriven's avatar
davescriven committed
29
30
31
32
33
34
35
36
37
38
        /// </summary>
        /// <value>The width.</value>
        [SvgAttribute("width")]
        public SvgUnit Width
        {
            get { return this._width; }
            set { this._width = value; }
        }

        /// <summary>
39
        /// Gets or sets the height of the fragment.
davescriven's avatar
davescriven committed
40
41
42
43
44
45
46
47
48
        /// </summary>
        /// <value>The height.</value>
        [SvgAttribute("height")]
        public SvgUnit Height
        {
            get { return this._height; }
            set { this._height = value; }
        }

49
50
51
52
53
54
55
		[SvgAttribute("overflow")]
		public virtual SvgOverflow Overflow
		{
			get { return this.Attributes.GetAttribute<SvgOverflow>("overflow"); }
			set { this.Attributes["overflow"] = value; }
		}

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

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

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

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

            	AddPaths(this, path);
  
106
            	return path;
Tebjan Halm's avatar
Tebjan Halm committed
107
108
            }
        }
109
110
111
112
113
114
115
116
117
118
119
120
121
        
        /// <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
122
123
124
125
126
127
128

        /// <summary>
        /// Initializes a new instance of the <see cref="SvgFragment"/> class.
        /// </summary>
        public SvgFragment()
        {
            this._height = new SvgUnit(SvgUnitType.Percentage, 100.0f);
Tebjan Halm's avatar
Tebjan Halm committed
129
            this._width = new SvgUnit(SvgUnitType.Percentage, 100.0f);
davescriven's avatar
davescriven committed
130
            this.ViewBox = SvgViewBox.Empty;
131
            this.AspectRatio = new SvgAspectRatio(SvgPreserveAspectRatio.None);
davescriven's avatar
davescriven committed
132
        }
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149


		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
150
151
    }
}