Browse Source

Fix PixelRect snapping.

Steven Kirk 5 years ago
parent
commit
07fd1731e7
1 changed files with 11 additions and 4 deletions
  1. 11 4
      src/Avalonia.Visuals/Media/PixelRect.cs

+ 11 - 4
src/Avalonia.Visuals/Media/PixelRect.cs

@@ -377,7 +377,7 @@ namespace Avalonia
         /// <returns>The device-independent rect.</returns>
         public static PixelRect FromRect(Rect rect, double scale) => new PixelRect(
             PixelPoint.FromPoint(rect.Position, scale),
-            PixelSize.FromSize(rect.Size, scale));
+            FromPointCeiling(rect.BottomRight, new Vector(scale, scale)));
 
         /// <summary>
         /// Converts a <see cref="Rect"/> to device pixels using the specified scaling factor.
@@ -387,7 +387,7 @@ namespace Avalonia
         /// <returns>The device-independent point.</returns>
         public static PixelRect FromRect(Rect rect, Vector scale) => new PixelRect(
             PixelPoint.FromPoint(rect.Position, scale),
-            PixelSize.FromSize(rect.Size, scale));
+            FromPointCeiling(rect.BottomRight, scale));
 
         /// <summary>
         /// Converts a <see cref="Rect"/> to device pixels using the specified dots per inch (DPI).
@@ -397,7 +397,7 @@ namespace Avalonia
         /// <returns>The device-independent point.</returns>
         public static PixelRect FromRectWithDpi(Rect rect, double dpi) => new PixelRect(
             PixelPoint.FromPointWithDpi(rect.Position, dpi),
-            PixelSize.FromSizeWithDpi(rect.Size, dpi));
+            FromPointCeiling(rect.BottomRight, new Vector(dpi / 96, dpi / 96)));
 
         /// <summary>
         /// Converts a <see cref="Rect"/> to device pixels using the specified dots per inch (DPI).
@@ -407,7 +407,7 @@ namespace Avalonia
         /// <returns>The device-independent point.</returns>
         public static PixelRect FromRectWithDpi(Rect rect, Vector dpi) => new PixelRect(
             PixelPoint.FromPointWithDpi(rect.Position, dpi),
-            PixelSize.FromSizeWithDpi(rect.Size, dpi));
+            FromPointCeiling(rect.BottomRight, dpi / 96));
 
         /// <summary>
         /// Returns the string representation of the rectangle.
@@ -441,5 +441,12 @@ namespace Avalonia
                 );
             }
         }
+
+        private static PixelPoint FromPointCeiling(Point point, Vector scale)
+        {
+            return new PixelPoint(
+                (int)Math.Ceiling(point.X * scale.X),
+                (int)Math.Ceiling(point.Y * scale.Y));
+        }
     }
 }