7 changed files with 141 additions and 152 deletions
@ -1,31 +0,0 @@ |
|||||
using System; |
|
||||
using Avalonia.Media; |
|
||||
using Avalonia.Utilities; |
|
||||
|
|
||||
namespace Avalonia.Rendering.SceneGraph; |
|
||||
|
|
||||
internal static class GeometryBoundsHelper |
|
||||
{ |
|
||||
/// <summary>
|
|
||||
/// Calculates the bounds of a given geometry with respect to the pens <see cref="IPen.LineCap"/>
|
|
||||
/// </summary>
|
|
||||
/// <param name="originalBounds">The calculated bounds without <see cref="IPen.LineCap"/>s</param>
|
|
||||
/// <param name="pen">The pen with information about the <see cref="IPen.LineCap"/>s</param>
|
|
||||
/// <returns></returns>
|
|
||||
public static Rect CalculateBoundsWithLineCaps(this Rect originalBounds, IPen? pen) |
|
||||
{ |
|
||||
if (pen is null || MathUtilities.IsZero(pen.Thickness)) return originalBounds; |
|
||||
|
|
||||
switch (pen.LineCap) |
|
||||
{ |
|
||||
case PenLineCap.Flat: |
|
||||
return originalBounds; |
|
||||
case PenLineCap.Round: |
|
||||
return originalBounds.Inflate(pen.Thickness / 2); |
|
||||
case PenLineCap.Square: |
|
||||
return originalBounds.Inflate(pen.Thickness); |
|
||||
default: |
|
||||
throw new ArgumentOutOfRangeException(); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,43 @@ |
|||||
|
using System; |
||||
|
using Avalonia.Controls.Shapes; |
||||
|
using Avalonia.Layout; |
||||
|
using Avalonia.Media; |
||||
|
using Avalonia.Platform; |
||||
|
using Avalonia.Rendering; |
||||
|
using Avalonia.UnitTests; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Avalonia.Skia.UnitTests |
||||
|
{ |
||||
|
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), |
||||
|
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) |
||||
|
|
||||
|
] |
||||
|
public void RenderBoundsAreCorrectlyCalculated(string path, PenLineCap cap, PenLineJoin join, double thickness, double miterLimit, double x, double y, double width, double height) |
||||
|
{ |
||||
|
using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface |
||||
|
.With(renderInterface: new PlatformRenderInterface()))) |
||||
|
{ |
||||
|
var geo = PathGeometry.Parse(path); |
||||
|
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); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue