Esempio n. 1
0
        private DrawingGroup CreateOuterGroup()
        {
            DrawingGroup drawGroup = null;

            SvgSymbolElement svgElm = (SvgSymbolElement)_svgElement;

            ISvgAnimatedPreserveAspectRatio animatedAspectRatio = svgElm.PreserveAspectRatio;

            if (animatedAspectRatio == null || animatedAspectRatio.AnimVal == null)
            {
                return(drawGroup);
            }

            double x      = svgElm.X.AnimVal.Value;
            double y      = svgElm.Y.AnimVal.Value;
            double width  = svgElm.Width.AnimVal.Value;
            double height = svgElm.Height.AnimVal.Value;

            Rect clipRect = new Rect(x, y, width, height);

            if (!clipRect.IsEmpty)
            {
                drawGroup = new DrawingGroup();
                drawGroup.ClipGeometry = new RectangleGeometry(clipRect);
            }

            return(drawGroup);
        }
Esempio n. 2
0
 public virtual void VisitSymbolElement(SvgSymbolElement element)
 => DefaultVisit(element);
        public override void BeforeRender(WpfDrawingRenderer renderer)
        {
            base.BeforeRender(renderer);

            WpfDrawingContext context = renderer.Context;

            Geometry  clipGeom  = this.ClipGeometry;
            Transform transform = this.Transform;

            if (transform == null &&
                (_svgElement.FirstChild != null && _svgElement.FirstChild == _svgElement.LastChild))
            {
                try
                {
                    SvgUseElement useElement = (SvgUseElement)_svgElement;

                    // If none of the following attribute exists, an exception is thrown...
                    double x      = useElement.X.AnimVal.Value;
                    double y      = useElement.Y.AnimVal.Value;
                    double width  = useElement.Width.AnimVal.Value;
                    double height = useElement.Height.AnimVal.Value;
                    if (width > 0 && height > 0)
                    {
                        Rect elementBounds = new Rect(x, y, width, height);

                        // Try handling the cases of "symbol" and "svg" sources within the "use"...
                        XmlNode childNode = _svgElement.FirstChild;
                        string  childName = childNode.Name;
                        if (String.Equals(childName, "symbol", StringComparison.OrdinalIgnoreCase))
                        {
                            SvgSymbolElement symbolElement = (SvgSymbolElement)childNode;

                            this.FitToViewbox(context, symbolElement, elementBounds);
                        }
                    }

                    transform = this.Transform;
                }
                catch
                {
                }
            }

            if (transform != null)
            {
                try
                {
                    SvgUseElement useElement = (SvgUseElement)_svgElement;

                    // If none of the following attribute exists, an exception is thrown...
                    double x      = useElement.X.AnimVal.Value;
                    double y      = useElement.Y.AnimVal.Value;
                    double width  = useElement.Width.AnimVal.Value;
                    double height = useElement.Height.AnimVal.Value;
                    if (width > 0 && height > 0)
                    {
                        Rect elementBounds = new Rect(x, y, width, height);

                        // Try handling the cases of "symbol" and "svg" sources within the "use"...
                        XmlNode childNode = _svgElement.FirstChild;
                        string  childName = childNode.Name;
                        if (String.Equals(childName, "symbol", StringComparison.OrdinalIgnoreCase))
                        {
                            SvgSymbolElement symbolElement = (SvgSymbolElement)childNode;

                            this.FitToViewbox(context, symbolElement, elementBounds);
                        }
                    }

                    Transform symbolTransform = this.Transform;
                    if (symbolTransform != null && !symbolTransform.Value.IsIdentity)
                    {
                        TransformGroup combinedTransform = new TransformGroup();

                        combinedTransform.Children.Add(transform);
                        combinedTransform.Children.Add(symbolTransform);

                        transform = combinedTransform;
                    }
                }
                catch
                {
                }
            }

            if (clipGeom != null || transform != null)
            {
                _drawGroup = new DrawingGroup();

                DrawingGroup currentGroup = context.Peek();

                if (currentGroup == null)
                {
                    throw new InvalidOperationException("An existing group is expected.");
                }

                currentGroup.Children.Add(_drawGroup);
                context.Push(_drawGroup);

                if (clipGeom != null)
                {
                    _drawGroup.ClipGeometry = clipGeom;
                }

                if (transform != null)
                {
                    _drawGroup.Transform = transform;
                }
            }
        }
