From 7b3352687c313950f53e567b7c36a8da15c57a5f Mon Sep 17 00:00:00 2001 From: Tim Date: Mon, 8 Aug 2022 11:24:12 +0200 Subject: [PATCH] Backport PR 7778 --- .../SceneGraph/GeometryBoundsHelper.cs | 31 +++++++++++++++++++ .../Rendering/SceneGraph/GeometryNode.cs | 2 +- 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 src/Avalonia.Visuals/Rendering/SceneGraph/GeometryBoundsHelper.cs diff --git a/src/Avalonia.Visuals/Rendering/SceneGraph/GeometryBoundsHelper.cs b/src/Avalonia.Visuals/Rendering/SceneGraph/GeometryBoundsHelper.cs new file mode 100644 index 00000000000..b3a91008502 --- /dev/null +++ b/src/Avalonia.Visuals/Rendering/SceneGraph/GeometryBoundsHelper.cs @@ -0,0 +1,31 @@ +using System; +using Avalonia.Media; +using Avalonia.Utilities; + +namespace Avalonia.Rendering.SceneGraph; + +internal static class GeometryBoundsHelper +{ + /// + /// Calculates the bounds of a given geometry with respect to the pens + /// + /// The calculated bounds without s + /// The pen with information about the s + /// + 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(); + } + } +} diff --git a/src/Avalonia.Visuals/Rendering/SceneGraph/GeometryNode.cs b/src/Avalonia.Visuals/Rendering/SceneGraph/GeometryNode.cs index 508ca0ad183..378a4cdd201 100644 --- a/src/Avalonia.Visuals/Rendering/SceneGraph/GeometryNode.cs +++ b/src/Avalonia.Visuals/Rendering/SceneGraph/GeometryNode.cs @@ -24,7 +24,7 @@ public GeometryNode(Matrix transform, IPen pen, IGeometryImpl geometry, IDictionary childScenes = null) - : base(geometry.GetRenderBounds(pen), transform) + : base(geometry.GetRenderBounds(pen).CalculateBoundsWithLineCaps(pen), transform) { Transform = transform; Brush = brush?.ToImmutable();