Skip to content

Commit c6ec000

Browse files
committed
Introduce OfFloat classes for better scaling #62
This commit introduces Rectangle.OfFloat and Point.OfFloat classes as an extension of Rectangle and Point respectively to improve scaling precision in the DPIUtil by using residuals obtained from rounding. Contributes to #62 and #128
1 parent a7e8867 commit c6ec000

File tree

4 files changed

+188
-40
lines changed

4 files changed

+188
-40
lines changed

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

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
* @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
4444
*/
4545

46-
public sealed class Point implements Serializable permits Point.WithMonitor {
46+
public sealed class Point implements Serializable permits Point.OfFloat {
4747

4848
/**
4949
* the x coordinate of the point
@@ -68,6 +68,8 @@ public Point (int x, int y) {
6868
this.y = y;
6969
}
7070

71+
private Point() {}
72+
7173
/**
7274
* Compares the argument to the receiver, and returns true
7375
* if they represent the <em>same</em> object using a class
@@ -120,14 +122,58 @@ public String toString () {
120122

121123
/**
122124
* Instances of this class represent {@link org.eclipse.swt.graphics.Point}
125+
* objects with the fields capable of storing more precise value in float.
126+
*
127+
* @since 3.130
128+
* @noreference This class is not intended to be referenced by clients
129+
*/
130+
public static sealed class OfFloat extends Point permits Point.WithMonitor {
131+
132+
private static final long serialVersionUID = -1862062276431597053L;
133+
134+
public float residualX, residualY;
135+
136+
public OfFloat(int x, int y) {
137+
super(x, y);
138+
}
139+
140+
public OfFloat(float x, float y) {
141+
super();
142+
this.x = Math.round(x);
143+
this.y = Math.round(y);
144+
this.residualX = x - this.x;
145+
this.residualY = y - this.y;
146+
}
147+
148+
public float getX() {
149+
return x + residualX;
150+
}
151+
152+
public float getY() {
153+
return y + residualY;
154+
}
155+
156+
public void setX(float x) {
157+
this.x = Math.round(x);
158+
this.residualX = x - this.x;
159+
}
160+
161+
public void setY(float y) {
162+
this.y = Math.round(y);
163+
this.residualY = y - this.y;
164+
}
165+
}
166+
167+
/**
168+
* Instances of this class represent {@link org.eclipse.swt.graphics.Point.OfFloat}
123169
* objects along with the context of the monitor in relation to which they are
124170
* placed on the display. The monitor awareness makes it easy to scale and
125171
* translate the points between pixels and points.
126172
*
127173
* @since 3.130
128174
* @noreference This class is not intended to be referenced by clients
129175
*/
130-
public static final class WithMonitor extends Point {
176+
public static final class WithMonitor extends Point.OfFloat {
131177

132178
private static final long serialVersionUID = 6077427420686999194L;
133179

@@ -155,4 +201,3 @@ public Monitor getMonitor() {
155201
}
156202

157203
}
158-

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

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
* @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
4747
*/
4848

49-
public sealed class Rectangle implements Serializable, Cloneable permits Rectangle.WithMonitor {
49+
public sealed class Rectangle implements Serializable, Cloneable permits Rectangle.OfFloat {
5050

5151
/**
5252
* the x coordinate of the rectangle
@@ -86,6 +86,8 @@ public Rectangle (int x, int y, int width, int height) {
8686
this.height = height;
8787
}
8888

89+
private Rectangle() {}
90+
8991
/**
9092
* Destructively replaces the x, y, width and height values
9193
* in the receiver with ones which represent the union of the
@@ -400,14 +402,81 @@ public Rectangle clone() {
400402

401403
/**
402404
* Instances of this class represent {@link org.eclipse.swt.graphics.Rectangle}
405+
* objects which supports values of Float type for it's fields
406+
*
407+
* @since 3.131
408+
* @noreference This class is not intended to be referenced by clients
409+
*/
410+
public static sealed class OfFloat extends Rectangle permits Rectangle.WithMonitor {
411+
412+
private static final long serialVersionUID = -3006999002677468391L;
413+
414+
private float residualX, residualY, residualWidth, residualHeight;
415+
416+
public OfFloat(int x, int y, int width, int height) {
417+
super(x, y, width, height);
418+
}
419+
420+
public OfFloat(float x, float y, float width, float height) {
421+
super();
422+
this.x = Math.round(x);
423+
this.y = Math.round(y);
424+
this.width = Math.round(width);
425+
this.height = Math.round(height);
426+
this.residualX = x - this.x;
427+
this.residualY = y - this.y;
428+
this.residualWidth = width - this.width;
429+
this.residualHeight = height - this.height;
430+
}
431+
432+
public float getX() {
433+
return x + residualX;
434+
}
435+
436+
public float getY() {
437+
return y + residualY;
438+
}
439+
440+
public float getWidth() {
441+
return width + residualWidth;
442+
}
443+
444+
public float getHeight() {
445+
return height + residualHeight;
446+
}
447+
448+
public void setX(float x) {
449+
this.x = Math.round(x);
450+
this.residualX = x - this.x;
451+
}
452+
453+
public void setY(float y) {
454+
this.y = Math.round(y);
455+
this.residualY = y - this.y;
456+
}
457+
458+
public void setWidth(float width) {
459+
this.width = Math.round(width);
460+
this.residualWidth = width - this.width;
461+
}
462+
463+
public void setHeight(float height) {
464+
this.height = Math.round(height);
465+
this.residualHeight = height - this.height;
466+
}
467+
468+
}
469+
470+
/**
471+
* Instances of this class represent {@link org.eclipse.swt.graphics.Rectangle.OfFloat}
403472
* objects along with the context of the monitor in relation to which they are
404473
* placed on the display. The monitor awareness makes it easy to scale and
405474
* translate the rectangles between pixels and points.
406475
*
407476
* @since 3.131
408477
* @noreference This class is not intended to be referenced by clients
409478
*/
410-
public static final class WithMonitor extends Rectangle {
479+
public static final class WithMonitor extends Rectangle.OfFloat {
411480

412481
private static final long serialVersionUID = 5041911840525116925L;
413482

@@ -440,4 +509,4 @@ public MonitorAwareRectangle clone() {
440509
}
441510

442511
}
443-
}
512+
}

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+
Point.OfFloat fPoint = FloatAwareGeometryFactory.createFloatAwarePoint(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 Point.OfFloat(scaledX, scaledY);
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+
Rectangle.OfFloat fRect = FloatAwareGeometryFactory.createFloatAwareRectangle(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 Rectangle.OfFloat(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+
Point.OfFloat fPoint = FloatAwareGeometryFactory.createFloatAwarePoint(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 Point.OfFloat(scaledX, scaledY);
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 Rectangle.OfFloat createFloatAwareRectangle(Rectangle rectangle) {
739+
if (rectangle instanceof Rectangle.OfFloat) {
740+
return (Rectangle.OfFloat) rectangle;
741+
}
742+
return new Rectangle.OfFloat(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
743+
}
744+
745+
static Point.OfFloat createFloatAwarePoint(Point point) {
746+
if (point instanceof Point.OfFloat) {
747+
return (Point.OfFloat) point;
748+
}
749+
return new Point.OfFloat(point.x, point.y);
750+
}
751+
}
754752
}

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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)