Skip to content

Commit 1f6354b

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 66ba81c commit 1f6354b

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 permits Rectangle.WithMonitor {
49+
public sealed class Rectangle implements Serializable 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
@@ -359,14 +361,81 @@ public Rectangle union (Rectangle rect) {
359361

360362
/**
361363
* Instances of this class represent {@link org.eclipse.swt.graphics.Rectangle}
364+
* objects which supports values of Float type for it's fields
365+
*
366+
* @since 3.130
367+
* @noreference This class is not intended to be referenced by clients
368+
*/
369+
public static sealed class OfFloat extends Rectangle permits Rectangle.WithMonitor {
370+
371+
private static final long serialVersionUID = -3006999002677468391L;
372+
373+
private float residualX, residualY, residualWidth, residualHeight;
374+
375+
public OfFloat(int x, int y, int width, int height) {
376+
super(x, y, width, height);
377+
}
378+
379+
public OfFloat(float x, float y, float width, float height) {
380+
super();
381+
this.x = Math.round(x);
382+
this.y = Math.round(y);
383+
this.width = Math.round(width);
384+
this.height = Math.round(height);
385+
this.residualX = x - this.x;
386+
this.residualY = y - this.y;
387+
this.residualWidth = width - this.width;
388+
this.residualHeight = height - this.height;
389+
}
390+
391+
public float getX() {
392+
return x + residualX;
393+
}
394+
395+
public float getY() {
396+
return y + residualY;
397+
}
398+
399+
public float getWidth() {
400+
return width + residualWidth;
401+
}
402+
403+
public float getHeight() {
404+
return height + residualHeight;
405+
}
406+
407+
public void setX(float x) {
408+
this.x = Math.round(x);
409+
this.residualX = x - this.x;
410+
}
411+
412+
public void setY(float y) {
413+
this.y = Math.round(y);
414+
this.residualY = y - this.y;
415+
}
416+
417+
public void setWidth(float width) {
418+
this.width = Math.round(width);
419+
this.residualWidth = width - this.width;
420+
}
421+
422+
public void setHeight(float height) {
423+
this.height = Math.round(height);
424+
this.residualHeight = height - this.height;
425+
}
426+
427+
}
428+
429+
/**
430+
* Instances of this class represent {@link org.eclipse.swt.graphics.Rectangle.OfFloat}
362431
* objects along with the context of the monitor in relation to which they are
363432
* placed on the display. The monitor awareness makes it easy to scale and
364433
* translate the rectangles between pixels and points.
365434
*
366435
* @since 3.130
367436
* @noreference This class is not intended to be referenced by clients
368437
*/
369-
public static final class WithMonitor extends Rectangle {
438+
public static final class WithMonitor extends Rectangle.OfFloat {
370439

371440
private static final long serialVersionUID = 5041911840525116925L;
372441

@@ -395,4 +464,4 @@ public Monitor getMonitor() {
395464

396465
}
397466

398-
}
467+
}

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

0 commit comments

Comments
 (0)