SvgGraphicsElement.cs 5.16 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
                            renderer.DrawPath(pen, this.Path);
davescriven's avatar
davescriven committed
109
110
111
112
113
                        }
                    }
                }

                // Reset the smoothing mode
114
                if (this.RequiresSmoothRendering && renderer.SmoothingMode == SmoothingMode.AntiAlias)
davescriven's avatar
davescriven committed
115
                {
116
                    renderer.SmoothingMode = SmoothingMode.Default;
davescriven's avatar
davescriven committed
117
118
                }

119
                this.ResetClip(renderer);
120
                this.PopTransforms(renderer);
davescriven's avatar
davescriven committed
121
122
            }
        }
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151

        protected internal virtual void SetClip(SvgRenderer renderer)
        {
            if (this.ClipPath != null)
            {
                SvgClipPath clipPath = this.OwnerDocument.GetElementById<SvgClipPath>(this.ClipPath.ToString());
                this._previousClip = renderer.Clip;
                renderer.SetClip(clipPath.GetClipRegion());
            }
        }

        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
152
153
    }
}