private Brush GetRadialGradientBrush(SvgRadialGradientElement res)
        {
            double centerX = res.Cx.AnimVal.Value;
            double centerY = res.Cy.AnimVal.Value;
            double focusX  = res.Fx.AnimVal.Value;
            double focusY  = res.Fy.AnimVal.Value;
            double radius  = res.R.AnimVal.Value;

            GradientStopCollection gradientStops = GetGradientStops(res.Stops);

            if (gradientStops == null || gradientStops.Count == 0)
            {
                return(null);
            }

            RadialGradientBrush brush = new RadialGradientBrush(gradientStops);

            brush.RadiusX        = radius;
            brush.RadiusY        = radius;
            brush.Center         = new Point(centerX, centerY);
            brush.GradientOrigin = new Point(focusX, focusY);

            if (res.SpreadMethod != null)
            {
                SvgSpreadMethod spreadMethod = (SvgSpreadMethod)res.SpreadMethod.AnimVal;

                if (spreadMethod != SvgSpreadMethod.None)
                {
                    brush.SpreadMethod = WpfConvert.ToSpreadMethod(spreadMethod);
                }
            }
            if (res.GradientUnits != null)
            {
                SvgUnitType mappingMode = (SvgUnitType)res.GradientUnits.AnimVal;
                if (mappingMode == SvgUnitType.ObjectBoundingBox)
                {
                    brush.MappingMode = BrushMappingMode.RelativeToBoundingBox;
                }
                else if (mappingMode == SvgUnitType.UserSpaceOnUse)
                {
                    brush.MappingMode = BrushMappingMode.Absolute;

                    if (res.Fx.AnimVal.UnitType == SvgLengthType.Percentage)
                    {
                        brush.GradientOrigin = brush.Center;
                    }
                }
            }

            MatrixTransform transform = GetTransformMatrix(res);

            if (transform != null && !transform.Matrix.IsIdentity)
            {
                brush.Transform = transform;
            }

            string colorInterpolation = res.GetPropertyValue("color-interpolation");

            if (!string.IsNullOrWhiteSpace(colorInterpolation))
            {
                if (colorInterpolation == "linearRGB")
                {
                    brush.ColorInterpolationMode = ColorInterpolationMode.SRgbLinearInterpolation;
                }
                else
                {
                    brush.ColorInterpolationMode = ColorInterpolationMode.ScRgbLinearInterpolation;
                }
            }

            return(brush);
        }
