1 | using System;
|
---|
2 | using System.Linq;
|
---|
3 | using System.Text;
|
---|
4 | using System.Diagnostics;
|
---|
5 | using System.Windows;
|
---|
6 | using System.Windows.Media;
|
---|
7 | using System.Collections.Generic;
|
---|
8 |
|
---|
9 | using SharpVectors.Dom.Css;
|
---|
10 | using SharpVectors.Dom.Svg;
|
---|
11 |
|
---|
12 | using SharpVectors.Runtime;
|
---|
13 |
|
---|
14 | namespace SharpVectors.Renderers.Wpf
|
---|
15 | {
|
---|
16 | public sealed class WpfPathRendering : WpfRendering
|
---|
17 | {
|
---|
18 | #region Constructors and Destructor
|
---|
19 |
|
---|
20 | public WpfPathRendering(SvgElement element)
|
---|
21 | : base(element)
|
---|
22 | {
|
---|
23 | }
|
---|
24 |
|
---|
25 | #endregion
|
---|
26 |
|
---|
27 | #region Public Methods
|
---|
28 |
|
---|
29 | public override void BeforeRender(WpfDrawingRenderer renderer)
|
---|
30 | {
|
---|
31 | if (renderer == null)
|
---|
32 | {
|
---|
33 | return;
|
---|
34 | }
|
---|
35 |
|
---|
36 | WpfDrawingContext context = renderer.Context;
|
---|
37 |
|
---|
38 | SetQuality(context);
|
---|
39 | SetTransform(context);
|
---|
40 | SetMask(context);
|
---|
41 | }
|
---|
42 |
|
---|
43 | public override void Render(WpfDrawingRenderer renderer)
|
---|
44 | {
|
---|
45 | WpfDrawingContext context = renderer.Context;
|
---|
46 |
|
---|
47 | SvgRenderingHint hint = _svgElement.RenderingHint;
|
---|
48 | if (hint != SvgRenderingHint.Shape || hint == SvgRenderingHint.Clipping)
|
---|
49 | {
|
---|
50 | return;
|
---|
51 | }
|
---|
52 | // We do not directly render the contents of the clip-path, unless specifically requested...
|
---|
53 | if (String.Equals(_svgElement.ParentNode.LocalName, "clipPath") &&
|
---|
54 | !context.RenderingClipRegion)
|
---|
55 | {
|
---|
56 | return;
|
---|
57 | }
|
---|
58 |
|
---|
59 | SvgStyleableElement styleElm = (SvgStyleableElement)_svgElement;
|
---|
60 |
|
---|
61 | string sVisibility = styleElm.GetPropertyValue("visibility");
|
---|
62 | string sDisplay = styleElm.GetPropertyValue("display");
|
---|
63 | if (String.Equals(sVisibility, "hidden") || String.Equals(sDisplay, "none"))
|
---|
64 | {
|
---|
65 | return;
|
---|
66 | }
|
---|
67 |
|
---|
68 | DrawingGroup drawGroup = context.Peek();
|
---|
69 | Debug.Assert(drawGroup != null);
|
---|
70 |
|
---|
71 | Geometry geometry = CreateGeometry(_svgElement, context.OptimizePath);
|
---|
72 |
|
---|
73 | if (geometry != null && !geometry.IsEmpty())
|
---|
74 | {
|
---|
75 | SetClip(context);
|
---|
76 |
|
---|
77 | WpfSvgPaint fillPaint = new WpfSvgPaint(context, styleElm, "fill");
|
---|
78 |
|
---|
79 | string fileValue = styleElm.GetAttribute("fill");
|
---|
80 |
|
---|
81 | Brush brush = fillPaint.GetBrush();
|
---|
82 |
|
---|
83 | WpfSvgPaint strokePaint = new WpfSvgPaint(context, styleElm, "stroke");
|
---|
84 | Pen pen = strokePaint.GetPen();
|
---|
85 |
|
---|
86 | if (brush != null || pen != null)
|
---|
87 | {
|
---|
88 | Transform transform = this.Transform;
|
---|
89 | if (transform != null && !transform.Value.IsIdentity)
|
---|
90 | {
|
---|
91 | geometry.Transform = transform;
|
---|
92 | if (brush != null)
|
---|
93 | {
|
---|
94 | Transform brushTransform = brush.Transform;
|
---|
95 | if (brushTransform == null || brushTransform == Transform.Identity)
|
---|
96 | {
|
---|
97 | brush.Transform = transform;
|
---|
98 | }
|
---|
99 | }
|
---|
100 | }
|
---|
101 | else
|
---|
102 | {
|
---|
103 | transform = null; // render any identity transform useless...
|
---|
104 | }
|
---|
105 |
|
---|
106 | GeometryDrawing drawing = new GeometryDrawing(brush, pen, geometry);
|
---|
107 |
|
---|
108 | string elementId = this.GetElementName();
|
---|
109 | if (!String.IsNullOrEmpty(elementId) && !context.IsRegisteredId(elementId))
|
---|
110 | {
|
---|
111 | drawing.SetValue(FrameworkElement.NameProperty, elementId);
|
---|
112 |
|
---|
113 | context.RegisterId(elementId);
|
---|
114 |
|
---|
115 | if (context.IncludeRuntime)
|
---|
116 | {
|
---|
117 | SvgObject.SetId(drawing, elementId);
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | Brush maskBrush = this.Masking;
|
---|
122 | Geometry clipGeom = this.ClipGeometry;
|
---|
123 | if (clipGeom != null || maskBrush != null)
|
---|
124 | {
|
---|
125 | //Geometry clipped = Geometry.Combine(geometry, clipGeom,
|
---|
126 | // GeometryCombineMode.Exclude, null);
|
---|
127 |
|
---|
128 | //if (clipped != null && !clipped.IsEmpty())
|
---|
129 | //{
|
---|
130 | // geometry = clipped;
|
---|
131 | //}
|
---|
132 | DrawingGroup clipMaskGroup = new DrawingGroup();
|
---|
133 |
|
---|
134 | Rect geometryBounds = geometry.Bounds;
|
---|
135 |
|
---|
136 | if (clipGeom != null)
|
---|
137 | {
|
---|
138 | clipMaskGroup.ClipGeometry = clipGeom;
|
---|
139 |
|
---|
140 | SvgUnitType clipUnits = this.ClipUnits;
|
---|
141 | if (clipUnits == SvgUnitType.ObjectBoundingBox)
|
---|
142 | {
|
---|
143 | Rect drawingBounds = geometryBounds;
|
---|
144 |
|
---|
145 | if (transform != null)
|
---|
146 | {
|
---|
147 | drawingBounds = transform.TransformBounds(drawingBounds);
|
---|
148 | }
|
---|
149 |
|
---|
150 | TransformGroup transformGroup = new TransformGroup();
|
---|
151 |
|
---|
152 | // Scale the clip region (at (0, 0)) and translate to the top-left corner of the target.
|
---|
153 | transformGroup.Children.Add(
|
---|
154 | new ScaleTransform(drawingBounds.Width, drawingBounds.Height));
|
---|
155 | transformGroup.Children.Add(
|
---|
156 | new TranslateTransform(drawingBounds.X, drawingBounds.Y));
|
---|
157 |
|
---|
158 | clipGeom.Transform = transformGroup;
|
---|
159 | }
|
---|
160 | else
|
---|
161 | {
|
---|
162 | if (transform != null)
|
---|
163 | {
|
---|
164 | clipGeom.Transform = transform;
|
---|
165 | }
|
---|
166 | }
|
---|
167 | }
|
---|
168 | if (maskBrush != null)
|
---|
169 | {
|
---|
170 | SvgUnitType maskUnits = this.MaskUnits;
|
---|
171 | if (maskUnits == SvgUnitType.ObjectBoundingBox)
|
---|
172 | {
|
---|
173 | Rect drawingBounds = geometryBounds;
|
---|
174 |
|
---|
175 | if (transform != null)
|
---|
176 | {
|
---|
177 | drawingBounds = transform.TransformBounds(drawingBounds);
|
---|
178 | }
|
---|
179 |
|
---|
180 | TransformGroup transformGroup = new TransformGroup();
|
---|
181 |
|
---|
182 | // Scale the clip region (at (0, 0)) and translate to the top-left corner of the target.
|
---|
183 | transformGroup.Children.Add(
|
---|
184 | new ScaleTransform(drawingBounds.Width, drawingBounds.Height));
|
---|
185 | transformGroup.Children.Add(
|
---|
186 | new TranslateTransform(drawingBounds.X, drawingBounds.Y));
|
---|
187 |
|
---|
188 | DrawingGroup maskGroup = ((DrawingBrush)maskBrush).Drawing as DrawingGroup;
|
---|
189 | if (maskGroup != null)
|
---|
190 | {
|
---|
191 | DrawingCollection maskDrawings = maskGroup.Children;
|
---|
192 | for (int i = 0; i < maskDrawings.Count; i++)
|
---|
193 | {
|
---|
194 | Drawing maskDrawing = maskDrawings[i];
|
---|
195 | GeometryDrawing maskGeomDraw = maskDrawing as GeometryDrawing;
|
---|
196 | if (maskGeomDraw != null)
|
---|
197 | {
|
---|
198 | if (maskGeomDraw.Brush != null)
|
---|
199 | {
|
---|
200 | ConvertColors(maskGeomDraw.Brush);
|
---|
201 | }
|
---|
202 | if (maskGeomDraw.Pen != null)
|
---|
203 | {
|
---|
204 | ConvertColors(maskGeomDraw.Pen.Brush);
|
---|
205 | }
|
---|
206 | }
|
---|
207 | }
|
---|
208 | }
|
---|
209 |
|
---|
210 | //if (transformGroup != null)
|
---|
211 | //{
|
---|
212 | // drawingBounds = transformGroup.TransformBounds(drawingBounds);
|
---|
213 | //}
|
---|
214 |
|
---|
215 | //maskBrush.Viewbox = drawingBounds;
|
---|
216 | //maskBrush.ViewboxUnits = BrushMappingMode.Absolute;
|
---|
217 |
|
---|
218 | //maskBrush.Stretch = Stretch.Uniform;
|
---|
219 |
|
---|
220 | //maskBrush.Viewport = drawingBounds;
|
---|
221 | //maskBrush.ViewportUnits = BrushMappingMode.Absolute;
|
---|
222 |
|
---|
223 | maskBrush.Transform = transformGroup;
|
---|
224 | }
|
---|
225 | else
|
---|
226 | {
|
---|
227 | if (transform != null)
|
---|
228 | {
|
---|
229 | maskBrush.Transform = transform;
|
---|
230 | }
|
---|
231 | }
|
---|
232 |
|
---|
233 | clipMaskGroup.OpacityMask = maskBrush;
|
---|
234 | }
|
---|
235 |
|
---|
236 | clipMaskGroup.Children.Add(drawing);
|
---|
237 | drawGroup.Children.Add(clipMaskGroup);
|
---|
238 | }
|
---|
239 | else
|
---|
240 | {
|
---|
241 | drawGroup.Children.Add(drawing);
|
---|
242 | }
|
---|
243 | }
|
---|
244 | }
|
---|
245 |
|
---|
246 | RenderMarkers(renderer, styleElm, context);
|
---|
247 | }
|
---|
248 |
|
---|
249 | //==========================================================================
|
---|
250 | private static float AlphaComposition(Color color)
|
---|
251 | {
|
---|
252 | float max = Math.Max(Math.Max(color.ScR, color.ScG), color.ScB);
|
---|
253 | float min = Math.Min(Math.Min(color.ScR, color.ScG), color.ScB);
|
---|
254 |
|
---|
255 | return (min + max) / 2.0f;
|
---|
256 | }
|
---|
257 |
|
---|
258 | //==========================================================================
|
---|
259 | private static float AlphaComposition(Brush brush)
|
---|
260 | {
|
---|
261 | float alphaValue = 1.0f;
|
---|
262 |
|
---|
263 | if (brush != null)
|
---|
264 | {
|
---|
265 | if (brush is SolidColorBrush)
|
---|
266 | {
|
---|
267 | float nextValue = AlphaComposition((brush as SolidColorBrush).Color);
|
---|
268 | if (nextValue > 0 && nextValue < 1)
|
---|
269 | {
|
---|
270 | alphaValue = nextValue;
|
---|
271 | }
|
---|
272 | }
|
---|
273 | else if (brush is GradientBrush)
|
---|
274 | {
|
---|
275 | foreach (GradientStop gradient_stop in (brush as GradientBrush).GradientStops)
|
---|
276 | {
|
---|
277 | float nextValue = AlphaComposition(gradient_stop.Color);
|
---|
278 | if (nextValue > 0 && nextValue < 1)
|
---|
279 | {
|
---|
280 | alphaValue = nextValue;
|
---|
281 | }
|
---|
282 | }
|
---|
283 | }
|
---|
284 | //else if (brush is DrawingBrush)
|
---|
285 | //{
|
---|
286 | // ConvertColors((brush as DrawingBrush).Drawing);
|
---|
287 | //}
|
---|
288 | else
|
---|
289 | throw new NotSupportedException();
|
---|
290 |
|
---|
291 | }
|
---|
292 |
|
---|
293 | return alphaValue;
|
---|
294 | }
|
---|
295 |
|
---|
296 | //==========================================================================
|
---|
297 | private static Color ConvertColor(Color color)
|
---|
298 | {
|
---|
299 | float max = Math.Max(Math.Max(color.ScR, color.ScG), color.ScB);
|
---|
300 | float min = Math.Min(Math.Min(color.ScR, color.ScG), color.ScB);
|
---|
301 |
|
---|
302 | return Color.FromScRgb((min + max) / 2.0f, color.ScR, color.ScG, color.ScB);
|
---|
303 | }
|
---|
304 |
|
---|
305 | //==========================================================================
|
---|
306 | private static void ConvertColors(Brush brush)
|
---|
307 | {
|
---|
308 | if (brush != null)
|
---|
309 | {
|
---|
310 | SolidColorBrush solidBrush = null;
|
---|
311 | GradientBrush gradientBrush = null;
|
---|
312 |
|
---|
313 | if (DynamicCast.Cast(brush, out solidBrush))
|
---|
314 | {
|
---|
315 | solidBrush.Color = ConvertColor(solidBrush.Color);
|
---|
316 | }
|
---|
317 | else if (DynamicCast.Cast(brush, out gradientBrush))
|
---|
318 | {
|
---|
319 | GradientStopCollection stopColl = gradientBrush.GradientStops;
|
---|
320 |
|
---|
321 | foreach (GradientStop stop in stopColl)
|
---|
322 | {
|
---|
323 | stop.Color = ConvertColor(stop.Color);
|
---|
324 | }
|
---|
325 | }
|
---|
326 | //else if (brush is DrawingBrush)
|
---|
327 | //{
|
---|
328 | // ConvertColors((brush as DrawingBrush).Drawing);
|
---|
329 | //}
|
---|
330 | else
|
---|
331 | throw new NotSupportedException();
|
---|
332 |
|
---|
333 | }
|
---|
334 | }
|
---|
335 |
|
---|
336 | #endregion
|
---|
337 | }
|
---|
338 | }
|
---|