Skip to content

Commit e0763ee

Browse files
committed
Add float precision to MonitorAwarePoint and MonitorAwareRectangle
1 parent edc5529 commit e0763ee

File tree

6 files changed

+164
-53
lines changed

6 files changed

+164
-53
lines changed

bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/widgets/ControlWin32Tests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,11 @@ public void testCorrectScaleUpUsingDifferentSetBoundsMethod() {
100100

101101
button.setBounds(new Rectangle(0, 47, 200, 47));
102102
assertEquals("Control::setBounds(Rectangle) doesn't scale up correctly",
103-
new Rectangle(0, 82, 350, 83), button.getBoundsInPixels());
103+
new Rectangle(0, 82, 350, 82), button.getBoundsInPixels());
104104

105105
button.setBounds(0, 47, 200, 47);
106106
assertEquals("Control::setBounds(int, int, int, int) doesn't scale up correctly",
107-
new Rectangle(0, 82, 350, 83), button.getBoundsInPixels());
107+
new Rectangle(0, 82, 350, 82), button.getBoundsInPixels());
108108
}
109109

110110
record FontComparison(int originalFontHeight, int currentFontHeight) {

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/MonitorAwarePoint.java

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,24 @@
2626
*/
2727
public final class MonitorAwarePoint extends Point {
2828

29-
private static final long serialVersionUID = 6077427420686999194L;
29+
private static final long serialVersionUID = 7516155847004716654L;
30+
31+
public float residualX, residualY;
3032

3133
private final Monitor monitor;
3234

35+
public MonitorAwarePoint(int x, int y) {
36+
this(x, y, null);
37+
}
38+
39+
public MonitorAwarePoint(float x, float y, Monitor monitor) {
40+
super(Math.round(x), Math.round(y));
41+
this.residualX = x - this.x;
42+
this.residualY = y - this.y;
43+
this.monitor = monitor;
44+
}
45+
46+
3347
/**
3448
* Constructs a new MonitorAwarePoint
3549
*
@@ -58,5 +72,22 @@ public boolean equals(Object object) {
5872
public int hashCode() {
5973
return super.hashCode();
6074
}
75+
76+
public float getX() {
77+
return x + residualX;
78+
}
79+
80+
public float getY() {
81+
return y + residualY;
82+
}
6183

62-
}
84+
public void setX(float x) {
85+
this.x = Math.round(x);
86+
this.residualX = x - this.x;
87+
}
88+
89+
public void setY(float y) {
90+
this.y = Math.round(y);
91+
this.residualY = y - this.y;
92+
}
93+
}

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/MonitorAwareRectangle.java

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,34 @@
2626
*/
2727
public final class MonitorAwareRectangle extends Rectangle {
2828

29-
private static final long serialVersionUID = 5041911840525116925L;
29+
private static final long serialVersionUID = -54807918875027527L;
30+
31+
private float residualX, residualY, residualWidth, residualHeight;
3032

3133
private final Monitor monitor;
3234

33-
/**
34-
* Constructs a new MonitorAwareRectangle
35-
*
36-
* @param x the x coordinate of the top left corner of the rectangle
37-
* @param y the y coordinate of the top left corner of the rectangle
38-
* @param width the width of the rectangle
39-
* @param height the height of the rectangle
40-
* @param monitor the monitor with whose context the rectangle is created
41-
*/
35+
public MonitorAwareRectangle(int x, int y, int width, int height) {
36+
this(x, y, width, height, null);
37+
}
38+
39+
public MonitorAwareRectangle(float x, float y, float width, float height) {
40+
this(x, y, width, height, null);
41+
}
42+
4243
public MonitorAwareRectangle(int x, int y, int width, int height, Monitor monitor) {
4344
super(x, y, width, height);
4445
this.monitor = monitor;
4546
}
4647

48+
MonitorAwareRectangle(float x, float y, float width, float height, Monitor monitor) {
49+
super(Math.round(x), Math.round(y), Math.round(width), Math.round(height));
50+
this.residualX = x - this.x;
51+
this.residualY = y - this.y;
52+
this.residualWidth = width - this.width;
53+
this.residualHeight = height - this.height;
54+
this.monitor = monitor;
55+
}
56+
4757
/**
4858
* {@return the monitor with whose context the instance is created}
4959
*/
@@ -60,10 +70,46 @@ public boolean equals(Object object) {
6070
public int hashCode() {
6171
return super.hashCode();
6272
}
63-
73+
6474
@Override
6575
public MonitorAwareRectangle clone() {
66-
return new MonitorAwareRectangle(x, y, width, height, monitor);
76+
return new MonitorAwareRectangle(getX(), getY(), getWidth(), getHeight(), monitor);
77+
}
78+
79+
public float getX() {
80+
return x + residualX;
81+
}
82+
83+
public float getY() {
84+
return y + residualY;
85+
}
86+
87+
public float getWidth() {
88+
return width + residualWidth;
89+
}
90+
91+
public float getHeight() {
92+
return height + residualHeight;
93+
}
94+
95+
public void setX(float x) {
96+
this.x = Math.round(x);
97+
this.residualX = x - this.x;
98+
}
99+
100+
public void setY(float y) {
101+
this.y = Math.round(y);
102+
this.residualY = y - this.y;
103+
}
104+
105+
public void setWidth(float width) {
106+
this.width = Math.round(width);
107+
this.residualWidth = width - this.width;
108+
}
109+
110+
public void setHeight(float height) {
111+
this.height = Math.round(height);
112+
this.residualHeight = height - this.height;
67113
}
68114

69-
}
115+
}

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/Rectangle.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ public Rectangle union (Rectangle rect) {
374374
*/
375375
public static Rectangle of(Point topLeft, int width, int height) {
376376
if (topLeft instanceof MonitorAwarePoint monitorAwareTopLeft) {
377-
return new MonitorAwareRectangle(topLeft.x, topLeft.y, width, height, monitorAwareTopLeft.getMonitor());
377+
return new MonitorAwareRectangle(monitorAwareTopLeft.getX(), monitorAwareTopLeft.getY(), width, height, monitorAwareTopLeft.getMonitor());
378378
}
379379
return new Rectangle(topLeft.x, topLeft.y, width, height);
380380
}

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/DPIUtil.java

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,11 @@ public static Point autoScaleDown(Point point) {
228228

229229
public static Point scaleDown(Point point, int zoom) {
230230
if (zoom == 100 || point == null) return point;
231+
MonitorAwarePoint fPoint = FloatAwareGeometryFactory.createFrom(point);
231232
float scaleFactor = getScalingFactor(zoom);
232-
Point scaledPoint = new Point (0,0);
233-
scaledPoint.x = Math.round (point.x / scaleFactor);
234-
scaledPoint.y = Math.round (point.y / scaleFactor);
235-
return scaledPoint;
233+
float scaledX = fPoint.getX() / scaleFactor;
234+
float scaledY = fPoint.getY() / scaleFactor;
235+
return new MonitorAwarePoint(scaledX, scaledY, fPoint.getMonitor());
236236
}
237237

238238
/**
@@ -255,16 +255,7 @@ public static Rectangle autoScaleDown(Rectangle rect) {
255255
}
256256

257257
public static Rectangle scaleDown(Rectangle rect, int zoom) {
258-
if (zoom == 100 || rect == null) return rect;
259-
Rectangle scaledRect = new Rectangle (0,0,0,0);
260-
Point scaledTopLeft = scaleDown(new Point (rect.x, rect.y), zoom);
261-
Point scaledBottomRight = scaleDown(new Point (rect.x + rect.width, rect.y + rect.height), zoom);
262-
263-
scaledRect.x = scaledTopLeft.x;
264-
scaledRect.y = scaledTopLeft.y;
265-
scaledRect.width = scaledBottomRight.x - scaledTopLeft.x;
266-
scaledRect.height = scaledBottomRight.y - scaledTopLeft.y;
267-
return scaledRect;
258+
return scaleBounds(rect, 100, zoom);
268259
}
269260
/**
270261
* Returns a new scaled down Rectangle if enabled for Drawable class.
@@ -333,13 +324,13 @@ public static boolean isSmoothScalingEnabled() {
333324
*/
334325
public static Rectangle scaleBounds (Rectangle rect, int targetZoom, int currentZoom) {
335326
if (rect == null || targetZoom == currentZoom) return rect;
327+
MonitorAwareRectangle fRect = FloatAwareGeometryFactory.createFrom(rect);
336328
float scaleFactor = ((float)targetZoom) / (float)currentZoom;
337-
Rectangle returnRect = new Rectangle (0,0,0,0);
338-
returnRect.x = Math.round (rect.x * scaleFactor);
339-
returnRect.y = Math.round (rect.y * scaleFactor);
340-
returnRect.width = Math.round (rect.width * scaleFactor);
341-
returnRect.height = Math.round (rect.height * scaleFactor);
342-
return returnRect;
329+
float scaledX = fRect.getX() * scaleFactor;
330+
float scaledY = fRect.getY() * scaleFactor;
331+
float scaledWidth = fRect.getWidth() * scaleFactor;
332+
float scaledHeight = fRect.getHeight() * scaleFactor;
333+
return new MonitorAwareRectangle(scaledX, scaledY, scaledWidth, scaledHeight);
343334
}
344335

345336
/**
@@ -436,11 +427,11 @@ public static Point autoScaleUp(Point point) {
436427

437428
public static Point scaleUp(Point point, int zoom) {
438429
if (zoom == 100 || point == null) return point;
430+
MonitorAwarePoint fPoint = FloatAwareGeometryFactory.createFrom(point);
439431
float scaleFactor = getScalingFactor(zoom);
440-
Point scaledPoint = new Point(0,0);
441-
scaledPoint.x = Math.round (point.x * scaleFactor);
442-
scaledPoint.y = Math.round (point.y * scaleFactor);
443-
return scaledPoint;
432+
float scaledX = fPoint.getX() * scaleFactor;
433+
float scaledY = fPoint.getY() * scaleFactor;
434+
return new MonitorAwarePoint(scaledX, scaledY, fPoint.getMonitor());
444435
}
445436

446437
/**
@@ -463,16 +454,7 @@ public static Rectangle autoScaleUp(Rectangle rect) {
463454
}
464455

465456
public static Rectangle scaleUp(Rectangle rect, int zoom) {
466-
if (zoom == 100 || rect == null) return rect;
467-
Rectangle scaledRect = new Rectangle(0,0,0,0);
468-
Point scaledTopLeft = scaleUp (new Point(rect.x, rect.y), zoom);
469-
Point scaledBottomRight = scaleUp (new Point(rect.x + rect.width, rect.y + rect.height), zoom);
470-
471-
scaledRect.x = scaledTopLeft.x;
472-
scaledRect.y = scaledTopLeft.y;
473-
scaledRect.width = scaledBottomRight.x - scaledTopLeft.x;
474-
scaledRect.height = scaledBottomRight.y - scaledTopLeft.y;
475-
return scaledRect;
457+
return scaleBounds(rect, zoom, 100);
476458
}
477459

478460
/**
@@ -751,4 +733,20 @@ public ImageData getImageData(int zoom) {
751733
return DPIUtil.scaleImageData(device, imageData, zoom, currentZoom);
752734
}
753735
}
736+
737+
private class FloatAwareGeometryFactory {
738+
static MonitorAwareRectangle createFrom(Rectangle rectangle) {
739+
if (rectangle instanceof MonitorAwareRectangle monitorAwareRectangle) {
740+
return monitorAwareRectangle;
741+
}
742+
return new MonitorAwareRectangle(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
743+
}
744+
745+
static MonitorAwarePoint createFrom(Point point) {
746+
if (point instanceof MonitorAwarePoint monitorAwarePoint) {
747+
return monitorAwarePoint;
748+
}
749+
return new MonitorAwarePoint(point.x, point.y);
750+
}
751+
}
754752
}

tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/DPIUtilTests.java

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ public void scaleUpPoint() {
296296
@Test
297297
public void scaleUpRectangle() {
298298
Rectangle valueAt200 = new Rectangle(100, 150, 10, 14);
299-
Rectangle valueAt150 = new Rectangle(75, 113, 8, 10);
299+
Rectangle valueAt150 = new Rectangle(75, 113, 8, 11);
300300
Rectangle valueAt100 = new Rectangle(50, 75, 5, 7);
301301

302302
Rectangle scaledValue = DPIUtil.autoScaleUp(valueAt100);
@@ -319,4 +319,40 @@ public void scaleUpRectangle() {
319319
scaledValue = DPIUtil.scaleUp((Device) null, valueAt100, 100);
320320
assertSame(valueAt100, scaledValue, "Scaling up Rectangle without zoom change with device failed");
321321
}
322+
323+
@Test
324+
public void scaleDownscaleUpRectangleInvertible() {
325+
int[] zooms = new int[] {25, 50, 75, 100, 125, 150, 175, 200, 225, 250, 275, 300, 325, 350, 375, 400};
326+
for (int zoom1 : zooms) {
327+
for (int zoom2 : zooms) {
328+
for (int i = 1; i <= 10000; i++) {
329+
Rectangle rect = new Rectangle(0, 0, i, i);
330+
Rectangle scaleDown = DPIUtil.scaleDown(rect, zoom1);
331+
Rectangle scaleUp = DPIUtil.scaleUp(scaleDown, zoom2);
332+
scaleDown = DPIUtil.scaleDown(scaleUp, zoom2);
333+
scaleUp = DPIUtil.scaleUp(scaleDown, zoom1);
334+
assertEquals(rect.width, scaleUp.width);
335+
assertEquals(rect.height, scaleUp.height);
336+
}
337+
}
338+
}
339+
}
340+
341+
@Test
342+
public void scaleDownscaleUpPointInvertible() {
343+
int[] zooms = new int[] {25, 50, 75, 100, 125, 150, 175, 200, 225, 250, 275, 300, 325, 350, 375, 400};
344+
for (int zoom1 : zooms) {
345+
for (int zoom2 : zooms) {
346+
for (int i = 1; i <= 10000; i++) {
347+
Point pt = new Point(i, i);
348+
Point scaleDown = DPIUtil.scaleDown(pt, zoom1);
349+
Point scaleUp = DPIUtil.scaleUp(scaleDown, zoom2);
350+
scaleDown = DPIUtil.scaleDown(scaleUp, zoom2);
351+
scaleUp = DPIUtil.scaleUp(scaleDown, zoom1);
352+
assertEquals(pt.x, scaleUp.x);
353+
assertEquals(pt.y, scaleUp.y);
354+
}
355+
}
356+
}
357+
}
322358
}

0 commit comments

Comments
 (0)