예제 #2
0
        private void RenderEndAlignedText(DrawingContext dc, PathGeometry svgPath, ISvgAnimatedLength startOffset)
        {
            if (svgPath == null || svgPath.Figures == null || svgPath.Figures.Count != 1)
            {
                Debug.Assert(false, "Monitor invalid path cases in debug mode!");
                return;
            }

            var pathFigure = svgPath.Figures[0];

            _pathLength = WpfConvert.GetPathFigureLength(pathFigure);
            if (_pathLength.Equals(0) || _textLength.Equals(0))
            {
                return;
            }

            double scalingFactor = 1.0; // Not scaling the text to fit...
            double progress      = 1.0;

            if (startOffset != null && startOffset.AnimVal != null)
            {
                ISvgLength offsetLength = startOffset.AnimVal;
                if (offsetLength.Value < 0)
                {
                    progress = 0;
                }

                switch (offsetLength.UnitType)
                {
                // If a percentage is given, then the startOffset represents a
                // percentage distance along the entire path.
                case SvgLengthType.Percentage:
                    if (!offsetLength.ValueInSpecifiedUnits.Equals(0))
                    {
                        progress += offsetLength.ValueInSpecifiedUnits / 100d;
                    }
                    break;

                // If a length other than a percentage is given, then the startOffset represents a distance along
                // the path measured in the current user coordinate system.
                case SvgLengthType.Number:
                    progress += offsetLength.ValueInSpecifiedUnits / _pathLength;
                    break;
                }
            }
            if (progress < 0)
            {
                progress *= -1;
            }

            if (progress > 1)
            {
                progress = progress - 1;
                while (progress > 1)
                {
                    progress = progress - 1;
                }
            }
            PathGeometry pathGeometry = new PathGeometry(new PathFigure[] { pathFigure });

            Point ptPrevPos = new Point(0, 0);

            int charCount = _pathChars.Count - 1;

            for (int i = charCount; i >= 0; i--)
            {
                var pathChar = _pathChars[i];

                var pathTextRun = _pathTextRuns[pathChar.Index];
                var textBuilder = pathTextRun.Builder;

                double width    = scalingFactor * pathChar.Width;
                double baseline = scalingFactor * textBuilder.Baseline;

                progress -= width / 2 / _pathLength;
                if (progress < 0)
                {
                    break;
                }
                Point ptNext, ptTangent;

                pathGeometry.GetPointAtFractionLength(progress, out ptNext, out ptTangent);

                if (i != charCount && AreEqual(ptNext, ptPrevPos))
                {
                    break;
                }

                var textPath = textBuilder.Build(pathTextRun.Element, pathChar.Text, pathChar.X, pathChar.Y);

                var textTransform = new TransformGroup();
                textTransform.Children.Add(new RotateTransform(Math.Atan2(ptTangent.Y, ptTangent.X) * 180 / Math.PI, width / 2, baseline));
                textTransform.Children.Add(new TranslateTransform(ptNext.X - width / 2, ptNext.Y - baseline));

                var curTransform = textPath.Transform;
                if (curTransform != null && curTransform.Value.IsIdentity == false)
                {
                    //Debug.Assert(false, "TODO: Wish to see the case");
                    textTransform.Children.Add(curTransform);
                }

                textPath.Transform = new MatrixTransform(textTransform.Value);

                pathTextRun.AddRun(textPath);

                progress -= width / 2 / _pathLength;

                ptPrevPos = ptNext;
            }

            foreach (var pathTextRun in _pathTextRuns)
            {
                pathTextRun.RenderRun(dc);
            }
        }
        private Brush GetLinearGradientBrush(SvgLinearGradientElement res, Transform viewBoxTransform = null)
        {
            GradientStopCollection gradientStops = GetGradientStops(res.Stops);

            if (gradientStops == null || gradientStops.Count == 0)
            {
                return(null);
            }

            double x1 = res.X1.AnimVal.Value;
            double x2 = res.X2.AnimVal.Value;
            double y1 = res.Y1.AnimVal.Value;
            double y2 = res.Y2.AnimVal.Value;

            LinearGradientBrush brush = new LinearGradientBrush(gradientStops,
                                                                new Point(x1, y1), new Point(x2, y2));

            SvgSpreadMethod spreadMethod = SvgSpreadMethod.Pad;

            if (res.SpreadMethod != null)
            {
                spreadMethod = (SvgSpreadMethod)res.SpreadMethod.AnimVal;

                if (spreadMethod != SvgSpreadMethod.None)
                {
                    brush.SpreadMethod = WpfConvert.ToSpreadMethod(spreadMethod);
                }
            }

            SvgUnitType mappingMode = SvgUnitType.ObjectBoundingBox;

            if (res.GradientUnits != null)
            {
                mappingMode = (SvgUnitType)res.GradientUnits.AnimVal;
                if (mappingMode == SvgUnitType.ObjectBoundingBox)
                {
                    brush.MappingMode = BrushMappingMode.RelativeToBoundingBox;
                }
                else if (mappingMode == SvgUnitType.UserSpaceOnUse)
                {
                    brush.MappingMode = BrushMappingMode.Absolute;

                    _isUserSpace = true;

                    //if (viewBoxTransform == null || viewBoxTransform.Value.IsIdentity)
                    //{
                    //    if (!elementBounds.IsEmpty)
                    //    {
                    //        viewBoxTransform = FitToViewbox(new SvgRect(x1, y1,
                    //            Math.Abs(x2 - x1), Math.Abs(y2 - y1)), elementBounds);
                    //    }
                    //}
                }
            }

            string colorInterpolation = res.GetPropertyValue("color-interpolation");

            if (!string.IsNullOrWhiteSpace(colorInterpolation))
            {
                if (string.Equals(colorInterpolation, "linearRGB", StringComparison.OrdinalIgnoreCase))
                {
                    brush.ColorInterpolationMode = ColorInterpolationMode.ScRgbLinearInterpolation;
                }
                else
                {
                    brush.ColorInterpolationMode = ColorInterpolationMode.SRgbLinearInterpolation;
                }
            }

            MatrixTransform transform = GetTransformMatrix(res);

            if (transform != null && !transform.Matrix.IsIdentity)
            {
                if (viewBoxTransform != null && !viewBoxTransform.Value.IsIdentity)
                {
                    TransformGroup group = new TransformGroup();
                    group.Children.Add(viewBoxTransform);
                    group.Children.Add(transform);

                    brush.Transform = group;
                }
                else
                {
                    brush.Transform = transform;
                    //brush.StartPoint = new Point(0, 0.5);
                    //brush.EndPoint = new Point(1, 0.5);
                }

                //brush.StartPoint = new Point(0, 0);
                //brush.EndPoint = new Point(1, 1);
            }
            else
            {
                float fLeft   = (float)res.X1.AnimVal.Value;
                float fRight  = (float)res.X2.AnimVal.Value;
                float fTop    = (float)res.Y1.AnimVal.Value;
                float fBottom = (float)res.Y2.AnimVal.Value;

                if (mappingMode == SvgUnitType.ObjectBoundingBox)
                {
                    if (!fTop.Equals(fBottom) && !fLeft.Equals(fRight))
                    {
                        var drawingBrush = new DrawingBrush();
                        drawingBrush.Stretch = Stretch.Fill;
                        drawingBrush.Viewbox = new Rect(0, 0, 1, 1);
                        var DrawingRect = new GeometryDrawing(brush, null, new RectangleGeometry(new Rect(0, 0, 1, 1)));
                        drawingBrush.Drawing = DrawingRect;
                        return(drawingBrush);
                    }
                }

                if (fTop.Equals(fBottom))
                {
                    //mode = LinearGradientMode.Horizontal;

                    //brush.StartPoint = new Point(0, 0.5);
                    //brush.EndPoint = new Point(1, 0.5);
                }
                else
                {
                    if (fLeft.Equals(fRight))
                    {
                        //mode = LinearGradientMode.Vertical;

                        //brush.StartPoint = new Point(0.5, 0);
                        //brush.EndPoint = new Point(0.5, 1);
                    }
                    else
                    {
                        if (fLeft < fRight)
                        {
                            if (viewBoxTransform != null && !viewBoxTransform.Value.IsIdentity)
                            {
                                //TransformGroup group = new TransformGroup();
                                //group.Children.Add(viewBoxTransform);
                                //group.Children.Add(new RotateTransform(45, 0.5, 0.5));

                                //brush.Transform = group;
                                brush.Transform = viewBoxTransform;
                            }
                            //else
                            //{
                            //    brush.RelativeTransform = new RotateTransform(45, 0.5, 0.5);
                            //}

                            //mode = LinearGradientMode.ForwardDiagonal;
                            //brush.EndPoint = new Point(x1, y1 + 1);

                            //brush.StartPoint = new Point(0, 0);
                            //brush.EndPoint = new Point(1, 1);
                        }
                        else
                        {
                            //mode = LinearGradientMode.BackwardDiagonal;
                            if (viewBoxTransform != null && !viewBoxTransform.Value.IsIdentity)
                            {
                                //TransformGroup group = new TransformGroup();
                                //group.Children.Add(viewBoxTransform);
                                //group.Children.Add(new RotateTransform(-45, 0.5, 0.5));

                                //brush.Transform = group;
                                brush.Transform = viewBoxTransform;
                            }
                            //else
                            //{
                            //    brush.RelativeTransform = new RotateTransform(-45, 0.5, 0.5);
                            //}

                            //brush.StartPoint = new Point(0, 0);
                            //brush.EndPoint = new Point(1, 1);
                        }
                    }
                }
            }

            return(brush);
        }
예제 #4
0
        public override void BeforeRender(WpfDrawingRenderer renderer)
        {
            base.BeforeRender(renderer);

            WpfDrawingContext context = renderer.Context;

            if (context.Count == 0)
            {
                _drawGroup = new DrawingGroup();
                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)
                {
                    if (context.IsFragment)
                    {
                        // Do not add extra layer to fragments...
                        _drawGroup = currentGroup;
                    }
                    else
                    {
                        _drawGroup = new DrawingGroup();
                        SvgObject.SetName(_drawGroup, SvgObject.DrawLayer);
                        if (context.IncludeRuntime)
                        {
                            SvgLink.SetKey(_drawGroup, SvgObject.DrawLayer);
                        }

                        currentGroup.Children.Add(_drawGroup);
                        context.Push(_drawGroup);
                    }
                }
                else
                {
                    _drawGroup = new DrawingGroup();
                    currentGroup.Children.Add(_drawGroup);
                    context.Push(_drawGroup);
                }
            }
            else
            {
                _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);
            }

            SvgSvgElement svgElm = (SvgSvgElement)_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);

            if (width < 0 || height < 0)
            {
                // For invalid dimension, prevent the drawing of the children...
                _isRecursive = true;
                return;
            }

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

            XmlNode parentNode = _svgElement.ParentNode;

            ISvgFitToViewBox fitToView = svgElm as ISvgFitToViewBox;
            ISvgAnimatedPreserveAspectRatio preserveAspectRatio = null;

            if (fitToView != null && fitToView.PreserveAspectRatio != null)
            {
                preserveAspectRatio = fitToView.PreserveAspectRatio;
                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;
                        }
                    }
                }
            }

            Transform transform   = null;
            var       aspectRatio = (preserveAspectRatio != null) ? preserveAspectRatio.AnimVal : null;

            if (parentNode.NodeType != XmlNodeType.Document ||
                (aspectRatio != null && aspectRatio.Align == SvgPreserveAspectRatioType.None))
            {
                FitToViewbox(context, elmRect);

                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);
                    }
                }
            }
        }
