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

namespace Svg
{
    /// <summary>
    /// The class that all SVG elements should derive from when they are to be rendered.
    /// </summary>
James Welle's avatar
James Welle committed
10
    public abstract partial class SvgVisualElement : SvgElement, ISvgBoundable, ISvgStylable, ISvgClipable
davescriven's avatar
davescriven committed
11
12
13
    {
        private bool _dirty;
        private bool _requiresSmoothRendering;
14
        private Region _previousClip;
davescriven's avatar
davescriven committed
15
16
17
18

        /// <summary>
        /// Gets the <see cref="GraphicsPath"/> for this element.
        /// </summary>
19
        public abstract GraphicsPath Path(SvgRenderer renderer);
James Welle's avatar
James Welle committed
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

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

        SizeF ISvgBoundable.Size
        {
            get
            {
                return Bounds.Size;
            }
        }

davescriven's avatar
davescriven committed
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
        /// <summary>
        /// Gets the bounds of the element.
        /// </summary>
        /// <value>The bounds.</value>
        public abstract RectangleF Bounds { get; }

        /// <summary>
        /// Gets or sets a value indicating whether this element's <see cref="Path"/> is dirty.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if the path is dirty; otherwise, <c>false</c>.
        /// </value>
        protected virtual bool IsPathDirty
        {
            get { return this._dirty; }
            set { this._dirty = value; }
        }

55
56
57
58
59
60
61
62
63
64
        /// <summary>
        /// Gets the associated <see cref="SvgClipPath"/> if one has been specified.
        /// </summary>
        [SvgAttribute("clip-path")]
        public virtual Uri ClipPath
        {
            get { return this.Attributes.GetAttribute<Uri>("clip-path"); }
            set { this.Attributes["clip-path"] = value; }
        }

65
        /// <summary>
66
        /// Gets or sets the algorithm which is to be used to determine the clipping region.
67
68
69
70
71
72
73
74
        /// </summary>
        [SvgAttribute("clip-rule")]
        public SvgClipRule ClipRule
        {
            get { return this.Attributes.GetAttribute<SvgClipRule>("clip-rule", SvgClipRule.NonZero); }
            set { this.Attributes["clip-rule"] = value; }
        }

James Welle's avatar
James Welle committed
75
76
77
78
79
80
81
82
83
        /// <summary>
        /// Gets the associated <see cref="SvgClipPath"/> if one has been specified.
        /// </summary>
        [SvgAttribute("filter")]
        public virtual Uri Filter
        {
            get { return this.Attributes.GetAttribute<Uri>("filter"); }
            set { this.Attributes["filter"] = value; }
        }
84

davescriven's avatar
davescriven committed
85
86
87
88
89
90
91
92
93
94
95
        /// <summary>
        /// Gets or sets a value to determine if anti-aliasing should occur when the element is being rendered.
        /// </summary>
        protected virtual bool RequiresSmoothRendering
        {
            get { return this._requiresSmoothRendering; }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SvgGraphicsElement"/> class.
        /// </summary>
96
        public SvgVisualElement()
davescriven's avatar
davescriven committed
97
98
99
100
101
        {
            this._dirty = true;
            this._requiresSmoothRendering = false;
        }

Eric Domke's avatar
Eric Domke committed
102
103
        protected virtual bool Renderable { get { return true; } }

davescriven's avatar
davescriven committed
104
105
106
        /// <summary>
        /// Renders the <see cref="SvgElement"/> and contents to the specified <see cref="Graphics"/> object.
        /// </summary>
107
        /// <param name="renderer">The <see cref="SvgRenderer"/> object to render to.</param>
108
        protected override void Render(SvgRenderer renderer)
davescriven's avatar
davescriven committed
109
        {
Eric Domke's avatar
Eric Domke committed
110
111
112
113
114
115
116
            this.Render(renderer, true);
        }

        private void Render(SvgRenderer renderer, bool renderFilter)
        {
            if (this.Visible && this.Displayable && this.PushTransforms(renderer) &&
                (!Renderable || this.Path(renderer) != null))
davescriven's avatar
davescriven committed
117
            {
Eric Domke's avatar
Eric Domke committed
118
                bool renderNormal = true;
davescriven's avatar
davescriven committed
119

Eric Domke's avatar
Eric Domke committed
120
                if (renderFilter && this.Filter != null)
davescriven's avatar
davescriven committed
121
                {
Eric Domke's avatar
Eric Domke committed
122
123
124
125
126
127
128
                    var filter = this.OwnerDocument.IdManager.GetElementById(this.Filter) as FilterEffects.SvgFilter;
                    if (filter != null)
                    {
                        this.PopTransforms(renderer);
                        filter.ApplyFilter(this, renderer, (r) => this.Render(r, false));
                        renderNormal = false;
                    }
davescriven's avatar
davescriven committed
129
130
                }

131

Eric Domke's avatar
Eric Domke committed
132
                if (renderNormal)
davescriven's avatar
davescriven committed
133
                {
Eric Domke's avatar
Eric Domke committed
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
                    this.SetClip(renderer);

                    if (Renderable)
                    {
                        // If this element needs smoothing enabled turn anti-aliasing on
                        if (this.RequiresSmoothRendering)
                        {
                            renderer.SmoothingMode = SmoothingMode.AntiAlias;
                        }

                        this.RenderFill(renderer);
                        this.RenderStroke(renderer);

                        // Reset the smoothing mode
                        if (this.RequiresSmoothRendering && renderer.SmoothingMode == SmoothingMode.AntiAlias)
                        {
                            renderer.SmoothingMode = SmoothingMode.Default;
                        }
                    }
                    else
                    {
                        base.RenderChildren(renderer);
                    }

                    this.ResetClip(renderer);
                    this.PopTransforms(renderer);
160
161
162
163
164
165
166
167
168
169
170
171
172
                }

            }
        }

        /// <summary>
        /// Renders the fill of the <see cref="SvgVisualElement"/> to the specified <see cref="SvgRenderer"/>
        /// </summary>
        /// <param name="renderer">The <see cref="SvgRenderer"/> object to render to.</param>
        protected internal virtual void RenderFill(SvgRenderer renderer)
        {
            if (this.Fill != null)
            {
173
                using (Brush brush = this.Fill.GetBrush(this, renderer, Math.Min(Math.Max(this.FillOpacity * this.Opacity, 0), 1)))
174
175
                {
                    if (brush != null)
davescriven's avatar
davescriven committed
176
                    {
177
178
                        this.Path(renderer).FillMode = this.FillRule == SvgFillRule.NonZero ? FillMode.Winding : FillMode.Alternate;
                        renderer.FillPath(brush, this.Path(renderer));
davescriven's avatar
davescriven committed
179
180
                    }
                }
181
182
            }
        }
davescriven's avatar
davescriven committed
183

184
185
186
187
188
189
190
191
        /// <summary>
        /// Renders the stroke of the <see cref="SvgVisualElement"/> to the specified <see cref="SvgRenderer"/>
        /// </summary>
        /// <param name="renderer">The <see cref="SvgRenderer"/> object to render to.</param>
        protected internal virtual void RenderStroke(SvgRenderer renderer)
        {
            if (this.Stroke != null)
            {
192
193
                float strokeWidth = this.StrokeWidth.ToDeviceValue(renderer, UnitRenderingType.Other, this);
                using (var pen = new Pen(this.Stroke.GetBrush(this, renderer, Math.Min(Math.Max(this.StrokeOpacity * this.Opacity, 0), 1)), strokeWidth))
davescriven's avatar
davescriven committed
194
                {
195
                    if (this.StrokeDashArray != null && this.StrokeDashArray.Count > 0)
davescriven's avatar
davescriven committed
196
                    {
197
                        /* divide by stroke width - GDI behaviour that I don't quite understand yet.*/
198
                        pen.DashPattern = this.StrokeDashArray.ConvertAll(u => ((u.Value <= 0) ? 1 : u.Value) / ((strokeWidth <= 0) ? 1 : strokeWidth)).ToArray();
199
                    }
200

201
                    renderer.DrawPath(pen, this.Path(renderer));
davescriven's avatar
davescriven committed
202
203
204
                }
            }
        }
205

206
207
208
209
        /// <summary>
        /// Sets the clipping region of the specified <see cref="SvgRenderer"/>.
        /// </summary>
        /// <param name="renderer">The <see cref="SvgRenderer"/> to have its clipping region set.</param>
210
211
212
213
214
215
        protected internal virtual void SetClip(SvgRenderer renderer)
        {
            if (this.ClipPath != null)
            {
                SvgClipPath clipPath = this.OwnerDocument.GetElementById<SvgClipPath>(this.ClipPath.ToString());
                this._previousClip = renderer.Clip;
216
217
218

                if (clipPath != null)
                {
219
                    renderer.AddClip(clipPath.GetClipRegion(this));
220
                }
221
222
223
            }
        }

224
225
226
227
        /// <summary>
        /// Resets the clipping region of the specified <see cref="SvgRenderer"/> back to where it was before the <see cref="SetClip"/> method was called.
        /// </summary>
        /// <param name="renderer">The <see cref="SvgRenderer"/> to have its clipping region reset.</param>
228
229
        protected internal virtual void ResetClip(SvgRenderer renderer)
        {
230
            if (this._previousClip != null)
231
            {
232
                renderer.Clip = this._previousClip;
233
234
235
236
                this._previousClip = null;
            }
        }

237
238
239
240
        /// <summary>
        /// Sets the clipping region of the specified <see cref="SvgRenderer"/>.
        /// </summary>
        /// <param name="renderer">The <see cref="SvgRenderer"/> to have its clipping region set.</param>
241
242
243
244
245
        void ISvgClipable.SetClip(SvgRenderer renderer)
        {
            this.SetClip(renderer);
        }

246
247
248
249
        /// <summary>
        /// Resets the clipping region of the specified <see cref="SvgRenderer"/> back to where it was before the <see cref="SetClip"/> method was called.
        /// </summary>
        /// <param name="renderer">The <see cref="SvgRenderer"/> to have its clipping region reset.</param>
250
251
252
253
        void ISvgClipable.ResetClip(SvgRenderer renderer)
        {
            this.ResetClip(renderer);
        }
254

James Welle's avatar
James Welle committed
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
        public override SvgElement DeepCopy<T>()
        {
            var newObj = base.DeepCopy<T>() as SvgVisualElement;
            newObj.ClipPath = this.ClipPath;
            newObj.ClipRule = this.ClipRule;
            newObj.Filter = this.Filter;

            newObj.Visible = this.Visible;
            if (this.Fill != null)
                newObj.Fill = this.Fill;
            if (this.Stroke != null)
                newObj.Stroke = this.Stroke;
            newObj.FillRule = this.FillRule;
            newObj.FillOpacity = this.FillOpacity;
            newObj.StrokeWidth = this.StrokeWidth;
            newObj.StrokeLineCap = this.StrokeLineCap;
            newObj.StrokeLineJoin = this.StrokeLineJoin;
            newObj.StrokeMiterLimit = this.StrokeMiterLimit;
            newObj.StrokeDashArray = this.StrokeDashArray;
            newObj.StrokeDashOffset = this.StrokeDashOffset;
            newObj.StrokeOpacity = this.StrokeOpacity;
            newObj.Opacity = this.Opacity;

            return newObj;
        }
280

davescriven's avatar
davescriven committed
281
    }
282
}