SvgGraphicsElement.cs 5.73 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 SvgGraphicsElement : 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; }
        }

davescriven's avatar
davescriven committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
        /// <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>
        public SvgGraphicsElement()
        {
            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>
75
        protected override void Render(SvgRenderer renderer)
davescriven's avatar
davescriven committed
76
77
78
        {
            if (this.Path != null && this.Visible)
            {
79
                this.PushTransforms(renderer);
80
                this.SetClip(renderer);
davescriven's avatar
davescriven committed
81
82
83
84

                // If this element needs smoothing enabled turn anti aliasing on
                if (this.RequiresSmoothRendering)
                {
85
                    renderer.SmoothingMode = SmoothingMode.AntiAlias;
davescriven's avatar
davescriven committed
86
87
88
                }

                // Fill first so that the stroke can overlay
89
                if (this.Fill != null)
davescriven's avatar
davescriven committed
90
91
92
93
94
                {
                    using (Brush brush = this.Fill.GetBrush(this, this.FillOpacity))
                    {
                        if (brush != null)
                        {
95
                            renderer.FillPath(brush, this.Path);
davescriven's avatar
davescriven committed
96
97
98
99
100
                        }
                    }
                }

                // Stroke is the last thing to do
101
                if (this.Stroke != null)
davescriven's avatar
davescriven committed
102
103
104
105
106
107
                {
                    float strokeWidth = this.StrokeWidth.ToDeviceValue(this);
                    using (Pen pen = new Pen(this.Stroke.GetBrush(this, this.StrokeOpacity), strokeWidth))
                    {
                        if (pen != null)
                        {
108
109
110
111
112
113
114
115
116
                            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.
                                    return unit.Value / strokeWidth;
                                }).ToArray();
                            }

117
                            renderer.DrawPath(pen, this.Path);
davescriven's avatar
davescriven committed
118
119
120
121
122
                        }
                    }
                }

                // Reset the smoothing mode
123
                if (this.RequiresSmoothRendering && renderer.SmoothingMode == SmoothingMode.AntiAlias)
davescriven's avatar
davescriven committed
124
                {
125
                    renderer.SmoothingMode = SmoothingMode.Default;
davescriven's avatar
davescriven committed
126
127
                }

128
                this.ResetClip(renderer);
129
                this.PopTransforms(renderer);
davescriven's avatar
davescriven committed
130
131
            }
        }
132
133
134
135
136
137
138

        protected internal virtual void SetClip(SvgRenderer renderer)
        {
            if (this.ClipPath != null)
            {
                SvgClipPath clipPath = this.OwnerDocument.GetElementById<SvgClipPath>(this.ClipPath.ToString());
                this._previousClip = renderer.Clip;
139
140
141
142
143

                if (clipPath != null)
                {
                    renderer.SetClip(clipPath.GetClipRegion());
                }
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
            }
        }

        protected internal virtual void ResetClip(SvgRenderer renderer)
        {
            if (this.ClipPath != null)
            {
                renderer.SetClip(this._previousClip);
                this._previousClip = null;
            }
        }

        void ISvgClipable.SetClip(SvgRenderer renderer)
        {
            this.SetClip(renderer);
        }

        void ISvgClipable.ResetClip(SvgRenderer renderer)
        {
            this.ResetClip(renderer);
        }
davescriven's avatar
davescriven committed
165
166
    }
}