Пример #1
0
 public void RegisterPaintContext(WpfSvgPaintContext paintContext)
 {
     if (_paintContexts != null)
     {
         _paintContexts[paintContext.Id] = paintContext;
     }
 }
Пример #2
0
        private WpfSvgPaintContext GetStrokeContext()
        {
            WpfSvgPaintContext paintContext = null;

            if (_element != null && _context != null)
            {
                SvgElement element = _element;
                paintContext = _context.GetPaintContext(element.UniqueId);
                while (element != null && (paintContext != null && paintContext.Stroke == null))
                {
                    element      = element.ParentNode as SvgElement;
                    paintContext = null;
                    if (element != null)
                    {
                        paintContext = _context.GetPaintContext(element.UniqueId);

                        if (paintContext != null && paintContext.HasTarget)
                        {
                            if (paintContext.Stroke == null)
                            {
                                paintContext = _context.GetPaintContext(paintContext.TargetId);
                            }
                        }
                    }
                }
            }
            return(paintContext);
        }
Пример #3
0
        public WpfSvgPaint GetScopeStroke()
        {
            WpfSvgPaintContext paintContext = this.GetStrokeContext();

            if (paintContext != null)
            {
                return(paintContext.Stroke);
            }
            return(null);
        }
Пример #4
0
 protected virtual void Initialize(SvgElement element)
 {
     _isReady    = true;
     _elementId  = null;
     _uniqueId   = null;
     _svgElement = element;
     if (element != null)
     {
         _uniqueId  = element.UniqueId;
         _elementId = this.GetElementName();
     }
     _paintContext = null;
 }
Пример #5
0
        // define empty handlers by default
        public virtual void BeforeRender(WpfDrawingRenderer renderer)
        {
            if (renderer == null)
            {
                return;
            }
            _context = renderer.Context;

            if (_svgElement != null && _context != null)
            {
                _paintContext = new WpfSvgPaintContext(_svgElement.UniqueId);
                _context.RegisterPaintContext(_paintContext);
            }
        }
Пример #6
0
        // define empty handlers by default
        public virtual void BeforeRender(WpfDrawingRenderer renderer)
        {
            if (renderer == null)
            {
                return;
            }
            _isReady = false;
            _context = renderer.Context;

            if (_svgElement != null && _context != null)
            {
                if (string.IsNullOrWhiteSpace(_uniqueId))
                {
                    _uniqueId = _svgElement.UniqueId;
                }
                _paintContext = new WpfSvgPaintContext(_uniqueId);
                _context.RegisterPaintContext(_paintContext);
            }
        }
Пример #7
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, "color");
                }
                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, "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);

            if (setOpacity)
            {
                solidBrush.Opacity = GetOpacity(propPrefix);
            }
            return(solidBrush);
        }
Пример #8
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, "color");
            }
            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, "stroke", setOpacity);

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

            pen.StartLineCap = pen.EndLineCap = GetLineCap();
            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;
                //Do not draw if dash array had a zero value in it
                for (int i = 0; i < dashArray.Count; i++)
                {
                    if (dashArray[i].Equals(0.0d))
                    {
                        isValidDashes = false;
                    }
                }

                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;
                }
            }
            return(pen);
        }
Пример #9
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);
        }
Пример #10
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);
        }