Skip to content

Commit

Permalink
apply Alignments to TileBrushes with uniform stretch
Browse files Browse the repository at this point in the history
  • Loading branch information
emmauss committed May 29, 2024
1 parent 22231f5 commit 4630cd5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/Skia/Avalonia.Skia/DrawingContextImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<CrossDrawing>()
{
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()
{
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 4630cd5

Please sign in to comment.