SvgRadialGradientServer.cs 3.81 KB
Newer Older
davescriven's avatar
davescriven committed
1
2
3
4
5
using System.Drawing;
using System.Drawing.Drawing2D;

namespace Svg
{
6
    [SvgElement("radialGradient")]
davescriven's avatar
davescriven committed
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
    public sealed class SvgRadialGradientServer : SvgGradientServer
    {
        [SvgAttribute("cx")]
        public SvgUnit CenterX
        {
            get { return this.Attributes.GetAttribute<SvgUnit>("cx"); }
            set { this.Attributes["cx"] = value; }
        }

        [SvgAttribute("cy")]
        public SvgUnit CenterY
        {
            get { return this.Attributes.GetAttribute<SvgUnit>("cy"); }
            set { this.Attributes["cy"] = value; }
        }

        [SvgAttribute("r")]
        public SvgUnit Radius
        {
            get { return this.Attributes.GetAttribute<SvgUnit>("r"); }
            set { this.Attributes["r"] = value; }
        }

        [SvgAttribute("fx")]
        public SvgUnit FocalX
        {
33
34
35
36
37
38
39
40
41
42
43
44
            get
            {
                var value = this.Attributes.GetAttribute<SvgUnit>("fx");

                if (value.IsEmpty || value.IsNone)
                {
                    value = this.CenterX;
                }

                return value;
            }

davescriven's avatar
davescriven committed
45
46
47
48
49
50
            set { this.Attributes["fx"] = value; }
        }

        [SvgAttribute("fy")]
        public SvgUnit FocalY
        {
51
52
53
54
55
56
57
58
59
60
61
62
            get
            {
                var value = this.Attributes.GetAttribute<SvgUnit>("fy");

                if (value.IsEmpty || value.IsNone)
                {
                    value = this.CenterY;
                }

                return value;
            }

davescriven's avatar
davescriven committed
63
64
65
66
67
68
69
70
            set { this.Attributes["fy"] = value; }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SvgRadialGradientServer"/> class.
        /// </summary>
        public SvgRadialGradientServer()
        {
71
72
73
74
            //Apply default values of 50% to cX,cY and r
            CenterX = new SvgUnit(SvgUnitType.Percentage, 50);
            CenterY = new SvgUnit(SvgUnitType.Percentage, 50);
            Radius = new SvgUnit(SvgUnitType.Percentage, 50);
davescriven's avatar
davescriven committed
75
76
        }

77
        public override Brush GetBrush(SvgVisualElement renderingElement, float opacity)
davescriven's avatar
davescriven committed
78
79
80
        {
            float radius = this.Radius.ToDeviceValue(renderingElement);

81
            if (radius <= 0)
82
            {
83
                return null;
84
85
            }
            
86
87
88
89
90
91
92
93
94
95
96
97
98
            GraphicsPath path = new GraphicsPath();
            float left = this.CenterX.ToDeviceValue(renderingElement);
            float top = this.CenterY.ToDeviceValue(renderingElement, true);
            
            RectangleF boundingBox = (this.GradientUnits == SvgCoordinateUnits.ObjectBoundingBox) ? renderingElement.Bounds : renderingElement.OwnerDocument.GetDimensions();

            path.AddEllipse(
                boundingBox.Left + left - radius,
                boundingBox.Top + top - radius,
                radius * 2,
                radius * 2);

            PathGradientBrush brush = new PathGradientBrush(path);
99
            ColorBlend blend = base.GetColourBlend(renderingElement, opacity, true);
100
101
102
103
104
105
106
107

            brush.InterpolationColors = blend;
            brush.CenterPoint =
                new PointF(
                    boundingBox.Left + this.FocalX.ToDeviceValue(renderingElement),
                    boundingBox.Top + this.FocalY.ToDeviceValue(renderingElement, true));

            return brush;
davescriven's avatar
davescriven committed
108
        }
109
110
111
112
113
114
115
116
117
118
119


		public override SvgElement DeepCopy()
		{
			return DeepCopy<SvgRadialGradientServer>();
		}


		public override SvgElement DeepCopy<T>()
		{
			var newObj = base.DeepCopy<T>() as SvgRadialGradientServer;
120

121
122
123
124
125
126
			newObj.CenterX = this.CenterX;
			newObj.CenterY = this.CenterY;
			newObj.Radius = this.Radius;
			newObj.FocalX = this.FocalX;
			newObj.FocalY = this.FocalY;

127
			return newObj;
128
		}
davescriven's avatar
davescriven committed
129
130
    }
}