예제 #5
0
        private void RenderStartAlignedText(DrawingContext dc, PathGeometry svgPath,
                                            ISvgAnimatedLength startOffset, TextAlignment alignment)
        {
            if (svgPath == null || svgPath.Figures == null || svgPath.Figures.Count != 1)
            {
//                Debug.Assert(false, "Monitor invalid path cases in debug mode!");
                return;
            }

            var pathFigure = svgPath.Figures[0];

            _pathLength = WpfConvert.GetPathFigureLength(pathFigure);
            if (_pathLength.Equals(0) || _textLength.Equals(0))
            {
                return;
            }

            //double scalingFactor = pathLength / textLength;
            double scalingFactor = 1.0; // Not scaling the text to fit...
            double progress      = 0;

            if (startOffset != null && startOffset.AnimVal != null)
            {
                ISvgLength offsetLength = startOffset.AnimVal;
                switch (offsetLength.UnitType)
                {
                // If a percentage is given, then the startOffset represents a
                // percentage distance along the entire path.
                case SvgLengthType.Percentage:
                    if (!offsetLength.ValueInSpecifiedUnits.Equals(0))
                    {
                        progress += offsetLength.ValueInSpecifiedUnits / 100d;
                    }
                    break;

                // If a length other than a percentage is given, then the startOffset represents a distance along
                // the path measured in the current user coordinate system.
                case SvgLengthType.Number:
                    progress += offsetLength.ValueInSpecifiedUnits / _pathLength;
                    break;
                }
            }
            PathGeometry pathGeometry = new PathGeometry(new PathFigure[] { pathFigure });

            Point ptPrevPos = new Point(0, 0);

            double letterSpacing = 0;

            if (_textElement.LetterSpacing != null && _textElement.LetterSpacing.AnimVal != null)
            {
                letterSpacing = _textElement.LetterSpacing.AnimVal.ValueInSpecifiedUnits;
            }

            _pathLength += letterSpacing;

            var lengthFactor = 1.0;

            var textLength = this.GetTextLength();

            if (textLength != null && textLength.AnimVal != null)
            {
                var textLengthValue = textLength.AnimVal.ValueInSpecifiedUnits;
                if (textLengthValue > 0)
                {
                    lengthFactor = textLengthValue / _pathLength;
//                    lengthFactor = _textLength / textLengthValue;
//                    lengthFactor = textLengthValue / _textLength;
//                    lengthFactor = _pathLength / textLengthValue;
                    if (lengthFactor < 0.5) // Check for abuse!
                    {
                        lengthFactor = 1;
                    }
                }
            }

            int charCount = _pathChars.Count;

            for (int i = 0; i < charCount; i++)
            {
                var pathChar = _pathChars[i];

                var pathTextRun = _pathTextRuns[pathChar.Index];
                var textBuilder = pathTextRun.Builder;

                double width    = (scalingFactor * pathChar.Width * lengthFactor + letterSpacing);
                double baseline = scalingFactor * textBuilder.Baseline;

                progress += width / 2 / _pathLength;
                if (progress > 1)
                {
                    break;
                }
                Point ptNextPos, ptTangent;

                pathGeometry.GetPointAtFractionLength(progress, out ptNextPos, out ptTangent);

                if (i != 0 && AreEqual(ptNextPos, ptPrevPos))
                {
                    break;
                }

                var textPath = textBuilder.Build(pathTextRun.Element, pathChar.Text, pathChar.X, pathChar.Y);

                var textTransform = new TransformGroup();
                textTransform.Children.Add(new RotateTransform(Math.Atan2(ptTangent.Y, ptTangent.X) * 180 / Math.PI, width / 2, baseline));
                textTransform.Children.Add(new TranslateTransform(ptNextPos.X - width / 2, ptNextPos.Y - baseline));

                var curTransform = textPath.Transform;
                if (curTransform != null && curTransform.Value.IsIdentity == false)
                {
                    textTransform.Children.Add(curTransform);
                }

                textPath.Transform = new MatrixTransform(textTransform.Value);

                pathTextRun.AddRun(textPath);

                progress += width / 2 / _pathLength;

                ptPrevPos = ptNextPos;
            }

            foreach (var pathTextRun in _pathTextRuns)
            {
                pathTextRun.RenderRun(dc);
            }
        }
예제 #6
0
        private Brush GetBrush(Geometry geometry, string propPrefix, bool setOpacity)
        {
            SvgPaint fill;

            if (PaintType == SvgPaintType.None)
            {
                return(null);
            }
            if (PaintType == SvgPaintType.CurrentColor)
            {
                //TODO: Find a better way to support currentColor specified on parent element.
                var deferredFill = this.GetDeferredFill();
                if (deferredFill == null)
                {
                    fill = new WpfSvgPaint(_context, _element, "color");
                }
                else
                {
                    fill = deferredFill;
                }
            }
            else
            {
                fill = this;
            }

            SvgPaintType paintType = fill.PaintType;

            if (paintType == SvgPaintType.Uri || paintType == SvgPaintType.UriCurrentColor ||
                paintType == SvgPaintType.UriNone || paintType == SvgPaintType.UriRgbColor ||
                paintType == SvgPaintType.UriRgbColorIccColor)
            {
                _paintFill = GetPaintFill(fill.Uri);
                if (_paintFill != null)
                {
                    Brush brush = null;
                    if (geometry != null)
                    {
                        brush = _paintFill.GetBrush(geometry.Bounds, _context, geometry.Transform);
                    }
                    else
                    {
                        brush = _paintFill.GetBrush(Rect.Empty, _context, null);
                    }

                    if (brush != null)
                    {
                        brush.Opacity = GetOpacity(propPrefix);
                    }

                    return(brush);
                }
                else
                {
                    if (PaintType == SvgPaintType.UriNone || PaintType == SvgPaintType.Uri)
                    {
                        return(null);
                    }
                    if (PaintType == SvgPaintType.UriCurrentColor)
                    {
                        fill = new WpfSvgPaint(_context, _element, "color");
                    }
                    else
                    {
                        fill = this;
                    }
                }
            }

            if (fill == null || fill.RgbColor == null)
            {
                return(null);
            }

            Color?solidColor = WpfConvert.ToColor(fill.RgbColor);

            if (solidColor == null)
            {
                return(null);
            }

            SolidColorBrush solidBrush = new SolidColorBrush(solidColor.Value);

            //int opacity = GetOpacity(propPrefix);
            //solidBrush.Color = Color.FromArgb(opacity, brush.Color);
            if (setOpacity)
            {
                solidBrush.Opacity = GetOpacity(propPrefix);
            }
            return(solidBrush);
        }
