SvgFragment.cs 11.4 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
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

        PointF ISvgBoundable.Location
        {
            get
            {
                return PointF.Empty;
            }
        }

        SizeF ISvgBoundable.Size
        {
            get
            {
                return GetDimensions();
            }
        }

        RectangleF ISvgBoundable.Bounds
        {
            get
            {
                return new RectangleF(((ISvgBoundable)this).Location, ((ISvgBoundable)this).Size);
            }
        }

tebjan's avatar
tebjan committed
42
43
44
45
46
47
48
49
50
        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
51
52
53
54
55
56
57
58
59
            get { return _x; }
            set
            {
                if(_x != value)
                {
                    _x = value;
                    OnAttributeChanged(new AttributeEventArgs{ Attribute = "x", Value = value });
                }
            }
tebjan's avatar
tebjan committed
60
61
62
63
64
65
66
67
        }

        /// <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
68
69
70
71
72
73
74
75
76
            get { return _y; }
            set
            {
                if(_y != value)
                {
                    _y = value;
                    OnAttributeChanged(new AttributeEventArgs{ Attribute = "y", Value = value });
                }
            }
tebjan's avatar
tebjan committed
77
        }
davescriven's avatar
davescriven committed
78
79

        /// <summary>
80
        /// Gets or sets the width of the fragment.
davescriven's avatar
davescriven committed
81
82
83
84
85
        /// </summary>
        /// <value>The width.</value>
        [SvgAttribute("width")]
        public SvgUnit Width
        {
86
            get { return this.Attributes.GetAttribute<SvgUnit>("width"); }
James Welle's avatar
James Welle committed
87
            set { this.Attributes["width"] = value; }
davescriven's avatar
davescriven committed
88
89
90
        }

        /// <summary>
91
        /// Gets or sets the height of the fragment.
davescriven's avatar
davescriven committed
92
93
94
95
96
        /// </summary>
        /// <value>The height.</value>
        [SvgAttribute("height")]
        public SvgUnit Height
        {
97
            get { return this.Attributes.GetAttribute<SvgUnit>("height"); }
James Welle's avatar
James Welle committed
98
            set { this.Attributes["height"] = value; }
davescriven's avatar
davescriven committed
99
100
        }

James Welle's avatar
James Welle committed
101
102
103
104
105
106
        [SvgAttribute("overflow")]
        public virtual SvgOverflow Overflow
        {
            get { return this.Attributes.GetAttribute<SvgOverflow>("overflow"); }
            set { this.Attributes["overflow"] = value; }
        }
107

davescriven's avatar
davescriven committed
108
109
110
111
112
113
114
        /// <summary>
        /// Gets or sets the viewport of the element.
        /// </summary>
        /// <value></value>
        [SvgAttribute("viewBox")]
        public SvgViewBox ViewBox
        {
115
116
            get { return this.Attributes.GetAttribute<SvgViewBox>("viewBox"); }
            set { this.Attributes["viewBox"] = value; }
davescriven's avatar
davescriven committed
117
        }
118
119
120
121
122
        
        /// <summary>
        /// Gets or sets the aspect of the viewport.
        /// </summary>
        /// <value></value>
James Welle's avatar
James Welle committed
123
        [SvgAttribute("preserveAspectRatio")]
124
125
        public SvgAspectRatio AspectRatio
        {
James Welle's avatar
James Welle committed
126
127
            get { return this.Attributes.GetAttribute<SvgAspectRatio>("preserveAspectRatio"); }
            set { this.Attributes["preserveAspectRatio"] = value; }
128
        }
davescriven's avatar
davescriven committed
129

130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
        /// <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
150
        /// <summary>
151
        /// Applies the required transforms to <see cref="SvgRenderer"/>.
davescriven's avatar
davescriven committed
152
        /// </summary>
153
        /// <param name="renderer">The <see cref="SvgRenderer"/> to be transformed.</param>
154
        protected internal override bool PushTransforms(SvgRenderer renderer)
