Beispiel #1
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);
        }