Skip to content

Commit

Permalink
Backport PR 7778
Browse files Browse the repository at this point in the history
  • Loading branch information
timunie committed Aug 8, 2022
1 parent 765c4d7 commit 7b33526
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
31 changes: 31 additions & 0 deletions src/Avalonia.Visuals/Rendering/SceneGraph/GeometryBoundsHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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();
}
}
}
2 changes: 1 addition & 1 deletion src/Avalonia.Visuals/Rendering/SceneGraph/GeometryNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ internal class GeometryNode : BrushDrawOperation
IPen pen,
IGeometryImpl geometry,
IDictionary<IVisual, Scene> childScenes = null)
: base(geometry.GetRenderBounds(pen), transform)
: base(geometry.GetRenderBounds(pen).CalculateBoundsWithLineCaps(pen), transform)
{
Transform = transform;
Brush = brush?.ToImmutable();
Expand Down

0 comments on commit 7b33526

Please sign in to comment.