From 861c0e4c42dc1f28a50ad0f516c64823618093f6 Mon Sep 17 00:00:00 2001 From: Emmanuel Hansen Date: Wed, 29 May 2024 14:31:20 +0000 Subject: [PATCH] Apply Alignments to TileBrushes with uniform stretch (#15846) * add tiled brush alignment cross tests * apply Alignments to TileBrushes with uniform stretch --- src/Skia/Avalonia.Skia/DrawingContextImpl.cs | 8 +++-- .../CrossTests/Brushes/CrossTileBrushTests.cs | 30 ++++++++++++++++++ .../Should_Render_Aligned_TileBrush.wpf.png | Bin 0 -> 393 bytes 3 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 tests/TestFiles/CrossTests/Media/TileBrushes/Should_Render_Aligned_TileBrush.wpf.png diff --git a/src/Skia/Avalonia.Skia/DrawingContextImpl.cs b/src/Skia/Avalonia.Skia/DrawingContextImpl.cs index 82c3040366a..c54643a8daa 100644 --- a/src/Skia/Avalonia.Skia/DrawingContextImpl.cs +++ b/src/Skia/Avalonia.Skia/DrawingContextImpl.cs @@ -1168,10 +1168,12 @@ private void ConfigureSceneBrushContentWithPicture(ref PaintWrapper paintWrapper brushTransform *= Matrix.CreateTranslation(-sourceRect.Position); + var scale = Vector.One; + if (sourceRect.Size != destinationRect.Size) { //scale source to destination size - var scale = tileBrush.Stretch.CalculateScaling(destinationRect.Size, sourceRect.Size); + scale = tileBrush.Stretch.CalculateScaling(destinationRect.Size, sourceRect.Size); var scaleTransform = Matrix.CreateScale(scale); @@ -1197,11 +1199,11 @@ private void ConfigureSceneBrushContentWithPicture(ref PaintWrapper paintWrapper } } - if (tileBrush.Stretch == Stretch.None && transform == Matrix.Identity) + if (tileBrush.Stretch != Stretch.Fill && transform == Matrix.Identity) { //align content var alignmentOffset = TileBrushCalculator.CalculateTranslate(tileBrush.AlignmentX, tileBrush.AlignmentY, - contentBounds, destinationRect, Vector.One); + contentBounds, destinationRect, tileBrush.Stretch == Stretch.None ? Vector.One : scale); brushTransform *= Matrix.CreateTranslation(alignmentOffset); } diff --git a/tests/Avalonia.RenderTests/CrossTests/Brushes/CrossTileBrushTests.cs b/tests/Avalonia.RenderTests/CrossTests/Brushes/CrossTileBrushTests.cs index 160aa259422..13d4930bc97 100644 --- a/tests/Avalonia.RenderTests/CrossTests/Brushes/CrossTileBrushTests.cs +++ b/tests/Avalonia.RenderTests/CrossTests/Brushes/CrossTileBrushTests.cs @@ -78,6 +78,36 @@ public void Should_Render_Scaled_TileBrush() } + [CrossFact] + public void Should_Render_Aligned_TileBrush() + { + var brush = new CrossDrawingBrush + { + TileMode = TileMode.Tile, + AlignmentX = AlignmentX.Center, + AlignmentY = AlignmentY.Center, + Stretch = Stretch.Uniform, + Drawing = new CrossDrawingGroup() + { + Children = new List() + { + new CrossGeometryDrawing(new CrossRectangleGeometry(new(0, 0, 100, 150))) + { + Brush = new CrossSolidColorBrush(Colors.Crimson) + }, + } + } + }; + + RenderAndCompare(new CrossControl() + { + Width = 100, + Height = 100, + Background = brush + }); + + } + [CrossFact] public void Should_Render_With_Transform() { diff --git a/tests/TestFiles/CrossTests/Media/TileBrushes/Should_Render_Aligned_TileBrush.wpf.png b/tests/TestFiles/CrossTests/Media/TileBrushes/Should_Render_Aligned_TileBrush.wpf.png new file mode 100644 index 0000000000000000000000000000000000000000..959613825b5b82d67807cc5eb6466196f28daf87 GIT binary patch literal 393 zcmeAS@N?(olHy`uVBq!ia0vp^DIm