From 12312f99e0f238fa724c5dd63844928b0c4eb21e Mon Sep 17 00:00:00 2001 From: fedejeanne Date: Thu, 26 Jun 2025 11:13:04 +0200 Subject: [PATCH] Reintroduce OfFloat (internal) classes #62 Deprecate MonitorAwarePoint and MonitorAwareRectangle for removal --- .../swt/widgets/ControlWin32Tests.java | 4 +- .../widgets/CoordinateSystemMapperTests.java | 6 +- .../swt/graphics/MonitorAwarePoint.java | 2 + .../swt/graphics/MonitorAwareRectangle.java | 2 + .../org/eclipse/swt/graphics/Point.java | 82 +++++++++++- .../org/eclipse/swt/graphics/Rectangle.java | 119 +++++++++++++++++- .../org/eclipse/swt/internal/DPIUtil.java | 78 ++++++------ .../MultiZoomCoordinateSystemMapper.java | 12 +- .../eclipse/swt/tests/junit/DPIUtilTests.java | 38 +++++- 9 files changed, 289 insertions(+), 54 deletions(-) diff --git a/bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/widgets/ControlWin32Tests.java b/bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/widgets/ControlWin32Tests.java index 7926fb9e16..759580dc17 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/widgets/ControlWin32Tests.java +++ b/bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/widgets/ControlWin32Tests.java @@ -100,11 +100,11 @@ public void testCorrectScaleUpUsingDifferentSetBoundsMethod() { button.setBounds(new Rectangle(0, 47, 200, 47)); assertEquals("Control::setBounds(Rectangle) doesn't scale up correctly", - new Rectangle(0, 82, 350, 83), button.getBoundsInPixels()); + new Rectangle(0, 82, 350, 82), button.getBoundsInPixels()); button.setBounds(0, 47, 200, 47); assertEquals("Control::setBounds(int, int, int, int) doesn't scale up correctly", - new Rectangle(0, 82, 350, 83), button.getBoundsInPixels()); + new Rectangle(0, 82, 350, 82), button.getBoundsInPixels()); } record FontComparison(int originalFontHeight, int currentFontHeight) { diff --git a/bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/widgets/CoordinateSystemMapperTests.java b/bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/widgets/CoordinateSystemMapperTests.java index d2c8068877..a485273500 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/widgets/CoordinateSystemMapperTests.java +++ b/bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/widgets/CoordinateSystemMapperTests.java @@ -137,7 +137,7 @@ void translateRectangleInGapPartiallyInRightBackAndForthInSingleZoomShouldBeTheS void translateRectangleInGapPartiallyInRightBackAndForthInMultiZoomShouldBeInside() { MultiZoomCoordinateSystemMapper mapper = getMultiZoomCoordinateSystemMapper(); setupMonitors(mapper); - Rectangle rectInPts = new MonitorAwareRectangle(1950, 400, 150, 100, monitors[1]); + Rectangle rectInPts = new Rectangle.WithMonitor(1950, 400, 150, 100, monitors[1]); Rectangle rectInPxs = mapper.translateToDisplayCoordinates(rectInPts, monitors[0].getZoom()); assertEquals(rectInPts, mapper.translateFromDisplayCoordinates(rectInPxs, monitors[0].getZoom())); } @@ -223,7 +223,7 @@ private Point createExpectedPoint(CoordinateSystemMapper mapper, int x, int y, M if (mapper instanceof SingleZoomCoordinateSystemMapper) { return new Point(x, y); } else { - return new MonitorAwarePoint(x, y, monitor); + return new Point.WithMonitor(x, y, monitor); } } @@ -231,7 +231,7 @@ private Rectangle createExpectedRectangle(CoordinateSystemMapper mapper, int x, if (mapper instanceof SingleZoomCoordinateSystemMapper) { return new Rectangle(x, y, width, height); } else { - return new MonitorAwareRectangle(x, y, width, height, monitor); + return new Rectangle.WithMonitor(x, y, width, height, monitor); } } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/MonitorAwarePoint.java b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/MonitorAwarePoint.java index 5e73e051e6..48efc4262b 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/MonitorAwarePoint.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/MonitorAwarePoint.java @@ -23,7 +23,9 @@ * * @since 3.129 * @noreference This class is not intended to be referenced by clients + * @deprecated */ +@Deprecated(forRemoval = true, since = "2025-09") public final class MonitorAwarePoint extends Point { private static final long serialVersionUID = 6077427420686999194L; diff --git a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/MonitorAwareRectangle.java b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/MonitorAwareRectangle.java index ea0597621c..7e03912234 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/MonitorAwareRectangle.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/MonitorAwareRectangle.java @@ -23,7 +23,9 @@ * * @since 3.129 * @noreference This class is not intended to be referenced by clients + * @deprecated */ +@Deprecated(forRemoval = true, since = "2025-09") public final class MonitorAwareRectangle extends Rectangle { private static final long serialVersionUID = 5041911840525116925L; diff --git a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/Point.java b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/Point.java index 2aef2d0682..83edb88ff8 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/Point.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/Point.java @@ -16,6 +16,8 @@ import java.io.*; +import org.eclipse.swt.widgets.*; + /** * Instances of this class represent places on the (x, y) * coordinate plane. @@ -41,7 +43,8 @@ * @see Sample code and further information */ -public sealed class Point implements Serializable permits MonitorAwarePoint { +@SuppressWarnings("removal") +public sealed class Point implements Serializable permits MonitorAwarePoint, Point.OfFloat { /** * the x coordinate of the point @@ -116,5 +119,82 @@ public String toString () { return "Point {" + x + ", " + y + "}"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ } +/** + * Instances of this class represent {@link org.eclipse.swt.graphics.Point} + * objects with the fields capable of storing more precise value in float. + * + * @since 3.131 + * @noreference This class is not intended to be referenced by clients + */ +public static sealed class OfFloat extends Point permits Point.WithMonitor { + + private static final long serialVersionUID = -1862062276431597053L; + + public float residualX, residualY; + + public OfFloat(int x, int y) { + super(x, y); + } + + public OfFloat(float x, float y) { + super(Math.round(x), Math.round(y)); + this.residualX = x - this.x; + this.residualY = y - this.y; + } + + public float getX() { + return x + residualX; + } + + public float getY() { + return y + residualY; + } + + public void setX(float x) { + this.x = Math.round(x); + this.residualX = x - this.x; + } + + public void setY(float y) { + this.y = Math.round(y); + this.residualY = y - this.y; + } } +/** + * Instances of this class represent {@link org.eclipse.swt.graphics.Point.OfFloat} + * objects along with the context of the monitor in relation to which they are + * placed on the display. The monitor awareness makes it easy to scale and + * translate the points between pixels and points. + * + * @since 3.131 + * @noreference This class is not intended to be referenced by clients + */ +public static final class WithMonitor extends Point.OfFloat { + + private static final long serialVersionUID = 6077427420686999194L; + + private final Monitor monitor; + + /** + * Constructs a new Point.WithMonitor + * + * @param x the x coordinate of the point + * @param y the y coordinate of the point + * @param monitor the monitor with whose context the point is created + */ + public WithMonitor(int x, int y, Monitor monitor) { + super(x, y); + this.monitor = monitor; + } + + /** + * {@return the monitor with whose context the instance is created} + */ + public Monitor getMonitor() { + return monitor; + } + +} + +} diff --git a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/Rectangle.java b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/Rectangle.java index b50cb13e6a..685f0e09be 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/Rectangle.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/Rectangle.java @@ -17,6 +17,7 @@ import java.io.*; import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; /** * Instances of this class represent rectangular areas in an @@ -45,7 +46,8 @@ * @see Sample code and further information */ -public sealed class Rectangle implements Serializable, Cloneable permits MonitorAwareRectangle { +@SuppressWarnings("removal") +public sealed class Rectangle implements Serializable, Cloneable permits MonitorAwareRectangle, Rectangle.OfFloat { /** * the x coordinate of the rectangle @@ -373,8 +375,8 @@ public Rectangle union (Rectangle rect) { * @since 3.131 */ public static Rectangle of(Point topLeft, int width, int height) { - if (topLeft instanceof MonitorAwarePoint monitorAwareTopLeft) { - return new MonitorAwareRectangle(topLeft.x, topLeft.y, width, height, monitorAwareTopLeft.getMonitor()); + if (topLeft instanceof Point.WithMonitor monitorAwareTopLeft) { + return new Rectangle.WithMonitor(monitorAwareTopLeft.getX(), monitorAwareTopLeft.getY(), width, height, monitorAwareTopLeft.getMonitor()); } return new Rectangle(topLeft.x, topLeft.y, width, height); } @@ -396,4 +398,115 @@ public static Rectangle of(Point topLeft, int width, int height) { public Rectangle clone() { return new Rectangle(x, y, width, height); } + +/** + * Instances of this class represent {@link org.eclipse.swt.graphics.Rectangle} + * objects which supports values of Float type for it's fields + * + * @since 3.131 + * @noreference This class is not intended to be referenced by clients + */ +public static sealed class OfFloat extends Rectangle permits Rectangle.WithMonitor { + + private static final long serialVersionUID = -3006999002677468391L; + + private float residualX, residualY, residualWidth, residualHeight; + + public OfFloat(int x, int y, int width, int height) { + super(x, y, width, height); + } + + public OfFloat(float x, float y, float width, float height) { + super(Math.round(x), Math.round(y), Math.round(width), Math.round(height)); + this.residualX = x - this.x; + this.residualY = y - this.y; + this.residualWidth = width - this.width; + this.residualHeight = height - this.height; + } + + public float getX() { + return x + residualX; + } + + public float getY() { + return y + residualY; + } + + public float getWidth() { + return width + residualWidth; + } + + public float getHeight() { + return height + residualHeight; + } + + public void setX(float x) { + this.x = Math.round(x); + this.residualX = x - this.x; + } + + public void setY(float y) { + this.y = Math.round(y); + this.residualY = y - this.y; + } + + public void setWidth(float width) { + this.width = Math.round(width); + this.residualWidth = width - this.width; + } + + public void setHeight(float height) { + this.height = Math.round(height); + this.residualHeight = height - this.height; + } + +} + +/** + * Instances of this class represent {@link org.eclipse.swt.graphics.Rectangle.OfFloat} + * objects along with the context of the monitor in relation to which they are + * placed on the display. The monitor awareness makes it easy to scale and + * translate the rectangles between pixels and points. + * + * @since 3.131 + * @noreference This class is not intended to be referenced by clients + */ +public static final class WithMonitor extends Rectangle.OfFloat { + + private static final long serialVersionUID = 5041911840525116925L; + + private final Monitor monitor; + + /** + * Constructs a new Rectangle.WithMonitor + * + * @param x the x coordinate of the top left corner of the rectangle + * @param y the y coordinate of the top left corner of the rectangle + * @param width the width of the rectangle + * @param height the height of the rectangle + * @param monitor the monitor with whose context the rectangle is created + */ + public WithMonitor(int x, int y, int width, int height, Monitor monitor) { + super(x, y, width, height); + this.monitor = monitor; + } + + private WithMonitor(float x, float y, float width, float height, Monitor monitor) { + super(x, y, width, height); + this.monitor = monitor; + } + + /** + * {@return the monitor with whose context the instance is created} + */ + public Monitor getMonitor() { + return monitor; + } + + @Override + public Rectangle.WithMonitor clone() { + return new Rectangle.WithMonitor(getX(), getY(), getWidth(), getHeight(), monitor); + } + } +} \ No newline at end of file diff --git a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/DPIUtil.java b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/DPIUtil.java index a15f4683ae..f55fe211a5 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/DPIUtil.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/DPIUtil.java @@ -228,11 +228,11 @@ public static Point autoScaleDown(Point point) { public static Point scaleDown(Point point, int zoom) { if (zoom == 100 || point == null) return point; + Point.OfFloat fPoint = FloatAwareGeometryFactory.createFrom(point); float scaleFactor = getScalingFactor(zoom); - Point scaledPoint = new Point (0,0); - scaledPoint.x = Math.round (point.x / scaleFactor); - scaledPoint.y = Math.round (point.y / scaleFactor); - return scaledPoint; + float scaledX = fPoint.getX() / scaleFactor; + float scaledY = fPoint.getY() / scaleFactor; + return new Point.OfFloat(scaledX, scaledY); } /** @@ -255,16 +255,7 @@ public static Rectangle autoScaleDown(Rectangle rect) { } public static Rectangle scaleDown(Rectangle rect, int zoom) { - if (zoom == 100 || rect == null) return rect; - Rectangle scaledRect = new Rectangle (0,0,0,0); - Point scaledTopLeft = scaleDown(new Point (rect.x, rect.y), zoom); - Point scaledBottomRight = scaleDown(new Point (rect.x + rect.width, rect.y + rect.height), zoom); - - scaledRect.x = scaledTopLeft.x; - scaledRect.y = scaledTopLeft.y; - scaledRect.width = scaledBottomRight.x - scaledTopLeft.x; - scaledRect.height = scaledBottomRight.y - scaledTopLeft.y; - return scaledRect; + return scaleBounds(rect, 100, zoom); } /** * Returns a new scaled down Rectangle if enabled for Drawable class. @@ -333,13 +324,13 @@ public static boolean isSmoothScalingEnabled() { */ public static Rectangle scaleBounds (Rectangle rect, int targetZoom, int currentZoom) { if (rect == null || targetZoom == currentZoom) return rect; - float scaleFactor = ((float)targetZoom) / (float)currentZoom; - Rectangle returnRect = new Rectangle (0,0,0,0); - returnRect.x = Math.round (rect.x * scaleFactor); - returnRect.y = Math.round (rect.y * scaleFactor); - returnRect.width = Math.round (rect.width * scaleFactor); - returnRect.height = Math.round (rect.height * scaleFactor); - return returnRect; + Rectangle.OfFloat fRect = FloatAwareGeometryFactory.createFrom(rect); + float scaleFactor = getScalingFactor(targetZoom, currentZoom); + float scaledX = fRect.getX() * scaleFactor; + float scaledY = fRect.getY() * scaleFactor; + float scaledWidth = fRect.getWidth() * scaleFactor; + float scaledHeight = fRect.getHeight() * scaleFactor; + return new Rectangle.OfFloat(scaledX, scaledY, scaledWidth, scaledHeight); } /** @@ -436,11 +427,11 @@ public static Point autoScaleUp(Point point) { public static Point scaleUp(Point point, int zoom) { if (zoom == 100 || point == null) return point; + Point.OfFloat fPoint = FloatAwareGeometryFactory.createFrom(point); float scaleFactor = getScalingFactor(zoom); - Point scaledPoint = new Point(0,0); - scaledPoint.x = Math.round (point.x * scaleFactor); - scaledPoint.y = Math.round (point.y * scaleFactor); - return scaledPoint; + float scaledX = fPoint.getX() * scaleFactor; + float scaledY = fPoint.getY() * scaleFactor; + return new Point.OfFloat(scaledX, scaledY); } /** @@ -463,16 +454,7 @@ public static Rectangle autoScaleUp(Rectangle rect) { } public static Rectangle scaleUp(Rectangle rect, int zoom) { - if (zoom == 100 || rect == null) return rect; - Rectangle scaledRect = new Rectangle(0,0,0,0); - Point scaledTopLeft = scaleUp (new Point(rect.x, rect.y), zoom); - Point scaledBottomRight = scaleUp (new Point(rect.x + rect.width, rect.y + rect.height), zoom); - - scaledRect.x = scaledTopLeft.x; - scaledRect.y = scaledTopLeft.y; - scaledRect.width = scaledBottomRight.x - scaledTopLeft.x; - scaledRect.height = scaledBottomRight.y - scaledTopLeft.y; - return scaledRect; + return scaleBounds(rect, zoom, 100); } /** @@ -492,13 +474,17 @@ public static Rectangle scaleUp(Drawable drawable, Rectangle rect, int zoom) { * @return float scaling factor */ private static float getScalingFactor(int zoom) { + return getScalingFactor(zoom, 100); +} + +private static float getScalingFactor(int targetZoom, int currentZoom) { if (USE_CAIRO_AUTOSCALE) { return 1; } - if (zoom <= 0) { - zoom = deviceZoom; + if (targetZoom <= 0) { + targetZoom = deviceZoom; } - return zoom / 100f; + return targetZoom / (float) currentZoom; } /** @@ -751,4 +737,20 @@ public ImageData getImageData(int zoom) { return DPIUtil.scaleImageData(device, imageData, zoom, currentZoom); } } + +private class FloatAwareGeometryFactory { + static Rectangle.OfFloat createFrom(Rectangle rectangle) { + if (rectangle instanceof Rectangle.OfFloat) { + return (Rectangle.OfFloat) rectangle; + } + return new Rectangle.OfFloat(rectangle.x, rectangle.y, rectangle.width, rectangle.height); + } + + static Point.OfFloat createFrom(Point point) { + if (point instanceof Point.OfFloat) { + return (Point.OfFloat) point; + } + return new Point.OfFloat(point.x, point.y); + } +} } \ No newline at end of file diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/MultiZoomCoordinateSystemMapper.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/MultiZoomCoordinateSystemMapper.java index 886a47701e..80347e36e1 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/MultiZoomCoordinateSystemMapper.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/MultiZoomCoordinateSystemMapper.java @@ -98,19 +98,19 @@ public Point translateFromDisplayCoordinates(Point point, int zoom) { @Override public Point translateToDisplayCoordinates(Point point, int zoom) { - Monitor monitor = point instanceof MonitorAwarePoint monitorAwarePoint ? monitorAwarePoint.getMonitor() : null; + Monitor monitor = point instanceof Point.WithMonitor pointWithMonitor ? pointWithMonitor.getMonitor() : null; return translateLocationInPointsToPixels(point.x, point.y, monitor); } @Override public Rectangle translateFromDisplayCoordinates(Rectangle rect, int zoom) { - Monitor monitor = rect instanceof MonitorAwareRectangle monitorAwareRect ? monitorAwareRect.getMonitor() : null; + Monitor monitor = rect instanceof Rectangle.WithMonitor rectWithMonitor ? rectWithMonitor.getMonitor() : null; return translateRectangleInPixelsToPoints(rect.x, rect.y, rect.width, rect.height, monitor); } @Override public Rectangle translateToDisplayCoordinates(Rectangle rect, int zoom) { - Monitor monitor = rect instanceof MonitorAwareRectangle monitorAwareRect ? monitorAwareRect.getMonitor() : null; + Monitor monitor = rect instanceof Rectangle.WithMonitor rectWithMonitor ? rectWithMonitor.getMonitor() : null; return translateRectangleInPointsToPixels(rect.x, rect.y, rect.width, rect.height, monitor); } @@ -152,7 +152,7 @@ private Rectangle translateRectangleInPixelsToPoints(int x, int y, int widthInPi Point topLeft = getPointFromPixels(monitor, x, y); int width = DPIUtil.scaleDown(widthInPixels, zoom); int height = DPIUtil.scaleDown(heightInPixels, zoom); - MonitorAwareRectangle rect = new MonitorAwareRectangle(topLeft.x, topLeft.y, width, height, monitor); + Rectangle.WithMonitor rect = new Rectangle.WithMonitor(topLeft.x, topLeft.y, width, height, monitor); return rect; } @@ -264,7 +264,7 @@ private Point getPointFromPixels(Monitor monitor, int x, int y) { int zoom = getApplicableMonitorZoom(monitor); int mappedX = DPIUtil.scaleDown(x - monitor.clientX, zoom) + monitor.clientX; int mappedY = DPIUtil.scaleDown(y - monitor.clientY, zoom) + monitor.clientY; - return new MonitorAwarePoint(mappedX, mappedY, monitor); + return new Point.WithMonitor(mappedX, mappedY, monitor); } private int getApplicableMonitorZoom(Monitor monitor) { @@ -273,7 +273,7 @@ private int getApplicableMonitorZoom(Monitor monitor) { @Override public Rectangle getContainingMonitorBoundsInPixels(Point point) { - Monitor monitor = point instanceof MonitorAwarePoint monitorAwarePoint ? monitorAwarePoint.getMonitor() + Monitor monitor = point instanceof Point.WithMonitor monitorAwarePoint ? monitorAwarePoint.getMonitor() : getContainingMonitorForPoints(point.x, point.y); return getMonitorClientAreaInPixels(monitor); } diff --git a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/DPIUtilTests.java b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/DPIUtilTests.java index d501c4d0a6..8453daa68f 100644 --- a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/DPIUtilTests.java +++ b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/DPIUtilTests.java @@ -296,7 +296,7 @@ public void scaleUpPoint() { @Test public void scaleUpRectangle() { Rectangle valueAt200 = new Rectangle(100, 150, 10, 14); - Rectangle valueAt150 = new Rectangle(75, 113, 8, 10); + Rectangle valueAt150 = new Rectangle(75, 113, 8, 11); Rectangle valueAt100 = new Rectangle(50, 75, 5, 7); Rectangle scaledValue = DPIUtil.autoScaleUp(valueAt100); @@ -319,4 +319,40 @@ public void scaleUpRectangle() { scaledValue = DPIUtil.scaleUp((Device) null, valueAt100, 100); assertSame(valueAt100, scaledValue, "Scaling up Rectangle without zoom change with device failed"); } + + @Test + public void scaleDownscaleUpRectangleInvertible() { + int[] zooms = new int[] {25, 50, 75, 100, 125, 150, 175, 200, 225, 250, 275, 300, 325, 350, 375, 400}; + for (int zoom1 : zooms) { + for (int zoom2 : zooms) { + for (int i = 1; i <= 10000; i++) { + Rectangle rect = new Rectangle(0, 0, i, i); + Rectangle scaleDown = DPIUtil.scaleDown(rect, zoom1); + Rectangle scaleUp = DPIUtil.scaleUp(scaleDown, zoom2); + scaleDown = DPIUtil.scaleDown(scaleUp, zoom2); + scaleUp = DPIUtil.scaleUp(scaleDown, zoom1); + assertEquals(rect.width, scaleUp.width); + assertEquals(rect.height, scaleUp.height); + } + } + } + } + + @Test + public void scaleDownscaleUpPointInvertible() { + int[] zooms = new int[] {25, 50, 75, 100, 125, 150, 175, 200, 225, 250, 275, 300, 325, 350, 375, 400}; + for (int zoom1 : zooms) { + for (int zoom2 : zooms) { + for (int i = 1; i <= 10000; i++) { + Point pt = new Point(i, i); + Point scaleDown = DPIUtil.scaleDown(pt, zoom1); + Point scaleUp = DPIUtil.scaleUp(scaleDown, zoom2); + scaleDown = DPIUtil.scaleDown(scaleUp, zoom2); + scaleUp = DPIUtil.scaleUp(scaleDown, zoom1); + assertEquals(pt.x, scaleUp.x); + assertEquals(pt.y, scaleUp.y); + } + } + } + } }