davescriven's avatar
davescriven committed
155
        {
156
            if (!base.PushTransforms(renderer)) return false;
davescriven's avatar
davescriven committed
157
158
159

            if (!this.ViewBox.Equals(SvgViewBox.Empty))
            {
160
161
162
163
164
165
166
                var width = this.Width.ToDeviceValue(renderer, UnitRenderingType.Horizontal, this);
                var height = this.Height.ToDeviceValue(renderer, UnitRenderingType.Vertical, this);

                var fScaleX = width / this.ViewBox.Width;
                var fScaleY = height / this.ViewBox.Height;
                var fMinX = -this.ViewBox.MinX;
                var fMinY = -this.ViewBox.MinY;
James Welle's avatar
James Welle committed
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181

                if (AspectRatio.Align != SvgPreserveAspectRatio.none)
                {
                    if (AspectRatio.Slice)
                    {
                        fScaleX = Math.Max(fScaleX, fScaleY);
                        fScaleY = Math.Max(fScaleX, fScaleY);
                    }
                    else
                    {
                        fScaleX = Math.Min(fScaleX, fScaleY);
                        fScaleY = Math.Min(fScaleX, fScaleY);
                    }
                    float fViewMidX = (this.ViewBox.Width / 2) * fScaleX;
                    float fViewMidY = (this.ViewBox.Height / 2) * fScaleY;
182
183
                    float fMidX = width / 2;
                    float fMidY = height / 2;
James Welle's avatar
James Welle committed
184
185
186
187
188
189

                    switch (AspectRatio.Align)
                    {
                        case SvgPreserveAspectRatio.xMinYMin:
                            break;
                        case SvgPreserveAspectRatio.xMidYMin:
190
                            fMinX += fMidX - fViewMidX;
James Welle's avatar
James Welle committed
191
192
                            break;
                        case SvgPreserveAspectRatio.xMaxYMin:
193
                            fMinX += width - this.ViewBox.Width * fScaleX;
James Welle's avatar
James Welle committed
194
195
                            break;
                        case SvgPreserveAspectRatio.xMinYMid:
196
                            fMinY += fMidY - fViewMidY;
James Welle's avatar
James Welle committed
197
198
                            break;
                        case SvgPreserveAspectRatio.xMidYMid:
199
200
                            fMinX += fMidX - fViewMidX;
                            fMinY += fMidY - fViewMidY;
James Welle's avatar
James Welle committed
201
202
                            break;
                        case SvgPreserveAspectRatio.xMaxYMid:
203
204
                            fMinX += width - this.ViewBox.Width * fScaleX;
                            fMinY += fMidY - fViewMidY;
James Welle's avatar
James Welle committed
205
206
                            break;
                        case SvgPreserveAspectRatio.xMinYMax:
207
                            fMinY += height - this.ViewBox.Height * fScaleY;
James Welle's avatar
James Welle committed
208
209
                            break;
                        case SvgPreserveAspectRatio.xMidYMax:
210
211
                            fMinX += fMidX - fViewMidX;
                            fMinY += height - this.ViewBox.Height * fScaleY;
James Welle's avatar
James Welle committed
212
213
                            break;
                        case SvgPreserveAspectRatio.xMaxYMax:
214
215
                            fMinX += width - this.ViewBox.Width * fScaleX;
                            fMinY += height - this.ViewBox.Height * fScaleY;
James Welle's avatar
James Welle committed
216
217
218
219
220
221
                            break;
                        default:
                            break;
                    }
                }

222
223
224
225
226
227
                var x = _x.ToDeviceValue(renderer, UnitRenderingType.Horizontal, this);
                var y = _y.ToDeviceValue(renderer, UnitRenderingType.Vertical, this);

                renderer.AddClip(new Region(new RectangleF(x, y, width, height)));
                renderer.ScaleTransform(fScaleX, fScaleY, MatrixOrder.Prepend);
                renderer.TranslateTransform(x,y);
James Welle's avatar
James Welle committed
228
                renderer.TranslateTransform(fMinX, fMinY);
davescriven's avatar
davescriven committed
229
            }
230
231

            return true;
davescriven's avatar
davescriven committed
232
        }
Tebjan Halm's avatar
Tebjan Halm committed
233
234
235
236
237
238
239
240
241
        
        /// <summary>
        /// Gets the <see cref="GraphicsPath"/> for this element.
        /// </summary>
        /// <value></value>
        public GraphicsPath Path
        {
            get 
            { 
James Welle's avatar
James Welle committed
242
                var path = new GraphicsPath();
Tebjan Halm's avatar
Tebjan Halm committed
243

James Welle's avatar
James Welle committed
244
                AddPaths(this, path);
Tebjan Halm's avatar
Tebjan Halm committed
245
  
James Welle's avatar
James Welle committed
246
                return path;
Tebjan Halm's avatar
Tebjan Halm committed
247
248
            }
        }
249
250
251
252
253
254
255
        
        /// <summary>
        /// Gets the bounds of the svg element.
        /// </summary>
        /// <value>The bounds.</value>
        public RectangleF Bounds 
        { 
James Welle's avatar
James Welle committed
256
257
258
259
            get
            {
                return this.Path.GetBounds();
            }
260
        }
davescriven's avatar
davescriven committed
261
262
263
264
265
266

        /// <summary>
        /// Initializes a new instance of the <see cref="SvgFragment"/> class.
        /// </summary>
        public SvgFragment()
        {
James Welle's avatar
James Welle committed
267
            _x = 0.0f;
tebjan's avatar
tebjan committed
268
            _y = 0.0f;
269
270
            this.Height = new SvgUnit(SvgUnitType.Percentage, 100.0f);
            this.Width = new SvgUnit(SvgUnitType.Percentage, 100.0f);
davescriven's avatar
davescriven committed
271
            this.ViewBox = SvgViewBox.Empty;
Brian C. Barnes's avatar
Brian C. Barnes committed
272
            this.AspectRatio = new SvgAspectRatio(SvgPreserveAspectRatio.xMidYMid);
davescriven's avatar
davescriven committed
273
        }
274

James Welle's avatar
James Welle committed
275
276
        public SizeF GetDimensions()
        {
277
            float w, h;
James Welle's avatar
James Welle committed
278
279
280
            var isWidthperc = Width.Type == SvgUnitType.Percentage;
            var isHeightperc = Height.Type == SvgUnitType.Percentage;

281
            RectangleF bounds = new RectangleF();
James Welle's avatar
James Welle committed
282
283
            if (isWidthperc || isHeightperc)
            {
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
                if (ViewBox.Width > 0 && ViewBox.Height > 0)
                {
                    bounds = new RectangleF(ViewBox.MinX, ViewBox.MinY, ViewBox.Width, ViewBox.Height);
                }
                else
                {
                    bounds = this.Bounds; //do just one call to the recursive bounds property
                }
            }

            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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
            }

            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;
        }
329
330


davescriven's avatar
davescriven committed
331
332
    }
}