SvgRadialGradientServer.cs 7.51 KB
Newer Older
James Welle's avatar
James Welle committed
1
2
using System;
using System.Diagnostics;
davescriven's avatar
davescriven committed
3
4
using System.Drawing;
using System.Drawing.Drawing2D;
James Welle's avatar
James Welle committed
5
using System.Linq;
davescriven's avatar
davescriven committed
6
7
8

namespace Svg
{
9
    [SvgElement("radialGradient")]
davescriven's avatar
davescriven committed
10
11
12
13
14
    public sealed class SvgRadialGradientServer : SvgGradientServer
    {
        [SvgAttribute("cx")]
        public SvgUnit CenterX
        {
James Welle's avatar
James Welle committed
15
16
17
18
19
20
21
22
            get
            {
                return this.Attributes.GetAttribute<SvgUnit>("cx");
            }
            set
            {
                this.Attributes["cx"] = value;
            }
davescriven's avatar
davescriven committed
23
24
25
26
27
        }

        [SvgAttribute("cy")]
        public SvgUnit CenterY
        {
James Welle's avatar
James Welle committed
28
29
30
31
32
33
34
35
            get
            {
                return this.Attributes.GetAttribute<SvgUnit>("cy");
            }
            set
            {
                this.Attributes["cy"] = value;
            }
davescriven's avatar
davescriven committed
36
37
38
39
40
        }

        [SvgAttribute("r")]
        public SvgUnit Radius
        {
James Welle's avatar
James Welle committed
41
42
43
44
45
46
47
48
            get
            {
                return this.Attributes.GetAttribute<SvgUnit>("r");
            }
            set
            {
                this.Attributes["r"] = value;
            }
davescriven's avatar
davescriven committed
49
50
51
52
53
        }

        [SvgAttribute("fx")]
        public SvgUnit FocalX
        {
54
55
56
57
58
59
60
61
62
63
64
            get
            {
                var value = this.Attributes.GetAttribute<SvgUnit>("fx");

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

                return value;
            }
James Welle's avatar
James Welle committed
65
66
67
68
            set
            {
                this.Attributes["fx"] = value;
            }
davescriven's avatar
davescriven committed
69
70
71
72
73
        }

        [SvgAttribute("fy")]
        public SvgUnit FocalY
        {
74
75
76
77
78
79
80
81
82
83
84
            get
            {
                var value = this.Attributes.GetAttribute<SvgUnit>("fy");

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

                return value;
            }
James Welle's avatar
James Welle committed
85
86
87
88
            set
            {
                this.Attributes["fy"] = value;
            }
davescriven's avatar
davescriven committed
89
90
91
92
        }

        public SvgRadialGradientServer()
        {
James Welle's avatar
James Welle committed
93
94
95
            CenterX = new SvgUnit(SvgUnitType.Percentage, 50F);
            CenterY = new SvgUnit(SvgUnitType.Percentage, 50F);
            Radius = new SvgUnit(SvgUnitType.Percentage, 50F);
davescriven's avatar
davescriven committed
96
97
        }

98
        public override Brush GetBrush(SvgVisualElement renderingElement, float opacity)
davescriven's avatar
davescriven committed
99
        {
James Welle's avatar
James Welle committed
100
101
102
103
104
105
106
107
            var origin = CalculateOrigin(renderingElement);

            var centerPoint = CalculateCenterPoint(renderingElement, origin);

            var focalPoint = CalculateFocalPoint(renderingElement, origin);

            var specifiedRadius = CalculateRadius(renderingElement);
            var effectiveRadius = CalculateEffectiveRadius(renderingElement, centerPoint, specifiedRadius);
davescriven's avatar
davescriven committed
108

James Welle's avatar
James Welle committed
109
            var brush = new PathGradientBrush(CreateGraphicsPath(origin, centerPoint, effectiveRadius))
110
            {
James Welle's avatar
James Welle committed
111
112
113
114
115
116
117
118
119
120
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
152
153
                InterpolationColors = CalculateColorBlend(renderingElement, opacity, specifiedRadius, effectiveRadius),
                CenterPoint = focalPoint
            };

            Debug.Assert(brush.Rectangle.Contains(renderingElement.Bounds), "Brush rectangle does not contain rendering element bounds!");

            return brush;
        }

        private PointF CalculateOrigin(SvgVisualElement renderingElement)
        {
            return CalculateBoundable(renderingElement).Location;
        }

