SvgGraphicsElement.cs 3.79 KB
Newer Older
davescriven's avatar
davescriven committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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>
    public abstract partial class SvgGraphicsElement : SvgElement, ISvgStylable
    {
        private bool _dirty;
        private bool _requiresSmoothRendering;

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

        /// <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>
64
        protected override void Render(SvgRenderer renderer)
davescriven's avatar
davescriven committed
65
66
67
        {
            if (this.Path != null && this.Visible)
            {
68
                this.PushTransforms(renderer);
davescriven's avatar
davescriven committed
69
70
71
72

                // If this element needs smoothing enabled turn anti aliasing on
                if (this.RequiresSmoothRendering)
                {
73
                    renderer.SmoothingMode = SmoothingMode.AntiAlias;
davescriven's avatar
davescriven committed
74
75
76
                }

                // Fill first so that the stroke can overlay
77
                if (this.Fill != null)
davescriven's avatar
davescriven committed
78
79
80
81
82
                {
                    using (Brush brush = this.Fill.GetBrush(this, this.FillOpacity))
                    {
                        if (brush != null)
                        {
83
                            renderer.FillPath(brush, this.Path);
davescriven's avatar
davescriven committed
84
85
86
87
88
                        }
                    }
                }

                // Stroke is the last thing to do
89
                if (this.Stroke != null)
davescriven's avatar
davescriven committed
90
91
92
93
94
95
                {
                    float strokeWidth = this.StrokeWidth.ToDeviceValue(this);
                    using (Pen pen = new Pen(this.Stroke.GetBrush(this, this.StrokeOpacity), strokeWidth))
                    {
                        if (pen != null)
                        {
96
                            renderer.DrawPath(pen, this.Path);
davescriven's avatar
davescriven committed
97
98
99
100
101
                        }
                    }
                }

                // Reset the smoothing mode
102
                if (this.RequiresSmoothRendering && renderer.SmoothingMode == SmoothingMode.AntiAlias)
davescriven's avatar
davescriven committed
103
                {
104
                    renderer.SmoothingMode = SmoothingMode.Default;
davescriven's avatar
davescriven committed
105
106
                }

107
                this.PopTransforms(renderer);
davescriven's avatar
davescriven committed
108
109
110
111
            }
        }
    }
}