Beispiel #1
0
        private WpfSvgPaint GetDeferredFill()
        {
            var nodeList = _element.OwnerDocument.SelectNodes("//*[@fill='currentColor']");

            if (nodeList == null || nodeList.Count == 0)
            {
                return(null);
            }
            // Try a shortcut...
            SvgStyleableElement svgElement = _element.ParentNode as SvgStyleableElement;

            if (svgElement != null && svgElement.HasAttribute("fill") &&
                string.Equals("currentColor", svgElement.GetAttribute("fill")))
            {
                string color = svgElement.GetAttribute("color");
                if (!string.IsNullOrWhiteSpace(color))
                {
                    return(new WpfSvgPaint(_context, svgElement, "color"));
                }
            }

            foreach (XmlNode nodeItem in nodeList)
            {
                svgElement = nodeItem as SvgStyleableElement;
                if (svgElement == null)
                {
                    continue;
                }

                var childNodeList = svgElement.GetElementsByTagName(_element.LocalName);
                if (childNodeList != null && childNodeList.Count != 0)
                {
                    foreach (var childNode in childNodeList)
                    {
                        if (childNode == _element)
                        {
                            string color = svgElement.GetAttribute("color");
                            if (!string.IsNullOrWhiteSpace(color))
                            {
                                return(new WpfSvgPaint(_context, svgElement, "color"));
                            }
                        }
                    }
                }
            }
            return(null);
        }
Beispiel #2
0
        public override void BeforeRender(WpfDrawingRenderer renderer)
        {
            base.BeforeRender(renderer);

            if (renderer == null)
            {
                return;
            }

            _isLineSegment   = false;
            _setBrushOpacity = true;

            WpfDrawingContext context = renderer.Context;

            //SetQuality(context);
            //SetTransform(context);
            //SetMask(context);

//            _drawGroup = new DrawingGroup();

            SvgStyleableElement styleElm = (SvgStyleableElement)_svgElement;

            float opacityValue   = -1;
            bool  isStyleOpacity = false;

            string opacity = styleElm.GetAttribute("opacity");

            if (string.IsNullOrWhiteSpace(opacity))
            {
                opacity = styleElm.GetPropertyValue("opacity");
                if (!string.IsNullOrWhiteSpace(opacity))
                {
                    isStyleOpacity = true;
                }
            }
            if (!string.IsNullOrWhiteSpace(opacity))
            {
                opacityValue = (float)SvgNumber.ParseNumber(opacity);
                opacityValue = Math.Min(opacityValue, 1);
                opacityValue = Math.Max(opacityValue, 0);
                if (isStyleOpacity && (opacityValue >= 0 && opacityValue < 1))
                {
                    _setBrushOpacity = false;
                    if (styleElm.HasAttribute("fill-opacity") || (styleElm.HasAttribute("style") &&
                                                                  styleElm.GetAttribute("style").Contains("fill-opacity")))
                    {
                        _setBrushOpacity = true;
                    }
                }
            }
            //string eVisibility = _svgElement.GetAttribute("visibility");
            //string eDisplay    = _svgElement.GetAttribute("display");
            //if (string.Equals(eVisibility, "hidden") || string.Equals(eDisplay, "none"))
            //{
            //    opacityValue = 0;
            //}
            //else
            //{
            //    string sVisibility = styleElm.GetPropertyValue("visibility");
            //    string sDisplay = styleElm.GetPropertyValue("display");
            //    if (string.Equals(sVisibility, "hidden") || string.Equals(sDisplay, "none"))
            //    {
            //        opacityValue = 0;
            //    }
            //}
            string sVisibility = styleElm.GetPropertyValue("visibility");

            if (string.IsNullOrWhiteSpace(sVisibility))
            {
                sVisibility = _svgElement.GetAttribute("visibility");
            }
            string sDisplay = styleElm.GetPropertyValue("display");

            if (string.IsNullOrWhiteSpace(sDisplay))
            {
                sDisplay = _svgElement.GetAttribute("display");
            }
            if (string.Equals(sVisibility, "hidden") || string.Equals(sDisplay, "none"))
            {
                opacityValue = 0;
            }

            Transform pathTransform = this.Transform;

            if (pathTransform != null && !pathTransform.Value.IsIdentity)
            {
                if (_drawGroup == null)
                {
                    _drawGroup = new DrawingGroup();
                }
                _drawGroup.Transform = pathTransform;
            }
            else
            {
                pathTransform = null; // render any identity transform useless...
            }

            Geometry pathClip = this.ClipGeometry;

            if (pathClip != null && !pathClip.IsEmpty())
            {
                if (_drawGroup == null)
                {
                    _drawGroup = new DrawingGroup();
                }
                _drawGroup.ClipGeometry = pathClip;
            }
            else
            {
                pathClip = null; // render any empty geometry useless...
            }
            Brush pathMask = this.Masking;

            if (pathMask != null)
            {
                if (_drawGroup == null)
                {
                    _drawGroup = new DrawingGroup();
                }
                _drawGroup.OpacityMask = pathMask;
            }

            if (pathTransform != null || pathClip != null || pathMask != null || (opacityValue >= 0 && opacityValue < 1))
            {
                if (_drawGroup == null)
                {
                    _drawGroup = new DrawingGroup();
                }
                if ((opacityValue >= 0 && opacityValue < 1))
                {
                    _drawGroup.Opacity = opacityValue;
                }

                DrawingGroup curGroup = _context.Peek();
                Debug.Assert(curGroup != null);
                if (curGroup != null)
                {
                    curGroup.Children.Add(_drawGroup);
                    context.Push(_drawGroup);
                }
            }
            else
            {
                _drawGroup = null;
            }

            if (_drawGroup != null)
            {
                string elementClass = this.GetElementClass();
                if (!string.IsNullOrWhiteSpace(elementClass) && context.IncludeRuntime)
                {
                    SvgObject.SetClass(_drawGroup, elementClass);
                }

                string elementId = this.GetElementName();
                if (!string.IsNullOrWhiteSpace(elementId) && !context.IsRegisteredId(elementId))
                {
                    SvgObject.SetName(_drawGroup, elementId);

                    context.RegisterId(elementId);

                    if (context.IncludeRuntime)
                    {
                        SvgObject.SetId(_drawGroup, elementId);
                    }
                }
            }
        }
