SvgFragment.cs 8.82 KB
Newer Older
davescriven's avatar
davescriven committed
1
2
using System;
using System.Drawing;
James Welle's avatar
James Welle committed
3
using System.Drawing.Drawing2D;
davescriven's avatar
davescriven committed
4
5
6
7
8
9

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>
10
    [SvgElement("svg")]
James Welle's avatar
James Welle committed
11
    public class SvgFragment : SvgElement, ISvgViewPort, ISvgBoundable
davescriven's avatar
davescriven committed
12
13
14
15
16
    {
        /// <summary>
        /// Gets the SVG namespace string.
        /// </summary>
        public static readonly Uri Namespace = new Uri("http://www.w3.org/2000/svg");
James Welle's avatar
James Welle committed
17

Dan Backes's avatar
Dan Backes committed
18
        RectangleF ISvgBoundable.CalculateBounds()
James Welle's avatar
James Welle committed
19
        {
Dan Backes's avatar
Dan Backes committed
20
            return new RectangleF(PointF.Empty, GetDimensions());
James Welle's avatar
James Welle committed
21
22
        }

tebjan's avatar
tebjan committed
23
24
25
26
27
28
29
30
31
        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
        {
James Welle's avatar
James Welle committed
32
33
34
35
36
37
38
39
40
            get { return _x; }
            set
            {
                if(_x != value)
                {
                    _x = value;
                    OnAttributeChanged(new AttributeEventArgs{ Attribute = "x", Value = value });
                }
            }
tebjan's avatar
tebjan committed
41
42
43
44
45
46
47
48
        }

        /// <summary>
        /// Gets or sets the position where the top point of the svg should start.
        /// </summary>
        [SvgAttribute("y")]
        public SvgUnit Y
        {
James Welle's avatar
James Welle committed
49
50
51
52
53
54
55
56
57
            get { return _y; }
            set
            {
                if(_y != value)
                {
                    _y = value;
                    OnAttributeChanged(new AttributeEventArgs{ Attribute = "y", Value = value });
                }
            }
tebjan's avatar
tebjan committed
58
        }
davescriven's avatar
davescriven committed
59
60

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

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

James Welle's avatar
James Welle committed
82
83
84
85
86
87
        [SvgAttribute("overflow")]
        public virtual SvgOverflow Overflow
        {
            get { return this.Attributes.GetAttribute<SvgOverflow>("overflow"); }
            set { this.Attributes["overflow"] = value; }
        }
88

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

111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
        /// <summary>
        /// Refers to the size of the font from baseline to baseline when multiple lines of text are set solid in a multiline layout environment.
        /// </summary>
        [SvgAttribute("font-size")]
        public virtual SvgUnit FontSize
        {
            get { return (this.Attributes["font-size"] == null) ? SvgUnit.Empty : (SvgUnit)this.Attributes["font-size"]; }
            set { this.Attributes["font-size"] = value; }
        }

        /// <summary>
        /// Indicates which font family is to be used to render the text.
        /// </summary>
        [SvgAttribute("font-family")]
        public virtual string FontFamily
        {
            get { return this.Attributes["font-family"] as string; }
            set { this.Attributes["font-family"] = value; }
        }

davescriven's avatar
davescriven committed
131
        /// <summary>
Eric Domke's avatar
Eric Domke committed
132
        /// Applies the required transforms to <see cref="ISvgRenderer"/>.
davescriven's avatar
davescriven committed
133
        /// </summary>
Eric Domke's avatar
Eric Domke committed
134
135
        /// <param name="renderer">The <see cref="ISvgRenderer"/> to be transformed.</param>
        protected internal override bool PushTransforms(ISvgRenderer renderer)
davescriven's avatar
davescriven committed
136
        {
137
            if (!base.PushTransforms(renderer)) return false;
Eric Domke's avatar
Eric Domke committed
138
            this.ViewBox.AddViewBoxTransform(this.AspectRatio, renderer, this);
139
            return true;
davescriven's avatar
davescriven committed
140
        }
Eric Domke's avatar
Eric Domke committed
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168

        protected override void Render(ISvgRenderer renderer)
        {
            switch (this.Overflow)
            {
                case SvgOverflow.auto:
                case SvgOverflow.visible:
                case SvgOverflow.scroll:
                    base.Render(renderer);
                    break;
                default:
                    var prevClip = renderer.GetClip();
                    try
                    {
                        var size = (this.Parent == null ? renderer.GetBoundable().Bounds.Size : GetDimensions());
                        var clip = new RectangleF(this.X.ToDeviceValue(renderer, UnitRenderingType.Horizontal, this),
                                                  this.Y.ToDeviceValue(renderer, UnitRenderingType.Horizontal, this),
                                                  size.Width, size.Height);
                        renderer.SetClip(new Region(clip), CombineMode.Intersect);
                        base.Render(renderer);
                    }
                    finally
                    {
                        renderer.SetClip(prevClip, CombineMode.Replace);
                    }
                    break;
            }
        }
Dan Backes's avatar
Dan Backes committed
169

Tebjan Halm's avatar
Tebjan Halm committed
170
171
172
173
        /// <summary>
        /// Gets the <see cref="GraphicsPath"/> for this element.
        /// </summary>
        /// <value></value>
Dan Backes's avatar
Dan Backes committed
174
        public GraphicsPath CreatePath()
Tebjan Halm's avatar
Tebjan Halm committed
175
        {
Dan Backes's avatar
Dan Backes committed
176
            var path = new GraphicsPath();
Tebjan Halm's avatar
Tebjan Halm committed
177

Dan Backes's avatar
Dan Backes committed
178
179
180
            AddPaths(this, path);

            return path;
Tebjan Halm's avatar
Tebjan Halm committed
181
        }
Dan Backes's avatar
Dan Backes committed
182

183
184
185
        /// <summary>
        /// Gets the bounds of the svg element.
        /// </summary>
Dan Backes's avatar
Dan Backes committed
186
187
188
189
        /// <returns>The bounds.</returns>
        public RectangleF CalculateBounds()
        {
            using (var path = CreatePath())
James Welle's avatar
James Welle committed
190
            {
Dan Backes's avatar
Dan Backes committed
191
                return path.GetBounds();
James Welle's avatar
James Welle committed
192
            }
193
        }
davescriven's avatar
davescriven committed
194
195
196
197
198
199

        /// <summary>
        /// Initializes a new instance of the <see cref="SvgFragment"/> class.
        /// </summary>
        public SvgFragment()
        {
James Welle's avatar
James Welle committed
200
            _x = 0.0f;
tebjan's avatar
tebjan committed
201
            _y = 0.0f;
202
203
            this.Height = new SvgUnit(SvgUnitType.Percentage, 100.0f);
            this.Width = new SvgUnit(SvgUnitType.Percentage, 100.0f);
davescriven's avatar
davescriven committed
204
            this.ViewBox = SvgViewBox.Empty;
Brian C. Barnes's avatar
Brian C. Barnes committed
205
            this.AspectRatio = new SvgAspectRatio(SvgPreserveAspectRatio.xMidYMid);
davescriven's avatar
davescriven committed
206
        }
207

James Welle's avatar
James Welle committed
208
209
        public SizeF GetDimensions()
        {
210
            float w, h;
James Welle's avatar
James Welle committed
211
212
213
            var isWidthperc = Width.Type == SvgUnitType.Percentage;
            var isHeightperc = Height.Type == SvgUnitType.Percentage;

214
            RectangleF bounds = new RectangleF();
James Welle's avatar
James Welle committed
215
216
            if (isWidthperc || isHeightperc)
            {
217
218
219
220
221
222
                if (ViewBox.Width > 0 && ViewBox.Height > 0)
                {
                    bounds = new RectangleF(ViewBox.MinX, ViewBox.MinY, ViewBox.Width, ViewBox.Height);
                }
                else
                {
Dan Backes's avatar
Dan Backes committed
223
                    bounds = this.CalculateBounds(); //do just one call to the expensive bounds calculation method
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
                }
            }

            if (isWidthperc) 
            {
                w = (bounds.Width + bounds.X) * (Width.Value * 0.01f);
            }
            else
            {
                w = Width.ToDeviceValue(null, UnitRenderingType.Horizontal, this);
            }
            if (isHeightperc) 
            {
                h = (bounds.Height + bounds.Y) * (Height.Value * 0.01f);
            }
            else 
            {
                h = Height.ToDeviceValue(null, UnitRenderingType.Vertical, this);
James Welle's avatar
James Welle committed
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
            }

            return new SizeF(w, h);
        }

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


davescriven's avatar
davescriven committed
264
265
    }
}