Esempio n. 4
0
        public override void BeforeRender(WpfDrawingRenderer renderer)
        {
            base.BeforeRender(renderer);

            WpfDrawingContext context = renderer.Context;

            _drawGroup = new DrawingGroup();

            if (context.Count == 0)
            {
                context.Push(_drawGroup);
                context.Root = _drawGroup;
            }
            else if (context.Count == 1)
            {
                DrawingGroup currentGroup = context.Peek();

                if (currentGroup == null)
                {
                    throw new InvalidOperationException("An existing group is expected.");
                }
                if (currentGroup == context.Root && !context.IsFragment)
                {
                    SvgObject.SetName(_drawGroup, SvgObject.DrawLayer);
                    if (context.IncludeRuntime)
                    {
                        SvgLink.SetKey(_drawGroup, SvgObject.DrawLayer);
                    }
                }

                currentGroup.Children.Add(_drawGroup);
                context.Push(_drawGroup);
            }
            else
            {
                DrawingGroup currentGroup = context.Peek();

                if (currentGroup == null)
                {
                    throw new InvalidOperationException("An existing group is expected.");
                }

                currentGroup.Children.Add(_drawGroup);
                context.Push(_drawGroup);
            }

            SvgSymbolElement svgElm = (SvgSymbolElement)_svgElement;

            double x      = Math.Round(svgElm.X.AnimVal.Value, 4);
            double y      = Math.Round(svgElm.Y.AnimVal.Value, 4);
            double width  = Math.Round(svgElm.Width.AnimVal.Value, 4);
            double height = Math.Round(svgElm.Height.AnimVal.Value, 4);

            Rect elmRect = new Rect(x, y, width, height);

            XmlNode parentNode = _svgElement.ParentNode;

            ISvgFitToViewBox fitToView = svgElm as ISvgFitToViewBox;

            if (fitToView != null)
            {
                ISvgAnimatedRect animRect = fitToView.ViewBox;
                if (animRect != null)
                {
                    ISvgRect viewRect = animRect.AnimVal;
                    if (viewRect != null)
                    {
                        Rect wpfViewRect = WpfConvert.ToRect(viewRect);
                        if (!wpfViewRect.IsEmpty && wpfViewRect.Width > 0 && wpfViewRect.Height > 0)
                        {
                            elmRect = wpfViewRect;
                        }
                    }
                }
            }

            FitToViewbox(context, elmRect);

            Transform transform = this.Transform;

            if (transform != null)
            {
                _drawGroup.Transform = transform;
            }

            if (!elmRect.IsEmpty && !elmRect.Width.Equals(0) && !elmRect.Height.Equals(0))
            {
                // Elements such as "pattern" are also rendered by this renderer, so we make sure we are
                // dealing with the root SVG element...
                if (parentNode != null && parentNode.NodeType == XmlNodeType.Document)
                {
                    _drawGroup.ClipGeometry = new RectangleGeometry(elmRect);
                }
                else
                {
                    if (transform != null)
                    {
                        // We have already applied the transform, which will translate to the start point...
                        if (transform is TranslateTransform)
                        {
                            _drawGroup.ClipGeometry = new RectangleGeometry(
                                new Rect(0, 0, elmRect.Width, elmRect.Height));
                        }
                        else
                        {
                            _drawGroup.ClipGeometry = new RectangleGeometry(elmRect);
                        }
                    }
                    else
                    {
                        _drawGroup.ClipGeometry = new RectangleGeometry(elmRect);
                    }
                }
            }
        }
Esempio n. 5
0
        private void OnAfterRender(WpfDrawingRenderer renderer)
        {
            Debug.Assert(_drawGroup != null);

            WpfDrawingContext context = renderer.Context;

            DrawingGroup currentGroup = context.Peek();

            if (currentGroup == null || currentGroup != _drawGroup)
            {
                throw new InvalidOperationException("An existing group is expected.");
            }

            context.Pop();

            DrawingGroup drawGroup = CreateOuterGroup();

            if (drawGroup == null)
            {
                return;
            }

            SvgSymbolElement symbolElement = (SvgSymbolElement)_svgElement;

            var transform = _drawGroup.Transform;

            Transform useTransform = null;

            XmlNode childNode = _svgElement.FirstChild;
            string  childName = childNode.Name;

            try
            {
                // If none of the following attribute exists, an exception is thrown...
                double x      = symbolElement.X.AnimVal.Value;
                double y      = symbolElement.Y.AnimVal.Value;
                double width  = symbolElement.Width.AnimVal.Value;
                double height = symbolElement.Height.AnimVal.Value;
                if (width > 0 && height > 0)
                {
                    Rect elementBounds = new Rect(x, y, width, height);

                    useTransform = this.UseFitToViewbox(symbolElement, elementBounds);
                }
            }
            catch (Exception ex)
            {
                Trace.TraceError(ex.ToString());
            }

            if (useTransform != null)
            {
                if (transform == null)
                {
                    transform = useTransform;
                }
                else // if (transform != null)
                {
                    transform = Combine(useTransform, transform, true);
                }
            }

            _drawGroup.Transform = transform;

            currentGroup = context.Peek();
            if (currentGroup == null || currentGroup.Children.Remove(_drawGroup) == false)
            {
                return;
            }

            drawGroup.Children.Add(_drawGroup);
            currentGroup.Children.Add(drawGroup);
        }
Esempio n. 6
0
 public override PageElement VisitSymbolElement(SvgSymbolElement element) => null;