SvgRadialGradientServer.cs 7.88 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, SvgRenderer renderer, float opacity)
davescriven's avatar
davescriven committed
99
        {
100
            LoadStops(renderingElement);
James Welle's avatar
James Welle committed
101

102
            try
103
            {
104
105
106
107
                if (this.GradientUnits == SvgCoordinateUnits.ObjectBoundingBox) renderer.Boundable(renderingElement);
                var origin = renderer.Boundable().Location;
                var centerPoint = CalculateCenterPoint(renderer, origin);
                var focalPoint = CalculateFocalPoint(renderer, origin);
James Welle's avatar
James Welle committed
108

109
110
                var specifiedRadius = CalculateRadius(renderer);
                var effectiveRadius = CalculateEffectiveRadius(renderingElement, centerPoint, specifiedRadius);
James Welle's avatar
James Welle committed
111

112
113
114
115
116
                var brush = new PathGradientBrush(CreateGraphicsPath(origin, centerPoint, effectiveRadius))
                {
                    InterpolationColors = CalculateColorBlend(renderer, opacity, specifiedRadius, effectiveRadius),
                    CenterPoint = focalPoint
                };
James Welle's avatar
James Welle committed
117

118
119
120
121
122
123
124
125
                Debug.Assert(brush.Rectangle.Contains(renderingElement.Bounds), "Brush rectangle does not contain rendering element bounds!");

                return brush;
            }
            finally
            {
                if (this.GradientUnits == SvgCoordinateUnits.ObjectBoundingBox) renderer.PopBoundable();
            }
James Welle's avatar
James Welle committed
126
127
        }

128
        private PointF CalculateCenterPoint(SvgRenderer renderer, PointF origin)
James Welle's avatar
James Welle committed
129
        {
130
131
            var deviceCenterX = origin.X + CenterX.ToDeviceValue(renderer, UnitRenderingType.HorizontalOffset, this);
            var deviceCenterY = origin.Y + CenterY.ToDeviceValue(renderer, UnitRenderingType.VerticalOffset, this);
James Welle's avatar
James Welle committed
132
133
134
135
            var transformedCenterPoint = TransformPoint(new PointF(deviceCenterX, deviceCenterY));
            return transformedCenterPoint;
        }

136
        private PointF CalculateFocalPoint(SvgRenderer renderer, PointF origin)
James Welle's avatar
James Welle committed
137
        {
138
139
            var deviceFocalX = origin.X + FocalX.ToDeviceValue(renderer, UnitRenderingType.HorizontalOffset, this);
            var deviceFocalY = origin.Y + FocalY.ToDeviceValue(renderer, UnitRenderingType.VerticalOffset, this);
James Welle's avatar
James Welle committed
140
141
142
143
            var transformedFocalPoint = TransformPoint(new PointF(deviceFocalX, deviceFocalY));
            return transformedFocalPoint;
        }

144
        private float CalculateRadius(SvgRenderer renderer)
James Welle's avatar
James Welle committed
145
        {
146
            var radius = Radius.ToDeviceValue(renderer, UnitRenderingType.Other, this);
James Welle's avatar
James Welle committed
147
148
149
150
151
152
153
154
155
156
            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;
157
            }
158

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

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

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

James Welle's avatar
James Welle committed
193
            return path;
davescriven's avatar
davescriven committed
194
        }
195

196
        private ColorBlend CalculateColorBlend(SvgRenderer renderer, float opacity, float specifiedRadius, float effectiveRadius)
James Welle's avatar
James Welle committed
197
        {
198
            var colorBlend = GetColorBlend(renderer, opacity, true);
199

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

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

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

James Welle's avatar
James Welle committed
213
214
            return colorBlend;
        }
215

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