From 785dba0fc200e23f46482a3d4cbca4732780f157 Mon Sep 17 00:00:00 2001 From: "Klare, Heiko" Date: Fri, 27 Feb 2026 11:59:41 +0100 Subject: [PATCH] [Win32] Ensure GC.textExtent/stringExtent returns large enough results The GC.textExtent()/stringExtent() operations return values in logical SWT points. On Windows, this involves a conversion from the actual pixel values according to the current zoom factor. This conversion includes a rounding to integer values which can lead to a value that is slightly smaller than the actual pixel value. The expectation of consumers is, however, that the result effectively represents the size of a bounding box, i.e., that the whole string fully fits into a rectangle of the returned size. To ensure that the results of GC.textExtent()/stringExtent() are always large enough so that the text fits into an accordingly sized rectangle on every zoom, this change adapts the performed rounding operations to always round up the values. This makes the behavior consistent to rounding behavior of the Control.computeSize() methods with a similar semantics. Contributes to https://github.com/eclipse-platform/eclipse.platform/issues/2295 --- .../Eclipse SWT/win32/org/eclipse/swt/graphics/GC.java | 6 +++--- .../win32/org/eclipse/swt/internal/Win32DPIUtils.java | 5 +++++ 2 files changed, 8 insertions(+), 3 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 59d1cd0de9f..0fdc863a15e 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 @@ -5877,7 +5877,7 @@ void apply() { */ public Point stringExtent (String string) { if (string == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); - return Win32DPIUtils.pixelToPointAsSize(drawable, stringExtentInPixels(string), getZoom()); + return Win32DPIUtils.pixelToPointAsSufficientlyLargeSize(drawable, stringExtentInPixels(string), getZoom()); } Point stringExtentInPixels (String string) { @@ -5922,7 +5922,7 @@ Point stringExtentInPixels (String string) { * */ public Point textExtent (String string) { - return Win32DPIUtils.pixelToPointAsSize(drawable, textExtentInPixels(string, SWT.DRAW_DELIMITER | SWT.DRAW_TAB), getZoom()); + return textExtent(string, SWT.DRAW_DELIMITER | SWT.DRAW_TAB); } /** @@ -5957,7 +5957,7 @@ public Point textExtent (String string) { * */ public Point textExtent (String string, int flags) { - return Win32DPIUtils.pixelToPointAsSize(drawable, textExtentInPixels(string, flags), getZoom()); + return Win32DPIUtils.pixelToPointAsSufficientlyLargeSize(textExtentInPixels(string, flags), getZoom()); } Point textExtentInPixels(String string, int flags) { diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/Win32DPIUtils.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/Win32DPIUtils.java index 9263b5de6df..af869f40d0e 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/Win32DPIUtils.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/Win32DPIUtils.java @@ -137,6 +137,11 @@ public static Point pixelToPointAsSize(Drawable drawable, Point point, int zoom) return pixelToPointAsSize (point, zoom); } + public static Point pixelToPointAsSufficientlyLargeSize(Drawable drawable, Point point, int zoom) { + if (drawable != null && !drawable.isAutoScalable()) return point; + return pixelToPointAsSufficientlyLargeSize (point, zoom); + } + public static Point pixelToPointAsLocation(Drawable drawable, Point point, int zoom) { if (drawable != null && !drawable.isAutoScalable()) return point; return pixelToPointAsLocation (point, zoom);