SvgVisualElement.cs 7.21 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;
21
        private SvgClipRule clipRule = SvgClipRule.NonZero;
davescriven's avatar
davescriven committed
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

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

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

55
56
57
58
59
60
61
62
63
64
        /// <summary>
        /// 
        /// </summary>
        [SvgAttribute("clip-rule")]
        public SvgClipRule ClipRule
        {
            get { return this.Attributes.GetAttribute<SvgClipRule>("clip-rule", SvgClipRule.NonZero); }
            set { this.Attributes["clip-rule"] = value; }
        }

davescriven's avatar
davescriven committed
65
66
67
68
69
70
71
72
73
74
75
        /// <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>
76
        public SvgVisualElement()
davescriven's avatar
davescriven committed
77
78
79
80
81
82
83
84
85
        {
            this._dirty = true;
            this._requiresSmoothRendering = false;
        }

        /// <summary>
        /// Renders the <see cref="SvgElement"/> and contents to the specified <see cref="Graphics"/> object.
        /// </summary>
        /// <param name="graphics">The <see cref="Graphics"/> object to render to.</param>
86
        protected override void Render(SvgRenderer renderer)
davescriven's avatar
davescriven committed
87
88
89
        {
            if (this.Path != null && this.Visible)
            {
90
                this.PushTransforms(renderer);
91
                this.SetClip(renderer);
davescriven's avatar
davescriven committed
92
93
94
95

                // If this element needs smoothing enabled turn anti aliasing on
                if (this.RequiresSmoothRendering)
                {
96
                    renderer.SmoothingMode = SmoothingMode.AntiAlias;
davescriven's avatar
davescriven committed
97
98
99
                }

                // Fill first so that the stroke can overlay
100
                if (this.Fill != null)
davescriven's avatar
davescriven committed
101
102
103
104
105
                {
                    using (Brush brush = this.Fill.GetBrush(this, this.FillOpacity))
                    {
                        if (brush != null)
                        {
106
                            renderer.FillPath(brush, this.Path);
davescriven's avatar
davescriven committed
107
108
109
110
111
                        }
                    }
                }

                // Stroke is the last thing to do
112
                if (this.Stroke != null)
davescriven's avatar
davescriven committed
113
114
115
116
117
118
                {
                    float strokeWidth = this.StrokeWidth.ToDeviceValue(this);
                    using (Pen pen = new Pen(this.Stroke.GetBrush(this, this.StrokeOpacity), strokeWidth))
                    {
                        if (pen != null)
                        {
119
120
121
122
123
                            if (this.StrokeDashArray != null)
                            {
                                pen.DashPattern = this.StrokeDashArray.ConvertAll<float>(delegate(SvgUnit unit)
                                {
                                    // divide by stroke width - GDI behaviour that I don't quite understand yet.
124
                                    return unit.Value / ((strokeWidth <= 0) ? 1 : strokeWidth);
125
126
127
                                }).ToArray();
                            }

128
                            renderer.DrawPath(pen, this.Path);
davescriven's avatar
davescriven committed
129
130
131
132
133
                        }
                    }
                }

                // Reset the smoothing mode
134
                if (this.RequiresSmoothRendering && renderer.SmoothingMode == SmoothingMode.AntiAlias)
davescriven's avatar
davescriven committed
135
                {
136
                    renderer.SmoothingMode = SmoothingMode.Default;
davescriven's avatar
davescriven committed
137
138
                }

139
                this.ResetClip(renderer);
140
                this.PopTransforms(renderer);
davescriven's avatar
davescriven committed
141
142
            }
        }
143

144
145
146
147
        /// <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>
148
149
150
151
152
153
        protected internal virtual void SetClip(SvgRenderer renderer)
        {
            if (this.ClipPath != null)
            {
                SvgClipPath clipPath = this.OwnerDocument.GetElementById<SvgClipPath>(this.ClipPath.ToString());
                this._previousClip = renderer.Clip;
154
155
156

                if (clipPath != null)
                {
157
                    renderer.Clip = clipPath.GetClipRegion(this);
158
                }
159
160
161
            }
        }

162
163
164
165
        /// <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>
166
167
        protected internal virtual void ResetClip(SvgRenderer renderer)
        {
168
            if (this._previousClip != null)
169
            {
170
                renderer.Clip = this._previousClip;
171
172
173
174
                this._previousClip = null;
            }
        }

175
176
177
178
        /// <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>
179
180
181
182
183
        void ISvgClipable.SetClip(SvgRenderer renderer)
        {
            this.SetClip(renderer);
        }

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
190
191
        void ISvgClipable.ResetClip(SvgRenderer renderer)
        {
            this.ResetClip(renderer);
        }
davescriven's avatar
davescriven committed
192
193
    }
}