Skip to content

Commit ca6ef3a

Browse files
committed
Win32DPIUtils:pointToPixel shouldn't have residual
This commit contributes to scaling points and rectangles from point to pixels while making sure they do not have any residuals since the OS doesn't support sub-pixel drawing. contributes to #62
1 parent 13934ee commit ca6ef3a

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/Win32DPIUtils.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,8 @@ public static Point pointToPixel(Point point, int zoom) {
205205
float scaleFactor = DPIUtil.getScalingFactor(zoom);
206206
float scaledX = fPoint.getX() * scaleFactor;
207207
float scaledY = fPoint.getY() * scaleFactor;
208-
return new Point.OfFloat(scaledX, scaledY);
208+
Point scaledPoint = new Point.OfFloat(scaledX, scaledY);
209+
return new Point.OfFloat(scaledPoint.x, scaledPoint.y);
209210
}
210211

211212
public static Point pointToPixel(Drawable drawable, Point point, int zoom) {
@@ -228,7 +229,8 @@ public static Rectangle pointToPixel(Rectangle rect, int zoom) {
228229
}
229230

230231
private static Rectangle pointToPixel(Rectangle.OfFloat rect, int zoom) {
231-
return scaleBounds(rect, zoom, 100);
232+
Rectangle scaledRectangle = scaleBounds(rect, zoom, 100);
233+
return new Rectangle.OfFloat(scaledRectangle.x, scaledRectangle.y, scaledRectangle.width, scaledRectangle.height);
232234
}
233235

234236
public static Rectangle pointToPixel(Drawable drawable, Rectangle rect, int zoom) {

tests/org.eclipse.swt.tests.win32/JUnit Tests/org/eclipse/swt/tests/win32/Win32DPIUtilTests.java

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -222,15 +222,27 @@ public void scaleUpRectangle() {
222222

223223
@Test
224224
public void scaleDownscaleUpRectangleInvertible() {
225+
int[] zooms = new int[] {25, 50, 75, 100, 125, 150, 175, 200, 225, 250, 275, 300, 325, 350, 375, 400};
226+
for (int zoom : zooms) {
227+
for (int i = 1; i <= 10000; i++) {
228+
Rectangle rect = new Rectangle.OfFloat(0, 0, i, i);
229+
Rectangle scaleDown = Win32DPIUtils.pixelToPoint(rect, zoom);
230+
Rectangle scaleUp = Win32DPIUtils.pointToPixel(scaleDown, zoom);
231+
assertEquals(rect.width, scaleUp.width);
232+
assertEquals(rect.height, scaleUp.height);
233+
}
234+
}
235+
}
236+
237+
@Test
238+
public void scaleBoundsRectangleInvertible() {
225239
int[] zooms = new int[] {25, 50, 75, 100, 125, 150, 175, 200, 225, 250, 275, 300, 325, 350, 375, 400};
226240
for (int zoom1 : zooms) {
227241
for (int zoom2 : zooms) {
228242
for (int i = 1; i <= 10000; i++) {
229243
Rectangle rect = new Rectangle.OfFloat(0, 0, i, i);
230-
Rectangle scaleDown = Win32DPIUtils.pixelToPoint(rect, zoom1);
231-
Rectangle scaleUp = Win32DPIUtils.pointToPixel(scaleDown, zoom2);
232-
scaleDown = Win32DPIUtils.pixelToPoint(scaleUp, zoom2);
233-
scaleUp = Win32DPIUtils.pointToPixel(scaleDown, zoom1);
244+
Rectangle scaleDown = Win32DPIUtils.scaleBounds(rect, zoom1, zoom2);
245+
Rectangle scaleUp = Win32DPIUtils.scaleBounds(scaleDown, zoom2, zoom1);
234246
assertEquals(rect.width, scaleUp.width);
235247
assertEquals(rect.height, scaleUp.height);
236248
}
@@ -240,18 +252,14 @@ public void scaleDownscaleUpRectangleInvertible() {
240252

241253
@Test
242254
public void scaleDownscaleUpPointInvertible() {
243-
int[] zooms = new int[] {25, 50, 75, 100, 125, 150, 175, 200, 225, 250, 275, 300, 325, 350, 375, 400};
244-
for (int zoom1 : zooms) {
245-
for (int zoom2 : zooms) {
246-
for (int i = 1; i <= 10000; i++) {
247-
Point pt = new Point(i, i);
248-
Point scaleDown = Win32DPIUtils.pixelToPoint(pt, zoom1);
249-
Point scaleUp = Win32DPIUtils.pointToPixel(scaleDown, zoom2);
250-
scaleDown = Win32DPIUtils.pixelToPoint(scaleUp, zoom2);
251-
scaleUp = Win32DPIUtils.pointToPixel(scaleDown, zoom1);
252-
assertEquals(pt.x, scaleUp.x);
253-
assertEquals(pt.y, scaleUp.y);
254-
}
255+
int[] zooms = new int[] {100, 125, 150, 175, 200, 225, 250, 275, 300, 325, 350, 375, 400};
256+
for (int zoom : zooms) {
257+
for (int i = 1; i <= 10000; i++) {
258+
Point pt = new Point(i, i);
259+
Point scaleDown = Win32DPIUtils.pixelToPoint(pt, zoom);
260+
Point scaleUp = Win32DPIUtils.pointToPixel(scaleDown, zoom);
261+
assertEquals(pt.x, scaleUp.x);
262+
assertEquals(pt.y, scaleUp.y);
255263
}
256264
}
257265
}

0 commit comments

Comments
 (0)