diff --git a/src/Avalonia.Controls/Presenters/ContentPresenter.cs b/src/Avalonia.Controls/Presenters/ContentPresenter.cs index 50aa8a9e71..f75184c686 100644 --- a/src/Avalonia.Controls/Presenters/ContentPresenter.cs +++ b/src/Avalonia.Controls/Presenters/ContentPresenter.cs @@ -365,12 +365,8 @@ namespace Avalonia.Controls.Presenters if (useLayoutRounding) { - sizeForChild = new Size( - Math.Ceiling(sizeForChild.Width * scale) / scale, - Math.Ceiling(sizeForChild.Height * scale) / scale); - availableSize = new Size( - Math.Ceiling(availableSize.Width * scale) / scale, - Math.Ceiling(availableSize.Height * scale) / scale); + sizeForChild = LayoutHelper.RoundLayoutSize(sizeForChild, scale, scale); + availableSize = LayoutHelper.RoundLayoutSize(availableSize, scale, scale); } switch (horizontalContentAlignment) @@ -395,8 +391,8 @@ namespace Avalonia.Controls.Presenters if (useLayoutRounding) { - originX = Math.Floor(originX * scale) / scale; - originY = Math.Floor(originY * scale) / scale; + originX = LayoutHelper.RoundLayoutValue(originX, scale); + originY = LayoutHelper.RoundLayoutValue(originY, scale); } var boundsForChild = diff --git a/src/Avalonia.Layout/LayoutHelper.cs b/src/Avalonia.Layout/LayoutHelper.cs index 2b61de00a7..d8fa00deb7 100644 --- a/src/Avalonia.Layout/LayoutHelper.cs +++ b/src/Avalonia.Layout/LayoutHelper.cs @@ -82,6 +82,64 @@ namespace Avalonia.Layout InnerInvalidateMeasure(control); } + /// + /// Rounds a size to integer values for layout purposes, compensating for high DPI screen + /// coordinates. + /// + /// Input size. + /// DPI along x-dimension. + /// DPI along y-dimension. + /// Value of size that will be rounded under screen DPI. + /// + /// This is a layout helper method. It takes DPI into account and also does not return + /// the rounded value if it is unacceptable for layout, e.g. Infinity or NaN. It's a helper + /// associated with the UseLayoutRounding property and should not be used as a general rounding + /// utility. + /// + public static Size RoundLayoutSize(Size size, double dpiScaleX, double dpiScaleY) + { + return new Size(RoundLayoutValue(size.Width, dpiScaleX), RoundLayoutValue(size.Height, dpiScaleY)); + } + + /// + /// Calculates the value to be used for layout rounding at high DPI. + /// + /// Input value to be rounded. + /// Ratio of screen's DPI to layout DPI + /// Adjusted value that will produce layout rounding on screen at high dpi. + /// + /// This is a layout helper method. It takes DPI into account and also does not return + /// the rounded value if it is unacceptable for layout, e.g. Infinity or NaN. It's a helper + /// associated with the UseLayoutRounding property and should not be used as a general rounding + /// utility. + /// + public static double RoundLayoutValue(double value, double dpiScale) + { + double newValue; + + // If DPI == 1, don't use DPI-aware rounding. + if (!MathUtilities.AreClose(dpiScale, 1.0)) + { + newValue = Math.Round(value * dpiScale) / dpiScale; + + // If rounding produces a value unacceptable to layout (NaN, Infinity or MaxValue), + // use the original value. + if (double.IsNaN(newValue) || + double.IsInfinity(newValue) || + MathUtilities.AreClose(newValue, double.MaxValue)) + { + newValue = value; + } + } + else + { + newValue = Math.Round(value); + } + + return newValue; + } + + /// /// Calculates the min and max height for a control. Ported from WPF. /// diff --git a/src/Avalonia.Layout/Layoutable.cs b/src/Avalonia.Layout/Layoutable.cs index cbeab0482e..ce5200f4a4 100644 --- a/src/Avalonia.Layout/Layoutable.cs +++ b/src/Avalonia.Layout/Layoutable.cs @@ -1,5 +1,6 @@ using System; using Avalonia.Logging; +using Avalonia.Utilities; using Avalonia.VisualTree; namespace Avalonia.Layout @@ -545,8 +546,8 @@ namespace Avalonia.Layout if (UseLayoutRounding) { var scale = GetLayoutScale(); - width = Math.Ceiling(width * scale) / scale; - height = Math.Ceiling(height * scale) / scale; + width = LayoutHelper.RoundLayoutValue(width, scale); + height = LayoutHelper.RoundLayoutValue(height, scale); } return NonNegative(new Size(width, height).Inflate(margin)); @@ -623,12 +624,8 @@ namespace Avalonia.Layout if (useLayoutRounding) { - size = new Size( - Math.Ceiling(size.Width * scale) / scale, - Math.Ceiling(size.Height * scale) / scale); - availableSizeMinusMargins = new Size( - Math.Ceiling(availableSizeMinusMargins.Width * scale) / scale, - Math.Ceiling(availableSizeMinusMargins.Height * scale) / scale); + size = LayoutHelper.RoundLayoutSize(size, scale, scale); + availableSizeMinusMargins = LayoutHelper.RoundLayoutSize(availableSizeMinusMargins, scale, scale); } size = ArrangeOverride(size).Constrain(size); @@ -657,8 +654,8 @@ namespace Avalonia.Layout if (useLayoutRounding) { - originX = Math.Floor(originX * scale) / scale; - originY = Math.Floor(originY * scale) / scale; + originX = LayoutHelper.RoundLayoutValue(originX, scale); + originY = LayoutHelper.RoundLayoutValue(originY, scale); } Bounds = new Rect(originX, originY, size.Width, size.Height); diff --git a/tests/Avalonia.Controls.UnitTests/Primitives/TrackTests.cs b/tests/Avalonia.Controls.UnitTests/Primitives/TrackTests.cs index f4001a8ca1..59276a94d0 100644 --- a/tests/Avalonia.Controls.UnitTests/Primitives/TrackTests.cs +++ b/tests/Avalonia.Controls.UnitTests/Primitives/TrackTests.cs @@ -67,7 +67,7 @@ namespace Avalonia.Controls.UnitTests.Primitives target.Measure(new Size(100, 100)); target.Arrange(new Rect(0, 0, 100, 100)); - Assert.Equal(new Rect(33, 0, 34, 12), thumb.Bounds); + Assert.Equal(new Rect(33, 0, 33, 12), thumb.Bounds); } [Fact] @@ -92,7 +92,7 @@ namespace Avalonia.Controls.UnitTests.Primitives target.Measure(new Size(100, 100)); target.Arrange(new Rect(0, 0, 100, 100)); - Assert.Equal(new Rect(0, 33, 12, 34), thumb.Bounds); + Assert.Equal(new Rect(0, 33, 12, 33), thumb.Bounds); } [Fact] diff --git a/tests/TestFiles/Direct2D1/Shapes/Path/Path_Expander_With_Border.expected.png b/tests/TestFiles/Direct2D1/Shapes/Path/Path_Expander_With_Border.expected.png index 4a7f4c26b0..7a7d70a521 100644 Binary files a/tests/TestFiles/Direct2D1/Shapes/Path/Path_Expander_With_Border.expected.png and b/tests/TestFiles/Direct2D1/Shapes/Path/Path_Expander_With_Border.expected.png differ diff --git a/tests/TestFiles/Skia/Shapes/Path/Path_Expander_With_Border.expected.png b/tests/TestFiles/Skia/Shapes/Path/Path_Expander_With_Border.expected.png index 4a7f4c26b0..7727284050 100644 Binary files a/tests/TestFiles/Skia/Shapes/Path/Path_Expander_With_Border.expected.png and b/tests/TestFiles/Skia/Shapes/Path/Path_Expander_With_Border.expected.png differ