Skip to content

Commit 5da0ca1

Browse files
committed
[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: #2461
1 parent c6a29a9 commit 5da0ca1

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/DPIUtil.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@ public static float pixelToPoint(float size, int zoom) {
125125
return (size / scaleFactor);
126126
}
127127

128+
public static double pixelToPoint(double size, int zoom) {
129+
if (zoom == 100 || size == SWT.DEFAULT) return size;
130+
double scaleFactor = getScalingFactorD (zoom, 100);
131+
return (size / scaleFactor);
132+
}
133+
128134

129135
/**
130136
* Auto-scale image with ImageData
@@ -194,6 +200,13 @@ public static float getScalingFactor(int zoom) {
194200
}
195201

196202

203+
public static double getScalingFactorD(int targetZoom, int currentZoom) {
204+
if (targetZoom <= 0) {
205+
targetZoom = deviceZoom;
206+
}
207+
return targetZoom / (double) currentZoom;
208+
}
209+
197210
public static float getScalingFactor(int targetZoom, int currentZoom) {
198211
if (targetZoom <= 0) {
199212
targetZoom = deviceZoom;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public int getAscent() {
108108
* @since 3.107
109109
*/
110110
public double getAverageCharacterWidth() {
111-
return getAverageCharWidth();
111+
return DPIUtil.pixelToPoint((double)handle.tmAveCharWidth, getZoom());
112112
}
113113

114114
/**

0 commit comments

Comments
 (0)