예제 #7
0
        public override void BeforeRender(WpfDrawingRenderer renderer)
        {
            base.BeforeRender(renderer);

            _idElement = string.Empty;

            WpfDrawingContext context = renderer.Context;

            DrawingGroup currentGroup = context.Peek();

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

            if (currentGroup == context.Root)
            {
                if (context.IsFragment)
                {
                    // Do not add extra layer to fragments...
                    _drawGroup = currentGroup;
                }
                else
                {
                    _drawGroup = new DrawingGroup();
                    SvgObject.SetName(_drawGroup, SvgObject.DrawLayer);
                    if (context.IncludeRuntime)
                    {
                        SvgLink.SetKey(_drawGroup, SvgObject.DrawLayer);
                    }

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

            SvgPatternElement svgElm = (SvgPatternElement)_svgElement;

            _idElement = svgElm.Id;
            if (!string.IsNullOrWhiteSpace(_idElement))
            {
                context.AddUrl(_idElement);
            }

            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);

            if (width < 0 || height < 0)
            {
                // For invalid dimension, prevent the drawing of the children...
                _isRecursive = true;
                return;
            }

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

//            XmlNode parentNode = _svgElement.ParentNode;

            ISvgFitToViewBox fitToView = svgElm as ISvgFitToViewBox;
            ISvgAnimatedPreserveAspectRatio preserveAspectRatio = null;

            if (fitToView != null && fitToView.PreserveAspectRatio != null)
            {
                preserveAspectRatio = fitToView.PreserveAspectRatio;
                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;
                        }
                    }
                }
            }

            Transform transform   = null;
            var       aspectRatio = (preserveAspectRatio != null) ? preserveAspectRatio.AnimVal : null;

            if (aspectRatio != null /* && aspectRatio.Align == SvgPreserveAspectRatioType.None*/)
            {
                FitToViewbox(context, elmRect);

                transform = this.Transform;
                if (transform != null)
                {
                    _drawGroup.Transform = transform;
                }
            }
        }
예제 #8
0
        private LinearGradientBrush GetLinearGradientBrush(Rect elementBounds,
                                                           SvgLinearGradientElement res)
        {
            double x1 = res.X1.AnimVal.Value;
            double x2 = res.X2.AnimVal.Value;
            double y1 = res.Y1.AnimVal.Value;
            double y2 = res.Y2.AnimVal.Value;

            GradientStopCollection gradientStops = GetGradientStops(res.Stops);

            //LinearGradientBrush brush = new LinearGradientBrush(gradientStops);
            LinearGradientBrush brush = new LinearGradientBrush(gradientStops,
                                                                new Point(x1, y1), new Point(x2, y2));

            SvgSpreadMethod spreadMethod = SvgSpreadMethod.Pad;

            if (res.SpreadMethod != null)
            {
                spreadMethod = (SvgSpreadMethod)res.SpreadMethod.AnimVal;

                if (spreadMethod != SvgSpreadMethod.None)
                {
                    brush.SpreadMethod = WpfConvert.ToSpreadMethod(spreadMethod);
                }
            }

            Transform viewBoxTransform = null;

            SvgUnitType mappingMode = SvgUnitType.ObjectBoundingBox;

            if (res.GradientUnits != null)
            {
                mappingMode = (SvgUnitType)res.GradientUnits.AnimVal;
                if (mappingMode == SvgUnitType.ObjectBoundingBox)
                {
                    brush.MappingMode = BrushMappingMode.RelativeToBoundingBox;
                }
                else if (mappingMode == SvgUnitType.UserSpaceOnUse)
                {
                    brush.MappingMode = BrushMappingMode.Absolute;

                    viewBoxTransform = FitToViewbox(new SvgRect(x1, y1,
                                                                Math.Abs(x2 - x1), Math.Abs(y2 - y1)),
                                                    elementBounds);
                }
            }

            MatrixTransform transform = GetTransformMatrix(res);

            if (transform != null && !transform.Matrix.IsIdentity)
            {
                if (viewBoxTransform != null)
                {
                    TransformGroup group = new TransformGroup();
                    group.Children.Add(viewBoxTransform);
                    group.Children.Add(transform);

                    brush.Transform = group;
                }
                else
                {
                    brush.Transform = transform;
                    //brush.StartPoint = new Point(0, 0.5);
                    //brush.EndPoint = new Point(1, 0.5);
                }

                //brush.StartPoint = new Point(0, 0);
                //brush.EndPoint = new Point(1, 1);
            }
            else
            {
                float fLeft   = (float)res.X1.AnimVal.Value;
                float fRight  = (float)res.X2.AnimVal.Value;
                float fTop    = (float)res.Y1.AnimVal.Value;
                float fBottom = (float)res.Y2.AnimVal.Value;

                if (fTop == fBottom)
                {
                    //mode = LinearGradientMode.Horizontal;

                    //brush.StartPoint = new Point(0, 0.5);
                    //brush.EndPoint = new Point(1, 0.5);
                }
                else
                {
                    if (fLeft == fRight)
                    {
                        //var mode = LinearGradientMode.Vertical;

                        //brush.StartPoint = new Point(0.5, 0);
                        // brush.EndPoint = new Point(0.5, 1);
                    }
                    else
                    {
                        if (fLeft < fRight)
                        {
                            if (viewBoxTransform != null)
                            {
                                TransformGroup group = new TransformGroup();
                                group.Children.Add(viewBoxTransform);
                                group.Children.Add(new RotateTransform(45, 0.5, 0.5));

                                brush.Transform = group;
                            }
                            else
                            {
                                //brush.RelativeTransform = new RotateTransform(45, 0.5, 0.5);
                            }

                            //mode = LinearGradientMode.ForwardDiagonal;
                            //brush.EndPoint = new Point(x1, y1 + 1);

                            //brush.StartPoint = new Point(0, 0);
                            //brush.EndPoint = new Point(1, 1);
                        }
                        else
                        {
                            //mode = LinearGradientMode.BackwardDiagonal;
                            if (viewBoxTransform != null)
                            {
                                TransformGroup group = new TransformGroup();
                                group.Children.Add(viewBoxTransform);
                                group.Children.Add(new RotateTransform(-45, 0.5, 0.5));

                                brush.Transform = group;
                            }
                            else
                            {
                                brush.RelativeTransform = new RotateTransform(-45, 0.5, 0.5);
                            }

                            //brush.StartPoint = new Point(0, 0);
                            //brush.EndPoint = new Point(1, 1);
                        }
                    }
                }
            }

            string colorInterpolation = res.GetPropertyValue("color-interpolation");

            if (!String.IsNullOrEmpty(colorInterpolation))
            {
                if (colorInterpolation == "linearRGB")
                {
                    brush.ColorInterpolationMode = ColorInterpolationMode.ScRgbLinearInterpolation;
                }
                else
                {
                    brush.ColorInterpolationMode = ColorInterpolationMode.SRgbLinearInterpolation;
                }
            }

            return(brush);
        }
