Skip to content

Commit 341a86b

Browse files
amartya4256fedejeanne
authored andcommitted
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 810cbcc commit 341a86b

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
@@ -225,7 +225,8 @@ public static Point pointToPixel(Point point, int zoom) {
225225
float scaleFactor = DPIUtil.getScalingFactor(zoom);
226226
float scaledX = fPoint.getX() * scaleFactor;
227227
float scaledY = fPoint.getY() * scaleFactor;
228-
return new Point.OfFloat(scaledX, scaledY);
228+
Point scaledPoint = new Point.OfFloat(scaledX, scaledY);
229+
return new Point.OfFloat(scaledPoint.x, scaledPoint.y);
229230
}
230231

231232
public static Point pointToPixel(Drawable drawable, Point point, int zoom) {
@@ -248,7 +249,8 @@ public static Rectangle pointToPixel(Rectangle rect, int zoom) {
248249
}
249250

250251
private static Rectangle pointToPixel(Rectangle.OfFloat rect, int zoom) {
251-
return scaleBounds(rect, zoom, 100);
252+
Rectangle scaledRectangle = scaleBounds(rect, zoom, 100);
253+
return new Rectangle.OfFloat(scaledRectangle.x, scaledRectangle.y, scaledRectangle.width, scaledRectangle.height);
252254
}
253255

254256
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)