SvgGradientServer.cs 6.88 KB
Newer Older
davescriven's avatar
davescriven committed
1
2
3
4
5
6
7
8
9
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace Svg
{
10
11
12
    /// <summary>
    /// Provides the base class for all paint servers that wish to render a gradient.
    /// </summary>
davescriven's avatar
davescriven committed
13
14
    public abstract class SvgGradientServer : SvgPaintServer
    {
15
        private SvgCoordinateUnits _gradientUnits;
davescriven's avatar
davescriven committed
16
17
18
19
        private SvgGradientSpreadMethod _spreadMethod = SvgGradientSpreadMethod.Pad;
        private SvgGradientServer _inheritGradient;
        private List<SvgGradientStop> _stops;

20
21
22
        /// <summary>
        /// Initializes a new instance of the <see cref="SvgGradientServer"/> class.
        /// </summary>
davescriven's avatar
davescriven committed
23
24
        internal SvgGradientServer()
        {
25
            this.GradientUnits = SvgCoordinateUnits.ObjectBoundingBox;
davescriven's avatar
davescriven committed
26
27
28
            this._stops = new List<SvgGradientStop>();
        }

29
30
31
32
33
34
        /// <summary>
        /// Called by the underlying <see cref="SvgElement"/> when an element has been added to the
        /// <see cref="Children"/> collection.
        /// </summary>
        /// <param name="child">The <see cref="SvgElement"/> that has been added.</param>
        /// <param name="index">An <see cref="int"/> representing the index where the element was added to the collection.</param>
35
        protected override void AddElement(SvgElement child, int index)
davescriven's avatar
davescriven committed
36
37
        {
            if (child is SvgGradientStop)
38
            {
davescriven's avatar
davescriven committed
39
                this.Stops.Add((SvgGradientStop)child);
40
41
            }

42
            base.AddElement(child, index);
davescriven's avatar
davescriven committed
43
44
        }

45
46
47
48
49
        /// <summary>
        /// Called by the underlying <see cref="SvgElement"/> when an element has been removed from the
        /// <see cref="Children"/> collection.
        /// </summary>
        /// <param name="child">The <see cref="SvgElement"/> that has been removed.</param>
50
        protected override void RemoveElement(SvgElement child)
davescriven's avatar
davescriven committed
51
52
        {
            if (child is SvgGradientStop)
53
54
55
56
            {
                this.Stops.Remove((SvgGradientStop)child);
            }

57
            base.RemoveElement(child);
davescriven's avatar
davescriven committed
58
59
        }

60
61
62
        /// <summary>
        /// Gets the ramp of colors to use on a gradient.
        /// </summary>
davescriven's avatar
davescriven committed
63
64
65
66
67
        public List<SvgGradientStop> Stops
        {
            get { return this._stops; }
        }

68
69
70
        /// <summary>
        /// Specifies what happens if the gradient starts or ends inside the bounds of the target rectangle.
        /// </summary>
davescriven's avatar
davescriven committed
71
72
73
74
75
76
77
        [SvgAttribute("spreadMethod")]
        public SvgGradientSpreadMethod SpreadMethod
        {
            get { return this._spreadMethod; }
            set { this._spreadMethod = value; }
        }

78
79
80
        /// <summary>
        /// Gets or sets the coordinate system of the gradient.
        /// </summary>
davescriven's avatar
davescriven committed
81
        [SvgAttribute("gradientUnits")]
82
        public SvgCoordinateUnits GradientUnits
davescriven's avatar
davescriven committed
83
84
85
86
87
        {
            get { return this._gradientUnits; }
            set { this._gradientUnits = value; }
        }

88
89
90
91
        /// <summary>
        /// Gets or sets another gradient fill from which to inherit the stops from.
        /// </summary>
        [SvgAttributeAttribute("href")]
davescriven's avatar
davescriven committed
92
93
94
        public SvgGradientServer InheritGradient
        {
            get { return this._inheritGradient; }
95
96
97
98
99
            set 
            { 
                this._inheritGradient = value;
                this.InheritStops();
            }
davescriven's avatar
davescriven committed
100
101
        }

102
103
104
105
106
        /// <summary>
        /// Gets a <see cref="ColourBlend"/> representing the <see cref="SvgGradientServer"/>'s gradient stops.
        /// </summary>
        /// <param name="owner">The parent <see cref="SvgVisualElement"/>.</param>
        /// <param name="opacity">The opacity of the colour blend.</param>
107
        protected ColorBlend GetColourBlend(SvgVisualElement owner, float opacity)
davescriven's avatar
davescriven committed
108
109
110
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
        {
            ColorBlend blend = new ColorBlend();
            int colourBlends = this.Stops.Count;
            bool insertStart = false;
            bool insertEnd = false;

            //gradient.Transform = renderingElement.Transforms.Matrix;

            // May need to increase the number of colour blends because the range *must* be from 0.0 to 1.0.
            // E.g. 0.5 - 0.8 isn't valid therefore the rest need to be calculated.

            // If the first stop doesn't start at zero
            if (this.Stops[0].Offset.Value > 0)
            {
                colourBlends++;
                // Indicate that a colour has to be dynamically added at the start
                insertStart = true;
            }

            // If the last stop doesn't end at 1 a stop
            float lastValue = this.Stops[this.Stops.Count - 1].Offset.Value;
            if (lastValue < 100 || lastValue < 1)
            {
                colourBlends++;
                // Indicate that a colour has to be dynamically added at the end
                insertEnd = true;
            }

            blend.Positions = new float[colourBlends];
            blend.Colors = new Color[colourBlends];

            // Set positions and colour values
            int actualStops = 0;
            float mergedOpacity = 0.0f;
            float position = 0.0f;
            Color colour = Color.Black;

            for (int i = 0; i < colourBlends; i++)
            {
                mergedOpacity = opacity * this.Stops[actualStops].Opacity;
                position = (this.Stops[actualStops].Offset.ToDeviceValue(owner) / owner.Bounds.Width);
                colour = Color.FromArgb((int)(mergedOpacity * 255), this.Stops[actualStops++].Colour);

                // Insert this colour before itself at position 0
                if (insertStart && i == 0)
                {
                    blend.Positions[i] = 0.0f;
                    blend.Colors[i++] = colour;
                }

                blend.Positions[i] = position;
                blend.Colors[i] = colour;

                // Insert this colour after itself at position 0
                if (insertEnd && i == colourBlends - 2)
                {
                    blend.Positions[i + 1] = 1.0f;
                    blend.Colors[++i] = colour;
                }
            }

            return blend;
        }

172
173
174
175
        /// <summary>
        // If this gradient contains no stops then it will search any inherited gradients for stops.
        /// </summary>
        protected virtual void InheritStops()
davescriven's avatar
davescriven committed
176
        {
177
            if (this.Stops.Count == 0 && this.InheritGradient != null)
178
            {
179
                _stops.AddRange(this.InheritGradient.Stops);
180
            }
davescriven's avatar
davescriven committed
181
        }
182
183
184
185
186
187
188
189
190
191
192
193


		public override SvgElement DeepCopy<T>()
		{
			var newObj = base.DeepCopy<T>() as SvgGradientServer;
			newObj.SpreadMethod = this.SpreadMethod;
			newObj.GradientUnits = this.GradientUnits;
			newObj.InheritGradient = this.InheritGradient;
			return newObj;

		}

davescriven's avatar
davescriven committed
194
195
    }
}