예제 #9
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)
                {
                    _drawGroup.SetValue(FrameworkElement.NameProperty, 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);
            }

            SvgSvgElement svgElm = (SvgSvgElement)_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);

            //if (element.ParentNode is SvgElement)
            //{
            //    // TODO: should it be moved with x and y?
            //}

            XmlNode parentNode = _svgElement.ParentNode;

            //if (parentNode.NodeType == XmlNodeType.Document)
            {
                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;
                            }
                        }
                    }
                }
            }

            Transform transform = null;

            if (parentNode.NodeType != XmlNodeType.Document)
            {
                FitToViewbox(context, elmRect);

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

            //if (height > 0 && width > 0)
            //{
            //    ClipGeometry = new RectangleGeometry(elmRect);
            //}
            //Geometry clipGeom = this.ClipGeometry;
            //if (clipGeom != null)
            //{
            //    _drawGroup.ClipGeometry = clipGeom;
            //}

            if (((float)elmRect.Width != 0 && (float)elmRect.Height != 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);
                    }
                }

                //DrawingGroup animationGroup = context.Links;
                //if (animationGroup != null)
                //{
                //    animationGroup.ClipGeometry = clipGeom;
                //}
            }
        }
예제 #10
0
        private Brush GetRadialGradientBrush(SvgRadialGradientElement res)
        {
            var refElem = res.ReferencedElement;

            double centerX = res.Cx.AnimVal.Value;
            double centerY = res.Cy.AnimVal.Value;

            // 'fx', 'fy', and 'fr' define the start circle for the radial gradient.
            double focusX = res.Fx.AnimVal.Value;
            double focusY = res.Fy.AnimVal.Value;
            double radius = res.R.AnimVal.Value;

            var lengthUnit = res.Cx.AnimVal.UnitType;

            // If attribute 'fx' is not specified, 'fx' will coincide with the presentational
            // value of 'cx' for the element whether the value for 'cx' was inherited or not.
            if (lengthUnit == SvgLengthType.Percentage)
            {
                if (!res.HasAttribute("fx") && (refElem == null || !refElem.HasAttribute("fx")))
                {
                    focusX = centerX;
                }
                else if (focusX.Equals(0.0))
                {
                    focusX = centerX;
                    if (focusX > 0 && focusX >= radius)
                    {
                        focusX = (centerX > radius) ? centerX - radius : focusX = radius;
                    }
                }
                else
                {
                    if (focusX > 0 && focusX >= radius)
                    {
                        focusX = (centerX > radius) ? centerX - radius : focusX = radius;
                    }
                }
            }

            lengthUnit = res.Cy.AnimVal.UnitType;
            // If attribute 'fy' is not specified, 'fy' will coincide with the presentational
            // value of 'cy' for the element whether the value for 'cy' was inherited or not.
            if (lengthUnit == SvgLengthType.Percentage)
            {
                if (!res.HasAttribute("fy") && (refElem == null || !refElem.HasAttribute("fy")))
                {
                    focusY = centerY;
                }
                else if (focusY.Equals(0.0))
                {
                    focusY = centerY;
                    if (focusY > 0 && focusY >= radius)
                    {
                        focusY = (centerY > radius) ? centerY - radius : focusY = radius;
                    }
                }
                else
                {
                    if (focusY > 0 && focusY >= radius)
                    {
                        focusY = (centerY > radius) ? centerY - radius : focusY = radius;
                    }
                }
            }

            GradientStopCollection gradientStops = GetGradientStops(res.Stops);

            if (gradientStops == null || gradientStops.Count == 0)
            {
                return(null);
            }

            RadialGradientBrush brush = new RadialGradientBrush(gradientStops);

            brush.RadiusX        = radius;
            brush.RadiusY        = radius;
            brush.Center         = new Point(centerX, centerY);
            brush.GradientOrigin = new Point(focusX, focusY);

            if (res.SpreadMethod != null)
            {
                SvgSpreadMethod spreadMethod = (SvgSpreadMethod)res.SpreadMethod.AnimVal;

                if (spreadMethod != SvgSpreadMethod.None)
                {
                    brush.SpreadMethod = WpfConvert.ToSpreadMethod(spreadMethod);
                }
            }
            if (res.GradientUnits != null)
            {
                SvgUnitType mappingMode = (SvgUnitType)res.GradientUnits.AnimVal;
                if (mappingMode == SvgUnitType.ObjectBoundingBox)
                {
                    brush.MappingMode = BrushMappingMode.RelativeToBoundingBox;
                }
                else if (mappingMode == SvgUnitType.UserSpaceOnUse)
                {
                    brush.MappingMode = BrushMappingMode.Absolute;

                    if (res.Fx.AnimVal.UnitType == SvgLengthType.Percentage)
                    {
                        brush.GradientOrigin = brush.Center;
                    }

                    _isUserSpace = true;
                }
            }

            MatrixTransform transform = GetTransformMatrix(res);

            if (transform != null && !transform.Matrix.IsIdentity)
            {
                brush.Transform = transform;
            }

            string colorInterpolation = res.GetPropertyValue("color-interpolation");

            if (!string.IsNullOrWhiteSpace(colorInterpolation))
            {
                if (string.Equals(colorInterpolation, "linearRGB", StringComparison.OrdinalIgnoreCase))
                {
                    brush.ColorInterpolationMode = ColorInterpolationMode.SRgbLinearInterpolation;
                }
                else
                {
                    brush.ColorInterpolationMode = ColorInterpolationMode.ScRgbLinearInterpolation;
                }
            }

            return(brush);
        }
