Exemplo n.º 1
0
        private void RenderGeometory(CanvasDrawingSession session, CanvasGeometry geometry, SvgMatrix transform, CssStyleDeclaration style)
        {
            bool change    = false;
            var  geometry2 = geometry;
            CanvasSolidColorBrush opacityBrush = null;

            try
            {
                using (var t = TransformSession.CreateTransformSession(session, transform))
                {
                    var clipPath = style.ClipPath;
                    if (clipPath != null)
                    {
                        if (clipPath.Uri[0] != '#')
                        {
                            throw new ArgumentException();
                        }
                        var clipPathElement = (SvgClipPathElement)this.TargetDocument.GetElementById(clipPath.Uri.Substring(1));
                        var clipGeometory   = this.CreateClipPath(session, clipPathElement);
                        geometry2 = geometry.CombineWith(
                            clipGeometory,
                            new Matrix3x2 {
                            M11 = 1.0F, M12 = 0.0F, M21 = 0.0F, M22 = 1.0F, M31 = 0.0F, M32 = 0.0F
                        },
                            CanvasGeometryCombine.Intersect,
                            CanvasGeometry.ComputeFlatteningTolerance(session.Dpi, 1.0F, session.Transform));
                        change = true;
                    }

                    var area = geometry2.ComputeBounds();

                    var opacity = style.Opacity;
                    if (opacity != null)
                    {
                        opacityBrush = this.CreateOpacity(session, opacity.Value);
                    }

                    var fill = style.Fill;
                    if (fill == null || fill != null && fill.PaintType != SvgPaintType.None)
                    {
                        var pen = this.CreatePaint(session, area, fill, style.FillOpacity, style);
                        if (opacityBrush == null)
                        {
                            session.FillGeometry(geometry2, pen);
                        }
                        else
                        {
                            session.FillGeometry(geometry2, pen, opacityBrush);
                        }
                    }

                    var stroke = style.Stroke;
                    if (stroke != null && stroke.PaintType != SvgPaintType.None)
                    {
                        var pen         = this.CreatePaint(session, area, stroke, style.StrokeOpacity, style);
                        var strokeWidth = this.LengthConverter.Convert(style.StrokeWidth, 1.0F);
                        using (var strokeStyle = this.CreateStrokeStyle(style))
                        {
                            session.DrawGeometry(geometry2, pen, strokeWidth, strokeStyle);
                        }
                    }
                }
            }
            finally
            {
                if (change)
                {
                    geometry2.Dispose();
                }
                if (opacityBrush != null)
                {
                    opacityBrush.Dispose();
                }
            }
        }
Exemplo n.º 2
0
        private ICanvasBrush CreatePaint(CanvasDrawingSession session, Rect area, SvgPaint paint, SvgNumber?opacity, CssStyleDeclaration style)
        {
            if (paint == null || paint.PaintType == SvgPaintType.RgbColor)
            {
                var alpha = (byte)(255.0F * (opacity?.Value ?? 1.0F));
                var code  = paint == null ? alpha << 24 : alpha << 24 | paint.RgbColor.Red << 16 | paint.RgbColor.Green << 8 | paint.RgbColor.Blue;
                if (this.SolidResourceCache.ContainsKey(code))
                {
                    return(this.SolidResourceCache[code]);
                }

                var brush = new CanvasSolidColorBrush(this.ResourceCreator, paint?.ToPlatformColor(alpha) ?? Color.FromArgb(alpha, 0, 0, 0));
                this.DisposableObjects.Add(brush);
                this.SolidResourceCache.Add(code, brush);
                return(brush);
            }
            if (paint.PaintType == SvgPaintType.CurrentColor)
            {
                var color = style.Color;
                var alpha = (byte)(255.0F * (opacity?.Value ?? 1.0F));
                var code  = alpha << 24 | color.RgbColor.Red << 16 | color.RgbColor.Green << 8 | color.RgbColor.Blue;
                if (this.SolidResourceCache.ContainsKey(code))
                {
                    return(this.SolidResourceCache[code]);
                }

                var brush = new CanvasSolidColorBrush(this.ResourceCreator, color.ToPlatformColor(alpha));
                this.DisposableObjects.Add(brush);
                this.SolidResourceCache.Add(code, brush);
                return(brush);
            }
            if (paint.PaintType == SvgPaintType.Uri && paint.Uri[0] == '#')
            {
                var key  = paint.Uri.Substring(1);
                var grad = this.TargetDocument.GetElementById(key);
                if (grad.GetType() == typeof(SvgLinearGradientElement))
                {
                    return(this.CreateLinearGradient(session, area, (SvgLinearGradientElement)grad));
                }
                if (grad.GetType() == typeof(SvgRadialGradientElement))
                {
                    return(this.CreateRadialGradient(session, area, (SvgRadialGradientElement)grad));
                }
            }
            throw new NotImplementedException();
        }