From 2715deac1d9245d146ea9c0c42139167dc92eb37 Mon Sep 17 00:00:00 2001 From: Shahzaib Ibrahim Date: Wed, 10 Dec 2025 14:37:58 +0100 Subject: [PATCH] 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. --- .../cocoa/org/eclipse/swt/graphics/Device.java | 16 ++++++++++++++-- .../gtk/org/eclipse/swt/graphics/Device.java | 17 ++++++++++++++--- .../win32/org/eclipse/swt/graphics/Device.java | 11 +++++++++++ 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/Device.java b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/Device.java index ef30e780765..36a6e040824 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/Device.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/Device.java @@ -391,7 +391,18 @@ public int getDepth () { * @exception SWTException + * + * @deprecated

This method returns a single global DPI value + * that does not reflect per-monitor DPI settings on modern operating systems. + * In environments with different scaling factors across monitors, it may provide + * a misleading or meaningless result, as it does not correspond to the actual DPI + * of any specific monitor.

+ * + *

Note: While deprecated for general {@code Device} instances like {@code Display}, + * this method may still be validly used when called on a {@code Printer} instance, + * where a single global DPI value is meaningful and expected.

*/ +@Deprecated public Point getDPI () { checkDevice (); return getScreenDPI(); @@ -611,8 +622,9 @@ protected void init () { /* Initialize the system font slot */ boolean smallFonts = System.getProperty("org.eclipse.swt.internal.carbon.smallFonts") != null; double systemFontSize = smallFonts ? NSFont.smallSystemFontSize() : NSFont.systemFontSize(); - Point dpi = this.dpi = getDPI(), screenDPI = getScreenDPI(); - NSFont font = NSFont.systemFontOfSize(systemFontSize * dpi.y / screenDPI.y); + final int DOTS_PER_INCH = 72; + Point dpi = this.dpi = getDPI(); + NSFont font = NSFont.systemFontOfSize(systemFontSize * dpi.y / DOTS_PER_INCH); font.retain(); systemFont = Font.cocoa_new(this, font); } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/Device.java b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/Device.java index d329fb6670f..48c9d9bab5f 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/Device.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/Device.java @@ -455,7 +455,18 @@ public int getDepth () { * @exception SWTException + * + * @deprecated

This method returns a single global DPI value + * that does not reflect per-monitor DPI settings on modern operating systems. + * In environments with different scaling factors across monitors, it may provide + * a misleading or meaningless result, as it does not correspond to the actual DPI + * of any specific monitor.

+ * + *

Note: While deprecated for general {@code Device} instances like {@code Display}, + * this method may still be validly used when called on a {@code Printer} instance, + * where a single global DPI value is meaningful and expected.

*/ +@Deprecated public Point getDPI () { checkDevice (); return getScreenDPI(); @@ -641,6 +652,7 @@ public boolean getWarnings () { * @see #create */ protected void init () { + final int DOTS_PER_INCH = 96; if (debug) { if (xDisplay != 0) { /* Create the warning and error callbacks */ @@ -723,10 +735,9 @@ protected void init () { } } defaultFont = OS.pango_font_description_copy (defaultFont); - Point dpi = getDPI(), screenDPI = getScreenDPI(); - if (dpi.y != screenDPI.y) { + if (this.dpi.y != DOTS_PER_INCH) { int size = OS.pango_font_description_get_size(defaultFont); - OS.pango_font_description_set_size(defaultFont, size * dpi.y / screenDPI.y); + OS.pango_font_description_set_size(defaultFont, size * this.dpi.y / DOTS_PER_INCH); } systemFont = Font.gtk_new (this, defaultFont); diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Device.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Device.java index 04c7c51b1c4..1a3783448d3 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Device.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Device.java @@ -519,7 +519,18 @@ public int getDepth () { * @exception SWTException + * + * @deprecated

This method returns a single global DPI value + * that does not reflect per-monitor DPI settings on modern operating systems. + * In environments with different scaling factors across monitors, it may provide + * a misleading or meaningless result, as it does not correspond to the actual DPI + * of any specific monitor.

+ * + *

Note: While deprecated for general {@code Device} instances like {@code Display}, + * this method may still be validly used when called on a {@code Printer} instance, + * where a single global DPI value is meaningful and expected.

*/ +@Deprecated public Point getDPI () { checkDevice (); long hDC = internal_new_GC (null);