Exemple #1
0
        unsafe NativeBrushContainer CreateBrush(Brush brush, Size targetSize)
        {
            var rv = NativeBrushPool.Instance.Get();

            rv.Brush->Opacity = brush.Opacity;


            var solid = brush as SolidColorBrush;

            if (solid != null)
            {
                rv.Brush->Type  = NativeBrushType.Solid;
                rv.Brush->Color = solid.Color.ToUint32();
                return(rv);
            }
            var gradient = brush as GradientBrush;

            if (gradient != null)
            {
                if (gradient.GradientStops.Count > NativeBrush.MaxGradientStops)
                {
                    throw new NotSupportedException("Maximum supported gradient stop count is " +
                                                    NativeBrush.MaxGradientStops);
                }
                rv.Brush->GradientSpreadMethod = gradient.SpreadMethod;
                rv.Brush->GradientStopCount    = gradient.GradientStops.Count;

                for (var c = 0; c < gradient.GradientStops.Count; c++)
                {
                    var st = gradient.GradientStops[c];
                    rv.Brush->GradientStops[c]      = (float)st.Offset;
                    rv.Brush->GradientStopColors[c] = st.Color.ToUint32();
                }
            }

            var linearGradient = brush as LinearGradientBrush;

            if (linearGradient != null)
            {
                rv.Brush->Type = NativeBrushType.LinearGradient;
                rv.Brush->GradientStartPoint = linearGradient.StartPoint.ToPixels(targetSize);
                rv.Brush->GradientEndPoint   = linearGradient.EndPoint.ToPixels(targetSize);
            }
            var radialGradient = brush as RadialGradientBrush;

            if (radialGradient != null)
            {
                rv.Brush->Type = NativeBrushType.RadialGradient;
                rv.Brush->GradientStartPoint = radialGradient.Center.ToPixels(targetSize);
                rv.Brush->GradientRadius     = (float)radialGradient.Radius;
            }
            var tileBrush = brush as TileBrush;

            if (tileBrush != null)
            {
                rv.Brush->Type = NativeBrushType.Image;
                var helper = new TileBrushImplHelper(tileBrush, targetSize);
                var bitmap = new BitmapImpl((int)helper.IntermediateSize.Width, (int)helper.IntermediateSize.Height);
                rv.AddDisposable(bitmap);
                using (var ctx = bitmap.CreateDrawingContext())
                    helper.DrawIntermediate(ctx);
                rv.Brush->Bitmap            = bitmap.Handle;
                rv.Brush->BitmapTileMode    = tileBrush.TileMode;
                rv.Brush->BitmapTranslation = new SkiaPoint(-helper.DestinationRect.X, -helper.DestinationRect.Y);
            }

            return(rv);
        }
        unsafe NativeBrushContainer CreateBrush(Brush brush, Size targetSize)
        {
            var rv = NativeBrushPool.Instance.Get();
            rv.Brush->Opacity = brush.Opacity;

            var solid = brush as SolidColorBrush;
            if (solid != null)
            {
                rv.Brush->Type = NativeBrushType.Solid;
                rv.Brush->Color = solid.Color.ToUint32();
                return rv;
            }
            var gradient = brush as GradientBrush;
            if (gradient != null)
            {
                if (gradient.GradientStops.Count > NativeBrush.MaxGradientStops)
                    throw new NotSupportedException("Maximum supported gradient stop count is " +
                                                    NativeBrush.MaxGradientStops);
                rv.Brush->GradientSpreadMethod = gradient.SpreadMethod;
                rv.Brush->GradientStopCount = gradient.GradientStops.Count;

                for (var c = 0; c < gradient.GradientStops.Count; c++)
                {
                    var st = gradient.GradientStops[c];
                    rv.Brush->GradientStops[c] = (float) st.Offset;
                    rv.Brush->GradientStopColors[c] = st.Color.ToUint32();
                }

            }

            var linearGradient = brush as LinearGradientBrush;
            if (linearGradient != null)
            {
                rv.Brush->Type = NativeBrushType.LinearGradient;
                rv.Brush->GradientStartPoint = linearGradient.StartPoint.ToPixels(targetSize);
                rv.Brush->GradientEndPoint = linearGradient.EndPoint.ToPixels(targetSize);
            }
            var radialGradient = brush as RadialGradientBrush;
            if (radialGradient != null)
            {
                rv.Brush->Type = NativeBrushType.RadialGradient;
                rv.Brush->GradientStartPoint = radialGradient.Center.ToPixels(targetSize);
                rv.Brush->GradientRadius = (float)radialGradient.Radius;
            }
            var tileBrush = brush as TileBrush;
            if (tileBrush != null)
            {
                rv.Brush->Type = NativeBrushType.Image;
                var helper = new TileBrushImplHelper(tileBrush, targetSize);
                var bitmap = new BitmapImpl((int) helper.IntermediateSize.Width, (int) helper.IntermediateSize.Height);
                rv.AddDisposable(bitmap);
                using (var ctx = bitmap.CreateDrawingContext())
                    helper.DrawIntermediate(ctx);
                rv.Brush->Bitmap = bitmap.Handle;
                rv.Brush->BitmapTileMode = tileBrush.TileMode;
                rv.Brush->BitmapTranslation = new SkiaPoint(-helper.DestinationRect.X, -helper.DestinationRect.Y);
            }

            return rv;
        }
