Exemplo n.º 1
0
        void ApplyStyle(Dictionary <string, string> style, ref Pen pen, out bool hasPen, ref Brush brush, out bool hasBrush)
        {
            //
            // Pen attributes
            //
            var strokeWidth = GetString(style, "stroke-width");

            if (!string.IsNullOrWhiteSpace(strokeWidth))
            {
                if (pen == null)
                {
                    pen = new Pen();
                }
                pen.Width = ReadNumber(strokeWidth);
            }

            var strokeOpacity = GetString(style, "stroke-opacity");

            if (!string.IsNullOrWhiteSpace(strokeOpacity))
            {
                if (pen == null)
                {
                    pen = new Pen();
                }
                pen.Color = pen.Color.WithAlpha(ReadNumber(strokeOpacity));
            }

            //
            // Pen
            //
            var stroke = GetString(style, "stroke").Trim();

            if (string.IsNullOrEmpty(stroke))
            {
                // No change
                hasPen = false;
            }
            else if (stroke.Equals("none", StringComparison.OrdinalIgnoreCase))
            {
                hasPen = true;
                pen    = null;
            }
            else
            {
                hasPen = true;
                if (pen == null)
                {
                    pen = new Pen();
                }
                Color color;
                if (Colors.TryParse(stroke, out color))
                {
                    if (pen.Color.Alpha == 1)
                    {
                        pen.Color = color;
                    }
                    else
                    {
                        pen.Color = color.WithAlpha(pen.Color.Alpha);
                    }
                }
            }

            //
            // Brush attributes
            //
            var fillOpacity = GetString(style, "fill-opacity");

            if (!string.IsNullOrWhiteSpace(fillOpacity))
            {
                if (brush == null)
                {
                    brush = new SolidBrush();
                }
                var sb = brush as SolidBrush;
                if (sb != null)
                {
                    sb.Color = sb.Color.WithAlpha(ReadNumber(fillOpacity));
                }
            }

            //
            // Brush
            //
            var fill = GetString(style, "fill").Trim();

            if (string.IsNullOrEmpty(fill))
            {
                // No change
                hasBrush = false;
            }
            else if (fill.Equals("none", StringComparison.OrdinalIgnoreCase))
            {
                hasBrush = true;
                brush    = null;
            }
            else
            {
                hasBrush = true;
                Color color;
                if (Colors.TryParse(fill, out color))
                {
                    var sb = brush as SolidBrush;
                    if (sb == null)
                    {
                        brush = new SolidBrush(color);
                    }
                    else
                    {
                        if (sb.Color.Alpha == 1)
                        {
                            sb.Color = color;
                        }
                        else
                        {
                            sb.Color = color.WithAlpha(sb.Color.Alpha);
                        }
                    }
                }
                else
                {
                    var urlM = fillUrlRe.Match(fill);
                    if (urlM.Success)
                    {
                        var id = urlM.Groups [1].Value.Trim();
                        brush = GetGradientBrush(id, null);
                    }
                    else
                    {
                        throw new NotSupportedException("Fill " + fill);
                    }
                }
            }
        }