Skip to content

Commit 06813ee

Browse files
author
Thomas Singer
committed
#1596: [Win32] Drawing produces unsymmetric results without swt.autoScale=false for 4k monitor with zooming
When drawing a vertical line at x == 1 with 200% zoom level (HiDPI), MacOS draws it on the physical pixel coordinates 2 and 3 while on Windows (with the existing code) it was drawn at 1 and 2. In other words: the existing code drew lines 1 pixel too far left/top. This commit should resolve this.
1 parent 96d4f16 commit 06813ee

File tree

3 files changed

+52
-38
lines changed

3 files changed

+52
-38
lines changed

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/DPIUtil.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,10 @@ public static int autoScaleUp(Drawable drawable, int size) {
400400
return scaleUp(drawable, size, deviceZoom);
401401
}
402402

403+
public static int scaleUpXY(Drawable drawable, int size, int zoom) {
404+
return scaleUp(drawable, size + 1, zoom) - 1;
405+
}
406+
403407
public static int scaleUp(Drawable drawable, int size, int zoom) {
404408
if (drawable != null && !drawable.isAutoScalable()) return size;
405409
return scaleUp (size, zoom);
@@ -419,6 +423,10 @@ public static float autoScaleUp(Drawable drawable, float size) {
419423
return scaleUp(drawable, size, deviceZoom);
420424
}
421425

426+
public static float scaleUpXY(Drawable drawable, float size, int zoom) {
427+
return scaleUp(drawable, size + 1, zoom) - 1;
428+
}
429+
422430
public static float scaleUp(Drawable drawable, float size, int zoom) {
423431
if (drawable != null && !drawable.isAutoScalable()) return size;
424432
return scaleUp (size, zoom);

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/GC.java

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1644,10 +1644,10 @@ void drawBitmapColor(Image srcImage, int srcX, int srcY, int srcWidth, int srcHe
16441644
*/
16451645
public void drawLine (int x1, int y1, int x2, int y2) {
16461646
int deviceZoom = getZoom();
1647-
x1 = DPIUtil.scaleUp (drawable, x1, deviceZoom);
1648-
x2 = DPIUtil.scaleUp (drawable, x2, deviceZoom);
1649-
y1 = DPIUtil.scaleUp (drawable, y1, deviceZoom);
1650-
y2 = DPIUtil.scaleUp (drawable, y2, deviceZoom);
1647+
x1 = scaleUpXY (x1, deviceZoom);
1648+
x2 = scaleUpXY (x2, deviceZoom);
1649+
y1 = scaleUpXY (y1, deviceZoom);
1650+
y2 = scaleUpXY (y2, deviceZoom);
16511651
drawLineInPixels(x1, y1, x2, y2);
16521652
}
16531653

@@ -1911,8 +1911,8 @@ void drawPolylineInPixels(int[] pointArray) {
19111911
*/
19121912
public void drawRectangle (int x, int y, int width, int height) {
19131913
int deviceZoom = getZoom();
1914-
x = DPIUtil.scaleUp (drawable, x, deviceZoom);
1915-
y = DPIUtil.scaleUp (drawable, y, deviceZoom);
1914+
x = scaleUpXY (x, deviceZoom);
1915+
y = scaleUpXY (y, deviceZoom);
19161916
width = DPIUtil.scaleUp (drawable, width, deviceZoom);
19171917
height = DPIUtil.scaleUp (drawable, height, deviceZoom);
19181918
drawRectangleInPixels(x, y, width, height);
@@ -1997,8 +1997,8 @@ public void drawRectangle (Rectangle rect) {
19971997
*/
19981998
public void drawRoundRectangle (int x, int y, int width, int height, int arcWidth, int arcHeight) {
19991999
int deviceZoom = getZoom();
2000-
x = DPIUtil.scaleUp (drawable, x, deviceZoom);
2001-
y = DPIUtil.scaleUp (drawable, y, deviceZoom);
2000+
x = scaleUpXY (x, deviceZoom);
2001+
y = scaleUpXY (y, deviceZoom);
20022002
width = DPIUtil.scaleUp (drawable, width, deviceZoom);
20032003
height = DPIUtil.scaleUp (drawable, height, deviceZoom);
20042004
arcWidth = DPIUtil.scaleUp (drawable, arcWidth, deviceZoom);
@@ -2095,8 +2095,8 @@ void drawRoundRectangleGdip (long gdipGraphics, long pen, int x, int y, int widt
20952095
*/
20962096
public void drawString (String string, int x, int y) {
20972097
int deviceZoom = getZoom();
2098-
x = DPIUtil.scaleUp(drawable, x, deviceZoom);
2099-
y = DPIUtil.scaleUp(drawable, y, deviceZoom);
2098+
x = scaleUpXY(x, deviceZoom);
2099+
y = scaleUpXY(y, deviceZoom);
21002100
drawStringInPixels(string, x, y, false);
21012101
}
21022102

@@ -2129,8 +2129,8 @@ public void drawString (String string, int x, int y) {
21292129
*/
21302130
public void drawString (String string, int x, int y, boolean isTransparent) {
21312131
int deviceZoom = getZoom();
2132-
x = DPIUtil.scaleUp(drawable, x, deviceZoom);
2133-
y = DPIUtil.scaleUp(drawable, y, deviceZoom);
2132+
x = scaleUpXY(x, deviceZoom);
2133+
y = scaleUpXY(y, deviceZoom);
21342134
drawStringInPixels(string, x, y, isTransparent);
21352135
}
21362136

@@ -2221,8 +2221,8 @@ void drawStringInPixels (String string, int x, int y, boolean isTransparent) {
22212221
*/
22222222
public void drawText (String string, int x, int y) {
22232223
int deviceZoom = getZoom();
2224-
x = DPIUtil.scaleUp(drawable, x, deviceZoom);
2225-
y = DPIUtil.scaleUp(drawable, y, deviceZoom);
2224+
x = scaleUpXY(x, deviceZoom);
2225+
y = scaleUpXY(y, deviceZoom);
22262226
drawTextInPixels(string, x, y);
22272227
}
22282228

@@ -2256,8 +2256,8 @@ void drawTextInPixels (String string, int x, int y) {
22562256
*/
22572257
public void drawText (String string, int x, int y, boolean isTransparent) {
22582258
int deviceZoom = getZoom();
2259-
x = DPIUtil.scaleUp(drawable, x, deviceZoom);
2260-
y = DPIUtil.scaleUp(drawable, y, deviceZoom);
2259+
x = scaleUpXY(x, deviceZoom);
2260+
y = scaleUpXY(y, deviceZoom);
22612261
drawTextInPixels(string, x, y, isTransparent);
22622262
}
22632263

@@ -2308,8 +2308,8 @@ void drawTextInPixels (String string, int x, int y, boolean isTransparent) {
23082308
*/
23092309
public void drawText (String string, int x, int y, int flags) {
23102310
int deviceZoom = getZoom();
2311-
x = DPIUtil.scaleUp(drawable, x, deviceZoom);
2312-
y = DPIUtil.scaleUp(drawable, y, deviceZoom);
2311+
x = scaleUpXY(x, deviceZoom);
2312+
y = scaleUpXY(y, deviceZoom);
23132313
drawTextInPixels(string, x, y, flags);
23142314
}
23152315

@@ -2688,8 +2688,8 @@ public boolean equals (Object object) {
26882688
*/
26892689
public void fillArc (int x, int y, int width, int height, int startAngle, int arcAngle) {
26902690
int deviceZoom = getZoom();
2691-
x = DPIUtil.scaleUp (drawable, x, deviceZoom);
2692-
y = DPIUtil.scaleUp (drawable, y, deviceZoom);
2691+
x = scaleUpXY (x, deviceZoom);
2692+
y = scaleUpXY (y, deviceZoom);
26932693
width = DPIUtil.scaleUp (drawable, width, deviceZoom);
26942694
height = DPIUtil.scaleUp (drawable, height, deviceZoom);
26952695
fillArcInPixels(x, y, width, height, startAngle, arcAngle);
@@ -5163,4 +5163,7 @@ private int getZoom() {
51635163
return DPIUtil.getZoomForAutoscaleProperty(data.nativeZoom);
51645164
}
51655165

5166+
private int scaleUpXY(int xy, int deviceZoom) {
5167+
return DPIUtil.scaleUpXY (drawable, xy, deviceZoom);
5168+
}
51665169
}

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Path.java

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,8 @@ private Path(Device device, PathData data, int zoom) {
209209
public void addArc (float x, float y, float width, float height, float startAngle, float arcAngle) {
210210
if (width == 0 || height == 0 || arcAngle == 0) return;
211211
Drawable drawable = getDevice();
212-
x = DPIUtil.scaleUp(drawable, x, initialZoom);
213-
y = DPIUtil.scaleUp(drawable, y, initialZoom);
212+
x = scaleUpXY(drawable, x);
213+
y = scaleUpXY(drawable, y);
214214
width = DPIUtil.scaleUp(drawable, width, initialZoom);
215215
height = DPIUtil.scaleUp(drawable, height, initialZoom);
216216
addArcInPixels(x, y, width, height, startAngle, arcAngle);
@@ -312,8 +312,8 @@ void addRectangleInPixels(float x, float y, float width, float height) {
312312
*/
313313
public void addString (String string, float x, float y, Font font) {
314314
Drawable drawable = getDevice();
315-
x = DPIUtil.scaleUp(drawable, x, initialZoom);
316-
y = DPIUtil.scaleUp(drawable, y, initialZoom);
315+
x = scaleUpXY(drawable, x);
316+
y = scaleUpXY(drawable, y);
317317
addStringInPixels(string, x, y, font);
318318
}
319319
void addStringInPixels(String string, float x, float y, Font font) {
@@ -384,8 +384,8 @@ public void close() {
384384
*/
385385
public boolean contains (float x, float y, GC gc, boolean outline) {
386386
Drawable drawable = getDevice();
387-
x = DPIUtil.scaleUp(drawable, x, initialZoom);
388-
y = DPIUtil.scaleUp(drawable, y, initialZoom);
387+
x = scaleUpXY(drawable, x);
388+
y = scaleUpXY(drawable, y);
389389
return containsInPixels(x, y, gc, outline);
390390
}
391391
boolean containsInPixels(float x, float y, GC gc, boolean outline) {
@@ -420,12 +420,12 @@ boolean containsInPixels(float x, float y, GC gc, boolean outline) {
420420
*/
421421
public void cubicTo (float cx1, float cy1, float cx2, float cy2, float x, float y) {
422422
Drawable drawable = getDevice();
423-
cx1 = DPIUtil.scaleUp(drawable, cx1, initialZoom);
424-
cy1 = DPIUtil.scaleUp(drawable, cy1, initialZoom);
425-
cx2 = DPIUtil.scaleUp(drawable, cx2, initialZoom);
426-
cy2 = DPIUtil.scaleUp(drawable, cy2, initialZoom);
427-
x = DPIUtil.scaleUp(drawable, x, initialZoom);
428-
y = DPIUtil.scaleUp(drawable, y, initialZoom);
423+
cx1 = scaleUpXY(drawable, cx1);
424+
cy1 = scaleUpXY(drawable, cy1);
425+
cx2 = scaleUpXY(drawable, cx2);
426+
cy2 = scaleUpXY(drawable, cy2);
427+
x = scaleUpXY(drawable, x);
428+
y = scaleUpXY(drawable, y);
429429
cubicToInPixels(cx1, cy1, cx2, cy2, x, y);
430430
}
431431

@@ -591,7 +591,7 @@ PathData getPathDataInPixels() {
591591
*/
592592
public void lineTo (float x, float y) {
593593
Drawable drawable = getDevice();
594-
lineToInPixels(DPIUtil.scaleUp(drawable, x, initialZoom), DPIUtil.scaleUp(drawable, y, initialZoom));
594+
lineToInPixels(scaleUpXY(drawable, x), scaleUpXY(drawable, y));
595595
}
596596

597597
void lineToInPixels(float x, float y) {
@@ -656,7 +656,7 @@ public boolean isDisposed() {
656656
*/
657657
public void moveTo (float x, float y) {
658658
Drawable drawable = getDevice();
659-
moveToInPixels(DPIUtil.scaleUp(drawable, x, initialZoom), DPIUtil.scaleUp(drawable, y, initialZoom));
659+
moveToInPixels(scaleUpXY(drawable, x), scaleUpXY(drawable, y));
660660
}
661661

662662
void moveToInPixels(float x, float y) {
@@ -680,10 +680,10 @@ void moveToInPixels(float x, float y) {
680680
*/
681681
public void quadTo (float cx, float cy, float x, float y) {
682682
Drawable drawable = getDevice();
683-
cx = DPIUtil.scaleUp(drawable, cx, initialZoom);
684-
cy = DPIUtil.scaleUp(drawable, cy, initialZoom);
685-
x = DPIUtil.scaleUp(drawable, x, initialZoom);
686-
y = DPIUtil.scaleUp(drawable, y, initialZoom);
683+
cx = scaleUpXY(drawable, cx);
684+
cy = scaleUpXY(drawable, cy);
685+
x = scaleUpXY(drawable, x);
686+
y = scaleUpXY(drawable, y);
687687
quadToInPixels(cx, cy, x, y);
688688
}
689689

@@ -718,4 +718,7 @@ long getHandle(int zoom) {
718718
return zoomLevelToHandle.get(zoom);
719719
}
720720

721+
private float scaleUpXY(Drawable drawable, float xy) {
722+
return DPIUtil.scaleUpXY(drawable, xy, initialZoom);
723+
}
721724
}

0 commit comments

Comments
 (0)