        private PointF CalculateCenterPoint(ISvgBoundable boundable, PointF origin)
        {
            var deviceCenterX = origin.X + CenterX.ToDeviceValue(boundable);
            var deviceCenterY = origin.Y + CenterY.ToDeviceValue(boundable, true);
            var transformedCenterPoint = TransformPoint(new PointF(deviceCenterX, deviceCenterY));
            return transformedCenterPoint;
        }

        private PointF CalculateFocalPoint(ISvgBoundable boundable, PointF origin)
        {
            var deviceFocalX = origin.X + FocalX.ToDeviceValue(boundable);
            var deviceFocalY = origin.Y + FocalY.ToDeviceValue(boundable, true);
            var transformedFocalPoint = TransformPoint(new PointF(deviceFocalX, deviceFocalY));
            return transformedFocalPoint;
        }

        private float CalculateRadius(ISvgBoundable boundable)
        {
            var radius = Radius.ToDeviceValue(boundable);
            var transformRadiusVector = TransformVector(new PointF(radius, 0));
            var transformedRadius = CalculateLength(transformRadiusVector);
            return transformedRadius;
        }

        private float CalculateEffectiveRadius(ISvgBoundable boundable, PointF centerPoint, float specifiedRadius)
        {
            if (SpreadMethod != SvgGradientSpreadMethod.Pad)
            {
                return specifiedRadius;
154
            }
155

James Welle's avatar
James Welle committed
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
            var topLeft = new PointF(boundable.Bounds.Left, boundable.Bounds.Top);
            var topRight = new PointF(boundable.Bounds.Right, boundable.Bounds.Top);
            var bottomRight = new PointF(boundable.Bounds.Right, boundable.Bounds.Bottom);
            var bottomLeft = new PointF(boundable.Bounds.Left, boundable.Bounds.Bottom);

            var effectiveRadius = (float)Math.Ceiling(
                Math.Max(
                    Math.Max(
                        CalculateDistance(centerPoint, topLeft),
                        CalculateDistance(centerPoint, topRight)
                    ),
                    Math.Max(
                        CalculateDistance(centerPoint, bottomRight),
                        CalculateDistance(centerPoint, bottomLeft)
                    )
                )
            );

            effectiveRadius = Math.Max(effectiveRadius, specifiedRadius);

            return effectiveRadius;
        }
178

James Welle's avatar
James Welle committed
179
180
181
        private static GraphicsPath CreateGraphicsPath(PointF origin, PointF centerPoint, float effectiveRadius)
        {
            var path = new GraphicsPath();
182

James Welle's avatar
James Welle committed
183
184
185
186
187
188
            path.AddEllipse(
                origin.X + centerPoint.X - effectiveRadius,
                origin.Y + centerPoint.Y - effectiveRadius,
                effectiveRadius * 2,
                effectiveRadius * 2
            );
189

James Welle's avatar
James Welle committed
190
            return path;
davescriven's avatar
davescriven committed
191
        }
192

James Welle's avatar
James Welle committed
193
194
195
        private ColorBlend CalculateColorBlend(SvgVisualElement renderingElement, float opacity, float specifiedRadius, float effectiveRadius)
        {
            var colorBlend = GetColorBlend(renderingElement, opacity, true);
196

James Welle's avatar
James Welle committed
197
198
199
200
            if (specifiedRadius >= effectiveRadius)
            {
                return colorBlend;
            }
201

James Welle's avatar
James Welle committed
202
203
204
205
            for (var i = 0; i < colorBlend.Positions.Length - 1; i++)
            {
                colorBlend.Positions[i] = 1 - (specifiedRadius / effectiveRadius) * (1 - colorBlend.Positions[i]);
            }
206

James Welle's avatar
James Welle committed
207
208
            colorBlend.Positions = new[] { 0F }.Concat(colorBlend.Positions).ToArray();
            colorBlend.Colors = new[] { colorBlend.Colors.First() }.Concat(colorBlend.Colors).ToArray();
209

James Welle's avatar
James Welle committed
210
211
            return colorBlend;
        }
212

James Welle's avatar
James Welle committed
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
        public override SvgElement DeepCopy()
        {
            return DeepCopy<SvgRadialGradientServer>();
        }

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

            newObj.CenterX = this.CenterX;
            newObj.CenterY = this.CenterY;
            newObj.Radius = this.Radius;
            newObj.FocalX = this.FocalX;
            newObj.FocalY = this.FocalY;

            return newObj;
        }
davescriven's avatar
davescriven committed
230
231
    }
}