예제 #11
0
        protected void SetClip(WpfDrawingContext context)
        {
            _clipPathUnits = SvgUnitType.UserSpaceOnUse;

            if (_svgElement == null)
            {
                return;
            }

            #region Clip with clip

            // see http://www.w3.org/TR/SVG/masking.html#OverflowAndClipProperties
            if (_svgElement is ISvgSvgElement || _svgElement is ISvgMarkerElement ||
                _svgElement is ISvgSymbolElement || _svgElement is ISvgPatternElement)
            {
                // check overflow property
                CssValue overflow = _svgElement.GetComputedCssValue("overflow", string.Empty) as CssValue;
                // TODO: clip can have "rect(10 10 auto 10)"
                CssPrimitiveValue clip = _svgElement.GetComputedCssValue("clip", string.Empty) as CssPrimitiveValue;

                string sOverflow = null;

                if (overflow != null && !string.IsNullOrWhiteSpace(overflow.CssText))
                {
                    sOverflow = overflow.CssText;
                }
                else
                {
                    if (this is ISvgSvgElement)
                    {
                        sOverflow = "hidden";
                    }
                }

                if (sOverflow != null)
                {
                    // "If the 'overflow' property has a value other than hidden or scroll,
                    // the property has no effect (i.e., a clipping rectangle is not created)."
                    if (sOverflow == "hidden" || sOverflow == "scroll")
                    {
                        Rect clipRect = Rect.Empty;
                        if (clip != null && clip.PrimitiveType == CssPrimitiveType.Rect)
                        {
                            if (_svgElement is ISvgSvgElement)
                            {
                                ISvgSvgElement svgElement = (ISvgSvgElement)_svgElement;
                                SvgRect        viewPort   = svgElement.Viewport as SvgRect;
                                clipRect = WpfConvert.ToRect(viewPort);
                                ICssRect clipShape = (CssRect)clip.GetRectValue();
                                if (clipShape.Top.PrimitiveType != CssPrimitiveType.Ident)
                                {
                                    clipRect.Y += clipShape.Top.GetFloatValue(CssPrimitiveType.Number);
                                }
                                if (clipShape.Left.PrimitiveType != CssPrimitiveType.Ident)
                                {
                                    clipRect.X += clipShape.Left.GetFloatValue(CssPrimitiveType.Number);
                                }
                                if (clipShape.Right.PrimitiveType != CssPrimitiveType.Ident)
                                {
                                    clipRect.Width = (clipRect.Right - clipRect.X) - clipShape.Right.GetFloatValue(CssPrimitiveType.Number);
                                }
                                if (clipShape.Bottom.PrimitiveType != CssPrimitiveType.Ident)
                                {
                                    clipRect.Height = (clipRect.Bottom - clipRect.Y) - clipShape.Bottom.GetFloatValue(CssPrimitiveType.Number);
                                }
                            }
                        }
                        else if (clip == null || (clip.PrimitiveType == CssPrimitiveType.Ident && clip.GetStringValue() == "auto"))
                        {
                            if (_svgElement is ISvgSvgElement)
                            {
                                ISvgSvgElement svgElement = (ISvgSvgElement)_svgElement;
                                SvgRect        viewPort   = svgElement.Viewport as SvgRect;
                                clipRect = WpfConvert.ToRect(viewPort);
                            }
                            else if (_svgElement is ISvgMarkerElement || _svgElement is ISvgSymbolElement ||
                                     _svgElement is ISvgPatternElement)
                            {
                                // TODO: what to do here?
                            }
                        }
                        if (clipRect != Rect.Empty)
                        {
                            _clipGeometry = new RectangleGeometry(clipRect);
                            //gr.SetClip(clipRect);
                        }
                    }
                }
            }
            #endregion

            #region Clip with clip-path

            SvgRenderingHint hint = _svgElement.RenderingHint;

            if (hint == SvgRenderingHint.Image)
            {
            }

            // see: http://www.w3.org/TR/SVG/masking.html#EstablishingANewClippingPath

            if (hint == SvgRenderingHint.Shape || hint == SvgRenderingHint.Text ||
                hint == SvgRenderingHint.Clipping || hint == SvgRenderingHint.Masking ||
                hint == SvgRenderingHint.Containment || hint == SvgRenderingHint.Image)
            {
                CssPrimitiveValue clipPath = _svgElement.GetComputedCssValue("clip-path", string.Empty) as CssPrimitiveValue;

                if (clipPath != null && clipPath.PrimitiveType == CssPrimitiveType.Uri)
                {
                    string absoluteUri = _svgElement.ResolveUri(clipPath.GetStringValue());

                    SvgClipPathElement eClipPath = _svgElement.OwnerDocument.GetNodeByUri(absoluteUri) as SvgClipPathElement;

                    if (eClipPath != null)
                    {
                        GeometryCollection geomColl = CreateClippingRegion(eClipPath, context);
                        if (geomColl == null || geomColl.Count == 0)
                        {
                            return;
                        }
                        Geometry gpClip    = geomColl[0];
                        int      geomCount = geomColl.Count;
                        if (geomCount > 1)
                        {
                            //GeometryGroup clipGroup = new GeometryGroup();
                            //clipGroup.Children.Add(gpClip);
                            for (int k = 1; k < geomCount; k++)
                            {
                                gpClip = Geometry.Combine(gpClip, geomColl[k],
                                                          GeometryCombineMode.Union, null);
                                //clipGroup.Children.Add(geomColl[k]);
                            }

                            //clipGroup.Children.Reverse();

                            //gpClip = clipGroup;
                        }

                        if (gpClip == null || gpClip.IsEmpty())
                        {
                            return;
                        }

                        _clipPathUnits = (SvgUnitType)eClipPath.ClipPathUnits.AnimVal;

                        //if (_clipPathUnits == SvgUnitType.ObjectBoundingBox)
                        //{
                        //    SvgTransformableElement transElement = _svgElement as SvgTransformableElement;

                        //    if (transElement != null)
                        //    {
                        //        ISvgRect bbox = transElement.GetBBox();

                        //        // scale clipping path
                        //        gpClip.Transform = new ScaleTransform(bbox.Width, bbox.Height);
                        //        //gr.SetClip(gpClip);

                        //        // offset clip
                        //        //TODO--PAUL gr.TranslateClip((float)bbox.X, (float)bbox.Y);

                        //        _clipGeometry = gpClip;
                        //    }
                        //    else
                        //    {
                        //        throw new NotImplementedException("clip-path with SvgUnitType.ObjectBoundingBox "
                        //          + "not supported for this type of element: " + _svgElement.GetType());
                        //    }
                        //}
                        //else
                        {
                            //gr.SetClip(gpClip);

                            _clipGeometry = gpClip;
                        }
                    }
                }
            }

            #endregion
        }
예제 #12
0
        public override Brush GetBrush(Rect elementBounds, WpfDrawingContext context, Transform viewTransform)
        {
            Rect bounds = elementBounds;

            DrawingGroup image = this.GetImage(context, bounds);

            if (image == null || image.Bounds.Width.Equals(0) || image.Bounds.Height.Equals(0))
            {
                return(null);
            }

            bool isUserSpace = true;

            if (_renderedElement.PatternContentUnits.AnimVal.Equals((ushort)SvgUnitType.ObjectBoundingBox))
            {
                bounds      = new Rect(0, 0, 1, 1);
                isUserSpace = false;
            }
            Rect destRect = GetDestRect(bounds);

            // Check for validity of the brush...
            if (destRect.Width.Equals(0) || destRect.Height.Equals(0) || destRect.IsEmpty)
            {
                return(null);
            }

            // Apply a scale if needed
            if (isUserSpace && image.Transform != null)
            {
                ISvgFitToViewBox fitToView = _renderedElement as ISvgFitToViewBox;
                if (fitToView != null && fitToView.ViewBox != null)
                {
                    ISvgAnimatedRect animRect = fitToView.ViewBox;
                    ISvgRect         viewRect = animRect.AnimVal;
                    if (viewRect != null)
                    {
                        Rect wpfViewRect = WpfConvert.ToRect(viewRect);
                        if (!wpfViewRect.IsEmpty && wpfViewRect.Width > 0 && wpfViewRect.Height > 0)
                        {
                            var scaleX = elementBounds.Width > 0 ? destRect.Width / wpfViewRect.Width : 1;
                            var scaleY = elementBounds.Height > 0 ? destRect.Height / wpfViewRect.Height : 1;

                            if (!scaleX.Equals(1) || !scaleY.Equals(1))
                            {
                                var currentTransform = image.Transform as ScaleTransform;
                                if (currentTransform != null)
                                {
                                    image.Transform = new ScaleTransform(scaleX, scaleY);
                                }
                            }
                        }
                    }
                }
            }

            DrawingBrush tb = new DrawingBrush(image);

            tb.Viewbox  = destRect;
            tb.Viewport = destRect;
            //tb.Viewbox = new Rect(0, 0, destRect.Width, destRect.Height);
            //tb.Viewport = new Rect(0, 0, bounds.Width, bounds.Height);
            tb.ViewboxUnits  = BrushMappingMode.Absolute;
            tb.ViewportUnits = isUserSpace ? BrushMappingMode.Absolute : BrushMappingMode.RelativeToBoundingBox;
            tb.TileMode      = TileMode.Tile;
//            tb.Stretch       = isUserSpace ? Stretch.Fill : Stretch.Uniform;

            if (isUserSpace)
            {
                MatrixTransform transform = GetTransformMatrix(image.Bounds, isUserSpace);
                if (transform != null && !transform.Matrix.IsIdentity)
                {
                    tb.Transform = transform;
                }
            }
            else
            {
                MatrixTransform transform = GetTransformMatrix(bounds, isUserSpace);
                if (transform != null && !transform.Matrix.IsIdentity)
                {
                    tb.RelativeTransform = transform;
                }
            }

            return(tb);
        }
