From 63049a16d73b914266cc99a9930c94a2632f1ca5 Mon Sep 17 00:00:00 2001 From: arunjose696 Date: Tue, 5 Aug 2025 11:17:40 +0200 Subject: [PATCH 1/2] Revert Use font zoom in stringExtent and textExtent Fixes #2361 --- .../Eclipse SWT/win32/org/eclipse/swt/graphics/GC.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/GC.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/GC.java index 4b0c4785994..cd4f197b702 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/GC.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/GC.java @@ -5725,7 +5725,7 @@ void apply() { */ public Point stringExtent (String string) { if (string == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); - return Win32DPIUtils.pixelToPoint(drawable, stringExtentInPixels(string), data.font.zoom); + return Win32DPIUtils.pixelToPoint(drawable, stringExtentInPixels(string), getZoom()); } Point stringExtentInPixels (String string) { @@ -5805,7 +5805,7 @@ public Point textExtent (String string) { * */ public Point textExtent (String string, int flags) { - return Win32DPIUtils.pixelToPoint(drawable, textExtentInPixels(string, flags), data.font.zoom); + return Win32DPIUtils.pixelToPoint(drawable, textExtentInPixels(string, flags), getZoom()); } Point textExtentInPixels(String string, int flags) { From 518533c5b8bcee8585e981530874bf955df53558 Mon Sep 17 00:00:00 2001 From: arunjose696 Date: Tue, 5 Aug 2025 14:10:18 +0200 Subject: [PATCH 2/2] Modify gc.data.nativeDeviceZoom to use autoscaled zoom when swt.autoScale is fixed This change ensures that images and fonts share the same zoom level when a fixed autoscale value is used. Previously, when different GCs were created for images and widgets, their native zoom levels could differ, causing inconsistencies. For example, in LineNumberRuler, one GC is created from an image and another from a widget for text measurement. The differing native zooms led to incorrect text width calculations when applied across GCs. This update aligns fontZoom to the autoscaled zoom value when autoScale value is fixed Fixes #2311 --- .../swt/graphics/TextLayoutWin32Tests.java | 2 +- .../org/eclipse/swt/internal/DPIUtil.java | 47 +++++++++++++++---- .../win32/org/eclipse/swt/graphics/GC.java | 22 +++++---- .../org/eclipse/swt/graphics/TextLayout.java | 24 +++++----- .../org/eclipse/swt/widgets/Control.java | 2 +- 5 files changed, 64 insertions(+), 33 deletions(-) diff --git a/bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/graphics/TextLayoutWin32Tests.java b/bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/graphics/TextLayoutWin32Tests.java index 33957562d0a..e2af938db68 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/graphics/TextLayoutWin32Tests.java +++ b/bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/graphics/TextLayoutWin32Tests.java @@ -66,7 +66,7 @@ public void testCalculateGetBoundsWithVerticalIndent() { scaledLayout.setText(text); Rectangle scaledBounds = scaledLayout.getBounds(); - assertNotEquals(layout.nativeZoom, scaledLayout.nativeZoom, "The native zoom for the TextLayouts must differ"); + assertNotEquals(layout.fontZoom, scaledLayout.fontZoom, "The font zooms for the TextLayouts must differ"); assertEquals(unscaledBounds.height, scaledBounds.height, 1, "The public API for getBounds with vertical indent > 0 should give a similar result for any zoom level"); } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/DPIUtil.java b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/DPIUtil.java index 6dce4cff809..2c5a833ab29 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/DPIUtil.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/DPIUtil.java @@ -57,8 +57,7 @@ public static Optional forString(String s) { } private static final AutoScaleMethod AUTO_SCALE_METHOD_SETTING; private static AutoScaleMethod autoScaleMethod; - - private static String autoScaleValue; + private static AutoScaleConfig autoScaleConfig; /** * System property that controls the autoScale functionality. @@ -97,21 +96,41 @@ public static Optional forString(String s) { private static final String SWT_AUTOSCALE_METHOD = "swt.autoScale.method"; static { - autoScaleValue = System.getProperty (SWT_AUTOSCALE); - + setAutoScaleValue(System.getProperty (SWT_AUTOSCALE)); String value = System.getProperty (SWT_AUTOSCALE_METHOD); AUTO_SCALE_METHOD_SETTING = AutoScaleMethod.forString(value).orElse(AutoScaleMethod.AUTO); autoScaleMethod = AUTO_SCALE_METHOD_SETTING != AutoScaleMethod.AUTO ? AUTO_SCALE_METHOD_SETTING : AutoScaleMethod.NEAREST; } static String getAutoScaleValue() { - return autoScaleValue; + return autoScaleConfig.value; } static void setAutoScaleValue(String autoScaleValueArg) { - autoScaleValue = autoScaleValueArg; + autoScaleConfig = new AutoScaleConfig(autoScaleValueArg); } + private static final class AutoScaleConfig { + private final String value; + private final boolean useAutoScaledFontZoom; + + AutoScaleConfig(String value) { + this.value = value; + useAutoScaledFontZoom = isIntegerAutoScale(value); + } + + private static boolean isIntegerAutoScale(String value) { + if (value == null) + return false; + try { + Integer.parseInt(value); + return true; + } catch (NumberFormatException e) { + return false; + } + } + } + public static int pixelToPoint(int size, int zoom) { if (zoom == 100 || size == SWT.DEFAULT) return size; float scaleFactor = getScalingFactor (zoom); @@ -343,7 +362,15 @@ static void setUseSmoothScalingByDefaultProvider(UseSmoothScalingProvider provid } public static int getZoomForAutoscaleProperty (int nativeDeviceZoom) { - return getZoomForAutoscaleProperty(nativeDeviceZoom, autoScaleValue); + return getZoomForAutoscaleProperty(nativeDeviceZoom, autoScaleConfig.value); +} + +public static int getFontZoomForAutoscaleProperty(int nativeDeviceZoom) { + if (autoScaleConfig.useAutoScaledFontZoom ) { + return getZoomForAutoscaleProperty(nativeDeviceZoom); + } else { + return nativeDeviceZoom; + } } private static int getZoomForAutoscaleProperty (int nativeDeviceZoom, String autoScaleValue) { @@ -376,13 +403,13 @@ private static int getZoomForAutoscaleProperty (int nativeDeviceZoom, String aut } public static void runWithAutoScaleValue(String autoScaleValue, Runnable runnable) { - String initialAutoScaleValue = DPIUtil.autoScaleValue; - DPIUtil.autoScaleValue = autoScaleValue; + String initialAutoScaleValue = autoScaleConfig.value; + setAutoScaleValue(autoScaleValue); DPIUtil.deviceZoom = getZoomForAutoscaleProperty(nativeDeviceZoom); try { runnable.run(); } finally { - DPIUtil.autoScaleValue = initialAutoScaleValue; + setAutoScaleValue(initialAutoScaleValue); DPIUtil.deviceZoom = getZoomForAutoscaleProperty(nativeDeviceZoom); } } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/GC.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/GC.java index cd4f197b702..57f21dc7b7a 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/GC.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/GC.java @@ -342,7 +342,7 @@ void checkGC(int mask) { } } if ((state & FONT) != 0) { - long fontHandle = SWTFontProvider.getFontHandle(data.font, data.nativeZoom); + long fontHandle = SWTFontProvider.getFontHandle(data.font, getFontZoom()); OS.SelectObject(handle, fontHandle); long[] hFont = new long[1]; long gdipFont = createGdipFont(handle, fontHandle, gdipGraphics, device.fontCollection, null, hFont); @@ -463,7 +463,7 @@ void checkGC(int mask) { OS.SetTextColor(handle, data.foreground); } if ((state & FONT) != 0) { - long fontHandle = SWTFontProvider.getFontHandle(data.font, data.nativeZoom); + long fontHandle = SWTFontProvider.getFontHandle(data.font, getFontZoom()); OS.SelectObject(handle, fontHandle); } } @@ -2712,7 +2712,7 @@ void drawText(long gdipGraphics, String string, int x, int y, int flags, Point s char[] chars = string.toCharArray(); long hdc = Gdip.Graphics_GetHDC(gdipGraphics); long hFont = data.hGDIFont; - if (hFont == 0 && data.font != null) hFont = SWTFontProvider.getFontHandle(data.font, data.nativeZoom); + if (hFont == 0 && data.font != null) hFont = SWTFontProvider.getFontHandle(data.font, getFontZoom()); long oldFont = 0; if (hFont != 0) oldFont = OS.SelectObject(hdc, hFont); TEXTMETRIC lptm = new TEXTMETRIC(); @@ -2802,7 +2802,7 @@ private RectF drawText(long gdipGraphics, char[] buffer, int start, int length, } long hdc = Gdip.Graphics_GetHDC(gdipGraphics); long hFont = data.hGDIFont; - if (hFont == 0 && data.font != null) hFont = SWTFontProvider.getFontHandle(data.font, data.nativeZoom); + if (hFont == 0 && data.font != null) hFont = SWTFontProvider.getFontHandle(data.font, getFontZoom()); long oldFont = 0; if (hFont != 0) oldFont = OS.SelectObject(hdc, hFont); if (start != 0) { @@ -3945,7 +3945,7 @@ public FontMetrics getFontMetrics() { checkGC(FONT); TEXTMETRIC lptm = new TEXTMETRIC(); OS.GetTextMetrics(handle, lptm); - return FontMetrics.win32_new(lptm, data.nativeZoom); + return FontMetrics.win32_new(lptm, getFontZoom()); } /** @@ -4378,9 +4378,9 @@ private void init(Drawable drawable, GCData data, long hDC) { } if (data.font != null) { data.state &= ~FONT; - data.font = Font.win32_new(data.font, data.nativeZoom); + data.font = Font.win32_new(data.font, DPIUtil.getFontZoomForAutoscaleProperty(data.nativeZoom)); } else { - data.font = SWTFontProvider.getFont(device, OS.GetCurrentObject(hDC, OS.OBJ_FONT), data.nativeZoom); + data.font = SWTFontProvider.getFont(device, OS.GetCurrentObject(hDC, OS.OBJ_FONT), DPIUtil.getFontZoomForAutoscaleProperty(data.nativeZoom)); } Image image = data.image; if (image != null) { @@ -5015,12 +5015,12 @@ private class SetFontOperation extends Operation { private final Font font; SetFontOperation(Font font) { - this.font = font != null ? SWTFontProvider.getFont(font.getDevice(), font.getFontData()[0], data.nativeZoom) : null; + this.font = font != null ? SWTFontProvider.getFont(font.getDevice(), font.getFontData()[0], getFontZoom()) : null; } @Override void apply() { - data.font = font != null ? SWTFontProvider.getFont(font.getDevice(), font.getFontData()[0], data.nativeZoom) : SWTFontProvider.getSystemFont(device, data.nativeZoom); + data.font = font != null ? SWTFontProvider.getFont(font.getDevice(), font.getFontData()[0], getFontZoom()) : SWTFontProvider.getSystemFont(device, getFontZoom()); data.state &= ~FONT; } } @@ -5942,6 +5942,10 @@ int getZoom() { return DPIUtil.getZoomForAutoscaleProperty(data.nativeZoom); } +int getFontZoom() { + return DPIUtil.getFontZoomForAutoscaleProperty(data.nativeZoom); +} + private void storeAndApplyOperationForExistingHandle(Operation operation) { operations.add(operation); operation.apply(); diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/TextLayout.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/TextLayout.java index a01a5f881d9..706e1c3e28b 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/TextLayout.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/TextLayout.java @@ -67,7 +67,7 @@ public final class TextLayout extends Resource { private MetricsAdapter metricsAdapter = new MetricsAdapter(); - int nativeZoom = DPIUtil.getNativeDeviceZoom(); + int fontZoom = DPIUtil.getFontZoomForAutoscaleProperty(DPIUtil.getNativeDeviceZoom()); static final char LTR_MARK = '\u200E', RTL_MARK = '\u200F'; static final int SCRIPT_VISATTR_SIZEOF = 2; @@ -363,9 +363,9 @@ void checkLayout () { * Break paragraphs into lines, wraps the text, and initialize caches. */ void computeRuns (GC gc) { - int newNativeZoom = getNativeZoom(gc); - if (nativeZoom != newNativeZoom) { - nativeZoom = newNativeZoom; + int newFontZoom = getFontZoom(gc); + if (fontZoom != newFontZoom) { + fontZoom = newFontZoom; freeRuns(); } if (runs != null) return; @@ -768,19 +768,19 @@ public void draw (GC gc, int x, int y, int selectionStart, int selectionEnd, Col drawInPixels(gc, x, y, selectionStart, selectionEnd, selectionForeground, selectionBackground, flags); } -private int getNativeZoom(GC gc) { +private int getFontZoom(GC gc) { if (gc != null) { - return gc.data.nativeZoom; + return DPIUtil.getFontZoomForAutoscaleProperty(gc.data.nativeZoom); } - return nativeZoom; + return fontZoom; } private int getZoom(GC gc){ - return DPIUtil.getZoomForAutoscaleProperty(getNativeZoom(gc)); + return DPIUtil.getZoomForAutoscaleProperty(getFontZoom(gc)); } private int getZoom() { - return DPIUtil.getZoomForAutoscaleProperty(nativeZoom); + return DPIUtil.getZoomForAutoscaleProperty(fontZoom); } void drawInPixels (GC gc, int xInPoints, int yInPoints) { @@ -1970,7 +1970,7 @@ public boolean getJustify () { long getItemFont (StyleItem item, GC gc) { if (item.fallbackFont != 0) return item.fallbackFont; - final int zoom = getNativeZoom(gc); + final int zoom = getFontZoom(gc); if (item.style != null && item.style.font != null) { return SWTFontProvider.getFontHandle(item.style.font, zoom); } @@ -2157,7 +2157,7 @@ public FontMetrics getLineMetrics (int lineIndex) { lptm.tmHeight = Win32DPIUtils.pointToPixel(this.device, ascentInPoints + descentInPoints, zoom); lptm.tmInternalLeading = Win32DPIUtils.pointToPixel(this.device, leadingInPoints, zoom); lptm.tmAveCharWidth = 0; - return FontMetrics.win32_new(lptm, nativeZoom); + return FontMetrics.win32_new(lptm, fontZoom); } /** @@ -3258,7 +3258,7 @@ public void setFont (Font font) { Font oldFont = this.font; if (oldFont == font) return; this.font = font; - this.nativeZoom = this.font == null ? nativeZoom : this.font.zoom; + this.fontZoom = this.font == null ? fontZoom : this.font.zoom; if (oldFont != null && oldFont.equals(font)) return; freeRuns(); } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Control.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Control.java index be5a77eb33e..4f6f988c59a 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Control.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Control.java @@ -1763,7 +1763,7 @@ public long internal_new_GC (GCData data) { if (font != null) { data.font = font; } else { - data.font = SWTFontProvider.getFont(display, OS.SendMessage (hwnd, OS.WM_GETFONT, 0, 0), data.nativeZoom); + data.font = SWTFontProvider.getFont(display, OS.SendMessage (hwnd, OS.WM_GETFONT, 0, 0), DPIUtil.getFontZoomForAutoscaleProperty(data.nativeZoom)); } data.uiState = (int)OS.SendMessage (hwnd, OS.WM_QUERYUISTATE, 0, 0); }