Skip to content

Commit a4c7fee

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 1eabfa3 commit a4c7fee

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
@@ -208,6 +214,13 @@ public static float getScalingFactor(int zoom) {
208214
}
209215

210216

217+
public static double getScalingFactorD(int targetZoom, int currentZoom) {
218+
if (targetZoom <= 0) {
219+
targetZoom = deviceZoom;
220+
}
221+
return targetZoom / (double) currentZoom;
222+
}
223+
211224
public static float getScalingFactor(int targetZoom, int currentZoom) {
212225
if (targetZoom <= 0) {
213226
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)