1 | using System;
|
---|
2 | using System.Xml;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using System.Collections.Generic;
|
---|
6 | using System.Windows;
|
---|
7 | using System.Windows.Media;
|
---|
8 |
|
---|
9 | using SharpVectors.Dom.Svg;
|
---|
10 | using SharpVectors.Dom.Css;
|
---|
11 | using SharpVectors.Renderers.Utils;
|
---|
12 |
|
---|
13 | namespace SharpVectors.Renderers.Wpf
|
---|
14 | {
|
---|
15 | public sealed class WpfGradientFill : WpfFill
|
---|
16 | {
|
---|
17 | #region Private Fields
|
---|
18 |
|
---|
19 | private SvgGradientElement _gradientElement;
|
---|
20 |
|
---|
21 | #endregion
|
---|
22 |
|
---|
23 | #region Constructors and Destructor
|
---|
24 |
|
---|
25 | public WpfGradientFill(SvgGradientElement gradientElement)
|
---|
26 | {
|
---|
27 | _gradientElement = gradientElement;
|
---|
28 | }
|
---|
29 |
|
---|
30 | #endregion
|
---|
31 |
|
---|
32 | #region Public Methods
|
---|
33 |
|
---|
34 | public override Brush GetBrush(WpfDrawingContext context)
|
---|
35 | {
|
---|
36 | if (_gradientElement is SvgLinearGradientElement)
|
---|
37 | {
|
---|
38 | return GetLinearGradientBrush((SvgLinearGradientElement)_gradientElement);
|
---|
39 | }
|
---|
40 | else if (_gradientElement is SvgRadialGradientElement)
|
---|
41 | {
|
---|
42 | return GetRadialGradientBrush((SvgRadialGradientElement)_gradientElement);
|
---|
43 | }
|
---|
44 | else
|
---|
45 | {
|
---|
46 | return new SolidColorBrush(Colors.Black);
|
---|
47 | }
|
---|
48 | }
|
---|
49 |
|
---|
50 | #endregion
|
---|
51 |
|
---|
52 | #region Private Methods
|
---|
53 |
|
---|
54 | private LinearGradientBrush GetLinearGradientBrush(SvgLinearGradientElement res)
|
---|
55 | {
|
---|
56 | double x1 = res.X1.AnimVal.Value;
|
---|
57 | double x2 = res.X2.AnimVal.Value;
|
---|
58 | double y1 = res.Y1.AnimVal.Value;
|
---|
59 | double y2 = res.Y2.AnimVal.Value;
|
---|
60 |
|
---|
61 | GradientStopCollection gradientStops = GetGradientStops(res.Stops);
|
---|
62 |
|
---|
63 | LinearGradientBrush brush = new LinearGradientBrush(gradientStops,
|
---|
64 | new Point(x1, y1), new Point(x2, y2));
|
---|
65 |
|
---|
66 | SvgSpreadMethod spreadMethod = SvgSpreadMethod.None;
|
---|
67 | if (res.SpreadMethod != null)
|
---|
68 | {
|
---|
69 | spreadMethod = (SvgSpreadMethod)res.SpreadMethod.AnimVal;
|
---|
70 |
|
---|
71 | if (spreadMethod != SvgSpreadMethod.None)
|
---|
72 | {
|
---|
73 | brush.SpreadMethod = WpfConvert.ToSpreadMethod(spreadMethod);
|
---|
74 | }
|
---|
75 | }
|
---|
76 | if (res.GradientUnits != null)
|
---|
77 | {
|
---|
78 | SvgUnitType mappingMode = (SvgUnitType)res.GradientUnits.AnimVal;
|
---|
79 | if (mappingMode == SvgUnitType.ObjectBoundingBox)
|
---|
80 | {
|
---|
81 | brush.MappingMode = BrushMappingMode.RelativeToBoundingBox;
|
---|
82 | }
|
---|
83 | else if (mappingMode == SvgUnitType.UserSpaceOnUse)
|
---|
84 | {
|
---|
85 | brush.MappingMode = BrushMappingMode.Absolute;
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | MatrixTransform transform = GetTransformMatrix(res);
|
---|
90 | if (transform != null && !transform.Matrix.IsIdentity)
|
---|
91 | {
|
---|
92 | brush.Transform = transform;
|
---|
93 | }
|
---|
94 | //else
|
---|
95 | //{
|
---|
96 | // float fLeft = (float)res.X1.AnimVal.Value;
|
---|
97 | // float fRight = (float)res.X2.AnimVal.Value;
|
---|
98 | // float fTop = (float)res.Y1.AnimVal.Value;
|
---|
99 | // float fBottom = (float)res.Y2.AnimVal.Value;
|
---|
100 |
|
---|
101 | // if (fTop == fBottom)
|
---|
102 | // {
|
---|
103 | // //mode = LinearGradientMode.Horizontal;
|
---|
104 | // }
|
---|
105 | // else
|
---|
106 | // {
|
---|
107 | // if (fLeft == fRight)
|
---|
108 | // {
|
---|
109 | // //mode = LinearGradientMode.Vertical;
|
---|
110 | // }
|
---|
111 | // else
|
---|
112 | // {
|
---|
113 | // if (fLeft < fRight)
|
---|
114 | // {
|
---|
115 | // //mode = LinearGradientMode.ForwardDiagonal;
|
---|
116 | // brush.Transform = new RotateTransform(45, 0, 0);
|
---|
117 | // //brush.EndPoint = new Point(x1, y1 + 1);
|
---|
118 | // }
|
---|
119 | // else
|
---|
120 | // {
|
---|
121 | // //mode = LinearGradientMode.BackwardDiagonal;
|
---|
122 | // brush.Transform = new RotateTransform(-45);
|
---|
123 | // }
|
---|
124 | // }
|
---|
125 | // }
|
---|
126 | //}
|
---|
127 |
|
---|
128 | string colorInterpolation = res.GetPropertyValue("color-interpolation");
|
---|
129 | if (!String.IsNullOrEmpty(colorInterpolation))
|
---|
130 | {
|
---|
131 | if (colorInterpolation == "linearRGB")
|
---|
132 | {
|
---|
133 | brush.ColorInterpolationMode = ColorInterpolationMode.ScRgbLinearInterpolation;
|
---|
134 | }
|
---|
135 | else
|
---|
136 | {
|
---|
137 | brush.ColorInterpolationMode = ColorInterpolationMode.SRgbLinearInterpolation;
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | return brush;
|
---|
142 | }
|
---|
143 |
|
---|
144 | private RadialGradientBrush GetRadialGradientBrush(SvgRadialGradientElement res)
|
---|
145 | {
|
---|
146 | double centerX = res.Cx.AnimVal.Value;
|
---|
147 | double centerY = res.Cy.AnimVal.Value;
|
---|
148 | double focusX = res.Fx.AnimVal.Value;
|
---|
149 | double focusY = res.Fy.AnimVal.Value;
|
---|
150 | double radius = res.R.AnimVal.Value;
|
---|
151 |
|
---|
152 | GradientStopCollection gradientStops = GetGradientStops(res.Stops);
|
---|
153 |
|
---|
154 | RadialGradientBrush brush = new RadialGradientBrush(gradientStops);
|
---|
155 |
|
---|
156 | brush.RadiusX = radius;
|
---|
157 | brush.RadiusY = radius;
|
---|
158 | brush.Center = new Point(centerX, centerY);
|
---|
159 | brush.GradientOrigin = new Point(focusX, focusY);
|
---|
160 |
|
---|
161 | if (res.SpreadMethod != null)
|
---|
162 | {
|
---|
163 | SvgSpreadMethod spreadMethod = (SvgSpreadMethod)res.SpreadMethod.AnimVal;
|
---|
164 |
|
---|
165 | if (spreadMethod != SvgSpreadMethod.None)
|
---|
166 | {
|
---|
167 | brush.SpreadMethod = WpfConvert.ToSpreadMethod(spreadMethod);
|
---|
168 | }
|
---|
169 | }
|
---|
170 | if (res.GradientUnits != null)
|
---|
171 | {
|
---|
172 | SvgUnitType mappingMode = (SvgUnitType)res.GradientUnits.AnimVal;
|
---|
173 | if (mappingMode == SvgUnitType.ObjectBoundingBox)
|
---|
174 | {
|
---|
175 | brush.MappingMode = BrushMappingMode.RelativeToBoundingBox;
|
---|
176 | }
|
---|
177 | else if (mappingMode == SvgUnitType.UserSpaceOnUse)
|
---|
178 | {
|
---|
179 | brush.MappingMode = BrushMappingMode.Absolute;
|
---|
180 | }
|
---|
181 | }
|
---|
182 |
|
---|
183 | MatrixTransform transform = GetTransformMatrix(res);
|
---|
184 | if (transform != null && !transform.Matrix.IsIdentity)
|
---|
185 | {
|
---|
186 | brush.Transform = transform;
|
---|
187 | }
|
---|
188 |
|
---|
189 | string colorInterpolation = res.GetPropertyValue("color-interpolation");
|
---|
190 | if (!String.IsNullOrEmpty(colorInterpolation))
|
---|
191 | {
|
---|
192 | if (colorInterpolation == "linearRGB")
|
---|
193 | {
|
---|
194 | brush.ColorInterpolationMode = ColorInterpolationMode.SRgbLinearInterpolation;
|
---|
195 | }
|
---|
196 | else
|
---|
197 | {
|
---|
198 | brush.ColorInterpolationMode = ColorInterpolationMode.ScRgbLinearInterpolation;
|
---|
199 | }
|
---|
200 | }
|
---|
201 |
|
---|
202 | return brush;
|
---|
203 | }
|
---|
204 |
|
---|
205 | private MatrixTransform GetTransformMatrix(SvgGradientElement gradientElement)
|
---|
206 | {
|
---|
207 | SvgMatrix svgMatrix =
|
---|
208 | ((SvgTransformList)gradientElement.GradientTransform.AnimVal).TotalMatrix;
|
---|
209 |
|
---|
210 | MatrixTransform transformMatrix = new MatrixTransform(svgMatrix.A, svgMatrix.B, svgMatrix.C,
|
---|
211 | svgMatrix.D, svgMatrix.E, svgMatrix.F);
|
---|
212 |
|
---|
213 | return transformMatrix;
|
---|
214 | }
|
---|
215 |
|
---|
216 | private GradientStopCollection GetGradientStops(XmlNodeList stops)
|
---|
217 | {
|
---|
218 | int itemCount = stops.Count;
|
---|
219 | GradientStopCollection gradientStops = new GradientStopCollection(itemCount);
|
---|
220 |
|
---|
221 | double lastOffset = 0;
|
---|
222 | for (int i = 0; i < itemCount; i++)
|
---|
223 | {
|
---|
224 | SvgStopElement stop = (SvgStopElement)stops.Item(i);
|
---|
225 | string prop = stop.GetPropertyValue("stop-color");
|
---|
226 | WpfSvgColor svgColor = new WpfSvgColor(stop, "stop-color");
|
---|
227 |
|
---|
228 | double offset = stop.Offset.AnimVal;
|
---|
229 |
|
---|
230 | offset /= 100;
|
---|
231 | offset = Math.Max(lastOffset, offset);
|
---|
232 |
|
---|
233 | gradientStops.Add(new GradientStop(svgColor.Color, offset));
|
---|
234 | lastOffset = offset;
|
---|
235 | }
|
---|
236 |
|
---|
237 | if (itemCount == 0)
|
---|
238 | {
|
---|
239 | gradientStops.Add(new GradientStop(Colors.Black, 0));
|
---|
240 | gradientStops.Add(new GradientStop(Colors.Black, 1));
|
---|
241 | }
|
---|
242 |
|
---|
243 | return gradientStops;
|
---|
244 | }
|
---|
245 |
|
---|
246 | #endregion
|
---|
247 | }
|
---|
248 | }
|
---|