1 | using System;
|
---|
2 | using System.Xml;
|
---|
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.Svg;
|
---|
10 |
|
---|
11 | namespace SharpVectors.Renderers.Wpf
|
---|
12 | {
|
---|
13 | public sealed class WpfUseRendering : WpfRendering
|
---|
14 | {
|
---|
15 | #region Private Fields
|
---|
16 |
|
---|
17 | private DrawingGroup _drawGroup;
|
---|
18 |
|
---|
19 | #endregion
|
---|
20 |
|
---|
21 | #region Constructors and Destructor
|
---|
22 |
|
---|
23 | public WpfUseRendering(SvgElement element)
|
---|
24 | : base(element)
|
---|
25 | {
|
---|
26 | }
|
---|
27 |
|
---|
28 | #endregion
|
---|
29 |
|
---|
30 | #region Public Methods
|
---|
31 |
|
---|
32 | public override void BeforeRender(WpfDrawingRenderer renderer)
|
---|
33 | {
|
---|
34 | base.BeforeRender(renderer);
|
---|
35 |
|
---|
36 | WpfDrawingContext context = renderer.Context;
|
---|
37 |
|
---|
38 | Geometry clipGeom = this.ClipGeometry;
|
---|
39 | Transform transform = this.Transform;
|
---|
40 |
|
---|
41 | if (transform == null &&
|
---|
42 | (_svgElement.FirstChild != null && _svgElement.FirstChild == _svgElement.LastChild))
|
---|
43 | {
|
---|
44 | try
|
---|
45 | {
|
---|
46 | SvgUseElement useElement = (SvgUseElement)_svgElement;
|
---|
47 |
|
---|
48 | // If none of the following attribute exists, an exception is thrown...
|
---|
49 | double x = useElement.X.AnimVal.Value;
|
---|
50 | double y = useElement.Y.AnimVal.Value;
|
---|
51 | double width = useElement.Width.AnimVal.Value;
|
---|
52 | double height = useElement.Height.AnimVal.Value;
|
---|
53 | if (width > 0 && height > 0)
|
---|
54 | {
|
---|
55 | Rect elementBounds = new Rect(x, y, width, height);
|
---|
56 |
|
---|
57 | // Try handling the cases of "symbol" and "svg" sources within the "use"...
|
---|
58 | XmlNode childNode = _svgElement.FirstChild;
|
---|
59 | string childName = childNode.Name;
|
---|
60 | if (String.Equals(childName, "symbol", StringComparison.OrdinalIgnoreCase))
|
---|
61 | {
|
---|
62 | SvgSymbolElement symbolElement = (SvgSymbolElement)childNode;
|
---|
63 |
|
---|
64 | this.FitToViewbox(context, symbolElement, elementBounds);
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | transform = this.Transform;
|
---|
69 | }
|
---|
70 | catch
|
---|
71 | {
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | if (clipGeom != null || transform != null)
|
---|
76 | {
|
---|
77 | _drawGroup = new DrawingGroup();
|
---|
78 |
|
---|
79 | DrawingGroup currentGroup = context.Peek();
|
---|
80 |
|
---|
81 | if (currentGroup == null)
|
---|
82 | {
|
---|
83 | throw new InvalidOperationException("An existing group is expected.");
|
---|
84 | }
|
---|
85 |
|
---|
86 | currentGroup.Children.Add(_drawGroup);
|
---|
87 | context.Push(_drawGroup);
|
---|
88 |
|
---|
89 | if (clipGeom != null)
|
---|
90 | {
|
---|
91 | _drawGroup.ClipGeometry = clipGeom;
|
---|
92 | }
|
---|
93 |
|
---|
94 | if (transform != null)
|
---|
95 | {
|
---|
96 | _drawGroup.Transform = transform;
|
---|
97 | }
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | public override void Render(WpfDrawingRenderer renderer)
|
---|
102 | {
|
---|
103 | base.Render(renderer);
|
---|
104 | }
|
---|
105 |
|
---|
106 | public override void AfterRender(WpfDrawingRenderer renderer)
|
---|
107 | {
|
---|
108 | if (_drawGroup != null)
|
---|
109 | {
|
---|
110 | WpfDrawingContext context = renderer.Context;
|
---|
111 |
|
---|
112 | DrawingGroup currentGroup = context.Peek();
|
---|
113 |
|
---|
114 | if (currentGroup == null || currentGroup != _drawGroup)
|
---|
115 | {
|
---|
116 | throw new InvalidOperationException("An existing group is expected.");
|
---|
117 | }
|
---|
118 |
|
---|
119 | context.Pop();
|
---|
120 | }
|
---|
121 |
|
---|
122 | base.AfterRender(renderer);
|
---|
123 | }
|
---|
124 |
|
---|
125 | #endregion
|
---|
126 | }
|
---|
127 | }
|
---|