예제 #13
0
        private Brush GetBrush(Geometry geometry, string propPrefix, bool setOpacity)
        {
            WpfSvgPaintContext paintContext = null;

            SvgPaintType paintType = this.PaintType;

            WpfSvgPaint fill;

            if (paintType == SvgPaintType.None)
            {
                return(null);
            }
            if (paintType == SvgPaintType.CurrentColor)
            {
                //TODO: Find a better way to support currentColor specified on parent element.
                var deferredFill = this.GetDeferredFill();
                if (deferredFill == null)
                {
                    fill = new WpfSvgPaint(_context, _element, CssConstants.PropColor);
                }
                else
                {
                    fill = deferredFill;
                }
            }
            else if (paintType == SvgPaintType.ContextFill)
            {
                paintContext = GetFillContext();
                if (paintContext != null)
                {
                    fill = paintContext.Fill;
                }
                else
                {
                    fill = this;
                }
            }
            else if (paintType == SvgPaintType.ContextStroke)
            {
                paintContext = GetStrokeContext();
                if (paintContext != null)
                {
                    fill = paintContext.Stroke;
                }
                else
                {
                    fill = this;
                }
            }
            else
            {
                fill = this;
            }

            SvgPaintType fillType = fill.PaintType;

            if (fillType == SvgPaintType.Uri || fillType == SvgPaintType.UriCurrentColor ||
                fillType == SvgPaintType.UriNone || fillType == SvgPaintType.UriRgbColor ||
                fillType == SvgPaintType.UriRgbColorIccColor)
            {
                _paintFill = GetPaintFill(fill.Uri);
                if (_paintFill != null)
                {
                    Brush brush = null;
                    if (geometry != null)
                    {
                        brush = _paintFill.GetBrush(geometry.Bounds, _context, geometry.Transform);
                    }
                    else
                    {
                        brush = _paintFill.GetBrush(Rect.Empty, _context, null);
                    }

                    if (brush != null)
                    {
                        brush.Opacity = GetOpacity(propPrefix);
                    }

                    return(brush);
                }

                if (paintType == SvgPaintType.UriNone || paintType == SvgPaintType.Uri)
                {
                    return(null);
                }
                if (paintType == SvgPaintType.UriCurrentColor)
                {
                    fill = new WpfSvgPaint(_context, _element, CssConstants.PropColor);
                }
                else
                {
                    fill = this;
                }
            }

            if (fill == null || fill.RgbColor == null)
            {
                return(null);
            }

            if (fill.RgbColor.IsVarColor)
            {
                var cssVar = this.GetVarsValue(fill);
                if (cssVar != null)
                {
                    var cssVariables = _context.Settings.CssVariables;
                    if (cssVariables != null && cssVariables.ContainsKey(cssVar.VarName))
                    {
                        var   cssColor = new CssColor(cssVariables[cssVar.VarName]);
                        Color?varColor = WpfConvert.ToColor(cssColor);
                        if (varColor != null)
                        {
                            var varBrush = new SolidColorBrush(varColor.Value);
                            if (setOpacity)
                            {
                                varBrush.Opacity = GetOpacity(propPrefix);
                            }
                            return(varBrush);
                        }
                    }

                    var cssValue = _element.GetComputedCssValue(cssVar.VarName, string.Empty) as CssAbsPrimitiveValue;
                    if (cssValue != null)
                    {
                        Color?varColor = WpfConvert.ToColor(cssValue.GetRgbColorValue());
                        if (varColor != null)
                        {
                            var varBrush = new SolidColorBrush(varColor.Value);
                            if (setOpacity)
                            {
                                varBrush.Opacity = GetOpacity(propPrefix);
                            }
                            return(varBrush);
                        }
                    }

                    var fallbackValue = cssVar.VarValue;
                    if (!string.IsNullOrWhiteSpace(fallbackValue))
                    {
                        var   cssColor = new CssColor(fallbackValue);
                        Color?varColor = WpfConvert.ToColor(cssColor);
                        if (varColor != null)
                        {
                            var varBrush = new SolidColorBrush(varColor.Value);
                            if (setOpacity)
                            {
                                varBrush.Opacity = GetOpacity(propPrefix);
                            }
                            return(varBrush);
                        }
                    }
                }
            }

            Color?solidColor = WpfConvert.ToColor(fill.RgbColor);

            if (solidColor == null)
            {
                return(null);
            }

            SolidColorBrush solidBrush = new SolidColorBrush(solidColor.Value);

            if (setOpacity)
            {
                solidBrush.Opacity = GetOpacity(propPrefix);
            }
            return(solidBrush);
        }
