Skip to content

Commit db8608f

Browse files
amartya4256akoch-yatta
authored andcommitted
Win32DPIUtils:pointToPixel should return Point/Rectangle
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. Hence Win32DPIUtils:pointToPixel returns Point/Rectangle instead of Point.OfFloat/Rectangle.OfFloat contributes to #62
1 parent 7fa0622 commit db8608f

File tree

2 files changed

+34
-20
lines changed

2 files changed

+34
-20
lines changed

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,14 +232,19 @@ private static Point pointToPixel(Point point, int zoom, RoundingMode mode) {
232232
return pointToPixel(new Point.OfFloat(floatPoint.getX(), floatPoint.getY(), mode), zoom);
233233
}
234234

235-
private static Point.OfFloat pointToPixel(Point.OfFloat point, int zoom) {
235+
private static Point.OfFloat pointToPixelOfFloat(Point.OfFloat point, int zoom) {
236236
Point.OfFloat scaledPoint = point.clone();
237237
float scaleFactor = DPIUtil.getScalingFactor(zoom);
238238
scaledPoint.setX(point.getX() * scaleFactor);
239239
scaledPoint.setY(point.getY() * scaleFactor);
240240
return scaledPoint;
241241
}
242242

243+
private static Point pointToPixel(Point.OfFloat point, int zoom) {
244+
Point scaledPoint = pointToPixelOfFloat(point, zoom);
245+
return new Point(scaledPoint.x, scaledPoint.y);
246+
}
247+
243248
public static Point pointToPixelAsSize(Drawable drawable, Point point, int zoom) {
244249
if (drawable != null && !drawable.isAutoScalable()) return point;
245250
return pointToPixelAsSize(point, zoom);
@@ -276,13 +281,14 @@ private static Rectangle pointToPixel(Rectangle rect, int zoom, RoundingMode siz
276281
return scaleBounds(rect, zoom, 100);
277282
}
278283
Rectangle.OfFloat floatRect = Rectangle.OfFloat.from(rect);
279-
Point.OfFloat scaledTopLeft = pointToPixel(floatRect.getTopLeft(), zoom);
280-
Point.OfFloat scaledBottomRight = pointToPixel(floatRect.getBottomRight(), zoom);
284+
Point.OfFloat scaledTopLeft = pointToPixelOfFloat(floatRect.getTopLeft(), zoom);
285+
Point.OfFloat scaledBottomRight = pointToPixelOfFloat(floatRect.getBottomRight(), zoom);
281286
float scaledX = scaledTopLeft.x;
282287
float scaleyY = scaledTopLeft.y;
283288
float scaledWidth = scaledBottomRight.x - scaledTopLeft.x;
284289
float scaledHeight = scaledBottomRight.y - scaledTopLeft.y;
285-
return new Rectangle.OfFloat(scaledX, scaleyY, scaledWidth, scaledHeight, RoundingMode.ROUND, sizeRounding);
290+
Rectangle scaledRectangle = new Rectangle.OfFloat(scaledX, scaleyY, scaledWidth, scaledHeight, RoundingMode.ROUND, sizeRounding);
291+
return new Rectangle(scaledRectangle.x, scaledRectangle.y, scaledRectangle.width, scaledRectangle.height);
286292
}
287293

288294
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
@@ -216,15 +216,27 @@ public void scaleUpRectangle() {
216216

217217
@Test
218218
public void scaleDownscaleUpRectangleInvertible() {
219+
int[] zooms = new int[] {25, 50, 75, 100, 125, 150, 175, 200, 225, 250, 275, 300, 325, 350, 375, 400};
220+
for (int zoom : zooms) {
221+
for (int i = 1; i <= 10000; i++) {
222+
Rectangle rect = new Rectangle.OfFloat(0, 0, i, i);
223+
Rectangle scaleDown = Win32DPIUtils.pixelToPoint(rect, zoom);
224+
Rectangle scaleUp = Win32DPIUtils.pointToPixel(scaleDown, zoom);
225+
assertEquals(rect.width, scaleUp.width);
226+
assertEquals(rect.height, scaleUp.height);
227+
}
228+
}
229+
}
230+
231+
@Test
232+
public void scaleBoundsRectangleInvertible() {
219233
int[] zooms = new int[] {25, 50, 75, 100, 125, 150, 175, 200, 225, 250, 275, 300, 325, 350, 375, 400};
220234
for (int zoom1 : zooms) {
221235
for (int zoom2 : zooms) {
222236
for (int i = 1; i <= 10000; i++) {
223237
Rectangle rect = new Rectangle.OfFloat(0, 0, i, i);
224-
Rectangle scaleDown = Win32DPIUtils.pixelToPoint(rect, zoom1);
225-
Rectangle scaleUp = Win32DPIUtils.pointToPixel(scaleDown, zoom2);
226-
scaleDown = Win32DPIUtils.pixelToPoint(scaleUp, zoom2);
227-
scaleUp = Win32DPIUtils.pointToPixel(scaleDown, zoom1);
238+
Rectangle scaleDown = Win32DPIUtils.scaleBounds(rect, zoom1, zoom2);
239+
Rectangle scaleUp = Win32DPIUtils.scaleBounds(scaleDown, zoom2, zoom1);
228240
assertEquals(rect.width, scaleUp.width);
229241
assertEquals(rect.height, scaleUp.height);
230242
}
@@ -234,18 +246,14 @@ public void scaleDownscaleUpRectangleInvertible() {
234246

235247
@Test
236248
public void scaleDownscaleUpPointInvertible() {
237-
int[] zooms = new int[] {25, 50, 75, 100, 125, 150, 175, 200, 225, 250, 275, 300, 325, 350, 375, 400};
238-
for (int zoom1 : zooms) {
239-
for (int zoom2 : zooms) {
240-
for (int i = 1; i <= 10000; i++) {
241-
Point pt = new Point(i, i);
242-
Point scaleDown = Win32DPIUtils.pixelToPointAsSize(pt, zoom1);
243-
Point scaleUp = Win32DPIUtils.pointToPixelAsLocation(scaleDown, zoom2);
244-
scaleDown = Win32DPIUtils.pixelToPointAsSize(scaleUp, zoom2);
245-
scaleUp = Win32DPIUtils.pointToPixelAsLocation(scaleDown, zoom1);
246-
assertEquals(pt.x, scaleUp.x);
247-
assertEquals(pt.y, scaleUp.y);
248-
}
249+
int[] zooms = new int[] {100, 125, 150, 175, 200, 225, 250, 275, 300, 325, 350, 375, 400};
250+
for (int zoom : zooms) {
251+
for (int i = 1; i <= 10000; i++) {
252+
Point pt = new Point(i, i);
253+
Point scaleDown = Win32DPIUtils.pixelToPointAsLocation(pt, zoom);
254+
Point scaleUp = Win32DPIUtils.pointToPixelAsSize(scaleDown, zoom);
255+
assertEquals(pt.x, scaleUp.x);
256+
assertEquals(pt.y, scaleUp.y);
249257
}
250258
}
251259
}

0 commit comments

Comments
 (0)