SvgRadialGradientServer.cs 7.54 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
        {
Eric Domke's avatar
Eric Domke committed
100
            LoadStops();
James Welle's avatar
James Welle committed
101
102
103
104
105
106
107
108
            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
109

James Welle's avatar
James Welle committed
110
            var brush = new PathGradientBrush(CreateGraphicsPath(origin, centerPoint, effectiveRadius))
111
            {
James Welle's avatar
James Welle committed
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
154
                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;
155
            }
156

James Welle's avatar
James Welle committed
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
            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;
        }
179

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

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

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

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

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

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

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

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

James Welle's avatar
James Welle committed
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
        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
231
232
    }
}