예제 #14
0
        public override void AfterRender(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.");
            }
            ISvgAnimatedEnumeration markerUnits = _markerElement.MarkerUnits;

            if (markerUnits.AnimVal.Equals((ushort)SvgMarkerUnit.StrokeWidth))
            {
                var    comparer     = StringComparison.OrdinalIgnoreCase;
                string overflowAttr = _markerElement.GetAttribute("overflow");
                if (string.IsNullOrWhiteSpace(overflowAttr) ||
                    overflowAttr.Equals("scroll", comparer) || overflowAttr.Equals("hidden", comparer))
                {
                    Geometry markerClip = this.ClipGeometry;
                    if (markerClip == null || markerClip.IsEmpty())
                    {
                        SvgRect clipRect = (SvgRect)_markerElement.ViewBox.AnimVal;
                        if (clipRect != null && !clipRect.IsEmpty)
                        {
                            _drawGroup.ClipGeometry = new RectangleGeometry(
                                new Rect(clipRect.X, clipRect.Y, clipRect.Width, clipRect.Height));
                        }
                        else if (_markerElement.IsSizeDefined)
                        {
                            _drawGroup.ClipGeometry = new RectangleGeometry(new Rect(0, 0,
                                                                                     _markerElement.MarkerWidth.AnimVal.Value, _markerElement.MarkerHeight.AnimVal.Value));
                        }
                        else if (_hostElement != null)
                        {
                            // Special cases for zero-length 'path' and 'line' segments.
                            var isLineSegment = false;
                            if (_hostGeometry != null)
                            {
                                var bounds = _hostGeometry.Bounds;
                                if (string.Equals(_hostElement.LocalName, "line", StringComparison.Ordinal))
                                {
                                    isLineSegment = true;
                                }
                                else if (string.Equals(_hostElement.LocalName, "rect", StringComparison.Ordinal))
                                {
                                    isLineSegment = bounds.Width.Equals(0) || bounds.Height.Equals(0);
                                }
                                else if (string.Equals(_hostElement.LocalName, "path", StringComparison.Ordinal))
                                {
                                    isLineSegment = bounds.Width.Equals(0) || bounds.Height.Equals(0);
                                }
                            }
                            else
                            {
                                if (string.Equals(_hostElement.LocalName, "line", StringComparison.Ordinal))
                                {
                                    isLineSegment = true;
                                }
                            }
                            if (isLineSegment)
                            {
                                bool isZeroWidthLine = false;
                                if (_pathFigures != null)
                                {
                                    if (_pathFigures.Count == 0)
                                    {
                                        isZeroWidthLine = true;
                                    }
                                    else
                                    {
                                        var pathWidth = 0.0d;
                                        foreach (PathFigure pathFigure in _pathFigures)
                                        {
                                            pathWidth += WpfConvert.GetPathFigureLength(pathFigure);
                                        }
                                        isZeroWidthLine = pathWidth.Equals(0.0d);
                                    }
                                }

                                if (isZeroWidthLine)
                                {
                                    _drawGroup.ClipGeometry = new RectangleGeometry(new Rect(0, 0,
                                                                                             _markerElement.MarkerWidth.AnimVal.Value, _markerElement.MarkerHeight.AnimVal.Value));
                                }
                            }
                        }
                    }
                }
            }

            context.Pop();

            base.AfterRender(renderer);
        }
예제 #15
0
        private Brush GetBrush(string propPrefix, bool setOpacity)
        {
            SvgPaint fill;

            if (PaintType == SvgPaintType.None)
            {
                return(null);
            }
            else if (PaintType == SvgPaintType.CurrentColor)
            {
                fill = new WpfSvgPaint(_context, _element, "color");
            }
            else
            {
                fill = this;
            }

            SvgPaintType paintType = fill.PaintType;

            if (paintType == SvgPaintType.Uri || paintType == SvgPaintType.UriCurrentColor ||
                paintType == SvgPaintType.UriNone || paintType == SvgPaintType.UriRgbColor ||
                paintType == SvgPaintType.UriRgbColorIccColor)
            {
                _paintFill = GetPaintFill(fill.Uri);
                if (_paintFill != null)
                {
                    Brush brush = _paintFill.GetBrush(_context);

                    if (brush != null)
                    {
                        brush.Opacity = GetOpacity(propPrefix);
                    }

                    return(brush);
                }
                else
                {
                    if (PaintType == SvgPaintType.UriNone || PaintType == SvgPaintType.Uri)
                    {
                        return(null);
                    }
                    else if (PaintType == SvgPaintType.UriCurrentColor)
                    {
                        fill = new WpfSvgPaint(_context, _element, "color");
                    }
                    else
                    {
                        fill = this;
                    }
                }
            }

            if (fill == null || fill.RgbColor == null)
            {
                return(null);
            }

            Color?solidColor = WpfConvert.ToColor(fill.RgbColor);

            if (solidColor == null)
            {
                return(null);
            }

            SolidColorBrush solidBrush = new SolidColorBrush(solidColor.Value);

            //int opacity = GetOpacity(propPrefix);
            //solidBrush.Color = Color.FromArgb(opacity, brush.Color);
            if (setOpacity)
            {
                solidBrush.Opacity = GetOpacity(propPrefix);
            }
            return(solidBrush);
        }
예제 #16
0
        private LinearGradientBrush GetLinearGradientBrush(SvgLinearGradientElement res)
        {
            double x1 = res.X1.AnimVal.Value;
            double x2 = res.X2.AnimVal.Value;
            double y1 = res.Y1.AnimVal.Value;
            double y2 = res.Y2.AnimVal.Value;

            GradientStopCollection gradientStops = GetGradientStops(res.Stops);

            LinearGradientBrush brush = new LinearGradientBrush(gradientStops,
                                                                new Point(x1, y1), new Point(x2, y2));

            SvgSpreadMethod spreadMethod = SvgSpreadMethod.None;

            if (res.SpreadMethod != null)
            {
                spreadMethod = (SvgSpreadMethod)res.SpreadMethod.AnimVal;

                if (spreadMethod != SvgSpreadMethod.None)
                {
                    brush.SpreadMethod = WpfConvert.ToSpreadMethod(spreadMethod);
                }
            }
            if (res.GradientUnits != null)
            {
                SvgUnitType mappingMode = (SvgUnitType)res.GradientUnits.AnimVal;
                if (mappingMode == SvgUnitType.ObjectBoundingBox)
                {
                    brush.MappingMode = BrushMappingMode.RelativeToBoundingBox;
                }
                else if (mappingMode == SvgUnitType.UserSpaceOnUse)
                {
                    brush.MappingMode = BrushMappingMode.Absolute;
                }
            }

            MatrixTransform transform = GetTransformMatrix(res);

            if (transform != null && !transform.Matrix.IsIdentity)
            {
                brush.Transform = transform;
            }
            //else
            //{
            //    float fLeft = (float)res.X1.AnimVal.Value;
            //    float fRight = (float)res.X2.AnimVal.Value;
            //    float fTop = (float)res.Y1.AnimVal.Value;
            //    float fBottom = (float)res.Y2.AnimVal.Value;

            //    if (fTop == fBottom)
            //    {
            //        //mode = LinearGradientMode.Horizontal;
            //    }
            //    else
            //    {
            //        if (fLeft == fRight)
            //        {
            //            //mode = LinearGradientMode.Vertical;
            //        }
            //        else
            //        {
            //            if (fLeft < fRight)
            //            {
            //                //mode = LinearGradientMode.ForwardDiagonal;
            //                brush.Transform = new RotateTransform(45, 0, 0);
            //                //brush.EndPoint = new Point(x1, y1 + 1);
            //            }
            //            else
            //            {
            //                //mode = LinearGradientMode.BackwardDiagonal;
            //                brush.Transform = new RotateTransform(-45);
            //            }
            //        }
            //    }
            //}

            string colorInterpolation = res.GetPropertyValue("color-interpolation");

            if (!String.IsNullOrEmpty(colorInterpolation))
            {
                if (colorInterpolation == "linearRGB")
                {
                    brush.ColorInterpolationMode = ColorInterpolationMode.ScRgbLinearInterpolation;
                }
                else
                {
                    brush.ColorInterpolationMode = ColorInterpolationMode.SRgbLinearInterpolation;
                }
            }

            return(brush);
        }