1 | using System;
|
---|
2 | using System.Linq;
|
---|
3 | using System.Text;
|
---|
4 | using System.Diagnostics;
|
---|
5 | using System.Collections.Generic;
|
---|
6 |
|
---|
7 | using System.Windows;
|
---|
8 | using System.Windows.Media;
|
---|
9 |
|
---|
10 | using SharpVectors.Dom.Svg;
|
---|
11 | using SharpVectors.Runtime;
|
---|
12 |
|
---|
13 | namespace SharpVectors.Renderers.Wpf
|
---|
14 | {
|
---|
15 | public sealed class WpfGroupRendering : WpfRendering
|
---|
16 | {
|
---|
17 | #region Private Fields
|
---|
18 |
|
---|
19 | private DrawingGroup _drawGroup;
|
---|
20 |
|
---|
21 | #endregion
|
---|
22 |
|
---|
23 | #region Constructors and Destructor
|
---|
24 |
|
---|
25 | public WpfGroupRendering(SvgElement element)
|
---|
26 | : base(element)
|
---|
27 | {
|
---|
28 | }
|
---|
29 |
|
---|
30 | #endregion
|
---|
31 |
|
---|
32 | #region Public Methods
|
---|
33 |
|
---|
34 | public override void BeforeRender(WpfDrawingRenderer renderer)
|
---|
35 | {
|
---|
36 | base.BeforeRender(renderer);
|
---|
37 |
|
---|
38 | WpfDrawingContext context = renderer.Context;
|
---|
39 | _drawGroup = new DrawingGroup();
|
---|
40 |
|
---|
41 | string elementId = this.GetElementName();
|
---|
42 | if (!String.IsNullOrEmpty(elementId) && !context.IsRegisteredId(elementId))
|
---|
43 | {
|
---|
44 | _drawGroup.SetValue(FrameworkElement.NameProperty, elementId);
|
---|
45 |
|
---|
46 | context.RegisterId(elementId);
|
---|
47 |
|
---|
48 | if (context.IncludeRuntime)
|
---|
49 | {
|
---|
50 | SvgObject.SetId(_drawGroup, elementId);
|
---|
51 | }
|
---|
52 | }
|
---|
53 |
|
---|
54 | DrawingGroup currentGroup = context.Peek();
|
---|
55 |
|
---|
56 | if (currentGroup == null)
|
---|
57 | {
|
---|
58 | throw new InvalidOperationException("An existing group is expected.");
|
---|
59 | }
|
---|
60 |
|
---|
61 | currentGroup.Children.Add(_drawGroup);
|
---|
62 | context.Push(_drawGroup);
|
---|
63 | }
|
---|
64 |
|
---|
65 | public override void Render(WpfDrawingRenderer renderer)
|
---|
66 | {
|
---|
67 | if (_drawGroup != null)
|
---|
68 | {
|
---|
69 | Geometry clipGeom = this.ClipGeometry;
|
---|
70 | if (clipGeom != null)
|
---|
71 | {
|
---|
72 | _drawGroup.ClipGeometry = clipGeom;
|
---|
73 | }
|
---|
74 |
|
---|
75 | Transform transform = this.Transform;
|
---|
76 | if (transform != null)
|
---|
77 | {
|
---|
78 | _drawGroup.Transform = transform;
|
---|
79 | }
|
---|
80 |
|
---|
81 | float opacityValue = -1;
|
---|
82 |
|
---|
83 | SvgGElement element = (SvgGElement)_svgElement;
|
---|
84 | string opacity = element.GetAttribute("opacity");
|
---|
85 | if (opacity != null && opacity.Length > 0)
|
---|
86 | {
|
---|
87 | opacityValue = (float)SvgNumber.ParseNumber(opacity);
|
---|
88 | opacityValue = Math.Min(opacityValue, 1);
|
---|
89 | opacityValue = Math.Max(opacityValue, 0);
|
---|
90 | }
|
---|
91 |
|
---|
92 | if (opacityValue >= 0)
|
---|
93 | {
|
---|
94 | _drawGroup.Opacity = opacityValue;
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | base.Render(renderer);
|
---|
99 | }
|
---|
100 |
|
---|
101 | public override void AfterRender(WpfDrawingRenderer renderer)
|
---|
102 | {
|
---|
103 | Debug.Assert(_drawGroup != null);
|
---|
104 |
|
---|
105 | WpfDrawingContext context = renderer.Context;
|
---|
106 |
|
---|
107 | DrawingGroup currentGroup = context.Peek();
|
---|
108 |
|
---|
109 | if (currentGroup == null || currentGroup != _drawGroup)
|
---|
110 | {
|
---|
111 | throw new InvalidOperationException("An existing group is expected.");
|
---|
112 | }
|
---|
113 |
|
---|
114 | // Remove the added group from the stack...
|
---|
115 | context.Pop();
|
---|
116 |
|
---|
117 | // If the group is empty, we simply remove it...
|
---|
118 | if (_drawGroup.Children.Count == 0 && _drawGroup.ClipGeometry == null &&
|
---|
119 | _drawGroup.Transform == null)
|
---|
120 | {
|
---|
121 | currentGroup = context.Peek();
|
---|
122 | if (currentGroup != null)
|
---|
123 | {
|
---|
124 | currentGroup.Children.Remove(_drawGroup);
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | base.AfterRender(renderer);
|
---|
129 | }
|
---|
130 |
|
---|
131 | #endregion
|
---|
132 | }
|
---|
133 | }
|
---|