Exemple #3
0
        private PaintWrapper CreatePaint(IBrush brush, Size targetSize)
        {
            SKPaint paint = new SKPaint();
            var     rv    = new PaintWrapper(paint);

            paint.IsStroke = false;

            // TODO: SkiaSharp does not contain alpha yet!
            double opacity = brush.Opacity * _currentOpacity;

            //paint.SetAlpha(paint.GetAlpha() * opacity);
            paint.IsAntialias = true;

            SKColor color = new SKColor(255, 255, 255, 255);

            var solid = brush as SolidColorBrush;

            if (solid != null)
            {
                color = solid.Color.ToSKColor();
            }

            paint.Color = (new SKColor(color.Red, color.Green, color.Blue, (byte)(color.Alpha * opacity)));
            if (solid != null)
            {
                return(rv);
            }


            var gradient = brush as GradientBrush;

            if (gradient != null)
            {
                var tileMode    = gradient.SpreadMethod.ToSKShaderTileMode();
                var stopColors  = gradient.GradientStops.Select(s => s.Color.ToSKColor()).ToArray();
                var stopOffsets = gradient.GradientStops.Select(s => (float)s.Offset).ToArray();

                var linearGradient = brush as LinearGradientBrush;
                if (linearGradient != null)
                {
                    var start = linearGradient.StartPoint.ToPixels(targetSize).ToSKPoint();
                    var end   = linearGradient.EndPoint.ToPixels(targetSize).ToSKPoint();

                    // would be nice to cache these shaders possibly?
                    var shader = SKShader.CreateLinearGradient(start, end, stopColors, stopOffsets, tileMode);
                    paint.Shader = shader;
                    shader.Dispose();
                }
                else
                {
                    var radialGradient = brush as RadialGradientBrush;
                    if (radialGradient != null)
                    {
                        var center = radialGradient.Center.ToPixels(targetSize).ToSKPoint();
                        var radius = (float)radialGradient.Radius;

                        // TODO: There is no SetAlpha in SkiaSharp
                        //paint.setAlpha(128);

                        // would be nice to cache these shaders possibly?
                        var shader = SKShader.CreateRadialGradient(center, radius, stopColors, stopOffsets, tileMode);
                        paint.Shader = shader;
                        shader.Dispose();
                    }
                }

                return(rv);
            }

            var tileBrush = brush as TileBrush;

            if (tileBrush != null)
            {
                var helper = new TileBrushImplHelper(tileBrush, targetSize);
                var bitmap = new BitmapImpl((int)helper.IntermediateSize.Width, (int)helper.IntermediateSize.Height);
                rv.AddDisposable(bitmap);
                using (var ctx = bitmap.CreateDrawingContext())
                    helper.DrawIntermediate(ctx);
                SKMatrix         translation = SKMatrix.MakeTranslation(-(float)helper.DestinationRect.X, -(float)helper.DestinationRect.Y);
                SKShaderTileMode tileX       =
                    tileBrush.TileMode == TileMode.None
                        ? SKShaderTileMode.Clamp
                        : tileBrush.TileMode == TileMode.FlipX || tileBrush.TileMode == TileMode.FlipXY
                            ? SKShaderTileMode.Mirror
                            : SKShaderTileMode.Repeat;

                SKShaderTileMode tileY =
                    tileBrush.TileMode == TileMode.None
                        ? SKShaderTileMode.Clamp
                        : tileBrush.TileMode == TileMode.FlipY || tileBrush.TileMode == TileMode.FlipXY
                            ? SKShaderTileMode.Mirror
                            : SKShaderTileMode.Repeat;
                paint.Shader = SKShader.CreateBitmap(bitmap.Bitmap, tileX, tileY, translation);
                paint.Shader.Dispose();
            }

            return(rv);
        }