Skip to content

Commit 7be1451

Browse files
Deprecating Display#getDPI
Having the scale factor being based on the screen DPI leads to unexpected result e.g. Image too big/small. Having a screen dpi independent factor leads to consistent results
1 parent 48e5878 commit 7be1451

File tree

10 files changed

+53
-17
lines changed

10 files changed

+53
-17
lines changed

bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/Device.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
package org.eclipse.swt.graphics;
1515

1616
import org.eclipse.swt.*;
17-
import org.eclipse.swt.internal.ExceptionStash;
17+
import org.eclipse.swt.internal.*;
1818
import org.eclipse.swt.internal.cocoa.*;
1919

2020
/**
@@ -244,7 +244,7 @@ public void dispose () {
244244
}
245245

246246
destroy ();
247-
disposed = true;
247+
disposed = true;
248248
if (tracking) {
249249
synchronized (trackingLock) {
250250
printErrors ();
@@ -391,7 +391,18 @@ public int getDepth () {
391391
* @exception SWTException <ul>
392392
* <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li>
393393
* </ul>
394+
*
395+
* @deprecated <p>This method returns a single global DPI value
396+
* that does not reflect per-monitor DPI settings on modern operating systems.
397+
* In environments with different scaling factors across monitors, it may provide
398+
* a misleading or meaningless result, as it does not correspond to the actual DPI
399+
* of any specific monitor.</p>
400+
*
401+
* <p>Note: While deprecated for general {@code Device} instances like {@code Display},
402+
* this method may still be validly used when called on a {@code Printer} instance,
403+
* where a single global DPI value is meaningful and expected.</p>
394404
*/
405+
@Deprecated
395406
public Point getDPI () {
396407
checkDevice ();
397408
return getScreenDPI();

bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/Device.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,18 @@ public int getDepth () {
455455
* @exception SWTException <ul>
456456
* <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li>
457457
* </ul>
458+
*
459+
* @deprecated <p>This method returns a single global DPI value
460+
* that does not reflect per-monitor DPI settings on modern operating systems.
461+
* In environments with different scaling factors across monitors, it may provide
462+
* a misleading or meaningless result, as it does not correspond to the actual DPI
463+
* of any specific monitor.</p>
464+
*
465+
* <p>Note: While deprecated for general {@code Device} instances like {@code Display},
466+
* this method may still be validly used when called on a {@code Printer} instance,
467+
* where a single global DPI value is meaningful and expected.</p>
458468
*/
469+
@Deprecated
459470
public Point getDPI () {
460471
checkDevice ();
461472
return getScreenDPI();
@@ -641,6 +652,7 @@ public boolean getWarnings () {
641652
* @see #create
642653
*/
643654
protected void init () {
655+
final int DOTS_PER_INCH = 96;
644656
if (debug) {
645657
if (xDisplay != 0) {
646658
/* Create the warning and error callbacks */
@@ -723,10 +735,9 @@ protected void init () {
723735
}
724736
}
725737
defaultFont = OS.pango_font_description_copy (defaultFont);
726-
Point dpi = getDPI(), screenDPI = getScreenDPI();
727-
if (dpi.y != screenDPI.y) {
738+
if (this.dpi.y != DOTS_PER_INCH) {
728739
int size = OS.pango_font_description_get_size(defaultFont);
729-
OS.pango_font_description_set_size(defaultFont, size * dpi.y / screenDPI.y);
740+
OS.pango_font_description_set_size(defaultFont, size * this.dpi.y / DOTS_PER_INCH);
730741
}
731742
systemFont = Font.gtk_new (this, defaultFont);
732743

bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/Font.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -184,15 +184,15 @@ public boolean equals(Object object) {
184184
*/
185185
public FontData[] getFontData() {
186186
if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
187-
187+
final int DOTS_PER_INCH = 96;
188188
long family = OS.pango_font_description_get_family(handle);
189189
int length = C.strlen(family);
190190
byte[] buffer = new byte[length];
191191
C.memmove(buffer, family, length);
192192
String name = new String(Converter.mbcsToWcs(buffer));
193193
float height = (float)OS.pango_font_description_get_size(handle) / OS.PANGO_SCALE;
194-
Point dpi = device.dpi, screenDPI = device.getScreenDPI();
195-
float size = height * screenDPI.y / dpi.y;
194+
Point dpi = device.dpi;
195+
float size = height * DOTS_PER_INCH / dpi.y;
196196
int pangoStyle = OS.pango_font_description_get_style(handle);
197197
int pangoWeight = OS.pango_font_description_get_weight(handle);
198198
int style = SWT.NORMAL;
@@ -254,8 +254,9 @@ public int hashCode() {
254254
void init(String name, float height, int style, byte[] fontString) {
255255
if (name == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
256256
if (height < 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
257-
Point dpi = device.dpi, screenDPI = device.getScreenDPI();
258-
float size = height * dpi.y / screenDPI.y;
257+
final int DOTS_PER_INCH = 96;
258+
Point dpi = device.dpi;
259+
float size = height * dpi.y / DOTS_PER_INCH;
259260
if (fontString != null) {
260261
handle = OS.pango_font_description_from_string (fontString);
261262
if (handle == 0) SWT.error(SWT.ERROR_NO_HANDLES);

bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Display.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5043,7 +5043,8 @@ String debugInfoForIndex(long index) {
50435043
}
50445044

50455045
void dpiChanged(int newScaleFactor) {
5046-
DPIUtil.setDeviceZoom (DPIUtil.mapDPIToZoom(getDPI().x * newScaleFactor));
5046+
final int DOTS_PER_INCH = 96;
5047+
DPIUtil.setDeviceZoom (DPIUtil.mapDPIToZoom(DOTS_PER_INCH * newScaleFactor));
50475048
Shell[] shells = getShells();
50485049
for (int i = 0; i < shells.length; i++) {
50495050
shells[i].layout(true, true);

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,18 @@ public int getDepth () {
519519
* @exception SWTException <ul>
520520
* <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li>
521521
* </ul>
522+
*
523+
* @deprecated <p>This method returns a single global DPI value
524+
* that does not reflect per-monitor DPI settings on modern operating systems.
525+
* In environments with different scaling factors across monitors, it may provide
526+
* a misleading or meaningless result, as it does not correspond to the actual DPI
527+
* of any specific monitor.</p>
528+
*
529+
* <p>Note: While deprecated for general {@code Device} instances like {@code Display},
530+
* this method may still be validly used when called on a {@code Printer} instance,
531+
* where a single global DPI value is meaningful and expected.</p>
522532
*/
533+
@Deprecated
523534
public Point getDPI () {
524535
checkDevice ();
525536
long hDC = internal_new_GC (null);

examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/imageanalyzer/ImageAnalyzer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,16 +1165,16 @@ void menuPrint() {
11651165
if (image == null) return;
11661166

11671167
try {
1168+
final int DOTS_PER_INCH = 96;
11681169
// Ask the user to specify the printer.
11691170
PrintDialog dialog = new PrintDialog(shell, SWT.NONE);
11701171
if (printerData != null) dialog.setPrinterData(printerData);
11711172
printerData = dialog.open();
11721173
if (printerData == null) return;
11731174

11741175
Printer printer = new Printer(printerData);
1175-
Point screenDPI = display.getDPI();
11761176
Point printerDPI = printer.getDPI();
1177-
int scaleFactor = printerDPI.x / screenDPI.x;
1177+
int scaleFactor = printerDPI.x / DOTS_PER_INCH;
11781178
Rectangle trim = printer.computeTrim(0, 0, 0, 0);
11791179
if (printer.startJob(currentName)) {
11801180
if (printer.startPage()) {

examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet361.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ public void getName(AccessibleEvent e) {
146146
}
147147

148148
private static void performPrintAction(final Display display, final Shell shell) {
149+
final int DOTS_PER_INCH = 96;
149150
Rectangle r = composite.getBounds();
150151
Point p = shell.toDisplay(r.x, r.y);
151152
org.eclipse.swt.graphics.Image snapshotImage
@@ -159,9 +160,8 @@ private static void performPrintAction(final Display display, final Shell shell)
159160
data = dialog.open();
160161
if (data != null) {
161162
Printer printer = new Printer(data);
162-
Point screenDPI = display.getDPI();
163163
Point printerDPI = printer.getDPI();
164-
int scaleFactor = printerDPI.x / screenDPI.x;
164+
int scaleFactor = printerDPI.x / DOTS_PER_INCH;
165165
Rectangle trim = printer.computeTrim(0, 0, 0, 0);
166166
if (printer.startJob("Print Image")) {
167167
ImageData imageData = snapshotImage.getImageData();

examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet367.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ public int getGcStyle() {
185185

186186
createSeparator(shell);
187187

188-
new Label (shell, SWT.NONE).setText ("5. 50x50 box\n(Display#getDPI(): " + display.getDPI().x + ")");
188+
new Label (shell, SWT.NONE).setText ("5. 50x50 box");
189189
Label box= new Label (shell, SWT.NONE);
190190
box.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_DARK_SHADOW));
191191
box.setLayoutData (new GridData (50, 50));

tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug507020_WaylandIconsDoubleScaled.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static void main (String [] args) {
4242
gridData.horizontalSpan = 2;
4343
canvas.setLayoutData (gridData);
4444

45-
new Label (shell, SWT.NONE).setText ("5. 50x50 box\n(Display#getDPI(): " + display.getDPI().x + ")");
45+
new Label(shell, SWT.NONE).setText("5. 50x50 box");
4646
Label box= new Label (shell, SWT.NONE);
4747
box.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_DARK_SHADOW));
4848
box.setLayoutData (new GridData (50, 50));

tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_Display.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1573,6 +1573,7 @@ public void test_wake() {
15731573
/* custom */
15741574
boolean disposeExecRan;
15751575

1576+
@SuppressWarnings("deprecation")
15761577
@Test
15771578
public void test_getDPI() {
15781579
Display display = new Display();

0 commit comments

Comments
 (0)