From a4c7feebc1d44a901a479effd57c8aa077d33c44 Mon Sep 17 00:00:00 2001 From: Stephan Wahlbrink Date: Wed, 10 Sep 2025 23:27:59 +0200 Subject: [PATCH] [Win32] Return unrounded value in FontMetrics.getAverageCharacterWidth The value for getAverageCharacterWidth was rounded, even though its method signature returns a double. The unrounded value provides a better estimation of the average character width for non-integer zoom factors. Fixes: https://github.com/eclipse-platform/eclipse.platform.swt/issues/2461 --- .../common/org/eclipse/swt/internal/DPIUtil.java | 13 +++++++++++++ .../win32/org/eclipse/swt/graphics/FontMetrics.java | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) 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 bc55a3aac1a..eff027fa770 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 @@ -125,6 +125,12 @@ public static float pixelToPoint(float size, int zoom) { return (size / scaleFactor); } +public static double pixelToPoint(double size, int zoom) { + if (zoom == 100 || size == SWT.DEFAULT) return size; + double scaleFactor = getScalingFactorD (zoom, 100); + return (size / scaleFactor); +} + /** * Auto-scale image with ImageData @@ -208,6 +214,13 @@ public static float getScalingFactor(int zoom) { } +public static double getScalingFactorD(int targetZoom, int currentZoom) { + if (targetZoom <= 0) { + targetZoom = deviceZoom; + } + return targetZoom / (double) currentZoom; +} + public static float getScalingFactor(int targetZoom, int currentZoom) { if (targetZoom <= 0) { targetZoom = deviceZoom; diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/FontMetrics.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/FontMetrics.java index ae6298be4ce..5eeb08b765a 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/FontMetrics.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/FontMetrics.java @@ -108,7 +108,7 @@ public int getAscent() { * @since 3.107 */ public double getAverageCharacterWidth() { - return getAverageCharWidth(); + return DPIUtil.pixelToPoint((double)handle.tmAveCharWidth, getZoom()); } /**