Beispiel #3
0
        public Pen GetPen(Geometry geometry, bool setOpacity = true)
        {
            double strokeWidth = GetStrokeWidth();

            if (strokeWidth.Equals(0.0d))
            {
                return(null);
            }

            WpfSvgPaintContext paintContext = null;

            SvgPaintType paintType = this.PaintType;

            WpfSvgPaint stroke;

            if (paintType == SvgPaintType.None)
            {
                return(null);
            }
            if (paintType == SvgPaintType.CurrentColor)
            {
                stroke = new WpfSvgPaint(_context, _element, CssConstants.PropColor);
            }
            else if (paintType == SvgPaintType.ContextFill)
            {
                paintContext = GetFillContext();
                if (paintContext != null)
                {
                    stroke = paintContext.Fill;
                }
                else
                {
                    stroke = this;
                }
            }
            else if (paintType == SvgPaintType.ContextStroke)
            {
                paintContext = GetStrokeContext();
                if (paintContext != null)
                {
                    stroke = paintContext.Stroke;
                }
                else
                {
                    stroke = this;
                }
            }
            else
            {
                stroke = this;
            }

            Brush brush = stroke.GetBrush(geometry, CssConstants.PropStroke, setOpacity);

            if (brush == null)
            {
                WpfSvgPaint fallbackPaint = stroke.WpfFallback;
                if (fallbackPaint != null)
                {
                    brush = fallbackPaint.GetBrush(geometry, CssConstants.PropStroke, setOpacity);
                }
            }
            Pen pen = new Pen(brush, strokeWidth);

            if (_element.HasAttribute(CssConstants.PropStrokeOpacity))
            {
                double opacityValue = -1;

                string opacity = _element.GetAttribute(CssConstants.PropStrokeOpacity);
                if (!string.IsNullOrWhiteSpace(opacity))
                {
                    opacityValue = SvgNumber.ParseNumber(opacity);
                    opacityValue = Math.Min(opacityValue, 1);
                    opacityValue = Math.Max(opacityValue, 0);
                    if (opacityValue >= 0 && opacityValue < 1)
                    {
                        brush.Opacity = opacityValue;
                    }
                }
            }

            var lineCap = this.GetLineCap();

            pen.StartLineCap = pen.EndLineCap = lineCap;
            pen.LineJoin     = GetLineJoin();
            double miterLimit = GetMiterLimit(strokeWidth);

            if (miterLimit > 0)
            {
                pen.MiterLimit = miterLimit;
            }

            //pen.MiterLimit = 1.0f;

            DoubleCollection dashArray = GetDashArray(strokeWidth);

            if (dashArray != null && dashArray.Count != 0)
            {
                bool isValidDashes = true;
                int  nonZeroCount  = 0;

                // Specs: If all of the values in the list are zero, then the stroke is
                // rendered as a solid line without any dashing.
                // If any value in the list is negative, the dash-array value is invalid.
                for (int i = 0; i < dashArray.Count; i++)
                {
                    if (dashArray[i] < 0.0d)
                    {
                        isValidDashes = false;
                        break;
                    }
                    if (dashArray[i].Equals(0.0d) == false)
                    {
                        nonZeroCount++;
                    }
                }
                isValidDashes = isValidDashes && nonZeroCount != 0;

                if (isValidDashes)
                {
                    DashStyle dashStyle = new DashStyle(dashArray, GetDashOffset(strokeWidth));

                    pen.DashStyle = dashStyle;
                    // This is the one that works well for the XAML, the default is not Flat as
                    // stated in the documentations...
                    //pen.DashCap = PenLineCap.Flat;
                    pen.DashCap = lineCap;
                }
            }
            return(pen);
        }