Example #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="TileBrushImpl"/> class.
        /// </summary>
        /// <param name="brush"></param>
        /// <param name="target"></param>
        /// <param name="targetSize"></param>
        public TileBrushImpl(
            TileBrush brush,
            SharpDX.Direct2D1.RenderTarget target,
            Size targetSize)
        {
            var helper = new TileBrushImplHelper(brush, targetSize);

            if (!helper.IsValid)
            {
                return;
            }

            using (var intermediate = new BitmapRenderTarget(target, CompatibleRenderTargetOptions.None, helper.IntermediateSize.ToSharpDX()))
            {
                using (var ctx = new RenderTarget(intermediate).CreateDrawingContext())
                {
                    intermediate.Clear(null);
                    helper.DrawIntermediate(ctx);
                }

                PlatformBrush = new BitmapBrush(
                    target,
                    intermediate.Bitmap,
                    GetBitmapBrushProperties(brush),
                    GetBrushProperties(brush, helper.DestinationRect));
            }
        }
Example #2
0
        public static SurfacePattern CreateTileBrush(TileBrush brush, Size targetSize)
        {
            var helper = new TileBrushImplHelper(brush, targetSize);
            if (!helper.IsValid)
                return null;
            
			using (var intermediate = new ImageSurface(Format.ARGB32, (int)helper.IntermediateSize.Width, (int)helper.IntermediateSize.Height))
            using (var ctx = new RenderTarget(intermediate).CreateDrawingContext())
            {
                helper.DrawIntermediate(ctx);

                var result = new SurfacePattern(intermediate);

                if ((brush.TileMode & TileMode.FlipXY) != 0)
                {
                    // TODO: Currently always FlipXY as that's all cairo supports natively. 
                    // Support separate FlipX and FlipY by drawing flipped images to intermediate
                    // surface.
                    result.Extend = Extend.Reflect;
                }
                else
                {
                    result.Extend = Extend.Repeat;
                }

                if (brush.TileMode != TileMode.None)
                {
                    var matrix = result.Matrix;
                    matrix.InitTranslate(-helper.DestinationRect.X, -helper.DestinationRect.Y);
                    result.Matrix = matrix;
                }

                return result;
            }
        }
Example #3
0
        public TileBrushImpl(
            TileBrush brush,
            SharpDX.Direct2D1.RenderTarget target,
            Size targetSize)
        {
            var helper = new TileBrushImplHelper(brush, targetSize);
            if (!helper.IsValid)
                return;

            using (var intermediate = new BitmapRenderTarget(target, CompatibleRenderTargetOptions.None, helper.IntermediateSize.ToSharpDX()))
            {
                using (var ctx = new RenderTarget(intermediate).CreateDrawingContext())
                    helper.DrawIntermediate(ctx);

                PlatformBrush = new BitmapBrush(
                    target,
                    intermediate.Bitmap,
                    GetBitmapBrushProperties(brush),
                    GetBrushProperties(brush, helper.DestinationRect));
            }
        }
Example #4
0
        public static SurfacePattern CreateTileBrush(ITileBrush brush, Size targetSize)
        {
            var helper = new TileBrushImplHelper(brush, targetSize);

            if (!helper.IsValid)
            {
                return(null);
            }

            using (var intermediate = new ImageSurface(Format.ARGB32, (int)helper.IntermediateSize.Width, (int)helper.IntermediateSize.Height))
                using (var ctx = new RenderTarget(intermediate).CreateDrawingContext())
                {
                    helper.DrawIntermediate(ctx);

                    var result = new SurfacePattern(intermediate);

                    if ((brush.TileMode & TileMode.FlipXY) != 0)
                    {
                        // TODO: Currently always FlipXY as that's all cairo supports natively.
                        // Support separate FlipX and FlipY by drawing flipped images to intermediate
                        // surface.
                        result.Extend = Extend.Reflect;
                    }
                    else
                    {
                        result.Extend = Extend.Repeat;
                    }

                    if (brush.TileMode != TileMode.None)
                    {
                        var matrix = result.Matrix;
                        matrix.InitTranslate(-helper.DestinationRect.X, -helper.DestinationRect.Y);
                        result.Matrix = matrix;
                    }

                    return(result);
                }
        }
        internal 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 ISolidColorBrush;
            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;
        }
        internal 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 ISolidColorBrush;

            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?
                    using (var shader = SKShader.CreateLinearGradient(start, end, stopColors, stopOffsets, tileMode))
                        paint.Shader = shader;
                }
                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?
                        using (var shader = SKShader.CreateRadialGradient(center, radius, stopColors, stopOffsets, tileMode))
                            paint.Shader = shader;
                    }
                }

                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;
                using (var shader = SKShader.CreateBitmap(bitmap.Bitmap, tileX, tileY, translation))
                    paint.Shader = shader;
            }

            return(rv);
        }
Example #7
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;
        }
Example #8
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);
        }