[12762] | 1 | using System;
|
---|
| 2 | using System.Xml;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.Text;
|
---|
| 5 | using System.Diagnostics;
|
---|
| 6 | using System.Collections.Generic;
|
---|
| 7 |
|
---|
| 8 | using System.Windows;
|
---|
| 9 | using System.Windows.Media;
|
---|
| 10 |
|
---|
| 11 | using SharpVectors.Dom.Svg;
|
---|
| 12 | using SharpVectors.Runtime;
|
---|
| 13 | using SharpVectors.Renderers.Utils;
|
---|
| 14 |
|
---|
| 15 | namespace SharpVectors.Renderers.Wpf
|
---|
| 16 | {
|
---|
| 17 | public sealed class WpfSvgRendering : WpfRendering
|
---|
| 18 | {
|
---|
| 19 | #region Private Fields
|
---|
| 20 |
|
---|
| 21 | private DrawingGroup _drawGroup;
|
---|
| 22 |
|
---|
| 23 | #endregion
|
---|
| 24 |
|
---|
| 25 | #region Constructors and Destructor
|
---|
| 26 |
|
---|
| 27 | public WpfSvgRendering(SvgElement element)
|
---|
| 28 | : base(element)
|
---|
| 29 | {
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | #endregion
|
---|
| 33 |
|
---|
| 34 | #region Public Methods
|
---|
| 35 |
|
---|
| 36 | public override void BeforeRender(WpfDrawingRenderer renderer)
|
---|
| 37 | {
|
---|
| 38 | base.BeforeRender(renderer);
|
---|
| 39 |
|
---|
| 40 | WpfDrawingContext context = renderer.Context;
|
---|
| 41 | _drawGroup = new DrawingGroup();
|
---|
| 42 |
|
---|
| 43 | if (context.Count == 0)
|
---|
| 44 | {
|
---|
| 45 | context.Push(_drawGroup);
|
---|
| 46 | context.Root = _drawGroup;
|
---|
| 47 | }
|
---|
| 48 | else if (context.Count == 1)
|
---|
| 49 | {
|
---|
| 50 | DrawingGroup currentGroup = context.Peek();
|
---|
| 51 |
|
---|
| 52 | if (currentGroup == null)
|
---|
| 53 | {
|
---|
| 54 | throw new InvalidOperationException("An existing group is expected.");
|
---|
| 55 | }
|
---|
| 56 | if (currentGroup == context.Root && !context.IsFragment)
|
---|
| 57 | {
|
---|
| 58 | _drawGroup.SetValue(FrameworkElement.NameProperty, SvgObject.DrawLayer);
|
---|
| 59 | if (context.IncludeRuntime)
|
---|
| 60 | {
|
---|
| 61 | SvgLink.SetKey(_drawGroup, SvgObject.DrawLayer);
|
---|
| 62 | }
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | currentGroup.Children.Add(_drawGroup);
|
---|
| 66 | context.Push(_drawGroup);
|
---|
| 67 | }
|
---|
| 68 | else
|
---|
| 69 | {
|
---|
| 70 | DrawingGroup currentGroup = context.Peek();
|
---|
| 71 |
|
---|
| 72 | if (currentGroup == null)
|
---|
| 73 | {
|
---|
| 74 | throw new InvalidOperationException("An existing group is expected.");
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | currentGroup.Children.Add(_drawGroup);
|
---|
| 78 | context.Push(_drawGroup);
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | SvgSvgElement svgElm = (SvgSvgElement)_svgElement;
|
---|
| 82 |
|
---|
| 83 | double x = Math.Round(svgElm.X.AnimVal.Value, 4);
|
---|
| 84 | double y = Math.Round(svgElm.Y.AnimVal.Value, 4);
|
---|
| 85 | double width = Math.Round(svgElm.Width.AnimVal.Value, 4);
|
---|
| 86 | double height = Math.Round(svgElm.Height.AnimVal.Value, 4);
|
---|
| 87 |
|
---|
| 88 | Rect elmRect = new Rect(x, y, width, height);
|
---|
| 89 |
|
---|
| 90 | //if (element.ParentNode is SvgElement)
|
---|
| 91 | //{
|
---|
| 92 | // // TODO: should it be moved with x and y?
|
---|
| 93 | //}
|
---|
| 94 |
|
---|
| 95 | XmlNode parentNode = _svgElement.ParentNode;
|
---|
| 96 |
|
---|
| 97 | //if (parentNode.NodeType == XmlNodeType.Document)
|
---|
| 98 | {
|
---|
| 99 | ISvgFitToViewBox fitToView = svgElm as ISvgFitToViewBox;
|
---|
| 100 | if (fitToView != null)
|
---|
| 101 | {
|
---|
| 102 | ISvgAnimatedRect animRect = fitToView.ViewBox;
|
---|
| 103 | if (animRect != null)
|
---|
| 104 | {
|
---|
| 105 | ISvgRect viewRect = animRect.AnimVal;
|
---|
| 106 | if (viewRect != null)
|
---|
| 107 | {
|
---|
| 108 | Rect wpfViewRect = WpfConvert.ToRect(viewRect);
|
---|
| 109 | if (!wpfViewRect.IsEmpty && wpfViewRect.Width > 0 && wpfViewRect.Height > 0)
|
---|
| 110 | {
|
---|
| 111 | elmRect = wpfViewRect;
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
| 114 | }
|
---|
| 115 | }
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | Transform transform = null;
|
---|
| 119 | if (parentNode.NodeType != XmlNodeType.Document)
|
---|
| 120 | {
|
---|
| 121 | FitToViewbox(context, elmRect);
|
---|
| 122 |
|
---|
| 123 | transform = this.Transform;
|
---|
| 124 | if (transform != null)
|
---|
| 125 | {
|
---|
| 126 | _drawGroup.Transform = transform;
|
---|
| 127 | }
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | //if (height > 0 && width > 0)
|
---|
| 131 | //{
|
---|
| 132 | // ClipGeometry = new RectangleGeometry(elmRect);
|
---|
| 133 | //}
|
---|
| 134 | //Geometry clipGeom = this.ClipGeometry;
|
---|
| 135 | //if (clipGeom != null)
|
---|
| 136 | //{
|
---|
| 137 | // _drawGroup.ClipGeometry = clipGeom;
|
---|
| 138 | //}
|
---|
| 139 |
|
---|
| 140 | if (((float)elmRect.Width != 0 && (float)elmRect.Height != 0))
|
---|
| 141 | {
|
---|
| 142 | // Elements such as "pattern" are also rendered by this renderer, so we make sure we are
|
---|
| 143 | // dealing with the root SVG element...
|
---|
| 144 | if (parentNode != null && parentNode.NodeType == XmlNodeType.Document)
|
---|
| 145 | {
|
---|
| 146 | _drawGroup.ClipGeometry = new RectangleGeometry(elmRect);
|
---|
| 147 | }
|
---|
| 148 | else
|
---|
| 149 | {
|
---|
| 150 | if (transform != null)
|
---|
| 151 | {
|
---|
| 152 | // We have already applied the transform, which will translate to the start point...
|
---|
| 153 | if (transform is TranslateTransform)
|
---|
| 154 | {
|
---|
| 155 | _drawGroup.ClipGeometry = new RectangleGeometry(
|
---|
| 156 | new Rect(0, 0, elmRect.Width, elmRect.Height));
|
---|
| 157 | }
|
---|
| 158 | else
|
---|
| 159 | {
|
---|
| 160 | _drawGroup.ClipGeometry = new RectangleGeometry(elmRect);
|
---|
| 161 | }
|
---|
| 162 | }
|
---|
| 163 | else
|
---|
| 164 | {
|
---|
| 165 | _drawGroup.ClipGeometry = new RectangleGeometry(elmRect);
|
---|
| 166 | }
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | //DrawingGroup animationGroup = context.Links;
|
---|
| 170 | //if (animationGroup != null)
|
---|
| 171 | //{
|
---|
| 172 | // animationGroup.ClipGeometry = clipGeom;
|
---|
| 173 | //}
|
---|
| 174 | }
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | public override void Render(WpfDrawingRenderer renderer)
|
---|
| 178 | {
|
---|
| 179 | base.Render(renderer);
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | public override void AfterRender(WpfDrawingRenderer renderer)
|
---|
| 183 | {
|
---|
| 184 | base.AfterRender(renderer);
|
---|
| 185 |
|
---|
| 186 | Debug.Assert(_drawGroup != null);
|
---|
| 187 |
|
---|
| 188 | WpfDrawingContext context = renderer.Context;
|
---|
| 189 |
|
---|
| 190 | DrawingGroup currentGroup = context.Peek();
|
---|
| 191 |
|
---|
| 192 | if (currentGroup == null || currentGroup != _drawGroup)
|
---|
| 193 | {
|
---|
| 194 | throw new InvalidOperationException("An existing group is expected.");
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | context.Pop();
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | #endregion
|
---|
| 201 | }
|
---|
| 202 | }
|
---|