SvgVisualElement.cs 8.94 KB
Newer Older
davescriven's avatar
davescriven committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml.Serialization;
using System.ComponentModel;
using System.Diagnostics;

namespace Svg
{
    /// <summary>
    /// The class that all SVG elements should derive from when they are to be rendered.
    /// </summary>
16
    public abstract partial class SvgVisualElement : SvgElement, ISvgStylable, ISvgClipable
davescriven's avatar
davescriven committed
17
18
19
    {
        private bool _dirty;
        private bool _requiresSmoothRendering;
20
        private Region _previousClip;
davescriven's avatar
davescriven committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

        /// <summary>
        /// Gets the <see cref="GraphicsPath"/> for this element.
        /// </summary>
        public abstract GraphicsPath Path { get; }
        /// <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; }
        }

44
45
46
47
48
49
50
51
52
53
        /// <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; }
        }

54
        /// <summary>
55
        /// Gets or sets the algorithm which is to be used to determine the clipping region.
56
57
58
59
60
61
62
63
        /// </summary>
        [SvgAttribute("clip-rule")]
        public SvgClipRule ClipRule
        {
            get { return this.Attributes.GetAttribute<SvgClipRule>("clip-rule", SvgClipRule.NonZero); }
            set { this.Attributes["clip-rule"] = value; }
        }

64
65
66
67
68
69
70
71
72
73
74
75

		/// <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; }
		}


davescriven's avatar
davescriven committed
76
77
78
79
80
81
82
83
84
85
86
        /// <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>
87
        public SvgVisualElement()
davescriven's avatar
davescriven committed
88
89
90
        {
            this._dirty = true;
            this._requiresSmoothRendering = false;
Tebjan Halm's avatar
Tebjan Halm committed
91
            this.Fill = new SvgColourServer(); //in case fill attribute is not set by xml, default fill is black
davescriven's avatar
davescriven committed
92
93
94
95
96
        }

        /// <summary>
        /// Renders the <see cref="SvgElement"/> and contents to the specified <see cref="Graphics"/> object.
        /// </summary>
97
        /// <param name="graphics">The <see cref="SvgRenderer"/> object to render to.</param>
98
        protected override void Render(SvgRenderer renderer)
davescriven's avatar
davescriven committed
99
100
101
        {
            if (this.Path != null && this.Visible)
            {
102
                this.PushTransforms(renderer);
103
                this.SetClip(renderer);
davescriven's avatar
davescriven committed
104

105
                // If this element needs smoothing enabled turn anti-aliasing on
davescriven's avatar
davescriven committed
106
107
                if (this.RequiresSmoothRendering)
                {
108
                    renderer.SmoothingMode = SmoothingMode.AntiAlias;
davescriven's avatar
davescriven committed
109
110
                }

111
112
113
114
115
                this.RenderFill(renderer);
                this.RenderStroke(renderer);

                // Reset the smoothing mode
                if (this.RequiresSmoothRendering && renderer.SmoothingMode == SmoothingMode.AntiAlias)
davescriven's avatar
davescriven committed
116
                {
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
                    renderer.SmoothingMode = SmoothingMode.Default;
                }

                this.ResetClip(renderer);
                this.PopTransforms(renderer);
            }
        }

        /// <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)
            {
                using (Brush brush = this.Fill.GetBrush(this, this.FillOpacity))
                {
                    if (brush != null)
davescriven's avatar
davescriven committed
136
                    {
Tebjan Halm's avatar
Tebjan Halm committed
137
                    	this.Path.FillMode = this.FillRule == SvgFillRule.NonZero ? FillMode.Winding : FillMode.Alternate;
138
                        renderer.FillPath(brush, this.Path);
davescriven's avatar
davescriven committed
139
140
                    }
                }
141
142
            }
        }
davescriven's avatar
davescriven committed
143

144
145
146
147
148
149
150
151
152
        /// <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)
            {
                float strokeWidth = this.StrokeWidth.ToDeviceValue(this);
153
                using (var pen = new Pen(this.Stroke.GetBrush(this, this.StrokeOpacity), strokeWidth))
davescriven's avatar
davescriven committed
154
                {
155
                    if (this.StrokeDashArray != null && this.StrokeDashArray.Count > 0)
davescriven's avatar
davescriven committed
156
                    {
157
158
                        /* divide by stroke width - GDI behaviour that I don't quite understand yet.*/
                        pen.DashPattern = this.StrokeDashArray.ConvertAll(u => u.Value/((strokeWidth <= 0) ? 1 : strokeWidth)).ToArray();
159
                    }
160
161

                    renderer.DrawPath(pen, this.Path);
davescriven's avatar
davescriven committed
162
163
164
                }
            }
        }
165

166
167
168
169
        /// <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>
170
171
172
173
174
175
        protected internal virtual void SetClip(SvgRenderer renderer)
        {
            if (this.ClipPath != null)
            {
                SvgClipPath clipPath = this.OwnerDocument.GetElementById<SvgClipPath>(this.ClipPath.ToString());
                this._previousClip = renderer.Clip;
176
177
178

                if (clipPath != null)
                {
179
                    renderer.Clip = clipPath.GetClipRegion(this);
180
                }
181
182
183
            }
        }

184
185
186
187
        /// <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>
188
189
        protected internal virtual void ResetClip(SvgRenderer renderer)
        {
190
            if (this._previousClip != null)
191
            {
192
                renderer.Clip = this._previousClip;
193
194
195
196
                this._previousClip = null;
            }
        }

197
198
199
200
        /// <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>
201
202
203
204
205
        void ISvgClipable.SetClip(SvgRenderer renderer)
        {
            this.SetClip(renderer);
        }

206
207
208
209
        /// <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>
210
211
212
213
        void ISvgClipable.ResetClip(SvgRenderer renderer)
        {
            this.ResetClip(renderer);
        }
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240

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

davescriven's avatar
davescriven committed
241
242
    }
}