Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes geometry render bounds when curves are present #16756

Merged
merged 4 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Skia/Avalonia.Skia/GeometryImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public Rect GetRenderBounds(IPen? pen)
_pathCache.UpdateIfNeeded(StrokePath, pen);
var bounds = _pathCache.RenderBounds;
if (StrokePath != FillPath && FillPath != null)
bounds = bounds.Union(FillPath.Bounds.ToAvaloniaRect());
bounds = bounds.Union(FillPath.TightBounds.ToAvaloniaRect());
return bounds;
}
}
Expand Down Expand Up @@ -178,7 +178,7 @@ private struct PathCache : IDisposable
private Rect? _renderBounds;
private static readonly SKPath s_emptyPath = new();

public Rect RenderBounds => _renderBounds ??= (_path ?? _cachedFor ?? s_emptyPath).Bounds.ToAvaloniaRect();
public Rect RenderBounds => _renderBounds ??= (_path ?? _cachedFor ?? s_emptyPath).TightBounds.ToAvaloniaRect();
public SKPath ExpandedPath => _path ?? s_emptyPath;

public void UpdateIfNeeded(SKPath? strokePath, IPen? pen)
Expand Down
27 changes: 10 additions & 17 deletions tests/Avalonia.Skia.UnitTests/RenderBoundsTests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using System;
using Avalonia.Controls.Shapes;
using Avalonia.Layout;
using Avalonia.Media;
using Avalonia.Platform;
using Avalonia.Rendering;
using Avalonia.Media;
using Avalonia.UnitTests;
using Xunit;

Expand All @@ -13,12 +8,13 @@ public class RenderBoundsTests
{
[Theory,
InlineData("M10 20 L 20 10 L 30 20", PenLineCap.Round, PenLineJoin.Miter, 2, 10,
8.585786819458008, 8.585786819458008, 22.828428268432617, 12.828428268432617),
9, 8.585786819458008, 22.000001907348633, 12.414215087890625),
InlineData("M10 10 L 20 10", PenLineCap.Round, PenLineJoin.Miter,2, 10,
9,9,12,2),
InlineData("M10 10 L 20 15 L 10 20", PenLineCap.Flat, PenLineJoin.Miter, 2, 20,
9.552786827087402, 9.105572700500488, 12.683281898498535, 11.788853645324707)

9.552786827087402, 9.105572700500488, 12.683281898498535, 11.788853645324707),
InlineData("M0,0 A128,128 0 0 0 128,0", PenLineCap.Flat, PenLineJoin.Bevel, 0, 0,
0, 0, 128, 17.14875030517578)
]
public void RenderBoundsAreCorrectlyCalculated(string path, PenLineCap cap, PenLineJoin join, double thickness, double miterLimit, double x, double y, double width, double height)
{
Expand All @@ -29,14 +25,11 @@ public void RenderBoundsAreCorrectlyCalculated(string path, PenLineCap cap, PenL
var pen = new Pen(Brushes.Black, thickness, null, cap, join, miterLimit);
var bounds = geo.GetRenderBounds(pen);
var tolerance = 0.001;
if (
Math.Abs(bounds.X - x) > tolerance
|| Math.Abs(bounds.Y - y) > tolerance
|| Math.Abs(bounds.Width - width) > tolerance
|| Math.Abs(bounds.Height - height) > tolerance)
Assert.Fail($"Expected {x}:{y}:{width}:{height}, got {bounds}");

Assert.Equal(new Rect(x, y, width, height), bounds);

Assert.Equal(bounds.X, x, tolerance);
Assert.Equal(bounds.Y, y, tolerance);
Assert.Equal(bounds.Width, width, tolerance);
Assert.Equal(bounds.Height, height, tolerance);
}
}
}
Expand Down
Loading